Skip to content

Free games for graphing calculators: NumWorks, TI-Nspire CX, Casio fx-CG50 and soon TI-84 Plus CE. Balatro, Celeste, Hollow Knight, Minecraft and more.

4.2 KB
Raw Blame
#!/usr/bin/env python3
"""NumPowder: The Powder Toy's font, from its resources/font.bz2.

The original's font (src/graphics/FontReader.cpp) is a list of glyphs, each a
24-bit code point and an 8-bit width, then 12 rows of `width` pixels, two bits
of alpha each (0..3), four to a byte, low bits first, row by row. Its glyphs
U+E000 and on are the interface's icons (menu sections, tools, render modes).

This keeps the glyphs the interface uses, in the same format, in
src/font_data.c, indexed by NumPowder's one-byte text code (font.h):
  0x20..0x7E  ASCII
  0x7F        U+00B0, the degree sign
  0x80..0xEE  U+E000..U+E06E, the icons
  0xEF        U+00B7, the middle dot
  0xF0..0xF4  the keypad's symbols, drawn here in the font's style (the original has none):
              left, right, up and down triangles (the arrow keys), and the delete key's symbol
and U+FFFD for anything else.

Usage: gen_font.py [the Powder Toy's source folder]
"""
import bz2
import os
import sys

TPT = sys.argv[1] if len(sys.argv) > 1 else os.environ.get(
    "TPT_SRC", os.path.expanduser("~/Documents/Assets/gamesrc/powdertoy"))
OUT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "src", "font_data.c")

data = bz2.decompress(open(os.path.join(TPT, "resources", "font.bz2"), "rb").read())
glyphs = {}
i = 0
while i < len(data):
    cp = data[i] | data[i + 1] << 8 | data[i + 2] << 16
    w = data[i + 3]
    glyphs[cp] = (w, data[i + 4:i + 4 + 3 * w])
    i += 4 + 3 * w

# the keypad's symbols: rows of the glyph from its top (row 0 is 2 pixels above the text's y: capitals take rows 2 to
# 8), '#' a full pixel; a column of spacing after each, as the font's own glyphs
KEYPAD = [
    # left: the arrow key
    ["", "", "    #", "   ##", "  ###", " ####", "  ###", "   ##", "    #"],
    # right
    ["", "", "#", "##", "###", "####", "###", "##", "#"],
    # up
    ["", "", "", "", "   #", "  ###", " #####", "#######"],
    # down
    ["", "", "", "", "#######", " #####", "  ###", "   #"],
    # the delete key (backspace)
    ["", "", "  #######", " #      #", "#  # #  #", "#   #   #", "#  # #  #", " #      #", "  #######"],
]


def keypad_glyph(rows):
    w = max(len(r) for r in rows) + 1
    px = []
    for y in range(12):
        r = rows[y] if y < len(rows) else ""
        px += [3 if x < len(r) and r[x] == "#" else 0 for x in range(w)]
    b = bytearray(3 * w)
    for k, a in enumerate(px):
        b[k // 4] |= a << (2 * (k % 4))
    return w, bytes(b)


for k, rows in enumerate(KEYPAD):
    glyphs[0x110000 + k] = keypad_glyph(rows)

codes = list(range(0x20, 0x7F)) + [0xB0] + list(range(0xE000, 0xE06F)) + [0xB7] + \
    [0x110000 + k for k in range(len(KEYPAD))] + [0xFFFD]
missing = [hex(c) for c in codes if c not in glyphs]
if missing:
    raise SystemExit("no glyph for " + ", ".join(missing))

widths, offsets, bits = [], [], bytearray()
for c in codes:
    w, b = glyphs[c]
    widths.append(w)
    offsets.append(len(bits))
    bits += b

out = []
out.append("/* NumPowder: The Powder Toy's font (resources/font.bz2), the glyphs the interface uses (generated by\n"
           " * tools/gen_font.py: do not edit). Each glyph is 12 rows of its width in pixels, 2 bits of alpha each, four to\n"
           " * a byte, low bits first (FontReader.cpp). Indexed as font.h's text codes: ASCII from 0x20, 0x7F the degree\n"
           " * sign, 0x80 to 0xEE the icons U+E000 to U+E06E, 0xEF the middle dot, 0xF0 to 0xF4 the keypad's symbols (left, right,\n"
           " * up, down, delete: drawn by gen_font.py), then U+FFFD. */")
out.append('#include "font.h"\n')
out.append("const uint8_t font_widths[FONT_GLYPHS] = {")
for k in range(0, len(widths), 24):
    out.append("  " + ", ".join(str(w) for w in widths[k:k + 24]) + ",")
out.append("};")
out.append("const uint16_t font_offsets[FONT_GLYPHS] = {")
for k in range(0, len(offsets), 16):
    out.append("  " + ", ".join(str(o) for o in offsets[k:k + 16]) + ",")
out.append("};")
out.append("const uint8_t font_bits[%d] = {" % len(bits))
for k in range(0, len(bits), 20):
    out.append("  " + ", ".join("0x%02X" % v for v in bits[k:k + 20]) + ",")
out.append("};")
open(OUT, "w").write("\n".join(out) + "\n")
print("wrote %s: %d glyphs, %d bytes" % (OUT, len(codes), len(bits)))