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