Skip to content

Commit 011b7e4

Browse files
authored
Merge pull request #10 from anthrotype/cff-set-post-3
remove glyph names from post table when converting to CFF1
2 parents e4d05f8 + 19945e5 commit 011b7e4

2 files changed

Lines changed: 47 additions & 11 deletions

File tree

src/cffsubr/__init__.py

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,8 @@ def subroutinize(
170170
if not inplace:
171171
otf = copy.deepcopy(otf)
172172

173-
glyphOrder = otf.getGlyphOrder()
173+
# ensure the glyph order is decompiled before CFF table is replaced
174+
_ = otf.getGlyphOrder()
174175

175176
buf = io.BytesIO()
176177
otf.save(buf)
@@ -190,16 +191,36 @@ def subroutinize(
190191
and keep_glyph_names
191192
):
192193
# set 'post' to format 2 to keep the glyph names dropped from CFF2
193-
post_table = otf.get("post")
194-
if post_table and post_table.formatType != 2.0:
195-
post_table.formatType = 2.0
196-
post_table.extraNames = []
197-
post_table.mapping = {}
198-
post_table.glyphOrder = glyphOrder
194+
set_post_table_format(otf, 2.0)
195+
elif (
196+
input_format == CFFTableTag.CFF2
197+
and output_format == CFFTableTag.CFF
198+
):
199+
# set 'post' to format 3 so CFF glyph names are not stored twice
200+
# TODO convert to CID when keep_glyph_names=False?
201+
set_post_table_format(otf, 3.0)
199202

200203
return otf
201204

202205

206+
def set_post_table_format(otf, formatType):
207+
if formatType not in (2.0, 3.0):
208+
raise NotImplementedError(formatType)
209+
210+
post = otf.get("post")
211+
if post and post.formatType != formatType:
212+
post.formatType = formatType
213+
if formatType == 2.0:
214+
post.extraNames = []
215+
post.mapping = {}
216+
post.glyphOrder = otf.getGlyphOrder()
217+
else:
218+
for attr in ("extraNames", "mapping"):
219+
if hasattr(post, attr):
220+
delattr(post, attr)
221+
post.glyphOrder = None
222+
223+
203224
def has_subroutines(otf: ttLib.TTFont) -> bool:
204225
"""Return True if the font's CFF or CFF2 table contains any subroutines."""
205226
table_tag = _sniff_cff_table_format(otf)

tests/cffsubr_test.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ def load_test_font(name):
1717
return ttLib.TTFont(buf)
1818

1919

20+
def recompile_font(otf):
21+
buf = io.BytesIO()
22+
otf.save(buf)
23+
buf.seek(0)
24+
return ttLib.TTFont(buf)
25+
26+
2027
class TestSubroutinize:
2128
@pytest.mark.parametrize(
2229
"testfile, table_tag",
@@ -74,13 +81,21 @@ def test_keep_glyph_names(self):
7481
assert font["post"].formatType == 2.0
7582
assert font["post"].glyphOrder == glyph_order
7683

77-
buf = io.BytesIO()
78-
font.save(buf)
79-
buf.seek(0)
80-
font2 = ttLib.TTFont(buf)
84+
font2 = recompile_font(font)
8185

8286
assert font2.getGlyphOrder() == glyph_order
8387

88+
# now convert from CFF2 to CFF1 and check post format is set to 3.0
89+
# https://github.com/adobe-type-tools/cffsubr/issues/8
90+
cffsubr.subroutinize(font2, cff_version=1)
91+
92+
assert font2["post"].formatType == 3.0
93+
assert font2["post"].glyphOrder == None
94+
95+
font3 = recompile_font(font2)
96+
97+
assert font3.getGlyphOrder() == glyph_order
98+
8499
def test_drop_glyph_names(self):
85100
font = load_test_font("SourceSansPro-Regular.subset.ttx")
86101
glyph_order = font.getGlyphOrder()

0 commit comments

Comments
 (0)