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