Coverage Report

Created: 2025-07-12 07:23

/src/cairo/src/cairo-svg-glyph-render.c
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: c; c-basic-offset: 4; indent-tabs-mode: t; tab-width: 8; -*- */
2
/* cairo - a vector graphics library with display and print output
3
 *
4
 * Copyright © 2022 Adrian Johnson
5
 *
6
 * This library is free software; you can redistribute it and/or
7
 * modify it either under the terms of the GNU Lesser General Public
8
 * License version 2.1 as published by the Free Software Foundation
9
 * (the "LGPL") or, at your option, under the terms of the Mozilla
10
 * Public License Version 1.1 (the "MPL"). If you do not alter this
11
 * notice, a recipient may use your version of this file under either
12
 * the MPL or the LGPL.
13
 *
14
 * You should have received a copy of the LGPL along with this library
15
 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
16
 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
17
 * You should have received a copy of the MPL along with this library
18
 * in the file COPYING-MPL-1.1
19
 *
20
 * The contents of this file are subject to the Mozilla Public License
21
 * Version 1.1 (the "License"); you may not use this file except in
22
 * compliance with the License. You may obtain a copy of the License at
23
 * http://www.mozilla.org/MPL/
24
 *
25
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
26
 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
27
 * the specific language governing rights and limitations.
28
 *
29
 * The Original Code is the cairo graphics library.
30
 *
31
 * The Initial Developer of the Original Code is Adrian Johnson.
32
 *
33
 * Contributor(s):
34
 *  Adrian Johnson <ajohnson@redneon.com>
35
 */
36
37
#include "cairoint.h"
38
#include "cairo-array-private.h"
39
#include "cairo-ft-private.h"
40
#include "cairo-pattern-private.h"
41
#include "cairo-scaled-font-subsets-private.h"
42
43
#include <stdarg.h>
44
#include <stdio.h>
45
#include <string.h>
46
47
#if HAVE_FT_SVG_DOCUMENT
48
49
#include <ft2build.h>
50
#include FT_COLOR_H
51
52
/* #define SVG_RENDER_PRINT_FUNCTIONS 1 */
53
54
0
#define WHITE_SPACE_CHARS " \n\r\t\v\f"
55
56
typedef struct {
57
    const char *name;
58
    int red;
59
    int green;
60
    int blue;
61
} color_name_t;
62
63
/* Must be sorted */
64
static color_name_t color_names[] = {
65
    { "aliceblue", 240, 248, 255 },
66
    { "antiquewhite", 250, 235, 215 },
67
    { "aqua",  0, 255, 255 },
68
    { "aquamarine", 127, 255, 212 },
69
    { "azure", 240, 255, 255 },
70
    { "beige", 245, 245, 220 },
71
    { "bisque", 255, 228, 196 },
72
    { "black",  0, 0, 0 },
73
    { "blanchedalmond", 255, 235, 205 },
74
    { "blue",  0, 0, 255 },
75
    { "blueviolet", 138, 43, 226 },
76
    { "brown", 165, 42, 42 },
77
    { "burlywood", 222, 184, 135 },
78
    { "cadetblue",  95, 158, 160 },
79
    { "chartreuse", 127, 255, 0 },
80
    { "chocolate", 210, 105, 30 },
81
    { "coral", 255, 127, 80 },
82
    { "cornflowerblue", 100, 149, 237 },
83
    { "cornsilk", 255, 248, 220 },
84
    { "crimson", 220, 20, 60 },
85
    { "cyan",  0, 255, 255 },
86
    { "darkblue",  0, 0, 139 },
87
    { "darkcyan",  0, 139, 139 },
88
    { "darkgoldenrod", 184, 134, 11 },
89
    { "darkgray", 169, 169, 169 },
90
    { "darkgreen",  0, 100, 0 },
91
    { "darkgrey", 169, 169, 169 },
92
    { "darkkhaki", 189, 183, 107 },
93
    { "darkmagenta", 139, 0, 139 },
94
    { "darkolivegreen",  85, 107, 47 },
95
    { "darkorange", 255, 140, 0 },
96
    { "darkorchid", 153, 50, 204 },
97
    { "darkred", 139, 0, 0 },
98
    { "darksalmon", 233, 150, 122 },
99
    { "darkseagreen", 143, 188, 143 },
100
    { "darkslateblue",  72, 61, 139 },
101
    { "darkslategray",  47, 79, 79 },
102
    { "darkslategrey",  47, 79, 79 },
103
    { "darkturquoise",  0, 206, 209 },
104
    { "darkviolet", 148, 0, 211 },
105
    { "deeppink", 255, 20, 147 },
106
    { "deepskyblue",  0, 191, 255 },
107
    { "dimgray", 105, 105, 105 },
108
    { "dimgrey", 105, 105, 105 },
109
    { "dodgerblue",  30, 144, 255 },
110
    { "firebrick", 178, 34, 34 },
111
    { "floralwhite", 255, 250, 240 },
112
    { "forestgreen",  34, 139, 34 },
113
    { "fuchsia", 255, 0, 255 },
114
    { "gainsboro", 220, 220, 220 },
115
    { "ghostwhite", 248, 248, 255 },
116
    { "gold", 255, 215, 0 },
117
    { "goldenrod", 218, 165, 32 },
118
    { "gray", 128, 128, 128 },
119
    { "green",  0, 128, 0 },
120
    { "greenyellow", 173, 255, 47 },
121
    { "grey", 128, 128, 128 },
122
    { "honeydew", 240, 255, 240 },
123
    { "hotpink", 255, 105, 180 },
124
    { "indianred", 205, 92, 92 },
125
    { "indigo",  75, 0, 130 },
126
    { "ivory", 255, 255, 240 },
127
    { "khaki", 240, 230, 140 },
128
    { "lavender", 230, 230, 250 },
129
    { "lavenderblush", 255, 240, 245 },
130
    { "lawngreen", 124, 252, 0 },
131
    { "lemonchiffon", 255, 250, 205 },
132
    { "lightblue", 173, 216, 230 },
133
    { "lightcoral", 240, 128, 128 },
134
    { "lightcyan", 224, 255, 255 },
135
    { "lightgoldenrodyellow", 250, 250, 210 },
136
    { "lightgray", 211, 211, 211 },
137
    { "lightgreen", 144, 238, 144 },
138
    { "lightgrey", 211, 211, 211 },
139
    { "lightpink", 255, 182, 193 },
140
    { "lightsalmon", 255, 160, 122 },
141
    { "lightseagreen",  32, 178, 170 },
142
    { "lightskyblue", 135, 206, 250 },
143
    { "lightslategray", 119, 136, 153 },
144
    { "lightslategrey", 119, 136, 153 },
145
    { "lightsteelblue", 176, 196, 222 },
146
    { "lightyellow", 255, 255, 224 },
147
    { "lime",  0, 255, 0 },
148
    { "limegreen",  50, 205, 50 },
149
    { "linen", 250, 240, 230 },
150
    { "magenta", 255, 0, 255 },
151
    { "maroon", 128, 0, 0 },
152
    { "mediumaquamarine", 102, 205, 170 },
153
    { "mediumblue",  0, 0, 205 },
154
    { "mediumorchid", 186, 85, 211 },
155
    { "mediumpurple", 147, 112, 219 },
156
    { "mediumseagreen",  60, 179, 113 },
157
    { "mediumslateblue", 123, 104, 238 },
158
    { "mediumspringgreen",  0, 250, 154 },
159
    { "mediumturquoise",  72, 209, 204 },
160
    { "mediumvioletred", 199, 21, 133 },
161
    { "midnightblue",  25, 25, 112 },
162
    { "mintcream", 245, 255, 250 },
163
    { "mistyrose", 255, 228, 225 },
164
    { "moccasin", 255, 228, 181 },
165
    { "navajowhite", 255, 222, 173 },
166
    { "navy",  0, 0, 128 },
167
    { "oldlace", 253, 245, 230 },
168
    { "olive", 128, 128, 0 },
169
    { "olivedrab", 107, 142, 35 },
170
    { "orange", 255, 165, 0 },
171
    { "orangered", 255, 69, 0 },
172
    { "orchid", 218, 112, 214 },
173
    { "palegoldenrod", 238, 232, 170 },
174
    { "palegreen", 152, 251, 152 },
175
    { "paleturquoise", 175, 238, 238 },
176
    { "palevioletred", 219, 112, 147 },
177
    { "papayawhip", 255, 239, 213 },
178
    { "peachpuff", 255, 218, 185 },
179
    { "peru", 205, 133, 63 },
180
    { "pink", 255, 192, 203 },
181
    { "plum", 221, 160, 221 },
182
    { "powderblue", 176, 224, 230 },
183
    { "purple", 128, 0, 128 },
184
    { "red", 255, 0, 0 },
185
    { "rosybrown", 188, 143, 143 },
186
    { "royalblue",  65, 105, 225 },
187
    { "saddlebrown", 139, 69, 19 },
188
    { "salmon", 250, 128, 114 },
189
    { "sandybrown", 244, 164, 96 },
190
    { "seagreen",  46, 139, 87 },
191
    { "seashell", 255, 245, 238 },
192
    { "sienna", 160, 82, 45 },
193
    { "silver", 192, 192, 192 },
194
    { "skyblue", 135, 206, 235 },
195
    { "slateblue", 106, 90, 205 },
196
    { "slategray", 112, 128, 144 },
197
    { "slategrey", 112, 128, 144 },
198
    { "snow", 255, 250, 250 },
199
    { "springgreen",  0, 255, 127 },
200
    { "steelblue",  70, 130, 180 },
201
    { "tan", 210, 180, 140 },
202
    { "teal",  0, 128, 128 },
203
    { "thistle", 216, 191, 216 },
204
    { "tomato", 255, 99, 71 },
205
    { "turquoise",  64, 224, 208 },
206
    { "violet", 238, 130, 238 },
207
    { "wheat", 245, 222, 179 },
208
    { "white", 255, 255, 255 },
209
    { "whitesmoke", 245, 245, 245 },
210
    { "yellow", 255, 255, 0 },
211
    { "yellowgreen", 154, 205, 50 }
212
};
213
214
typedef struct {
215
    char *name;
216
    char *value;
217
} svg_attribute_t;
218
219
typedef enum {
220
    CONTAINER_ELEMENT,
221
    EMPTY_ELEMENT,
222
    PROCESSING_INSTRUCTION,
223
    DOCTYPE,
224
    CDATA,
225
    COMMENT
226
} tag_type_t;
227
228
0
#define TOP_ELEMENT_TAG "_top"
229
230
typedef struct _cairo_svg_element {
231
    cairo_hash_entry_t base;
232
    tag_type_t type;
233
    char *tag;
234
    char *id;
235
    cairo_array_t attributes; /* svg_attribute_t */
236
    cairo_array_t children; /* cairo_svg_element_t* */
237
    cairo_array_t  content; /* char */
238
    cairo_pattern_t *pattern; /* defined if a paint server */
239
    struct _cairo_svg_element *next; /* next on element stack */
240
} cairo_svg_element_t;
241
242
typedef struct _cairo_svg_color {
243
    enum { RGB, FOREGROUND } type;
244
    double red;
245
    double green;
246
    double blue;
247
} cairo_svg_color_t;
248
249
typedef struct _cairo_svg_paint {
250
    enum { PAINT_COLOR, PAINT_SERVER, PAINT_NONE } type;
251
    cairo_svg_color_t color;
252
    cairo_svg_element_t *paint_server;
253
} cairo_svg_paint_t;
254
255
typedef enum {
256
    GS_RENDER,
257
    GS_NO_RENDER,
258
    GS_COMPUTE_BBOX,
259
    GS_CLIP
260
} gs_mode_t;
261
262
typedef struct _cairo_svg_graphics_state {
263
    cairo_svg_paint_t fill;
264
    cairo_svg_paint_t stroke;
265
    cairo_svg_color_t color;
266
    double fill_opacity;
267
    double stroke_opacity;
268
    double opacity;
269
    cairo_fill_rule_t fill_rule;
270
    cairo_fill_rule_t clip_rule;
271
    cairo_path_t *clip_path;
272
    char *dash_array;
273
    double dash_offset;
274
    gs_mode_t mode;
275
    struct {
276
        double x;
277
        double y;
278
        double width;
279
        double height;
280
    } bbox;
281
    struct _cairo_svg_graphics_state *next;
282
} cairo_svg_graphics_state_t;
283
284
typedef enum {
285
    BUILD_PATTERN_NONE,
286
    BUILD_PATTERN_LINEAR,
287
    BUILD_PATTERN_RADIAL
288
} build_pattern_t;
289
290
typedef struct _cairo_svg_glyph_render {
291
    cairo_svg_element_t *tree;
292
    cairo_hash_table_t *ids;
293
    cairo_svg_graphics_state_t *graphics_state;
294
    cairo_t *cr;
295
    double units_per_em;
296
    struct {
297
        cairo_svg_element_t *paint_server;
298
        cairo_pattern_t *pattern;
299
        build_pattern_t type;
300
    } build_pattern;
301
    int render_element_tree_depth;
302
    int num_palette_entries;
303
    FT_Color* palette;
304
305
    /* Viewport */
306
    double width;
307
    double height;
308
    cairo_bool_t view_port_set;
309
310
    cairo_pattern_t *foreground_marker;
311
    cairo_pattern_t *foreground_source;
312
    cairo_bool_t foreground_source_used;
313
314
    int debug; /* 0 = quiet, 1 = errors, 2 = warnings, 3 = info */
315
} cairo_svg_glyph_render_t;
316
317
318
0
#define SVG_RENDER_ERROR 1
319
0
#define SVG_RENDER_WARNING 2
320
0
#define SVG_RENDER_INFO 3
321
322
0
#define print_error(render, ...) cairo_svg_glyph_render_printf(render, SVG_RENDER_ERROR, ##__VA_ARGS__)
323
0
#define print_warning(render, ...) cairo_svg_glyph_render_printf(render, SVG_RENDER_WARNING, ##__VA_ARGS__)
324
0
#define print_info(render, ...) cairo_svg_glyph_render_printf(render, SVG_RENDER_INFO, ##__VA_ARGS__)
325
326
static void
327
cairo_svg_glyph_render_printf (cairo_svg_glyph_render_t *svg_render,
328
                               int level,
329
                               const char *fmt, ...) CAIRO_PRINTF_FORMAT (3, 4);
330
331
static void
332
cairo_svg_glyph_render_printf (cairo_svg_glyph_render_t *svg_render,
333
                               int level,
334
                               const char *fmt, ...)
335
0
{
336
0
    va_list ap;
337
338
0
    if (svg_render->debug >= level ) {
339
0
        switch (level) {
340
0
            case SVG_RENDER_ERROR:
341
0
                printf("ERROR: ");
342
0
                break;
343
0
            case SVG_RENDER_WARNING:
344
0
                printf("WARNING: ");
345
0
                break;
346
0
        }
347
0
  va_start (ap, fmt);
348
0
  vprintf (fmt, ap);
349
0
  va_end (ap);
350
0
  printf ("\n");
351
0
    }
352
0
}
353
354
static cairo_bool_t
355
string_equal (const char *s1, const char *s2)
356
0
{
357
0
    if (s1 && s2)
358
0
        return strcmp (s1, s2) == 0;
359
360
0
    if (!s1 && !s2)
361
0
        return TRUE;
362
363
0
    return FALSE;
364
0
}
365
366
static cairo_bool_t
367
string_match (const char **p, const char *str)
368
0
{
369
0
    if (*p && strncmp (*p, str, strlen (str)) == 0) {
370
0
        *p += strlen (str);
371
0
        return TRUE;
372
0
    }
373
0
    return FALSE;
374
0
}
375
376
static const char *
377
skip_space (const char *p)
378
0
{
379
0
    while (*p && _cairo_isspace (*p))
380
0
        p++;
381
382
0
    return p;
383
0
}
384
385
/* Skip over character c and and whitespace before or after. Returns
386
 * NULL if c not found. */
387
static const char *
388
skip_char (const char *p, char c)
389
0
{
390
0
    while (_cairo_isspace (*p))
391
0
        p++;
392
393
0
    if (*p != c)
394
0
        return NULL;
395
396
0
    p++;
397
398
0
    while (_cairo_isspace (*p))
399
0
        p++;
400
401
0
    return p;
402
0
}
403
404
static int
405
_color_name_compare (const void *a, const void *b)
406
0
{
407
0
    const color_name_t *a_color = a;
408
0
    const color_name_t *b_color = b;
409
410
0
    return strcmp (a_color->name, b_color->name);
411
0
}
412
413
static void
414
init_element_id_key (cairo_svg_element_t *element)
415
0
{
416
0
    element->base.hash = _cairo_hash_string (element->id);
417
0
}
418
419
static cairo_bool_t
420
_element_id_equal (const void *key_a, const void *key_b)
421
0
{
422
0
    const cairo_svg_element_t *a = key_a;
423
0
    const cairo_svg_element_t *b = key_b;
424
425
0
    return string_equal (a->id, b->id);
426
0
}
427
428
/* Find element with the "id" attribute matching id. id may have the
429
 * '#' prefix. It will be stripped before searching.
430
 */
431
static cairo_svg_element_t *
432
lookup_element (cairo_svg_glyph_render_t *svg_render, const char *id)
433
0
{
434
0
    cairo_svg_element_t key;
435
436
0
    if (!id || strlen (id) < 1)
437
0
        return NULL;
438
439
0
    key.id = (char *)(id[0] == '#' ? id + 1 : id);
440
0
    init_element_id_key (&key);
441
0
    return _cairo_hash_table_lookup (svg_render->ids, &key.base);
442
0
}
443
444
/* Find element with the "id" attribute matching url where url is of
445
 * the form "url(#id)".
446
 */
447
static cairo_svg_element_t *
448
lookup_url_element (cairo_svg_glyph_render_t *svg_render, const char *url)
449
0
{
450
0
    const char *p = url;
451
0
    cairo_svg_element_t *element = NULL;
452
453
0
    if (p && string_match (&p, "url")) {
454
0
        p = skip_char (p, '(');
455
0
        if (!p)
456
0
            return NULL;
457
458
0
        const char *end = strpbrk(p, WHITE_SPACE_CHARS ")");
459
0
        if (end) {
460
0
            char *id = _cairo_strndup (p, end - p);
461
0
            element = lookup_element (svg_render, id);
462
0
            free (id);
463
0
        }
464
0
    }
465
0
    return element;
466
0
}
467
468
static const char *
469
get_attribute (const cairo_svg_element_t *element, const char *name)
470
0
{
471
0
    svg_attribute_t attr;
472
0
    int num_elems, i;
473
474
0
    num_elems = _cairo_array_num_elements (&element->attributes);
475
0
    for (i = 0; i < num_elems; i++) {
476
0
  _cairo_array_copy_element (&element->attributes, i, &attr);
477
0
        if (string_equal (attr.name, name))
478
0
            return attr.value;
479
0
    }
480
0
    return NULL;
481
0
}
482
483
static const char *
484
get_href_attribute (const cairo_svg_element_t *element)
485
0
{
486
0
    svg_attribute_t attr;
487
0
    int num_elems, i, len;
488
489
    /* SVG2 requires the href attribute to be "href". Older versions
490
     * used "xlink:href". I have seen at least one font that used an
491
     * alternative name space eg "ns1:href". To keep things simple we
492
     * search for an attribute named "href" or ending in ":href".
493
     */
494
0
    num_elems = _cairo_array_num_elements (&element->attributes);
495
0
    for (i = 0; i < num_elems; i++) {
496
0
  _cairo_array_copy_element (&element->attributes, i, &attr);
497
0
        if (string_equal (attr.name, "href"))
498
0
            return attr.value;
499
500
0
        len = strlen (attr.name);
501
0
        if (len > 4 && string_equal (attr.name + len - 5, ":href"))
502
0
            return attr.value;
503
0
    }
504
0
    return NULL;
505
0
}
506
507
/* Get a float attribute or float percentage. If attribute is a
508
 * percentage, the returned value is percentage * scale.  Does not
509
 * modify value if it returns FALSE. This allows value to be set to a
510
 * default before calling get_float_attribute(), then used without
511
 * checking the return value of this function.
512
 */
513
static cairo_bool_t
514
get_float_or_percent_attribute (const cairo_svg_element_t *element,
515
                                const char *name,
516
                                double scale,
517
                                double *value)
518
0
{
519
0
    const char *p;
520
0
    char *end;
521
0
    double v;
522
523
0
    p = get_attribute (element, name);
524
0
    if (p) {
525
0
        v = _cairo_strtod (p, &end);
526
0
        if (end != p) {
527
0
            *value = v;
528
0
            if (*end == '%')
529
0
                *value *= scale / 100.0;
530
0
            return TRUE;
531
0
        }
532
0
    }
533
0
    return FALSE;
534
0
}
535
536
/* Does not modify value if it returns FALSE. This allows value to be
537
 * set to a default before calling get_float_attribute(), then used
538
 * without checking the return value of this function.
539
 */
540
static cairo_bool_t
541
get_float_attribute (const cairo_svg_element_t *element, const char *name, double *value)
542
0
{
543
0
    const char *p;
544
0
    char *end;
545
0
    double v;
546
547
0
    p = get_attribute (element, name);
548
0
    if (p) {
549
0
        v = _cairo_strtod (p, &end);
550
0
        if (end != p) {
551
0
            *value = v;
552
0
            return TRUE;
553
0
        }
554
0
    }
555
0
    return FALSE;
556
0
}
557
558
static cairo_fill_rule_t
559
get_fill_rule_attribute (const cairo_svg_element_t *element, const char *name, cairo_fill_rule_t default_value)
560
0
{
561
0
    const char *p;
562
563
0
    p = get_attribute (element, name);
564
0
    if (string_equal (p, "nonzero"))
565
0
        return CAIRO_FILL_RULE_WINDING;
566
0
    else if (string_equal (p, "evenodd"))
567
0
        return CAIRO_FILL_RULE_EVEN_ODD;
568
0
    else
569
0
        return default_value;
570
0
}
571
572
static void
573
free_elements (cairo_svg_glyph_render_t *svg_render,
574
              cairo_svg_element_t      *element)
575
0
{
576
0
    int num_elems;
577
578
0
    num_elems = _cairo_array_num_elements (&element->children);
579
0
    for (int i = 0; i < num_elems; i++) {
580
0
  cairo_svg_element_t *child;
581
0
        _cairo_array_copy_element (&element->children, i, &child);
582
0
  free_elements (svg_render, child);
583
0
    }
584
0
    _cairo_array_fini (&element->children);
585
586
0
    num_elems = _cairo_array_num_elements (&element->attributes);
587
0
    for (int i = 0; i < num_elems; i++) {
588
0
  svg_attribute_t *attr = _cairo_array_index (&element->attributes, i);
589
0
  free (attr->name);
590
0
  free (attr->value);
591
0
    }
592
0
    _cairo_array_fini (&element->attributes);
593
0
    _cairo_array_fini (&element->content);
594
595
0
    free (element->tag);
596
597
0
    if (element->id) {
598
0
        _cairo_hash_table_remove (svg_render->ids, &element->base);
599
0
        free (element->id);
600
0
    }
601
602
0
    if (element->pattern)
603
0
        cairo_pattern_destroy (element->pattern);
604
605
0
    free (element);
606
0
}
607
608
#if SVG_RENDER_PRINT_FUNCTIONS
609
610
static void indent(int level)
611
{
612
    for (int i = 1; i < level; i++)
613
        printf("  ");
614
}
615
616
static void
617
print_element (cairo_svg_element_t *element, cairo_bool_t recurse, int level)
618
{
619
    char *content = strndup (_cairo_array_index_const (&element->content, 0),
620
                             _cairo_array_num_elements (&element->content));
621
622
    indent(level);
623
    if (element->type == COMMENT) {
624
        printf("<!--%s-->\n", content);
625
    } else if (element->type == CDATA) {
626
        printf("<![CDATA[%s]]>\n", content);
627
    } else if (element->type == DOCTYPE) {
628
        printf("<!DOCTYPE%s>\n", content);
629
    } else if (element->type == PROCESSING_INSTRUCTION) {
630
        printf("<?%s?>\n", content);
631
    } else {
632
        cairo_bool_t top_element = string_equal (element->tag, TOP_ELEMENT_TAG);
633
634
        if (!top_element) {
635
            printf("<%s", element->tag);
636
            int num_elems = _cairo_array_num_elements (&element->attributes);
637
            for (int i = 0; i < num_elems; i++) {
638
                svg_attribute_t *attr = _cairo_array_index (&element->attributes, i);
639
                printf(" %s=\"%s\"", attr->name, attr->value);
640
            }
641
            if (num_elems > 0)
642
                printf(" ");
643
644
            if (element->type == EMPTY_ELEMENT)
645
                printf("/>\n");
646
            else
647
                printf(">\n");
648
        }
649
650
        if (element->type == CONTAINER_ELEMENT) {
651
            if (recurse) {
652
                int num_elems = _cairo_array_num_elements (&element->children);
653
                for (int i = 0; i < num_elems; i++) {
654
                    cairo_svg_element_t *child;
655
                    _cairo_array_copy_element (&element->children, i, &child);
656
                    print_element (child, TRUE, level + 1);
657
                }
658
            }
659
            if (!top_element)
660
                printf("</%s>\n", element->tag);
661
        }
662
    }
663
    free (content);
664
}
665
#endif
666
667
static const char *
668
parse_list_of_floats (const char *p,
669
                      int num_required,
670
                      int num_optional,
671
                      cairo_bool_t *have_optional,
672
                      va_list ap)
673
0
{
674
0
    double d;
675
0
    double *dp;
676
0
    char *end;
677
0
    const char *q = NULL;
678
0
    int num_found = 0;
679
680
0
    for (int i = 0; i < num_required + num_optional; i++) {
681
0
        while (p && (*p == ',' || _cairo_isspace (*p)))
682
0
            p++;
683
684
0
        if (!p)
685
0
            break;
686
687
0
        d = _cairo_strtod (p, &end);
688
0
        if (end == p) {
689
0
            p = NULL;
690
0
            break;
691
0
        }
692
0
        p = end;
693
0
        dp = va_arg (ap, double *);
694
0
        *dp = d;
695
0
        num_found++;
696
0
        if (num_found == num_required)
697
0
            q = p;
698
0
    }
699
700
0
    if (num_optional > 0) {
701
0
        if (num_found == num_required + num_optional) {
702
0
            *have_optional = TRUE;
703
0
        } else {
704
0
            *have_optional = FALSE;
705
            /* restore pointer to end of required floats */
706
0
            p = q;
707
0
        }
708
0
    }
709
710
0
    return p;
711
0
}
712
713
static const char *
714
get_floats (const char *p,
715
            int num_required,
716
            int num_optional,
717
            cairo_bool_t *have_optional,
718
            ...)
719
0
{
720
0
    va_list ap;
721
722
0
    va_start (ap, have_optional);
723
0
    p = parse_list_of_floats (p, num_required, num_optional, have_optional, ap);
724
0
    va_end (ap);
725
0
    return p;
726
0
}
727
728
static const char *
729
get_path_params (const char *p, int num_params, ...)
730
0
{
731
0
    va_list ap;
732
733
0
    va_start (ap, num_params);
734
0
    p = parse_list_of_floats (p, num_params, 0, NULL, ap);
735
0
    va_end (ap);
736
0
    return p;
737
0
}
738
739
static cairo_bool_t
740
get_color (cairo_svg_glyph_render_t *svg_render,
741
           const char               *s,
742
           cairo_svg_color_t        *color)
743
0
{
744
0
    int len, matched;
745
0
    unsigned r = 0, g = 0, b = 0;
746
747
0
    if (!s)
748
0
        return FALSE;
749
750
0
    len = strlen(s);
751
752
0
    if (string_equal (s, "inherit")) {
753
0
  return FALSE;
754
0
    } else if (string_equal (s, "currentColor") ||
755
0
         string_equal (s, "context-fill") ||
756
0
         string_equal (s, "context-stroke"))
757
0
    {
758
0
  *color = svg_render->graphics_state->color;
759
0
        return TRUE;
760
0
    } else if (len > 0 && s[0] == '#') {
761
0
        if (len == 4) {
762
0
            matched = sscanf (s + 1, "%1x%1x%1x", &r, &g, &b);
763
0
            if (matched == 3) {
764
                /* Each digit is repeated to convert to 6 digits. eg 0x123 -> 0x112233 */
765
0
                color->type = RGB;
766
0
                color->red = 0x11*r/255.0;
767
0
                color->green = 0x11*g/255.0;
768
0
                color->blue = 0x11*b/255.0;
769
0
                return TRUE;
770
0
            }
771
0
        } else if (len == 7) {
772
0
            matched = sscanf (s + 1, "%2x%2x%2x", &r, &g, &b);
773
0
            if (matched == 3) {
774
0
                color->type = RGB;
775
0
                color->red = r/255.0;
776
0
                color->green = g/255.0;
777
0
                color->blue = b/255.0;
778
0
                return TRUE;
779
0
            }
780
0
        }
781
0
    } else if (strncmp (s, "rgb", 3) == 0) {
782
0
        matched = sscanf (s, "rgb ( %u , %u , %u )", &r, &g, &b);
783
0
        if (matched == 3) {
784
0
            color->type = RGB;
785
0
            color->red = r/255.0;
786
0
            color->green = g/255.0;
787
0
            color->blue = b/255.0;
788
0
            return TRUE;
789
0
        }
790
0
    } else if (strncmp (s, "var", 3) == 0) {
791
        /* CPAL palettes colors. eg "var(--color0, yellow)" */
792
0
        s += 3;
793
0
        s = skip_char (s, '(');
794
0
        if (!string_match (&s, "--color"))
795
0
            return FALSE;
796
797
0
        char *end;
798
0
        int entry = strtol (s, &end, 10);
799
0
        if (end == s)
800
0
            return FALSE;
801
802
0
        if (svg_render->palette && entry >= 0 && entry < svg_render->num_palette_entries) {
803
0
            FT_Color *palette_color = &svg_render->palette[entry];
804
0
            color->type = RGB;
805
0
            color->red = palette_color->red / 255.0;
806
0
            color->green = palette_color->green/ 255.0;
807
0
            color->blue = palette_color->blue / 255.0;
808
0
            return TRUE;
809
0
        } else {
810
            /* Fallback color */
811
0
            s = skip_char (end, ',');
812
0
            if (!s)
813
0
            return FALSE;
814
815
0
            end = strpbrk(s, WHITE_SPACE_CHARS ")");
816
0
            if (!end || end == s)
817
0
    return FALSE;
818
819
0
            char *fallback = _cairo_strndup (s, end - s);
820
0
            cairo_bool_t success = get_color (svg_render, fallback, color);
821
0
            free (fallback);
822
0
            return success;
823
0
        }
824
0
    } else {
825
0
        const color_name_t *color_name;
826
0
        color_name_t color_name_key;
827
828
0
        color_name_key.name = (char *) s;
829
0
        color_name = bsearch (&color_name_key,
830
0
                              color_names,
831
0
                              ARRAY_LENGTH (color_names),
832
0
                              sizeof (color_name_t),
833
0
                             _color_name_compare);
834
0
        if (color_name) {
835
0
            color->type = RGB;
836
0
            color->red = color_name->red/255.0;
837
0
            color->green = color_name->green/255.0;
838
0
            color->blue = color_name->blue/255.0;
839
0
            return TRUE;
840
0
        }
841
0
    }
842
0
    return FALSE;
843
0
}
844
845
static void
846
get_paint (cairo_svg_glyph_render_t *svg_render,
847
           const char *p,
848
           cairo_svg_paint_t *paint)
849
0
{
850
0
    cairo_svg_element_t *element;
851
852
0
    if (string_match (&p, "none")) {
853
0
        paint->type = PAINT_NONE;
854
0
        paint->paint_server = NULL;
855
0
    } else if (p && strncmp (p, "url", 3) == 0) {
856
0
        element = lookup_url_element (svg_render, p);
857
0
        if (element) {
858
0
            paint->type = PAINT_SERVER;
859
0
            paint->paint_server = element;
860
0
        }
861
0
    } else {
862
0
        if (get_color (svg_render, p, &paint->color)) {
863
0
            paint->type = PAINT_COLOR;
864
0
            paint->paint_server = NULL;
865
0
        }
866
0
    }
867
0
}
868
869
#ifdef SVG_RENDER_PRINT_FUNCTIONS
870
871
static void
872
print_color (cairo_svg_color_t *color)
873
{
874
    switch (color->type) {
875
        case FOREGROUND_COLOR:
876
            printf("foreground");
877
            break;
878
        case RGB:
879
            printf("#%02x%02x%02x",
880
                   (int)(color->red*255),
881
                   (int)(color->red*255),
882
                   (int)(color->red*255));
883
            break;
884
    }
885
}
886
887
static void
888
print_paint (cairo_svg_paint_t *paint)
889
{
890
    printf("Paint: ");
891
    switch (paint->type) {
892
        case PAINT_COLOR:
893
            printf("color: ");
894
            print_color (&paint->color);
895
            break;
896
        case PAINT_SERVER:
897
            printf("server: %s", paint->paint_server->tag);
898
            break;
899
        case PAINT_NONE:
900
            printf("none");
901
            break;
902
    }
903
    printf("\n");
904
}
905
906
#endif
907
908
static void
909
parse_error (cairo_svg_glyph_render_t *svg_render,
910
             const char *string,
911
             const char *location,
912
             const char *fmt,
913
             ...) CAIRO_PRINTF_FORMAT (4, 5);
914
915
static void
916
parse_error (cairo_svg_glyph_render_t *svg_render,
917
             const char *string,
918
             const char *location,
919
             const char *fmt,
920
             ...)
921
0
{
922
0
    va_list ap;
923
0
    const int context = 40;
924
0
    const char *start;
925
0
    const char *end;
926
927
0
    if (svg_render->debug >= SVG_RENDER_ERROR) {
928
0
        printf("ERROR: ");
929
0
  va_start (ap, fmt);
930
0
  vprintf (fmt, ap);
931
0
  va_end (ap);
932
0
        putchar ('\n');
933
0
        start = location - context;
934
0
        if (start < string)
935
0
            start = string;
936
937
0
        end = location + strlen (location);
938
0
        if (end - location > context)
939
0
            end = location + context;
940
941
0
        for (const char *p = start; p < end; p++) {
942
0
            if (_cairo_isspace (*p))
943
0
                putchar (' ');
944
0
            else
945
0
                putchar (*p);
946
0
        }
947
0
        putchar ('\n');
948
949
0
        for (int i = 0; i < location - start; i++)
950
0
            putchar(' ');
951
0
        putchar ('^');
952
0
        putchar ('\n');
953
0
  printf (" at position %td\n", location - string);
954
0
    }
955
0
}
956
957
static cairo_bool_t
958
append_attribute (cairo_svg_element_t *element, svg_attribute_t *attribute)
959
0
{
960
0
    const char *p;
961
0
    const char *end;
962
0
    svg_attribute_t attr;
963
964
0
    memset (&attr, 0, sizeof (attr));
965
0
    if (string_equal (attribute->name, "style")) {
966
        /* split style into individual attributes */
967
0
        p = attribute->value;
968
0
        while (*p) {
969
0
            end = strchr (p, ':');
970
0
            if (!end || end == p)
971
0
                break;
972
0
            attr.name = _cairo_strndup (p, end - p);
973
0
            p = end + 1;
974
0
            p = skip_space(p);
975
0
            end = strchr (p, ';');
976
0
            if (!end)
977
0
                end = strchr (p, 0);
978
0
            if (end == p)
979
0
                goto split_style_fail;
980
981
0
            attr.value = _cairo_strndup (p, end - p);
982
0
            if (*end)
983
0
                p = end + 1;
984
985
0
            if (_cairo_array_append (&element->attributes, &attr))
986
0
                goto split_style_fail;
987
988
0
            memset (&attr, 0, sizeof (attr));
989
0
            p = skip_space (p);
990
0
        }
991
0
    }
992
993
0
    if (_cairo_array_append (&element->attributes, attribute))
994
0
        return FALSE;
995
996
0
    return TRUE;
997
998
0
  split_style_fail:
999
0
    free (attr.name);
1000
0
    free (attr.value);
1001
0
    return FALSE;
1002
0
}
1003
1004
static cairo_bool_t
1005
add_child_element (cairo_svg_glyph_render_t *svg_render,
1006
                   cairo_svg_element_t *parent,
1007
                   cairo_svg_element_t *child)
1008
0
{
1009
0
    cairo_status_t status;
1010
0
    const char* id;
1011
1012
0
    id = get_attribute (child, "id");
1013
0
    if (id) {
1014
0
        child->id = strdup (id);
1015
0
        init_element_id_key (child);
1016
0
  status = _cairo_hash_table_insert (svg_render->ids, &child->base);
1017
0
  if (unlikely (status))
1018
0
            return FALSE;
1019
0
    }
1020
1021
0
    status = _cairo_array_append (&parent->children, &child);
1022
0
    return status == CAIRO_STATUS_SUCCESS;
1023
0
}
1024
1025
static cairo_svg_element_t *
1026
create_element (tag_type_t type, char *tag)
1027
0
{
1028
0
    cairo_svg_element_t *elem;
1029
0
    cairo_status_t status;
1030
1031
0
    elem = _cairo_calloc (sizeof (cairo_svg_element_t));
1032
0
    if (unlikely (elem == NULL)) {
1033
0
  status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
1034
0
        return NULL;
1035
0
    }
1036
1037
0
    elem->type = type;
1038
0
    elem->tag = tag;
1039
0
    elem->id = NULL;
1040
0
    _cairo_array_init (&elem->attributes, sizeof(svg_attribute_t));
1041
0
    _cairo_array_init (&elem->children, sizeof(cairo_svg_element_t *));
1042
0
    _cairo_array_init (&elem->content, sizeof(char));
1043
0
    elem->pattern = NULL;
1044
0
    elem->next = NULL;
1045
1046
0
    return elem;
1047
0
}
1048
1049
static const char *
1050
parse_attributes (cairo_svg_glyph_render_t *svg_render,
1051
                  const char               *attributes,
1052
                  cairo_svg_element_t      *element)
1053
0
{
1054
0
    svg_attribute_t attr;
1055
0
    char quote_char;
1056
0
    const char *p;
1057
0
    const char *end;
1058
1059
0
    p = attributes;
1060
0
    memset (&attr, 0, sizeof (svg_attribute_t));
1061
0
    p = skip_space (p);
1062
0
    while (*p && *p != '/' && *p != '>' && *p != '?') {
1063
0
        end = strpbrk(p, WHITE_SPACE_CHARS "=");
1064
0
        if (!end) {
1065
0
            parse_error (svg_render, attributes, p, "Could not find '='");
1066
0
            goto fail;
1067
0
        }
1068
1069
0
        if (end == p) {
1070
0
            parse_error (svg_render, attributes, p, "Missing attribute name");
1071
0
            goto fail;
1072
0
        }
1073
1074
0
        attr.name = _cairo_strndup (p, end - p);
1075
0
        p = end;
1076
1077
0
        p = skip_space (p);
1078
0
        if (*p != '=') {
1079
0
            parse_error (svg_render, attributes, p, "Expected '='");
1080
0
            goto fail;
1081
0
        }
1082
1083
0
        p++;
1084
0
        p = skip_space (p);
1085
0
        if (*p == '\"' || *p == '\'') {
1086
0
            quote_char = *p;
1087
0
        } else {
1088
0
            parse_error (svg_render, attributes, p, "Could not find '\"' or '''");
1089
0
            goto fail;
1090
0
        }
1091
1092
0
        p++;
1093
0
        end = strchr (p, quote_char);
1094
0
        if (!end) {
1095
0
            parse_error (svg_render, attributes, p, "Could not find '%c'", quote_char);
1096
0
            goto fail;
1097
0
        }
1098
1099
0
        attr.value = _cairo_strndup (p, end - p);
1100
0
        p = end + 1;
1101
1102
0
        if (!append_attribute (element, &attr))
1103
0
            goto fail;
1104
1105
0
        memset (&attr, 0, sizeof (svg_attribute_t));
1106
1107
0
        p = skip_space (p);
1108
0
    }
1109
1110
0
    return p;
1111
1112
0
  fail:
1113
0
    free (attr.name);
1114
0
    free (attr.value);
1115
0
    return NULL;
1116
0
}
1117
1118
static cairo_bool_t
1119
parse_svg (cairo_svg_glyph_render_t *svg_render,
1120
           const char               *svg_document)
1121
0
{
1122
0
    const char *p = svg_document;
1123
0
    const char *end;
1124
0
    int nesting; /* when > 0 we parse content */
1125
0
    cairo_svg_element_t *open_elem; /* Stack of open elements */
1126
0
    cairo_svg_element_t *new_elem = NULL;
1127
0
    char *name;
1128
0
    cairo_status_t status;
1129
1130
    /* Create top level element to use as a container for all top
1131
     * level elements in the document and push it on the stack. */
1132
0
    open_elem = create_element (CONTAINER_ELEMENT, strdup(TOP_ELEMENT_TAG));
1133
1134
    /* We don't want to add content to the top level container. There
1135
     * should only be whitesapce between tags. */
1136
0
    nesting = 0;
1137
1138
0
    while (*p) {
1139
0
        if (nesting > 0) {
1140
            /* In an open element. Anything before the next '<' is content */
1141
0
            end = strchr (p, '<');
1142
0
            if (!end) {
1143
0
                parse_error (svg_render, svg_document, p, "Could not find '<'");
1144
0
                goto fail;
1145
0
            }
1146
0
            status = _cairo_array_append_multiple (&open_elem->content, p, end - p);
1147
0
            p = end;
1148
1149
0
        } else {
1150
0
            p = skip_space (p);
1151
0
            if (*p == 0)
1152
0
                break; /* end of document */
1153
0
        }
1154
1155
        /* We should now be at the start of a tag */
1156
0
        if (*p != '<') {
1157
0
            parse_error (svg_render, svg_document, p, "Could not find '<'");
1158
0
            goto fail;
1159
0
        }
1160
1161
0
        p++;
1162
0
        if (*p == '!') {
1163
0
            p++;
1164
0
            if (string_match (&p, "[CDATA[")) {
1165
0
                new_elem = create_element (CDATA, NULL);
1166
0
                end = strstr (p, "]]>");
1167
0
                if (!end) {
1168
0
                    parse_error (svg_render, svg_document, p, "Could not find ']]>'");
1169
0
                    goto fail;
1170
0
                }
1171
1172
0
                status = _cairo_array_append_multiple (&new_elem->content, p, end - p);
1173
0
                p = end + 3;
1174
0
            } else if (string_match (&p, "--")) {
1175
0
                new_elem = create_element (COMMENT, NULL);
1176
0
                end = strstr (p, "-->");
1177
0
                if (!end) {
1178
0
                    parse_error (svg_render, svg_document, p, "Could not find '-->'");
1179
0
                    goto fail;
1180
0
                }
1181
1182
0
                status = _cairo_array_append_multiple (&new_elem->content, p, end - p);
1183
0
                p = end + 3;
1184
0
            } else if (string_match (&p, "DOCTYPE")) {
1185
0
                new_elem = create_element (DOCTYPE, NULL);
1186
0
                end = strchr (p, '>');
1187
0
                if (!end) {
1188
0
                    parse_error (svg_render, svg_document, p, "Could not find '>'");
1189
0
                    goto fail;
1190
0
                }
1191
1192
0
                status = _cairo_array_append_multiple (&new_elem->content, p, end - p);
1193
0
                p = end + 1;
1194
0
            } else {
1195
0
                parse_error (svg_render, svg_document, p, "Invalid");
1196
0
                goto fail;
1197
0
            }
1198
1199
0
            if (!add_child_element (svg_render, open_elem, new_elem))
1200
0
                goto fail;
1201
1202
0
            new_elem = NULL;
1203
0
            continue;
1204
0
        }
1205
1206
0
        if (*p == '?') {
1207
0
            p++;
1208
0
            new_elem = create_element (PROCESSING_INSTRUCTION, NULL);
1209
0
            end = strstr (p, "?>");
1210
0
            if (!end) {
1211
0
                parse_error (svg_render, svg_document, p, "Could not find '?>'");
1212
0
                goto fail;
1213
0
            }
1214
1215
0
            status = _cairo_array_append_multiple (&new_elem->content, p, end - p);
1216
0
            p = end + 2;
1217
1218
0
            if (!add_child_element (svg_render, open_elem, new_elem))
1219
0
                goto fail;
1220
1221
0
            new_elem = NULL;
1222
0
            continue;
1223
0
        }
1224
1225
0
        if (*p == '/') {
1226
            /* Closing tag */
1227
0
            p++;
1228
1229
            /* find end of tag name */
1230
0
            end = strpbrk(p, WHITE_SPACE_CHARS ">");
1231
0
            if (!end) {
1232
0
                parse_error (svg_render, svg_document, p, "Could not find '>'");
1233
0
                goto fail;
1234
0
            }
1235
1236
0
            name = _cairo_strndup (p, end - p);
1237
0
            p = end;
1238
0
            p = skip_space (p);
1239
0
            if (*p != '>') {
1240
0
                parse_error (svg_render, svg_document, p, "Could not find '>'");
1241
0
                free (name);
1242
0
                goto fail;
1243
0
            }
1244
1245
0
            p++;
1246
0
            if (nesting == 0) {
1247
0
                parse_error (svg_render, svg_document, p, "parse_elements: parsed </%s> but no matching start tag", name);
1248
0
                free (name);
1249
0
                goto fail;
1250
0
            }
1251
0
            if (!string_equal (name, open_elem->tag)) {
1252
0
                parse_error (svg_render, svg_document, p,
1253
0
                             "parse_elements: found </%s> but current open tag is <%s>",
1254
0
                             name, open_elem->tag);
1255
0
                free (name);
1256
0
                goto fail;
1257
0
            }
1258
1259
            /* pop top element on open elements stack into new_elem */
1260
0
            new_elem = open_elem;
1261
0
            open_elem = open_elem->next;
1262
0
            new_elem->next = NULL;
1263
0
            nesting--;
1264
1265
0
            free (name);
1266
0
            if (!add_child_element (svg_render, open_elem, new_elem))
1267
0
                goto fail;
1268
1269
0
            new_elem = NULL;
1270
0
            continue;
1271
0
        }
1272
1273
        /* We should now be in a start or empty element tag */
1274
1275
        /* find end of tag name */
1276
0
        end = strpbrk(p, WHITE_SPACE_CHARS "/>");
1277
0
        if (!end) {
1278
0
            parse_error (svg_render, svg_document, p, "Could not find '>'");
1279
0
            goto fail;
1280
0
        }
1281
1282
0
        name = _cairo_strndup (p, end - p);
1283
0
        p = end;
1284
1285
0
        new_elem = create_element (CONTAINER_ELEMENT, name);
1286
0
        p = parse_attributes (svg_render, p, new_elem);
1287
0
        if (!p)
1288
0
            goto fail;
1289
1290
0
        p = skip_space (p);
1291
0
        if (*p == '/') {
1292
0
            new_elem->type = EMPTY_ELEMENT;
1293
0
            p++;
1294
0
        }
1295
1296
0
        if (!p || *p != '>') {
1297
0
            print_error (svg_render, "Could not find '>'");
1298
0
            goto fail;
1299
0
        }
1300
1301
0
        p++;
1302
0
        if (new_elem->type == EMPTY_ELEMENT) {
1303
0
            if (!add_child_element (svg_render, open_elem, new_elem))
1304
0
                goto fail;
1305
1306
0
            new_elem = NULL;
1307
0
        } else {
1308
            /* push new elem onto open elements stack */
1309
0
            new_elem->next = open_elem;
1310
0
            open_elem = new_elem;
1311
0
            new_elem = NULL;
1312
0
            nesting++;
1313
0
        }
1314
0
    }
1315
1316
0
    if (nesting != 0) {
1317
0
        parse_error (svg_render, svg_document, p, "Missing closing tag for <%s>", open_elem->tag);
1318
0
        goto fail;
1319
0
    }
1320
1321
0
    svg_render->tree = open_elem;
1322
0
    return TRUE;
1323
1324
0
  fail:
1325
0
    if (new_elem)
1326
0
        free_elements (svg_render, new_elem);
1327
1328
0
    while (open_elem) {
1329
0
        cairo_svg_element_t *elem = open_elem;
1330
0
        open_elem = open_elem->next;
1331
0
        free_elements (svg_render, elem);
1332
0
    }
1333
1334
0
    return FALSE;
1335
0
}
1336
1337
static cairo_bool_t
1338
parse_transform (const char *p, cairo_matrix_t *matrix)
1339
0
{
1340
0
    cairo_matrix_t m;
1341
0
    double x, y, a;
1342
0
    cairo_bool_t have_optional;
1343
1344
0
    cairo_matrix_init_identity (matrix);
1345
0
    while (p) {
1346
0
        while (p && (*p == ',' || _cairo_isspace (*p)))
1347
0
            p++;
1348
1349
0
        if (!p || *p == 0)
1350
0
            break;
1351
1352
0
        if (string_match (&p, "matrix")) {
1353
0
            p = skip_char (p, '(');
1354
0
            if (!p)
1355
0
                break;
1356
1357
0
            p = get_floats (p, 6, 0, NULL, &m.xx, &m.yx, &m.xy, &m.yy, &m.x0, &m.y0);
1358
0
            if (!p)
1359
0
                break;
1360
1361
0
            p = skip_char (p, ')');
1362
0
            if (!p)
1363
0
                break;
1364
1365
0
            cairo_matrix_multiply (matrix, &m, matrix);
1366
1367
0
        } else if (string_match (&p, "translate")) {
1368
0
            p = skip_char (p, '(');
1369
0
            if (!p)
1370
0
                break;
1371
1372
0
            p = get_floats (p, 1, 1, &have_optional, &x, &y);
1373
0
            if (!p)
1374
0
                break;
1375
1376
0
            p = skip_char (p, ')');
1377
0
            if (!p)
1378
0
                break;
1379
1380
0
            if (!have_optional)
1381
0
                y = 0;
1382
1383
0
            cairo_matrix_translate (matrix, x, y);
1384
1385
0
        } else if (string_match (&p, "scale")) {
1386
0
            p = skip_char (p, '(');
1387
0
            if (!p)
1388
0
                break;
1389
1390
0
            p = get_floats (p, 1, 1, &have_optional, &x, &y);
1391
0
            if (!p)
1392
0
                break;
1393
1394
0
            p = skip_char (p, ')');
1395
0
            if (!p)
1396
0
                break;
1397
1398
0
            if (!have_optional)
1399
0
                y = x;
1400
1401
0
            cairo_matrix_scale (matrix, x, y);
1402
1403
0
        } else if (string_match (&p, "rotate")) {
1404
0
            p = skip_char (p, '(');
1405
0
            if (!p)
1406
0
                break;
1407
1408
0
            p = get_floats (p, 1, 2, &have_optional, &a, &x, &y);
1409
0
            if (!p)
1410
0
                break;
1411
1412
0
            p = skip_char (p, ')');
1413
0
            if (!p)
1414
0
                break;
1415
1416
0
            if (!have_optional) {
1417
0
                x = 0;
1418
0
                y = 0;
1419
0
            }
1420
1421
0
            a *= M_PI/180.0;
1422
0
            cairo_matrix_translate (matrix, x, y);
1423
0
            cairo_matrix_rotate (matrix, a);
1424
0
            cairo_matrix_translate (matrix, -x, -y);
1425
1426
0
        } else if (string_match (&p, "skewX")) {
1427
0
            p = skip_char (p, '(');
1428
0
            if (!p)
1429
0
                break;
1430
1431
0
            p = get_floats (p, 1, 0, NULL, &a);
1432
0
            if (!p)
1433
0
                break;
1434
1435
0
            p = skip_char (p, ')');
1436
0
            if (!p)
1437
0
                break;
1438
1439
0
            a *= M_PI/180.0;
1440
0
            cairo_matrix_init_identity (&m);
1441
0
            m.xy = tan (a);
1442
0
            cairo_matrix_multiply (matrix, &m, matrix);
1443
1444
0
        } else if (string_match (&p, "skewY")) {
1445
0
            p = skip_char (p, '(');
1446
0
            if (!p)
1447
0
                break;
1448
1449
0
            p = get_floats (p, 1, 0, NULL, &a);
1450
0
            if (!p)
1451
0
                break;
1452
1453
0
            p = skip_char (p, ')');
1454
0
            if (!p)
1455
0
                break;
1456
1457
0
            a *= M_PI/180.0;
1458
0
            cairo_matrix_init_identity (&m);
1459
0
            m.yx = tan (a);
1460
0
            cairo_matrix_multiply (matrix, &m, matrix);
1461
1462
0
        } else {
1463
0
            break;
1464
0
        }
1465
0
    }
1466
0
    return p != NULL;
1467
0
}
1468
1469
static void
1470
render_element_tree (cairo_svg_glyph_render_t *svg_render,
1471
                     cairo_svg_element_t      *element,
1472
                     cairo_svg_element_t      *display_element,
1473
                     cairo_bool_t              children_only);
1474
1475
static cairo_pattern_t *
1476
create_pattern (cairo_svg_glyph_render_t *svg_render,
1477
                cairo_svg_element_t      *paint_server)
1478
0
{
1479
0
    cairo_pattern_t *pattern = NULL;
1480
1481
0
    if (paint_server) {
1482
0
        svg_render->build_pattern.paint_server = paint_server;
1483
0
        render_element_tree (svg_render, paint_server, NULL, FALSE);
1484
0
        pattern = svg_render->build_pattern.pattern;
1485
0
        svg_render->build_pattern.pattern = NULL;
1486
0
        svg_render->build_pattern.paint_server = NULL;
1487
0
        svg_render->build_pattern.type = BUILD_PATTERN_NONE;
1488
0
    }
1489
1490
0
    if (!pattern)
1491
0
        pattern = cairo_pattern_create_rgb (0, 0, 0);
1492
1493
0
    return pattern;
1494
0
}
1495
1496
static cairo_bool_t
1497
render_element_svg (cairo_svg_glyph_render_t *svg_render,
1498
                    cairo_svg_element_t      *element,
1499
                    cairo_bool_t              end_tag)
1500
0
{
1501
0
    double width, height;
1502
0
    double vb_x, vb_y, vb_height, vb_width;
1503
0
    const char *p;
1504
0
    const char *end;
1505
1506
0
    if (end_tag)
1507
0
        return FALSE;
1508
1509
    /* Default viewport width, height is EM square */
1510
0
    if (!get_float_or_percent_attribute (element, "width", svg_render->units_per_em, &width))
1511
0
        width = svg_render->units_per_em;
1512
1513
0
    if (!get_float_or_percent_attribute (element, "height", svg_render->units_per_em, &height))
1514
0
        height = svg_render->units_per_em;
1515
1516
    /* Transform viewport to unit square, centering it if width != height. */
1517
0
    if (width > height) {
1518
0
        cairo_scale (svg_render->cr, 1.0/width, 1.0/width);
1519
0
        cairo_translate (svg_render->cr, 0, (width - height)/2.0);
1520
0
    } else {
1521
0
        cairo_scale (svg_render->cr, 1.0/height, 1.0/height);
1522
0
        cairo_translate (svg_render->cr, (height - width)/2.0, 0);
1523
0
    }
1524
1525
0
    svg_render->width = width;
1526
0
    svg_render->height = height;
1527
1528
0
    p = get_attribute (element, "viewBox");
1529
0
    if (p) {
1530
        /* Transform viewport to viewbox */
1531
0
        end = get_path_params (p, 4, &vb_x, &vb_y, &vb_width, &vb_height);
1532
0
        if (!end) {
1533
0
            print_warning (svg_render, "viewBox expected 4 numbers: %s", p);
1534
0
            return FALSE;
1535
0
        }
1536
0
        cairo_translate (svg_render->cr, -vb_x * width/vb_width, -vb_y * width/vb_width);
1537
0
        cairo_scale (svg_render->cr, width/vb_width, height/vb_height);
1538
0
        svg_render->width = vb_width;
1539
0
        svg_render->height = vb_height;
1540
0
    }
1541
1542
0
    svg_render->view_port_set = TRUE;
1543
0
    return TRUE;
1544
0
}
1545
1546
static cairo_bool_t
1547
render_element_clip_path (cairo_svg_glyph_render_t *svg_render,
1548
                          cairo_svg_element_t      *element,
1549
                          cairo_bool_t              end_tag)
1550
0
{
1551
0
    cairo_svg_graphics_state_t *gs = svg_render->graphics_state;
1552
0
    const char *p;
1553
1554
0
    if (end_tag || gs->mode != GS_CLIP || svg_render->build_pattern.type != BUILD_PATTERN_NONE) {
1555
0
        return FALSE;
1556
0
    }
1557
1558
0
    p = get_attribute (element, "clipPathUnits");
1559
0
    if (string_equal (p, "objectBoundingBox")) {
1560
0
        cairo_translate (svg_render->cr,
1561
0
                                svg_render->graphics_state->bbox.x,
1562
0
                                svg_render->graphics_state->bbox.y);
1563
0
        cairo_scale (svg_render->cr,
1564
0
                     svg_render->graphics_state->bbox.width,
1565
0
                     svg_render->graphics_state->bbox.height);
1566
0
    }
1567
1568
0
    return TRUE;
1569
0
}
1570
1571
static void
1572
apply_gradient_attributes (cairo_svg_glyph_render_t *svg_render,
1573
                           cairo_svg_element_t      *element)
1574
0
{
1575
0
    cairo_pattern_t *pattern = svg_render->build_pattern.pattern;
1576
0
    cairo_bool_t object_bbox = TRUE;
1577
0
    cairo_matrix_t transform;
1578
0
    cairo_matrix_t mat;
1579
0
    const char *p;
1580
1581
0
    if (!pattern)
1582
0
        return;
1583
1584
0
    p = get_attribute (element, "gradientUnits");
1585
0
    if (string_equal (p, "userSpaceOnUse"))
1586
0
        object_bbox = FALSE;
1587
1588
0
    cairo_matrix_init_identity (&mat);
1589
0
    if (object_bbox) {
1590
0
        cairo_matrix_translate (&mat,
1591
0
                                svg_render->graphics_state->bbox.x,
1592
0
                                svg_render->graphics_state->bbox.y);
1593
0
        cairo_matrix_scale (&mat,
1594
0
                            svg_render->graphics_state->bbox.width,
1595
0
                            svg_render->graphics_state->bbox.height);
1596
0
    }
1597
1598
0
    p = get_attribute (element, "gradientTransform");
1599
0
     if (parse_transform (p, &transform))
1600
0
         cairo_matrix_multiply (&mat, &transform, &mat);
1601
1602
0
    if (cairo_matrix_invert (&mat) == CAIRO_STATUS_SUCCESS)
1603
0
        cairo_pattern_set_matrix (pattern, &mat);
1604
1605
0
    p = get_attribute (element, "spreadMethod");
1606
0
    if (string_equal (p, "reflect"))
1607
0
        cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REFLECT);
1608
0
    else if (string_equal (p, "repeat"))
1609
0
        cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT);
1610
0
}
1611
1612
static cairo_bool_t
1613
render_element_linear_gradient (cairo_svg_glyph_render_t *svg_render,
1614
                                cairo_svg_element_t      *element,
1615
                                cairo_bool_t              end_tag)
1616
0
{
1617
0
    double x1, y1, x2, y2;
1618
1619
0
    if (svg_render->build_pattern.paint_server != element ||
1620
0
        end_tag ||
1621
0
        svg_render->build_pattern.type != BUILD_PATTERN_NONE)
1622
0
        return FALSE;
1623
1624
    /* FIXME default value for userSpaceOnUse? */
1625
0
    double width = 1.0;
1626
0
    double height = 1.0;
1627
1628
0
    if (!get_float_or_percent_attribute (element, "x1", width, &x1))
1629
0
        x1 = 0.0;
1630
1631
0
    if (!get_float_or_percent_attribute (element, "y1", height, &y1))
1632
0
        y1 = 0.0;
1633
1634
0
    if (!get_float_or_percent_attribute (element, "x2", width, &x2))
1635
0
        x2 = width;
1636
1637
0
    if (!get_float_or_percent_attribute (element, "y2", height, &y2))
1638
0
        y2 = 0.0;
1639
1640
0
    if (svg_render->build_pattern.pattern)
1641
0
        abort();
1642
1643
0
    svg_render->build_pattern.pattern = cairo_pattern_create_linear (x1, y1, x2, y2);
1644
0
    svg_render->build_pattern.type = BUILD_PATTERN_LINEAR;
1645
0
    apply_gradient_attributes (svg_render, element);
1646
0
    return TRUE;
1647
0
}
1648
1649
static cairo_bool_t
1650
render_element_radial_gradient (cairo_svg_glyph_render_t *svg_render,
1651
                                cairo_svg_element_t      *element,
1652
                                cairo_bool_t              end_tag)
1653
0
{
1654
0
    double cx, cy, r, fx, fy;
1655
1656
0
    if (svg_render->build_pattern.paint_server != element ||
1657
0
        end_tag ||
1658
0
        svg_render->build_pattern.type != BUILD_PATTERN_NONE)
1659
0
        return FALSE;
1660
1661
    /* FIXME default value for userSpaceOnUse? */
1662
0
    double width = 1.0;
1663
0
    double height = 1.0;
1664
1665
0
    if (!get_float_or_percent_attribute (element, "cx", width, &cx))
1666
0
        cx = 0.5 * width;
1667
1668
0
    if (!get_float_or_percent_attribute (element, "cy", height, &cy))
1669
0
        cy = 0.5 * height;
1670
1671
0
    if (!get_float_or_percent_attribute (element, "r", width, &r))
1672
0
        r = 0.5 * width;
1673
1674
0
    if (!get_float_or_percent_attribute (element, "fx", width, &fx))
1675
0
        fx = cx;
1676
1677
0
    if (!get_float_or_percent_attribute (element, "fy", height, &fy))
1678
0
        fy = cy;
1679
1680
0
    svg_render->build_pattern.pattern = cairo_pattern_create_radial (fx, fy, 0, cx, cy, r);
1681
0
    svg_render->build_pattern.type = BUILD_PATTERN_RADIAL;
1682
0
    apply_gradient_attributes (svg_render, element);
1683
0
    return TRUE;
1684
0
}
1685
1686
static cairo_bool_t
1687
render_element_stop (cairo_svg_glyph_render_t *svg_render,
1688
                     cairo_svg_element_t      *element,
1689
                     cairo_bool_t              end_tag)
1690
0
{
1691
0
    double offset, opacity;
1692
0
    cairo_pattern_t *pattern = svg_render->build_pattern.pattern;
1693
1694
0
    if (!pattern)
1695
0
        return FALSE;
1696
1697
0
    if (cairo_pattern_get_type (pattern) != CAIRO_PATTERN_TYPE_LINEAR &&
1698
0
        cairo_pattern_get_type (pattern) != CAIRO_PATTERN_TYPE_RADIAL)
1699
0
        return FALSE;
1700
1701
0
    if (!get_float_or_percent_attribute (element, "offset", 1.0, &offset))
1702
0
        return FALSE;
1703
1704
0
    if (!get_float_attribute (element, "stop-opacity", &opacity))
1705
0
        opacity = 1.0;
1706
1707
0
    cairo_svg_color_t color;
1708
0
    get_color (svg_render, "black", &color);
1709
0
    get_color (svg_render, get_attribute(element, "stop-color"), &color);
1710
0
    if (color.type == RGB) {
1711
0
        cairo_pattern_add_color_stop_rgba (pattern,
1712
0
                                           offset,
1713
0
                                           color.red,
1714
0
                                           color.green,
1715
0
                                           color.blue,
1716
0
                                           opacity);
1717
0
    } else { /* color.type == FOREGROUND */
1718
0
        double red, green, blue, alpha;
1719
0
        if (cairo_pattern_get_rgba (svg_render->foreground_source, &red, &green, &blue, &alpha) == CAIRO_STATUS_SUCCESS) {
1720
0
      svg_render->foreground_source_used = TRUE;
1721
0
  } else {
1722
0
            red = green = blue = 0;
1723
0
            alpha = 1;
1724
0
        }
1725
0
        cairo_pattern_add_color_stop_rgba (pattern, offset, red, green, blue, alpha);
1726
0
    }
1727
0
    return TRUE;
1728
0
}
1729
1730
static cairo_bool_t
1731
render_element_g (cairo_svg_glyph_render_t *svg_render,
1732
                  cairo_svg_element_t      *element,
1733
                  cairo_bool_t              end_tag)
1734
0
{
1735
0
    if (svg_render->graphics_state->mode == GS_NO_RENDER ||
1736
0
        svg_render->build_pattern.type != BUILD_PATTERN_NONE)
1737
0
        return FALSE;
1738
1739
0
    if (!end_tag) {
1740
0
        cairo_push_group (svg_render->cr);
1741
0
    } else {
1742
0
        cairo_pop_group_to_source (svg_render->cr);
1743
0
        cairo_paint_with_alpha (svg_render->cr, svg_render->graphics_state->opacity);
1744
0
    }
1745
0
    return TRUE;
1746
0
}
1747
1748
typedef struct {
1749
    const char *data; /* current position in base64 data */
1750
    char buf[3]; /* decode buffer */
1751
    int buf_pos; /* current position in buf_pos. */
1752
} base64_decode_t;
1753
1754
static cairo_status_t
1755
_read_png_from_base64 (void *closure, unsigned char *data, unsigned int length)
1756
0
{
1757
0
    base64_decode_t *decode = closure;
1758
0
    int n, c;
1759
0
    unsigned val;
1760
1761
0
    while (length) {
1762
0
        if (decode->buf_pos >= 0) {
1763
0
            *data++ = decode->buf[decode->buf_pos++];
1764
0
            length--;
1765
0
            if (decode->buf_pos == 3)
1766
0
                decode->buf_pos = -1;
1767
0
        }
1768
0
        if (length > 0 && decode->buf_pos < 0) {
1769
0
            n = 0;
1770
0
            val = 0;
1771
0
            while (*decode->data && n < 4) {
1772
0
                c = *decode->data++;
1773
0
                if (c >='A' && c <='Z') {
1774
0
                    val = (val << 6) | (c -'A');
1775
0
                    n++;
1776
0
                } else if (c >='a' && c <='z') {
1777
0
                    val = (val << 6) | (c -'a' + 26);
1778
0
                    n++;
1779
0
                } else if (c >='0' && c <='9') {
1780
0
                    val = (val << 6) | (c -'0' + 52);
1781
0
                    n++;
1782
0
                } else if (c =='+') {
1783
0
                    val = (val << 6) | 62;
1784
0
                    n++;
1785
0
                } else if (c =='/') {
1786
0
                    val = (val << 6) | 63;
1787
0
                    n++;
1788
0
                } else if (c == '=') {
1789
0
                    val = (val << 6);
1790
0
                    n++;
1791
0
                }
1792
0
            }
1793
0
            if (n < 4)
1794
0
                return CAIRO_STATUS_READ_ERROR;
1795
1796
0
            decode->buf[0] = val >> 16;
1797
0
            decode->buf[1] = val >> 8;
1798
0
            decode->buf[2] = val >> 0;
1799
0
            decode->buf_pos = 0;
1800
0
        }
1801
0
    }
1802
1803
0
    return CAIRO_STATUS_SUCCESS;
1804
0
}
1805
1806
static cairo_bool_t
1807
render_element_image (cairo_svg_glyph_render_t *svg_render,
1808
                      cairo_svg_element_t      *element,
1809
                      cairo_bool_t              end_tag)
1810
0
{
1811
0
    double x, y, width, height;
1812
0
    int w, h;
1813
0
    const char *data;
1814
0
    cairo_surface_t *surface;
1815
0
    base64_decode_t decode;
1816
1817
0
    if (svg_render->graphics_state->mode == GS_NO_RENDER ||
1818
0
        svg_render->build_pattern.type != BUILD_PATTERN_NONE)
1819
0
        return FALSE;
1820
1821
0
    if (!get_float_attribute (element, "x", &x))
1822
0
        x = 0;
1823
1824
0
    if (!get_float_attribute (element, "y", &y))
1825
0
        y = 0;
1826
1827
0
    if (!get_float_attribute (element, "width", &width))
1828
0
        return FALSE;
1829
1830
0
    if (!get_float_attribute (element, "height", &height))
1831
0
        return FALSE;
1832
1833
0
    data = get_href_attribute (element);
1834
0
    if (!data)
1835
0
        return FALSE;
1836
1837
0
    if (!string_match (&data, "data:image/png;base64,"))
1838
0
        return FALSE;
1839
1840
0
    decode.data = data;
1841
0
    decode.buf_pos = -1;
1842
0
    surface = cairo_image_surface_create_from_png_stream (_read_png_from_base64, &decode);
1843
0
    if (cairo_surface_status (surface)) {
1844
0
        print_warning (svg_render, "Unable to decode PNG");
1845
0
        cairo_surface_destroy (surface);
1846
0
        return FALSE;
1847
0
    }
1848
1849
0
    w = cairo_image_surface_get_width (surface);
1850
0
    h = cairo_image_surface_get_height (surface);
1851
1852
0
    if (w > 0 && h > 0) {
1853
0
        cairo_translate (svg_render->cr, x, y);
1854
0
        cairo_scale (svg_render->cr, width/w, height/h);
1855
0
        cairo_set_source_surface (svg_render->cr, surface, 0, 0);
1856
0
        cairo_paint (svg_render->cr);
1857
0
    }
1858
1859
0
    cairo_surface_destroy (surface);
1860
1861
0
    return FALSE;
1862
0
}
1863
1864
static cairo_bool_t
1865
render_element_use (cairo_svg_glyph_render_t *svg_render,
1866
                    cairo_svg_element_t      *element,
1867
                    cairo_bool_t              end_tag)
1868
0
{
1869
0
    double x = 0;
1870
0
    double y = 0;
1871
0
    const char *id;
1872
1873
0
    if (end_tag || svg_render->graphics_state->mode == GS_NO_RENDER ||
1874
0
        svg_render->build_pattern.type != BUILD_PATTERN_NONE)
1875
0
        return FALSE;
1876
1877
0
    get_float_attribute (element, "x", &x);
1878
0
    get_float_attribute (element, "y", &y);
1879
1880
0
    id = get_href_attribute (element);
1881
0
    if (!id)
1882
0
        return FALSE;
1883
1884
0
    cairo_svg_element_t *use_element = lookup_element (svg_render, id);
1885
0
    cairo_translate (svg_render->cr, x, y);
1886
0
    render_element_tree (svg_render, use_element, NULL, FALSE);
1887
0
    return TRUE;
1888
0
}
1889
1890
static cairo_bool_t
1891
draw_path (cairo_svg_glyph_render_t *svg_render)
1892
0
{
1893
0
    cairo_svg_graphics_state_t *gs = svg_render->graphics_state;
1894
0
    cairo_pattern_t *pattern;
1895
0
    cairo_bool_t opacity_group = FALSE;
1896
1897
0
    if (gs->mode == GS_COMPUTE_BBOX) {
1898
0
        cairo_set_source_rgb (svg_render->cr, 0, 0, 0);
1899
0
        cairo_set_fill_rule (svg_render->cr, gs->fill_rule);
1900
0
        cairo_fill (svg_render->cr);
1901
0
        return FALSE;
1902
0
    } else if (gs->mode == GS_CLIP) {
1903
0
        return FALSE;
1904
0
    }
1905
1906
0
    if (gs->opacity < 1.0) {
1907
0
        cairo_push_group (svg_render->cr);
1908
0
        opacity_group = TRUE;
1909
0
    }
1910
1911
0
    cairo_path_t *path = cairo_copy_path (svg_render->cr);
1912
0
    cairo_new_path (svg_render->cr);
1913
1914
0
    if (gs->fill.type != PAINT_NONE) {
1915
0
        cairo_bool_t group = FALSE;
1916
0
        if (gs->fill.type == PAINT_COLOR) {
1917
0
            if (gs->fill.color.type == RGB) {
1918
0
                cairo_set_source_rgba (svg_render->cr,
1919
0
                                       gs->fill.color.red,
1920
0
                                       gs->fill.color.green,
1921
0
                                       gs->fill.color.blue,
1922
0
                                       gs->fill_opacity);
1923
0
            } else if (gs->fill.color.type == FOREGROUND) {
1924
0
    cairo_set_source (svg_render->cr, svg_render->foreground_marker);
1925
0
    if (gs->fill_opacity < 1.0)
1926
0
        group = TRUE;
1927
0
            }
1928
0
        } else if (gs->fill.type == PAINT_SERVER) {
1929
0
            pattern = create_pattern (svg_render, gs->fill.paint_server);
1930
0
            cairo_set_source (svg_render->cr, pattern);
1931
0
            cairo_pattern_destroy (pattern);
1932
0
            if (gs->fill_opacity < 1.0)
1933
0
                group = TRUE;
1934
0
        }
1935
1936
0
        if (group)
1937
0
            cairo_push_group (svg_render->cr);
1938
1939
0
        cairo_append_path (svg_render->cr, path);
1940
0
        cairo_set_fill_rule (svg_render->cr, gs->fill_rule);
1941
0
        cairo_fill (svg_render->cr);
1942
0
        if (group) {
1943
0
            cairo_pop_group_to_source (svg_render->cr);
1944
0
            cairo_paint_with_alpha (svg_render->cr, gs->fill_opacity);
1945
0
        }
1946
0
    }
1947
1948
0
    if (gs->stroke.type != PAINT_NONE) {
1949
0
        cairo_bool_t group = FALSE;
1950
0
        if (gs->stroke.type == PAINT_COLOR) {
1951
0
            if (gs->stroke.color.type == RGB) {
1952
0
                cairo_set_source_rgba (svg_render->cr,
1953
0
                                       gs->stroke.color.red,
1954
0
                                       gs->stroke.color.green,
1955
0
                                       gs->stroke.color.blue,
1956
0
                                       gs->stroke_opacity);
1957
0
            } else if (gs->fill.color.type == FOREGROUND) {
1958
0
    cairo_set_source (svg_render->cr, svg_render->foreground_marker);
1959
0
    if (gs->fill_opacity < 1.0)
1960
0
        group = TRUE;
1961
0
            }
1962
0
        } else if (gs->stroke.type == PAINT_SERVER) {
1963
0
            pattern = create_pattern (svg_render, gs->stroke.paint_server);
1964
0
            cairo_set_source (svg_render->cr, pattern);
1965
0
            cairo_pattern_destroy (pattern);
1966
0
            if (gs->stroke_opacity < 1.0)
1967
0
                group = TRUE;
1968
0
        }
1969
1970
0
        if (group)
1971
0
            cairo_push_group (svg_render->cr);
1972
1973
0
        cairo_append_path (svg_render->cr, path);
1974
0
        cairo_stroke (svg_render->cr);
1975
1976
0
        if (group) {
1977
0
            cairo_pop_group_to_source (svg_render->cr);
1978
0
            cairo_paint_with_alpha (svg_render->cr, gs->stroke_opacity);
1979
0
        }
1980
0
    }
1981
1982
0
    cairo_path_destroy (path);
1983
1984
0
    if (opacity_group) {
1985
0
        cairo_pop_group_to_source (svg_render->cr);
1986
0
        cairo_paint_with_alpha (svg_render->cr, gs->opacity);
1987
0
    }
1988
0
    return TRUE;
1989
0
}
1990
1991
static void
1992
elliptical_arc (cairo_svg_glyph_render_t *svg_render,
1993
                double                    cx,
1994
                double                    cy,
1995
                double                    rx,
1996
                double                    ry,
1997
                double                    angle1,
1998
                double                    angle2)
1999
0
{
2000
0
    cairo_save (svg_render->cr);
2001
0
    cairo_translate (svg_render->cr, cx, cy);
2002
0
    cairo_scale (svg_render->cr, rx, ry);
2003
0
    cairo_arc (svg_render->cr, 0, 0, 1, angle1, angle2);
2004
0
    cairo_restore (svg_render->cr);
2005
0
}
2006
2007
static cairo_bool_t
2008
render_element_rect (cairo_svg_glyph_render_t *svg_render,
2009
                     cairo_svg_element_t      *element,
2010
                     cairo_bool_t              end_tag)
2011
0
{
2012
0
    double x = 0;
2013
0
    double y = 0;
2014
0
    double width = svg_render->width;
2015
0
    double height = svg_render->height;
2016
0
    double rx = 0;
2017
0
    double ry = 0;
2018
2019
0
    if (end_tag ||
2020
0
        svg_render->graphics_state->mode == GS_NO_RENDER ||
2021
0
        svg_render->build_pattern.type != BUILD_PATTERN_NONE)
2022
0
        return FALSE;
2023
2024
0
    get_float_or_percent_attribute (element, "x", svg_render->width, &x);
2025
0
    get_float_or_percent_attribute (element, "y", svg_render->height, &y);
2026
0
    get_float_or_percent_attribute (element, "width", svg_render->width, &width);
2027
0
    get_float_or_percent_attribute (element, "height", svg_render->height, &height);
2028
0
    get_float_or_percent_attribute (element, "rx", svg_render->width, &rx);
2029
0
    get_float_or_percent_attribute (element, "ry", svg_render->height, &ry);
2030
2031
0
    if (rx == 0 && ry == 0) {
2032
0
        cairo_rectangle (svg_render->cr, x, y, width, height);
2033
0
    } else {
2034
0
        cairo_move_to (svg_render->cr, x + rx, y);
2035
0
        cairo_line_to (svg_render->cr, x + width - rx, y);
2036
0
        elliptical_arc (svg_render,    x + width - rx, y + ry, rx, ry, -M_PI/2, 0);
2037
0
        cairo_line_to (svg_render->cr, x + width, y + height - ry);
2038
0
        elliptical_arc (svg_render,    x + width - rx, y + height - ry, rx, ry, 0, M_PI/2);
2039
0
        cairo_line_to (svg_render->cr, x + rx, y + height);
2040
0
        elliptical_arc (svg_render,    x + rx, y + height - ry, rx, ry, M_PI/2, M_PI);
2041
0
        cairo_line_to (svg_render->cr, x, y + ry);
2042
0
        elliptical_arc (svg_render,    x + rx, y + ry, rx, ry, M_PI, -M_PI/2);
2043
0
    }
2044
2045
0
    draw_path (svg_render);
2046
0
    return TRUE;
2047
0
}
2048
2049
static cairo_bool_t
2050
render_element_circle (cairo_svg_glyph_render_t *svg_render,
2051
                       cairo_svg_element_t      *element,
2052
                       cairo_bool_t              end_tag)
2053
0
{
2054
0
    double cx = 0;
2055
0
    double cy = 0;
2056
0
    double r = 0;
2057
2058
0
    if (end_tag ||
2059
0
        svg_render->graphics_state->mode == GS_NO_RENDER ||
2060
0
        svg_render->build_pattern.type != BUILD_PATTERN_NONE)
2061
0
        return FALSE;
2062
2063
0
    get_float_or_percent_attribute (element, "cx", svg_render->width, &cx);
2064
0
    get_float_or_percent_attribute (element, "cy", svg_render->height, &cy);
2065
0
    get_float_or_percent_attribute (element, "r", svg_render->width, &r);
2066
2067
0
    cairo_arc (svg_render->cr, cx, cy, r, 0, 2*M_PI);
2068
2069
0
    draw_path (svg_render);
2070
0
    return TRUE;
2071
0
}
2072
2073
static cairo_bool_t
2074
render_element_ellipse (cairo_svg_glyph_render_t *svg_render,
2075
                        cairo_svg_element_t      *element,
2076
                        cairo_bool_t              end_tag)
2077
0
{
2078
0
    double cx = 0;
2079
0
    double cy = 0;
2080
0
    double rx = 0;
2081
0
    double ry = 0;
2082
2083
0
    if (end_tag ||
2084
0
        svg_render->graphics_state->mode == GS_NO_RENDER ||
2085
0
        svg_render->build_pattern.type != BUILD_PATTERN_NONE)
2086
0
        return FALSE;
2087
2088
0
    get_float_or_percent_attribute (element, "cx", svg_render->width, &cx);
2089
0
    get_float_or_percent_attribute (element, "cy", svg_render->height, &cy);
2090
0
    get_float_or_percent_attribute (element, "rx", svg_render->width, &rx);
2091
0
    get_float_or_percent_attribute (element, "ry", svg_render->height, &ry);
2092
2093
0
    elliptical_arc (svg_render, cx, cy, rx, ry, 0, 2*M_PI);
2094
0
    draw_path (svg_render);
2095
0
    return TRUE;
2096
0
}
2097
2098
static cairo_bool_t
2099
render_element_line (cairo_svg_glyph_render_t *svg_render,
2100
                     cairo_svg_element_t      *element,
2101
                     cairo_bool_t              end_tag)
2102
0
{
2103
0
    double x1 = 0;
2104
0
    double y1 = 0;
2105
0
    double x2 = 0;
2106
0
    double y2 = 0;
2107
2108
0
    if (end_tag ||
2109
0
        svg_render->graphics_state->mode == GS_NO_RENDER ||
2110
0
        svg_render->build_pattern.type != BUILD_PATTERN_NONE)
2111
0
        return FALSE;
2112
2113
0
    get_float_or_percent_attribute (element, "x1", svg_render->width, &x1);
2114
0
    get_float_or_percent_attribute (element, "y1", svg_render->height, &y1);
2115
0
    get_float_or_percent_attribute (element, "x2", svg_render->width, &x2);
2116
0
    get_float_or_percent_attribute (element, "y2", svg_render->height, &y2);
2117
2118
0
    cairo_move_to (svg_render->cr, x1, y1);
2119
0
    cairo_line_to (svg_render->cr, x2, y2);
2120
2121
0
    draw_path (svg_render);
2122
0
    return TRUE;
2123
0
}
2124
2125
static cairo_bool_t
2126
render_element_polyline (cairo_svg_glyph_render_t *svg_render,
2127
                         cairo_svg_element_t      *element,
2128
                         cairo_bool_t              end_tag)
2129
0
{
2130
0
    const char *p;
2131
0
    const char *end;
2132
0
    double x, y;
2133
0
    cairo_bool_t have_move = FALSE;
2134
2135
0
    if (end_tag ||
2136
0
        svg_render->graphics_state->mode == GS_NO_RENDER ||
2137
0
        svg_render->build_pattern.type != BUILD_PATTERN_NONE)
2138
0
        return FALSE;
2139
2140
0
    p = get_attribute (element, "points");
2141
0
    do {
2142
0
        end = get_path_params (p, 2, &x, &y);
2143
0
        if (!end) {
2144
0
            print_warning (svg_render, "points expected 2 numbers: %s", p);
2145
0
            break;
2146
0
        }
2147
0
        p = end;
2148
0
        if (!have_move) {
2149
0
            cairo_move_to (svg_render->cr, x, y);
2150
0
            have_move = TRUE;
2151
0
        } else {
2152
0
            cairo_line_to (svg_render->cr, x, y);
2153
0
        }
2154
0
        p = skip_space (p);
2155
0
    } while (p && *p);
2156
2157
0
    if (string_equal (element->tag, "polygon"))
2158
0
        cairo_close_path (svg_render->cr);
2159
2160
0
    draw_path (svg_render);
2161
0
    return TRUE;
2162
0
}
2163
2164
static double
2165
angle_between_vectors (double ux,
2166
                       double uy,
2167
                       double vx,
2168
                       double vy)
2169
0
{
2170
0
    double dot = ux*vx + uy*vy;
2171
0
    double umag = sqrt (ux*ux + uy*uy);
2172
0
    double vmag = sqrt (vx*vx + vy*vy);
2173
0
    double c = dot/(umag*vmag);
2174
0
    if (c > 1.0)
2175
0
        c = 1.0;
2176
2177
0
    if (c < -1.0)
2178
0
        c = -1.0;
2179
2180
0
    double a = acos (c);
2181
0
    if (ux * vy - uy * vx < 0.0)
2182
0
        a = -a;
2183
2184
0
    return a;
2185
0
}
2186
2187
static void
2188
arc_path (cairo_t *cr,
2189
          double x1, double y1,
2190
          double x2, double y2,
2191
          double rx, double ry,
2192
          double rotate,
2193
          cairo_bool_t large_flag,
2194
          cairo_bool_t sweep_flag)
2195
0
{
2196
0
    double x1_, y1_, cx_, cy_;
2197
0
    double xm, ym, cx, cy;
2198
0
    double a, b, d;
2199
0
    double ux, uy, vx, vy;
2200
0
    double theta, delta_theta;
2201
0
    double epsilon;
2202
0
    cairo_matrix_t ctm;
2203
2204
0
    cairo_get_matrix (cr, &ctm);
2205
0
    epsilon = _cairo_matrix_transformed_circle_major_axis (&ctm, cairo_get_tolerance (cr));
2206
2207
0
    rotate *= M_PI/180.0;
2208
2209
    /* Convert endpoint to center parameterization.
2210
     * See SVG 1.1 Appendix F.6. Step numbers are the steps in the appendix.
2211
     */
2212
2213
0
    rx = fabs (rx);
2214
0
    ry = fabs (ry);
2215
0
    if (rx < epsilon || ry < epsilon) {
2216
0
        cairo_line_to (cr, x2, y2);
2217
0
        return;
2218
0
    }
2219
2220
0
    if (fabs(x1 - x2) < epsilon && fabs(y1 - y2) < epsilon) {
2221
0
        cairo_line_to (cr, x2, y2);
2222
0
        return;
2223
0
    }
2224
2225
    /* Step 1 */
2226
0
    xm = (x1 - x2)/2;
2227
0
    ym = (y1 - y2)/2;
2228
0
    x1_ = xm * cos (rotate) + ym * sin (rotate);
2229
0
    y1_ = xm * -sin (rotate) + ym * cos (rotate);
2230
2231
0
    d = (x1_*x1_)/(rx*rx) + (y1_*y1_)/(ry*ry);
2232
0
    if (d > 1.0) {
2233
0
        d = sqrt (d);
2234
0
        rx *= d;
2235
0
        ry *= d;
2236
0
    }
2237
2238
    /* Step 2 */
2239
0
    a = (rx*rx * y1_*y1_) + (ry*ry * x1_*x1_);
2240
0
    if (a == 0.0)
2241
0
        return;
2242
2243
0
    b = (rx*rx * ry*ry) / a - 1.0;
2244
0
    if (b < 0)
2245
0
        b = 0.0;
2246
2247
0
    d = sqrt(b);
2248
0
    if (large_flag == sweep_flag)
2249
0
        d = -d;
2250
2251
0
    cx_ = d * rx*y1_/ry;
2252
0
    cy_ = d * -ry*x1_/rx;
2253
2254
    /* Step 3 */
2255
0
    cx = cx_ * cos (rotate) - cy_ * sin (rotate) + (x1 + x2)/2;
2256
0
    cy = cx_ * sin (rotate) + cy_ * cos (rotate) + (y1 + y2)/2;
2257
2258
    /* Step 4 */
2259
0
    ux = (x1_ - cx_)/rx;
2260
0
    uy = (y1_ - cy_)/ry;
2261
0
    vx = (-x1_ - cx_)/rx;
2262
0
    vy = (-y1_ - cy_)/ry;
2263
0
    theta = angle_between_vectors (1.0, 0, ux, uy);
2264
0
    delta_theta = angle_between_vectors (ux, uy, vx, vy);
2265
2266
0
    if (!sweep_flag && delta_theta > 0)
2267
0
        delta_theta -= 2 * M_PI;
2268
0
    else if (sweep_flag && delta_theta < 0)
2269
0
        delta_theta += 2 * M_PI;
2270
2271
    /* Now we can call cairo_arc() */
2272
0
    cairo_save (cr);
2273
0
    cairo_translate (cr, cx, cy);
2274
0
    cairo_scale (cr, rx, ry);
2275
0
    cairo_rotate (cr, theta);
2276
0
    if (delta_theta >= 0.0)
2277
0
        cairo_arc (cr, 0, 0, 1, 0, delta_theta);
2278
0
    else
2279
0
        cairo_arc_negative (cr, 0, 0, 1, 0, delta_theta);
2280
0
    cairo_restore (cr);
2281
0
}
2282
2283
static void
2284
get_current_point (cairo_svg_glyph_render_t *svg_render, double *x, double *y)
2285
0
{
2286
0
    if (cairo_has_current_point (svg_render->cr)) {
2287
0
        cairo_get_current_point (svg_render->cr, x, y);
2288
0
    } else {
2289
0
        *x = 0;
2290
0
        *y = 0;
2291
0
    }
2292
0
}
2293
2294
static void
2295
reflect_point (double origin_x, double origin_y, double *x, double *y)
2296
0
{
2297
0
    *x = 2*origin_x - *x;
2298
0
    *y = 2*origin_y - *y;
2299
0
}
2300
2301
static cairo_bool_t
2302
render_element_path (cairo_svg_glyph_render_t *svg_render,
2303
                     cairo_svg_element_t      *element,
2304
                     cairo_bool_t              end_tag)
2305
0
{
2306
0
    double cur_x, cur_y;
2307
0
    double last_cp_x, last_cp_y;
2308
0
    double x, y, x1, y1, x2, y2;
2309
0
    double qx1, qy1, qx2, qy2;
2310
0
    double rx, ry, rotate, large_flag, sweep_flag;
2311
0
    cairo_bool_t rel, have_move;
2312
0
    enum { CUBIC, QUADRATIC, OTHER } last_op;
2313
2314
0
    if (end_tag ||
2315
0
        svg_render->graphics_state->mode == GS_NO_RENDER ||
2316
0
        svg_render->build_pattern.type != BUILD_PATTERN_NONE)
2317
0
        return FALSE;
2318
2319
0
    last_op = OTHER;
2320
0
    const char *p = get_attribute (element, "d");
2321
0
    const char *end;
2322
0
    int op;
2323
2324
0
    while (p) {
2325
0
        while (p && _cairo_isspace (*p))
2326
0
            p++;
2327
2328
0
        if (!p || *p == 0)
2329
0
            break;
2330
2331
0
        op = *p;
2332
0
        switch (op) {
2333
0
            case 'M':
2334
0
            case 'm':
2335
0
                rel = op == 'm';
2336
0
                p++;
2337
0
                have_move = FALSE;
2338
0
                do {
2339
0
                    end = get_path_params (p, 2, &x, &y);
2340
0
                    if (!end) {
2341
0
                        print_warning (svg_render, "path %c expected 2 numbers: %s", op, p);
2342
0
                        break;
2343
0
                    }
2344
0
                    p = end;
2345
0
                    if (rel) {
2346
0
                        get_current_point (svg_render, &cur_x, &cur_y);
2347
0
                        x += cur_x;
2348
0
                        y += cur_y;
2349
0
                    }
2350
0
                    if (!have_move) {
2351
0
                        cairo_move_to (svg_render->cr, x, y);
2352
0
                        have_move = TRUE;
2353
0
                    } else {
2354
0
                        cairo_line_to (svg_render->cr, x, y);
2355
0
                    }
2356
0
                    p = skip_space (p);
2357
0
                } while (p && *p && !_cairo_isalpha(*p));
2358
0
                last_op = OTHER;
2359
0
                break;
2360
0
            case 'Z':
2361
0
            case 'z':
2362
0
                p++;
2363
0
                cairo_close_path (svg_render->cr);
2364
0
                last_op = OTHER;
2365
0
                break;
2366
0
            case 'L':
2367
0
            case 'l':
2368
0
                rel = op == 'l';
2369
0
                p++;
2370
0
                do {
2371
0
                    end = get_path_params (p, 2, &x, &y);
2372
0
                    if (!end) {
2373
0
                        print_warning (svg_render, "path %c expected 2 numbers: %s", op, p);
2374
0
                        break;
2375
0
                    }
2376
0
                    p = end;
2377
0
                    if (rel) {
2378
0
                        get_current_point (svg_render, &cur_x, &cur_y);
2379
0
                        x += cur_x;
2380
0
                        y += cur_y;
2381
0
                    }
2382
0
                    cairo_line_to (svg_render->cr, x, y);
2383
0
                    p = skip_space (p);
2384
0
                } while (p && *p && !_cairo_isalpha(*p));
2385
0
                last_op = OTHER;
2386
0
                break;
2387
0
            case 'H':
2388
0
            case 'h':
2389
0
                rel = op == 'h';
2390
0
                p++;
2391
0
                do {
2392
0
                    end = get_path_params (p, 1, &x1);
2393
0
                    if (!end) {
2394
0
                        print_warning (svg_render, "path %c expected a number: %s", op, p);
2395
0
                        break;
2396
0
                    }
2397
0
                    p = end;
2398
0
                    get_current_point (svg_render, &cur_x, &cur_y);
2399
0
                    if (rel) {
2400
0
                        x1 += cur_x;
2401
0
                    }
2402
0
                    cairo_line_to (svg_render->cr, x1, cur_y);
2403
0
                    p = skip_space (p);
2404
0
                } while (p && *p && !_cairo_isalpha(*p));
2405
0
                last_op = OTHER;
2406
0
                break;
2407
0
            case 'V':
2408
0
            case 'v':
2409
0
                rel = op == 'v';
2410
0
                p++;
2411
0
                do {
2412
0
                    end = get_path_params (p, 1, &y1);
2413
0
                    if (!end) {
2414
0
                        print_warning (svg_render, "path %c expected a number: %s", op, p);
2415
0
                        break;
2416
0
                    }
2417
0
                    p = end;
2418
0
                    get_current_point (svg_render, &cur_x, &cur_y);
2419
0
                    if (rel) {
2420
0
                        y1 += cur_y;
2421
0
                    }
2422
0
                    cairo_line_to (svg_render->cr, cur_x, y1);
2423
0
                    p = skip_space (p);
2424
0
                } while (p && *p && !_cairo_isalpha(*p));
2425
0
                last_op = OTHER;
2426
0
                break;
2427
0
            case 'C':
2428
0
            case 'c':
2429
0
                rel = op == 'c';
2430
0
                p++;
2431
0
                do {
2432
0
                    end = get_path_params (p, 6, &x1, &y1, &x2, &y2, &x, &y);
2433
0
                    if (!end) {
2434
0
                        print_warning (svg_render, "path %c expected 6 numbers: %s", op, p);
2435
0
                        break;
2436
0
                    }
2437
0
                    p = end;
2438
0
                    if (rel) {
2439
0
                        get_current_point (svg_render, &cur_x, &cur_y);
2440
0
                        x1 += cur_x;
2441
0
                        y1 += cur_y;
2442
0
                        x2 += cur_x;
2443
0
                        y2 += cur_y;
2444
0
                        x += cur_x;
2445
0
                        y += cur_y;
2446
0
                    }
2447
0
                    cairo_curve_to (svg_render->cr, x1, y1, x2, y2, x, y);
2448
0
                    p = skip_space (p);
2449
0
                } while (p && *p && !_cairo_isalpha(*p));
2450
0
                last_op = CUBIC;
2451
0
                last_cp_x = x2;
2452
0
                last_cp_y = y2;
2453
0
                break;
2454
0
            case 'S':
2455
0
            case 's':
2456
0
                rel = op == 's';
2457
0
                p++;
2458
0
                do {
2459
0
                    end = get_path_params (p, 4, &x2, &y2, &x, &y);
2460
0
                    if (!end) {
2461
0
                        print_warning (svg_render, "path %c expected 4 numbers: %s", op, p);
2462
0
                        break;
2463
0
                    }
2464
0
                    p = end;
2465
0
                    get_current_point (svg_render, &cur_x, &cur_y);
2466
0
                    if (rel) {
2467
0
                        x2 += cur_x;
2468
0
                        y2 += cur_y;
2469
0
                        x += cur_x;
2470
0
                        y += cur_y;
2471
0
                    }
2472
0
                    if (last_op == CUBIC) {
2473
0
                        x1 = last_cp_x;
2474
0
                        y1 = last_cp_y;
2475
0
                        reflect_point (cur_x, cur_y, &x1, &y1);
2476
0
                    } else {
2477
0
                        x1 = cur_x;
2478
0
                        y1 = cur_y;
2479
0
                    }
2480
0
                    cairo_curve_to (svg_render->cr, x1, y1, x2, y2, x, y);
2481
0
                    last_op = CUBIC;
2482
0
                    last_cp_x = x2;
2483
0
                    last_cp_y = y2;
2484
0
                    p = skip_space (p);
2485
0
                } while (p && *p && !_cairo_isalpha(*p));
2486
0
                break;
2487
0
            case 'Q':
2488
0
            case 'q':
2489
0
                rel = op == 'q';
2490
0
                p++;
2491
0
                do {
2492
0
                    end = get_path_params (p, 4, &x1, &y1, &x, &y);
2493
0
                    if (!end) {
2494
0
                        print_warning (svg_render, "path %c expected 4 numbers: %s", op, p);
2495
0
                        break;
2496
0
                    }
2497
0
                    p = end;
2498
0
                    get_current_point (svg_render, &cur_x, &cur_y);
2499
0
                    if (rel) {
2500
0
                        x1 += cur_x;
2501
0
                        y1 += cur_y;
2502
0
                        x += cur_x;
2503
0
                        y += cur_y;
2504
0
                    }
2505
0
                    qx1 = cur_x + (x1 - cur_x)*2/3;
2506
0
                    qy1 = cur_y + (y1 - cur_y)*2/3;
2507
0
                    qx2 = x + (x1 - x)*2/3;
2508
0
                    qy2 = y + (y1 - y)*2/3;
2509
0
                    cairo_curve_to (svg_render->cr, qx1, qy1, qx2, qy2, x, y);
2510
0
                    p = skip_space (p);
2511
0
                } while (p && *p && !_cairo_isalpha(*p));
2512
0
                last_op = QUADRATIC;
2513
0
                last_cp_x = x1;
2514
0
                last_cp_y = y1;
2515
0
                break;
2516
0
            case 'T':
2517
0
            case 't':
2518
0
                rel = op == 't';
2519
0
                p++;
2520
0
                do {
2521
0
                    end = get_path_params (p, 2, &x, &y);
2522
0
                    if (!end) {
2523
0
                        print_warning (svg_render, "path %c expected 2 numbers: %s", op, p);
2524
0
                        break;
2525
0
                    }
2526
0
                    p = end;
2527
0
                    get_current_point (svg_render, &cur_x, &cur_y);
2528
0
                    if (rel) {
2529
0
                        x += cur_x;
2530
0
                        y += cur_y;
2531
0
                    }
2532
0
                    if (last_op == QUADRATIC) {
2533
0
                        x1 = last_cp_x;
2534
0
                        y1 = last_cp_y;
2535
0
                        reflect_point (cur_x, cur_y, &x1, &y1);
2536
0
                    } else {
2537
0
                        x1 = cur_x;
2538
0
                        y1 = cur_y;
2539
0
                    }
2540
0
                    qx1 = cur_x + (x1 - cur_x)*2/3;
2541
0
                    qy1 = cur_y + (y1 - cur_y)*2/3;
2542
0
                    qx2 = x + (x1 - x)*2/3;
2543
0
                    qy2 = y + (y1 - y)*2/3;
2544
0
                    cairo_curve_to (svg_render->cr, qx1, qy1, qx2, qy2, x, y);
2545
0
                    last_op = QUADRATIC;
2546
0
                    last_cp_x = x1;
2547
0
                    last_cp_y = y1;
2548
0
                    p = skip_space (p);
2549
0
                } while (p && *p && *p && !_cairo_isalpha(*p));
2550
0
                break;
2551
0
            case 'A':
2552
0
            case 'a':
2553
0
                rel = op == 'a';
2554
0
                p++;
2555
0
                do {
2556
0
                    end = get_path_params (p, 7, &rx, &ry, &rotate, &large_flag, &sweep_flag, &x, &y);
2557
0
                    if (!end) {
2558
0
                        print_warning (svg_render, "path %c expected 7 numbers: %s", op, p);
2559
0
                        break;
2560
0
                    }
2561
0
                    p = end;
2562
0
                    get_current_point (svg_render, &cur_x, &cur_y);
2563
0
                    if (rel) {
2564
0
                        x += cur_x;
2565
0
                        y += cur_y;
2566
0
                    }
2567
0
                    arc_path (svg_render->cr,
2568
0
                              cur_x, cur_y,
2569
0
                              x, y,
2570
0
                              rx, ry,
2571
0
                              rotate,
2572
0
                              large_flag > 0.5,
2573
0
                              sweep_flag > 0.5);
2574
0
                    p = skip_space (p);
2575
0
                } while (p && *p && !_cairo_isalpha(*p));
2576
0
                last_op = OTHER;
2577
0
                break;
2578
0
            default:
2579
0
                p = NULL;
2580
0
                break;
2581
0
        }
2582
0
    }
2583
2584
0
    draw_path (svg_render);
2585
0
    return TRUE;
2586
0
}
2587
2588
static void
2589
init_graphics_state (cairo_svg_glyph_render_t *svg_render)
2590
0
{
2591
0
    cairo_svg_graphics_state_t *gs;
2592
2593
0
    gs = _cairo_calloc (sizeof (cairo_svg_graphics_state_t));
2594
0
    get_paint (svg_render, "black", &gs->fill);
2595
0
    get_paint (svg_render, "none", &gs->stroke);
2596
0
    gs->color.type = FOREGROUND;
2597
0
    gs->fill_opacity = 1.0;
2598
0
    gs->stroke_opacity = 1.0;
2599
0
    gs->opacity = 1.0;
2600
0
    gs->fill_rule = CAIRO_FILL_RULE_WINDING;
2601
0
    gs->clip_rule = CAIRO_FILL_RULE_WINDING;
2602
0
    gs->clip_path = NULL;
2603
0
    gs->dash_array = NULL;
2604
0
    gs->dash_offset = 0.0;
2605
0
    gs->mode = GS_RENDER;
2606
0
    gs->bbox.x = 0;
2607
0
    gs->bbox.y = 0;
2608
0
    gs->bbox.width = 0;
2609
0
    gs->bbox.height = 0;
2610
0
    gs->next = NULL;
2611
2612
0
    svg_render->graphics_state = gs;
2613
2614
0
    cairo_save (svg_render->cr);
2615
0
    cairo_set_source_rgb (svg_render->cr, 0, 0, 0);
2616
0
    cairo_set_line_width (svg_render->cr, 1.0);
2617
0
    cairo_set_line_cap (svg_render->cr, CAIRO_LINE_CAP_BUTT);
2618
0
    cairo_set_line_join (svg_render->cr, CAIRO_LINE_JOIN_MITER);
2619
0
    cairo_set_miter_limit (svg_render->cr, 4.0);
2620
0
}
2621
2622
0
#define MAX_DASHES 100
2623
static void update_dash (cairo_svg_glyph_render_t *svg_render,
2624
                         cairo_svg_element_t      *element)
2625
0
{
2626
0
    cairo_svg_graphics_state_t *gs = svg_render->graphics_state;
2627
0
    const char *p;
2628
0
    char *end;
2629
0
    double value;
2630
0
    double dash_array[MAX_DASHES];
2631
0
    int num_dashes = 0;
2632
0
    cairo_bool_t not_zero = FALSE;
2633
    
2634
0
    if (gs->dash_array == NULL || string_equal (gs->dash_array, "none")) {
2635
0
        cairo_set_dash (svg_render->cr, NULL, 0, 0);
2636
0
        return;
2637
0
    }
2638
2639
0
    p = gs->dash_array;
2640
0
    while (*p && num_dashes < MAX_DASHES) {
2641
0
        while (*p && (*p == ',' || _cairo_isspace (*p)))
2642
0
            p++;
2643
2644
0
        if (*p == 0)
2645
0
            break;
2646
2647
0
        value = _cairo_strtod (p, &end);
2648
0
        if (end == p)
2649
0
            break;
2650
2651
0
        p = end;
2652
0
        if (*p == '%') {
2653
0
            value *= svg_render->width / 100.0;
2654
0
            p++;
2655
0
        }
2656
2657
0
        if (value < 0.0)
2658
0
            return;
2659
2660
0
        if (value > 0.0)
2661
0
            not_zero = TRUE;
2662
2663
0
        dash_array[num_dashes++] = value;
2664
0
    }
2665
2666
0
    if (not_zero)
2667
0
        cairo_set_dash (svg_render->cr, dash_array, num_dashes, gs->dash_offset);
2668
0
}
2669
2670
static cairo_bool_t
2671
pattern_requires_bbox (cairo_svg_glyph_render_t *svg_render,
2672
                       cairo_svg_element_t      *paint_server)
2673
0
{
2674
0
    const char *p;
2675
2676
0
    if (string_equal (paint_server->tag, "linearGradient") ||
2677
0
        string_equal (paint_server->tag, "radialGradient"))
2678
0
    {
2679
0
        p = get_attribute (paint_server, "gradientUnits");
2680
0
        if (string_equal (p, "userSpaceOnUse"))
2681
0
            return FALSE;
2682
2683
0
        return TRUE;
2684
0
    }
2685
0
    return FALSE;
2686
0
}
2687
2688
static cairo_bool_t
2689
clip_requires_bbox (cairo_svg_glyph_render_t *svg_render,
2690
                    const char               *clip_path)
2691
0
{
2692
0
    cairo_svg_element_t *element;
2693
0
    const char *p;
2694
2695
0
    if (clip_path && strncmp (clip_path, "url", 3) == 0) {
2696
0
        element = lookup_url_element (svg_render, clip_path);
2697
0
        if (element) {
2698
0
            p = get_attribute (element, "clipPathUnits");
2699
0
            if (string_equal (p, "objectBoundingBox"))
2700
0
                return TRUE;
2701
0
        }
2702
0
    }
2703
0
    return FALSE;
2704
0
}
2705
2706
static cairo_bool_t
2707
need_bbox (cairo_svg_glyph_render_t *svg_render,
2708
           cairo_svg_element_t      *element)
2709
0
{
2710
0
    cairo_svg_graphics_state_t *gs = svg_render->graphics_state;
2711
0
    cairo_bool_t fill_needs_bbox = FALSE;
2712
0
    cairo_bool_t stroke_needs_bbox = FALSE;
2713
0
    cairo_bool_t clip_needs_bbox = FALSE;
2714
    
2715
0
    if (gs->mode != GS_RENDER)
2716
0
        return FALSE;
2717
2718
0
    if (gs->fill.type == PAINT_SERVER && pattern_requires_bbox (svg_render, gs->fill.paint_server))
2719
0
        fill_needs_bbox = TRUE;
2720
2721
0
    if (gs->stroke.type == PAINT_SERVER && pattern_requires_bbox (svg_render, gs->stroke.paint_server))
2722
0
        stroke_needs_bbox = TRUE;
2723
2724
0
    if (clip_requires_bbox (svg_render, get_attribute (element, "clip-path")))
2725
0
        clip_needs_bbox = TRUE;
2726
2727
0
    if (string_equal (element->tag, "circle") ||
2728
0
        string_equal (element->tag, "ellipse") ||
2729
0
        string_equal (element->tag, "path") ||
2730
0
        string_equal (element->tag, "polygon") ||
2731
0
        string_equal (element->tag, "rect"))
2732
0
    {
2733
0
        return fill_needs_bbox || stroke_needs_bbox || clip_needs_bbox;
2734
0
    }
2735
2736
0
    if (string_equal (element->tag, "line") ||
2737
0
        string_equal (element->tag, "polyline"))
2738
0
    {
2739
0
        return stroke_needs_bbox || clip_needs_bbox;
2740
0
    }
2741
2742
0
    if (string_equal (element->tag, "g") ||
2743
0
        string_equal (element->tag, "image") ||
2744
0
        string_equal (element->tag, "use"))
2745
0
    {
2746
0
        return clip_needs_bbox;
2747
0
    }
2748
    
2749
0
    return FALSE;
2750
0
}
2751
2752
static cairo_bool_t
2753
call_element (cairo_svg_glyph_render_t *svg_render,
2754
              cairo_svg_element_t      *element,
2755
              cairo_bool_t              end_tag);
2756
2757
static void
2758
update_graphics_state (cairo_svg_glyph_render_t *svg_render,
2759
                       cairo_svg_element_t      *element)
2760
0
{
2761
0
    double value;
2762
0
    const char *p;
2763
0
    cairo_svg_graphics_state_t *gs = svg_render->graphics_state;
2764
2765
0
    p = get_attribute (element, "transform");
2766
0
    if (p) {
2767
0
        cairo_matrix_t m;
2768
0
        if (parse_transform (p, &m))
2769
0
            cairo_transform (svg_render->cr, &m);
2770
0
    }
2771
2772
    /* The transform is all we need for bbox computation. The SVG spec
2773
     * excludes clipping and stroke-width from the bbox. */
2774
0
    if (gs->mode == GS_COMPUTE_BBOX)
2775
0
        return;
2776
    
2777
0
    p = get_attribute (element, "color");
2778
0
    if (p)
2779
0
        get_color (svg_render, p, &gs->color);
2780
2781
0
    if (!get_float_attribute (element, "opacity", &gs->opacity))
2782
0
        gs->opacity = 1.0;
2783
2784
0
    p = get_attribute (element, "fill");
2785
0
    if (p) {
2786
0
        get_paint (svg_render, p, &gs->fill);
2787
0
    }
2788
2789
0
    get_float_attribute (element, "fill-opacity", &gs->fill_opacity);
2790
2791
0
    gs->fill_rule = get_fill_rule_attribute (element, "fill-rule", gs->fill_rule);
2792
2793
0
    gs->clip_rule = get_fill_rule_attribute (element, "fill-rule", gs->clip_rule);
2794
2795
0
    p = get_attribute (element, "stroke");
2796
0
    if (p)
2797
0
        get_paint (svg_render, p, &gs->stroke);
2798
2799
0
    if (get_float_or_percent_attribute (element, "stroke-width", svg_render->width, &value))
2800
0
        cairo_set_line_width (svg_render->cr, value);
2801
2802
0
    p = get_attribute (element, "stroke-linecap");
2803
0
    if (string_equal (p, "butt"))
2804
0
        cairo_set_line_cap (svg_render->cr, CAIRO_LINE_CAP_BUTT);
2805
0
    else if (string_equal (p, "round"))
2806
0
        cairo_set_line_cap (svg_render->cr, CAIRO_LINE_CAP_ROUND);
2807
0
    else if (string_equal (p, "square"))
2808
0
        cairo_set_line_cap (svg_render->cr, CAIRO_LINE_CAP_SQUARE);
2809
2810
0
    p = get_attribute (element, "stroke-linejoin");
2811
0
    if (string_equal (p, "miter"))
2812
0
        cairo_set_line_join (svg_render->cr, CAIRO_LINE_JOIN_MITER);
2813
0
    else if (string_equal (p, "round"))
2814
0
        cairo_set_line_join (svg_render->cr, CAIRO_LINE_JOIN_ROUND);
2815
0
    else if (string_equal (p, "bevel"))
2816
0
        cairo_set_line_join (svg_render->cr, CAIRO_LINE_JOIN_BEVEL);
2817
2818
0
    if (get_float_attribute (element, "stroke-miterlimit", &value))
2819
0
        cairo_set_miter_limit (svg_render->cr, value);
2820
2821
0
    p = get_attribute (element, "stroke-dasharray");
2822
0
    if (p) {
2823
0
        free (gs->dash_array);
2824
0
        gs->dash_array = strdup (p);
2825
0
    }
2826
2827
0
    get_float_or_percent_attribute (element, "stroke-dashoffset", svg_render->width, &gs->dash_offset);
2828
0
    update_dash (svg_render, element);
2829
2830
    /* Some elements may need the bounding box of the element thay are
2831
     * applied to.  As this recursively calls render_element on the
2832
     * same element while we are in render_element and setting up the
2833
     * graphics state, we check gs->mode to avoid re-entering the
2834
     * compute bbox code. The GS_COMPUTE_MODE flag is also used by
2835
     * render functions to ignore patterns and strokes (SVG spec
2836
     * ignores stroke with in bbox calculations) and just use a solid
2837
     * color.
2838
     */
2839
0
    if (gs->mode == GS_RENDER && need_bbox (svg_render, element)) {
2840
0
        cairo_surface_t *recording = cairo_recording_surface_create (CAIRO_CONTENT_COLOR_ALPHA, NULL);
2841
0
        cairo_t *old_cr = svg_render->cr;
2842
0
        svg_render->cr = cairo_create (recording);
2843
0
        gs_mode_t old_mode = gs->mode;
2844
0
        gs->mode = GS_COMPUTE_BBOX;
2845
        /* To avoid recursing back into this function, we call the
2846
         * element directory then use render_element_tree to render
2847
         * the children */
2848
0
        call_element (svg_render, element, FALSE);
2849
0
        render_element_tree (svg_render, element, NULL, TRUE);
2850
0
        if (element->type == CONTAINER_ELEMENT)
2851
0
            call_element (svg_render, element, TRUE);
2852
0
        gs->mode = old_mode;
2853
0
        cairo_destroy (svg_render->cr);
2854
0
        svg_render->cr = old_cr;
2855
0
        cairo_recording_surface_ink_extents (recording,
2856
0
                                             &gs->bbox.x,
2857
0
                                             &gs->bbox.y,
2858
0
                                             &gs->bbox.width,
2859
0
                                             &gs->bbox.height);
2860
0
        cairo_surface_destroy (recording);
2861
0
    }
2862
2863
    /* clip-path may require bbox */
2864
0
    p = get_attribute (element, "clip-path");
2865
0
    if (p && strncmp (p, "url", 3) == 0) {
2866
0
        element = lookup_url_element (svg_render, p);
2867
0
        if (element) {
2868
0
            gs_mode_t old_mode = gs->mode;
2869
0
            gs->mode = GS_CLIP;
2870
0
            render_element_tree (svg_render, element, NULL, FALSE);
2871
0
            cairo_set_fill_rule (svg_render->cr, gs->clip_rule);
2872
0
            cairo_clip (svg_render->cr);
2873
0
            gs->mode = old_mode;
2874
0
        }
2875
0
    }
2876
0
}
2877
2878
static void
2879
save_graphics_state (cairo_svg_glyph_render_t *svg_render)
2880
0
{
2881
0
    cairo_svg_graphics_state_t *gs;
2882
2883
0
    cairo_save (svg_render->cr);
2884
2885
0
    gs = _cairo_calloc (sizeof (cairo_svg_graphics_state_t));
2886
0
    gs->fill           = svg_render->graphics_state->fill;
2887
0
    gs->stroke         = svg_render->graphics_state->stroke;
2888
0
    gs->color          = svg_render->graphics_state->color;
2889
0
    gs->fill_opacity   = svg_render->graphics_state->fill_opacity;
2890
0
    gs->stroke_opacity = svg_render->graphics_state->stroke_opacity;
2891
0
    gs->opacity        = svg_render->graphics_state->opacity;
2892
0
    gs->fill_rule      = svg_render->graphics_state->fill_rule;
2893
0
    gs->clip_rule      = svg_render->graphics_state->clip_rule;
2894
0
    gs->clip_path      = NULL;
2895
0
    gs->dash_array     = NULL;
2896
0
    if (svg_render->graphics_state->dash_array)
2897
0
        gs->dash_array = strdup (svg_render->graphics_state->dash_array);
2898
0
    gs->dash_offset    = svg_render->graphics_state->dash_offset;
2899
0
    gs->mode           = svg_render->graphics_state->mode;
2900
0
    gs->bbox           = svg_render->graphics_state->bbox;
2901
0
    gs->next           = svg_render->graphics_state;
2902
0
    svg_render->graphics_state = gs;
2903
0
}
2904
2905
static void
2906
restore_graphics_state (cairo_svg_glyph_render_t *svg_render)
2907
0
{
2908
0
    cairo_svg_graphics_state_t *gs;
2909
2910
0
    gs = svg_render->graphics_state;
2911
0
    svg_render->graphics_state = gs->next;
2912
0
    if (gs->clip_path)
2913
0
        cairo_path_destroy (gs->clip_path);
2914
0
    free (gs->dash_array);
2915
0
    free (gs);
2916
2917
0
    cairo_restore (svg_render->cr);
2918
0
}
2919
2920
/* render function returns TRUE if render_element_tree() is to render
2921
 * the child nodes, FALSE if render_element_tree() is to skip the
2922
 * child nodes.
2923
 */
2924
struct render_func {
2925
    const char *tag;
2926
    cairo_bool_t (*render) (cairo_svg_glyph_render_t *, cairo_svg_element_t *, cairo_bool_t);
2927
};
2928
2929
/* Must be sorted */
2930
static const struct render_func render_funcs[] = {
2931
    { "circle", render_element_circle },
2932
    { "clipPath", render_element_clip_path },
2933
    { "defs", NULL },
2934
    { "desc", NULL },
2935
    { "ellipse", render_element_ellipse },
2936
    { "g", render_element_g },
2937
    { "image", render_element_image },
2938
    { "line", render_element_line },
2939
    { "linearGradient", render_element_linear_gradient },
2940
    { "metadata", NULL },
2941
    { "path", render_element_path },
2942
    { "polygon", render_element_polyline },
2943
    { "polyline", render_element_polyline },
2944
    { "radialGradient", render_element_radial_gradient },
2945
    { "rect", render_element_rect },
2946
    { "stop", render_element_stop },
2947
    { "svg", render_element_svg },
2948
    { "title", NULL },
2949
    { "use", render_element_use },
2950
};
2951
2952
static int
2953
_render_func_compare (const void *a, const void *b)
2954
0
{
2955
0
    const struct render_func *render_func_a = a;
2956
0
    const struct render_func *render_func_b = b;
2957
2958
0
    return strcmp (render_func_a->tag, render_func_b->tag);
2959
0
}
2960
2961
static cairo_bool_t
2962
call_element (cairo_svg_glyph_render_t *svg_render,
2963
              cairo_svg_element_t      *element,
2964
              cairo_bool_t              end_tag)
2965
0
{
2966
0
    const struct render_func *func;
2967
0
    struct render_func key;
2968
0
    cairo_bool_t recurse = FALSE;
2969
2970
0
    key.tag = element->tag;
2971
0
    key.render = NULL;
2972
0
    func = bsearch (&key,
2973
0
                    render_funcs,
2974
0
                    ARRAY_LENGTH (render_funcs),
2975
0
                    sizeof (struct render_func),
2976
0
                    _render_func_compare);
2977
0
    if (func) {
2978
0
        if (func->render) {
2979
0
            recurse = func->render (svg_render, element, end_tag);
2980
0
        }
2981
0
    } else {
2982
0
        print_warning (svg_render, "Unsupported element: %s", element->tag);
2983
0
    }
2984
2985
0
    return recurse;
2986
0
}
2987
2988
static cairo_bool_t
2989
render_element (cairo_svg_glyph_render_t *svg_render,
2990
                cairo_svg_element_t      *element,
2991
                cairo_bool_t              end_tag,
2992
                cairo_svg_element_t      *display_element)
2993
0
{
2994
0
    cairo_bool_t recurse = FALSE;
2995
0
    cairo_svg_graphics_state_t *gs;
2996
2997
    /* Ignore elements if we have not seen "<svg>". Ignore
2998
      * "<svg>" if we have seen it */
2999
0
    if (svg_render->view_port_set) {
3000
0
        if (string_equal (element->tag, "svg"))
3001
0
            return FALSE;
3002
0
    } else {
3003
0
        if (!string_equal (element->tag, "svg"))
3004
0
            return FALSE;
3005
0
    }
3006
3007
0
    if (element->type == EMPTY_ELEMENT ||
3008
0
        (element->type == CONTAINER_ELEMENT && !end_tag))
3009
0
    {
3010
0
        save_graphics_state (svg_render);
3011
0
        update_graphics_state (svg_render, element);
3012
0
    }
3013
3014
0
    gs = svg_render->graphics_state;
3015
0
    if (gs->mode == GS_NO_RENDER && element == display_element)
3016
0
        gs->mode = GS_RENDER;
3017
3018
0
    recurse = call_element (svg_render, element, end_tag);
3019
3020
0
    if (element->type == EMPTY_ELEMENT ||
3021
0
        (element->type == CONTAINER_ELEMENT && end_tag))
3022
0
    {
3023
0
        restore_graphics_state (svg_render);
3024
0
    }
3025
3026
0
    return recurse;
3027
0
}
3028
3029
0
#define MAX_DEPTH 100
3030
3031
static void
3032
render_element_tree (cairo_svg_glyph_render_t *svg_render,
3033
                     cairo_svg_element_t      *element,
3034
                     cairo_svg_element_t      *display_element,
3035
                     cairo_bool_t              children_only)
3036
0
{
3037
0
    if (!element)
3038
0
        return;
3039
3040
    /* Avoid circular references by limiting the number of recursive
3041
     * calls to this function. */
3042
0
    if (svg_render->render_element_tree_depth > MAX_DEPTH)
3043
0
        return;
3044
3045
0
    svg_render->render_element_tree_depth++;
3046
0
    if (element->type == EMPTY_ELEMENT && !children_only) {
3047
0
        render_element (svg_render, element, FALSE, display_element);
3048
3049
0
    } else if (element->type == CONTAINER_ELEMENT) {
3050
0
        int num_elems;
3051
0
        cairo_bool_t recurse = TRUE;;
3052
3053
0
        if (!children_only)
3054
0
            recurse = render_element (svg_render, element, FALSE, display_element);
3055
3056
        /* We only render the children if the parent returned
3057
         * success. This is how we avoid rendering non display
3058
         * elements like gradients, <defs>, and anything not
3059
         * implemented. */
3060
0
        if (recurse) {
3061
0
            num_elems = _cairo_array_num_elements (&element->children);
3062
0
            for (int i = 0; i < num_elems; i++) {
3063
0
                cairo_svg_element_t *child;
3064
0
                _cairo_array_copy_element (&element->children, i, &child);
3065
0
                render_element_tree (svg_render, child, display_element, FALSE);
3066
0
            }
3067
0
        }
3068
3069
0
        if (!children_only)
3070
0
            render_element (svg_render, element, TRUE, display_element);
3071
0
    }
3072
0
    svg_render->render_element_tree_depth--;
3073
0
}
3074
3075
static void
3076
render_element_tree_id (cairo_svg_glyph_render_t *svg_render,
3077
                        const char               *element_id)
3078
0
{
3079
0
    cairo_svg_element_t *glyph_element = NULL;
3080
3081
0
    if (element_id)
3082
0
        glyph_element = lookup_element (svg_render, element_id);
3083
3084
0
    if (glyph_element)
3085
0
        svg_render->graphics_state->mode = GS_NO_RENDER;
3086
0
    else
3087
0
        svg_render->graphics_state->mode = GS_RENDER;
3088
3089
0
    render_element_tree (svg_render, svg_render->tree, glyph_element, TRUE);
3090
0
}
3091
3092
cairo_status_t
3093
_cairo_render_svg_glyph (const char           *svg_document,
3094
                         unsigned long         first_glyph,
3095
                         unsigned long         last_glyph,
3096
                         unsigned long         glyph,
3097
                         double                units_per_em,
3098
                         FT_Color             *palette,
3099
                         int                   num_palette_entries,
3100
                         cairo_t              *cr,
3101
                         cairo_pattern_t      *foreground_source,
3102
       cairo_bool_t         *foreground_source_used)
3103
0
{
3104
0
    cairo_status_t status = CAIRO_STATUS_SUCCESS;
3105
3106
0
    cairo_svg_glyph_render_t *svg_render = _cairo_calloc (sizeof (cairo_svg_glyph_render_t));
3107
0
    if (unlikely (svg_render == NULL))
3108
0
  return _cairo_error (CAIRO_STATUS_NO_MEMORY);
3109
3110
0
    svg_render->tree = NULL;
3111
0
    svg_render->ids = _cairo_hash_table_create (_element_id_equal);
3112
0
    if (unlikely (svg_render->ids == NULL)) {
3113
0
        free (svg_render);
3114
0
  return _cairo_error (CAIRO_STATUS_NO_MEMORY);
3115
0
    }
3116
3117
0
    svg_render->debug = 0;
3118
0
    const char *s = getenv ("CAIRO_DEBUG_SVG_RENDER");
3119
0
    if (s) {
3120
0
        if (strlen (s) > 0)
3121
0
            svg_render->debug = atoi (s);
3122
0
        else
3123
0
            svg_render->debug = SVG_RENDER_ERROR;
3124
0
    }
3125
3126
0
    svg_render->cr = cr;
3127
0
    svg_render->units_per_em = units_per_em;
3128
0
    svg_render->build_pattern.paint_server = NULL;
3129
0
    svg_render->build_pattern.pattern = NULL;
3130
0
    svg_render->build_pattern.type = BUILD_PATTERN_NONE;
3131
0
    svg_render->render_element_tree_depth = 0;
3132
0
    svg_render->view_port_set = FALSE;
3133
0
    svg_render->num_palette_entries = num_palette_entries;
3134
0
    svg_render->palette = palette;
3135
3136
0
    svg_render->foreground_marker = _cairo_pattern_create_foreground_marker ();
3137
0
    svg_render->foreground_source = cairo_pattern_reference (foreground_source);;
3138
0
    svg_render->foreground_source_used = FALSE;
3139
3140
0
    init_graphics_state (svg_render);
3141
3142
0
    print_info (svg_render, "Glyph ID: %ld", glyph);
3143
0
    print_info (svg_render, "Palette Entries: %d", num_palette_entries);
3144
0
    print_info (svg_render, "Units per EM: %f", units_per_em);
3145
0
    print_info (svg_render, "SVG Document:\n%s\n", svg_document);
3146
3147
    /* First parse elements into a tree and populate ids hash table */
3148
0
    if (!parse_svg (svg_render, svg_document)) {
3149
0
        print_error (svg_render, "Parse SVG document failed");
3150
0
        status = CAIRO_STATUS_SVG_FONT_ERROR;
3151
0
        goto cleanup;
3152
0
    }
3153
3154
#if SVG_RENDER_PRINT_FUNCTIONS
3155
    printf("\nTREE\n");
3156
    if (svg_render->tree) {
3157
        print_element (svg_render->tree, TRUE, 0);
3158
        printf("\n");
3159
    }
3160
#endif
3161
3162
    /* Next, render glyph */
3163
0
    if (first_glyph == last_glyph) {
3164
        /* Render whole document */
3165
0
        render_element_tree_id (svg_render, NULL);
3166
0
    } else {
3167
        /* Render element with id "glyphID" where ID is glyph number. */
3168
3169
0
        char glyph_id[30];
3170
0
        snprintf(glyph_id, sizeof(glyph_id), "#glyph%ld", glyph);
3171
0
        render_element_tree_id (svg_render, glyph_id);
3172
0
    }
3173
3174
0
  cleanup:
3175
0
    if (svg_render->build_pattern.pattern)
3176
0
        cairo_pattern_destroy (svg_render->build_pattern.pattern);
3177
3178
0
    if (svg_render->tree)
3179
0
        free_elements (svg_render, svg_render->tree);
3180
3181
0
    while (svg_render->graphics_state)
3182
0
        restore_graphics_state (svg_render);
3183
3184
0
    cairo_pattern_destroy (svg_render->foreground_marker);
3185
0
    cairo_pattern_destroy (svg_render->foreground_source);
3186
0
    *foreground_source_used = svg_render->foreground_source_used;
3187
3188
    /* The hash entry for each element with an id is removed by
3189
     * free_elements() */
3190
0
    _cairo_hash_table_destroy (svg_render->ids);
3191
3192
0
    free (svg_render);
3193
3194
0
    return status;
3195
0
}
3196
3197
#ifdef DEBUG_SVG_RENDER
3198
3199
/**
3200
 * _cairo_debug_svg_render:
3201
 *
3202
 * Debug function for cairo-svg-glyph-render.c. Allows invoking the renderer from outside
3203
 * cairo to test with SVG documents, and to facilitate comparison with librsvg rendering.
3204
 * The viewport is .
3205
 *
3206
 * @cr: render target
3207
 * @svg_document: SVG Document
3208
 * @element: element within svg_document to render (eg "#glyph8"), or NULL to render entire document.
3209
 * @debug_level: 0 - quiet, 1 - print errors, 2 - print warnings, 3 - info
3210
 * @return TRUE on success, ie no errors, FALSE if error
3211
 **/
3212
cairo_public cairo_bool_t
3213
_cairo_debug_svg_render (cairo_t       *cr,
3214
                         const char    *svg_document,
3215
                         const char    *element,
3216
                         double         units_per_em,
3217
                         int            debug_level);
3218
3219
cairo_bool_t
3220
_cairo_debug_svg_render (cairo_t       *cr,
3221
                         const char    *svg_document,
3222
                         const char    *element,
3223
                         double         units_per_em,
3224
                         int            debug_level)
3225
{
3226
    cairo_status_t status;
3227
    cairo_bool_t foreground_source_used;
3228
    cairo_pattern_t *foreground = _cairo_pattern_create_foreground_marker ();
3229
3230
    status = _cairo_render_svg_glyph (svg_document,
3231
              1, 1, 1,
3232
              units_per_em,
3233
              NULL, 0,
3234
              cr,
3235
              foreground,
3236
              &foreground_source_used);
3237
    cairo_pattern_destroy (foreground);
3238
3239
    return status == CAIRO_STATUS_SUCCESS;
3240
}
3241
3242
#endif /* DEBUG_SVG_RENDER */
3243
3244
#endif /* HAVE_FT_SVG_DOCUMENT */