@kittl/pdfkit 0.17.4 → 0.17.51

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/js/writeSvg.js ADDED
@@ -0,0 +1,3686 @@
1
+ 'use strict';
2
+
3
+ function write_svg (doc, svg, x, y, options) {
4
+
5
+ const NamedColors = {
6
+ aliceblue: [240, 248, 255],
7
+ antiquewhite: [250, 235, 215],
8
+ aqua: [0, 255, 255],
9
+ aquamarine: [127, 255, 212],
10
+ azure: [240, 255, 255],
11
+ beige: [245, 245, 220],
12
+ bisque: [255, 228, 196],
13
+ black: [0, 0, 0],
14
+ blanchedalmond: [255, 235, 205],
15
+ blue: [0, 0, 255],
16
+ blueviolet: [138, 43, 226],
17
+ brown: [165, 42, 42],
18
+ burlywood: [222, 184, 135],
19
+ cadetblue: [95, 158, 160],
20
+ chartreuse: [127, 255, 0],
21
+ chocolate: [210, 105, 30],
22
+ coral: [255, 127, 80],
23
+ cornflowerblue: [100, 149, 237],
24
+ cornsilk: [255, 248, 220],
25
+ crimson: [220, 20, 60],
26
+ cyan: [0, 255, 255],
27
+ darkblue: [0, 0, 139],
28
+ darkcyan: [0, 139, 139],
29
+ darkgoldenrod: [184, 134, 11],
30
+ darkgray: [169, 169, 169],
31
+ darkgrey: [169, 169, 169],
32
+ darkgreen: [0, 100, 0],
33
+ darkkhaki: [189, 183, 107],
34
+ darkmagenta: [139, 0, 139],
35
+ darkolivegreen: [85, 107, 47],
36
+ darkorange: [255, 140, 0],
37
+ darkorchid: [153, 50, 204],
38
+ darkred: [139, 0, 0],
39
+ darksalmon: [233, 150, 122],
40
+ darkseagreen: [143, 188, 143],
41
+ darkslateblue: [72, 61, 139],
42
+ darkslategray: [47, 79, 79],
43
+ darkslategrey: [47, 79, 79],
44
+ darkturquoise: [0, 206, 209],
45
+ darkviolet: [148, 0, 211],
46
+ deeppink: [255, 20, 147],
47
+ deepskyblue: [0, 191, 255],
48
+ dimgray: [105, 105, 105],
49
+ dimgrey: [105, 105, 105],
50
+ dodgerblue: [30, 144, 255],
51
+ firebrick: [178, 34, 34],
52
+ floralwhite: [255, 250, 240],
53
+ forestgreen: [34, 139, 34],
54
+ fuchsia: [255, 0, 255],
55
+ gainsboro: [220, 220, 220],
56
+ ghostwhite: [248, 248, 255],
57
+ gold: [255, 215, 0],
58
+ goldenrod: [218, 165, 32],
59
+ gray: [128, 128, 128],
60
+ grey: [128, 128, 128],
61
+ green: [0, 128, 0],
62
+ greenyellow: [173, 255, 47],
63
+ honeydew: [240, 255, 240],
64
+ hotpink: [255, 105, 180],
65
+ indianred: [205, 92, 92],
66
+ indigo: [75, 0, 130],
67
+ ivory: [255, 255, 240],
68
+ khaki: [240, 230, 140],
69
+ lavender: [230, 230, 250],
70
+ lavenderblush: [255, 240, 245],
71
+ lawngreen: [124, 252, 0],
72
+ lemonchiffon: [255, 250, 205],
73
+ lightblue: [173, 216, 230],
74
+ lightcoral: [240, 128, 128],
75
+ lightcyan: [224, 255, 255],
76
+ lightgoldenrodyellow: [250, 250, 210],
77
+ lightgray: [211, 211, 211],
78
+ lightgrey: [211, 211, 211],
79
+ lightgreen: [144, 238, 144],
80
+ lightpink: [255, 182, 193],
81
+ lightsalmon: [255, 160, 122],
82
+ lightseagreen: [32, 178, 170],
83
+ lightskyblue: [135, 206, 250],
84
+ lightslategray: [119, 136, 153],
85
+ lightslategrey: [119, 136, 153],
86
+ lightsteelblue: [176, 196, 222],
87
+ lightyellow: [255, 255, 224],
88
+ lime: [0, 255, 0],
89
+ limegreen: [50, 205, 50],
90
+ linen: [250, 240, 230],
91
+ magenta: [255, 0, 255],
92
+ maroon: [128, 0, 0],
93
+ mediumaquamarine: [102, 205, 170],
94
+ mediumblue: [0, 0, 205],
95
+ mediumorchid: [186, 85, 211],
96
+ mediumpurple: [147, 112, 219],
97
+ mediumseagreen: [60, 179, 113],
98
+ mediumslateblue: [123, 104, 238],
99
+ mediumspringgreen: [0, 250, 154],
100
+ mediumturquoise: [72, 209, 204],
101
+ mediumvioletred: [199, 21, 133],
102
+ midnightblue: [25, 25, 112],
103
+ mintcream: [245, 255, 250],
104
+ mistyrose: [255, 228, 225],
105
+ moccasin: [255, 228, 181],
106
+ navajowhite: [255, 222, 173],
107
+ navy: [0, 0, 128],
108
+ oldlace: [253, 245, 230],
109
+ olive: [128, 128, 0],
110
+ olivedrab: [107, 142, 35],
111
+ orange: [255, 165, 0],
112
+ orangered: [255, 69, 0],
113
+ orchid: [218, 112, 214],
114
+ palegoldenrod: [238, 232, 170],
115
+ palegreen: [152, 251, 152],
116
+ paleturquoise: [175, 238, 238],
117
+ palevioletred: [219, 112, 147],
118
+ papayawhip: [255, 239, 213],
119
+ peachpuff: [255, 218, 185],
120
+ peru: [205, 133, 63],
121
+ pink: [255, 192, 203],
122
+ plum: [221, 160, 221],
123
+ powderblue: [176, 224, 230],
124
+ purple: [128, 0, 128],
125
+ rebeccapurple: [102, 51, 153],
126
+ red: [255, 0, 0],
127
+ rosybrown: [188, 143, 143],
128
+ royalblue: [65, 105, 225],
129
+ saddlebrown: [139, 69, 19],
130
+ salmon: [250, 128, 114],
131
+ sandybrown: [244, 164, 96],
132
+ seagreen: [46, 139, 87],
133
+ seashell: [255, 245, 238],
134
+ sienna: [160, 82, 45],
135
+ silver: [192, 192, 192],
136
+ skyblue: [135, 206, 235],
137
+ slateblue: [106, 90, 205],
138
+ slategray: [112, 128, 144],
139
+ slategrey: [112, 128, 144],
140
+ snow: [255, 250, 250],
141
+ springgreen: [0, 255, 127],
142
+ steelblue: [70, 130, 180],
143
+ tan: [210, 180, 140],
144
+ teal: [0, 128, 128],
145
+ thistle: [216, 191, 216],
146
+ tomato: [255, 99, 71],
147
+ turquoise: [64, 224, 208],
148
+ violet: [238, 130, 238],
149
+ wheat: [245, 222, 179],
150
+ white: [255, 255, 255],
151
+ whitesmoke: [245, 245, 245],
152
+ yellow: [255, 255, 0]
153
+ };
154
+ const DefaultColors = {
155
+ black: [NamedColors.black, 1],
156
+ white: [NamedColors.white, 1],
157
+ transparent: [NamedColors.black, 0]
158
+ };
159
+ const Entities = {
160
+ quot: 34,
161
+ amp: 38,
162
+ lt: 60,
163
+ gt: 62,
164
+ apos: 39,
165
+ OElig: 338,
166
+ oelig: 339,
167
+ Scaron: 352,
168
+ scaron: 353,
169
+ Yuml: 376,
170
+ circ: 710,
171
+ tilde: 732,
172
+ ensp: 8194,
173
+ emsp: 8195,
174
+ thinsp: 8201,
175
+ zwnj: 8204,
176
+ zwj: 8205,
177
+ lrm: 8206,
178
+ rlm: 8207,
179
+ ndash: 8211,
180
+ mdash: 8212,
181
+ lsquo: 8216,
182
+ rsquo: 8217,
183
+ sbquo: 8218,
184
+ ldquo: 8220,
185
+ rdquo: 8221,
186
+ bdquo: 8222,
187
+ dagger: 8224,
188
+ Dagger: 8225,
189
+ permil: 8240,
190
+ lsaquo: 8249,
191
+ rsaquo: 8250,
192
+ euro: 8364,
193
+ nbsp: 160,
194
+ iexcl: 161,
195
+ cent: 162,
196
+ pound: 163,
197
+ curren: 164,
198
+ yen: 165,
199
+ brvbar: 166,
200
+ sect: 167,
201
+ uml: 168,
202
+ copy: 169,
203
+ ordf: 170,
204
+ laquo: 171,
205
+ not: 172,
206
+ shy: 173,
207
+ reg: 174,
208
+ macr: 175,
209
+ deg: 176,
210
+ plusmn: 177,
211
+ sup2: 178,
212
+ sup3: 179,
213
+ acute: 180,
214
+ micro: 181,
215
+ para: 182,
216
+ middot: 183,
217
+ cedil: 184,
218
+ sup1: 185,
219
+ ordm: 186,
220
+ raquo: 187,
221
+ frac14: 188,
222
+ frac12: 189,
223
+ frac34: 190,
224
+ iquest: 191,
225
+ Agrave: 192,
226
+ Aacute: 193,
227
+ Acirc: 194,
228
+ Atilde: 195,
229
+ Auml: 196,
230
+ Aring: 197,
231
+ AElig: 198,
232
+ Ccedil: 199,
233
+ Egrave: 200,
234
+ Eacute: 201,
235
+ Ecirc: 202,
236
+ Euml: 203,
237
+ Igrave: 204,
238
+ Iacute: 205,
239
+ Icirc: 206,
240
+ Iuml: 207,
241
+ ETH: 208,
242
+ Ntilde: 209,
243
+ Ograve: 210,
244
+ Oacute: 211,
245
+ Ocirc: 212,
246
+ Otilde: 213,
247
+ Ouml: 214,
248
+ times: 215,
249
+ Oslash: 216,
250
+ Ugrave: 217,
251
+ Uacute: 218,
252
+ Ucirc: 219,
253
+ Uuml: 220,
254
+ Yacute: 221,
255
+ THORN: 222,
256
+ szlig: 223,
257
+ agrave: 224,
258
+ aacute: 225,
259
+ acirc: 226,
260
+ atilde: 227,
261
+ auml: 228,
262
+ aring: 229,
263
+ aelig: 230,
264
+ ccedil: 231,
265
+ egrave: 232,
266
+ eacute: 233,
267
+ ecirc: 234,
268
+ euml: 235,
269
+ igrave: 236,
270
+ iacute: 237,
271
+ icirc: 238,
272
+ iuml: 239,
273
+ eth: 240,
274
+ ntilde: 241,
275
+ ograve: 242,
276
+ oacute: 243,
277
+ ocirc: 244,
278
+ otilde: 245,
279
+ ouml: 246,
280
+ divide: 247,
281
+ oslash: 248,
282
+ ugrave: 249,
283
+ uacute: 250,
284
+ ucirc: 251,
285
+ uuml: 252,
286
+ yacute: 253,
287
+ thorn: 254,
288
+ yuml: 255,
289
+ fnof: 402,
290
+ Alpha: 913,
291
+ Beta: 914,
292
+ Gamma: 915,
293
+ Delta: 916,
294
+ Epsilon: 917,
295
+ Zeta: 918,
296
+ Eta: 919,
297
+ Theta: 920,
298
+ Iota: 921,
299
+ Kappa: 922,
300
+ Lambda: 923,
301
+ Mu: 924,
302
+ Nu: 925,
303
+ Xi: 926,
304
+ Omicron: 927,
305
+ Pi: 928,
306
+ Rho: 929,
307
+ Sigma: 931,
308
+ Tau: 932,
309
+ Upsilon: 933,
310
+ Phi: 934,
311
+ Chi: 935,
312
+ Psi: 936,
313
+ Omega: 937,
314
+ alpha: 945,
315
+ beta: 946,
316
+ gamma: 947,
317
+ delta: 948,
318
+ epsilon: 949,
319
+ zeta: 950,
320
+ eta: 951,
321
+ theta: 952,
322
+ iota: 953,
323
+ kappa: 954,
324
+ lambda: 955,
325
+ mu: 956,
326
+ nu: 957,
327
+ xi: 958,
328
+ omicron: 959,
329
+ pi: 960,
330
+ rho: 961,
331
+ sigmaf: 962,
332
+ sigma: 963,
333
+ tau: 964,
334
+ upsilon: 965,
335
+ phi: 966,
336
+ chi: 967,
337
+ psi: 968,
338
+ omega: 969,
339
+ thetasym: 977,
340
+ upsih: 978,
341
+ piv: 982,
342
+ bull: 8226,
343
+ hellip: 8230,
344
+ prime: 8242,
345
+ Prime: 8243,
346
+ oline: 8254,
347
+ frasl: 8260,
348
+ weierp: 8472,
349
+ image: 8465,
350
+ real: 8476,
351
+ trade: 8482,
352
+ alefsym: 8501,
353
+ larr: 8592,
354
+ uarr: 8593,
355
+ rarr: 8594,
356
+ darr: 8595,
357
+ harr: 8596,
358
+ crarr: 8629,
359
+ lArr: 8656,
360
+ uArr: 8657,
361
+ rArr: 8658,
362
+ dArr: 8659,
363
+ hArr: 8660,
364
+ forall: 8704,
365
+ part: 8706,
366
+ exist: 8707,
367
+ empty: 8709,
368
+ nabla: 8711,
369
+ isin: 8712,
370
+ notin: 8713,
371
+ ni: 8715,
372
+ prod: 8719,
373
+ sum: 8721,
374
+ minus: 8722,
375
+ lowast: 8727,
376
+ radic: 8730,
377
+ prop: 8733,
378
+ infin: 8734,
379
+ ang: 8736,
380
+ and: 8743,
381
+ or: 8744,
382
+ cap: 8745,
383
+ cup: 8746,
384
+ int: 8747,
385
+ there4: 8756,
386
+ sim: 8764,
387
+ cong: 8773,
388
+ asymp: 8776,
389
+ ne: 8800,
390
+ equiv: 8801,
391
+ le: 8804,
392
+ ge: 8805,
393
+ sub: 8834,
394
+ sup: 8835,
395
+ nsub: 8836,
396
+ sube: 8838,
397
+ supe: 8839,
398
+ oplus: 8853,
399
+ otimes: 8855,
400
+ perp: 8869,
401
+ sdot: 8901,
402
+ lceil: 8968,
403
+ rceil: 8969,
404
+ lfloor: 8970,
405
+ rfloor: 8971,
406
+ lang: 9001,
407
+ rang: 9002,
408
+ loz: 9674,
409
+ spades: 9824,
410
+ clubs: 9827,
411
+ hearts: 9829,
412
+ diams: 9830
413
+ };
414
+ const PathArguments = {
415
+ A: 7,
416
+ a: 7,
417
+ C: 6,
418
+ c: 6,
419
+ H: 1,
420
+ h: 1,
421
+ L: 2,
422
+ l: 2,
423
+ M: 2,
424
+ m: 2,
425
+ Q: 4,
426
+ q: 4,
427
+ S: 4,
428
+ s: 4,
429
+ T: 2,
430
+ t: 2,
431
+ V: 1,
432
+ v: 1,
433
+ Z: 0,
434
+ z: 0
435
+ };
436
+ const PathFlags = {
437
+ A3: true,
438
+ A4: true,
439
+ a3: true,
440
+ a4: true
441
+ };
442
+ const Properties = {
443
+ color: {
444
+ inherit: true,
445
+ initial: undefined
446
+ },
447
+ visibility: {
448
+ inherit: true,
449
+ initial: 'visible',
450
+ values: {
451
+ hidden: 'hidden',
452
+ collapse: 'hidden',
453
+ visible: 'visible'
454
+ }
455
+ },
456
+ fill: {
457
+ inherit: true,
458
+ initial: DefaultColors.black
459
+ },
460
+ stroke: {
461
+ inherit: true,
462
+ initial: 'none'
463
+ },
464
+ 'stop-color': {
465
+ inherit: false,
466
+ initial: DefaultColors.black
467
+ },
468
+ 'fill-opacity': {
469
+ inherit: true,
470
+ initial: 1
471
+ },
472
+ 'stroke-opacity': {
473
+ inherit: true,
474
+ initial: 1
475
+ },
476
+ 'stop-opacity': {
477
+ inherit: false,
478
+ initial: 1
479
+ },
480
+ 'fill-rule': {
481
+ inherit: true,
482
+ initial: 'nonzero',
483
+ values: {
484
+ nonzero: 'nonzero',
485
+ evenodd: 'evenodd'
486
+ }
487
+ },
488
+ 'clip-rule': {
489
+ inherit: true,
490
+ initial: 'nonzero',
491
+ values: {
492
+ nonzero: 'nonzero',
493
+ evenodd: 'evenodd'
494
+ }
495
+ },
496
+ 'stroke-width': {
497
+ inherit: true,
498
+ initial: 1
499
+ },
500
+ 'stroke-dasharray': {
501
+ inherit: true,
502
+ initial: []
503
+ },
504
+ 'stroke-dashoffset': {
505
+ inherit: true,
506
+ initial: 0
507
+ },
508
+ 'stroke-miterlimit': {
509
+ inherit: true,
510
+ initial: 4
511
+ },
512
+ 'stroke-linejoin': {
513
+ inherit: true,
514
+ initial: 'miter',
515
+ values: {
516
+ miter: 'miter',
517
+ round: 'round',
518
+ bevel: 'bevel'
519
+ }
520
+ },
521
+ 'stroke-linecap': {
522
+ inherit: true,
523
+ initial: 'butt',
524
+ values: {
525
+ butt: 'butt',
526
+ round: 'round',
527
+ square: 'square'
528
+ }
529
+ },
530
+ 'font-size': {
531
+ inherit: true,
532
+ initial: 16,
533
+ values: {
534
+ 'xx-small': 9,
535
+ 'x-small': 10,
536
+ small: 13,
537
+ medium: 16,
538
+ large: 18,
539
+ 'x-large': 24,
540
+ 'xx-large': 32
541
+ }
542
+ },
543
+ 'font-family': {
544
+ inherit: true,
545
+ initial: 'sans-serif'
546
+ },
547
+ 'font-weight': {
548
+ inherit: true,
549
+ initial: 'normal',
550
+ values: {
551
+ 600: 'bold',
552
+ 700: 'bold',
553
+ 800: 'bold',
554
+ 900: 'bold',
555
+ bold: 'bold',
556
+ bolder: 'bold',
557
+ 500: 'normal',
558
+ 400: 'normal',
559
+ 300: 'normal',
560
+ 200: 'normal',
561
+ 100: 'normal',
562
+ normal: 'normal',
563
+ lighter: 'normal'
564
+ }
565
+ },
566
+ 'font-style': {
567
+ inherit: true,
568
+ initial: 'normal',
569
+ values: {
570
+ italic: 'italic',
571
+ oblique: 'italic',
572
+ normal: 'normal'
573
+ }
574
+ },
575
+ 'text-anchor': {
576
+ inherit: true,
577
+ initial: 'start',
578
+ values: {
579
+ start: 'start',
580
+ middle: 'middle',
581
+ end: 'end'
582
+ }
583
+ },
584
+ direction: {
585
+ inherit: true,
586
+ initial: 'ltr',
587
+ values: {
588
+ ltr: 'ltr',
589
+ rtl: 'rtl'
590
+ }
591
+ },
592
+ 'dominant-baseline': {
593
+ inherit: true,
594
+ initial: 'baseline',
595
+ values: {
596
+ auto: 'baseline',
597
+ baseline: 'baseline',
598
+ 'before-edge': 'before-edge',
599
+ 'text-before-edge': 'before-edge',
600
+ middle: 'middle',
601
+ central: 'central',
602
+ 'after-edge': 'after-edge',
603
+ 'text-after-edge': 'after-edge',
604
+ ideographic: 'ideographic',
605
+ alphabetic: 'alphabetic',
606
+ hanging: 'hanging',
607
+ mathematical: 'mathematical'
608
+ }
609
+ },
610
+ 'alignment-baseline': {
611
+ inherit: false,
612
+ initial: undefined,
613
+ values: {
614
+ auto: 'baseline',
615
+ baseline: 'baseline',
616
+ 'before-edge': 'before-edge',
617
+ 'text-before-edge': 'before-edge',
618
+ middle: 'middle',
619
+ central: 'central',
620
+ 'after-edge': 'after-edge',
621
+ 'text-after-edge': 'after-edge',
622
+ ideographic: 'ideographic',
623
+ alphabetic: 'alphabetic',
624
+ hanging: 'hanging',
625
+ mathematical: 'mathematical'
626
+ }
627
+ },
628
+ 'baseline-shift': {
629
+ inherit: true,
630
+ initial: 'baseline',
631
+ values: {
632
+ baseline: 'baseline',
633
+ sub: 'sub',
634
+ super: 'super'
635
+ }
636
+ },
637
+ 'word-spacing': {
638
+ inherit: true,
639
+ initial: 0,
640
+ values: {
641
+ normal: 0
642
+ }
643
+ },
644
+ 'letter-spacing': {
645
+ inherit: true,
646
+ initial: 0,
647
+ values: {
648
+ normal: 0
649
+ }
650
+ },
651
+ 'text-decoration': {
652
+ inherit: false,
653
+ initial: 'none',
654
+ values: {
655
+ none: 'none',
656
+ underline: 'underline',
657
+ overline: 'overline',
658
+ 'line-through': 'line-through'
659
+ }
660
+ },
661
+ 'xml:space': {
662
+ inherit: true,
663
+ initial: 'default',
664
+ css: 'white-space',
665
+ values: {
666
+ preserve: 'preserve',
667
+ default: 'default',
668
+ pre: 'preserve',
669
+ 'pre-line': 'preserve',
670
+ 'pre-wrap': 'preserve',
671
+ nowrap: 'default'
672
+ }
673
+ },
674
+ 'marker-start': {
675
+ inherit: true,
676
+ initial: 'none'
677
+ },
678
+ 'marker-mid': {
679
+ inherit: true,
680
+ initial: 'none'
681
+ },
682
+ 'marker-end': {
683
+ inherit: true,
684
+ initial: 'none'
685
+ },
686
+ opacity: {
687
+ inherit: false,
688
+ initial: 1
689
+ },
690
+ transform: {
691
+ inherit: false,
692
+ initial: [1, 0, 0, 1, 0, 0]
693
+ },
694
+ display: {
695
+ inherit: false,
696
+ initial: 'inline',
697
+ values: {
698
+ none: 'none',
699
+ inline: 'inline',
700
+ block: 'inline'
701
+ }
702
+ },
703
+ 'clip-path': {
704
+ inherit: false,
705
+ initial: 'none'
706
+ },
707
+ mask: {
708
+ inherit: false,
709
+ initial: 'none'
710
+ },
711
+ overflow: {
712
+ inherit: false,
713
+ initial: 'hidden',
714
+ values: {
715
+ hidden: 'hidden',
716
+ scroll: 'hidden',
717
+ visible: 'visible'
718
+ }
719
+ }
720
+ };
721
+ function docBeginGroup(bbox) {
722
+ let group = new function PDFGroup() {}();
723
+ group.name = 'G' + (doc._groupCount = (doc._groupCount || 0) + 1);
724
+ group.resources = doc.ref();
725
+ group.xobj = doc.ref({
726
+ Type: 'XObject',
727
+ Subtype: 'Form',
728
+ FormType: 1,
729
+ BBox: bbox,
730
+ Group: {
731
+ S: 'Transparency',
732
+ CS: colorSpace,
733
+ I: true,
734
+ K: false
735
+ },
736
+ Resources: group.resources
737
+ });
738
+ group.xobj.write('');
739
+ group.savedMatrix = doc._ctm;
740
+ group.savedPage = doc.page;
741
+ groupStack.push(group);
742
+ doc._ctm = [1, 0, 0, 1, 0, 0];
743
+ doc.page = {
744
+ width: doc.page.width,
745
+ height: doc.page.height,
746
+ write: function (data) {
747
+ group.xobj.write(data);
748
+ },
749
+ fonts: {},
750
+ xobjects: {},
751
+ ext_gstates: {},
752
+ patterns: {}
753
+ };
754
+ return group;
755
+ }
756
+ function docEndGroup(group) {
757
+ if (group !== groupStack.pop()) {
758
+ throw 'Group not matching';
759
+ }
760
+ if (Object.keys(doc.page.fonts).length) {
761
+ group.resources.data.Font = doc.page.fonts;
762
+ }
763
+ if (Object.keys(doc.page.xobjects).length) {
764
+ group.resources.data.XObject = doc.page.xobjects;
765
+ }
766
+ if (Object.keys(doc.page.ext_gstates).length) {
767
+ group.resources.data.ExtGState = doc.page.ext_gstates;
768
+ }
769
+ if (Object.keys(doc.page.patterns).length) {
770
+ group.resources.data.Pattern = doc.page.patterns;
771
+ }
772
+ group.resources.end();
773
+ group.xobj.end();
774
+ doc._ctm = group.savedMatrix;
775
+ doc.page = group.savedPage;
776
+ }
777
+ function docInsertGroup(group) {
778
+ doc.page.xobjects[group.name] = group.xobj;
779
+ doc.addContent('/' + group.name + ' Do');
780
+ }
781
+ function docApplyMask(group, clip) {
782
+ let name = 'M' + (doc._maskCount = (doc._maskCount || 0) + 1);
783
+ let gstate = doc.ref({
784
+ Type: 'ExtGState',
785
+ CA: 1,
786
+ ca: 1,
787
+ BM: 'Normal',
788
+ SMask: {
789
+ S: 'Luminosity',
790
+ G: group.xobj,
791
+ BC: clip ? [0, 0, 0] : [1, 1, 1]
792
+ }
793
+ });
794
+ gstate.end();
795
+ doc.page.ext_gstates[name] = gstate;
796
+ doc.addContent('/' + name + ' gs');
797
+ }
798
+ function applyBlendMode(group, blendMode) {
799
+ let name = 'M' + blendMode;
800
+ let gstate = doc.ref({
801
+ Type: 'ExtGState',
802
+ CA: 1,
803
+ ca: 1,
804
+ BM: blendMode,
805
+ G: group.xobj
806
+ });
807
+ gstate.end();
808
+ doc.page.ext_gstates[name] = gstate;
809
+ doc.addContent('/' + name + ' gs');
810
+ }
811
+ function docCreatePattern(group, dx, dy, matrix) {
812
+ let pattern = new function PDFPattern() {}();
813
+ pattern.group = group;
814
+ pattern.dx = dx;
815
+ pattern.dy = dy;
816
+ pattern.matrix = matrix || [1, 0, 0, 1, 0, 0];
817
+ return pattern;
818
+ }
819
+ function docUsePattern(pattern, stroke) {
820
+ let name = 'P' + (doc._patternCount = (doc._patternCount || 0) + 1);
821
+ let ref = doc.ref({
822
+ Type: 'Pattern',
823
+ PatternType: 1,
824
+ PaintType: 1,
825
+ TilingType: 2,
826
+ BBox: [0, 0, pattern.dx, pattern.dy],
827
+ XStep: pattern.dx,
828
+ YStep: pattern.dy,
829
+ Matrix: multiplyMatrix(doc._ctm, pattern.matrix),
830
+ Resources: {
831
+ ProcSet: ['PDF', 'Text', 'ImageB', 'ImageC', 'ImageI'],
832
+ XObject: function () {
833
+ let temp = {};
834
+ temp[pattern.group.name] = pattern.group.xobj;
835
+ return temp;
836
+ }()
837
+ }
838
+ });
839
+ ref.write('/' + pattern.group.name + ' Do');
840
+ ref.end();
841
+ doc.page.patterns[name] = ref;
842
+ if (stroke) {
843
+ doc.addContent('/Pattern CS');
844
+ doc.addContent('/' + name + ' SCN');
845
+ } else {
846
+ doc.addContent('/Pattern cs');
847
+ doc.addContent('/' + name + ' scn');
848
+ }
849
+ }
850
+ function docBeginText(font, size) {
851
+ if (!doc.page.fonts[font.id]) {
852
+ doc.page.fonts[font.id] = font.ref();
853
+ }
854
+ doc.addContent('BT').addContent('/' + font.id + ' ' + size + ' Tf');
855
+ }
856
+ function docSetTextMatrix(a, b, c, d, e, f) {
857
+ doc.addContent(validateNumber(a) + ' ' + validateNumber(b) + ' ' + validateNumber(-c) + ' ' + validateNumber(-d) + ' ' + validateNumber(e) + ' ' + validateNumber(f) + ' Tm');
858
+ }
859
+ function docSetTextMode(fill, stroke) {
860
+ let mode = fill && stroke ? 2 : stroke ? 1 : fill ? 0 : 3;
861
+ doc.addContent(mode + ' Tr');
862
+ }
863
+ function docWriteGlyphs(positions, font) {
864
+ let commands = [];
865
+ let commandStr = '';
866
+ const skew = font.fauxItalic ? -0.25 : 0;
867
+ function addChar(char) {
868
+ commandStr += char.glyph;
869
+ if (char.kern === 0) return;
870
+ commands.push(`<${commandStr}> ${validateNumber(char.kern)}`);
871
+ commandStr = '';
872
+ }
873
+ function flush() {
874
+ if (commandStr.length) {
875
+ commands.push(`<${commandStr}> 0`);
876
+ commandStr = '';
877
+ }
878
+ if (commands.length) {
879
+ doc.addContent(`[${commands.join(' ')}] TJ`);
880
+ commands = [];
881
+ }
882
+ }
883
+ for (let i = 0; i < positions.length; i++) {
884
+ const pos = positions[i];
885
+ if (pos.hidden || isEqual(pos.width, 0)) {
886
+ flush();
887
+ continue;
888
+ }
889
+ if (pos.continuous) {
890
+ addChar(pos);
891
+ continue;
892
+ }
893
+ flush();
894
+ const cos = Math.cos(pos.rotate);
895
+ const sin = Math.sin(pos.rotate);
896
+ docSetTextMatrix(cos * pos.scale, sin * pos.scale, cos * skew - sin, sin * skew + cos, pos.x, pos.y);
897
+ addChar(pos);
898
+ }
899
+ flush();
900
+ }
901
+ function docEndText() {
902
+ doc.addContent('ET');
903
+ }
904
+ function docFillColor(color) {
905
+ if (color[0].constructor.name === 'PDFPattern') {
906
+ doc.fillOpacity(color[1]);
907
+ docUsePattern(color[0], false);
908
+ } else {
909
+ doc.fillColor(color[0], color[1]);
910
+ }
911
+ }
912
+ function docStrokeColor(color) {
913
+ if (color[0].constructor.name === 'PDFPattern') {
914
+ doc.strokeOpacity(color[1]);
915
+ docUsePattern(color[0], true);
916
+ } else {
917
+ doc.strokeColor(color[0], color[1]);
918
+ }
919
+ }
920
+ function docInsertLink(x, y, w, h, url) {
921
+ let ref = doc.ref({
922
+ Type: 'Annot',
923
+ Subtype: 'Link',
924
+ Rect: [x, y, w, h],
925
+ Border: [0, 0, 0],
926
+ A: {
927
+ S: 'URI',
928
+ URI: new String(url)
929
+ }
930
+ });
931
+ ref.end();
932
+ links.push(ref);
933
+ }
934
+ function parseXml(xml) {
935
+ let SvgNode = function (tag, type, value, error) {
936
+ this.error = error;
937
+ this.nodeName = tag;
938
+ this.nodeValue = value;
939
+ this.nodeType = type;
940
+ this.attributes = Object.create(null);
941
+ this.childNodes = [];
942
+ this.parentNode = null;
943
+ this.id = '';
944
+ this.textContent = '';
945
+ this.classList = [];
946
+ };
947
+ SvgNode.prototype.getAttribute = function (attr) {
948
+ return this.attributes[attr] != null ? this.attributes[attr] : null;
949
+ };
950
+ SvgNode.prototype.getElementById = function (id) {
951
+ let result = null;
952
+ (function recursive(node) {
953
+ if (result) {
954
+ return;
955
+ }
956
+ if (node.nodeType === 1) {
957
+ if (node.id === id) {
958
+ result = node;
959
+ }
960
+ for (let i = 0; i < node.childNodes.length; i++) {
961
+ recursive(node.childNodes[i]);
962
+ }
963
+ }
964
+ })(this);
965
+ return result;
966
+ };
967
+ SvgNode.prototype.getElementsByTagName = function (tag) {
968
+ let result = [];
969
+ (function recursive(node) {
970
+ if (node.nodeType === 1) {
971
+ if (node.nodeName === tag) {
972
+ result.push(node);
973
+ }
974
+ for (let i = 0; i < node.childNodes.length; i++) {
975
+ recursive(node.childNodes[i]);
976
+ }
977
+ }
978
+ })(this);
979
+ return result;
980
+ };
981
+ let parser = new StringParser(xml.trim()),
982
+ result,
983
+ child,
984
+ error = false;
985
+ let recursive = function () {
986
+ let temp, child;
987
+ if (temp = parser.match(/^<([\w:.-]+)\s*/, true)) {
988
+ let node = new SvgNode(temp[1], 1, null, error);
989
+ while (temp = parser.match(/^([\w:.-]+)(?:\s*=\s*"([^"]*)"|\s*=\s*'([^']*)')?\s*/, true)) {
990
+ let attr = temp[1],
991
+ value = decodeEntities(temp[2] || temp[3] || '');
992
+ if (!node.attributes[attr]) {
993
+ node.attributes[attr] = value;
994
+ if (attr === 'id') {
995
+ node.id = value;
996
+ }
997
+ if (attr === 'class') {
998
+ node.classList = value.split(' ');
999
+ }
1000
+ } else {
1001
+ warningCallback('parseXml: duplicate attribute "' + attr + '"');
1002
+ error = true;
1003
+ }
1004
+ }
1005
+ if (parser.match(/^>/)) {
1006
+ while (child = recursive()) {
1007
+ node.childNodes.push(child);
1008
+ child.parentNode = node;
1009
+ node.textContent += child.nodeType === 3 || child.nodeType === 4 ? child.nodeValue : child.textContent;
1010
+ }
1011
+ if (temp = parser.match(/^<\/([\w:.-]+)\s*>/, true)) {
1012
+ if (temp[1] === node.nodeName) {
1013
+ return node;
1014
+ } else {
1015
+ warningCallback('parseXml: tag not matching, opening "' + node.nodeName + '" & closing "' + temp[1] + '"');
1016
+ error = true;
1017
+ return node;
1018
+ }
1019
+ } else {
1020
+ warningCallback('parseXml: tag not matching, opening "' + node.nodeName + '" & not closing');
1021
+ error = true;
1022
+ return node;
1023
+ }
1024
+ } else if (parser.match(/^\/>/)) {
1025
+ return node;
1026
+ } else {
1027
+ warningCallback('parseXml: tag could not be parsed "' + node.nodeName + '"');
1028
+ error = true;
1029
+ }
1030
+ } else if (temp = parser.match(/^<!--[\s\S]*?-->/)) {
1031
+ return new SvgNode(null, 8, temp, error);
1032
+ } else if (temp = parser.match(/^<\?[\s\S]*?\?>/)) {
1033
+ return new SvgNode(null, 7, temp, error);
1034
+ } else if (temp = parser.match(/^<!DOCTYPE\s*([\s\S]*?)>/)) {
1035
+ return new SvgNode(null, 10, temp, error);
1036
+ } else if (temp = parser.match(/^<!\[CDATA\[([\s\S]*?)\]\]>/, true)) {
1037
+ return new SvgNode('#cdata-section', 4, temp[1], error);
1038
+ } else if (temp = parser.match(/^([^<]+)/, true)) {
1039
+ return new SvgNode('#text', 3, decodeEntities(temp[1]), error);
1040
+ }
1041
+ };
1042
+ while (child = recursive()) {
1043
+ if (child.nodeType === 1 && !result) {
1044
+ result = child;
1045
+ } else if (child.nodeType === 1 || child.nodeType === 3 && child.nodeValue.trim() !== '') {
1046
+ warningCallback('parseXml: data after document end has been discarded');
1047
+ }
1048
+ }
1049
+ if (parser.matchAll()) {
1050
+ warningCallback('parseXml: parsing error');
1051
+ }
1052
+ return result;
1053
+ }
1054
+ function decodeEntities(str) {
1055
+ return str.replace(/&(?:#([0-9]+)|#[xX]([0-9A-Fa-f]+)|([0-9A-Za-z]+));/g, function (mt, m0, m1, m2) {
1056
+ if (m0) {
1057
+ return String.fromCharCode(parseInt(m0, 10));
1058
+ } else if (m1) {
1059
+ return String.fromCharCode(parseInt(m1, 16));
1060
+ } else if (m2 && Entities[m2]) {
1061
+ return String.fromCharCode(Entities[m2]);
1062
+ } else {
1063
+ return mt;
1064
+ }
1065
+ });
1066
+ }
1067
+ function parseColor(raw) {
1068
+ let temp, result;
1069
+ raw = (raw || '').trim();
1070
+ if (temp = NamedColors[raw]) {
1071
+ result = [temp.slice(), 1];
1072
+ } else if (temp = raw.match(/^cmyk\(\s*([0-9]+)\s*,\s*([0-9]+)\s*,\s*([0-9]+)\s*,\s*([0-9.]+)\s*\)$/i)) {
1073
+ temp[1] = parseInt(temp[1]);
1074
+ temp[2] = parseInt(temp[2]);
1075
+ temp[3] = parseInt(temp[3]);
1076
+ temp[4] = parseInt(temp[4]);
1077
+ if (temp[1] <= 100 && temp[2] <= 100 && temp[3] <= 100 && temp[4] <= 100) {
1078
+ result = [temp.slice(1, 5), 1];
1079
+ }
1080
+ } else if (temp = raw.match(/^rgba\(\s*([0-9]+)\s*,\s*([0-9]+)\s*,\s*([0-9]+)\s*,\s*([0-9.]+)\s*\)$/i)) {
1081
+ temp[1] = parseInt(temp[1]);
1082
+ temp[2] = parseInt(temp[2]);
1083
+ temp[3] = parseInt(temp[3]);
1084
+ temp[4] = parseFloat(temp[4]);
1085
+ if (temp[1] < 256 && temp[2] < 256 && temp[3] < 256 && temp[4] <= 1) {
1086
+ result = [temp.slice(1, 4), temp[4]];
1087
+ }
1088
+ } else if (temp = raw.match(/^rgb\(\s*([0-9]+)\s*,\s*([0-9]+)\s*,\s*([0-9]+)\s*\)$/i)) {
1089
+ temp[1] = parseInt(temp[1]);
1090
+ temp[2] = parseInt(temp[2]);
1091
+ temp[3] = parseInt(temp[3]);
1092
+ if (temp[1] < 256 && temp[2] < 256 && temp[3] < 256) {
1093
+ result = [temp.slice(1, 4), 1];
1094
+ }
1095
+ } else if (temp = raw.match(/^rgb\(\s*([0-9.]+)%\s*,\s*([0-9.]+)%\s*,\s*([0-9.]+)%\s*\)$/i)) {
1096
+ temp[1] = 2.55 * parseFloat(temp[1]);
1097
+ temp[2] = 2.55 * parseFloat(temp[2]);
1098
+ temp[3] = 2.55 * parseFloat(temp[3]);
1099
+ if (temp[1] < 256 && temp[2] < 256 && temp[3] < 256) {
1100
+ result = [temp.slice(1, 4), 1];
1101
+ }
1102
+ } else if (temp = raw.match(/^#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i)) {
1103
+ result = [[parseInt(temp[1], 16), parseInt(temp[2], 16), parseInt(temp[3], 16)], 1];
1104
+ } else if (temp = raw.match(/^#([0-9a-f])([0-9a-f])([0-9a-f])$/i)) {
1105
+ result = [[0x11 * parseInt(temp[1], 16), 0x11 * parseInt(temp[2], 16), 0x11 * parseInt(temp[3], 16)], 1];
1106
+ }
1107
+ return colorCallback ? colorCallback(result, raw) : result;
1108
+ }
1109
+ function opacityToColor(color, opacity, isMask) {
1110
+ let newColor = color[0].slice(),
1111
+ newOpacity = color[1] * opacity;
1112
+ if (isMask) {
1113
+ for (let i = 0; i < color.length; i++) {
1114
+ newColor[i] *= newOpacity;
1115
+ }
1116
+ return [newColor, 1];
1117
+ } else {
1118
+ return [newColor, newOpacity];
1119
+ }
1120
+ }
1121
+ function multiplyMatrix() {
1122
+ function multiply(a, b) {
1123
+ return [a[0] * b[0] + a[2] * b[1], a[1] * b[0] + a[3] * b[1], a[0] * b[2] + a[2] * b[3], a[1] * b[2] + a[3] * b[3], a[0] * b[4] + a[2] * b[5] + a[4], a[1] * b[4] + a[3] * b[5] + a[5]];
1124
+ }
1125
+ let result = arguments[0];
1126
+ for (let i = 1; i < arguments.length; i++) {
1127
+ result = multiply(result, arguments[i]);
1128
+ }
1129
+ return result;
1130
+ }
1131
+ function transformPoint(p, m) {
1132
+ return [m[0] * p[0] + m[2] * p[1] + m[4], m[1] * p[0] + m[3] * p[1] + m[5]];
1133
+ }
1134
+ function getGlobalMatrix() {
1135
+ let ctm = doc._ctm;
1136
+ for (let i = groupStack.length - 1; i >= 0; i--) {
1137
+ ctm = multiplyMatrix(groupStack[i].savedMatrix, ctm);
1138
+ }
1139
+ return ctm;
1140
+ }
1141
+ function getPageBBox() {
1142
+ return new SvgShape().M(0, 0).L(doc.page.width, 0).L(doc.page.width, doc.page.height).L(0, doc.page.height).transform(inverseMatrix(getGlobalMatrix())).getBoundingBox();
1143
+ }
1144
+ function inverseMatrix(m) {
1145
+ let dt = m[0] * m[3] - m[1] * m[2];
1146
+ return [m[3] / dt, -m[1] / dt, -m[2] / dt, m[0] / dt, (m[2] * m[5] - m[3] * m[4]) / dt, (m[1] * m[4] - m[0] * m[5]) / dt];
1147
+ }
1148
+ function validateMatrix(m) {
1149
+ let m0 = validateNumber(m[0]),
1150
+ m1 = validateNumber(m[1]),
1151
+ m2 = validateNumber(m[2]),
1152
+ m3 = validateNumber(m[3]),
1153
+ m4 = validateNumber(m[4]),
1154
+ m5 = validateNumber(m[5]);
1155
+ if (isNotEqual(m0 * m3 - m1 * m2, 0)) {
1156
+ return [m0, m1, m2, m3, m4, m5];
1157
+ }
1158
+ }
1159
+ function solveEquation(curve) {
1160
+ let a = curve[2] || 0,
1161
+ b = curve[1] || 0,
1162
+ c = curve[0] || 0;
1163
+ if (isEqual(a, 0) && isEqual(b, 0)) {
1164
+ return [];
1165
+ } else if (isEqual(a, 0)) {
1166
+ return [-c / b];
1167
+ } else {
1168
+ let d = b * b - 4 * a * c;
1169
+ if (isNotEqual(d, 0) && d > 0) {
1170
+ return [(-b + Math.sqrt(d)) / (2 * a), (-b - Math.sqrt(d)) / (2 * a)];
1171
+ } else if (isEqual(d, 0)) {
1172
+ return [-b / (2 * a)];
1173
+ } else {
1174
+ return [];
1175
+ }
1176
+ }
1177
+ }
1178
+ function getCurveValue(t, curve) {
1179
+ return (curve[0] || 0) + (curve[1] || 0) * t + (curve[2] || 0) * t * t + (curve[3] || 0) * t * t * t;
1180
+ }
1181
+ function isEqual(number, ref) {
1182
+ return Math.abs(number - ref) < 1e-10;
1183
+ }
1184
+ function isNotEqual(number, ref) {
1185
+ return Math.abs(number - ref) >= 1e-10;
1186
+ }
1187
+ function validateNumber(n) {
1188
+ return n > -1e21 && n < 1e21 ? Math.round(n * 1e6) / 1e6 : 0;
1189
+ }
1190
+ function isArrayLike(v) {
1191
+ return typeof v === 'object' && v !== null && typeof v.length === 'number';
1192
+ }
1193
+ function parseTranform(v) {
1194
+ let parser = new StringParser((v || '').trim()),
1195
+ result = [1, 0, 0, 1, 0, 0],
1196
+ temp;
1197
+ while (temp = parser.match(/^([A-Za-z]+)\s*[(]([^(]+)[)]/, true)) {
1198
+ let func = temp[1],
1199
+ nums = [],
1200
+ parser2 = new StringParser(temp[2].trim()),
1201
+ temp2;
1202
+ while (temp2 = parser2.matchNumber()) {
1203
+ nums.push(Number(temp2));
1204
+ parser2.matchSeparator();
1205
+ }
1206
+ if (func === 'matrix' && nums.length === 6) {
1207
+ result = multiplyMatrix(result, [nums[0], nums[1], nums[2], nums[3], nums[4], nums[5]]);
1208
+ } else if (func === 'translate' && nums.length === 2) {
1209
+ result = multiplyMatrix(result, [1, 0, 0, 1, nums[0], nums[1]]);
1210
+ } else if (func === 'translate' && nums.length === 1) {
1211
+ result = multiplyMatrix(result, [1, 0, 0, 1, nums[0], 0]);
1212
+ } else if (func === 'scale' && nums.length === 2) {
1213
+ result = multiplyMatrix(result, [nums[0], 0, 0, nums[1], 0, 0]);
1214
+ } else if (func === 'scale' && nums.length === 1) {
1215
+ result = multiplyMatrix(result, [nums[0], 0, 0, nums[0], 0, 0]);
1216
+ } else if (func === 'rotate' && nums.length === 3) {
1217
+ let a = nums[0] * Math.PI / 180;
1218
+ result = multiplyMatrix(result, [1, 0, 0, 1, nums[1], nums[2]], [Math.cos(a), Math.sin(a), -Math.sin(a), Math.cos(a), 0, 0], [1, 0, 0, 1, -nums[1], -nums[2]]);
1219
+ } else if (func === 'rotate' && nums.length === 1) {
1220
+ let a = nums[0] * Math.PI / 180;
1221
+ result = multiplyMatrix(result, [Math.cos(a), Math.sin(a), -Math.sin(a), Math.cos(a), 0, 0]);
1222
+ } else if (func === 'skewX' && nums.length === 1) {
1223
+ let a = nums[0] * Math.PI / 180;
1224
+ result = multiplyMatrix(result, [1, 0, Math.tan(a), 1, 0, 0]);
1225
+ } else if (func === 'skewY' && nums.length === 1) {
1226
+ let a = nums[0] * Math.PI / 180;
1227
+ result = multiplyMatrix(result, [1, Math.tan(a), 0, 1, 0, 0]);
1228
+ } else {
1229
+ return;
1230
+ }
1231
+ parser.matchSeparator();
1232
+ }
1233
+ if (parser.matchAll()) {
1234
+ return;
1235
+ }
1236
+ return result;
1237
+ }
1238
+ function parseAspectRatio(aspectRatio, availWidth, availHeight, elemWidth, elemHeight, initAlign) {
1239
+ let temp = (aspectRatio || '').trim().match(/^(none)$|^x(Min|Mid|Max)Y(Min|Mid|Max)(?:\s+(meet|slice))?$/) || [],
1240
+ ratioType = temp[1] || temp[4] || 'meet',
1241
+ xAlign = temp[2] || 'Mid',
1242
+ yAlign = temp[3] || 'Mid',
1243
+ scaleX = availWidth / elemWidth,
1244
+ scaleY = availHeight / elemHeight,
1245
+ dx = {
1246
+ Min: 0,
1247
+ Mid: 0.5,
1248
+ Max: 1
1249
+ }[xAlign] - (initAlign || 0),
1250
+ dy = {
1251
+ Min: 0,
1252
+ Mid: 0.5,
1253
+ Max: 1
1254
+ }[yAlign] - (initAlign || 0);
1255
+ if (ratioType === 'slice') {
1256
+ scaleY = scaleX = Math.max(scaleX, scaleY);
1257
+ } else if (ratioType === 'meet') {
1258
+ scaleY = scaleX = Math.min(scaleX, scaleY);
1259
+ }
1260
+ return [scaleX, 0, 0, scaleY, dx * (availWidth - elemWidth * scaleX), dy * (availHeight - elemHeight * scaleY)];
1261
+ }
1262
+ function parseStyleAttr(v) {
1263
+ let result = Object.create(null);
1264
+ v = (v || '').trim().split(/;/);
1265
+ for (let i = 0; i < v.length; i++) {
1266
+ let key = (v[i].split(':')[0] || '').trim(),
1267
+ value = (v[i].split(':')[1] || '').trim();
1268
+ if (key) {
1269
+ result[key] = value;
1270
+ }
1271
+ }
1272
+ if (result['marker']) {
1273
+ if (!result['marker-start']) {
1274
+ result['marker-start'] = result['marker'];
1275
+ }
1276
+ if (!result['marker-mid']) {
1277
+ result['marker-mid'] = result['marker'];
1278
+ }
1279
+ if (!result['marker-end']) {
1280
+ result['marker-end'] = result['marker'];
1281
+ }
1282
+ }
1283
+ if (result['font']) {
1284
+ let fontFamily = null,
1285
+ fontSize = null,
1286
+ fontStyle = 'normal',
1287
+ fontWeight = 'normal',
1288
+ fontVariant = 'normal';
1289
+ let parts = result['font'].split(/\s+/);
1290
+ for (let i = 0; i < parts.length; i++) {
1291
+ switch (parts[i]) {
1292
+ case 'normal':
1293
+ break;
1294
+ case 'italic':
1295
+ case 'oblique':
1296
+ fontStyle = parts[i];
1297
+ break;
1298
+ case 'small-caps':
1299
+ fontVariant = parts[i];
1300
+ break;
1301
+ case 'bold':
1302
+ case 'bolder':
1303
+ case 'lighter':
1304
+ case '100':
1305
+ case '200':
1306
+ case '300':
1307
+ case '400':
1308
+ case '500':
1309
+ case '600':
1310
+ case '700':
1311
+ case '800':
1312
+ case '900':
1313
+ fontWeight = parts[i];
1314
+ break;
1315
+ default:
1316
+ if (!fontSize) {
1317
+ fontSize = parts[i].split('/')[0];
1318
+ } else {
1319
+ if (!fontFamily) {
1320
+ fontFamily = parts[i];
1321
+ } else {
1322
+ fontFamily += ' ' + parts[i];
1323
+ }
1324
+ }
1325
+ break;
1326
+ }
1327
+ }
1328
+ if (!result['font-style']) {
1329
+ result['font-style'] = fontStyle;
1330
+ }
1331
+ if (!result['font-variant']) {
1332
+ result['font-variant'] = fontVariant;
1333
+ }
1334
+ if (!result['font-weight']) {
1335
+ result['font-weight'] = fontWeight;
1336
+ }
1337
+ if (!result['font-size']) {
1338
+ result['font-size'] = fontSize;
1339
+ }
1340
+ if (!result['font-family']) {
1341
+ result['font-family'] = fontFamily;
1342
+ }
1343
+ }
1344
+ return result;
1345
+ }
1346
+ function parseSelector(v) {
1347
+ let parts = v.split(/(?=[.#])/g),
1348
+ ids = [],
1349
+ classes = [],
1350
+ tags = [],
1351
+ temp;
1352
+ for (let i = 0; i < parts.length; i++) {
1353
+ if (temp = parts[i].match(/^[#]([_A-Za-z0-9-]+)$/)) {
1354
+ ids.push(temp[1]);
1355
+ } else if (temp = parts[i].match(/^[.]([_A-Za-z0-9-]+)$/)) {
1356
+ classes.push(temp[1]);
1357
+ } else if (temp = parts[i].match(/^([_A-Za-z0-9-]+)$/)) {
1358
+ tags.push(temp[1]);
1359
+ } else if (parts[i] !== '*') {
1360
+ return;
1361
+ }
1362
+ }
1363
+ return {
1364
+ tags: tags,
1365
+ ids: ids,
1366
+ classes: classes,
1367
+ specificity: ids.length * 10000 + classes.length * 100 + tags.length
1368
+ };
1369
+ }
1370
+ function parseStyleSheet(v) {
1371
+ let parser = new StringParser(v.trim()),
1372
+ rules = [],
1373
+ rule;
1374
+ while (rule = parser.match(/^\s*([^\{\}]*?)\s*\{([^\{\}]*?)\}/, true)) {
1375
+ let selectors = rule[1].split(/\s*,\s*/g),
1376
+ css = parseStyleAttr(rule[2]);
1377
+ for (let i = 0; i < selectors.length; i++) {
1378
+ let selector = parseSelector(selectors[i]);
1379
+ if (selector) {
1380
+ rules.push({
1381
+ selector: selector,
1382
+ css: css
1383
+ });
1384
+ }
1385
+ }
1386
+ }
1387
+ return rules;
1388
+ }
1389
+ function matchesSelector(elem, selector) {
1390
+ if (elem.nodeType !== 1) {
1391
+ return false;
1392
+ }
1393
+ for (let i = 0; i < selector.tags.length; i++) {
1394
+ if (selector.tags[i] !== elem.nodeName) {
1395
+ return false;
1396
+ }
1397
+ }
1398
+ for (let i = 0; i < selector.ids.length; i++) {
1399
+ if (selector.ids[i] !== elem.id) {
1400
+ return false;
1401
+ }
1402
+ }
1403
+ for (let i = 0; i < selector.classes.length; i++) {
1404
+ if (elem.classList.indexOf(selector.classes[i]) === -1) {
1405
+ return false;
1406
+ }
1407
+ }
1408
+ return true;
1409
+ }
1410
+ function getStyle(elem) {
1411
+ let result = Object.create(null);
1412
+ let specificities = Object.create(null);
1413
+ for (let i = 0; i < styleRules.length; i++) {
1414
+ let rule = styleRules[i];
1415
+ if (matchesSelector(elem, rule.selector)) {
1416
+ for (let key in rule.css) {
1417
+ if (!(specificities[key] > rule.selector.specificity)) {
1418
+ result[key] = rule.css[key];
1419
+ specificities[key] = rule.selector.specificity;
1420
+ }
1421
+ }
1422
+ }
1423
+ }
1424
+ return result;
1425
+ }
1426
+ function combineArrays(array1, array2) {
1427
+ return array1.concat(array2.slice(array1.length));
1428
+ }
1429
+ function getAscent(font, size) {
1430
+ return Math.max(font.ascender, (font.bbox[3] || font.bbox.maxY) * (font.scale || 1)) * size / 1000;
1431
+ }
1432
+ function getDescent(font, size) {
1433
+ return Math.min(font.descender, (font.bbox[1] || font.bbox.minY) * (font.scale || 1)) * size / 1000;
1434
+ }
1435
+ function getXHeight(font, size) {
1436
+ return (font.xHeight || 0.5 * (font.ascender - font.descender)) * size / 1000;
1437
+ }
1438
+ function getBaseline(font, size, baseline, shift) {
1439
+ let dy1, dy2;
1440
+ switch (baseline) {
1441
+ case 'middle':
1442
+ dy1 = 0.5 * getXHeight(font, size);
1443
+ break;
1444
+ case 'central':
1445
+ dy1 = 0.5 * (getDescent(font, size) + getAscent(font, size));
1446
+ break;
1447
+ case 'after-edge':
1448
+ case 'text-after-edge':
1449
+ dy1 = getDescent(font, size);
1450
+ break;
1451
+ case 'alphabetic':
1452
+ case 'auto':
1453
+ case 'baseline':
1454
+ dy1 = 0;
1455
+ break;
1456
+ case 'mathematical':
1457
+ dy1 = 0.5 * getAscent(font, size);
1458
+ break;
1459
+ case 'hanging':
1460
+ dy1 = 0.8 * getAscent(font, size);
1461
+ break;
1462
+ case 'before-edge':
1463
+ case 'text-before-edge':
1464
+ dy1 = getAscent(font, size);
1465
+ break;
1466
+ default:
1467
+ dy1 = 0;
1468
+ break;
1469
+ }
1470
+ switch (shift) {
1471
+ case 'baseline':
1472
+ dy2 = 0;
1473
+ break;
1474
+ case 'super':
1475
+ dy2 = 0.6 * size;
1476
+ break;
1477
+ case 'sub':
1478
+ dy2 = -0.6 * size;
1479
+ break;
1480
+ default:
1481
+ dy2 = shift;
1482
+ break;
1483
+ }
1484
+ return dy1 - dy2;
1485
+ }
1486
+ function getTextPos(font, size, text) {
1487
+ let encoded = font.encode('' + text),
1488
+ hex = encoded[0],
1489
+ pos = encoded[1],
1490
+ data = [];
1491
+ for (let i = 0; i < hex.length; i++) {
1492
+ let unicode = font.unicode ? font.unicode[parseInt(hex[i], 16)] : [text.charCodeAt(i)];
1493
+ data.push({
1494
+ glyph: hex[i],
1495
+ unicode: unicode,
1496
+ kern: pos[i].advanceWidth - pos[i].xAdvance,
1497
+ width: pos[i].advanceWidth * size / 1000,
1498
+ xOffset: pos[i].xOffset * size / 1000,
1499
+ yOffset: pos[i].yOffset * size / 1000,
1500
+ xAdvance: pos[i].xAdvance * size / 1000,
1501
+ yAdvance: pos[i].yAdvance * size / 1000
1502
+ });
1503
+ }
1504
+ return data;
1505
+ }
1506
+ function createSVGElement(obj, inherits) {
1507
+ switch (obj.nodeName) {
1508
+ case 'use':
1509
+ return new SvgElemUse(obj, inherits);
1510
+ case 'symbol':
1511
+ return new SvgElemSymbol(obj, inherits);
1512
+ case 'g':
1513
+ return new SvgElemGroup(obj, inherits);
1514
+ case 'a':
1515
+ return new SvgElemLink(obj, inherits);
1516
+ case 'svg':
1517
+ return new SvgElemSvg(obj, inherits);
1518
+ case 'image':
1519
+ return new SVGElemImage(obj, inherits);
1520
+ case 'rect':
1521
+ return new SvgElemRect(obj, inherits);
1522
+ case 'circle':
1523
+ return new SvgElemCircle(obj, inherits);
1524
+ case 'ellipse':
1525
+ return new SvgElemEllipse(obj, inherits);
1526
+ case 'line':
1527
+ return new SvgElemLine(obj, inherits);
1528
+ case 'polyline':
1529
+ return new SvgElemPolyline(obj, inherits);
1530
+ case 'polygon':
1531
+ return new SvgElemPolygon(obj, inherits);
1532
+ case 'path':
1533
+ return new SvgElemPath(obj, inherits);
1534
+ case 'text':
1535
+ return new SvgElemText(obj, inherits);
1536
+ case 'tspan':
1537
+ return new SvgElemTspan(obj, inherits);
1538
+ case 'textPath':
1539
+ return new SvgElemTextPath(obj, inherits);
1540
+ case '#text':
1541
+ case '#cdata-section':
1542
+ return new SvgElemTextNode(obj);
1543
+ default:
1544
+ return new SvgElem(obj, inherits);
1545
+ }
1546
+ }
1547
+ function getObjectStyles(obj) {
1548
+ if (!obj.attributes.style) return {};
1549
+ const styles = obj.attributes.style.split(';');
1550
+ return styles.reduce((acc, style) => {
1551
+ const [key, value] = style.split(':');
1552
+ if (!key) {
1553
+ return acc;
1554
+ }
1555
+ acc[key.trim()] = value.trim();
1556
+ return acc;
1557
+ }, {});
1558
+ }
1559
+ function parseCSSBlendMode(blendMode) {
1560
+ return blendMode.split('-').map(string => string.charAt(0).toUpperCase() + string.slice(1)).join('');
1561
+ }
1562
+ var StringParser = function (str) {
1563
+ this.match = function (exp, all) {
1564
+ let temp = str.match(exp);
1565
+ if (!temp || temp.index !== 0) {
1566
+ return;
1567
+ }
1568
+ str = str.substring(temp[0].length);
1569
+ return all ? temp : temp[0];
1570
+ };
1571
+ this.matchSeparator = function () {
1572
+ return this.match(/^(?:\s*,\s*|\s*|)/);
1573
+ };
1574
+ this.matchSpace = function () {
1575
+ return this.match(/^(?:\s*)/);
1576
+ };
1577
+ this.matchLengthUnit = function () {
1578
+ return this.match(/^(?:px|pt|cm|mm|in|pc|em|ex|%|)/);
1579
+ };
1580
+ this.matchNumber = function () {
1581
+ return this.match(/^(?:[-+]?(?:[0-9]+[.][0-9]+|[0-9]+[.]|[.][0-9]+|[0-9]+)(?:[eE][-+]?[0-9]+)?)/);
1582
+ };
1583
+ this.matchAll = function () {
1584
+ return this.match(/^[\s\S]+/);
1585
+ };
1586
+ };
1587
+ var BezierSegment = function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y) {
1588
+ let divisions = 6 * precision;
1589
+ let equationX = [p1x, -3 * p1x + 3 * c1x, 3 * p1x - 6 * c1x + 3 * c2x, -p1x + 3 * c1x - 3 * c2x + p2x];
1590
+ let equationY = [p1y, -3 * p1y + 3 * c1y, 3 * p1y - 6 * c1y + 3 * c2y, -p1y + 3 * c1y - 3 * c2y + p2y];
1591
+ let derivativeX = [-3 * p1x + 3 * c1x, 6 * p1x - 12 * c1x + 6 * c2x, -3 * p1x + 9 * c1x - 9 * c2x + 3 * p2x];
1592
+ let derivativeY = [-3 * p1y + 3 * c1y, 6 * p1y - 12 * c1y + 6 * c2y, -3 * p1y + 9 * c1y - 9 * c2y + 3 * p2y];
1593
+ let lengthMap = [0];
1594
+ for (let i = 1; i <= divisions; i++) {
1595
+ let t = (i - 0.5) / divisions;
1596
+ let dx = getCurveValue(t, derivativeX) / divisions,
1597
+ dy = getCurveValue(t, derivativeY) / divisions,
1598
+ l = Math.sqrt(dx * dx + dy * dy);
1599
+ lengthMap[i] = lengthMap[i - 1] + l;
1600
+ }
1601
+ this.totalLength = lengthMap[divisions];
1602
+ this.startPoint = [p1x, p1y, isEqual(p1x, c1x) && isEqual(p1y, c1y) ? Math.atan2(c2y - c1y, c2x - c1x) : Math.atan2(c1y - p1y, c1x - p1x)];
1603
+ this.endPoint = [p2x, p2y, isEqual(c2x, p2x) && isEqual(c2y, p2y) ? Math.atan2(c2y - c1y, c2x - c1x) : Math.atan2(p2y - c2y, p2x - c2x)];
1604
+ this.getBoundingBox = function () {
1605
+ let temp;
1606
+ let minX = getCurveValue(0, equationX),
1607
+ minY = getCurveValue(0, equationY),
1608
+ maxX = getCurveValue(1, equationX),
1609
+ maxY = getCurveValue(1, equationY);
1610
+ if (minX > maxX) {
1611
+ temp = maxX;
1612
+ maxX = minX;
1613
+ minX = temp;
1614
+ }
1615
+ if (minY > maxY) {
1616
+ temp = maxY;
1617
+ maxY = minY;
1618
+ minY = temp;
1619
+ }
1620
+ let rootsX = solveEquation(derivativeX);
1621
+ for (let i = 0; i < rootsX.length; i++) {
1622
+ if (rootsX[i] >= 0 && rootsX[i] <= 1) {
1623
+ let x = getCurveValue(rootsX[i], equationX);
1624
+ if (x < minX) {
1625
+ minX = x;
1626
+ }
1627
+ if (x > maxX) {
1628
+ maxX = x;
1629
+ }
1630
+ }
1631
+ }
1632
+ let rootsY = solveEquation(derivativeY);
1633
+ for (let i = 0; i < rootsY.length; i++) {
1634
+ if (rootsY[i] >= 0 && rootsY[i] <= 1) {
1635
+ let y = getCurveValue(rootsY[i], equationY);
1636
+ if (y < minY) {
1637
+ minY = y;
1638
+ }
1639
+ if (y > maxY) {
1640
+ maxY = y;
1641
+ }
1642
+ }
1643
+ }
1644
+ return [minX, minY, maxX, maxY];
1645
+ };
1646
+ this.getPointAtLength = function (l) {
1647
+ if (isEqual(l, 0)) {
1648
+ return this.startPoint;
1649
+ }
1650
+ if (isEqual(l, this.totalLength)) {
1651
+ return this.endPoint;
1652
+ }
1653
+ if (l < 0 || l > this.totalLength) {
1654
+ return;
1655
+ }
1656
+ for (let i = 1; i <= divisions; i++) {
1657
+ let l1 = lengthMap[i - 1],
1658
+ l2 = lengthMap[i];
1659
+ if (l1 <= l && l <= l2) {
1660
+ let t = (i - (l2 - l) / (l2 - l1)) / divisions,
1661
+ x = getCurveValue(t, equationX),
1662
+ y = getCurveValue(t, equationY),
1663
+ dx = getCurveValue(t, derivativeX),
1664
+ dy = getCurveValue(t, derivativeY);
1665
+ return [x, y, Math.atan2(dy, dx)];
1666
+ }
1667
+ }
1668
+ };
1669
+ };
1670
+ var LineSegment = function (p1x, p1y, p2x, p2y) {
1671
+ this.totalLength = Math.sqrt((p2x - p1x) * (p2x - p1x) + (p2y - p1y) * (p2y - p1y));
1672
+ this.startPoint = [p1x, p1y, Math.atan2(p2y - p1y, p2x - p1x)];
1673
+ this.endPoint = [p2x, p2y, Math.atan2(p2y - p1y, p2x - p1x)];
1674
+ this.getBoundingBox = function () {
1675
+ return [Math.min(this.startPoint[0], this.endPoint[0]), Math.min(this.startPoint[1], this.endPoint[1]), Math.max(this.startPoint[0], this.endPoint[0]), Math.max(this.startPoint[1], this.endPoint[1])];
1676
+ };
1677
+ this.getPointAtLength = function (l) {
1678
+ if (l >= 0 && l <= this.totalLength) {
1679
+ let r = l / this.totalLength || 0,
1680
+ x = this.startPoint[0] + r * (this.endPoint[0] - this.startPoint[0]),
1681
+ y = this.startPoint[1] + r * (this.endPoint[1] - this.startPoint[1]);
1682
+ return [x, y, this.startPoint[2]];
1683
+ }
1684
+ };
1685
+ };
1686
+ var SvgShape = function () {
1687
+ this.pathCommands = [];
1688
+ this.pathSegments = [];
1689
+ this.startPoint = null;
1690
+ this.endPoint = null;
1691
+ this.totalLength = 0;
1692
+ let startX = 0,
1693
+ startY = 0,
1694
+ currX = 0,
1695
+ currY = 0,
1696
+ lastCom,
1697
+ lastCtrlX,
1698
+ lastCtrlY;
1699
+ this.move = function (x, y) {
1700
+ startX = currX = x;
1701
+ startY = currY = y;
1702
+ return null;
1703
+ };
1704
+ this.line = function (x, y) {
1705
+ let segment = new LineSegment(currX, currY, x, y);
1706
+ currX = x;
1707
+ currY = y;
1708
+ return segment;
1709
+ };
1710
+ this.curve = function (c1x, c1y, c2x, c2y, x, y) {
1711
+ let segment = new BezierSegment(currX, currY, c1x, c1y, c2x, c2y, x, y);
1712
+ currX = x;
1713
+ currY = y;
1714
+ return segment;
1715
+ };
1716
+ this.close = function () {
1717
+ let segment = new LineSegment(currX, currY, startX, startY);
1718
+ currX = startX;
1719
+ currY = startY;
1720
+ return segment;
1721
+ };
1722
+ this.addCommand = function (data) {
1723
+ this.pathCommands.push(data);
1724
+ let segment = this[data[0]].apply(this, data.slice(3));
1725
+ if (segment) {
1726
+ segment.hasStart = data[1];
1727
+ segment.hasEnd = data[2];
1728
+ this.startPoint = this.startPoint || segment.startPoint;
1729
+ this.endPoint = segment.endPoint;
1730
+ this.pathSegments.push(segment);
1731
+ this.totalLength += segment.totalLength;
1732
+ }
1733
+ };
1734
+ this.M = function (x, y) {
1735
+ this.addCommand(['move', true, true, x, y]);
1736
+ lastCom = 'M';
1737
+ return this;
1738
+ };
1739
+ this.m = function (x, y) {
1740
+ return this.M(currX + x, currY + y);
1741
+ };
1742
+ this.Z = this.z = function () {
1743
+ this.addCommand(['close', true, true]);
1744
+ lastCom = 'Z';
1745
+ return this;
1746
+ };
1747
+ this.L = function (x, y) {
1748
+ this.addCommand(['line', true, true, x, y]);
1749
+ lastCom = 'L';
1750
+ return this;
1751
+ };
1752
+ this.l = function (x, y) {
1753
+ return this.L(currX + x, currY + y);
1754
+ };
1755
+ this.H = function (x) {
1756
+ return this.L(x, currY);
1757
+ };
1758
+ this.h = function (x) {
1759
+ return this.L(currX + x, currY);
1760
+ };
1761
+ this.V = function (y) {
1762
+ return this.L(currX, y);
1763
+ };
1764
+ this.v = function (y) {
1765
+ return this.L(currX, currY + y);
1766
+ };
1767
+ this.C = function (c1x, c1y, c2x, c2y, x, y) {
1768
+ this.addCommand(['curve', true, true, c1x, c1y, c2x, c2y, x, y]);
1769
+ lastCom = 'C';
1770
+ lastCtrlX = c2x;
1771
+ lastCtrlY = c2y;
1772
+ return this;
1773
+ };
1774
+ this.c = function (c1x, c1y, c2x, c2y, x, y) {
1775
+ return this.C(currX + c1x, currY + c1y, currX + c2x, currY + c2y, currX + x, currY + y);
1776
+ };
1777
+ this.S = function (c1x, c1y, x, y) {
1778
+ return this.C(currX + (lastCom === 'C' ? currX - lastCtrlX : 0), currY + (lastCom === 'C' ? currY - lastCtrlY : 0), c1x, c1y, x, y);
1779
+ };
1780
+ this.s = function (c1x, c1y, x, y) {
1781
+ return this.C(currX + (lastCom === 'C' ? currX - lastCtrlX : 0), currY + (lastCom === 'C' ? currY - lastCtrlY : 0), currX + c1x, currY + c1y, currX + x, currY + y);
1782
+ };
1783
+ this.Q = function (cx, cy, x, y) {
1784
+ let c1x = currX + 2 / 3 * (cx - currX),
1785
+ c1y = currY + 2 / 3 * (cy - currY),
1786
+ c2x = x + 2 / 3 * (cx - x),
1787
+ c2y = y + 2 / 3 * (cy - y);
1788
+ this.addCommand(['curve', true, true, c1x, c1y, c2x, c2y, x, y]);
1789
+ lastCom = 'Q';
1790
+ lastCtrlX = cx;
1791
+ lastCtrlY = cy;
1792
+ return this;
1793
+ };
1794
+ this.q = function (c1x, c1y, x, y) {
1795
+ return this.Q(currX + c1x, currY + c1y, currX + x, currY + y);
1796
+ };
1797
+ this.T = function (x, y) {
1798
+ return this.Q(currX + (lastCom === 'Q' ? currX - lastCtrlX : 0), currY + (lastCom === 'Q' ? currY - lastCtrlY : 0), x, y);
1799
+ };
1800
+ this.t = function (x, y) {
1801
+ return this.Q(currX + (lastCom === 'Q' ? currX - lastCtrlX : 0), currY + (lastCom === 'Q' ? currY - lastCtrlY : 0), currX + x, currY + y);
1802
+ };
1803
+ this.A = function (rx, ry, fi, fa, fs, x, y) {
1804
+ if (isEqual(rx, 0) || isEqual(ry, 0)) {
1805
+ this.addCommand(['line', true, true, x, y]);
1806
+ } else {
1807
+ fi = fi * (Math.PI / 180);
1808
+ rx = Math.abs(rx);
1809
+ ry = Math.abs(ry);
1810
+ fa = 1 * !!fa;
1811
+ fs = 1 * !!fs;
1812
+ let x1 = Math.cos(fi) * (currX - x) / 2 + Math.sin(fi) * (currY - y) / 2,
1813
+ y1 = Math.cos(fi) * (currY - y) / 2 - Math.sin(fi) * (currX - x) / 2,
1814
+ lambda = x1 * x1 / (rx * rx) + y1 * y1 / (ry * ry);
1815
+ if (lambda > 1) {
1816
+ rx *= Math.sqrt(lambda);
1817
+ ry *= Math.sqrt(lambda);
1818
+ }
1819
+ let r = Math.sqrt(Math.max(0, rx * rx * ry * ry - rx * rx * y1 * y1 - ry * ry * x1 * x1) / (rx * rx * y1 * y1 + ry * ry * x1 * x1)),
1820
+ x2 = (fa === fs ? -1 : 1) * r * rx * y1 / ry,
1821
+ y2 = (fa === fs ? 1 : -1) * r * ry * x1 / rx;
1822
+ let cx = Math.cos(fi) * x2 - Math.sin(fi) * y2 + (currX + x) / 2,
1823
+ cy = Math.sin(fi) * x2 + Math.cos(fi) * y2 + (currY + y) / 2,
1824
+ th1 = Math.atan2((y1 - y2) / ry, (x1 - x2) / rx),
1825
+ th2 = Math.atan2((-y1 - y2) / ry, (-x1 - x2) / rx);
1826
+ if (fs === 0 && th2 - th1 > 0) {
1827
+ th2 -= 2 * Math.PI;
1828
+ } else if (fs === 1 && th2 - th1 < 0) {
1829
+ th2 += 2 * Math.PI;
1830
+ }
1831
+ let segms = Math.ceil(Math.abs(th2 - th1) / (Math.PI / precision));
1832
+ for (let i = 0; i < segms; i++) {
1833
+ let th3 = th1 + i * (th2 - th1) / segms,
1834
+ th4 = th1 + (i + 1) * (th2 - th1) / segms,
1835
+ t = 4 / 3 * Math.tan((th4 - th3) / 4);
1836
+ let c1x = cx + Math.cos(fi) * rx * (Math.cos(th3) - t * Math.sin(th3)) - Math.sin(fi) * ry * (Math.sin(th3) + t * Math.cos(th3)),
1837
+ c1y = cy + Math.sin(fi) * rx * (Math.cos(th3) - t * Math.sin(th3)) + Math.cos(fi) * ry * (Math.sin(th3) + t * Math.cos(th3)),
1838
+ c2x = cx + Math.cos(fi) * rx * (Math.cos(th4) + t * Math.sin(th4)) - Math.sin(fi) * ry * (Math.sin(th4) - t * Math.cos(th4)),
1839
+ c2y = cy + Math.sin(fi) * rx * (Math.cos(th4) + t * Math.sin(th4)) + Math.cos(fi) * ry * (Math.sin(th4) - t * Math.cos(th4)),
1840
+ endX = cx + Math.cos(fi) * rx * Math.cos(th4) - Math.sin(fi) * ry * Math.sin(th4),
1841
+ endY = cy + Math.sin(fi) * rx * Math.cos(th4) + Math.cos(fi) * ry * Math.sin(th4);
1842
+ this.addCommand(['curve', i === 0, i === segms - 1, c1x, c1y, c2x, c2y, endX, endY]);
1843
+ }
1844
+ }
1845
+ lastCom = 'A';
1846
+ return this;
1847
+ };
1848
+ this.a = function (rx, ry, fi, fa, fs, x, y) {
1849
+ return this.A(rx, ry, fi, fa, fs, currX + x, currY + y);
1850
+ };
1851
+ this.path = function (d) {
1852
+ let command,
1853
+ value,
1854
+ temp,
1855
+ parser = new StringParser((d || '').trim());
1856
+ while (command = parser.match(/^[astvzqmhlcASTVZQMHLC]/)) {
1857
+ parser.matchSeparator();
1858
+ let values = [];
1859
+ while (value = PathFlags[command + values.length] ? parser.match(/^[01]/) : parser.matchNumber()) {
1860
+ parser.matchSeparator();
1861
+ if (values.length === PathArguments[command]) {
1862
+ this[command].apply(this, values);
1863
+ values = [];
1864
+ if (command === 'M') {
1865
+ command = 'L';
1866
+ } else if (command === 'm') {
1867
+ command = 'l';
1868
+ }
1869
+ }
1870
+ values.push(Number(value));
1871
+ }
1872
+ if (values.length === PathArguments[command]) {
1873
+ this[command].apply(this, values);
1874
+ } else {
1875
+ warningCallback('SvgPath: command ' + command + ' with ' + values.length + ' numbers');
1876
+ return;
1877
+ }
1878
+ }
1879
+ if (temp = parser.matchAll()) {
1880
+ warningCallback('SvgPath: unexpected string ' + temp);
1881
+ }
1882
+ return this;
1883
+ };
1884
+ this.getBoundingBox = function () {
1885
+ let bbox = [Infinity, Infinity, -Infinity, -Infinity];
1886
+ function addBounds(bbox1) {
1887
+ if (bbox1[0] < bbox[0]) {
1888
+ bbox[0] = bbox1[0];
1889
+ }
1890
+ if (bbox1[2] > bbox[2]) {
1891
+ bbox[2] = bbox1[2];
1892
+ }
1893
+ if (bbox1[1] < bbox[1]) {
1894
+ bbox[1] = bbox1[1];
1895
+ }
1896
+ if (bbox1[3] > bbox[3]) {
1897
+ bbox[3] = bbox1[3];
1898
+ }
1899
+ }
1900
+ for (let i = 0; i < this.pathSegments.length; i++) {
1901
+ addBounds(this.pathSegments[i].getBoundingBox());
1902
+ }
1903
+ if (bbox[0] === Infinity) {
1904
+ bbox[0] = 0;
1905
+ }
1906
+ if (bbox[1] === Infinity) {
1907
+ bbox[1] = 0;
1908
+ }
1909
+ if (bbox[2] === -Infinity) {
1910
+ bbox[2] = 0;
1911
+ }
1912
+ if (bbox[3] === -Infinity) {
1913
+ bbox[3] = 0;
1914
+ }
1915
+ return bbox;
1916
+ };
1917
+ this.getPointAtLength = function (l) {
1918
+ if (l >= 0 && l <= this.totalLength) {
1919
+ let temp;
1920
+ for (let i = 0; i < this.pathSegments.length; i++) {
1921
+ if (temp = this.pathSegments[i].getPointAtLength(l)) {
1922
+ return temp;
1923
+ }
1924
+ l -= this.pathSegments[i].totalLength;
1925
+ }
1926
+ return this.endPoint;
1927
+ }
1928
+ };
1929
+ this.transform = function (m) {
1930
+ this.pathSegments = [];
1931
+ this.startPoint = null;
1932
+ this.endPoint = null;
1933
+ this.totalLength = 0;
1934
+ for (let i = 0; i < this.pathCommands.length; i++) {
1935
+ let data = this.pathCommands.shift();
1936
+ for (let j = 3; j < data.length; j += 2) {
1937
+ let p = transformPoint([data[j], data[j + 1]], m);
1938
+ data[j] = p[0];
1939
+ data[j + 1] = p[1];
1940
+ }
1941
+ this.addCommand(data);
1942
+ }
1943
+ return this;
1944
+ };
1945
+ this.mergeShape = function (shape) {
1946
+ for (let i = 0; i < shape.pathCommands.length; i++) {
1947
+ this.addCommand(shape.pathCommands[i].slice());
1948
+ }
1949
+ return this;
1950
+ };
1951
+ this.clone = function () {
1952
+ return new SvgShape().mergeShape(this);
1953
+ };
1954
+ this.insertInDocument = function () {
1955
+ for (let i = 0; i < this.pathCommands.length; i++) {
1956
+ let command = this.pathCommands[i][0],
1957
+ values = this.pathCommands[i].slice(3);
1958
+ switch (command) {
1959
+ case 'move':
1960
+ doc.moveTo(values[0], values[1]);
1961
+ break;
1962
+ case 'line':
1963
+ doc.lineTo(values[0], values[1]);
1964
+ break;
1965
+ case 'curve':
1966
+ doc.bezierCurveTo(values[0], values[1], values[2], values[3], values[4], values[5]);
1967
+ break;
1968
+ case 'close':
1969
+ doc.closePath();
1970
+ break;
1971
+ }
1972
+ }
1973
+ };
1974
+ this.getSubPaths = function () {
1975
+ let subPaths = [],
1976
+ shape = new SvgShape();
1977
+ for (let i = 0; i < this.pathCommands.length; i++) {
1978
+ let data = this.pathCommands[i],
1979
+ command = this.pathCommands[i][0];
1980
+ if (command === 'move' && i !== 0) {
1981
+ subPaths.push(shape);
1982
+ shape = new SvgShape();
1983
+ }
1984
+ shape.addCommand(data);
1985
+ }
1986
+ subPaths.push(shape);
1987
+ return subPaths;
1988
+ };
1989
+ this.getMarkers = function () {
1990
+ let markers = [],
1991
+ subPaths = this.getSubPaths();
1992
+ for (let i = 0; i < subPaths.length; i++) {
1993
+ let subPath = subPaths[i],
1994
+ subPathMarkers = [];
1995
+ for (let j = 0; j < subPath.pathSegments.length; j++) {
1996
+ let segment = subPath.pathSegments[j];
1997
+ if (isNotEqual(segment.totalLength, 0) || j === 0 || j === subPath.pathSegments.length - 1) {
1998
+ if (segment.hasStart) {
1999
+ let startMarker = segment.getPointAtLength(0),
2000
+ prevEndMarker = subPathMarkers.pop();
2001
+ if (prevEndMarker) {
2002
+ startMarker[2] = 0.5 * (prevEndMarker[2] + startMarker[2]);
2003
+ }
2004
+ subPathMarkers.push(startMarker);
2005
+ }
2006
+ if (segment.hasEnd) {
2007
+ let endMarker = segment.getPointAtLength(segment.totalLength);
2008
+ subPathMarkers.push(endMarker);
2009
+ }
2010
+ }
2011
+ }
2012
+ markers = markers.concat(subPathMarkers);
2013
+ }
2014
+ return markers;
2015
+ };
2016
+ };
2017
+ var SvgElem = function (obj, inherits) {
2018
+ let styleCache = Object.create(null);
2019
+ let childrenCache = null;
2020
+ this.name = obj.nodeName;
2021
+ this.isOuterElement = obj === svg || !obj.parentNode;
2022
+ this.inherits = inherits || (!this.isOuterElement ? createSVGElement(obj.parentNode, null) : null);
2023
+ this.stack = this.inherits ? this.inherits.stack.concat(obj) : [obj];
2024
+ this.style = parseStyleAttr(typeof obj.getAttribute === 'function' && obj.getAttribute('style'));
2025
+ this.css = useCSS ? getComputedStyle(obj) : getStyle(obj);
2026
+ this.allowedChildren = [];
2027
+ this.attr = function (key) {
2028
+ if (typeof obj.getAttribute === 'function') {
2029
+ return obj.getAttribute(key);
2030
+ }
2031
+ };
2032
+ this.resolveUrl = function (value) {
2033
+ let temp = (value || '').match(/^\s*(?:url\("(.*)#(.*)"\)|url\('(.*)#(.*)'\)|url\((.*)#(.*)\)|(.*)#(.*))\s*$/) || [];
2034
+ let file = temp[1] || temp[3] || temp[5] || temp[7],
2035
+ id = temp[2] || temp[4] || temp[6] || temp[8];
2036
+ if (id) {
2037
+ if (!file) {
2038
+ let svgObj = svg.getElementById(id);
2039
+ if (svgObj) {
2040
+ if (this.stack.indexOf(svgObj) === -1) {
2041
+ return svgObj;
2042
+ } else {
2043
+ warningCallback('SVGtoPDF: loop of circular references for id "' + id + '"');
2044
+ return;
2045
+ }
2046
+ }
2047
+ }
2048
+ if (documentCallback) {
2049
+ let svgs = documentCache[file];
2050
+ if (!svgs) {
2051
+ svgs = documentCallback(file);
2052
+ if (!isArrayLike(svgs)) {
2053
+ svgs = [svgs];
2054
+ }
2055
+ for (let i = 0; i < svgs.length; i++) {
2056
+ if (typeof svgs[i] === 'string') {
2057
+ svgs[i] = parseXml(svgs[i]);
2058
+ }
2059
+ }
2060
+ documentCache[file] = svgs;
2061
+ }
2062
+ for (let i = 0; i < svgs.length; i++) {
2063
+ let svgObj = svgs[i].getElementById(id);
2064
+ if (svgObj) {
2065
+ if (this.stack.indexOf(svgObj) === -1) {
2066
+ return svgObj;
2067
+ } else {
2068
+ warningCallback('SVGtoPDF: loop of circular references for id "' + file + '#' + id + '"');
2069
+ return;
2070
+ }
2071
+ }
2072
+ }
2073
+ }
2074
+ }
2075
+ };
2076
+ this.computeUnits = function (value, unit, percent, isFontSize) {
2077
+ if (unit === '%') {
2078
+ return parseFloat(value) / 100 * (isFontSize || percent != null ? percent : this.getViewport());
2079
+ } else if (unit === 'ex' || unit === 'em') {
2080
+ return value * {
2081
+ em: 1,
2082
+ ex: 0.5
2083
+ }[unit] * (isFontSize ? percent : this.get('font-size'));
2084
+ } else {
2085
+ return value * {
2086
+ '': 1,
2087
+ px: 1,
2088
+ pt: 96 / 72,
2089
+ cm: 96 / 2.54,
2090
+ mm: 96 / 25.4,
2091
+ in: 96,
2092
+ pc: 96 / 6
2093
+ }[unit];
2094
+ }
2095
+ };
2096
+ this.computeLength = function (value, percent, initial, isFontSize) {
2097
+ let parser = new StringParser((value || '').trim()),
2098
+ temp1,
2099
+ temp2;
2100
+ if (typeof (temp1 = parser.matchNumber()) === 'string' && typeof (temp2 = parser.matchLengthUnit()) === 'string' && !parser.matchAll()) {
2101
+ return this.computeUnits(temp1, temp2, percent, isFontSize);
2102
+ }
2103
+ return initial;
2104
+ };
2105
+ this.computeLengthList = function (value, percent, strict) {
2106
+ let parser = new StringParser((value || '').trim()),
2107
+ result = [],
2108
+ temp1,
2109
+ temp2;
2110
+ while (typeof (temp1 = parser.matchNumber()) === 'string' && typeof (temp2 = parser.matchLengthUnit()) === 'string') {
2111
+ result.push(this.computeUnits(temp1, temp2, percent));
2112
+ parser.matchSeparator();
2113
+ }
2114
+ if (strict && parser.matchAll()) {
2115
+ return;
2116
+ }
2117
+ return result;
2118
+ };
2119
+ this.getLength = function (key, percent, initial) {
2120
+ return this.computeLength(this.attr(key), percent, initial);
2121
+ };
2122
+ this.getLengthList = function (key, percent) {
2123
+ return this.computeLengthList(this.attr(key), percent);
2124
+ };
2125
+ this.getUrl = function (key) {
2126
+ return this.resolveUrl(this.attr(key));
2127
+ };
2128
+ this.getNumberList = function (key) {
2129
+ let parser = new StringParser((this.attr(key) || '').trim()),
2130
+ result = [],
2131
+ temp;
2132
+ while (temp = parser.matchNumber()) {
2133
+ result.push(Number(temp));
2134
+ parser.matchSeparator();
2135
+ }
2136
+ result.error = parser.matchAll();
2137
+ return result;
2138
+ };
2139
+ this.getViewbox = function (key, initial) {
2140
+ let viewBox = this.getNumberList(key);
2141
+ if (viewBox.length === 4 && viewBox[2] >= 0 && viewBox[3] >= 0) {
2142
+ return viewBox;
2143
+ }
2144
+ return initial;
2145
+ };
2146
+ this.getPercent = function (key, initial) {
2147
+ let value = this.attr(key);
2148
+ let parser = new StringParser((value || '').trim());
2149
+ let number = parser.matchNumber();
2150
+ if (!number) {
2151
+ return initial;
2152
+ }
2153
+ if (parser.match('%')) {
2154
+ number *= 0.01;
2155
+ }
2156
+ if (parser.matchAll()) {
2157
+ return initial;
2158
+ }
2159
+ return Math.max(0, Math.min(1, number));
2160
+ };
2161
+ this.chooseValue = function (args) {
2162
+ for (let i = 0; i < arguments.length; i++) {
2163
+ if (arguments[i] != null && arguments[i] === arguments[i]) {
2164
+ return arguments[i];
2165
+ }
2166
+ }
2167
+ return arguments[arguments.length - 1];
2168
+ };
2169
+ this.get = function (key) {
2170
+ if (styleCache[key] !== undefined) {
2171
+ return styleCache[key];
2172
+ }
2173
+ let keyInfo = Properties[key] || {},
2174
+ value,
2175
+ result;
2176
+ for (let i = 0; i < 3; i++) {
2177
+ switch (i) {
2178
+ case 0:
2179
+ if (key !== 'transform') {
2180
+ value = this.css[keyInfo.css || key];
2181
+ }
2182
+ break;
2183
+ case 1:
2184
+ value = this.style[key];
2185
+ break;
2186
+ case 2:
2187
+ value = this.attr(key);
2188
+ break;
2189
+ }
2190
+ if (value === 'inherit') {
2191
+ result = this.inherits ? this.inherits.get(key) : keyInfo.initial;
2192
+ if (result != null) {
2193
+ return styleCache[key] = result;
2194
+ }
2195
+ }
2196
+ if (keyInfo.values != null) {
2197
+ result = keyInfo.values[value];
2198
+ if (result != null) {
2199
+ return styleCache[key] = result;
2200
+ }
2201
+ }
2202
+ if (value != null) {
2203
+ let parsed;
2204
+ switch (key) {
2205
+ case 'font-size':
2206
+ result = this.computeLength(value, this.inherits ? this.inherits.get(key) : keyInfo.initial, undefined, true);
2207
+ break;
2208
+ case 'baseline-shift':
2209
+ result = this.computeLength(value, this.get('font-size'));
2210
+ break;
2211
+ case 'font-family':
2212
+ result = value || undefined;
2213
+ break;
2214
+ case 'opacity':
2215
+ case 'stroke-opacity':
2216
+ case 'fill-opacity':
2217
+ case 'stop-opacity':
2218
+ parsed = parseFloat(value);
2219
+ if (!isNaN(parsed)) {
2220
+ result = Math.max(0, Math.min(1, parsed));
2221
+ }
2222
+ break;
2223
+ case 'transform':
2224
+ result = parseTranform(value);
2225
+ break;
2226
+ case 'stroke-dasharray':
2227
+ if (value === 'none') {
2228
+ result = [];
2229
+ } else if (parsed = this.computeLengthList(value, this.getViewport(), true)) {
2230
+ let sum = 0,
2231
+ error = false;
2232
+ for (let j = 0; j < parsed.length; j++) {
2233
+ if (parsed[j] < 0) {
2234
+ error = true;
2235
+ }
2236
+ sum += parsed[j];
2237
+ }
2238
+ if (!error) {
2239
+ if (parsed.length % 2 === 1) {
2240
+ parsed = parsed.concat(parsed);
2241
+ }
2242
+ result = sum === 0 ? [] : parsed;
2243
+ }
2244
+ }
2245
+ break;
2246
+ case 'color':
2247
+ if (value === 'none' || value === 'transparent') {
2248
+ result = 'none';
2249
+ } else {
2250
+ result = parseColor(value);
2251
+ }
2252
+ break;
2253
+ case 'fill':
2254
+ case 'stroke':
2255
+ if (value === 'none' || value === 'transparent') {
2256
+ result = 'none';
2257
+ } else if (value === 'currentColor') {
2258
+ result = this.get('color');
2259
+ } else if (parsed = parseColor(value)) {
2260
+ return parsed;
2261
+ } else if (parsed = (value || '').split(' ')) {
2262
+ let object = this.resolveUrl(parsed[0]),
2263
+ fallbackColor = parseColor(parsed[1]);
2264
+ if (object == null) {
2265
+ result = fallbackColor;
2266
+ } else if (object.nodeName === 'linearGradient' || object.nodeName === 'radialGradient') {
2267
+ result = new SvgElemGradient(object, null, fallbackColor);
2268
+ } else if (object.nodeName === 'pattern') {
2269
+ result = new SvgElemPattern(object, null, fallbackColor);
2270
+ } else {
2271
+ result = fallbackColor;
2272
+ }
2273
+ }
2274
+ break;
2275
+ case 'stop-color':
2276
+ if (value === 'none' || value === 'transparent') {
2277
+ result = 'none';
2278
+ } else if (value === 'currentColor') {
2279
+ result = this.get('color');
2280
+ } else {
2281
+ result = parseColor(value);
2282
+ }
2283
+ break;
2284
+ case 'marker-start':
2285
+ case 'marker-mid':
2286
+ case 'marker-end':
2287
+ case 'clip-path':
2288
+ case 'mask':
2289
+ if (value === 'none') {
2290
+ result = 'none';
2291
+ } else {
2292
+ result = this.resolveUrl(value);
2293
+ }
2294
+ break;
2295
+ case 'stroke-width':
2296
+ parsed = this.computeLength(value, this.getViewport());
2297
+ if (parsed != null && parsed >= 0) {
2298
+ result = parsed;
2299
+ }
2300
+ break;
2301
+ case 'stroke-miterlimit':
2302
+ parsed = parseFloat(value);
2303
+ if (parsed != null && parsed >= 1) {
2304
+ result = parsed;
2305
+ }
2306
+ break;
2307
+ case 'word-spacing':
2308
+ case 'letter-spacing':
2309
+ result = this.computeLength(value, this.getViewport());
2310
+ break;
2311
+ case 'stroke-dashoffset':
2312
+ result = this.computeLength(value, this.getViewport());
2313
+ if (result != null) {
2314
+ if (result < 0) {
2315
+ let dasharray = this.get('stroke-dasharray');
2316
+ for (let j = 0; j < dasharray.length; j++) {
2317
+ result += dasharray[j];
2318
+ }
2319
+ }
2320
+ }
2321
+ break;
2322
+ }
2323
+ if (result != null) {
2324
+ return styleCache[key] = result;
2325
+ }
2326
+ }
2327
+ }
2328
+ return styleCache[key] = keyInfo.inherit && this.inherits ? this.inherits.get(key) : keyInfo.initial;
2329
+ };
2330
+ this.getChildren = function () {
2331
+ if (childrenCache != null) {
2332
+ return childrenCache;
2333
+ }
2334
+ let children = [];
2335
+ for (let i = 0; i < obj.childNodes.length; i++) {
2336
+ let child = obj.childNodes[i];
2337
+ if (!child.error && this.allowedChildren.indexOf(child.nodeName) !== -1) {
2338
+ children.push(createSVGElement(child, this));
2339
+ }
2340
+ }
2341
+ return childrenCache = children;
2342
+ };
2343
+ this.getParentVWidth = function () {
2344
+ return this.inherits ? this.inherits.getVWidth() : viewportWidth;
2345
+ };
2346
+ this.getParentVHeight = function () {
2347
+ return this.inherits ? this.inherits.getVHeight() : viewportHeight;
2348
+ };
2349
+ this.getParentViewport = function () {
2350
+ return Math.sqrt(0.5 * this.getParentVWidth() * this.getParentVWidth() + 0.5 * this.getParentVHeight() * this.getParentVHeight());
2351
+ };
2352
+ this.getVWidth = function () {
2353
+ return this.getParentVWidth();
2354
+ };
2355
+ this.getVHeight = function () {
2356
+ return this.getParentVHeight();
2357
+ };
2358
+ this.getViewport = function () {
2359
+ return Math.sqrt(0.5 * this.getVWidth() * this.getVWidth() + 0.5 * this.getVHeight() * this.getVHeight());
2360
+ };
2361
+ this.getBoundingBox = function () {
2362
+ let shape = this.getBoundingShape();
2363
+ return shape.getBoundingBox();
2364
+ };
2365
+ };
2366
+ var SvgElemStylable = function (obj, inherits) {
2367
+ SvgElem.call(this, obj, inherits);
2368
+ this.transform = function () {
2369
+ doc.transform.apply(doc, this.getTransformation());
2370
+ };
2371
+ this.clip = function () {
2372
+ if (this.get('clip-path') !== 'none') {
2373
+ let clipPath = new SvgElemClipPath(this.get('clip-path'), null);
2374
+ clipPath.useMask(this.getBoundingBox());
2375
+ return true;
2376
+ }
2377
+ };
2378
+ this.mask = function () {
2379
+ if (this.get('mask') !== 'none') {
2380
+ let mask = new SvgElemMask(this.get('mask'), null);
2381
+ mask.useMask(this.getBoundingBox());
2382
+ return true;
2383
+ }
2384
+ };
2385
+ this.getFill = function (isClip, isMask) {
2386
+ let opacity = this.get('opacity'),
2387
+ fill = this.get('fill'),
2388
+ fillOpacity = this.get('fill-opacity');
2389
+ if (isClip) {
2390
+ return DefaultColors.white;
2391
+ }
2392
+ if (fill !== 'none' && opacity && fillOpacity) {
2393
+ if (fill instanceof SvgElemGradient || fill instanceof SvgElemPattern) {
2394
+ return fill.getPaint(this.getBoundingBox(), fillOpacity * opacity, isClip, isMask);
2395
+ }
2396
+ return opacityToColor(fill, fillOpacity * opacity, isMask);
2397
+ }
2398
+ };
2399
+ this.getStroke = function (isClip, isMask) {
2400
+ let opacity = this.get('opacity'),
2401
+ stroke = this.get('stroke'),
2402
+ strokeOpacity = this.get('stroke-opacity');
2403
+ if (isClip || isEqual(this.get('stroke-width'), 0)) {
2404
+ return;
2405
+ }
2406
+ if (stroke !== 'none' && opacity && strokeOpacity) {
2407
+ if (stroke instanceof SvgElemGradient || stroke instanceof SvgElemPattern) {
2408
+ return stroke.getPaint(this.getBoundingBox(), strokeOpacity * opacity, isClip, isMask);
2409
+ }
2410
+ return opacityToColor(stroke, strokeOpacity * opacity, isMask);
2411
+ }
2412
+ };
2413
+ };
2414
+ var SvgElemHasChildren = function (obj, inherits) {
2415
+ SvgElemStylable.call(this, obj, inherits);
2416
+ this.allowedChildren = ['use', 'g', 'a', 'svg', 'image', 'rect', 'circle', 'ellipse', 'line', 'polyline', 'polygon', 'path', 'text'];
2417
+ this.getBoundingShape = function () {
2418
+ let shape = new SvgShape(),
2419
+ children = this.getChildren();
2420
+ for (let i = 0; i < children.length; i++) {
2421
+ if (children[i].get('display') !== 'none') {
2422
+ if (typeof children[i].getBoundingShape === 'function') {
2423
+ let childShape = children[i].getBoundingShape().clone();
2424
+ if (typeof children[i].getTransformation === 'function') {
2425
+ childShape.transform(children[i].getTransformation());
2426
+ }
2427
+ shape.mergeShape(childShape);
2428
+ }
2429
+ }
2430
+ }
2431
+ return shape;
2432
+ };
2433
+ this.drawChildren = function (isClip, isMask) {
2434
+ let children = this.getChildren();
2435
+ for (let i = 0; i < children.length; i++) {
2436
+ if (children[i].get('display') !== 'none') {
2437
+ if (typeof children[i].drawInDocument === 'function') {
2438
+ children[i].drawInDocument(isClip, isMask);
2439
+ }
2440
+ }
2441
+ }
2442
+ };
2443
+ };
2444
+ var SvgElemContainer = function (obj, inherits) {
2445
+ SvgElemHasChildren.call(this, obj, inherits);
2446
+ this.drawContent = function (isClip, isMask, blendMode) {
2447
+ this.transform();
2448
+ let clipped = this.clip(),
2449
+ masked = this.mask(),
2450
+ group;
2451
+ if ((this.get('opacity') < 1 || clipped || masked || blendMode) && !isClip) {
2452
+ group = docBeginGroup(getPageBBox());
2453
+ }
2454
+ this.drawChildren(isClip, isMask);
2455
+ if (group) {
2456
+ docEndGroup(group);
2457
+ if (blendMode) {
2458
+ applyBlendMode(group, blendMode);
2459
+ }
2460
+ doc.fillOpacity(this.get('opacity'));
2461
+ docInsertGroup(group);
2462
+ }
2463
+ };
2464
+ };
2465
+ var SvgElemUse = function (obj, inherits) {
2466
+ SvgElemContainer.call(this, obj, inherits);
2467
+ let x = this.getLength('x', this.getVWidth(), 0),
2468
+ y = this.getLength('y', this.getVHeight(), 0),
2469
+ child = this.getUrl('href') || this.getUrl('xlink:href');
2470
+ if (child) {
2471
+ child = createSVGElement(child, this);
2472
+ }
2473
+ this.getChildren = function () {
2474
+ return child ? [child] : [];
2475
+ };
2476
+ this.drawInDocument = function (isClip, isMask) {
2477
+ doc.save();
2478
+ this.drawContent(isClip, isMask);
2479
+ doc.restore();
2480
+ };
2481
+ this.getTransformation = function () {
2482
+ return multiplyMatrix(this.get('transform'), [1, 0, 0, 1, x, y]);
2483
+ };
2484
+ };
2485
+ var SvgElemSymbol = function (obj, inherits) {
2486
+ SvgElemContainer.call(this, obj, inherits);
2487
+ let width = this.getLength('width', this.getParentVWidth(), this.getParentVWidth()),
2488
+ height = this.getLength('height', this.getParentVHeight(), this.getParentVHeight());
2489
+ if (inherits instanceof SvgElemUse) {
2490
+ width = inherits.getLength('width', inherits.getParentVWidth(), width);
2491
+ height = inherits.getLength('height', inherits.getParentVHeight(), height);
2492
+ }
2493
+ let aspectRatio = (this.attr('preserveAspectRatio') || '').trim(),
2494
+ viewBox = this.getViewbox('viewBox', [0, 0, width, height]);
2495
+ this.getVWidth = function () {
2496
+ return viewBox[2];
2497
+ };
2498
+ this.getVHeight = function () {
2499
+ return viewBox[3];
2500
+ };
2501
+ this.drawInDocument = function (isClip, isMask) {
2502
+ doc.save();
2503
+ this.drawContent(isClip, isMask);
2504
+ doc.restore();
2505
+ };
2506
+ this.getTransformation = function () {
2507
+ return multiplyMatrix(parseAspectRatio(aspectRatio, width, height, viewBox[2], viewBox[3]), [1, 0, 0, 1, -viewBox[0], -viewBox[1]]);
2508
+ };
2509
+ };
2510
+ var SvgElemGroup = function (obj, inherits) {
2511
+ SvgElemContainer.call(this, obj, inherits);
2512
+ this.drawInDocument = function (isClip, isMask) {
2513
+ doc.save();
2514
+ if (this.link && !isClip && !isMask) {
2515
+ this.addLink();
2516
+ }
2517
+ const styles = getObjectStyles(obj);
2518
+ if (styles['mix-blend-mode']) {
2519
+ this.drawContent(isClip, isMask, parseCSSBlendMode(styles['mix-blend-mode']));
2520
+ } else {
2521
+ this.drawContent(isClip, isMask);
2522
+ }
2523
+ doc.restore();
2524
+ };
2525
+ this.getTransformation = function () {
2526
+ return this.get('transform');
2527
+ };
2528
+ };
2529
+ var SvgElemLink = function (obj, inherits) {
2530
+ if (inherits && inherits.isText) {
2531
+ SvgElemTspan.call(this, obj, inherits);
2532
+ this.allowedChildren = ['textPath', 'tspan', '#text', '#cdata-section', 'a'];
2533
+ } else {
2534
+ SvgElemGroup.call(this, obj, inherits);
2535
+ }
2536
+ this.link = this.attr('href') || this.attr('xlink:href');
2537
+ this.addLink = function () {
2538
+ if (this.link.match(/^(?:[a-z][a-z0-9+.-]*:|\/\/)?/i) && this.getChildren().length) {
2539
+ let bbox = this.getBoundingShape().transform(getGlobalMatrix()).getBoundingBox();
2540
+ docInsertLink(bbox[0], bbox[1], bbox[2], bbox[3], this.link);
2541
+ }
2542
+ };
2543
+ };
2544
+ var SvgElemSvg = function (obj, inherits) {
2545
+ SvgElemContainer.call(this, obj, inherits);
2546
+ let width = this.getLength('width', this.getParentVWidth(), this.getParentVWidth()),
2547
+ height = this.getLength('height', this.getParentVHeight(), this.getParentVHeight()),
2548
+ x = this.getLength('x', this.getParentVWidth(), 0),
2549
+ y = this.getLength('y', this.getParentVHeight(), 0);
2550
+ if (inherits instanceof SvgElemUse) {
2551
+ width = inherits.getLength('width', inherits.getParentVWidth(), width);
2552
+ height = inherits.getLength('height', inherits.getParentVHeight(), height);
2553
+ }
2554
+ let aspectRatio = this.attr('preserveAspectRatio'),
2555
+ viewBox = this.getViewbox('viewBox', [0, 0, width, height]);
2556
+ if (this.isOuterElement && preserveAspectRatio) {
2557
+ x = y = 0;
2558
+ width = viewportWidth;
2559
+ height = viewportHeight;
2560
+ aspectRatio = preserveAspectRatio;
2561
+ }
2562
+ this.getVWidth = function () {
2563
+ return viewBox[2];
2564
+ };
2565
+ this.getVHeight = function () {
2566
+ return viewBox[3];
2567
+ };
2568
+ this.drawInDocument = function (isClip, isMask) {
2569
+ doc.save();
2570
+ if (this.get('overflow') === 'hidden') {
2571
+ new SvgShape().M(x, y).L(x + width, y).L(x + width, y + height).L(x, y + height).Z().transform(this.get('transform')).insertInDocument();
2572
+ doc.clip();
2573
+ }
2574
+ this.drawContent(isClip, isMask);
2575
+ doc.restore();
2576
+ };
2577
+ this.getTransformation = function () {
2578
+ return multiplyMatrix(this.get('transform'), [1, 0, 0, 1, x, y], parseAspectRatio(aspectRatio, width, height, viewBox[2], viewBox[3]), [1, 0, 0, 1, -viewBox[0], -viewBox[1]]);
2579
+ };
2580
+ };
2581
+ var SVGElemImage = function (obj, inherits) {
2582
+ SvgElemStylable.call(this, obj, inherits);
2583
+ const {
2584
+ src: imageSrc,
2585
+ properties
2586
+ } = imageCallback(this.attr('href') || this.attr('xlink:href') || '');
2587
+ let x = this.getLength('x', this.getVWidth(), 0),
2588
+ y = this.getLength('y', this.getVHeight(), 0),
2589
+ width = this.getLength('width', this.getVWidth(), 'auto'),
2590
+ height = this.getLength('height', this.getVHeight(), 'auto'),
2591
+ image;
2592
+ try {
2593
+ image = doc.openImage(imageSrc, properties);
2594
+ } catch (e) {
2595
+ warningCallback('SVGElemImage: failed to open image "' + e + '" in PDFKit', e);
2596
+ }
2597
+ if (image) {
2598
+ if (width === 'auto' && height !== 'auto') {
2599
+ width = height * image.width / image.height;
2600
+ } else if (height === 'auto' && width !== 'auto') {
2601
+ height = width * image.height / image.width;
2602
+ } else if (width === 'auto' && height === 'auto') {
2603
+ width = image.width;
2604
+ height = image.height;
2605
+ }
2606
+ }
2607
+ if (width === 'auto' || width < 0) {
2608
+ width = 0;
2609
+ }
2610
+ if (height === 'auto' || height < 0) {
2611
+ height = 0;
2612
+ }
2613
+ this.getTransformation = function () {
2614
+ return this.get('transform');
2615
+ };
2616
+ this.getBoundingShape = function () {
2617
+ return new SvgShape().M(x, y).L(x + width, y).M(x + width, y + height).L(x, y + height);
2618
+ };
2619
+ this.drawInDocument = function (isClip, isMask) {
2620
+ if (this.get('visibility') === 'hidden' || !image) {
2621
+ return;
2622
+ }
2623
+ doc.save();
2624
+ this.transform();
2625
+ if (this.get('overflow') === 'hidden') {
2626
+ doc.rect(x, y, width, height).clip();
2627
+ }
2628
+ this.clip();
2629
+ this.mask();
2630
+ doc.translate(x, y);
2631
+ doc.transform.apply(doc, parseAspectRatio(this.attr('preserveAspectRatio'), width, height, image ? image.width : width, image ? image.height : height));
2632
+ if (!isClip) {
2633
+ doc.fillOpacity(this.get('opacity'));
2634
+ doc.image(image, 0, 0);
2635
+ } else {
2636
+ doc.rect(0, 0, image.width, image.height);
2637
+ docFillColor(DefaultColors.white).fill();
2638
+ }
2639
+ doc.restore();
2640
+ };
2641
+ };
2642
+ var SvgElemPattern = function (obj, inherits, fallback) {
2643
+ SvgElemHasChildren.call(this, obj, inherits);
2644
+ this.ref = function () {
2645
+ let ref = this.getUrl('href') || this.getUrl('xlink:href');
2646
+ if (ref && ref.nodeName === obj.nodeName) {
2647
+ return new SvgElemPattern(ref, inherits, fallback);
2648
+ }
2649
+ }.call(this);
2650
+ let _attr = this.attr;
2651
+ this.attr = function (key) {
2652
+ let attr = _attr.call(this, key);
2653
+ if (attr != null || key === 'href' || key === 'xlink:href') {
2654
+ return attr;
2655
+ }
2656
+ return this.ref ? this.ref.attr(key) : null;
2657
+ };
2658
+ let _getChildren = this.getChildren;
2659
+ this.getChildren = function () {
2660
+ let children = _getChildren.call(this);
2661
+ if (children.length > 0) {
2662
+ return children;
2663
+ }
2664
+ return this.ref ? this.ref.getChildren() : [];
2665
+ };
2666
+ this.getPaint = function (bBox, gOpacity, isClip, isMask) {
2667
+ let bBoxUnitsPattern = this.attr('patternUnits') !== 'userSpaceOnUse',
2668
+ bBoxUnitsContent = this.attr('patternContentUnits') === 'objectBoundingBox',
2669
+ x = this.getLength('x', bBoxUnitsPattern ? 1 : this.getParentVWidth(), 0),
2670
+ y = this.getLength('y', bBoxUnitsPattern ? 1 : this.getParentVHeight(), 0),
2671
+ width = this.getLength('width', bBoxUnitsPattern ? 1 : this.getParentVWidth(), 0),
2672
+ height = this.getLength('height', bBoxUnitsPattern ? 1 : this.getParentVHeight(), 0);
2673
+ if (bBoxUnitsContent && !bBoxUnitsPattern) {
2674
+ x = (x - bBox[0]) / (bBox[2] - bBox[0]) || 0;
2675
+ y = (y - bBox[1]) / (bBox[3] - bBox[1]) || 0;
2676
+ width = width / (bBox[2] - bBox[0]) || 0;
2677
+ height = height / (bBox[3] - bBox[1]) || 0;
2678
+ } else if (!bBoxUnitsContent && bBoxUnitsPattern) {
2679
+ x = bBox[0] + x * (bBox[2] - bBox[0]);
2680
+ y = bBox[1] + y * (bBox[3] - bBox[1]);
2681
+ width = width * (bBox[2] - bBox[0]);
2682
+ height = height * (bBox[3] - bBox[1]);
2683
+ }
2684
+ let viewBox = this.getViewbox('viewBox', [0, 0, width, height]),
2685
+ aspectRatio = (this.attr('preserveAspectRatio') || '').trim(),
2686
+ aspectRatioMatrix = multiplyMatrix(parseAspectRatio(aspectRatio, width, height, viewBox[2], viewBox[3], 0), [1, 0, 0, 1, -viewBox[0], -viewBox[1]]),
2687
+ matrix = parseTranform(this.attr('patternTransform'));
2688
+ if (bBoxUnitsContent) {
2689
+ matrix = multiplyMatrix([bBox[2] - bBox[0], 0, 0, bBox[3] - bBox[1], bBox[0], bBox[1]], matrix);
2690
+ }
2691
+ matrix = multiplyMatrix(matrix, [1, 0, 0, 1, x, y]);
2692
+ if ((matrix = validateMatrix(matrix)) && (aspectRatioMatrix = validateMatrix(aspectRatioMatrix)) && (width = validateNumber(width)) && (height = validateNumber(height))) {
2693
+ let group = docBeginGroup([0, 0, width, height]);
2694
+ doc.transform.apply(doc, aspectRatioMatrix);
2695
+ this.drawChildren(isClip, isMask);
2696
+ docEndGroup(group);
2697
+ return [docCreatePattern(group, width, height, matrix), gOpacity];
2698
+ } else {
2699
+ return fallback ? [fallback[0], fallback[1] * gOpacity] : undefined;
2700
+ }
2701
+ };
2702
+ this.getVWidth = function () {
2703
+ let bBoxUnitsPattern = this.attr('patternUnits') !== 'userSpaceOnUse',
2704
+ width = this.getLength('width', bBoxUnitsPattern ? 1 : this.getParentVWidth(), 0);
2705
+ return this.getViewbox('viewBox', [0, 0, width, 0])[2];
2706
+ };
2707
+ this.getVHeight = function () {
2708
+ let bBoxUnitsPattern = this.attr('patternUnits') !== 'userSpaceOnUse',
2709
+ height = this.getLength('height', bBoxUnitsPattern ? 1 : this.getParentVHeight(), 0);
2710
+ return this.getViewbox('viewBox', [0, 0, 0, height])[3];
2711
+ };
2712
+ };
2713
+ var SvgElemGradient = function (obj, inherits, fallback) {
2714
+ SvgElem.call(this, obj, inherits);
2715
+ this.allowedChildren = ['stop'];
2716
+ this.ref = function () {
2717
+ let ref = this.getUrl('href') || this.getUrl('xlink:href');
2718
+ if (ref && ref.nodeName === obj.nodeName) {
2719
+ return new SvgElemGradient(ref, inherits, fallback);
2720
+ }
2721
+ }.call(this);
2722
+ let _attr = this.attr;
2723
+ this.attr = function (key) {
2724
+ let attr = _attr.call(this, key);
2725
+ if (attr != null || key === 'href' || key === 'xlink:href') {
2726
+ return attr;
2727
+ }
2728
+ return this.ref ? this.ref.attr(key) : null;
2729
+ };
2730
+ let _getChildren = this.getChildren;
2731
+ this.getChildren = function () {
2732
+ let children = _getChildren.call(this);
2733
+ if (children.length > 0) {
2734
+ return children;
2735
+ }
2736
+ return this.ref ? this.ref.getChildren() : [];
2737
+ };
2738
+ this.getPaint = function (bBox, gOpacity, isClip, isMask) {
2739
+ let children = this.getChildren();
2740
+ if (children.length === 0) {
2741
+ return;
2742
+ }
2743
+ if (children.length === 1) {
2744
+ let child = children[0],
2745
+ stopColor = child.get('stop-color');
2746
+ if (stopColor === 'none') {
2747
+ return;
2748
+ }
2749
+ return opacityToColor(stopColor, child.get('stop-opacity') * gOpacity, isMask);
2750
+ }
2751
+ let bBoxUnits = this.attr('gradientUnits') !== 'userSpaceOnUse',
2752
+ matrix = parseTranform(this.attr('gradientTransform')),
2753
+ spread = this.attr('spreadMethod'),
2754
+ grad,
2755
+ x1,
2756
+ x2,
2757
+ y1,
2758
+ y2,
2759
+ r2,
2760
+ nAfter = 0,
2761
+ nBefore = 0,
2762
+ nTotal = 1;
2763
+ if (matrix && bBoxUnits) {
2764
+ matrix = multiplyMatrix([bBox[2] - bBox[0], 0, 0, bBox[3] - bBox[1], bBox[0], bBox[1]], matrix);
2765
+ }
2766
+ if (matrix && (matrix = validateMatrix(matrix))) {
2767
+ if (this.name === 'linearGradient') {
2768
+ x1 = this.getLength('x1', bBoxUnits ? 1 : this.getVWidth(), 0);
2769
+ x2 = this.getLength('x2', bBoxUnits ? 1 : this.getVWidth(), bBoxUnits ? 1 : this.getVWidth());
2770
+ y1 = this.getLength('y1', bBoxUnits ? 1 : this.getVHeight(), 0);
2771
+ y2 = this.getLength('y2', bBoxUnits ? 1 : this.getVHeight(), 0);
2772
+ } else {
2773
+ x2 = this.getLength('cx', bBoxUnits ? 1 : this.getVWidth(), bBoxUnits ? 0.5 : 0.5 * this.getVWidth());
2774
+ y2 = this.getLength('cy', bBoxUnits ? 1 : this.getVHeight(), bBoxUnits ? 0.5 : 0.5 * this.getVHeight());
2775
+ r2 = this.getLength('r', bBoxUnits ? 1 : this.getViewport(), bBoxUnits ? 0.5 : 0.5 * this.getViewport());
2776
+ x1 = this.getLength('fx', bBoxUnits ? 1 : this.getVWidth(), x2);
2777
+ y1 = this.getLength('fy', bBoxUnits ? 1 : this.getVHeight(), y2);
2778
+ if (r2 < 0) {
2779
+ warningCallback('SvgElemGradient: negative r value');
2780
+ }
2781
+ let d = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2)),
2782
+ multiplier = 1;
2783
+ if (d > r2) {
2784
+ multiplier = r2 / d;
2785
+ x1 = x2 + (x1 - x2) * multiplier;
2786
+ y1 = y2 + (y1 - y2) * multiplier;
2787
+ }
2788
+ r2 = Math.max(r2, d * multiplier * (1 + 1e-6));
2789
+ }
2790
+ if (spread === 'reflect' || spread === 'repeat') {
2791
+ let inv = inverseMatrix(matrix),
2792
+ corner1 = transformPoint([bBox[0], bBox[1]], inv),
2793
+ corner2 = transformPoint([bBox[2], bBox[1]], inv),
2794
+ corner3 = transformPoint([bBox[2], bBox[3]], inv),
2795
+ corner4 = transformPoint([bBox[0], bBox[3]], inv);
2796
+ if (this.name === 'linearGradient') {
2797
+ nAfter = Math.max((corner1[0] - x2) * (x2 - x1) + (corner1[1] - y2) * (y2 - y1), (corner2[0] - x2) * (x2 - x1) + (corner2[1] - y2) * (y2 - y1), (corner3[0] - x2) * (x2 - x1) + (corner3[1] - y2) * (y2 - y1), (corner4[0] - x2) * (x2 - x1) + (corner4[1] - y2) * (y2 - y1)) / (Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
2798
+ nBefore = Math.max((corner1[0] - x1) * (x1 - x2) + (corner1[1] - y1) * (y1 - y2), (corner2[0] - x1) * (x1 - x2) + (corner2[1] - y1) * (y1 - y2), (corner3[0] - x1) * (x1 - x2) + (corner3[1] - y1) * (y1 - y2), (corner4[0] - x1) * (x1 - x2) + (corner4[1] - y1) * (y1 - y2)) / (Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
2799
+ } else {
2800
+ nAfter = Math.sqrt(Math.max(Math.pow(corner1[0] - x2, 2) + Math.pow(corner1[1] - y2, 2), Math.pow(corner2[0] - x2, 2) + Math.pow(corner2[1] - y2, 2), Math.pow(corner3[0] - x2, 2) + Math.pow(corner3[1] - y2, 2), Math.pow(corner4[0] - x2, 2) + Math.pow(corner4[1] - y2, 2))) / r2 - 1;
2801
+ }
2802
+ nAfter = Math.ceil(nAfter + 0.5);
2803
+ nBefore = Math.ceil(nBefore + 0.5);
2804
+ nTotal = nBefore + 1 + nAfter;
2805
+ }
2806
+ if (this.name === 'linearGradient') {
2807
+ grad = doc.linearGradient(x1 - nBefore * (x2 - x1), y1 - nBefore * (y2 - y1), x2 + nAfter * (x2 - x1), y2 + nAfter * (y2 - y1));
2808
+ } else {
2809
+ grad = doc.radialGradient(x1, y1, 0, x2, y2, r2 + nAfter * r2);
2810
+ }
2811
+ for (let n = 0; n < nTotal; n++) {
2812
+ let offset = 0,
2813
+ inOrder = spread !== 'reflect' || (n - nBefore) % 2 === 0;
2814
+ for (let i = 0; i < children.length; i++) {
2815
+ let child = children[inOrder ? i : children.length - 1 - i],
2816
+ stopColor = child.get('stop-color');
2817
+ if (stopColor === 'none') {
2818
+ stopColor = DefaultColors.transparent;
2819
+ }
2820
+ stopColor = opacityToColor(stopColor, child.get('stop-opacity') * gOpacity, isMask);
2821
+ offset = Math.max(offset, inOrder ? child.getPercent('offset', 0) : 1 - child.getPercent('offset', 0));
2822
+ if (i === 0 && offset > 0) {
2823
+ grad.stop((n + 0) / nTotal, stopColor[0], stopColor[1]);
2824
+ }
2825
+ grad.stop((n + offset) / (nAfter + nBefore + 1), stopColor[0], stopColor[1]);
2826
+ if (i === children.length - 1 && offset < 1) {
2827
+ grad.stop((n + 1) / nTotal, stopColor[0], stopColor[1]);
2828
+ }
2829
+ }
2830
+ }
2831
+ grad.setTransform.apply(grad, matrix);
2832
+ return [grad, 1];
2833
+ } else {
2834
+ return fallback ? [fallback[0], fallback[1] * gOpacity] : undefined;
2835
+ }
2836
+ };
2837
+ };
2838
+ var SvgElemBasicShape = function (obj, inherits) {
2839
+ SvgElemStylable.call(this, obj, inherits);
2840
+ this.dashScale = 1;
2841
+ this.getBoundingShape = function () {
2842
+ return this.shape;
2843
+ };
2844
+ this.getTransformation = function () {
2845
+ return this.get('transform');
2846
+ };
2847
+ this.drawInDocument = function (isClip, isMask) {
2848
+ if (this.get('visibility') === 'hidden' || !this.shape) {
2849
+ return;
2850
+ }
2851
+ doc.save();
2852
+ this.transform();
2853
+ this.clip();
2854
+ if (!isClip) {
2855
+ let masked = this.mask(),
2856
+ group;
2857
+ if (masked) {
2858
+ group = docBeginGroup(getPageBBox());
2859
+ }
2860
+ let subPaths = this.shape.getSubPaths(),
2861
+ fill = this.getFill(isClip, isMask),
2862
+ stroke = this.getStroke(isClip, isMask),
2863
+ lineWidth = this.get('stroke-width'),
2864
+ lineCap = this.get('stroke-linecap');
2865
+ if (fill || stroke) {
2866
+ if (fill) {
2867
+ docFillColor(fill);
2868
+ }
2869
+ if (stroke) {
2870
+ for (let j = 0; j < subPaths.length; j++) {
2871
+ if (isEqual(subPaths[j].totalLength, 0)) {
2872
+ if ((lineCap === 'square' || lineCap === 'round') && lineWidth > 0) {
2873
+ if (subPaths[j].startPoint && subPaths[j].startPoint.length > 1) {
2874
+ let x = subPaths[j].startPoint[0],
2875
+ y = subPaths[j].startPoint[1];
2876
+ docFillColor(stroke);
2877
+ if (lineCap === 'square') {
2878
+ doc.rect(x - 0.5 * lineWidth, y - 0.5 * lineWidth, lineWidth, lineWidth);
2879
+ } else if (lineCap === 'round') {
2880
+ doc.circle(x, y, 0.5 * lineWidth);
2881
+ }
2882
+ doc.fill();
2883
+ }
2884
+ }
2885
+ }
2886
+ }
2887
+ let dashArray = this.get('stroke-dasharray'),
2888
+ dashOffset = this.get('stroke-dashoffset');
2889
+ if (isNotEqual(this.dashScale, 1)) {
2890
+ for (let j = 0; j < dashArray.length; j++) {
2891
+ dashArray[j] *= this.dashScale;
2892
+ }
2893
+ dashOffset *= this.dashScale;
2894
+ }
2895
+ docStrokeColor(stroke);
2896
+ doc.lineWidth(lineWidth).miterLimit(this.get('stroke-miterlimit')).lineJoin(this.get('stroke-linejoin')).lineCap(lineCap).dash(dashArray, {
2897
+ phase: dashOffset
2898
+ });
2899
+ }
2900
+ for (let j = 0; j < subPaths.length; j++) {
2901
+ if (subPaths[j].totalLength > 0) {
2902
+ subPaths[j].insertInDocument();
2903
+ }
2904
+ }
2905
+ if (fill && stroke) {
2906
+ doc.fillAndStroke(this.get('fill-rule'));
2907
+ } else if (fill) {
2908
+ doc.fill(this.get('fill-rule'));
2909
+ } else if (stroke) {
2910
+ doc.stroke();
2911
+ }
2912
+ }
2913
+ let markerStart = this.get('marker-start'),
2914
+ markerMid = this.get('marker-mid'),
2915
+ markerEnd = this.get('marker-end');
2916
+ if (markerStart !== 'none' || markerMid !== 'none' || markerEnd !== 'none') {
2917
+ let markersPos = this.shape.getMarkers();
2918
+ if (markerStart !== 'none' && markersPos.length > 0) {
2919
+ let marker = new SvgElemMarker(markerStart, null);
2920
+ marker.drawMarker(false, isMask, markersPos[0], lineWidth);
2921
+ }
2922
+ if (markerMid !== 'none') {
2923
+ for (let i = 1; i < markersPos.length - 1; i++) {
2924
+ let marker = new SvgElemMarker(markerMid, null);
2925
+ marker.drawMarker(false, isMask, markersPos[i], lineWidth);
2926
+ }
2927
+ }
2928
+ if (markerEnd !== 'none' && markersPos.length > 0) {
2929
+ let marker = new SvgElemMarker(markerEnd, null);
2930
+ marker.drawMarker(false, isMask, markersPos[markersPos.length - 1], lineWidth);
2931
+ }
2932
+ }
2933
+ if (group) {
2934
+ docEndGroup(group);
2935
+ docInsertGroup(group);
2936
+ }
2937
+ } else {
2938
+ this.shape.insertInDocument();
2939
+ docFillColor(DefaultColors.white);
2940
+ doc.fill(this.get('clip-rule'));
2941
+ }
2942
+ doc.restore();
2943
+ };
2944
+ };
2945
+ var SvgElemRect = function (obj, inherits) {
2946
+ SvgElemBasicShape.call(this, obj, inherits);
2947
+ let x = this.getLength('x', this.getVWidth(), 0),
2948
+ y = this.getLength('y', this.getVHeight(), 0),
2949
+ w = this.getLength('width', this.getVWidth(), 0),
2950
+ h = this.getLength('height', this.getVHeight(), 0),
2951
+ rx = this.getLength('rx', this.getVWidth()),
2952
+ ry = this.getLength('ry', this.getVHeight());
2953
+ if (rx === undefined && ry === undefined) {
2954
+ rx = ry = 0;
2955
+ } else if (rx === undefined && ry !== undefined) {
2956
+ rx = ry;
2957
+ } else if (rx !== undefined && ry === undefined) {
2958
+ ry = rx;
2959
+ }
2960
+ if (w > 0 && h > 0) {
2961
+ if (rx && ry) {
2962
+ rx = Math.min(rx, 0.5 * w);
2963
+ ry = Math.min(ry, 0.5 * h);
2964
+ this.shape = new SvgShape().M(x + rx, y).L(x + w - rx, y).A(rx, ry, 0, 0, 1, x + w, y + ry).L(x + w, y + h - ry).A(rx, ry, 0, 0, 1, x + w - rx, y + h).L(x + rx, y + h).A(rx, ry, 0, 0, 1, x, y + h - ry).L(x, y + ry).A(rx, ry, 0, 0, 1, x + rx, y).Z();
2965
+ } else {
2966
+ this.shape = new SvgShape().M(x, y).L(x + w, y).L(x + w, y + h).L(x, y + h).Z();
2967
+ }
2968
+ } else {
2969
+ this.shape = new SvgShape();
2970
+ }
2971
+ };
2972
+ var SvgElemCircle = function (obj, inherits) {
2973
+ SvgElemBasicShape.call(this, obj, inherits);
2974
+ let cx = this.getLength('cx', this.getVWidth(), 0),
2975
+ cy = this.getLength('cy', this.getVHeight(), 0),
2976
+ r = this.getLength('r', this.getViewport(), 0);
2977
+ if (r > 0) {
2978
+ this.shape = new SvgShape().M(cx + r, cy).A(r, r, 0, 0, 1, cx - r, cy).A(r, r, 0, 0, 1, cx + r, cy).Z();
2979
+ } else {
2980
+ this.shape = new SvgShape();
2981
+ }
2982
+ };
2983
+ var SvgElemEllipse = function (obj, inherits) {
2984
+ SvgElemBasicShape.call(this, obj, inherits);
2985
+ let cx = this.getLength('cx', this.getVWidth(), 0),
2986
+ cy = this.getLength('cy', this.getVHeight(), 0),
2987
+ rx = this.getLength('rx', this.getVWidth(), 0),
2988
+ ry = this.getLength('ry', this.getVHeight(), 0);
2989
+ if (rx > 0 && ry > 0) {
2990
+ this.shape = new SvgShape().M(cx + rx, cy).A(rx, ry, 0, 0, 1, cx - rx, cy).A(rx, ry, 0, 0, 1, cx + rx, cy).Z();
2991
+ } else {
2992
+ this.shape = new SvgShape();
2993
+ }
2994
+ };
2995
+ var SvgElemLine = function (obj, inherits) {
2996
+ SvgElemBasicShape.call(this, obj, inherits);
2997
+ let x1 = this.getLength('x1', this.getVWidth(), 0),
2998
+ y1 = this.getLength('y1', this.getVHeight(), 0),
2999
+ x2 = this.getLength('x2', this.getVWidth(), 0),
3000
+ y2 = this.getLength('y2', this.getVHeight(), 0);
3001
+ this.shape = new SvgShape().M(x1, y1).L(x2, y2);
3002
+ };
3003
+ var SvgElemPolyline = function (obj, inherits) {
3004
+ SvgElemBasicShape.call(this, obj, inherits);
3005
+ let points = this.getNumberList('points');
3006
+ this.shape = new SvgShape();
3007
+ for (let i = 0; i < points.length - 1; i += 2) {
3008
+ if (i === 0) {
3009
+ this.shape.M(points[i], points[i + 1]);
3010
+ } else {
3011
+ this.shape.L(points[i], points[i + 1]);
3012
+ }
3013
+ }
3014
+ if (points.error) {
3015
+ warningCallback('SvgElemPolygon: unexpected string ' + points.error);
3016
+ }
3017
+ if (points.length % 2 === 1) {
3018
+ warningCallback('SvgElemPolyline: uneven number of coordinates');
3019
+ }
3020
+ };
3021
+ var SvgElemPolygon = function (obj, inherits) {
3022
+ SvgElemBasicShape.call(this, obj, inherits);
3023
+ let points = this.getNumberList('points');
3024
+ this.shape = new SvgShape();
3025
+ for (let i = 0; i < points.length - 1; i += 2) {
3026
+ if (i === 0) {
3027
+ this.shape.M(points[i], points[i + 1]);
3028
+ } else {
3029
+ this.shape.L(points[i], points[i + 1]);
3030
+ }
3031
+ }
3032
+ this.shape.Z();
3033
+ if (points.error) {
3034
+ warningCallback('SvgElemPolygon: unexpected string ' + points.error);
3035
+ }
3036
+ if (points.length % 2 === 1) {
3037
+ warningCallback('SvgElemPolygon: uneven number of coordinates');
3038
+ }
3039
+ };
3040
+ var SvgElemPath = function (obj, inherits) {
3041
+ SvgElemBasicShape.call(this, obj, inherits);
3042
+ this.shape = new SvgShape().path(this.attr('d'));
3043
+ let pathLength = this.getLength('pathLength', this.getViewport());
3044
+ this.pathLength = pathLength > 0 ? pathLength : undefined;
3045
+ this.dashScale = this.pathLength !== undefined ? this.shape.totalLength / this.pathLength : 1;
3046
+ };
3047
+ var SvgElemMarker = function (obj, inherits) {
3048
+ SvgElemHasChildren.call(this, obj, inherits);
3049
+ let width = this.getLength('markerWidth', this.getParentVWidth(), 3),
3050
+ height = this.getLength('markerHeight', this.getParentVHeight(), 3),
3051
+ viewBox = this.getViewbox('viewBox', [0, 0, width, height]);
3052
+ this.getVWidth = function () {
3053
+ return viewBox[2];
3054
+ };
3055
+ this.getVHeight = function () {
3056
+ return viewBox[3];
3057
+ };
3058
+ this.drawMarker = function (isClip, isMask, posArray, strokeWidth) {
3059
+ doc.save();
3060
+ let orient = this.attr('orient'),
3061
+ units = this.attr('markerUnits'),
3062
+ rotate = orient === 'auto' ? posArray[2] : (parseFloat(orient) || 0) * Math.PI / 180,
3063
+ scale = units === 'userSpaceOnUse' ? 1 : strokeWidth;
3064
+ doc.transform(Math.cos(rotate) * scale, Math.sin(rotate) * scale, -Math.sin(rotate) * scale, Math.cos(rotate) * scale, posArray[0], posArray[1]);
3065
+ let refX = this.getLength('refX', this.getVWidth(), 0),
3066
+ refY = this.getLength('refY', this.getVHeight(), 0),
3067
+ aspectRatioMatrix = parseAspectRatio(this.attr('preserveAspectRatio'), width, height, viewBox[2], viewBox[3], 0.5);
3068
+ if (this.get('overflow') === 'hidden') {
3069
+ doc.rect(aspectRatioMatrix[0] * (viewBox[0] + viewBox[2] / 2 - refX) - width / 2, aspectRatioMatrix[3] * (viewBox[1] + viewBox[3] / 2 - refY) - height / 2, width, height).clip();
3070
+ }
3071
+ doc.transform.apply(doc, aspectRatioMatrix);
3072
+ doc.translate(-refX, -refY);
3073
+ let group;
3074
+ if (this.get('opacity') < 1 && !isClip) {
3075
+ group = docBeginGroup(getPageBBox());
3076
+ }
3077
+ this.drawChildren(isClip, isMask);
3078
+ if (group) {
3079
+ docEndGroup(group);
3080
+ doc.fillOpacity(this.get('opacity'));
3081
+ docInsertGroup(group);
3082
+ }
3083
+ doc.restore();
3084
+ };
3085
+ };
3086
+ var SvgElemClipPath = function (obj, inherits) {
3087
+ SvgElemHasChildren.call(this, obj, inherits);
3088
+ this.useMask = function (bBox) {
3089
+ let group = docBeginGroup(getPageBBox());
3090
+ doc.save();
3091
+ doc.transform.apply(doc, this.get('transform'));
3092
+ if (this.attr('clipPathUnits') === 'objectBoundingBox') {
3093
+ doc.transform(bBox[2] - bBox[0], 0, 0, bBox[3] - bBox[1], bBox[0], bBox[1]);
3094
+ }
3095
+ this.clip();
3096
+ this.drawChildren(true, false);
3097
+ doc.restore();
3098
+ docEndGroup(group);
3099
+ docApplyMask(group, true);
3100
+ };
3101
+ };
3102
+ var SvgElemMask = function (obj, inherits) {
3103
+ SvgElemHasChildren.call(this, obj, inherits);
3104
+ this.useMask = function (bBox) {
3105
+ let group = docBeginGroup(getPageBBox());
3106
+ doc.save();
3107
+ if (this.attr('maskUnits') === 'userSpaceOnUse') {
3108
+ this.getLength('x', this.getVWidth(), -0.1 * (bBox[2] - bBox[0]) + bBox[0]);
3109
+ this.getLength('y', this.getVHeight(), -0.1 * (bBox[3] - bBox[1]) + bBox[1]);
3110
+ this.getLength('width', this.getVWidth(), 1.2 * (bBox[2] - bBox[0]));
3111
+ this.getLength('height', this.getVHeight(), 1.2 * (bBox[3] - bBox[1]));
3112
+ } else {
3113
+ this.getLength('x', this.getVWidth(), -0.1) * (bBox[2] - bBox[0]) + bBox[0];
3114
+ this.getLength('y', this.getVHeight(), -0.1) * (bBox[3] - bBox[1]) + bBox[1];
3115
+ this.getLength('width', this.getVWidth(), 1.2) * (bBox[2] - bBox[0]);
3116
+ this.getLength('height', this.getVHeight(), 1.2) * (bBox[3] - bBox[1]);
3117
+ }
3118
+ if (this.attr('maskContentUnits') === 'objectBoundingBox') {
3119
+ doc.transform(bBox[2] - bBox[0], 0, 0, bBox[3] - bBox[1], bBox[0], bBox[1]);
3120
+ }
3121
+ this.clip();
3122
+ this.drawChildren(false, true);
3123
+ doc.restore();
3124
+ docEndGroup(group);
3125
+ docApplyMask(group, true);
3126
+ };
3127
+ };
3128
+ var SvgElemTextContainer = function (obj, inherits) {
3129
+ SvgElemStylable.call(this, obj, inherits);
3130
+ this.allowedChildren = ['tspan', '#text', '#cdata-section', 'a'];
3131
+ this.isText = true;
3132
+ this.getBoundingShape = function () {
3133
+ let shape = new SvgShape();
3134
+ for (let i = 0; i < this._pos.length; i++) {
3135
+ let pos = this._pos[i];
3136
+ if (!pos.hidden) {
3137
+ let dx0 = pos.ascent * Math.sin(pos.rotate),
3138
+ dy0 = -pos.ascent * Math.cos(pos.rotate),
3139
+ dx1 = pos.descent * Math.sin(pos.rotate),
3140
+ dy1 = -pos.descent * Math.cos(pos.rotate),
3141
+ dx2 = pos.width * Math.cos(pos.rotate),
3142
+ dy2 = pos.width * Math.sin(pos.rotate);
3143
+ shape.M(pos.x + dx0, pos.y + dy0).L(pos.x + dx0 + dx2, pos.y + dy0 + dy2).M(pos.x + dx1 + dx2, pos.y + dy1 + dy2).L(pos.x + dx1, pos.y + dy1);
3144
+ }
3145
+ }
3146
+ return shape;
3147
+ };
3148
+ this.drawTextInDocument = function (isClip, isMask) {
3149
+ if (this.link && !isClip && !isMask) {
3150
+ this.addLink();
3151
+ }
3152
+ if (this.get('text-decoration') === 'underline') {
3153
+ this.decorate(0.05 * this._font.size, -0.075 * this._font.size, isClip, isMask);
3154
+ }
3155
+ if (this.get('text-decoration') === 'overline') {
3156
+ this.decorate(0.05 * this._font.size, getAscent(this._font.font, this._font.size) + 0.075 * this._font.size, isClip, isMask);
3157
+ }
3158
+ let fill = this.getFill(isClip, isMask),
3159
+ stroke = this.getStroke(isClip, isMask),
3160
+ strokeWidth = this.get('stroke-width');
3161
+ if (this._font.fauxBold) {
3162
+ if (!stroke) {
3163
+ stroke = fill;
3164
+ strokeWidth = this._font.size * 0.03;
3165
+ } else {
3166
+ strokeWidth += this._font.size * 0.03;
3167
+ }
3168
+ }
3169
+ let children = this.getChildren();
3170
+ for (let i = 0; i < children.length; i++) {
3171
+ let childElem = children[i];
3172
+ switch (childElem.name) {
3173
+ case 'tspan':
3174
+ case 'textPath':
3175
+ case 'a':
3176
+ if (childElem.get('display') !== 'none') {
3177
+ childElem.drawTextInDocument(isClip, isMask);
3178
+ }
3179
+ break;
3180
+ case '#text':
3181
+ case '#cdata-section':
3182
+ if (this.get('visibility') === 'hidden') {
3183
+ continue;
3184
+ }
3185
+ if (fill || stroke || isClip) {
3186
+ if (fill) {
3187
+ docFillColor(fill);
3188
+ }
3189
+ if (stroke && strokeWidth) {
3190
+ docStrokeColor(stroke);
3191
+ doc.lineWidth(strokeWidth).miterLimit(this.get('stroke-miterlimit')).lineJoin(this.get('stroke-linejoin')).lineCap(this.get('stroke-linecap')).dash(this.get('stroke-dasharray'), {
3192
+ phase: this.get('stroke-dashoffset')
3193
+ });
3194
+ }
3195
+ docBeginText(this._font.font, this._font.size);
3196
+ docSetTextMode(!!fill, !!stroke);
3197
+ docWriteGlyphs(childElem._pos, this._font);
3198
+ docEndText();
3199
+ }
3200
+ break;
3201
+ }
3202
+ }
3203
+ if (this.get('text-decoration') === 'line-through') {
3204
+ this.decorate(0.05 * this._font.size, 0.5 * (getAscent(this._font.font, this._font.size) + getDescent(this._font.font, this._font.size)), isClip, isMask);
3205
+ }
3206
+ };
3207
+ this.decorate = function (lineWidth, linePosition, isClip, isMask) {
3208
+ let fill = this.getFill(isClip, isMask),
3209
+ stroke = this.getStroke(isClip, isMask);
3210
+ if (fill) {
3211
+ docFillColor(fill);
3212
+ }
3213
+ if (stroke) {
3214
+ docStrokeColor(stroke);
3215
+ doc.lineWidth(this.get('stroke-width')).miterLimit(this.get('stroke-miterlimit')).lineJoin(this.get('stroke-linejoin')).lineCap(this.get('stroke-linecap')).dash(this.get('stroke-dasharray'), {
3216
+ phase: this.get('stroke-dashoffset')
3217
+ });
3218
+ }
3219
+ for (let j = 0, pos = this._pos; j < pos.length; j++) {
3220
+ if (!pos[j].hidden && isNotEqual(pos[j].width, 0)) {
3221
+ let dx0 = (linePosition + lineWidth / 2) * Math.sin(pos[j].rotate),
3222
+ dy0 = -(linePosition + lineWidth / 2) * Math.cos(pos[j].rotate),
3223
+ dx1 = (linePosition - lineWidth / 2) * Math.sin(pos[j].rotate),
3224
+ dy1 = -(linePosition - lineWidth / 2) * Math.cos(pos[j].rotate),
3225
+ dx2 = pos[j].width * Math.cos(pos[j].rotate),
3226
+ dy2 = pos[j].width * Math.sin(pos[j].rotate);
3227
+ new SvgShape().M(pos[j].x + dx0, pos[j].y + dy0).L(pos[j].x + dx0 + dx2, pos[j].y + dy0 + dy2).L(pos[j].x + dx1 + dx2, pos[j].y + dy1 + dy2).L(pos[j].x + dx1, pos[j].y + dy1).Z().insertInDocument();
3228
+ if (fill && stroke) {
3229
+ doc.fillAndStroke();
3230
+ } else if (fill) {
3231
+ doc.fill();
3232
+ } else if (stroke) {
3233
+ doc.stroke();
3234
+ }
3235
+ }
3236
+ }
3237
+ };
3238
+ };
3239
+ var SvgElemTextNode = function (obj, inherits) {
3240
+ this.name = obj.nodeName;
3241
+ this.textContent = obj.nodeValue;
3242
+ };
3243
+ var SvgElemTspan = function (obj, inherits) {
3244
+ SvgElemTextContainer.call(this, obj, inherits);
3245
+ };
3246
+ var SvgElemTextPath = function (obj, inherits) {
3247
+ SvgElemTextContainer.call(this, obj, inherits);
3248
+ let temp;
3249
+ if ((temp = this.attr('path')) && temp.trim() !== '') {
3250
+ let pathLength = this.getLength('pathLength', this.getViewport());
3251
+ this.pathObject = new SvgShape().path(temp);
3252
+ this.pathLength = pathLength > 0 ? pathLength : this.pathObject.totalLength;
3253
+ this.pathScale = this.pathObject.totalLength / this.pathLength;
3254
+ } else if ((temp = this.getUrl('href') || this.getUrl('xlink:href')) && temp.nodeName === 'path') {
3255
+ let pathElem = new SvgElemPath(temp, this);
3256
+ this.pathObject = pathElem.shape.clone().transform(pathElem.get('transform'));
3257
+ this.pathLength = this.chooseValue(pathElem.pathLength, this.pathObject.totalLength);
3258
+ this.pathScale = this.pathObject.totalLength / this.pathLength;
3259
+ }
3260
+ };
3261
+ var SvgElemText = function (obj, inherits) {
3262
+ SvgElemTextContainer.call(this, obj, inherits);
3263
+ this.allowedChildren = ['textPath', 'tspan', '#text', '#cdata-section', 'a'];
3264
+ (function (textParentElem) {
3265
+ let processedText = '',
3266
+ remainingText = obj.textContent,
3267
+ textPaths = [],
3268
+ currentChunk = [],
3269
+ currentAnchor,
3270
+ currentDirection,
3271
+ currentX = 0,
3272
+ currentY = 0;
3273
+ function doAnchoring() {
3274
+ if (currentChunk.length) {
3275
+ let last = currentChunk[currentChunk.length - 1];
3276
+ let first = currentChunk[0];
3277
+ let width = last.x + last.width - first.x;
3278
+ let anchordx = {
3279
+ startltr: 0,
3280
+ middleltr: 0.5,
3281
+ endltr: 1,
3282
+ startrtl: 1,
3283
+ middlertl: 0.5,
3284
+ endrtl: 0
3285
+ }[currentAnchor + currentDirection] * width || 0;
3286
+ for (let i = 0; i < currentChunk.length; i++) {
3287
+ currentChunk[i].x -= anchordx;
3288
+ }
3289
+ }
3290
+ currentChunk = [];
3291
+ }
3292
+ function adjustLength(pos, length, spacingAndGlyphs) {
3293
+ let firstChar = pos[0],
3294
+ lastChar = pos[pos.length - 1],
3295
+ startX = firstChar.x,
3296
+ endX = lastChar.x + lastChar.width;
3297
+ if (spacingAndGlyphs) {
3298
+ let textScale = length / (endX - startX);
3299
+ if (textScale > 0 && textScale < Infinity) {
3300
+ for (let j = 0; j < pos.length; j++) {
3301
+ pos[j].continuous = false;
3302
+ pos[j].x = startX + textScale * (pos[j].x - startX);
3303
+ pos[j].scale *= textScale;
3304
+ pos[j].width *= textScale;
3305
+ }
3306
+ }
3307
+ } else {
3308
+ if (pos.length >= 2) {
3309
+ let spaceDiff = (length - (endX - startX)) / (pos.length - 1);
3310
+ for (let j = 0; j < pos.length; j++) {
3311
+ pos[j].continuous = false;
3312
+ pos[j].x += j * spaceDiff;
3313
+ }
3314
+ }
3315
+ }
3316
+ currentX += length - (endX - startX);
3317
+ }
3318
+ function recursive(currentElem, parentElem) {
3319
+ currentElem._x = combineArrays(currentElem.getLengthList('x', currentElem.getVWidth()), parentElem ? parentElem._x.slice(parentElem._pos.length) : []);
3320
+ currentElem._y = combineArrays(currentElem.getLengthList('y', currentElem.getVHeight()), parentElem ? parentElem._y.slice(parentElem._pos.length) : []);
3321
+ currentElem._dx = combineArrays(currentElem.getLengthList('dx', currentElem.getVWidth()), parentElem ? parentElem._dx.slice(parentElem._pos.length) : []);
3322
+ currentElem._dy = combineArrays(currentElem.getLengthList('dy', currentElem.getVHeight()), parentElem ? parentElem._dy.slice(parentElem._pos.length) : []);
3323
+ currentElem._rot = combineArrays(currentElem.getNumberList('rotate'), parentElem ? parentElem._rot.slice(parentElem._pos.length) : []);
3324
+ currentElem._defRot = currentElem.chooseValue(currentElem._rot[currentElem._rot.length - 1], parentElem && parentElem._defRot, 0);
3325
+ if (currentElem.name === 'textPath') {
3326
+ currentElem._y = [];
3327
+ }
3328
+ let fontOptions = {
3329
+ fauxItalic: false,
3330
+ fauxBold: false
3331
+ },
3332
+ fontNameorLink = fontCallback(currentElem.get('font-family'), currentElem.get('font-weight') === 'bold', currentElem.get('font-style') === 'italic', fontOptions);
3333
+ try {
3334
+ doc.font(fontNameorLink);
3335
+ } catch (e) {
3336
+ warningCallback('SVGElemText: failed to open font "' + fontNameorLink + '" in PDFKit');
3337
+ }
3338
+ currentElem._pos = [];
3339
+ currentElem._index = 0;
3340
+ currentElem._font = {
3341
+ font: doc._font,
3342
+ size: currentElem.get('font-size'),
3343
+ fauxItalic: fontOptions.fauxItalic,
3344
+ fauxBold: fontOptions.fauxBold
3345
+ };
3346
+ let textLength = currentElem.getLength('textLength', currentElem.getVWidth(), undefined),
3347
+ spacingAndGlyphs = currentElem.attr('lengthAdjust') === 'spacingAndGlyphs',
3348
+ wordSpacing = currentElem.get('word-spacing'),
3349
+ letterSpacing = currentElem.get('letter-spacing'),
3350
+ textAnchor = currentElem.get('text-anchor'),
3351
+ textDirection = currentElem.get('direction'),
3352
+ baseline = getBaseline(currentElem._font.font, currentElem._font.size, currentElem.get('alignment-baseline') || currentElem.get('dominant-baseline'), currentElem.get('baseline-shift'));
3353
+ if (currentElem.name === 'textPath') {
3354
+ doAnchoring();
3355
+ currentX = currentY = 0;
3356
+ }
3357
+ let children = currentElem.getChildren();
3358
+ for (let i = 0; i < children.length; i++) {
3359
+ let childElem = children[i];
3360
+ switch (childElem.name) {
3361
+ case 'tspan':
3362
+ case 'textPath':
3363
+ case 'a':
3364
+ recursive(childElem, currentElem);
3365
+ break;
3366
+ case '#text':
3367
+ case '#cdata-section':
3368
+ let rawText = childElem.textContent,
3369
+ renderedText = rawText,
3370
+ words;
3371
+ childElem._font = currentElem._font;
3372
+ childElem._pos = [];
3373
+ remainingText = remainingText.substring(rawText.length);
3374
+ if (currentElem.get('xml:space') === 'preserve') {
3375
+ renderedText = renderedText.replace(/[\s]/g, ' ');
3376
+ } else {
3377
+ renderedText = renderedText.replace(/[\s]+/g, ' ');
3378
+ if (processedText.match(/[\s]$|^$/)) {
3379
+ renderedText = renderedText.replace(/^[\s]/, '');
3380
+ }
3381
+ if (remainingText.match(/^[\s]*$/)) {
3382
+ renderedText = renderedText.replace(/[\s]$/, '');
3383
+ }
3384
+ }
3385
+ processedText += rawText;
3386
+ if (wordSpacing === 0) {
3387
+ words = [renderedText];
3388
+ } else {
3389
+ words = renderedText.split(/(\s)/);
3390
+ }
3391
+ for (let w = 0; w < words.length; w++) {
3392
+ let pos = getTextPos(currentElem._font.font, currentElem._font.size, words[w]);
3393
+ for (let j = 0; j < pos.length; j++) {
3394
+ let index = currentElem._index,
3395
+ xAttr = currentElem._x[index],
3396
+ yAttr = currentElem._y[index],
3397
+ dxAttr = currentElem._dx[index],
3398
+ dyAttr = currentElem._dy[index],
3399
+ rotAttr = currentElem._rot[index],
3400
+ continuous = !(w === 0 && j === 0);
3401
+ if (letterSpacing !== 0) {
3402
+ continuous = false;
3403
+ }
3404
+ if (wordSpacing !== 0) {
3405
+ continuous = false;
3406
+ }
3407
+ if (xAttr !== undefined) {
3408
+ continuous = false;
3409
+ doAnchoring();
3410
+ currentX = xAttr;
3411
+ }
3412
+ if (yAttr !== undefined) {
3413
+ continuous = false;
3414
+ doAnchoring();
3415
+ currentY = yAttr;
3416
+ }
3417
+ if (dxAttr !== undefined) {
3418
+ continuous = false;
3419
+ currentX += dxAttr;
3420
+ }
3421
+ if (dyAttr !== undefined) {
3422
+ continuous = false;
3423
+ currentY += dyAttr;
3424
+ }
3425
+ if (rotAttr !== undefined || currentElem._defRot !== 0) {
3426
+ continuous = false;
3427
+ }
3428
+ let position = {
3429
+ glyph: pos[j].glyph,
3430
+ rotate: Math.PI / 180 * currentElem.chooseValue(rotAttr, currentElem._defRot),
3431
+ x: currentX + pos[j].xOffset,
3432
+ kern: pos[j].kern,
3433
+ y: currentY + baseline + pos[j].yOffset,
3434
+ width: pos[j].width,
3435
+ ascent: getAscent(currentElem._font.font, currentElem._font.size),
3436
+ descent: getDescent(currentElem._font.font, currentElem._font.size),
3437
+ scale: 1,
3438
+ hidden: false,
3439
+ continuous: continuous
3440
+ };
3441
+ currentChunk.push(position);
3442
+ childElem._pos.push(position);
3443
+ currentElem._pos.push(position);
3444
+ currentElem._index += pos[j].unicode.length;
3445
+ if (currentChunk.length === 1) {
3446
+ currentAnchor = textAnchor;
3447
+ currentDirection = textDirection;
3448
+ }
3449
+ currentX += pos[j].xAdvance + letterSpacing;
3450
+ currentY += pos[j].yAdvance;
3451
+ }
3452
+ if (words[w] === ' ') {
3453
+ currentX += wordSpacing;
3454
+ }
3455
+ }
3456
+ break;
3457
+ default:
3458
+ remainingText = remainingText.substring(childElem.textContent.length);
3459
+ }
3460
+ }
3461
+ if (textLength && currentElem._pos.length) {
3462
+ adjustLength(currentElem._pos, textLength, spacingAndGlyphs);
3463
+ }
3464
+ if (currentElem.name === 'textPath' || currentElem.name === 'text') {
3465
+ doAnchoring();
3466
+ }
3467
+ if (currentElem.name === 'textPath') {
3468
+ textPaths.push(currentElem);
3469
+ let pathObject = currentElem.pathObject;
3470
+ if (pathObject) {
3471
+ currentX = pathObject.endPoint[0];
3472
+ currentY = pathObject.endPoint[1];
3473
+ }
3474
+ }
3475
+ if (parentElem) {
3476
+ parentElem._pos = parentElem._pos.concat(currentElem._pos);
3477
+ parentElem._index += currentElem._index;
3478
+ }
3479
+ }
3480
+ function textOnPath(currentElem) {
3481
+ let pathObject = currentElem.pathObject,
3482
+ pathLength = currentElem.pathLength,
3483
+ pathScale = currentElem.pathScale;
3484
+ if (pathObject) {
3485
+ let textOffset = currentElem.getLength('startOffset', pathLength, 0);
3486
+ for (let j = 0; j < currentElem._pos.length; j++) {
3487
+ let charMidX = textOffset + currentElem._pos[j].x + 0.5 * currentElem._pos[j].width;
3488
+ if (charMidX > pathLength || charMidX < 0) {
3489
+ currentElem._pos[j].hidden = true;
3490
+ } else {
3491
+ let pointOnPath = pathObject.getPointAtLength(charMidX * pathScale);
3492
+ if (isNotEqual(pathScale, 1)) {
3493
+ currentElem._pos[j].scale *= pathScale;
3494
+ currentElem._pos[j].width *= pathScale;
3495
+ }
3496
+ currentElem._pos[j].x = pointOnPath[0] - 0.5 * currentElem._pos[j].width * Math.cos(pointOnPath[2]) - currentElem._pos[j].y * Math.sin(pointOnPath[2]);
3497
+ currentElem._pos[j].y = pointOnPath[1] - 0.5 * currentElem._pos[j].width * Math.sin(pointOnPath[2]) + currentElem._pos[j].y * Math.cos(pointOnPath[2]);
3498
+ currentElem._pos[j].rotate = pointOnPath[2] + currentElem._pos[j].rotate;
3499
+ currentElem._pos[j].continuous = false;
3500
+ }
3501
+ }
3502
+ } else {
3503
+ for (let j = 0; j < currentElem._pos.length; j++) {
3504
+ currentElem._pos[j].hidden = true;
3505
+ }
3506
+ }
3507
+ }
3508
+ recursive(textParentElem, null);
3509
+ for (let i = 0; i < textPaths.length; i++) {
3510
+ textOnPath(textPaths[i]);
3511
+ }
3512
+ })(this);
3513
+ this.getTransformation = function () {
3514
+ return this.get('transform');
3515
+ };
3516
+ this.drawInDocument = function (isClip, isMask) {
3517
+ doc.save();
3518
+ this.transform();
3519
+ this.clip();
3520
+ let masked = this.mask(),
3521
+ group;
3522
+ if (masked) {
3523
+ group = docBeginGroup(getPageBBox());
3524
+ }
3525
+ this.drawTextInDocument(isClip, isMask);
3526
+ if (group) {
3527
+ docEndGroup(group);
3528
+ docInsertGroup(group);
3529
+ }
3530
+ doc.restore();
3531
+ };
3532
+ };
3533
+ options = options || {};
3534
+ var pxToPt = options.assumePt ? 1 : 72 / 96,
3535
+ viewportWidth = (options.width || doc.page.width) / pxToPt,
3536
+ viewportHeight = (options.height || doc.page.height) / pxToPt,
3537
+ preserveAspectRatio = options.preserveAspectRatio || null,
3538
+ useCSS = options.useCSS && typeof SVGElement !== 'undefined' && svg instanceof SVGElement && typeof getComputedStyle === 'function',
3539
+ warningCallback = options.warningCallback,
3540
+ fontCallback = options.fontCallback,
3541
+ imageCallback = options.imageCallback,
3542
+ colorCallback = options.colorCallback,
3543
+ documentCallback = options.documentCallback,
3544
+ precision = Math.ceil(Math.max(1, options.precision)) || 3,
3545
+ groupStack = [],
3546
+ documentCache = {},
3547
+ links = [],
3548
+ styleRules = [],
3549
+ colorSpace = doc._activeColorProfile || 'DeviceRGB';
3550
+ if (typeof warningCallback !== 'function') {
3551
+ warningCallback = function (str) {
3552
+ if (typeof console !== undefined && typeof console.warn === 'function') {
3553
+ console.warn(str);
3554
+ }
3555
+ };
3556
+ }
3557
+ if (typeof fontCallback !== 'function') {
3558
+ fontCallback = function (family, bold, italic, fontOptions) {
3559
+ if (bold && italic) {
3560
+ if (doc._registeredFonts.hasOwnProperty(family + '-BoldItalic')) {
3561
+ return family + '-BoldItalic';
3562
+ } else if (doc._registeredFonts.hasOwnProperty(family + '-Italic')) {
3563
+ fontOptions.fauxBold = true;
3564
+ return family + '-Italic';
3565
+ } else if (doc._registeredFonts.hasOwnProperty(family + '-Bold')) {
3566
+ fontOptions.fauxItalic = true;
3567
+ return family + '-Bold';
3568
+ } else if (doc._registeredFonts.hasOwnProperty(family)) {
3569
+ fontOptions.fauxBold = true;
3570
+ fontOptions.fauxItalic = true;
3571
+ return family;
3572
+ }
3573
+ }
3574
+ if (bold && !italic) {
3575
+ if (doc._registeredFonts.hasOwnProperty(family + '-Bold')) {
3576
+ return family + '-Bold';
3577
+ } else if (doc._registeredFonts.hasOwnProperty(family)) {
3578
+ fontOptions.fauxBold = true;
3579
+ return family;
3580
+ }
3581
+ }
3582
+ if (!bold && italic) {
3583
+ if (doc._registeredFonts.hasOwnProperty(family + '-Italic')) {
3584
+ return family + '-Italic';
3585
+ } else if (doc._registeredFonts.hasOwnProperty(family)) {
3586
+ fontOptions.fauxItalic = true;
3587
+ return family;
3588
+ }
3589
+ }
3590
+ if (!bold && !italic) {
3591
+ if (doc._registeredFonts.hasOwnProperty(family)) {
3592
+ return family;
3593
+ }
3594
+ }
3595
+ if (family.match(/(?:^|,)\s*serif\s*$/)) {
3596
+ if (bold && italic) {
3597
+ return 'Times-BoldItalic';
3598
+ }
3599
+ if (bold && !italic) {
3600
+ return 'Times-Bold';
3601
+ }
3602
+ if (!bold && italic) {
3603
+ return 'Times-Italic';
3604
+ }
3605
+ if (!bold && !italic) {
3606
+ return 'Times-Roman';
3607
+ }
3608
+ } else if (family.match(/(?:^|,)\s*monospace\s*$/)) {
3609
+ if (bold && italic) {
3610
+ return 'Courier-BoldOblique';
3611
+ }
3612
+ if (bold && !italic) {
3613
+ return 'Courier-Bold';
3614
+ }
3615
+ if (!bold && italic) {
3616
+ return 'Courier-Oblique';
3617
+ }
3618
+ if (!bold && !italic) {
3619
+ return 'Courier';
3620
+ }
3621
+ } else if (family.match(/(?:^|,)\s*sans-serif\s*$/) || true) {
3622
+ if (bold && italic) {
3623
+ return 'Helvetica-BoldOblique';
3624
+ }
3625
+ if (bold && !italic) {
3626
+ return 'Helvetica-Bold';
3627
+ }
3628
+ if (!bold && italic) {
3629
+ return 'Helvetica-Oblique';
3630
+ }
3631
+ if (!bold && !italic) {
3632
+ return 'Helvetica';
3633
+ }
3634
+ }
3635
+ };
3636
+ }
3637
+ if (typeof imageCallback !== 'function') {
3638
+ imageCallback = function (link) {
3639
+ return {
3640
+ src: link.trim()
3641
+ };
3642
+ };
3643
+ }
3644
+ if (typeof colorCallback !== 'function') {
3645
+ colorCallback = null;
3646
+ } else {
3647
+ for (let color in DefaultColors) {
3648
+ let newColor = colorCallback(DefaultColors[color]);
3649
+ DefaultColors[color][0] = newColor[0];
3650
+ DefaultColors[color][1] = newColor[1];
3651
+ }
3652
+ }
3653
+ if (typeof documentCallback !== 'function') {
3654
+ documentCallback = null;
3655
+ }
3656
+ if (typeof svg === 'string') {
3657
+ svg = parseXml(svg);
3658
+ }
3659
+ if (svg) {
3660
+ let styles = svg.getElementsByTagName('style');
3661
+ for (let i = 0; i < styles.length; i++) {
3662
+ styleRules = styleRules.concat(parseStyleSheet(styles[i].textContent));
3663
+ }
3664
+ let elem = createSVGElement(svg, null);
3665
+ if (typeof elem.drawInDocument === 'function') {
3666
+ if (options.useCSS && !useCSS) {
3667
+ warningCallback('SVGtoPDF: useCSS option can only be used for SVG *elements* in compatible browsers');
3668
+ }
3669
+ let savedFillColor = doc._fillColor;
3670
+ doc.save().translate(x || 0, y || 0).scale(pxToPt);
3671
+ elem.drawInDocument();
3672
+ for (let i = 0; i < links.length; i++) {
3673
+ doc.page.annotations.push(links[i]);
3674
+ }
3675
+ doc.restore();
3676
+ doc._fillColor = savedFillColor;
3677
+ } else {
3678
+ warningCallback("SVGtoPDF: this element can't be rendered directly: " + svg.nodeName);
3679
+ }
3680
+ } else {
3681
+ warningCallback('SVGtoPDF: the input does not look like a valid SVG');
3682
+ }
3683
+ }
3684
+
3685
+ module.exports = write_svg;
3686
+ //# sourceMappingURL=writeSvg.js.map