Coverage Report

Created: 2025-07-23 06:37

/src/mupdf/source/svg/svg-parse.c
Line
Count
Source (jump to first uncovered line)
1
// Copyright (C) 2004-2021 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
  nargs = 0;
150
0
  keywordlen = 0;
151
152
0
  while (*str)
153
0
  {
154
0
    while (svg_is_whitespace_or_comma(*str))
155
0
      str ++;
156
0
    if (*str == 0)
157
0
      break;
158
159
    /*
160
     * Parse keyword and opening parenthesis.
161
     */
162
163
0
    keywordlen = 0;
164
0
    while (svg_is_alpha(*str) && keywordlen < (int)sizeof(keyword) - 1)
165
0
      keyword[keywordlen++] = *str++;
166
0
    keyword[keywordlen] = 0;
167
168
0
    if (keywordlen == 0)
169
0
      fz_throw(ctx, FZ_ERROR_SYNTAX, "expected keyword in transform attribute");
170
171
0
    while (svg_is_whitespace(*str))
172
0
      str ++;
173
174
0
    if (*str != '(')
175
0
      fz_throw(ctx, FZ_ERROR_SYNTAX, "expected opening parenthesis in transform attribute");
176
0
    str ++;
177
178
    /*
179
     * Parse list of numbers until closing parenthesis
180
     */
181
182
0
    nargs = 0;
183
0
    while (*str && *str != ')' && nargs < 6)
184
0
    {
185
0
      while (svg_is_whitespace_or_comma(*str))
186
0
        str ++;
187
0
      if (svg_is_digit(*str))
188
0
        str = svg_lex_number(&args[nargs++], str);
189
0
      else
190
0
        break;
191
0
    }
192
193
0
    if (*str != ')')
194
0
      fz_throw(ctx, FZ_ERROR_SYNTAX, "expected closing parenthesis in transform attribute");
195
0
    str ++;
196
197
    /*
198
     * Execute the transform.
199
     */
200
201
0
    if (!strcmp(keyword, "matrix"))
202
0
    {
203
0
      if (nargs != 6)
204
0
        fz_throw(ctx, FZ_ERROR_SYNTAX, "wrong number of arguments to matrix(): %d", nargs);
205
0
      transform = fz_concat(fz_make_matrix(args[0], args[1], args[2], args[3], args[4], args[5]), transform);
206
0
    }
207
208
0
    else if (!strcmp(keyword, "translate"))
209
0
    {
210
0
      if (nargs == 1)
211
0
        transform = fz_concat(fz_translate(args[0], 0), transform);
212
0
      else if (nargs == 2)
213
0
        transform = fz_concat(fz_translate(args[0], args[1]), transform);
214
0
      else
215
0
        fz_throw(ctx, FZ_ERROR_SYNTAX, "wrong number of arguments to translate(): %d", nargs);
216
0
    }
217
218
0
    else if (!strcmp(keyword, "scale"))
219
0
    {
220
0
      if (nargs == 1)
221
0
        transform = fz_concat(fz_scale(args[0], args[0]), transform);
222
0
      else if (nargs == 2)
223
0
        transform = fz_concat(fz_scale(args[0], args[1]), transform);
224
0
      else
225
0
        fz_throw(ctx, FZ_ERROR_SYNTAX, "wrong number of arguments to scale(): %d", nargs);
226
0
    }
227
228
0
    else if (!strcmp(keyword, "rotate"))
229
0
    {
230
0
      if (nargs == 1)
231
0
        transform = fz_concat(fz_rotate(args[0]), transform);
232
0
      else if (nargs == 3)
233
0
      {
234
0
        transform = fz_concat(fz_translate(args[1], args[2]), transform);
235
0
        transform = fz_concat(fz_rotate(args[0]), transform);
236
0
        transform = fz_concat(fz_translate(-args[1], -args[2]), transform);
237
0
      }
238
0
      else
239
0
        fz_throw(ctx, FZ_ERROR_SYNTAX, "wrong number of arguments to rotate(): %d", nargs);
240
0
    }
241
242
0
    else if (!strcmp(keyword, "skewX"))
243
0
    {
244
0
      if (nargs != 1)
245
0
        fz_throw(ctx, FZ_ERROR_SYNTAX, "wrong number of arguments to skewX(): %d", nargs);
246
0
      transform = fz_concat(fz_make_matrix(1, 0, tanf(args[0] * FZ_DEGREE), 1, 0, 0), transform);
247
0
    }
248
249
0
    else if (!strcmp(keyword, "skewY"))
250
0
    {
251
0
      if (nargs != 1)
252
0
        fz_throw(ctx, FZ_ERROR_SYNTAX, "wrong number of arguments to skewY(): %d", nargs);
253
0
      transform = fz_concat(fz_make_matrix(1, tanf(args[0] * FZ_DEGREE), 0, 1, 0, 0), transform);
254
0
    }
255
256
0
    else
257
0
    {
258
0
      fz_throw(ctx, FZ_ERROR_SYNTAX, "unknown transform function: %s", keyword);
259
0
    }
260
0
  }
261
262
0
  return transform;
263
0
}
264
265
float
266
svg_parse_number_from_style(fz_context *ctx, svg_document *doc, const char *style, const char *att, float number)
267
0
{
268
0
  if (style)
269
0
  {
270
0
    char *end, *p = strstr(style, att);
271
0
    if (p)
272
0
    {
273
0
      size_t n = strlen(att);
274
0
      if (p[n] == ':')
275
0
      {
276
0
        p += n + 1;
277
0
        while (*p && svg_is_whitespace(*p))
278
0
          ++p;
279
0
        number = fz_strtof(p, &end);
280
0
        if (end[0] == 'i' && end[1] == 'n') return number * 72;
281
0
        if (end[0] == 'c' && end[1] == 'm') return number * 7200 / 254;
282
0
        if (end[0] == 'm' && end[1] == 'm') return number * 720 / 254;
283
0
        if (end[0] == 'p' && end[1] == 'c') return number * 12;
284
0
      }
285
0
    }
286
0
  }
287
0
  return number;
288
0
}
289
290
int
291
svg_parse_enum_from_style(fz_context *ctx, svg_document *doc, const char *style, const char *att,
292
  int ecount, const char *etable[], int value)
293
0
{
294
0
  char buf[100], *end, *p;
295
0
  int i;
296
0
  if (style)
297
0
  {
298
0
    p = strstr(style, att);
299
0
    if (p)
300
0
    {
301
0
      size_t n = strlen(att);
302
0
      if (p[n] == ':')
303
0
      {
304
0
        p += n + 1;
305
0
        while (*p && svg_is_whitespace(*p))
306
0
          ++p;
307
0
        fz_strlcpy(buf, p, sizeof buf);
308
0
        end = strchr(buf, ';');
309
0
        if (end)
310
0
          *end = 0;
311
0
        for (i = 0; i < ecount; ++i)
312
0
          if (!strcmp(etable[i], buf))
313
0
            return i;
314
0
      }
315
0
    }
316
0
  }
317
0
  return value;
318
0
}
319
320
char *
321
svg_parse_string_from_style(fz_context *ctx, svg_document *doc, const char *style, const char *att,
322
  char *buf, int buf_size, const char *value)
323
0
{
324
0
  char *end, *p, quote;
325
0
  if (style)
326
0
  {
327
0
    p = strstr(style, att);
328
0
    if (p)
329
0
    {
330
0
      size_t n = strlen(att);
331
0
      if (p[n] == ':')
332
0
      {
333
0
        p += n + 1;
334
0
        while (*p && svg_is_whitespace(*p))
335
0
          ++p;
336
0
        quote = *p;
337
0
        if (quote == '\'' || quote == '"')
338
0
        {
339
0
          fz_strlcpy(buf, p+1, buf_size);
340
0
          end = strchr(buf, quote);
341
0
        }
342
0
        else
343
0
        {
344
0
          fz_strlcpy(buf, p, buf_size);
345
0
          end = strchr(buf, ';');
346
0
        }
347
0
        if (end)
348
0
          *end = 0;
349
0
        return buf;
350
0
      }
351
0
    }
352
0
  }
353
0
  fz_strlcpy(buf, value, buf_size);
354
0
  return buf;
355
0
}