]> git.lyx.org Git - lyx.git/blob - lib/scripts/fig2pdftex.sh
Make lyx2lyx output the new external inset format.
[lyx.git] / lib / scripts / fig2pdftex.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     echo modern_xfig
26
27     # Can we find fig2dev?
28     type fig2dev > /dev/null || exit 1
29
30     input=$1
31     pdftex_t=$2
32     pdftex=$3.pdf
33
34     fig2dev -Lpdftex ${input} ${pdftex}
35     fig2dev -Lpdftex_t -p${outbase} ${input} ${pdftex_t}
36
37     exit 0;
38 }
39
40 # Older versions of xfig cannot do this, so we emulate the behaviour using
41 # pstex and pstex_t output.
42 legacy_xfig() {
43     echo legacy_xfig
44
45     # Can we find fig2dev, eps2eos or gs?
46     type fig2dev > /dev/null || exit 1
47     type eps2eps > /dev/null || exit 1
48     type gs > /dev/null || exit 1
49
50     input=$1
51     pdftex_t=$2
52     png=$3.png
53     pstex=$3.pstex
54
55     fig2dev -Lpstex ${input} ${pstex}
56     fig2dev -Lpstex_t -p${outbase} ${input} ${pdftex_t}
57
58     # Convert the ${pstex} EPS file (free of "special" text) to PDF format
59     # using gs.
60
61     # gs is extremely fussy about the EPS files it converts, so ensure it is
62     # "clean" first.
63     clean=${pstex}.$$
64     eps2eps ${pstex} ${clean}
65     rm -f ${pstex}
66
67     # Extract the width and height of the image using gs' bbox device.
68     # Ie, take output that includes line "%%BoundingBox: 0 0 <width> <height>"
69     # and rewrite it as "-g<width>x<height>"
70     geometry=`gs -q -dSAFER -dNOPAUSE -dBATCH -sDEVICE=bbox ${clean} 2>&1 | \
71         sed '/^%%BoundingBox/! d' | cut -d' ' -f4,5 | \
72         sed 's/^\([0-9]\{1,\}\) \([0-9]\{1,\}\)$/-g\1x\2/'`
73
74     # Generate a PNG file using the -g option to ensure the size is the same
75     # as the original.
76     # If we're using a version of gs that does not have a bbox device, then
77     # ${geometry} = "", so there are no unwanted side effects.
78     gs -q -dSAFER -dBATCH -dNOPAUSE ${geometry} -sDEVICE=png16m \
79         -sOutputFile=${png} ${clean}
80     rm -f ${clean}
81
82     exit 0;
83 }
84
85 # The main logic of the script is below.
86 # All it does is ascertain which of the two functions above to call.
87
88 # We expect two args, the names of the input and output files.
89 test $# -eq 2 || exit 1
90
91 input=$1
92 output=$2
93
94 # Strip the extension from ${output}
95 outbase=`echo ${output} | sed 's/[.][^.]*$//'`
96
97 # Ascertain whether fig2dev is "modern enough".
98 # Here "modern" means "fig2dev Version 3.2 Patchlevel 4"
99 version_info=`fig2dev -h | sed '/^fig2dev/! d'`
100 # If no line begins "fig2dev" then default to legacy_xfig
101 test "x${version_info}" = "x" && {
102     legacy_xfig ${input} ${output} ${outbase}
103 }
104
105 version=`echo ${version_info} | cut -d' ' -f3`
106 patchlevel=`echo ${version_info} | cut -d' ' -f5`
107 # If we cannot extract the version of patchlevel info
108 # then default to legacy_xfig
109 test "x${version}" = "x" -o "x${patchlevel}" = "x" && {
110     legacy_xfig ${input} ${output} ${outbase}
111 }
112 echo ${version} ${patchlevel} | grep '[0-9]!' -o && {
113     legacy_xfig ${input} ${output} ${outbase}
114 }
115
116 # So, is it an old version?
117 test ${version} != "3.2" -o ${patchlevel} -lt 4 && {
118     legacy_xfig ${input} ${output} ${outbase}
119 }
120 # I guess not ;-)
121
122 # Commented out for now to test legacy_xfig...
123 #modern_xfig ${input} ${output} ${outbase}
124 legacy_xfig ${input} ${output} ${outbase}
125
126 # The end