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.
31 import os, sys, re, locale
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 language, output_encoding = locale.getdefaultlocale()
48 if output_encoding == None:
49 output_encoding = 'latin1'
51 input = unicode(sys.argv[1], 'utf8').encode(output_encoding)
52 output = unicode(sys.argv[2], 'utf8').encode(output_encoding)
54 # Fail silently if the file doesn't exist
55 if not os.path.isfile(input):
58 # Strip the extension from ${output}
59 outbase = os.path.splitext(output)[0]
61 # Ascertain whether fig2dev is "modern enough".
62 # If it is, then the help info will mention "pdftex_t" as one of the
64 fout = os.popen('fig2dev -h')
65 help_msg = fout.read()
69 if 'pdftex_t' in help_msg:
70 # Modern versions of xfig can output the image without "special" text as
71 # a PDF file ${base}.pdf and place the text in a LaTeX file
72 # ${base}.pdftex_t for typesetting by pdflatex itself.
73 runCommand('fig2dev -Lpdftex -p1 %s %s.pdf' % (input, outbase))
74 runCommand('fig2dev -Lpdftex_t -p%s %s %s' % (outbase, input, output))
76 # Older versions of xfig cannot do this, so we emulate the behaviour using
77 # pstex and pstex_t output.
78 runCommand('fig2dev -Lpstex %s %s.pstex' % (input, outbase))
79 runCommand('fig2dev -Lpstex_t -p %s %s %s' % (outbase, input, output))
81 # manipulates the Bounding Box info to enable gs to produce
82 # the appropriate PDF file from an EPS one.
83 # The generated PostScript commands are extracted from epstopdf, distributed
85 epsfile = outbase + '.pstex'
86 tmp = open(epsfile + '.??', 'w')
87 boundingboxline = re.compile('%%BoundingBox:\s+(\d*)\s+(\d*)\s+(\d*)\s+(\d*)')
88 for line in open(epsfile).xreadlines():
89 if line[:13] == '%%BoundingBox':
90 (llx, lly, urx, ury) = map(int, boundingboxline.search(line).groups())
95 tmp.write('''%%%%BoundingBox: 0 0 %d %d
96 << /PageSize [%d %d] >> setpagedevice
98 ''' % (width, height, width, height, xoffset, yoffset))
102 # direct move(rename) may fail under windows
104 os.rename(epsfile + '.??', epsfile)
106 # Convert the ${pstex} EPS file (free of "special" text) to PDF format
108 runCommand('gs -q -dNOPAUSE -dBATCH -dSAFER -sDEVICE=pdfwrite -sOutputFile=%s.pdf %s.pstex'\
109 % (outbase, outbase))