#!/usr/bin/env python3 # Generate a 4x8 font (3x5 glyphs, left-aligned in a 4px cell) as RGBDS asm. # Output: 96 glyphs (ASCII 32..127), 8 bytes each; each byte = one 4-px row, # bit3=leftmost pixel of the cell, bits 3..1 = the 3px glyph, bit0 = gap. G = {} def d(ch, rows): # rows: 5 strings of width 3 ('#'/' ') G[ch] = rows # space d(' ', [" "," "," "," "," "]) # letters (uppercase; lowercase mapped to these) d('A',["## ","# #","###","# #","# #"]) d('B',["## ","# #","## ","# #","## "]) d('C',[" ##","# ","# ","# "," ##"]) d('D',["## ","# #","# #","# #","## "]) d('E',["###","# ","## ","# ","###"]) d('F',["###","# ","## ","# ","# "]) d('G',[" ##","# ","# #","# #"," ##"]) d('H',["# #","# #","###","# #","# #"]) d('I',["###"," # "," # "," # ","###"]) d('J',[" #"," #"," #","# #"," # "]) d('K',["# #","# #","## ","# #","# #"]) d('L',["# ","# ","# ","# ","###"]) d('M',["# #","###","###","# #","# #"]) d('N',["# #","###","###","###","# #"]) d('O',[" # ","# #","# #","# #"," # "]) d('P',["## ","# #","## ","# ","# "]) d('Q',[" # ","# #","# #"," # "," #"]) d('R',["## ","# #","## ","# #","# #"]) d('S',[" ##","# "," # "," #","## "]) d('T',["###"," # "," # "," # "," # "]) d('U',["# #","# #","# #","# #"," ##"]) d('V',["# #","# #","# #"," # "," # "]) d('W',["# #","# #","###","###","# #"]) d('X',["# #","# #"," # ","# #","# #"]) d('Y',["# #","# #"," # "," # "," # "]) d('Z',["###"," #"," # ","# ","###"]) # digits d('0',[" # ","# #","# #","# #"," # "]) d('1',[" # ","## "," # "," # ","###"]) d('2',["## "," #"," # ","# ","###"]) d('3',["## "," #"," # "," #","## "]) d('4',["# #","# #","###"," #"," #"]) d('5',["###","# ","## "," #","## "]) d('6',[" ##","# ","## ","# #"," # "]) d('7',["###"," #"," # "," # "," # "]) d('8',[" # ","# #"," # ","# #"," # "]) d('9',[" # ","# #"," ##"," #","## "]) # punctuation d('.',[" "," "," "," "," # "]) d(',',[" "," "," "," # ","# "]) d(':',[" "," # "," "," # "," "]) d(';',[" "," # "," "," # ","# "]) d('!',[" # "," # "," # "," "," # "]) d('?',["## "," #"," # "," "," # "]) d('-',[" "," ","###"," "," "]) d('+',[" "," # ","###"," # "," "]) d('=',[" ","###"," ","###"," "]) d('*',["# #"," # ","###"," # ","# #"]) d('/',[" #"," #"," # ","# ","# "]) d('\\',["# ","# "," # "," #"," #"]) d('(',[" #"," # "," # "," # "," #"]) d(')',["# "," # "," # "," # ","# "]) d('[',["## ","# ","# ","# ","## "]) d(']',[" ##"," #"," #"," #"," ##"]) d('<',[" #"," # ","# "," # "," #"]) d('>',["# "," # "," #"," # ","# "]) d('#',["# #","###","# #","###","# #"]) d('$',[" # "," ##","## "," ##","## "]) # rough d('%',["# #"," #"," # ","# ","# #"]) d('&',[" # ","# #"," # ","# #"," ##"]) d("'",[" # "," # "," "," "," "]) d('"',["# #","# #"," "," "," "]) d('@',[" # ","# #","###","# "," ##"]) d('_',[" "," "," "," ","###"]) d('~',[" "," ##","## "," "," "]) d('^',[" # ","# #"," "," "," "]) d('|',[" # "," # "," # "," # "," # "]) d('{',[" #"," # ","## "," # "," #"]) d('}',["# "," # "," ##"," # ","# "]) d('`',["# "," # "," "," "," "]) def glyph_bytes(rows): out=[] # 5 glyph rows in the top; cell is 4px wide, glyph 3px left-aligned (bit3..1) for r in range(8): if r < 5: s = rows[r] v = 0 for i,px in enumerate(s): # i=0 leftmost -> bit3 if px=='#': v |= (1 << (3-i)) out.append(v) else: out.append(0) return out lines=["; auto-generated by tools/genfont.py - 4x8 font (3x5 glyphs), 96 glyphs", "SECTION \"font\", ROM0", "FontData::"] for code in range(32,128): ch = chr(code) key = ch.upper() if ch.isalpha() else ch rows = G.get(key, G.get(ch, ["###","# #","# #","# #","###"])) # box for unknown b = glyph_bytes(rows) lines.append(" db " + ",".join("$%02X"%x for x in b) + " ; '%s'"%(ch if ch!='\\' and code!=127 else '?')) open("/home/user/dev/gbos/src/font.asm","w").write("\n".join(lines)+"\n") print("wrote src/font.inc:", (128-32), "glyphs")