3 # file lyxpreview2bitmap.sh
4 # This file is part of LyX, the document processor.
5 # Licence details can be found in the file COPYING.
8 # with much advice from David Kastrup, david.kastrup@t-online.de.
10 # Full author contact details are available in file CREDITS
12 # This script takes a LaTeX file and generates bitmap image files,
15 # The idea is to use it with preview.sty from the preview-latex project
16 # (http://preview-latex.sourceforge.net/) to create small bitmap
17 # previews of things like math equations.
19 # preview.sty can be obtained from
20 # CTAN/macros/latex/contrib/supported/preview.
22 # This script takes three arguments:
23 # TEXFILE: the name of the .tex file to be converted.
24 # SCALEFACTOR: a scale factor, used to ascertain the resolution of the
25 # generated image which is then passed to gs.
26 # OUTPUTFORMAT: the format of the output bitmap image files.
27 # Two formats are recognised: "ppm" and "png".
29 # If successful, this script will leave in dir ${DIR}:
30 # a (possibly large) number of image files with names like
31 # ${BASE}\([0-9]*\).${SUFFIX} where SUFFIX is ppm or png.
32 # a file containing info needed by LyX to position the images correctly
35 # All other files ${BASE}* will be deleted.
37 # A quick note on the choice of OUTPUTFORMAT:
39 # In general files in PPM format are 10-100 times larger than the
40 # equivalent files in PNG format. Larger files results in longer
41 # reading and writing times as well as greater disk usage.
43 # However, whilst the Qt image loader can load files in PNG format
44 # without difficulty, the xforms image loader cannot. They must first
45 # be converted to a loadable format (eg PPM!). Thus, previews will take
46 # longer to appear if the xforms loader is used to load snippets in
49 # You can always experiment by adding a line to your
50 # ${LYXUSERDIR}/preferences file
51 # \converter lyxpreview ${FORMAT} "lyxpreview2bitmap.sh" ""
52 # where ${FORMAT} is either ppm or png.
54 # These four programs are used by the script.
55 # Adjust their names to suit your setup.
60 readonly LATEX DVIPS GS PNMCROP
62 # Three helper functions.
65 test $# -eq 1 || exit 1
67 type $1 > /dev/null || {
68 echo "Unable to find \"$1\". Please install."
75 test $# -eq 1 && echo $1
77 # Remove everything except the original .tex file.
78 FILES=`ls ${BASE}* | sed -e "/${BASE}\.tex/d"`
79 rm -f ${FILES} texput.log
80 echo "Leaving ${BASE}.tex in ${DIR}"
86 test $# -eq 1 || exit 1
88 echo "We require preview.sty version 0.73 or newer. You're using"
89 grep 'Package: preview' $1
97 # We use latex, dvips and gs, so check that they're all there.
102 # Extract the params from the argument list.
104 BASE=`basename $1 .tex`
108 if [ "$3" = "ppm" ]; then
111 elif [ "$3" = "png" ]; then
115 BAIL_OUT "Unrecognised output format ${OUTPUTFORMAT}. \
116 Expected either \"ppm\" or \"png\"."
119 # Initialise some variables.
124 METRICSFILE=${BASE}.metrics
125 readonly TEXFILE LOGFILE DVIFILE PSFILE METRICSFILE
129 ${LATEX} ${TEXFILE} ||
131 BAIL_OUT "Failed: ${LATEX} ${TEXFILE}"
134 # Parse ${LOGFILE} to obtain bounding box info to output to
136 # This extracts lines starting "Preview: Tightpage" and
137 # "Preview: Snippet".
138 grep -E 'Preview: [ST]' ${LOGFILE} > ${METRICSFILE} ||
140 REQUIRED_VERSION ${LOGFILE}
141 BAIL_OUT "Failed: grep -E 'Preview: [ST]' ${LOGFILE}"
144 # Parse ${LOGFILE} to obtain ${RESOLUTION} for the gs process to follow.
145 # 1. Extract font size from a line like "Preview: Fontsize 20.74pt"
146 # Use grep for speed and because it gives an error if the line is
148 LINE=`grep 'Preview: Fontsize' ${LOGFILE}` ||
150 REQUIRED_VERSION ${LOGFILE}
151 BAIL_OUT "Failed: grep 'Preview: Fontsize' ${LOGFILE}"
153 # The sed script strips out everything that won't form a decimal number
154 # from the line. It bails out after the first match has been made in
155 # case there are multiple lines "Preview: Fontsize". (There shouldn't
157 # Note: use "" quotes in the echo to preserve newlines.
158 LATEXFONT=`echo "${LINE}" | sed 's/[^0-9\.]//g; 1q'`
160 # 2. Extract magnification from a line like
161 # "Preview: Magnification 2074"
162 # If no such line found, default to MAGNIFICATION=1000.
163 LINE=`grep 'Preview: Magnification' ${LOGFILE}`
164 if LINE=`grep 'Preview: Magnification' ${LOGFILE}`; then
165 # Strip out everything that won't form an /integer/.
166 MAGNIFICATION=`echo "${LINE}" | sed 's/[^0-9]//g; 1q'`
171 # 3. Compute resolution.
172 # "bc" allows floating-point arithmetic, unlike "expr" or "dc".
173 RESOLUTION=`echo "scale=2; \
174 ${SCALEFACTOR} * (10/${LATEXFONT}) * (1000/${MAGNIFICATION})" \
178 ${DVIPS} -o ${PSFILE} ${DVIFILE} ||
180 BAIL_OUT "Failed: ${DVIPS} -o ${PSFILE} ${DVIFILE}"
183 # PostScript -> Bitmap files
184 # Older versions of gs have problems with a large degree of
185 # anti-aliasing at high resolutions
187 # test expects integer arguments.
188 # ${RESOLUTION} may be a float. Truncate it.
189 INT_RESOLUTION=`echo "${RESOLUTION} / 1" | bc`
192 if [ ${INT_RESOLUTION} -gt 150 ]; then
196 ${GS} -q -dNOPAUSE -dBATCH -dSAFER \
197 -sDEVICE=${GSDEVICE} -sOutputFile=${BASE}%d.${GSSUFFIX} \
198 -dGraphicsAlphaBit=${ALPHA} -dTextAlphaBits=${ALPHA} \
199 -r${RESOLUTION} ${PSFILE} ||
201 BAIL_OUT "Failed: ${GS} ${PSFILE}"
204 # All has been successful, so remove everything except the bitmap files
205 # and the metrics file.
206 FILES=`ls ${BASE}* | sed -e "/${BASE}.metrics/d" \
207 -e "/${BASE}\([0-9]*\).${GSSUFFIX}/d"`
208 rm -f ${FILES} texput.log
210 # The bitmap files can have large amounts of whitespace to the left and
211 # right. This can be cropped if so desired.
213 type ${PNMCROP} > /dev/null || CROP=0
215 # There's no point cropping the image if using PNG images. If you want to
217 # Apparently dvipng will support cropping at some stage in the future...
218 if [ ${CROP} -eq 1 -a "${GSDEVICE}" = "pnmraw" ]; then
219 for FILE in ${BASE}*.${GSSUFFIX}
221 if ${PNMCROP} -left ${FILE} 2> /dev/null |\
222 ${PNMCROP} -right 2> /dev/null > ${BASE}.tmp; then
223 mv ${BASE}.tmp ${FILE}
231 echo "Previews generated!"