In order to be able to optimize the output better, add my own TTF generator. Fortunately, freetype-py handles all of the hard work. The generator is not yet integrated.
151 lines
5.7 KiB
Python
Executable File
151 lines
5.7 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
|
|
# Copyright (C) 2019 Max Regan
|
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
# of this software and associated documentation files (the "Software"), to deal
|
|
# in the Software without restriction, including without limitation the rights
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
# copies of the Software, and to permit persons to whom the Software is
|
|
# furnished to do so, subject to the following conditions:
|
|
# The above copyright notice and this permission notice shall be included in
|
|
# all copies or substantial portions of the Software.
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
# THE SOFTWARE.
|
|
|
|
import font
|
|
import argparse
|
|
import string
|
|
|
|
def emit_license(output):
|
|
output.write(
|
|
"""/*
|
|
* Copyright (C) 2019 Max Regan
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
* in the Software without restriction, including without limitation the rights
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
* furnished to do so, subject to the following conditions:
|
|
* The above copyright notice and this permission notice shall be included in
|
|
* all copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
* THE SOFTWARE.
|
|
*/
|
|
|
|
""")
|
|
|
|
def emit_include(output, header_dir):
|
|
if header_dir:
|
|
if header_dir[-1] == '/':
|
|
header_dir = header_dir[:-1]
|
|
header = header_dir + "/font.h"
|
|
else:
|
|
header = "font.h"
|
|
|
|
output.write(f"#include \"{header}\"\n\n")
|
|
|
|
def ceildiv(num, den):
|
|
return (num + (den - 1)) // den
|
|
|
|
def to_c_name(name):
|
|
return name.replace("-","_")
|
|
|
|
def emit_bmp(output, font_name, char, 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")
|
|
byte = 0
|
|
for y in range(bmp.height):
|
|
output.write(" ")
|
|
for x in range(bmp.width):
|
|
bit_idx = 7 - (x % 8)
|
|
bit = bmp.pixels[y * bmp.width + x]
|
|
byte |= bit << bit_idx
|
|
if bit_idx == 0 or x == bmp.width - 1:
|
|
output.write('0x{:02x}, '.format(byte))
|
|
byte = 0
|
|
|
|
output.write("\n")
|
|
|
|
output.write("};\n\n")
|
|
|
|
output.write(f"// Glyph data for '{char}'\n")
|
|
|
|
output.write(f"static 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" .bitmap = {name}_bitmap,\n")
|
|
output.write("};\n\n")
|
|
|
|
|
|
def emit_font(output, ff, font_name, chars):
|
|
c_font_name = to_c_name(font_name)
|
|
output.write(f"static 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("};\n\n")
|
|
|
|
|
|
def gen_c_font_file(ttf, output, charset, width=None, size=None, header_dir=None):
|
|
|
|
ff = font.FixedFont(ttf, size)
|
|
font_name = ff.face.postscript_name.decode('ASCII')
|
|
emit_license(output)
|
|
emit_include(output, header_dir)
|
|
width = 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)
|
|
|
|
def gen_h_font_file(ttf, output, charset, width=None, size=None):
|
|
emit_license(output)
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description="Fixed-width TrueType C code generator")
|
|
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("--chars", "-c", help="Character set. Defaults to all printable ASCII", action="store", type=str,
|
|
default="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-=_+{}[]:\";'<>,.?/~!@#$%^&*()|\\")
|
|
parser.add_argument("--header-dir", help="Directory of the font.h header", action="store", type=str)
|
|
|
|
group = parser.add_mutually_exclusive_group(required=True)
|
|
group.add_argument("--size", "-s", help="Font pitch", action="store", type=int)
|
|
group.add_argument("--width", "-w", help="Font width", action="store", type=int)
|
|
|
|
args = parser.parse_args()
|
|
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 __name__ == "__main__":
|
|
main()
|