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 # The external programs
21 # Used only by legacy_xfig
25 test $# -eq 1 || exit 1
27 type $1 > /dev/null || {
28 echo "Unable to find \"$1\". Please install."
33 # modern_xfig() and legacy_xfig() are the functions that do all the work.
35 # Modern versions of xfig can output the image without "special" text as
36 # a PDF file ${base}.pdf and place the text in a LaTeX file
37 # ${base}.pdftex_t for typesetting by pdflatex itself.
39 # Can we find fig2dev?
46 ${FIG2DEV} -Lpdftex -p1 ${input} ${pdftex}
47 ${FIG2DEV} -Lpdftex_t -p${outbase} ${input} ${pdftex_t}
53 # This function is used only by legacy_xfig.
54 # It manipulates the Bounding Box info to enable gs to produce
55 # the appropriate PDF file from an EPS one.
56 # The generated PostScript commands are extracted from epstopdf, distributed
59 test $# -eq 1 || exit 1
61 # No bounding box info
62 grep '%%BoundingBox' $1 > /dev/null || return 1;
64 bbox=`sed -n '/^%%BoundingBox/p' $1`
65 llx=`echo ${bbox} | cut -d' ' -f2`
66 lly=`echo ${bbox} | cut -d' ' -f3`
67 urx=`echo ${bbox} | cut -d' ' -f4`
68 ury=`echo ${bbox} | cut -d' ' -f5`
70 width=`expr $urx - $llx`
71 height=`expr $ury - $lly`
72 xoffset=`expr 0 - $llx`
73 yoffset=`expr 0 - $lly`
76 sed "/^%%BoundingBox/{
77 s/^\(%%BoundingBox:\).*/\1 0 0 ${width} ${height}\\
78 << \/PageSize [${width} ${height}] >> setpagedevice\\
79 gsave ${xoffset} ${yoffset} translate/
86 # Older versions of xfig cannot do this, so we emulate the behaviour using
87 # pstex and pstex_t output.
89 # Can we find fig2dev and epstopdf?
98 ${FIG2DEV} -Lpstex ${input} ${pstex}
99 ${FIG2DEV} -Lpstex_t -p${outbase} ${input} ${pdftex_t}
101 # Convert the ${pstex} EPS file (free of "special" text) to PDF format
103 clean_epsfile ${pstex}
104 ${GS} -q -dNOPAUSE -dBATCH -dSAFER \
105 -sDEVICE=pdfwrite -sOutputFile=${pdf} ${pstex}
111 # The main logic of the script is below.
112 # All it does is ascertain which of the two functions above to call.
114 # We expect two args, the names of the input and output files.
115 test $# -eq 2 || exit 1
120 # Fail silently if the file doesn't exist
121 test -r $input || exit 0
123 # Strip the extension from ${output}
124 outbase=`echo ${output} | sed 's/[.][^.]*$//'`
126 # Ascertain whether fig2dev is "modern enough".
127 # If it is, then the help info will mention "pdftex_t" as one of the
129 CONVERT_IT=modern_xfig
130 ${FIG2DEV} -h | grep 'pdftex_t' > /dev/null || CONVERT_IT=legacy_xfig
132 ${CONVERT_IT} ${input} ${output} ${outbase}