Files
Triforce-Source/nx2/ui_fonts.py
ruckus7382sfgsgdf 34b3683441 Triforce v0.2.0
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 19:32:48 -06:00

45 lines
1.9 KiB
Python

"""The overlays' bundled fonts (gpu/vk/vk_imgui.cpp): third_party/fonts/libertinus's TTF files as byte arrays in a C++ file of the
build's own, compiled into the Vulkan back end. They stand in when the player's game files do not have the game's own menu
font (which is read from them at run time and never copied anywhere).
python nx2/ui_fonts.py [out.cpp]
"""
import os
import sys
ROOT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
FONTS = os.path.join(ROOT, "third_party", "fonts", "libertinus")
# (the symbol, the file: vk_imgui.cpp declares the same names)
FILES = (("hookshot_font_body", "LibertinusSans-Regular.ttf"), ("hookshot_font_strong", "LibertinusSans-Bold.ttf"),
("hookshot_font_heading", "LibertinusSerif-Semibold.ttf"))
def source():
lines = ["// generated by nx2/ui_fonts.py from third_party/fonts/libertinus (SIL Open Font License 1.1): do not edit"]
for name, file in FILES:
data = open(os.path.join(FONTS, file), "rb").read()
lines.append("extern const unsigned %s_size = %d;" % (name, len(data)))
lines.append("extern const unsigned char %s[%d] = {" % (name, len(data)))
for i in range(0, len(data), 48):
lines.append(",".join(str(b) for b in data[i:i + 48]) + ",")
lines.append("};")
return "\n".join(lines) + "\n"
def write(out_path):
"""Writes the file when it differs (an unchanged one keeps its timestamp); returns the fonts' bytes, for the object's key"""
text = source()
os.makedirs(os.path.dirname(out_path), exist_ok=True)
try:
same = open(out_path, encoding="utf-8").read() == text
except OSError:
same = False
if not same:
with open(out_path, "w", encoding="utf-8", newline="\n") as f:
f.write(text)
return b"".join(open(os.path.join(FONTS, file), "rb").read() for _, file in FILES)
if __name__ == "__main__":
write(sys.argv[1] if len(sys.argv) > 1 else os.path.join(ROOT, "ui_fonts.cpp"))