arrayTools: calcIntBounds should use otRound, not round3

Somehow we forgot to replace round -> otRound in arrayTools.calcIntBounds.
This function is used by glyf table to compute the glyphs' bounding boxes.
We already use otRound (aka 'int(math.floor(v + .5))') to round glyph
coordinates upon compiling glyf table. So the use of python3's round
in calcIntBounds was producing inconsistent roundings between the glyph
coordinates and the glyph bbox (sometimes, i.e. only when the glyf table
contains float coordinates, e.g. after instantiating with varLib.mutator).
This commit is contained in:
Cosimo Lupo 2019-04-04 12:38:54 +01:00
parent 5e627c5228
commit 80306037b7
No known key found for this signature in database
GPG Key ID: 20D4A261E4A0E642
2 changed files with 12 additions and 4 deletions

View File

@ -6,6 +6,7 @@
from __future__ import print_function, division, absolute_import from __future__ import print_function, division, absolute_import
from fontTools.misc.py23 import * from fontTools.misc.py23 import *
from fontTools.misc.fixedTools import otRound
from numbers import Number from numbers import Number
import math import math
import operator import operator
@ -20,10 +21,11 @@ def calcBounds(array):
ys = [y for x, y in array] ys = [y for x, y in array]
return min(xs), min(ys), max(xs), max(ys) return min(xs), min(ys), max(xs), max(ys)
def calcIntBounds(array): def calcIntBounds(array, round=otRound):
"""Return the integer bounding rectangle of a 2D points array as a """Return the integer bounding rectangle of a 2D points array as a
tuple: (xMin, yMin, xMax, yMax) tuple: (xMin, yMin, xMax, yMax)
Values are rounded to closest integer. Values are rounded to closest integer towards +Infinity using otRound
function by default, unless an optional 'round' function is passed.
""" """
return tuple(round(v) for v in calcBounds(array)) return tuple(round(v) for v in calcBounds(array))

View File

@ -1,5 +1,6 @@
from __future__ import print_function, division, absolute_import from __future__ import print_function, division, absolute_import
from fontTools.misc.py23 import * from fontTools.misc.py23 import *
from fontTools.misc.py23 import round3
from fontTools.misc.arrayTools import ( from fontTools.misc.arrayTools import (
calcBounds, calcIntBounds, updateBounds, pointInRect, pointsInRect, calcBounds, calcIntBounds, updateBounds, pointInRect, pointsInRect,
vectorLength, asInt16, normRect, scaleRect, offsetRect, insetRect, vectorLength, asInt16, normRect, scaleRect, offsetRect, insetRect,
@ -15,8 +16,13 @@ def test_calcBounds():
def test_calcIntBounds(): def test_calcIntBounds():
assert calcIntBounds( assert calcIntBounds(
[(0.1, 40.1), (0.1, 100.1), (49.9, 49.9), (79.5, 9.5)] [(0.1, 40.1), (0.1, 100.1), (49.9, 49.9), (78.5, 9.5)]
) == (0, 10, 80, 100) ) == (0, 10, 79, 100)
assert calcIntBounds(
[(0.1, 40.1), (0.1, 100.1), (49.9, 49.9), (78.5, 9.5)],
round=round3
) == (0, 10, 78, 100)
def test_updateBounds(): def test_updateBounds():