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(value_is_null))
529 strcpy(value_string, "-");
531 else if(precision < 0) {
532 size_t len, l, lstop = 0;
534 calculated_number abs = value;
535 if(isless(value, 0)) {
540 if(isgreaterequal(abs, 1000)) len = snprintfz(value_string, VALUE_STRING_SIZE, "%0.0Lf%s%s", (long double)value, separator, units);
541 else if(isgreaterequal(abs, 100)) len = snprintfz(value_string, VALUE_STRING_SIZE, "%0.1Lf%s%s", (long double)value, separator, units);
542 else if(isgreaterequal(abs, 1)) len = snprintfz(value_string, VALUE_STRING_SIZE, "%0.2Lf%s%s", (long double)value, separator, units);
543 else if(isgreaterequal(abs, 0.1)) len = snprintfz(value_string, VALUE_STRING_SIZE, "%0.3Lf%s%s", (long double)value, separator, units);
544 else len = snprintfz(value_string, VALUE_STRING_SIZE, "%0.4Lf%s%s", (long double)value, separator, units);
546 // remove trailing zeros
547 for(l = len - 1; l > lstop ; l--) {
548 if(likely(value_string[l] == '0'))
549 value_string[l] = '\0';
551 else if(unlikely(value_string[l] == '.')) {
552 value_string[l] = '\0';
561 if(precision > 50) precision = 50;
562 snprintfz(value_string, VALUE_STRING_SIZE, "%0.*Lf%s%s", precision, (long double)value, separator, units);
565 // we need to copy the label, since verdana11_width may write to it
566 strncpyz(label_buffer, label, LABEL_STRING_SIZE);
568 label_width = verdana11_width(label_buffer) + (BADGE_HORIZONTAL_PADDING * 2);
569 value_width = verdana11_width(value_string) + (BADGE_HORIZONTAL_PADDING * 2);
570 total_width = label_width + value_width;
572 escape_xmlz(label_escaped, label_buffer, LABEL_STRING_SIZE);
573 escape_xmlz(value_escaped, value_string, VALUE_STRING_SIZE);
574 escape_xmlz(label_color_escaped, color_map(label_color), COLOR_STRING_SIZE);
575 escape_xmlz(value_color_escaped, color_map(value_color_buffer), COLOR_STRING_SIZE);
577 wb->contenttype = CT_IMAGE_SVG_XML;
579 // svg template from:
580 // https://raw.githubusercontent.com/badges/shields/master/templates/flat-template.svg
582 "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"%d\" height=\"20\">"
583 "<linearGradient id=\"smooth\" x2=\"0\" y2=\"100%%\">"
584 "<stop offset=\"0\" stop-color=\"#bbb\" stop-opacity=\".1\"/>"
585 "<stop offset=\"1\" stop-opacity=\".1\"/>"
587 "<mask id=\"round\">"
588 "<rect width=\"%d\" height=\"20\" rx=\"3\" fill=\"#fff\"/>"
590 "<g mask=\"url(#round)\">"
591 "<rect width=\"%d\" height=\"20\" fill=\"%s\"/>"
592 "<rect x=\"%d\" width=\"%d\" height=\"20\" fill=\"%s\"/>"
593 "<rect width=\"%d\" height=\"20\" fill=\"url(#smooth)\"/>"
595 "<g fill=\"#fff\" text-anchor=\"middle\" font-family=\"DejaVu Sans,Verdana,Geneva,sans-serif\" font-size=\"11\">"
596 "<text x=\"%d\" y=\"15\" fill=\"#010101\" fill-opacity=\".3\">%s</text>"
597 "<text x=\"%d\" y=\"14\">%s</text>"
598 "<text x=\"%d\" y=\"15\" fill=\"#010101\" fill-opacity=\".3\">%s</text>"
599 "<text x=\"%d\" y=\"14\">%s</text>"
602 total_width, total_width,
603 label_width, label_color_escaped,
604 label_width, value_width, value_color_escaped,
606 label_width / 2, label_escaped,
607 label_width / 2, label_escaped,
608 label_width + value_width / 2 -1, value_escaped,
609 label_width + value_width / 2 -1, value_escaped);