make_font_data.py 1.6 KB

1234567891011121314151617181920212223242526272829
  1. # Generates src/ui/CondensedFontData.h: Open Sans Condensed SemiBold and Bold (resources/fonts, SIL OFL 1.1, the same
  2. # Open Sans family LYNX already carries; unmodified files from https://github.com/googlefonts/opensans fonts/ttf), embedded in
  3. # the app for Settings › General › "Condensed labels (FL style)" (round 190je, design190ia FM-5 / FM-S8). Base64 in pieces
  4. # of 4000 characters (every compiler takes string literals of that size); ui/Theme.cpp decodes them once, on first use.
  5. # Run from the repository root: python3 tools/make_font_data.py
  6. import base64
  7. import hashlib
  8. FILES = [("semiBold", "resources/fonts/OpenSans-CondensedSemiBold.ttf"), ("bold", "resources/fonts/OpenSans-CondensedBold.ttf")]
  9. lines = [
  10. "#pragma once",
  11. "",
  12. "// Open Sans Condensed SemiBold and Bold (resources/fonts, SIL Open Font License 1.1: resources/fonts/OFL.txt), base64.",
  13. "// Generated by tools/make_font_data.py - do not edit by hand.",
  14. ]
  15. for name, path in FILES:
  16. data = open(path, "rb").read()
  17. lines.append("// %s: %s, %d bytes, sha256 %s" % (name, path, len(data), hashlib.sha256(data).hexdigest()))
  18. lines += ["", "namespace lynx::condensedfont", "{"]
  19. for name, path in FILES:
  20. text = base64.b64encode(open(path, "rb").read()).decode("ascii")
  21. pieces = [text[i:i + 4000] for i in range(0, len(text), 4000)]
  22. lines.append("inline constexpr const char* %s[] = {" % name)
  23. lines += [' "%s",' % p for p in pieces]
  24. lines.append("};")
  25. lines.append("inline constexpr int %sPieces = %d;" % (name, len(pieces)))
  26. lines += ["} // namespace lynx::condensedfont", ""]
  27. open("src/ui/CondensedFontData.h", "w").write("\n".join(lines))