-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfont.js
63 lines (54 loc) · 1.37 KB
/
font.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
'use strict';
const sharp = require('sharp');
const edscii = [
' !"#$%&\'()*+,-./',
'0123456789:;<=>?',
'@abcdefghijklmno',
'pqrstuvwxyz[£]✓π',
'█ABCDEFGHIJKLMNO',
'PQRSTUVWXYZ♠♥♣♦●',
];
let charsPromise;
module.exports = {
async getFont() {
if (!charsPromise) {
const font = sharp('./sprites/c64_edscii.png');
charsPromise = Promise
.all(edscii
.flatMap((row, y) => row.split('')
.map(async (char, x) => [
char,
await font
.extract({
left: x * 8,
top: y * 8,
width: 8,
height: 8,
})
.toBuffer(),
])))
.then(entries => Object.fromEntries(entries));
}
return charsPromise;
},
async renderLine(message) {
const chars = await this.getFont();
const lines = message.split('\n');
return sharp(
{
create: {
width: Math.max(...lines.map(line => line.length)) * 8,
height: lines.length * 8,
channels: 4,
background: { r: 0, g: 0, b: 0, alpha: 0 },
},
})
.composite(lines.flatMap((line, y) => line.split('').map((char, x) => ({
input: chars[char] || chars[' '],
top: y * 8,
left: x * 8,
}))))
.png()
.toBuffer();
},
};