3 # \file lyxpreview2ppm.sh
4 # Copyright 2002 the LyX Team
5 # Read the file COPYING
7 # \author Angus Leeming, leeming@lyx.org
8 # with much help from David Kastrup, david.kastrup@t-online.de.
10 # This script takes a LaTeX file and generates PPM files, one per page.
11 # The idea is to use it with preview.sty from the preview-latex project
12 # (http://preview-latex.sourceforge.net/) to create small bitmap previews of
13 # things like math equations.
15 # preview.sty can be obtained from CTAN/macros/latex/contrib/supported/preview.
17 # This script, lyxpreview2ppm.sh, takes two arguments, the name of the file
18 # to be converted and a scale factor, used to ascertain the resolution of the
19 # generated image which is then passed to gs.
21 # If successful it will leave in dir ${DIR} a number of image files
22 # ${BASE}[0-9]\{3\}.ppm and a file ${BASE}.metrics containing info needed by
23 # LyX to position the images correctly on the screen. All other files ${BASE}*
26 # Three helper functions.
28 which ${EXECUTABLE} > /dev/null
30 echo "Unable to find \"${EXECUTABLE}\". Please install."
36 # Remove everything except the original .tex file.
37 FILES=`ls ${BASE}* | sed -e "/${BASE}.tex/d"`
38 rm -f ${FILES} texput.log
43 echo "We require preview.sty version 0.73 or newer. You're using"
44 grep 'Package: preview' ${LOGFILE}
52 # We use latex, dvips and gs, so check that they're all there.
53 EXECUTABLE=latex; FIND_IT
54 EXECUTABLE=dvips; FIND_IT
55 EXECUTABLE=gs; FIND_IT
57 # Initialise some variables.
59 BASE=`basename $1 .tex`
67 METRICSFILE=${BASE}.metrics
73 echo "Failed: latex ${TEXFILE}"
77 # Parse ${LOGFILE} to obtain bounding box info to output to ${METRICSFILE}.
78 # This extracts lines starting "Preview: Tightpage" and "Preview: Snippet".
79 grep -E 'Preview: [ST]' ${LOGFILE} > ${METRICSFILE}
81 echo "Failed: grep -E 'Preview: [ST]' ${LOGFILE}"
86 # Parse ${LOGFILE} to obtain ${RESOLUTION} for the gs process to follow.
87 # 1. Extract font size from a line like "Preview: Fontsize 20.74pt"
88 # Use grep for speed and because it gives an error if the line is not found.
89 LINE=`grep 'Preview: Fontsize' ${LOGFILE}`
91 echo "Failed: grep 'Preview: Fontsize' ${LOGFILE}"
95 # Use "" quotes in the echo to preserve newlines (technically IFS separators).
96 # The sed script strips out everything that won't form a decimal number from the
97 # line. It bails out after the first match has been made in case there are
98 # multiple lines "Preview: Fontsize". (There shouldn't be.)
99 LATEXFONT=`echo "${LINE}" | sed 's/[^0-9\.]//g; 1q'`
101 # 2. Extract magnification from a line like "Preview: Magnification 2074"
102 # If no such line found, default to MAGNIFICATION=1000.
103 LINE=`grep 'Preview: Magnification' ${LOGFILE}`
104 if [ $? -ne 0 ]; then
107 # The sed script strips out everything that won't form an /integer/.
108 MAGNIFICATION=`echo "${LINE}" | sed 's/[^0-9]//g; 1q'`
111 # 3. Compute resolution.
112 # "bc" allows floating-point arithmetic, unlike "expr" or "dc".
113 RESOLUTION=`echo "scale=2; \
114 ${SCALEFACTOR} * (10/${LATEXFONT}) * (1000/${MAGNIFICATION})" \
118 dvips -o ${PSFILE} ${DVIFILE}
119 if [ $? -ne 0 ]; then
120 echo "Failed: dvips -o ${PSFILE} ${DVIFILE}"
124 # PostScript -> Bitmap files
125 # Older versions of gs have problems with a large degree of anti-aliasing
126 # at high resolutions
128 if [ ${RESOLUTION} -gt 150 ]; then
132 gs -q -dNOPAUSE -dBATCH -dSAFER -sDEVICE=pnmraw -sOutputFile=${BASE}%03d.ppm \
133 -dGraphicsAlphaBit=${ALPHA} -dTextAlphaBits=${ALPHA} -r${RESOLUTION} \
136 if [ $? -ne 0 ]; then
137 echo "Failed: gs ${PSFILE}"
141 # All has been successful, so remove everything except the bitmap files
142 # and the metrics file.
143 FILES=`ls ${BASE}* | sed -e "/${BASE}.metrics/d" -e "/${BASE}[0-9]\{3\}.ppm/d"`