]> git.lyx.org Git - lyx.git/blob - lib/scripts/fig2pdftex.py
Don't use widest label for numerical citations.
[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
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 input, output = sys.argv[1:]
48
49 # Fail silently if the file doesn't exist
50 if not os.path.isfile(input):
51     sys.exit(0)
52
53 # Strip the extension from ${output}
54 outbase = os.path.splitext(output)[0]
55
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()
62
63
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))
75
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)
100
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)