-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpatch_marks_rules.py
More file actions
284 lines (246 loc) · 10.7 KB
/
patch_marks_rules.py
File metadata and controls
284 lines (246 loc) · 10.7 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#!/usr/bin/env python3
from fontTools.ttLib import TTFont
from collections import defaultdict, deque
from pathlib import Path
import sys
import offsets_rules as cfg
SKIP_CHARS = {" ", "\t", "\n", "\r"}
# -----------------------------
# Unicode char -> glyph name
# -----------------------------
def glyph_for_char(ttfont: TTFont, ch: str):
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
# -----------------------------
# GSUB expansion (helps italics / alts)
# Covers SingleSubst (1) + AlternateSubst (3)
# -----------------------------
def gsub_targets(tt: TTFont, start_glyph: str, max_depth: int = 4):
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:
lt = lookup.LookupType
for st in lookup.SubTable:
if lt == 1: # SingleSubst
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))
elif lt == 3: # AlternateSubst
cov = getattr(st, "Coverage", None)
altsets = getattr(st, "AlternateSet", None)
if not cov or not altsets:
continue
if g in cov.glyphs:
i = cov.glyphs.index(g)
for tgt in altsets[i].Alternate:
if tgt not in out:
out.add(tgt)
q.append((tgt, d + 1))
return out
# -----------------------------
# Expand strings of chars in rules
# -----------------------------
def expand_chars(s: str):
for ch in s:
if ch in SKIP_CHARS:
continue
yield ch
# -----------------------------
# Iterate relevant GPOS subtables
# -----------------------------
def iter_gpos_mark_subtables(ttfont: TTFont):
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, 6): # MarkToBase, MarkToLigature, MarkToMark
for st in lookup.SubTable:
yield lookup.LookupType, st
# -----------------------------
# Build rule-expanded glyph sets
# -----------------------------
def chars_to_glyphs(tt: TTFont, chars: str, expand_gsub: bool):
"""
Convert a string of 1+ characters to a set of glyph names.
If expand_gsub=True, include likely GSUB-substituted variants too.
"""
out = set()
for ch in expand_chars(chars):
g = glyph_for_char(tt, ch)
if not g:
raise SystemExit(f"ERROR: Could not map '{ch}' (U+{ord(ch):04X}) via cmap.")
if expand_gsub:
out |= gsub_targets(tt, g)
else:
out.add(g)
return out
# -----------------------------
# Patch engine
# -----------------------------
def patch_font(in_path: str, out_path: str, style_cfg: dict):
tt = TTFont(in_path)
if "GPOS" not in tt:
raise SystemExit(f"ERROR: No GPOS table in {in_path}")
# Prepare rules -> glyph sets (GSUB-expand bases, not marks by default)
mark_to_base_rules = []
for base_chars, mark_chars, dx, dy in style_cfg.get("mark_to_base", []):
base_glyphs = chars_to_glyphs(tt, base_chars, expand_gsub=True)
mark_glyphs = chars_to_glyphs(tt, mark_chars, expand_gsub=False)
mark_to_base_rules.append((base_glyphs, mark_glyphs, int(dx), int(dy)))
mark2_deltas = defaultdict(lambda: [0, 0]) # mark2 glyph -> [dx, dy]
for mark2_chars, dx, dy in style_cfg.get("mark_to_mark_mark2", []):
for g in chars_to_glyphs(tt, mark2_chars, expand_gsub=False):
mark2_deltas[g][0] += int(dx)
mark2_deltas[g][1] += int(dy)
mark2_deltas = {g: tuple(v) for g, v in mark2_deltas.items() if v != [0, 0]}
mark1_deltas = defaultdict(lambda: [0, 0]) # mark1 glyph -> [dx, dy]
for mark1_chars, dx, dy in style_cfg.get("mark_to_mark_mark1", []):
for g in chars_to_glyphs(tt, mark1_chars, expand_gsub=False):
mark1_deltas[g][0] += int(dx)
mark1_deltas[g][1] += int(dy)
mark1_deltas = {g: tuple(v) for g, v in mark1_deltas.items() if v != [0, 0]}
if not mark_to_base_rules and not mark2_deltas and not mark1_deltas:
print(f"(No rules for {in_path}; skipping.)")
return False
print(f"\n== {Path(in_path).name}")
if mark_to_base_rules:
print(f" mark_to_base rules: {len(mark_to_base_rules)}")
if mark2_deltas:
print(f" mark_to_mark_mark2 glyphs: {len(mark2_deltas)}")
if mark1_deltas:
print(f" mark_to_mark_mark1 glyphs: {len(mark1_deltas)}")
changed = 0
for ltype, st in iter_gpos_mark_subtables(tt):
# -------------------------
# LookupType 4: MarkToBase
# -------------------------
if ltype == 4 and mark_to_base_rules:
if not all(hasattr(st, a) for a in ("MarkCoverage", "BaseCoverage", "MarkArray", "BaseArray")):
continue
mark_cov = st.MarkCoverage.glyphs
base_cov = st.BaseCoverage.glyphs
for base_glyphs, mark_glyphs, dx, dy in mark_to_base_rules:
# For each mark in this rule that is present in this subtable, find its class
for mg in (mark_glyphs & set(mark_cov)):
m_idx = mark_cov.index(mg)
m_class = st.MarkArray.MarkRecord[m_idx].Class
# For each base glyph in this rule present in this subtable, move only that class anchor
for bg in (base_glyphs & set(base_cov)):
b_idx = base_cov.index(bg)
b_rec = st.BaseArray.BaseRecord[b_idx]
if m_class >= len(b_rec.BaseAnchor):
continue
anchor = b_rec.BaseAnchor[m_class]
if anchor is None:
continue
anchor.XCoordinate += dx
anchor.YCoordinate += dy
changed += 1
# -------------------------
# LookupType 5: MarkToLigature
# -------------------------
elif ltype == 5 and mark_to_base_rules:
if not all(hasattr(st, a) for a in ("MarkCoverage", "LigatureCoverage", "MarkArray", "LigatureArray")):
continue
mark_cov = st.MarkCoverage.glyphs
lig_cov = st.LigatureCoverage.glyphs
for base_glyphs, mark_glyphs, dx, dy in mark_to_base_rules:
# Determine mark classes for marks present here
present_marks = (mark_glyphs & set(mark_cov))
if not present_marks:
continue
mark_classes = []
for mg in present_marks:
m_idx = mark_cov.index(mg)
mark_classes.append(st.MarkArray.MarkRecord[m_idx].Class)
# For each ligature base present here, move anchors for those classes
for bg in (base_glyphs & set(lig_cov)):
l_idx = lig_cov.index(bg)
lig_attach = st.LigatureArray.LigatureAttach[l_idx]
for comp in lig_attach.ComponentRecord:
for m_class in mark_classes:
if m_class >= len(comp.LigatureAnchor):
continue
anchor = comp.LigatureAnchor[m_class]
if anchor is None:
continue
anchor.XCoordinate += dx
anchor.YCoordinate += dy
changed += 1
# -------------------------
# LookupType 6: MarkToMark
# -------------------------
elif ltype == 6 and (mark2_deltas or mark1_deltas):
# Need Mark1Coverage/Mark2Coverage and arrays
if not all(hasattr(st, a) for a in ("Mark1Coverage", "Mark2Coverage", "Mark1Array", "Mark2Array")):
continue
mark1_cov = st.Mark1Coverage.glyphs
mark2_cov = st.Mark2Coverage.glyphs
# mark2: move Mark2Record.Mark2Anchor[class] for matching mark2 glyphs
if mark2_deltas:
for g, (dx, dy) in mark2_deltas.items():
if g not in mark2_cov:
continue
idx = mark2_cov.index(g)
rec = st.Mark2Array.Mark2Record[idx]
for anchor in rec.Mark2Anchor:
if anchor is None:
continue
anchor.XCoordinate += dx
anchor.YCoordinate += dy
changed += 1
# mark1: move MarkRecord.MarkAnchor for matching mark1 glyphs
if mark1_deltas:
for g, (dx, dy) in mark1_deltas.items():
if g not in mark1_cov:
continue
idx = mark1_cov.index(g)
mrec = st.Mark1Array.MarkRecord[idx]
anchor = mrec.MarkAnchor
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}. "
"Common causes: the base/mark isn’t covered by the relevant GPOS lookups, "
"or the mark uses a different attachment class/lookup than expected."
)
tt.save(out_path)
print(f"✓ wrote {out_path} ({changed} anchor edits)")
return True
def main():
any_done = False
for style_name, s in cfg.STYLES.items():
in_path = s["in"]
if not Path(in_path).is_file():
raise SystemExit(f"Missing font file for {style_name}: {in_path}")
out_path = str(Path(in_path).with_stem(Path(in_path).stem + "-patched"))
did = patch_font(in_path, out_path, s)
any_done |= did
if not any_done:
raise SystemExit("No patched fonts produced (all styles skipped).")
if __name__ == "__main__":
main()