-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcairo-svg-surface.c
2887 lines (2419 loc) · 86.7 KB
/
cairo-svg-surface.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
/* vim: set sw=4 sts=4: -*- Mode: c; tab-width: 8; c-basic-offset: 4; indent-tabs-mode: t; -*- */
/* cairo - a vector graphics library with display and print output
*
* Copyright © 2004 Red Hat, Inc
* Copyright © 2005-2007 Emmanuel Pacaud <emmanuel.pacaud@free.fr>
* Copyright © 2006 Red Hat, Inc
*
* This library is free software; you can redistribute it and/or
* modify it either under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation
* (the "LGPL") or, at your option, under the terms of the Mozilla
* Public License Version 1.1 (the "MPL"). If you do not alter this
* notice, a recipient may use your version of this file under either
* the MPL or the LGPL.
*
* You should have received a copy of the LGPL along with this library
* in the file COPYING-LGPL-2.1; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
* You should have received a copy of the MPL along with this library
* in the file COPYING-MPL-1.1
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
* OF ANY KIND, either express or implied. See the LGPL or the MPL for
* the specific language governing rights and limitations.
*
* The Original Code is the cairo graphics library.
*
* The Initial Developer of the Original Code is University of Southern
* California.
*
* Contributor(s):
* Kristian Høgsberg <krh@redhat.com>
* Emmanuel Pacaud <emmanuel.pacaud@free.fr>
* Carl Worth <cworth@cworth.org>
*/
#define _BSD_SOURCE /* for snprintf() */
#include "cairoint.h"
#include "cairo-svg.h"
#include "cairo-array-private.h"
#include "cairo-analysis-surface-private.h"
#include "cairo-default-context-private.h"
#include "cairo-error-private.h"
#include "cairo-image-info-private.h"
#include "cairo-image-surface-private.h"
#include "cairo-recording-surface-inline.h"
#include "cairo-output-stream-private.h"
#include "cairo-path-fixed-private.h"
#include "cairo-paginated-private.h"
#include "cairo-scaled-font-subsets-private.h"
#include "cairo-surface-clipper-private.h"
#include "cairo-surface-snapshot-inline.h"
#include "cairo-svg-surface-private.h"
/**
* SECTION:cairo-svg
* @Title: SVG Surfaces
* @Short_Description: Rendering SVG documents
* @See_Also: #cairo_surface_t
*
* The SVG surface is used to render cairo graphics to
* SVG files and is a multi-page vector surface backend.
**/
/**
* CAIRO_HAS_SVG_SURFACE:
*
* Defined if the SVG surface backend is available.
* This macro can be used to conditionally compile backend-specific code.
*
* Since: 1.2
**/
typedef struct cairo_svg_page cairo_svg_page_t;
static const int invalid_pattern_id = -1;
static const cairo_svg_version_t _cairo_svg_versions[] =
{
CAIRO_SVG_VERSION_1_1,
CAIRO_SVG_VERSION_1_2
};
#define CAIRO_SVG_VERSION_LAST ARRAY_LENGTH (_cairo_svg_versions)
static const char *_cairo_svg_supported_mime_types[] =
{
CAIRO_MIME_TYPE_JPEG,
CAIRO_MIME_TYPE_PNG,
CAIRO_MIME_TYPE_URI,
NULL
};
static void
_cairo_svg_surface_emit_path (cairo_output_stream_t *output,
const cairo_path_fixed_t *path,
const cairo_matrix_t *ctm_inverse);
static cairo_bool_t
_cairo_svg_version_has_page_set_support (cairo_svg_version_t version)
{
return version > CAIRO_SVG_VERSION_1_1;
}
static const char * _cairo_svg_version_strings[CAIRO_SVG_VERSION_LAST] =
{
"SVG 1.1",
"SVG 1.2"
};
static const char * _cairo_svg_internal_version_strings[CAIRO_SVG_VERSION_LAST] =
{
"1.1",
"1.2"
};
struct cairo_svg_page {
unsigned int surface_id;
unsigned int clip_level;
cairo_output_stream_t *xml_node;
};
struct cairo_svg_document {
cairo_output_stream_t *output_stream;
unsigned long refcount;
cairo_surface_t *owner;
cairo_bool_t finished;
double width;
double height;
cairo_output_stream_t *xml_node_defs;
cairo_output_stream_t *xml_node_glyphs;
unsigned int linear_pattern_id;
unsigned int radial_pattern_id;
unsigned int pattern_id;
unsigned int filter_id;
unsigned int clip_id;
unsigned int mask_id;
cairo_bool_t alpha_filter;
cairo_svg_version_t svg_version;
cairo_scaled_font_subsets_t *font_subsets;
};
static cairo_status_t
_cairo_svg_document_create (cairo_output_stream_t *stream,
double width,
double height,
cairo_svg_version_t version,
cairo_svg_document_t **document_out);
static cairo_status_t
_cairo_svg_document_destroy (cairo_svg_document_t *document);
static cairo_status_t
_cairo_svg_document_finish (cairo_svg_document_t *document);
static cairo_svg_document_t *
_cairo_svg_document_reference (cairo_svg_document_t *document);
static unsigned int
_cairo_svg_document_allocate_mask_id (cairo_svg_document_t *document);
static cairo_surface_t *
_cairo_svg_surface_create_for_document (cairo_svg_document_t *document,
cairo_content_t content,
double width,
double height);
static cairo_surface_t *
_cairo_svg_surface_create_for_stream_internal (cairo_output_stream_t *stream,
double width,
double height,
cairo_svg_version_t version);
static const cairo_surface_backend_t cairo_svg_surface_backend;
static const cairo_paginated_surface_backend_t cairo_svg_surface_paginated_backend;
/**
* cairo_svg_surface_create_for_stream:
* @write_func: a #cairo_write_func_t to accept the output data, may be %NULL
* to indicate a no-op @write_func. With a no-op @write_func,
* the surface may be queried or used as a source without
* generating any temporary files.
* @closure: the closure argument for @write_func
* @width_in_points: width of the surface, in points (1 point == 1/72.0 inch)
* @height_in_points: height of the surface, in points (1 point == 1/72.0 inch)
*
* Creates a SVG surface of the specified size in points to be written
* incrementally to the stream represented by @write_func and @closure.
*
* Return value: a pointer to the newly created surface. The caller
* owns the surface and should call cairo_surface_destroy() when done
* with it.
*
* This function always returns a valid pointer, but it will return a
* pointer to a "nil" surface if an error such as out of memory
* occurs. You can use cairo_surface_status() to check for this.
*
* Since: 1.2
**/
cairo_surface_t *
cairo_svg_surface_create_for_stream (cairo_write_func_t write_func,
void *closure,
double width,
double height)
{
cairo_output_stream_t *stream;
stream = _cairo_output_stream_create (write_func, NULL, closure);
if (_cairo_output_stream_get_status (stream))
return _cairo_surface_create_in_error (_cairo_output_stream_destroy (stream));
return _cairo_svg_surface_create_for_stream_internal (stream, width, height, CAIRO_SVG_VERSION_1_1);
}
/**
* cairo_svg_surface_create:
* @filename: a filename for the SVG output (must be writable), %NULL may be
* used to specify no output. This will generate a SVG surface that
* may be queried and used as a source, without generating a
* temporary file.
* @width_in_points: width of the surface, in points (1 point == 1/72.0 inch)
* @height_in_points: height of the surface, in points (1 point == 1/72.0 inch)
*
* Creates a SVG surface of the specified size in points to be written
* to @filename.
*
* The SVG surface backend recognizes the following MIME types for the
* data attached to a surface (see cairo_surface_set_mime_data()) when
* it is used as a source pattern for drawing on this surface:
* %CAIRO_MIME_TYPE_JPEG, %CAIRO_MIME_TYPE_PNG,
* %CAIRO_MIME_TYPE_URI. If any of them is specified, the SVG backend
* emits a href with the content of MIME data instead of a surface
* snapshot (PNG, Base64-encoded) in the corresponding image tag.
*
* The unofficial MIME type %CAIRO_MIME_TYPE_URI is examined
* first. If present, the URI is emitted as is: assuring the
* correctness of URI is left to the client code.
*
* If %CAIRO_MIME_TYPE_URI is not present, but %CAIRO_MIME_TYPE_JPEG
* or %CAIRO_MIME_TYPE_PNG is specified, the corresponding data is
* Base64-encoded and emitted.
*
* Return value: a pointer to the newly created surface. The caller
* owns the surface and should call cairo_surface_destroy() when done
* with it.
*
* This function always returns a valid pointer, but it will return a
* pointer to a "nil" surface if an error such as out of memory
* occurs. You can use cairo_surface_status() to check for this.
*
* Since: 1.2
**/
cairo_surface_t *
cairo_svg_surface_create (const char *filename,
double width,
double height)
{
cairo_output_stream_t *stream;
stream = _cairo_output_stream_create_for_filename (filename);
if (_cairo_output_stream_get_status (stream))
return _cairo_surface_create_in_error (_cairo_output_stream_destroy (stream));
return _cairo_svg_surface_create_for_stream_internal (stream, width, height, CAIRO_SVG_VERSION_1_1);
}
static cairo_bool_t
_cairo_surface_is_svg (cairo_surface_t *surface)
{
return surface->backend == &cairo_svg_surface_backend;
}
/* If the abstract_surface is a paginated surface, and that paginated
* surface's target is a svg_surface, then set svg_surface to that
* target. Otherwise return FALSE.
*/
static cairo_bool_t
_extract_svg_surface (cairo_surface_t *surface,
cairo_svg_surface_t **svg_surface)
{
cairo_surface_t *target;
cairo_status_t status_ignored;
if (surface->status)
return FALSE;
if (surface->finished) {
status_ignored = _cairo_surface_set_error (surface,
_cairo_error (CAIRO_STATUS_SURFACE_FINISHED));
return FALSE;
}
if (! _cairo_surface_is_paginated (surface)) {
status_ignored = _cairo_surface_set_error (surface,
_cairo_error (CAIRO_STATUS_SURFACE_TYPE_MISMATCH));
return FALSE;
}
target = _cairo_paginated_surface_get_target (surface);
if (target->status) {
status_ignored = _cairo_surface_set_error (surface,
target->status);
return FALSE;
}
if (target->finished) {
status_ignored = _cairo_surface_set_error (surface,
_cairo_error (CAIRO_STATUS_SURFACE_FINISHED));
return FALSE;
}
if (! _cairo_surface_is_svg (target)) {
status_ignored = _cairo_surface_set_error (surface,
_cairo_error (CAIRO_STATUS_SURFACE_TYPE_MISMATCH));
return FALSE;
}
*svg_surface = (cairo_svg_surface_t *) target;
return TRUE;
}
/**
* cairo_svg_surface_restrict_to_version:
* @surface: a SVG #cairo_surface_t
* @version: SVG version
*
* Restricts the generated SVG file to @version. See cairo_svg_get_versions()
* for a list of available version values that can be used here.
*
* This function should only be called before any drawing operations
* have been performed on the given surface. The simplest way to do
* this is to call this function immediately after creating the
* surface.
*
* Since: 1.2
**/
void
cairo_svg_surface_restrict_to_version (cairo_surface_t *abstract_surface,
cairo_svg_version_t version)
{
cairo_svg_surface_t *surface = NULL; /* hide compiler warning */
if (! _extract_svg_surface (abstract_surface, &surface))
return;
if (version < CAIRO_SVG_VERSION_LAST)
surface->document->svg_version = version;
}
/**
* cairo_svg_get_versions:
* @versions: supported version list
* @num_versions: list length
*
* Used to retrieve the list of supported versions. See
* cairo_svg_surface_restrict_to_version().
*
* Since: 1.2
**/
void
cairo_svg_get_versions (cairo_svg_version_t const **versions,
int *num_versions)
{
if (versions != NULL)
*versions = _cairo_svg_versions;
if (num_versions != NULL)
*num_versions = CAIRO_SVG_VERSION_LAST;
}
/**
* cairo_svg_version_to_string:
* @version: a version id
*
* Get the string representation of the given @version id. This function
* will return %NULL if @version isn't valid. See cairo_svg_get_versions()
* for a way to get the list of valid version ids.
*
* Return value: the string associated to given version.
*
* Since: 1.2
**/
const char *
cairo_svg_version_to_string (cairo_svg_version_t version)
{
if (version >= CAIRO_SVG_VERSION_LAST)
return NULL;
return _cairo_svg_version_strings[version];
}
static cairo_bool_t
_cliprect_covers_surface (cairo_svg_surface_t *surface,
cairo_path_fixed_t *path)
{
cairo_box_t box;
if (_cairo_path_fixed_is_box (path, &box)) {
if (box.p1.x <= 0 &&
box.p1.y <= 0 &&
_cairo_fixed_to_double (box.p2.x) >= surface->width &&
_cairo_fixed_to_double (box.p2.y) >= surface->height)
{
return TRUE;
}
}
return FALSE;
}
static cairo_status_t
_cairo_svg_surface_clipper_intersect_clip_path (cairo_surface_clipper_t *clipper,
cairo_path_fixed_t *path,
cairo_fill_rule_t fill_rule,
double tolerance,
cairo_antialias_t antialias)
{
cairo_svg_surface_t *surface = cairo_container_of (clipper,
cairo_svg_surface_t,
clipper);
cairo_svg_document_t *document = surface->document;
unsigned int i;
if (path == NULL) {
for (i = 0; i < surface->clip_level; i++)
_cairo_output_stream_printf (surface->xml_node, "</g>\n");
surface->clip_level = 0;
return CAIRO_STATUS_SUCCESS;
}
/* skip trivial whole-page clips */
if (_cliprect_covers_surface (surface, path))
return CAIRO_STATUS_SUCCESS;
_cairo_output_stream_printf (document->xml_node_defs,
"<clipPath id=\"clip%d\">\n"
" <path ",
document->clip_id);
_cairo_svg_surface_emit_path (document->xml_node_defs, path, NULL);
_cairo_output_stream_printf (document->xml_node_defs,
"/>\n"
"</clipPath>\n");
_cairo_output_stream_printf (surface->xml_node,
"<g clip-path=\"url(#clip%d)\" "
"clip-rule=\"%s\">\n",
document->clip_id,
fill_rule == CAIRO_FILL_RULE_EVEN_ODD ?
"evenodd" : "nonzero");
document->clip_id++;
surface->clip_level++;
return CAIRO_STATUS_SUCCESS;
}
static cairo_surface_t *
_cairo_svg_surface_create_for_document (cairo_svg_document_t *document,
cairo_content_t content,
double width,
double height)
{
cairo_svg_surface_t *surface;
cairo_surface_t *paginated;
cairo_status_t status, status_ignored;
surface = malloc (sizeof (cairo_svg_surface_t));
if (unlikely (surface == NULL))
return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
_cairo_surface_init (&surface->base,
&cairo_svg_surface_backend,
NULL, /* device */
content,
TRUE); /* is_vector */
surface->width = width;
surface->height = height;
surface->document = _cairo_svg_document_reference (document);
surface->clip_level = 0;
_cairo_surface_clipper_init (&surface->clipper,
_cairo_svg_surface_clipper_intersect_clip_path);
surface->base_clip = document->clip_id++;
surface->is_base_clip_emitted = FALSE;
surface->xml_node = _cairo_memory_stream_create ();
status = _cairo_output_stream_get_status (surface->xml_node);
if (unlikely (status))
goto CLEANUP;
_cairo_array_init (&surface->page_set, sizeof (cairo_svg_page_t));
if (content == CAIRO_CONTENT_COLOR) {
_cairo_output_stream_printf (surface->xml_node,
"<rect width=\"%f\" height=\"%f\" "
"style=\"opacity:1;stroke:none;"
"fill:rgb(0,0,0);\"/>\n",
width, height);
status = _cairo_output_stream_get_status (surface->xml_node);
if (unlikely (status))
goto CLEANUP;
}
surface->paginated_mode = CAIRO_PAGINATED_MODE_ANALYZE;
surface->force_fallbacks = FALSE;
surface->content = content;
paginated = _cairo_paginated_surface_create (&surface->base,
surface->content,
&cairo_svg_surface_paginated_backend);
status = paginated->status;
if (status == CAIRO_STATUS_SUCCESS) {
/* paginated keeps the only reference to surface now, drop ours */
cairo_surface_destroy (&surface->base);
return paginated;
}
/* ignore status as we are on the error path */
CLEANUP:
status_ignored = _cairo_output_stream_destroy (surface->xml_node);
status_ignored = _cairo_svg_document_destroy (document);
free (surface);
return _cairo_surface_create_in_error (status);
}
static cairo_surface_t *
_cairo_svg_surface_create_for_stream_internal (cairo_output_stream_t *stream,
double width,
double height,
cairo_svg_version_t version)
{
cairo_svg_document_t *document = NULL; /* silence compiler */
cairo_surface_t *surface;
cairo_status_t status;
status = _cairo_svg_document_create (stream,
width, height, version,
&document);
if (unlikely (status)) {
surface = _cairo_surface_create_in_error (status);
/* consume the output stream on behalf of caller */
status = _cairo_output_stream_destroy (stream);
return surface;
}
surface = _cairo_svg_surface_create_for_document (document, CAIRO_CONTENT_COLOR_ALPHA,
width, height);
if (surface->status) {
status = _cairo_svg_document_destroy (document);
return surface;
}
document->owner = surface;
status = _cairo_svg_document_destroy (document);
/* the ref count should be 2 at this point */
assert (status == CAIRO_STATUS_SUCCESS);
return surface;
}
static cairo_svg_page_t *
_cairo_svg_surface_store_page (cairo_svg_surface_t *surface)
{
cairo_svg_page_t page;
cairo_output_stream_t *stream;
cairo_int_status_t status;
unsigned int i;
stream = _cairo_memory_stream_create ();
if (_cairo_output_stream_get_status (stream)) {
status = _cairo_output_stream_destroy (stream);
return NULL;
}
page.surface_id = surface->base.unique_id;
page.clip_level = surface->clip_level;
page.xml_node = surface->xml_node;
if (_cairo_array_append (&surface->page_set, &page)) {
status = _cairo_output_stream_destroy (stream);
return NULL;
}
surface->xml_node = stream;
surface->clip_level = 0;
for (i = 0; i < page.clip_level; i++)
_cairo_output_stream_printf (page.xml_node, "</g>\n");
_cairo_surface_clipper_reset (&surface->clipper);
return _cairo_array_index (&surface->page_set,
surface->page_set.num_elements - 1);
}
static cairo_int_status_t
_cairo_svg_surface_copy_page (void *abstract_surface)
{
cairo_svg_surface_t *surface = abstract_surface;
cairo_svg_page_t *page;
page = _cairo_svg_surface_store_page (surface);
if (unlikely (page == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
_cairo_memory_stream_copy (page->xml_node, surface->xml_node);
return CAIRO_STATUS_SUCCESS;
}
static cairo_int_status_t
_cairo_svg_surface_show_page (void *abstract_surface)
{
cairo_svg_surface_t *surface = abstract_surface;
if (unlikely (_cairo_svg_surface_store_page (surface) == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
return CAIRO_STATUS_SUCCESS;
}
static void
_cairo_svg_surface_emit_transform (cairo_output_stream_t *output,
char const *attribute_str,
const cairo_matrix_t *object_matrix,
const cairo_matrix_t *parent_matrix)
{
cairo_matrix_t matrix = *object_matrix;
if (parent_matrix != NULL)
cairo_matrix_multiply (&matrix, &matrix, parent_matrix);
if (!_cairo_matrix_is_identity (&matrix))
_cairo_output_stream_printf (output,
"%s=\"matrix(%f,%f,%f,%f,%f,%f)\"",
attribute_str,
matrix.xx, matrix.yx,
matrix.xy, matrix.yy,
matrix.x0, matrix.y0);
}
typedef struct {
cairo_output_stream_t *output;
const cairo_matrix_t *ctm_inverse;
} svg_path_info_t;
static cairo_status_t
_cairo_svg_path_move_to (void *closure,
const cairo_point_t *point)
{
svg_path_info_t *info = closure;
double x = _cairo_fixed_to_double (point->x);
double y = _cairo_fixed_to_double (point->y);
if (info->ctm_inverse)
cairo_matrix_transform_point (info->ctm_inverse, &x, &y);
_cairo_output_stream_printf (info->output, "M %f %f ", x, y);
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_cairo_svg_path_line_to (void *closure,
const cairo_point_t *point)
{
svg_path_info_t *info = closure;
double x = _cairo_fixed_to_double (point->x);
double y = _cairo_fixed_to_double (point->y);
if (info->ctm_inverse)
cairo_matrix_transform_point (info->ctm_inverse, &x, &y);
_cairo_output_stream_printf (info->output, "L %f %f ", x, y);
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_cairo_svg_path_curve_to (void *closure,
const cairo_point_t *b,
const cairo_point_t *c,
const cairo_point_t *d)
{
svg_path_info_t *info = closure;
double bx = _cairo_fixed_to_double (b->x);
double by = _cairo_fixed_to_double (b->y);
double cx = _cairo_fixed_to_double (c->x);
double cy = _cairo_fixed_to_double (c->y);
double dx = _cairo_fixed_to_double (d->x);
double dy = _cairo_fixed_to_double (d->y);
if (info->ctm_inverse) {
cairo_matrix_transform_point (info->ctm_inverse, &bx, &by);
cairo_matrix_transform_point (info->ctm_inverse, &cx, &cy);
cairo_matrix_transform_point (info->ctm_inverse, &dx, &dy);
}
_cairo_output_stream_printf (info->output,
"C %f %f %f %f %f %f ",
bx, by, cx, cy, dx, dy);
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_cairo_svg_path_close_path (void *closure)
{
svg_path_info_t *info = closure;
_cairo_output_stream_printf (info->output, "Z ");
return CAIRO_STATUS_SUCCESS;
}
static void
_cairo_svg_surface_emit_path (cairo_output_stream_t *output,
const cairo_path_fixed_t *path,
const cairo_matrix_t *ctm_inverse)
{
cairo_status_t status;
svg_path_info_t info;
_cairo_output_stream_printf (output, "d=\"");
info.output = output;
info.ctm_inverse = ctm_inverse;
status = _cairo_path_fixed_interpret (path,
_cairo_svg_path_move_to,
_cairo_svg_path_line_to,
_cairo_svg_path_curve_to,
_cairo_svg_path_close_path,
&info);
assert (status == CAIRO_STATUS_SUCCESS);
_cairo_output_stream_printf (output, "\"");
}
static cairo_int_status_t
_cairo_svg_document_emit_outline_glyph_data (cairo_svg_document_t *document,
cairo_scaled_font_t *scaled_font,
unsigned long glyph_index)
{
cairo_scaled_glyph_t *scaled_glyph;
cairo_int_status_t status;
status = _cairo_scaled_glyph_lookup (scaled_font,
glyph_index,
CAIRO_SCALED_GLYPH_INFO_METRICS|
CAIRO_SCALED_GLYPH_INFO_PATH,
&scaled_glyph);
if (unlikely (status))
return status;
_cairo_output_stream_printf (document->xml_node_glyphs,
"<path style=\"stroke:none;\" ");
_cairo_svg_surface_emit_path (document->xml_node_glyphs,
scaled_glyph->path, NULL);
_cairo_output_stream_printf (document->xml_node_glyphs,
"/>\n");
return status;
}
static cairo_int_status_t
_cairo_svg_document_emit_bitmap_glyph_data (cairo_svg_document_t *document,
cairo_scaled_font_t *scaled_font,
unsigned long glyph_index)
{
cairo_scaled_glyph_t *scaled_glyph;
cairo_image_surface_t *image;
cairo_status_t status;
uint8_t *row, *byte;
int rows, cols;
int x, y, bit;
status = _cairo_scaled_glyph_lookup (scaled_font,
glyph_index,
CAIRO_SCALED_GLYPH_INFO_METRICS |
CAIRO_SCALED_GLYPH_INFO_SURFACE,
&scaled_glyph);
if (unlikely (status))
return status;
image = _cairo_image_surface_coerce_to_format (scaled_glyph->surface,
CAIRO_FORMAT_A1);
status = image->base.status;
if (unlikely (status))
return status;
_cairo_output_stream_printf (document->xml_node_glyphs, "<g");
_cairo_svg_surface_emit_transform (document->xml_node_glyphs, " transform",
&image->base.device_transform_inverse, NULL);
_cairo_output_stream_printf (document->xml_node_glyphs, ">\n");
for (y = 0, row = image->data, rows = image->height; rows; row += image->stride, rows--, y++) {
for (x = 0, byte = row, cols = (image->width + 7) / 8; cols; byte++, cols--) {
uint8_t output_byte = CAIRO_BITSWAP8_IF_LITTLE_ENDIAN (*byte);
for (bit = 7; bit >= 0 && x < image->width; bit--, x++) {
if (output_byte & (1 << bit)) {
_cairo_output_stream_printf (document->xml_node_glyphs,
"<rect x=\"%d\" y=\"%d\" width=\"1\" height=\"1\"/>\n",
x, y);
}
}
}
}
_cairo_output_stream_printf (document->xml_node_glyphs, "</g>\n");
cairo_surface_destroy (&image->base);
return CAIRO_STATUS_SUCCESS;
}
static cairo_int_status_t
_cairo_svg_document_emit_glyph (cairo_svg_document_t *document,
cairo_scaled_font_t *scaled_font,
unsigned long scaled_font_glyph_index,
unsigned int font_id,
unsigned int subset_glyph_index)
{
cairo_int_status_t status;
_cairo_output_stream_printf (document->xml_node_glyphs,
"<symbol overflow=\"visible\" id=\"glyph%d-%d\">\n",
font_id,
subset_glyph_index);
status = _cairo_svg_document_emit_outline_glyph_data (document,
scaled_font,
scaled_font_glyph_index);
if (status == CAIRO_INT_STATUS_UNSUPPORTED)
status = _cairo_svg_document_emit_bitmap_glyph_data (document,
scaled_font,
scaled_font_glyph_index);
if (unlikely (status))
return status;
_cairo_output_stream_printf (document->xml_node_glyphs, "</symbol>\n");
return CAIRO_INT_STATUS_SUCCESS;
}
static cairo_int_status_t
_cairo_svg_document_emit_font_subset (cairo_scaled_font_subset_t *font_subset,
void *closure)
{
cairo_svg_document_t *document = closure;
cairo_int_status_t status = CAIRO_INT_STATUS_SUCCESS;
unsigned int i;
_cairo_scaled_font_freeze_cache (font_subset->scaled_font);
for (i = 0; i < font_subset->num_glyphs; i++) {
status = _cairo_svg_document_emit_glyph (document,
font_subset->scaled_font,
font_subset->glyphs[i],
font_subset->font_id, i);
if (unlikely (status))
break;
}
_cairo_scaled_font_thaw_cache (font_subset->scaled_font);
return status;
}
static cairo_status_t
_cairo_svg_document_emit_font_subsets (cairo_svg_document_t *document)
{
cairo_status_t status;
status = _cairo_scaled_font_subsets_foreach_scaled (document->font_subsets,
_cairo_svg_document_emit_font_subset,
document);
if (unlikely (status))
goto FAIL;
status = _cairo_scaled_font_subsets_foreach_user (document->font_subsets,
_cairo_svg_document_emit_font_subset,
document);
FAIL:
_cairo_scaled_font_subsets_destroy (document->font_subsets);
document->font_subsets = NULL;
return status;
}
static char const *
_cairo_svg_surface_operators[] = {
"clear",
"src", "src-over", "src-in",
"src-out", "src-atop",
"dst", "dst-over", "dst-in",
"dst-out", "dst-atop",
"xor", "plus",
"color-dodge", /* FIXME: saturate ? */
"multiply", "screen", "overlay",
"darken", "lighten",
"color-dodge", "color-burn",
"hard-light", "soft-light",
"difference", "exclusion"
};
static cairo_bool_t
_cairo_svg_surface_analyze_operator (cairo_svg_surface_t *surface,
cairo_operator_t op)
{
/* guard against newly added operators */
if (op >= ARRAY_LENGTH (_cairo_svg_surface_operators))
return CAIRO_INT_STATUS_UNSUPPORTED;
/* allow operators being NULL if they are unsupported */
if (_cairo_svg_surface_operators[op] == NULL)
return CAIRO_INT_STATUS_UNSUPPORTED;
return CAIRO_STATUS_SUCCESS;
}
static cairo_int_status_t
_cairo_svg_surface_analyze_operation (cairo_svg_surface_t *surface,
cairo_operator_t op,
const cairo_pattern_t *pattern)
{
cairo_svg_document_t *document = surface->document;
if (surface->force_fallbacks &&
surface->paginated_mode == CAIRO_PAGINATED_MODE_ANALYZE)
{
return CAIRO_INT_STATUS_UNSUPPORTED;
}
if (pattern->type == CAIRO_PATTERN_TYPE_MESH)
return CAIRO_INT_STATUS_UNSUPPORTED;
/* SVG doesn't support extend reflect for image pattern */
if (pattern->type == CAIRO_PATTERN_TYPE_SURFACE &&
pattern->extend == CAIRO_EXTEND_REFLECT)
return CAIRO_INT_STATUS_UNSUPPORTED;
if (document->svg_version >= CAIRO_SVG_VERSION_1_2)
return _cairo_svg_surface_analyze_operator (surface, op);
if (op == CAIRO_OPERATOR_OVER)
return CAIRO_STATUS_SUCCESS;
/* The SOURCE operator is only supported if there is nothing
* painted underneath. */
if (op == CAIRO_OPERATOR_SOURCE)
return CAIRO_INT_STATUS_FLATTEN_TRANSPARENCY;
return CAIRO_INT_STATUS_UNSUPPORTED;
}
static cairo_int_status_t
_cairo_svg_surface_operation_supported (cairo_svg_surface_t *surface,
cairo_operator_t op,
const cairo_pattern_t *pattern)
{
return _cairo_svg_surface_analyze_operation (surface, op, pattern) != CAIRO_INT_STATUS_UNSUPPORTED;
}
static cairo_status_t
_cairo_svg_surface_finish (void *abstract_surface)
{
cairo_status_t status, status2;
cairo_svg_surface_t *surface = abstract_surface;
cairo_svg_document_t *document = surface->document;
cairo_svg_page_t *page;
unsigned int i;
if (_cairo_paginated_surface_get_target (document->owner) == &surface->base)
status = _cairo_svg_document_finish (document);
else
status = CAIRO_STATUS_SUCCESS;
if (surface->xml_node != NULL) {
status2 = _cairo_output_stream_destroy (surface->xml_node);
if (status == CAIRO_STATUS_SUCCESS)