]> git.lyx.org Git - features.git/blob - lib/scripts/fig2pdf.sh
A little script to generate high quality PDF from XFIG files.
[features.git] / lib / scripts / fig2pdf.sh
1 #! /bin/sh
2
3 # file fig2pdf.sh
4 # This file is part of LyX, the document processor.
5 # Licence details can be found in the file COPYING.
6 #
7 # author Angus Leeming
8 #
9 # Full author contact details are available in file CREDITS
10
11
12 # This script converts an XFIG image to something that pdflatex can process
13 # into high quality PDF.
14
15 # Usage: sh fig2pdf.sh ${base}.xfig
16 # to generate ${base}.pdftex_t
17 # Thereafter, you need only '\input{${base}.pdftex_t}' in your latex document.
18
19 # modern_xfig() and legacy_xfig() are the functions that do all the work.
20
21 # Modern versions of xfig can output the image without "special" text as
22 # a PDF file ${base}.pdf and place the text in a LaTeX file
23 # ${base}.pdftex_t for typesetting by pdflatex itself.
24 modern_xfig() {
25     input=$1.fig
26     pdftex=$1.pdf
27     pdftex_t=$1.pdftex_t
28
29     fig2dev -Lpdftex ${input} ${pdftex}
30     fig2dev -Lpdftex_t -p$1 ${input} ${pdftex_t}
31
32     exit 0;
33 }
34
35 # Older versions of xfig cannot do this, so we emulate the behaviour using
36 # pstex and pstex_t output.
37 legacy_xfig() {
38     input=$1.fig
39     pstex=$1.pstex
40     png=$1.png
41     pdftex_t=$1.pdftex_t
42
43     fig2dev -Lpstex ${input} ${pstex}
44     fig2dev -Lpstex_t -p$1 ${input} ${pdftex_t}
45
46     # Convert the ${pstex} EPS file (free of "special" text) to PDF format
47     # using gs.
48
49     # gs is extremely fussy about the EPS files it converts, so ensure it is
50     # "clean" first.
51     clean=${pstex}.$$
52     eps2eps ${pstex} ${clean}
53     rm -f ${pstex}
54
55     # Extract the width and height of the image using gs' bbox device.
56     # Ie, take output that includes line "%%BoundingBox: 0 0 <width> <height>"
57     # and rewrite it as "-g<width>x<height>"
58     geometry=`gs -q -dSAFER -dNOPAUSE -dBATCH -sDEVICE=bbox ${clean} 2>&1 | \
59         sed '/^%%BoundingBox/! d' | cut -d' ' -f4,5 | \
60         sed 's/^\([0-9]\{1,\}\) \([0-9]\{1,\}\)$/-g\1x\2/'`
61
62     # Generate a PNG file using the -g option to ensure the size is the same
63     # as the original.
64     # If we're using a version of gs that does not have a bbox device, then
65     # ${geometry} = "", so there are no unwanted side effects.
66     gs -q -dSAFER -dBATCH -dNOPAUSE ${geometry} -sDEVICE=png16m \
67         -sOutputFile=${png} ${clean}
68     rm -f ${clean}
69
70     exit 0;
71 }
72
73 # The main logic of the script is below.
74 # All it does is ascertain which of the two functions above to call.
75
76 # We expect a single arg, the name of the input file.
77 test $# -eq 1 || exit 1
78
79 # Remove the .fig extension
80 input=`basename $1`
81 base=`echo ${input} | sed 's/\.fig$//'`
82
83 # Ascertain whether fig2dev is "modern enough".
84 # Here "modern" means "fig2dev Version 3.2 Patchlevel 4"
85 version_info=`fig2dev -h | sed '/^fig2dev/! d'`
86 # If no line begins "fig2dev" then default to legacy_xfig
87 test "x${version_info}" = "x" && legacy_xfig ${base}
88
89 version=`echo ${version_info} | cut -d' ' -f3`
90 patchlevel=`echo ${version_info} | cut -d' ' -f5`
91 # If we cannot extract the version of patchlevel info
92 # then default to legacy_xfig
93 test "x${version}" = "x" -o "x${patchlevel}" = "x" && legacy_xfig ${base}
94 echo ${version} ${patchlevel} | grep '[0-9]!' -o && legacy_xfig ${base}
95
96 # So, is it am old version?
97 test ${version} != "3.2" -o ${patchlevel} -lt 4 && legacy_xfig ${base}
98 # I guess not ;-)
99 modern_xfig ${base}
100
101 # The end