-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpatch_libertinus_marks.py
More file actions
191 lines (159 loc) · 6.35 KB
/
Copy pathpatch_libertinus_marks.py
File metadata and controls
191 lines (159 loc) · 6.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/env python3
from fontTools.ttLib import TTFont
from collections import defaultdict
from pathlib import Path
import offsets # config file
from collections import defaultdict, deque
SKIP_CHARS = {" ", "\t", "\n", "\r"}
def glyph_for_char(ttfont: TTFont, ch: str):
"""Resolve a single Unicode character to a glyph name via cmap."""
uni = ord(ch)
cmap = ttfont.getBestCmap()
if cmap and uni in cmap:
return cmap[uni]
for st in ttfont["cmap"].tables:
if st.isUnicode() and uni in st.cmap:
return st.cmap[uni]
return None
def iter_mark_subtables(ttfont: TTFont):
"""Yield (lookupType, subtable) for MarkToBase (4) and MarkToLigature (5)."""
if "GPOS" not in ttfont:
return
gpos = ttfont["GPOS"].table
if not getattr(gpos, "LookupList", None):
return
for lookup in gpos.LookupList.Lookup:
if lookup.LookupType in (4, 5):
for st in lookup.SubTable:
yield lookup.LookupType, st
def gsub_targets(tt: TTFont, start_glyph: str, max_depth: int = 4):
"""
Return a set of glyphs reachable from start_glyph via GSUB substitutions.
Covers:
- LookupType 1 (SingleSubst)
- LookupType 3 (AlternateSubst)
Limited depth to avoid weird cycles.
"""
out = set([start_glyph])
if "GSUB" not in tt:
return out
gsub = tt["GSUB"].table
if not getattr(gsub, "LookupList", None):
return out
q = deque([(start_glyph, 0)])
while q:
g, d = q.popleft()
if d >= max_depth:
continue
for lookup in gsub.LookupList.Lookup:
for st in lookup.SubTable:
# SingleSubst
if lookup.LookupType == 1:
m = getattr(st, "mapping", None)
if m and g in m:
tgt = m[g]
if tgt not in out:
out.add(tgt)
q.append((tgt, d + 1))
# AlternateSubst
elif lookup.LookupType == 3:
cov = getattr(st, "Coverage", None)
alts = getattr(st, "AlternateSet", None)
if not cov or not alts:
continue
if g in cov.glyphs:
i = cov.glyphs.index(g)
for tgt in alts[i].Alternate:
if tgt not in out:
out.add(tgt)
q.append((tgt, d + 1))
return out
def build_glyph_delta_map_from_offsets(tt: TTFont, offsets_list):
"""
offsets_list: [(letters, dx, dy), ...] where letters is 1+ chars.
Applies dx/dy to each character, and ALSO to likely GSUB-substituted variants
of the base glyph, so italics/locl/alts still move.
"""
acc = defaultdict(lambda: [0, 0]) # glyph -> [dx, dy]
for letters, dx, dy in offsets_list:
if not isinstance(letters, str) or not letters:
raise SystemExit(f"ERROR: letters must be a non-empty string, got {letters!r}")
dx = int(dx); dy = int(dy)
for ch in letters:
if ch in SKIP_CHARS:
continue
base = glyph_for_char(tt, ch)
if not base:
raise SystemExit(f"ERROR: Could not map '{ch}' (U+{ord(ch):04X}) via cmap.")
# Patch base glyph + GSUB-derived variants (without overwriting)
for g in gsub_targets(tt, base):
acc[g][0] += dx
acc[g][1] += dy
return {g: (dx, dy) for g, (dx, dy) in acc.items() if dx or dy}
def patch_font(in_path: str, out_path: str, offsets_list):
tt = TTFont(in_path)
if "GPOS" not in tt:
raise SystemExit(f"ERROR: No GPOS table in {in_path}")
glyph_deltas = build_glyph_delta_map_from_offsets(tt, offsets_list)
if not glyph_deltas:
print(f"(No offsets for {in_path}; skipping.)")
return False
print(f"\n== {Path(in_path).name}")
print("Targets (glyph -> dx, dy):")
for g, (dx, dy) in glyph_deltas.items():
print(f" {g}: dx={dx}, dy={dy}")
changed = 0
for ltype, st in iter_mark_subtables(tt):
# LookupType 4: MarkToBase
if ltype == 4:
if not (hasattr(st, "BaseCoverage") and hasattr(st, "BaseArray")):
continue
base_glyphs = st.BaseCoverage.glyphs
for g, (dx, dy) in glyph_deltas.items():
if g not in base_glyphs:
continue
rec = st.BaseArray.BaseRecord[base_glyphs.index(g)]
for anchor in rec.BaseAnchor:
if anchor is None:
continue
anchor.XCoordinate += dx
anchor.YCoordinate += dy
changed += 1
# LookupType 5: MarkToLigature
elif ltype == 5:
if not (hasattr(st, "LigatureCoverage") and hasattr(st, "LigatureArray")):
continue
lig_glyphs = st.LigatureCoverage.glyphs
for g, (dx, dy) in glyph_deltas.items():
if g not in lig_glyphs:
continue
attach = st.LigatureArray.LigatureAttach[lig_glyphs.index(g)]
for comp in attach.ComponentRecord:
for anchor in comp.LigatureAnchor:
if anchor is None:
continue
anchor.XCoordinate += dx
anchor.YCoordinate += dy
changed += 1
if changed == 0:
raise SystemExit(
f"ERROR: No anchors changed in {in_path}. "
"Possibly the target glyphs are not covered by MarkToBase/MarkToLigature in this font."
)
tt.save(out_path)
print(f"✓ wrote {out_path} ({changed} anchors changed)")
return True
def main():
any_done = False
for style_name, cfg in offsets.STYLES.items():
in_path = cfg["in"]
out_path = str(Path(in_path).with_stem(Path(in_path).stem + "-patched"))
offs = cfg.get("offsets", [])
if not Path(in_path).is_file():
raise SystemExit(f"Missing font file for {style_name}: {in_path}")
did = patch_font(in_path, out_path, offs)
any_done |= did
if not any_done:
raise SystemExit("No patched fonts produced.")
if __name__ == "__main__":
main()