-
Notifications
You must be signed in to change notification settings - Fork 97
/
generate.js
151 lines (134 loc) · 4.14 KB
/
generate.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
var fs = require('fs'),
path = require('path');
var fontnik = require('fontnik'),
glyphCompose = require('@mapbox/glyph-pbf-composite');
var DEBUG = false;
var outputDir = '_output';
var sizeSumTotal = 0;
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir);
}
var doFonts = function(dir, fonts) {
var makeGlyphs = function(config) {
var sourceFonts = {};
var folderName = outputDir + '/' + config.name;
config.sources.forEach(function(sourceName) {
if (!sourceFonts[sourceName]) {
try {
sourceFonts[sourceName] = fs.readFileSync(dir + '/' + sourceName);
} catch (e) {}
}
});
if (!fs.existsSync(folderName)) {
fs.mkdirSync(folderName);
}
var sizeSum = 0;
var histogram = new Array(256);
var doRange = function(start, end) {
return Promise.all(config.sources.map(function(sourceName) {
var source = sourceFonts[sourceName];
if (!source) {
console.log('[%s] Source "%s" not found', config.name, sourceName);
return Promise.resolve();
}
return new Promise(function(resolve, reject) {
fontnik.range({
font: source,
start: start,
end: end
}, function(err, data) {
if (err) {
reject();
} else {
resolve(data);
}
});
});
})).then(function(results) {
results = results.filter(function(r) {return !!r;});
var combined = glyphCompose.combine(results);
var size = combined.length;
sizeSum += size;
histogram[start / 256] = size;
if (DEBUG) {
console.log('[%s] Range %s-%s size %s B', config.name, start, end, size);
}
fs.writeFileSync(folderName + '/' + start + '-' + end + '.pbf', combined);
});
};
var ranges = [];
for (var i = 0; i < 65536; (i = i + 256)) {
ranges.push([i, Math.min(i + 255, 65535)]);
}
console.log('[%s]', config.name);
var fontPromise;
if (DEBUG) {
return ranges.reduce(function(p, range) {
return p.then(function() {
return doRange(range[0], range[1]);
});
}, Promise.resolve()
);
} else {
fontPromise = Promise.all(ranges.map(function(range) {
return doRange(range[0], range[1]);
}));
}
return fontPromise.then(function() {
console.log(' Size histo [kB]: %s', histogram.map(function(v) {
return v > 512 ? Math.round(v / 1024) : '';
}).join('|'));
console.log(' Total size %s B', sizeSum);
sizeSumTotal += sizeSum;
});
};
// would be much faster in parallel, but this is better for logging
return fonts.reduce(function(p, font) {
return p.then(function() {
return makeGlyphs(font);
});
}, Promise.resolve()
);
};
var todo = [];
fs.readdirSync('.').forEach(function(dir) {
if (fs.lstatSync(dir).isDirectory()) {
var fonts;
try {
fonts = require(path.resolve(__dirname, dir, 'fonts.json'));
fonts.forEach(function(font) {
font.sources = font.sources.filter(function(f) {
// skip sources starting with '//' -- these are "commented"
return f.indexOf('//') === -1;
});
});
} catch (e) {
fonts = [];
fs.readdirSync(dir).forEach(function(file) {
if (path.extname(file) == '.ttf' || path.extname(file) == '.otf') {
// compatible font name generation with genfontgl
var rex = /([A-Z])([A-Z])([a-z])|([a-z])([A-Z])/g;
fonts.push({
name: path.basename(file).slice(0, -4).replace('-','').replace(rex, '$1$4 $2$3$5'),
sources: [
path.basename(file)
]
});
}
});
}
if (fonts && fonts.length) {
todo.push([dir, fonts]);
}
}
});
// would be much faster in parallel, but this is better for logging
todo.reduce(function(p, pair) {
return p.then(function() {
console.log('Directory [%s]:', pair[0]);
return doFonts(pair[0], pair[1]);
});
}, Promise.resolve()
).then(function() {
console.log('Total size %s B', sizeSumTotal);
});