update files
[lyx.git] / lib / scripts / lyxpreview2bitmap.sh
blob7e4ece1f96a1914b1a98720a6404f6fc72ff8a9d
1 #! /bin/sh
3 # file lyxpreview2bitmap.sh
4 # This file is part of LyX, the document processor.
5 # Licence details can be found in the file COPYING.
7 # author Angus Leeming
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,
13 # one per page.
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
33 # on the screen.
34 # ${BASE}.metrics
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
47 # PNG format.
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 # Three helper functions.
55 FIND_IT ()
57 test $# -eq 1 || exit 1
59 type $1 > /dev/null || {
60 echo "Unable to find \"$1\". Please install."
61 exit 1
65 BAIL_OUT ()
67 # Remove everything except the original .tex file.
68 FILES=`ls ${BASE}* | sed -e "/${BASE}.tex/d"`
69 rm -f ${FILES} texput.log
70 echo "Leaving ${BASE}.tex in ${DIR}"
71 exit 1
74 REQUIRED_VERSION ()
76 echo "We require preview.sty version 0.73 or newer. You're using"
77 grep 'Package: preview' ${LOGFILE}
80 # Preliminary check.
81 if [ $# -ne 3 ]; then
82 exit 1
85 # Extract the params from the argument list.
86 DIR=`dirname $1`
87 BASE=`basename $1 .tex`
89 SCALEFACTOR=$2
91 if [ "$3" = "ppm" ]; then
92 GSDEVICE=pnmraw
93 GSSUFFIX=ppm
94 elif [ "$3" = "png" ]; then
95 GSDEVICE=png16m
96 GSSUFFIX=png
97 else
98 echo "Unrecognised output format ${OUTPUTFORMAT}."
99 echo "Expected either \"ppm\" or \"png\"."
100 BAIL_OUT
103 # We use latex, dvips and gs, so check that they're all there.
104 FIND_IT latex
105 FIND_IT dvips
106 FIND_IT gs
108 # Initialise some variables.
109 TEXFILE=${BASE}.tex
110 LOGFILE=${BASE}.log
111 DVIFILE=${BASE}.dvi
112 PSFILE=${BASE}.ps
113 METRICSFILE=${BASE}.metrics
115 # LaTeX -> DVI.
116 cd ${DIR}
117 latex ${TEXFILE} ||
119 echo "Failed: latex ${TEXFILE}"
120 BAIL_OUT
123 # Parse ${LOGFILE} to obtain bounding box info to output to
124 # ${METRICSFILE}.
125 # This extracts lines starting "Preview: Tightpage" and
126 # "Preview: Snippet".
127 grep -E 'Preview: [ST]' ${LOGFILE} > ${METRICSFILE} ||
129 echo "Failed: grep -E 'Preview: [ST]' ${LOGFILE}"
130 REQUIRED_VERSION
131 BAIL_OUT
134 # Parse ${LOGFILE} to obtain ${RESOLUTION} for the gs process to follow.
135 # 1. Extract font size from a line like "Preview: Fontsize 20.74pt"
136 # Use grep for speed and because it gives an error if the line is
137 # not found.
138 LINE=`grep 'Preview: Fontsize' ${LOGFILE}` ||
140 echo "Failed: grep 'Preview: Fontsize' ${LOGFILE}"
141 REQUIRED_VERSION
142 BAIL_OUT
144 # The sed script strips out everything that won't form a decimal number
145 # from the line. It bails out after the first match has been made in
146 # case there are multiple lines "Preview: Fontsize". (There shouldn't
147 # be.)
148 # Note: use "" quotes in the echo to preserve newlines.
149 LATEXFONT=`echo "${LINE}" | sed 's/[^0-9\.]//g; 1q'`
151 # 2. Extract magnification from a line like
152 # "Preview: Magnification 2074"
153 # If no such line found, default to MAGNIFICATION=1000.
154 LINE=`grep 'Preview: Magnification' ${LOGFILE}`
155 if LINE=`grep 'Preview: Magnification' ${LOGFILE}`; then
156 # Strip out everything that won't form an /integer/.
157 MAGNIFICATION=`echo "${LINE}" | sed 's/[^0-9]//g; 1q'`
158 else
159 MAGNIFICATION=1000
162 # 3. Compute resolution.
163 # "bc" allows floating-point arithmetic, unlike "expr" or "dc".
164 RESOLUTION=`echo "scale=2; \
165 ${SCALEFACTOR} * (10/${LATEXFONT}) * (1000/${MAGNIFICATION})" \
166 | bc`
168 # DVI -> PostScript
169 dvips -o ${PSFILE} ${DVIFILE} ||
171 echo "Failed: dvips -o ${PSFILE} ${DVIFILE}"
172 BAIL_OUT
175 # PostScript -> Bitmap files
176 # Older versions of gs have problems with a large degree of
177 # anti-aliasing at high resolutions
179 # test expects integer arguments.
180 # ${RESOLUTION} may be a float. Truncate it.
181 INT_RESOLUTION=`echo "${RESOLUTION} / 1" | bc`
183 ALPHA=4
184 if [ ${INT_RESOLUTION} -gt 150 ]; then
185 ALPHA=2
188 gs -q -dNOPAUSE -dBATCH -dSAFER \
189 -sDEVICE=${GSDEVICE} -sOutputFile=${BASE}%d.${GSSUFFIX} \
190 -dGraphicsAlphaBit=${ALPHA} -dTextAlphaBits=${ALPHA} \
191 -r${RESOLUTION} ${PSFILE} ||
193 echo "Failed: gs ${PSFILE}"
194 BAIL_OUT
197 # All has been successful, so remove everything except the bitmap files
198 # and the metrics file.
199 FILES=`ls ${BASE}* | sed -e "/${BASE}.metrics/d" \
200 -e "/${BASE}\([0-9]*\).${GSSUFFIX}/d"`
201 rm -f ${FILES} texput.log
203 # The bitmap files can have large amounts of whitespace to the left and
204 # right. This can be cropped if so desired.
205 CROP=1
206 type pnmcrop > /dev/null || CROP=0
208 # There's no point cropping the image if using PNG images. If you want to
209 # crop, use PPM.
210 # Apparently dvipng will support cropping at some stage in the future...
211 if [ ${CROP} -eq 1 -a "${GSDEVICE}" = "pnmraw" ]; then
212 for FILE in ${BASE}*.${GSSUFFIX}
214 if pnmcrop -left ${FILE} 2> /dev/null |\
215 pnmcrop -right 2> /dev/null > ${BASE}.tmp; then
216 mv ${BASE}.tmp ${FILE}
217 else
218 rm -f ${BASE}.tmp
220 done
221 rm -f ${BASE}.tmp
224 echo "Previews generated!"