[cu2qu/ufo] return set of modified glyph names from fonts_to_quadratic

In ufo2ft preProcessor, we need to know which specific glyphs were actually modified (converted to quadratic), in order to do other things while processing filters, not simply if the fonts were modified as a whole; thus, here I changed fonts_to_quadratic to return the set of modified glyph names instead of just True/False. The change is backward compatible because code that checks whether the returned value is True/False will continue to work since bool(set) is True for non-empty set, False for empty ones.
This commit is contained in:
Cosimo Lupo 2024-03-04 12:08:52 +00:00
parent d7eed23e34
commit f02813bd54
No known key found for this signature in database
GPG Key ID: DF65A8A5A119C9A8
2 changed files with 12 additions and 6 deletions

View File

@ -250,7 +250,7 @@ def fonts_to_quadratic(
compatibility. If this is not required, calling fonts_to_quadratic with one compatibility. If this is not required, calling fonts_to_quadratic with one
font at a time may yield slightly more optimized results. font at a time may yield slightly more optimized results.
Return True if fonts were modified, else return False. Return the set of modified glyph names if any, else return an empty set.
By default, cu2qu stores the curve type in the fonts' lib, under a private By default, cu2qu stores the curve type in the fonts' lib, under a private
key "com.github.googlei18n.cu2qu.curve_type", and will not try to convert key "com.github.googlei18n.cu2qu.curve_type", and will not try to convert
@ -296,7 +296,7 @@ def fonts_to_quadratic(
elif max_err_em: elif max_err_em:
max_errors = [f.info.unitsPerEm * max_err_em for f in fonts] max_errors = [f.info.unitsPerEm * max_err_em for f in fonts]
modified = False modified = set()
glyph_errors = {} glyph_errors = {}
for name in set().union(*(f.keys() for f in fonts)): for name in set().union(*(f.keys() for f in fonts)):
glyphs = [] glyphs = []
@ -306,9 +306,10 @@ def fonts_to_quadratic(
glyphs.append(font[name]) glyphs.append(font[name])
cur_max_errors.append(error) cur_max_errors.append(error)
try: try:
modified |= _glyphs_to_quadratic( if _glyphs_to_quadratic(
glyphs, cur_max_errors, reverse_direction, stats, all_quadratic glyphs, cur_max_errors, reverse_direction, stats, all_quadratic
) ):
modified.add(name)
except IncompatibleGlyphsError as exc: except IncompatibleGlyphsError as exc:
logger.error(exc) logger.error(exc)
glyph_errors[name] = exc glyph_errors[name] = exc
@ -329,7 +330,6 @@ def fonts_to_quadratic(
new_curve_type = "quadratic" if all_quadratic else "mixed" new_curve_type = "quadratic" if all_quadratic else "mixed"
if curve_type != new_curve_type: if curve_type != new_curve_type:
font.lib[CURVE_TYPE_LIB_KEY] = new_curve_type font.lib[CURVE_TYPE_LIB_KEY] = new_curve_type
modified = True
return modified return modified
@ -343,7 +343,7 @@ def glyph_to_quadratic(glyph, **kwargs):
def font_to_quadratic(font, **kwargs): def font_to_quadratic(font, **kwargs):
"""Convenience wrapper around fonts_to_quadratic, for just one font. """Convenience wrapper around fonts_to_quadratic, for just one font.
Return True if the font was modified, else return False. Return the set of modified glyph names if any, else return empty set.
""" """
return fonts_to_quadratic([font], **kwargs) return fonts_to_quadratic([font], **kwargs)

View File

@ -35,8 +35,14 @@ def fonts():
class FontsToQuadraticTest(object): class FontsToQuadraticTest(object):
def test_modified(self, fonts): def test_modified(self, fonts):
# previously this method returned True/False, now it returns a set of modified
# glyph names.
modified = fonts_to_quadratic(fonts) modified = fonts_to_quadratic(fonts)
# the first assertion continues to work whether the return value is a bool/set
# so the change is backward compatible
assert modified assert modified
assert len(modified) > 0
assert "B" in modified
def test_stats(self, fonts): def test_stats(self, fonts):
stats = {} stats = {}