From 6f317aa362329a8587d2a9c06dd2bd37d209de11 Mon Sep 17 00:00:00 2001 From: Cosimo Lupo Date: Thu, 20 Dec 2018 12:47:37 +0000 Subject: [PATCH] ttFont: add copy() method that return new instance with same data and use it in varLib to copy master font --- Lib/fontTools/ttLib/ttFont.py | 13 +++++++++++++ Lib/fontTools/varLib/__init__.py | 15 +-------------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/Lib/fontTools/ttLib/ttFont.py b/Lib/fontTools/ttLib/ttFont.py index 3a89592ff..82a35fdfc 100644 --- a/Lib/fontTools/ttLib/ttFont.py +++ b/Lib/fontTools/ttLib/ttFont.py @@ -693,6 +693,19 @@ class TTFont(object): """ return self["cmap"].getBestCmap(cmapPreferences=cmapPreferences) + def copy(self): + """Return a new TTFont instance containing the same data. + + >>> f1 = TTFont() + >>> f2 = f1.copy() + >>> f2 is not f1 + True + """ + buf = BytesIO() + self.save(buf) + buf.seek(0) + return TTFont(buf) + class _TTGlyphSet(object): diff --git a/Lib/fontTools/varLib/__init__.py b/Lib/fontTools/varLib/__init__.py index 76b5ad83e..ead9bec51 100644 --- a/Lib/fontTools/varLib/__init__.py +++ b/Lib/fontTools/varLib/__init__.py @@ -38,7 +38,6 @@ from fontTools.varLib.iup import iup_delta_optimize from fontTools.varLib.featureVars import addFeatureVariations from fontTools.designspaceLib import DesignSpaceDocument, AxisDescriptor from collections import OrderedDict, namedtuple -import io import os.path import logging from pprint import pformat @@ -737,18 +736,6 @@ def load_designspace(designspace): ) -def _copy_font(ttFont): - try: - filename = ttFont.reader.file.name - except AttributeError: - buf = io.BytesIO() - ttFont.save(buf) - buf.seek(0) - return TTFont(buf) - else: - return TTFont(filename) - - def build(designspace, master_finder=lambda s:s, exclude=[], optimize=True): """ Build variation font from a designspace file. @@ -777,7 +764,7 @@ def build(designspace, master_finder=lambda s:s, exclude=[], optimize=True): master_ttfs.append(None) # in-memory fonts have no path # Copy the base master to work from it - vf = _copy_font(master_fonts[ds.base_idx]) + vf = master_fonts[ds.base_idx].copy() # TODO append masters as named-instances as well; needs .designspace change. fvar = _add_fvar(vf, ds.axes, ds.instances)