3 #define BADGE_HORIZONTAL_PADDING 4
4 #define VERDANA_KERNING 0.5
7 * verdana11_widths[] has been generated with this method:
8 * https://github.com/badges/shields/blob/master/measure-text.js
11 double verdana11_widths[256] = {
44 [32] = 3.8671874999999996, //
45 [33] = 4.3291015625, // !
46 [34] = 5.048828125, // "
47 [35] = 9.001953125, // #
48 [36] = 6.9931640625, // $
49 [37] = 11.837890625, // %
50 [38] = 7.992187499999999, // &
51 [39] = 2.9541015625, // '
52 [40] = 4.9951171875, // (
53 [41] = 4.9951171875, // )
54 [42] = 6.9931640625, // *
55 [43] = 9.001953125, // +
56 [44] = 4.00146484375, // ,
57 [45] = 4.9951171875, // -
58 [46] = 4.00146484375, // .
59 [47] = 4.9951171875, // /
60 [48] = 6.9931640625, // 0
61 [49] = 6.9931640625, // 1
62 [50] = 6.9931640625, // 2
63 [51] = 6.9931640625, // 3
64 [52] = 6.9931640625, // 4
65 [53] = 6.9931640625, // 5
66 [54] = 6.9931640625, // 6
67 [55] = 6.9931640625, // 7
68 [56] = 6.9931640625, // 8
69 [57] = 6.9931640625, // 9
70 [58] = 4.9951171875, // :
71 [59] = 4.9951171875, // ;
72 [60] = 9.001953125, // <
73 [61] = 9.001953125, // =
74 [62] = 9.001953125, // >
75 [63] = 5.99951171875, // ?
77 [65] = 7.51953125, // A
78 [66] = 7.541015625, // B
79 [67] = 7.680664062499999, // C
80 [68] = 8.4755859375, // D
81 [69] = 6.95556640625, // E
82 [70] = 6.32177734375, // F
83 [71] = 8.529296875, // G
84 [72] = 8.26611328125, // H
85 [73] = 4.6298828125, // I
86 [74] = 5.00048828125, // J
87 [75] = 7.62158203125, // K
88 [76] = 6.123046875, // L
89 [77] = 9.2705078125, // M
90 [78] = 8.228515625, // N
91 [79] = 8.658203125, // O
92 [80] = 6.63330078125, // P
93 [81] = 8.658203125, // Q
94 [82] = 7.6484375, // R
95 [83] = 7.51953125, // S
96 [84] = 6.7783203125, // T
97 [85] = 8.05126953125, // U
98 [86] = 7.51953125, // V
99 [87] = 10.87646484375, // W
100 [88] = 7.53564453125, // X
101 [89] = 6.767578125, // Y
102 [90] = 7.53564453125, // Z
103 [91] = 4.9951171875, // [
104 [92] = 4.9951171875, // backslash
105 [93] = 4.9951171875, // ]
106 [94] = 9.001953125, // ^
107 [95] = 6.9931640625, // _
108 [96] = 6.9931640625, // `
109 [97] = 6.6064453125, // a
110 [98] = 6.853515625, // b
111 [99] = 5.73095703125, // c
112 [100] = 6.853515625, // d
113 [101] = 6.552734375, // e
114 [102] = 3.8671874999999996, // f
115 [103] = 6.853515625, // g
116 [104] = 6.9609375, // h
117 [105] = 3.0185546875, // i
118 [106] = 3.78662109375, // j
119 [107] = 6.509765625, // k
120 [108] = 3.0185546875, // l
121 [109] = 10.69921875, // m
122 [110] = 6.9609375, // n
123 [111] = 6.67626953125, // o
124 [112] = 6.853515625, // p
125 [113] = 6.853515625, // q
126 [114] = 4.6943359375, // r
127 [115] = 5.73095703125, // s
128 [116] = 4.33447265625, // t
129 [117] = 6.9609375, // u
130 [118] = 6.509765625, // v
131 [119] = 9.001953125, // w
132 [120] = 6.509765625, // x
133 [121] = 6.509765625, // y
134 [122] = 5.779296875, // z
135 [123] = 6.982421875, // {
136 [124] = 4.9951171875, // |
137 [125] = 6.982421875, // }
138 [126] = 9.001953125, // ~
270 // find the width of the string using the verdana 11points font
271 // re-write the string in place, skiping zero-length characters
272 static inline int verdana11_width(char *s) {
277 double t = verdana11_widths[(unsigned char)*s];
281 w += t + VERDANA_KERNING;
290 w -= VERDANA_KERNING;
294 static inline size_t escape_xmlz(char *dst, const char *src, size_t len) {
297 // required escapes from
298 // https://github.com/badges/shields/blob/master/badge.js
309 strcpy(dst, "&");
339 strcpy(dst, """);
349 strcpy(dst, "'");
369 static inline const char *fix_units(const char *units) {
370 if(!units || !*units || !strcmp(units, "empty") || !strcmp(units, "null")) return "";
371 if(!strcmp(units, "percentage") || !strcmp(units, "percent") || !strcmp(units, "pcent")) return "%";
375 static inline const char *color_map(const char *color) {
377 // https://github.com/badges/shields/blob/master/colorscheme.json
378 if(!strcmp(color, "brightgreen")) return "#4c1";
379 else if(!strcmp(color, "green")) return "#97CA00";
380 else if(!strcmp(color, "yellow")) return "#dfb317";
381 else if(!strcmp(color, "yellowgreen")) return "#a4a61d";
382 else if(!strcmp(color, "orange")) return "#fe7d37";
383 else if(!strcmp(color, "red")) return "#e05d44";
384 else if(!strcmp(color, "blue")) return "#007ec6";
385 else if(!strcmp(color, "grey")) return "#555";
386 else if(!strcmp(color, "gray")) return "#555";
387 else if(!strcmp(color, "lightgrey")) return "#9f9f9f";
388 else if(!strcmp(color, "lightgray")) return "#9f9f9f";
392 static inline void calc_colorz(const char *color, char *final, size_t len, calculated_number value, int value_is_null) {
393 char color_buffer[256 + 1] = "";
394 char value_buffer[256 + 1] = "";
395 char comparison = '>';
398 // color<max|color>min|color:null...
400 const char *c = color;
402 char *dc = color_buffer, *dv = NULL;
403 size_t ci = 0, vi = 0;
407 while(*t && *t != '|') {
457 // prepare for next iteration
467 if(!*value_buffer || !strcmp(value_buffer, "null"))
471 calculated_number v = strtold(value_buffer, NULL);
473 if(comparison == '<' && value < v) break;
474 else if(comparison == '(' && value <= v) break;
475 else if(comparison == '>' && value > v) break;
476 else if(comparison == ')' && value >= v) break;
477 else if(comparison == '=' && value == v) break;
490 strncpyz(final, b, len);
493 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) {
494 char label_buffer[256 + 1], value_string[512 + 1], value_color_buffer[256 + 1];
495 char label_escaped[256 + 1], value_escaped[512 + 1], label_color_escaped[256 + 1], value_color_escaped[256 + 1];
496 int label_width, value_width, total_width;
498 if(!label_color || !*label_color) label_color = "#555";
499 if(!value_color || !*value_color) value_color = (value_is_null)?"#999":"#4c1";
501 units = fix_units(units);
502 calc_colorz(value_color, value_color_buffer, 256, value, value_is_null);
504 char *separator = "";
505 if(isalnum(*units)) separator = " ";
508 strcpy(value_string, "-");
509 else if(precision < 0) {
510 calculated_number abs = (value < (calculated_number)0)?-value:value;
511 if(abs > (calculated_number)1000.0) snprintfz(value_string, 512, "%0.0Lf%s%s", (long double)value, separator, units);
512 else if(abs > (calculated_number)100.0) snprintfz(value_string, 512, "%0.1Lf%s%s", (long double)value, separator, units);
513 else if(abs > (calculated_number)1.0) snprintfz(value_string, 512, "%0.2Lf%s%s", (long double)value, separator, units);
514 else if(abs > (calculated_number)0.1) snprintfz(value_string, 512, "%0.3Lf%s%s", (long double)value, separator, units);
515 else snprintfz(value_string, 512, "%0.4Lf%s%s", (long double)value, separator, units);
518 if(precision > 50) precision = 50;
519 snprintfz(value_string, 512, "%0.*Lf%s%s", precision, (long double)value, separator, units);
522 // we need to copy the label, since verdana11_width may write to it
523 strncpyz(label_buffer, label, 256);
525 label_width = verdana11_width(label_buffer) + (BADGE_HORIZONTAL_PADDING * 2);
526 value_width = verdana11_width(value_string) + (BADGE_HORIZONTAL_PADDING * 2);
527 total_width = label_width + value_width;
529 escape_xmlz(label_escaped, label_buffer, 256);
530 escape_xmlz(value_escaped, value_string, 256);
531 escape_xmlz(label_color_escaped, color_map(label_color), 256);
532 escape_xmlz(value_color_escaped, color_map(value_color_buffer), 256);
534 wb->contenttype = CT_IMAGE_SVG_XML;
536 // svg template from:
537 // https://raw.githubusercontent.com/badges/shields/master/templates/flat-template.svg
539 "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"%d\" height=\"20\">"
540 "<linearGradient id=\"smooth\" x2=\"0\" y2=\"100%%\">"
541 "<stop offset=\"0\" stop-color=\"#bbb\" stop-opacity=\".1\"/>"
542 "<stop offset=\"1\" stop-opacity=\".1\"/>"
544 "<mask id=\"round\">"
545 "<rect width=\"%d\" height=\"20\" rx=\"3\" fill=\"#fff\"/>"
547 "<g mask=\"url(#round)\">"
548 "<rect width=\"%d\" height=\"20\" fill=\"%s\"/>"
549 "<rect x=\"%d\" width=\"%d\" height=\"20\" fill=\"%s\"/>"
550 "<rect width=\"%d\" height=\"20\" fill=\"url(#smooth)\"/>"
552 "<g fill=\"#fff\" text-anchor=\"middle\" font-family=\"DejaVu Sans,Verdana,Geneva,sans-serif\" font-size=\"11\">"
553 "<text x=\"%d\" y=\"15\" fill=\"#010101\" fill-opacity=\".3\">%s</text>"
554 "<text x=\"%d\" y=\"14\">%s</text>"
555 "<text x=\"%d\" y=\"15\" fill=\"#010101\" fill-opacity=\".3\">%s</text>"
556 "<text x=\"%d\" y=\"14\">%s</text>"
559 total_width, total_width,
560 label_width, label_color_escaped,
561 label_width, value_width, value_color_escaped,
563 label_width / 2, label_escaped,
564 label_width / 2, label_escaped,
565 label_width + value_width / 2 -1, value_escaped,
566 label_width + value_width / 2 -1, value_escaped);