This repository has been archived by the owner on Aug 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
300 lines (262 loc) · 8.57 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
require('dotenv').config()
const path = require('path')
const chalk = require('chalk')
const fs = require('fs-extra')
const isEqual = require('lodash.isequal')
const fonts = require('./fonts')
const { createCanvas, registerFont } = require('canvas')
const inspect = value =>
require('util').inspect(value, false, null, true)
const w = 8, h = 8
const chars =
`ABCDEFGHIJKLMNO` +
`PQRSTUVWXYZ0123` +
`456789[]()<>!?@` +
`#$%&'"*+-=^~•:;` +
`.,|/\\`
const toReplace = `_‘’“”`
const rgbaKeys = ['r', 'g', 'b', 'a']
const white = { r: 255, g: 255, b: 255, a: 255 }
const run = async () => {
await clearOutput()
for (let i = 0; i < fonts.length; i++) {
const group = fonts[i]
await processGroup(group)
}
outputBaseModule(fonts)
}
const clearOutput = async () =>
await fs.remove(getOutputPath())
const processGroup = async group => {
if (process.env.TRIM_FONTS) await trimFonts(group)
for (let i = 0; i < group.fonts.length; i++) {
const font = group.fonts[i]
await processFont(group, font)
}
await outputGroupModule(group)
}
const trimFonts = async group => {
const whitelist = group.fonts.map(font => getFontPath(group, font))
const groupPath = getInputPath(group.path)
const groupFiles = await fs.readdir(groupPath)
for (let i = 0; i < groupFiles.length; i++) {
const groupFile = groupFiles[i]
const filePath = path.resolve(groupPath, groupFiles[i])
const ext = path.extname(groupFile)
if (ext !== '.ttf') continue
if (whitelist.includes(filePath)) continue
await trimGroupFont(groupFile, filePath)
}
}
const trimGroupFont = async (groupFile, filePath) => {
console.log(chalk.red(`Trimming → ${groupFile}`))
console.log(chalk.gray(filePath))
await fs.remove(filePath)
}
const processFont = async (group, font) => {
const fontPath = getFontPath(group, font)
registerFont(fontPath, { family: font.name })
for (let i = 0; i < chars.length; i++) {
await processChar({ group, font, charIndex: i })
}
await outputFontFile(group, font)
await outputFontImage(group, font)
await outputFontModule(group, font)
}
const getFontPath = (group, font) =>
getInputPath(group.path, font.path)
const processChar = async ({ group, font, charIndex }) => {
const x = 0, y = 8, size = '8px'
const canvas = createCanvas(w, h)
const ctx = canvas.getContext('2d')
ctx.fillStyle = 'black'
ctx.fillRect(0, 0, w, h)
ctx.fillStyle = 'white';
ctx.font = `${size} "${font.name}"`
const char = chars[charIndex]
ctx.fillText(char, x, y)
const { data } = ctx.getImageData(0, 0, w, h)
const colors = getColors(data)
const bounds = getBounds(colors)
previewChar(group, font, char, colors, bounds)
const outputImagePath = getOutputImagePath(group.name, font.name, `chars/${charIndex}.png`)
await fs.outputFile(outputImagePath, canvas.toBuffer())
const outputModulePath = getOutputModulePath(group.name, font.name, `chars/${charIndex}.js`)
await fs.outputFile(outputModulePath, getCharModule(char, colors, bounds))
}
const getColors = data =>
Object.values(data.reduce((colors, value, index) => {
const pixelIndex = Math.floor(index / 4)
const rgbaIndex = index % 4
if (!colors[pixelIndex]) colors[pixelIndex] = { r: 0, g: 0, b: 0, a: 0 }
colors[pixelIndex][rgbaKeys[rgbaIndex]] = value
return colors
}, {})).map(rgba => isEqual(rgba, white) ? 'X' : ' ')
const getBounds = colors => {
let left, right, top, bottom
left:
for (let c = 0; c < 8; c++) {
for (let r = 0; r < 8; r++) {
const pixel = colors[r * 8 + c]
if (pixel === ' ') continue
left = c
break left
}
}
right:
for (let c = 8 - 1; c >= 0; c--) {
for (let r = 0; r < 8; r++) {
const pixel = colors[r * 8 + c]
if (pixel === ' ') continue
right = c
break right
}
}
top:
for (let r = 0; r < 8; r++) {
for (let c = 0; c < 8; c++) {
const pixel = colors[r * 8 + c]
if (pixel === ' ') continue
top = r
break top
}
}
bottom:
for (let r = 8 - 1; r >= 0; r--) {
for (let c = 0; c < 8; c++) {
const pixel = colors[r * 8 + c]
if (pixel === ' ') continue
bottom = r
break bottom
}
}
const width = right - (left - 1)
const height = bottom - (top - 1)
return { left, right, top, bottom, width, height }
}
const previewChar = (group, font, char, colors, bounds) => {
console.log(`\n`)
console.log(chalk.yellow('Group:'), group.name)
console.log(chalk.yellow('Font:'), font.name)
console.log(chalk.yellow('Char:'), char)
console.log(chalk.yellow('Bounds:'), bounds)
for (let i = 0; i < colors.length; i += 8) {
const row = colors.slice(i, i + 8)
console.log(row.map(_ =>
_ === 'X' ? chalk.white('█') : chalk.gray('·')
).join(''))
}
}
const getCharModule = (char, colors, bounds) => {
let js = 'module.exports = {\n'
js += tab() + `char: \`${encodeChar(char)}\`,\n`
js += tab() + 'bounds: {\n'
const kv = Object.entries(bounds)
for (let i = 0; i < kv.length; i++) {
const [key, value] = kv[i];
js += tab(2) + `${key}: ${value},\n`
}
js += tab() + '},\n'
js += tab() + 'colors: [\n'
for (let i = 0; i < colors.length; i += 8) {
const row = colors.slice(i, i + 8)
js += tab(2) + row.map(_ => `'${_}',`).join('') + '\n'
}
js += tab() + ']\n'
js += '}'
return js
}
const encodeChar = char => {
if (char === '\\') return '\\\\'
return char
}
const outputFontFile = async (group, font) =>
await fs.outputFile(
getOutputFontFilePath(group, font),
await fs.readFile(getFontPath(group, font))
)
const outputFontImage = async (group, font) => {
const x = 0, y = 8, size = '8px'
const w = 15 * 8
const h = Math.ceil(chars.length / 15) * 8
const canvas = createCanvas(w, h)
const ctx = canvas.getContext('2d')
ctx.fillStyle = 'black'
ctx.fillRect(0, 0, w, h)
ctx.fillStyle = 'white';
ctx.font = `${size} "${font.name}"`
for (let i = 0; i < chars.length; i++) {
const char = chars[i]
ctx.fillText(char, ((i * 8) % w) + x, (Math.floor(i / 15) * 8) + y)
}
const outputImagePath = getOutputImagePath(group.name, font.name, `preview.png`)
await fs.outputFile(outputImagePath, canvas.toBuffer())
}
const outputFontModule = async (group, font) =>
await fs.outputFile(getOutputFontModulePath(group, font), getFontModule(group, font))
const getFontModule = (group, font) => {
let js = 'module.exports = {\n'
js += tab() + `name: '${font.name}',\n`
js += tab() + `path: '${getFontFilePath(group, font)}',\n`
js += tab() + 'chars: {\n'
for (let i = 0; i < chars.length; i++) {
js += tab(2) + `'${i}': require('./chars/${i}.js'),\n`
}
js += tab() + '}\n'
js += '}\n'
return js
}
const getFontFilePath = (group, font) =>
getOutputFontFilePath(group, font)
.replace(RegExp(`^${getOutputPath()}\/`), '')
const outputGroupModule = async group =>
await fs.outputFile(getOutputModulePath(group.name, 'index.js'), getGroupModule(group))
const getGroupModule = group => {
let js = 'module.exports = {\n'
js += tab() + `name: '${group.name}',\n`
js += tab() + `source: '${group.source}',\n`
js += tab() + 'fonts: {\n'
for (let i = 0; i < group.fonts.length; i++) {
const font = group.fonts[i]
js += tab(2) + `'${font.name}': require('./${font.name}/index.js'),\n`
}
js += tab() + '}\n'
js += '}\n'
return js
}
const outputBaseModule = async fonts =>
await fs.outputFile(getOutputModulePath('index.js'), getBaseModule(fonts))
const getBaseModule = fonts => {
let js = 'module.exports = {\n'
js += tab() + `chars: \`${chars.split('').map(char => encodeChar(char)).join('')}\`,\n`
js += tab() + 'groups: {\n'
for (let i = 0; i < fonts.length; i++) {
const group = fonts[i]
js += tab(2) + `'${group.name}': require('./${group.name}/index.js'),\n`
}
js += tab() + '}\n'
js += '}\n'
return js
}
const getOutputFontFilePath = (group, font) =>
getOutputFilePath(group.name, `${font.name}.ttf`)
const getOutputFontModulePath = (group, font) =>
getOutputModulePath(group.name, font.name, 'index.js')
const getOutputFilePath = (...params) =>
getOutputPath('ttf', ...params)
const getOutputImagePath = (...params) =>
getOutputPath('png', ...params)
const getOutputModulePath = (...params) =>
getOutputPath('js', ...params)
const getOutputPath = (...params) =>
path.resolve(process.env.OUTPUT_PATH || './fonts/output', ...params)
const getInputPath = (...params) =>
path.resolve('./fonts/input', ...params)
const exit = async message => {
if (message) console.log(chalk.red(message))
console.log(chalk.gray('Exiting…'))
process.exit()
}
const tab = (total = 1) =>
' '.repeat(total)
run().catch(console.error)