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__":
|
||||
|
||||
16
gen/font.py
16
gen/font.py
@@ -70,12 +70,13 @@ class Bitmap(object):
|
||||
|
||||
|
||||
class Glyph(object):
|
||||
def __init__(self, pixels, width, height, top, advance_width):
|
||||
def __init__(self, pixels, width, height, top, left, advance_width):
|
||||
self.bitmap = Bitmap(width, height, pixels)
|
||||
|
||||
# The glyph bitmap's top-side bearing, i.e. the vertical distance from the
|
||||
# baseline to the bitmap's top-most scanline.
|
||||
self.top = top
|
||||
self.left = left
|
||||
|
||||
# Ascent and descent determine how many pixels the glyph extends
|
||||
# above or below the baseline.
|
||||
@@ -100,13 +101,14 @@ class Glyph(object):
|
||||
pixels = Glyph.unpack_mono_bitmap(slot.bitmap)
|
||||
width, height = slot.bitmap.width, slot.bitmap.rows
|
||||
top = slot.bitmap_top
|
||||
left = slot.bitmap_left
|
||||
|
||||
# The advance width is given in FreeType's 26.6 fixed point format,
|
||||
# which means that the pixel values are multiples of 64.
|
||||
assert slot.advance.x % 64 == 0
|
||||
advance_width = slot.advance.x // 64
|
||||
|
||||
return Glyph(pixels, width, height, top, advance_width)
|
||||
return Glyph(pixels, width, height, top, left, advance_width)
|
||||
|
||||
@staticmethod
|
||||
def unpack_mono_bitmap(bitmap):
|
||||
@@ -151,12 +153,14 @@ class Glyph(object):
|
||||
|
||||
|
||||
class FixedFont(object):
|
||||
def __init__(self, filename, size):
|
||||
def __init__(self, filename, width=0, height=0):
|
||||
self.face = freetype.Face(filename)
|
||||
self.face.set_pixel_sizes(0, size)
|
||||
if not width and not height:
|
||||
raise ValueError("Width or height must be set non-zero")
|
||||
self.face.set_pixel_sizes(width, height)
|
||||
|
||||
if not self.face.is_fixed_width:
|
||||
raise ValueError("Font is not fixed width")
|
||||
# if not self.face.is_fixed_width:
|
||||
# raise ValueError("Font is not fixed width")
|
||||
|
||||
self.face.load_char('A', freetype.FT_LOAD_RENDER | freetype.FT_LOAD_TARGET_MONO)
|
||||
self.advance = self.face.glyph.advance.x // 64
|
||||
|
||||
2438
gen/output.c
2438
gen/output.c
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user