# arrayTools: Various array and rectangle tools

Routines for calculating bounding boxes, point in rectangle calculations and so on.

fontTools.misc.arrayTools.calcBounds(array)[source]

Calculate the bounding rectangle of a 2D points array.

Parameters:

array – A sequence of 2D tuples.

Returns:

A four-item tuple representing the bounding rectangle (xMin, yMin, xMax, yMax).

fontTools.misc.arrayTools.calcIntBounds(array, round=<function otRound>)[source]

Calculate the integer bounding rectangle of a 2D points array.

Values are rounded to closest integer towards +Infinity using the fontTools.misc.fixedTools.otRound() function by default, unless an optional round function is passed.

Parameters:
• array – A sequence of 2D tuples.

• round – A rounding function of type f(x: float) -> int.

Returns:

(xMin, yMin, xMax, yMax).

Return type:

A four-item tuple of integers representing the bounding rectangle

fontTools.misc.arrayTools.updateBounds(bounds, p, min=<built-in function min>, max=<built-in function max>)[source]

Add a point to a bounding rectangle.

Parameters:
• bounds – A bounding rectangle expressed as a tuple (xMin, yMin, xMax, yMax), or None.

• p – A 2D tuple representing a point.

• min – functions to compute the minimum and maximum.

• max – functions to compute the minimum and maximum.

Returns:

The updated bounding rectangle (xMin, yMin, xMax, yMax).

fontTools.misc.arrayTools.pointInRect(p, rect)[source]

Test if a point is inside a bounding rectangle.

Parameters:
• p – A 2D tuple representing a point.

• rect – A bounding rectangle expressed as a tuple (xMin, yMin, xMax, yMax).

Returns:

True if the point is inside the rectangle, False otherwise.

fontTools.misc.arrayTools.pointsInRect(array, rect)[source]

Determine which points are inside a bounding rectangle.

Parameters:
• array – A sequence of 2D tuples.

• rect – A bounding rectangle expressed as a tuple (xMin, yMin, xMax, yMax).

Returns:

A list containing the points inside the rectangle.

fontTools.misc.arrayTools.vectorLength(vector)[source]

Calculate the length of the given vector.

Parameters:

vector – A 2D tuple.

Returns:

The Euclidean length of the vector.

fontTools.misc.arrayTools.asInt16(array)[source]

Round a list of floats to 16-bit signed integers.

Parameters:

array – List of float values.

Returns:

A list of rounded integers.

fontTools.misc.arrayTools.normRect(rect)[source]

Normalize a bounding box rectangle.

This function “turns the rectangle the right way up”, so that the following holds:

xMin <= xMax and yMin <= yMax
Parameters:

rect – A bounding rectangle expressed as a tuple (xMin, yMin, xMax, yMax).

Returns:

A normalized bounding rectangle.

fontTools.misc.arrayTools.scaleRect(rect, x, y)[source]

Scale a bounding box rectangle.

Parameters:
• rect – A bounding rectangle expressed as a tuple (xMin, yMin, xMax, yMax).

• x – Factor to scale the rectangle along the X axis.

• Y – Factor to scale the rectangle along the Y axis.

Returns:

A scaled bounding rectangle.

fontTools.misc.arrayTools.offsetRect(rect, dx, dy)[source]

Offset a bounding box rectangle.

Parameters:
• rect – A bounding rectangle expressed as a tuple (xMin, yMin, xMax, yMax).

• dx – Amount to offset the rectangle along the X axis.

• dY – Amount to offset the rectangle along the Y axis.

Returns:

An offset bounding rectangle.

fontTools.misc.arrayTools.insetRect(rect, dx, dy)[source]

Inset a bounding box rectangle on all sides.

Parameters:
• rect – A bounding rectangle expressed as a tuple (xMin, yMin, xMax, yMax).

• dx – Amount to inset the rectangle along the X axis.

• dY – Amount to inset the rectangle along the Y axis.

Returns:

An inset bounding rectangle.

fontTools.misc.arrayTools.sectRect(rect1, rect2)[source]

Test for rectangle-rectangle intersection.

Parameters:
• rect1 – First bounding rectangle, expressed as tuples (xMin, yMin, xMax, yMax).

• rect2 – Second bounding rectangle.

Returns:

A boolean and a rectangle. If the input rectangles intersect, returns True and the intersecting rectangle. Returns False and (0, 0, 0, 0) if the input rectangles don’t intersect.

fontTools.misc.arrayTools.unionRect(rect1, rect2)[source]

Determine union of bounding rectangles.

Parameters:
• rect1 – First bounding rectangle, expressed as tuples (xMin, yMin, xMax, yMax).

• rect2 – Second bounding rectangle.

Returns:

The smallest rectangle in which both input rectangles are fully enclosed.

fontTools.misc.arrayTools.rectCenter(rect)[source]

Determine rectangle center.

Parameters:

rect – Bounding rectangle, expressed as tuples (xMin, yMin, xMax, yMax).

Returns:

A 2D tuple representing the point at the center of the rectangle.

fontTools.misc.arrayTools.rectArea(rect)[source]

Determine rectangle area.

Parameters:

rect – Bounding rectangle, expressed as tuples (xMin, yMin, xMax, yMax).

Returns:

The area of the rectangle.

fontTools.misc.arrayTools.intRect(rect)[source]

Round a rectangle to integer values.

Guarantees that the resulting rectangle is NOT smaller than the original.

Parameters:

rect – Bounding rectangle, expressed as tuples (xMin, yMin, xMax, yMax).

Returns:

A rounded bounding rectangle.

fontTools.misc.arrayTools.quantizeRect(rect, factor=1)[source]
>>> bounds = (72.3, -218.4, 1201.3, 919.1)
>>> quantizeRect(bounds)
(72, -219, 1202, 920)
>>> quantizeRect(bounds, factor=10)
(70, -220, 1210, 920)
>>> quantizeRect(bounds, factor=100)
(0, -300, 1300, 1000)
class fontTools.misc.arrayTools.Vector(values, keep=False)[source]
count(value, /)

Return number of occurrences of value.

dot(other)[source]

Performs vector dot product, returning the sum of a[0] * b[0], a[1] * b[1], ...

index(value, start=0, stop=9223372036854775807, /)

Return first index of value.

Raises ValueError if the value is not present.

isclose(other: Vector, **kwargs) bool[source]

Return True if the vector is close to another Vector.

length()[source]

Return the length of the vector. Equivalent to abs(vector).

normalized()[source]

Return the normalized vector of the vector.

toInt()[source]
property values
fontTools.misc.arrayTools.pairwise(iterable, reverse=False)[source]

Iterate over current and next items in iterable.

Parameters:
• iterable – An iterable

• reverse – If true, iterate in reverse order.

Returns:

A iterable yielding two elements per iteration.

Example

>>> tuple(pairwise([]))
()
>>> tuple(pairwise([], reverse=True))
()
>>> tuple(pairwise([0]))
((0, 0),)
>>> tuple(pairwise([0], reverse=True))
((0, 0),)
>>> tuple(pairwise([0, 1]))
((0, 1), (1, 0))
>>> tuple(pairwise([0, 1], reverse=True))
((1, 0), (0, 1))
>>> tuple(pairwise([0, 1, 2]))
((0, 1), (1, 2), (2, 0))
>>> tuple(pairwise([0, 1, 2], reverse=True))
((2, 1), (1, 0), (0, 2))
>>> tuple(pairwise(['a', 'b', 'c', 'd']))
(('a', 'b'), ('b', 'c'), ('c', 'd'), ('d', 'a'))
>>> tuple(pairwise(['a', 'b', 'c', 'd'], reverse=True))
(('d', 'c'), ('c', 'b'), ('b', 'a'), ('a', 'd'))