]> git.lyx.org Git - lyx.git/blob - lib/scripts/lyxpreview2bitmap.sh
add algorithm counter
[lyx.git] / lib / scripts / lyxpreview2bitmap.sh
1 #! /bin/sh
2
3 # file lyxpreview2bitmap.sh
4 # Read the file COPYING
5 #
6 # author Angus Leeming
7 # with much advice from David Kastrup, david.kastrup@t-online.de.
8 #
9 # Full author contact details are available in file CREDITS
10
11 # This script takes a LaTeX file and generates bitmap image files,
12 # one per page.
13
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.
17
18 # preview.sty can be obtained from
19 # CTAN/macros/latex/contrib/supported/preview.
20
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".
27
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
32 # on the screen.
33 #         ${BASE}.metrics
34 # All other files ${BASE}* will be deleted.
35
36 # A quick note on the choice of OUTPUTFORMAT:
37
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.
41
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
46 # PNG format.
47
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.
52
53 # Three helper functions.
54 FIND_IT () {
55         which ${EXECUTABLE} > /dev/null
56         if [ $? -ne 0 ]; then
57                 echo "Unable to find \"${EXECUTABLE}\". Please install."
58                 exit 1
59         fi
60 }
61
62 BAIL_OUT () {
63         # Remove everything except the original .tex file.
64         FILES=`ls ${BASE}* | sed -e "/${BASE}.tex/d"`
65         rm -f ${FILES} texput.log
66         exit 1
67 }
68
69 REQUIRED_VERSION () {
70         echo "We require preview.sty version 0.73 or newer. You're using"
71         grep 'Package: preview' ${LOGFILE}
72 }
73
74 # Preliminary check.
75 if [ $# -ne 3 ]; then
76         exit 1
77 fi
78
79 # Extract the params from the argument list.
80 DIR=`dirname $1`
81 BASE=`basename $1 .tex`
82
83 SCALEFACTOR=$2
84
85 if [ "$3" = "ppm" ]; then
86         GSDEVICE=pnmraw
87         GSSUFFIX=ppm
88 elif [ "$3" = "png" ]; then
89         GSDEVICE=png16m
90         GSSUFFIX=png
91 else
92         echo "Unrecognised output format ${OUTPUTFORMAT}."
93         echo "Expected either \"ppm\" or \"png\"."
94         BAIL_OUT
95 fi
96
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
101
102 # Initialise some variables.
103 TEXFILE=${BASE}.tex
104 LOGFILE=${BASE}.log
105 DVIFILE=${BASE}.dvi
106 PSFILE=${BASE}.ps
107 METRICSFILE=${BASE}.metrics
108
109 # LaTeX -> DVI.
110 cd ${DIR}
111 latex ${TEXFILE}
112 if [ $? -ne 0 ]; then
113         echo "Failed: latex ${TEXFILE}"
114         BAIL_OUT
115 fi
116
117 # Parse ${LOGFILE} to obtain bounding box info to output to
118 # ${METRICSFILE}.
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}"
124         REQUIRED_VERSION
125         BAIL_OUT
126 fi
127
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
131 # not found.
132 LINE=`grep 'Preview: Fontsize' ${LOGFILE}`
133 if [ $? -ne 0 ]; then
134         echo "Failed: grep 'Preview: Fontsize' ${LOGFILE}"
135         REQUIRED_VERSION
136         BAIL_OUT
137 fi
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
141 # be.)
142 # Note: use "" quotes in the echo to preserve newlines.
143 LATEXFONT=`echo "${LINE}" | sed 's/[^0-9\.]//g; 1q'`
144
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
150         MAGNIFICATION=1000
151 else
152         # Strip out everything that won't form an /integer/.
153         MAGNIFICATION=`echo "${LINE}" | sed 's/[^0-9]//g; 1q'`
154 fi
155
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})" \
160         | bc`
161
162 # DVI -> PostScript
163 dvips -o ${PSFILE} ${DVIFILE}
164 if [ $? -ne 0 ]; then
165         echo "Failed: dvips -o ${PSFILE} ${DVIFILE}"
166         BAIL_OUT
167 fi
168
169 # PostScript -> Bitmap files
170 # Older versions of gs have problems with a large degree of
171 # anti-aliasing at high resolutions
172
173 # test expects integer arguments.
174 # ${RESOLUTION} may be a float. Truncate it.
175 INT_RESOLUTION=`echo "${RESOLUTION} / 1" | bc`
176
177 ALPHA=4
178 if [ ${INT_RESOLUTION} -gt 150 ]; then
179         ALPHA=2
180 fi
181
182 gs -q -dNOPAUSE -dBATCH -dSAFER \
183         -sDEVICE=${GSDEVICE} -sOutputFile=${BASE}%d.${GSSUFFIX} \
184         -dGraphicsAlphaBit=${ALPHA} -dTextAlphaBits=${ALPHA} \
185         -r${RESOLUTION} ${PSFILE}
186
187 if [ $? -ne 0 ]; then
188         echo "Failed: gs ${PSFILE}"
189         BAIL_OUT
190 fi
191
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