3 # file lyxpreview2bitmap.sh
4 # Read the file COPYING
7 # with much advice from David Kastrup, david.kastrup@t-online.de.
9 # Full author contact details are available in file CREDITS
11 # This script takes a LaTeX file and generates bitmap image files,
14 # The idea is to use it with preview.sty from the preview-latex project
15 # (http://preview-latex.sourceforge.net/) to create small bitmap
16 # previews of things like math equations.
18 # preview.sty can be obtained from
19 # CTAN/macros/latex/contrib/supported/preview.
21 # This script takes three arguments:
22 # TEXFILE: the name of the .tex file to be converted.
23 # SCALEFACTOR: a scale factor, used to ascertain the resolution of the
24 # generated image which is then passed to gs.
25 # OUTPUTFORMAT: the format of the output bitmap image files.
26 # Two formats are recognised: "ppm" and "png".
28 # If successful, this script will leave in dir ${DIR}:
29 # a (possibly large) number of image files with names like
30 # ${BASE}\([0-9]*\).${SUFFIX} where SUFFIX is ppm or png.
31 # a file containing info needed by LyX to position the images correctly
34 # All other files ${BASE}* will be deleted.
36 # A quick note on the choice of OUTPUTFORMAT:
38 # In general files in PPM format are 10-100 times larger than the
39 # equivalent files in PNG format. Larger files results in longer
40 # reading and writing times as well as greater disk usage.
42 # However, whilst the Qt image loader can load files in PNG format
43 # without difficulty, the xforms image loader cannot. They must first
44 # be converted to a loadable format (eg PPM!). Thus, previews will take
45 # longer to appear if the xforms loader is used to load snippets in
48 # You can always experiment by adding a line to your
49 # ${LYXUSERDIR}/preferences file
50 # \converter lyxpreview ${FORMAT} "lyxpreview2bitmap.sh" ""
51 # where ${FORMAT} is either ppm or png.
53 # Three helper functions.
55 which ${EXECUTABLE} > /dev/null
57 echo "Unable to find \"${EXECUTABLE}\". Please install."
63 # Remove everything except the original .tex file.
64 FILES=`ls ${BASE}* | sed -e "/${BASE}.tex/d"`
65 rm -f ${FILES} texput.log
70 echo "We require preview.sty version 0.73 or newer. You're using"
71 grep 'Package: preview' ${LOGFILE}
79 # Extract the params from the argument list.
81 BASE=`basename $1 .tex`
85 if [ "$3" = "ppm" ]; then
88 elif [ "$3" = "png" ]; then
92 echo "Unrecognised output format ${OUTPUTFORMAT}."
93 echo "Expected either \"ppm\" or \"png\"."
97 # We use latex, dvips and gs, so check that they're all there.
98 EXECUTABLE=latex; FIND_IT
99 EXECUTABLE=dvips; FIND_IT
100 EXECUTABLE=gs; FIND_IT
102 # Initialise some variables.
107 METRICSFILE=${BASE}.metrics
112 if [ $? -ne 0 ]; then
113 echo "Failed: latex ${TEXFILE}"
117 # Parse ${LOGFILE} to obtain bounding box info to output to
119 # This extracts lines starting "Preview: Tightpage" and
120 # "Preview: Snippet".
121 grep -E 'Preview: [ST]' ${LOGFILE} > ${METRICSFILE}
122 if [ $? -ne 0 ]; then
123 echo "Failed: grep -E 'Preview: [ST]' ${LOGFILE}"
128 # Parse ${LOGFILE} to obtain ${RESOLUTION} for the gs process to follow.
129 # 1. Extract font size from a line like "Preview: Fontsize 20.74pt"
130 # Use grep for speed and because it gives an error if the line is
132 LINE=`grep 'Preview: Fontsize' ${LOGFILE}`
133 if [ $? -ne 0 ]; then
134 echo "Failed: grep 'Preview: Fontsize' ${LOGFILE}"
138 # The sed script strips out everything that won't form a decimal number
139 # from the line. It bails out after the first match has been made in
140 # case there are multiple lines "Preview: Fontsize". (There shouldn't
142 # Note: use "" quotes in the echo to preserve newlines.
143 LATEXFONT=`echo "${LINE}" | sed 's/[^0-9\.]//g; 1q'`
145 # 2. Extract magnification from a line like
146 # "Preview: Magnification 2074"
147 # If no such line found, default to MAGNIFICATION=1000.
148 LINE=`grep 'Preview: Magnification' ${LOGFILE}`
149 if [ $? -ne 0 ]; then
152 # Strip out everything that won't form an /integer/.
153 MAGNIFICATION=`echo "${LINE}" | sed 's/[^0-9]//g; 1q'`
156 # 3. Compute resolution.
157 # "bc" allows floating-point arithmetic, unlike "expr" or "dc".
158 RESOLUTION=`echo "scale=2; \
159 ${SCALEFACTOR} * (10/${LATEXFONT}) * (1000/${MAGNIFICATION})" \
163 dvips -o ${PSFILE} ${DVIFILE}
164 if [ $? -ne 0 ]; then
165 echo "Failed: dvips -o ${PSFILE} ${DVIFILE}"
169 # PostScript -> Bitmap files
170 # Older versions of gs have problems with a large degree of
171 # anti-aliasing at high resolutions
173 # test expects integer arguments.
174 # ${RESOLUTION} may be a float. Truncate it.
175 INT_RESOLUTION=`echo "${RESOLUTION} / 1" | bc`
178 if [ ${INT_RESOLUTION} -gt 150 ]; then
182 gs -q -dNOPAUSE -dBATCH -dSAFER \
183 -sDEVICE=${GSDEVICE} -sOutputFile=${BASE}%d.${GSSUFFIX} \
184 -dGraphicsAlphaBit=${ALPHA} -dTextAlphaBits=${ALPHA} \
185 -r${RESOLUTION} ${PSFILE}
187 if [ $? -ne 0 ]; then
188 echo "Failed: gs ${PSFILE}"
192 # All has been successful, so remove everything except the bitmap files
193 # and the metrics file.
194 FILES=`ls ${BASE}* | sed -e "/${BASE}.metrics/d" \
195 -e "/${BASE}\([0-9]*\).${GSSUFFIX}/d"`
196 rm -f ${FILES} texput.log