At least: font code generator, exchange code support for color 128x128
This commit is contained in:
@@ -64,15 +64,14 @@ def ceildiv(num, den):
|
||||
def to_c_name(name):
|
||||
return name.replace("-","_")
|
||||
|
||||
def emit_bmp(output, font_name, char, bmp):
|
||||
def emit_bmp(output, font_name, char, glyph, bmp):
|
||||
|
||||
width_bytes = ceildiv(bmp.width, 8)
|
||||
height_bytes = ceildiv(bmp.height, 8)
|
||||
|
||||
output.write(f"// Bitmap for '{char}'\n")
|
||||
|
||||
name = to_c_name(f"{font_name}_{ord(char)}")
|
||||
output.write(f"static uint8_t {name}_bitmap[] {{\n")
|
||||
output.write(f"static const uint8_t {name}_bitmap[] = {{\n")
|
||||
byte = 0
|
||||
for y in range(bmp.height):
|
||||
output.write(" ")
|
||||
@@ -90,50 +89,86 @@ def emit_bmp(output, font_name, char, bmp):
|
||||
|
||||
output.write(f"// Glyph data for '{char}'\n")
|
||||
|
||||
output.write(f"static struct glyph {name}_glyph {{\n")
|
||||
output.write(f"static const struct glyph {name}_glyph = {{\n")
|
||||
output.write(f" .width = {bmp.width},\n")
|
||||
output.write(f" .width_bytes = {width_bytes},\n")
|
||||
output.write(f" .height = {bmp.height},\n")
|
||||
output.write(f" .height_bytes = {height_bytes},\n")
|
||||
output.write(f" .top = {glyph.top},\n")
|
||||
output.write(f" .left = {glyph.left},\n")
|
||||
output.write(f" .bitmap = {name}_bitmap,\n")
|
||||
output.write("};\n\n")
|
||||
|
||||
|
||||
def emit_font(output, ff, font_name, chars):
|
||||
def emit_font(output, ff, font_name, charset, height, width):
|
||||
c_font_name = to_c_name(font_name)
|
||||
output.write(f"static struct font {c_font_name} {{\n")
|
||||
output.write(f"const struct font {c_font_name} = {{\n")
|
||||
output.write(f" .name = \"{font_name}\",\n")
|
||||
output.write(f" .glypyhs = [\n")
|
||||
for i in range(127):
|
||||
if i in
|
||||
output.write(f" .height = {height},\n")
|
||||
output.write(f" .width = {width},\n")
|
||||
output.write(f" .glyphs = {{\n")
|
||||
for i in range(128):
|
||||
if chr(i) in charset:
|
||||
name = to_c_name(f"{font_name}_{i}_glyph")
|
||||
output.write(f" [{i}] = &{name},\n")
|
||||
else:
|
||||
output.write(f" [{i}] = NULL,\n")
|
||||
output.write(f" }}\n")
|
||||
output.write("};\n\n")
|
||||
|
||||
|
||||
def gen_c_font_file(ttf, output, charset, width=None, size=None, header_dir=None):
|
||||
def gen_c_font_file(ttf, output, charset, width=None, size=None, header_dir=None, font_name=None):
|
||||
|
||||
ff = font.FixedFont(ttf, width=width, height=size)
|
||||
|
||||
if font_name is None:
|
||||
font_name = ff.face.postscript_name.decode('ASCII')
|
||||
|
||||
ff = font.FixedFont(ttf, size)
|
||||
font_name = ff.face.postscript_name.decode('ASCII')
|
||||
emit_license(output)
|
||||
emit_include(output, header_dir)
|
||||
width = None
|
||||
|
||||
max_advance = None
|
||||
max_height = None
|
||||
|
||||
for char in charset:
|
||||
bmp = ff.render_character(char)
|
||||
emit_bmp(output, font_name, char, bmp)
|
||||
if width is None:
|
||||
width = ceildiv(bmp.width, 8)
|
||||
emit_font(output, ff, font_name)
|
||||
glyph = ff.glyph_for_character(char)
|
||||
emit_bmp(output, font_name, char, glyph, bmp)
|
||||
advance = glyph.advance_width
|
||||
if not max_advance or advance > max_advance:
|
||||
max_advance = advance
|
||||
height = glyph.top
|
||||
if not max_height or height > max_height:
|
||||
max_height = height
|
||||
emit_font(output, ff, font_name, charset, max_height, max_advance)
|
||||
|
||||
def gen_h_font_file(ttf, output, header_dir=None, font_name=None):
|
||||
|
||||
if font_name is None:
|
||||
font_name = ff.face.postscript_name.decode('ASCII')
|
||||
|
||||
c_font_name = to_c_name(font_name)
|
||||
define_name = f"_FONT_{c_font_name.upper()}_H_"
|
||||
|
||||
def gen_h_font_file(ttf, output, charset, width=None, size=None):
|
||||
emit_license(output)
|
||||
output.write("\n")
|
||||
output.write(f"#ifndef {define_name}\n")
|
||||
output.write(f"#define {define_name}\n")
|
||||
output.write("\n")
|
||||
emit_include(output, header_dir)
|
||||
output.write(f"extern const struct font {c_font_name};\n")
|
||||
output.write("\n")
|
||||
output.write("#endif")
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description="Fixed-width TrueType C code generator")
|
||||
parser.add_argument(dest="filetype", help="The filetype to produce", action="store", type=str, choices=["c","h"])
|
||||
parser.add_argument(dest="ttf", help="TrueType font file", action="store", type=str)
|
||||
parser.add_argument(dest="output", help="Output C file", action="store", type=argparse.FileType('w'))
|
||||
parser.add_argument(dest="output", help="Output file", action="store", type=argparse.FileType('w'))
|
||||
parser.add_argument("--chars", "-c", help="Character set. Defaults to all printable ASCII", action="store", type=str,
|
||||
default="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-=_+{}[]:\";'<>,.?/~!@#$%^&*()|\\")
|
||||
default="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 -=_+{}[]:\";'<>,.?/~!@#$%^&*()|\\")
|
||||
parser.add_argument("--header-dir", help="Directory of the font.h header", action="store", type=str)
|
||||
parser.add_argument("--name", help="Font name to use in the generated code", action="store", type=str)
|
||||
|
||||
group = parser.add_mutually_exclusive_group(required=True)
|
||||
group.add_argument("--size", "-s", help="Font pitch", action="store", type=int)
|
||||
@@ -143,7 +178,11 @@ def main():
|
||||
args.chars = list(set(args.chars))
|
||||
args.chars.sort()
|
||||
|
||||
gen_c_font_file(args.ttf, args.output, args.chars, width=args.width, size=args.size, header_dir=args.header_dir)
|
||||
if args.filetype == "c":
|
||||
gen_c_font_file(args.ttf, args.output, args.chars, width=args.width,
|
||||
size=args.size, header_dir=args.header_dir, font_name=args.name)
|
||||
elif args.filetype == "h":
|
||||
gen_h_font_file(args.ttf, args.output, header_dir=args.header_dir, font_name=args.name)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user