ttFont: add copy() method that return new instance with same data

and use it in varLib to copy master font
This commit is contained in:
Cosimo Lupo 2018-12-20 12:47:37 +00:00
parent 04d506072c
commit 6f317aa362
No known key found for this signature in database
GPG Key ID: 59D54DB0C9976482
2 changed files with 14 additions and 14 deletions

View File

@ -693,6 +693,19 @@ class TTFont(object):
""" """
return self["cmap"].getBestCmap(cmapPreferences=cmapPreferences) 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): class _TTGlyphSet(object):

View File

@ -38,7 +38,6 @@ from fontTools.varLib.iup import iup_delta_optimize
from fontTools.varLib.featureVars import addFeatureVariations from fontTools.varLib.featureVars import addFeatureVariations
from fontTools.designspaceLib import DesignSpaceDocument, AxisDescriptor from fontTools.designspaceLib import DesignSpaceDocument, AxisDescriptor
from collections import OrderedDict, namedtuple from collections import OrderedDict, namedtuple
import io
import os.path import os.path
import logging import logging
from pprint import pformat 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): def build(designspace, master_finder=lambda s:s, exclude=[], optimize=True):
""" """
Build variation font from a designspace file. 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 master_ttfs.append(None) # in-memory fonts have no path
# Copy the base master to work from it # 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. # TODO append masters as named-instances as well; needs .designspace change.
fvar = _add_fvar(vf, ds.axes, ds.instances) fvar = _add_fvar(vf, ds.axes, ds.instances)