-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathsvg-resizer.js
executable file
·134 lines (106 loc) · 3.51 KB
/
svg-resizer.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
#!/usr/bin/env node
require('shelljs/global');
var path = require('path');
var fs = require('fs-extra');
var _ = require('lodash');
var xml2js = require('xml2js');
var builder = new xml2js.Builder();
var parseString = xml2js.parseString;
var ptToPx = function(pt) {
return pt * dpi * (1 / 72);
};
var pxToPt = function(px) {
return px / (dpi * (1 / 72));
};
if (!which('rsvg-convert')) {
echo('rsvg-convert bin from libsrvg is required');
exit(1);
}
var opts = require("nomnom")
.option('width', {
abbr: 'x',
help: 'Output svg width'
})
.option('height', {
abbr: 'y',
help: 'Output svg height'
})
.option('fit', {
abbr: 'f',
flag: true,
help: 'Fit to specified dimensions preserving aspect ratio'
})
.option('output', {
abbr: 'o',
default: 'resized/',
help: 'Output svg path'
})
.option('format', {
abbr: 'e',
default: 'svg',
help: 'Output file format'
})
.parse();
// create output folder if dont exist
mkdir('-p', path.join(opts.output));
var dpi = 96;
var svgFiles = opts._;
var finalWidth = pxToPt(opts.width || opts.height);
var finalHeight = pxToPt(opts.height || opts.width);
var finalRatio = finalWidth / finalHeight;
svgFiles.forEach(function(svgPath) {
if (opts.fit) {
var origWidth, origHeight;
var newWidth, newHeight;
var fileContent = fs.readFileSync(svgPath, 'utf8');
parseString(fileContent, function (err, parsedFileContent) {
origWidth = pxToPt(parseInt(parsedFileContent.svg.$.width, 10));
origHeight = pxToPt(parseInt(parsedFileContent.svg.$.height, 10));
});
var origRatio = origWidth / origHeight;
if (origRatio < finalRatio) {
newHeight = finalHeight;
newWidth = origWidth / (origHeight / newHeight);
newWidth = Math.floor(newWidth);
} else {
newWidth = finalWidth;
newHeight = origHeight / (origWidth / newWidth);
newHeight = Math.floor(newHeight);
}
opts.width = newWidth;
opts.height = newHeight;
}
// build args
var outputPath = opts.output ? path.join(opts.output, path.basename(svgPath, '.svg') + '.' + opts.format) : '';
var args = _.compact([
opts.width ? '-w ' + opts.width : null,
opts.height ? '-h ' + opts.height : null,
'--keep-aspect-ratio',
'--dpi-x 90', // work with pixels
'--dpi-y 90', // work with pixels
'-f ' + opts.format,
svgPath,
'-o ' + outputPath
]);
// print rsvg command
echo('rsvg-convert ' + args.join(' '));
// resize file with librsvg
var convert = exec('rsvg-convert ' + args.join(' '));
if (convert.code !== 0) {
echo('Error converting file: ' + svgPath);
}
// read resized file and change pt to px
var resizedFileContent = fs.readFileSync(outputPath, 'utf8');
parseString(resizedFileContent, function (err, parsedFileContent) {
var w = parsedFileContent.svg.$.width;
var h = parsedFileContent.svg.$.height;
w = w.match(/pt$/) ? ptToPx(parseInt(w, 10)) + 'px' : w;
h = h.match(/pt$/) ? ptToPx(parseInt(h, 10)) + 'px' : h;
parsedFileContent.svg.$.width = w;
parsedFileContent.svg.$.height = h;
// parsedFileContent.svg.$.viewBox = [0, 0, parseInt(w, 10), parseInt(h, 10)].join(' ');
var finalSVG = builder.buildObject(parsedFileContent);
fs.outputFileSync(outputPath, finalSVG);
});
});
exit(0);