Code

whitespace and fix sodipodi URI
[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
32 def create_equation_tex(filename, equation):
33         tex = open(filename, 'w')
34         tex.write("""%% processed with EqTeXSVG.py
35 \documentclass{article}
36         
37 \\thispagestyle{empty}
38 \\begin{document}
39 """)
40         tex.write("$$\n")
41         tex.write(equation)
42         tex.write("\n$$\n")
43         tex.write("\end{document}\n")
44         tex.close()
46 def svg_open(self,filename):
47 # parsing of SVG file with the equation 
48 # real parsing XML to use!!!! it will be easier !!!
49         svg = open(filename, 'r')
50         svg_lines = svg.readlines()
52 # trip top/bottom lines from svg file
53         svg_lines.pop(0)
54         svg_lines.pop(1)
55         svg_lines.pop(len(svg_lines)-1)
57         group = self.document.createElement('svg:g')
58         self.document.documentElement.appendChild(group)
60 # deleting "<g... >" "</g>" "<path d=" and "/>" from svg_lines
61         nodegroup=''
62         s_nodegroup_path=''
63         
64         for i in range(1,len(svg_lines)):
65             if svg_lines[i].find("<g") != -1:
66                 nodegroup=svg_lines[i].split("<g")
67                 nodegroup=nodegroup[1].split(" >")
68                 nodegroup=nodegroup[0]+'\n'
69             elif svg_lines[i].find("<path d=") != -1:
70                 s_nodegroup_path=svg_lines[i].split("<path d=")
71                 s_nodegroup_path=s_nodegroup_path[1]                
72             elif svg_lines[i].find("/>") != -1:
73                 s_nodegroup_path=s_nodegroup_path+'"\n'
74             elif svg_lines[i].find("</g>") != -1:
75                 nodegroup_svg = self.document.createElement('svg:g')
76                 nodegroup_svg.setAttribute('style',nodegroup)
77                 nodegroup_path = self.document.createElement('svg:path')
78                 nodegroup_path.setAttribute('d',s_nodegroup_path)
79                 group.appendChild(nodegroup_svg)                                
80                 nodegroup_svg.appendChild(nodegroup_path)
81             else:
82                 s_nodegroup_path=s_nodegroup_path+svg_lines[i]
84 class EQTEXSVG(inkex.Effect):
85         def __init__(self):
86                 inkex.Effect.__init__(self)
87                 self.OptionParser.add_option("-f", "--formule",
88                                                 action="store", type="string", 
89                                                 dest="formule", default=10.0,
90                                                 help="Formule LaTeX")
91         def effect(self):
92                 
93                 base_file = os.path.join(tempfile.gettempdir(), "inkscape-latex.tmp")
94                 latex_file = base_file + ".tex"
95                 create_equation_tex(latex_file, self.options.formule)
97                 out_file = os.path.join(tempfile.gettempdir(), "inkscape-latex.tmp.output")
98                 os.system('latex -output-directory=' + tempfile.gettempdir() + ' ' + latex_file + '> ' + out_file)
100                 ps_file = base_file + ".ps"
101                 dvi_file = base_file + ".dvi"
102                 svg_file = base_file + ".svg"
103                 os.system('dvips -q -f -E -D 600 -y 5000 -o ' + ps_file + ' ' + dvi_file)
104                 os.system('pstoedit -f svg -dt -ssp ' + ps_file + ' ' + svg_file + '>> ' + out_file)
106                 # ouvrir le svg et remplacer #7F7F7F par #000000
107                 svg_open(self, svg_file)
109               # clean up
110                 aux_file = base_file + ".aux"
111                 log_file = base_file + ".log"
112                 os.remove(latex_file)
113                 os.remove(aux_file)
114                 os.remove(log_file)
115                 os.remove(dvi_file)
116                 os.remove(ps_file)
117                 os.remove(svg_file)
118                 os.remove(out_file)
119                 
120 e = EQTEXSVG()
121 e.affect()