forked from ThunderGemios10/The-Super-Duper-Script-Editor-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
font_gen_widget.py
264 lines (205 loc) · 8.18 KB
/
font_gen_widget.py
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
################################################################################
### Copyright © 2012-2013 BlackDragonHunt
###
### This file is part of the Super Duper Script Editor.
###
### The Super Duper Script Editor is free software: you can redistribute it
### and/or modify it under the terms of the GNU General Public License as
### published by the Free Software Foundation, either version 3 of the License,
### or (at your option) any later version.
###
### The Super Duper Script Editor is distributed in the hope that it will be
### useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
### GNU General Public License for more details.
###
### You should have received a copy of the GNU General Public License
### along with the Super Duper Script Editor.
### If not, see <http://www.gnu.org/licenses/>.
################################################################################
from PyQt4 import Qt, QtGui, QtCore
from PyQt4.QtCore import pyqtSignal
from ui.fontgenwidget import Ui_FontGenWidget
import common
from font_generator import FontConfig, gen_font, FONT_TYPES
# A few escapes for our substitutions.
SUB_ESCAPES = [
("\\n", "\n"),
("\\r", "\r"),
("\\t", "\t"),
]
def unescape(text):
for escape in SUB_ESCAPES:
text = text.replace(escape[0], escape[1])
return text
def escape(text):
for escape in SUB_ESCAPES:
text = text.replace(escape[1], escape[0])
return text
class FontGenWidget(QtGui.QWidget):
### SIGNALS ###
modified = pyqtSignal()
def __init__(self, parent = None):
super(FontGenWidget, self).__init__(parent)
self.ui = Ui_FontGenWidget()
self.ui.setupUi(self)
self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
self.importing = True
self.ui.treeSubs.clear()
self.ui.treeSubs.header().setResizeMode(QtGui.QHeaderView.ResizeToContents)
self.ui.cboPenCap.clear()
self.ui.cboPenCap.addItem("Flat", Qt.Qt.FlatCap)
self.ui.cboPenCap.addItem("Square", Qt.Qt.SquareCap)
self.ui.cboPenCap.addItem("Round", Qt.Qt.RoundCap)
self.ui.cboPenJoin.clear()
self.ui.cboPenJoin.addItem("Miter", Qt.Qt.MiterJoin)
self.ui.cboPenJoin.addItem("Bevel", Qt.Qt.BevelJoin)
self.ui.cboPenJoin.addItem("Round", Qt.Qt.RoundJoin)
self.ui.cboPenJoin.addItem("SVG Miter", Qt.Qt.SvgMiterJoin)
self.font_type = FONT_TYPES.font01
self.importing = False
##############################################################################
### DATA FUNCTIONS
##############################################################################
def get_data(self):
return FontConfig(
family = self.font_name(),
size = self.font_size(),
weight = self.ui.spnFontWeight.value(),
x_offset = self.ui.spnXOffset.value(),
y_offset = self.ui.spnYOffset.value(),
x_margin = self.ui.spnXMargin.value(),
y_margin = self.ui.spnYMargin.value(),
y_shift = self.ui.spnYShift.value(),
pen_size = self.ui.spnPenSize.value(),
pen_cap = self.ui.cboPenCap.itemData(self.ui.cboPenCap.currentIndex()).toInt()[0],
pen_join = self.ui.cboPenJoin.itemData(self.ui.cboPenJoin.currentIndex()).toInt()[0],
use_pen = self.ui.chkTrace.isChecked(),
chars = common.qt_to_unicode(self.ui.txtChars.toPlainText()),
subs = self.get_subs(),
)
def import_data(self, config):
# In case we're coming from pickled data that might
# not have all the members we need.
temp_config = config
config = FontConfig()
config.__dict__.update(temp_config.__dict__)
# So we're not signaling a bunch of refreshes.
self.importing = True
self.ui.cboFont .setCurrentFont(QtGui.QFont(config.family))
self.ui.spnFontSize .setValue(config.size)
self.ui.spnFontWeight .setValue(config.weight)
self.ui.spnXOffset .setValue(config.x_offset)
self.ui.spnYOffset .setValue(config.y_offset)
self.ui.spnXMargin .setValue(config.x_margin)
self.ui.spnYMargin .setValue(config.y_margin)
self.ui.spnYShift .setValue(config.y_shift)
self.ui.spnPenSize .setValue(config.pen_size)
self.ui.chkTrace .setChecked(config.use_pen)
self.ui.cboPenCap .setCurrentIndex(self.ui.cboPenCap.findData(config.pen_cap))
self.ui.cboPenJoin.setCurrentIndex(self.ui.cboPenJoin.findData(config.pen_join))
self.ui.txtChars.setPlainText(config.chars)
self.set_subs(config.subs)
# All done~
self.importing = False
self.font_changed()
self.update_preview()
def font_name(self):
return common.qt_to_unicode(self.ui.cboFont.currentFont().family(), normalize = False)
def font_size(self):
return self.ui.spnFontSize.value()
##############################################################################
### STUFF CHANGED
##############################################################################
def chars_changed(self):
if self.importing:
return
self.auto_update()
self.modified.emit()
def font_changed(self):
if self.importing:
return
cursor = self.ui.txtChars.textCursor()
self.ui.txtChars.selectAll()
self.ui.txtChars.setFontFamily(self.ui.cboFont.currentFont().family())
self.ui.txtChars.setFontPointSize(self.ui.spnFontSize.value())
self.ui.txtChars.setFontWeight(self.ui.spnFontWeight.value())
self.ui.txtChars.setTextCursor(cursor)
self.auto_update()
self.modified.emit()
def adv_changed(self):
if self.importing:
return
self.auto_update()
self.modified.emit()
def auto_update(self):
if self.ui.chkAutoRefresh.isChecked():
self.update_preview()
def update_preview(self):
if self.importing:
return
config = self.get_data()
config.chars = config.chars[:256]
font = gen_font([config], font_type = self.font_type, img_width = 256, draw_outlines = self.ui.chkBoundingBoxes.isChecked())
# font.save("debug/test")
qt_pixmap = QtGui.QPixmap.fromImage(font.trans.copy(0, 0, 256, 256))
self.ui.lblPreview.setPixmap(qt_pixmap)
##############################################################################
### SUBSTITUTION FUNCTIONS
##############################################################################
def add_sub(self, repl = u"?", repl_with = u"?"):
new_sub = QtGui.QTreeWidgetItem([repl, repl_with])
new_sub.setFlags(new_sub.flags() | Qt.Qt.ItemIsEditable)
self.ui.treeSubs.addTopLevelItem(new_sub)
if not self.importing:
self.auto_update()
self.modified.emit()
def del_sub(self):
rows = self.ui.treeSubs.selectionModel().selectedRows()
for row in rows:
self.ui.treeSubs.takeTopLevelItem(row.row())
if not self.importing:
self.auto_update()
self.modified.emit()
def sub_changed(self, item, col):
# Make sure everything's valid.
sub = common.qt_to_unicode(item.text(col))
sub = unescape(sub)
# The replacement column can only be single characters
if col == 0:
sub = sub[:1]
sub = escape(sub)
item.setText(col, sub)
if not self.importing:
self.auto_update()
self.modified.emit()
def set_subs(self, subs):
self.ui.treeSubs.clear()
for repl, repl_with in subs.items():
repl = escape(repl)
repl_with = escape(repl_with)
self.add_sub(repl, repl_with)
if not self.importing:
self.auto_update()
self.modified.emit()
def get_subs(self):
# return {u"\t": u' ', u"…": u"..."}
subs = {}
for i in range(self.ui.treeSubs.topLevelItemCount()):
item = self.ui.treeSubs.topLevelItem(i)
repl = unescape(common.qt_to_unicode(item.text(0)))
repl_with = unescape(common.qt_to_unicode(item.text(1)))
if len(repl) > 0:
subs[repl] = repl_with
return subs
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
app.connect(app, QtCore.SIGNAL("lastWindowClosed()"),
app,
QtCore.SLOT("quit()")
)
form = FontGenWidget()
form.show()
sys.exit(app.exec_())
### EOF ###