Skip to content

Commit

Permalink
[Condition] Implement ConditionValue
Browse files Browse the repository at this point in the history
  • Loading branch information
behdad committed Apr 23, 2024
1 parent 040f333 commit 12ece0b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 11 deletions.
28 changes: 24 additions & 4 deletions Lib/fontTools/ttLib/tables/otData.py
Expand Up @@ -3237,6 +3237,26 @@
"ConditionTableFormat2",
[
("uint16", "Format", None, None, "Format, = 2"),
(
"int16",
"DefaultValue",
None,
None,
"Value at default instance.",
),
(
"uint32",
"VarIdx",
None,
None,
"Variation index to vary the value based on current designspace location.",
),
],
),
(
"ConditionTableFormat3",
[
("uint16", "Format", None, None, "Format, = 3"),
(
"uint8",
"ConditionCount",
Expand All @@ -3254,9 +3274,9 @@
],
),
(
"ConditionTableFormat3",
"ConditionTableFormat4",
[
("uint16", "Format", None, None, "Format, = 3"),
("uint16", "Format", None, None, "Format, = 4"),
(
"uint8",
"ConditionCount",
Expand All @@ -3274,9 +3294,9 @@
],
),
(
"ConditionTableFormat4",
"ConditionTableFormat5",
[
("uint16", "Format", None, None, "Format, = 4"),
("uint16", "Format", None, None, "Format, = 5"),
(
"Offset24",
"ConditionTable",
Expand Down
31 changes: 24 additions & 7 deletions Lib/fontTools/ttLib/ttGlyphSet.py
Expand Up @@ -276,29 +276,37 @@ def draw(self, pen):
self.glyphSet.charStrings[self.name].draw(pen, self.glyphSet.blender)


def _evaluateCondition(condition, fvarAxes, location):
def _evaluateCondition(condition, fvarAxes, location, instancer):
if condition.Format == 1:
# ConditionAxisRange
axisIndex = condition.AxisIndex
axisTag = fvarAxes[axisIndex].axisTag
axisValue = location.get(axisTag, 0)
minValue = condition.FilterRangeMinValue
maxValue = condition.FilterRangeMaxValue
return minValue <= axisValue <= maxValue
elif condition.Format == 2:
# ConditionValue
value = condition.DefaultValue
value += instancer[condition.VarIdx]
return value >= 0

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

View check run for this annotation

Codecov / codecov/patch

Lib/fontTools/ttLib/ttGlyphSet.py#L290-L292

Added lines #L290 - L292 were not covered by tests
elif condition.Format == 3:
# ConditionAnd
for subcondition in condition.ConditionTable:
if not _evaluateCondition(subcondition, fvarAxes, location):
if not _evaluateCondition(subcondition, fvarAxes, location, instancer):
return False
return True

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

View check run for this annotation

Codecov / codecov/patch

Lib/fontTools/ttLib/ttGlyphSet.py#L297-L298

Added lines #L297 - L298 were not covered by tests
elif condition.Format == 3:
elif condition.Format == 4:
# ConditionOr
for subcondition in condition.ConditionTable:
if _evaluateCondition(subcondition, fvarAxes, location):
if _evaluateCondition(subcondition, fvarAxes, location, instancer):
return True
return False

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#L303-L304

Added lines #L303 - L304 were not covered by tests
elif condition.Format == 4:
elif condition.Format == 5:
# ConditionNegate
return not _evaluateCondition(condition.conditionTable, fvarAxes, location)
return not _evaluateCondition(

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

View check run for this annotation

Codecov / codecov/patch

Lib/fontTools/ttLib/ttGlyphSet.py#L307

Added line #L307 was not covered by tests
condition.conditionTable, fvarAxes, location, instancer
)
else:
return False # Unkonwn condition format

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

View check run for this annotation

Codecov / codecov/patch

Lib/fontTools/ttLib/ttGlyphSet.py#L311

Added line #L311 was not covered by tests

Expand All @@ -319,17 +327,26 @@ def _draw(self, pen, isPointPen):
glyph = varc.VarCompositeGlyphs.VarCompositeGlyph[idx]

from fontTools.varLib.multiVarStore import MultiVarStoreInstancer
from fontTools.varLib.varStore import VarStoreInstancer

fvarAxes = glyphSet.font["fvar"].axes
instancer = MultiVarStoreInstancer(
varc.MultiVarStore, fvarAxes, self.glyphSet.location
)
gdef = glyphSet.font.get("GDEF") if "GDEF" in glyphSet.font else None
gdefInstancer = VarStoreInstancer(
getattr(gdef.table, "VarStore") if gdef is not None else None,
fvarAxes,
self.glyphSet.location,
)

for comp in glyph.components:

if comp.flags & VarComponentFlags.HAVE_CONDITION:
condition = varc.ConditionList.ConditionTable[comp.conditionIndex]
if not _evaluateCondition(condition, fvarAxes, self.glyphSet.location):
if not _evaluateCondition(
condition, fvarAxes, self.glyphSet.location, gdefInstancer
):
continue

location = {}
Expand Down

0 comments on commit 12ece0b

Please sign in to comment.