[misc.roundTools] Add noRound(), maybeRound(), and roundFunc()
Moving out of CFF code, to be used in VariationModel(). Part of https://github.com/fonttools/fonttools/issues/2213
This commit is contained in:
parent
abc1ba07a4
commit
3a9a2bd4b1
@ -3,14 +3,21 @@ Various round-to-integer helpers.
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
import math
|
import math
|
||||||
|
import functools
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
|
"noRound",
|
||||||
"otRound",
|
"otRound",
|
||||||
|
"maybeRound",
|
||||||
|
"roundFunc",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
def noRound(value):
|
||||||
|
return value
|
||||||
|
|
||||||
def otRound(value):
|
def otRound(value):
|
||||||
"""Round float value to nearest integer towards ``+Infinity``.
|
"""Round float value to nearest integer towards ``+Infinity``.
|
||||||
|
|
||||||
@ -34,3 +41,18 @@ def otRound(value):
|
|||||||
# https://github.com/fonttools/fonttools/issues/1248#issuecomment-383198166
|
# https://github.com/fonttools/fonttools/issues/1248#issuecomment-383198166
|
||||||
return int(math.floor(value + 0.5))
|
return int(math.floor(value + 0.5))
|
||||||
|
|
||||||
|
def maybeRound(v, tolerance, round=otRound):
|
||||||
|
rounded = round(v)
|
||||||
|
return rounded if abs(rounded - v) <= tolerance else v
|
||||||
|
|
||||||
|
def roundFunc(tolerance, round=otRound):
|
||||||
|
if tolerance < 0:
|
||||||
|
raise ValueError("Rounding tolerance must be positive")
|
||||||
|
|
||||||
|
if tolerance == 0:
|
||||||
|
return noRound
|
||||||
|
|
||||||
|
if tolerance >= .5:
|
||||||
|
return round
|
||||||
|
|
||||||
|
return functools.partial(maybeRound, tolerance=tolerance, round=round)
|
||||||
|
@ -1,29 +1,12 @@
|
|||||||
# Copyright (c) 2009 Type Supply LLC
|
# Copyright (c) 2009 Type Supply LLC
|
||||||
# Author: Tal Leming
|
# Author: Tal Leming
|
||||||
|
|
||||||
from fontTools.misc.roundTools import otRound
|
from fontTools.misc.roundTools import otRound, roundFunc
|
||||||
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
|
||||||
|
|
||||||
|
|
||||||
def roundFunc(tolerance, round=otRound):
|
|
||||||
if tolerance < 0:
|
|
||||||
raise ValueError("Rounding tolerance must be positive")
|
|
||||||
|
|
||||||
if tolerance == 0:
|
|
||||||
return lambda x: x
|
|
||||||
|
|
||||||
if tolerance >= .5:
|
|
||||||
return round
|
|
||||||
|
|
||||||
def maybe_round(v):
|
|
||||||
rounded = round(v)
|
|
||||||
return rounded if abs(rounded - v) <= tolerance else v
|
|
||||||
|
|
||||||
return maybe_round
|
|
||||||
|
|
||||||
|
|
||||||
class T2CharStringPen(BasePen):
|
class T2CharStringPen(BasePen):
|
||||||
"""Pen to draw Type 2 CharStrings.
|
"""Pen to draw Type 2 CharStrings.
|
||||||
|
|
||||||
|
@ -17,8 +17,9 @@ from fontTools.cffLib.specializer import (
|
|||||||
from fontTools.ttLib import newTable
|
from fontTools.ttLib import newTable
|
||||||
from fontTools import varLib
|
from fontTools import varLib
|
||||||
from fontTools.varLib.models import allEqual
|
from fontTools.varLib.models import allEqual
|
||||||
|
from fontTools.misc.roundTools import roundFunc
|
||||||
from fontTools.misc.psCharStrings import T2CharString, T2OutlineExtractor
|
from fontTools.misc.psCharStrings import T2CharString, T2OutlineExtractor
|
||||||
from fontTools.pens.t2CharStringPen import T2CharStringPen, roundFunc
|
from fontTools.pens.t2CharStringPen import T2CharStringPen
|
||||||
|
|
||||||
from .errors import VarLibCFFDictMergeError, VarLibCFFPointTypeMergeError, VarLibMergeError
|
from .errors import VarLibCFFDictMergeError, VarLibCFFPointTypeMergeError, VarLibMergeError
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user