| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
import os |
| 5 |
import tempfile |
| 6 |
|
| 7 |
import fontforge |
| 8 |
|
| 9 |
font = fontforge.font() |
| 10 |
font.em = 1000 |
| 11 |
|
| 12 |
base = font.createChar(ord("A"), "A") |
| 13 |
base.width = 600 |
| 14 |
pen = base.glyphPen() |
| 15 |
pen.moveTo((100, 100)) |
| 16 |
pen.lineTo((500, 100)) |
| 17 |
pen.lineTo((300, 700)) |
| 18 |
pen.closePath() |
| 19 |
pen = None |
| 20 |
|
| 21 |
ref = font.createChar(ord("B"), "B") |
| 22 |
ref.width = 600 |
| 23 |
ref.addReference("A") |
| 24 |
|
| 25 |
if len(ref.references) != 1: |
| 26 |
raise ValueError("Expected exactly one reference in glyph B") |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
with tempfile.TemporaryDirectory() as temp_dir: |
| 31 |
svg_path = os.path.join(temp_dir, "glyph-B.svg") |
| 32 |
|
| 33 |
ref.export(svg_path) |
| 34 |
|
| 35 |
if not os.path.exists(svg_path): |
| 36 |
raise ValueError("SVG file was not created") |
| 37 |
|
| 38 |
with open(svg_path, "r", encoding="utf-8") as svg_file: |
| 39 |
svg_text = svg_file.read() |
| 40 |
print(svg_text) |
| 41 |
|
| 42 |
if "<path" not in svg_text: |
| 43 |
raise ValueError("Exported SVG does not contain <path") |
| 44 |
|
| 45 |
font.close() |