]> arthur.barton.de Git - netdata.git/blob - src/web_buffer_svg.c
added fping alarms and various aesthetic improvements to alarms
[netdata.git] / src / web_buffer_svg.c
1 #include "common.h"
2
3 #define BADGE_HORIZONTAL_PADDING 4
4 #define VERDANA_KERNING 0.2
5 #define VERDANA_PADDING 1.0
6
7 /*
8  * verdana11_widths[] has been generated with this method:
9  * https://github.com/badges/shields/blob/master/measure-text.js
10 */
11
12 double verdana11_widths[256] = {
13     [0] = 0.0,
14     [1] = 0.0,
15     [2] = 0.0,
16     [3] = 0.0,
17     [4] = 0.0,
18     [5] = 0.0,
19     [6] = 0.0,
20     [7] = 0.0,
21     [8] = 0.0,
22     [9] = 0.0,
23     [10] = 0.0,
24     [11] = 0.0,
25     [12] = 0.0,
26     [13] = 0.0,
27     [14] = 0.0,
28     [15] = 0.0,
29     [16] = 0.0,
30     [17] = 0.0,
31     [18] = 0.0,
32     [19] = 0.0,
33     [20] = 0.0,
34     [21] = 0.0,
35     [22] = 0.0,
36     [23] = 0.0,
37     [24] = 0.0,
38     [25] = 0.0,
39     [26] = 0.0,
40     [27] = 0.0,
41     [28] = 0.0,
42     [29] = 0.0,
43     [30] = 0.0,
44     [31] = 0.0,
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, // ?
77     [64] = 11.0, // @
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, // ~
140     [127] = 0.0,
141     [128] = 0.0,
142     [129] = 0.0,
143     [130] = 0.0,
144     [131] = 0.0,
145     [132] = 0.0,
146     [133] = 0.0,
147     [134] = 0.0,
148     [135] = 0.0,
149     [136] = 0.0,
150     [137] = 0.0,
151     [138] = 0.0,
152     [139] = 0.0,
153     [140] = 0.0,
154     [141] = 0.0,
155     [142] = 0.0,
156     [143] = 0.0,
157     [144] = 0.0,
158     [145] = 0.0,
159     [146] = 0.0,
160     [147] = 0.0,
161     [148] = 0.0,
162     [149] = 0.0,
163     [150] = 0.0,
164     [151] = 0.0,
165     [152] = 0.0,
166     [153] = 0.0,
167     [154] = 0.0,
168     [155] = 0.0,
169     [156] = 0.0,
170     [157] = 0.0,
171     [158] = 0.0,
172     [159] = 0.0,
173     [160] = 0.0,
174     [161] = 0.0,
175     [162] = 0.0,
176     [163] = 0.0,
177     [164] = 0.0,
178     [165] = 0.0,
179     [166] = 0.0,
180     [167] = 0.0,
181     [168] = 0.0,
182     [169] = 0.0,
183     [170] = 0.0,
184     [171] = 0.0,
185     [172] = 0.0,
186     [173] = 0.0,
187     [174] = 0.0,
188     [175] = 0.0,
189     [176] = 0.0,
190     [177] = 0.0,
191     [178] = 0.0,
192     [179] = 0.0,
193     [180] = 0.0,
194     [181] = 0.0,
195     [182] = 0.0,
196     [183] = 0.0,
197     [184] = 0.0,
198     [185] = 0.0,
199     [186] = 0.0,
200     [187] = 0.0,
201     [188] = 0.0,
202     [189] = 0.0,
203     [190] = 0.0,
204     [191] = 0.0,
205     [192] = 0.0,
206     [193] = 0.0,
207     [194] = 0.0,
208     [195] = 0.0,
209     [196] = 0.0,
210     [197] = 0.0,
211     [198] = 0.0,
212     [199] = 0.0,
213     [200] = 0.0,
214     [201] = 0.0,
215     [202] = 0.0,
216     [203] = 0.0,
217     [204] = 0.0,
218     [205] = 0.0,
219     [206] = 0.0,
220     [207] = 0.0,
221     [208] = 0.0,
222     [209] = 0.0,
223     [210] = 0.0,
224     [211] = 0.0,
225     [212] = 0.0,
226     [213] = 0.0,
227     [214] = 0.0,
228     [215] = 0.0,
229     [216] = 0.0,
230     [217] = 0.0,
231     [218] = 0.0,
232     [219] = 0.0,
233     [220] = 0.0,
234     [221] = 0.0,
235     [222] = 0.0,
236     [223] = 0.0,
237     [224] = 0.0,
238     [225] = 0.0,
239     [226] = 0.0,
240     [227] = 0.0,
241     [228] = 0.0,
242     [229] = 0.0,
243     [230] = 0.0,
244     [231] = 0.0,
245     [232] = 0.0,
246     [233] = 0.0,
247     [234] = 0.0,
248     [235] = 0.0,
249     [236] = 0.0,
250     [237] = 0.0,
251     [238] = 0.0,
252     [239] = 0.0,
253     [240] = 0.0,
254     [241] = 0.0,
255     [242] = 0.0,
256     [243] = 0.0,
257     [244] = 0.0,
258     [245] = 0.0,
259     [246] = 0.0,
260     [247] = 0.0,
261     [248] = 0.0,
262     [249] = 0.0,
263     [250] = 0.0,
264     [251] = 0.0,
265     [252] = 0.0,
266     [253] = 0.0,
267     [254] = 0.0,
268     [255] = 0.0
269 };
270
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) {
274     double w = 0.0;
275     char *d = s;
276
277     while(*s) {
278         double t = verdana11_widths[(unsigned char)*s];
279         if(t == 0.0)
280             s++;
281         else {
282             w += t + VERDANA_KERNING;
283             if(d != s)
284                 *d++ = *s++;
285             else
286                 d = ++s;
287         }
288     }
289
290     *d = '\0';
291     w -= VERDANA_KERNING;
292     w += VERDANA_PADDING;
293     return (int)ceil(w);
294 }
295
296 static inline size_t escape_xmlz(char *dst, const char *src, size_t len) {
297     size_t i = len;
298
299     // required escapes from
300     // https://github.com/badges/shields/blob/master/badge.js
301     while(*src && i) {
302         switch(*src) {
303             case '\\':
304                 *dst++ = '/';
305                 src++;
306                 i--;
307                 break;
308
309             case '&':
310                 if(i > 5) {
311                     strcpy(dst, "&amp;");
312                     i -= 5;
313                     dst += 5;
314                     src++;
315                 }
316                 else goto cleanup;
317                 break;
318
319             case '<':
320                 if(i > 4) {
321                     strcpy(dst, "&lt;");
322                     i -= 4;
323                     dst += 4;
324                     src++;
325                 }
326                 else goto cleanup;
327                 break;
328
329             case '>':
330                 if(i > 4) {
331                     strcpy(dst, "&gt;");
332                     i -= 4;
333                     dst += 4;
334                     src++;
335                 }
336                 else goto cleanup;
337                 break;
338
339             case '"':
340                 if(i > 6) {
341                     strcpy(dst, "&quot;");
342                     i -= 6;
343                     dst += 6;
344                     src++;
345                 }
346                 else goto cleanup;
347                 break;
348
349             case '\'':
350                 if(i > 6) {
351                     strcpy(dst, "&apos;");
352                     i -= 6;
353                     dst += 6;
354                     src++;
355                 }
356                 else goto cleanup;
357                 break;
358
359             default:
360                 i--;
361                 *dst++ = *src++;
362                 break;
363         }
364     }
365
366 cleanup:
367     *dst = '\0';
368     return len - i;
369 }
370
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) {
372     static uint32_t
373             hash_seconds = 0,
374             hash_seconds_ago = 0,
375             hash_minutes = 0,
376             hash_minutes_ago = 0,
377             hash_hours = 0,
378             hash_hours_ago = 0,
379             hash_onoff = 0,
380             hash_updown = 0,
381             hash_okerror = 0,
382             hash_okfailed = 0,
383             hash_empty = 0,
384             hash_null = 0,
385             hash_percentage = 0,
386             hash_percent = 0,
387             hash_pcent = 0;
388
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");
405     }
406
407     const char *units = (units_ptr)?*units_ptr:NULL;
408     if(unlikely(!units)) units = "";
409
410     uint32_t hash_units = simple_hash(units);
411
412     if(unlikely((hash_units == hash_seconds && !strcmp(units, "seconds")) || (hash_units == hash_seconds_ago && !strcmp(units, "seconds ago")))) {
413         if(value == 0.0) {
414             snprintfz(value_string, value_string_len, "%s", "now");
415             units = "";
416             goto finish;
417         }
418         else if(isnan(value) || isinf(value)) {
419             snprintfz(value_string, value_string_len, "%s", "never");
420             units = "";
421             goto finish;
422         }
423
424         const char *suffix = (hash_units == hash_seconds_ago)?" ago":"";
425
426         size_t s = (size_t)value;
427         size_t d = s / 86400;
428         s = s % 86400;
429
430         size_t h = s / 3600;
431         s = s % 3600;
432
433         size_t m = s / 60;
434         s = s % 60;
435
436         if(d)
437             snprintfz(value_string, value_string_len, "%zu %s %02zu:%02zu:%02zu%s", d, (d == 1)?"day":"days", h, m, s, suffix);
438         else
439             snprintfz(value_string, value_string_len, "%02zu:%02zu:%02zu%s", h, m, s, suffix);
440
441         units = "";
442     }
443
444     else if(unlikely((hash_units == hash_minutes && !strcmp(units, "minutes")) || (hash_units == hash_minutes_ago && !strcmp(units, "minutes ago")))) {
445         if(value == 0.0) {
446             snprintfz(value_string, value_string_len, "%s", "now");
447             units = "";
448             goto finish;
449         }
450         else if(isnan(value) || isinf(value)) {
451             snprintfz(value_string, value_string_len, "%s", "never");
452             units = "";
453             goto finish;
454         }
455
456         const char *suffix = (hash_units == hash_minutes_ago)?" ago":"";
457
458         size_t m = (size_t)value;
459         size_t d = m / (60 * 24);
460         m = m % (60 * 24);
461
462         size_t h = m / 60;
463         m = m % 60;
464
465         if(d)
466             snprintfz(value_string, value_string_len, "%zud %02zuh %02zum%s", d, h, m, suffix);
467         else
468             snprintfz(value_string, value_string_len, "%zuh %zum%s", h, m, suffix);
469
470         units = "";
471     }
472
473     else if(unlikely((hash_units == hash_hours && !strcmp(units, "hours")) || (hash_units == hash_hours_ago && !strcmp(units, "hours ago")))) {
474         if(value == 0.0) {
475             snprintfz(value_string, value_string_len, "%s", "now");
476             units = "";
477             goto finish;
478         }
479         else if(isnan(value) || isinf(value)) {
480             snprintfz(value_string, value_string_len, "%s", "never");
481             units = "";
482             goto finish;
483         }
484
485         const char *suffix = (hash_units == hash_hours_ago)?" ago":"";
486
487         size_t h = (size_t)value;
488         size_t d = h / 24;
489         h = h % 24;
490
491         if(d)
492             snprintfz(value_string, value_string_len, "%zud %zuh%s", d, h, suffix);
493         else
494             snprintfz(value_string, value_string_len, "%zuh%s", h, suffix);
495
496         units = "";
497     }
498
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");
501         units = "";
502     }
503
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");
506         units = "";
507     }
508
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");
511         units = "";
512     }
513
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");
516         units = "";
517     }
518
519     else if(unlikely(hash_units == hash_empty && !strcmp(units, "empty")))
520         units = "";
521
522     else if(unlikely(hash_units == hash_null && !strcmp(units, "null")))
523         units = "";
524
525     else if(unlikely(hash_units == hash_percentage && !strcmp(units, "percentage")))
526         units = "%";
527
528     else if(unlikely(hash_units == hash_percent && !strcmp(units, "percent")))
529         units = "%";
530
531     else if(unlikely(hash_units == hash_pcent && !strcmp(units, "pcent")))
532         units = "%";
533
534     else if(unlikely(value_is_null)) {
535         strcpy(value_string, "-");
536         units = "";
537     }
538     else
539         return 0;
540
541 finish:
542     if(units_ptr) *units_ptr = units;
543     return 1;
544 }
545
546 static inline const char *color_map(const char *color) {
547     // colors from:
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";
560     return color;
561 }
562
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))
565         value = 0.0;
566
567     char color_buffer[256 + 1] = "";
568     char value_buffer[256 + 1] = "";
569     char comparison = '>';
570
571     // example input:
572     // color<max|color>min|color:null...
573
574     const char *c = color;
575     while(*c) {
576         char *dc = color_buffer, *dv = NULL;
577         size_t ci = 0, vi = 0;
578
579         const char *t = c;
580
581         while(*t && *t != '|') {
582             switch(*t) {
583                 case ':':
584                     comparison = '=';
585                     dv = value_buffer;
586                     break;
587
588                 case '}':
589                 case ')':
590                 case '>':
591                     if(t[1] == '=') {
592                         comparison = ')';
593                         t++;
594                     }
595                     else
596                         comparison = '>';
597                     dv = value_buffer;
598                     break;
599
600                 case '{':
601                 case '(':
602                 case '<':
603                     if(t[1] == '=') {
604                         comparison = '(';
605                         t++;
606                     }
607                     else
608                         comparison = '<';
609                     dv = value_buffer;
610                     break;
611
612                 default:
613                     if(dv) {
614                         if(vi < 256) {
615                             vi++;
616                             *dv++ = *t;
617                         }
618                     }
619                     else {
620                         if(ci < 256) {
621                             ci++;
622                             *dc++ = *t;
623                         }
624                     }
625                     break;
626             }
627
628             t++;
629         }
630
631         // prepare for next iteration
632         if(*t == '|') t++;
633         c = t;
634
635         // do the math
636         *dc = '\0';
637         if(dv) {
638             *dv = '\0';
639
640             if(value_is_null) {
641                 if(!*value_buffer || !strcmp(value_buffer, "null"))
642                     break;
643             }
644             else {
645                 calculated_number v = str2l(value_buffer);
646
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;
652             }
653         }
654         else
655             break;
656     }
657
658     const char *b;
659     if(color_buffer[0])
660         b = color_buffer;
661     else
662         b = color;
663
664     strncpyz(final, b, len);
665 }
666
667 // value + units
668 #define VALUE_STRING_SIZE 100
669
670 // label
671 #define LABEL_STRING_SIZE 200
672
673 // colors
674 #define COLOR_STRING_SIZE 100
675
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];
684
685     int label_width, value_width, total_width;
686
687     if(unlikely(!label_color || !*label_color))
688         label_color = "#555";
689
690     if(unlikely(!value_color || !*value_color))
691         value_color = (value_is_null)?"#999":"#4c1";
692
693     calc_colorz(value_color, value_color_buffer, COLOR_STRING_SIZE, value, value_is_null);
694
695     if(!fix_value_and_units(value_string, VALUE_STRING_SIZE, value, &units, value_is_null)) {
696         // we have to print the value
697
698         if(isnan(value) || isinf(value))
699             value = 0.0;
700
701         char *separator = "";
702         if(unlikely(isalnum(*units)))
703             separator = " ";
704
705         if(precision < 0) {
706             int len, lstop = 0, trim_zeros = 1;
707
708             calculated_number abs = value;
709             if(isless(value, 0)) {
710                 lstop = 1;
711                 abs = -value;
712             }
713
714             if(isgreaterequal(abs, 1000)) {
715                 len = snprintfz(value_string, VALUE_STRING_SIZE, "%0.0Lf", (long double) value);
716                 trim_zeros = 0;
717             }
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);
722
723             if(unlikely(trim_zeros)) {
724                 int l;
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';
729                         len--;
730                     }
731
732                     else if(unlikely(value_string[l] == '.')) {
733                         value_string[l] = '\0';
734                         len--;
735                         break;
736                     }
737
738                     else
739                         break;
740                 }
741             }
742
743             if(len >= 0)
744                 snprintfz(&value_string[len], VALUE_STRING_SIZE - len, "%s%s", separator, units);
745         }
746         else {
747             if(precision > 50) precision = 50;
748             snprintfz(value_string, VALUE_STRING_SIZE, "%0.*Lf%s%s", precision, (long double) value, separator, units);
749         }
750     }
751
752     // we need to copy the label, since verdana11_width may write to it
753     strncpyz(label_buffer, label, LABEL_STRING_SIZE);
754
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;
758
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);
763
764     wb->contenttype = CT_IMAGE_SVG_XML;
765
766     // svg template from:
767     // https://raw.githubusercontent.com/badges/shields/master/templates/flat-template.svg
768     buffer_sprintf(wb,
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\"/>"
773             "</linearGradient>"
774             "<mask id=\"round\">"
775                 "<rect width=\"%d\" height=\"20\" rx=\"3\" fill=\"#fff\"/>"
776             "</mask>"
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)\"/>"
781             "</g>"
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>"
787             "</g>"
788         "</svg>",
789         total_width, total_width,
790         label_width, label_color_escaped,
791         label_width, value_width, value_color_escaped,
792         total_width,
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);
797 }