diff --git a/Lib/fontTools/misc/arrayTools.py b/Lib/fontTools/misc/arrayTools.py index f2cfac838..ed20230c9 100644 --- a/Lib/fontTools/misc/arrayTools.py +++ b/Lib/fontTools/misc/arrayTools.py @@ -6,6 +6,7 @@ from __future__ import print_function, division, absolute_import from fontTools.misc.py23 import * +from fontTools.misc.fixedTools import otRound from numbers import Number import math import operator @@ -20,10 +21,11 @@ def calcBounds(array): ys = [y for x, y in array] 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 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)) diff --git a/Tests/misc/arrayTools_test.py b/Tests/misc/arrayTools_test.py index 108b50da2..2610c0fcc 100644 --- a/Tests/misc/arrayTools_test.py +++ b/Tests/misc/arrayTools_test.py @@ -1,5 +1,6 @@ from __future__ import print_function, division, absolute_import from fontTools.misc.py23 import * +from fontTools.misc.py23 import round3 from fontTools.misc.arrayTools import ( calcBounds, calcIntBounds, updateBounds, pointInRect, pointsInRect, vectorLength, asInt16, normRect, scaleRect, offsetRect, insetRect, @@ -15,8 +16,13 @@ def test_calcBounds(): def test_calcIntBounds(): assert calcIntBounds( - [(0.1, 40.1), (0.1, 100.1), (49.9, 49.9), (79.5, 9.5)] - ) == (0, 10, 80, 100) + [(0.1, 40.1), (0.1, 100.1), (49.9, 49.9), (78.5, 9.5)] + ) == (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():