remove deprecated fontTools.varLib.designspace
This commit is contained in:
parent
9557c6a229
commit
6db88d7cb7
@ -1,123 +0,0 @@
|
||||
"""Rudimentary support for loading MutatorMath .designspace files.
|
||||
|
||||
DEPRECATED. Use ``fonttools.designspaceLib`` instead.
|
||||
"""
|
||||
from __future__ import print_function, division, absolute_import
|
||||
from fontTools.misc.py23 import *
|
||||
try:
|
||||
import xml.etree.cElementTree as ET
|
||||
except ImportError:
|
||||
import xml.etree.ElementTree as ET
|
||||
|
||||
import warnings
|
||||
warnings.warn(
|
||||
"fontTools.varLib.designspace is deprecated. "
|
||||
"Use fontTools.designspaceLib instead.",
|
||||
DeprecationWarning,
|
||||
)
|
||||
|
||||
__all__ = ['load', 'loads']
|
||||
|
||||
namespaces = {'xml': '{http://www.w3.org/XML/1998/namespace}'}
|
||||
|
||||
|
||||
def _xml_parse_location(et):
|
||||
loc = {}
|
||||
for dim in et.find('location'):
|
||||
assert dim.tag == 'dimension'
|
||||
name = dim.attrib['name']
|
||||
value = float(dim.attrib['xvalue'])
|
||||
assert name not in loc
|
||||
loc[name] = value
|
||||
return loc
|
||||
|
||||
|
||||
def _load_item(et):
|
||||
item = dict(et.attrib)
|
||||
for element in et:
|
||||
if element.tag == 'location':
|
||||
value = _xml_parse_location(et)
|
||||
else:
|
||||
value = {}
|
||||
if 'copy' in element.attrib:
|
||||
value['copy'] = bool(int(element.attrib['copy']))
|
||||
# TODO load more?!
|
||||
item[element.tag] = value
|
||||
return item
|
||||
|
||||
|
||||
def _xml_parse_axis_or_map(element):
|
||||
dic = {}
|
||||
for name in element.attrib:
|
||||
if name in ['name', 'tag']:
|
||||
dic[name] = element.attrib[name]
|
||||
else:
|
||||
dic[name] = float(element.attrib[name])
|
||||
return dic
|
||||
|
||||
|
||||
def _load_axis(et):
|
||||
item = _xml_parse_axis_or_map(et)
|
||||
maps = []
|
||||
labelnames = {}
|
||||
for element in et:
|
||||
assert element.tag in ['labelname', 'map']
|
||||
if element.tag == 'labelname':
|
||||
lang = element.attrib["{0}lang".format(namespaces['xml'])]
|
||||
labelnames[lang] = element.text
|
||||
elif element.tag == 'map':
|
||||
maps.append(_xml_parse_axis_or_map(element))
|
||||
if labelnames:
|
||||
item['labelname'] = labelnames
|
||||
if maps:
|
||||
item['map'] = maps
|
||||
return item
|
||||
|
||||
|
||||
def _load(et):
|
||||
designspace = {}
|
||||
ds = et.getroot()
|
||||
|
||||
axes_element = ds.find('axes')
|
||||
if axes_element is not None:
|
||||
axes = []
|
||||
for et in axes_element:
|
||||
axes.append(_load_axis(et))
|
||||
designspace['axes'] = axes
|
||||
|
||||
sources_element = ds.find('sources')
|
||||
if sources_element is not None:
|
||||
sources = []
|
||||
for et in sources_element:
|
||||
sources.append(_load_item(et))
|
||||
designspace['sources'] = sources
|
||||
|
||||
instances_element = ds.find('instances')
|
||||
if instances_element is not None:
|
||||
instances = []
|
||||
for et in instances_element:
|
||||
instances.append(_load_item(et))
|
||||
designspace['instances'] = instances
|
||||
|
||||
return designspace
|
||||
|
||||
|
||||
def load(filename):
|
||||
"""Load designspace from a file name or object.
|
||||
Returns a dictionary containing three (optional) items:
|
||||
- list of "axes"
|
||||
- list of "sources" (aka masters)
|
||||
- list of "instances"
|
||||
"""
|
||||
return _load(ET.parse(filename))
|
||||
|
||||
|
||||
def loads(string):
|
||||
"""Load designspace from a string."""
|
||||
return _load(ET.fromstring(string))
|
||||
|
||||
if __name__ == '__main__':
|
||||
import sys
|
||||
from pprint import pprint
|
||||
for f in sys.argv[1:]:
|
||||
pprint(load(f))
|
@ -1,39 +0,0 @@
|
||||
<?xml version="1.0"?>
|
||||
<designspace format="3">
|
||||
<axes>
|
||||
<axis default="0.0" maximum="1000.0" minimum="0.0" name="weight" tag="wght">
|
||||
<map input="0.0" output="10.0" />
|
||||
<map input="401.0" output="66.0" />
|
||||
<map input="1000.0" output="990.0" />
|
||||
</axis>
|
||||
<axis default="250.0" maximum="1000.0" minimum="0.0" name="width" tag="wdth" />
|
||||
<axis default="0.0" maximum="100.0" minimum="0.0" name="contrast" tag="cntr">
|
||||
<labelname xml:lang="en">Contrast</labelname>
|
||||
<labelname xml:lang="de">Kontrast</labelname>
|
||||
</axis>
|
||||
</axes>
|
||||
<sources>
|
||||
<source filename="DesignspaceTest-Light.ufo" name="master_1">
|
||||
<lib copy="1"/>
|
||||
<groups copy="1"/>
|
||||
<info copy="1"/>
|
||||
<location>
|
||||
<dimension name="weight" xvalue="0.0"/>
|
||||
</location>
|
||||
</source>
|
||||
<source filename="DesignspaceTest-Bold.ufo" name="master_2">
|
||||
<location>
|
||||
<dimension name="weight" xvalue="1.0"/>
|
||||
</location>
|
||||
</source>
|
||||
</sources>
|
||||
<instances>
|
||||
<instance familyname="DesignspaceTest" filename="instance/DesignspaceTest-Medium.ufo" stylename="Medium">
|
||||
<location>
|
||||
<dimension name="weight" xvalue="0.5"/>
|
||||
</location>
|
||||
<info/>
|
||||
<kerning/>
|
||||
</instance>
|
||||
</instances>
|
||||
</designspace>
|
@ -1,8 +0,0 @@
|
||||
<?xml version="1.0"?>
|
||||
<designspace format="3">
|
||||
<!-- no <axes> element -->
|
||||
<sources/><!-- empty <sources> element -->
|
||||
<instances>
|
||||
<instance/><!-- bare-bones <instance> element -->
|
||||
</instances>
|
||||
</designspace>
|
@ -1,69 +0,0 @@
|
||||
from __future__ import print_function, division, absolute_import
|
||||
from __future__ import unicode_literals
|
||||
from fontTools.varLib import designspace
|
||||
import os
|
||||
import unittest
|
||||
|
||||
|
||||
class DesignspaceTest(unittest.TestCase):
|
||||
def test_load(self):
|
||||
self.maxDiff = None
|
||||
self.assertEqual(
|
||||
designspace.load(_getpath("Designspace.designspace")),
|
||||
|
||||
{'sources':
|
||||
[{'location': {'weight': 0.0},
|
||||
'groups': {'copy': True},
|
||||
'filename': 'DesignspaceTest-Light.ufo',
|
||||
'info': {'copy': True},
|
||||
'name': 'master_1',
|
||||
'lib': {'copy': True}},
|
||||
{'location': {'weight': 1.0},
|
||||
'name': 'master_2',
|
||||
'filename': 'DesignspaceTest-Bold.ufo'}],
|
||||
|
||||
'instances':
|
||||
[{'location': {'weight': 0.5},
|
||||
'familyname': 'DesignspaceTest',
|
||||
'filename': 'instance/DesignspaceTest-Medium.ufo',
|
||||
'kerning': {},
|
||||
'info': {},
|
||||
'stylename': 'Medium'}],
|
||||
|
||||
'axes':
|
||||
[{'name': 'weight',
|
||||
'map': [{'input': 0.0, 'output': 10.0},
|
||||
{'input': 401.0, 'output': 66.0},
|
||||
{'input': 1000.0, 'output': 990.0}],
|
||||
'tag': 'wght',
|
||||
'maximum': 1000.0,
|
||||
'minimum': 0.0,
|
||||
'default': 0.0},
|
||||
{'maximum': 1000.0,
|
||||
'default': 250.0,
|
||||
'minimum': 0.0,
|
||||
'name': 'width',
|
||||
'tag': 'wdth'},
|
||||
{'name': 'contrast',
|
||||
'tag': 'cntr',
|
||||
'maximum': 100.0,
|
||||
'minimum': 0.0,
|
||||
'default': 0.0,
|
||||
'labelname': {'de': 'Kontrast', 'en': 'Contrast'}}]
|
||||
}
|
||||
)
|
||||
|
||||
def test_load2(self):
|
||||
self.assertEqual(
|
||||
designspace.load(_getpath("Designspace2.designspace")),
|
||||
{'sources': [], 'instances': [{}]})
|
||||
|
||||
|
||||
def _getpath(testfile):
|
||||
path, _ = os.path.split(__file__)
|
||||
return os.path.join(path, "data", testfile)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import sys
|
||||
sys.exit(unittest.main())
|
Loading…
x
Reference in New Issue
Block a user