4 # This file is part of LyX, the document processor.
5 # Licence details can be found in the file COPYING.
9 # Full author contact details are available in file CREDITS
12 # This script converts an XFIG image to something that pdflatex can process
13 # into high quality PDF.
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.
19 # modern_xfig() and legacy_xfig() are the functions that do all the work.
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.
27 # Can we find fig2dev?
28 type fig2dev > /dev/null || exit 1
34 fig2dev -Lpdftex ${input} ${pdftex}
35 fig2dev -Lpdftex_t -p${outbase} ${input} ${pdftex_t}
40 # Older versions of xfig cannot do this, so we emulate the behaviour using
41 # pstex and pstex_t output.
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
55 fig2dev -Lpstex ${input} ${pstex}
56 fig2dev -Lpstex_t -p${outbase} ${input} ${pdftex_t}
58 # Convert the ${pstex} EPS file (free of "special" text) to PDF format
61 # gs is extremely fussy about the EPS files it converts, so ensure it is
64 eps2eps ${pstex} ${clean}
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/'`
74 # Generate a PNG file using the -g option to ensure the size is the same
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}
85 # The main logic of the script is below.
86 # All it does is ascertain which of the two functions above to call.
88 # We expect two args, the names of the input and output files.
89 test $# -eq 2 || exit 1
94 # Strip the extension from ${output}
95 outbase=`echo ${output} | sed 's/[.][^.]*$//'`
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}
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}
112 echo ${version} ${patchlevel} | grep '[0-9]!' -o && {
113 legacy_xfig ${input} ${output} ${outbase}
116 # So, is it an old version?
117 test ${version} != "3.2" -o ${patchlevel} -lt 4 && {
118 legacy_xfig ${input} ${output} ${outbase}
122 # Commented out for now to test legacy_xfig...
123 #modern_xfig ${input} ${output} ${outbase}
124 legacy_xfig ${input} ${output} ${outbase}