Skip to content
Permalink
Newer
Older
100644 169 lines (153 sloc) 4.69 KB
Jul 13, 2016
1
"use strict";
2
3
var execSync = require('child_process').execSync;
4
var readline = require('readline');
5
var fs = require('fs');
6
Jul 13, 2016
7
8
/**
9
* print errors to console in an asynchronuous chain
10
*
11
* @param {string} err message text
12
* @param {string} cmd error triggering command
13
* @param {function (boolean)} [callback] allways called with true
14
* @return void
Jul 13, 2016
15
*/
16
exports.errorPrinter = function (callback, self) {
17
return function (err) {
18
if (err) {
19
console.error(`Error in ${err.toolbox || "svg-icon-toolbox"}:\n ${err.message}`);
20
return callback(true, self);
21
}
22
return callback(null, self);
23
};
24
};
25
26
/**
27
* enhances error with a custom header line
28
*/
29
exports.raiseErr = function (err, cmd, callback) {
30
if (!(err instanceof Error)) {
31
err = new Error(err);
32
}
33
if (err.toolbox === undefined) err.toolbox = cmd;
34
return callback(err);
Jul 13, 2016
35
};
36
37
/**
38
* synchronuously make sure a directory exists
39
*
40
* @param {string} dir directory name to make as needed
41
* @param {function (any)} callback
42
*/
43
exports.testDir = function (dir, callback) {
44
try {
45
execSync(`mkdir -p ${dir}`);
46
return callback(null);
47
} catch (err) {
Jul 17, 2016
48
return callback(err.message);
Jul 13, 2016
49
}
52
exports.readLines = function (fn, callback) {
53
var reader;
54
try {
55
reader = readline.createInterface({
56
input: fs.createReadStream(fn)
57
});
58
} catch (err) {
59
return callback(err);
60
}
61
62
var lines = [];
63
reader.on('line', (line) => {
64
line = line.trim();
65
if (line.length) lines.push(line);
66
}).on('close', () => {
67
callback(null, lines);
68
});
69
};
70
71
// determine incomplete values or throw error on incomputables
72
function viewPortValues (width, height, viewBox) {
73
var errMessage = 'Cannot determine the size of the drawing.';
74
75
var values = {}, vb, widthUnit, heightUnit;
76
if (viewBox) {
77
vb = viewBox.split(/[,\s]+/).map((str) => {
78
return parseFloat(str);
79
});
80
if (vb[2] === 0 || vb[3] === 0) throw new Error(errMessage);
81
}
82
83
var regex = /[\d\.]+(?:px)?(.+)?$/;
84
if (width) {
85
widthUnit = width.match(regex)[1];
86
values.width = parseFloat(width);
87
if (values.width === 0) throw new Error(errMessage);
88
}
89
if (height) {
90
heightUnit = height.match(regex)[1];
91
values.height = parseFloat(height);
92
if (values.height === 0) throw new Error(errMessage);
93
}
94
95
if (vb && vb.length === 4) {
96
values.viewBox = vb;
97
if (widthUnit) {
98
if (widthUnit === '%' && values.width) {
99
values.width *= vb[2] / 100;
100
} else {
101
throw new Error(errMessage);
102
}
103
}
104
if (!values.width) {
105
values.width = vb[2];
106
}
107
if (heightUnit) {
108
if (heightUnit === '%' && values.height) {
109
values.height *= vb[3] / 100;
110
} else {
111
throw new Error(errMessage);
112
}
113
}
114
if (!values.height) {
115
values.height = vb[3];
116
}
117
} else if (widthUnit || heightUnit || !values.width || !values.height) {
118
throw new Error(errMessage);
119
}
120
return values;
121
}
122
124
* compute equivalent transform from a viewport definition
125
* @see https://svgwg.org/svg2-draft/coords.html#ComputingAViewportsTransform
126
* @see https://www.w3.org/Bugs/Public/show_bug.cgi?id=29751
127
*/
128
exports.computeTransform = function (width, height, viewBox, preserveAspectRatio) {
129
var viewport = viewPortValues(width, height, viewBox);
130
if (!viewport.viewBox) return [];
131
132
var par = preserveAspectRatio ? preserveAspectRatio.split(/\s/) : [];
133
var align = par[0] || 'xMidYMid';
134
var meetOrSlice = par[1] || 'meet';
135
136
var sx = viewport.width / viewport.viewBox[2],
137
sy = viewport.height / viewport.viewBox[3];
138
if (align !== 'none') {
139
if (meetOrSlice === 'slice') {
140
sx = sy = Math.max(sx, sy);
141
} else {
142
sx = sy = Math.min(sx, sy);
143
}
144
}
145
var tx = -viewport.viewBox[0] * sx,
146
ty = -viewport.viewBox[1] * sy;
147
148
if (align.includes('xMid')) {
149
tx += (viewport.width - viewport.viewBox[2] * sx) / 2;
150
}
151
if (align.includes('xMax')) {
152
tx += (viewport.width - viewport.viewBox[2] * sx);
153
}
154
if (align.includes('YMid')) {
155
ty += (viewport.height - viewport.viewBox[3] * sy) / 2;
156
}
157
if (align.includes('YMax')) {
158
ty += (viewport.height - viewport.viewBox[3] * sy);
159
}
160
161
var transform = [];
162
if (tx !== 0 || ty !== 0) {
163
transform.push(`translate(${tx} ${ty})`);
164
}
165
if (sx !== 1 || sy !== 1) {
166
transform.push(`scale(${sx} ${sy})`);
167
}
168
return transform;