]> git.lyx.org Git - lyx.git/blob - lib/scripts/lyxpreview2bitmap.sh
Rename lyxpreview2ppm.sh as lyxpreview2bitmap.sh because the script now
[lyx.git] / lib / scripts / lyxpreview2bitmap.sh
1 #! /bin/sh
2
3 # \file tex2preview.sh
4 # Copyright 2002 the LyX Team
5 # Read the file COPYING
6 # \author Angus Leeming, leeming@lyx.org
7 # with much advice from David Kastrup, david.kastrup@t-online.de.
8
9 # This script takes a LaTeX file and generates bitmap image files,
10 # one per page.
11
12 # The idea is to use it with preview.sty from the preview-latex project
13 # (http://preview-latex.sourceforge.net/) to create small bitmap previews of
14 # things like math equations.
15
16 # preview.sty can be obtained from CTAN/macros/latex/contrib/supported/preview.
17
18 # This script takes three arguments:
19 # TEXFILE:       the name of the .tex file to be converted.
20 # SCALEFACTOR:   a scale factor, used to ascertain the resolution of the
21 #                generated image which is then passed to gs.
22 # OUTPUTFORMAT:  the format of the output bitmap image files. Two formats
23 #                are recognised: PPM and PNG
24
25 # If successful, this script will leave in dir ${DIR}:
26 # ${BASE}\([0-9]*\).ppm/png:  a (possibly large) number of image files.
27 # ${BASE}.metrics:            a file containing info needed by LyX to
28 #                             position the images correctly on the screen.
29 # All other files ${BASE}* will be deleted.
30
31 # Three helper functions.
32 FIND_IT () {
33         which ${EXECUTABLE} > /dev/null
34         if [ $? -ne 0 ]; then
35                 echo "Unable to find \"${EXECUTABLE}\". Please install."
36                 exit 1
37         fi
38 }
39
40 BAIL_OUT () {
41         # Remove everything except the original .tex file.
42         FILES=`ls ${BASE}* | sed -e "/${BASE}.tex/d"`
43         rm -f ${FILES} texput.log
44         exit 1
45 }
46
47 REQUIRED_VERSION () {
48         echo "We require preview.sty version 0.73 or newer. You're using"
49         grep 'Package: preview' ${LOGFILE}
50 }
51
52 # Preliminary check.
53 if [ $# -ne 3 ]; then
54         exit 1
55 fi
56
57 # Extract the params from the argument list.
58 DIR=`dirname $1`
59 BASE=`basename $1 .tex`
60
61 SCALEFACTOR=$2
62
63 if [ "$3" = "ppm" ]; then
64         GSDEVICE=pnmraw
65         GSSUFFIX=ppm
66 elif [ "$3" = "png" ]; then
67         GSDEVICE=png16m
68         GSSUFFIX=png
69 else
70         echo "Unrecognised output format ${OUTPUTFORMAT}."
71         echo "Expected either \"ppm\" or \"png\"."
72         BAIL_OUT
73 fi
74
75 # We use latex, dvips and gs, so check that they're all there.
76 EXECUTABLE=latex; FIND_IT
77 EXECUTABLE=dvips; FIND_IT
78 EXECUTABLE=gs;    FIND_IT
79
80 # Initialise some variables.
81 TEXFILE=${BASE}.tex
82 LOGFILE=${BASE}.log
83 DVIFILE=${BASE}.dvi
84 PSFILE=${BASE}.ps
85 METRICSFILE=${BASE}.metrics
86
87 # LaTeX -> DVI.
88 cd ${DIR}
89 latex ${TEXFILE}
90 if [ $? -ne 0 ]; then
91         echo "Failed: latex ${TEXFILE}"
92         BAIL_OUT
93 fi
94
95 # Parse ${LOGFILE} to obtain bounding box info to output to ${METRICSFILE}.
96 # This extracts lines starting "Preview: Tightpage" and "Preview: Snippet".
97 grep -E 'Preview: [ST]' ${LOGFILE} > ${METRICSFILE}
98 if [ $? -ne 0 ]; then
99         echo "Failed: grep -E 'Preview: [ST]' ${LOGFILE}"
100         REQUIRED_VERSION
101         BAIL_OUT
102 fi
103
104 # Parse ${LOGFILE} to obtain ${RESOLUTION} for the gs process to follow.
105 # 1. Extract font size from a line like "Preview: Fontsize 20.74pt"
106 # Use grep for speed and because it gives an error if the line is not found.
107 LINE=`grep 'Preview: Fontsize' ${LOGFILE}`
108 if [ $? -ne 0 ]; then
109         echo "Failed: grep 'Preview: Fontsize' ${LOGFILE}"
110         REQUIRED_VERSION
111         BAIL_OUT
112 fi
113 # Use "" quotes in the echo to preserve newlines (technically IFS separators).
114 # The sed script strips out everything that won't form a decimal number from the
115 # line. It bails out after the first match has been made in case there are
116 # multiple lines "Preview: Fontsize". (There shouldn't be.)
117 LATEXFONT=`echo "${LINE}" | sed 's/[^0-9\.]//g; 1q'`
118
119 # 2. Extract magnification from a line like "Preview: Magnification 2074"
120 # If no such line found, default to MAGNIFICATION=1000.
121 LINE=`grep 'Preview: Magnification' ${LOGFILE}`
122 if [ $? -ne 0 ]; then
123         MAGNIFICATION=1000
124 else
125         # The sed script strips out everything that won't form an /integer/.
126         MAGNIFICATION=`echo "${LINE}" | sed 's/[^0-9]//g; 1q'`
127 fi
128
129 # 3. Compute resolution.
130 # "bc" allows floating-point arithmetic, unlike "expr" or "dc".
131 RESOLUTION=`echo "scale=2; \
132                 ${SCALEFACTOR} * (10/${LATEXFONT}) * (1000/${MAGNIFICATION})" \
133                 | bc`
134
135 # DVI -> PostScript
136 dvips -o ${PSFILE} ${DVIFILE}
137 if [ $? -ne 0 ]; then
138         echo "Failed: dvips -o ${PSFILE} ${DVIFILE}"
139         BAIL_OUT
140 fi
141
142 # PostScript -> Bitmap files
143 # Older versions of gs have problems with a large degree of anti-aliasing
144 # at high resolutions
145
146 # test expects integer arguments. ${RESOLUTION} may be a float. Truncate it.
147 INT_RESOLUTION=`echo "${RESOLUTION} / 1" | bc`
148
149 ALPHA=4
150 if [ ${INT_RESOLUTION} -gt 150 ]; then
151         ALPHA=2
152 fi
153
154 gs -q -dNOPAUSE -dBATCH -dSAFER \
155     -sDEVICE=${GSDEVICE} -sOutputFile=${BASE}%d.${GSSUFFIX} \
156     -dGraphicsAlphaBit=${ALPHA} -dTextAlphaBits=${ALPHA} -r${RESOLUTION} \
157     ${PSFILE}
158
159 if [ $? -ne 0 ]; then
160         echo "Failed: gs ${PSFILE}"
161         BAIL_OUT
162 fi
163
164 # All has been successful, so remove everything except the bitmap files
165 # and the metrics file.
166 FILES=`ls ${BASE}* | \
167         sed -e "/${BASE}.metrics/d" -e "/${BASE}\([0-9]*\).${GSSUFFIX}/d"`
168 rm -f ${FILES} texput.log