# to generate ${base}.pdftex_t
# Thereafter, you need only '\input{${base}.pdftex_t}' in your latex document.
+# The external programs
+FIG2DEV=fig2dev
+# Used only by legacy_xfig
+GS=gs
+
+find_exe() {
+ test $# -eq 1 || exit 1
+
+ type $1 > /dev/null || {
+ echo "Unable to find \"$1\". Please install."
+ exit 1
+ }
+}
+
# modern_xfig() and legacy_xfig() are the functions that do all the work.
# Modern versions of xfig can output the image without "special" text as
# a PDF file ${base}.pdf and place the text in a LaTeX file
# ${base}.pdftex_t for typesetting by pdflatex itself.
modern_xfig() {
- input=$1.fig
- pdftex=$1.pdf
- pdftex_t=$1.pdftex_t
+ # Can we find fig2dev?
+ find_exe ${FIG2DEV}
- fig2dev -Lpdftex ${input} ${pdftex}
- fig2dev -Lpdftex_t -p$1 ${input} ${pdftex_t}
+ input=$1
+ pdftex_t=$2
+ pdftex=$3.pdf
+
+ ${FIG2DEV} -Lpdftex -p1 ${input} ${pdftex}
+ ${FIG2DEV} -Lpdftex_t -p${outbase} ${input} ${pdftex_t}
exit 0;
}
+
+# This function is used only by legacy_xfig.
+# It manipulates the Bounding Box info to enable gs to produce
+# the appropriate PDF file from an EPS one.
+# The generated PostScript commands are extracted from epstopdf, distributed
+# with tetex.
+clean_epsfile() {
+ test $# -eq 1 || exit 1
+
+ # No bounding box info
+ grep '%%BoundingBox' $1 > /dev/null || return 1;
+
+ bbox=`sed -n '/^%%BoundingBox/p' $1`
+ llx=`echo ${bbox} | cut -d' ' -f2`
+ lly=`echo ${bbox} | cut -d' ' -f3`
+ urx=`echo ${bbox} | cut -d' ' -f4`
+ ury=`echo ${bbox} | cut -d' ' -f5`
+
+ width=`expr $urx - $llx`
+ height=`expr $ury - $lly`
+ xoffset=`expr 0 - $llx`
+ yoffset=`expr 0 - $lly`
+
+ temp=$1.??
+ sed "/^%%BoundingBox/{
+s/^\(%%BoundingBox:\).*/\1 0 0 ${width} ${height}\\
+<< \/PageSize [${width} ${height}] >> setpagedevice\\
+gsave ${xoffset} ${yoffset} translate/
+}" $1 > $temp
+
+ mv -f $temp $1
+}
+
+
# Older versions of xfig cannot do this, so we emulate the behaviour using
# pstex and pstex_t output.
legacy_xfig() {
- input=$1.fig
- pstex=$1.pstex
- png=$1.png
- pdftex_t=$1.pdftex_t
+ # Can we find fig2dev and epstopdf?
+ find_exe ${FIG2DEV}
+ find_exe ${GS}
- fig2dev -Lpstex ${input} ${pstex}
- fig2dev -Lpstex_t -p$1 ${input} ${pdftex_t}
+ input=$1
+ pdftex_t=$2
+ pdf=$3.pdf
+ pstex=$3.pstex
- # Convert the ${pstex} EPS file (free of "special" text) to PDF format
- # using gs.
+ ${FIG2DEV} -Lpstex ${input} ${pstex}
+ ${FIG2DEV} -Lpstex_t -p${outbase} ${input} ${pdftex_t}
- # gs is extremely fussy about the EPS files it converts, so ensure it is
- # "clean" first.
- clean=${pstex}.$$
- eps2eps ${pstex} ${clean}
+ # Convert the ${pstex} EPS file (free of "special" text) to PDF format
+ # using gs
+ clean_epsfile ${pstex}
+ ${GS} -q -dNOPAUSE -dBATCH -dSAFER \
+ -sDEVICE=pdfwrite -sOutputFile=${pdf} ${pstex}
rm -f ${pstex}
- # Extract the width and height of the image using gs' bbox device.
- # Ie, take output that includes line "%%BoundingBox: 0 0 <width> <height>"
- # and rewrite it as "-g<width>x<height>"
- geometry=`gs -q -dSAFER -dNOPAUSE -dBATCH -sDEVICE=bbox ${clean} 2>&1 | \
- sed '/^%%BoundingBox/! d' | cut -d' ' -f4,5 | \
- sed 's/^\([0-9]\{1,\}\) \([0-9]\{1,\}\)$/-g\1x\2/'`
-
- # Generate a PNG file using the -g option to ensure the size is the same
- # as the original.
- # If we're using a version of gs that does not have a bbox device, then
- # ${geometry} = "", so there are no unwanted side effects.
- gs -q -dSAFER -dBATCH -dNOPAUSE ${geometry} -sDEVICE=png16m \
- -sOutputFile=${png} ${clean}
- rm -f ${clean}
-
exit 0;
}
# The main logic of the script is below.
# All it does is ascertain which of the two functions above to call.
-# We expect a single arg, the name of the input file.
-test $# -eq 1 || exit 1
+# We expect two args, the names of the input and output files.
+test $# -eq 2 || exit 1
-# Remove the .fig extension
-input=`basename $1`
-base=`echo ${input} | sed 's/\.fig$//'`
+input=$1
+output=$2
+
+# Fail silently if the file doesn't exist
+test -r $input || exit 0
+
+# Strip the extension from ${output}
+outbase=`echo ${output} | sed 's/[.][^.]*$//'`
# Ascertain whether fig2dev is "modern enough".
-# Here "modern" means "fig2dev Version 3.2 Patchlevel 4"
-version_info=`fig2dev -h | sed '/^fig2dev/! d'`
-# If no line begins "fig2dev" then default to legacy_xfig
-test "x${version_info}" = "x" && legacy_xfig ${base}
-
-version=`echo ${version_info} | cut -d' ' -f3`
-patchlevel=`echo ${version_info} | cut -d' ' -f5`
-# If we cannot extract the version of patchlevel info
-# then default to legacy_xfig
-test "x${version}" = "x" -o "x${patchlevel}" = "x" && legacy_xfig ${base}
-echo ${version} ${patchlevel} | grep '[0-9]!' -o && legacy_xfig ${base}
-
-# So, is it am old version?
-test ${version} != "3.2" -o ${patchlevel} -lt 4 && legacy_xfig ${base}
-# I guess not ;-)
-modern_xfig ${base}
+# If it is, then the help info will mention "pdftex_t" as one of the
+# available outputs.
+CONVERT_IT=modern_xfig
+${FIG2DEV} -h | grep 'pdftex_t' > /dev/null || CONVERT_IT=legacy_xfig
+
+${CONVERT_IT} ${input} ${output} ${outbase}
# The end