From 28fae1d95f29f5fc866c439a391ff2ee138842d6 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 3 Mar 2021 18:27:06 -0700 Subject: [PATCH] [cff] Simplify rounding logic No semantic change. Just refactoring and simplification in anticipation of coming changes. --- Lib/fontTools/pens/t2CharStringPen.py | 13 ++++++------- Lib/fontTools/varLib/cff.py | 17 +++-------------- Lib/fontTools/varLib/plot.py | 4 ++-- 3 files changed, 11 insertions(+), 23 deletions(-) diff --git a/Lib/fontTools/pens/t2CharStringPen.py b/Lib/fontTools/pens/t2CharStringPen.py index a4b1d8f67..2824b5cf5 100644 --- a/Lib/fontTools/pens/t2CharStringPen.py +++ b/Lib/fontTools/pens/t2CharStringPen.py @@ -20,15 +20,14 @@ def t2c_round(number, tolerance=0.5): # else return the value un-rounded return number -def makeRoundFunc(tolerance): +def roundFunc(tolerance): if tolerance < 0: raise ValueError("Rounding tolerance must be positive") - def roundPoint(point): - x, y = point - return t2c_round(x, tolerance), t2c_round(y, tolerance) + def round(v): + return t2c_round(v, tolerance) - return roundPoint + return round class T2CharStringPen(BasePen): @@ -44,7 +43,7 @@ class T2CharStringPen(BasePen): def __init__(self, width, glyphSet, roundTolerance=0.5, CFF2=False): super(T2CharStringPen, self).__init__(glyphSet) - self.roundPoint = makeRoundFunc(roundTolerance) + self.round = roundFunc(roundTolerance) self._CFF2 = CFF2 self._width = width self._commands = [] @@ -52,7 +51,7 @@ class T2CharStringPen(BasePen): def _p(self, pt): p0 = self._p0 - pt = self._p0 = self.roundPoint(pt) + pt = self._p0 = (self.round(pt[0]), self.round(pt[1])) return [pt[0]-p0[0], pt[1]-p0[1]] def _moveTo(self, pt): diff --git a/Lib/fontTools/varLib/cff.py b/Lib/fontTools/varLib/cff.py index 0a6ba220b..6f704bbee 100644 --- a/Lib/fontTools/varLib/cff.py +++ b/Lib/fontTools/varLib/cff.py @@ -18,7 +18,7 @@ from fontTools.ttLib import newTable from fontTools import varLib from fontTools.varLib.models import allEqual from fontTools.misc.psCharStrings import T2CharString, T2OutlineExtractor -from fontTools.pens.t2CharStringPen import T2CharStringPen, t2c_round +from fontTools.pens.t2CharStringPen import T2CharStringPen, roundFunc from .errors import VarLibCFFDictMergeError, VarLibCFFPointTypeMergeError, VarLibMergeError @@ -422,16 +422,6 @@ def merge_charstrings(glyphOrder, num_masters, top_dicts, masterModel): return cvData -def makeRoundNumberFunc(tolerance): - if tolerance < 0: - raise ValueError("Rounding tolerance must be positive") - - def roundNumber(val): - return t2c_round(val, tolerance) - - return roundNumber - - class CFFToCFF2OutlineExtractor(T2OutlineExtractor): """ This class is used to remove the initial width from the CFF charstring without trying to add the width to self.nominalWidthX, @@ -518,7 +508,7 @@ class CFF2CharStringMergePen(T2CharStringPen): self.prev_move_idx = 0 self.seen_moveto = False self.glyphName = glyphName - self.roundNumber = makeRoundNumberFunc(roundTolerance) + self.round = roundFunc(roundTolerance) def add_point(self, point_type, pt_coords): if self.m_index == 0: @@ -649,8 +639,7 @@ class CFF2CharStringMergePen(T2CharStringPen): self, private=None, globalSubrs=None, var_model=None, optimize=True): commands = self._commands - commands = self.reorder_blend_args(commands, var_model.getDeltas, - self.roundNumber) + commands = self.reorder_blend_args(commands, var_model.getDeltas, self.round) if optimize: commands = specializeCommands( commands, generalizeFirst=False, diff --git a/Lib/fontTools/varLib/plot.py b/Lib/fontTools/varLib/plot.py index 0eb654327..811559fa6 100644 --- a/Lib/fontTools/varLib/plot.py +++ b/Lib/fontTools/varLib/plot.py @@ -68,10 +68,10 @@ def plotLocations(locations, fig, names=None, **kwargs): def _plotLocations2D(model, axis, fig, cols, rows, names, **kwargs): + subplot = fig.add_subplot(111) for i, (support, color, name) in enumerate( zip(model.supports, cycle(pyplot.cm.Set1.colors), cycle(names)) ): - subplot = fig.add_subplot(rows, cols, i + 1) if name is not None: subplot.set_title(name) subplot.set_xlabel(axis) @@ -91,10 +91,10 @@ def _plotLocations2D(model, axis, fig, cols, rows, names, **kwargs): def _plotLocations3D(model, axes, fig, rows, cols, names, **kwargs): ax1, ax2 = axes + axis3D = fig.add_subplot(111, projection='3d') for i, (support, color, name) in enumerate( zip(model.supports, cycle(pyplot.cm.Set1.colors), cycle(names)) ): - axis3D = fig.add_subplot(rows, cols, i + 1, projection='3d') if name is not None: axis3D.set_title(name) axis3D.set_xlabel(ax1)