From 99c4546444dac556dc31e84acc3c9d26f9722c9b Mon Sep 17 00:00:00 2001 From: Cosimo Lupo Date: Fri, 22 Jul 2022 16:02:54 +0200 Subject: [PATCH] add option to override FEA include directory; set it to the .glyphs directory when building from Glyphs Fixes https://github.com/googlefonts/glyphsLib/issues/797 Requires https://github.com/googlefonts/ufo2ft/pull/637 --- Lib/fontmake/__main__.py | 11 +++++++++++ Lib/fontmake/font_project.py | 30 +++++++++++++++++++++++++++--- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/Lib/fontmake/__main__.py b/Lib/fontmake/__main__.py index bd9d058b..845e9de4 100644 --- a/Lib/fontmake/__main__.py +++ b/Lib/fontmake/__main__.py @@ -380,6 +380,13 @@ def main(args=None): "interpolating. Use if you share feature files of masters in " "external files, as instances can end up elsewhere.", ) + outputGroup.add_argument( + "--fea-include-dir", + default=None, + help="Overrides the default directory where to search for included " + "feature files with relative paths. This only works when the input is a " + "Designspace or UFOs, not from Glyphs at the moment.", + ) outputGroup.add_argument( "--no-generate-GDEF", dest="generate_GDEF", @@ -641,6 +648,10 @@ def main(args=None): project = FontProject(validate_ufo=args.pop("validate_ufo")) if inputs.glyphs_path: + # we don't support customizing include directory for .glyphs input + # for Glyphs.app does not either. + exclude_args(parser, args, ["fea_include_dir"], inputs.format_name) + with _make_tempdirs(parser, args): project.run_from_glyphs(inputs.glyphs_path, **args) return diff --git a/Lib/fontmake/font_project.py b/Lib/fontmake/font_project.py index ae5bb728..85b7ab89 100644 --- a/Lib/fontmake/font_project.py +++ b/Lib/fontmake/font_project.py @@ -295,6 +295,7 @@ def _build_interpolatable_masters( feature_writers=None, cff_round_tolerance=None, debug_feature_file=None, + fea_include_dir=None, flatten_components=False, filters=None, **kwargs, @@ -307,6 +308,7 @@ def _build_interpolatable_masters( cubicConversionError=conversion_error, featureWriters=feature_writers, debugFeatureFile=debug_feature_file, + feaIncludeDir=fea_include_dir, filters=filters, flattenComponents=flatten_components, inplace=True, @@ -318,6 +320,7 @@ def _build_interpolatable_masters( roundTolerance=cff_round_tolerance, featureWriters=feature_writers, debugFeatureFile=debug_feature_file, + feaIncludeDir=fea_include_dir, filters=filters, inplace=True, ) @@ -349,6 +352,7 @@ def build_variable_fonts( feature_writers=None, cff_round_tolerance=None, debug_feature_file=None, + fea_include_dir=None, flatten_components=False, filters=None, **kwargs, @@ -408,6 +412,7 @@ def build_variable_fonts( optimizeGvar=optimize_gvar, flattenComponents=flatten_components, debugFeatureFile=debug_feature_file, + feaIncludeDir=fea_include_dir, filters=filters, inplace=True, variableFontNames=list(vf_name_to_output_path), @@ -419,6 +424,7 @@ def build_variable_fonts( useProductionNames=use_production_names, roundTolerance=cff_round_tolerance, debugFeatureFile=debug_feature_file, + feaIncludeDir=fea_include_dir, optimizeCFF=optimize_cff, filters=filters, inplace=True, @@ -486,6 +492,7 @@ def save_otfs( flatten_components=False, filters=None, generate_GDEF=True, + fea_include_dir=None, ): """Build OpenType binaries from UFOs. @@ -588,6 +595,7 @@ def save_otfs( cubicConversionError=conversion_error, featureWriters=feature_writers, debugFeatureFile=debug_feature_file, + feaIncludeDir=fea_include_dir, cffVersion=cff_version, subroutinizer=subroutinizer, flattenComponents=flatten_components, @@ -801,8 +809,14 @@ def run_from_glyphs( generate_GDEF=generate_GDEF, ufo_structure=kwargs.get("ufo_structure"), ) + # 'include' statements in features.fea should be resolved relative to + # the input .glyphs path, like Glyphs.app would do, and not relative + # to the UFOs that are generated by glyphsLib. + fea_include_dir = os.path.dirname(glyphs_path) try: - self.run_from_designspace(designspace_path, **kwargs) + self.run_from_designspace( + designspace_path, fea_include_dir=fea_include_dir, **kwargs + ) except FontmakeError as e: e.source_trail.append(glyphs_path) raise @@ -813,6 +827,7 @@ def interpolate_instance_ufos( include=None, round_instances=False, expand_features_to_instances=False, + fea_include_dir=None, ufo_structure="package", ): """Interpolate master UFOs with Instantiator and return instance UFOs. @@ -857,7 +872,9 @@ def interpolate_instance_ufos( if expand_features_to_instances: logger.debug("Expanding features to instance UFOs") - fea_txt = parseLayoutFeatures(subDoc.default.font).asFea() + fea_txt = parseLayoutFeatures( + subDoc.default.font, includeDir=fea_include_dir + ).asFea() generator = attr.evolve(generator, copy_feature_text=fea_txt) for instance in subDoc.instances: @@ -903,6 +920,7 @@ def interpolate_instance_ufos_mutatormath( include=None, round_instances=False, expand_features_to_instances=False, + fea_include_dir=None, ): """Interpolate master UFOs with MutatorMath and return instance UFOs. @@ -965,7 +983,9 @@ def interpolate_instance_ufos_mutatormath( raise ValueError("No source is designated as the master for features.") else: master_source_font = builder.sources[master_source.name][0] - master_source_features = parseLayoutFeatures(master_source_font).asFea() + master_source_features = parseLayoutFeatures( + master_source_font, includeDir=fea_include_dir + ).asFea() for instance_ufo in instance_ufos: instance_ufo.features.text = master_source_features instance_ufo.save() @@ -1111,6 +1131,7 @@ def _run_from_designspace_static( round_instances=False, feature_writers=None, expand_features_to_instances=False, + fea_include_dir=None, use_mutatormath=False, ufo_structure="package", **kwargs, @@ -1127,6 +1148,7 @@ def _run_from_designspace_static( include=pattern, round_instances=round_instances, expand_features_to_instances=expand_features_to_instances, + fea_include_dir=fea_include_dir, ) ) else: @@ -1136,6 +1158,7 @@ def _run_from_designspace_static( include=pattern, round_instances=round_instances, expand_features_to_instances=expand_features_to_instances, + fea_include_dir=fea_include_dir, ufo_structure=ufo_structure, ) ) @@ -1159,6 +1182,7 @@ def _run_from_designspace_static( interpolate_layout_from=interpolate_layout_from, interpolate_layout_dir=interpolate_layout_dir, feature_writers=feature_writers, + fea_include_dir=fea_include_dir, **kwargs, )