-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmkfont.js
82 lines (70 loc) · 2.55 KB
/
mkfont.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
'use strict';
// Copyright 2024 Jeff Bush
//
// Licensed under the Apache License, Version 2.0 (the 'License');
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an 'AS IS' BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// This is a standalone command line utility that is used to generate the
// .png file with all of the font glyphs. It reads from games/font8x8.fth,
// as I used the sprite editor to create the font.
//
const {PNG} = require('pngjs');
const fs = require('fs');
const SRC_PATH = 'games/font8x8.fth';
const DEST_PATH = 'font8x8.png';
const GRID_COLS = 16;
const GRID_ROWS = 6;
const GLYPH_WIDTH = 8;
const GLYPH_HEIGHT = 8;
const TOTAL_GLYPHS = GRID_COLS * GRID_ROWS;
const fileContents = fs.readFileSync(SRC_PATH, 'utf8');
const SPRITE_DELIMITER = '\n--SPRITE DATA------\n';
const SOUND_DELIMITER = '\n--SOUND DATA--------\n';
// Split this into sections.
const split1 = fileContents.indexOf(SPRITE_DELIMITER);
const split2 = fileContents.indexOf(SOUND_DELIMITER);
if (split1 == -1 || split2 == -1) {
throw new Error('error loading file: missing sprite data');
}
const rawSpriteData = fileContents.substring(split1 + SPRITE_DELIMITER.length,
split2);
const packedSrc = rawSpriteData.replace(/\s/g, '').padEnd(TOTAL_GLYPHS *
GLYPH_WIDTH * GLYPH_HEIGHT, '0');
// Create a new PNG instance
const png = new PNG({
width: TOTAL_GLYPHS * GLYPH_WIDTH,
height: GLYPH_HEIGHT,
colorType: 6, // RGBA
});
let destIndex = 0;
const srcStride = GLYPH_WIDTH * GRID_COLS;
for (let y = 0; y < GLYPH_HEIGHT; y++) {
for (let glyphIndex = 0; glyphIndex < TOTAL_GLYPHS; glyphIndex++) {
const srcRow = Math.floor(glyphIndex / 16);
const srcCol = Math.floor(glyphIndex % 16);
const srcOffset = (srcRow * GLYPH_HEIGHT + y) * srcStride + (srcCol *
GLYPH_WIDTH);
for (let x = 0; x < GLYPH_WIDTH; x++) {
png.data[destIndex++] = 255;
png.data[destIndex++] = 255;
png.data[destIndex++] = 255;
if (packedSrc[srcOffset + x] == '0') {
png.data[destIndex++] = 0;
} else {
png.data[destIndex++] = 255;
}
}
}
}
png.pack().pipe(fs.createWriteStream(DEST_PATH)).on('finish', () => {
console.log('PNG file written successfully!');
});