forked from shinnn/node-font2svg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·130 lines (103 loc) · 3.17 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
/*!
* font2svg | MIT (c) Shinnosuke Watanabe
* https://github.com/shinnn/node-font2svg
*/
'use strict';
var exec = require('child_process').exec;
var allFontFaceAttrs = require('all-font-face-attrs');
var codePoints = require('code-points');
var fontCmap = require('font-cmap');
var rimraf = require('rimraf');
var singleCodePoint = require('code-point');
var sortNumbers = require('sort-numbers');
var tempWrite = require('temp-write');
var xml2js = require('xml2js');
var parseXML = xml2js.parseString;
var xmlBuilder = new xml2js.Builder();
module.exports = function font2svg(fontBuf, options, cb) {
if (cb === undefined) {
cb = options;
options = {};
} else if (!options) {
options = {};
}
if (typeof cb !== 'function') {
throw new TypeError(cb + ' is not a function.');
}
options.fontFaceAttr = options.fontFaceAttr || {};
if (typeof options.fontFaceAttr !== 'object') {
throw new TypeError('fontFaceAttr option should be an object.');
}
Object.keys(options.fontFaceAttr).forEach(function(attrName) {
if (allFontFaceAttrs.indexOf(attrName) === -1) {
throw new Error(attrName + ' is not a valid attribute name of the font-face element.');
}
});
options.maxBuffer = options.maxBuffer || 300000000;
var cmap = fontCmap(fontBuf);
var points;
if (Array.isArray(options.include)) {
points = options.include.map(function(char) {
return singleCodePoint(char);
});
} else if (options.include === undefined) {
points = [];
} else {
points = codePoints('' + options.include, {unique: true});
}
points = sortNumbers(points);
if (points.indexOf(0) === -1) {
points.unshift(0);
}
var includeIds = [];
var uniquePoints = [];
points.forEach(function(point) {
if (cmap[point] !== undefined && includeIds.indexOf(cmap[point]) === -1) {
includeIds.push(cmap[point]);
uniquePoints.push(point);
}
});
includeIds = sortNumbers(includeIds);
tempWrite(fontBuf, '.svg', function(err, tmpPath) {
if (err) {
cb(err);
return;
}
var command = 'tx -svg -sa -g ' + includeIds.join(',') + ' ' + tmpPath;
var encoding = options.encoding;
options.encoding = 'utf8';
exec(command, options, function(err, stdout) {
if (err) {
cb(err);
return;
}
rimraf(tmpPath, function(err) {
if (err) {
cb(err);
return;
}
parseXML(stdout, function(err, result) {
if (err) {
cb(err);
return;
}
var fontFace = result.svg.font[0]['font-face'][0];
Object.keys(options.fontFaceAttr).forEach(function(attrName) {
fontFace.$[attrName] = options.fontFaceAttr[attrName];
});
var glyphs = result.svg.font[0].glyph;
var svgString = xmlBuilder.buildObject(result).replace(/&/g, '&');
if (encoding) {
if (encoding === 'utf8' || encoding === 'utf-8') {
cb(null, svgString);
return;
}
cb(null, new Buffer(svgString).toString(encoding));
return;
}
cb(null, new Buffer(svgString));
});
});
});
});
};