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