Initial import: grid-bot — grid trading bot for BTC-USDT on Cifra Markets
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
"""fontTools.feaLib -- a package for dealing with OpenType feature files."""
|
||||
|
||||
# The structure of OpenType feature files is defined here:
|
||||
# http://www.adobe.com/devnet/opentype/afdko/topic_feature_file_syntax.html
|
||||
@@ -0,0 +1,78 @@
|
||||
from fontTools.ttLib import TTFont
|
||||
from fontTools.feaLib.builder import addOpenTypeFeatures, Builder
|
||||
from fontTools.feaLib.error import FeatureLibError
|
||||
from fontTools import configLogger
|
||||
from fontTools.misc.cliTools import makeOutputFileName
|
||||
import sys
|
||||
import argparse
|
||||
import logging
|
||||
|
||||
|
||||
log = logging.getLogger("fontTools.feaLib")
|
||||
|
||||
|
||||
def main(args=None):
|
||||
"""Add features from a feature file (.fea) into an OTF font"""
|
||||
parser = argparse.ArgumentParser(
|
||||
description="Use fontTools to compile OpenType feature files (*.fea)."
|
||||
)
|
||||
parser.add_argument(
|
||||
"input_fea", metavar="FEATURES", help="Path to the feature file"
|
||||
)
|
||||
parser.add_argument(
|
||||
"input_font", metavar="INPUT_FONT", help="Path to the input font"
|
||||
)
|
||||
parser.add_argument(
|
||||
"-o",
|
||||
"--output",
|
||||
dest="output_font",
|
||||
metavar="OUTPUT_FONT",
|
||||
help="Path to the output font.",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-t",
|
||||
"--tables",
|
||||
metavar="TABLE_TAG",
|
||||
choices=Builder.supportedTables,
|
||||
nargs="+",
|
||||
help="Specify the table(s) to be built.",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-d",
|
||||
"--debug",
|
||||
action="store_true",
|
||||
help="Add source-level debugging information to font.",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-v",
|
||||
"--verbose",
|
||||
help="Increase the logger verbosity. Multiple -v " "options are allowed.",
|
||||
action="count",
|
||||
default=0,
|
||||
)
|
||||
parser.add_argument(
|
||||
"--traceback", help="show traceback for exceptions.", action="store_true"
|
||||
)
|
||||
options = parser.parse_args(args)
|
||||
|
||||
levels = ["WARNING", "INFO", "DEBUG"]
|
||||
configLogger(level=levels[min(len(levels) - 1, options.verbose)])
|
||||
|
||||
output_font = options.output_font or makeOutputFileName(options.input_font)
|
||||
log.info("Compiling features to '%s'" % (output_font))
|
||||
|
||||
font = TTFont(options.input_font)
|
||||
try:
|
||||
addOpenTypeFeatures(
|
||||
font, options.input_fea, tables=options.tables, debug=options.debug
|
||||
)
|
||||
except FeatureLibError as e:
|
||||
if options.traceback:
|
||||
raise
|
||||
log.error(e)
|
||||
sys.exit(1)
|
||||
font.save(output_font)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,22 @@
|
||||
class FeatureLibError(Exception):
|
||||
def __init__(self, message, location=None):
|
||||
Exception.__init__(self, message)
|
||||
self.location = location
|
||||
|
||||
def __str__(self):
|
||||
message = Exception.__str__(self)
|
||||
if self.location:
|
||||
return f"{self.location}: {message}"
|
||||
else:
|
||||
return message
|
||||
|
||||
|
||||
class IncludedFeaNotFound(FeatureLibError):
|
||||
def __str__(self):
|
||||
assert self.location is not None
|
||||
|
||||
message = (
|
||||
"The following feature file should be included but cannot be found: "
|
||||
f"{Exception.__str__(self)}"
|
||||
)
|
||||
return f"{self.location}: {message}"
|
||||
File diff suppressed because one or more lines are too long
Executable
BIN
Binary file not shown.
@@ -0,0 +1,287 @@
|
||||
from fontTools.feaLib.error import FeatureLibError, IncludedFeaNotFound
|
||||
from fontTools.feaLib.location import FeatureLibLocation
|
||||
import re
|
||||
import os
|
||||
|
||||
try:
|
||||
import cython
|
||||
except ImportError:
|
||||
# if cython not installed, use mock module with no-op decorators and types
|
||||
from fontTools.misc import cython
|
||||
|
||||
|
||||
class Lexer(object):
|
||||
NUMBER = "NUMBER"
|
||||
HEXADECIMAL = "HEXADECIMAL"
|
||||
OCTAL = "OCTAL"
|
||||
NUMBERS = (NUMBER, HEXADECIMAL, OCTAL)
|
||||
FLOAT = "FLOAT"
|
||||
STRING = "STRING"
|
||||
NAME = "NAME"
|
||||
FILENAME = "FILENAME"
|
||||
GLYPHCLASS = "GLYPHCLASS"
|
||||
CID = "CID"
|
||||
SYMBOL = "SYMBOL"
|
||||
COMMENT = "COMMENT"
|
||||
NEWLINE = "NEWLINE"
|
||||
ANONYMOUS_BLOCK = "ANONYMOUS_BLOCK"
|
||||
|
||||
CHAR_WHITESPACE_ = " \t"
|
||||
CHAR_NEWLINE_ = "\r\n"
|
||||
CHAR_SYMBOL_ = ",;:-+'{}[]<>()="
|
||||
CHAR_DIGIT_ = "0123456789"
|
||||
CHAR_HEXDIGIT_ = "0123456789ABCDEFabcdef"
|
||||
CHAR_LETTER_ = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
||||
CHAR_NAME_START_ = CHAR_LETTER_ + "_+*:.^~!\\"
|
||||
CHAR_NAME_CONTINUATION_ = CHAR_LETTER_ + CHAR_DIGIT_ + "_.+*:^~!/-"
|
||||
|
||||
RE_GLYPHCLASS = re.compile(r"^[A-Za-z_0-9.\-]+$")
|
||||
|
||||
MODE_NORMAL_ = "NORMAL"
|
||||
MODE_FILENAME_ = "FILENAME"
|
||||
|
||||
def __init__(self, text, filename):
|
||||
self.filename_ = filename
|
||||
self.line_ = 1
|
||||
self.pos_ = 0
|
||||
self.line_start_ = 0
|
||||
self.text_ = text
|
||||
self.text_length_ = len(text)
|
||||
self.mode_ = Lexer.MODE_NORMAL_
|
||||
|
||||
def __iter__(self):
|
||||
return self
|
||||
|
||||
def next(self): # Python 2
|
||||
return self.__next__()
|
||||
|
||||
def __next__(self): # Python 3
|
||||
while True:
|
||||
token_type, token, location = self.next_()
|
||||
if token_type != Lexer.NEWLINE:
|
||||
return (token_type, token, location)
|
||||
|
||||
def location_(self):
|
||||
column = self.pos_ - self.line_start_ + 1
|
||||
return FeatureLibLocation(self.filename_ or "<features>", self.line_, column)
|
||||
|
||||
def next_(self):
|
||||
self.scan_over_(Lexer.CHAR_WHITESPACE_)
|
||||
location = self.location_()
|
||||
start = self.pos_
|
||||
text = self.text_
|
||||
limit = len(text)
|
||||
if start >= limit:
|
||||
raise StopIteration()
|
||||
cur_char = text[start]
|
||||
next_char = text[start + 1] if start + 1 < limit else None
|
||||
|
||||
if cur_char == "\n":
|
||||
self.pos_ += 1
|
||||
self.line_ += 1
|
||||
self.line_start_ = self.pos_
|
||||
return (Lexer.NEWLINE, None, location)
|
||||
if cur_char == "\r":
|
||||
self.pos_ += 2 if next_char == "\n" else 1
|
||||
self.line_ += 1
|
||||
self.line_start_ = self.pos_
|
||||
return (Lexer.NEWLINE, None, location)
|
||||
if cur_char == "#":
|
||||
self.scan_until_(Lexer.CHAR_NEWLINE_)
|
||||
return (Lexer.COMMENT, text[start : self.pos_], location)
|
||||
|
||||
if self.mode_ is Lexer.MODE_FILENAME_:
|
||||
if cur_char != "(":
|
||||
raise FeatureLibError("Expected '(' before file name", location)
|
||||
self.scan_until_(")")
|
||||
cur_char = text[self.pos_] if self.pos_ < limit else None
|
||||
if cur_char != ")":
|
||||
raise FeatureLibError("Expected ')' after file name", location)
|
||||
self.pos_ += 1
|
||||
self.mode_ = Lexer.MODE_NORMAL_
|
||||
return (Lexer.FILENAME, text[start + 1 : self.pos_ - 1], location)
|
||||
|
||||
if cur_char == "\\" and next_char in Lexer.CHAR_DIGIT_:
|
||||
self.pos_ += 1
|
||||
self.scan_over_(Lexer.CHAR_DIGIT_)
|
||||
return (Lexer.CID, int(text[start + 1 : self.pos_], 10), location)
|
||||
if cur_char == "@":
|
||||
self.pos_ += 1
|
||||
self.scan_over_(Lexer.CHAR_NAME_CONTINUATION_)
|
||||
glyphclass = text[start + 1 : self.pos_]
|
||||
if len(glyphclass) < 1:
|
||||
raise FeatureLibError("Expected glyph class name", location)
|
||||
if not Lexer.RE_GLYPHCLASS.match(glyphclass):
|
||||
raise FeatureLibError(
|
||||
"Glyph class names must consist of letters, digits, "
|
||||
"underscore, period or hyphen",
|
||||
location,
|
||||
)
|
||||
return (Lexer.GLYPHCLASS, glyphclass, location)
|
||||
if cur_char in Lexer.CHAR_NAME_START_:
|
||||
self.pos_ += 1
|
||||
self.scan_over_(Lexer.CHAR_NAME_CONTINUATION_)
|
||||
token = text[start : self.pos_]
|
||||
if token == "include":
|
||||
self.mode_ = Lexer.MODE_FILENAME_
|
||||
return (Lexer.NAME, token, location)
|
||||
if cur_char == "0" and next_char in "xX":
|
||||
self.pos_ += 2
|
||||
self.scan_over_(Lexer.CHAR_HEXDIGIT_)
|
||||
return (Lexer.HEXADECIMAL, int(text[start : self.pos_], 16), location)
|
||||
if cur_char == "0" and next_char in Lexer.CHAR_DIGIT_:
|
||||
self.scan_over_(Lexer.CHAR_DIGIT_)
|
||||
return (Lexer.OCTAL, int(text[start : self.pos_], 8), location)
|
||||
if cur_char in Lexer.CHAR_DIGIT_:
|
||||
self.scan_over_(Lexer.CHAR_DIGIT_)
|
||||
if self.pos_ >= limit or text[self.pos_] != ".":
|
||||
return (Lexer.NUMBER, int(text[start : self.pos_], 10), location)
|
||||
self.scan_over_(".")
|
||||
self.scan_over_(Lexer.CHAR_DIGIT_)
|
||||
return (Lexer.FLOAT, float(text[start : self.pos_]), location)
|
||||
if cur_char == "-" and next_char in Lexer.CHAR_DIGIT_:
|
||||
self.pos_ += 1
|
||||
self.scan_over_(Lexer.CHAR_DIGIT_)
|
||||
if self.pos_ >= limit or text[self.pos_] != ".":
|
||||
return (Lexer.NUMBER, int(text[start : self.pos_], 10), location)
|
||||
self.scan_over_(".")
|
||||
self.scan_over_(Lexer.CHAR_DIGIT_)
|
||||
return (Lexer.FLOAT, float(text[start : self.pos_]), location)
|
||||
if cur_char in Lexer.CHAR_SYMBOL_:
|
||||
self.pos_ += 1
|
||||
return (Lexer.SYMBOL, cur_char, location)
|
||||
if cur_char == '"':
|
||||
self.pos_ += 1
|
||||
self.scan_until_('"')
|
||||
if self.pos_ < self.text_length_ and self.text_[self.pos_] == '"':
|
||||
self.pos_ += 1
|
||||
# strip newlines embedded within a string
|
||||
string = re.sub("[\r\n]", "", text[start + 1 : self.pos_ - 1])
|
||||
return (Lexer.STRING, string, location)
|
||||
else:
|
||||
raise FeatureLibError("Expected '\"' to terminate string", location)
|
||||
raise FeatureLibError("Unexpected character: %r" % cur_char, location)
|
||||
|
||||
def scan_over_(self, valid):
|
||||
p = self.pos_
|
||||
while p < self.text_length_ and self.text_[p] in valid:
|
||||
p += 1
|
||||
self.pos_ = p
|
||||
|
||||
def scan_until_(self, stop_at):
|
||||
p = self.pos_
|
||||
while p < self.text_length_ and self.text_[p] not in stop_at:
|
||||
p += 1
|
||||
self.pos_ = p
|
||||
|
||||
def scan_anonymous_block(self, tag):
|
||||
location = self.location_()
|
||||
tag = tag.strip()
|
||||
self.scan_until_(Lexer.CHAR_NEWLINE_)
|
||||
self.scan_over_(Lexer.CHAR_NEWLINE_)
|
||||
regexp = r"}\s*" + tag + r"\s*;"
|
||||
split = re.split(regexp, self.text_[self.pos_ :], maxsplit=1)
|
||||
if len(split) != 2:
|
||||
raise FeatureLibError(
|
||||
"Expected '} %s;' to terminate anonymous block" % tag, location
|
||||
)
|
||||
self.pos_ += len(split[0])
|
||||
return (Lexer.ANONYMOUS_BLOCK, split[0], location)
|
||||
|
||||
|
||||
class IncludingLexer(object):
|
||||
"""A Lexer that follows include statements.
|
||||
|
||||
The OpenType feature file specification states that due to
|
||||
historical reasons, relative imports should be resolved in this
|
||||
order:
|
||||
|
||||
1. If the source font is UFO format, then relative to the UFO's
|
||||
font directory
|
||||
2. relative to the top-level include file
|
||||
3. relative to the parent include file
|
||||
|
||||
We only support 1 (via includeDir) and 2.
|
||||
"""
|
||||
|
||||
def __init__(self, featurefile, *, includeDir=None):
|
||||
"""Initializes an IncludingLexer.
|
||||
|
||||
Behavior:
|
||||
If includeDir is passed, it will be used to determine the top-level
|
||||
include directory to use for all encountered include statements. If it is
|
||||
not passed, ``os.path.dirname(featurefile)`` will be considered the
|
||||
include directory.
|
||||
"""
|
||||
|
||||
self.lexers_ = [self.make_lexer_(featurefile)]
|
||||
self.featurefilepath = self.lexers_[0].filename_
|
||||
self.includeDir = includeDir
|
||||
|
||||
def __iter__(self):
|
||||
return self
|
||||
|
||||
def next(self): # Python 2
|
||||
return self.__next__()
|
||||
|
||||
def __next__(self): # Python 3
|
||||
while self.lexers_:
|
||||
lexer = self.lexers_[-1]
|
||||
try:
|
||||
token_type, token, location = next(lexer)
|
||||
except StopIteration:
|
||||
self.lexers_.pop()
|
||||
continue
|
||||
if token_type is Lexer.NAME and token == "include":
|
||||
fname_type, fname_token, fname_location = lexer.next()
|
||||
if fname_type is not Lexer.FILENAME:
|
||||
raise FeatureLibError("Expected file name", fname_location)
|
||||
# semi_type, semi_token, semi_location = lexer.next()
|
||||
# if semi_type is not Lexer.SYMBOL or semi_token != ";":
|
||||
# raise FeatureLibError("Expected ';'", semi_location)
|
||||
if os.path.isabs(fname_token):
|
||||
path = fname_token
|
||||
else:
|
||||
if self.includeDir is not None:
|
||||
curpath = self.includeDir
|
||||
elif self.featurefilepath is not None:
|
||||
curpath = os.path.dirname(self.featurefilepath)
|
||||
else:
|
||||
# if the IncludingLexer was initialized from an in-memory
|
||||
# file-like stream, it doesn't have a 'name' pointing to
|
||||
# its filesystem path, therefore we fall back to using the
|
||||
# current working directory to resolve relative includes
|
||||
curpath = os.getcwd()
|
||||
path = os.path.join(curpath, fname_token)
|
||||
if len(self.lexers_) >= 5:
|
||||
raise FeatureLibError("Too many recursive includes", fname_location)
|
||||
try:
|
||||
self.lexers_.append(self.make_lexer_(path))
|
||||
except FileNotFoundError as err:
|
||||
raise IncludedFeaNotFound(fname_token, fname_location) from err
|
||||
else:
|
||||
return (token_type, token, location)
|
||||
raise StopIteration()
|
||||
|
||||
@staticmethod
|
||||
def make_lexer_(file_or_path):
|
||||
if hasattr(file_or_path, "read"):
|
||||
fileobj, closing = file_or_path, False
|
||||
else:
|
||||
filename, closing = file_or_path, True
|
||||
fileobj = open(filename, "r", encoding="utf-8-sig")
|
||||
data = fileobj.read()
|
||||
filename = getattr(fileobj, "name", None)
|
||||
if closing:
|
||||
fileobj.close()
|
||||
return Lexer(data, filename)
|
||||
|
||||
def scan_anonymous_block(self, tag):
|
||||
return self.lexers_[-1].scan_anonymous_block(tag)
|
||||
|
||||
|
||||
class NonIncludingLexer(IncludingLexer):
|
||||
"""Lexer that does not follow `include` statements, emits them as-is."""
|
||||
|
||||
def __next__(self): # Python 3
|
||||
return next(self.lexers_[0])
|
||||
@@ -0,0 +1,12 @@
|
||||
from typing import NamedTuple
|
||||
|
||||
|
||||
class FeatureLibLocation(NamedTuple):
|
||||
"""A location in a feature file"""
|
||||
|
||||
file: str
|
||||
line: int
|
||||
column: int
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.file}:{self.line}:{self.column}"
|
||||
@@ -0,0 +1,12 @@
|
||||
from typing import NamedTuple
|
||||
|
||||
LOOKUP_DEBUG_INFO_KEY = "com.github.fonttools.feaLib"
|
||||
LOOKUP_DEBUG_ENV_VAR = "FONTTOOLS_LOOKUP_DEBUGGING"
|
||||
|
||||
|
||||
class LookupDebugInfo(NamedTuple):
|
||||
"""Information about where a lookup came from, to be embedded in a font"""
|
||||
|
||||
location: str
|
||||
name: str
|
||||
feature: list
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,265 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Mapping
|
||||
from dataclasses import dataclass
|
||||
|
||||
from fontTools.designspaceLib import DesignSpaceDocument
|
||||
from fontTools.ttLib.ttFont import TTFont
|
||||
from fontTools.varLib.models import (
|
||||
VariationModel,
|
||||
noRound,
|
||||
normalizeValue,
|
||||
piecewiseLinearMap,
|
||||
)
|
||||
|
||||
import typing
|
||||
import warnings
|
||||
|
||||
if typing.TYPE_CHECKING:
|
||||
from typing import Self
|
||||
|
||||
LocationTuple = tuple[tuple[str, float], ...]
|
||||
"""A hashable location."""
|
||||
|
||||
|
||||
def Location(location: Mapping[str, float]) -> LocationTuple:
|
||||
"""Create a hashable location from a dictionary-like location."""
|
||||
return tuple(sorted(location.items()))
|
||||
|
||||
|
||||
class VariableScalar:
|
||||
"""A scalar with different values at different points in the designspace."""
|
||||
|
||||
values: dict[LocationTuple, int]
|
||||
"""The values across various user-locations. Must always include the default
|
||||
location by time of building."""
|
||||
|
||||
def __init__(self, location_value=None):
|
||||
self.values = {
|
||||
Location(location): value
|
||||
for location, value in (location_value or {}).items()
|
||||
}
|
||||
# Deprecated: only used by the add_to_variation_store() backwards-compat
|
||||
# shim. New code should use VariableScalarBuilder instead.
|
||||
self.axes = []
|
||||
|
||||
def __repr__(self):
|
||||
items = []
|
||||
for location, value in self.values.items():
|
||||
loc = ",".join(
|
||||
[
|
||||
f"{ax}={int(coord) if float(coord).is_integer() else coord}"
|
||||
for ax, coord in location
|
||||
]
|
||||
)
|
||||
items.append("%s:%i" % (loc, value))
|
||||
return "(" + (" ".join(items)) + ")"
|
||||
|
||||
@property
|
||||
def does_vary(self) -> bool:
|
||||
values = list(self.values.values())
|
||||
return any(v != values[0] for v in values[1:])
|
||||
|
||||
def add_value(self, location: Mapping[str, float], value: int):
|
||||
self.values[Location(location)] = value
|
||||
|
||||
def add_to_variation_store(self, store_builder, model_cache=None, avar=None):
|
||||
"""Deprecated: use VariableScalarBuilder.add_to_variation_store() instead."""
|
||||
warnings.warn(
|
||||
"VariableScalar.add_to_variation_store() is deprecated. "
|
||||
"Use VariableScalarBuilder.add_to_variation_store() instead.",
|
||||
DeprecationWarning,
|
||||
stacklevel=2,
|
||||
)
|
||||
if not self.axes:
|
||||
raise ValueError(
|
||||
".axes must be defined on variable scalar before calling "
|
||||
"add_to_variation_store()"
|
||||
)
|
||||
builder = VariableScalarBuilder(
|
||||
axis_triples={
|
||||
ax.axisTag: (ax.minValue, ax.defaultValue, ax.maxValue)
|
||||
for ax in self.axes
|
||||
},
|
||||
axis_mappings=({} if avar is None else dict(avar.segments)),
|
||||
model_cache=model_cache if model_cache is not None else {},
|
||||
)
|
||||
return builder.add_to_variation_store(self, store_builder)
|
||||
|
||||
|
||||
@dataclass
|
||||
class VariableScalarBuilder:
|
||||
"""A helper class for building variable scalars, or otherwise interrogating
|
||||
their variation model for interpolation or similar."""
|
||||
|
||||
axis_triples: dict[str, tuple[float, float, float]]
|
||||
"""Minimum, default, and maximum for each axis in user-coordinates."""
|
||||
axis_mappings: dict[str, Mapping[float, float]]
|
||||
"""Optional mappings from normalized user-coordinates to normalized
|
||||
design-coordinates."""
|
||||
|
||||
model_cache: dict[tuple[LocationTuple, ...], VariationModel]
|
||||
"""We often use the same exact locations (i.e. font sources) for a large
|
||||
number of variable scalars. Instead of creating a model for each, cache
|
||||
them. Cache by user-location to avoid repeated mapping computations."""
|
||||
|
||||
@classmethod
|
||||
def from_ttf(cls, ttf: TTFont) -> Self:
|
||||
return cls(
|
||||
axis_triples={
|
||||
axis.axisTag: (axis.minValue, axis.defaultValue, axis.maxValue)
|
||||
for axis in ttf["fvar"].axes
|
||||
},
|
||||
axis_mappings=(
|
||||
{}
|
||||
if (avar := ttf.get("avar")) is None
|
||||
else {axis: segments for axis, segments in avar.segments.items()}
|
||||
),
|
||||
model_cache={},
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def from_designspace(cls, doc: DesignSpaceDocument) -> Self:
|
||||
return cls(
|
||||
axis_triples={
|
||||
axis.tag: (axis.minimum, axis.default, axis.maximum)
|
||||
for axis in doc.axes
|
||||
},
|
||||
axis_mappings={
|
||||
axis.tag: {
|
||||
normalizeValue(
|
||||
user, (axis.minimum, axis.default, axis.maximum)
|
||||
): normalizeValue(
|
||||
design,
|
||||
(
|
||||
axis.map_forward(axis.minimum),
|
||||
axis.map_forward(axis.default),
|
||||
axis.map_forward(axis.maximum),
|
||||
),
|
||||
)
|
||||
for user, design in axis.map
|
||||
}
|
||||
for axis in doc.axes
|
||||
if axis.map
|
||||
},
|
||||
model_cache={},
|
||||
)
|
||||
|
||||
def _fully_specify_location(self, location: LocationTuple) -> LocationTuple:
|
||||
"""Validate and fully-specify a user-space location by filling in
|
||||
missing axes with their user-space defaults."""
|
||||
|
||||
full = {}
|
||||
for axtag, value in location:
|
||||
if axtag not in self.axis_triples:
|
||||
raise ValueError("Unknown axis %s in %s" % (axtag, location))
|
||||
full[axtag] = value
|
||||
|
||||
for axtag, (_, axis_default, _) in self.axis_triples.items():
|
||||
if axtag not in full:
|
||||
full[axtag] = axis_default
|
||||
|
||||
return Location(full)
|
||||
|
||||
def _normalize_location(self, location: LocationTuple) -> dict[str, float]:
|
||||
"""Normalize a user-space location, applying avar mappings if present.
|
||||
|
||||
TODO: This only handles avar1 (per-axis piecewise linear mappings),
|
||||
not avar2 (multi-dimensional mappings).
|
||||
"""
|
||||
|
||||
result = {}
|
||||
for axtag, value in location:
|
||||
axis_min, axis_default, axis_max = self.axis_triples[axtag]
|
||||
normalized = normalizeValue(value, (axis_min, axis_default, axis_max))
|
||||
mapping = self.axis_mappings.get(axtag)
|
||||
if mapping is not None:
|
||||
normalized = piecewiseLinearMap(normalized, mapping)
|
||||
result[axtag] = normalized
|
||||
|
||||
return result
|
||||
|
||||
def _full_locations_and_values(
|
||||
self, scalar: VariableScalar
|
||||
) -> list[tuple[LocationTuple, int]]:
|
||||
"""Return a list of (fully-specified user-space location, value) pairs,
|
||||
preserving order and length of scalar.values."""
|
||||
|
||||
return [
|
||||
(self._fully_specify_location(loc), val)
|
||||
for loc, val in scalar.values.items()
|
||||
]
|
||||
|
||||
def default_value(self, scalar: VariableScalar) -> int:
|
||||
"""Get the default value of a variable scalar."""
|
||||
|
||||
default_loc = Location(
|
||||
{tag: default for tag, (_, default, _) in self.axis_triples.items()}
|
||||
)
|
||||
for location, value in self._full_locations_and_values(scalar):
|
||||
if location == default_loc:
|
||||
return value
|
||||
|
||||
raise ValueError("Default value could not be found")
|
||||
|
||||
def value_at_location(
|
||||
self, scalar: VariableScalar, location: LocationTuple
|
||||
) -> float:
|
||||
"""Interpolate the value of a scalar from a user-location."""
|
||||
|
||||
location = self._fully_specify_location(location)
|
||||
pairs = self._full_locations_and_values(scalar)
|
||||
|
||||
# If user location matches exactly, no axis mapping or variation model needed.
|
||||
for loc, val in pairs:
|
||||
if loc == location:
|
||||
return val
|
||||
|
||||
values = [val for _, val in pairs]
|
||||
normalized_location = self._normalize_location(location)
|
||||
|
||||
value = self.model(scalar).interpolateFromMasters(normalized_location, values)
|
||||
if value is None:
|
||||
raise ValueError("Insufficient number of values to interpolate")
|
||||
|
||||
return value
|
||||
|
||||
def model(self, scalar: VariableScalar) -> VariationModel:
|
||||
"""Return a variation model based on a scalar's values.
|
||||
|
||||
Variable scalars with the same fully-specified user-locations will use
|
||||
the same cached variation model."""
|
||||
|
||||
pairs = self._full_locations_and_values(scalar)
|
||||
cache_key = tuple(loc for loc, _ in pairs)
|
||||
|
||||
cached_model = self.model_cache.get(cache_key)
|
||||
if cached_model is not None:
|
||||
return cached_model
|
||||
|
||||
normalized_locations = [self._normalize_location(loc) for loc, _ in pairs]
|
||||
axisOrder = list(self.axis_triples.keys())
|
||||
model = self.model_cache[cache_key] = VariationModel(
|
||||
normalized_locations, axisOrder=axisOrder
|
||||
)
|
||||
|
||||
return model
|
||||
|
||||
def get_deltas_and_supports(self, scalar: VariableScalar):
|
||||
"""Calculate deltas and supports from this scalar's variation model."""
|
||||
values = list(scalar.values.values())
|
||||
return self.model(scalar).getDeltasAndSupports(values, round=round)
|
||||
|
||||
def add_to_variation_store(
|
||||
self, scalar: VariableScalar, store_builder
|
||||
) -> tuple[int, int]:
|
||||
"""Serialize this scalar's variation model to a store, returning the
|
||||
default value and variation index."""
|
||||
|
||||
deltas, supports = self.get_deltas_and_supports(scalar)
|
||||
store_builder.setSupports(supports)
|
||||
index = store_builder.storeDeltas(deltas, round=noRound)
|
||||
|
||||
# NOTE: Default value should be an exact integer by construction of
|
||||
# VariableScalar.
|
||||
return int(self.default_value(scalar)), index
|
||||
Reference in New Issue
Block a user