Debian Codegraph @

Lib/fontFeatures/ttLib/GDEFUnparser.py

raw
1 """GDEF Unparser: Extract fontFeatures information from GDEF table."""
2
3
4 class GDEFUnparser:
5 """Extract GDEF information into a fontFeatures object.
6
7 Args:
8 table: a GDEF table (e.g. ``font["GDEF"]``)
9 ff: a fontFeatures object.
10 """
11
12 def __init__(self, table, fontfeatures):
13 self.table = table.table
14 self.ff = fontfeatures
15
16 def unparse(self):
17 """Extracts glyph class definitions and stores them in the fontFeatures
18 object.
19
20 In theory other information can be retrieved, but currently there is no
21 place to store it in the fontFeatures object.
22 """
23 # if self.table.MarkAttachClassDef is not None:
24 # self.unparseMarkAttachClassDefinitions()
25
26 # if (
27 # hasattr(self.table, "MarkGlyphSetsDef")
28 # and self.table.MarkGlyphSetsDef is not None
29 # ):
30 # self.unparseMarkGlyphSetDefinitions()
31
32 if self.table.GlyphClassDef is not None:
33 self.unparseGlyphClassDefinitions()
34
35 # if self.table.AttachList is not None:
36 # self.unparseAttachList()
37
38 # if self.table.LigCaretList is not None:
39 # self.unparseLigCaretList()
40
41 # def unparseMarkAttachClassDefinitions(self):
42 # self.preamble.append(Comment("# Mark attachment classes\n"))
43 # markAttachClassDef = self.table.MarkAttachClassDef
44 # attachClasses = {}
45
46 # for key, val in markAttachClassDef.classDefs.items():
47 # if not val in attachClasses:
48 # attachClasses[val] = GlyphClass()
49 # attachClasses[val].append(key)
50
51 # for key, val in attachClasses.items():
52 # self.preamble.append(
53 # GlyphClassDefinition("markClass_" + str(key), val)
54 # )
55
56 # def unparseMarkGlyphSetDefinitions(self):
57 # lines = []
58 # self.preamble.append(Comment("# Mark glyph set classes\n"))
59 # for idx, coverage in enumerate(self.table.MarkGlyphSetsDef.Coverage):
60 # glyphclass = GlyphClass()
61 # for g in coverage.glyphs:
62 # glyphclass.append(g)
63 # self.preamble.append(
64 # GlyphClassDefinition("markGlyphSet_" + str(idx), glyphclass)
65 # )
66
67 def unparseGlyphClassDefinitions(self):
68 """Fills the fontFeatures ``glyphclasses`` dictionary from the
69 ``GlyphClassDef`` subtable."""
70 classDefs = self.table.GlyphClassDef.classDefs
71 classNames = {
72 1: "base", # Base glyph (single character, spacing glyph)
73 2: "ligature", # Ligature glyph (multiple character, spacing glyph)
74 3: "mark", # Mark glyph (non-spacing combining glyph)
75 4: "component", # Component glyph (part of single character, spacing glyph)
76 }
77 for key, val in classDefs.items():
78 self.ff.glyphclasses[key] = classNames[val]
79
80 # def unparseAttachList(self):
81 # for idx, glyph in enumerate(self.table.AttachList.Coverage.glyphs):
82 # attachmentPoints = self.table.AttachList.AttachPoint[idx]
83 # self.feature.statements.append(
84 # AttachStatement(GlyphName(glyph), attachmentPoints.PointIndex)
85 # )
86
87 # def unparseLigCaretList(self):
88 # ligCaretList = self.table.LigCaretList
89 # for idx, glyph in enumerate(ligCaretList.Coverage.glyphs):
90 # glyph = GlyphName(glyph)
91 # ligGlyph = ligCaretList.LigGlyph[idx]
92 # formats = list({cv.Format for cv in ligGlyph.CaretValue})
93 # assert (
94 # len(formats) == 1
95 # ), "Can't use different CaretValue formats in one LigatureCaret entry"
96 # caretsFormat = formats[0]
97 # if caretsFormat == 1:
98 # # Format == 1: Design units only
99 # # LigatureCaretByPos <glyph|glyphclass> <caret position value>+;
100 # values = [str(cv.Coordinate) for cv in ligGlyph.CaretValue]
101 # self.feature.statements.append(
102 # LigatureCaretByPosStatement(glyph, values)
103 # )
104 # elif caretsFormat == 2:
105 # # Format == 2: Contour point
106 # values = [str(cv.CaretValuePoint) for cv in ligGlyph.CaretValue]
107 # self.feature.statements.append(
108 # LigatureCaretByIndexStatement(glyph, values)
109 # )
110 # elif caretsFormat == 3:
111 # # Format == 3: Design units plus Device table
112 # self.feature.statements.append(
113 # Comment("# LigatureCaretByDev found, but not implemented")
114 # )