-
Notifications
You must be signed in to change notification settings - Fork 0
/
RoboCJKPreviewer.py
217 lines (190 loc) · 7.05 KB
/
RoboCJKPreviewer.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
import sys
import os
import objc
from AppKit import NSFormatter
from vanilla import *
import drawBot as db
from drawBot.ui.drawView import DrawView
from drawBot.drawBotDrawingTools import _drawBotDrawingTool
from drawBot.context.drawBotContext import DrawBotContext
try:
from rcjktools.project import RoboCJKProject
except ImportError:
libPath = os.path.join(os.path.dirname(os.path.abspath(__file__)), "Lib")
assert libPath not in sys.path
sys.path.append(libPath)
from rcjktools.project import RoboCJKProject
def ClassNameIncrementer(clsName, bases, dct):
orgName = clsName
counter = 0
while True:
try:
objc.lookUpClass(clsName)
except objc.nosuchclass_error:
break
counter += 1
clsName = orgName + str(counter)
return type(clsName, bases, dct)
class UnicodesFormatter(NSFormatter, metaclass=ClassNameIncrementer):
def stringForObjectValue_(self, unicodes):
return " ".join(f"U+{u:04X}" for u in unicodes)
# def getObjectValue_forString_errorDescription_(self, value, string, error):
# XXX
class RoboCJKPreviewer:
def __init__(self, rcjkProjectPath):
self.project = RoboCJKProject(rcjkProjectPath, decomposeClassicComponents=True)
self.glyphList = [
dict(glyphName=glyphName, unicode=unicodes)
for glyphName, unicodes in self.project.getGlyphNamesAndUnicodes().items()
]
self.glyphList.sort(key=lambda item: (item["unicode"], item["glyphName"]))
self.w = Window(
(1000, 400),
f"RoboCJK Previewer — {rcjkProjectPath}",
minSize=(1000, 400),
autosaveName="RoboCJKPreviewer",
)
self.w.findGlyphField = EditText(
(10, 10, 180, 20), callback=self.findGlyphFieldCallback
)
self.w.axisSlider = Slider(
(210, 8, 180, 20),
value=0,
minValue=0,
maxValue=1,
callback=self.axisSliderCallback,
)
top = 40
columnDescriptions = [
dict(title="glyph name", key="glyphName"),
dict(
title="unicode",
key="unicode",
formatter=UnicodesFormatter.alloc().init(),
),
]
self.w.characterGlyphList = List(
(0, top, 200, 0),
self.glyphList,
columnDescriptions=columnDescriptions,
allowsMultipleSelection=False,
allowsSorting=False,
showColumnTitles=False,
drawFocusRing=False,
selectionCallback=self.characterGlyphListSelectionChangedCallback,
)
self.w.deepComponentList = List(
(200, top, 200, 0),
[],
allowsMultipleSelection=False,
drawFocusRing=False,
selectionCallback=self.deepComponentListSelectionChangedCallback,
)
self.w.atomicElementList = List(
(400, top, 200, 0),
[],
allowsMultipleSelection=False,
drawFocusRing=False,
selectionCallback=self.atomicElementListSelectionChangedCallback,
)
self.w.dbView = DrawView((600, 0, 0, 0)) # The DrawBot PDF view
self.w.characterGlyphList.setSelection([])
self.w.open()
def findGlyphFieldCallback(self, sender):
pat = sender.get().lower()
if not pat:
self.w.characterGlyphList.set(self.glyphList)
else:
items = [
item for item in self.glyphList if pat in item["glyphName"].lower()
]
self.w.characterGlyphList.set(items)
self.w.characterGlyphList.setSelection([])
def characterGlyphListSelectionChangedCallback(self, sender):
self.updateCurrentGlyph()
deepComponents = [
dcName for dcName, atomicElements in self._currentGlyphComponents
]
self.w.deepComponentList.set(deepComponents)
self.w.deepComponentList.setSelection([])
self.w.atomicElementList.set([])
self.w.atomicElementList.setSelection([])
def updateCurrentGlyph(self):
sel = self.w.characterGlyphList.getSelection()
if sel:
glyphName = self.w.characterGlyphList[sel[0]]["glyphName"]
(
outline,
dcItems,
classicComponents,
width,
) = self.project.instantiateCharacterGlyph(
glyphName, location={"wght": self.w.axisSlider.get()}
)
assert not classicComponents
else:
outline = None
dcItems = []
self._currentGlyphOutline = outline
self._currentGlyphComponents = dcItems
def deepComponentListSelectionChangedCallback(self, sender):
sel = sender.getSelection()
if sel:
dcName, atomicElements = self._currentGlyphComponents[sel[0]]
aeNames = [aeName for aeName, aeOutline in atomicElements]
self.w.atomicElementList.set(aeNames)
else:
self.w.atomicElementList.set([])
self.w.atomicElementList.setSelection([])
def atomicElementListSelectionChangedCallback(self, sender):
self.drawCurrentGlyph()
self.displayDrawing()
def axisSliderCallback(self, sender):
self.updateCurrentGlyph()
self.drawCurrentGlyph()
self.displayDrawing()
def drawCurrentGlyph(self):
db.newDrawing()
db.translate(100, 100)
db.scale(0.8)
db.fill(None)
db.stroke(0.2, 0.3, 1)
db.rect(0, 0, 1000, 1000)
db.stroke(None)
db.translate(
0, 120
) # Baseline at 120 from the bottom of the Ideographic Em Square
db.fill(0, 1, 0, 0.3)
db.stroke(0)
db.lineJoin("round")
dcSelection = set(self.w.deepComponentList.getSelection())
aeSelection = set(self.w.atomicElementList.getSelection())
if self._currentGlyphOutline is not None:
drawOutline(self._currentGlyphOutline)
if self._currentGlyphComponents:
for dcIndex, (dcName, atomicElements) in enumerate(
self._currentGlyphComponents
):
for aeIndex, (aeName, atomicOutline) in enumerate(atomicElements):
if dcIndex in dcSelection:
if aeIndex in aeSelection:
db.fill(1, 0, 0, 0.3)
else:
db.fill(0, 0, 1, 0.3)
else:
db.fill(0, 0.3)
drawOutline(atomicOutline)
db.endDrawing()
def displayDrawing(self):
context = DrawBotContext()
_drawBotDrawingTool._drawInContext(context)
self.w.dbView.setPDFDocument(context.getNSPDFDocument())
def drawOutline(outline):
bez = db.BezierPath()
outline.drawPoints(bez)
db.drawPath(bez)
if __name__ == "__main__":
from vanilla.dialogs import getFolder
result = getFolder("Please select a .rcjk project folder")
if result:
RoboCJKPreviewer(result[0])