Skip to content

Commit

Permalink
[designspaceLib/avar2] Add AxisMappingDescriptor
Browse files Browse the repository at this point in the history
  • Loading branch information
behdad committed May 26, 2023
1 parent 76657ef commit cb4a2f4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
22 changes: 20 additions & 2 deletions Lib/fontTools/designspaceLib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,18 @@ def processRules(rules, location, glyphNames):
return glyphNames


class AxisMappingDescriptor(SimpleDescriptor):
"""XXX TODO"""

_attrs = ["inputLocation", "outputLocation"] # what do we need here

def __init__(self, *, inputLocation=None, outputLocation=None):
self.inputLocation = inputLocation or {}
"""XXX TODO"""
self.outputLocation = outputLocation or {}
"""XXX TODO"""

Check warning on line 458 in Lib/fontTools/designspaceLib/__init__.py

View check run for this annotation

Codecov / codecov/patch

Lib/fontTools/designspaceLib/__init__.py#L455-L458

Added lines #L455 - L458 were not covered by tests


AnisotropicLocationDict = Dict[str, Union[float, Tuple[float, float]]]
SimpleLocationDict = Dict[str, float]

Expand Down Expand Up @@ -1323,6 +1335,7 @@ def __init__(self, *, name, userValue):
class BaseDocWriter(object):
_whiteSpace = " "
axisDescriptorClass = AxisDescriptor
axisMappingDescriptorClass = AxisMappingDescriptor
discreteAxisDescriptorClass = DiscreteAxisDescriptor
axisLabelDescriptorClass = AxisLabelDescriptor
locationLabelDescriptorClass = LocationLabelDescriptor
Expand Down Expand Up @@ -1852,6 +1865,7 @@ def _writeGlyphElement(self, instanceElement, instanceObject, glyphName, data):

class BaseDocReader(LogMixin):
axisDescriptorClass = AxisDescriptor
axisMappingDescriptorClass = AxisMappingDescriptor
discreteAxisDescriptorClass = DiscreteAxisDescriptor
axisLabelDescriptorClass = AxisLabelDescriptor
locationLabelDescriptorClass = LocationLabelDescriptor
Expand Down Expand Up @@ -2021,7 +2035,10 @@ def readAxes(self):
tag = dimElement.attrib["tag"]
value = float(dimElement.attrib["xvalue"])
outputLoc[tag] = value
self.documentObject.axisMappings.append([inputLoc, outputLoc])
axisMappingObject = self.axisMappingDescriptorClass(

Check warning on line 2038 in Lib/fontTools/designspaceLib/__init__.py

View check run for this annotation

Codecov / codecov/patch

Lib/fontTools/designspaceLib/__init__.py#L2035-L2038

Added lines #L2035 - L2038 were not covered by tests
inputLocation=inputLoc, outputLocation=outputLoc
)
self.documentObject.axisMappings.append(axisMappingObject)

Check warning on line 2041 in Lib/fontTools/designspaceLib/__init__.py

View check run for this annotation

Codecov / codecov/patch

Lib/fontTools/designspaceLib/__init__.py#L2041

Added line #L2041 was not covered by tests

def readAxisLabel(self, element: ET.Element):
xml_attrs = {
Expand Down Expand Up @@ -2528,6 +2545,7 @@ class DesignSpaceDocument(LogMixin, AsDictMixin):
doc.formatVersion
doc.elidedFallbackName
doc.axes
doc.axisMappings
doc.locationLabels
doc.rules
doc.rulesProcessingLast
Expand Down Expand Up @@ -2566,7 +2584,7 @@ def __init__(self, readerClass=None, writerClass=None):
self.axes: List[Union[AxisDescriptor, DiscreteAxisDescriptor]] = []
"""List of this document's axes."""

self.axisMappings: List = []
self.axisMappings: List[AxisMappingDescriptor] = []
"""List of this document's axis mappings."""

self.locationLabels: List[LocationLabelDescriptor] = []
Expand Down
2 changes: 1 addition & 1 deletion Lib/fontTools/designspaceLib/split.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ def maybeExpandDesignLocation(object):
)

subDoc.lib = doc.lib
subDoc.axisMappings = doc.axisMappings # TODO
subDoc.axisMappings = doc.axisMappings # XXX TODO

return subDoc

Expand Down
12 changes: 6 additions & 6 deletions Lib/fontTools/varLib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def _add_fvar(font, axes, instances: List[InstanceDescriptor]):
return fvar


def _add_avar(font, axes, mapping, axisTags):
def _add_avar(font, axes, mappings, axisTags):
"""
Add 'avar' table to font.
Expand Down Expand Up @@ -212,22 +212,22 @@ def _add_avar(font, axes, mapping, axisTags):
assert +1.0 not in curve or curve[+1.0] == +1.0
# curve.update({-1.0: -1.0, 0.0: 0.0, 1.0: 1.0})

if mapping:
if mappings:
interesting = True

Check warning on line 216 in Lib/fontTools/varLib/__init__.py

View check run for this annotation

Codecov / codecov/patch

Lib/fontTools/varLib/__init__.py#L216

Added line #L216 was not covered by tests

derived = [
{
tag: models.normalizeValue(v, vals_triples[tag])
for tag, v in inputLoc.items()
for tag, v in mapping.inputLocation.items()
}
for inputLoc, outputLoc in mapping
for mapping in mappings
]
source = [
{
tag: models.normalizeValue(v, vals_triples[tag])
for tag, v in outputLoc.items()
for tag, v in mapping.outputLocation.items()
}
for inputLoc, outputLoc in mapping
for mapping in mappings
]

model = models.VariationModel(derived, axisTags)
Expand Down

0 comments on commit cb4a2f4

Please sign in to comment.