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