]> git.lyx.org Git - lyx.git/blob - lib/scripts/svg2pstex.py
Make BufferView::singeParUpdate more robust
[lyx.git] / lib / scripts / svg2pstex.py
1 #!/usr/bin/python3
2
3 # file svg2pstex.py
4 # This file is part of LyX, the document processor.
5 # Licence details can be found in the file COPYING.
6
7 # author Daniel Gloger
8 # author Martin Vermeer
9 # author Jürgen Spitzmüller
10
11 # Full author contact details are available in file CREDITS
12
13 # This script converts an SVG image to two files that can be processed
14 # with latex into high quality DVI/PostScript. It requires Inkscape.
15
16 # Usage:
17 #   python svg2pstex.py [--unstable] [inkscape_command] inputfile.svg outputfile.eps_tex
18 # This command generates
19 #   1. outputfile.eps     -- the converted EPS file (text from SVG stripped)
20 #   2. outputfile.eps_tex -- a TeX file that can be included in your
21 #                             LaTeX document using '\input{outputfile.eps_text}'
22 # use --unstable for inkscape < 1.0
23 #
24 # Note:
25 #   Do not use this command as
26 #     python svg2pstex.py [inkscape_command] inputfile.svg outputfile.pdf
27 #   the real EPS file would be overwritten by a TeX file named outputfile.eps.
28 #
29
30 # This script converts an SVG image to something that latex can process
31 # into high quality PostScript.
32
33
34 import os, sys, re, subprocess
35
36 def runCommand(cmd):
37     ''' Utility function:
38         run a command, quit if fails
39     '''
40     res = subprocess.check_call(cmd)
41     if res != 0:
42         print("Command '%s' fails (exit code: %i)." % (res.cmd, res.returncode))
43         sys.exit(1)
44
45 InkscapeCmd = "inkscape"
46 InputFile = ""
47 OutputFile = ""
48 unstable = False
49
50 # We expect two to four args: the names of the input and output files
51 # and optionally the inkscape command (with path if needed) and --unstable.
52 args = len(sys.argv)
53 if args == 3:
54     # Two args: input and output file only
55     InputFile, OutputFile = sys.argv[1:]
56 elif args == 4:
57     # Three args: check whether we have --unstable as first arg
58     if sys.argv[1] == "--unstable":
59         unstable = True
60         InputFile, OutputFile = sys.argv[2:]
61     else:
62         InkscapeCmd, InputFile, OutputFile = sys.argv[1:]
63 elif args == 5:
64     # Four args: check whether we have --unstable as first arg
65     if sys.argv[1] != "--unstable":
66         # Invalid number of args. Exit with error.
67         sys.exit(1)
68     else:
69         unstable = True
70         InkscapeCmd, InputFile, OutputFile = sys.argv[2:]
71 else:
72     # Invalid number of args. Exit with error.
73     sys.exit(1)
74
75 # Fail silently if the file doesn't exist
76 if not os.path.isfile(InputFile):
77     sys.exit(0)
78
79 # Strip the extension from ${OutputFile}
80 OutBase = os.path.splitext(OutputFile)[0]
81
82 # Inkscape (as of 0.48) can output SVG images as an EPS file without text, ${OutBase}.eps,
83 # while outsourcing the text to a LaTeX file ${OutBase}.eps_tex which includes and overlays
84 # the EPS image and can be \input to LaTeX files. We rename the latter file to ${OutputFile}
85 # (although this is probably the name it already has).
86 if unstable:
87     runCommand([r'%s' % InkscapeCmd, '--file=%s' % InputFile, '--export-eps=%s.eps' % OutBase, '--export-latex'])
88 else:
89     runCommand([r'%s' % InkscapeCmd, '%s' % InputFile, '--export-filename=%s.eps' % OutBase, '--export-latex'])
90
91 os.rename('%s.eps_tex' % OutBase, OutputFile)
92