send DeprecationWarning if FONTTOOLS_GPOS_COMPACT_MODE env var is set
and factor out shared code to read the env var
This commit is contained in:
parent
f8611bf848
commit
20de0784c2
@ -12,9 +12,8 @@ from fontTools.ttLib.tables.otBase import (
|
|||||||
from fontTools.ttLib.tables import otBase
|
from fontTools.ttLib.tables import otBase
|
||||||
from fontTools.feaLib.ast import STATNameStatement
|
from fontTools.feaLib.ast import STATNameStatement
|
||||||
from fontTools.otlLib.optimize.gpos import (
|
from fontTools.otlLib.optimize.gpos import (
|
||||||
|
_compression_level_from_env,
|
||||||
compact_lookup,
|
compact_lookup,
|
||||||
GPOS_COMPACT_MODE_DEFAULT,
|
|
||||||
GPOS_COMPACT_MODE_ENV_KEY,
|
|
||||||
)
|
)
|
||||||
from fontTools.otlLib.error import OpenTypeLibError
|
from fontTools.otlLib.error import OpenTypeLibError
|
||||||
from functools import reduce
|
from functools import reduce
|
||||||
@ -1415,13 +1414,11 @@ class PairPosBuilder(LookupBuilder):
|
|||||||
# This is a good moment to do it because the compaction should create
|
# This is a good moment to do it because the compaction should create
|
||||||
# smaller subtables, which may prevent overflows from happening.
|
# smaller subtables, which may prevent overflows from happening.
|
||||||
# Keep reading the value from the ENV until ufo2ft switches to the config system
|
# Keep reading the value from the ENV until ufo2ft switches to the config system
|
||||||
env_level = os.environ.get(GPOS_COMPACT_MODE_ENV_KEY, GPOS_COMPACT_MODE_DEFAULT)
|
level = self.font.cfg.get(
|
||||||
if len(env_level) == 1 and env_level in "0123456789":
|
"fontTools.otlLib.optimize.gpos:COMPRESSION_LEVEL",
|
||||||
env_level = int(env_level)
|
default=_compression_level_from_env(),
|
||||||
else:
|
)
|
||||||
raise ValueError(f"Bad {GPOS_COMPACT_MODE_ENV_KEY}={env_level}")
|
if level != 0:
|
||||||
level = self.font.cfg.get("fontTools.otlLib.optimize.gpos:COMPRESSION_LEVEL", env_level)
|
|
||||||
if level and level != 0:
|
|
||||||
log.info("Compacting GPOS...")
|
log.info("Compacting GPOS...")
|
||||||
compact_lookup(self.font, level, lookup)
|
compact_lookup(self.font, level, lookup)
|
||||||
|
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import logging
|
import logging
|
||||||
|
import os
|
||||||
from collections import defaultdict, namedtuple
|
from collections import defaultdict, namedtuple
|
||||||
from functools import reduce
|
from functools import reduce
|
||||||
from itertools import chain
|
from itertools import chain
|
||||||
@ -14,13 +15,30 @@ log = logging.getLogger(__name__)
|
|||||||
|
|
||||||
COMPRESSION_LEVEL = OPTIONS[f"{__name__}:COMPRESSION_LEVEL"]
|
COMPRESSION_LEVEL = OPTIONS[f"{__name__}:COMPRESSION_LEVEL"]
|
||||||
|
|
||||||
|
|
||||||
# Kept because ufo2ft depends on it, to be removed once ufo2ft uses the config instead
|
# Kept because ufo2ft depends on it, to be removed once ufo2ft uses the config instead
|
||||||
# https://github.com/fonttools/fonttools/issues/2592
|
# https://github.com/fonttools/fonttools/issues/2592
|
||||||
GPOS_COMPACT_MODE_ENV_KEY = "FONTTOOLS_GPOS_COMPACT_MODE"
|
GPOS_COMPACT_MODE_ENV_KEY = "FONTTOOLS_GPOS_COMPACT_MODE"
|
||||||
GPOS_COMPACT_MODE_DEFAULT = str(COMPRESSION_LEVEL.default)
|
GPOS_COMPACT_MODE_DEFAULT = str(COMPRESSION_LEVEL.default)
|
||||||
|
|
||||||
|
|
||||||
|
def _compression_level_from_env() -> int:
|
||||||
|
env_level = GPOS_COMPACT_MODE_DEFAULT
|
||||||
|
if GPOS_COMPACT_MODE_ENV_KEY in os.environ:
|
||||||
|
import warnings
|
||||||
|
|
||||||
|
warnings.warn(
|
||||||
|
f"'{GPOS_COMPACT_MODE_ENV_KEY}' environment variable is deprecated. "
|
||||||
|
"Please set the 'fontTools.otlLib.optimize.gpos:COMPRESSION_LEVEL' option "
|
||||||
|
"in TTFont.cfg.",
|
||||||
|
DeprecationWarning,
|
||||||
|
)
|
||||||
|
|
||||||
|
env_level = os.environ[GPOS_COMPACT_MODE_ENV_KEY]
|
||||||
|
if len(env_level) == 1 and env_level in "0123456789":
|
||||||
|
return int(env_level)
|
||||||
|
raise ValueError(f"Bad {GPOS_COMPACT_MODE_ENV_KEY}={env_level}")
|
||||||
|
|
||||||
|
|
||||||
def compact(font: TTFont, level: int) -> TTFont:
|
def compact(font: TTFont, level: int) -> TTFont:
|
||||||
# Ideal plan:
|
# Ideal plan:
|
||||||
# 1. Find lookups of Lookup Type 2: Pair Adjustment Positioning Subtable
|
# 1. Find lookups of Lookup Type 2: Pair Adjustment Positioning Subtable
|
||||||
|
@ -16,9 +16,8 @@ from fontTools.varLib.varStore import VarStoreInstancer
|
|||||||
from functools import reduce
|
from functools import reduce
|
||||||
from fontTools.otlLib.builder import buildSinglePos
|
from fontTools.otlLib.builder import buildSinglePos
|
||||||
from fontTools.otlLib.optimize.gpos import (
|
from fontTools.otlLib.optimize.gpos import (
|
||||||
compact_pair_pos,
|
_compression_level_from_env,
|
||||||
GPOS_COMPACT_MODE_DEFAULT,
|
compact_pair_pos,
|
||||||
GPOS_COMPACT_MODE_ENV_KEY,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
log = logging.getLogger("fontTools.varLib.merger")
|
log = logging.getLogger("fontTools.varLib.merger")
|
||||||
@ -851,13 +850,11 @@ def merge(merger, self, lst):
|
|||||||
# This is a good moment to do it because the compaction should create
|
# This is a good moment to do it because the compaction should create
|
||||||
# smaller subtables, which may prevent overflows from happening.
|
# smaller subtables, which may prevent overflows from happening.
|
||||||
# Keep reading the value from the ENV until ufo2ft switches to the config system
|
# Keep reading the value from the ENV until ufo2ft switches to the config system
|
||||||
env_level = os.environ.get(GPOS_COMPACT_MODE_ENV_KEY, GPOS_COMPACT_MODE_DEFAULT)
|
level = merger.font.cfg.get(
|
||||||
if len(env_level) == 1 and env_level in "0123456789":
|
"fontTools.otlLib.optimize.gpos:COMPRESSION_LEVEL",
|
||||||
env_level = int(env_level)
|
default=_compression_level_from_env(),
|
||||||
else:
|
)
|
||||||
raise ValueError(f"Bad {GPOS_COMPACT_MODE_ENV_KEY}={env_level}")
|
if level != 0:
|
||||||
level = merger.font.cfg.get("fontTools.otlLib.optimize.gpos:COMPRESSION_LEVEL", env_level)
|
|
||||||
if level and level != 0:
|
|
||||||
log.info("Compacting GPOS...")
|
log.info("Compacting GPOS...")
|
||||||
self.SubTable = compact_pair_pos(merger.font, level, self.SubTable)
|
self.SubTable = compact_pair_pos(merger.font, level, self.SubTable)
|
||||||
self.SubTableCount = len(self.SubTable)
|
self.SubTableCount = len(self.SubTable)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user