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