Skip to content

Commit

Permalink
remove glyph names from post table when converting to CFF1
Browse files Browse the repository at this point in the history
  • Loading branch information
anthrotype committed Jun 1, 2020
1 parent e4d05f8 commit 19945e5
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 11 deletions.
35 changes: 28 additions & 7 deletions src/cffsubr/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ def subroutinize(
if not inplace:
otf = copy.deepcopy(otf)

glyphOrder = otf.getGlyphOrder()
# ensure the glyph order is decompiled before CFF table is replaced
_ = otf.getGlyphOrder()

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

return otf


def set_post_table_format(otf, formatType):
if formatType not in (2.0, 3.0):
raise NotImplementedError(formatType)

post = otf.get("post")
if post and post.formatType != formatType:
post.formatType = formatType
if formatType == 2.0:
post.extraNames = []
post.mapping = {}
post.glyphOrder = otf.getGlyphOrder()
else:
for attr in ("extraNames", "mapping"):
if hasattr(post, attr):
delattr(post, attr)
post.glyphOrder = None


def has_subroutines(otf: ttLib.TTFont) -> bool:
"""Return True if the font's CFF or CFF2 table contains any subroutines."""
table_tag = _sniff_cff_table_format(otf)
Expand Down
23 changes: 19 additions & 4 deletions tests/cffsubr_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ def load_test_font(name):
return ttLib.TTFont(buf)


def recompile_font(otf):
buf = io.BytesIO()
otf.save(buf)
buf.seek(0)
return ttLib.TTFont(buf)


class TestSubroutinize:
@pytest.mark.parametrize(
"testfile, table_tag",
Expand Down Expand Up @@ -74,13 +81,21 @@ def test_keep_glyph_names(self):
assert font["post"].formatType == 2.0
assert font["post"].glyphOrder == glyph_order

buf = io.BytesIO()
font.save(buf)
buf.seek(0)
font2 = ttLib.TTFont(buf)
font2 = recompile_font(font)

assert font2.getGlyphOrder() == glyph_order

# now convert from CFF2 to CFF1 and check post format is set to 3.0
# https://github.com/adobe-type-tools/cffsubr/issues/8
cffsubr.subroutinize(font2, cff_version=1)

assert font2["post"].formatType == 3.0
assert font2["post"].glyphOrder == None

font3 = recompile_font(font2)

assert font3.getGlyphOrder() == glyph_order

def test_drop_glyph_names(self):
font = load_test_font("SourceSansPro-Regular.subset.ttx")
glyph_order = font.getGlyphOrder()
Expand Down

0 comments on commit 19945e5

Please sign in to comment.