Skip to content

Commit

Permalink
[varc] Add ConditionSets
Browse files Browse the repository at this point in the history
  • Loading branch information
behdad committed Mar 20, 2024
1 parent fe60644 commit 5f9add9
Show file tree
Hide file tree
Showing 9 changed files with 287 additions and 291 deletions.
16 changes: 16 additions & 0 deletions Lib/fontTools/subset/__init__.py
Expand Up @@ -2693,6 +2693,22 @@ def prune_post_subset(self, font, options):
if comp.axisIndicesIndex is not None:
comp.axisIndicesIndex = mapping[comp.axisIndicesIndex]

conditionSetList = table.ConditionSetList
if conditionSetList is not None:
conditionSets = conditionSetList.ConditionSet
usedIndices = set()

Check warning on line 2699 in Lib/fontTools/subset/__init__.py

View check run for this annotation

Codecov / codecov/patch

Lib/fontTools/subset/__init__.py#L2698-L2699

Added lines #L2698 - L2699 were not covered by tests
for glyph in table.VarCompositeGlyphs.VarCompositeGlyph:
for comp in glyph.components:
if comp.conditionSetIndex is not None:
usedIndices.add(comp.conditionSetIndex)
usedIndices = sorted(usedIndices)
conditionSetList.ConditionSet = _list_subset(conditionSets, usedIndices)

Check warning on line 2705 in Lib/fontTools/subset/__init__.py

View check run for this annotation

Codecov / codecov/patch

Lib/fontTools/subset/__init__.py#L2703-L2705

Added lines #L2703 - L2705 were not covered by tests
mapping = {old: new for new, old in enumerate(usedIndices)}
for glyph in table.VarCompositeGlyphs.VarCompositeGlyph:
for comp in glyph.components:
if comp.conditionSetIndex is not None:
comp.conditionSetIndex = mapping[comp.conditionSetIndex]

Check warning on line 2710 in Lib/fontTools/subset/__init__.py

View check run for this annotation

Codecov / codecov/patch

Lib/fontTools/subset/__init__.py#L2710

Added line #L2710 was not covered by tests

return True


Expand Down
20 changes: 20 additions & 0 deletions Lib/fontTools/ttLib/tables/otData.py
Expand Up @@ -3168,6 +3168,25 @@
),
],
),
(
"ConditionSetList",
[
(
"uint32",
"ConditionSetCount",
None,
None,
"Number of condition-set tables in the ConditionSet array",
),
(
"LOffset",
"ConditionSet",
"ConditionSetCount",
0,
"Array of condition-set tables.",
),
],
),
(
"ConditionSet",
[
Expand Down Expand Up @@ -3377,6 +3396,7 @@
),
("LOffset", "Coverage", None, None, ""),
("LOffset", "MultiVarStore", None, None, "(may be NULL)"),
("LOffset", "ConditionSetList", None, None, "(may be NULL)"),
("LOffset", "AxisIndicesList", None, None, "(may be NULL)"),
("LOffset", "VarCompositeGlyphs", None, None, ""),
],
Expand Down
14 changes: 13 additions & 1 deletion Lib/fontTools/ttLib/tables/otTables.py
Expand Up @@ -60,7 +60,7 @@ class VarComponentFlags(IntFlag):
HAVE_TRANSLATE_Y = 1 << 5
HAVE_ROTATION = 1 << 6

USE_MY_METRICS = 1 << 7
HAVE_CONDITION = 1 << 7

HAVE_SCALE_X = 1 << 8
HAVE_SCALE_Y = 1 << 9
Expand Down Expand Up @@ -158,6 +158,7 @@ def __init__(self):
def populateDefaults(self, propagator=None):
self.flags = 0
self.glyphName = None
self.conditionSetIndex = None
self.axisIndicesIndex = None
self.axisValues = ()
self.axisValuesVarIndex = NO_VARIATION_INDEX
Expand All @@ -174,6 +175,9 @@ def decompile(self, data, font, localState):
i += gidSize
self.glyphName = font.glyphOrder[glyphID]

if flags & VarComponentFlags.HAVE_CONDITION:
self.conditionSetIndex, i = _read_uint32var(data, i)

Check warning on line 179 in Lib/fontTools/ttLib/tables/otTables.py

View check run for this annotation

Codecov / codecov/patch

Lib/fontTools/ttLib/tables/otTables.py#L179

Added line #L179 was not covered by tests

if flags & VarComponentFlags.HAVE_AXES:
self.axisIndicesIndex, i = _read_uint32var(data, i)
else:
Expand Down Expand Up @@ -244,6 +248,10 @@ def compile(self, font):
flags &= ~VarComponentFlags.GID_IS_24BIT
data.append(_packer[2](glyphID))

if self.conditionSetIndex is not None:
flags |= VarComponentFlags.HAVE_CONDITION
data.append(_write_uint32var(self.conditionSetIndex))

Check warning on line 253 in Lib/fontTools/ttLib/tables/otTables.py

View check run for this annotation

Codecov / codecov/patch

Lib/fontTools/ttLib/tables/otTables.py#L252-L253

Added lines #L252 - L253 were not covered by tests

numAxes = len(self.axisValues)

if numAxes:
Expand Down Expand Up @@ -293,6 +301,8 @@ def write(name, value, attrs=()):

write("glyphName", self.glyphName)

if self.conditionSetIndex is not None:
write("conditionSetIndex", self.conditionSetIndex)

Check warning on line 305 in Lib/fontTools/ttLib/tables/otTables.py

View check run for this annotation

Codecov / codecov/patch

Lib/fontTools/ttLib/tables/otTables.py#L305

Added line #L305 was not covered by tests
if self.axisIndicesIndex is not None:
write("axisIndicesIndex", self.axisIndicesIndex)
if (
Expand Down Expand Up @@ -332,6 +342,8 @@ def fromXML(self, name, attrs, content, ttFont):

if name == "glyphName":
self.glyphName = v
elif name == "conditionSetIndex":
self.conditionSetIndex = safeEval(v)

Check warning on line 346 in Lib/fontTools/ttLib/tables/otTables.py

View check run for this annotation

Codecov / codecov/patch

Lib/fontTools/ttLib/tables/otTables.py#L346

Added line #L346 was not covered by tests
elif name == "axisIndicesIndex":
self.axisIndicesIndex = safeEval(v)
elif name == "axisValues":
Expand Down
22 changes: 22 additions & 0 deletions Lib/fontTools/ttLib/ttGlyphSet.py
Expand Up @@ -299,6 +299,28 @@ def _draw(self, pen, isPointPen):
)

for comp in glyph.components:

if comp.flags & VarComponentFlags.HAVE_CONDITION:
conditionSet = varc.ConditionSetList.ConditionSet[

Check warning on line 304 in Lib/fontTools/ttLib/ttGlyphSet.py

View check run for this annotation

Codecov / codecov/patch

Lib/fontTools/ttLib/ttGlyphSet.py#L304

Added line #L304 was not covered by tests
comp.conditionSetIndex
]
# Evaluate condition
show = True

Check warning on line 308 in Lib/fontTools/ttLib/ttGlyphSet.py

View check run for this annotation

Codecov / codecov/patch

Lib/fontTools/ttLib/ttGlyphSet.py#L308

Added line #L308 was not covered by tests
for condition in conditionSet.ConditionTable:
if condition.Format == 1:
axisIndex = condition.AxisIndex
axisTag = fvarAxes[axisIndex].axisTag
axisValue = self.glyphSet.location[axisTag]
minValue = condition.FilterRangeMinValue
maxValue = condition.FilterRangeMaxValue

Check warning on line 315 in Lib/fontTools/ttLib/ttGlyphSet.py

View check run for this annotation

Codecov / codecov/patch

Lib/fontTools/ttLib/ttGlyphSet.py#L311-L315

Added lines #L311 - L315 were not covered by tests
if not (minValue <= axisValue <= maxValue):
show = False
break

Check warning on line 318 in Lib/fontTools/ttLib/ttGlyphSet.py

View check run for this annotation

Codecov / codecov/patch

Lib/fontTools/ttLib/ttGlyphSet.py#L317-L318

Added lines #L317 - L318 were not covered by tests
else:
show = False # Unkonwn condition format

Check warning on line 320 in Lib/fontTools/ttLib/ttGlyphSet.py

View check run for this annotation

Codecov / codecov/patch

Lib/fontTools/ttLib/ttGlyphSet.py#L320

Added line #L320 was not covered by tests
if not show:
continue

Check warning on line 322 in Lib/fontTools/ttLib/ttGlyphSet.py

View check run for this annotation

Codecov / codecov/patch

Lib/fontTools/ttLib/ttGlyphSet.py#L322

Added line #L322 was not covered by tests

location = {}
if comp.axisIndicesIndex is not None:
axisIndices = varc.AxisIndicesList.Item[comp.axisIndicesIndex]
Expand Down
Binary file modified Tests/ttLib/data/varc-6868.ttf
Binary file not shown.

0 comments on commit 5f9add9

Please sign in to comment.