Skip to content

Commit

Permalink
[preProcessor] add inplace=False argument
Browse files Browse the repository at this point in the history
By default, we still make a copy of the input glyphs before applying
filters (e.g. needed when compiling fonts from editors like TruFont).
Sometimes it may be slightly faster to avoid the copy, and apply the
filters in-place. E.g. fontmake reads the UFO from file without
saving the modified Defcon objects.
  • Loading branch information
anthrotype committed Oct 25, 2017
1 parent 47f5cd2 commit 2efc79b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
12 changes: 8 additions & 4 deletions Lib/ufo2ft/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ def compileOTF(ufo, preProcessorClass=OTFPreProcessor,
featureCompilerClass=FeatureCompiler,
kernWriterClass=KernFeatureWriter, markWriterClass=MarkFeatureWriter,
glyphOrder=None, useProductionNames=True, optimizeCFF=True,
roundTolerance=None, removeOverlaps=False):
roundTolerance=None, removeOverlaps=False,
inplace=False):
"""Create FontTools CFF font from a UFO.
*removeOverlaps* performs a union operation on all the glyphs' contours.
Expand All @@ -32,7 +33,8 @@ def compileOTF(ufo, preProcessorClass=OTFPreProcessor,
of 0 completely disables rounding; values in between only round floats
which are close to their integral part within the tolerated range.
"""
preProcessor = preProcessorClass(ufo, removeOverlaps=removeOverlaps)
preProcessor = preProcessorClass(
ufo, inplace=inplace, removeOverlaps=removeOverlaps)
glyphSet = preProcessor.process()

outlineCompiler = outlineCompilerClass(
Expand All @@ -57,7 +59,8 @@ def compileTTF(ufo, preProcessorClass=TTFPreProcessor,
kernWriterClass=KernFeatureWriter, markWriterClass=MarkFeatureWriter,
glyphOrder=None, useProductionNames=True,
convertCubics=True, cubicConversionError=None,
reverseDirection=True, removeOverlaps=False):
reverseDirection=True, removeOverlaps=False,
inplace=False):
"""Create FontTools TrueType font from a UFO.
*removeOverlaps* performs a union operation on all the glyphs' contours.
Expand All @@ -66,7 +69,8 @@ def compileTTF(ufo, preProcessorClass=TTFPreProcessor,
to quadratic curves should be handled.
"""
preProcessor = preProcessorClass(
ufo, removeOverlaps=removeOverlaps,
ufo, inplace=inplace,
removeOverlaps=removeOverlaps,
convertCubics=convertCubics,
conversionError=cubicConversionError,
reverseDirection=reverseDirection)
Expand Down
13 changes: 9 additions & 4 deletions Lib/ufo2ft/preProcessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ class BasePreProcessor(object):
the UFO glyphs, such as decomposing composites, removing overlaps, or
applying custom filters.
The input UFO is **not** modified. The ``process`` method returns a
dictionary containing the modified glyphset, keyed by glyph name.
By default the input UFO is **not** modified. The ``process`` method
returns a dictionary containing the new modified glyphset, keyed by
glyph name. If ``inplace`` is True, the input UFO is modified directly
without the need to first copy the glyphs.
Subclasses can override the ``initDefaultFilters`` method and return
a list of built-in filters which are performed in a predefined order,
Expand All @@ -26,9 +28,12 @@ class BasePreProcessor(object):
"com.github.googlei18n.ufo2ft.filters".
"""

def __init__(self, ufo, **kwargs):
def __init__(self, ufo, inplace=False, **kwargs):
self.ufo = ufo
self.glyphSet = {g.name: _copyGlyph(g) for g in ufo}
if inplace:
self.glyphSet = {g.name: g for g in ufo}
else:
self.glyphSet = {g.name: _copyGlyph(g) for g in ufo}
self.defaultFilters = self.initDefaultFilters(**kwargs)
self.preFilters, self.postFilters = loadFilters(self.ufo)

Expand Down

0 comments on commit 2efc79b

Please sign in to comment.