]> git.lyx.org Git - lyx.git/blobdiff - lib/scripts/lyxpreview2bitmap.sh
Try and make convertDefault a little more robust.
[lyx.git] / lib / scripts / lyxpreview2bitmap.sh
index 119f1df88a115325ad705014ba3f35527a87ca01..7e4ece1f96a1914b1a98720a6404f6fc72ff8a9d 100644 (file)
 # where ${FORMAT} is either ppm or png.
 
 # 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 ()
+{
        # Remove everything except the original .tex file.
        FILES=`ls ${BASE}* | sed -e "/${BASE}.tex/d"`
        rm -f ${FILES} texput.log
@@ -68,7 +71,8 @@ BAIL_OUT () {
        exit 1
 }
 
-REQUIRED_VERSION () {
+REQUIRED_VERSION ()
+{
        echo "We require preview.sty version 0.73 or newer. You're using"
        grep 'Package: preview' ${LOGFILE}
 }
@@ -97,9 +101,9 @@ else
 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
+FIND_IT latex
+FIND_IT dvips
+FIND_IT gs
 
 # Initialise some variables.
 TEXFILE=${BASE}.tex
@@ -110,33 +114,33 @@ METRICSFILE=${BASE}.metrics
 
 # LaTeX -> DVI.
 cd ${DIR}
-latex ${TEXFILE}
-if [ $? -ne 0 ]; then
+latex ${TEXFILE} ||
+{
        echo "Failed: latex ${TEXFILE}"
        BAIL_OUT
-fi
+}
 
 # 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
+grep -E 'Preview: [ST]' ${LOGFILE} > ${METRICSFILE} ||
+{
        echo "Failed: grep -E 'Preview: [ST]' ${LOGFILE}"
        REQUIRED_VERSION
        BAIL_OUT
-fi
+}
 
 # 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
+LINE=`grep 'Preview: Fontsize' ${LOGFILE}` ||
+{
        echo "Failed: grep 'Preview: Fontsize' ${LOGFILE}"
        REQUIRED_VERSION
        BAIL_OUT
-fi
+}
 # 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 +152,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 +166,11 @@ RESOLUTION=`echo "scale=2; \
        | bc`
 
 # DVI -> PostScript
-dvips -o ${PSFILE} ${DVIFILE}
-if [ $? -ne 0 ]; then
+dvips -o ${PSFILE} ${DVIFILE} ||
+{
        echo "Failed: dvips -o ${PSFILE} ${DVIFILE}"
        BAIL_OUT
-fi
+}
 
 # PostScript -> Bitmap files
 # Older versions of gs have problems with a large degree of
@@ -184,12 +188,11 @@ fi
 gs -q -dNOPAUSE -dBATCH -dSAFER \
        -sDEVICE=${GSDEVICE} -sOutputFile=${BASE}%d.${GSSUFFIX} \
        -dGraphicsAlphaBit=${ALPHA} -dTextAlphaBits=${ALPHA} \
-       -r${RESOLUTION} ${PSFILE}
-
-if [ $? -ne 0 ]; then
+       -r${RESOLUTION} ${PSFILE} ||
+{
        echo "Failed: gs ${PSFILE}"
        BAIL_OUT
-fi
+}
 
 # All has been successful, so remove everything except the bitmap files
 # and the metrics file.
@@ -200,11 +203,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 +211,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 +220,5 @@ if [ ${CROP} -eq 1 -a "${GSDEVICE}" = "pnmraw" ]; then
        done
        rm -f ${BASE}.tmp
 fi
+
+echo "Previews generated!"