]> git.lyx.org Git - lyx.git/blob - lib/scripts/svg2pdftex.py
Move DrawStrategy enum to update_flags.h.
[lyx.git] / lib / scripts / svg2pdftex.py
1 #!/usr/bin/python3
2
3 # file svg2pdftex.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 pdflatex into high quality PDF. It requires Inkscape.
15
16 # Usage:
17 #   python svg2pdftex.py [--unstable] [inkscape_command] inputfile.svg outputfile.pdf_tex
18 # This command generates
19 #   1. outputfile.pdf     -- the converted PDF file (text from SVG stripped)
20 #   2. outputfile.pdf_tex -- a TeX file that can be included in your
21 #                             LaTeX document using '\input{outputfile.pdf_text}'
22 # use --unstable for inkscape < 1.0
23 #
24 # Note:
25 #   Do not use this command as
26 #     python svg2pdftex.py [inkscape_command] inputfile.svg outputfile.pdf
27 #   the real PDF file would be overwritten by a TeX file named outputfile.pdf.
28 #
29
30
31 import os, sys, re, subprocess
32
33 def runCommand(cmd):
34     ''' Utility function:
35         run a command, quit if fails
36     '''
37     res = subprocess.check_call(cmd)
38     if res != 0:
39         print("Command '%s' fails (exit code: %i)." % (res.cmd, res.returncode))
40         sys.exit(1)
41
42 InkscapeCmd = "inkscape"
43 InputFile = ""
44 OutputFile = ""
45 unstable = False
46
47 # We expect two to four args: the names of the input and output files
48 # and optionally the inkscape command (with path if needed) and --unstable.
49 args = len(sys.argv)
50 if args == 3:
51     # Two args: input and output file only
52     InputFile, OutputFile = sys.argv[1:]
53 elif args == 4:
54     # Three args: check whether we have --unstable as first arg
55     if sys.argv[1] == "--unstable":
56         unstable = True
57         InputFile, OutputFile = sys.argv[2:]
58     else:
59         InkscapeCmd, InputFile, OutputFile = sys.argv[1:]
60 elif args == 5:
61     # Four args: check whether we have --unstable as first arg
62     if sys.argv[1] != "--unstable":
63         # Invalid number of args. Exit with error.
64         sys.exit(1)
65     else:
66         unstable = True
67         InkscapeCmd, InputFile, OutputFile = sys.argv[2:]
68 else:
69     # Invalid number of args. Exit with error.
70     sys.exit(1)
71
72 # Fail silently if the file doesn't exist
73 if not os.path.isfile(InputFile):
74     sys.exit(0)
75
76 # Strip the extension from ${OutputFile}
77 OutBase = os.path.splitext(OutputFile)[0]
78
79 # Inkscape (as of 0.48) can output SVG images as a PDF file without text, ${OutBase}.pdf,
80 # while outsourcing the text to a LaTeX file ${OutBase}.pdf_tex which includes and overlays
81 # the PDF image and can be \input to LaTeX files. We rename the latter file to ${OutputFile}
82 # (although this is probably the name it already has).
83 if unstable:
84     runCommand([r'%s' % InkscapeCmd, '--file=%s' % InputFile, '--export-pdf=%s.pdf' % OutBase, '--export-latex'])
85 else:
86     runCommand([r'%s' % InkscapeCmd, '%s' % InputFile, '--export-filename=%s.pdf' % OutBase, '--export-latex'])
87
88 os.rename('%s.pdf_tex' % OutBase, OutputFile)
89