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