[varLib] Make parsing of ‘axes’ element more robust
This commit is contained in:
parent
aed9caf964
commit
0dd2227e49
@ -9,13 +9,7 @@ except ImportError:
|
|||||||
|
|
||||||
__all__ = ['load', 'loads']
|
__all__ = ['load', 'loads']
|
||||||
|
|
||||||
standard_axis_map = collections.OrderedDict(
|
namespaces = {'xml': '{http://www.w3.org/XML/1998/namespace}'}
|
||||||
[['weight', ('wght', 'Weight')],
|
|
||||||
['width', ('wdth', 'Width')],
|
|
||||||
['slant', ('slnt', 'Slant')],
|
|
||||||
['optical', ('opsz', 'Optical Size')],
|
|
||||||
['custom',('xxxx', 'Custom')]]
|
|
||||||
)
|
|
||||||
|
|
||||||
def _xmlParseLocation(et):
|
def _xmlParseLocation(et):
|
||||||
loc = {}
|
loc = {}
|
||||||
@ -40,23 +34,40 @@ def _loadItem(et):
|
|||||||
item[elt.tag] = value
|
item[elt.tag] = value
|
||||||
return item
|
return item
|
||||||
|
|
||||||
|
def _xmlParseAxisOrMap(elt):
|
||||||
|
dic = {}
|
||||||
|
for name in elt.attrib:
|
||||||
|
if name in ['name', 'tag']:
|
||||||
|
dic[name] = elt.attrib[name]
|
||||||
|
else:
|
||||||
|
dic[name] = float(elt.attrib[name])
|
||||||
|
return dic
|
||||||
|
|
||||||
|
def _loadAxis(et):
|
||||||
|
item = dict(_xmlParseAxisOrMap(et))
|
||||||
|
maps = []
|
||||||
|
labelnames = {}
|
||||||
|
for elt in et:
|
||||||
|
assert elt.tag in ['labelname', 'map']
|
||||||
|
if elt.tag == 'labelname':
|
||||||
|
lang = elt.attrib["{0}lang".format(namespaces['xml'])]
|
||||||
|
labelnames[lang] = elt.text
|
||||||
|
elif elt.tag == 'map':
|
||||||
|
maps.append(_xmlParseAxisOrMap(elt))
|
||||||
|
if labelnames:
|
||||||
|
item['labelname'] = labelnames
|
||||||
|
if maps:
|
||||||
|
item['map'] = maps
|
||||||
|
return item
|
||||||
|
|
||||||
def _load(et):
|
def _load(et):
|
||||||
ds = et.getroot()
|
ds = et.getroot()
|
||||||
|
|
||||||
axisMap = collections.OrderedDict()
|
axes = []
|
||||||
axesET = ds.find('axes')
|
ds_axes = ds.find('axes')
|
||||||
if axesET:
|
if ds_axes:
|
||||||
axisList = axesET.findall('axis')
|
for et in ds_axes:
|
||||||
for axisET in axisList:
|
axes.append(_loadAxis(et))
|
||||||
axisName = axisET.attrib["name"]
|
|
||||||
labelET = axisET.find('labelname')
|
|
||||||
if (None == labelET):
|
|
||||||
# If the designpsace file axes is a std axes, the label name may be omitted.
|
|
||||||
tag, label = standard_axis_map[axisName]
|
|
||||||
else:
|
|
||||||
label = labelET.text
|
|
||||||
tag = axisET.attrib["tag"]
|
|
||||||
axisMap[axisName] = (tag, label)
|
|
||||||
|
|
||||||
masters = []
|
masters = []
|
||||||
for et in ds.find('sources'):
|
for et in ds.find('sources'):
|
||||||
@ -66,11 +77,14 @@ def _load(et):
|
|||||||
for et in ds.find('instances'):
|
for et in ds.find('instances'):
|
||||||
instances.append(_loadItem(et))
|
instances.append(_loadItem(et))
|
||||||
|
|
||||||
return masters, instances, axisMap
|
return axes, masters, instances
|
||||||
|
|
||||||
def load(filename):
|
def load(filename):
|
||||||
"""Load designspace from a file name or object. Returns two items:
|
"""Load designspace from a file name or object.
|
||||||
list of masters (aka sources) and list of instances."""
|
Returns three items:
|
||||||
|
- list of axes
|
||||||
|
- list of masters (aka sources)
|
||||||
|
- list of instances"""
|
||||||
return _load(ET.parse(filename))
|
return _load(ET.parse(filename))
|
||||||
|
|
||||||
def loads(string):
|
def loads(string):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user