-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsvg-font-dump.js
executable file
·268 lines (206 loc) · 6.8 KB
/
svg-font-dump.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
#!/usr/bin/env node
'use strict';
var fs = require('fs');
var path = require('path');
var crypto = require('crypto');
var _ = require('lodash');
var yaml = require('js-yaml');
var SvgPath = require('svgpath');
var XMLDOMParser = require('xmldom').DOMParser;
var ArgumentParser = require('argparse').ArgumentParser;
var svg_template = _.template(
'<svg height="<%= height %>" width="<%= width %>" xmlns="http://www.w3.org/2000/svg">' +
'<path d="<%= d %>" />' +
'</svg>'
);
var parser = new ArgumentParser({
version: require('./package.json').version,
addHelp: true,
description: 'Dump SVG font to separate glyphs'
});
parser.addArgument([ '-c', '--config' ], { help: 'Font config file' });
parser.addArgument([ '-i', '--src_font' ], { help: 'Source font path', required: true });
parser.addArgument([ '-o', '--glyphs_dir' ], { help: 'Glyphs output folder', required: true });
parser.addArgument([ '-d', '--diff_config' ], { help: 'Difference config output file' });
parser.addArgument([ '-f', '--force' ], { help: 'Force override glyphs from config', action: 'storeTrue'});
parser.addArgument([ '-n', '--names' ], { help: 'Try to guess new glyphs names', action: 'storeTrue'});
var args = parser.parseArgs();
////////////////////////////////////////////////////////////////////////////////
// Int to char, with fix for big numbers
// see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/fromCharCode
//
function fixedFromCharCode(code) {
/*jshint bitwise: false*/
if (code > 0xffff) {
code -= 0x10000;
var surrogate1 = 0xd800 + (code >> 10)
, surrogate2 = 0xdc00 + (code & 0x3ff);
return String.fromCharCode(surrogate1, surrogate2);
} else {
return String.fromCharCode(code);
}
}
// Char to Int, with fix for big numbers
//
function fixedCharCodeAt(chr) {
/*jshint bitwise: false*/
var char1 = chr.charCodeAt(0)
, char2 = chr.charCodeAt(1);
if ((chr.length >= 2) &&
((char1 & 0xfc00) === 0xd800) &&
((char2 & 0xfc00) === 0xdc00)) {
return 0x10000 + ((char1 - 0xd800) << 10) + (char2 - 0xdc00);
} else {
return char1;
}
}
// Load glyphs data from SVG font
//
function load_svg_data(data) {
var result = [];
var xmlDoc = (new XMLDOMParser()).parseFromString(data, "application/xml");
var svgFont = xmlDoc.getElementsByTagName('font')[0];
var svgFontface = xmlDoc.getElementsByTagName('font-face')[0];
var svgGlyps = xmlDoc.getElementsByTagName('glyph');
var fontHorizAdvX = svgFont.getAttribute('horiz-adv-x');
var fontAscent = svgFontface.getAttribute('ascent');
var fontUnitsPerEm = svgFontface.getAttribute('units-per-em') || 1000;
var scale = 1000 / fontUnitsPerEm;
_.each(svgGlyps, function (svgGlyph) {
var d = svgGlyph.getAttribute('d');
// FIXME
// Now just ignore glyphs without image, however
// that can be space. Does anyone needs it?
if (!d) { return; }
var unicode = svgGlyph.getAttribute('unicode');
var name = svgGlyph.getAttribute('glyph-name') || ('glyph' + unicode);
var width = svgGlyph.getAttribute('horiz-adv-x') || fontHorizAdvX;
result.push({
d: new SvgPath(d)
.translate(0, -fontAscent)
.scale(scale, -scale)
.abs()
.round(1)
.rel()
.round(1)
.toString(),
unicode: unicode,
name: name,
width: (width*scale).toFixed(1),
height: 1000
});
});
return result;
}
// Load glyphs data from fontello's config (custom icons only)
//
function load_fontello_data(data) {
var result = [];
_.each(data.glyphs, function (glyph) {
// FIXME
// Now just ignore glyphs without image, however
// that can be space. Does anyone needs it?
if (!(glyph.svg && glyph.svg.path)) { return; }
result.push({
//d: glyph.svg.path,
d: new SvgPath(glyph.svg.path)
.abs()
.round(1)
.rel()
.round(1)
.toString(),
width: glyph.svg.width.toFixed(1),
height: 1000,
uid: glyph.uid,
unicode: fixedFromCharCode(glyph.code),
name: glyph.css || ('glyph' + glyph.code),
search: glyph.search || []
});
});
return result;
}
////////////////////////////////////////////////////////////////////////////////
var data, config, diff = [];
try {
data = fs.readFileSync(args.src_font, 'utf-8');
} catch (e) {
console.error('Can\'t read font file ' + args.src_font);
process.exit(1);
}
if (args.config) {
try {
config = yaml.load(fs.readFileSync(args.config, 'utf-8'));
} catch (e) {
console.error('Can\'t read config file ' + args.config);
process.exit(1);
}
} else {
config = { glyphs: [] };
}
var glyphs;
if (path.extname(args.src_font) === '.json') {
glyphs = load_fontello_data(JSON.parse(data));
} else {
glyphs = load_svg_data(data);
}
glyphs.forEach(function(glyph) {
var exists,
glyph_out = {};
// Convert multibyte unicode char to number
glyph.unicode = fixedCharCodeAt(glyph.unicode);
// if got config from existing font, then write only missed files
if (config) {
exists = _.find(config.glyphs, function(element) {
//console.log('---' + element.from + '---' + glyph.unicode)
return (element.from || element.code) === glyph.unicode;
});
if (exists && !args.force) {
console.log((glyph.unicode.toString(16)) + ' exists, skipping');
return;
}
}
// Fix for FontForge: need space between old and new polyline
glyph.d = glyph.d.replace(/zm/g, 'z m');
glyph.svg = svg_template({
d : glyph.d,
width : glyph.width,
height : glyph.height
});
if (exists) {
// glyph exists in config, but we forced dump
fs.writeFileSync(path.join(args.glyphs_dir, (exists.file || exists.css) + '.svg'), glyph.svg);
console.log((glyph.unicode.toString(16)) + ' - Found, but override forced');
return;
}
// Completely new glyph
glyph_out = {
css: glyph.name,
code: glyph.unicode,
uid: glyph.uid || crypto.randomBytes(16).toString('hex'),
search: glyph.search || []
};
console.log((glyph.unicode.toString(16)) + ' - NEW glyph, writing...');
var filename;
if (args.names) {
filename = glyph.name + '.svg';
} else {
if (glyph.unicode === +glyph.unicode) {
filename = 'glyph__' + glyph.unicode.toString(16) + '.svg';
} else {
filename = 'glyph__' + glyph.unicode + '.svg';
}
}
fs.writeFile(path.join(args.glyphs_dir, filename), glyph.svg);
diff.push(glyph_out);
});
// Create config template for new glyphs, if option set
if (args.diff_config) {
if (!diff.length) {
console.log("No new glyphs, skip writing diff");
return;
}
fs.writeFileSync(
args.diff_config,
yaml.dump({ glyphs: diff }, { flowLevel: 3, styles: { '!!int': 'hexadecimal' } })
);
}