aboutsummaryrefslogtreecommitdiffstats
path: root/Lib/defconQt/objects/defcon.py
blob: 48de1732d32636575d21a15edae8d465eaedc069 (plain)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
from defcon import Font, Contour, Glyph, Point
from defcon.objects.base import BaseObject
from PyQt5.QtWidgets import QApplication
import fontTools


class TFont(Font):

    def __init__(self, *args, **kwargs):
        if "glyphClass" not in kwargs:
            kwargs["glyphClass"] = TGlyph
        if "glyphContourClass" not in kwargs:
            kwargs["glyphContourClass"] = TContour
        if "glyphPointClass" not in kwargs:
            kwargs["glyphPointClass"] = TPoint
        super(TFont, self).__init__(*args, **kwargs)

    def newStandardGlyph(self, name, override=False, addUnicode=True,
                         asTemplate=False, width=500):
        if not override:
            if name in self:
                return None
        glyph = self.newGlyph(name)
        glyph.width = width
        # TODO: list ought to be changeable from AGL2UV
        if addUnicode:
            glyph.autoUnicodes()
        glyph.template = asTemplate
        return glyph

    # TODO: stop using that workaround now that we're ufo3
    def save(self, path=None, formatVersion=None):
        for glyph in self:
            if glyph.template:
                glyph.dirty = False
        super(TFont, self).save(path, formatVersion)


class TGlyph(Glyph):

    def __init__(self, *args, **kwargs):
        super(TGlyph, self).__init__(*args, **kwargs)
        self._template = False

    def _get_template(self):
        return self._template

    def _set_template(self, value):
        self._template = value

    template = property(
        _get_template, _set_template,
        doc="A boolean indicating whether the glyph is a template glyph.")

    def _set_dirty(self, value):
        BaseObject._set_dirty(self, value)
        if value:
            self.template = False

    dirty = property(BaseObject._get_dirty, _set_dirty)

    def autoUnicodes(self):
        app = QApplication.instance()
        if app.GL2UV is not None:
            GL2UV = app.GL2UV
        else:
            GL2UV = fontTools.agl.AGL2UV
        hexes = "ABCDEF0123456789"
        name = self.name
        if name in GL2UV:
            uni = GL2UV[name]
        elif (name.startswith("uni") and len(name) == 7 and
              all(c in hexes for c in name[3:])):
            uni = int(name[3:], 16)
        elif (name.startswith("u") and len(name) in (5, 7) and
              all(c in hexes for c in name[1:])):
            uni = int(name[1:], 16)
        else:
            return
        self.unicodes = [uni]


class TContour(Contour):

    def __init__(self, pointClass=None, **kwargs):
        if pointClass is None:
            pointClass = TPoint
        super(TContour, self).__init__(pointClass=pointClass, **kwargs)

    def drawPoints(self, pointPen):
        """
        Draw the contour with **pointPen**.
        """
        pointPen.beginPath()
        for point in self._points:
            pointPen.addPoint((point.x, point.y),
                              segmentType=point.segmentType,
                              smooth=point.smooth, name=point.name,
                              selected=point.selected)
        pointPen.endPath()

    def getPoint(self, index):
        return self[index % len(self)]


class TPoint(Point):
    __slots__ = ["_selected"]

    def __init__(self, pt, selected=False, **kwargs):
        super(TPoint, self).__init__(pt, **kwargs)
        self._selected = selected

    def _get_selected(self):
        return self._selected

    def _set_selected(self, value):
        self._selected = value

    selected = property(
        _get_selected, _set_selected,
        doc="A boolean indicating the selected state of the point.")


class GlyphSet(object):
    __slots__ = ["_name", "_glyphNames"]

    def __init__(self, glyphNames, name):
        self._name = str(name)
        self._glyphNames = glyphNames

    def _get_name(self):
        return self._name

    def _set_name(self, name):
        self._name = name

    name = property(_get_name, _set_name, doc="Glyph set name.")

    def _get_glyphNames(self):
        return self._glyphNames

    def _set_glyphNames(self, glyphNames):
        self._glyphNames = glyphNames

    glyphNames = property(_get_glyphNames, _set_glyphNames,
                          doc="List of glyph names.")