Code

added a "selected only" checkbox for fixing some obscure usability issue
[inkscape.git] / share / extensions / eqtexsvg.py
1 #!/usr/bin/env python
2 # -*- coding: cp1252 -*-
3 """
4 eqtexsvg.py
5 functions for converting LaTeX equation string into SVG path
6 This extension need, to work properly:
7     - a TeX/LaTeX distribution (MiKTeX ...)
8     - pstoedit software: <http://www.pstoedit.net/pstoedit>
10 Copyright (C) 2006 Julien Vitard <julienvitard@gmail.com>
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
26 """
28 import inkex, os, tempfile, sys, xml.dom.minidom
30 def create_equation_tex(filename, equation):
31     tex = open(filename, 'w')
32     tex.write("""%% processed with eqtexsvg.py
33 \\documentclass{article}
34 \\usepackage{amsmath}
35 \\usepackage{amssymb}
36 \\usepackage{amsfonts}
38 \\thispagestyle{empty}
39 \\begin{document}
40 """)
41     tex.write(equation)
42     tex.write("\n\\end{document}\n")
43     tex.close()
45 def svg_open(self,filename):
46     doc_width = inkex.unittouu(self.document.documentElement.getAttribute('width'))
47     doc_height = inkex.unittouu(self.document.documentElement.getAttribute('height'))
48     doc_sizeH = min(doc_width,doc_height)
49     doc_sizeW = max(doc_width,doc_height)
51     def clone_and_rewrite(self, node_in):
52         if node_in.localName != 'svg':
53             node_out = self.document.createElement('svg:' + node_in.localName)
54             for i in range(0, node_in.attributes.length):
55                 name = node_in.attributes.item(i).name
56                 value = node_in.attributes.item(i).value
57                 node_out.setAttribute(name, value)
58         else:
59             node_out = self.document.createElement('svg:g')
60         for c in node_in.childNodes:
61             if c.localName in ('g', 'path', 'polyline', 'polygon'):
62                 child = clone_and_rewrite(self, c)
63                 if c.localName == 'g':
64                     child.setAttribute('transform','matrix('+str(doc_sizeH/700.)+',0,0,'+str(-doc_sizeH/700.)+','+str(-doc_sizeH*0.25)+','+str(doc_sizeW*0.75)+')')
65                 node_out.appendChild(child)
67         return node_out
69     doc = xml.dom.minidom.parse(filename)
70     svg = doc.getElementsByTagName('svg')[0]
71     group = clone_and_rewrite(self, svg)
72     self.current_layer.appendChild(group)
74 class EQTEXSVG(inkex.Effect):
75     def __init__(self):
76         inkex.Effect.__init__(self)
77         self.OptionParser.add_option("-f", "--formule",
78                         action="store", type="string",
79                         dest="formula", default=10.0,
80                         help="LaTeX formula")
81     def effect(self):
83         base_dir = tempfile.mkdtemp("", "inkscape-");
84         latex_file = os.path.join(base_dir, "eq.tex")
85         aux_file = os.path.join(base_dir, "eq.aux")
86         log_file = os.path.join(base_dir, "eq.log")
87         ps_file = os.path.join(base_dir, "eq.ps")
88         dvi_file = os.path.join(base_dir, "eq.dvi")
89         svg_file = os.path.join(base_dir, "eq.svg")
90         out_file = os.path.join(base_dir, "eq.out")
92         def clean():
93             os.remove(latex_file)
94             os.remove(aux_file)
95             os.remove(log_file)
96             os.remove(ps_file)
97             os.remove(dvi_file)
98             os.remove(svg_file)
99             os.remove(out_file)
100             os.rmdir(base_dir)
102         create_equation_tex(latex_file, self.options.formula)
103         #os.system('cd ' + base_dir)
104         os.system('latex -output-directory=' + base_dir + ' -halt-on-error ' + latex_file + ' > ' + out_file)
105         try:
106             os.stat(dvi_file)
107         except OSError:
108             print >>sys.stderr, "invalid LaTeX input:"
109             print >>sys.stderr, self.options.formula
110             print >>sys.stderr, "temporary files were left in:", base_dir
111             sys.exit(1)
113         os.system('dvips -q -f -E -D 600 -y 5000 -o ' + ps_file + ' ' + dvi_file)
114         #os.system('cd ' + base_dir)
115         os.system('pstoedit -f plot-svg -dt -ssp ' + ps_file + ' ' + svg_file + '> ' + out_file)
116         svg_open(self, svg_file)
118         clean()
120 e = EQTEXSVG()
121 e.affect()