Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Font generation script #4429

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions core/tools/codegen/gen_font.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ def _normalize(s: str) -> str:
HERE = Path(__file__).parent
CORE_ROOT = HERE.parent.parent
FONTS_DIR = HERE / "fonts"
OUT_DIR = HERE / ".." / ".." / "embed" / "drv" / "gfx" / "fonts"
C_FONTS_DEST = CORE_ROOT / "embed" / "drv" / "gfx" / "fonts"
OUT_DIR = HERE / ".." / ".." / "embed" / "gfx" / "fonts"
C_FONTS_DEST = CORE_ROOT / "embed" / "gfx" / "fonts"
JSON_FONTS_DEST = CORE_ROOT / "translations" / "fonts"

MIN_GLYPH = ord(" ")
Expand Down Expand Up @@ -276,6 +276,8 @@ def __init__(
self.fontname = f"{name.lower()}_{style.lower()}_{size}"
self.font_ymin = 0
self.font_ymax = 0
self.mem_footprint = 0 # size of all font characters in uC in Bytes
self.glyph_count = 0 # number of characters in the font

@property
def _name_style_size(self) -> str:
Expand Down Expand Up @@ -304,6 +306,8 @@ def write_c_files(self) -> None:

def write_foreign_json(self, upper_cased=False) -> None:
for lang, language_chars in all_languages.items():
mem_footprint = 0 # size of all font characters in uC in Bytes
glyph_count = 0 # number of characters in the font
fontdata = {}
for item in language_chars:
c = _normalize(item)
Expand All @@ -317,12 +321,15 @@ def write_foreign_json(self, upper_cased=False) -> None:
glyph = Glyph.from_face(self.face, c, self.shaveX)
glyph.print_metrics()
fontdata[map_from] = glyph.to_bytes(self.bpp).hex()
mem_footprint += len(glyph.to_bytes(self.bpp))
glyph_count += 1
file = (
JSON_FONTS_DEST
/ f"font_{self.fontname}{'_upper' if upper_cased else ''}_{lang}.json"
)
json_content = json.dumps(fontdata, indent=2, ensure_ascii=False)
file.write_text(json_content + "\n")
print(f"Memory footprint of {self.name} {self.style} {self.size} {lang} extension set is {glyph_count} characters {mem_footprint} Bytes")

def write_char_widths_files(self) -> None:
chars: set[str] = set()
Expand Down Expand Up @@ -397,10 +404,15 @@ def _write_c_file_content(self, f: TextIO) -> None:
f.write(f" Font_{self._name_style_size}_glyph_{i},{comment}\n")
f.write("};\n")

print(f"Memory footprint of {self.name} {self.style} {self.size} basic character set is {self.glyph_count} characters {self.mem_footprint} Bytes")

def _write_char_definition(self, f: TextIO, c: str, i: int) -> None:
self._load_char(c)
glyph = Glyph.from_face(self.face, c, self.shaveX)
glyph.print_metrics()
self.mem_footprint += len(glyph.to_bytes(self.bpp))
self.glyph_count += 1

definition_line = glyph.get_definition_line(self._name_style_size, self.bpp, i)
f.write(definition_line)

Expand All @@ -424,6 +436,8 @@ def _get_nonprintable_definition_line(self, upper: bool = False) -> str:
c = "?"
self._load_char(c)
glyph = Glyph.from_face(self.face, c, self.shaveX, inverse_colors=True)
self.mem_footprint += len(glyph.to_bytes(self.bpp))
self.glyph_count += 1
if upper:
return glyph.get_definition_line(
f"{self._name_style_size}_upper", self.bpp, "nonprintable", static=False
Expand Down
Loading