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.

1.6 KB
Raw Blame
#!/usr/bin/env python3
"""Rasterises the doodle's PixelMplus10 font at its native 10 px into src/font_data.c.

    python3 tools/font.py [DOODLE_DIR]
"""
import os
import sys

from PIL import Image, ImageDraw, ImageFont

ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SRC = sys.argv[1] if len(sys.argv) > 1 else os.path.join(ROOT, '../../../potherca-blog/google-doodle-champion-island')
font = ImageFont.truetype(os.path.join(SRC, 'PixelMplus10-Regular.ttf'), 10)
CHARS = [chr(c) for c in range(32, 127)] + list('àáéíóúñçü–‘’“”…←↑→↓♥•×·')

rows, glyphs = [], []
for ch in CHARS:
    adv = int(round(font.getlength(ch)))
    im = Image.new('L', (16, 12), 0)
    ImageDraw.Draw(im).text((0, 0), ch, font=font, fill=255)
    bits = []
    for y in range(11):
        v = 0
        for x in range(12):
            if im.getpixel((x, y)) > 127:
                v |= 1 << x
        bits.append(v)
    glyphs.append((ord(ch), adv, bits))

out = ['/* Generated by tools/font.py from PixelMplus10 (M+ FONTS license). Do not edit. */', '#include <stdint.h>',
       '#define FONT_GLYPHS %d' % len(glyphs), 'const uint16_t font_code[FONT_GLYPHS] = {' + ','.join(str(g[0]) for g in glyphs) + '};',
       'const uint8_t font_adv[FONT_GLYPHS] = {' + ','.join(str(g[1]) for g in glyphs) + '};',
       'const uint16_t font_bits[FONT_GLYPHS][11] = {']
for g in glyphs:
    out.append('  {' + ','.join(str(b) for b in g[2]) + '},')
out.append('};')
out.append('const unsigned font_glyphs = FONT_GLYPHS;')
open(os.path.join(ROOT, 'src/font_data.c'), 'w').write('\n'.join(out) + '\n')
print(len(glyphs), 'glyphs')