-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathpull_font_svg.js
42 lines (34 loc) · 1.21 KB
/
pull_font_svg.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
var fs = require('fs');
var xml2js = require('xml2js');
var parser = new xml2js.Parser();
module.exports = function pullFontSVG(data, pathOut) {
parser.parseString(data, function(err, result) {
if(err) throw err;
var font_obj = result.svg.defs[0].font[0],
default_width = Number(font_obj.$['horiz-adv-x']),
ascent = Number(font_obj['font-face'][0].$.ascent),
descent = Number(font_obj['font-face'][0].$.descent),
chars = {};
font_obj.glyph.forEach(function(glyph) {
chars[glyph.$['glyph-name']] = {
width: Number(glyph.$['horiz-adv-x']) || default_width,
path: glyph.$.d,
ascent: ascent,
descent: descent
};
});
// turn remaining double quotes into single
var charStr = JSON.stringify(chars, null, 4).replace(/\"/g, '\'');
var outStr = [
'/* jshint quotmark:true */',
'',
'\'use strict\';',
'',
'module.exports = ' + charStr + ';',
''
].join('\n');
fs.writeFile(pathOut, outStr, function(err) {
if(err) throw err;
});
});
};