2002-09-03 13:52:37 +00:00
|
|
|
#! /bin/sh
|
2002-08-09 15:42:45 +00:00
|
|
|
#
|
|
|
|
# \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.
|
2002-09-03 13:52:37 +00:00
|
|
|
#
|
2002-08-09 15:42:45 +00:00
|
|
|
# This script takes a LaTeX file and generates PPM files, one per page.
|
2002-09-03 13:52:37 +00:00
|
|
|
# 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
|
2002-08-09 15:42:45 +00:00
|
|
|
# things like math equations.
|
|
|
|
|
2002-09-03 13:52:37 +00:00
|
|
|
# preview.sty can be obtained from CTAN/macros/latex/contrib/supported/preview.
|
|
|
|
|
2002-09-03 16:15:03 +00:00
|
|
|
# This script, lyxpreview2ppm.sh, takes three arguments:
|
|
|
|
# FILE: the name of the file to be converted.
|
|
|
|
# SCALEFACTOR: scale factor, used to ascertain the resolution of the
|
|
|
|
# generated imagewhich is then passed to gs.
|
|
|
|
# NDIGITS: the number of digits in the filenames generated by gs,
|
|
|
|
# ${BASE}%${NDIGITS}d.ppm
|
2002-09-03 13:52:37 +00:00
|
|
|
|
|
|
|
# If successful it will leave in dir ${DIR} a number of image files
|
2002-09-03 16:15:03 +00:00
|
|
|
# ${BASE}[0-9]\{${NDIGITS}\}.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.
|
2002-08-09 15:42:45 +00:00
|
|
|
|
2002-09-03 13:52:37 +00:00
|
|
|
# Three helper functions.
|
2002-08-09 15:42:45 +00:00
|
|
|
FIND_IT () {
|
|
|
|
which ${EXECUTABLE} > /dev/null
|
2002-09-03 13:52:37 +00:00
|
|
|
if [ $? -ne 0 ]; then
|
2002-08-09 15:42:45 +00:00
|
|
|
echo "Unable to find \"${EXECUTABLE}\". Please install."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
BAIL_OUT () {
|
|
|
|
# Remove everything except the original .tex file.
|
|
|
|
FILES=`ls ${BASE}* | sed -e "/${BASE}.tex/d"`
|
|
|
|
rm -f ${FILES} texput.log
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
2002-09-03 13:52:37 +00:00
|
|
|
REQUIRED_VERSION () {
|
|
|
|
echo "We require preview.sty version 0.73 or newer. You're using"
|
|
|
|
grep 'Package: preview' ${LOGFILE}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Preliminary check
|
2002-09-03 16:15:03 +00:00
|
|
|
if [ $# -ne 3 ]; then
|
2002-09-03 13:52:37 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2002-08-09 15:42:45 +00:00
|
|
|
# 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.
|
|
|
|
DIR=`dirname $1`
|
|
|
|
BASE=`basename $1 .tex`
|
2002-09-03 13:52:37 +00:00
|
|
|
|
|
|
|
SCALEFACTOR=$2
|
2002-09-03 16:15:03 +00:00
|
|
|
NDIGITS=$3
|
2002-09-03 13:52:37 +00:00
|
|
|
|
|
|
|
TEXFILE=${BASE}.tex
|
|
|
|
LOGFILE=${BASE}.log
|
2002-08-09 15:42:45 +00:00
|
|
|
DVIFILE=${BASE}.dvi
|
|
|
|
PSFILE=${BASE}.ps
|
|
|
|
METRICSFILE=${BASE}.metrics
|
|
|
|
|
|
|
|
# LaTeX -> DVI.
|
|
|
|
cd ${DIR}
|
|
|
|
latex ${TEXFILE}
|
2002-09-03 13:52:37 +00:00
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
echo "Failed: latex ${TEXFILE}"
|
|
|
|
BAIL_OUT
|
|
|
|
fi
|
2002-08-09 15:42:45 +00:00
|
|
|
|
2002-09-03 13:52:37 +00:00
|
|
|
# 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
|
2002-08-09 15:42:45 +00:00
|
|
|
fi
|
|
|
|
|
2002-09-03 13:52:37 +00:00
|
|
|
# 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`
|
|
|
|
|
2002-08-09 15:42:45 +00:00
|
|
|
# DVI -> PostScript
|
|
|
|
dvips -o ${PSFILE} ${DVIFILE}
|
2002-09-03 13:52:37 +00:00
|
|
|
if [ $? -ne 0 ]; then
|
2002-08-09 15:42:45 +00:00
|
|
|
echo "Failed: dvips -o ${PSFILE} ${DVIFILE}"
|
|
|
|
BAIL_OUT
|
|
|
|
fi
|
|
|
|
|
|
|
|
# PostScript -> Bitmap files
|
|
|
|
# Older versions of gs have problems with a large degree of anti-aliasing
|
|
|
|
# at high resolutions
|
|
|
|
ALPHA=4
|
|
|
|
if [ ${RESOLUTION} -gt 150 ]; then
|
|
|
|
ALPHA=2
|
|
|
|
fi
|
|
|
|
|
2002-09-03 16:15:03 +00:00
|
|
|
gs -q -dNOPAUSE -dBATCH -dSAFER \
|
|
|
|
-sDEVICE=pnmraw -sOutputFile=${BASE}%0${NDIGITS}d.ppm \
|
2002-08-09 15:42:45 +00:00
|
|
|
-dGraphicsAlphaBit=${ALPHA} -dTextAlphaBits=${ALPHA} -r${RESOLUTION} \
|
|
|
|
${PSFILE}
|
|
|
|
|
2002-09-03 13:52:37 +00:00
|
|
|
if [ $? -ne 0 ]; then
|
2002-08-09 15:42:45 +00:00
|
|
|
echo "Failed: gs ${PSFILE}"
|
|
|
|
BAIL_OUT
|
|
|
|
fi
|
|
|
|
|
2002-09-03 13:52:37 +00:00
|
|
|
# All has been successful, so remove everything except the bitmap files
|
|
|
|
# and the metrics file.
|
2002-09-03 16:15:03 +00:00
|
|
|
FILES=`ls ${BASE}* | \
|
|
|
|
sed -e "/${BASE}.metrics/d" -e "/${BASE}[0-9]\{${NDIGITS}\}.ppm/d"`
|
2002-08-09 15:42:45 +00:00
|
|
|
rm -f ${FILES}
|