aboutsummaryrefslogtreecommitdiffstats
path: root/tools/genfont.py
blob: 7fe7689c0faf2d28161fcf8c733cd7ae472d1f4f (plain) (blame)
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/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")