include/qr/qr_svg.h

filter: Statementstmt 88.3% 364 / 412 Branchbran 79.3% 319 / 402 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 83.9% 683 / 814 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« Code.xs ↑ Summary include/qr/qr_bits.h »
1 #ifndef QR_SVG_H
2 #define QR_SVG_H
3  
4 #include <stdarg.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <math.h>
9  
10 #include "qr/qr_encode.h"
11 #include "qr/qr_img.h"
12  
13 /* qr_svg.h - the SVG serialiser, with styling that answers to decoders.
14 *
15 * Every styling option here is also a way to produce a symbol that does
16 * not scan, so each carries the rule that keeps it decodable:
17 *
18 * - Colour is checked as luminance contrast, because a decoder
19 * binarizes: it converts to luminance and thresholds, and a loud
20 * red-on-green pair with near-equal luminance is invisible to it.
21 * - Dark must be darker. Some decoders try the inversion as a
22 * fallback; many do not.
23 * - Finder patterns are always drawn as solid shapes, never as their
24 * constituent modules, because the locator wants an unbroken
25 * 1:1:3:1:1 run and gaps between dots break it.
26 *
27 * The floors are calibrated against zbar blur sweeps (2026-08, level H,
28 * 600px render, gaussian sigma):
29 *
30 * - Dots at radius 0.30 to 0.50 meet or beat the square baseline;
31 * 0.25 falls below it and 0.20 falls off a cliff. Floor: 0.30.
32 * - Blur tolerance holds at the baseline down to a luminance gap of
33 * 0.40 and degrades steadily below it. zbar itself decodes a clean
34 * render at gaps far smaller, but a clean 600px PNG is the
35 * friendliest case a symbol will ever meet. Floor: 0.40.
36 */
37  
38 #define QR_CONTRAST_FLOOR 0.40
39 #define QR_DOT_RADIUS_FLOOR 0.30
40  
41 #define QR_SHAPE_SQUARE 0
42 #define QR_SHAPE_ROUNDED 1
43 #define QR_SHAPE_DOT 2
44  
45 #define QR_FINDER_SQUARE 0
46 #define QR_FINDER_ROUNDED 1
47 #define QR_FINDER_CIRCLE 2
48  
49 #define QR_LOGO_NONE 0
50 #define QR_LOGO_TEXT 1
51 #define QR_LOGO_SVG 2
52 #define QR_LOGO_IMAGE 3
53  
54 #define QR_MAX_STOPS 8
55  
56 typedef struct {
57 int shape; /* QR_SHAPE_* */
58 double radius; /* modules: corner radius or dot radius */
59 int finder; /* QR_FINDER_* */
60 char dark[12]; /* as given, normalised to #rrggbb */
61 char light[12]; /* likewise, or "none" */
62 char finder_dark[12];
63 int nstops; /* 0 = no gradient */
64 int grad_radial;
65 double grad_angle; /* degrees, linear only */
66 char stops[QR_MAX_STOPS][12];
67 } qr_style;
68  
69 typedef struct {
70 int kind; /* QR_LOGO_* */
71 const char *text; /* TEXT: the string, escaped at emit */
72 size_t text_len;
73 const char *markup; /* SVG: caller markup */
74 size_t markup_len;
75 const unsigned char *img; /* IMAGE: raw bytes */
76 size_t img_len;
77 int img_fmt; /* QR_IMG_PNG or QR_IMG_JPEG */
78 double scale; /* fraction of the symbol side, 0 = 0.17 */
79 double em; /* TEXT cap height in modules, 0 = auto */
80 } qr_logo;
81  
82 typedef struct {
83 int version, ecc, mask, size;
84 double bx, by, bw, bh; /* logo box, module coords sans quiet */
85 int covered; /* modules under the box */
86 int fn_hit; /* function pattern modules under it */
87 char hits[64]; /* first few "r,c" pairs */
88 } qr_svg_info;
89  
90 115 static void qr_style_init(qr_style *st)
91 {
92 115 memset(st, 0, sizeof *st);
93 115 st->shape = QR_SHAPE_SQUARE;
94 115 st->finder = QR_FINDER_SQUARE;
95 115 st->radius = 0.0;
96 115 strcpy(st->dark, "#000000");
97 115 strcpy(st->light, "#ffffff");
98 115 st->finder_dark[0] = '\0'; /* empty = follow dark */
99 115 }
100  
101 150 static void qr_logo_init(qr_logo *lg)
102 {
103 150 memset(lg, 0, sizeof *lg);
104 150 lg->kind = QR_LOGO_NONE;
105 150 }
106  
107 /* --- a growable string --------------------------------------------------- */
108  
109 typedef struct {
110 char *p;
111 size_t len, cap;
112 } qr_str;
113  
114 28533 static int qr_str_grow(qr_str *s, size_t need)
115 {
116 char *np;
117 28533 ▶ size_t cap = s->cap ? s->cap : 4096;
Branch: 1 branch
BranchTrueFalseTotal
size_t cap = s->cap ? s->cap : 4096; 28475 58 28533
118  
119 28590 ▶ while (cap < s->len + need + 1)
Branch: 1 branch
BranchTrueFalseTotal
while (cap < s->len + need + 1) 57 28533 28590
120 57 cap *= 2;
121 28533 ▶ if (cap == s->cap)
Branch: 1 branch
BranchTrueFalseTotal
if (cap == s->cap) 28418 115 28533
122 28418 return 0;
123 115 np = (char *)realloc(s->p, cap);
124 115 ▶ if (!np)
Branch: 1 branch
BranchTrueFalseTotal
if (!np) 0 115 115
125 0 return -1;
126 115 s->p = np;
127 115 s->cap = cap;
128 115 return 0;
129 }
130  
131 28491 static int qr_str_catf(qr_str *s, const char *fmt, ...)
132 {
133 va_list ap, ap2;
134 int n;
135  
136 28491 va_start(ap, fmt);
137 28491 va_copy(ap2, ap);
138 28491 n = vsnprintf(NULL, 0, fmt, ap);
139 28491 va_end(ap);
140 28491 ▶ if (n < 0 || qr_str_grow(s, (size_t)n) < 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (n < 0 || qr_str_grow(s, (size_t)n) < 0) { 28491 0 28491
if (n < 0 || qr_str_grow(s, (size_t)n) < 0) { 0 28491 28491
141 0 va_end(ap2);
142 0 return -1;
143 }
144 28491 vsnprintf(s->p + s->len, (size_t)n + 1, fmt, ap2);
145 28491 va_end(ap2);
146 28491 s->len += (size_t)n;
147 28491 return 0;
148 }
149  
150 42 static int qr_str_catn(qr_str *s, const char *src, size_t n)
151 {
152 42 ▶ if (qr_str_grow(s, n) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (qr_str_grow(s, n) < 0) 0 42 42
153 0 return -1;
154 42 memcpy(s->p + s->len, src, n);
155 42 s->len += n;
156 42 s->p[s->len] = '\0';
157 42 return 0;
158 }
159  
160 /* --- colour -------------------------------------------------------------- */
161  
162 1001 static int qr_hexval(char c)
163 {
164 1001 ▶ if (c >= '0' && c <= '9') return c - '0';
Branch: 2 branches
BranchTrueFalseTotal
if (c >= '0' && c <= '9') return c - '0'; 1001 0 1001
if (c >= '0' && c <= '9') return c - '0'; 521 480 1001
165 480 ▶ if (c >= 'a' && c <= 'f') return c - 'a' + 10;
Branch: 2 branches
BranchTrueFalseTotal
if (c >= 'a' && c <= 'f') return c - 'a' + 10; 479 1 480
if (c >= 'a' && c <= 'f') return c - 'a' + 10; 478 1 479
166 2 ▶ if (c >= 'A' && c <= 'F') return c - 'A' + 10;
Branch: 2 branches
BranchTrueFalseTotal
if (c >= 'A' && c <= 'F') return c - 'A' + 10; 2 0 2
if (c >= 'A' && c <= 'F') return c - 'A' + 10; 1 1 2
167 1 return -1;
168 }
169  
170 /* Parse #rgb, #rrggbb or #rrggbbaa into 0..1 channels. Returns -1 for
171 * anything else: the luminance check needs numbers, and a CSS
172 * named-colour table is bloat with no consumer. */
173 171 static int qr_color_parse(const char *s, size_t n,
174 double *r, double *g, double *b, double *a)
175 {
176 int v[8], i;
177  
178 171 ▶ if (n == 0 || s[0] != '#')
Branch: 2 branches
BranchTrueFalseTotal
if (n == 0 || s[0] != '#') 171 0 171
if (n == 0 || s[0] != '#') 3 168 171
179 3 return -1;
180 168 s++; n--;
181 168 ▶ if (n != 3 && n != 6 && n != 8)
Branch: 3 branches
BranchTrueFalseTotal
if (n != 3 && n != 6 && n != 8) 166 2 168
if (n != 3 && n != 6 && n != 8) 4 162 166
if (n != 3 && n != 6 && n != 8) 1 3 4
182 1 return -1;
183 1167 ▶ for (i = 0; i < (int)n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < (int)n; i++) { 1001 166 1167
184 1001 v[i] = qr_hexval(s[i]);
185 1001 ▶ if (v[i] < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (v[i] < 0) 1 1000 1001
186 1 return -1;
187 }
188 166 *a = 1.0;
189 166 ▶ if (n == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (n == 3) { 2 164 166
190 2 *r = (v[0] * 17) / 255.0;
191 2 *g = (v[1] * 17) / 255.0;
192 2 *b = (v[2] * 17) / 255.0;
193 } else {
194 164 *r = (v[0] * 16 + v[1]) / 255.0;
195 164 *g = (v[2] * 16 + v[3]) / 255.0;
196 164 *b = (v[4] * 16 + v[5]) / 255.0;
197 164 ▶ if (n == 8)
Branch: 1 branch
BranchTrueFalseTotal
if (n == 8) 3 161 164
198 3 *a = (v[6] * 16 + v[7]) / 255.0;
199 }
200 166 return 0;
201 }
202  
203 /* Relative luminance, sRGB linearised - the quantity a binarizer
204 * effectively thresholds on. */
205 492 static double qr_lum_chan(double c)
206 {
207 492 ▶ return c <= 0.04045 ? c / 12.92 : pow((c + 0.055) / 1.055, 2.4);
Branch: 1 branch
BranchTrueFalseTotal
return c <= 0.04045 ? c / 12.92 : pow((c + 0.055) / 1.055, 2.4); 204 288 492
208 }
209  
210 164 static double qr_luminance(double r, double g, double b)
211 {
212 164 return 0.2126 * qr_lum_chan(r) + 0.7152 * qr_lum_chan(g)
213 164 + 0.0722 * qr_lum_chan(b);
214 }
215  
216 /* Validate one colour option: hex, opaque, and store it normalised to
217 * #rrggbb. Fills *lum. Returns -1 with err set. */
218 171 static int qr_color_check(const char *what, const char *s, size_t n,
219 char out[8], double *lum,
220 char *err, size_t errlen)
221 {
222 double r, g, b, a;
223  
224 171 ▶ if (qr_color_parse(s, n, &r, &g, &b, &a) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (qr_color_parse(s, n, &r, &g, &b, &a) < 0) { 5 166 171
225 5 ▶ snprintf(err, errlen,
Branch: 1 branch
BranchTrueFalseTotal
snprintf(err, errlen, 5 0 5
226 "%s '%.*s' is not a hex colour (#rgb, #rrggbb or #rrggbbaa)",
227 what, (int)(n > 32 ? 32 : n), s);
228 5 return -1;
229 }
230 166 ▶ if (a < 1.0) {
Branch: 1 branch
BranchTrueFalseTotal
if (a < 1.0) { 2 164 166
231 2 snprintf(err, errlen,
232 "%s '%.*s' is translucent (alpha %.2f); a translucent module "
233 "is a contrast reduction wearing a costume",
234 what, (int)n, s, a);
235 2 return -1;
236 }
237 164 snprintf(out, 8, "#%02x%02x%02x",
238 164 (int)(r * 255 + 0.5), (int)(g * 255 + 0.5), (int)(b * 255 + 0.5));
239 164 *lum = qr_luminance(r, g, b);
240 164 return 0;
241 }
242  
243 /* One dark-role colour against the light ground. Inversion and a
244 * too-small gap are different defects and get different messages. */
245 88 static int qr_contrast_check(const char *what, const char *hex, double dl,
246 double ll, char *err, size_t errlen)
247 {
248 88 ▶ if (dl >= ll) {
Branch: 1 branch
BranchTrueFalseTotal
if (dl >= ll) { 2 86 88
249 2 snprintf(err, errlen,
250 "inverted colours: %s %s (luminance %.3f) is not darker than "
251 "light (luminance %.3f); decoders assume dark modules on a "
252 "light ground",
253 what, hex, dl, ll);
254 2 return -1;
255 }
256 86 ▶ if (ll - dl < QR_CONTRAST_FLOOR) {
Branch: 1 branch
BranchTrueFalseTotal
if (ll - dl < QR_CONTRAST_FLOOR) { 6 80 86
257 6 snprintf(err, errlen,
258 "contrast too low: %s %s (luminance %.3f) against light "
259 "(luminance %.3f) leaves %.3f, floor is %.2f",
260 what, hex, dl, ll, ll - dl, QR_CONTRAST_FLOOR);
261 6 return -1;
262 }
263 80 return 0;
264 }
265  
266 /* --- shape subpaths ------------------------------------------------------ */
267  
268 /* A clockwise rounded-rect subpath; rx 0 degenerates to the rect. */
269 504 static int qr_sub_rrect(qr_str *s, double x, double y,
270 double w, double h, double rx)
271 {
272 504 ▶ if (rx <= 0.0)
Branch: 1 branch
BranchTrueFalseTotal
if (rx <= 0.0) 495 9 504
273 495 return qr_str_catf(s, "M%.7g %.7gh%.7gv%.7gh-%.7gz", x, y, w, h, w);
274 9 return qr_str_catf(s,
275 "M%.7g %.7gh%.7ga%.7g %.7g 0 0 1 %.7g %.7gv%.7g"
276 "a%.7g %.7g 0 0 1 -%.7g %.7gh-%.7ga%.7g %.7g 0 0 1 -%.7g -%.7g"
277 "v-%.7ga%.7g %.7g 0 0 1 %.7g -%.7gz",
278 9 x + rx, y, w - 2 * rx, rx, rx, rx, rx, h - 2 * rx,
279 9 rx, rx, rx, rx, w - 2 * rx, rx, rx, rx, rx,
280 9 h - 2 * rx, rx, rx, rx, rx);
281 }
282  
283 1632 static int qr_sub_circle(qr_str *s, double cx, double cy, double r)
284 {
285 1632 return qr_str_catf(s,
286 "M%.7g %.7ga%.7g %.7g 0 1 0 %.7g 0a%.7g %.7g 0 1 0 -%.7g 0z",
287 cx - r, cy, r, r, 2 * r, r, r, 2 * r);
288 }
289  
290 /* One module, rounded only on corners where both orthogonal neighbours
291 * are light, so runs of modules stay fused into solid bars and only the
292 * outline of each blob curves. Rounding every module individually opens
293 * a gap at every joint, which reads as contrast loss under blur. */
294 1122 static int qr_sub_cell_rounded(qr_str *s, const qr_matrix *q,
295 int r, int c, double x, double y, double rad)
296 {
297 1122 int size = q->size;
298 1122 const unsigned char *m = q->mod;
299 1122 ▶ int up = r > 0 && m[(r - 1) * size + c];
Branch: 2 branches
BranchTrueFalseTotal
int up = r > 0 && m[(r - 1) * size + c]; 1096 26 1122
int up = r > 0 && m[(r - 1) * size + c]; 534 562 1096
300 1122 ▶ int down = r < size - 1 && m[(r + 1) * size + c];
Branch: 2 branches
BranchTrueFalseTotal
int down = r < size - 1 && m[(r + 1) * size + c]; 1076 46 1122
int down = r < size - 1 && m[(r + 1) * size + c]; 534 542 1076
301 1122 ▶ int left = c > 0 && m[r * size + c - 1];
Branch: 2 branches
BranchTrueFalseTotal
int left = c > 0 && m[r * size + c - 1]; 1097 25 1122
int left = c > 0 && m[r * size + c - 1]; 523 574 1097
302 1122 ▶ int right = c < size - 1 && m[r * size + c + 1];
Branch: 2 branches
BranchTrueFalseTotal
int right = c < size - 1 && m[r * size + c + 1]; 1084 38 1122
int right = c < size - 1 && m[r * size + c + 1]; 523 561 1084
303 1122 ▶ int nw = !up && !left, ne = !up && !right;
Branch: 4 branches
BranchTrueFalseTotal
int nw = !up && !left, ne = !up && !right; 588 534 1122
int nw = !up && !left, ne = !up && !right; 307 281 588
int nw = !up && !left, ne = !up && !right; 588 534 1122
int nw = !up && !left, ne = !up && !right; 298 290 588
304 1122 ▶ int se = !down && !right, sw = !down && !left;
Branch: 4 branches
BranchTrueFalseTotal
int se = !down && !right, sw = !down && !left; 588 534 1122
int se = !down && !right, sw = !down && !left; 304 284 588
int se = !down && !right, sw = !down && !left; 588 534 1122
int se = !down && !right, sw = !down && !left; 299 289 588
305  
306 1122 ▶ if (qr_str_catf(s, "M%.7g %.7g", x + (nw ? rad : 0), y) < 0) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (qr_str_catf(s, "M%.7g %.7g", x + (nw ? rad : 0), y) < 0) return -1; 307 815 1122
if (qr_str_catf(s, "M%.7g %.7g", x + (nw ? rad : 0), y) < 0) return -1; 0 1122 1122
307 1122 ▶ if (qr_str_catf(s, "H%.7g", x + 1 - (ne ? rad : 0)) < 0) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (qr_str_catf(s, "H%.7g", x + 1 - (ne ? rad : 0)) < 0) return -1; 298 824 1122
if (qr_str_catf(s, "H%.7g", x + 1 - (ne ? rad : 0)) < 0) return -1; 0 1122 1122
308 1122 ▶ if (ne && qr_str_catf(s, "a%.7g %.7g 0 0 1 %.7g %.7g",
Branch: 2 branches
BranchTrueFalseTotal
if (ne && qr_str_catf(s, "a%.7g %.7g 0 0 1 %.7g %.7g", 298 824 1122
if (ne && qr_str_catf(s, "a%.7g %.7g 0 0 1 %.7g %.7g", 0 298 298
309 0 rad, rad, rad, rad) < 0) return -1;
310 1122 ▶ if (qr_str_catf(s, "V%.7g", y + 1 - (se ? rad : 0)) < 0) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (qr_str_catf(s, "V%.7g", y + 1 - (se ? rad : 0)) < 0) return -1; 304 818 1122
if (qr_str_catf(s, "V%.7g", y + 1 - (se ? rad : 0)) < 0) return -1; 0 1122 1122
311 1122 ▶ if (se && qr_str_catf(s, "a%.7g %.7g 0 0 1 -%.7g %.7g",
Branch: 2 branches
BranchTrueFalseTotal
if (se && qr_str_catf(s, "a%.7g %.7g 0 0 1 -%.7g %.7g", 304 818 1122
if (se && qr_str_catf(s, "a%.7g %.7g 0 0 1 -%.7g %.7g", 0 304 304
312 0 rad, rad, rad, rad) < 0) return -1;
313 1122 ▶ if (qr_str_catf(s, "H%.7g", x + (sw ? rad : 0)) < 0) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (qr_str_catf(s, "H%.7g", x + (sw ? rad : 0)) < 0) return -1; 299 823 1122
if (qr_str_catf(s, "H%.7g", x + (sw ? rad : 0)) < 0) return -1; 0 1122 1122
314 1122 ▶ if (sw && qr_str_catf(s, "a%.7g %.7g 0 0 1 -%.7g -%.7g",
Branch: 2 branches
BranchTrueFalseTotal
if (sw && qr_str_catf(s, "a%.7g %.7g 0 0 1 -%.7g -%.7g", 299 823 1122
if (sw && qr_str_catf(s, "a%.7g %.7g 0 0 1 -%.7g -%.7g", 0 299 299
315 0 rad, rad, rad, rad) < 0) return -1;
316 1122 ▶ if (qr_str_catf(s, "V%.7g", y + (nw ? rad : 0)) < 0) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (qr_str_catf(s, "V%.7g", y + (nw ? rad : 0)) < 0) return -1; 307 815 1122
if (qr_str_catf(s, "V%.7g", y + (nw ? rad : 0)) < 0) return -1; 0 1122 1122
317 1122 ▶ if (nw && qr_str_catf(s, "a%.7g %.7g 0 0 1 %.7g -%.7g",
Branch: 2 branches
BranchTrueFalseTotal
if (nw && qr_str_catf(s, "a%.7g %.7g 0 0 1 %.7g -%.7g", 307 815 1122
if (nw && qr_str_catf(s, "a%.7g %.7g 0 0 1 %.7g -%.7g", 0 307 307
318 0 rad, rad, rad, rad) < 0) return -1;
319 1122 return qr_str_catf(s, "z");
320 }
321  
322 /* --- the pieces ---------------------------------------------------------- */
323  
324 59807 static int qr_in_finder(int r, int c, int size)
325 {
326 115786 ▶ return (r < 7 && c < 7) || (r < 7 && c >= size - 7) ||
Branch: 4 branches
BranchTrueFalseTotal
return (r < 7 && c < 7) || (r < 7 && c >= size - 7) || 9991 49816 59807
return (r < 7 && c < 7) || (r < 7 && c >= size - 7) || 8077 1914 9991
return (r < 7 && c < 7) || (r < 7 && c >= size - 7) || 8077 49816 57893
return (r < 7 && c < 7) || (r < 7 && c >= size - 7) || 6163 1914 8077
327 55979 ▶ (r >= size - 7 && c < 7);
Branch: 2 branches
BranchTrueFalseTotal
(r >= size - 7 && c < 7); 10299 45680 55979
(r >= size - 7 && c < 7); 1914 8385 10299
328 }
329  
330 /* The data-and-function module path: everything dark except the three
331 * finders, which are drawn separately as solid shapes. Timing,
332 * alignment and format modules follow the module shape; they are
333 * sampled at their centres exactly as data modules are. */
334 58 static int qr_emit_modules(qr_str *s, const qr_matrix *q, int quiet,
335 const qr_style *st, const char *fill)
336 {
337 58 int size = q->size, r, c;
338 58 double rad = st->radius;
339  
340 58 ▶ if (qr_str_catf(s, "<path%s fill=\"%s\" d=\"",
Branch: 1 branch
BranchTrueFalseTotal
if (qr_str_catf(s, "<path%s fill=\"%s\" d=\"", 0 58 58
341 58 ▶ st->shape == QR_SHAPE_SQUARE
Branch: 1 branch
BranchTrueFalseTotal
st->shape == QR_SHAPE_SQUARE 48 10 58
342 ? " shape-rendering=\"crispEdges\"" : "",
343 fill) < 0)
344 0 return -1;
345  
346 2104 ▶ for (r = 0; r < size; r++) {
Branch: 1 branch
BranchTrueFalseTotal
for (r = 0; r < size; r++) { 2046 58 2104
347 68033 ▶ for (c = 0; c < size; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < size; c++) { 65987 2046 68033
348 65987 double x = c + quiet, y = r + quiet;
349  
350 65987 ▶ if (!q->mod[r * size + c] || qr_in_finder(r, c, size))
Branch: 2 branches
BranchTrueFalseTotal
if (!q->mod[r * size + c] || qr_in_finder(r, c, size)) 25991 39996 65987
if (!q->mod[r * size + c] || qr_in_finder(r, c, size)) 5742 20249 25991
351 45738 continue;
352  
353 20249 ▶ if (st->shape == QR_SHAPE_SQUARE) {
Branch: 1 branch
BranchTrueFalseTotal
if (st->shape == QR_SHAPE_SQUARE) { 17513 2736 20249
354 17513 int run = 0;
355 85145 ▶ while (c + run < size && q->mod[r * size + c + run] &&
Branch: 1 branch
BranchTrueFalseTotal
while (c + run < size && q->mod[r * size + c + run] && 50535 794 51329
356 33816 !qr_in_finder(r, c + run, size))
357 33816 run++;
358 17513 ▶ if (qr_str_catf(s, "M%d %dh%dv1h-%dz",
Branch: 1 branch
BranchTrueFalseTotal
if (qr_str_catf(s, "M%d %dh%dv1h-%dz", 0 17513 17513
359 c + quiet, r + quiet, run, run) < 0)
360 0 return -1;
361 17513 c += run - 1;
362 2736 ▶ } else if (st->shape == QR_SHAPE_DOT) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (st->shape == QR_SHAPE_DOT) { 1614 1122 2736
363 1614 ▶ if (qr_sub_circle(s, x + 0.5, y + 0.5, rad) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (qr_sub_circle(s, x + 0.5, y + 0.5, rad) < 0) 0 1614 1614
364 0 return -1;
365 } else {
366 1122 ▶ if (qr_sub_cell_rounded(s, q, r, c, x, y, rad) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (qr_sub_cell_rounded(s, q, r, c, x, y, rad) < 0) 0 1122 1122
367 0 return -1;
368 }
369 }
370 }
371 58 return qr_str_catf(s, "\"/>\n");
372 }
373  
374 /* One finder: a ring drawn with fill-rule evenodd so the 5x5 gap is a
375 * hole showing the ground, then the 3x3 pupil. Two elements, never 33
376 * modules. A horizontal line through the centre of the circle form
377 * reads exactly 1:1:3:1:1, which is the line the locator tests; the
378 * corners it rounds away are the part the ratio test never touches. */
379 174 static int qr_emit_finder(qr_str *s, int top, int left, int quiet,
380 const qr_style *st, const char *fill)
381 {
382 174 double x = left + quiet, y = top + quiet;
383 174 double cx = x + 3.5, cy = y + 3.5;
384 174 int sq = st->finder == QR_FINDER_SQUARE;
385  
386 174 ▶ if (qr_str_catf(s, "<path%s fill-rule=\"evenodd\" fill=\"%s\" d=\"",
Branch: 2 branches
BranchTrueFalseTotal
if (qr_str_catf(s, "<path%s fill-rule=\"evenodd\" fill=\"%s\" d=\"", 165 9 174
if (qr_str_catf(s, "<path%s fill-rule=\"evenodd\" fill=\"%s\" d=\"", 0 174 174
387 sq ? " shape-rendering=\"crispEdges\"" : "", fill) < 0)
388 0 return -1;
389 174 ▶ if (st->finder == QR_FINDER_CIRCLE) {
Branch: 1 branch
BranchTrueFalseTotal
if (st->finder == QR_FINDER_CIRCLE) { 6 168 174
390 6 ▶ if (qr_sub_circle(s, cx, cy, 3.5) < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (qr_sub_circle(s, cx, cy, 3.5) < 0) return -1; 0 6 6
391 6 ▶ if (qr_sub_circle(s, cx, cy, 2.5) < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (qr_sub_circle(s, cx, cy, 2.5) < 0) return -1; 0 6 6
392 } else {
393 168 ▶ double rx = sq ? 0.0 : 0.3;
Branch: 1 branch
BranchTrueFalseTotal
double rx = sq ? 0.0 : 0.3; 165 3 168
394 168 ▶ if (qr_sub_rrect(s, x, y, 7, 7, rx * 7) < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (qr_sub_rrect(s, x, y, 7, 7, rx * 7) < 0) return -1; 0 168 168
395 168 ▶ if (qr_sub_rrect(s, x + 1, y + 1, 5, 5, rx * 5) < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (qr_sub_rrect(s, x + 1, y + 1, 5, 5, rx * 5) < 0) return -1; 0 168 168
396 }
397 174 ▶ if (qr_str_catf(s, "\"/>\n<path%s fill=\"%s\" d=\"",
Branch: 2 branches
BranchTrueFalseTotal
if (qr_str_catf(s, "\"/>\n<path%s fill=\"%s\" d=\"", 165 9 174
if (qr_str_catf(s, "\"/>\n<path%s fill=\"%s\" d=\"", 0 174 174
398 sq ? " shape-rendering=\"crispEdges\"" : "", fill) < 0)
399 0 return -1;
400 174 ▶ if (st->finder == QR_FINDER_CIRCLE) {
Branch: 1 branch
BranchTrueFalseTotal
if (st->finder == QR_FINDER_CIRCLE) { 6 168 174
401 6 ▶ if (qr_sub_circle(s, cx, cy, 1.5) < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (qr_sub_circle(s, cx, cy, 1.5) < 0) return -1; 0 6 6
402 } else {
403 168 ▶ if (qr_sub_rrect(s, x + 2, y + 2, 3, 3,
Branch: 1 branch
BranchTrueFalseTotal
if (qr_sub_rrect(s, x + 2, y + 2, 3, 3, 0 168 168
404 168 ▶ (sq ? 0.0 : 0.3) * 3) < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
(sq ? 0.0 : 0.3) * 3) < 0) return -1; 165 3 168
405 }
406 174 return qr_str_catf(s, "\"/>\n");
407 }
408  
409 /* --- the logo box -------------------------------------------------------- */
410  
411 /* Text wants a box wider than it is tall, and that matters beyond
412 * looks: alignment patterns sit on a diagonal grid, and a short wide
413 * box slips between rows of them where a square box of the same area
414 * does not. An image's box follows its aspect ratio, so a wide
415 * wordmark collects the same advantage. */
416 24 static int qr_logo_box(const qr_matrix *q, const qr_logo *lg,
417 double *bw, double *bh,
418 double *aw, double *ah, /* art size inside the pad */
419 char *err, size_t errlen)
420 {
421 24 int size = q->size;
422 24 ▶ double scale = lg->scale > 0 ? lg->scale : 0.17;
Branch: 1 branch
BranchTrueFalseTotal
double scale = lg->scale > 0 ? lg->scale : 0.17; 0 24 24
423 24 double side = size * scale;
424 24 double aspect = 1.0;
425  
426 24 ▶ if (lg->kind == QR_LOGO_TEXT) {
Branch: 1 branch
BranchTrueFalseTotal
if (lg->kind == QR_LOGO_TEXT) { 14 10 24
427 14 ▶ double em = lg->em > 0 ? lg->em : size * 0.105;
Branch: 1 branch
BranchTrueFalseTotal
double em = lg->em > 0 ? lg->em : size * 0.105; 0 14 14
428 /* a generic bold sans averages a little over half an em per
429 * character; the box is padded and the text centred inside it */
430 14 *aw = em * (0.62 * (double)lg->text_len + 0.85);
431 14 *ah = em * 1.75;
432 14 *bw = *aw;
433 14 *bh = *ah;
434 14 return 0;
435 }
436  
437 10 ▶ if (lg->kind == QR_LOGO_IMAGE) {
Branch: 1 branch
BranchTrueFalseTotal
if (lg->kind == QR_LOGO_IMAGE) { 5 5 10
438 unsigned long w, h;
439 5 ▶ if (qr_img_dims(lg->img, lg->img_len, lg->img_fmt, &w, &h) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (qr_img_dims(lg->img, lg->img_len, lg->img_fmt, &w, &h) < 0) { 0 5 5
440 0 snprintf(err, errlen, "logo image bytes are truncated or "
441 "malformed; no pixel dimensions found");
442 0 return -1;
443 }
444 5 aspect = (double)w / (double)h;
445 5 ▶ } else if (lg->kind == QR_LOGO_SVG) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (lg->kind == QR_LOGO_SVG) { 5 0 5
446 /* aspect from the root viewBox when there is one */
447 5 const char *vb = NULL, *end = lg->markup + lg->markup_len;
448 5 const char *p = lg->markup;
449 71 ▶ while (p + 8 < end && !vb) {
Branch: 2 branches
BranchTrueFalseTotal
while (p + 8 < end && !vb) { 69 2 71
while (p + 8 < end && !vb) { 66 3 69
450 66 ▶ if (memcmp(p, "viewBox=\"", 9) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (memcmp(p, "viewBox=\"", 9) == 0) 3 63 66
451 3 vb = p + 9;
452 66 p++;
453 }
454 5 ▶ if (vb) {
Branch: 1 branch
BranchTrueFalseTotal
if (vb) { 3 2 5
455 double a, b, c, d;
456 3 ▶ if (sscanf(vb, "%lf %lf %lf %lf", &a, &b, &c, &d) == 4 &&
Branch: 1 branch
BranchTrueFalseTotal
if (sscanf(vb, "%lf %lf %lf %lf", &a, &b, &c, &d) == 4 && 3 0 3
457 3 ▶ c > 0 && d > 0)
Branch: 2 branches
BranchTrueFalseTotal
c > 0 && d > 0) 3 0 3
c > 0 && d > 0) 3 0 3
458 3 aspect = c / d;
459 }
460 }
461  
462 10 ▶ if (aspect >= 1.0) {
Branch: 1 branch
BranchTrueFalseTotal
if (aspect >= 1.0) { 8 2 10
463 8 *aw = side;
464 8 *ah = side / aspect;
465 } else {
466 2 *ah = side;
467 2 *aw = side * aspect;
468 }
469 10 *bw = *aw * 1.22;
470 10 *bh = *ah * 1.22;
471 10 return 0;
472 }
473  
474 11 static int qr_xml_escape(qr_str *s, const char *p, size_t n)
475 {
476 size_t i;
477 51 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 40 11 51
478 int rc;
479 40 switch (p[i]) {
480 2 case '&': rc = qr_str_catf(s, "&amp;"); break;
481 2 case '<': rc = qr_str_catf(s, "&lt;"); break;
482 2 case '>': rc = qr_str_catf(s, "&gt;"); break;
483 34 default: rc = qr_str_catn(s, p + i, 1); break;
484 }
485 40 ▶ if (rc < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (rc < 0) 0 40 40
486 0 return -1;
487 }
488 11 return 0;
489 }
490  
491 21 static int qr_emit_logo(qr_str *s, const qr_matrix *q, int quiet,
492 const qr_style *st, const qr_logo *lg,
493 double bw, double bh, double aw, double ah)
494 {
495 21 double cx = quiet + q->size / 2.0, cy = quiet + q->size / 2.0;
496  
497 /* The knockout pad. It takes the light colour, and when the ground
498 * is transparent it stays an opaque white pad: the logo needs a
499 * ground, and modules showing through are modules the overlap
500 * budget never counted. */
501 42 ▶ if (qr_str_catf(s,
Branch: 1 branch
BranchTrueFalseTotal
if (qr_str_catf(s, 0 21 21
502 "<rect x=\"%.3f\" y=\"%.3f\" width=\"%.3f\" height=\"%.3f\" "
503 "rx=\"%.3f\" fill=\"%s\"/>\n",
504 21 cx - bw / 2, cy - bh / 2, bw, bh, bh * 0.22,
505 21 ▶ strcmp(st->light, "none") == 0 ? "#ffffff" : st->light) < 0)
Branch: 1 branch
BranchTrueFalseTotal
strcmp(st->light, "none") == 0 ? "#ffffff" : st->light) < 0) 20 1 21
506 0 return -1;
507  
508 21 ▶ if (lg->kind == QR_LOGO_TEXT) {
Branch: 1 branch
BranchTrueFalseTotal
if (lg->kind == QR_LOGO_TEXT) { 11 10 21
509 11 ▶ double em = lg->em > 0 ? lg->em : q->size * 0.105;
Branch: 1 branch
BranchTrueFalseTotal
double em = lg->em > 0 ? lg->em : q->size * 0.105; 0 11 11
510 /* dy rather than dominant-baseline, which renderers honour
511 * inconsistently; half a line low is modules not budgeted for */
512 11 ▶ if (qr_str_catf(s,
Branch: 1 branch
BranchTrueFalseTotal
if (qr_str_catf(s, 0 11 11
513 "<text x=\"%.3f\" y=\"%.3f\" dy=\"%.3f\" "
514 "text-anchor=\"middle\" "
515 "font-family=\"Helvetica,Arial,sans-serif\" "
516 "font-weight=\"700\" font-size=\"%.3f\" fill=\"%s\">",
517 11 cx, cy, em * 0.35, em, st->dark) < 0)
518 0 return -1;
519 11 ▶ if (qr_xml_escape(s, lg->text, lg->text_len) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (qr_xml_escape(s, lg->text, lg->text_len) < 0) 0 11 11
520 0 return -1;
521 11 return qr_str_catf(s, "</text>\n");
522 }
523  
524 10 ▶ if (lg->kind == QR_LOGO_IMAGE) {
Branch: 1 branch
BranchTrueFalseTotal
if (lg->kind == QR_LOGO_IMAGE) { 5 5 10
525 5 size_t b64 = qr_b64_len(lg->img_len);
526 5 char *enc = (char *)malloc(b64 + 1);
527 int rc;
528  
529 5 ▶ if (!enc)
Branch: 1 branch
BranchTrueFalseTotal
if (!enc) 0 5 5
530 0 return -1;
531 5 qr_b64_encode(lg->img, lg->img_len, enc);
532 5 rc = qr_str_catf(s,
533 "<image x=\"%.3f\" y=\"%.3f\" width=\"%.3f\" height=\"%.3f\" "
534 "preserveAspectRatio=\"xMidYMid meet\" "
535 "href=\"data:image/%s;base64,%s\"/>\n",
536 5 cx - aw / 2, cy - ah / 2, aw, ah,
537 5 ▶ lg->img_fmt == QR_IMG_PNG ? "png" : "jpeg", enc);
Branch: 1 branch
BranchTrueFalseTotal
lg->img_fmt == QR_IMG_PNG ? "png" : "jpeg", enc); 2 3 5
538 5 free(enc);
539 5 return rc;
540 }
541  
542 /* SVG markup. A document with an <svg> root nests: its content is
543 * lifted into an inner <svg> with the original viewBox, so it
544 * scales with the symbol and needs no encoding. A rootless
545 * fragment is assumed to live in a 100x100 box. */
546 {
547 5 const char *p = lg->markup, *end = p + lg->markup_len;
548 5 const char *root = NULL, *open_end = NULL, *close = NULL;
549 const char *scan;
550  
551 53 ▶ for (scan = p; scan + 4 <= end; scan++) {
Branch: 1 branch
BranchTrueFalseTotal
for (scan = p; scan + 4 <= end; scan++) { 51 2 53
552 51 ▶ if (memcmp(scan, "<svg", 4) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (memcmp(scan, "<svg", 4) == 0) { 3 48 51
553 3 root = scan;
554 3 break;
555 }
556 }
557 5 ▶ if (root) {
Branch: 1 branch
BranchTrueFalseTotal
if (root) { 3 2 5
558 87 ▶ for (scan = root; scan < end; scan++)
Branch: 1 branch
BranchTrueFalseTotal
for (scan = root; scan < end; scan++) 87 0 87
559 87 ▶ if (*scan == '>') { open_end = scan + 1; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (*scan == '>') { open_end = scan + 1; break; } 3 84 87
560 3 ▶ for (scan = end - 6; scan >= open_end; scan--)
Branch: 1 branch
BranchTrueFalseTotal
for (scan = end - 6; scan >= open_end; scan--) 3 0 3
561 3 ▶ if (memcmp(scan, "</svg", 5) == 0) { close = scan; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (memcmp(scan, "</svg", 5) == 0) { close = scan; break; } 3 0 3
562 }
563 5 ▶ if (root && open_end && close) {
Branch: 3 branches
BranchTrueFalseTotal
if (root && open_end && close) { 3 2 5
if (root && open_end && close) { 3 0 3
if (root && open_end && close) { 3 0 3
564 3 const char *vb = NULL;
565 28 ▶ for (scan = root; scan + 9 < open_end; scan++)
Branch: 1 branch
BranchTrueFalseTotal
for (scan = root; scan + 9 < open_end; scan++) 28 0 28
566 28 ▶ if (memcmp(scan, "viewBox=\"", 9) == 0) { vb = scan; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (memcmp(scan, "viewBox=\"", 9) == 0) { vb = scan; break; } 3 25 28
567 3 ▶ if (qr_str_catf(s,
Branch: 1 branch
BranchTrueFalseTotal
if (qr_str_catf(s, 0 3 3
568 "<svg x=\"%.3f\" y=\"%.3f\" width=\"%.3f\" "
569 "height=\"%.3f\" preserveAspectRatio=\"xMidYMid meet\"",
570 3 cx - aw / 2, cy - ah / 2, aw, ah) < 0)
571 0 return -1;
572 3 ▶ if (vb) {
Branch: 1 branch
BranchTrueFalseTotal
if (vb) { 3 0 3
573 3 const char *q2 = vb + 9;
574 32 ▶ while (q2 < open_end && *q2 != '"')
Branch: 2 branches
BranchTrueFalseTotal
while (q2 < open_end && *q2 != '"') 32 0 32
while (q2 < open_end && *q2 != '"') 29 3 32
575 29 q2++;
576 3 ▶ if (qr_str_catf(s, " viewBox=\"") < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (qr_str_catf(s, " viewBox=\"") < 0) 0 3 3
577 0 return -1;
578 3 ▶ if (qr_str_catn(s, vb + 9, (size_t)(q2 - vb - 9)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (qr_str_catn(s, vb + 9, (size_t)(q2 - vb - 9)) < 0) 0 3 3
579 0 return -1;
580 3 ▶ if (qr_str_catf(s, "\"") < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (qr_str_catf(s, "\"") < 0) 0 3 3
581 0 return -1;
582 } else {
583 0 ▶ if (qr_str_catf(s, " viewBox=\"0 0 100 100\"") < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (qr_str_catf(s, " viewBox=\"0 0 100 100\"") < 0) 0 0 0
584 0 return -1;
585 }
586 3 ▶ if (qr_str_catf(s, ">") < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (qr_str_catf(s, ">") < 0) 0 3 3
587 0 return -1;
588 3 ▶ if (qr_str_catn(s, open_end, (size_t)(close - open_end)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (qr_str_catn(s, open_end, (size_t)(close - open_end)) < 0) 0 3 3
589 0 return -1;
590 3 return qr_str_catf(s, "</svg>\n");
591 }
592  
593 2 ▶ if (qr_str_catf(s,
Branch: 1 branch
BranchTrueFalseTotal
if (qr_str_catf(s, 0 2 2
594 "<g transform=\"translate(%.4f %.4f) scale(%.6f)\">",
595 2 cx - aw / 2, cy - ah / 2, aw / 100.0) < 0)
596 0 return -1;
597 2 ▶ if (qr_str_catn(s, p, lg->markup_len) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (qr_str_catn(s, p, lg->markup_len) < 0) 0 2 2
598 0 return -1;
599 2 return qr_str_catf(s, "</g>\n");
600 }
601 }
602  
603 /* --- validation ---------------------------------------------------------- */
604  
605 /* Normalises colours in place and runs every contrast check. The
606 * gradient stops each face the same check a flat dark does; the worst
607 * stop is what gates, so all of them are tested. */
608 81 static int qr_style_check(qr_style *st, char *err, size_t errlen)
609 {
610 81 double dl, ll = 1.0, fl, sl;
611 char norm[8];
612 81 int transparent = strcmp(st->light, "none") == 0;
613 int i;
614  
615 81 ▶ if (qr_color_check("style dark", st->dark, strlen(st->dark),
Branch: 1 branch
BranchTrueFalseTotal
if (qr_color_check("style dark", st->dark, strlen(st->dark), 5 76 81
616 norm, &dl, err, errlen) < 0)
617 5 return -1;
618 76 memcpy(st->dark, norm, 8);
619  
620 76 ▶ if (!transparent) {
Branch: 1 branch
BranchTrueFalseTotal
if (!transparent) { 72 4 76
621 72 ▶ if (qr_color_check("style light", st->light, strlen(st->light),
Branch: 1 branch
BranchTrueFalseTotal
if (qr_color_check("style light", st->light, strlen(st->light), 1 71 72
622 norm, &ll, err, errlen) < 0)
623 1 return -1;
624 71 memcpy(st->light, norm, 8);
625 71 ▶ if (qr_contrast_check("dark", st->dark, dl, ll, err, errlen) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (qr_contrast_check("dark", st->dark, dl, ll, err, errlen) < 0) 5 66 71
626 5 return -1;
627 }
628  
629 70 ▶ if (st->finder_dark[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (st->finder_dark[0]) { 2 68 70
630 2 ▶ if (qr_color_check("style finder_dark", st->finder_dark,
Branch: 1 branch
BranchTrueFalseTotal
if (qr_color_check("style finder_dark", st->finder_dark, 0 2 2
631 2 strlen(st->finder_dark), norm, &fl,
632 err, errlen) < 0)
633 0 return -1;
634 2 memcpy(st->finder_dark, norm, 8);
635 4 if (!transparent &&
636 2 qr_contrast_check("finder_dark", st->finder_dark, fl, ll,
637 err, errlen) < 0)
638 1 return -1;
639 }
640  
641 82 ▶ for (i = 0; i < st->nstops; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < st->nstops; i++) { 16 66 82
642 char what[32];
643 16 snprintf(what, sizeof what, "gradient stop %d", i + 1);
644 16 ▶ if (qr_color_check(what, st->stops[i], strlen(st->stops[i]),
Branch: 1 branch
BranchTrueFalseTotal
if (qr_color_check(what, st->stops[i], strlen(st->stops[i]), 1 15 16
645 norm, &sl, err, errlen) < 0)
646 3 return -1;
647 15 memcpy(st->stops[i], norm, 8);
648 30 if (!transparent &&
649 15 qr_contrast_check(what, st->stops[i], sl, ll, err, errlen) < 0)
650 2 return -1;
651 }
652  
653 66 ▶ if (st->shape == QR_SHAPE_DOT) {
Branch: 1 branch
BranchTrueFalseTotal
if (st->shape == QR_SHAPE_DOT) { 10 56 66
654 10 ▶ if (st->radius <= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (st->radius <= 0) 4 6 10
655 4 st->radius = 0.5;
656 10 ▶ if (st->radius > 0.5) {
Branch: 1 branch
BranchTrueFalseTotal
if (st->radius > 0.5) { 2 8 10
657 2 snprintf(err, errlen, "dot radius %.2f overlaps neighbouring "
658 "modules; the maximum is 0.5", st->radius);
659 2 return -1;
660 }
661 8 ▶ if (st->radius < QR_DOT_RADIUS_FLOOR) {
Branch: 1 branch
BranchTrueFalseTotal
if (st->radius < QR_DOT_RADIUS_FLOOR) { 2 6 8
662 2 snprintf(err, errlen, "dot radius %.2f is below the floor of "
663 "%.2f; smaller dots stop scanning under blur",
664 st->radius, QR_DOT_RADIUS_FLOOR);
665 2 return -1;
666 }
667 56 ▶ } else if (st->shape == QR_SHAPE_ROUNDED) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (st->shape == QR_SHAPE_ROUNDED) { 5 51 56
668 5 ▶ if (st->radius <= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (st->radius <= 0) 4 1 5
669 4 st->radius = 0.3;
670 5 ▶ if (st->radius > 0.5) {
Branch: 1 branch
BranchTrueFalseTotal
if (st->radius > 0.5) { 1 4 5
671 1 snprintf(err, errlen, "corner radius %.2f exceeds half a "
672 "module; the maximum is 0.5", st->radius);
673 1 return -1;
674 }
675 }
676  
677 61 return 0;
678 }
679  
680 /* --- the renderer -------------------------------------------------------- */
681  
682 /* Serialise a matrix. Returns a malloc'd SVG document, or NULL with
683 * err filled in (or empty on allocation failure). The caller frees. */
684 81 static char *qr_svg_render(const qr_matrix *q, int quiet,
685 const qr_style *st_in, const qr_logo *lg,
686 qr_svg_info *info, char *err, size_t errlen)
687 {
688 qr_str s;
689 81 qr_style st = *st_in;
690 81 int size = q->size, span = size + 2 * quiet;
691 const char *module_fill;
692 const char *finder_fill;
693 81 double bw = 0, bh = 0, aw = 0, ah = 0;
694  
695 81 err[0] = '\0';
696 81 memset(&s, 0, sizeof s);
697 81 ▶ if (info) {
Branch: 1 branch
BranchTrueFalseTotal
if (info) { 79 2 81
698 79 memset(info, 0, sizeof *info);
699 79 info->version = q->version;
700 79 info->ecc = q->ecc;
701 79 info->mask = q->mask;
702 79 info->size = size;
703 }
704  
705 81 ▶ if (qr_style_check(&st, err, errlen) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (qr_style_check(&st, err, errlen) < 0) 20 61 81
706 20 return NULL;
707 61 ▶ finder_fill = st.finder_dark[0] ? st.finder_dark : st.dark;
Branch: 1 branch
BranchTrueFalseTotal
finder_fill = st.finder_dark[0] ? st.finder_dark : st.dark; 1 60 61
708 61 ▶ module_fill = st.nstops ? "url(#qg)" : st.dark;
Branch: 1 branch
BranchTrueFalseTotal
module_fill = st.nstops ? "url(#qg)" : st.dark; 56 5 61
709  
710 /* the logo box, clamped to the data region before anything renders:
711 * rows and columns 8 to size-9, so a wide word cannot reach the
712 * timing patterns or the format block */
713 61 ▶ if (lg && lg->kind != QR_LOGO_NONE) {
Branch: 2 branches
BranchTrueFalseTotal
if (lg && lg->kind != QR_LOGO_NONE) { 61 0 61
if (lg && lg->kind != QR_LOGO_NONE) { 24 37 61
714 double half;
715  
716 24 ▶ if (qr_logo_box(q, lg, &bw, &bh, &aw, &ah, err, errlen) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (qr_logo_box(q, lg, &bw, &bh, &aw, &ah, err, errlen) < 0) 0 24 24
717 0 return NULL;
718 24 half = size / 2.0;
719 24 ▶ if (half - bw / 2 < 8 || half + bw / 2 > size - 8 ||
Branch: 2 branches
BranchTrueFalseTotal
if (half - bw / 2 < 8 || half + bw / 2 > size - 8 || 21 3 24
if (half - bw / 2 < 8 || half + bw / 2 > size - 8 || 21 0 21
720 21 ▶ half - bh / 2 < 8 || half + bh / 2 > size - 8) {
Branch: 2 branches
BranchTrueFalseTotal
half - bh / 2 < 8 || half + bh / 2 > size - 8) { 21 0 21
half - bh / 2 < 8 || half + bh / 2 > size - 8) { 0 21 21
721 3 snprintf(err, errlen,
722 "logo box %.1f x %.1f modules exceeds the data region of "
723 "the version %d symbol (%d x %d); shorten the text, lower "
724 "the scale, or raise the version",
725 3 bw, bh, q->version, size, size);
726 3 return NULL;
727 }
728  
729 21 ▶ if (info) {
Branch: 1 branch
BranchTrueFalseTotal
if (info) { 21 0 21
730 21 int r, c, hits = 0;
731 21 size_t hl = 0;
732  
733 21 info->bx = half - bw / 2;
734 21 info->by = half - bh / 2;
735 21 info->bw = bw;
736 21 info->bh = bh;
737 1094 ▶ for (r = 0; r < size; r++)
Branch: 1 branch
BranchTrueFalseTotal
for (r = 0; r < size; r++) 1073 21 1094
738 57038 ▶ for (c = 0; c < size; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < size; c++) { 55965 1073 57038
739 55965 double mx = c + 0.5, my = r + 0.5;
740 55965 ▶ if (mx < half - bw / 2 || mx > half + bw / 2 ||
Branch: 2 branches
BranchTrueFalseTotal
if (mx < half - bw / 2 || mx > half + bw / 2 || 35317 20648 55965
if (mx < half - bw / 2 || mx > half + bw / 2 || 14669 20648 35317
741 14669 ▶ my < half - bh / 2 || my > half + bh / 2)
Branch: 2 branches
BranchTrueFalseTotal
my < half - bh / 2 || my > half + bh / 2) 8571 6098 14669
my < half - bh / 2 || my > half + bh / 2) 6098 2473 8571
742 53492 continue;
743 2473 info->covered++;
744 2473 ▶ if (q->fixed[r * size + c]) {
Branch: 1 branch
BranchTrueFalseTotal
if (q->fixed[r * size + c]) { 465 2008 2473
745 465 info->fn_hit++;
746 465 ▶ if (hits < 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (hits < 4) { 76 389 465
747 76 ▶ int n = snprintf(info->hits + hl,
Branch: 1 branch
BranchTrueFalseTotal
int n = snprintf(info->hits + hl, 57 19 76
748 sizeof info->hits - hl,
749 "%s%d,%d", hits ? " " : "",
750 r, c);
751 76 ▶ if (n > 0 && hl + (size_t)n < sizeof info->hits)
Branch: 2 branches
BranchTrueFalseTotal
if (n > 0 && hl + (size_t)n < sizeof info->hits) 76 0 76
if (n > 0 && hl + (size_t)n < sizeof info->hits) 76 0 76
752 76 hl += (size_t)n;
753 76 hits++;
754 }
755 }
756 }
757 }
758 }
759  
760 58 ▶ if (qr_str_catf(&s,
Branch: 1 branch
BranchTrueFalseTotal
if (qr_str_catf(&s, 0 58 58
761 "<svg xmlns=\"http://www.w3.org/2000/svg\" "
762 "viewBox=\"0 0 %d %d\" width=\"%d\" height=\"%d\">\n",
763 span, span, span * 10, span * 10) < 0)
764 0 goto oom;
765  
766 58 ▶ if (st.nstops) {
Branch: 1 branch
BranchTrueFalseTotal
if (st.nstops) { 5 53 58
767 int i;
768 5 ▶ if (st.grad_radial) {
Branch: 1 branch
BranchTrueFalseTotal
if (st.grad_radial) { 2 3 5
769 2 ▶ if (qr_str_catf(&s,
Branch: 1 branch
BranchTrueFalseTotal
if (qr_str_catf(&s, 0 2 2
770 "<defs><radialGradient id=\"qg\" cx=\"0.5\" cy=\"0.5\" "
771 "r=\"0.7071\">") < 0)
772 0 goto oom;
773 } else {
774 3 double a = st.grad_angle * (3.14159265358979323846 / 180.0);
775 3 double dx = cos(a) / 2, dy = sin(a) / 2;
776 3 ▶ if (qr_str_catf(&s,
Branch: 1 branch
BranchTrueFalseTotal
if (qr_str_catf(&s, 0 3 3
777 "<defs><linearGradient id=\"qg\" "
778 "x1=\"%.4f\" y1=\"%.4f\" x2=\"%.4f\" y2=\"%.4f\">",
779 0.5 - dx, 0.5 - dy, 0.5 + dx, 0.5 + dy) < 0)
780 0 goto oom;
781 }
782 16 ▶ for (i = 0; i < st.nstops; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < st.nstops; i++) 11 5 16
783 11 ▶ if (qr_str_catf(&s,
Branch: 1 branch
BranchTrueFalseTotal
if (qr_str_catf(&s, 0 11 11
784 "<stop offset=\"%.4f\" stop-color=\"%s\"/>",
785 11 st.nstops == 1 ? 0.0 : (double)i / (st.nstops - 1),
786 11 ▶ st.stops[i]) < 0)
Branch: 1 branch
BranchTrueFalseTotal
st.stops[i]) < 0) 11 0 11
787 0 goto oom;
788 5 ▶ if (qr_str_catf(&s, st.grad_radial
Branch: 2 branches
BranchTrueFalseTotal
if (qr_str_catf(&s, st.grad_radial 2 3 5
if (qr_str_catf(&s, st.grad_radial 0 5 5
789 ? "</radialGradient></defs>\n"
790 : "</linearGradient></defs>\n") < 0)
791 0 goto oom;
792 }
793  
794 58 ▶ if (strcmp(st.light, "none") != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (strcmp(st.light, "none") != 0) 54 4 58
795 54 ▶ if (qr_str_catf(&s,
Branch: 1 branch
BranchTrueFalseTotal
if (qr_str_catf(&s, 0 54 54
796 "<rect width=\"%d\" height=\"%d\" fill=\"%s\"/>\n",
797 span, span, st.light) < 0)
798 0 goto oom;
799  
800 58 ▶ if (qr_emit_modules(&s, q, quiet, &st, module_fill) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (qr_emit_modules(&s, q, quiet, &st, module_fill) < 0) 0 58 58
801 0 goto oom;
802  
803 116 if (qr_emit_finder(&s, 0, 0, quiet, &st, finder_fill) < 0 ||
804 116 ▶ qr_emit_finder(&s, 0, size - 7, quiet, &st, finder_fill) < 0 ||
Branch: 1 branch
BranchTrueFalseTotal
qr_emit_finder(&s, 0, size - 7, quiet, &st, finder_fill) < 0 || 0 58 58
805 58 qr_emit_finder(&s, size - 7, 0, quiet, &st, finder_fill) < 0)
806 0 goto oom;
807  
808 58 ▶ if (lg && lg->kind != QR_LOGO_NONE) {
Branch: 2 branches
BranchTrueFalseTotal
if (lg && lg->kind != QR_LOGO_NONE) { 58 0 58
if (lg && lg->kind != QR_LOGO_NONE) { 21 37 58
809 21 ▶ if (qr_emit_logo(&s, q, quiet, &st, lg, bw, bh, aw, ah) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (qr_emit_logo(&s, q, quiet, &st, lg, bw, bh, aw, ah) < 0) 0 21 21
810 0 goto oom;
811 }
812  
813 58 ▶ if (qr_str_catf(&s, "</svg>\n") < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (qr_str_catf(&s, "</svg>\n") < 0) 0 58 58
814 0 goto oom;
815  
816 58 return s.p;
817  
818 0 oom:
819 0 free(s.p);
820 0 ▶ if (!err[0])
Branch: 1 branch
BranchTrueFalseTotal
if (!err[0]) 0 0 0
821 0 err[0] = '\0';
822 0 return NULL;
823 }
824  
825 #endif /* QR_SVG_H */
« Code.xs ↑ Summary include/qr/qr_bits.h »