-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmsdf-minifier.js
74 lines (59 loc) · 1.65 KB
/
msdf-minifier.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
/**
* msdf-minifier.js
* @author quinton-ashley
*
* Minifies JSON files generated by msdf-bmfont-xml
* https://msdf-bmfont.donmccurdy.com/
*
* Converts the chars and kernings arrays to CSV strings
* and removes unnecessary data.
*
* Font must be in the same folder as this script.
*
* CLI Usage: node msdf-minifier.js <font-name>
*/
const fs = require('fs');
const { Parser } = require('json2csv');
// Read input file path from command line
const inputFile = __dirname + '/' + process.argv[2] + '-msdf.json';
// Read and parse JSON file
let data = JSON.parse(fs.readFileSync(inputFile, 'utf8'));
// Convert chars array to CSV
const parser = new Parser({ delimiter: ' ' });
let chars = data.chars;
for (let char of chars) {
delete char.index;
delete char.chnl;
delete char.page;
}
// Create modified JSON with CSV chars
chars = parser.parse(chars);
// handle special cases when the
// character is a double quote or backslash
chars = chars.replace('"\\"', '"\\\\"');
chars = chars.replace('""""', '"\\""');
data.chars = chars;
if (data.kernings.length) {
data.kernings = parser.parse(data.kernings);
}
let info = data.info;
if (!info.bold) delete info.bold;
if (!info.italic) delete info.italic;
delete info.charset;
delete info.unicode;
delete info.stretchH;
delete info.smooth;
delete info.aa;
delete info.padding;
delete info.spacing;
let common = data.common;
delete common.pages;
delete common.packed;
delete common.alphaChnl;
delete common.redChnl;
delete common.greenChnl;
delete common.blueChnl;
delete data.distanceField;
// Write modified JSON back to file
fs.writeFileSync(inputFile, JSON.stringify(data));
console.log(`Minified ${inputFile}`);