-
Notifications
You must be signed in to change notification settings - Fork 0
/
extractImages.js
80 lines (54 loc) · 1.42 KB
/
extractImages.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
'use strict';
const fontkit = require('fontkit'),
fs = require('fs'),
path = require('path');
const imageSize = 64;
function extractImages(emojiCategories, inFontFile, outDir, callback){
let failures = [];
let emojis = [];
let counter = 0;
let worked = [];
let errors = [];
for (let category in emojiCategories){
emojiCategories[category].forEach(function(emoji){
emojis.push(emoji);
});
}
fontkit.open(inFontFile, null, function(err, font){
if (err) {
callback(err);
} else {
console.log(`Found ${font.numGlyphs} glyphs in the font and ${emojis.length} to render out from the JSON file.`);
emojis.forEach(function(emoji){
let layout;
try {
layout = font.layout(emoji);
} catch (e) {
failures.push(emoji);
console.error(`Error converting ${emoji} to image:`, e, e.stack);
console.log('failed:', failures);
}
if (layout){
let glyph = layout.glyphs[0];
let img = glyph.getImageForSize(imageSize);
worked.push(emoji);
fs.writeFile(path.resolve(outDir,`${emoji}.${img.type.trim()}`), img.data, "binary", function(err){
counter++;
if (err){
errors.push(err);
}
if (counter == emojis.length){
if (errors.length === 0){
errors = null;
}
callback(errors, worked);
}
});
} else {
counter++;
}
});
}
});
}
module.exports = extractImages;