forked from GNOME/librsvg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsvg-base.c
2189 lines (1839 loc) · 62.5 KB
/
rsvg-base.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* vim: set sw=4 sts=4 ts=4 expandtab: */
/*
rsvg.c: SAX-based renderer for SVG files into a GdkPixbuf.
Copyright (C) 2000 Eazel, Inc.
Copyright (C) 2002 Dom Lachowicz <cinamod@hotmail.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this program; if not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
Author: Raph Levien <raph@artofcode.com>
*/
#include "config.h"
#include "rsvg.h"
#include "rsvg-private.h"
#include "rsvg-css.h"
#include "rsvg-styles.h"
#include "rsvg-shapes.h"
#include "rsvg-structure.h"
#include "rsvg-image.h"
#include "rsvg-io.h"
#include "rsvg-text.h"
#include "rsvg-filter.h"
#include "rsvg-mask.h"
#include "rsvg-marker.h"
#include "rsvg-cairo-render.h"
#include <libxml/uri.h>
#include <libxml/parser.h>
#include <libxml/parserInternals.h>
#include <gio/gio.h>
#include <math.h>
#include <string.h>
#include <stdarg.h>
#include "rsvg-path.h"
#include "rsvg-paint-server.h"
#include "rsvg-xml.h"
/*
* This is configurable at runtime
*/
#define RSVG_DEFAULT_DPI_X 90.0
#define RSVG_DEFAULT_DPI_Y 90.0
G_GNUC_INTERNAL
double rsvg_internal_dpi_x = RSVG_DEFAULT_DPI_X;
G_GNUC_INTERNAL
double rsvg_internal_dpi_y = RSVG_DEFAULT_DPI_Y;
static xmlSAXHandler rsvgSAXHandlerStruct;
static gboolean rsvgSAXHandlerStructInited = FALSE;
typedef struct _RsvgSaxHandlerDefs {
RsvgSaxHandler super;
RsvgHandle *ctx;
} RsvgSaxHandlerDefs;
typedef struct _RsvgSaxHandlerStyle {
RsvgSaxHandler super;
RsvgSaxHandlerDefs *parent;
RsvgHandle *ctx;
GString *style;
gboolean is_text_css;
} RsvgSaxHandlerStyle;
typedef struct {
RsvgSaxHandler super;
RsvgHandle *ctx;
const char *name;
GString *string;
GString **stringptr;
} RsvgSaxHandlerExtra;
/* hide this fact from the general public */
typedef RsvgSaxHandlerExtra RsvgSaxHandlerTitle;
typedef RsvgSaxHandlerExtra RsvgSaxHandlerDesc;
typedef RsvgSaxHandlerExtra RsvgSaxHandlerMetadata;
static void
rsvg_style_handler_free (RsvgSaxHandler * self)
{
RsvgSaxHandlerStyle *z = (RsvgSaxHandlerStyle *) self;
RsvgHandle *ctx = z->ctx;
if (z->is_text_css)
rsvg_parse_cssbuffer (ctx, z->style->str, z->style->len);
g_string_free (z->style, TRUE);
g_free (z);
}
static void
rsvg_style_handler_characters (RsvgSaxHandler * self, const char *ch, int len)
{
RsvgSaxHandlerStyle *z = (RsvgSaxHandlerStyle *) self;
g_string_append_len (z->style, ch, len);
}
static void
rsvg_style_handler_start (RsvgSaxHandler * self, const char *name, RsvgPropertyBag * atts)
{
}
static void
rsvg_style_handler_end (RsvgSaxHandler * self, const char *name)
{
RsvgSaxHandlerStyle *z = (RsvgSaxHandlerStyle *) self;
RsvgHandle *ctx = z->ctx;
RsvgSaxHandler *prev = &z->parent->super;
if (!strcmp (name, "style")) {
if (ctx->priv->handler != NULL) {
ctx->priv->handler->free (ctx->priv->handler);
ctx->priv->handler = prev;
}
}
}
static void
rsvg_start_style (RsvgHandle * ctx, RsvgPropertyBag *atts)
{
RsvgSaxHandlerStyle *handler = g_new0 (RsvgSaxHandlerStyle, 1);
const char *type;
type = rsvg_property_bag_lookup (atts, "type");
handler->super.free = rsvg_style_handler_free;
handler->super.characters = rsvg_style_handler_characters;
handler->super.start_element = rsvg_style_handler_start;
handler->super.end_element = rsvg_style_handler_end;
handler->ctx = ctx;
handler->style = g_string_new (NULL);
handler->is_text_css = type && g_ascii_strcasecmp (type, "text/css") == 0;
handler->parent = (RsvgSaxHandlerDefs *) ctx->priv->handler;
ctx->priv->handler = &handler->super;
}
static void
rsvg_standard_element_start (RsvgHandle * ctx, const char *name, RsvgPropertyBag * atts)
{
/*replace this stuff with a hash for fast reading! */
RsvgNode *newnode = NULL;
if (!strcmp (name, "g"))
newnode = rsvg_new_group ();
else if (!strcmp (name, "a")) /*treat anchors as groups for now */
newnode = rsvg_new_group ();
else if (!strcmp (name, "switch"))
newnode = rsvg_new_switch ();
else if (!strcmp (name, "defs"))
newnode = rsvg_new_defs ();
else if (!strcmp (name, "use"))
newnode = rsvg_new_use ();
else if (!strcmp (name, "path"))
newnode = rsvg_new_path ();
else if (!strcmp (name, "line"))
newnode = rsvg_new_line ();
else if (!strcmp (name, "rect"))
newnode = rsvg_new_rect ();
else if (!strcmp (name, "ellipse"))
newnode = rsvg_new_ellipse ();
else if (!strcmp (name, "circle"))
newnode = rsvg_new_circle ();
else if (!strcmp (name, "polygon"))
newnode = rsvg_new_polygon ();
else if (!strcmp (name, "polyline"))
newnode = rsvg_new_polyline ();
else if (!strcmp (name, "symbol"))
newnode = rsvg_new_symbol ();
else if (!strcmp (name, "svg"))
newnode = rsvg_new_svg ();
else if (!strcmp (name, "mask"))
newnode = rsvg_new_mask ();
else if (!strcmp (name, "clipPath"))
newnode = rsvg_new_clip_path ();
else if (!strcmp (name, "image"))
newnode = rsvg_new_image ();
else if (!strcmp (name, "marker"))
newnode = rsvg_new_marker ();
else if (!strcmp (name, "stop"))
newnode = rsvg_new_stop ();
else if (!strcmp (name, "pattern"))
newnode = rsvg_new_pattern ();
else if (!strcmp (name, "linearGradient"))
newnode = rsvg_new_linear_gradient ();
else if (!strcmp (name, "radialGradient"))
newnode = rsvg_new_radial_gradient ();
else if (!strcmp (name, "conicalGradient"))
newnode = rsvg_new_radial_gradient ();
else if (!strcmp (name, "filter"))
newnode = rsvg_new_filter ();
else if (!strcmp (name, "feBlend"))
newnode = rsvg_new_filter_primitive_blend ();
else if (!strcmp (name, "feColorMatrix"))
newnode = rsvg_new_filter_primitive_colour_matrix ();
else if (!strcmp (name, "feComponentTransfer"))
newnode = rsvg_new_filter_primitive_component_transfer ();
else if (!strcmp (name, "feComposite"))
newnode = rsvg_new_filter_primitive_composite ();
else if (!strcmp (name, "feConvolveMatrix"))
newnode = rsvg_new_filter_primitive_convolve_matrix ();
else if (!strcmp (name, "feDiffuseLighting"))
newnode = rsvg_new_filter_primitive_diffuse_lighting ();
else if (!strcmp (name, "feDisplacementMap"))
newnode = rsvg_new_filter_primitive_displacement_map ();
else if (!strcmp (name, "feFlood"))
newnode = rsvg_new_filter_primitive_flood ();
else if (!strcmp (name, "feGaussianBlur"))
newnode = rsvg_new_filter_primitive_gaussian_blur ();
else if (!strcmp (name, "feImage"))
newnode = rsvg_new_filter_primitive_image ();
else if (!strcmp (name, "feMerge"))
newnode = rsvg_new_filter_primitive_merge ();
else if (!strcmp (name, "feMorphology"))
newnode = rsvg_new_filter_primitive_erode ();
else if (!strcmp (name, "feOffset"))
newnode = rsvg_new_filter_primitive_offset ();
else if (!strcmp (name, "feSpecularLighting"))
newnode = rsvg_new_filter_primitive_specular_lighting ();
else if (!strcmp (name, "feTile"))
newnode = rsvg_new_filter_primitive_tile ();
else if (!strcmp (name, "feTurbulence"))
newnode = rsvg_new_filter_primitive_turbulence ();
else if (!strcmp (name, "feMergeNode"))
newnode = rsvg_new_filter_primitive_merge_node ();
else if (!strcmp (name, "feFuncR"))
newnode = rsvg_new_node_component_transfer_function ('r');
else if (!strcmp (name, "feFuncG"))
newnode = rsvg_new_node_component_transfer_function ('g');
else if (!strcmp (name, "feFuncB"))
newnode = rsvg_new_node_component_transfer_function ('b');
else if (!strcmp (name, "feFuncA"))
newnode = rsvg_new_node_component_transfer_function ('a');
else if (!strcmp (name, "feDistantLight"))
newnode = rsvg_new_node_light_source ('d');
else if (!strcmp (name, "feSpotLight"))
newnode = rsvg_new_node_light_source ('s');
else if (!strcmp (name, "fePointLight"))
newnode = rsvg_new_node_light_source ('p');
/* hack to make multiImage sort-of work */
else if (!strcmp (name, "multiImage"))
newnode = rsvg_new_switch ();
else if (!strcmp (name, "subImageRef"))
newnode = rsvg_new_image ();
else if (!strcmp (name, "subImage"))
newnode = rsvg_new_group ();
else if (!strcmp (name, "text"))
newnode = rsvg_new_text ();
else if (!strcmp (name, "tspan"))
newnode = rsvg_new_tspan ();
else if (!strcmp (name, "tref"))
newnode = rsvg_new_tref ();
else {
/* hack for bug 401115. whenever we encounter a node we don't understand, push it into a group.
this will allow us to handle things like conditionals properly. */
newnode = rsvg_new_group ();
}
if (newnode) {
g_assert (RSVG_NODE_TYPE (newnode) != RSVG_NODE_TYPE_INVALID);
newnode->name = (char *) name; /* libxml will keep this while parsing */
newnode->parent = ctx->priv->currentnode;
rsvg_node_set_atts (newnode, ctx, atts);
rsvg_defs_register_memory (ctx->priv->defs, newnode);
if (ctx->priv->currentnode) {
rsvg_node_group_pack (ctx->priv->currentnode, newnode);
ctx->priv->currentnode = newnode;
} else if (RSVG_NODE_TYPE (newnode) == RSVG_NODE_TYPE_SVG) {
ctx->priv->treebase = newnode;
ctx->priv->currentnode = newnode;
}
}
}
/* extra (title, desc, metadata) */
static void
rsvg_extra_handler_free (RsvgSaxHandler * self)
{
RsvgSaxHandlerExtra *z = (RsvgSaxHandlerExtra *) self;
if (z->stringptr) {
if (*z->stringptr)
g_string_free (*z->stringptr, TRUE);
*z->stringptr = z->string;
} else if (z->string) {
g_string_free (z->string, TRUE);
}
g_free (self);
}
static void
rsvg_extra_handler_characters (RsvgSaxHandler * self, const char *ch, int len)
{
RsvgSaxHandlerExtra *z = (RsvgSaxHandlerExtra *) self;
/* This isn't quite the correct behavior - in theory, any graphics
element may contain a title, desc, or metadata element */
if (!z->string)
return;
if (!ch || !len)
return;
if (!g_utf8_validate ((char *) ch, len, NULL)) {
char *utf8;
utf8 = rsvg_make_valid_utf8 ((char *) ch, len);
g_string_append (z->string, utf8);
g_free (utf8);
} else {
g_string_append_len (z->string, (char *) ch, len);
}
}
static void
rsvg_extra_handler_start (RsvgSaxHandler * self, const char *name, RsvgPropertyBag * atts)
{
}
static void
rsvg_extra_handler_end (RsvgSaxHandler * self, const char *name)
{
RsvgSaxHandlerExtra *z = (RsvgSaxHandlerExtra *) self;
RsvgHandle *ctx = z->ctx;
if (!strcmp (name, z->name)) {
if (ctx->priv->handler != NULL) {
ctx->priv->handler->free (ctx->priv->handler);
ctx->priv->handler = NULL;
}
}
}
static RsvgSaxHandlerExtra *
rsvg_start_extra (RsvgHandle * ctx,
const char *name,
GString **stringptr)
{
RsvgSaxHandlerExtra *handler = g_new0 (RsvgSaxHandlerExtra, 1);
RsvgNode *treebase = ctx->priv->treebase;
RsvgNode *currentnode = ctx->priv->currentnode;
gboolean do_care;
/* only parse <extra> for the <svg> node.
* This isn't quite the correct behavior - any graphics
* element may contain a <extra> element.
*/
do_care = treebase != NULL && treebase == currentnode;
handler->super.free = rsvg_extra_handler_free;
handler->super.characters = rsvg_extra_handler_characters;
handler->super.start_element = rsvg_extra_handler_start;
handler->super.end_element = rsvg_extra_handler_end;
handler->ctx = ctx;
handler->name = name; /* interned */
handler->string = do_care ? g_string_new (NULL) : NULL;
handler->stringptr = do_care ? stringptr : NULL;
ctx->priv->handler = &handler->super;
return handler;
}
/* start desc */
static void
rsvg_start_desc (RsvgHandle * ctx)
{
rsvg_start_extra (ctx, "desc", &ctx->priv->desc);
}
/* end desc */
/* start title */
static void
rsvg_start_title (RsvgHandle * ctx)
{
rsvg_start_extra (ctx, "title", &ctx->priv->title);
}
/* end title */
/* start metadata */
static void
rsvg_metadata_props_enumerate (const char *key, const char *value, gpointer user_data)
{
GString *metadata = (GString *) user_data;
g_string_append_printf (metadata, "%s=\"%s\" ", key, value);
}
static void
rsvg_metadata_handler_start (RsvgSaxHandler * self, const char *name, RsvgPropertyBag * atts)
{
RsvgSaxHandlerMetadata *z = (RsvgSaxHandlerMetadata *) self;
rsvg_extra_handler_start (self, name, atts);
if (!z->string)
return;
g_string_append_printf (z->string, "<%s ", name);
rsvg_property_bag_enumerate (atts, rsvg_metadata_props_enumerate, z->string);
g_string_append (z->string, ">\n");
}
static void
rsvg_metadata_handler_end (RsvgSaxHandler * self, const char *name)
{
RsvgSaxHandlerMetadata *z = (RsvgSaxHandlerMetadata *) self;
if (strcmp (name, z->name) != 0) {
if (z->string)
g_string_append_printf (z->string, "</%s>\n", name);
} else {
rsvg_extra_handler_end (self, name);
}
}
static void
rsvg_start_metadata (RsvgHandle * ctx)
{
RsvgSaxHandlerMetadata *handler = rsvg_start_extra (ctx, "metadata", &ctx->priv->metadata);
handler->super.start_element = rsvg_metadata_handler_start;
handler->super.end_element = rsvg_metadata_handler_end;
}
/* end metadata */
/* start xinclude */
typedef struct _RsvgSaxHandlerXinclude {
RsvgSaxHandler super;
RsvgSaxHandler *prev_handler;
RsvgHandle *ctx;
gboolean success;
gboolean in_fallback;
} RsvgSaxHandlerXinclude;
static void
rsvg_start_xinclude (RsvgHandle * ctx, RsvgPropertyBag * atts);
static void
rsvg_characters_impl (RsvgHandle * ctx, const xmlChar * ch, int len);
static void
rsvg_xinclude_handler_free (RsvgSaxHandler * self)
{
g_free (self);
}
static void
rsvg_xinclude_handler_characters (RsvgSaxHandler * self, const char *ch, int len)
{
RsvgSaxHandlerXinclude *z = (RsvgSaxHandlerXinclude *) self;
if (z->in_fallback) {
rsvg_characters_impl (z->ctx, (const xmlChar *) ch, len);
}
}
static void
rsvg_xinclude_handler_start (RsvgSaxHandler * self, const char *name, RsvgPropertyBag * atts)
{
RsvgSaxHandlerXinclude *z = (RsvgSaxHandlerXinclude *) self;
if (!z->success) {
if (z->in_fallback) {
if (!strcmp (name, "xi:include"))
rsvg_start_xinclude (z->ctx, atts);
else
rsvg_standard_element_start (z->ctx, (const char *) name, atts);
} else if (!strcmp (name, "xi:fallback")) {
z->in_fallback = TRUE;
}
}
}
static void
rsvg_xinclude_handler_end (RsvgSaxHandler * self, const char *name)
{
RsvgSaxHandlerXinclude *z = (RsvgSaxHandlerXinclude *) self;
RsvgHandle *ctx = z->ctx;
if (!strcmp (name, "include") || !strcmp (name, "xi:include")) {
if (ctx->priv->handler != NULL) {
RsvgSaxHandler *previous_handler;
previous_handler = z->prev_handler;
ctx->priv->handler->free (ctx->priv->handler);
ctx->priv->handler = previous_handler;
}
} else if (z->in_fallback) {
if (!strcmp (name, "xi:fallback"))
z->in_fallback = FALSE;
}
}
/* http://www.w3.org/TR/xinclude/ */
static void
rsvg_start_xinclude (RsvgHandle * ctx, RsvgPropertyBag * atts)
{
RsvgSaxHandlerXinclude *handler;
const char *href, *parse;
gboolean success = FALSE;
href = rsvg_property_bag_lookup (atts, "href");
if (href == NULL)
goto fallback;
parse = rsvg_property_bag_lookup (atts, "parse");
if (parse && !strcmp (parse, "text")) {
guint8 *data;
gsize data_len;
const char *encoding;
data = _rsvg_handle_acquire_data (ctx, href, NULL, &data_len, NULL);
if (data == NULL)
goto fallback;
encoding = rsvg_property_bag_lookup (atts, "encoding");
if (encoding && g_ascii_strcasecmp (encoding, "UTF-8") != 0) {
char *text_data;
gsize text_data_len;
text_data = g_convert (data, data_len, "utf-8", encoding, NULL,
&text_data_len, NULL);
g_free (data);
data = text_data;
data_len = text_data_len;
}
rsvg_characters_impl (ctx, (const xmlChar *) data, data_len);
g_free (data);
} else {
/* xml */
GInputStream *stream;
GError *err = NULL;
xmlDocPtr xml_doc;
xmlParserCtxtPtr xml_parser;
xmlParserInputBufferPtr buffer;
xmlParserInputPtr input;
stream = _rsvg_handle_acquire_stream (ctx, href, NULL, NULL);
if (stream == NULL)
goto fallback;
xml_parser = xmlCreatePushParserCtxt (&rsvgSAXHandlerStruct, ctx, NULL, 0, NULL);
buffer = _rsvg_xml_input_buffer_new_from_stream (stream, NULL /* cancellable */, XML_CHAR_ENCODING_NONE, &err);
g_object_unref (stream);
input = xmlNewIOInputStream (xml_parser, buffer /* adopts */, XML_CHAR_ENCODING_NONE);
if (xmlPushInput (xml_parser, input) < 0) {
g_clear_error (&err);
xmlFreeInputStream (input);
xmlFreeParserCtxt (xml_parser);
goto fallback;
}
(void) xmlParseDocument (xml_parser);
xml_doc = xml_parser->myDoc;
xmlFreeParserCtxt (xml_parser);
if (xml_doc)
xmlFreeDoc (xml_doc);
g_clear_error (&err);
}
success = TRUE;
fallback:
/* needed to handle xi:fallback */
handler = g_new0 (RsvgSaxHandlerXinclude, 1);
handler->super.free = rsvg_xinclude_handler_free;
handler->super.characters = rsvg_xinclude_handler_characters;
handler->super.start_element = rsvg_xinclude_handler_start;
handler->super.end_element = rsvg_xinclude_handler_end;
handler->prev_handler = ctx->priv->handler;
handler->ctx = ctx;
handler->success = success;
ctx->priv->handler = &handler->super;
}
/* end xinclude */
static void
rsvg_start_element (void *data, const xmlChar * name, const xmlChar ** atts)
{
RsvgPropertyBag *bag;
RsvgHandle *ctx = (RsvgHandle *) data;
bag = rsvg_property_bag_new ((const char **) atts);
if (ctx->priv->handler) {
ctx->priv->handler_nest++;
if (ctx->priv->handler->start_element != NULL)
ctx->priv->handler->start_element (ctx->priv->handler, (const char *) name, bag);
} else {
const char *tempname;
for (tempname = (const char *) name; *tempname != '\0'; tempname++)
if (*tempname == ':')
name = (const xmlChar *) (tempname + 1);
if (!strcmp ((const char *) name, "style"))
rsvg_start_style (ctx, bag);
else if (!strcmp ((const char *) name, "title"))
rsvg_start_title (ctx);
else if (!strcmp ((const char *) name, "desc"))
rsvg_start_desc (ctx);
else if (!strcmp ((const char *) name, "metadata"))
rsvg_start_metadata (ctx);
else if (!strcmp ((const char *) name, "include")) /* xi:include */
rsvg_start_xinclude (ctx, bag);
else
rsvg_standard_element_start (ctx, (const char *) name, bag);
}
rsvg_property_bag_free (bag);
}
static void
rsvg_end_element (void *data, const xmlChar * name)
{
RsvgHandle *ctx = (RsvgHandle *) data;
if (ctx->priv->handler_nest > 0 && ctx->priv->handler != NULL) {
if (ctx->priv->handler->end_element != NULL)
ctx->priv->handler->end_element (ctx->priv->handler, (const char *) name);
ctx->priv->handler_nest--;
} else {
const char *tempname;
for (tempname = (const char *) name; *tempname != '\0'; tempname++)
if (*tempname == ':')
name = (const xmlChar *) (tempname + 1);
if (ctx->priv->handler != NULL) {
ctx->priv->handler->free (ctx->priv->handler);
ctx->priv->handler = NULL;
}
if (ctx->priv->currentnode &&
!strcmp ((const char *) name, ctx->priv->currentnode->name))
rsvg_pop_def_group (ctx);
/* FIXMEchpe: shouldn't this check that currentnode == treebase or sth like that? */
if (ctx->priv->treebase && !strcmp ((const char *)name, "svg"))
_rsvg_node_svg_apply_atts ((RsvgNodeSvg *)ctx->priv->treebase, ctx);
}
}
static void
_rsvg_node_chars_free (RsvgNode * node)
{
RsvgNodeChars *self = (RsvgNodeChars *) node;
g_string_free (self->contents, TRUE);
_rsvg_node_free (node);
}
static RsvgNodeChars *
rsvg_new_node_chars (const char *text,
int len)
{
RsvgNodeChars *self;
self = g_new (RsvgNodeChars, 1);
_rsvg_node_init (&self->super, RSVG_NODE_TYPE_CHARS);
if (!g_utf8_validate (text, len, NULL)) {
char *utf8;
utf8 = rsvg_make_valid_utf8 (text, len);
self->contents = g_string_new (utf8);
g_free (utf8);
} else {
self->contents = g_string_new_len (text, len);
}
self->super.free = _rsvg_node_chars_free;
self->super.state->cond_true = FALSE;
return self;
}
static void
rsvg_characters_impl (RsvgHandle * ctx, const xmlChar * ch, int len)
{
RsvgNodeChars *self;
if (!ch || !len)
return;
if (ctx->priv->currentnode) {
RsvgNodeType type = RSVG_NODE_TYPE (ctx->priv->currentnode);
if (type == RSVG_NODE_TYPE_TSPAN ||
type == RSVG_NODE_TYPE_TEXT) {
guint i;
/* find the last CHARS node in the text or tspan node, so that we
can coalesce the text, and thus avoid screwing up the Pango layouts */
self = NULL;
for (i = 0; i < ctx->priv->currentnode->children->len; i++) {
RsvgNode *node = g_ptr_array_index (ctx->priv->currentnode->children, i);
if (RSVG_NODE_TYPE (node) == RSVG_NODE_TYPE_CHARS) {
self = (RsvgNodeChars*)node;
}
else if (RSVG_NODE_TYPE (node) == RSVG_NODE_TYPE_TSPAN) {
self = NULL;
}
}
if (self != NULL) {
if (!g_utf8_validate ((char *) ch, len, NULL)) {
char *utf8;
utf8 = rsvg_make_valid_utf8 ((char *) ch, len);
g_string_append (self->contents, utf8);
g_free (utf8);
} else {
g_string_append_len (self->contents, (char *)ch, len);
}
return;
}
}
}
self = rsvg_new_node_chars ((char *) ch, len);
rsvg_defs_register_memory (ctx->priv->defs, (RsvgNode *) self);
if (ctx->priv->currentnode)
rsvg_node_group_pack (ctx->priv->currentnode, (RsvgNode *) self);
}
static void
rsvg_characters (void *data, const xmlChar * ch, int len)
{
RsvgHandle *ctx = (RsvgHandle *) data;
if (ctx->priv->handler && ctx->priv->handler->characters != NULL) {
ctx->priv->handler->characters (ctx->priv->handler, (const char *) ch, len);
return;
}
rsvg_characters_impl (ctx, ch, len);
}
static xmlEntityPtr
rsvg_get_entity (void *data, const xmlChar * name)
{
RsvgHandle *ctx = (RsvgHandle *) data;
xmlEntityPtr entity;
entity = g_hash_table_lookup (ctx->priv->entities, name);
return entity;
}
static void
rsvg_entity_decl (void *data, const xmlChar * name, int type,
const xmlChar * publicId, const xmlChar * systemId, xmlChar * content)
{
RsvgHandle *ctx = (RsvgHandle *) data;
GHashTable *entities = ctx->priv->entities;
xmlEntityPtr entity;
xmlChar *resolvedSystemId = NULL, *resolvedPublicId = NULL;
if (systemId)
resolvedSystemId = xmlBuildRelativeURI (systemId, (xmlChar*) rsvg_handle_get_base_uri (ctx));
else if (publicId)
resolvedPublicId = xmlBuildRelativeURI (publicId, (xmlChar*) rsvg_handle_get_base_uri (ctx));
if (type == XML_EXTERNAL_PARAMETER_ENTITY && !content) {
guint8 *entity_data;
gsize entity_data_len;
if (systemId)
entity_data = _rsvg_handle_acquire_data (ctx,
(const char *) systemId,
NULL,
&entity_data_len,
NULL);
else if (publicId)
entity_data = _rsvg_handle_acquire_data (ctx,
(const char *) publicId,
NULL,
&entity_data_len,
NULL);
else
entity_data = NULL;
if (entity_data) {
content = xmlCharStrndup (entity_data, entity_data_len);
g_free (entity_data);
}
}
entity = xmlNewEntity(NULL, name, type, resolvedPublicId, resolvedSystemId, content);
xmlFree(resolvedPublicId);
xmlFree(resolvedSystemId);
g_hash_table_insert (entities, g_strdup ((const char*) name), entity);
}
static void
rsvg_unparsed_entity_decl (void *ctx,
const xmlChar * name,
const xmlChar * publicId,
const xmlChar * systemId, const xmlChar * notationName)
{
rsvg_entity_decl (ctx, name, XML_INTERNAL_GENERAL_ENTITY, publicId, systemId, NULL);
}
static xmlEntityPtr
rsvg_get_parameter_entity (void *data, const xmlChar * name)
{
RsvgHandle *ctx = (RsvgHandle *) data;
xmlEntityPtr entity;
entity = g_hash_table_lookup (ctx->priv->entities, name);
return entity;
}
static void
rsvg_error_cb (void *data, const char *msg, ...)
{
#ifdef G_ENABLE_DEBUG
va_list args;
va_start (args, msg);
vfprintf (stderr, msg, args);
va_end (args);
#endif
}
static void
rsvg_processing_instruction (void *ctx, const xmlChar * target, const xmlChar * data)
{
/* http://www.w3.org/TR/xml-stylesheet/ */
RsvgHandle *handle = (RsvgHandle *) ctx;
if (!strcmp ((const char *) target, "xml-stylesheet")) {
RsvgPropertyBag *atts;
char **xml_atts;
xml_atts = rsvg_css_parse_xml_attribute_string ((const char *) data);
if (xml_atts) {
const char *value;
atts = rsvg_property_bag_new ((const char **) xml_atts);
value = rsvg_property_bag_lookup (atts, "alternate");
if (!value || !value[0] || (strcmp (value, "no") != 0)) {
value = rsvg_property_bag_lookup (atts, "type");
if (value && strcmp (value, "text/css") == 0) {
value = rsvg_property_bag_lookup (atts, "href");
if (value && value[0]) {
guint8 *style_data;
gsize style_data_len;
char *mime_type = NULL;
style_data = _rsvg_handle_acquire_data (handle,
value,
&mime_type,
&style_data_len,
NULL);
if (style_data &&
mime_type &&
strcmp (mime_type, "text/css") == 0) {
rsvg_parse_cssbuffer (handle, (char *) style_data, style_data_len);
}
g_free (mime_type);
g_free (style_data);
}
}
}
rsvg_property_bag_free (atts);
g_strfreev (xml_atts);
}
}
}
void
rsvg_SAX_handler_struct_init (void)
{
if (!rsvgSAXHandlerStructInited) {
rsvgSAXHandlerStructInited = TRUE;
memset (&rsvgSAXHandlerStruct, 0, sizeof (rsvgSAXHandlerStruct));
rsvgSAXHandlerStruct.getEntity = rsvg_get_entity;
rsvgSAXHandlerStruct.entityDecl = rsvg_entity_decl;
rsvgSAXHandlerStruct.unparsedEntityDecl = rsvg_unparsed_entity_decl;
rsvgSAXHandlerStruct.getParameterEntity = rsvg_get_parameter_entity;
rsvgSAXHandlerStruct.characters = rsvg_characters;
rsvgSAXHandlerStruct.error = rsvg_error_cb;
rsvgSAXHandlerStruct.cdataBlock = rsvg_characters;
rsvgSAXHandlerStruct.startElement = rsvg_start_element;
rsvgSAXHandlerStruct.endElement = rsvg_end_element;
rsvgSAXHandlerStruct.processingInstruction = rsvg_processing_instruction;
}
}
/* http://www.ietf.org/rfc/rfc2396.txt */
static gboolean
rsvg_path_is_uri (char const *path)
{
char const *p;
if (path == NULL)
return FALSE;
if (strlen (path) < 4)
return FALSE;
if ((path[0] < 'a' || path[0] > 'z') &&
(path[0] < 'A' || path[0] > 'Z')) {
return FALSE;
}
for (p = &path[1];
(*p >= 'a' && *p <= 'z') ||
(*p >= 'A' && *p <= 'Z') ||
(*p >= '0' && *p <= '9') ||
*p == '+' ||
*p == '-' ||
*p == '.';
p++);
if (strlen (p) < 3)
return FALSE;
return (p[0] == ':' && p[1] == '/' && p[2] == '/');
}
gchar *
rsvg_get_base_uri_from_filename (const gchar * filename)
{
gchar *current_dir;
gchar *absolute_filename;
gchar *base_uri;
if (g_path_is_absolute (filename))
return g_filename_to_uri (filename, NULL, NULL);
current_dir = g_get_current_dir ();
absolute_filename = g_build_filename (current_dir, filename, NULL);
base_uri = g_filename_to_uri (absolute_filename, NULL, NULL);
g_free (absolute_filename);
g_free (current_dir);
return base_uri;
}
/**
* rsvg_handle_set_base_uri
* @handle: A #RsvgHandle
* @base_uri: The base uri
*
* Set the base URI for this SVG. This can only be called before rsvg_handle_write()
* has been called.
*
* Since: 2.9
*/
void