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