[misc.roundTools] New module; move otRound() here

Code relying on old place (fixedTools.otRound) still works.
This commit is contained in:
Behdad Esfahbod 2021-03-03 18:48:08 -07:00
parent ed77aeaebf
commit abc1ba07a4
14 changed files with 50 additions and 36 deletions

View File

@ -1,7 +1,7 @@
"""Helpers for manipulating 2D points and vectors in COLR table.""" """Helpers for manipulating 2D points and vectors in COLR table."""
from math import copysign, cos, hypot, pi from math import copysign, cos, hypot, pi
from fontTools.misc.fixedTools import otRound from fontTools.misc.roundTools import otRound
def _vector_between(origin, target): def _vector_between(origin, target):

View File

@ -22,7 +22,7 @@ from fontTools.ttLib.tables.otConverters import (
IntValue, IntValue,
FloatValue, FloatValue,
) )
from fontTools.misc.fixedTools import otRound from fontTools.misc.roundTools import otRound
class BuildCallback(enum.Enum): class BuildCallback(enum.Enum):

View File

@ -3,7 +3,7 @@ so on.
""" """
from fontTools.misc.py23 import * from fontTools.misc.py23 import *
from fontTools.misc.fixedTools import otRound from fontTools.misc.roundTools import otRound
from fontTools.misc.vector import Vector as _Vector from fontTools.misc.vector import Vector as _Vector
import math import math
import warnings import warnings

View File

@ -18,6 +18,7 @@ functions for converting between fixed-point, float and string representations.
""" """
from fontTools.misc.py23 import * from fontTools.misc.py23 import *
from .roundTools import otRound
import math import math
import logging import logging
@ -25,7 +26,6 @@ log = logging.getLogger(__name__)
__all__ = [ __all__ = [
"MAX_F2DOT14", "MAX_F2DOT14",
"otRound",
"fixedToFloat", "fixedToFloat",
"floatToFixed", "floatToFixed",
"floatToFixedToFloat", "floatToFixedToFloat",
@ -41,30 +41,6 @@ __all__ = [
MAX_F2DOT14 = 0x7FFF / (1 << 14) MAX_F2DOT14 = 0x7FFF / (1 << 14)
def otRound(value):
"""Round float value to nearest integer towards ``+Infinity``.
The OpenType spec (in the section on `"normalization" of OpenType Font Variations <https://docs.microsoft.com/en-us/typography/opentype/spec/otvaroverview#coordinate-scales-and-normalization>`_)
defines the required method for converting floating point values to
fixed-point. In particular it specifies the following rounding strategy:
for fractional values of 0.5 and higher, take the next higher integer;
for other fractional values, truncate.
This function rounds the floating-point value according to this strategy
in preparation for conversion to fixed-point.
Args:
value (float): The input floating-point value.
Returns
float: The rounded value.
"""
# See this thread for how we ended up with this implementation:
# https://github.com/fonttools/fonttools/issues/1248#issuecomment-383198166
return int(math.floor(value + 0.5))
def fixedToFloat(value, precisionBits): def fixedToFloat(value, precisionBits):
"""Converts a fixed-point number to a float given the number of """Converts a fixed-point number to a float given the number of
precision bits. precision bits.

View File

@ -0,0 +1,36 @@
"""
Various round-to-integer helpers.
"""
import math
import logging
log = logging.getLogger(__name__)
__all__ = [
"otRound",
]
def otRound(value):
"""Round float value to nearest integer towards ``+Infinity``.
The OpenType spec (in the section on `"normalization" of OpenType Font Variations <https://docs.microsoft.com/en-us/typography/opentype/spec/otvaroverview#coordinate-scales-and-normalization>`_)
defines the required method for converting floating point values to
fixed-point. In particular it specifies the following rounding strategy:
for fractional values of 0.5 and higher, take the next higher integer;
for other fractional values, truncate.
This function rounds the floating-point value according to this strategy
in preparation for conversion to fixed-point.
Args:
value (float): The input floating-point value.
Returns
float: The rounded value.
"""
# See this thread for how we ended up with this implementation:
# https://github.com/fonttools/fonttools/issues/1248#issuecomment-383198166
return int(math.floor(value + 0.5))

View File

@ -1,4 +1,4 @@
from fontTools.misc.fixedTools import otRound from fontTools.misc.roundTools import otRound
from fontTools.misc.transform import Transform from fontTools.misc.transform import Transform
from fontTools.pens.filterPen import FilterPen, FilterPointPen from fontTools.pens.filterPen import FilterPen, FilterPointPen

View File

@ -1,7 +1,7 @@
# Copyright (c) 2009 Type Supply LLC # Copyright (c) 2009 Type Supply LLC
# Author: Tal Leming # Author: Tal Leming
from fontTools.misc.fixedTools import otRound from fontTools.misc.roundTools import otRound
from fontTools.misc.psCharStrings import T2CharString from fontTools.misc.psCharStrings import T2CharString
from fontTools.pens.basePen import BasePen from fontTools.pens.basePen import BasePen
from fontTools.cffLib.specializer import specializeCommands, commandsToProgram from fontTools.cffLib.specializer import specializeCommands, commandsToProgram

View File

@ -1,5 +1,6 @@
from array import array from array import array
from fontTools.misc.fixedTools import MAX_F2DOT14, otRound, floatToFixedToFloat from fontTools.misc.fixedTools import MAX_F2DOT14, otRound, floatToFixedToFloat
from fontTools.misc.roundTools import otRound
from fontTools.pens.basePen import LoggingPen from fontTools.pens.basePen import LoggingPen
from fontTools.pens.transformPen import TransformPen from fontTools.pens.transformPen import TransformPen
from fontTools.ttLib.tables import ttProgram from fontTools.ttLib.tables import ttProgram

View File

@ -2,7 +2,7 @@
# #
# Google Author(s): Behdad Esfahbod # Google Author(s): Behdad Esfahbod
from fontTools.misc.fixedTools import otRound from fontTools.misc.roundTools import otRound
from fontTools import ttLib from fontTools import ttLib
from fontTools.ttLib.tables import otTables from fontTools.ttLib.tables import otTables
from fontTools.otlLib.maxContextCalc import maxCtxFont from fontTools.otlLib.maxContextCalc import maxCtxFont

View File

@ -1,7 +1,7 @@
from fontTools.misc import psCharStrings from fontTools.misc import psCharStrings
from fontTools import ttLib from fontTools import ttLib
from fontTools.pens.basePen import NullPen from fontTools.pens.basePen import NullPen
from fontTools.misc.fixedTools import otRound from fontTools.misc.roundTools import otRound
from fontTools.varLib.varStore import VarStoreInstancer from fontTools.varLib.varStore import VarStoreInstancer
def _add_method(*clazzes): def _add_method(*clazzes):

View File

@ -1,5 +1,5 @@
from fontTools.misc.py23 import * from fontTools.misc.py23 import *
from fontTools.misc.fixedTools import otRound from fontTools.misc.roundTools import otRound
from fontTools import ttLib from fontTools import ttLib
from fontTools.misc.textTools import safeEval from fontTools.misc.textTools import safeEval
from . import DefaultTable from . import DefaultTable

View File

@ -9,7 +9,7 @@ from enum import IntEnum
import itertools import itertools
from collections import namedtuple from collections import namedtuple
from fontTools.misc.py23 import * from fontTools.misc.py23 import *
from fontTools.misc.fixedTools import otRound from fontTools.misc.roundTools import otRound
from fontTools.misc.textTools import pad, safeEval from fontTools.misc.textTools import pad, safeEval
from .otBase import ( from .otBase import (
BaseTable, FormatSwitchingBaseTable, ValueRecord, CountReference, BaseTable, FormatSwitchingBaseTable, ValueRecord, CountReference,

View File

@ -3,8 +3,8 @@ Merge OpenType Layout tables (GDEF / GPOS / GSUB).
""" """
import copy import copy
from operator import ior from operator import ior
from fontTools.misc.fixedTools import otRound
from fontTools.misc import classifyTools from fontTools.misc import classifyTools
from fontTools.misc.roundTools import otRound
from fontTools.ttLib.tables import otTables as ot from fontTools.ttLib.tables import otTables as ot
from fontTools.ttLib.tables import otBase as otBase from fontTools.ttLib.tables import otBase as otBase
from fontTools.ttLib.tables.DefaultTable import DefaultTable from fontTools.ttLib.tables.DefaultTable import DefaultTable

View File

@ -3,7 +3,8 @@ Instantiate a variation font. Run, eg:
$ fonttools varLib.mutator ./NotoSansArabic-VF.ttf wght=140 wdth=85 $ fonttools varLib.mutator ./NotoSansArabic-VF.ttf wght=140 wdth=85
""" """
from fontTools.misc.fixedTools import floatToFixedToFloat, otRound, floatToFixed from fontTools.misc.fixedTools import floatToFixedToFloat, floatToFixed
from fontTools.misc.roundTools import otRound
from fontTools.pens.boundsPen import BoundsPen from fontTools.pens.boundsPen import BoundsPen
from fontTools.ttLib import TTFont, newTable from fontTools.ttLib import TTFont, newTable
from fontTools.ttLib.tables import ttProgram from fontTools.ttLib.tables import ttProgram