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