#! /bin/sh
# file lyxpreview2bitmap.sh
-# Read the file COPYING
+# This file is part of LyX, the document processor.
+# Licence details can be found in the file COPYING.
#
# author Angus Leeming
# with much advice from David Kastrup, david.kastrup@t-online.de.
# \converter lyxpreview ${FORMAT} "lyxpreview2bitmap.sh" ""
# where ${FORMAT} is either ppm or png.
+# These four programs are used by the script.
+# Adjust their names to suit your setup.
+LATEX=latex
+DVIPS=dvips
+GS=gs
+PNMCROP=pnmcrop
+readonly LATEX DVIPS GS PNMCROP
+
# Three helper functions.
-FIND_IT () {
- which ${EXECUTABLE} > /dev/null
- if [ $? -ne 0 ]; then
- echo "Unable to find \"${EXECUTABLE}\". Please install."
+FIND_IT ()
+{
+ test $# -eq 1 || exit 1
+
+ type $1 > /dev/null || {
+ echo "Unable to find \"$1\". Please install."
exit 1
- fi
+ }
}
-BAIL_OUT () {
+BAIL_OUT ()
+{
+ test $# -eq 1 && echo $1
+
# Remove everything except the original .tex file.
- FILES=`ls ${BASE}* | sed -e "/${BASE}.tex/d"`
+ FILES=`ls ${BASE}* | sed -e "/${BASE}\.tex/d"`
rm -f ${FILES} texput.log
+ echo "Leaving ${BASE}.tex in ${DIR}"
exit 1
}
-REQUIRED_VERSION () {
+REQUIRED_VERSION ()
+{
+ test $# -eq 1 || exit 1
+
echo "We require preview.sty version 0.73 or newer. You're using"
- grep 'Package: preview' ${LOGFILE}
+ grep 'Package: preview' $1
}
# Preliminary check.
exit 1
fi
+# We use latex, dvips and gs, so check that they're all there.
+FIND_IT ${LATEX}
+FIND_IT ${DVIPS}
+FIND_IT ${GS}
+
# Extract the params from the argument list.
DIR=`dirname $1`
BASE=`basename $1 .tex`
GSDEVICE=png16m
GSSUFFIX=png
else
- echo "Unrecognised output format ${OUTPUTFORMAT}."
- echo "Expected either \"ppm\" or \"png\"."
- BAIL_OUT
+ BAIL_OUT "Unrecognised output format ${OUTPUTFORMAT}. \
+ Expected either \"ppm\" or \"png\"."
fi
-# We use latex, dvips and gs, so check that they're all there.
-EXECUTABLE=latex; FIND_IT
-EXECUTABLE=dvips; FIND_IT
-EXECUTABLE=gs; FIND_IT
-
# Initialise some variables.
TEXFILE=${BASE}.tex
LOGFILE=${BASE}.log
DVIFILE=${BASE}.dvi
PSFILE=${BASE}.ps
METRICSFILE=${BASE}.metrics
+readonly TEXFILE LOGFILE DVIFILE PSFILE METRICSFILE
# LaTeX -> DVI.
cd ${DIR}
-latex ${TEXFILE}
-if [ $? -ne 0 ]; then
- echo "Failed: latex ${TEXFILE}"
- BAIL_OUT
-fi
+${LATEX} ${TEXFILE} ||
+{
+ BAIL_OUT "Failed: ${LATEX} ${TEXFILE}"
+}
# Parse ${LOGFILE} to obtain bounding box info to output to
# ${METRICSFILE}.
# This extracts lines starting "Preview: Tightpage" and
# "Preview: Snippet".
-grep -E 'Preview: [ST]' ${LOGFILE} > ${METRICSFILE}
-if [ $? -ne 0 ]; then
- echo "Failed: grep -E 'Preview: [ST]' ${LOGFILE}"
- REQUIRED_VERSION
- BAIL_OUT
-fi
+grep -E 'Preview: [ST]' ${LOGFILE} > ${METRICSFILE} ||
+{
+ REQUIRED_VERSION ${LOGFILE}
+ BAIL_OUT "Failed: grep -E 'Preview: [ST]' ${LOGFILE}"
+}
# Parse ${LOGFILE} to obtain ${RESOLUTION} for the gs process to follow.
# 1. Extract font size from a line like "Preview: Fontsize 20.74pt"
# Use grep for speed and because it gives an error if the line is
# not found.
-LINE=`grep 'Preview: Fontsize' ${LOGFILE}`
-if [ $? -ne 0 ]; then
- echo "Failed: grep 'Preview: Fontsize' ${LOGFILE}"
- REQUIRED_VERSION
- BAIL_OUT
-fi
+LINE=`grep 'Preview: Fontsize' ${LOGFILE}` ||
+{
+ REQUIRED_VERSION ${LOGFILE}
+ BAIL_OUT "Failed: grep 'Preview: Fontsize' ${LOGFILE}"
+}
# The sed script strips out everything that won't form a decimal number
# from the line. It bails out after the first match has been made in
# case there are multiple lines "Preview: Fontsize". (There shouldn't
# "Preview: Magnification 2074"
# If no such line found, default to MAGNIFICATION=1000.
LINE=`grep 'Preview: Magnification' ${LOGFILE}`
-if [ $? -ne 0 ]; then
- MAGNIFICATION=1000
-else
+if LINE=`grep 'Preview: Magnification' ${LOGFILE}`; then
# Strip out everything that won't form an /integer/.
MAGNIFICATION=`echo "${LINE}" | sed 's/[^0-9]//g; 1q'`
+else
+ MAGNIFICATION=1000
fi
# 3. Compute resolution.
| bc`
# DVI -> PostScript
-dvips -o ${PSFILE} ${DVIFILE}
-if [ $? -ne 0 ]; then
- echo "Failed: dvips -o ${PSFILE} ${DVIFILE}"
- BAIL_OUT
-fi
+${DVIPS} -o ${PSFILE} ${DVIFILE} ||
+{
+ BAIL_OUT "Failed: ${DVIPS} -o ${PSFILE} ${DVIFILE}"
+}
# PostScript -> Bitmap files
# Older versions of gs have problems with a large degree of
ALPHA=2
fi
-gs -q -dNOPAUSE -dBATCH -dSAFER \
+${GS} -q -dNOPAUSE -dBATCH -dSAFER \
-sDEVICE=${GSDEVICE} -sOutputFile=${BASE}%d.${GSSUFFIX} \
-dGraphicsAlphaBit=${ALPHA} -dTextAlphaBits=${ALPHA} \
- -r${RESOLUTION} ${PSFILE}
-
-if [ $? -ne 0 ]; then
- echo "Failed: gs ${PSFILE}"
- BAIL_OUT
-fi
+ -r${RESOLUTION} ${PSFILE} ||
+{
+ BAIL_OUT "Failed: ${GS} ${PSFILE}"
+}
# All has been successful, so remove everything except the bitmap files
# and the metrics file.
FILES=`ls ${BASE}* | sed -e "/${BASE}.metrics/d" \
-e "/${BASE}\([0-9]*\).${GSSUFFIX}/d"`
rm -f ${FILES} texput.log
+
+# The bitmap files can have large amounts of whitespace to the left and
+# right. This can be cropped if so desired.
+CROP=1
+type ${PNMCROP} > /dev/null || CROP=0
+
+# There's no point cropping the image if using PNG images. If you want to
+# crop, use PPM.
+# Apparently dvipng will support cropping at some stage in the future...
+if [ ${CROP} -eq 1 -a "${GSDEVICE}" = "pnmraw" ]; then
+ for FILE in ${BASE}*.${GSSUFFIX}
+ do
+ if ${PNMCROP} -left ${FILE} 2> /dev/null |\
+ ${PNMCROP} -right 2> /dev/null > ${BASE}.tmp; then
+ mv ${BASE}.tmp ${FILE}
+ else
+ rm -f ${BASE}.tmp
+ fi
+ done
+ rm -f ${BASE}.tmp
+fi
+
+echo "Previews generated!"