include/punk_observe/po_svg.h

filter: Statementstmt 88.7% 95 / 107 Branchbran 75.8% 85 / 112 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 82.1% 180 / 219 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/live.xs ↑ Summary include/punk_observe/po_target.h »
1 /* po_svg.h - charts, generated in C, emitted as inline SVG.
2 *
3 * Server-rendered because that puts the work where the data already is, which
4 * matters when a chart is four thousand points. It also means every chart is
5 * in the HTML: it prints, it survives a screenshot, and it renders with
6 * scripting off.
7 *
8 * THE FORMATTER IS HAND-ROLLED, AND THAT IS THE POINT OF THIS FILE.
9 *
10 * Every coordinate here is a computed double, and the obvious way to write
11 * one into a path is sprintf("%f"). In this codebase that is the single most
12 * dangerous line available:
13 *
14 * - A Perl-flavoured formatter (croak, warn, sv_catpvf, my_snprintf) reads
15 * an NV for %f. Passing a C double panics the quadmath smokers and reads
16 * silent garbage on x86_64.
17 * - The Windows CRT prints three-digit exponents: `1e-005`. An SVG path
18 * containing that is not a valid path, and the chart simply does not
19 * draw - on one platform, with no error.
20 * - %f also emits six decimals for every number, which triples the size of
21 * a path nobody can read anyway.
22 *
23 * So po_fmt below writes a fixed-point decimal itself, with no library call,
24 * no exponent form possible, and two decimals. t/0092-svg.t greps the output
25 * for exponent forms and fails if one appears.
26 */
27 #ifndef PO_SVG_H
28 #define PO_SVG_H
29  
30 #include "punk_observe/po_compat.h"
31  
32 #define PO_SVG_MAX 2048
33  
34 /* Write `v` with two decimals into `out` (at least 32 bytes). Returns the
35 * length. No sprintf, no locale, no exponent, ever.
36 *
37 * Values are clamped to a range a chart can contain. A NaN or an infinity
38 * reaching a coordinate is a bug upstream, but it must not produce `nan` in
39 * an attribute - that kills the whole path rather than one point. */
40 2256 static size_t po_fmt(double v, char *out) {
41 char tmp[24];
42 2256 size_t n = 0;
43 2256 int neg = 0;
44 po_u64 whole, frac;
45  
46 /* NaN is the only value not equal to itself. */
47 2256 ▶ if (!(v == v)) v = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!(v == v)) v = 0; 1 2255 2256
48 2256 ▶ if (v > 1e9) v = 1e9;
Branch: 1 branch
BranchTrueFalseTotal
if (v > 1e9) v = 1e9; 81 2175 2256
49 2256 ▶ if (v < -1e9) v = -1e9;
Branch: 1 branch
BranchTrueFalseTotal
if (v < -1e9) v = -1e9; 76 2180 2256
50  
51 2256 ▶ if (v < 0) { neg = 1; v = -v; }
Branch: 1 branch
BranchTrueFalseTotal
if (v < 0) { neg = 1; v = -v; } 1010 1246 2256
52 2256 whole = (po_u64)v;
53 2256 frac = (po_u64)((v - (double)whole) * 100.0 + 0.5);
54 2256 ▶ if (frac >= 100) { whole++; frac -= 100; }
Branch: 1 branch
BranchTrueFalseTotal
if (frac >= 100) { whole++; frac -= 100; } 3 2253 2256
55  
56 2256 ▶ if (neg && (whole || frac)) out[n++] = '-';
Branch: 3 branches
BranchTrueFalseTotal
if (neg && (whole || frac)) out[n++] = '-'; 1010 1246 2256
if (neg && (whole || frac)) out[n++] = '-'; 562 448 1010
if (neg && (whole || frac)) out[n++] = '-'; 87 475 562
57 2256 ▶ if (whole == 0) out[n++] = '0';
Branch: 1 branch
BranchTrueFalseTotal
if (whole == 0) out[n++] = '0'; 1119 1137 2256
58 else {
59 1137 size_t t = 0;
60 7043 ▶ while (whole) { tmp[t++] = (char)('0' + (int)(whole % 10)); whole /= 10; }
Branch: 1 branch
BranchTrueFalseTotal
while (whole) { tmp[t++] = (char)('0' + (int)(whole % 10)); whole /= 10; } 5906 1137 7043
61 7043 ▶ while (t) out[n++] = tmp[--t];
Branch: 1 branch
BranchTrueFalseTotal
while (t) out[n++] = tmp[--t]; 5906 1137 7043
62 }
63 2256 ▶ if (frac) {
Branch: 1 branch
BranchTrueFalseTotal
if (frac) { 961 1295 2256
64 961 out[n++] = '.';
65 961 out[n++] = (char)('0' + (int)(frac / 10));
66 961 ▶ if (frac % 10) out[n++] = (char)('0' + (int)(frac % 10));
Branch: 1 branch
BranchTrueFalseTotal
if (frac % 10) out[n++] = (char)('0' + (int)(frac % 10)); 865 96 961
67 /* a trailing zero is noise: 1.50 and 1.5 draw identically */
68 961 ▶ if (out[n - 1] == '0') n--;
Branch: 1 branch
BranchTrueFalseTotal
if (out[n - 1] == '0') n--; 0 961 961
69 }
70 2256 out[n] = '\0';
71 2256 return n;
72 }
73  
74 /* An integer, for counts and indices. */
75 0 static size_t po_fmt_i(po_u64 v, char *out) {
76 char tmp[24];
77 0 size_t n = 0, t = 0;
78 0 ▶ if (v == 0) { out[0] = '0'; out[1] = '\0'; return 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (v == 0) { out[0] = '0'; out[1] = '\0'; return 1; } 0 0 0
79 0 ▶ while (v) { tmp[t++] = (char)('0' + (int)(v % 10)); v /= 10; }
Branch: 1 branch
BranchTrueFalseTotal
while (v) { tmp[t++] = (char)('0' + (int)(v % 10)); v /= 10; } 0 0 0
80 0 ▶ while (t) out[n++] = tmp[--t];
Branch: 1 branch
BranchTrueFalseTotal
while (t) out[n++] = tmp[--t]; 0 0 0
81 0 out[n] = '\0';
82 0 return n;
83 }
84  
85 /* ---- escaping -------------------------------------------------------------
86 *
87 * SVG ATTRIBUTE CONTEXT IS NOT HTML TEXT CONTEXT.
88 *
89 * A span name is untrusted input and it goes into both. In text, `<` and `&`
90 * matter. In an attribute, the QUOTE matters most: a name containing one ends
91 * the attribute early and everything after it becomes markup. Stencil
92 * autoescapes for HTML; nothing autoescapes for the attribute of an element
93 * this file builds by hand, so it is done here.
94 */
95 9 static size_t po_esc_attr(const char *p, size_t len, char *out, size_t cap) {
96 9 size_t n = 0, i;
97 59 ▶ for (i = 0; i < len && n + 8 < cap; i++) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < len && n + 8 < cap; i++) { 50 9 59
for (i = 0; i < len && n + 8 < cap; i++) { 50 0 50
98 50 unsigned char c = (unsigned char)p[i];
99 50 switch (c) {
100 3 case '"': memcpy(out + n, "&quot;", 6); n += 6; break;
101 1 case '\'': memcpy(out + n, "&#39;", 5); n += 5; break;
102 1 case '&': memcpy(out + n, "&amp;", 5); n += 5; break;
103 1 case '<': memcpy(out + n, "&lt;", 4); n += 4; break;
104 1 case '>': memcpy(out + n, "&gt;", 4); n += 4; break;
105 43 default:
106 /* Control characters are dropped rather than escaped: they
107 * cannot appear in a valid name and a numeric entity for one
108 * is not valid XML either. */
109 43 ▶ if (c >= 0x20 || c == '\t') out[n++] = (char)c;
Branch: 2 branches
BranchTrueFalseTotal
if (c >= 0x20 || c == '\t') out[n++] = (char)c; 3 40 43
if (c >= 0x20 || c == '\t') out[n++] = (char)c; 1 2 3
110 43 break;
111 }
112 }
113 9 out[n] = '\0';
114 9 return n;
115 }
116  
117 /* ---- nice numbers ---------------------------------------------------------
118 *
119 * An axis labelled 0, 33.33, 66.67, 100 is a chart nobody can read. The
120 * classic algorithm: round the range to a "nice" magnitude, then pick a step
121 * from {1, 2, 5} times a power of ten.
122 */
123 24 static double po_nice_num(double range, int round_it) {
124 double expo, f, nf;
125 24 ▶ if (range <= 0) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (range <= 0) return 1; 0 24 24
126 24 expo = 0;
127 24 { double r = range;
128 62 ▶ while (r >= 10) { r /= 10; expo += 1; }
Branch: 1 branch
BranchTrueFalseTotal
while (r >= 10) { r /= 10; expo += 1; } 38 24 62
129 26 ▶ while (r < 1) { r *= 10; expo -= 1; }
Branch: 1 branch
BranchTrueFalseTotal
while (r < 1) { r *= 10; expo -= 1; } 2 24 26
130 24 f = r; }
131 24 ▶ if (round_it) nf = f < 1.5 ? 1 : f < 3 ? 2 : f < 7 ? 5 : 10;
Branch: 4 branches
BranchTrueFalseTotal
if (round_it) nf = f < 1.5 ? 1 : f < 3 ? 2 : f < 7 ? 5 : 10; 12 12 24
if (round_it) nf = f < 1.5 ? 1 : f < 3 ? 2 : f < 7 ? 5 : 10; 0 12 12
if (round_it) nf = f < 1.5 ? 1 : f < 3 ? 2 : f < 7 ? 5 : 10; 11 1 12
if (round_it) nf = f < 1.5 ? 1 : f < 3 ? 2 : f < 7 ? 5 : 10; 1 0 1
132 12 ▶ else nf = f <= 1 ? 1 : f <= 2 ? 2 : f <= 5 ? 5 : 10;
Branch: 3 branches
BranchTrueFalseTotal
else nf = f <= 1 ? 1 : f <= 2 ? 2 : f <= 5 ? 5 : 10; 9 3 12
else nf = f <= 1 ? 1 : f <= 2 ? 2 : f <= 5 ? 5 : 10; 1 2 3
else nf = f <= 1 ? 1 : f <= 2 ? 2 : f <= 5 ? 5 : 10; 1 1 2
133 24 { double p = 1; int i;
134 62 ▶ if (expo > 0) for (i = 0; i < (int)expo; i++) p *= 10;
Branch: 2 branches
BranchTrueFalseTotal
if (expo > 0) for (i = 0; i < (int)expo; i++) p *= 10; 17 7 24
if (expo > 0) for (i = 0; i < (int)expo; i++) p *= 10; 38 17 55
135 9 ▶ else for (i = 0; i < -(int)expo; i++) p /= 10;
Branch: 1 branch
BranchTrueFalseTotal
else for (i = 0; i < -(int)expo; i++) p /= 10; 2 7 9
136 24 return nf * p; }
137 }
138  
139 typedef struct {
140 double lo, hi, step;
141 double tick[16];
142 int n;
143 } po_axis;
144  
145 /* Choose an axis covering [min, max] with about `want` ticks. */
146 12 static void po_axis_make(po_axis *a, double min, double max, int want) {
147 double range, i;
148 12 memset(a, 0, sizeof(*a));
149 12 ▶ if (want < 2) want = 2;
Branch: 1 branch
BranchTrueFalseTotal
if (want < 2) want = 2; 0 12 12
150 12 ▶ if (want > 15) want = 15;
Branch: 1 branch
BranchTrueFalseTotal
if (want > 15) want = 15; 0 12 12
151  
152 /* A flat series is a real case - a gauge that has not moved - and
153 * range 0 would divide by zero. Give it a band around the value so the
154 * line sits in the middle instead of on an edge. */
155 12 ▶ if (!(max > min)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(max > min)) { 2 10 12
156 2 ▶ double v = (max == max) ? max : 0;
Branch: 1 branch
BranchTrueFalseTotal
double v = (max == max) ? max : 0; 2 0 2
157 2 ▶ double pad = v != 0 ? (v < 0 ? -v : v) * 0.1 : 1;
Branch: 2 branches
BranchTrueFalseTotal
double pad = v != 0 ? (v < 0 ? -v : v) * 0.1 : 1; 1 1 2
double pad = v != 0 ? (v < 0 ? -v : v) * 0.1 : 1; 0 1 1
158 2 min = v - pad;
159 2 max = v + pad;
160 }
161  
162 12 range = po_nice_num(max - min, 0);
163 12 a->step = po_nice_num(range / (want - 1), 1);
164 12 ▶ a->lo = a->step * (double)(po_i64)(min / a->step - (min < 0 ? 1 : 0));
Branch: 1 branch
BranchTrueFalseTotal
a->lo = a->step * (double)(po_i64)(min / a->step - (min < 0 ? 1 : 0)); 2 10 12
165 12 ▶ a->hi = a->step * (double)(po_i64)(max / a->step + (max > 0 ? 1 : 0));
Branch: 1 branch
BranchTrueFalseTotal
a->hi = a->step * (double)(po_i64)(max / a->step + (max > 0 ? 1 : 0)); 12 0 12
166 12 ▶ if (a->lo > min) a->lo -= a->step;
Branch: 1 branch
BranchTrueFalseTotal
if (a->lo > min) a->lo -= a->step; 0 12 12
167 12 ▶ if (a->hi < max) a->hi += a->step;
Branch: 1 branch
BranchTrueFalseTotal
if (a->hi < max) a->hi += a->step; 0 12 12
168  
169 92 ▶ for (i = a->lo; i <= a->hi + a->step * 0.5 && a->n < 16; i += a->step)
Branch: 2 branches
BranchTrueFalseTotal
for (i = a->lo; i <= a->hi + a->step * 0.5 && a->n < 16; i += a->step) 80 12 92
for (i = a->lo; i <= a->hi + a->step * 0.5 && a->n < 16; i += a->step) 80 0 80
170 80 a->tick[a->n++] = i;
171 12 }
172  
173 /* ---- paths ---------------------------------------------------------------- */
174  
175 typedef struct {
176 char *b;
177 size_t n, cap;
178 int err;
179 } po_svg;
180  
181 3 static int po_svg_init(po_svg *s, size_t hint) {
182 3 memset(s, 0, sizeof(*s));
183 3 ▶ s->cap = hint ? hint : 1024;
Branch: 1 branch
BranchTrueFalseTotal
s->cap = hint ? hint : 1024; 3 0 3
184 3 s->b = (char *)malloc(s->cap);
185 3 ▶ if (s->b) s->b[0] = '\0';
Branch: 1 branch
BranchTrueFalseTotal
if (s->b) s->b[0] = '\0'; 3 0 3
186 3 return s->b != NULL;
187 }
188 3 static void po_svg_free(po_svg *s) { free(s->b); s->b = NULL; }
189  
190 32 static void po_svg_put(po_svg *s, const char *p, size_t n) {
191 32 ▶ if (s->err) return;
Branch: 1 branch
BranchTrueFalseTotal
if (s->err) return; 0 32 32
192 32 ▶ if (s->n + n + 1 > s->cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (s->n + n + 1 > s->cap) { 0 32 32
193 0 size_t want = s->cap * 2;
194 char *nb;
195 0 ▶ while (want < s->n + n + 1) want *= 2;
Branch: 1 branch
BranchTrueFalseTotal
while (want < s->n + n + 1) want *= 2; 0 0 0
196 0 nb = (char *)realloc(s->b, want);
197 0 ▶ if (!nb) { s->err = 1; return; }
Branch: 1 branch
BranchTrueFalseTotal
if (!nb) { s->err = 1; return; } 0 0 0
198 0 s->b = nb; s->cap = want;
199 }
200 32 memcpy(s->b + s->n, p, n);
201 32 s->n += n;
202 32 s->b[s->n] = '\0';
203 }
204 16 static void po_svg_s(po_svg *s, const char *p) { po_svg_put(s, p, strlen(p)); }
205 16 static void po_svg_n(po_svg *s, double v) {
206 char t[32];
207 16 size_t n = po_fmt(v, t);
208 16 po_svg_put(s, t, n);
209 16 }
210  
211 /* A polyline through the points, scaled into a viewport. Returns the path's
212 * `d` attribute value. */
213 3 static void po_svg_line(po_svg *s, const double *x, const double *y, int n,
214 double x0, double x1, double y0, double y1,
215 double w, double h) {
216 int i;
217 3 ▶ double xs = (x1 > x0) ? w / (x1 - x0) : 0;
Branch: 1 branch
BranchTrueFalseTotal
double xs = (x1 > x0) ? w / (x1 - x0) : 0; 2 1 3
218 3 ▶ double ys = (y1 > y0) ? h / (y1 - y0) : 0;
Branch: 1 branch
BranchTrueFalseTotal
double ys = (y1 > y0) ? h / (y1 - y0) : 0; 1 2 3
219 11 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 8 3 11
220 8 ▶ po_svg_s(s, i ? " L" : "M");
Branch: 1 branch
BranchTrueFalseTotal
po_svg_s(s, i ? " L" : "M"); 5 3 8
221 8 po_svg_n(s, (x[i] - x0) * xs);
222 8 po_svg_s(s, " ");
223 /* SVG y grows downward; a chart's does not. */
224 8 po_svg_n(s, h - (y[i] - y0) * ys);
225 }
226 3 }
227  
228 #endif /* PO_SVG_H */
« xs/live.xs ↑ Summary include/punk_observe/po_target.h »