]> git.lyx.org Git - lyx.git/blobdiff - lib/scripts/lyxpreview2bitmap.sh
Enable the external inset to output different flavours of latex.
[lyx.git] / lib / scripts / lyxpreview2bitmap.sh
index 119f1df88a115325ad705014ba3f35527a87ca01..3efc75408cf5aa34acdae3265533cb254944feca 100644 (file)
 #        \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.
@@ -78,6 +94,11 @@ if [ $# -ne 3 ]; then
        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`
@@ -91,52 +112,44 @@ elif [ "$3" = "png" ]; then
        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
@@ -148,11 +161,11 @@ LATEXFONT=`echo "${LINE}" | sed 's/[^0-9\.]//g; 1q'`
 # "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.
@@ -162,11 +175,10 @@ RESOLUTION=`echo "scale=2; \
        | 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
@@ -181,15 +193,13 @@ if [ ${INT_RESOLUTION} -gt 150 ]; then
        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.
@@ -200,11 +210,7 @@ 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
-
-which pnmcrop > /dev/null
-if [ $? -ne 0 ]; then
-       CROP=0
-fi
+type ${PNMCROP} > /dev/null || CROP=0
 
 # There's no point cropping the image if using PNG images. If you want to
 # crop, use PPM.
@@ -212,8 +218,8 @@ fi
 if [ ${CROP} -eq 1 -a "${GSDEVICE}" = "pnmraw" ]; then
        for FILE in ${BASE}*.${GSSUFFIX}
        do
-               pnmcrop -left ${FILE} | pnmcrop -right > ${BASE}.tmp
-               if [ $? -eq 0 ]; then
+               if ${PNMCROP} -left ${FILE} 2> /dev/null |\
+                  ${PNMCROP} -right  2> /dev/null > ${BASE}.tmp; then
                        mv ${BASE}.tmp ${FILE}
                else
                        rm -f ${BASE}.tmp
@@ -221,3 +227,5 @@ if [ ${CROP} -eq 1 -a "${GSDEVICE}" = "pnmraw" ]; then
        done
        rm -f ${BASE}.tmp
 fi
+
+echo "Previews generated!"