aboutsummaryrefslogtreecommitdiffstats
path: root/Lib/defconQt/featureTextEditor.py
blob: cadb9d517f97c782a5976f6146aca4403e63506a (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
from PyQt5.QtCore import QFile, QRegExp, Qt
from PyQt5.QtGui import QColor, QFont, QKeySequence, QPainter, QSyntaxHighlighter, QTextCharFormat, QTextCursor
from PyQt5.QtWidgets import (QApplication, QFileDialog, QMainWindow, QMenu,
        QMessageBox, QPlainTextEdit, QWidget)

# TODO: implement search and replace
class MainEditWindow(QMainWindow):
    def __init__(self, font=None, parent=None):
        super(MainEditWindow, self).__init__(parent)

        self.font = font
        self.setupFileMenu()
        self.editor = TextEditor(self.font.features.text, self)
        # arm `undoAvailable` to `setWindowModified`
        self.editor.setFileChangedCallback(self.setWindowModified)
        self.resize(600, 500)

        self.setCentralWidget(self.editor)
        self.setWindowTitle("Font features", self.font)

    def setWindowTitle(self, title, font):
        if font is not None: puts = "[*]%s%s %s" % (title, self.font.info.familyName, self.font.info.styleName)
        else: puts = "[*]%s" % title
        super(MainEditWindow, self).setWindowTitle(puts)

    def closeEvent(self, event):
        if self.editor.document().isModified():
            closeDialog = QMessageBox(QMessageBox.Question, "Me", "Save your changes?",
                  QMessageBox.Save | QMessageBox.Discard | QMessageBox.Cancel, self)
            closeDialog.setInformativeText("Your changes will be lost if you don’t save them.")
            closeDialog.setModal(True)
            ret = closeDialog.exec_()
            if ret == QMessageBox.Save:
                self.save()
                event.accept()
            elif ret == QMessageBox.Discard:
                event.accept()
            else:
                event.ignore()

    def reload(self):
        self.font.reloadFeatures()
        self.editor.setPlainText(self.font.features.text)

    def save(self):
        self.editor.write(self.font.features)

    def setupFileMenu(self):
        fileMenu = QMenu("&File", self)
        self.menuBar().addMenu(fileMenu)

        fileMenu.addAction("&Save...", self.save, QKeySequence.Save)
        fileMenu.addSeparator()
        fileMenu.addAction("Reload from UFO", self.reload)
        fileMenu.addAction("E&xit", self.close, QKeySequence.Quit)

class LineNumberArea(QWidget):
    def __init__(self, editor):
        super(LineNumberArea, self).__init__(editor)

    def sizeHint(self):
        return QSize(self.parent().lineNumberAreaWidth(), 0)

    def paintEvent(self, event):
        self.parent().lineNumberAreaPaintEvent(event)

class TextEditor(QPlainTextEdit):
    def __init__(self, text=None, parent=None):
        super(TextEditor, self).__init__(parent)
        # https://gist.github.com/murphyrandle/2921575
        font = QFont('Roboto Mono', 10)
        font.setFixedPitch(True)
        self.setFont(font)

        self._indent = "    "
        self.highlighter = Highlighter(self.document())
        self.lineNumbers = LineNumberArea(self)
        self.setPlainText(text)
        # kick-in geometry update before arming signals bc blockCountChanged
        # won't initially trigger if text is None or one-liner.
        self.updateLineNumberAreaWidth()
        self.blockCountChanged.connect(self.updateLineNumberAreaWidth)
        self.updateRequest.connect(self.updateLineNumberArea)

    def setFontParams(self, family='Roboto Mono', ptSize=10, isMono=True):
        font = QFont(family, ptSize)
        font.setFixedPitch(isMono)
        self.setFont(font)

    # TODO: add way to unset callback?
    def setFileChangedCallback(self, fileChangedCallback):
        self.undoAvailable.connect(fileChangedCallback)

    def write(self, features):
        features.text = self.toPlainText()

    def lineNumberAreaPaintEvent(self, event):
        painter = QPainter(self.lineNumbers)
        painter.fillRect(event.rect(), QColor(230, 230, 230))
        d = event.rect().topRight()
        a = event.rect().bottomRight()
        painter.setPen(Qt.darkGray)
        painter.drawLine(d.x(), d.y(), a.x(), a.y())
        painter.setPen(QColor(100, 100, 100))
        painter.setFont(self.font())

        block = self.firstVisibleBlock()
        blockNumber = block.blockNumber();
        top = int(self.blockBoundingGeometry(block).translated(self.contentOffset()).top())
        bottom = top + int(self.blockBoundingRect(block).height())

        while block.isValid() and top <= event.rect().bottom():
            if block.isVisible() and bottom >= event.rect().top():
                number = str(blockNumber + 1)
                painter.drawText(4, top, self.lineNumbers.width() - 8,
                    self.fontMetrics().height(),
                    Qt.AlignRight, number)
            block = block.next()
            top = bottom
            bottom = top + int(self.blockBoundingRect(block).height())
            blockNumber += 1

    def lineNumberAreaWidth(self):
        digits = 1
        top = max(1, self.blockCount())
        while top >= 10:
            top /= 10
            digits += 1
        # Avoid too frequent geometry changes
        if digits < 3: digits = 3
        return 10 + self.fontMetrics().width('9') * digits

    def updateLineNumberArea(self, rect, dy):
        if dy:
            self.lineNumbers.scroll(0, dy);
        else:
            self.lineNumbers.update(0, rect.y(),
                self.lineNumbers.width(), rect.height())

        if rect.contains(self.viewport().rect()):
            self.updateLineNumberAreaWidth(0)

    def updateLineNumberAreaWidth(self, newBlockCount=None):
        self.setViewportMargins(self.lineNumberAreaWidth(), 0, 0, 0)

    def resizeEvent(self, event):
        super(TextEditor, self).resizeEvent(event)
        cr = self.contentsRect()
        self.lineNumbers.setGeometry(cr.left(), cr.top(), self.lineNumberAreaWidth(), cr.height())

    def findLineIndentLevel(self, cursor):
        indent = 0
        cursor.select(QTextCursor.LineUnderCursor)
        lineLength = len(cursor.selectedText()) // len(self._indent)
        cursor.movePosition(QTextCursor.StartOfLine)
        while lineLength > 0:
            cursor.movePosition(QTextCursor.NextCharacter, QTextCursor.KeepAnchor, len(self._indent))
            if cursor.selectedText() == self._indent:
                indent += 1
            else: break
            # Now move the anchor back to the position()
            #cursor.movePosition(QTextCursor.NoMove) # shouldn't NoMove work here?
            cursor.setPosition(cursor.position())
            lineLength -= 1
        cursor.movePosition(QTextCursor.EndOfLine)
        return indent

    def keyPressEvent(self, event):
        key = event.key()
        if key == Qt.Key_Return:
            cursor = self.textCursor()
            indentLvl = self.findLineIndentLevel(cursor)
            newBlock = False

            cursor.movePosition(QTextCursor.PreviousCharacter, QTextCursor.KeepAnchor)
            if cursor.selectedText() == '{':
                # We don't add a closing tag if there is text right below with the same
                # indentation level because in that case the user might just be looking
                # to add a new line
                ok = cursor.movePosition(QTextCursor.Down)
                if ok:
                    downIndentLvl = self.findLineIndentLevel(cursor)
                    cursor.select(QTextCursor.LineUnderCursor)
                    if cursor.selectedText().strip() == '' or downIndentLvl <= indentLvl:
                        newBlock = True
                    cursor.movePosition(QTextCursor.Up)
                else: newBlock = True
                indentLvl += 1

            cursor.select(QTextCursor.LineUnderCursor)
            if newBlock:
                txt = cursor.selectedText().lstrip(" ").split(" ")
                if len(txt) > 1:
                    if len(txt) < 3 and txt[-1][-1] == '{':
                        feature = txt[-1][:-1]
                    else:
                        feature = txt[1]
                else:
                    feature = None
            cursor.movePosition(QTextCursor.EndOfLine)

            super(TextEditor, self).keyPressEvent(event)
            newLineSpace = "".join(self._indent for _ in range(indentLvl))
            cursor.insertText(newLineSpace)
            if newBlock:
                super(TextEditor, self).keyPressEvent(event)
                newLineSpace = "".join((newLineSpace[:-len(self._indent)], "} ", feature, ";"))
                cursor.insertText(newLineSpace)
                cursor.movePosition(QTextCursor.Up)
                cursor.movePosition(QTextCursor.EndOfLine)
                self.setTextCursor(cursor)
        elif key == Qt.Key_Tab:
            cursor = self.textCursor()
            cursor.insertText(self._indent)
        elif key == Qt.Key_Backspace:
            cursor = self.textCursor()
            cursor.movePosition(QTextCursor.PreviousCharacter, QTextCursor.KeepAnchor,
                  len(self._indent))
            if cursor.selectedText() == self._indent:
                cursor.removeSelectedText()
            else:
                super(TextEditor, self).keyPressEvent(event)
        else:
            super(TextEditor, self).keyPressEvent(event)

keywordPatterns = ["\\bAscender\\b", "\\bAttach\\b", "\\bCapHeight\\b", "\\bCaretOffset\\b", "\\bCodePageRange\\b",
    "\\bDescender\\b", "\\bFontRevision\\b", "\\bGlyphClassDef\\b", "\\bHorizAxis.BaseScriptList\\b",
    "\\bHorizAxis.BaseTagList\\b", "\\bHorizAxis.MinMax\\b", "\\bIgnoreBaseGlyphs\\b", "\\bIgnoreLigatures\\b",
    "\\bIgnoreMarks\\b", "\\bLigatureCaretByDev\\b", "\\bLigatureCaretByIndex\\b", "\\bLigatureCaretByPos\\b",
    "\\bLineGap\\b", "\\bMarkAttachClass\\b", "\\bMarkAttachmentType\\b", "\\bNULL\\b", "\\bPanose\\b", "\\bRightToLeft\\b",
    "\\bTypoAscender\\b", "\\bTypoDescender\\b", "\\bTypoLineGap\\b", "\\bUnicodeRange\\b", "\\bUseMarkFilteringSet\\b",
    "\\bVendor\\b", "\\bVertAdvanceY\\b", "\\bVertAxis.BaseScriptList\\b", "\\bVertAxis.BaseTagList\\b",
    "\\bVertAxis.MinMax\\b", "\\bVertOriginY\\b", "\\bVertTypoAscender\\b", "\\bVertTypoDescender\\b",
    "\\bVertTypoLineGap\\b", "\\bXHeight\\b", "\\banchorDef\\b", "\\banchor\\b", "\\banonymous\\b", "\\banon\\b",
    "\\bby\\b", "\\bcontour\\b", "\\bcursive\\b", "\\bdevice\\b", "\\benumerate\\b", "\\benum\\b", "\\bexclude_dflt\\b",
    "\\bfeatureNames\\b", "\\bfeature\\b", "\\bfrom\\b", "\\bignore\\b", "\\binclude_dflt\\b", "\\binclude\\b",
    "\\blanguagesystem\\b", "\\blanguage\\b", "\\blookupflag\\b", "\\blookup\\b", "\\bmarkClass\\b", "\\bmark\\b",
    "\\bnameid\\b", "\\bname\\b", "\\bparameters\\b", "\\bposition\\b", "\\bpos\\b", "\\brequired\\b", "\\breversesub\\b",
    "\\brsub\\b", "\\bscript\\b", "\\bsizemenuname\\b", "\\bsubstitute\\b", "\\bsubtable\\b", "\\bsub\\b", "\\btable\\b",
    "\\buseExtension\\b", "\\bvalueRecordDef\\b", "\\bwinAscent\\b", "\\bwinDescent\\b"]

class Highlighter(QSyntaxHighlighter):
    def __init__(self, parent=None):
        super(Highlighter, self).__init__(parent)

        keywordFormat = QTextCharFormat()
        keywordFormat.setForeground(QColor(30, 150, 220))
        keywordFormat.setFontWeight(QFont.Bold)

        self.highlightingRules = [(QRegExp("(%s)" % ("|".join(keywordPatterns))), keywordFormat)]

        singleLineCommentFormat = QTextCharFormat()
        singleLineCommentFormat.setForeground(Qt.darkGray)
        self.highlightingRules.append((QRegExp("#[^\n]*"),
                singleLineCommentFormat))

        classFormat = QTextCharFormat()
        classFormat.setFontWeight(QFont.Bold)
        classFormat.setForeground(QColor(200, 50, 150))
        self.highlightingRules.append((QRegExp("@[A-Za-z0-9_.]+"),
                classFormat))

    def highlightBlock(self, text):
        for pattern, format in self.highlightingRules:
            expression = QRegExp(pattern)
            index = expression.indexIn(text)
            while index >= 0:
                length = expression.matchedLength()
                self.setFormat(index, length, format)
                index = expression.indexIn(text, index + length)

        self.setCurrentBlockState(0)