]> arthur.barton.de Git - netdata.git/blob - src/web_buffer_svg.c
basic rendering of the alarm log ready
[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 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 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 "%";
374     return units;
375 }
376
377 static inline const char *color_map(const char *color) {
378     // colors from:
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";
391     return color;
392 }
393
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 = '>';
398
399     // example input:
400     // color<max|color>min|color:null...
401
402     const char *c = color;
403     while(*c) {
404         char *dc = color_buffer, *dv = NULL;
405         size_t ci = 0, vi = 0;
406
407         const char *t = c;
408
409         while(*t && *t != '|') {
410             switch(*t) {
411                 case ':':
412                     comparison = '=';
413                     dv = value_buffer;
414                     break;
415
416                 case '}':
417                 case ')':
418                 case '>':
419                     if(t[1] == '=') {
420                         comparison = ')';
421                         t++;
422                     }
423                     else
424                         comparison = '>';
425                     dv = value_buffer;
426                     break;
427
428                 case '{':
429                 case '(':
430                 case '<':
431                     if(t[1] == '=') {
432                         comparison = '(';
433                         t++;
434                     }
435                     else
436                         comparison = '<';
437                     dv = value_buffer;
438                     break;
439
440                 default:
441                     if(dv) {
442                         if(vi < 256) {
443                             vi++;
444                             *dv++ = *t;
445                         }
446                     }
447                     else {
448                         if(ci < 256) {
449                             ci++;
450                             *dc++ = *t;
451                         }
452                     }
453                     break;
454             }
455
456             t++;
457         }
458
459         // prepare for next iteration
460         if(*t == '|') t++;
461         c = t;
462
463         // do the math
464         *dc = '\0';
465         if(dv) {
466             *dv = '\0';
467
468             if(value_is_null) {
469                 if(!*value_buffer || !strcmp(value_buffer, "null"))
470                     break;
471             }
472             else {
473                 calculated_number v = strtold(value_buffer, NULL);
474
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;
480             }
481         }
482         else
483             break;
484     }
485
486     const char *b;
487     if(color_buffer[0])
488         b = color_buffer;
489     else
490         b = color;
491
492     strncpyz(final, b, len);
493 }
494
495 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) {
496     char label_buffer[256 + 1], value_string[512 + 1], value_color_buffer[256 + 1];
497     char label_escaped[256 + 1], value_escaped[512 + 1], label_color_escaped[256 + 1], value_color_escaped[256 + 1];
498     int label_width, value_width, total_width;
499
500     if(!label_color || !*label_color) label_color = "#555";
501     if(!value_color || !*value_color) value_color = (value_is_null)?"#999":"#4c1";
502
503     units = fix_units(units);
504     calc_colorz(value_color, value_color_buffer, 256, value, value_is_null);
505
506     char *separator = "";
507     if(isalnum(*units)) separator = " ";
508
509     if(value_is_null)
510         strcpy(value_string, "-");
511     else if(precision < 0) {
512         calculated_number abs = (value < (calculated_number)0)?-value:value;
513         if(abs > (calculated_number)1000.0)      snprintfz(value_string, 512, "%0.0Lf%s%s", (long double)value, separator, units);
514         else if(abs > (calculated_number)100.0)  snprintfz(value_string, 512, "%0.1Lf%s%s", (long double)value, separator, units);
515         else if(abs > (calculated_number)1.0)    snprintfz(value_string, 512, "%0.2Lf%s%s", (long double)value, separator, units);
516         else if(abs > (calculated_number)0.1)    snprintfz(value_string, 512, "%0.3Lf%s%s", (long double)value, separator, units);
517         else                                     snprintfz(value_string, 512, "%0.4Lf%s%s", (long double)value, separator, units);
518     }
519     else {
520         if(precision > 50) precision = 50;
521         snprintfz(value_string, 512, "%0.*Lf%s%s", precision, (long double)value, separator, units);
522     }
523
524     // we need to copy the label, since verdana11_width may write to it
525     strncpyz(label_buffer, label, 256);
526
527     label_width = verdana11_width(label_buffer) + (BADGE_HORIZONTAL_PADDING * 2);
528     value_width = verdana11_width(value_string) + (BADGE_HORIZONTAL_PADDING * 2);
529     total_width = label_width + value_width;
530
531     escape_xmlz(label_escaped, label_buffer, 256);
532     escape_xmlz(value_escaped, value_string, 256);
533     escape_xmlz(label_color_escaped, color_map(label_color), 256);
534     escape_xmlz(value_color_escaped, color_map(value_color_buffer), 256);
535
536     wb->contenttype = CT_IMAGE_SVG_XML;
537
538     // svg template from:
539     // https://raw.githubusercontent.com/badges/shields/master/templates/flat-template.svg
540     buffer_sprintf(wb,
541         "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"%d\" height=\"20\">"
542             "<linearGradient id=\"smooth\" x2=\"0\" y2=\"100%%\">"
543                 "<stop offset=\"0\" stop-color=\"#bbb\" stop-opacity=\".1\"/>"
544                 "<stop offset=\"1\" stop-opacity=\".1\"/>"
545             "</linearGradient>"
546             "<mask id=\"round\">"
547                 "<rect width=\"%d\" height=\"20\" rx=\"3\" fill=\"#fff\"/>"
548             "</mask>"
549             "<g mask=\"url(#round)\">"
550                 "<rect width=\"%d\" height=\"20\" fill=\"%s\"/>"
551                 "<rect x=\"%d\" width=\"%d\" height=\"20\" fill=\"%s\"/>"
552                 "<rect width=\"%d\" height=\"20\" fill=\"url(#smooth)\"/>"
553             "</g>"
554             "<g fill=\"#fff\" text-anchor=\"middle\" font-family=\"DejaVu Sans,Verdana,Geneva,sans-serif\" font-size=\"11\">"
555                 "<text x=\"%d\" y=\"15\" fill=\"#010101\" fill-opacity=\".3\">%s</text>"
556                 "<text x=\"%d\" y=\"14\">%s</text>"
557                 "<text x=\"%d\" y=\"15\" fill=\"#010101\" fill-opacity=\".3\">%s</text>"
558                 "<text x=\"%d\" y=\"14\">%s</text>"
559             "</g>"
560         "</svg>",
561         total_width, total_width,
562         label_width, label_color_escaped,
563         label_width, value_width, value_color_escaped,
564         total_width,
565         label_width / 2, label_escaped,
566         label_width / 2, label_escaped,
567         label_width + value_width / 2 -1, value_escaped,
568         label_width + value_width / 2 -1, value_escaped);
569 }