tufte layout files:
[lyx.git] / lib / scripts / fig2pdftex.py
blobe20b2ad06b32e92169113cdc69af643de1c7da61
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
4 # file fig2pdf.py
5 # This file is part of LyX, the document processor.
6 # Licence details can be found in the file COPYING.
8 # \author Angus Leeming
9 # \author Bo Peng
11 # Full author contact details are available in file CREDITS
14 # This script converts an XFIG image to something that pdflatex can process
15 # into high quality PDF.
17 # Usage:
18 # python fig2pdftex.py ${base}.fig ${base}.pdft
19 # This command generates
20 # ${base}.pdf the converted pdf file
21 # ${base}.pdft a tex file that can be included in your latex document
22 # using '\input{${base}.pdft}'
24 # Note:
25 # Do not use this command as
26 # python fig2pdftex.py file.fig file.pdf
27 # the real pdf file will be overwritten by a tex file named file.pdf.
31 import os, sys, re
34 def runCommand(cmd):
35 ''' Utility function:
36 run a command, quit if fails
37 '''
38 if os.system(cmd) != 0:
39 print "Command '%s' fails." % cmd
40 sys.exit(1)
43 # We expect two args, the names of the input and output files.
44 if len(sys.argv) != 3:
45 sys.exit(1)
47 input, output = sys.argv[1:]
49 # Fail silently if the file doesn't exist
50 if not os.path.isfile(input):
51 sys.exit(0)
53 # Strip the extension from ${output}
54 outbase = os.path.splitext(output)[0]
56 # Ascertain whether fig2dev is "modern enough".
57 # If it is, then the help info will mention "pdftex_t" as one of the
58 # available outputs.
59 fout = os.popen('fig2dev -h')
60 help_msg = fout.read()
61 fout.close()
64 if 'pdftex_t' in help_msg:
65 # Modern versions of xfig can output the image without "special" text as
66 # a PDF file ${base}.pdf and place the text in a LaTeX file
67 # ${base}.pdftex_t for typesetting by pdflatex itself.
68 runCommand('fig2dev -Lpdftex -p1 %s %s.pdf' % (input, outbase))
69 runCommand('fig2dev -Lpdftex_t -p%s %s %s' % (outbase, input, output))
70 else:
71 # Older versions of xfig cannot do this, so we emulate the behaviour using
72 # pstex and pstex_t output.
73 runCommand('fig2dev -Lpstex %s %s.pstex' % (input, outbase))
74 runCommand('fig2dev -Lpstex_t -p %s %s %s' % (outbase, input, output))
76 # manipulates the Bounding Box info to enable gs to produce
77 # the appropriate PDF file from an EPS one.
78 # The generated PostScript commands are extracted from epstopdf, distributed
79 # with tetex.
80 epsfile = outbase + '.pstex'
81 tmp = mkstemp()
82 boundingboxline = re.compile('%%BoundingBox:\s+(\d*)\s+(\d*)\s+(\d*)\s+(\d*)')
83 for line in open(epsfile).xreadlines():
84 if line[:13] == '%%BoundingBox':
85 (llx, lly, urx, ury) = map(int, boundingboxline.search(line).groups())
86 width = urx - llx
87 height = ury - lly
88 xoffset = - llx
89 yoffset = - lly
90 tmp.write('''%%%%BoundingBox: 0 0 %d %d
91 << /PageSize [%d %d] >> setpagedevice
92 gsave %d %d translate
93 ''' % (width, height, width, height, xoffset, yoffset))
94 else:
95 tmp.write(line)
96 tmp.close()
97 # direct move(rename) may fail under windows
98 os.unlink(epsfile)
99 os.rename(epsfile + '.??', epsfile)
101 # Convert the ${pstex} EPS file (free of "special" text) to PDF format
102 # using gs
103 runCommand('gs -q -dNOPAUSE -dBATCH -dSAFER -sDEVICE=pdfwrite -sOutputFile=%s.pdf %s.pstex'\
104 % (outbase, outbase))
105 os.unlink(epsfile)