-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathindex.js
911 lines (857 loc) Β· 30.8 KB
/
index.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
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
const path = require('path');
const Promise = require('bluebird');
const puppeteer = require('puppeteer');
const _ = require('lodash');
const Jimp = require('jimp');
const SVGO = require('svgo');
const addText = require('./addText');
const maxSize = 16000; //original SVG files should be up to this size
const debugInfo = function() {
if (process.env.DEBUG_SVG) {
console.info.apply(this, arguments);
}
}
const makeHelpers = function(root) {
return function(x) {
x.isElem = root.isElem.bind(x);
x.isEmpty = root.isEmpty.bind(x);
x.hasAttr = root.hasAttr.bind(x);
x.attr = root.attr.bind(x);
x.eachAttr = root.eachAttr.bind(x);
return x;
}
}
async function svgo({content, title}) {
let rootStyle = '';
let result;
result = await (new SVGO({
full: true,
plugins: [{
removeDoctype: true,
},{
removeXMLProcInst: true,
},{
removeComments: true,
},{
removeMetadata: true,
},{
removeTitle: true,
},{
removeDesc: true,
},{
removeStyleFromRoot: {
type: 'full',
fn: function(data) {
const root = data.content[0];
const addHelpers = makeHelpers(root);
if (root.attrs && root.attrs.style) {
rootStyle = `svg {${root.attrs.style.value}}`;
}
else if (root.content[0].elem === 'style' && root.content[0].content[0].text.indexOf('svg {' === 0)) {
rootStyle = root.content[0].content[0].text;
}
return data;
}
}
}
]
})).optimize(content);
if (process.env.DEBUG_SVG) {
require('fs').writeFileSync('/tmp/s1.svg', result.data);
}
let rootUpdated = false;
result = await (new SVGO({
plugins: [{
cleanupAttrs: true,
}, {
inlineStyles: true
}, {
removeDoctype: true,
},{
removeXMLProcInst: true,
},{
removeComments: true,
},{
removeMetadata: true,
},{
removeTitle: true,
},{
removeDesc: true,
},{
removeUselessDefs: true,
},{
removeEditorsNSData: true,
},{
removeEmptyAttrs: true,
},{
removeHiddenElems: true,
},{
removeEmptyText: true,
},{
removeEmptyContainers: true,
},{
removeDimensions: true,
},{
cleanupEnableBackground: true,
},{
minifyStyles: true,
},{
convertStyleToAttrs: true,
},{
convertColors: true,
},{
convertPathData: false,
},{
convertTransform: true,
},{
removeUnknownsAndDefaults: true,
},{
removeNonInheritableGroupAttrs: true,
},{
removeUselessStrokeAndFill: true,
},{
removeUnusedNS: true,
},{
cleanupIDs: false,
},{
cleanupNumericValues: true,
},{
cleanupListOfValues: true,
},{
moveElemsAttrsToGroup: true,
},{
moveGroupAttrsToElems: true,
},{
collapseGroups: false,
},{
removeRasterImages: false,
},{
mergePaths: {noSpaceAfterFlags: false },
},{
convertShapeToPath: true,
},{
sortAttrs: true,
},{
removeDimensions: true,
}, {
removeScriptElements: true
}, {
removeAttrs: {
attrs: ['aria.*', 'font.*', '-inkspace.*', 'line.*', 'font', 'letter.*', 'word.*', 'direction', 'white.*']
}
}, {
removeTextStyles: {
type: 'perItem',
fn: function(item) {
if (item.hasAttr('style')) {
var elemStyle = item.attr('style').value;
const elemParts = elemStyle.split(';').map( (x) => x.trim());
const goodParts = elemParts.filter(function(part) {
const [name, value] = part.split(':').map( (x) => x.trim());
// console.info('Propery:',name);
if (name.indexOf('-inkscape') === 0 ) {
return false;
}
if (name.indexOf('line-') === 0) {
return false;
}
if (name.indexOf('font') === 0) {
return false;
}
if (name.indexOf('letter') === 0) {
return false;
}
if (name.indexOf('word') === 0) {
return false;
}
if (name.indexOf('direction') === 0) {
return false;
}
if (name.indexOf('white') === 0) {
return false;
}
return true;
});
if (goodParts.length > 0) {
item.attr('style').value = goodParts.join(';');
} else {
item.removeAttr('style');
}
}
}
}
}, {
cleanupSvgDeclaration: {
type: 'perItem',
fn: function(item) {
if (item.elem === 'svg') {
if (!rootUpdated) {
rootUpdated = true;
const xmlns = item.attrs.xmlns;
const xmlnsxlink = item.attrs['xmlns:xlink'];
item.attrs = { xmlns: xmlns, role: {name: 'role', value: 'img', local: 'role', prefix: ''}};
if (xmlnsxlink) {
item.attrs['xmlns:xlink'] = xmlnsxlink;
}
} else {
throw new Error('We do not support SVG inside of SVG because that format is unlikely to reproduce reliably on different devices.');
}
}
}
}
}]
})).optimize(result.data);
if (process.env.DEBUG_SVG) {
require('fs').writeFileSync('/tmp/s2.svg', result.data);
}
if (rootStyle) {
result = await (new SVGO({
full: true,
plugins: [{
addRootStyle: {
type: 'full',
fn: function(data) {
const root = data.content[0];
const addHelpers = makeHelpers(root);
const styleElem = addHelpers({
elem: 'style',
prefix: '',
local: 'style',
content: [addHelpers({text:rootStyle})]
})
root.content = [styleElem].concat(root.content);
return data;
}
}
}]
})).optimize(result.data);
}
if (process.env.DEBUG_SVG) {
require('fs').writeFileSync('/tmp/s3.svg', result.data);
}
if (title) {
result = await (new SVGO({
full: true,
plugins: [{
insertTitle: {
type: 'full',
fn: function(data) {
const root = data.content[0];
const addHelpers = makeHelpers(root);
root.content = [addHelpers({
elem: 'title',
prefix: '',
local: 'title',
content: [addHelpers({text: title})]
})].concat(root.content);
return data;
}
}
}]
})).optimize(result.data);
}
if (process.env.DEBUG_SVG) {
require('fs').writeFileSync('/tmp/s4.svg', result.data);
}
return result.data;
}
async function extraTransform(svg) {
result = await (new SVGO({
full: true,
plugins: [{
cleanupIDs: true
}, {
collapseGroups: true,
}, {
convertPathData: {
noSpaceAfterFlags: false,
floatPrecision: 5,
transformPrecision: 7
}
}]
})).optimize(svg);
return result.data;
}
async function updateViewbox(content, {x, y, width, height}) {
const newValue = `${x.toFixed(2)} ${y.toFixed(2)} ${width.toFixed(2)} ${height.toFixed(2)}`;
const svgo = new SVGO({
full: true,
plugins: [{
updateViewbox: {
type: 'full',
fn: function(data) {
const root = data.content[0];
root.attrs.viewBox = {
name: 'viewBox',
local: 'viewBox',
prefix: '',
value: newValue
};
return data;
}
}
}]
});
const result = await svgo.optimize(content);
return result.data;
}
async function getCropRegionWithWhiteBackgroundDetection({svg, image, allowScaling}) {
const newViewbox = await getCropRegion(image);
let totalBorderPixels = 0;
let whiteBorderPixels = 0;
let totalPixelsInside = 0;
let transparentPixelsInside = 0;
let nonWhiteOrNonTransparent = 0;
debugInfo('292', newViewbox);
for (let x = newViewbox.x; x <= newViewbox.x + newViewbox.width; x += 1) {
for (let y = newViewbox.y; y <= newViewbox.y + newViewbox.height; y += 1) {
const r = image.bitmap.data[ (y * image.bitmap.width + x) * 4 + 0];
const g = image.bitmap.data[ (y * image.bitmap.width + x) * 4 + 1];
const b = image.bitmap.data[ (y * image.bitmap.width + x) * 4 + 2];
const a = image.bitmap.data[ (y * image.bitmap.width + x) * 4 + 3];
const isBorderPixel = x === newViewbox.x || x === newViewbox.x + newViewbox.width || y === newViewbox.y || y === newViewbox.y + newViewbox.height;
const isWhiteBorderPixel = (isBorderPixel && r >= 250 && g >= 250 && b >= 250 );
const isTransparentPixel = a === 0;
if (isBorderPixel) {
totalBorderPixels += 1;
}
if (isWhiteBorderPixel) {
whiteBorderPixels += 1
}
totalPixelsInside += 1;
if (isTransparentPixel) {
transparentPixelsInside += 1;
}
}
}
debugInfo({totalBorderPixels, whiteBorderPixels, allowScaling});
if (totalBorderPixels < 400 && allowScaling) {
debugInfo(totalBorderPixels, 'Too few border pixels on estimate - not possible to detect a white background, scaling the image again');
return newViewbox;
}
var borderRatio = totalBorderPixels ? whiteBorderPixels / totalBorderPixels : 0;
var transparentRatio = totalPixelsInside ? transparentPixelsInside / totalPixelsInside : 0;
debugInfo(borderRatio, transparentRatio);
if (borderRatio > 0.99 && transparentRatio < 0.01) {
debugInfo('Converting image to transparent');
await whiteToTransparent(image);
try {
const result = await getCropRegion(image);
debugInfo('Diff in results: ', newViewbox, result);
return result;
} catch(ex) {
debugInfo('Can not return a transparent image, using an original one');
debugInfo(newViewbox);
return newViewbox;
}
} else {
return newViewbox;
}
}
async function getCropRegion(image) {
let top, left, right, bottom;
// pixels go in pack of 4 bytes in the R G B A order
// thus a pixel has an offset of 4 * (y * width + x)
// and an alpha channel is at the last position, that a + 3 offset
// scan from top to bottom, left to right till we find a non transparent pixel
for (var y = 0; y < image.bitmap.height; y++) {
for (var x = 0; x < image.bitmap.width; x++) {
const idx = (image.bitmap.width * y + x) * 4;
const alpha = image.bitmap.data[idx + 3];
if (alpha !== 0) {
top = y;
break;
}
}
if (top) {
break;
}
}
// scan from bottom to top, left to right till we find a non transparent pixel
for (var y = image.bitmap.height - 1; y >= 0; y--) {
for (var x = 0; x < image.bitmap.width; x++) {
const idx = (image.bitmap.width * y + x) * 4;
const alpha = image.bitmap.data[idx + 3];
if (alpha !== 0) {
bottom = y;
break;
}
}
if (bottom) {
break;
}
}
// scan from left to right, top to bottom till we find a non transparent pixel
for (var x = 0; x < image.bitmap.width; x++) {
for (var y = 0; y < image.bitmap.height; y++) {
const idx = (image.bitmap.width * y + x) * 4;
const alpha = image.bitmap.data[idx + 3];
if (alpha !== 0) {
left = x;
break;
}
}
if (left) {
break;
}
}
// scan from right to left, top to bottom till we find a non transparent pixel
for (var x = image.bitmap.width - 1; x >= 0; x--) {
for (var y = 0; y < image.bitmap.height; y++) {
const idx = (image.bitmap.width * y + x) * 4;
const alpha = image.bitmap.data[idx + 3];
if (alpha !== 0) {
right = x;
break;
}
}
if (right) {
break;
}
}
debugInfo('Crop region result: ', {left, top, right, bottom});
if (!_.isNumber(left) || !_.isNumber(top) || !_.isNumber(right) || !_.isNumber(bottom)) {
throw new Error('SVG image has dimension more than 4000x4000, we do not support SVG images of this size or larger');
}
// add a 1 pixel border around
// console.info(left, top, right - left, bottom - top);
const newViewbox = { x: left, y: top, width: right - left, height: bottom - top };
return newViewbox;
}
// this method works really fast because it gets a scaled png version for a
// given svg file. So if an svg file is 4000x4000, than a 0.1 scaled version
// is just 400x400 and thus way faster to process
// the obvious side effect that we have a +- (1 / scale) error
let browser;
const closeBrowser = async function() {
try {
await browser.close();
} catch (ex) {
}
browser = null;
return;
}
async function convert({svg, width, height, scale = 1 }) {
let start = svg.indexOf('<svg');
let html = `<!DOCTYPE html>
<style>
* { margin: 0; padding: 0; }
html { background-color: transparent; overflow: hidden; }
</style>`;
if (start >= 0) {
html += svg.substring(start);
} else {
throw new Error('SVG element open tag not found in input. Check the SVG input');
}
const fileName = `/tmp/convert-svg-${Math.random()}.html`;
require('fs').writeFileSync(fileName, html);
browser = browser || await puppeteer.launch({args: ['--no-sandbox', '--disable-setuid-sandbox', '--disable-dev-shm-usage']});
const page = await browser.newPage();
const url = `file://${fileName}`;
await page.goto(url);
// await new Promise(function(){});
const totalWidth = width * scale;
const totalHeight = height * scale;
// await page.waitForSelector('svg');
// await page.evaluate(`
// const el = document.querySelector('svg');
// el.setAttribute('width', '${totalWidth}px');
// el.setAttribute('height', '${totalHeight}px');
// `);
debugInfo(`ViewportSize: ${totalWidth} x ${totalHeight}`);
await page.setViewport({ width: Math.round(totalWidth), height: Math.round(totalHeight) });
debugInfo(`Started a screenshot`);
// A workaround for a known bug: https://github.com/puppeteer/puppeteer/issues/4273
// the page.screenshot may hang because of random reasons
// the solution is to retry with a new browser instance
const output = await new Promise(function(resolve, reject) {
let resolved = false;
page.screenshot({
type: 'png',
omitBackground: true,
clip: { x: 0, y: 0, width: totalWidth, height: totalHeight }
}).then( function(result) { resolved = true; resolve(result) }).catch(function(ex) {
reject(new Error(`Screenshot Failure: ${ex.message}`));
});
setTimeout(function() {
if (!resolved) {
reject(new Error('Timeout'));
}
}, 10 * 1000);
});
debugInfo(`Finished a screenshot`);
require('fs').unlinkSync(fileName);
// await browser.close();
debugInfo('Screenshot done');
return output;
}
async function getEstimatedViewbox({svg, scale}) {
const svgCopy = svg.toString();
svg = await updateViewbox(svgCopy, {
x: -maxSize,
y: -maxSize,
width: 2 * maxSize,
height: 2 * maxSize
});
const png = await tryToConvert({svg, scale, width: 2 * maxSize, height: 2 * maxSize});
if (!png) {
throw new Error('Not a valid svg');
}
const image = await Jimp.read(png);
async function save(fileName) {
const data = await new Promise(function(resolve) {
image.getBuffer('image/png', function(err, data) {
resolve(data);
});
});
require('fs').writeFileSync(fileName, data);
}
if (process.env.DEBUG_SVG) {
await save('/tmp/r01.png');
}
const newViewbox = await getCropRegionWithWhiteBackgroundDetection({svg: svgCopy, image, allowScaling: true});
if (newViewbox === false) { // too small size
return await getEstimatedViewbox({svg, scale: scale * 4});
}
const border = 2 / scale;
// translate to original coordinats
newViewbox.x = newViewbox.x / scale - maxSize - border;
newViewbox.y = newViewbox.y / scale - maxSize - border;
newViewbox.width = newViewbox.width / scale + 2 * border;
newViewbox.height = newViewbox.height / scale + 2 * border;
return newViewbox;
}
const compareImages = function(jimp1, jimp2) {
if (jimp1.bitmap.width !== jimp2.bitmap.width) return false;
if (jimp1.bitmap.height !== jimp2.bitmap.height) return false;
let index = 0;
let diffBytes = 0;
let maxDiff = 0;
for (y = 0; y < jimp1.bitmap.height; y ++ ) {
for (x = 0; x < jimp1.bitmap.width; x++) {
for (i = 0; i < 4; i ++) {
if (jimp1.bitmap.data[index] !== jimp2.bitmap.data[index]) {
// console.info(x,y,i, jimp1.bitmap.data[index], jimp2.bitmap.data[index]);
diffBytes += 1;
maxDiff = Math.max(maxDiff, Math.abs(jimp1.bitmap.data[index] - jimp2.bitmap.data[index]))
}
index += 1;
}
}
}
// console.info(diffBytes, maxDiff);
return diffBytes === 0;
}
// var sha = function(input){
// return require('crypto').createHash('sha1').update(JSON.stringify(input)).digest('hex')
// }
// If anything is completely white - make it black and transparent
async function whiteToTransparent(image) {
let c1 = 0, c2 = 0;
await image.scan(0, 0, image.bitmap.width, image.bitmap.height, function (x, y, idx) {
// x, y is the position of this pixel on the image
// idx is the position start position of this rgba tuple in the bitmap Buffer
// this is the image
var red = this.bitmap.data[ idx + 0 ];
var green = this.bitmap.data[ idx + 1 ];
var blue = this.bitmap.data[ idx + 2 ];
var alpha = this.bitmap.data[ idx + 3 ];
if (red > 250 && green > 250 && blue > 250) {
c1 += 1;
this.bitmap.data[idx + 0] = 0;
this.bitmap.data[idx + 1] = 0;
this.bitmap.data[idx + 2] = 0;
this.bitmap.data[idx + 3] = 0;
} else {
c2 += 1;
}
});
async function save(fileName) {
const data = await new Promise(function(resolve) {
image.getBuffer('image/png', function(err, data) {
resolve(data);
});
});
require('fs').writeFileSync(fileName, data);
}
if (process.env.DEBUG_SVG) {
await save('/tmp/r03.png');
}
debugInfo({c1, c2});
}
function getScale(dimensions) {
const maxSize = Math.max(dimensions.width, dimensions.height);
if (maxSize > 8000) {
return 0.1;
}
else if (maxSize > 4000) {
return 0.2;
}
else if (maxSize > 2000) {
return 0.4;
}
else if (maxSize > 1000) {
return 1;
}
else if (maxSize > 500) {
return 2;
}
else if (maxSize > 250) {
return 4;
}
else if (maxSize > 125) {
return 8;
}
else if (maxSize > 64) {
return 16;
}
else if (maxSize > 32) {
return 32;
}
else if (maxSize > 16) {
return 64;
}
else if (maxSize > 8) {
return 128;
}
else if (maxSize > 4) {
return 256;
} else {
return 512;
}
};
async function tryToConvert({svg, scale, width, height}) {
var counter = 6;
for (var attempt = 0; attempt < counter; attempt ++) {
try {
return await convert({svg, scale, width, height})
} catch(ex) {
await closeBrowser();
browser = null;
debugInfo(`attempt ${attempt} failed`);
}
}
return null;
}
async function autoCropSvg(svg, options) {
options = options || {};
svg = svg.toString();
// running it up to 5 times helps to reduce amount of nested groups
let previousSvg = svg;
for (var i = 0; i < 5; i ++) {
svg = await svgo({content: svg, title: options.title});
if (svg === previousSvg) {
break;
} else {
previousSvg = svg;
}
}
if (process.env.DEBUG_SVG) {
require('fs').writeFileSync('/tmp/s5.svg', svg);
}
// get a maximum possible viewbox which covers the whole region;
const width = maxSize;
const height = maxSize;
if (process.env.DEBUG_SVG) {
console.time('Estimated');
}
// get a border on a small scale
const estimatedViewbox = await getEstimatedViewbox({svg, scale: 0.05 });
if (process.env.DEBUG_SVG) {
console.timeEnd('Estimated');
}
if (process.env.DEBUG_SVG) {
debugInfo('estimated1: ', estimatedViewbox);
}
//get an svg in that new viewbox
svg = await updateViewbox(svg, estimatedViewbox);
// attempt to convert it again if it fails
// estimated viewBox has a size which is dividable by 20,
// because previewScale is 0.05 (x1/20)
if (process.env.DEBUG_SVG) {
require('fs').writeFileSync('/tmp/s6.svg', svg);
}
const scale = getScale(estimatedViewbox);
// console.info('using scale: ', scale);
if (process.env.DEBUG_SVG) {
console.time('Convert1');
}
const png = await tryToConvert({svg, scale, width: estimatedViewbox.width, height: estimatedViewbox.height});
// await (new Promise(function(){}));
if (!png) {
throw new Error('Not a valid svg');
}
const image = await Jimp.read(png);
if (process.env.DEBUG_SVG) {
console.timeEnd('Convert1');
}
async function save(fileName, image) {
const data = await new Promise(function(resolve) {
image.getBuffer('image/png', function(err, data) {
resolve(data);
});
});
require('fs').writeFileSync(fileName, data);
}
if (process.env.DEBUG_SVG) {
await save('/tmp/r1.png', image);
}
if (process.env.DEBUG_SVG) {
console.time('CropRegion');
}
const newViewbox = await getCropRegionWithWhiteBackgroundDetection({image});
if (process.env.DEBUG_SVG) {
console.timeEnd('CropRegion');
}
// TODO: detect if viewBox approach is not possible for that image.
// Example: ibm.input.svg logo
// render with a new Viewbox on svg - what was a scale ?
// render with the original image and make a crop
if (process.env.DEBUG_SVG) {
// image.crop(false);
// await save('/tmp/r3.png');
debugInfo({newViewbox, scale});
}
// add a bit of padding around the svg
let extraRatio = 0.02;
let borderX;
let borderY;
if (newViewbox.width > newViewbox.height) {
borderX = newViewbox.width * extraRatio;
borderY = borderX;
} else {
borderY = newViewbox.height * extraRatio;
borderX = borderY;
}
newViewbox.x = newViewbox.x - borderX;
newViewbox.y = newViewbox.y - borderY;
newViewbox.width = newViewbox.width + 2 * borderX;
newViewbox.height = newViewbox.height + 2 * borderY;
// console.info(newViewbox);
// translate to original coordinats, our estimated svg
// was saved as a png file starting from (estimatedViewbox.x,estimatedViewbox.y)
// and having a size (estimatedViewbox.width, estimatedViewbox.height)
newViewbox.x = newViewbox.x / scale + estimatedViewbox.x;
newViewbox.y = newViewbox.y / scale + estimatedViewbox.y;
newViewbox.width = newViewbox.width / scale;
newViewbox.height = newViewbox.height / scale;
// console.info(newViewbox);
// apply a new viewbox to the svg
const newSvg = await updateViewbox(svg, newViewbox);
if (process.env.DEBUG_SVG) {
require('fs').writeFileSync('/tmp/s5.svg', newSvg);
}
// validate svg for common errors
if (newSvg.indexOf('base64,') !== -1) {
throw new Error('SVG file embeds a png. Please use a pure svg file');
}
if (newSvg.indexOf('<image') !== -1) {
throw new Error('SVG file contains an image. Please use a pure svg file');
}
if (newSvg.indexOf('<text') !== -1) {
throw new Error('SVG file has a <text> element. Please convert it to the glyph first, because we can not render it the same way on all computers, especially on our render server');
}
if (newSvg.indexOf('<tspan') !== -1) {
throw new Error('SVG file has a <tspan> element. Please convert it to the glyph first, because we can not render it the same way on all computers, especially on our render server');
}
if (process.env.DEBUG_SVG) {
console.time('BadAutocrop');
}
if (!options.fast) {
const scale = getScale(newViewbox);
const viewBoxToCompare = { x: newViewbox.x - .1 * newViewbox.width, y: newViewbox.y - .1 * newViewbox.height, width: newViewbox.width * 1.2, height: newViewbox.height * 1.2};
// console.info(viewBoxToCompare);
const s2 = await updateViewbox(newSvg, viewBoxToCompare);
const originalPng = await tryToConvert({svg: newSvg, scale, width: newViewbox.width, height: newViewbox.height});
const doublePng = await tryToConvert({svg: s2, scale, width: viewBoxToCompare.width, height: viewBoxToCompare.height });
debugInfo('Screenshots created');
const originalImg = await Jimp.read(originalPng);
const doubleImg = await Jimp.read(doublePng);
debugInfo('Images loaded');
require('fs').writeFileSync('/tmp/r3.png', originalPng);
require('fs').writeFileSync('/tmp/r4.png', doublePng);
const originalViewbox = await getCropRegionWithWhiteBackgroundDetection({image: originalImg});
debugInfo('originalViewbox calculated');
const doubleViewbox = await getCropRegionWithWhiteBackgroundDetection({image: doubleImg});
debugInfo('doubleViewbox calculated');
const maxDiffWidth = Math.abs(originalViewbox.width - doubleViewbox.width);
const maxDiffHeight = Math.abs(originalViewbox.height - doubleViewbox.height);
if (maxDiffWidth > 3 || maxDiffHeight > 3) {
console.info({originalViewbox, doubleViewbox});
throw new Error('This logo cannot be autocropped because of an unusual interaction with its viewbox. Please find a different logo or convert again from the original to SVG.');
}
}
if (process.env.DEBUG_SVG) {
console.timeEnd('BadAutocrop');
}
let svgWithText;
if (options.caption) {
svgWithText = addText({
input: newSvg,
color: 'black',
text: options.caption,
captionWidth: options.captionWidth
});
newOptions = JSON.parse(JSON.stringify(options));
newOptions.caption = '';
newOptions.fast = true;
require('fs').writeFileSync('/tmp/text.svg', svgWithText);
svgWithText = (await autoCropSvg(svgWithText, newOptions)).result;
} else {
svgWithText = newSvg;
}
if (options.fast) {
return {
result: svgWithText,
}
}
// try extra transformations
const compareScale = 0.1 * getScale(newViewbox);
const originalPng = await tryToConvert({svg: svgWithText, scale: compareScale, width: newViewbox.width, height: newViewbox.height});
const originalJimp = await Jimp.read(originalPng);
const transformedSvg = await extraTransform(svgWithText);
async function tryToCompare() {
for ( i = 0; i < 5; i++ ) {
const modifiedPng = await tryToConvert({svg: transformedSvg, scale: compareScale, width: newViewbox.width, height: newViewbox.height});
const modifiedJimp = await Jimp.read(modifiedPng);
if (compareImages(originalJimp, modifiedJimp)) {
return true;
}
}
return false;
}
if (process.env.DEBUG_SVG) {
console.time('compare');
}
let compareResult = await tryToCompare();
if (process.env.DEBUG_SVG) {
console.timeEnd('compare');
console.info({compareResult});
}
if (compareResult) {
return {
result: transformedSvg,
skipRiskyTransformations: false
}
} else {
debugInfo('different');
return {
result: svgWithText,
skipRiskyTransformations: true
}
}
}
module.exports = async function main(svg, options) {
if (browser) {
await Promise.delay(1000);
return await main(svg, options);
}
try {
const result = await autoCropSvg(svg, options);
await closeBrowser();
return result;
} catch(ex) {
await closeBrowser();
throw ex;
}
}