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