From 92fbac8a64af0e7c59b3b766904a2922b2c42ba7 Mon Sep 17 00:00:00 2001 From: Cosimo Lupo Date: Wed, 8 Mar 2023 19:03:50 +0000 Subject: [PATCH] [arrayTools] add quantizeRect --- Lib/fontTools/misc/arrayTools.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/Lib/fontTools/misc/arrayTools.py b/Lib/fontTools/misc/arrayTools.py index f5a5a705c..5fb01a838 100644 --- a/Lib/fontTools/misc/arrayTools.py +++ b/Lib/fontTools/misc/arrayTools.py @@ -282,6 +282,27 @@ def intRect(rect): return (xMin, yMin, xMax, yMax) +def quantizeRect(rect, factor=1): + """ + >>> 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) + """ + if factor < 1: + raise ValueError(f"Expected quantization factor >= 1, found: {factor!r}") + xMin, yMin, xMax, yMax = normRect(rect) + return ( + int(math.floor(xMin / factor) * factor), + int(math.floor(yMin / factor) * factor), + int(math.ceil(xMax / factor) * factor), + int(math.ceil(yMax / factor) * factor), + ) + + class Vector(_Vector): def __init__(self, *args, **kwargs): warnings.warn(