4 # Copyright 2002 the LyX Team
5 # Read the file COPYING
6 # \author Angus Leeming, leeming@lyx.org
7 # with much advice from David Kastrup, david.kastrup@t-online.de.
9 # This script takes a LaTeX file and generates bitmap image files,
12 # The idea is to use it with preview.sty from the preview-latex project
13 # (http://preview-latex.sourceforge.net/) to create small bitmap previews of
14 # things like math equations.
16 # preview.sty can be obtained from CTAN/macros/latex/contrib/supported/preview.
18 # This script takes three arguments:
19 # TEXFILE: the name of the .tex file to be converted.
20 # SCALEFACTOR: a scale factor, used to ascertain the resolution of the
21 # generated image which is then passed to gs.
22 # OUTPUTFORMAT: the format of the output bitmap image files. Two formats
23 # are recognised: PPM and PNG
25 # If successful, this script will leave in dir ${DIR}:
26 # ${BASE}\([0-9]*\).ppm/png: a (possibly large) number of image files.
27 # ${BASE}.metrics: a file containing info needed by LyX to
28 # position the images correctly on the screen.
29 # All other files ${BASE}* will be deleted.
31 # Three helper functions.
33 which ${EXECUTABLE} > /dev/null
35 echo "Unable to find \"${EXECUTABLE}\". Please install."
41 # Remove everything except the original .tex file.
42 FILES=`ls ${BASE}* | sed -e "/${BASE}.tex/d"`
43 rm -f ${FILES} texput.log
48 echo "We require preview.sty version 0.73 or newer. You're using"
49 grep 'Package: preview' ${LOGFILE}
57 # Extract the params from the argument list.
59 BASE=`basename $1 .tex`
63 if [ "$3" = "ppm" ]; then
66 elif [ "$3" = "png" ]; then
70 echo "Unrecognised output format ${OUTPUTFORMAT}."
71 echo "Expected either \"ppm\" or \"png\"."
75 # We use latex, dvips and gs, so check that they're all there.
76 EXECUTABLE=latex; FIND_IT
77 EXECUTABLE=dvips; FIND_IT
78 EXECUTABLE=gs; FIND_IT
80 # Initialise some variables.
85 METRICSFILE=${BASE}.metrics
91 echo "Failed: latex ${TEXFILE}"
95 # Parse ${LOGFILE} to obtain bounding box info to output to ${METRICSFILE}.
96 # This extracts lines starting "Preview: Tightpage" and "Preview: Snippet".
97 grep -E 'Preview: [ST]' ${LOGFILE} > ${METRICSFILE}
99 echo "Failed: grep -E 'Preview: [ST]' ${LOGFILE}"
104 # Parse ${LOGFILE} to obtain ${RESOLUTION} for the gs process to follow.
105 # 1. Extract font size from a line like "Preview: Fontsize 20.74pt"
106 # Use grep for speed and because it gives an error if the line is not found.
107 LINE=`grep 'Preview: Fontsize' ${LOGFILE}`
108 if [ $? -ne 0 ]; then
109 echo "Failed: grep 'Preview: Fontsize' ${LOGFILE}"
113 # Use "" quotes in the echo to preserve newlines (technically IFS separators).
114 # The sed script strips out everything that won't form a decimal number from the
115 # line. It bails out after the first match has been made in case there are
116 # multiple lines "Preview: Fontsize". (There shouldn't be.)
117 LATEXFONT=`echo "${LINE}" | sed 's/[^0-9\.]//g; 1q'`
119 # 2. Extract magnification from a line like "Preview: Magnification 2074"
120 # If no such line found, default to MAGNIFICATION=1000.
121 LINE=`grep 'Preview: Magnification' ${LOGFILE}`
122 if [ $? -ne 0 ]; then
125 # The sed script strips out everything that won't form an /integer/.
126 MAGNIFICATION=`echo "${LINE}" | sed 's/[^0-9]//g; 1q'`
129 # 3. Compute resolution.
130 # "bc" allows floating-point arithmetic, unlike "expr" or "dc".
131 RESOLUTION=`echo "scale=2; \
132 ${SCALEFACTOR} * (10/${LATEXFONT}) * (1000/${MAGNIFICATION})" \
136 dvips -o ${PSFILE} ${DVIFILE}
137 if [ $? -ne 0 ]; then
138 echo "Failed: dvips -o ${PSFILE} ${DVIFILE}"
142 # PostScript -> Bitmap files
143 # Older versions of gs have problems with a large degree of anti-aliasing
144 # at high resolutions
146 # test expects integer arguments. ${RESOLUTION} may be a float. Truncate it.
147 INT_RESOLUTION=`echo "${RESOLUTION} / 1" | bc`
150 if [ ${INT_RESOLUTION} -gt 150 ]; then
154 gs -q -dNOPAUSE -dBATCH -dSAFER \
155 -sDEVICE=${GSDEVICE} -sOutputFile=${BASE}%d.${GSSUFFIX} \
156 -dGraphicsAlphaBit=${ALPHA} -dTextAlphaBits=${ALPHA} -r${RESOLUTION} \
159 if [ $? -ne 0 ]; then
160 echo "Failed: gs ${PSFILE}"
164 # All has been successful, so remove everything except the bitmap files
165 # and the metrics file.
166 FILES=`ls ${BASE}* | \
167 sed -e "/${BASE}.metrics/d" -e "/${BASE}\([0-9]*\).${GSSUFFIX}/d"`
168 rm -f ${FILES} texput.log