2016-09-03 14:17:45 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# file svg2pstex.py
|
2017-08-26 14:10:55 +00:00
|
|
|
# This file is part of LyX, the document processor.
|
|
|
|
# Licence details can be found in the file COPYING.
|
|
|
|
|
|
|
|
# author Daniel Gloger
|
|
|
|
# author Martin Vermeer
|
2017-08-28 06:00:56 +00:00
|
|
|
# author Jürgen Spitzmüller
|
2016-09-03 14:17:45 +00:00
|
|
|
|
2017-08-26 14:10:55 +00:00
|
|
|
# Full author contact details are available in file CREDITS
|
|
|
|
|
2017-08-28 06:00:56 +00:00
|
|
|
# This script converts an SVG image to two files that can be processed
|
|
|
|
# with latex into high quality DVI/PostScript. It requires Inkscape.
|
|
|
|
|
2016-09-03 14:17:45 +00:00
|
|
|
# Usage:
|
2020-05-15 12:08:46 +00:00
|
|
|
# python svg2pstex.py [--unstable] [inkscape_command] inputfile.svg outputfile.eps_tex
|
2016-09-03 14:17:45 +00:00
|
|
|
# This command generates
|
2017-08-28 06:00:56 +00:00
|
|
|
# 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}'
|
2020-05-15 12:08:46 +00:00
|
|
|
# use --unstable for inkscape < 1.0
|
2016-09-03 14:17:45 +00:00
|
|
|
#
|
|
|
|
# Note:
|
|
|
|
# Do not use this command as
|
2017-08-28 06:00:56 +00:00
|
|
|
# python svg2pstex.py [inkscape_command] inputfile.svg outputfile.pdf
|
|
|
|
# the real EPS file would be overwritten by a TeX file named outputfile.eps.
|
2016-09-03 14:17:45 +00:00
|
|
|
#
|
|
|
|
|
2017-08-28 06:00:56 +00:00
|
|
|
# This script converts an SVG image to something that latex can process
|
|
|
|
# into high quality PostScript.
|
|
|
|
|
2017-11-24 10:56:41 +00:00
|
|
|
from __future__ import print_function
|
|
|
|
|
2017-09-10 15:02:58 +00:00
|
|
|
import os, sys, re, subprocess
|
2016-09-03 14:17:45 +00:00
|
|
|
|
|
|
|
def runCommand(cmd):
|
|
|
|
''' Utility function:
|
|
|
|
run a command, quit if fails
|
|
|
|
'''
|
2017-09-12 05:57:29 +00:00
|
|
|
res = subprocess.check_call(cmd)
|
|
|
|
if res != 0:
|
2017-11-23 07:16:10 +00:00
|
|
|
print("Command '%s' fails (exit code: %i)." % (res.cmd, res.returncode))
|
2016-09-03 14:17:45 +00:00
|
|
|
sys.exit(1)
|
|
|
|
|
2017-08-28 06:00:56 +00:00
|
|
|
InkscapeCmd = "inkscape"
|
|
|
|
InputFile = ""
|
|
|
|
OutputFile = ""
|
2020-05-15 12:08:46 +00:00
|
|
|
unstable = False
|
2016-09-03 14:17:45 +00:00
|
|
|
|
2020-05-15 12:08:46 +00:00
|
|
|
# We expect two to four args: the names of the input and output files
|
|
|
|
# and optionally the inkscape command (with path if needed) and --unstable.
|
2017-08-28 06:00:56 +00:00
|
|
|
args = len(sys.argv)
|
|
|
|
if args == 3:
|
|
|
|
# Two args: input and output file only
|
|
|
|
InputFile, OutputFile = sys.argv[1:]
|
|
|
|
elif args == 4:
|
2020-05-15 12:08:46 +00:00
|
|
|
# Three args: check whether we have --unstable as first arg
|
|
|
|
if sys.argv[1] == "--unstable":
|
|
|
|
unstable = True
|
|
|
|
InputFile, OutputFile = sys.argv[2:]
|
|
|
|
else:
|
|
|
|
InkscapeCmd, InputFile, OutputFile = sys.argv[1:]
|
|
|
|
elif args == 5:
|
|
|
|
# Four args: check whether we have --unstable as first arg
|
|
|
|
if sys.argv[1] != "--unstable":
|
|
|
|
# Invalid number of args. Exit with error.
|
|
|
|
sys.exit(1)
|
|
|
|
else:
|
|
|
|
unstable = True
|
|
|
|
InkscapeCmd, InputFile, OutputFile = sys.argv[2:]
|
2017-08-28 06:00:56 +00:00
|
|
|
else:
|
|
|
|
# Invalid number of args. Exit with error.
|
|
|
|
sys.exit(1)
|
2016-09-03 14:17:45 +00:00
|
|
|
|
|
|
|
# Fail silently if the file doesn't exist
|
2017-08-28 06:00:56 +00:00
|
|
|
if not os.path.isfile(InputFile):
|
2016-09-03 14:17:45 +00:00
|
|
|
sys.exit(0)
|
|
|
|
|
2017-08-28 06:00:56 +00:00
|
|
|
# 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).
|
2017-09-12 05:57:29 +00:00
|
|
|
runCommand([r'%s' % InkscapeCmd, '--file=%s' % InputFile, '--export-eps=%s.eps' % OutBase, '--export-latex'])
|
2017-09-10 15:02:58 +00:00
|
|
|
|
2017-08-28 06:00:56 +00:00
|
|
|
os.rename('%s.eps_tex' % OutBase, OutputFile)
|
2016-09-03 14:17:45 +00:00
|
|
|
|