Files
texrra-engine/scripts/build-math-outlines.py
T

34 lines
1.1 KiB
Python

#!/usr/bin/env python3
"""Generate SVG paths from the exact Type1 programs embedded in math PDFs.
Requires fontTools only when regenerating. Runtime previews use the bundled
paths, so they need neither installed fonts nor an external conversion tool.
"""
import json
from pathlib import Path
from fontTools.pens.svgPathPen import SVGPathPen
from fontTools.t1Lib import T1Font
ROOT = Path(__file__).resolve().parent.parent
NAMES = ["cmr10", "cmr7", "cmr5", "cmmi10", "cmmi7", "cmmi5",
"cmsy10", "cmsy7", "cmsy5", "cmex10"]
result = []
for name in NAMES:
font = T1Font(str(ROOT / "assets/fonts" / (name + ".pfb")))
glyphs = font.getGlyphSet()
encoding = font["Encoding"]
paths = []
for slot in range(256):
glyph = encoding[slot]
if glyph == ".notdef" or glyph not in glyphs:
paths.append("")
continue
pen = SVGPathPen(glyphs)
glyphs[glyph].draw(pen)
paths.append(pen.getCommands())
result.append(paths)
output = ROOT / "crates/texrra-tex-core/assets/math-outlines.json"
output.write_text(json.dumps(result, separators=(",", ":")) + "\n")
print(output)