3 # file lyxpreview2bitmap.sh
4 # This file is part of LyX, the document processor.
5 # Licence details can be found in the file COPYING.
8 # with much advice from David Kastrup, david.kastrup@t-online.de.
10 # Full author contact details are available in file CREDITS
12 # This script takes a LaTeX file and generates bitmap image files,
15 # The idea is to use it with preview.sty from the preview-latex project
16 # (http://preview-latex.sourceforge.net/) to create small bitmap
17 # previews of things like math equations.
19 # preview.sty can be obtained from
20 # CTAN/macros/latex/contrib/supported/preview.
22 # This script takes three arguments:
23 # TEXFILE: the name of the .tex file to be converted.
24 # SCALEFACTOR: a scale factor, used to ascertain the resolution of the
25 # generated image which is then passed to gs.
26 # OUTPUTFORMAT: the format of the output bitmap image files.
27 # Two formats are recognised: "ppm" and "png".
29 # If successful, this script will leave in dir ${DIR}:
30 # a (possibly large) number of image files with names like
31 # ${BASE}\([0-9]*\).${SUFFIX} where SUFFIX is ppm or png.
32 # a file containing info needed by LyX to position the images correctly
35 # All other files ${BASE}* will be deleted.
37 # A quick note on the choice of OUTPUTFORMAT:
39 # In general files in PPM format are 10-100 times larger than the
40 # equivalent files in PNG format. Larger files results in longer
41 # reading and writing times as well as greater disk usage.
43 # However, whilst the Qt image loader can load files in PNG format
44 # without difficulty, the xforms image loader cannot. They must first
45 # be converted to a loadable format (eg PPM!). Thus, previews will take
46 # longer to appear if the xforms loader is used to load snippets in
49 # You can always experiment by adding a line to your
50 # ${LYXUSERDIR}/preferences file
51 # \converter lyxpreview ${FORMAT} "lyxpreview2bitmap.sh" ""
52 # where ${FORMAT} is either ppm or png.
54 # These programs are used by the script.
55 # Adjust their names to suit your setup.
56 test -n "$LATEX" || LATEX=latex
57 DVIPNG=/home/angus/preview-latex/devel/dvipng/dvipng
61 readonly LATEX DVIPS GS PNMCROP
63 # Some helper functions.
66 test $# -eq 1 || exit 1
68 type $1 > /dev/null || {
69 echo "Unable to find \"$1\". Please install."
76 test $# -eq 1 && echo $1
78 # Remove everything except the original .tex file.
79 FILES=`ls ${BASE}* | sed -e "/${BASE}\.tex/d"`
80 rm -f ${FILES} texput.log
81 echo "Leaving ${BASE}.tex in ${DIR}"
87 test $# -eq 1 || exit 1
89 echo "We require preview.sty version 0.73 or newer. You're using"
90 grep 'Package: preview' $1
93 CHECK_FOR_PROGRAMS () {
94 test $# -eq 1 || exit 1
95 if [ $1 = "ppm" ]; then
96 # We use latex, dvips and gs, so check that they're all there.
100 elif [ "$3" = "png" ]; then
101 # We use latex and dvipng
107 GENERATE_PPM_FILES () {
108 test $# -eq 2 || exit 1
116 ${DVIPS} -o ${PSFILE} ${DVIFILE} || {
117 BAIL_OUT "Failed: ${DVIPS} -o ${PSFILE} ${DVIFILE}"
120 # PostScript -> Bitmap files
121 # Older versions of gs have problems with a large degree of
122 # anti-aliasing at high resolutions
124 # test expects integer arguments.
125 # ${RESOLUTION} may be a float. Truncate it.
126 INT_RESOLUTION=`echo "${RESOLUTION} / 1" | bc`
129 if [ ${INT_RESOLUTION} -gt 150 ]; then
136 ${GS} -q -dNOPAUSE -dBATCH -dSAFER \
137 -sDEVICE=${GSDEVICE} -sOutputFile=${BASE}%d.${GSSUFFIX} \
138 -dGraphicsAlphaBit=${ALPHA} -dTextAlphaBits=${ALPHA} \
139 -r${RESOLUTION} ${PSFILE} ||
141 BAIL_OUT "Failed: ${GS} ${PSFILE}"
144 # All has been successful, so remove everything except the bitmap files
145 # and the metrics file.
146 FILES=`ls ${BASE}* | sed -e "/${BASE}.metrics/d" \
147 -e "/${BASE}\([0-9]*\).${GSSUFFIX}/d"`
148 # rm -f ${FILES} texput.log
150 # The bitmap files can have large amounts of whitespace to the left and
151 # right. This can be cropped if so desired.
153 type ${PNMCROP} > /dev/null || CROP=0
155 if [ ${CROP} -eq 1 ]; then
156 for FILE in ${BASE}*.${GSSUFFIX}
158 if ${PNMCROP} -left ${FILE} 2> /dev/null |\
159 ${PNMCROP} -right 2> /dev/null > ${BASE}.tmp; then
160 mv ${BASE}.tmp ${FILE}
169 GENERATE_PNG_FILES () {
170 test $# -eq 2 || exit 1
175 ${DVIPNG} -D${RESOLUTION} -o${BASE}%d.png ${DVIFILE}
181 if [ $# -ne 3 ]; then
185 # Extract the params from the argument list.
187 BASE=`basename $1 .tex`
192 if [ "${OUTPUTFORMAT}" != "ppm" -a "${OUTPUTFORMAT}" != "png" ]; then
193 BAIL_OUT "Unrecognised output format ${OUTPUTFORMAT}. \
194 Expected either \"ppm\" or \"png\"."
197 CHECK_FOR_PROGRAMS ${OUTPUTFORMAT}
199 # Initialise some variables.
203 METRICSFILE=${BASE}.metrics
204 readonly TEXFILE LOGFILE DVIFILE METRICSFILE
208 ${LATEX} ${TEXFILE} ||
210 BAIL_OUT "Failed: ${LATEX} ${TEXFILE}"
213 # Parse ${LOGFILE} to obtain bounding box info to output to
215 # This extracts lines starting "Preview: Tightpage" and
216 # "Preview: Snippet".
217 grep -E 'Preview: [ST]' ${LOGFILE} > ${METRICSFILE} ||
219 REQUIRED_VERSION ${LOGFILE}
220 BAIL_OUT "Failed: grep -E 'Preview: [ST]' ${LOGFILE}"
223 # Parse ${LOGFILE} to obtain ${RESOLUTION} for the gs process to follow.
224 # 1. Extract font size from a line like "Preview: Fontsize 20.74pt"
225 # Use grep for speed and because it gives an error if the line is
227 LINE=`grep 'Preview: Fontsize' ${LOGFILE}` ||
229 REQUIRED_VERSION ${LOGFILE}
230 BAIL_OUT "Failed: grep 'Preview: Fontsize' ${LOGFILE}"
232 # The sed script strips out everything that won't form a decimal number
233 # from the line. It bails out after the first match has been made in
234 # case there are multiple lines "Preview: Fontsize". (There shouldn't
236 # Note: use "" quotes in the echo to preserve newlines.
237 LATEXFONT=`echo "${LINE}" | sed 's/[^0-9\.]//g; 1q'`
239 # 2. Extract magnification from a line like
240 # "Preview: Magnification 2074"
241 # If no such line found, default to MAGNIFICATION=1000.
242 LINE=`grep 'Preview: Magnification' ${LOGFILE}`
243 if LINE=`grep 'Preview: Magnification' ${LOGFILE}`; then
244 # Strip out everything that won't form an /integer/.
245 MAGNIFICATION=`echo "${LINE}" | sed 's/[^0-9]//g; 1q'`
250 # 3. Compute resolution.
251 # "bc" allows floating-point arithmetic, unlike "expr" or "dc".
252 RESOLUTION=`echo "scale=2; \
253 ${SCALEFACTOR} * (10/${LATEXFONT}) * (1000/${MAGNIFICATION})" \
256 # 4. Generate the bitmap files and clean-up.
257 if [ "${OUTPUTFORMAT}" = "ppm" ]; then
258 GENERATE_PPM_FILES ${BASE} ${RESOLUTION}
260 GENERATE_PNG_FILES ${BASE} ${RESOLUTION}
263 echo "Previews generated!"