/src/mupdf/source/svg/svg-parse.c
Line | Count | Source |
1 | | // Copyright (C) 2004-2026 Artifex Software, Inc. |
2 | | // |
3 | | // This file is part of MuPDF. |
4 | | // |
5 | | // MuPDF is free software: you can redistribute it and/or modify it under the |
6 | | // terms of the GNU Affero General Public License as published by the Free |
7 | | // Software Foundation, either version 3 of the License, or (at your option) |
8 | | // any later version. |
9 | | // |
10 | | // MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY |
11 | | // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
12 | | // FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more |
13 | | // details. |
14 | | // |
15 | | // You should have received a copy of the GNU Affero General Public License |
16 | | // along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html> |
17 | | // |
18 | | // Alternative licensing terms are available from the licensor. |
19 | | // For commercial licensing, see <https://www.artifex.com/> or contact |
20 | | // Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco, |
21 | | // CA 94129, USA, for further information. |
22 | | |
23 | | #include "mupdf/fitz.h" |
24 | | #include "svg-imp.h" |
25 | | |
26 | | #include <string.h> |
27 | | #include <math.h> |
28 | | |
29 | | int svg_is_whitespace_or_comma(int c) |
30 | 0 | { |
31 | 0 | return (c == 0x20) || (c == 0x9) || (c == 0xD) || (c == 0xA) || (c == ','); |
32 | 0 | } |
33 | | |
34 | | int svg_is_whitespace(int c) |
35 | 0 | { |
36 | 0 | return (c == 0x20) || (c == 0x9) || (c == 0xD) || (c == 0xA); |
37 | 0 | } |
38 | | |
39 | | int svg_is_alpha(int c) |
40 | 0 | { |
41 | 0 | return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'); |
42 | 0 | } |
43 | | |
44 | | int svg_is_digit(int c) |
45 | 0 | { |
46 | 0 | return (c >= '0' && c <= '9') || |
47 | 0 | (c == 'e') || (c == 'E') || |
48 | 0 | (c == '+') || (c == '-') || (c == '.'); |
49 | 0 | } |
50 | | |
51 | | const char * |
52 | | svg_lex_number(float *fp, const char *ss) |
53 | 0 | { |
54 | 0 | const char *s = ss; |
55 | 0 | if (*s == '+' || *s == '-') |
56 | 0 | ++s; |
57 | 0 | while (*s >= '0' && *s <= '9') |
58 | 0 | ++s; |
59 | 0 | if (*s == '.') { |
60 | 0 | ++s; |
61 | 0 | while (*s >= '0' && *s <= '9') |
62 | 0 | ++s; |
63 | 0 | } |
64 | 0 | if (*s == 'e' || *s == 'E') { |
65 | 0 | ++s; |
66 | 0 | if (*s == '+' || *s == '-') |
67 | 0 | ++s; |
68 | 0 | while (*s >= '0' && *s <= '9') |
69 | 0 | ++s; |
70 | 0 | } |
71 | 0 | *fp = fz_atof(ss); |
72 | 0 | return s; |
73 | 0 | } |
74 | | |
75 | | float |
76 | | svg_parse_number(const char *str, float min, float max, float inherit) |
77 | 0 | { |
78 | 0 | float x; |
79 | 0 | if (!strcmp(str, "inherit")) |
80 | 0 | return inherit; |
81 | 0 | x = fz_atof(str); |
82 | 0 | if (x < min) return min; |
83 | 0 | if (x > max) return max; |
84 | 0 | return x; |
85 | 0 | } |
86 | | |
87 | | float |
88 | | svg_parse_length(const char *str, float percent, float font_size) |
89 | 0 | { |
90 | 0 | char *end; |
91 | 0 | float val; |
92 | |
|
93 | 0 | val = fz_strtof(str, &end); |
94 | 0 | if (end == str) |
95 | 0 | return 0; /* failed */ |
96 | | |
97 | 0 | if (!strcmp(end, "px")) return val; |
98 | | |
99 | 0 | if (!strcmp(end, "pt")) return val * 1.0f; |
100 | 0 | if (!strcmp(end, "pc")) return val * 12.0f; |
101 | 0 | if (!strcmp(end, "mm")) return val * 2.83464567f; |
102 | 0 | if (!strcmp(end, "cm")) return val * 28.3464567f; |
103 | 0 | if (!strcmp(end, "in")) return val * 72.0f; |
104 | | |
105 | 0 | if (!strcmp(end, "em")) return val * font_size; |
106 | 0 | if (!strcmp(end, "ex")) return val * font_size * 0.5f; |
107 | | |
108 | 0 | if (!strcmp(end, "%")) |
109 | 0 | return val * percent * 0.01f; |
110 | | |
111 | 0 | if (end[0] == 0) |
112 | 0 | return val; |
113 | | |
114 | 0 | return 0; |
115 | 0 | } |
116 | | |
117 | | /* Return angle in degrees */ |
118 | | float |
119 | | svg_parse_angle(const char *str) |
120 | 0 | { |
121 | 0 | char *end; |
122 | 0 | float val; |
123 | |
|
124 | 0 | val = fz_strtof(str, &end); |
125 | 0 | if (end == str) |
126 | 0 | return 0; /* failed */ |
127 | | |
128 | 0 | if (!strcmp(end, "deg")) |
129 | 0 | return val; |
130 | | |
131 | 0 | if (!strcmp(end, "grad")) |
132 | 0 | return val * 0.9f; |
133 | | |
134 | 0 | if (!strcmp(end, "rad")) |
135 | 0 | return val * FZ_RADIAN; |
136 | | |
137 | 0 | return val; |
138 | 0 | } |
139 | | |
140 | | /* Coordinate transformations */ |
141 | | fz_matrix |
142 | | svg_parse_transform(fz_context *ctx, svg_document *doc, const char *str, fz_matrix transform) |
143 | 0 | { |
144 | 0 | char keyword[20]; |
145 | 0 | int keywordlen; |
146 | 0 | float args[6]; |
147 | 0 | int nargs; |
148 | |
|
149 | 0 | if (str == NULL) |
150 | 0 | return transform; |
151 | | |
152 | 0 | nargs = 0; |
153 | 0 | keywordlen = 0; |
154 | |
|
155 | 0 | while (*str) |
156 | 0 | { |
157 | 0 | while (svg_is_whitespace_or_comma(*str)) |
158 | 0 | str ++; |
159 | 0 | if (*str == 0) |
160 | 0 | break; |
161 | | |
162 | | /* |
163 | | * Parse keyword and opening parenthesis. |
164 | | */ |
165 | | |
166 | 0 | keywordlen = 0; |
167 | 0 | while (svg_is_alpha(*str) && keywordlen < (int)sizeof(keyword) - 1) |
168 | 0 | keyword[keywordlen++] = *str++; |
169 | 0 | keyword[keywordlen] = 0; |
170 | |
|
171 | 0 | if (keywordlen == 0) |
172 | 0 | fz_throw(ctx, FZ_ERROR_SYNTAX, "expected keyword in transform attribute"); |
173 | | |
174 | 0 | while (svg_is_whitespace(*str)) |
175 | 0 | str ++; |
176 | |
|
177 | 0 | if (*str != '(') |
178 | 0 | fz_throw(ctx, FZ_ERROR_SYNTAX, "expected opening parenthesis in transform attribute"); |
179 | 0 | str ++; |
180 | | |
181 | | /* |
182 | | * Parse list of numbers until closing parenthesis |
183 | | */ |
184 | |
|
185 | 0 | nargs = 0; |
186 | 0 | while (*str && *str != ')' && nargs < 6) |
187 | 0 | { |
188 | 0 | while (svg_is_whitespace_or_comma(*str)) |
189 | 0 | str ++; |
190 | 0 | if (svg_is_digit(*str)) |
191 | 0 | str = svg_lex_number(&args[nargs++], str); |
192 | 0 | else |
193 | 0 | break; |
194 | 0 | } |
195 | |
|
196 | 0 | if (*str != ')') |
197 | 0 | fz_throw(ctx, FZ_ERROR_SYNTAX, "expected closing parenthesis in transform attribute"); |
198 | 0 | str ++; |
199 | | |
200 | | /* |
201 | | * Execute the transform. |
202 | | */ |
203 | |
|
204 | 0 | if (!strcmp(keyword, "matrix")) |
205 | 0 | { |
206 | 0 | if (nargs != 6) |
207 | 0 | fz_throw(ctx, FZ_ERROR_SYNTAX, "wrong number of arguments to matrix(): %d", nargs); |
208 | 0 | transform = fz_concat(fz_make_matrix(args[0], args[1], args[2], args[3], args[4], args[5]), transform); |
209 | 0 | } |
210 | | |
211 | 0 | else if (!strcmp(keyword, "translate")) |
212 | 0 | { |
213 | 0 | if (nargs == 1) |
214 | 0 | transform = fz_concat(fz_translate(args[0], 0), transform); |
215 | 0 | else if (nargs == 2) |
216 | 0 | transform = fz_concat(fz_translate(args[0], args[1]), transform); |
217 | 0 | else |
218 | 0 | fz_throw(ctx, FZ_ERROR_SYNTAX, "wrong number of arguments to translate(): %d", nargs); |
219 | 0 | } |
220 | | |
221 | 0 | else if (!strcmp(keyword, "scale")) |
222 | 0 | { |
223 | 0 | if (nargs == 1) |
224 | 0 | transform = fz_concat(fz_scale(args[0], args[0]), transform); |
225 | 0 | else if (nargs == 2) |
226 | 0 | transform = fz_concat(fz_scale(args[0], args[1]), transform); |
227 | 0 | else |
228 | 0 | fz_throw(ctx, FZ_ERROR_SYNTAX, "wrong number of arguments to scale(): %d", nargs); |
229 | 0 | } |
230 | | |
231 | 0 | else if (!strcmp(keyword, "rotate")) |
232 | 0 | { |
233 | 0 | if (nargs == 1) |
234 | 0 | transform = fz_concat(fz_rotate(args[0]), transform); |
235 | 0 | else if (nargs == 3) |
236 | 0 | { |
237 | 0 | transform = fz_concat(fz_translate(args[1], args[2]), transform); |
238 | 0 | transform = fz_concat(fz_rotate(args[0]), transform); |
239 | 0 | transform = fz_concat(fz_translate(-args[1], -args[2]), transform); |
240 | 0 | } |
241 | 0 | else |
242 | 0 | fz_throw(ctx, FZ_ERROR_SYNTAX, "wrong number of arguments to rotate(): %d", nargs); |
243 | 0 | } |
244 | | |
245 | 0 | else if (!strcmp(keyword, "skewX")) |
246 | 0 | { |
247 | 0 | if (nargs != 1) |
248 | 0 | fz_throw(ctx, FZ_ERROR_SYNTAX, "wrong number of arguments to skewX(): %d", nargs); |
249 | 0 | transform = fz_concat(fz_make_matrix(1, 0, tanf(args[0] * FZ_DEGREE), 1, 0, 0), transform); |
250 | 0 | } |
251 | | |
252 | 0 | else if (!strcmp(keyword, "skewY")) |
253 | 0 | { |
254 | 0 | if (nargs != 1) |
255 | 0 | fz_throw(ctx, FZ_ERROR_SYNTAX, "wrong number of arguments to skewY(): %d", nargs); |
256 | 0 | transform = fz_concat(fz_make_matrix(1, tanf(args[0] * FZ_DEGREE), 0, 1, 0, 0), transform); |
257 | 0 | } |
258 | | |
259 | 0 | else |
260 | 0 | { |
261 | 0 | fz_throw(ctx, FZ_ERROR_SYNTAX, "unknown transform function: %s", keyword); |
262 | 0 | } |
263 | 0 | } |
264 | | |
265 | 0 | return transform; |
266 | 0 | } |
267 | | |
268 | | float |
269 | | svg_parse_number_from_style(fz_context *ctx, svg_document *doc, const char *style, const char *att, float number) |
270 | 0 | { |
271 | 0 | if (style) |
272 | 0 | { |
273 | 0 | const char *p = strstr(style, att); |
274 | 0 | char *end; |
275 | 0 | if (p) |
276 | 0 | { |
277 | 0 | size_t n = strlen(att); |
278 | 0 | if (p[n] == ':') |
279 | 0 | { |
280 | 0 | p += n + 1; |
281 | 0 | while (*p && svg_is_whitespace(*p)) |
282 | 0 | ++p; |
283 | 0 | number = fz_strtof(p, &end); |
284 | 0 | if (end[0] == 'i' && end[1] == 'n') return number * 72; |
285 | 0 | if (end[0] == 'c' && end[1] == 'm') return number * 7200 / 254; |
286 | 0 | if (end[0] == 'm' && end[1] == 'm') return number * 720 / 254; |
287 | 0 | if (end[0] == 'p' && end[1] == 'c') return number * 12; |
288 | 0 | } |
289 | 0 | } |
290 | 0 | } |
291 | 0 | return number; |
292 | 0 | } |
293 | | |
294 | | int |
295 | | svg_parse_enum_from_style(fz_context *ctx, svg_document *doc, const char *style, const char *att, |
296 | | int ecount, const char *etable[], int value) |
297 | 0 | { |
298 | 0 | char buf[100], *end; |
299 | 0 | const char *p; |
300 | 0 | int i; |
301 | 0 | if (style) |
302 | 0 | { |
303 | 0 | p = strstr(style, att); |
304 | 0 | if (p) |
305 | 0 | { |
306 | 0 | size_t n = strlen(att); |
307 | 0 | if (p[n] == ':') |
308 | 0 | { |
309 | 0 | p += n + 1; |
310 | 0 | while (*p && svg_is_whitespace(*p)) |
311 | 0 | ++p; |
312 | 0 | fz_strlcpy(buf, p, sizeof buf); |
313 | 0 | end = strchr(buf, ';'); |
314 | 0 | if (end) |
315 | 0 | *end = 0; |
316 | 0 | for (i = 0; i < ecount; ++i) |
317 | 0 | if (!strcmp(etable[i], buf)) |
318 | 0 | return i; |
319 | 0 | } |
320 | 0 | } |
321 | 0 | } |
322 | 0 | return value; |
323 | 0 | } |
324 | | |
325 | | char * |
326 | | svg_parse_string_from_style(fz_context *ctx, svg_document *doc, const char *style, const char *att, |
327 | | char *buf, int buf_size, const char *value) |
328 | 0 | { |
329 | 0 | char *end, quote; |
330 | 0 | const char *p; |
331 | 0 | if (style) |
332 | 0 | { |
333 | 0 | p = strstr(style, att); |
334 | 0 | if (p) |
335 | 0 | { |
336 | 0 | size_t n = strlen(att); |
337 | 0 | if (p[n] == ':') |
338 | 0 | { |
339 | 0 | p += n + 1; |
340 | 0 | while (*p && svg_is_whitespace(*p)) |
341 | 0 | ++p; |
342 | 0 | quote = *p; |
343 | 0 | if (quote == '\'' || quote == '"') |
344 | 0 | { |
345 | 0 | fz_strlcpy(buf, p+1, buf_size); |
346 | 0 | end = strchr(buf, quote); |
347 | 0 | } |
348 | 0 | else |
349 | 0 | { |
350 | 0 | fz_strlcpy(buf, p, buf_size); |
351 | 0 | end = strchr(buf, ';'); |
352 | 0 | } |
353 | 0 | if (end) |
354 | 0 | *end = 0; |
355 | 0 | return buf; |
356 | 0 | } |
357 | 0 | } |
358 | 0 | } |
359 | 0 | fz_strlcpy(buf, value, buf_size); |
360 | 0 | return buf; |
361 | 0 | } |
362 | | |
363 | | static void |
364 | | svg_apply_css_match(fz_context *ctx, fz_pool *pool, fz_xml *node, fz_css_match *match) |
365 | 0 | { |
366 | 0 | const char *att; |
367 | 0 | char val[100]; |
368 | 0 | int i; |
369 | 0 | for (i = 0; i < SVG_NUM_PROPERTIES; ++i) |
370 | 0 | { |
371 | 0 | if (match->spec[i] >= 0) |
372 | 0 | { |
373 | 0 | att = fz_css_property_name_in_svg(i); |
374 | 0 | if (!fz_xml_att(node, att)) |
375 | 0 | fz_xml_add_att(ctx, pool, node, att, fz_string_from_css_value(ctx, val, sizeof val, match->value[i])); |
376 | 0 | } |
377 | 0 | } |
378 | 0 | } |
379 | | |
380 | | void |
381 | | svg_apply_css_cascade(fz_context *ctx, fz_pool *pool, fz_css *css, fz_xml *node) |
382 | 0 | { |
383 | 0 | fz_css_match match; |
384 | 0 | fz_xml *down; |
385 | |
|
386 | 0 | while (node) |
387 | 0 | { |
388 | | // NOTE: we don't inherit CSS attributes like in HTML; since we |
389 | | // apply them to the matching elements and use SVG's normal |
390 | | // attribute inheritance. Therefore we leave the "up" match NULL. |
391 | |
|
392 | 0 | fz_match_css_in_svg(ctx, &match, NULL, css, node); |
393 | |
|
394 | 0 | svg_apply_css_match(ctx, pool, node, &match); |
395 | |
|
396 | 0 | down = fz_xml_down(node); |
397 | 0 | if (down) |
398 | 0 | svg_apply_css_cascade(ctx, pool, css, down); |
399 | |
|
400 | 0 | node = fz_xml_next(node); |
401 | 0 | } |
402 | 0 | } |