]> git.lyx.org Git - lyx.git/blobdiff - lib/scripts/svg2pstex.py
Consistent output of breakable/non-breakable dashes on all TeX engines.
[lyx.git] / lib / scripts / svg2pstex.py
index 601d2b93305fda067f6b1c1f41a048d4e05fabf9..97d6ae103981b5c9bd85924a720b8d532e5211a7 100644 (file)
@@ -7,50 +7,71 @@
 
 # author Daniel Gloger
 # author Martin Vermeer
-
-# This script converts an SVG image to something that latex can process
-# into high quality PostScript.
+# author Jürgen Spitzmüller
 
 # Full author contact details are available in file CREDITS
 
+# This script converts an SVG image to two files that can be processed
+# with latex into high quality DVI/PostScript. It requires Inkscape.
+
 # Usage:
-#   python svg2pstex.py ${base}.fig ${base}.pstex
+#   python svg2pstex.py [inkscape_command] inputfile.svg outputfile.eps_tex
 # This command generates
-#   ${base}.eps    the converted eps file
-#   ${base}.pstex  a tex file that can be included in your latex document
-#       using '\input{${output}}'.
+#   1. outputfile.eps     -- the converted EPS file (text from SVG stripped)
+#   2. outputfile.eps_tex -- a TeX file that can be included in your
+#                             LaTeX document using '\input{outputfile.eps_text}'
 #
 # Note:
 #   Do not use this command as
-#     python svg2pstex.py file.fig file.eps
-#   the real eps file will be overwritten by a tex file named file.eps.
+#     python svg2pstex.py [inkscape_command] inputfile.svg outputfile.pdf
+#   the real EPS file would be overwritten by a TeX file named outputfile.eps.
 #
 
-import os, sys
+# This script converts an SVG image to something that latex can process
+# into high quality PostScript.
+
+from __future__ import print_function
+
+import os, sys, re, subprocess
 
 def runCommand(cmd):
     ''' Utility function:
         run a command, quit if fails
     '''
-    if os.system(cmd) != 0:
-        print "Command '%s' fails." % cmd
+    res = subprocess.check_call(cmd)
+    if res != 0:
+        print("Command '%s' fails (exit code: %i)." % (res.cmd, res.returncode))
         sys.exit(1)
 
-# We expect two args, the names of the input and output files.
-if len(sys.argv) != 3:
-    sys.exit(1)
+InkscapeCmd = "inkscape"
+InputFile = ""
+OutputFile = ""
 
-input, output = sys.argv[1:]
+# We expect two or three args: the names of the input and output files
+# and optionally the inkscape command (with path if needed).
+args = len(sys.argv)
+if args == 3:
+    # Two args: input and output file only
+    InputFile, OutputFile = sys.argv[1:]
+elif args == 4:
+    # Three args: first arg is inkscape command
+    InkscapeCmd, InputFile, OutputFile = sys.argv[1:]
+else:
+    # Invalid number of args. Exit with error.
+    sys.exit(1)
 
 # Fail silently if the file doesn't exist
-if not os.path.isfile(input):
+if not os.path.isfile(InputFile):
     sys.exit(0)
 
-# Strip the extension from ${output}
-outbase = os.path.splitext(output)[0]
+# Strip the extension from ${OutputFile}
+OutBase = os.path.splitext(OutputFile)[0]
+
+# Inkscape (as of 0.48) can output SVG images as an EPS file without text, ${OutBase}.eps,
+# while outsourcing the text to a LaTeX file ${OutBase}.eps_tex which includes and overlays
+# the EPS image and can be \input to LaTeX files. We rename the latter file to ${OutputFile}
+# (although this is probably the name it already has).
+runCommand([r'%s' % InkscapeCmd, '--file=%s' % InputFile, '--export-eps=%s.eps' % OutBase, '--export-latex'])
+
+os.rename('%s.eps_tex' % OutBase, OutputFile)
 
-# Inkscape 0.48 can output the image as a EPS file ${base}.pdf and place the text 
-# in a LaTeX file ${base}.eps_tex, which is renamed to ${output}, for typesetting 
-# by latex itself. 
-runCommand('inkscape --file=%s --export-eps=%s.eps --export-latex' % (input, outbase))
-os.rename('%s.eps_tex' % outbase, output)