forked from ahwolf/jsPDF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjspdf.plugin.svgToPdf.js
669 lines (608 loc) · 27.4 KB
/
jspdf.plugin.svgToPdf.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
/*
* svgToPdf.js
*
* Copyright 2012 Florian Hülsmann <[email protected]>
* Updated in 2013 by Datascope Analytics <[email protected]>
* This script is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this file. If not, see <http://www.gnu.org/licenses/>.
*
*/
// These are all of the currently supported attributes of the svg elements
// that we are converting to PDF. The path is empty and taken care of in a
// different way below.
var pdfSvgAttr = {
// allowed attributes. all others are removed from the preview.
g: ['stroke', 'fill', 'stroke-width'],
line: ['x1', 'y1', 'x2', 'y2', 'stroke', 'stroke-width', 'fill'],
rect: ['x', 'y', 'width', 'height', 'stroke', 'fill', 'stroke-width'],
ellipse: ['cx', 'cy', 'rx', 'ry', 'stroke', 'fill', 'stroke-width'],
circle: ['cx', 'cy', 'r', 'stroke', 'fill', 'stroke-width'],
text: ['x', 'y', 'font-size', 'font-family', 'text-anchor', 'font-weight', 'font-style', 'fill'],
path: []
};
var svgElementToPdf = function(element, pdf, options) {
// pdf is a jsPDF object
var remove = (typeof(options.removeInvalid) == 'undefined' ? remove = false : options.removeInvalid);
var k = (typeof(options.removeInvalid) == 'undefined' ? 1.0 : options.scale);
var x_offset = (typeof(options.x_offset) == 'undefined' ? 0 : options.x_offset);
var y_offset = (typeof(options.y_offset) == 'undefined' ? 0 : options.y_offset);
var ego_or_link = (typeof(options.ego_or_link) == 'undefined' ? false : options.ego_or_link);
var colorMode = null;
$(element).children().each(function(i, node) {
var n = $(node);
if (n.is("g")) {
if (n.attr('transform') !== undefined) {
var matrix = n.attr('transform').replace(/,/g, ' ').replace('matrix(', '').replace(')', ' cm');
pdf.internal.write('q');
pdf.internal.write(matrix);
svgElementToPdf(n, pdf, options);
pdf.internal.write('Q');
} else {
svgElementToPdf(n, pdf, options);
}
}
var hasFillColor = false;
var hasStrokeColor = false;
if (n.is('g,line,rect,ellipse,circle,text,path')) {
var fillColor = n.attr('fill');
if (typeof(fillColor) != 'undefined') {
var fillRGB = new RGBColor(fillColor);
if (fillRGB.ok) {
//console.log("fillRGB is okay");
hasFillColor = true;
colorMode = 'F';
} else {
colorMode = null;
}
}
}
if (hasFillColor) {
pdf.setFillColor(fillRGB.r, fillRGB.g, fillRGB.b);
}
var strokeColor = n.attr('stroke');
if (typeof(strokeColor) != 'undefined' && strokeColor !== 'none') {
var strokeRGB = new RGBColor(strokeColor);
if (strokeRGB.ok) {
hasStrokeColor = true;
pdf.setDrawColor(strokeRGB.r, strokeRGB.g, strokeRGB.b);
if (colorMode == 'F') {
colorMode = 'FD';
} else {
colorMode = "S";
}
} else {
colorMode = null;
}
}
switch (n.get(0).tagName.toLowerCase()) {
case 'line':
pdf.line(
k * parseInt(n.attr('x1')),
k * parseInt(n.attr('y1')),
k * parseInt(n.attr('x2')),
k * parseInt(n.attr('y2')));
// $.each(node.attributes, function(i, a) {
// if(typeof(a) != 'undefined' && pdfSvgAttr.line.indexOf(a.name.toLowerCase()) == -1) {
// node.removeAttribute(a.name);
// }
// });
break;
case 'rect':
//console.log("in rectangle");
pdf.rect(
k * (parseInt(n.attr('x')) + x_offset),
k * (parseInt(n.attr('y')) + y_offset),
k * parseInt(n.attr('width')),
k * parseInt(n.attr('height')),
colorMode);
// $.each(node.attributes, function(i, a) {
// if(typeof(a) != 'undefined' && pdfSvgAttr.rect.indexOf(a.name.toLowerCase()) == -1) {
// node.removeAttribute(a.name);
// }
// });
break;
case 'ellipse':
pdf.ellipse(
k * parseInt(n.attr('cx')),
k * parseInt(n.attr('cy')),
k * parseInt(n.attr('rx')),
k * parseInt(n.attr('ry')),
colorMode);
// $.each(node.attributes, function(i, a) {
// if(typeof(a) != 'undefined' && pdfSvgAttr.ellipse.indexOf(a.name.toLowerCase()) == -1) {
// node.removeAttribute(a.name);
// }
// });
break;
case 'circle':
pdf.circle(
k * (parseInt(n.attr('cx')) + x_offset),
k * (parseInt(n.attr('cy')) + y_offset),
k * parseInt(n.attr('r')),
colorMode);
// $.each(node.attributes, function(i, a) {
// if(typeof(a) != 'undefined' && pdfSvgAttr.circle.indexOf(a.name.toLowerCase()) == -1) {
// node.removeAttribute(a.name);
// }
// });
break;
case 'text':
var fontFamily = 'helvetica';
if (node.hasAttribute('font-family')) {
switch (n.attr('font-family').toLowerCase().split(',')[0]) {
case 'times':
var fontFamily = 'times';
pdf.setFont('times');
break;
case 'courier':
var fontFamily = 'courier';
pdf.setFont('courier');
break;
default:
pdf.setFont('helvetica');
}
}
// If no font family, we set the font to the times by default
else {
pdf.setFont('helvetica');
}
// if (colorMode !== "FD" || colorMode !== 'F') {
// console.log("I'm setting text color");
// pdf.setTextColor(fillRGB.r, fillRGB.g, fillRGB.b);
// }
var fontType = "normal";
if (node.hasAttribute('font-weight')) {
if (n.attr('font-weight') == "bold") {
fontType = "bold";
} else {
node.removeAttribute('font-weight');
}
}
if (node.hasAttribute('font-style')) {
if (n.attr('font-style') == "italic") {
fontType += "italic";
} else {
node.removeAttribute('font-style');
}
}
pdf.setFontType(fontType);
var pdfFontSize = 9;
if (node.hasAttribute('font-size')) {
pdfFontSize = parseInt(n.attr('font-size'));
}
var fontMetrics = pdf.internal.getFont(fontFamily, fontType)
.metadata.Unicode;
var text_value = n.text();
var name_length = pdf.getStringUnitWidth(
text_value,
fontMetrics) * pdfFontSize;
//FIXME: use more accurate positioning!!
var label_offset = 0;
var x = parseInt(n.attr('x')) + x_offset - label_offset;
var y = parseInt(n.attr('y')) + y_offset;
if (node.hasAttribute('text-anchor')) {
switch (n.attr('text-anchor')) {
case 'end':
break;
case 'middle':
label_offset = name_length / 2;
break;
case 'start':
label_offset = 0;
break;
case 'default':
label_offset = 0;
break;
}
x = parseInt(n.attr('x')) + x_offset - label_offset;
y = parseInt(n.attr('y')) + y_offset;
}
pdf.setFontSize(pdfFontSize).text(
k * x,
k * y,
text_value);
// $.each(node.attributes, function(i, a) {
// if(typeof(a) != 'undefined' && pdfSvgAttr.text.indexOf(a.name.toLowerCase()) == -1) {
// node.removeAttribute(a.name);
// }
// });
break;
case 'path':
// //console.log("node is: ",node.attributes)
// $.each(node.attributes, function(i, a) {
// //console.log(pdfSvgAttr.path.indexOf(a.name.toLowerCase()));
// });
var updated_path = n.attr('d');
// Separate the svg 'd' string to a list of letter
// and number elements. Iterate on this list.
// console.log('path before',path);
svg_regex = /(m|l|h|v|c|s|a|z)/gi;
// console.log('TESTING REGULAR
// EXPRESSION',svg_regex.test('m')); The crazy ie9 case
// where they take our path and make some crazy scientific
// notation
updated_path = updated_path.replace(/(e)?-/g, function($0, $1) {
return $1 ? $0 : ' -';
})
// .replace(/-/g, ' -')
.replace(svg_regex, ' $1 ')
.replace(/,+/g, ' ')
.replace(/^\s+|\s+$/g, '')
.split(/[\s]+/);
var svg_element = null;
var i = 0;
// mx and my will define the starting points from each m/M case
var mx = null;
var my = null;
// x and y will redefine the starting points
var x = null;
var y = null;
// big list contains the large list of lists to pass to
// jspdf to render the appropriate path
var big_list = [];
// for S/s shorthand bezier calculations of 2nd control pts
var previous_element = {
element: null,
prev_numbers: [],
point: []
};
var m_flag = 0;
// Go through our list until we are done with the updated_path
while (i < updated_path.length) {
// Numbers will hold the list of numbers for the
// appropriate updated_path
var numbers = [];
// svg_element is a letter corresponding to the type
// of updated_path to draw
var sci_regex = /[+\-]?(?:0|[1-9]\d*)(?:\.\d*)?(?:[eE][+\-]?\d+)?/;
var svg_element = updated_path[i];
if (sci_regex.test(svg_element)) {
svg_element = String(Number(svg_element));
}
i++
//if svg_element is s/S, need to find 1st control pts
if (/s/i.test(svg_element)) {
previous_element.point = find_s_points(svg_element,
previous_element);
}
// for some reason z followed by another letter
// i.e. 'z m' skips that 2nd letter, so added if
// statement to get around that.
if (/z/i.test(svg_element) == false) {
// Parse through the updated_path until we find the next
// letter or we are at the end of the updated_path
while ((svg_regex.test(updated_path[i]) == false) && (i != updated_path.length)) {
numbers.push(k * parseFloat(updated_path[i]));
i++;
}
}
switch (svg_element) {
case 'm':
//paths and subpaths must always start with m/M.
//thus we call pdf.lines
if (big_list.length != 0) {
pdf.lines(big_list, mx, my, [1, 1], null);
}
big_list = [];
// check if this is 1st command in the path
if (previous_element.element == null) {
x = numbers[0];
mx = numbers[0];
y = numbers[1];
my = numbers[1];
} else {
x += numbers[0];
mx += numbers[0];
y += numbers[1];
my += numbers[1];
}
if (numbers.length != 2) {
var lines_numbers = numbers.slice(2, numbers.length);
var new_numbers = change_numbers(lines_numbers,
x, y, true);
_.each(new_numbers, function(num) {
big_list.push(num);
});
// pdf.lines(new_numbers,x,y,[1,1],null);
x += sums(new_numbers, true);
y += sums(new_numbers, false);
}
break;
case 'M':
if (big_list.length != 0) {
pdf.lines(big_list, mx, my, [1, 1], null);
}
big_list = [];
if (previous_element == null) {
x = numbers[0];
mx = numbers[0];
y = numbers[1];
my = numbers[1];
}
if (numbers.length != 2) {
x = numbers[0];
y = numbers[1];
var lines_numbers = numbers.slice(2, numbers.length);
var new_numbers = change_numbers(lines_numbers,
x, y, false);
pdf.lines(new_numbers, x, y, [1, 1], null);
x += new_numbers[new_numbers.length - 1][0];
y += new_numbers[new_numbers.length - 1][1];
}
break;
case 'l':
var new_numbers = change_numbers(numbers, x, y, true);
_.each(new_numbers, function(num) {
big_list.push(num);
});
// pdf.lines(new_numbers,x,y,[1,1],null);
x += sums(new_numbers, true);
y += sums(new_numbers, false);
break;
case 'L':
var new_numbers = change_numbers(numbers, x, y, false);
_.each(new_numbers, function(num) {
big_list.push(num);
});
//pdf.lines(new_numbers,x,y,[1,1],null);
x += new_numbers[new_numbers.length - 1][0];
y += new_numbers[new_numbers.length - 1][1];
break;
case 'h':
// x does not change. Only y changes
var sum = _.reduce(numbers,
function(memo, num) {
return memo + num;
}, 0);
var new_numbers = [
[sum, 0]
];
_.each(new_numbers, function(num) {
big_list.push(num);
});
// pdf.lines([[sum,0]],x,y,[1,1],null);
x += sum;
break;
case 'H':
big_list.push([numbers[numbers.length - 1] - x, 0]);
// pdf.lines([[numbers[numbers.length-1]-x,0]],
// x,y,[1,1],null);
x = numbers[numbers.length - 1];
break;
case 'v':
var sum = _.reduce(numbers,
function(memo, num) {
return memo + num;
}, 0);
var new_numbers = [
[0, sum]
];
_.each(new_numbers, function(num) {
big_list.push(num);
});
// pdf.lines([[0,sum]],x,y,[1,1],null);
y += sum;
break;
case 'V':
big_list.push([0, numbers[numbers.length - 1] - y]);
// pdf.lines([[0,numbers[numbers.length-1]-y]],
// x,y,[1,1],null);
y = numbers[numbers.length - 1];
break;
case 'c':
var new_numbers = bezier_numbers(numbers, x, y, true);
_.each(new_numbers, function(num) {
big_list.push(num);
});
// pdf.lines(new_numbers,x,y,[1,1],null);
x += sums(new_numbers, true);
y += sums(new_numbers, false);
break;
case 'C':
var new_numbers = bezier_numbers(numbers, x, y, false);
_.each(new_numbers, function(num) {
big_list.push(num);
});
// pdf.lines(new_numbers,x,y,[1,1],null);
x = numbers[numbers.length - 2];
y = numbers[numbers.length - 1];
break;
case 's':
var new_numbers = s_bezier_numbers(numbers, x, y,
true, previous_element);
_.each(new_numbers, function(num) {
big_list.push(num);
});
// pdf.lines(new_numbers,x,y,[1,1],null);
x += sums(new_numbers, true);
y += sums(new_numbers, false);
break;
case 'S':
var new_numbers = s_bezier_numbers(numbers, x, y, false,
previous_element);
_.each(new_numbers, function(num) {
big_list.push(num);
});
// pdf.lines(new_numbers,x,y,[1,1],null);
x = numbers[numbers.length - 2];
y = numbers[numbers.length - 1];
break;
case 'A':
// for now a hack.treat this as a line
break;
case 'a':
break;
case 'z':
big_list.push([mx - x, my - y]);
x = mx;
y = my;
// pdf.lines([[mx-x,my-y]],x,y,[1,1],null);
break;
case 'Z':
big_list.push([mx - x, my - y]);
x = mx;
y = my;
// pdf.lines([[mx-x,my-y]],x,y,[1,1],null);
break;
default:
console.log('Sorry, the', svg_element, 'svg command is not yet available.');
}
previous_element.element = svg_element;
previous_element.prev_numbers = numbers;
}
pdf.lines(big_list, mx, my, [1, 1], colorMode);
var numbs = null;
break;
//TODO: image
default:
//console.log("cannot identify: ", node);
if (remove) {
//console.log("can't translate to pdf:", node);
n.remove();
}
}
});
return pdf;
}
function sums(ListOfLists, is_x) {
if (is_x) {
var sum = _.reduce(ListOfLists,
function(memo, num) {
return memo + num[num.length - 2];
}, 0);
} else {
var sum = _.reduce(ListOfLists,
function(memo, num) {
return memo + num[num.length - 1];
}, 0);
}
return sum;
}
function change_numbers(numbers, x, y, relative) {
var i = 0;
var prev_x = x;
var prev_y = y;
var new_numbers = [];
while (i < numbers.length) {
if (relative) {
x = numbers[i];
y = numbers[i + 1];
} else {
x = numbers[i] - prev_x;
y = numbers[i + 1] - prev_y;
}
prev_x = numbers[i];
prev_y = numbers[i + 1];
new_numbers.push([x, y]);
i += 2;
}
return new_numbers;
}
function bezier_numbers(numbers, x, y, relative) {
// the bezier numbers are ALL relative to the
// previous case line's (x,y), not all relative to
// each other.
var i = 0;
var prev_x = x;
var prev_y = y;
var new_numbers = [];
while (i < numbers.length) {
if (relative) {
var numbers_to_push = numbers.slice(i, i + 6);
} else {
var numbers_to_push = [];
for (var k = i; k < i + 6; k = k + 2) {
numbers_to_push.push(
numbers[k] - prev_x,
numbers[k + 1] - prev_y);
}
}
prev_x = numbers[i + 4];
prev_y = numbers[i + 5];
new_numbers.push(numbers_to_push);
i += 6;
}
return new_numbers;
}
function s_bezier_numbers(numbers, x, y, relative,
previous_element) {
var i = 0;
var prev_x = x;
var prev_y = y;
var new_numbers = [];
while (i < numbers.length) {
var numbers_to_push = [];
//need to check if relative for the 4 s/S numbers
if (relative) {
//find that 1st control point
if (i < 4 && (previous_element.element == 'c' || previous_element.element == 's')) {
// case 1: there was a prev c/C/s/S
// outside this numbers segment
numbers_to_push.push(previous_element.point[0],
previous_element.point[1]);
} else if (i >= 4) {
//case 1: there was a prev s/S
//within this numbers segment
numbers_to_push.push(numbers[i - 2] - numbers[i - 4],
numbers[i - 1] - numbers[i - 3]);
} else {
// case 2: no prev c/C/s/S, therefore
// 1st control pt = current pt.
numbers_to_push.push(prev_x, prev_y);
}
//then add the rest of the s numbers
for (var k = i; k < i + 4; k++) {
numbers_to_push.push(numbers[k]);
}
} else {
//find that 1st control point
if (i < 4 && (previous_element.element == 'C' || previous_element.element == 'S')) {
// case 1: there was a prev c/C/s/S
// outside this numbers segment
numbers_to_push.push(previous_element.point[0] - prev_x,
previous_element.point[1] - prev_y);
} else if (i >= 4) {
//case 1: there was a prev s/S
//within this numbers segment
numbers_to_push.push(numbers[i - 2] + numbers[i - 2] - numbers[i - 4],
numbers[i - 1] + numbers[i - 1] - numbers[i - 3]);
} else {
// case 2: no prev c/C/s/S, therefore
// 1st control pt = current pt.
numbers_to_push.push(prev_x, prev_y);
}
//then add the rest of the s numbers
for (var k = i; k < i + 4; k = k + 2) {
numbers_to_push.push(numbers[k] - prev_x);
numbers_to_push.push(numbers[k + 1] - prev_y);
}
}
prev_x = numbers[i + 2];
prev_y = numbers[i + 3];
new_numbers.push(numbers_to_push);
i += 4;
}
return new_numbers;
}
function find_s_points(svg_element, previous_element) {
if (/(s|c)/.test(previous_element.element)) {
numbs = previous_element.prev_numbers;
previous_element.point = [numbs[numbs.length - 2] - numbs[numbs.length - 4],
numbs[numbs.length - 1] - numbs[numbs.length - 3]];
} else if (/(S|C)/.test(previous_element.element)) {
numbs = previous_element.prev_numbers;
previous_element.point = [2 * numbs[numbs.length - 2] - numbs[numbs.length - 4],
2 * numbs[numbs.length - 1] - numbs[numbs.length - 3]];
}
return previous_element.point
}