[cff] Simplify rounding logic

No semantic change. Just refactoring and simplification in anticipation
of coming changes.
This commit is contained in:
Behdad Esfahbod 2021-03-03 18:27:06 -07:00
parent ff46e2838f
commit 28fae1d95f
3 changed files with 11 additions and 23 deletions

View File

@ -20,15 +20,14 @@ def t2c_round(number, tolerance=0.5):
# else return the value un-rounded # else return the value un-rounded
return number return number
def makeRoundFunc(tolerance): def roundFunc(tolerance):
if tolerance < 0: if tolerance < 0:
raise ValueError("Rounding tolerance must be positive") raise ValueError("Rounding tolerance must be positive")
def roundPoint(point): def round(v):
x, y = point return t2c_round(v, tolerance)
return t2c_round(x, tolerance), t2c_round(y, tolerance)
return roundPoint return round
class T2CharStringPen(BasePen): class T2CharStringPen(BasePen):
@ -44,7 +43,7 @@ class T2CharStringPen(BasePen):
def __init__(self, width, glyphSet, roundTolerance=0.5, CFF2=False): def __init__(self, width, glyphSet, roundTolerance=0.5, CFF2=False):
super(T2CharStringPen, self).__init__(glyphSet) super(T2CharStringPen, self).__init__(glyphSet)
self.roundPoint = makeRoundFunc(roundTolerance) self.round = roundFunc(roundTolerance)
self._CFF2 = CFF2 self._CFF2 = CFF2
self._width = width self._width = width
self._commands = [] self._commands = []
@ -52,7 +51,7 @@ class T2CharStringPen(BasePen):
def _p(self, pt): def _p(self, pt):
p0 = self._p0 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]] return [pt[0]-p0[0], pt[1]-p0[1]]
def _moveTo(self, pt): def _moveTo(self, pt):

View File

@ -18,7 +18,7 @@ from fontTools.ttLib import newTable
from fontTools import varLib from fontTools import varLib
from fontTools.varLib.models import allEqual from fontTools.varLib.models import allEqual
from fontTools.misc.psCharStrings import T2CharString, T2OutlineExtractor 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 from .errors import VarLibCFFDictMergeError, VarLibCFFPointTypeMergeError, VarLibMergeError
@ -422,16 +422,6 @@ def merge_charstrings(glyphOrder, num_masters, top_dicts, masterModel):
return cvData 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): class CFFToCFF2OutlineExtractor(T2OutlineExtractor):
""" This class is used to remove the initial width from the CFF """ This class is used to remove the initial width from the CFF
charstring without trying to add the width to self.nominalWidthX, charstring without trying to add the width to self.nominalWidthX,
@ -518,7 +508,7 @@ class CFF2CharStringMergePen(T2CharStringPen):
self.prev_move_idx = 0 self.prev_move_idx = 0
self.seen_moveto = False self.seen_moveto = False
self.glyphName = glyphName self.glyphName = glyphName
self.roundNumber = makeRoundNumberFunc(roundTolerance) self.round = roundFunc(roundTolerance)
def add_point(self, point_type, pt_coords): def add_point(self, point_type, pt_coords):
if self.m_index == 0: if self.m_index == 0:
@ -649,8 +639,7 @@ class CFF2CharStringMergePen(T2CharStringPen):
self, private=None, globalSubrs=None, self, private=None, globalSubrs=None,
var_model=None, optimize=True): var_model=None, optimize=True):
commands = self._commands commands = self._commands
commands = self.reorder_blend_args(commands, var_model.getDeltas, commands = self.reorder_blend_args(commands, var_model.getDeltas, self.round)
self.roundNumber)
if optimize: if optimize:
commands = specializeCommands( commands = specializeCommands(
commands, generalizeFirst=False, commands, generalizeFirst=False,

View File

@ -68,10 +68,10 @@ def plotLocations(locations, fig, names=None, **kwargs):
def _plotLocations2D(model, axis, fig, cols, rows, names, **kwargs): def _plotLocations2D(model, axis, fig, cols, rows, names, **kwargs):
subplot = fig.add_subplot(111)
for i, (support, color, name) in enumerate( for i, (support, color, name) in enumerate(
zip(model.supports, cycle(pyplot.cm.Set1.colors), cycle(names)) zip(model.supports, cycle(pyplot.cm.Set1.colors), cycle(names))
): ):
subplot = fig.add_subplot(rows, cols, i + 1)
if name is not None: if name is not None:
subplot.set_title(name) subplot.set_title(name)
subplot.set_xlabel(axis) 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): def _plotLocations3D(model, axes, fig, rows, cols, names, **kwargs):
ax1, ax2 = axes ax1, ax2 = axes
axis3D = fig.add_subplot(111, projection='3d')
for i, (support, color, name) in enumerate( for i, (support, color, name) in enumerate(
zip(model.supports, cycle(pyplot.cm.Set1.colors), cycle(names)) 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: if name is not None:
axis3D.set_title(name) axis3D.set_title(name)
axis3D.set_xlabel(ax1) axis3D.set_xlabel(ax1)