3 #define BADGE_HORIZONTAL_PADDING 4
4 #define VERDANA_KERNING 0.2
5 #define VERDANA_PADDING 1.0
8 * verdana11_widths[] has been generated with this method:
9 * https://github.com/badges/shields/blob/master/measure-text.js
12 double verdana11_widths[256] = {
45 [32] = 3.8671874999999996, //
46 [33] = 4.3291015625, // !
47 [34] = 5.048828125, // "
48 [35] = 9.001953125, // #
49 [36] = 6.9931640625, // $
50 [37] = 11.837890625, // %
51 [38] = 7.992187499999999, // &
52 [39] = 2.9541015625, // '
53 [40] = 4.9951171875, // (
54 [41] = 4.9951171875, // )
55 [42] = 6.9931640625, // *
56 [43] = 9.001953125, // +
57 [44] = 4.00146484375, // ,
58 [45] = 4.9951171875, // -
59 [46] = 4.00146484375, // .
60 [47] = 4.9951171875, // /
61 [48] = 6.9931640625, // 0
62 [49] = 6.9931640625, // 1
63 [50] = 6.9931640625, // 2
64 [51] = 6.9931640625, // 3
65 [52] = 6.9931640625, // 4
66 [53] = 6.9931640625, // 5
67 [54] = 6.9931640625, // 6
68 [55] = 6.9931640625, // 7
69 [56] = 6.9931640625, // 8
70 [57] = 6.9931640625, // 9
71 [58] = 4.9951171875, // :
72 [59] = 4.9951171875, // ;
73 [60] = 9.001953125, // <
74 [61] = 9.001953125, // =
75 [62] = 9.001953125, // >
76 [63] = 5.99951171875, // ?
78 [65] = 7.51953125, // A
79 [66] = 7.541015625, // B
80 [67] = 7.680664062499999, // C
81 [68] = 8.4755859375, // D
82 [69] = 6.95556640625, // E
83 [70] = 6.32177734375, // F
84 [71] = 8.529296875, // G
85 [72] = 8.26611328125, // H
86 [73] = 4.6298828125, // I
87 [74] = 5.00048828125, // J
88 [75] = 7.62158203125, // K
89 [76] = 6.123046875, // L
90 [77] = 9.2705078125, // M
91 [78] = 8.228515625, // N
92 [79] = 8.658203125, // O
93 [80] = 6.63330078125, // P
94 [81] = 8.658203125, // Q
95 [82] = 7.6484375, // R
96 [83] = 7.51953125, // S
97 [84] = 6.7783203125, // T
98 [85] = 8.05126953125, // U
99 [86] = 7.51953125, // V
100 [87] = 10.87646484375, // W
101 [88] = 7.53564453125, // X
102 [89] = 6.767578125, // Y
103 [90] = 7.53564453125, // Z
104 [91] = 4.9951171875, // [
105 [92] = 4.9951171875, // backslash
106 [93] = 4.9951171875, // ]
107 [94] = 9.001953125, // ^
108 [95] = 6.9931640625, // _
109 [96] = 6.9931640625, // `
110 [97] = 6.6064453125, // a
111 [98] = 6.853515625, // b
112 [99] = 5.73095703125, // c
113 [100] = 6.853515625, // d
114 [101] = 6.552734375, // e
115 [102] = 3.8671874999999996, // f
116 [103] = 6.853515625, // g
117 [104] = 6.9609375, // h
118 [105] = 3.0185546875, // i
119 [106] = 3.78662109375, // j
120 [107] = 6.509765625, // k
121 [108] = 3.0185546875, // l
122 [109] = 10.69921875, // m
123 [110] = 6.9609375, // n
124 [111] = 6.67626953125, // o
125 [112] = 6.853515625, // p
126 [113] = 6.853515625, // q
127 [114] = 4.6943359375, // r
128 [115] = 5.73095703125, // s
129 [116] = 4.33447265625, // t
130 [117] = 6.9609375, // u
131 [118] = 6.509765625, // v
132 [119] = 9.001953125, // w
133 [120] = 6.509765625, // x
134 [121] = 6.509765625, // y
135 [122] = 5.779296875, // z
136 [123] = 6.982421875, // {
137 [124] = 4.9951171875, // |
138 [125] = 6.982421875, // }
139 [126] = 9.001953125, // ~
271 // find the width of the string using the verdana 11points font
272 // re-write the string in place, skiping zero-length characters
273 static inline int verdana11_width(char *s) {
278 double t = verdana11_widths[(unsigned char)*s];
282 w += t + VERDANA_KERNING;
291 w -= VERDANA_KERNING;
292 w += VERDANA_PADDING;
296 static inline size_t escape_xmlz(char *dst, const char *src, size_t len) {
299 // required escapes from
300 // https://github.com/badges/shields/blob/master/badge.js
311 strcpy(dst, "&");
341 strcpy(dst, """);
351 strcpy(dst, "'");
371 static inline int fix_value_and_units(char *value_string, size_t value_string_len, calculated_number value, const char **units_ptr, int value_is_null) {
374 hash_seconds_ago = 0,
376 hash_minutes_ago = 0,
389 if(unlikely(!hash_seconds)) {
390 hash_seconds = simple_hash("seconds");
391 hash_seconds_ago = simple_hash("seconds ago");
392 hash_minutes = simple_hash("minutes");
393 hash_minutes_ago = simple_hash("minutes ago");
394 hash_hours = simple_hash("hours");
395 hash_hours_ago = simple_hash("hours ago");
396 hash_onoff = simple_hash("on/off");
397 hash_updown = simple_hash("up/down");
398 hash_okerror = simple_hash("ok/error");
399 hash_okfailed = simple_hash("ok/failed");
400 hash_empty = simple_hash("empty");
401 hash_null = simple_hash("null");
402 hash_percentage = simple_hash("percentage");
403 hash_percent = simple_hash("percent");
404 hash_pcent = simple_hash("pcent");
407 const char *units = (units_ptr)?*units_ptr:NULL;
408 if(unlikely(!units)) units = "";
410 uint32_t hash_units = simple_hash(units);
412 if(unlikely((hash_units == hash_seconds && !strcmp(units, "seconds")) || (hash_units == hash_seconds_ago && !strcmp(units, "seconds ago")))) {
414 snprintfz(value_string, value_string_len, "%s", "now");
418 else if(isnan(value) || isinf(value)) {
419 snprintfz(value_string, value_string_len, "%s", "never");
424 const char *suffix = (hash_units == hash_seconds_ago)?" ago":"";
426 size_t s = (size_t)value;
427 size_t d = s / 86400;
437 snprintfz(value_string, value_string_len, "%zu %s %02zu:%02zu:%02zu%s", d, (d == 1)?"day":"days", h, m, s, suffix);
439 snprintfz(value_string, value_string_len, "%02zu:%02zu:%02zu%s", h, m, s, suffix);
444 else if(unlikely((hash_units == hash_minutes && !strcmp(units, "minutes")) || (hash_units == hash_minutes_ago && !strcmp(units, "minutes ago")))) {
446 snprintfz(value_string, value_string_len, "%s", "now");
450 else if(isnan(value) || isinf(value)) {
451 snprintfz(value_string, value_string_len, "%s", "never");
456 const char *suffix = (hash_units == hash_minutes_ago)?" ago":"";
458 size_t m = (size_t)value;
459 size_t d = m / (60 * 24);
466 snprintfz(value_string, value_string_len, "%zud %02zuh %02zum%s", d, h, m, suffix);
468 snprintfz(value_string, value_string_len, "%zuh %zum%s", h, m, suffix);
473 else if(unlikely((hash_units == hash_hours && !strcmp(units, "hours")) || (hash_units == hash_hours_ago && !strcmp(units, "hours ago")))) {
475 snprintfz(value_string, value_string_len, "%s", "now");
479 else if(isnan(value) || isinf(value)) {
480 snprintfz(value_string, value_string_len, "%s", "never");
485 const char *suffix = (hash_units == hash_hours_ago)?" ago":"";
487 size_t h = (size_t)value;
492 snprintfz(value_string, value_string_len, "%zud %zuh%s", d, h, suffix);
494 snprintfz(value_string, value_string_len, "%zuh%s", h, suffix);
499 else if(unlikely(hash_units == hash_onoff && !strcmp(units, "on/off"))) {
500 snprintfz(value_string, value_string_len, "%s", (value != 0.0)?"on":"off");
504 else if(unlikely(hash_units == hash_updown && !strcmp(units, "up/down"))) {
505 snprintfz(value_string, value_string_len, "%s", (value != 0.0)?"up":"down");
509 else if(unlikely(hash_units == hash_okerror && !strcmp(units, "ok/error"))) {
510 snprintfz(value_string, value_string_len, "%s", (value != 0.0)?"ok":"error");
514 else if(unlikely(hash_units == hash_okfailed && !strcmp(units, "ok/failed"))) {
515 snprintfz(value_string, value_string_len, "%s", (value != 0.0)?"ok":"failed");
519 else if(unlikely(hash_units == hash_empty && !strcmp(units, "empty")))
522 else if(unlikely(hash_units == hash_null && !strcmp(units, "null")))
525 else if(unlikely(hash_units == hash_percentage && !strcmp(units, "percentage")))
528 else if(unlikely(hash_units == hash_percent && !strcmp(units, "percent")))
531 else if(unlikely(hash_units == hash_pcent && !strcmp(units, "pcent")))
534 else if(unlikely(value_is_null)) {
535 strcpy(value_string, "-");
542 if(units_ptr) *units_ptr = units;
546 static inline const char *color_map(const char *color) {
548 // https://github.com/badges/shields/blob/master/colorscheme.json
549 if(!strcmp(color, "brightgreen")) return "#4c1";
550 else if(!strcmp(color, "green")) return "#97CA00";
551 else if(!strcmp(color, "yellow")) return "#dfb317";
552 else if(!strcmp(color, "yellowgreen")) return "#a4a61d";
553 else if(!strcmp(color, "orange")) return "#fe7d37";
554 else if(!strcmp(color, "red")) return "#e05d44";
555 else if(!strcmp(color, "blue")) return "#007ec6";
556 else if(!strcmp(color, "grey")) return "#555";
557 else if(!strcmp(color, "gray")) return "#555";
558 else if(!strcmp(color, "lightgrey")) return "#9f9f9f";
559 else if(!strcmp(color, "lightgray")) return "#9f9f9f";
563 static inline void calc_colorz(const char *color, char *final, size_t len, calculated_number value, int value_is_null) {
564 if(isnan(value) || isinf(value))
567 char color_buffer[256 + 1] = "";
568 char value_buffer[256 + 1] = "";
569 char comparison = '>';
572 // color<max|color>min|color:null...
574 const char *c = color;
576 char *dc = color_buffer, *dv = NULL;
577 size_t ci = 0, vi = 0;
581 while(*t && *t != '|') {
631 // prepare for next iteration
641 if(!*value_buffer || !strcmp(value_buffer, "null"))
645 calculated_number v = str2l(value_buffer);
647 if(comparison == '<' && value < v) break;
648 else if(comparison == '(' && value <= v) break;
649 else if(comparison == '>' && value > v) break;
650 else if(comparison == ')' && value >= v) break;
651 else if(comparison == '=' && value == v) break;
664 strncpyz(final, b, len);
668 #define VALUE_STRING_SIZE 100
671 #define LABEL_STRING_SIZE 200
674 #define COLOR_STRING_SIZE 100
676 void buffer_svg(BUFFER *wb, const char *label, calculated_number value, const char *units, const char *label_color, const char *value_color, int value_is_null, int precision) {
677 char label_buffer[LABEL_STRING_SIZE + 1]
678 , value_color_buffer[COLOR_STRING_SIZE + 1]
679 , value_string[VALUE_STRING_SIZE + 1]
680 , label_escaped[LABEL_STRING_SIZE + 1]
681 , value_escaped[VALUE_STRING_SIZE + 1]
682 , label_color_escaped[COLOR_STRING_SIZE + 1]
683 , value_color_escaped[COLOR_STRING_SIZE + 1];
685 int label_width, value_width, total_width;
687 if(unlikely(!label_color || !*label_color))
688 label_color = "#555";
690 if(unlikely(!value_color || !*value_color))
691 value_color = (value_is_null)?"#999":"#4c1";
693 calc_colorz(value_color, value_color_buffer, COLOR_STRING_SIZE, value, value_is_null);
695 if(!fix_value_and_units(value_string, VALUE_STRING_SIZE, value, &units, value_is_null)) {
696 // we have to print the value
698 if(isnan(value) || isinf(value))
701 char *separator = "";
702 if(unlikely(isalnum(*units)))
706 int len, lstop = 0, trim_zeros = 1;
708 calculated_number abs = value;
709 if(isless(value, 0)) {
714 if(isgreaterequal(abs, 1000)) {
715 len = snprintfz(value_string, VALUE_STRING_SIZE, "%0.0Lf", (long double) value);
718 else if(isgreaterequal(abs, 100)) len = snprintfz(value_string, VALUE_STRING_SIZE, "%0.1Lf", (long double) value);
719 else if(isgreaterequal(abs, 1)) len = snprintfz(value_string, VALUE_STRING_SIZE, "%0.2Lf", (long double) value);
720 else if(isgreaterequal(abs, 0.1)) len = snprintfz(value_string, VALUE_STRING_SIZE, "%0.3Lf", (long double) value);
721 else len = snprintfz(value_string, VALUE_STRING_SIZE, "%0.4Lf", (long double) value);
723 if(unlikely(trim_zeros)) {
725 // remove trailing zeros from the decimal part
726 for(l = len - 1; l > lstop; l--) {
727 if(likely(value_string[l] == '0')) {
728 value_string[l] = '\0';
732 else if(unlikely(value_string[l] == '.')) {
733 value_string[l] = '\0';
744 snprintfz(&value_string[len], VALUE_STRING_SIZE - len, "%s%s", separator, units);
747 if(precision > 50) precision = 50;
748 snprintfz(value_string, VALUE_STRING_SIZE, "%0.*Lf%s%s", precision, (long double) value, separator, units);
752 // we need to copy the label, since verdana11_width may write to it
753 strncpyz(label_buffer, label, LABEL_STRING_SIZE);
755 label_width = verdana11_width(label_buffer) + (BADGE_HORIZONTAL_PADDING * 2);
756 value_width = verdana11_width(value_string) + (BADGE_HORIZONTAL_PADDING * 2);
757 total_width = label_width + value_width;
759 escape_xmlz(label_escaped, label_buffer, LABEL_STRING_SIZE);
760 escape_xmlz(value_escaped, value_string, VALUE_STRING_SIZE);
761 escape_xmlz(label_color_escaped, color_map(label_color), COLOR_STRING_SIZE);
762 escape_xmlz(value_color_escaped, color_map(value_color_buffer), COLOR_STRING_SIZE);
764 wb->contenttype = CT_IMAGE_SVG_XML;
766 // svg template from:
767 // https://raw.githubusercontent.com/badges/shields/master/templates/flat-template.svg
769 "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"%d\" height=\"20\">"
770 "<linearGradient id=\"smooth\" x2=\"0\" y2=\"100%%\">"
771 "<stop offset=\"0\" stop-color=\"#bbb\" stop-opacity=\".1\"/>"
772 "<stop offset=\"1\" stop-opacity=\".1\"/>"
774 "<mask id=\"round\">"
775 "<rect width=\"%d\" height=\"20\" rx=\"3\" fill=\"#fff\"/>"
777 "<g mask=\"url(#round)\">"
778 "<rect width=\"%d\" height=\"20\" fill=\"%s\"/>"
779 "<rect x=\"%d\" width=\"%d\" height=\"20\" fill=\"%s\"/>"
780 "<rect width=\"%d\" height=\"20\" fill=\"url(#smooth)\"/>"
782 "<g fill=\"#fff\" text-anchor=\"middle\" font-family=\"DejaVu Sans,Verdana,Geneva,sans-serif\" font-size=\"11\">"
783 "<text x=\"%d\" y=\"15\" fill=\"#010101\" fill-opacity=\".3\">%s</text>"
784 "<text x=\"%d\" y=\"14\">%s</text>"
785 "<text x=\"%d\" y=\"15\" fill=\"#010101\" fill-opacity=\".3\">%s</text>"
786 "<text x=\"%d\" y=\"14\">%s</text>"
789 total_width, total_width,
790 label_width, label_color_escaped,
791 label_width, value_width, value_color_escaped,
793 label_width / 2, label_escaped,
794 label_width / 2, label_escaped,
795 label_width + value_width / 2 -1, value_escaped,
796 label_width + value_width / 2 -1, value_escaped);