Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

3.1 KB
Raw Blame
#!/usr/bin/env python3
"""Pre-rasterize OFL Jgs7 into an ASCII-only, 2-bit fixed-cell atlas.

Derived atlas name: Habitat ASCII Art 28. Jgs7 is a Reserved Font Name.
Copyright (c) 2022 Adel Faure; original distributed by Velvetyne.
Source and full license: fonts/jgs7.ttf and fonts/Jgs-OFL.txt.
"""
from pathlib import Path
from PIL import Image, ImageDraw, ImageFont

root = Path(__file__).resolve().parents[1]
def pack(im):
    values = [min(3, (v+42)//85) for v in im.get_flattened_data()]
    values += [0] * (-len(values) % 4)
    return [sum(values[j+k] << (6-2*k) for k in range(4)) for j in range(0, len(values), 4)]

atlases = []
for height in (12, 14, 16, 20, 28, 48):
    width = height // 2
    font = ImageFont.truetype(str(root / 'fonts/jgs7.ttf'), height)
    data = []
    for cp in range(32, 127):
        im = Image.new('L', (width, height))
        ImageDraw.Draw(im).text((0, round(height*22/28)), chr(cp), font=font, fill=255, anchor='ls')
        data.extend(pack(im))
    atlases.append((f'ht_ascii_art_{height}',32,126,width,height,data))

# Standard Unicode block/shade glyphs, authored geometrically. No character image
# is stored in the font: the same tiny alphabet is reused by every text frame.
width, height = 8, 16
data = []
square_data = []
for cp in range(0x2580, 0x25a0):
    im = Image.new('L', (width,height)); px = im.load()
    for y in range(height):
        for x in range(width):
            on = False
            if cp == 0x2580: on = y < height//2
            elif 0x2581 <= cp <= 0x2588: on = y >= height-(cp-0x2580)*height//8
            elif 0x2589 <= cp <= 0x258f: on = x < (0x2590-cp)*width//8
            elif cp == 0x2590: on = x >= width//2
            elif cp in (0x2591,0x2592,0x2593):
                rank = ((0,2),(3,1))[y%2][x%2]
                on = rank < cp-0x2590
            elif cp == 0x2594: on = y < height//8
            elif cp == 0x2595: on = x >= width-width//8
            elif cp >= 0x2596:
                masks = (4,8,1,13,9,7,11,2,6,14)
                bit = (1 if x<width//2 else 2) * (1 if y<height//2 else 4)
                on = bool(masks[cp-0x2596]&bit)
            px[x,y] = 255 if on else 0
    data.extend(pack(im))
    square_data.extend(pack(im.resize((10,10),Image.Resampling.NEAREST)))
atlases.append(('ht_block_art_16',0x2580,0x259f,width,height,data))
atlases.append(('ht_block_art_square_10',0x2580,0x259f,10,10,square_data))
out = root / 'main/ui/habitat/creature_font.c'
with out.open('w') as f:
    f.write('// Generated by gen_creature_font.py. Habitat ASCII Art 28.\n')
    f.write('// Derived from Jgs7, (c) 2022 Adel Faure, Velvetyne; SIL OFL 1.1.\n')
    f.write('// This atlas remains OFL-licensed; see fonts/Jgs-OFL.txt.\n#include "creature_gallery.h"\n')
    for name,first,last,width,height,data in atlases:
        f.write(f'static const uint8_t {name}_pixels[] = {{\n')
        for i in range(0,len(data),24):
            f.write('    '+','.join(map(str,data[i:i+24]))+',\n')
        f.write(f'}};\nconst ht_font_t {name} = {{{first},{last},{width},{height},{name}_pixels}};\n')
        print(f'{name}: {len(data)} bytes, {width}x{height} cells')