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