Code

redesign the dialog into 3 tabs, rewrite most texts, increase preview size
[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 - I will try to code XML parsing, not the hard way ;-)
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
28 """
30 import inkex, os, tempfile, xml.dom.ext, xml.dom.ext.reader.Sax
31 import sys
32 from xml.dom import Node
34 def create_equation_tex(filename, equation):
35     tex = open(filename, 'w')
36     tex.write("""%% processed with EqTeXSVG.py
37 \documentclass{article}
38 \usepackage{amsmath}    
39 \\thispagestyle{empty}
40 \\begin{document}
41 """)
42     tex.write("$$\n")
43     tex.write(equation)
44     tex.write("\n$$\n")
45     tex.write("\end{document}\n")
46     tex.close()
48 def add_paths(self,group,node):
49     next = node.firstChild
50     while next is not None:
51         child = next
52         next = child.nextSibling
54         if child.nodeType is not Node.ELEMENT_NODE: continue
55         if child.nodeName == "title": continue
56         if child.nodeName == "desc": continue
57         if child.nodeName == "rect" and child.getAttribute("id") == "background": continue
59         if child.nodeName == "g":
60             svg_group = self.document.createElement('svg:g')
61             if child.hasAttribute("transform"):
62                 svg_group.setAttribute("transform",child.getAttribute("transform"))
63             group.appendChild(svg_group)
64             add_paths(self,svg_group,child)
65         else:
66             group.appendChild(self.document.importNode(child,1))
68 def svg_open(self,filename):
69     # parsing of SVG file with the equation 
70     reader = xml.dom.ext.reader.Sax.Reader()
71     stream = open(filename,'r')
72     svgdoc = reader.fromStream(stream)
74     group = self.document.createElement('svg:g')
75     self.current_layer.appendChild(group)
76     child = svgdoc.firstChild
77     while child is not None:
78         if child.nodeName == "svg" and child.nodeType == Node.ELEMENT_NODE:
79             add_paths(self,group,child)
80         child = child.nextSibling
83 class EQTEXSVG(inkex.Effect):
84     def __init__(self):
85         inkex.Effect.__init__(self)
86         self.OptionParser.add_option("-f", "--formule",
87                         action="store", type="string", 
88                         dest="formule", default=10.0,
89                         help="Formule LaTeX")
90     def effect(self):
91         base_file = os.path.join(tempfile.gettempdir(), "inkscape-latex.tmp")
92         latex_file = base_file + ".tex"
93         create_equation_tex(latex_file, self.options.formule)
95         out_file = os.path.join(tempfile.gettempdir(), "inkscape-latex.tmp.output")
96         os.system('latex -output-directory=' + tempfile.gettempdir() + ' ' + latex_file + '> ' + out_file)
98         ps_file = base_file + ".ps"
99         dvi_file = base_file + ".dvi"
100         svg_file = base_file + ".svg"
101         os.system('dvips -q -f -E -D 600 -y 50000 -o ' + ps_file + ' ' + dvi_file)
102         os.system('pstoedit -f plot-svg -centered -dt -ssp ' + ps_file + ' ' + svg_file + '>> ' + out_file + ' 2>&1')
104         # ouvrir le svg et remplacer #7F7F7F par #000000
105         svg_open(self, svg_file)
107         # clean up
108         aux_file = base_file + ".aux"
109         log_file = base_file + ".log"
110         os.remove(latex_file)
111         os.remove(aux_file)
112         os.remove(log_file)
113         os.remove(dvi_file)
114         os.remove(ps_file)
115         os.remove(svg_file)
116         os.remove(out_file)
118 #e = EQTEXSVG()
119 #e.affect()
120 try:        
121     e = EQTEXSVG()
122     e.affect()
123 except Exception, inst:
124     sys.stderr.write(str(inst) + "\n")