From 5c0f05cc42fe0e4f149a7488419d4a3cf5ef8c47 Mon Sep 17 00:00:00 2001 From: Cosimo Lupo Date: Thu, 8 Jun 2023 11:41:07 +0100 Subject: [PATCH] allow to pass 'round' parameter in ttGlyphPen, optimize for noRound --- Lib/fontTools/pens/ttGlyphPen.py | 8 ++++---- Lib/fontTools/ttLib/tables/_g_l_y_f.py | 4 +++- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/Lib/fontTools/pens/ttGlyphPen.py b/Lib/fontTools/pens/ttGlyphPen.py index f06bcf5b9..de2ccaeeb 100644 --- a/Lib/fontTools/pens/ttGlyphPen.py +++ b/Lib/fontTools/pens/ttGlyphPen.py @@ -1,5 +1,5 @@ from array import array -from typing import Any, Dict, Optional, Tuple +from typing import Any, Callable, Dict, Optional, Tuple from fontTools.misc.fixedTools import MAX_F2DOT14, floatToFixedToFloat from fontTools.misc.loggingTools import LogMixin from fontTools.pens.pointPen import AbstractPointPen @@ -131,7 +131,8 @@ class _TTGlyphBasePen: self, componentFlags: int = 0x04, dropImpliedOnCurves: bool = False, - roundCoordinates: bool = True, + *, + round: Callable[[float], int] = otRound, ) -> Glyph: """ Returns a :py:class:`~._g_l_y_f.Glyph` object representing the glyph. @@ -162,8 +163,7 @@ class _TTGlyphBasePen: glyph.program.fromBytecode(b"") if dropImpliedOnCurves: dropImpliedOnCurvePoints(glyph) - if roundCoordinates: - glyph.coordinates.toInt() + glyph.coordinates.toInt(round=round) return glyph diff --git a/Lib/fontTools/ttLib/tables/_g_l_y_f.py b/Lib/fontTools/ttLib/tables/_g_l_y_f.py index 4554efc2e..46cb2fd61 100644 --- a/Lib/fontTools/ttLib/tables/_g_l_y_f.py +++ b/Lib/fontTools/ttLib/tables/_g_l_y_f.py @@ -13,8 +13,8 @@ from fontTools.misc.fixedTools import ( floatToFixed as fl2fi, floatToFixedToStr as fl2str, strToFixedToFloat as str2fl, - otRound, ) +from fontTools.misc.roundTools import noRound, otRound from fontTools.misc.vector import Vector from numbers import Number from . import DefaultTable @@ -2322,6 +2322,8 @@ class GlyphCoordinates(object): self._a.extend(p) def toInt(self, *, round=otRound): + if round is noRound: + return a = self._a for i in range(len(a)): a[i] = round(a[i])