def subset_otf_from_ufo(self, otf_path, ufo): """Subset a font using export flags set by glyphsLib. There are two more settings that can change export behavior: "Export Glyphs" and "Remove Glyphs", which are currently not supported for complexity reasons. See https://github.com/googlei18n/glyphsLib/issues/295. """ from fontTools import subset # ufo2ft always inserts a ".notdef" glyph as the first glyph ufo_order = makeOfficialGlyphOrder(ufo) if ".notdef" not in ufo_order: ufo_order.insert(0, ".notdef") ot_order = TTFont(otf_path).getGlyphOrder() assert ot_order[0] == ".notdef" assert len(ufo_order) == len(ot_order) for key in (KEEP_GLYPHS_NEW_KEY, KEEP_GLYPHS_OLD_KEY): keep_glyphs_list = ufo.lib.get(key) if keep_glyphs_list is not None: keep_glyphs = set(keep_glyphs_list) break else: keep_glyphs = None include = [] for source_name, binary_name in zip(ufo_order, ot_order): if keep_glyphs and source_name not in keep_glyphs: continue if source_name in ufo: exported = ufo[source_name].lib.get(GLYPH_EXPORT_KEY, True) if not exported: continue include.append(binary_name) # copied from nototools.subset opt = subset.Options() opt.name_IDs = ['*'] opt.name_legacy = True opt.name_languages = ['*'] opt.layout_features = ['*'] opt.notdef_outline = True opt.recalc_bounds = True opt.recalc_timestamp = True opt.canonical_order = True opt.glyph_names = True font = subset.load_font(otf_path, opt, lazy=False) subsetter = subset.Subsetter(options=opt) subsetter.populate(glyphs=include) subsetter.subset(font) subset.save_font(font, otf_path, opt)
def save_otfs(self, ufos, ttf=False, is_instance=False, interpolatable=False, use_afdko=False, autohint=None, subset=None, use_production_names=None, subroutinize=False, cff_round_tolerance=None, remove_overlaps=True, reverse_direction=True, conversion_error=None, feature_writers=None, interpolate_layout_from=None, interpolate_layout_dir=None, output_path=None, output_dir=None, inplace=True): """Build OpenType binaries from UFOs. Args: ufos: Font objects to compile. ttf: If True, build fonts with TrueType outlines and .ttf extension. is_instance: If output fonts are instances, for generating paths. interpolatable: If output is interpolatable, for generating paths. use_afdko: If True, use AFDKO to compile feature source. autohint: Parameters to provide to ttfautohint. If not provided, the autohinting step is skipped. subset: Whether to subset the output according to data in the UFOs. If not provided, also determined by flags in the UFOs. use_production_names: Whether to use production glyph names in the output. If not provided, determined by flags in the UFOs. subroutinize: If True, subroutinize CFF outlines in output. cff_round_tolerance (float): controls the rounding of point coordinates in CFF table. It is defined as the maximum absolute difference between the original float and the rounded integer value. By default, all floats are rounded to integer (tolerance 0.5); a value of 0 completely disables rounding; values in between only round floats which are close to their integral part within the tolerated range. Ignored if ttf=True. remove_overlaps: If True, remove overlaps in glyph shapes. reverse_direction: If True, reverse contour directions when compiling TrueType outlines. conversion_error: Error to allow when converting cubic CFF contours to quadratic TrueType contours. feature_writers: list of ufo2ft-compatible feature writer classes or pre-initialized objects that are passed on to ufo2ft feature compiler to generate automatic feature code. The default value (None) means that ufo2ft will use its built-in default feature writers (for kern, mark, mkmk, etc.). An empty list ([]) will skip any automatic feature generation. interpolate_layout_from: A designspace path to give varLib for interpolating layout tables to use in output. interpolate_layout_dir: Directory containing the compiled master fonts to use for interpolating binary layout tables. output_path: output font file path. Only works when the input 'ufos' list contains a single font. output_dir: directory where to save output files. Mutually exclusive with 'output_path' argument. """ assert not (output_path and output_dir), "mutually exclusive args" if output_path is not None and len(ufos) > 1: raise ValueError("output_path requires a single input") ext = 'ttf' if ttf else 'otf' if interpolate_layout_from is not None: if interpolate_layout_dir is None: interpolate_layout_dir = self._output_dir( ext, is_instance=False, interpolatable=interpolatable) finder = partial(_varLib_finder, directory=interpolate_layout_dir, ext=ext) # no need to generate automatic features in ufo2ft, since here we # are interpolating precompiled GPOS table with fontTools.varLib. # An empty 'featureWriters' list tells ufo2ft to not generate any # automatic features. # TODO: Add an argument to ufo2ft.compileOTF/compileTTF to # completely skip compiling features into OTL tables feature_writers = [] compiler_options = dict( useProductionNames=use_production_names, reverseDirection=reverse_direction, cubicConversionError=conversion_error, featureWriters=feature_writers, inplace=True, # avoid extra copy ) if use_afdko: compiler_options["featureCompilerClass"] = FDKFeatureCompiler if interpolatable: if not ttf: raise NotImplementedError( "interpolatable CFF not supported yet") logger.info('Building interpolation-compatible TTFs') fonts = compileInterpolatableTTFs(ufos, **compiler_options) else: fonts = self._iter_compile(ufos, ttf, removeOverlaps=remove_overlaps, optimizeCFF=subroutinize, roundTolerance=cff_round_tolerance, **compiler_options) do_autohint = ttf and autohint is not None for font, ufo in zip(fonts, ufos): if interpolate_layout_from is not None: master_locations, instance_locations = self._designspace_locations( interpolate_layout_from) loc = instance_locations[_normpath(ufo.path)] gpos_src = interpolate_layout(interpolate_layout_from, loc, finder, mapped=True) font['GPOS'] = gpos_src['GPOS'] gsub_src = TTFont( finder(self._closest_location(master_locations, loc))) if 'GDEF' in gsub_src: font['GDEF'] = gsub_src['GDEF'] if 'GSUB' in gsub_src: font['GSUB'] = gsub_src['GSUB'] if do_autohint: # if we are autohinting, we save the unhinted font to a # temporary path, and the hinted one to the final destination fd, otf_path = tempfile.mkstemp("." + ext) os.close(fd) elif output_path is None: otf_path = self._output_path(ufo, ext, is_instance, interpolatable, output_dir=output_dir) else: otf_path = output_path logger.info("Saving %s", otf_path) font.save(otf_path) # 'subset' is an Optional[bool], can be None, True or False. # When False, we never subset; when True, we always do; when # None (default), we check the presence of custom parameters if subset is False: pass elif subset is True or ( (KEEP_GLYPHS_OLD_KEY in ufo.lib or KEEP_GLYPHS_NEW_KEY in ufo.lib) or any( glyph.lib.get(GLYPH_EXPORT_KEY, True) is False for glyph in ufo)): self.subset_otf_from_ufo(otf_path, ufo) if not do_autohint: continue if output_path is not None: hinted_otf_path = output_path else: hinted_otf_path = self._output_path(ufo, ext, is_instance, interpolatable, autohinted=True, output_dir=output_dir) try: ttfautohint(otf_path, hinted_otf_path, args=autohint) except TTFAError: # copy unhinted font to destination before re-raising error shutil.copyfile(otf_path, hinted_otf_path) raise finally: # must clean up temp file os.remove(otf_path)