File: Rule.py

package info (click to toggle)
python-fontfeatures 1.9.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,096 kB
  • sloc: python: 9,112; makefile: 22
file content (55 lines) | stat: -rw-r--r-- 1,782 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
"""Routines for converting Rule objects to and from XML."""
from lxml import etree
from fontFeatures.xmlLib.utils import put_languages, put_address


@classmethod
def fromXML(klass, el):
    """Creates a Rule from a lxml Element object."""
    import fontFeatures

    subklass = getattr(fontFeatures, el.tag.title())
    assert subklass
    return subklass.fromXML(el)


def toXML(self):
    """Serializes a Rule to a lxml Element object."""
    root = etree.Element(self.__class__.__name__.lower())
    put_address(self, root)
    put_languages(self, root)
    if hasattr(self, "reverse") and self.reverse:
        root.attrib["reverse"] = "true"
    if hasattr(self, "flags") and self.flags:
        root.attrib["flags"] = str(self.flags)
    if hasattr(self, "precontext"):
        self._makeglyphslots(root, "precontext", self.precontext)
    if hasattr(self, "postcontext"):
        self._makeglyphslots(root, "postcontext", self.postcontext)
    if hasattr(self, "lookups") and self.lookups:
        wrapper = etree.SubElement(root, "lookups")
        for slot in self.lookups:
            xmlslot = etree.SubElement(wrapper, "slot")
            if slot:
                for lu in slot:
                    xmlslot.append(lu.toXML())
            else:
                etree.SubElement(xmlslot, "lookup")

    return self._toXML(root)


def _makeglyphslots(self, root, tag, list_of_lists):
    if not list_of_lists:
        return
    wrapper = etree.SubElement(root, tag)
    for slot in list_of_lists:
        xmlslot = etree.SubElement(wrapper, "slot")
        for g in slot:
            etree.SubElement(xmlslot, "glyph").text = g


def _slotArray(self, el):
    if el is None:
        return None
    return [[g.text for g in slot.findall("glyph")] for slot in list(el)]