]> git.lyx.org Git - lyx.git/blobdiff - lib/scripts/lyxpreview2ppm.sh
Activate the code using preview.sty 0.73. Ditch support for older versions.
[lyx.git] / lib / scripts / lyxpreview2ppm.sh
index 076a8807b87405dd043f1bceafcc34d2d6060237..bf20cca108baa5d0e2e7d0514284e94b4514729a 100644 (file)
@@ -1,30 +1,32 @@
-#!/bin/sh
+#! /bin/sh
 #
 # \file lyxpreview2ppm.sh
 # Copyright 2002 the LyX Team
 # Read the file COPYING
 #
 # \author Angus Leeming, leeming@lyx.org
-#
 # with much help from David Kastrup, david.kastrup@t-online.de.
-# The sed script was created with advice from Praveen D V, praveend@sasken.com
-# and the sed users' list, sed-users@yahoogroups.com.
-
+#
 # This script takes a LaTeX file and generates PPM files, one per page.
-# The idea is to use it with preview.sty to create small bitmap previews of
+# The idea is to use it with preview.sty from the preview-latex project
+# (http://preview-latex.sourceforge.net/) to create small bitmap previews of
 # things like math equations.
 
-# The script takes two arguments, the name of the file to be converted and
-# the resolution of the generated image, to be passed to gs.
-if [ $# -ne 2 ]; then
-       exit 1
-fi
+# preview.sty can be obtained from CTAN/macros/latex/contrib/supported/preview.
+
+# This script, lyxpreview2ppm.sh, takes two arguments, the name of the file
+# to be converted and a scale factor, used to ascertain the resolution of the
+# generated image which is then passed to gs.
+
+# If successful it will leave in dir ${DIR} a number of image files
+# ${BASE}[0-9]\{3\}.ppm and a file ${BASE}.metrics containing info needed by
+# LyX to position the images correctly on the screen. All other files ${BASE}*
+# will be deleted.
 
-# A couple of helper functions
+# Three helper functions.
 FIND_IT () {
        which ${EXECUTABLE} > /dev/null
-       STATUS=$?
-       if [ ${STATUS} -ne 0 ]; then
+       if [ $? -ne 0 ]; then
                echo "Unable to find \"${EXECUTABLE}\". Please install."
                exit 1
        fi
@@ -37,52 +39,84 @@ BAIL_OUT () {
        exit 1
 }
 
+REQUIRED_VERSION () {
+       echo "We require preview.sty version 0.73 or newer. You're using"
+       grep 'Package: preview' ${LOGFILE}
+}
+
+# Preliminary check
+if [ $# -ne 2 ]; then
+       exit 1
+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=`basename $1`
-RESOLUTION=$2
-
 DIR=`dirname $1`
 BASE=`basename $1 .tex`
+
+SCALEFACTOR=$2
+
+TEXFILE=${BASE}.tex
+LOGFILE=${BASE}.log
 DVIFILE=${BASE}.dvi
 PSFILE=${BASE}.ps
-METRICS=${BASE}.metrics
+METRICSFILE=${BASE}.metrics
 
 # LaTeX -> DVI.
 cd ${DIR}
 latex ${TEXFILE}
-STATUS=$?
-if [ ${STATUS} -ne 0 ]; then
-       # LaTeX failed.
-       # preview.sty has known problems with the showlabels option,
-       # so remove it and try again.
-       sed -e "/^[\]usepackage\(.*\){preview}/s/,showlabels//" \
-               < ${TEXFILE} > .${TEXFILE}
-       cmp -s ${TEXFILE} .${TEXFILE}
-       STATUS=$?
-       if [ ${STATUS} -eq 0 ]; then
-               rm -f .${TEXFILE}
-               echo "Failed: latex ${TEXFILE}"
-               BAIL_OUT
-       fi
+if [ $? -ne 0 ]; then
+       echo "Failed: latex ${TEXFILE}"
+       BAIL_OUT
+fi
 
-       mv -f .${TEXFILE} ${TEXFILE}
-       latex ${TEXFILE}
-       STATUS=$?
-       if [ ${STATUS} -ne 0 ]; then
-               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
+       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
+       echo "Failed: grep 'Preview: Fontsize' ${LOGFILE}"
+       REQUIRED_VERSION
+       BAIL_OUT
+fi
+# Use "" quotes in the echo to preserve newlines (technically IFS separators).
+# 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 be.)
+LATEXFONT=`echo "${LINE}" | sed 's/[^0-9\.]//g; 1q'`
+
+# 2. Extract magnification from a line like "Preview: Magnification 2074"
+# If no such line found, default to MAGNIFICATION=1000.
+LINE=`grep 'Preview: Magnification' ${LOGFILE}`
+if [ $? -ne 0 ]; then
+       MAGNIFICATION=1000
+else
+       # The sed script strips out everything that won't form an /integer/.
+       MAGNIFICATION=`echo "${LINE}" | sed 's/[^0-9]//g; 1q'`
+fi
+
+# 3. Compute resolution.
+# "bc" allows floating-point arithmetic, unlike "expr" or "dc".
+RESOLUTION=`echo "scale=2; \
+               ${SCALEFACTOR} * (10/${LATEXFONT}) * (1000/${MAGNIFICATION})" \
+               | bc`
+
 # DVI -> PostScript
 dvips -o ${PSFILE} ${DVIFILE}
-STATUS=$?
-if [ ${STATUS} -ne 0 ]; then
+if [ $? -ne 0 ]; then
        echo "Failed: dvips -o ${PSFILE} ${DVIFILE}"
        BAIL_OUT
 fi
@@ -99,67 +133,12 @@ gs -q -dNOPAUSE -dBATCH -dSAFER -sDEVICE=pnmraw -sOutputFile=${BASE}%03d.ppm \
     -dGraphicsAlphaBit=${ALPHA} -dTextAlphaBits=${ALPHA} -r${RESOLUTION} \
     ${PSFILE}
 
-STATUS=$?
-if [ ${STATUS} -ne 0 ]; then
+if [ $? -ne 0 ]; then
        echo "Failed: gs ${PSFILE}"
        BAIL_OUT
 fi
 
-# Attempt to generate a file ${METRICS} that contains only the tightpage
-# bounding box info, extract from ${PSFILE}
-
-# 1. Create a file containing the sed instructions
-SEDSCRIPT=bbox.sed
-cat - > ${SEDSCRIPT} <<EOF
-# Delete everything that's enclosed between %%BeginDocument and %%EndDocument
-/^\%\%BeginDocument/,/^\%\%EndDocument/d
-
-# Extract the tightpage bounding box info.
-# Given this snippet:
-# %%Page: 1 1
-# 1 0 bop
-# -32890 -32890 32890 32890 492688 0 744653
-# The sed command gives this:
-# %%Page 1: -32890 -32890 32890 32890 492688 0 744653
-
-/^\%\%Page:/{
-  s/\: \(.*\) .*$/ \1: /;N;N
-  s/\n[^\n]*\n//p
-}
-
-# Delete everything (so only the stuff that's printed, above, goes into the
-# metrics file).
-d
-EOF
-
-# 2. Run sed!
-sed -f ${SEDSCRIPT} < ${PSFILE} > ${METRICS}
-rm -f ${SEDSCRIPT}
-
-# The ppm files have spurious (?! say some !) white space on the left and right
-# sides. If you want this removed set REMOVE_WS=1.
-REMOVE_WS=0
-
-which pnmcrop > /dev/null
-STATUS=$?
-
-if [ ${STATUS} -ne 0 ]; then
-       REMOVE_WS=0
-fi
-
-if [ ${REMOVE_WS} -eq 1 ]; then
-       TMP=.${BASE}.ppm
-       for FILE in `ls ${BASE}???.ppm`
-       do
-               pnmcrop -left ${FILE} | pnmcrop -right > ${TMP}
-               STATUS=$?
-               if [ ${STATUS} -eq 0 ]; then
-                       mv -f ${TMP} ${FILE}
-               fi
-       done
-fi
-
-# All was successful, so remove everything except the ppm files and the
-# metrics file.
+# 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]\{3\}.ppm/d"`
 rm -f ${FILES}