From 0a2593b2c5e24b46bf5db8894a02099af44df6a4 Mon Sep 17 00:00:00 2001 From: Cosimo Lupo Date: Tue, 10 Apr 2018 18:22:32 +0100 Subject: [PATCH] [cu2qu.ufo] remember the curve type in lib to skip converting twice --- Lib/cu2qu/ufo.py | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/Lib/cu2qu/ufo.py b/Lib/cu2qu/ufo.py index 3ef899dac..3c1bfaae1 100644 --- a/Lib/cu2qu/ufo.py +++ b/Lib/cu2qu/ufo.py @@ -40,6 +40,7 @@ from cu2qu.errors import ( __all__ = ['fonts_to_quadratic', 'font_to_quadratic'] DEFAULT_MAX_ERR = 0.001 +CURVE_TYPE_LIB_KEY = "com.github.googlei18n.cu2qu.curve_type" logger = logging.getLogger(__name__) @@ -208,7 +209,7 @@ def glyphs_to_quadratic( def fonts_to_quadratic( fonts, max_err_em=None, max_err=None, reverse_direction=False, - stats=None, dump_stats=False): + stats=None, dump_stats=False, remember_curve_type=True): """Convert the curves of a collection of fonts to quadratic. All curves will be converted to quadratic at once, ensuring interpolation @@ -217,10 +218,30 @@ def fonts_to_quadratic( Return True if fonts were modified, else return False. + 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 + them again if the curve type is already set to "quadratic". + Setting 'remember_curve_type' to False disables this optimization. + Raises IncompatibleFontsError if same-named glyphs from different fonts have non-interpolatable outlines. """ + if remember_curve_type: + curve_types = {f.lib.get(CURVE_TYPE_LIB_KEY, "cubic") for f in fonts} + if len(curve_types) == 1: + curve_type = next(iter(curve_types)) + if curve_type == "quadratic": + logger.info("Curves already converted to quadratic") + return False + elif curve_type == "cubic": + pass # keep converting + else: + raise NotImplementedError(curve_type) + elif len(curve_types) > 1: + # going to crash later if they do differ + logger.warning("fonts may contain different curve types") + if stats is None: stats = {} @@ -265,6 +286,13 @@ def fonts_to_quadratic( spline_lengths = sorted(stats.keys()) logger.info('New spline lengths: %s' % (', '.join( '%s: %d' % (l, stats[l]) for l in spline_lengths))) + + if remember_curve_type: + for font in fonts: + curve_type = font.lib.get(CURVE_TYPE_LIB_KEY, "cubic") + if curve_type != "quadratic": + font.lib[CURVE_TYPE_LIB_KEY] = "quadratic" + modified = True return modified