@kittl/pdfkit 0.17.3 → 0.17.5

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