2 # -*- coding: utf-8 -*-
5 # This file is part of LyX, the document processor.
6 # Licence details can be found in the file COPYING.
8 # \author Angus Leeming
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.
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}'
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.
36 run a command, quit if fails
38 if os
.system(cmd
) != 0:
39 print "Command '%s' fails." % cmd
43 # We expect two args, the names of the input and output files.
44 if len(sys
.argv
) != 3:
47 input, output
= sys
.argv
[1:]
49 # Fail silently if the file doesn't exist
50 if not os
.path
.isfile(input):
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
59 fout
= os
.popen('fig2dev -h')
60 help_msg
= fout
.read()
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
))
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
80 epsfile
= outbase
+ '.pstex'
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())
90 tmp
.write('''%%%%BoundingBox: 0 0 %d %d
91 << /PageSize [%d %d] >> setpagedevice
93 ''' % (width
, height
, width
, height
, xoffset
, yoffset
))
97 # direct move(rename) may fail under windows
99 os
.rename(epsfile
+ '.??', epsfile
)
101 # Convert the ${pstex} EPS file (free of "special" text) to PDF format
103 runCommand('gs -q -dNOPAUSE -dBATCH -dSAFER -sDEVICE=pdfwrite -sOutputFile=%s.pdf %s.pstex'\
104 % (outbase
, outbase
))