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 const char *fix_units(const char *units) {
372 if(!units || !*units || !strcmp(units, "empty") || !strcmp(units, "null")) return "";
373 if(!strcmp(units, "percentage") || !strcmp(units, "percent") || !strcmp(units, "pcent")) return "%";
377 static inline const char *color_map(const char *color) {
379 // https://github.com/badges/shields/blob/master/colorscheme.json
380 if(!strcmp(color, "brightgreen")) return "#4c1";
381 else if(!strcmp(color, "green")) return "#97CA00";
382 else if(!strcmp(color, "yellow")) return "#dfb317";
383 else if(!strcmp(color, "yellowgreen")) return "#a4a61d";
384 else if(!strcmp(color, "orange")) return "#fe7d37";
385 else if(!strcmp(color, "red")) return "#e05d44";
386 else if(!strcmp(color, "blue")) return "#007ec6";
387 else if(!strcmp(color, "grey")) return "#555";
388 else if(!strcmp(color, "gray")) return "#555";
389 else if(!strcmp(color, "lightgrey")) return "#9f9f9f";
390 else if(!strcmp(color, "lightgray")) return "#9f9f9f";
394 static inline void calc_colorz(const char *color, char *final, size_t len, calculated_number value, int value_is_null) {
395 char color_buffer[256 + 1] = "";
396 char value_buffer[256 + 1] = "";
397 char comparison = '>';
400 // color<max|color>min|color:null...
402 const char *c = color;
404 char *dc = color_buffer, *dv = NULL;
405 size_t ci = 0, vi = 0;
409 while(*t && *t != '|') {
459 // prepare for next iteration
469 if(!*value_buffer || !strcmp(value_buffer, "null"))
473 calculated_number v = strtold(value_buffer, NULL);
475 if(comparison == '<' && value < v) break;
476 else if(comparison == '(' && value <= v) break;
477 else if(comparison == '>' && value > v) break;
478 else if(comparison == ')' && value >= v) break;
479 else if(comparison == '=' && value == v) break;
492 strncpyz(final, b, len);
496 #define VALUE_STRING_SIZE 100
499 #define LABEL_STRING_SIZE 200
502 #define COLOR_STRING_SIZE 100
504 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) {
505 char label_buffer[LABEL_STRING_SIZE + 1]
506 , value_color_buffer[COLOR_STRING_SIZE + 1]
507 , value_string[VALUE_STRING_SIZE + 1]
508 , label_escaped[LABEL_STRING_SIZE + 1]
509 , value_escaped[VALUE_STRING_SIZE + 1]
510 , label_color_escaped[COLOR_STRING_SIZE + 1]
511 , value_color_escaped[COLOR_STRING_SIZE + 1];
513 int label_width, value_width, total_width;
515 if(unlikely(!label_color || !*label_color))
516 label_color = "#555";
518 if(unlikely(!value_color || !*value_color))
519 value_color = (value_is_null)?"#999":"#4c1";
521 units = fix_units(units);
522 calc_colorz(value_color, value_color_buffer, COLOR_STRING_SIZE, value, value_is_null);
524 char *separator = "";
525 if(unlikely(isalnum(*units)))
528 if(unlikely(!strcmp(units, "seconds"))) {
529 size_t s = (size_t)value;
530 size_t d = s / 86400;
540 snprintfz(value_string, VALUE_STRING_SIZE, "%zu %s %02zu:%02zu:%02zu", d, (d == 1)?"day":"days", h, m, s);
542 snprintfz(value_string, VALUE_STRING_SIZE, "%02zu:%02zu:%02zu", h, m, s);
545 else if(unlikely(!strcmp(units, "minutes"))) {
546 size_t m = (size_t)value;
547 size_t d = m / (60 * 24);
554 snprintfz(value_string, VALUE_STRING_SIZE, "%zud %02zuh %02zum", d, h, m);
556 snprintfz(value_string, VALUE_STRING_SIZE, "%zuh %zum", h, m);
559 else if(unlikely(!strcmp(units, "hours"))) {
560 size_t h = (size_t)value;
565 snprintfz(value_string, VALUE_STRING_SIZE, "%zud %zuh", d, h);
567 snprintfz(value_string, VALUE_STRING_SIZE, "%zuh", h);
570 else if(unlikely(value_is_null))
571 strcpy(value_string, "-");
573 else if(precision < 0) {
574 int len, lstop = 0, trim_zeros = 1;
576 calculated_number abs = value;
577 if(isless(value, 0)) {
582 if(isgreaterequal(abs, 1000)) { len = snprintfz(value_string, VALUE_STRING_SIZE, "%0.0Lf", (long double)value); trim_zeros = 0; }
583 else if(isgreaterequal(abs, 100)) len = snprintfz(value_string, VALUE_STRING_SIZE, "%0.1Lf", (long double)value);
584 else if(isgreaterequal(abs, 1)) len = snprintfz(value_string, VALUE_STRING_SIZE, "%0.2Lf", (long double)value);
585 else if(isgreaterequal(abs, 0.1)) len = snprintfz(value_string, VALUE_STRING_SIZE, "%0.3Lf", (long double)value);
586 else len = snprintfz(value_string, VALUE_STRING_SIZE, "%0.4Lf", (long double)value);
588 if(unlikely(trim_zeros)) {
590 // remove trailing zeros from the decimal part
591 for(l = len - 1; l > lstop ; l--) {
592 if(likely(value_string[l] == '0')) {
593 value_string[l] = '\0';
597 else if(unlikely(value_string[l] == '.')) {
598 value_string[l] = '\0';
609 snprintfz(&value_string[len], VALUE_STRING_SIZE - len, "%s%s", separator, units);
612 if(precision > 50) precision = 50;
613 snprintfz(value_string, VALUE_STRING_SIZE, "%0.*Lf%s%s", precision, (long double)value, separator, units);
616 // we need to copy the label, since verdana11_width may write to it
617 strncpyz(label_buffer, label, LABEL_STRING_SIZE);
619 label_width = verdana11_width(label_buffer) + (BADGE_HORIZONTAL_PADDING * 2);
620 value_width = verdana11_width(value_string) + (BADGE_HORIZONTAL_PADDING * 2);
621 total_width = label_width + value_width;
623 escape_xmlz(label_escaped, label_buffer, LABEL_STRING_SIZE);
624 escape_xmlz(value_escaped, value_string, VALUE_STRING_SIZE);
625 escape_xmlz(label_color_escaped, color_map(label_color), COLOR_STRING_SIZE);
626 escape_xmlz(value_color_escaped, color_map(value_color_buffer), COLOR_STRING_SIZE);
628 wb->contenttype = CT_IMAGE_SVG_XML;
630 // svg template from:
631 // https://raw.githubusercontent.com/badges/shields/master/templates/flat-template.svg
633 "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"%d\" height=\"20\">"
634 "<linearGradient id=\"smooth\" x2=\"0\" y2=\"100%%\">"
635 "<stop offset=\"0\" stop-color=\"#bbb\" stop-opacity=\".1\"/>"
636 "<stop offset=\"1\" stop-opacity=\".1\"/>"
638 "<mask id=\"round\">"
639 "<rect width=\"%d\" height=\"20\" rx=\"3\" fill=\"#fff\"/>"
641 "<g mask=\"url(#round)\">"
642 "<rect width=\"%d\" height=\"20\" fill=\"%s\"/>"
643 "<rect x=\"%d\" width=\"%d\" height=\"20\" fill=\"%s\"/>"
644 "<rect width=\"%d\" height=\"20\" fill=\"url(#smooth)\"/>"
646 "<g fill=\"#fff\" text-anchor=\"middle\" font-family=\"DejaVu Sans,Verdana,Geneva,sans-serif\" font-size=\"11\">"
647 "<text x=\"%d\" y=\"15\" fill=\"#010101\" fill-opacity=\".3\">%s</text>"
648 "<text x=\"%d\" y=\"14\">%s</text>"
649 "<text x=\"%d\" y=\"15\" fill=\"#010101\" fill-opacity=\".3\">%s</text>"
650 "<text x=\"%d\" y=\"14\">%s</text>"
653 total_width, total_width,
654 label_width, label_color_escaped,
655 label_width, value_width, value_color_escaped,
657 label_width / 2, label_escaped,
658 label_width / 2, label_escaped,
659 label_width + value_width / 2 -1, value_escaped,
660 label_width + value_width / 2 -1, value_escaped);