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