2020-10-29 21:37:01 -06:00
|
|
|
import uno
|
|
|
|
import unohelper
|
|
|
|
from com.sun.star.task import XJobExecutor
|
|
|
|
import easymacro2 as app
|
|
|
|
|
|
|
|
|
|
|
|
ID_EXTENSION = 'net.elmau.zaz.latex2svg'
|
|
|
|
SERVICE = ('com.sun.star.task.Job',)
|
2020-10-30 22:22:25 -06:00
|
|
|
TITLE = 'ZAZ Latex2SVG'
|
|
|
|
|
|
|
|
|
|
|
|
_ = app.install_locales(__file__)
|
|
|
|
|
|
|
|
|
|
|
|
TEMPLATE = """\documentclass{{article}}
|
|
|
|
\\usepackage[a5paper, landscape]{{geometry}}
|
|
|
|
\\usepackage{{xcolor}}
|
|
|
|
\\usepackage{{amssymb}}
|
|
|
|
\\usepackage{{amsmath}}
|
|
|
|
\pagestyle{{empty}}
|
|
|
|
\\begin{{document}}
|
2020-11-01 23:30:58 -06:00
|
|
|
\\begingroup
|
|
|
|
\huge
|
2020-10-30 22:22:25 -06:00
|
|
|
|
|
|
|
\[ {} \]
|
|
|
|
|
2020-11-01 23:30:58 -06:00
|
|
|
\endgroup
|
2020-10-30 22:22:25 -06:00
|
|
|
\end{{document}}
|
|
|
|
"""
|
2020-11-01 19:18:19 -06:00
|
|
|
# ~ TEMPLATE = """\documentclass{{standalone}}
|
|
|
|
# ~ \\usepackage[a5paper, landscape]{{geometry}}
|
|
|
|
# ~ \\usepackage{{xcolor}}
|
|
|
|
# ~ \\usepackage{{amssymb}}
|
|
|
|
# ~ \\usepackage{{amsmath}}
|
|
|
|
# ~ \\usepackage{{tikz}}
|
|
|
|
# ~ \pagestyle{{empty}}
|
|
|
|
# ~ \\begin{{document}}
|
|
|
|
# ~ \\begin{{tikzpicture}}
|
|
|
|
|
2020-11-01 19:29:17 -06:00
|
|
|
# ~ \\node at (0, 0) {{
|
|
|
|
# ~ \\begin{{equation*}}
|
|
|
|
|
2020-11-01 19:18:19 -06:00
|
|
|
# ~ \[ {} \]
|
|
|
|
|
2020-11-01 19:29:17 -06:00
|
|
|
# ~ \end{{equation*}}
|
|
|
|
# ~ }};
|
|
|
|
|
2020-11-01 19:18:19 -06:00
|
|
|
# ~ \end{{tikzpicture}}
|
|
|
|
# ~ \end{{document}}
|
|
|
|
# ~ """
|
2020-10-30 22:22:25 -06:00
|
|
|
|
|
|
|
|
|
|
|
class Controllers(object):
|
|
|
|
|
|
|
|
def __init__(self, dlg):
|
|
|
|
self.d = dlg
|
2020-11-01 23:30:58 -06:00
|
|
|
self._path = ''
|
2020-10-30 22:22:25 -06:00
|
|
|
|
2020-10-31 22:45:30 -06:00
|
|
|
def cmd_close_action(self, event):
|
|
|
|
self.d.close()
|
|
|
|
return
|
|
|
|
|
2020-11-01 23:30:58 -06:00
|
|
|
@app.catch_exception
|
2020-10-30 22:22:25 -06:00
|
|
|
def cmd_preview_action(self, event):
|
2020-11-01 23:30:58 -06:00
|
|
|
code = self.d.text.value
|
|
|
|
if not code:
|
2020-10-30 22:22:25 -06:00
|
|
|
msg = _('Write some code')
|
|
|
|
app.errorbox(msg)
|
|
|
|
return
|
|
|
|
|
2020-11-01 23:30:58 -06:00
|
|
|
self._path = _latex_to_svg(code)
|
|
|
|
self.d.image.value = self._path
|
2020-10-30 22:22:25 -06:00
|
|
|
return
|
2020-10-29 21:37:01 -06:00
|
|
|
|
2020-10-31 22:45:30 -06:00
|
|
|
def cmd_insert_action(self, event):
|
2020-10-31 22:48:02 -06:00
|
|
|
data = self.d.text.value
|
|
|
|
if not data:
|
|
|
|
msg = _('Write some code')
|
|
|
|
app.errorbox(msg)
|
|
|
|
return
|
|
|
|
|
2020-10-31 22:45:30 -06:00
|
|
|
msg = _('You shure insert this equation')
|
|
|
|
if not app.question(msg):
|
|
|
|
return
|
|
|
|
|
2020-11-01 23:30:58 -06:00
|
|
|
app.selection.insert_image(self._path, {})
|
2020-10-31 22:45:30 -06:00
|
|
|
self.d.close()
|
|
|
|
return
|
|
|
|
|
2020-10-29 21:37:01 -06:00
|
|
|
|
2020-11-01 23:30:58 -06:00
|
|
|
def _latex_to_svg(code):
|
|
|
|
NAME = 'temp'
|
|
|
|
data = TEMPLATE.format(code)
|
|
|
|
dt = app.paths.dir_tmp()
|
|
|
|
path_tex = app._P.join(dt.name, f'{NAME}.tex')
|
|
|
|
path_pdf = app._P.join(dt.name, f'{NAME}.pdf')
|
|
|
|
path_svg = app._P.join(app._P.temp_dir, f'{NAME}.svg')
|
|
|
|
|
|
|
|
app.paths.save(path_tex, data)
|
|
|
|
cmd = f'pdflatex --interaction=batchmode -output-directory="{dt.name}" "{path_tex}"'
|
|
|
|
app.run(cmd)
|
|
|
|
cmd = f'pdfcrop "{path_pdf}" "{path_pdf}"'
|
|
|
|
app.run(cmd)
|
|
|
|
cmd = f'pdf2svg "{path_pdf}" "{path_svg}"'
|
|
|
|
app.run(cmd)
|
|
|
|
|
|
|
|
if not app.paths.exists(path_svg):
|
|
|
|
path_svg = ''
|
|
|
|
dt.cleanup()
|
|
|
|
return path_svg
|
|
|
|
|
|
|
|
|
2020-10-29 21:37:01 -06:00
|
|
|
class ZAZLaTex2SVG(unohelper.Base, XJobExecutor):
|
2020-10-30 22:22:25 -06:00
|
|
|
NAME = 'temp'
|
|
|
|
_msg1 = _('Not found')
|
|
|
|
_msg2 = _('Found')
|
2020-10-29 21:37:01 -06:00
|
|
|
|
|
|
|
def __init__(self, ctx):
|
|
|
|
self.ctx = ctx
|
|
|
|
|
2020-10-30 22:22:25 -06:00
|
|
|
def trigger(self, args=''):
|
2020-10-29 21:37:01 -06:00
|
|
|
if args == 'app':
|
|
|
|
self._app()
|
|
|
|
return
|
|
|
|
|
2020-10-30 22:22:25 -06:00
|
|
|
if args == 'dlg':
|
|
|
|
self._dlg()
|
|
|
|
return
|
|
|
|
|
2020-10-29 21:37:01 -06:00
|
|
|
self._from_selection()
|
|
|
|
return
|
|
|
|
|
|
|
|
def _app(self):
|
2020-10-30 22:22:25 -06:00
|
|
|
result = self._msg1
|
2020-10-29 21:37:01 -06:00
|
|
|
if app.paths.exists_app('pdflatex'):
|
2020-10-30 22:22:25 -06:00
|
|
|
result = self._msg2
|
2020-10-29 21:37:01 -06:00
|
|
|
msg = f'pdflatex = {result}\n'
|
|
|
|
|
2020-10-30 22:22:25 -06:00
|
|
|
result = self._msg1
|
2020-10-29 21:37:01 -06:00
|
|
|
if app.paths.exists_app('pdfcrop'):
|
2020-10-30 22:22:25 -06:00
|
|
|
result = self._msg2
|
2020-10-29 21:37:01 -06:00
|
|
|
msg += f'pdfcrop = {result}\n'
|
|
|
|
|
2020-10-30 22:22:25 -06:00
|
|
|
result = self._msg1
|
2020-10-29 21:37:01 -06:00
|
|
|
if app.paths.exists_app('pdf2svg'):
|
2020-10-30 22:22:25 -06:00
|
|
|
result = self._msg2
|
2020-10-29 21:37:01 -06:00
|
|
|
msg += f'pdf2svg = {result}\n\n'
|
|
|
|
|
2020-10-30 22:22:25 -06:00
|
|
|
msg += _('Not used, if not found some application.')
|
2020-10-29 21:37:01 -06:00
|
|
|
|
|
|
|
app.msgbox(msg)
|
|
|
|
return
|
|
|
|
|
2020-10-30 22:22:25 -06:00
|
|
|
def _dlg(self):
|
|
|
|
dlg = self._create_dialog()
|
|
|
|
dlg.open()
|
|
|
|
return
|
|
|
|
|
|
|
|
def _create_dialog(self):
|
|
|
|
args = {
|
|
|
|
'Name': 'dialog',
|
|
|
|
'Title': TITLE,
|
|
|
|
'Width': 270,
|
|
|
|
'Height': 250,
|
|
|
|
}
|
|
|
|
dlg = app.create_dialog(args)
|
|
|
|
dlg.id = ID_EXTENSION
|
|
|
|
dlg.events = Controllers
|
|
|
|
|
|
|
|
args = {
|
|
|
|
'Type': 'Label',
|
|
|
|
'Name': 'lbl_code',
|
|
|
|
'Label': _('Latex code'),
|
|
|
|
'Width': 70,
|
2020-11-01 23:30:58 -06:00
|
|
|
'Height': 10,
|
2020-10-30 22:22:25 -06:00
|
|
|
'X': 10,
|
|
|
|
'Y': 5,
|
|
|
|
'VerticalAlign': 1,
|
|
|
|
}
|
|
|
|
dlg.add_control(args)
|
|
|
|
|
|
|
|
args = {
|
|
|
|
'Type': 'Text',
|
|
|
|
'Name': 'text',
|
|
|
|
'Width': 250,
|
|
|
|
'Height': 75,
|
|
|
|
'MultiLine': True,
|
|
|
|
'VScroll': True,
|
|
|
|
}
|
|
|
|
dlg.add_control(args)
|
|
|
|
|
|
|
|
args = {
|
|
|
|
'Type': 'Button',
|
|
|
|
'Name': 'cmd_preview',
|
|
|
|
'Label': _('Preview'),
|
|
|
|
'Width': 70,
|
|
|
|
'Height': 15,
|
|
|
|
'ImageURL': 'view.png',
|
|
|
|
'ImagePosition': 1,
|
|
|
|
}
|
|
|
|
dlg.add_control(args)
|
|
|
|
|
2020-11-01 23:30:58 -06:00
|
|
|
args = {
|
|
|
|
'Type': 'Image',
|
|
|
|
'Name': 'image',
|
|
|
|
'Width': 250,
|
|
|
|
'Height': 100,
|
|
|
|
}
|
|
|
|
dlg.add_control(args)
|
|
|
|
|
2020-10-30 22:22:25 -06:00
|
|
|
args = {
|
|
|
|
'Type': 'Button',
|
|
|
|
'Name': 'cmd_insert',
|
|
|
|
'Label': _('Insert'),
|
|
|
|
'Width': 70,
|
|
|
|
'Height': 15,
|
|
|
|
'ImageURL': 'insert.png',
|
|
|
|
'ImagePosition': 1,
|
|
|
|
}
|
|
|
|
dlg.add_control(args)
|
|
|
|
args = {
|
|
|
|
'Type': 'Button',
|
|
|
|
'Name': 'cmd_close',
|
|
|
|
'Label': _('Close'),
|
|
|
|
'Width': 70,
|
|
|
|
'Height': 15,
|
|
|
|
'ImageURL': 'close.png',
|
|
|
|
'ImagePosition': 1,
|
|
|
|
}
|
|
|
|
dlg.add_control(args)
|
|
|
|
|
|
|
|
dlg.text.move(dlg.lbl_code)
|
|
|
|
dlg.cmd_preview.move(dlg.text, center=True)
|
2020-11-01 23:30:58 -06:00
|
|
|
dlg.image.move(dlg.cmd_preview, center=True)
|
|
|
|
dlg.cmd_insert.move(dlg.image)
|
|
|
|
dlg.cmd_close.move(dlg.image)
|
2020-10-30 22:22:25 -06:00
|
|
|
controls = (dlg.cmd_insert, dlg.cmd_close)
|
|
|
|
dlg.center(controls)
|
|
|
|
|
|
|
|
return dlg
|
|
|
|
|
2020-10-29 21:37:01 -06:00
|
|
|
def _from_selection(self):
|
|
|
|
doc = app.active
|
|
|
|
sel = doc.selection
|
|
|
|
if doc.type == 'writer':
|
|
|
|
sel = sel[0]
|
|
|
|
data = sel.value
|
|
|
|
|
2020-10-30 22:22:25 -06:00
|
|
|
data = TEMPLATE.format(data)
|
|
|
|
dt = app.paths.dir_tmp()
|
|
|
|
path_tex = app._P.join(dt.name, f'{self.NAME}.tex')
|
|
|
|
path_pdf = app._P.join(dt.name, f'{self.NAME}.pdf')
|
|
|
|
path_svg = app._P.join(dt.name, f'{self.NAME}.svg')
|
2020-10-29 21:37:01 -06:00
|
|
|
|
|
|
|
app.paths.save(path_tex, data)
|
2020-10-30 22:22:25 -06:00
|
|
|
cmd = f'pdflatex --interaction=batchmode -output-directory="{dt.name}" "{path_tex}"'
|
2020-10-29 21:37:01 -06:00
|
|
|
app.run(cmd)
|
2020-10-30 22:22:25 -06:00
|
|
|
cmd = f'pdfcrop "{path_pdf}" "{path_pdf}"'
|
2020-10-29 21:37:01 -06:00
|
|
|
app.run(cmd)
|
2020-11-01 19:29:17 -06:00
|
|
|
# ~ app.paths.copy(path_pdf, '/home/mau')
|
2020-10-30 22:22:25 -06:00
|
|
|
cmd = f'pdf2svg "{path_pdf}" "{path_svg}"'
|
2020-10-29 21:37:01 -06:00
|
|
|
app.run(cmd)
|
|
|
|
|
|
|
|
sel = sel.offset()
|
|
|
|
args = {}
|
|
|
|
if doc.type == 'writer':
|
|
|
|
args = {'Width': 5000, 'Height': 2000}
|
|
|
|
sel.insert_image(path_svg, args)
|
|
|
|
|
2020-10-30 22:22:25 -06:00
|
|
|
dt.cleanup()
|
2020-10-29 21:37:01 -06:00
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
g_ImplementationHelper = unohelper.ImplementationHelper()
|
|
|
|
g_ImplementationHelper.addImplementation(ZAZLaTex2SVG, ID_EXTENSION, SERVICE)
|