/
latex_eqn_preview_wx.py
executable file
·347 lines (295 loc) · 12.1 KB
/
latex_eqn_preview_wx.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#!/usr/bin/env python
import wx
#from scipy import *
import os, cPickle, pdb, sys, re
import time
import rwkmisc
#reload(rwkmisc)
if len(sys.argv) > 1:
headless = int(sys.argv[1])
else:
headless = 0
if headless:
frame_size = (400, 200)
text_ctrl_size = (380, 50)
sizer_rows = 2
else:
frame_size = (900, 250)
text_ctrl_size = (850, 50)
sizer_rows = 3
import rwkos
#reload(rwkos)
#from PIL import Image
import latex_dvi_png
keyMap = {}
import os, copy
import rwkos
cache_dir = latex_dvi_png.find_cache_dir()
from xdotool import send_xdotool_cmd, get_window_ids, \
windows_with_str_in_title, GIMP_windows, \
Untitled_windows, get_Untitled_GIMP, activate_window, \
latex_eqn_preview_id, set_window_focus, read_window_id
def gen_keymap():
keys = ("BACK", "TAB", "RETURN", "ESCAPE", "SPACE", "DELETE", "START",
"LBUTTON", "RBUTTON", "CANCEL", "MBUTTON", "CLEAR", "PAUSE",
"CAPITAL", "PRIOR", "NEXT", "END", "HOME", "LEFT", "UP", "RIGHT",
"DOWN", "SELECT", "PRINT", "EXECUTE", "SNAPSHOT", "INSERT", "HELP",
"NUMPAD0", "NUMPAD1", "NUMPAD2", "NUMPAD3", "NUMPAD4", "NUMPAD5",
"NUMPAD6", "NUMPAD7", "NUMPAD8", "NUMPAD9", "MULTIPLY", "ADD",
"SEPARATOR", "SUBTRACT", "DECIMAL", "DIVIDE", "F1", "F2", "F3", "F4",
"F5", "F6", "F7", "F8", "F9", "F10", "F11", "F12", "F13", "F14",
"F15", "F16", "F17", "F18", "F19", "F20", "F21", "F22", "F23", "F24",
"NUMLOCK", "SCROLL", "PAGEUP", "PAGEDOWN", "NUMPAD_SPACE",
"NUMPAD_TAB", "NUMPAD_ENTER", "NUMPAD_F1", "NUMPAD_F2", "NUMPAD_F3",
"NUMPAD_F4", "NUMPAD_HOME", "NUMPAD_LEFT", "NUMPAD_UP",
"NUMPAD_RIGHT", "NUMPAD_DOWN", "NUMPAD_PRIOR", "NUMPAD_PAGEUP",
"NUMPAD_NEXT", "NUMPAD_PAGEDOWN", "NUMPAD_END", "NUMPAD_BEGIN",
"NUMPAD_INSERT", "NUMPAD_DELETE", "NUMPAD_EQUAL", "NUMPAD_MULTIPLY",
"NUMPAD_ADD", "NUMPAD_SEPARATOR", "NUMPAD_SUBTRACT", "NUMPAD_DECIMAL",
"NUMPAD_DIVIDE")
for i in keys:
keyMap[getattr(wx, "WXK_"+i)] = i
for i in ("SHIFT", "ALT", "CONTROL", "MENU"):
keyMap[getattr(wx, "WXK_"+i)] = ''
def GetKeyPress(evt):
keycode = evt.GetKeyCode()
keyname = keyMap.get(keycode, None)
modifiers = ""
for mod, ch in ((evt.ControlDown(), 'Ctrl+'),
(evt.AltDown(), 'Alt+'),
(evt.ShiftDown(), 'Shift+'),
(evt.MetaDown(), 'Meta+')):
if mod:
modifiers += ch
if keyname is None:
if 27 < keycode < 256:
keyname = chr(keycode)
else:
keyname = "(%s)unknown" % keycode
return modifiers + keyname
#return keyname
#settingsname = 'pytexguisettings.pkl'
## def Load_PIL_and_Resize(pathin, size):
## img = Image.open(pathin)
## #img2 = copy.deepcopy(img)#return fullsize copy along with resized
## img.thumbnail(size, Image.CUBIC)#Image.ANTIALIAS)
## return img#, img2
## def PIL_to_wx(img):
## image = wx.EmptyImage(img.size[0],img.size[1])
## image.SetData(img.convert("RGB").tostring())
## return wx.BitmapFromImage(image)
class MyFrame(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, -1, title,
pos=(150, 500), size=frame_size)
# Create the menubar
menuBar = wx.MenuBar()
# and a menu
menu = wx.Menu()
# add an item to the menu, using \tKeyName automatically
# creates an accelerator, the third param is some help text
# that will show up in the statusbar
menu.Append(wx.ID_EXIT, "E&xit\tAlt-X", "Exit the application")
# bind the menu event to an event handler
self.Bind(wx.EVT_MENU, self.OnTimeToClose, id=wx.ID_EXIT)
# and put the menu on the menubar
menuBar.Append(menu, "&File")
self.SetMenuBar(menuBar)
self.p_re = re.compile('\$\$(.*?)\$\$')
#create top level controls (i.e. those not on a notebook page)
#create the main directory chooser
imageFile = os.path.join(cache_dir, 'temp_out1.png')
if not os.path.exists(imageFile):
pngpath = latex_dvi_png.eq_to_dvi_png('\v{x} = \\twobyone{1/3}{1}')
mysize = self.GetSize()
w, h = mysize
#w = 500
#h = 100
#pil = Load_PIL_and_Resize(imageFile, [int(0.95*w), int(0.7*h)])
#wxbmp = PIL_to_wx(pil)
png1 = wx.Image(imageFile, wx.BITMAP_TYPE_ANY)
pw, ph = png1.GetSize()
aspect_ratio = float(pw)/float(ph)
new_w = 400
new_h = 100
png1.Rescale(int(new_w), int(new_h))
wxbmp = png1.ConvertToBitmap()
self.bmp = wx.StaticBitmap(self, -1, wxbmp, \
(10 + wxbmp.GetWidth(), 5), \
(wxbmp.GetWidth(), \
wxbmp.GetHeight()))
if not headless:
png1 = wx.Image(imageFile, wx.BITMAP_TYPE_ANY)
pw, ph = png1.GetSize()
aspect_ratio = float(pw)/float(ph)
new_w = 400
new_h = 100
png1.Rescale(int(new_w), int(new_h))
wxbmp = png1.ConvertToBitmap()
self.bmp = wx.StaticBitmap(self, -1, wxbmp, \
(10 + wxbmp.GetWidth(), 5), \
(wxbmp.GetWidth(), \
wxbmp.GetHeight()))
#change the size if headless
self.text = wx.TextCtrl(self, -1, "", size=text_ctrl_size,
style = wx.TE_MULTILINE
|wx.WANTS_CHARS
#|wx.TE_CHARWRAP
|wx.TE_WORDWRAP
#| wx.TE_RICH
#| wx.TE_RICH2
)
myfont = wx.Font(18, wx.DEFAULT, wx.NORMAL, wx.NORMAL, \
False, u'Nimbus Roman No 9')
#self.bmp.SetSize([300, 100])
self.text.SetFont(myfont)
self.exitbutton = wx.Button(self, -1, "Exit")
self.refresh_button = wx.Button(self, -1, "Refresh")
self.eqn_button = wx.Button(self, -1, "Eqn. LaTeX")
self.tex_button = wx.Button(self, -1, "LaTeX")
buttonsizer = wx.FlexGridSizer(1, 4, 0, 0)
buttonsizer.Add(self.tex_button, 1, wx.ALL, 5)
buttonsizer.Add(self.eqn_button, 1, wx.ALL, 5)
buttonsizer.Add(self.refresh_button, 1, wx.ALL, 5)
buttonsizer.Add(self.exitbutton, 1, wx.ALL, 5)
mainsizer = wx.FlexGridSizer(sizer_rows, 1, 0, 0)
mainsizer.AddGrowableCol(0)
mainsizer.AddGrowableRow(0)
#add the controls to the main sizer
if not headless:
mainsizer.AddGrowableRow(1)
mainsizer.Add(self.bmp, 1, wx.EXPAND|wx.ALL|wx.ALIGN_RIGHT, 5)
mainsizer.Add(self.text, 1, wx.EXPAND|wx.ALL|wx.ALIGN_RIGHT, 5)
mainsizer.Add(buttonsizer, 1, wx.ALL|wx.ALIGN_RIGHT, 5)
self.Bind(wx.EVT_BUTTON, self.On_Tex_Button, self.tex_button)
self.Bind(wx.EVT_BUTTON, self.On_Eqn_Tex_Button, \
self.eqn_button)
self.Bind(wx.EVT_BUTTON, self.On_Refresh_Button,\
self.refresh_button)
self.Bind(wx.EVT_BUTTON, self.OnTimeToClose, self.exitbutton)
self.text.Bind(wx.EVT_KEY_DOWN, self.KeyPressed, self.text)
## self.startdir = os.getcwd()
## self.settingspath = os.path.join(self.startdir, settingsname)
## if self.startdir not in sys.path:
## sys.path.insert(0, self.startdir)
self.SetSizer(mainsizer)
## self.savepanellist = [self.texpypane, self.purelatexpane, self.pyppane, self.fancyhdrpane, self.wltpane]
#mainsizer.Fit(self)
self.Layout()
if not headless:
self.load_png(imageFile)
self.text.SetFocus()
self.my_id = None
def _size_png(self, png):
mysize = self.GetSize()
w, h = mysize
pw, ph = png.GetSize()
if float(ph) == 0.0:
aspect_ratio = float(pw)
else:
aspect_ratio = float(pw)/float(ph)
new_w = w*0.95
if aspect_ratio == 0:
new_h = new_w
else:
new_h = new_w/aspect_ratio
if new_h > 100:
new_h = 100
new_w = aspect_ratio*new_h
png.Rescale(int(new_w), int(new_h))
return png
def load_png(self, pngpath):
png1 = wx.Image(pngpath, wx.BITMAP_TYPE_ANY)
self._size_png(png1)
wxbmp = png1.ConvertToBitmap()
self.bmp.SetBitmap(wxbmp)
def clean_double_dollar_signs(self, text):
return self.p_re.sub(r'\n\\begin{equation*}\n\1\n\end{equation*}\n', \
text)
def _get_clean_text(self):
raw_text = self.text.GetValue()
print('raw_text = %s' % raw_text)
text = raw_text.replace('\n' ,' ')
text = self.clean_double_dollar_signs(text)
return text
def On_Tex_Button(self, evt):
"""Run LaTeX on the connents of the text control and show the
result in the png viewer."""
text = self._get_clean_text()
pngpath = latex_dvi_png.latex_to_dvi_png(text)
if not headless:
self.load_png(pngpath)
def On_Eqn_Tex_Button(self, evt):
text = self._get_clean_text()
pngpath = latex_dvi_png.eq_to_dvi_png(text)
if not headless:
self.load_png(pngpath)
def On_Refresh_Button(self, evt):
print('in On_Refresh_Button')
def KeyPressed(self, evt):
keycode = evt.GetKeyCode()
flist = [346]#seem to go in order 346 = f7
if keycode==92 and (evt.ControlDown() or evt.AltDown()):
#ctrl or alt \
if evt.AltDown():
print('AltDown = True')
self.On_Tex_Button(evt)
else:
self.On_Eqn_Tex_Button(evt)
GIMP_id = read_window_id()
if self.my_id is None:
self.my_id = latex_eqn_preview_id()
set_window_focus(GIMP_id)
time.sleep(0.5)
send_xdotool_cmd('key "F7"')
set_window_focus(self.my_id)
elif keycode==77 and evt.ControlDown():#ctrl + m
#new equation without clearing the text box
GIMP_id = read_window_id()
activate_window(GIMP_id)
time.sleep(0.25)
send_xdotool_cmd('key "shift+F8"')
elif keycode==78 and evt.ControlDown():#ctrl + n
#new equation and clear the text box
self.text.SetValue("")
GIMP_id = read_window_id()
activate_window(GIMP_id)
time.sleep(0.25)
send_xdotool_cmd('key "shift+F8"')
elif keycode==69 and evt.ControlDown():#ctrl + e
self.text.SetValue("")
elif keycode==344:#F5
untitled_GIMP_id = get_Untitled_GIMP()
if untitled_GIMP_id:
activate_window(untitled_GIMP_id)
send_xdotool_cmd('key "F11"')
return
print('keycode=%s' % keycode)
print('evt.ControlDown()=%s' % evt.ControlDown())
if keycode not in flist:
evt.Skip()
return
## print('keycode=%s' % keycode)
## print('type(keycode)=%s' % type(keycode))
## print('evt.ControlDown()=%s' % evt.ControlDown())
## print('evt.AltDown()=%s' % evt.AltDown())
## print('evt.ShiftDown()=%s' % evt.ShiftDown())
if evt.ControlDown():
self.On_Tex_Button(evt)
else:
self.On_Eqn_Tex_Button(evt)
def OnTimeToClose(self, evt):
"""Event handler for the button click."""
# self.SaveSettings()
print('closing...')
self.Close()
class MyApp(wx.App):
def OnInit(self):
frame = MyFrame(None, "LaTeX Equation Preview")
self.SetTopWindow(frame)
#print "Print statements go to this stdout window by default."
frame.Show(True)
return True
if __name__ == "__main__":
app = MyApp(redirect=False)
app.MainLoop()