2018-06-14 15:25:09 +01:00
|
|
|
"""Module to build FeatureVariation tables:
|
|
|
|
https://docs.microsoft.com/en-us/typography/opentype/spec/chapter2#featurevariations-table
|
|
|
|
|
|
|
|
NOTE: The API is experimental and subject to change.
|
|
|
|
"""
|
2018-11-10 15:14:36 -05:00
|
|
|
from fontTools.misc.dictTools import hashdict
|
2018-11-10 15:16:52 -05:00
|
|
|
from fontTools.misc.intTools import popCount
|
2018-04-16 10:21:19 +02:00
|
|
|
from fontTools.ttLib import newTable
|
|
|
|
from fontTools.ttLib.tables import otTables as ot
|
2022-10-28 13:20:32 -06:00
|
|
|
from fontTools.ttLib.ttVisitor import TTVisitor
|
2018-04-16 10:21:19 +02:00
|
|
|
from fontTools.otlLib.builder import buildLookup, buildSingleSubstSubtable
|
2018-11-09 10:39:19 -05:00
|
|
|
from collections import OrderedDict
|
2018-04-16 10:21:19 +02:00
|
|
|
|
2021-01-19 16:42:40 +00:00
|
|
|
from .errors import VarLibError, VarLibValidationError
|
2020-02-11 13:54:08 +00:00
|
|
|
|
2018-04-16 10:21:19 +02:00
|
|
|
|
2023-05-02 10:53:43 -06:00
|
|
|
def addFeatureVariations(font, conditionalSubstitutions, featureTag="rvrn"):
|
2018-04-16 10:21:19 +02:00
|
|
|
"""Add conditional substitutions to a Variable Font.
|
|
|
|
|
|
|
|
The `conditionalSubstitutions` argument is a list of (Region, Substitutions)
|
|
|
|
tuples.
|
|
|
|
|
[varLib.featureVars] Rewrite algorithm
Whereas previous algorithm had exponential running time and return
value size, new one has quadratic.
For featureVars_test.py test case, for example, which is a pathological
test case of n sliding intervals, the number of output intervals of
various algorithms are:
- Previous algorithm: 2**n - 1
- New algorithm: n*(n-1)/2
- Optimal algorithm: 2*n - 1
Ie, we go from exponential to quadratic, whereas in this case the optimal
solution is linear.
Running time of said test, for n=20, goes from over 20s, to 0.06s.
The algorithm can be improved. The overlayBox() function currently does
not try to shrink the remainder box. Doing that will probably bring us
to optimal solution for this test case.
Fixes https://github.com/fonttools/fonttools/pull/1372
One test is failing. Needs to be investigated that new output is correct,
and test expectations updated.
2018-11-09 14:15:32 -05:00
|
|
|
A Region is a list of Boxes. A Box is a dict mapping axisTags to
|
|
|
|
(minValue, maxValue) tuples. Irrelevant axes may be omitted and they are
|
|
|
|
interpretted as extending to end of axis in each direction. A Box represents
|
|
|
|
an orthogonal 'rectangular' subset of an N-dimensional design space.
|
2018-04-16 10:21:19 +02:00
|
|
|
A Region represents a more complex subset of an N-dimensional design space,
|
[varLib.featureVars] Rewrite algorithm
Whereas previous algorithm had exponential running time and return
value size, new one has quadratic.
For featureVars_test.py test case, for example, which is a pathological
test case of n sliding intervals, the number of output intervals of
various algorithms are:
- Previous algorithm: 2**n - 1
- New algorithm: n*(n-1)/2
- Optimal algorithm: 2*n - 1
Ie, we go from exponential to quadratic, whereas in this case the optimal
solution is linear.
Running time of said test, for n=20, goes from over 20s, to 0.06s.
The algorithm can be improved. The overlayBox() function currently does
not try to shrink the remainder box. Doing that will probably bring us
to optimal solution for this test case.
Fixes https://github.com/fonttools/fonttools/pull/1372
One test is failing. Needs to be investigated that new output is correct,
and test expectations updated.
2018-11-09 14:15:32 -05:00
|
|
|
ie. the union of all the Boxes in the Region.
|
|
|
|
For efficiency, Boxes within a Region should ideally not overlap, but
|
2018-04-16 10:21:19 +02:00
|
|
|
functionality is not compromised if they do.
|
|
|
|
|
2018-04-18 08:51:45 +02:00
|
|
|
The minimum and maximum values are expressed in normalized coordinates.
|
2018-04-16 10:21:19 +02:00
|
|
|
|
|
|
|
A Substitution is a dict mapping source glyph names to substitute glyph names.
|
2018-11-09 11:03:19 -05:00
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
# >>> f = TTFont(srcPath)
|
2018-11-09 12:11:48 -05:00
|
|
|
# >>> condSubst = [
|
|
|
|
# ... # A list of (Region, Substitution) tuples.
|
|
|
|
# ... ([{"wdth": (0.5, 1.0)}], {"cent": "cent.rvrn"}),
|
[varLib.featureVars] Rewrite algorithm
Whereas previous algorithm had exponential running time and return
value size, new one has quadratic.
For featureVars_test.py test case, for example, which is a pathological
test case of n sliding intervals, the number of output intervals of
various algorithms are:
- Previous algorithm: 2**n - 1
- New algorithm: n*(n-1)/2
- Optimal algorithm: 2*n - 1
Ie, we go from exponential to quadratic, whereas in this case the optimal
solution is linear.
Running time of said test, for n=20, goes from over 20s, to 0.06s.
The algorithm can be improved. The overlayBox() function currently does
not try to shrink the remainder box. Doing that will probably bring us
to optimal solution for this test case.
Fixes https://github.com/fonttools/fonttools/pull/1372
One test is failing. Needs to be investigated that new output is correct,
and test expectations updated.
2018-11-09 14:15:32 -05:00
|
|
|
# ... ([{"wght": (0.5, 1.0)}], {"dollar": "dollar.rvrn"}),
|
2018-11-09 12:11:48 -05:00
|
|
|
# ... ]
|
|
|
|
# >>> addFeatureVariations(f, condSubst)
|
|
|
|
# >>> f.save(dstPath)
|
2018-04-16 10:21:19 +02:00
|
|
|
"""
|
|
|
|
|
2023-05-02 10:53:43 -06:00
|
|
|
processLast = featureTag != "rvrn"
|
2023-05-01 11:36:39 -06:00
|
|
|
|
2022-06-17 15:24:54 +02:00
|
|
|
_checkSubstitutionGlyphsExist(
|
|
|
|
glyphNames=set(font.getGlyphOrder()),
|
|
|
|
substitutions=conditionalSubstitutions,
|
|
|
|
)
|
2021-10-28 11:58:54 +01:00
|
|
|
|
|
|
|
substitutions = overlayFeatureVariations(conditionalSubstitutions)
|
|
|
|
|
|
|
|
# turn substitution dicts into tuples of tuples, so they are hashable
|
|
|
|
conditionalSubstitutions, allSubstitutions = makeSubstitutionsHashable(
|
|
|
|
substitutions
|
|
|
|
)
|
|
|
|
if "GSUB" not in font:
|
|
|
|
font["GSUB"] = buildGSUB()
|
|
|
|
|
|
|
|
# setup lookups
|
2023-05-01 11:36:39 -06:00
|
|
|
lookupMap = buildSubstitutionLookups(
|
|
|
|
font["GSUB"].table, allSubstitutions, processLast
|
|
|
|
)
|
2021-10-28 11:58:54 +01:00
|
|
|
|
|
|
|
# addFeatureVariationsRaw takes a list of
|
|
|
|
# ( {condition}, [ lookup indices ] )
|
|
|
|
# so rearrange our lookups to match
|
|
|
|
conditionsAndLookups = []
|
|
|
|
for conditionSet, substitutions in conditionalSubstitutions:
|
|
|
|
conditionsAndLookups.append(
|
|
|
|
(conditionSet, [lookupMap[s] for s in substitutions])
|
2022-12-13 11:26:36 +00:00
|
|
|
)
|
|
|
|
|
2023-05-02 10:53:43 -06:00
|
|
|
addFeatureVariationsRaw(font, font["GSUB"].table, conditionsAndLookups, featureTag)
|
2021-10-28 11:58:54 +01:00
|
|
|
|
2018-11-09 11:03:19 -05:00
|
|
|
|
2022-06-17 15:24:54 +02:00
|
|
|
def _checkSubstitutionGlyphsExist(glyphNames, substitutions):
|
|
|
|
referencedGlyphNames = set()
|
|
|
|
for _, substitution in substitutions:
|
|
|
|
referencedGlyphNames |= substitution.keys()
|
|
|
|
referencedGlyphNames |= set(substitution.values())
|
|
|
|
missing = referencedGlyphNames - glyphNames
|
|
|
|
if missing:
|
|
|
|
raise VarLibValidationError(
|
|
|
|
"Missing glyphs are referenced in conditional substitution rules:"
|
|
|
|
f" {', '.join(missing)}"
|
|
|
|
)
|
|
|
|
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2018-11-09 11:03:19 -05:00
|
|
|
def overlayFeatureVariations(conditionalSubstitutions):
|
|
|
|
"""Compute overlaps between all conditional substitutions.
|
|
|
|
|
|
|
|
The `conditionalSubstitutions` argument is a list of (Region, Substitutions)
|
|
|
|
tuples.
|
|
|
|
|
[varLib.featureVars] Rewrite algorithm
Whereas previous algorithm had exponential running time and return
value size, new one has quadratic.
For featureVars_test.py test case, for example, which is a pathological
test case of n sliding intervals, the number of output intervals of
various algorithms are:
- Previous algorithm: 2**n - 1
- New algorithm: n*(n-1)/2
- Optimal algorithm: 2*n - 1
Ie, we go from exponential to quadratic, whereas in this case the optimal
solution is linear.
Running time of said test, for n=20, goes from over 20s, to 0.06s.
The algorithm can be improved. The overlayBox() function currently does
not try to shrink the remainder box. Doing that will probably bring us
to optimal solution for this test case.
Fixes https://github.com/fonttools/fonttools/pull/1372
One test is failing. Needs to be investigated that new output is correct,
and test expectations updated.
2018-11-09 14:15:32 -05:00
|
|
|
A Region is a list of Boxes. A Box is a dict mapping axisTags to
|
|
|
|
(minValue, maxValue) tuples. Irrelevant axes may be omitted and they are
|
|
|
|
interpretted as extending to end of axis in each direction. A Box represents
|
|
|
|
an orthogonal 'rectangular' subset of an N-dimensional design space.
|
2018-04-16 10:21:19 +02:00
|
|
|
A Region represents a more complex subset of an N-dimensional design space,
|
[varLib.featureVars] Rewrite algorithm
Whereas previous algorithm had exponential running time and return
value size, new one has quadratic.
For featureVars_test.py test case, for example, which is a pathological
test case of n sliding intervals, the number of output intervals of
various algorithms are:
- Previous algorithm: 2**n - 1
- New algorithm: n*(n-1)/2
- Optimal algorithm: 2*n - 1
Ie, we go from exponential to quadratic, whereas in this case the optimal
solution is linear.
Running time of said test, for n=20, goes from over 20s, to 0.06s.
The algorithm can be improved. The overlayBox() function currently does
not try to shrink the remainder box. Doing that will probably bring us
to optimal solution for this test case.
Fixes https://github.com/fonttools/fonttools/pull/1372
One test is failing. Needs to be investigated that new output is correct,
and test expectations updated.
2018-11-09 14:15:32 -05:00
|
|
|
ie. the union of all the Boxes in the Region.
|
|
|
|
For efficiency, Boxes within a Region should ideally not overlap, but
|
2018-04-16 10:21:19 +02:00
|
|
|
functionality is not compromised if they do.
|
|
|
|
|
2018-04-18 08:51:45 +02:00
|
|
|
The minimum and maximum values are expressed in normalized coordinates.
|
2018-04-16 10:21:19 +02:00
|
|
|
|
|
|
|
A Substitution is a dict mapping source glyph names to substitute glyph names.
|
2018-11-09 11:03:19 -05:00
|
|
|
|
|
|
|
Returns data is in similar but different format. Overlaps of distinct
|
[varLib.featureVars] Rewrite algorithm
Whereas previous algorithm had exponential running time and return
value size, new one has quadratic.
For featureVars_test.py test case, for example, which is a pathological
test case of n sliding intervals, the number of output intervals of
various algorithms are:
- Previous algorithm: 2**n - 1
- New algorithm: n*(n-1)/2
- Optimal algorithm: 2*n - 1
Ie, we go from exponential to quadratic, whereas in this case the optimal
solution is linear.
Running time of said test, for n=20, goes from over 20s, to 0.06s.
The algorithm can be improved. The overlayBox() function currently does
not try to shrink the remainder box. Doing that will probably bring us
to optimal solution for this test case.
Fixes https://github.com/fonttools/fonttools/pull/1372
One test is failing. Needs to be investigated that new output is correct,
and test expectations updated.
2018-11-09 14:15:32 -05:00
|
|
|
substitution Boxes (*not* Regions) are explicitly listed as distinct rules,
|
|
|
|
and rules with the same Box merged. The more specific rules appear earlier
|
2018-11-09 12:15:37 -05:00
|
|
|
in the resulting list. Moreover, instead of just a dictionary of substitutions,
|
|
|
|
a list of dictionaries is returned for substitutions corresponding to each
|
2019-07-09 16:14:01 -07:00
|
|
|
unique space, with each dictionary being identical to one of the input
|
2018-11-09 12:15:37 -05:00
|
|
|
substitution dictionaries. These dictionaries are not merged to allow data
|
|
|
|
sharing when they are converted into font tables.
|
2018-11-09 11:03:19 -05:00
|
|
|
|
2021-12-02 15:31:49 +00:00
|
|
|
Example::
|
|
|
|
|
|
|
|
>>> condSubst = [
|
|
|
|
... # A list of (Region, Substitution) tuples.
|
|
|
|
... ([{"wght": (0.5, 1.0)}], {"dollar": "dollar.rvrn"}),
|
|
|
|
... ([{"wght": (0.5, 1.0)}], {"dollar": "dollar.rvrn"}),
|
|
|
|
... ([{"wdth": (0.5, 1.0)}], {"cent": "cent.rvrn"}),
|
|
|
|
... ([{"wght": (0.5, 1.0), "wdth": (-1, 1.0)}], {"dollar": "dollar.rvrn"}),
|
|
|
|
... ]
|
|
|
|
>>> from pprint import pprint
|
|
|
|
>>> pprint(overlayFeatureVariations(condSubst))
|
|
|
|
[({'wdth': (0.5, 1.0), 'wght': (0.5, 1.0)},
|
|
|
|
[{'dollar': 'dollar.rvrn'}, {'cent': 'cent.rvrn'}]),
|
|
|
|
({'wdth': (0.5, 1.0)}, [{'cent': 'cent.rvrn'}]),
|
|
|
|
({'wght': (0.5, 1.0)}, [{'dollar': 'dollar.rvrn'}])]
|
|
|
|
|
2018-04-16 10:21:19 +02:00
|
|
|
"""
|
|
|
|
|
2018-11-09 23:58:08 -05:00
|
|
|
# Merge same-substitutions rules, as this creates fewer number oflookups.
|
|
|
|
merged = OrderedDict()
|
|
|
|
for value, key in conditionalSubstitutions:
|
|
|
|
key = hashdict(key)
|
|
|
|
if key in merged:
|
|
|
|
merged[key].extend(value)
|
|
|
|
else:
|
|
|
|
merged[key] = value
|
|
|
|
conditionalSubstitutions = [(v, dict(k)) for k, v in merged.items()]
|
|
|
|
del merged
|
|
|
|
|
|
|
|
# Merge same-region rules, as this is cheaper.
|
[varLib.featureVars] Rewrite algorithm
Whereas previous algorithm had exponential running time and return
value size, new one has quadratic.
For featureVars_test.py test case, for example, which is a pathological
test case of n sliding intervals, the number of output intervals of
various algorithms are:
- Previous algorithm: 2**n - 1
- New algorithm: n*(n-1)/2
- Optimal algorithm: 2*n - 1
Ie, we go from exponential to quadratic, whereas in this case the optimal
solution is linear.
Running time of said test, for n=20, goes from over 20s, to 0.06s.
The algorithm can be improved. The overlayBox() function currently does
not try to shrink the remainder box. Doing that will probably bring us
to optimal solution for this test case.
Fixes https://github.com/fonttools/fonttools/pull/1372
One test is failing. Needs to be investigated that new output is correct,
and test expectations updated.
2018-11-09 14:15:32 -05:00
|
|
|
# Also convert boxes to hashdict()
|
2018-06-14 15:25:09 +01:00
|
|
|
#
|
[varLib.featureVars] Rewrite algorithm
Whereas previous algorithm had exponential running time and return
value size, new one has quadratic.
For featureVars_test.py test case, for example, which is a pathological
test case of n sliding intervals, the number of output intervals of
various algorithms are:
- Previous algorithm: 2**n - 1
- New algorithm: n*(n-1)/2
- Optimal algorithm: 2*n - 1
Ie, we go from exponential to quadratic, whereas in this case the optimal
solution is linear.
Running time of said test, for n=20, goes from over 20s, to 0.06s.
The algorithm can be improved. The overlayBox() function currently does
not try to shrink the remainder box. Doing that will probably bring us
to optimal solution for this test case.
Fixes https://github.com/fonttools/fonttools/pull/1372
One test is failing. Needs to be investigated that new output is correct,
and test expectations updated.
2018-11-09 14:15:32 -05:00
|
|
|
# Reversing is such that earlier entries win in case of conflicting substitution
|
|
|
|
# rules for the same region.
|
2018-11-09 10:39:19 -05:00
|
|
|
merged = OrderedDict()
|
[varLib.featureVars] Rewrite algorithm
Whereas previous algorithm had exponential running time and return
value size, new one has quadratic.
For featureVars_test.py test case, for example, which is a pathological
test case of n sliding intervals, the number of output intervals of
various algorithms are:
- Previous algorithm: 2**n - 1
- New algorithm: n*(n-1)/2
- Optimal algorithm: 2*n - 1
Ie, we go from exponential to quadratic, whereas in this case the optimal
solution is linear.
Running time of said test, for n=20, goes from over 20s, to 0.06s.
The algorithm can be improved. The overlayBox() function currently does
not try to shrink the remainder box. Doing that will probably bring us
to optimal solution for this test case.
Fixes https://github.com/fonttools/fonttools/pull/1372
One test is failing. Needs to be investigated that new output is correct,
and test expectations updated.
2018-11-09 14:15:32 -05:00
|
|
|
for key, value in reversed(conditionalSubstitutions):
|
2019-07-09 16:14:01 -07:00
|
|
|
key = tuple(
|
|
|
|
sorted(
|
|
|
|
(hashdict(cleanupBox(k)) for k in key),
|
|
|
|
key=lambda d: tuple(sorted(d.items())),
|
|
|
|
)
|
2022-12-13 11:26:36 +00:00
|
|
|
)
|
2018-11-09 10:39:19 -05:00
|
|
|
if key in merged:
|
|
|
|
merged[key].update(value)
|
|
|
|
else:
|
[varLib.featureVars] Rewrite algorithm
Whereas previous algorithm had exponential running time and return
value size, new one has quadratic.
For featureVars_test.py test case, for example, which is a pathological
test case of n sliding intervals, the number of output intervals of
various algorithms are:
- Previous algorithm: 2**n - 1
- New algorithm: n*(n-1)/2
- Optimal algorithm: 2*n - 1
Ie, we go from exponential to quadratic, whereas in this case the optimal
solution is linear.
Running time of said test, for n=20, goes from over 20s, to 0.06s.
The algorithm can be improved. The overlayBox() function currently does
not try to shrink the remainder box. Doing that will probably bring us
to optimal solution for this test case.
Fixes https://github.com/fonttools/fonttools/pull/1372
One test is failing. Needs to be investigated that new output is correct,
and test expectations updated.
2018-11-09 14:15:32 -05:00
|
|
|
merged[key] = dict(value)
|
|
|
|
conditionalSubstitutions = list(reversed(merged.items()))
|
|
|
|
del merged
|
|
|
|
|
|
|
|
# Overlay
|
|
|
|
#
|
|
|
|
# Rank is the bit-set of the index of all contributing layers.
|
|
|
|
initMapInit = ((hashdict(), 0),) # Initializer representing the entire space
|
|
|
|
boxMap = OrderedDict(initMapInit) # Map from Box to Rank
|
|
|
|
for i, (currRegion, _) in enumerate(conditionalSubstitutions):
|
|
|
|
newMap = OrderedDict(initMapInit)
|
|
|
|
currRank = 1 << i
|
|
|
|
for box, rank in boxMap.items():
|
|
|
|
for currBox in currRegion:
|
|
|
|
intersection, remainder = overlayBox(currBox, box)
|
|
|
|
if intersection is not None:
|
|
|
|
intersection = hashdict(intersection)
|
|
|
|
newMap[intersection] = newMap.get(intersection, 0) | rank | currRank
|
|
|
|
if remainder is not None:
|
|
|
|
remainder = hashdict(remainder)
|
|
|
|
newMap[remainder] = newMap.get(remainder, 0) | rank
|
|
|
|
boxMap = newMap
|
|
|
|
|
|
|
|
# Generate output
|
|
|
|
items = []
|
|
|
|
for box, rank in sorted(
|
|
|
|
boxMap.items(), key=(lambda BoxAndRank: -popCount(BoxAndRank[1]))
|
|
|
|
):
|
2020-05-15 17:27:15 +01:00
|
|
|
# Skip any box that doesn't have any substitution.
|
|
|
|
if rank == 0:
|
|
|
|
continue
|
[varLib.featureVars] Rewrite algorithm
Whereas previous algorithm had exponential running time and return
value size, new one has quadratic.
For featureVars_test.py test case, for example, which is a pathological
test case of n sliding intervals, the number of output intervals of
various algorithms are:
- Previous algorithm: 2**n - 1
- New algorithm: n*(n-1)/2
- Optimal algorithm: 2*n - 1
Ie, we go from exponential to quadratic, whereas in this case the optimal
solution is linear.
Running time of said test, for n=20, goes from over 20s, to 0.06s.
The algorithm can be improved. The overlayBox() function currently does
not try to shrink the remainder box. Doing that will probably bring us
to optimal solution for this test case.
Fixes https://github.com/fonttools/fonttools/pull/1372
One test is failing. Needs to be investigated that new output is correct,
and test expectations updated.
2018-11-09 14:15:32 -05:00
|
|
|
substsList = []
|
|
|
|
i = 0
|
|
|
|
while rank:
|
|
|
|
if rank & 1:
|
|
|
|
substsList.append(conditionalSubstitutions[i][1])
|
|
|
|
rank >>= 1
|
|
|
|
i += 1
|
|
|
|
items.append((dict(box), substsList))
|
|
|
|
return items
|
2018-04-16 10:21:19 +02:00
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# Terminology:
|
|
|
|
#
|
[varLib.featureVars] Rewrite algorithm
Whereas previous algorithm had exponential running time and return
value size, new one has quadratic.
For featureVars_test.py test case, for example, which is a pathological
test case of n sliding intervals, the number of output intervals of
various algorithms are:
- Previous algorithm: 2**n - 1
- New algorithm: n*(n-1)/2
- Optimal algorithm: 2*n - 1
Ie, we go from exponential to quadratic, whereas in this case the optimal
solution is linear.
Running time of said test, for n=20, goes from over 20s, to 0.06s.
The algorithm can be improved. The overlayBox() function currently does
not try to shrink the remainder box. Doing that will probably bring us
to optimal solution for this test case.
Fixes https://github.com/fonttools/fonttools/pull/1372
One test is failing. Needs to be investigated that new output is correct,
and test expectations updated.
2018-11-09 14:15:32 -05:00
|
|
|
# A 'Box' is a dict representing an orthogonal "rectangular" bit of N-dimensional space.
|
2018-04-16 10:21:19 +02:00
|
|
|
# The keys in the dict are axis tags, the values are (minValue, maxValue) tuples.
|
|
|
|
# Missing dimensions (keys) are substituted by the default min and max values
|
|
|
|
# from the corresponding axes.
|
|
|
|
#
|
|
|
|
|
2022-12-13 11:26:36 +00:00
|
|
|
|
[varLib.featureVars] Rewrite algorithm
Whereas previous algorithm had exponential running time and return
value size, new one has quadratic.
For featureVars_test.py test case, for example, which is a pathological
test case of n sliding intervals, the number of output intervals of
various algorithms are:
- Previous algorithm: 2**n - 1
- New algorithm: n*(n-1)/2
- Optimal algorithm: 2*n - 1
Ie, we go from exponential to quadratic, whereas in this case the optimal
solution is linear.
Running time of said test, for n=20, goes from over 20s, to 0.06s.
The algorithm can be improved. The overlayBox() function currently does
not try to shrink the remainder box. Doing that will probably bring us
to optimal solution for this test case.
Fixes https://github.com/fonttools/fonttools/pull/1372
One test is failing. Needs to be investigated that new output is correct,
and test expectations updated.
2018-11-09 14:15:32 -05:00
|
|
|
def overlayBox(top, bot):
|
2021-12-02 15:31:49 +00:00
|
|
|
"""Overlays ``top`` box on top of ``bot`` box.
|
2018-04-16 10:21:19 +02:00
|
|
|
|
[varLib.featureVars] Rewrite algorithm
Whereas previous algorithm had exponential running time and return
value size, new one has quadratic.
For featureVars_test.py test case, for example, which is a pathological
test case of n sliding intervals, the number of output intervals of
various algorithms are:
- Previous algorithm: 2**n - 1
- New algorithm: n*(n-1)/2
- Optimal algorithm: 2*n - 1
Ie, we go from exponential to quadratic, whereas in this case the optimal
solution is linear.
Running time of said test, for n=20, goes from over 20s, to 0.06s.
The algorithm can be improved. The overlayBox() function currently does
not try to shrink the remainder box. Doing that will probably bring us
to optimal solution for this test case.
Fixes https://github.com/fonttools/fonttools/pull/1372
One test is failing. Needs to be investigated that new output is correct,
and test expectations updated.
2018-11-09 14:15:32 -05:00
|
|
|
Returns two items:
|
2021-12-02 15:31:49 +00:00
|
|
|
|
|
|
|
* Box for intersection of ``top`` and ``bot``, or None if they don't intersect.
|
|
|
|
* Box for remainder of ``bot``. Remainder box might not be exact (since the
|
[varLib.featureVars] Rewrite algorithm
Whereas previous algorithm had exponential running time and return
value size, new one has quadratic.
For featureVars_test.py test case, for example, which is a pathological
test case of n sliding intervals, the number of output intervals of
various algorithms are:
- Previous algorithm: 2**n - 1
- New algorithm: n*(n-1)/2
- Optimal algorithm: 2*n - 1
Ie, we go from exponential to quadratic, whereas in this case the optimal
solution is linear.
Running time of said test, for n=20, goes from over 20s, to 0.06s.
The algorithm can be improved. The overlayBox() function currently does
not try to shrink the remainder box. Doing that will probably bring us
to optimal solution for this test case.
Fixes https://github.com/fonttools/fonttools/pull/1372
One test is failing. Needs to be investigated that new output is correct,
and test expectations updated.
2018-11-09 14:15:32 -05:00
|
|
|
remainder might not be a simple box), but is inclusive of the exact
|
|
|
|
remainder.
|
2018-04-16 10:21:19 +02:00
|
|
|
"""
|
|
|
|
|
[varLib.featureVars] Rewrite algorithm
Whereas previous algorithm had exponential running time and return
value size, new one has quadratic.
For featureVars_test.py test case, for example, which is a pathological
test case of n sliding intervals, the number of output intervals of
various algorithms are:
- Previous algorithm: 2**n - 1
- New algorithm: n*(n-1)/2
- Optimal algorithm: 2*n - 1
Ie, we go from exponential to quadratic, whereas in this case the optimal
solution is linear.
Running time of said test, for n=20, goes from over 20s, to 0.06s.
The algorithm can be improved. The overlayBox() function currently does
not try to shrink the remainder box. Doing that will probably bring us
to optimal solution for this test case.
Fixes https://github.com/fonttools/fonttools/pull/1372
One test is failing. Needs to be investigated that new output is correct,
and test expectations updated.
2018-11-09 14:15:32 -05:00
|
|
|
# Intersection
|
|
|
|
intersection = {}
|
|
|
|
intersection.update(top)
|
|
|
|
intersection.update(bot)
|
|
|
|
for axisTag in set(top) & set(bot):
|
|
|
|
min1, max1 = top[axisTag]
|
|
|
|
min2, max2 = bot[axisTag]
|
2018-04-16 10:21:19 +02:00
|
|
|
minimum = max(min1, min2)
|
|
|
|
maximum = min(max1, max2)
|
|
|
|
if not minimum < maximum:
|
[varLib.featureVars] Rewrite algorithm
Whereas previous algorithm had exponential running time and return
value size, new one has quadratic.
For featureVars_test.py test case, for example, which is a pathological
test case of n sliding intervals, the number of output intervals of
various algorithms are:
- Previous algorithm: 2**n - 1
- New algorithm: n*(n-1)/2
- Optimal algorithm: 2*n - 1
Ie, we go from exponential to quadratic, whereas in this case the optimal
solution is linear.
Running time of said test, for n=20, goes from over 20s, to 0.06s.
The algorithm can be improved. The overlayBox() function currently does
not try to shrink the remainder box. Doing that will probably bring us
to optimal solution for this test case.
Fixes https://github.com/fonttools/fonttools/pull/1372
One test is failing. Needs to be investigated that new output is correct,
and test expectations updated.
2018-11-09 14:15:32 -05:00
|
|
|
return None, bot # Do not intersect
|
2018-11-09 14:54:34 -05:00
|
|
|
intersection[axisTag] = minimum, maximum
|
[varLib.featureVars] Rewrite algorithm
Whereas previous algorithm had exponential running time and return
value size, new one has quadratic.
For featureVars_test.py test case, for example, which is a pathological
test case of n sliding intervals, the number of output intervals of
various algorithms are:
- Previous algorithm: 2**n - 1
- New algorithm: n*(n-1)/2
- Optimal algorithm: 2*n - 1
Ie, we go from exponential to quadratic, whereas in this case the optimal
solution is linear.
Running time of said test, for n=20, goes from over 20s, to 0.06s.
The algorithm can be improved. The overlayBox() function currently does
not try to shrink the remainder box. Doing that will probably bring us
to optimal solution for this test case.
Fixes https://github.com/fonttools/fonttools/pull/1372
One test is failing. Needs to be investigated that new output is correct,
and test expectations updated.
2018-11-09 14:15:32 -05:00
|
|
|
|
|
|
|
# Remainder
|
2018-11-09 14:54:34 -05:00
|
|
|
#
|
|
|
|
# Remainder is empty if bot's each axis range lies within that of intersection.
|
|
|
|
#
|
|
|
|
# Remainder is shrank if bot's each, except for exactly one, axis range lies
|
2023-02-23 19:01:12 -07:00
|
|
|
# within that of intersection, and that one axis, it extrudes out of the
|
2018-12-05 10:33:14 -08:00
|
|
|
# intersection only on one side.
|
2018-11-09 14:54:34 -05:00
|
|
|
#
|
|
|
|
# Bot is returned in full as remainder otherwise, as true remainder is not
|
|
|
|
# representable as a single box.
|
|
|
|
|
|
|
|
remainder = dict(bot)
|
2023-02-23 19:01:12 -07:00
|
|
|
extruding = False
|
|
|
|
fullyInside = True
|
|
|
|
for axisTag in top:
|
|
|
|
if axisTag in bot:
|
|
|
|
continue
|
|
|
|
extruding = True
|
|
|
|
fullyInside = False
|
|
|
|
break
|
2018-11-09 14:54:34 -05:00
|
|
|
for axisTag in bot:
|
2023-02-23 19:01:12 -07:00
|
|
|
if axisTag not in top:
|
2018-12-05 10:33:14 -08:00
|
|
|
continue # Axis range lies fully within
|
2018-11-09 14:54:34 -05:00
|
|
|
min1, max1 = intersection[axisTag]
|
|
|
|
min2, max2 = bot[axisTag]
|
|
|
|
if min1 <= min2 and max2 <= max1:
|
2018-12-05 10:33:14 -08:00
|
|
|
continue # Axis range lies fully within
|
2018-11-09 14:54:34 -05:00
|
|
|
|
|
|
|
# Bot's range doesn't fully lie within that of top's for this axis.
|
|
|
|
# We know they intersect, so it cannot lie fully without either; so they
|
|
|
|
# overlap.
|
|
|
|
|
|
|
|
# If we have had an overlapping axis before, remainder is not
|
|
|
|
# representable as a box, so return full bottom and go home.
|
2023-02-23 19:01:12 -07:00
|
|
|
if extruding:
|
2018-11-09 14:54:34 -05:00
|
|
|
return intersection, bot
|
2023-02-23 19:01:12 -07:00
|
|
|
extruding = True
|
2018-11-09 16:09:34 -05:00
|
|
|
fullyInside = False
|
2018-11-09 14:54:34 -05:00
|
|
|
|
|
|
|
# Otherwise, cut remainder on this axis and continue.
|
2018-12-05 10:33:14 -08:00
|
|
|
if min1 <= min2:
|
2018-11-09 14:54:34 -05:00
|
|
|
# Right side survives.
|
|
|
|
minimum = max(max1, min2)
|
|
|
|
maximum = max2
|
2018-12-05 10:33:14 -08:00
|
|
|
elif max2 <= max1:
|
2018-11-09 14:54:34 -05:00
|
|
|
# Left side survives.
|
|
|
|
minimum = min2
|
|
|
|
maximum = min(min1, max2)
|
|
|
|
else:
|
|
|
|
# Remainder leaks out from both sides. Can't cut either.
|
|
|
|
return intersection, bot
|
|
|
|
|
|
|
|
remainder[axisTag] = minimum, maximum
|
2018-04-16 10:21:19 +02:00
|
|
|
|
2018-11-09 16:09:34 -05:00
|
|
|
if fullyInside:
|
|
|
|
# bot is fully within intersection. Remainder is empty.
|
|
|
|
return intersection, None
|
2018-04-16 10:21:19 +02:00
|
|
|
|
[varLib.featureVars] Rewrite algorithm
Whereas previous algorithm had exponential running time and return
value size, new one has quadratic.
For featureVars_test.py test case, for example, which is a pathological
test case of n sliding intervals, the number of output intervals of
various algorithms are:
- Previous algorithm: 2**n - 1
- New algorithm: n*(n-1)/2
- Optimal algorithm: 2*n - 1
Ie, we go from exponential to quadratic, whereas in this case the optimal
solution is linear.
Running time of said test, for n=20, goes from over 20s, to 0.06s.
The algorithm can be improved. The overlayBox() function currently does
not try to shrink the remainder box. Doing that will probably bring us
to optimal solution for this test case.
Fixes https://github.com/fonttools/fonttools/pull/1372
One test is failing. Needs to be investigated that new output is correct,
and test expectations updated.
2018-11-09 14:15:32 -05:00
|
|
|
return intersection, remainder
|
2018-04-16 10:21:19 +02:00
|
|
|
|
2022-12-13 11:26:36 +00:00
|
|
|
|
[varLib.featureVars] Rewrite algorithm
Whereas previous algorithm had exponential running time and return
value size, new one has quadratic.
For featureVars_test.py test case, for example, which is a pathological
test case of n sliding intervals, the number of output intervals of
various algorithms are:
- Previous algorithm: 2**n - 1
- New algorithm: n*(n-1)/2
- Optimal algorithm: 2*n - 1
Ie, we go from exponential to quadratic, whereas in this case the optimal
solution is linear.
Running time of said test, for n=20, goes from over 20s, to 0.06s.
The algorithm can be improved. The overlayBox() function currently does
not try to shrink the remainder box. Doing that will probably bring us
to optimal solution for this test case.
Fixes https://github.com/fonttools/fonttools/pull/1372
One test is failing. Needs to be investigated that new output is correct,
and test expectations updated.
2018-11-09 14:15:32 -05:00
|
|
|
def cleanupBox(box):
|
|
|
|
"""Return a sparse copy of `box`, without redundant (default) values.
|
2018-04-16 10:21:19 +02:00
|
|
|
|
[varLib.featureVars] Rewrite algorithm
Whereas previous algorithm had exponential running time and return
value size, new one has quadratic.
For featureVars_test.py test case, for example, which is a pathological
test case of n sliding intervals, the number of output intervals of
various algorithms are:
- Previous algorithm: 2**n - 1
- New algorithm: n*(n-1)/2
- Optimal algorithm: 2*n - 1
Ie, we go from exponential to quadratic, whereas in this case the optimal
solution is linear.
Running time of said test, for n=20, goes from over 20s, to 0.06s.
The algorithm can be improved. The overlayBox() function currently does
not try to shrink the remainder box. Doing that will probably bring us
to optimal solution for this test case.
Fixes https://github.com/fonttools/fonttools/pull/1372
One test is failing. Needs to be investigated that new output is correct,
and test expectations updated.
2018-11-09 14:15:32 -05:00
|
|
|
>>> cleanupBox({})
|
2018-04-16 10:21:19 +02:00
|
|
|
{}
|
[varLib.featureVars] Rewrite algorithm
Whereas previous algorithm had exponential running time and return
value size, new one has quadratic.
For featureVars_test.py test case, for example, which is a pathological
test case of n sliding intervals, the number of output intervals of
various algorithms are:
- Previous algorithm: 2**n - 1
- New algorithm: n*(n-1)/2
- Optimal algorithm: 2*n - 1
Ie, we go from exponential to quadratic, whereas in this case the optimal
solution is linear.
Running time of said test, for n=20, goes from over 20s, to 0.06s.
The algorithm can be improved. The overlayBox() function currently does
not try to shrink the remainder box. Doing that will probably bring us
to optimal solution for this test case.
Fixes https://github.com/fonttools/fonttools/pull/1372
One test is failing. Needs to be investigated that new output is correct,
and test expectations updated.
2018-11-09 14:15:32 -05:00
|
|
|
>>> cleanupBox({'wdth': (0.0, 1.0)})
|
2018-04-16 10:21:19 +02:00
|
|
|
{'wdth': (0.0, 1.0)}
|
[varLib.featureVars] Rewrite algorithm
Whereas previous algorithm had exponential running time and return
value size, new one has quadratic.
For featureVars_test.py test case, for example, which is a pathological
test case of n sliding intervals, the number of output intervals of
various algorithms are:
- Previous algorithm: 2**n - 1
- New algorithm: n*(n-1)/2
- Optimal algorithm: 2*n - 1
Ie, we go from exponential to quadratic, whereas in this case the optimal
solution is linear.
Running time of said test, for n=20, goes from over 20s, to 0.06s.
The algorithm can be improved. The overlayBox() function currently does
not try to shrink the remainder box. Doing that will probably bring us
to optimal solution for this test case.
Fixes https://github.com/fonttools/fonttools/pull/1372
One test is failing. Needs to be investigated that new output is correct,
and test expectations updated.
2018-11-09 14:15:32 -05:00
|
|
|
>>> cleanupBox({'wdth': (-1.0, 1.0)})
|
2018-04-16 10:21:19 +02:00
|
|
|
{}
|
|
|
|
|
|
|
|
"""
|
[varLib.featureVars] Rewrite algorithm
Whereas previous algorithm had exponential running time and return
value size, new one has quadratic.
For featureVars_test.py test case, for example, which is a pathological
test case of n sliding intervals, the number of output intervals of
various algorithms are:
- Previous algorithm: 2**n - 1
- New algorithm: n*(n-1)/2
- Optimal algorithm: 2*n - 1
Ie, we go from exponential to quadratic, whereas in this case the optimal
solution is linear.
Running time of said test, for n=20, goes from over 20s, to 0.06s.
The algorithm can be improved. The overlayBox() function currently does
not try to shrink the remainder box. Doing that will probably bring us
to optimal solution for this test case.
Fixes https://github.com/fonttools/fonttools/pull/1372
One test is failing. Needs to be investigated that new output is correct,
and test expectations updated.
2018-11-09 14:15:32 -05:00
|
|
|
return {tag: limit for tag, limit in box.items() if limit != (-1.0, 1.0)}
|
2018-04-16 10:21:19 +02:00
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# Low level implementation
|
|
|
|
#
|
|
|
|
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2023-05-02 10:53:43 -06:00
|
|
|
def addFeatureVariationsRaw(font, table, conditionalSubstitutions, featureTag="rvrn"):
|
2018-04-16 10:21:19 +02:00
|
|
|
"""Low level implementation of addFeatureVariations that directly
|
|
|
|
models the possibilities of the FeatureVariations table."""
|
|
|
|
|
2023-05-02 10:53:43 -06:00
|
|
|
processLast = featureTag != "rvrn"
|
2023-05-01 11:36:39 -06:00
|
|
|
|
2018-04-16 10:21:19 +02:00
|
|
|
#
|
2019-10-17 20:01:05 +02:00
|
|
|
# if there is no <featureTag> feature:
|
|
|
|
# make empty <featureTag> feature
|
|
|
|
# sort features, get <featureTag> feature index
|
|
|
|
# add <featureTag> feature to all scripts
|
2018-04-16 10:21:19 +02:00
|
|
|
# make lookups
|
|
|
|
# add feature variations
|
|
|
|
#
|
2021-10-28 11:58:54 +01:00
|
|
|
if table.Version < 0x00010001:
|
|
|
|
table.Version = 0x00010001 # allow table.FeatureVariations
|
2018-04-16 10:21:19 +02:00
|
|
|
|
2021-10-28 11:58:54 +01:00
|
|
|
table.FeatureVariations = None # delete any existing FeatureVariations
|
2018-04-16 10:21:19 +02:00
|
|
|
|
2019-10-17 20:46:59 +02:00
|
|
|
varFeatureIndices = []
|
2021-10-28 11:58:54 +01:00
|
|
|
for index, feature in enumerate(table.FeatureList.FeatureRecord):
|
2019-10-17 20:01:05 +02:00
|
|
|
if feature.FeatureTag == featureTag:
|
2019-10-17 20:46:59 +02:00
|
|
|
varFeatureIndices.append(index)
|
2018-04-16 10:21:19 +02:00
|
|
|
|
2019-10-17 20:46:59 +02:00
|
|
|
if not varFeatureIndices:
|
2019-10-17 20:01:05 +02:00
|
|
|
varFeature = buildFeatureRecord(featureTag, [])
|
2021-10-28 11:58:54 +01:00
|
|
|
table.FeatureList.FeatureRecord.append(varFeature)
|
|
|
|
table.FeatureList.FeatureCount = len(table.FeatureList.FeatureRecord)
|
2018-04-16 10:21:19 +02:00
|
|
|
|
2021-10-28 11:58:54 +01:00
|
|
|
sortFeatureList(table)
|
|
|
|
varFeatureIndex = table.FeatureList.FeatureRecord.index(varFeature)
|
2018-04-16 10:21:19 +02:00
|
|
|
|
2021-10-28 11:58:54 +01:00
|
|
|
for scriptRecord in table.ScriptList.ScriptRecord:
|
2021-01-19 16:42:40 +00:00
|
|
|
if scriptRecord.Script.DefaultLangSys is None:
|
|
|
|
raise VarLibError(
|
|
|
|
"Feature variations require that the script "
|
|
|
|
f"'{scriptRecord.ScriptTag}' defines a default language system."
|
|
|
|
)
|
2019-10-17 20:01:05 +02:00
|
|
|
langSystems = [lsr.LangSys for lsr in scriptRecord.Script.LangSysRecord]
|
|
|
|
for langSys in [scriptRecord.Script.DefaultLangSys] + langSystems:
|
|
|
|
langSys.FeatureIndex.append(varFeatureIndex)
|
2022-10-20 16:45:44 +01:00
|
|
|
langSys.FeatureCount = len(langSys.FeatureIndex)
|
2018-04-16 10:21:19 +02:00
|
|
|
|
2019-10-17 20:46:59 +02:00
|
|
|
varFeatureIndices = [varFeatureIndex]
|
|
|
|
|
2018-04-16 10:21:19 +02:00
|
|
|
axisIndices = {
|
|
|
|
axis.axisTag: axisIndex for axisIndex, axis in enumerate(font["fvar"].axes)
|
|
|
|
}
|
|
|
|
|
|
|
|
featureVariationRecords = []
|
2021-10-28 11:58:54 +01:00
|
|
|
for conditionSet, lookupIndices in conditionalSubstitutions:
|
2018-04-16 10:21:19 +02:00
|
|
|
conditionTable = []
|
|
|
|
for axisTag, (minValue, maxValue) in sorted(conditionSet.items()):
|
2020-02-11 13:54:08 +00:00
|
|
|
if minValue > maxValue:
|
|
|
|
raise VarLibValidationError(
|
|
|
|
"A condition set has a minimum value above the maximum value."
|
|
|
|
)
|
2018-04-16 10:21:19 +02:00
|
|
|
ct = buildConditionTable(axisIndices[axisTag], minValue, maxValue)
|
|
|
|
conditionTable.append(ct)
|
2019-10-17 20:46:59 +02:00
|
|
|
records = []
|
|
|
|
for varFeatureIndex in varFeatureIndices:
|
2021-10-28 11:58:54 +01:00
|
|
|
existingLookupIndices = table.FeatureList.FeatureRecord[
|
|
|
|
varFeatureIndex
|
|
|
|
].Feature.LookupListIndex
|
2023-05-01 11:36:39 -06:00
|
|
|
combinedLookupIndices = (
|
|
|
|
existingLookupIndices + lookupIndices
|
|
|
|
if processLast
|
|
|
|
else lookupIndices + existingLookupIndices
|
|
|
|
)
|
|
|
|
|
2022-10-28 13:20:32 -06:00
|
|
|
records.append(
|
|
|
|
buildFeatureTableSubstitutionRecord(
|
2023-05-01 11:36:39 -06:00
|
|
|
varFeatureIndex, combinedLookupIndices
|
2022-10-28 13:20:32 -06:00
|
|
|
)
|
2022-12-13 11:26:36 +00:00
|
|
|
)
|
2019-10-17 20:46:59 +02:00
|
|
|
featureVariationRecords.append(
|
|
|
|
buildFeatureVariationRecord(conditionTable, records)
|
2022-12-13 11:26:36 +00:00
|
|
|
)
|
2018-04-16 10:21:19 +02:00
|
|
|
|
2021-10-28 11:58:54 +01:00
|
|
|
table.FeatureVariations = buildFeatureVariations(featureVariationRecords)
|
2018-04-16 10:21:19 +02:00
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# Building GSUB/FeatureVariations internals
|
|
|
|
#
|
|
|
|
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2018-04-16 10:21:19 +02:00
|
|
|
def buildGSUB():
|
|
|
|
"""Build a GSUB table from scratch."""
|
|
|
|
fontTable = newTable("GSUB")
|
|
|
|
gsub = fontTable.table = ot.GSUB()
|
|
|
|
gsub.Version = 0x00010001 # allow gsub.FeatureVariations
|
|
|
|
|
|
|
|
gsub.ScriptList = ot.ScriptList()
|
|
|
|
gsub.ScriptList.ScriptRecord = []
|
|
|
|
gsub.FeatureList = ot.FeatureList()
|
|
|
|
gsub.FeatureList.FeatureRecord = []
|
|
|
|
gsub.LookupList = ot.LookupList()
|
|
|
|
gsub.LookupList.Lookup = []
|
|
|
|
|
|
|
|
srec = ot.ScriptRecord()
|
|
|
|
srec.ScriptTag = "DFLT"
|
|
|
|
srec.Script = ot.Script()
|
|
|
|
srec.Script.DefaultLangSys = None
|
|
|
|
srec.Script.LangSysRecord = []
|
2022-10-20 16:45:44 +01:00
|
|
|
srec.Script.LangSysCount = 0
|
2018-04-16 10:21:19 +02:00
|
|
|
|
|
|
|
langrec = ot.LangSysRecord()
|
|
|
|
langrec.LangSys = ot.LangSys()
|
|
|
|
langrec.LangSys.ReqFeatureIndex = 0xFFFF
|
2019-05-29 13:40:57 +01:00
|
|
|
langrec.LangSys.FeatureIndex = []
|
2018-04-16 10:21:19 +02:00
|
|
|
srec.Script.DefaultLangSys = langrec.LangSys
|
|
|
|
|
|
|
|
gsub.ScriptList.ScriptRecord.append(srec)
|
2019-05-30 12:29:23 +01:00
|
|
|
gsub.ScriptList.ScriptCount = 1
|
2018-04-16 10:21:19 +02:00
|
|
|
gsub.FeatureVariations = None
|
|
|
|
|
|
|
|
return fontTable
|
|
|
|
|
|
|
|
|
|
|
|
def makeSubstitutionsHashable(conditionalSubstitutions):
|
|
|
|
"""Turn all the substitution dictionaries in sorted tuples of tuples so
|
|
|
|
they are hashable, to detect duplicates so we don't write out redundant
|
|
|
|
data."""
|
|
|
|
allSubstitutions = set()
|
|
|
|
condSubst = []
|
|
|
|
for conditionSet, substitutionMaps in conditionalSubstitutions:
|
|
|
|
substitutions = []
|
|
|
|
for substitutionMap in substitutionMaps:
|
|
|
|
subst = tuple(sorted(substitutionMap.items()))
|
|
|
|
substitutions.append(subst)
|
|
|
|
allSubstitutions.add(subst)
|
|
|
|
condSubst.append((conditionSet, substitutions))
|
|
|
|
return condSubst, sorted(allSubstitutions)
|
|
|
|
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2022-10-28 13:20:32 -06:00
|
|
|
class ShifterVisitor(TTVisitor):
|
|
|
|
def __init__(self, shift):
|
|
|
|
self.shift = shift
|
|
|
|
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2022-10-28 13:20:32 -06:00
|
|
|
@ShifterVisitor.register_attr(ot.Feature, "LookupListIndex") # GSUB/GPOS
|
|
|
|
def visit(visitor, obj, attr, value):
|
|
|
|
shift = visitor.shift
|
|
|
|
value = [l + shift for l in value]
|
|
|
|
setattr(obj, attr, value)
|
|
|
|
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2022-10-28 13:20:32 -06:00
|
|
|
@ShifterVisitor.register_attr(
|
|
|
|
(ot.SubstLookupRecord, ot.PosLookupRecord), "LookupListIndex"
|
|
|
|
)
|
|
|
|
def visit(visitor, obj, attr, value):
|
|
|
|
setattr(obj, attr, visitor.shift + value)
|
2018-04-16 10:21:19 +02:00
|
|
|
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2023-05-01 11:36:39 -06:00
|
|
|
def buildSubstitutionLookups(gsub, allSubstitutions, processLast=False):
|
2018-04-16 10:21:19 +02:00
|
|
|
"""Build the lookups for the glyph substitutions, return a dict mapping
|
|
|
|
the substitution to lookup indices."""
|
2022-10-28 13:20:32 -06:00
|
|
|
|
|
|
|
# Insert lookups at the beginning of the lookup vector
|
|
|
|
# https://github.com/googlefonts/fontmake/issues/950
|
|
|
|
|
2023-05-01 11:36:39 -06:00
|
|
|
firstIndex = len(gsub.LookupList.Lookup) if processLast else 0
|
2018-04-16 10:21:19 +02:00
|
|
|
lookupMap = {}
|
|
|
|
for i, substitutionMap in enumerate(allSubstitutions):
|
2023-05-01 11:36:39 -06:00
|
|
|
lookupMap[substitutionMap] = firstIndex + i
|
2022-10-28 13:20:32 -06:00
|
|
|
|
2023-05-01 11:36:39 -06:00
|
|
|
if not processLast:
|
|
|
|
# Shift all lookup indices in gsub by len(allSubstitutions)
|
|
|
|
shift = len(allSubstitutions)
|
|
|
|
visitor = ShifterVisitor(shift)
|
|
|
|
visitor.visit(gsub.FeatureList.FeatureRecord)
|
|
|
|
visitor.visit(gsub.LookupList.Lookup)
|
2018-04-16 10:21:19 +02:00
|
|
|
|
2022-10-28 13:20:32 -06:00
|
|
|
for i, subst in enumerate(allSubstitutions):
|
2018-04-16 10:21:19 +02:00
|
|
|
substMap = dict(subst)
|
|
|
|
lookup = buildLookup([buildSingleSubstSubtable(substMap)])
|
2023-05-01 11:36:39 -06:00
|
|
|
if processLast:
|
|
|
|
gsub.LookupList.Lookup.append(lookup)
|
|
|
|
else:
|
|
|
|
gsub.LookupList.Lookup.insert(i, lookup)
|
2018-04-16 10:21:19 +02:00
|
|
|
assert gsub.LookupList.Lookup[lookupMap[subst]] is lookup
|
2019-05-30 12:29:23 +01:00
|
|
|
gsub.LookupList.LookupCount = len(gsub.LookupList.Lookup)
|
2018-04-16 10:21:19 +02:00
|
|
|
return lookupMap
|
|
|
|
|
|
|
|
|
|
|
|
def buildFeatureVariations(featureVariationRecords):
|
|
|
|
"""Build the FeatureVariations subtable."""
|
|
|
|
fv = ot.FeatureVariations()
|
|
|
|
fv.Version = 0x00010000
|
|
|
|
fv.FeatureVariationRecord = featureVariationRecords
|
2021-10-28 11:58:54 +01:00
|
|
|
fv.FeatureVariationCount = len(featureVariationRecords)
|
2018-04-16 10:21:19 +02:00
|
|
|
return fv
|
|
|
|
|
2018-04-18 12:07:06 +01:00
|
|
|
|
2018-04-16 10:21:19 +02:00
|
|
|
def buildFeatureRecord(featureTag, lookupListIndices):
|
|
|
|
"""Build a FeatureRecord."""
|
|
|
|
fr = ot.FeatureRecord()
|
|
|
|
fr.FeatureTag = featureTag
|
|
|
|
fr.Feature = ot.Feature()
|
|
|
|
fr.Feature.LookupListIndex = lookupListIndices
|
2019-05-30 12:29:23 +01:00
|
|
|
fr.Feature.populateDefaults()
|
2018-04-16 10:21:19 +02:00
|
|
|
return fr
|
|
|
|
|
|
|
|
|
|
|
|
def buildFeatureVariationRecord(conditionTable, substitutionRecords):
|
|
|
|
"""Build a FeatureVariationRecord."""
|
|
|
|
fvr = ot.FeatureVariationRecord()
|
|
|
|
fvr.ConditionSet = ot.ConditionSet()
|
|
|
|
fvr.ConditionSet.ConditionTable = conditionTable
|
2021-10-28 11:58:54 +01:00
|
|
|
fvr.ConditionSet.ConditionCount = len(conditionTable)
|
2018-04-16 10:21:19 +02:00
|
|
|
fvr.FeatureTableSubstitution = ot.FeatureTableSubstitution()
|
2018-11-10 08:55:30 +01:00
|
|
|
fvr.FeatureTableSubstitution.Version = 0x00010000
|
2018-04-16 10:21:19 +02:00
|
|
|
fvr.FeatureTableSubstitution.SubstitutionRecord = substitutionRecords
|
2021-10-28 11:58:54 +01:00
|
|
|
fvr.FeatureTableSubstitution.SubstitutionCount = len(substitutionRecords)
|
2018-04-16 10:21:19 +02:00
|
|
|
return fvr
|
|
|
|
|
|
|
|
|
|
|
|
def buildFeatureTableSubstitutionRecord(featureIndex, lookupListIndices):
|
|
|
|
"""Build a FeatureTableSubstitutionRecord."""
|
|
|
|
ftsr = ot.FeatureTableSubstitutionRecord()
|
|
|
|
ftsr.FeatureIndex = featureIndex
|
|
|
|
ftsr.Feature = ot.Feature()
|
|
|
|
ftsr.Feature.LookupListIndex = lookupListIndices
|
2021-10-28 11:58:54 +01:00
|
|
|
ftsr.Feature.LookupCount = len(lookupListIndices)
|
2018-04-16 10:21:19 +02:00
|
|
|
return ftsr
|
|
|
|
|
|
|
|
|
|
|
|
def buildConditionTable(axisIndex, filterRangeMinValue, filterRangeMaxValue):
|
|
|
|
"""Build a ConditionTable."""
|
|
|
|
ct = ot.ConditionTable()
|
|
|
|
ct.Format = 1
|
|
|
|
ct.AxisIndex = axisIndex
|
|
|
|
ct.FilterRangeMinValue = filterRangeMinValue
|
|
|
|
ct.FilterRangeMaxValue = filterRangeMaxValue
|
|
|
|
return ct
|
|
|
|
|
|
|
|
|
|
|
|
def sortFeatureList(table):
|
|
|
|
"""Sort the feature list by feature tag, and remap the feature indices
|
|
|
|
elsewhere. This is needed after the feature list has been modified.
|
|
|
|
"""
|
|
|
|
# decorate, sort, undecorate, because we need to make an index remapping table
|
|
|
|
tagIndexFea = [
|
|
|
|
(fea.FeatureTag, index, fea)
|
|
|
|
for index, fea in enumerate(table.FeatureList.FeatureRecord)
|
|
|
|
]
|
|
|
|
tagIndexFea.sort()
|
|
|
|
table.FeatureList.FeatureRecord = [fea for tag, index, fea in tagIndexFea]
|
|
|
|
featureRemap = dict(
|
|
|
|
zip([index for tag, index, fea in tagIndexFea], range(len(tagIndexFea)))
|
2022-12-13 11:26:36 +00:00
|
|
|
)
|
2018-04-16 10:21:19 +02:00
|
|
|
|
|
|
|
# Remap the feature indices
|
|
|
|
remapFeatures(table, featureRemap)
|
|
|
|
|
|
|
|
|
|
|
|
def remapFeatures(table, featureRemap):
|
|
|
|
"""Go through the scripts list, and remap feature indices."""
|
|
|
|
for scriptIndex, script in enumerate(table.ScriptList.ScriptRecord):
|
|
|
|
defaultLangSys = script.Script.DefaultLangSys
|
|
|
|
if defaultLangSys is not None:
|
|
|
|
_remapLangSys(defaultLangSys, featureRemap)
|
|
|
|
for langSysRecordIndex, langSysRec in enumerate(script.Script.LangSysRecord):
|
|
|
|
langSys = langSysRec.LangSys
|
|
|
|
_remapLangSys(langSys, featureRemap)
|
|
|
|
|
|
|
|
if hasattr(table, "FeatureVariations") and table.FeatureVariations is not None:
|
|
|
|
for fvr in table.FeatureVariations.FeatureVariationRecord:
|
|
|
|
for ftsr in fvr.FeatureTableSubstitution.SubstitutionRecord:
|
|
|
|
ftsr.FeatureIndex = featureRemap[ftsr.FeatureIndex]
|
|
|
|
|
|
|
|
|
|
|
|
def _remapLangSys(langSys, featureRemap):
|
|
|
|
if langSys.ReqFeatureIndex != 0xFFFF:
|
|
|
|
langSys.ReqFeatureIndex = featureRemap[langSys.ReqFeatureIndex]
|
|
|
|
langSys.FeatureIndex = [featureRemap[index] for index in langSys.FeatureIndex]
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2018-11-09 11:03:19 -05:00
|
|
|
import doctest, sys
|
2022-12-13 11:26:36 +00:00
|
|
|
|
2018-11-09 11:03:19 -05:00
|
|
|
sys.exit(doctest.testmod().failed)
|