1from fontParts.base.glyph import BaseGlyph
2from babelfont.image import Image
3from babelfont import addUnderscoreProperty
4from babelfont.lib import Lib
5from babelfont.anchor import Anchor
6
7
8# @addUnderscoreProperty(["name", "unicodes", "width", "height", "lib"])
9@addUnderscoreProperty("name")
10@addUnderscoreProperty("unicodes")
11@addUnderscoreProperty("guidelines")
12@addUnderscoreProperty("image")
13@addUnderscoreProperty("width")
14@addUnderscoreProperty("height")
15@addUnderscoreProperty("lib")
16@addUnderscoreProperty("note")
17@addUnderscoreProperty("markColor")
18class Glyph(BaseGlyph):
19    def _init(self, *args, **kwargs):
20        self._lib = Lib()
21        self._components = []
22        self._anchors = []
23        self._unicodes = []
24        self._guidelines = []
25        self._contours = []
26        self._image = Image()
27        self.exported = True
28        self._width = 0
29        self._height = 0
30        self._note = ""
31        self._markColor = None
32
33    def _autoUnicodes(self):
34        # Maybe someday
35        self.raiseNotImplementedError()
36
37    def _get_lib(self):
38        return self._lib
39
40    def _lenContours(self):
41        return len(self._contours)
42
43    def _lenGuidelines(self):
44        return 0
45
46    def _clearImage(self):
47        self._image = Image()
48
49    def _getContour(self, index, **kwargs):
50        return self._contours[index]
51
52    def _appendContour(self, contour, offset=None, **kwargs):
53        copy = contour.copy()
54        if offset != (0, 0):
55            copy.moveBy(offset)
56        copy._glyph = self
57        self._contours.append(copy)
58
59    def _removeContour(self, index, **kwargs):
60        del(self._contours[index])
61
62
63    def _lenComponents(self):
64        return len(self._components)
65
66    def _getComponent(self, index, **kwargs):
67        return self._components[index]
68
69    def _removeComponent(self, index, **kwargs):
70        del(self._components[index])
71
72
73    def _lenAnchors(self):
74        return len(self._anchors)
75
76    def _getAnchor(self, index, **kwargs):
77        return self._anchors[index]
78
79    def _removeAnchor(self, index, **kwargs):
80        del(self._anchors[index])
81
82    def _appendAnchor(self, name, **kwargs):
83        self._anchors.append(Anchor(name=name, **kwargs))
84
85    # Babelfont glyphs have a category, even if fontParts ones don't
86    @property
87    def category(self):
88        if "public.openTypeCategory" in self.lib:
89            return self.lib["public.openTypeCategory"]
90
91    def set_category(self, category):
92        assert(category in ["ligature", "mark", "base"])
93        self.lib["public.openTypeCategory"] = category
94        if category == "ligature" and \
95            "com.schriftgestaltung.Glyphs.category" not in self.lib:
96            self.lib["com.schriftgestaltung.Glyphs.category"] = "Letter"
97            self.lib["com.schriftgestaltung.Glyphs.subcategory"] = "Ligature"
98        elif category == "mark" and \
99            "com.schriftgestaltung.Glyphs.category" not in self.lib:
100            self.lib["com.schriftgestaltung.Glyphs.category"] = "Mark"
101        elif category == "base" and \
102            "com.schriftgestaltung.Glyphs.category" not in self.lib:
103            self.lib["com.schriftgestaltung.Glyphs.category"] = "Letter"
104