-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy patheqtexsvg.py
544 lines (440 loc) · 18.3 KB
/
eqtexsvg.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
eqtexsvg.py
An extension to convert LaTeX equation string into SVG path
This extension need, to work properly:
- a TeX/LaTeX distribution (MikTeX ...) with latex and dvips
Depending on which program you prefer:
- dvisvgm (http://dvisvgm.sourceforge.net/) version 0.8.3 or higher
or
- pstoedit (http://www.helga-glunz.homepage.t-online.de/pstoedit/)
Recent version can be downloaded via :
http://www.julienvitard.eu/
Copyright (C) 2006 - 2014 Julien Vitard, [email protected]
* 2015-02-25: fix Windows compatibility
* 2014-12-21: add command line capabilities
* 2011-05-17: changes pstoedit option -quiet
* 2011-05-16: inx file modification
* 2011-05-15: uname replacement by platform
* 2011-03-14: debug info option
* 2011-03-15: custom packages
* 2011-03-16: support of dvisvgm and dvips/pstoedit process
This program 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 2 of the License, or
(at your option) any later version.
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""
import os
import logging
import optparse
LOG = os.path.join(os.path.expanduser('~'), 'eqtexsvg.log')
logging.basicConfig(filename=LOG,
level=logging.DEBUG,
filemode='w',
datefmt='%d/%m/%Y %H:%M:%S',
format="%(asctime)s: %(message)s")
try:
import inkex
STANDALONE = False
except ImportError:
STANDALONE = True
class inkex:
class Effect:
current_layer = []
def __init__(self):
self.OptionParser = optparse.OptionParser()
def affect(self):
(self.options, self.args) = self.OptionParser.parse_args()
self.effect()
def debug(self, *args, **kwargs):
pass
import sys
import tempfile
from subprocess import Popen, PIPE
from StringIO import StringIO
import platform
def exec_cmd(cmd_line=None, debug=True):
"""Launch given command line (and report in log if debug)"""
clean = lambda x: '\n'.join([l for l in x.split('\n') if l != ""])
process = Popen(cmd_line, shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE)
(std_out, std_err) = process.communicate()
std_out = clean(std_out)
std_err = clean(std_err)
if debug:
logging.debug(cmd_line)
logging.debug('returncode: ' + str(process.returncode))
logging.debug('stderr:\n' + std_err)
logging.debug('stdout:\n' + std_out)
return (process.returncode, std_out, std_err)
class Equation:
"""Current LaTeX Equation"""
def __init__(self, param=None):
param = param or {}
self.document = None
self.formula = None
self.temp_dir = None
self.header = None
self.debug = False
self.output = param['output']
if 'debug' in param:
self.debug = param['debug']
inkscape_version = exec_cmd('inkscape -V', False)[1]
if self.debug:
logging.debug(inkscape_version)
# get Python informations system, release and machine informations
logging.debug("Python %s\nCompiler: %s\nBuild : %s" % (
platform.python_version(),
platform.python_compiler(),
' '.join(platform.python_build())))
logging.debug("Platform: %s" % (platform.platform()))
logging.debug("Current Working Directory: %s" % (os.getcwd()))
logging.debug("Current Extension Directory: %s" % (
os.path.abspath(__file__)))
try:
self.temp_dir = tempfile.mkdtemp('', 'inkscape-')
if self.debug:
logging.debug(self.temp_dir)
except:
if self.debug:
logging.debug('Temporary directory cannot be created')
sys.exit('Temporary directory cannot be created')
if 'document' in param:
self.document = param['document']
if 'formula' in param:
self.formula = param['formula']
if self.debug:
logging.debug(self.formula)
else:
if self.debug:
logging.debug('No formula detected')
sys.exit('Without formula, no equation will be generated')
if 'packages' in param:
self.pkgstring = param['packages']
else:
self.pkgstring = ""
self.file_ext = ['.tex', '.aux', '.log', '.dvi', '.out', '.err', '.ps']
self.file = 'eq'
self.svg = None
os.chdir(self.temp_dir)
self.file_tex = self.file + '.tex'
self.file_ps = self.file + '.ps'
self.file_dvi = self.file + '.dvi'
self.file_svg = self.file + '.svg'
self.file_out = self.file + '.out'
self.file_err = self.file + '.err'
self.latex = False
self.dvips = False
self.pstoedit = False
self.dvisvgm = False
self.process = True
def path_programs(self, cmd_line=None, code=True):
"""Try to launch given command line with pass return code"""
# convention ==0 : True, !=0 : False
# 'latex --version' ==0
# 'dvips -v' !=0
# 'pstoedit -v' !=0
# 'dvisvgm -V' ==0
program_name = cmd_line.split()[0]
try:
retcode = exec_cmd(cmd_line, self.debug)[0]
if ((retcode == 0) if code else retcode):
exec('self.' + program_name + ' = True')
if self.debug:
logging.debug(program_name + " OK")
else:
exec('self.' + program_name + ' = False')
if self.debug:
logging.debug(program_name + " not OK")
except OSError, err:
if self.debug:
logging.debug(program_name + " failed: " + err)
sys.stderr.write(program_name + " failed:" + err)
sys.exit(1)
def parse_pkgs(self):
"""Add custom packages to TeX source"""
header = ""
if self.pkgstring != "":
pkglist = self.pkgstring.replace(" ", "").split(",")
for pkg in pkglist:
if pkg:
header += "\\usepackage{%s}\n" % pkg
else:
if self.debug:
logging.debug('No package')
self.header = header
if self.debug:
logging.debug('packages:\n' + self.header)
def generate_tex(self):
"""Generate the LaTeX Equation file"""
self.parse_pkgs()
texstring = "%% processed with eqtexsvg.py\n"
texstring += "\\documentclass{article}\n"
texstring += "\\usepackage{amsmath}\n"
texstring += "\\usepackage{amssymb}\n"
texstring += "\\usepackage{amsfonts}\n"
texstring += self.header
texstring += "\\thispagestyle{empty}\n"
texstring += "\\begin{document}\n"
# Todo1: Check the argument carefully, especially Math delimiters
# delimiters : ('$','$')('$$','$$')('\(','\)')('\[','\]')
# ('\begin{equation*}', '\end{equation*}')
# ('\begin{equation}', '\end{equation}')
# ('\begin{math}', '\end{math}')
# ('\begin{displaymath}', '\end{displaymath}')
# ...
texstring += self.formula
texstring += "\n\\end{document}\n"
if self.debug:
logging.debug('\n' + texstring)
try:
tex = open(self.file_tex, 'w')
tex.write(texstring)
tex.close()
if self.debug:
logging.debug('TEX file generated')
except IOError:
if self.debug:
logging.debug('TEX file not generated')
def generate_dvi(self):
"""Generate the DVI Equation file"""
cmd_line = 'latex '
cmd_line += '-output-directory="%s"' % (self.temp_dir)
cmd_line += ' -halt-on-error '
cmd_line += '"%s"' % (self.file_tex)
retcode = exec_cmd(cmd_line, self.debug)[0]
if retcode:
if self.debug:
logging.debug('DVI file not generated with latex')
sys.exit('Problem to generate DVI file')
else:
if self.debug:
logging.debug('DVI file generated with latex')
def generate_svg(self):
"""Generate the SVG Equation string/file"""
if self.process:
# Use dvisvgm
cmd_line = 'dvisvgm '
cmd_line += '-v0 ' # set verbosity level to 0
cmd_line += '-a ' # trace all glyphs of bitmap fonts
cmd_line += '-n ' # draw glyphs by using path elements
cmd_line += '-s ' # write SVG output to stdout
cmd_line += '"%s"' % (self.file_dvi) # Input file
retcode, std_out, std_err = exec_cmd(cmd_line, self.debug)
# Get SVG from dvisvgm output
self.svg = std_out
if not retcode:
if self.debug:
logging.debug('SVG file generated with dvisvgm')
else:
if self.debug:
logging.debug('SVG file not generated with dvisvgm')
sys.exit('SVG file not generated with dvisvgm')
else:
# Use dvips to produce PS file
cmd_line = 'dvips '
cmd_line += '-q ' # Run quietly
cmd_line += '-f ' # Run as filter
cmd_line += '-E ' # Try to create EPSF
cmd_line += '-D 600 ' # Resolution
# cmd_line += '-y 1000 ' # Multiply by dvi magnification
cmd_line += '-o "%s" ' % (self.file_ps) # Output file
cmd_line += '"%s"' % (self.file_dvi) # Input file
retcode, std_out, std_err = exec_cmd(cmd_line, self.debug)
if not retcode:
if self.debug:
logging.debug('PS file generated with dvips')
else:
if self.debug:
logging.debug('PS file not generated with dvips')
sys.exit('PS file not generated with dvips')
# Use pstoedit to produce SVG file
cmd_line = 'pstoedit '
cmd_line += '-f plot-svg ' # svg via GNU libplot
cmd_line += '-dt ' # convert text to polygons
cmd_line += '-ssp ' # simulate subpaths
# cmd_line += '-quiet ' # quiet mode
cmd_line += '"%s" ' % (self.file_ps) # Input file
retcode, std_out, std_err = exec_cmd(cmd_line, self.debug)
# Get SVG from pstoedit output
self.svg = std_out
if not retcode:
if self.debug:
logging.debug('SVG file generated with pstoedit')
else:
if self.debug:
logging.debug('SVG file not generated with pstoedit')
sys.exit('SVG file not generated with pstoedit')
def import_svg(self):
"""Import the SVG Equation file into Current layer"""
svg_uri = inkex.NSS['svg']
xlink_uri = inkex.NSS['xlink']
if self.debug:
logging.debug('import_svg():\n' + self.svg + '\n')
try:
# parsing self.svg from file:
tree = inkex.etree.parse(StringIO(self.svg))
eq_tree = tree.getroot()
if self.debug:
logging.debug('SVG file imported from parse')
except Exception:
if self.debug:
logging.debug('SVG file not imported')
sys.exit('Problem to import svg string/file')
# Collect document ids
doc_ids = {}
doc_id_nodes = self.document.xpath('//@id')
for id_nodes in doc_id_nodes:
doc_ids[id_nodes] = 1
name = 'equation_00'
# Make sure that the id/name is unique
index = 0
while name in doc_ids:
name = 'equation_%02d' % index
index = index + 1
# Create new group node containing the equation
eqn = inkex.etree.Element('{%s}%s' % (svg_uri, 'g'))
eqn.set('id', name)
eqn.set('style', 'fill: black;')
eqn.set('title', str(self.formula))
if not self.process:
# Apply transform to pstoedit result
doc_width = inkex.unittouu(self.document.getroot().get('width'))
doc_height = inkex.unittouu(self.document.getroot().get('height'))
doc_sizeH = min(doc_width, doc_height)
doc_sizeW = max(doc_width, doc_height)
matrix_transform = 'matrix(1,0,0,-1,%f,%f)' % (-doc_sizeH * 0.2,
doc_sizeW * 0.65)
if self.debug:
logging.debug('Dimensions:\n'
+ ' W:' + str(doc_width)
+ ' H:' + str(doc_height)
+ ' sH:' + str(doc_sizeH)
+ ' sW:' + str(doc_sizeW))
logging.debug('Applying matrix: ' + matrix_transform)
eqn.set('transform', matrix_transform)
dic = {}
counter = 0
# Get the Ids from <defs/>
# And make unique Ids from name and counter
for elt in eq_tree:
if elt.tag == ('{%s}%s' % (svg_uri, 'defs')):
for subelt in elt:
dic[subelt.get('id')] = "%s_%02d" % (name, counter)
counter += 1
# Build new equation nodes
for elt in eq_tree:
eqn_elt = inkex.etree.SubElement(eqn, elt.tag)
if 'id' in elt.keys():
eqn_elt.set('id', name + '_' + elt.tag.split('}')[-1])
for subelt in elt:
eqn_subelt = inkex.etree.SubElement(eqn_elt, subelt.tag)
for key in subelt.keys():
eqn_subelt.set(key, subelt.attrib[key])
if 'id' in subelt.attrib:
eqn_subelt.set('id', dic[subelt.get('id')])
xlink = '{%s}%s' % (xlink_uri, 'href')
if xlink in subelt.attrib:
eqn_subelt.set(xlink,
'#' + dic[subelt.get(xlink).split("#")[-1]])
self.svg = eqn
def clean(self):
"""Clean all necessary file"""
for ext in self.file_ext:
try:
os.unlink(os.path.join(self.temp_dir, self.file + ext))
if self.debug:
logging.debug(self.file + ext + ' file deleted')
except OSError:
if self.debug:
logging.debug(self.file + ext + ' file not deleted')
try:
os.rmdir(self.temp_dir)
if self.debug:
logging.debug(self.temp_dir + ' is removed')
except OSError:
if self.debug:
logging.debug(self.temp_dir + 'cannot be removed')
def generate(self, standalone=False):
"""Generate SVG from LaTeX equation file"""
self.path_programs('latex --version', True)
self.path_programs('dvips -v', True)
self.path_programs('pstoedit -v', False)
self.path_programs('dvisvgm -V', True)
self.generate_tex()
self.generate_dvi()
if self.latex and self.dvisvgm: # and False:
self.process = True
if self.debug:
logging.debug('latex and dvisvgm process in use')
elif self.latex and self.dvips and self.pstoedit:
self.process = False
if self.debug:
logging.debug('latex, dvips and pstoedit process in use')
else:
if self.debug:
logging.debug('No process in use!')
sys.exit('No process in use!')
self.generate_svg()
if standalone:
with open(self.output, "w") as svg_file:
svg_file.write(self.svg)
else:
self.import_svg()
self.clean()
return self.svg
class InsertEquation(inkex.Effect):
"""Insert LaTeX Equation into the current Inscape instance"""
def __init__(self):
formula = '\(\displaystyle \lim_{n\\to \infty}\sum_{k=1}^n\\frac{1}{k^2}' \
+ '=\\frac{\pi^2}{6}\)'
inkex.Effect.__init__(self)
self.OptionParser.add_option(
'-f', '--formule', action='store', type='string',
dest='formula', help='LaTeX formula', default=formula,)
self.OptionParser.add_option(
"-p", "--packages", action="store", type="string",
dest="packages", help="Additional packages", default="",)
self.OptionParser.add_option(
"-o", "--output", action="store",
dest="output", help="output file", default="output.svg",)
if STANDALONE:
self.OptionParser.add_option(
"-d", "--debug", action="store_true",
dest="debug", help="Debug information", default=False,)
else:
self.OptionParser.add_option(
"-d", "--debug", action="store", type="inkbool",
dest="debug", help="Debug information", default=False,)
def effect(self):
"""Generate inline Equation"""
param = {
'formula': self.options.formula,
'document': getattr(self, 'document', None),
'packages': self.options.packages,
'debug': self.options.debug,
'output': self.options.output,
}
debug = self.options.debug
if debug:
logging.debug("Standalone Mode")
equation = Equation(param=param)
current_eq = equation.generate(standalone=STANDALONE)
if current_eq != None:
self.current_layer.append(current_eq)
if debug:
logging.debug('Equation added to current layer')
else:
if debug:
logging.debug('Equation not generated')
inkex.debug('No Equation was generated\n')
if __name__ == "__main__":
EFFECT = InsertEquation()
EFFECT.affect()