]> git.lyx.org Git - lyx.git/blob - lib/scripts/lyxpreview2bitmap.sh
src/support/ChangeLog
[lyx.git] / lib / scripts / lyxpreview2bitmap.sh
1 #! /bin/sh
2
3 # file lyxpreview2bitmap.sh
4 # This file is part of LyX, the document processor.
5 # Licence details can be found in the file COPYING.
6 #
7 # author Angus Leeming
8 # with much advice from David Kastrup, david.kastrup@t-online.de.
9 #
10 # Full author contact details are available in file CREDITS
11
12 # This script takes a LaTeX file and generates bitmap image files,
13 # one per page.
14
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.
18
19 # preview.sty can be obtained from
20 # CTAN/macros/latex/contrib/supported/preview.
21
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".
28
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.
36
37 # A quick note on the choice of OUTPUTFORMAT:
38
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.
42
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.
48
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.
53
54 # These programs are used by the script.
55 # Adjust their names to suit your setup.
56 test -n "$LATEX" || LATEX=latex
57 DVIPNG=/home/angus/preview-latex/devel/dvipng/dvipng
58 DVIPS=dvips
59 GS=gs
60 PNMCROP=pnmcrop
61 readonly LATEX DVIPS GS PNMCROP
62
63 # Some helper functions.
64 FIND_IT ()
65 {
66         test $# -eq 1 || exit 1
67
68         type $1 > /dev/null || {
69                 echo "Unable to find \"$1\". Please install."
70                 exit 1
71         }
72 }
73
74 BAIL_OUT ()
75 {
76         test $# -eq 1 && echo $1
77
78         # Remove everything except the original .tex file.
79         FILES=`ls ${BASE}* | sed -e "/${BASE}\.tex/d"`
80         rm -f ${FILES} texput.log
81         echo "Leaving ${BASE}.tex in ${DIR}"
82         exit 1
83 }
84
85 REQUIRED_VERSION ()
86 {
87         test $# -eq 1 || exit 1
88
89         echo "We require preview.sty version 0.73 or newer. You're using"
90         grep 'Package: preview' $1
91 }
92
93 CHECK_FOR_PROGRAMS () {
94         test $# -eq 1 || exit 1
95         if [ $1 = "ppm" ]; then
96                 # We use latex, dvips and gs, so check that they're all there.
97                 FIND_IT ${LATEX}
98                 FIND_IT ${DVIPS}
99                 FIND_IT ${GS}
100         elif [ "$3" = "png" ]; then
101                 # We use latex and dvipng
102                 FIND_IT ${LATEX}
103                 FIND_IT ${DVIPNG}
104         fi
105 }
106
107 GENERATE_PPM_FILES () {
108         test $# -eq 2 || exit 1
109
110         BASE=$1
111         RESOLUTION=$2
112
113         # DVI -> PostScript
114         PSFILE=${1}.ps
115
116         ${DVIPS} -o ${PSFILE} ${DVIFILE} || {
117                 BAIL_OUT "Failed: ${DVIPS} -o ${PSFILE} ${DVIFILE}"
118         }
119
120         # PostScript -> Bitmap files
121         # Older versions of gs have problems with a large degree of
122         # anti-aliasing at high resolutions
123
124         # test expects integer arguments.
125         # ${RESOLUTION} may be a float. Truncate it.
126         INT_RESOLUTION=`echo "${RESOLUTION} / 1" | bc`
127
128         ALPHA=4
129         if [ ${INT_RESOLUTION} -gt 150 ]; then
130                 ALPHA=2
131         fi
132
133         GSDEVICE=pnmraw
134         GSSUFFIX=ppm
135
136         ${GS} -q -dNOPAUSE -dBATCH -dSAFER \
137                 -sDEVICE=${GSDEVICE} -sOutputFile=${BASE}%d.${GSSUFFIX} \
138                 -dGraphicsAlphaBit=${ALPHA} -dTextAlphaBits=${ALPHA} \
139                 -r${RESOLUTION} ${PSFILE} ||
140         {
141                 BAIL_OUT "Failed: ${GS} ${PSFILE}"
142         }
143
144         # All has been successful, so remove everything except the bitmap files
145         # and the metrics file.
146         FILES=`ls ${BASE}* | sed -e "/${BASE}.metrics/d" \
147                          -e "/${BASE}\([0-9]*\).${GSSUFFIX}/d"`
148 #       rm -f ${FILES} texput.log
149
150         # The bitmap files can have large amounts of whitespace to the left and
151         # right. This can be cropped if so desired.
152         CROP=1
153         type ${PNMCROP} > /dev/null || CROP=0
154
155         if [ ${CROP} -eq 1 ]; then
156                 for FILE in ${BASE}*.${GSSUFFIX}
157                 do
158                         if ${PNMCROP} -left ${FILE} 2> /dev/null |\
159                            ${PNMCROP} -right  2> /dev/null > ${BASE}.tmp; then
160                                 mv ${BASE}.tmp ${FILE}
161                         else
162                                 rm -f ${BASE}.tmp
163                         fi
164                 done
165                 rm -f ${BASE}.tmp
166         fi
167 }
168
169 GENERATE_PNG_FILES () {
170         test $# -eq 2 || exit 1
171
172         BASE=$1
173         RESOLUTION=$2
174
175         ${DVIPNG} -D${RESOLUTION} -o${BASE}%d.png ${DVIFILE}
176 }
177
178 echo $*
179
180 # Preliminary check.
181 if [ $# -ne 3 ]; then
182         exit 1
183 fi
184
185 # Extract the params from the argument list.
186 DIR=`dirname $1`
187 BASE=`basename $1 .tex`
188
189 SCALEFACTOR=$2
190 OUTPUTFORMAT=$3
191
192 if [ "${OUTPUTFORMAT}" != "ppm" -a "${OUTPUTFORMAT}" != "png" ]; then
193         BAIL_OUT "Unrecognised output format ${OUTPUTFORMAT}. \
194         Expected either \"ppm\" or \"png\"."
195 fi
196
197 CHECK_FOR_PROGRAMS ${OUTPUTFORMAT}
198
199 # Initialise some variables.
200 TEXFILE=${BASE}.tex
201 LOGFILE=${BASE}.log
202 DVIFILE=${BASE}.dvi
203 METRICSFILE=${BASE}.metrics
204 readonly TEXFILE LOGFILE DVIFILE METRICSFILE
205
206 # LaTeX -> DVI.
207 cd ${DIR}
208 ${LATEX} ${TEXFILE} ||
209 {
210         BAIL_OUT "Failed: ${LATEX} ${TEXFILE}"
211 }
212
213 # Parse ${LOGFILE} to obtain bounding box info to output to
214 # ${METRICSFILE}.
215 # This extracts lines starting "Preview: Tightpage" and
216 # "Preview: Snippet".
217 grep -E 'Preview: [ST]' ${LOGFILE} > ${METRICSFILE} ||
218 {
219         REQUIRED_VERSION ${LOGFILE}
220         BAIL_OUT "Failed: grep -E 'Preview: [ST]' ${LOGFILE}"
221 }
222
223 # Parse ${LOGFILE} to obtain ${RESOLUTION} for the gs process to follow.
224 # 1. Extract font size from a line like "Preview: Fontsize 20.74pt"
225 # Use grep for speed and because it gives an error if the line is
226 # not found.
227 LINE=`grep 'Preview: Fontsize' ${LOGFILE}` ||
228 {
229         REQUIRED_VERSION ${LOGFILE}
230         BAIL_OUT "Failed: grep 'Preview: Fontsize' ${LOGFILE}"
231 }
232 # The sed script strips out everything that won't form a decimal number
233 # from the line. It bails out after the first match has been made in
234 # case there are multiple lines "Preview: Fontsize". (There shouldn't
235 # be.)
236 # Note: use "" quotes in the echo to preserve newlines.
237 LATEXFONT=`echo "${LINE}" | sed 's/[^0-9\.]//g; 1q'`
238
239 # 2. Extract magnification from a line like
240 # "Preview: Magnification 2074"
241 # If no such line found, default to MAGNIFICATION=1000.
242 LINE=`grep 'Preview: Magnification' ${LOGFILE}`
243 if LINE=`grep 'Preview: Magnification' ${LOGFILE}`; then
244         # Strip out everything that won't form an /integer/.
245         MAGNIFICATION=`echo "${LINE}" | sed 's/[^0-9]//g; 1q'`
246 else
247         MAGNIFICATION=1000
248 fi
249
250 # 3. Compute resolution.
251 # "bc" allows floating-point arithmetic, unlike "expr" or "dc".
252 RESOLUTION=`echo "scale=2; \
253         ${SCALEFACTOR} * (10/${LATEXFONT}) * (1000/${MAGNIFICATION})" \
254         | bc`
255
256 # 4. Generate the bitmap files and clean-up.
257 if [ "${OUTPUTFORMAT}" = "ppm" ]; then
258         GENERATE_PPM_FILES ${BASE} ${RESOLUTION}
259 else
260         GENERATE_PNG_FILES ${BASE} ${RESOLUTION}
261 fi
262
263 echo "Previews generated!"