]> git.lyx.org Git - lyx.git/blob - lib/scripts/fig2pdftex.py
revert mistake.
[lyx.git] / lib / scripts / fig2pdftex.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 # file fig2pdf.py
5 # This file is part of LyX, the document processor.
6 # Licence details can be found in the file COPYING.
7 #
8 # \author Angus Leeming
9 # \author Bo Peng
10 #
11 # Full author contact details are available in file CREDITS
12
13
14 # This script converts an XFIG image to something that pdflatex can process
15 # into high quality PDF.
16
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}'
23 #
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.
28 #
29
30
31 import os, sys, re, locale
32
33
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)
41
42
43 # We expect two args, the names of the input and output files.
44 if len(sys.argv) != 3:
45     sys.exit(1)
46
47 language, output_encoding = locale.getdefaultlocale()
48 if output_encoding == None:
49     output_encoding = 'latin1'
50
51 input = unicode(sys.argv[1], 'utf8').encode(output_encoding)
52 output = unicode(sys.argv[2], 'utf8').encode(output_encoding)
53
54 # Fail silently if the file doesn't exist
55 if not os.path.isfile(input):
56     sys.exit(0)
57
58 # Strip the extension from ${output}
59 outbase = os.path.splitext(output)[0]
60
61 # Ascertain whether fig2dev is "modern enough".
62 # If it is, then the help info will mention "pdftex_t" as one of the
63 # available outputs.
64 fout = os.popen('fig2dev -h')
65 help_msg = fout.read()
66 fout.close()
67
68
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))
75 else:
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))
80
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
84     # with tetex.
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())
91             width = urx - llx
92             height = ury - lly
93             xoffset = - llx
94             yoffset = - lly
95             tmp.write('''%%%%BoundingBox: 0 0 %d %d
96 << /PageSize  [%d %d] >> setpagedevice
97 gsave %d %d translate
98 ''' % (width, height, width, height, xoffset, yoffset))
99         else:
100             tmp.write(line)
101     tmp.close()
102     # direct move(rename) may fail under windows
103     os.unlink(epsfile)
104     os.rename(epsfile + '.??', epsfile)
105
106     # Convert the ${pstex} EPS file (free of "special" text) to PDF format
107     # using gs
108     runCommand('gs -q -dNOPAUSE -dBATCH -dSAFER -sDEVICE=pdfwrite -sOutputFile=%s.pdf %s.pstex'\
109       % (outbase, outbase))
110     os.unlink(epsfile)