File: GDEFUnparser.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 (114 lines) | stat: -rw-r--r-- 4,700 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
"""GDEF Unparser: Extract fontFeatures information from GDEF table."""


class GDEFUnparser:
    """Extract GDEF information into a fontFeatures object.

    Args:
        table: a GDEF table (e.g. ``font["GDEF"]``)
        ff: a fontFeatures object.
    """

    def __init__(self, table, fontfeatures):
        self.table = table.table
        self.ff = fontfeatures

    def unparse(self):
        """Extracts glyph class definitions and stores them in the fontFeatures
        object.

        In theory other information can be retrieved, but currently there is no
        place to store it in the fontFeatures object.
        """
        # if self.table.MarkAttachClassDef is not None:
        #     self.unparseMarkAttachClassDefinitions()

        # if (
        #     hasattr(self.table, "MarkGlyphSetsDef")
        #     and self.table.MarkGlyphSetsDef is not None
        # ):
        #     self.unparseMarkGlyphSetDefinitions()

        if self.table.GlyphClassDef is not None:
            self.unparseGlyphClassDefinitions()

        # if self.table.AttachList is not None:
        #     self.unparseAttachList()

        # if self.table.LigCaretList is not None:
        #     self.unparseLigCaretList()

    # def unparseMarkAttachClassDefinitions(self):
    #     self.preamble.append(Comment("# Mark attachment classes\n"))
    #     markAttachClassDef = self.table.MarkAttachClassDef
    #     attachClasses = {}

    #     for key, val in markAttachClassDef.classDefs.items():
    #         if not val in attachClasses:
    #             attachClasses[val] = GlyphClass()
    #         attachClasses[val].append(key)

    #     for key, val in attachClasses.items():
    #         self.preamble.append(
    #             GlyphClassDefinition("markClass_" + str(key), val)
    #         )

    # def unparseMarkGlyphSetDefinitions(self):
    #     lines = []
    #     self.preamble.append(Comment("# Mark glyph set classes\n"))
    #     for idx, coverage in enumerate(self.table.MarkGlyphSetsDef.Coverage):
    #         glyphclass = GlyphClass()
    #         for g in coverage.glyphs:
    #             glyphclass.append(g)
    #         self.preamble.append(
    #             GlyphClassDefinition("markGlyphSet_" + str(idx), glyphclass)
    #         )

    def unparseGlyphClassDefinitions(self):
        """Fills the fontFeatures ``glyphclasses`` dictionary from the
        ``GlyphClassDef`` subtable."""
        classDefs = self.table.GlyphClassDef.classDefs
        classNames = {
            1: "base",  # Base glyph (single character, spacing glyph)
            2: "ligature",  # Ligature glyph (multiple character, spacing glyph)
            3: "mark",  # Mark glyph (non-spacing combining glyph)
            4: "component",  # Component glyph (part of single character, spacing glyph)
        }
        for key, val in classDefs.items():
            self.ff.glyphclasses[key] = classNames[val]

    # def unparseAttachList(self):
    #     for idx, glyph in enumerate(self.table.AttachList.Coverage.glyphs):
    #         attachmentPoints = self.table.AttachList.AttachPoint[idx]
    #         self.feature.statements.append(
    #             AttachStatement(GlyphName(glyph), attachmentPoints.PointIndex)
    #         )

    # def unparseLigCaretList(self):
    #     ligCaretList = self.table.LigCaretList
    #     for idx, glyph in enumerate(ligCaretList.Coverage.glyphs):
    #         glyph = GlyphName(glyph)
    #         ligGlyph = ligCaretList.LigGlyph[idx]
    #         formats = list({cv.Format for cv in ligGlyph.CaretValue})
    #         assert (
    #             len(formats) == 1
    #         ), "Can't use different CaretValue formats in one LigatureCaret entry"
    #         caretsFormat = formats[0]
    #         if caretsFormat == 1:
    #             # Format == 1: Design units only
    #             # LigatureCaretByPos <glyph|glyphclass> <caret position value>+;
    #             values = [str(cv.Coordinate) for cv in ligGlyph.CaretValue]
    #             self.feature.statements.append(
    #                 LigatureCaretByPosStatement(glyph, values)
    #             )
    #         elif caretsFormat == 2:
    #             # Format == 2: Contour point
    #             values = [str(cv.CaretValuePoint) for cv in ligGlyph.CaretValue]
    #             self.feature.statements.append(
    #                 LigatureCaretByIndexStatement(glyph, values)
    #             )
    #         elif caretsFormat == 3:
    #             # Format == 3: Design units plus Device table
    #             self.feature.statements.append(
    #                 Comment("# LigatureCaretByDev found, but not implemented")
    #             )