]> git.lyx.org Git - lyx.git/blob - lib/scripts/lyxpreview2ppm.sh
* Improve error reporting.
[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 #
9 # with much help from David Kastrup, david.kastrup@t-online.de.
10 # The sed script was created with advice from Praveen D V, praveend@sasken.com
11 # and the sed users' list, sed-users@yahoogroups.com.
12
13 # This script takes a LaTeX file and generates PPM files, one per page.
14 # The idea is to use it with preview.sty to create small bitmap previews of
15 # things like math equations.
16
17 # The script takes two arguments, the name of the file to be converted and
18 # the resolution of the generated image, to be passed to gs.
19 if [ $# -ne 2 ]; then
20         exit 1
21 fi
22
23 # A couple of helper functions
24 FIND_IT () {
25         which ${EXECUTABLE} > /dev/null
26         STATUS=$?
27         if [ ${STATUS} -ne 0 ]; then
28                 echo "Unable to find \"${EXECUTABLE}\". Please install."
29                 exit 1
30         fi
31 }
32
33 BAIL_OUT () {
34         # Remove everything except the original .tex file.
35         FILES=`ls ${BASE}* | sed -e "/${BASE}.tex/d"`
36         rm -f ${FILES} texput.log
37         exit 1
38 }
39
40 # We use latex, dvips and gs, so check that they're all there.
41 EXECUTABLE=latex; FIND_IT
42 EXECUTABLE=dvips; FIND_IT
43 EXECUTABLE=gs;    FIND_IT
44
45 # Initialise some variables.
46 TEXFILE=`basename $1`
47 RESOLUTION=$2
48
49 DIR=`dirname $1`
50 BASE=`basename $1 .tex`
51 DVIFILE=${BASE}.dvi
52 PSFILE=${BASE}.ps
53 METRICS=${BASE}.metrics
54
55 # LaTeX -> DVI.
56 cd ${DIR}
57 latex ${TEXFILE}
58 STATUS=$?
59 if [ ${STATUS} -ne 0 ]; then
60         # LaTeX failed.
61         # preview.sty has known problems with the showlabels option,
62         # so remove it and try again.
63         sed -e "/^[\]usepackage\(.*\){preview}/s/,showlabels//" \
64                 < ${TEXFILE} > .${TEXFILE}
65         cmp -s ${TEXFILE} .${TEXFILE}
66         STATUS=$?
67         if [ ${STATUS} -eq 0 ]; then
68                 rm -f .${TEXFILE}
69                 echo "Failed: latex ${TEXFILE}"
70                 BAIL_OUT
71         fi
72
73         mv -f .${TEXFILE} ${TEXFILE}
74         latex ${TEXFILE}
75         STATUS=$?
76         if [ ${STATUS} -ne 0 ]; then
77                 echo "Failed: latex ${TEXFILE}"
78                 BAIL_OUT
79         fi
80 fi
81
82 # DVI -> PostScript
83 dvips -o ${PSFILE} ${DVIFILE}
84 STATUS=$?
85 if [ ${STATUS} -ne 0 ]; then
86         echo "Failed: dvips -o ${PSFILE} ${DVIFILE}"
87         BAIL_OUT
88 fi
89
90 # PostScript -> Bitmap files
91 # Older versions of gs have problems with a large degree of anti-aliasing
92 # at high resolutions
93 ALPHA=4
94 if [ ${RESOLUTION} -gt 150 ]; then
95         ALPHA=2
96 fi
97
98 gs -q -dNOPAUSE -dBATCH -dSAFER -sDEVICE=pnmraw -sOutputFile=${BASE}%03d.ppm \
99     -dGraphicsAlphaBit=${ALPHA} -dTextAlphaBits=${ALPHA} -r${RESOLUTION} \
100     ${PSFILE}
101
102 STATUS=$?
103 if [ ${STATUS} -ne 0 ]; then
104         echo "Failed: gs ${PSFILE}"
105         BAIL_OUT
106 fi
107
108 # Attempt to generate a file ${METRICS} that contains only the tightpage
109 # bounding box info, extract from ${PSFILE}
110
111 # 1. Create a file containing the sed instructions
112 SEDSCRIPT=bbox.sed
113 cat - > ${SEDSCRIPT} <<EOF
114 # Delete everything that's enclosed between %%BeginDocument and %%EndDocument
115 /^\%\%BeginDocument/,/^\%\%EndDocument/d
116
117 # Extract the tightpage bounding box info.
118 # Given this snippet:
119 # %%Page: 1 1
120 # 1 0 bop
121 # -32890 -32890 32890 32890 492688 0 744653
122 # The sed command gives this:
123 # %%Page 1: -32890 -32890 32890 32890 492688 0 744653
124
125 /^\%\%Page:/{
126   s/\: \(.*\) .*$/ \1: /;N;N
127   s/\n[^\n]*\n//p
128 }
129
130 # Delete everything (so only the stuff that's printed, above, goes into the
131 # metrics file).
132 d
133 EOF
134
135 # 2. Run sed!
136 sed -f ${SEDSCRIPT} < ${PSFILE} > ${METRICS}
137 rm -f ${SEDSCRIPT}
138
139 # The ppm files have spurious (?! say some !) white space on the left and right
140 # sides. If you want this removed set REMOVE_WS=1.
141 REMOVE_WS=0
142
143 which pnmcrop > /dev/null
144 STATUS=$?
145
146 if [ ${STATUS} -ne 0 ]; then
147         REMOVE_WS=0
148 fi
149
150 if [ ${REMOVE_WS} -eq 1 ]; then
151         TMP=.${BASE}.ppm
152         for FILE in `ls ${BASE}???.ppm`
153         do
154                 pnmcrop -left ${FILE} | pnmcrop -right > ${TMP}
155                 STATUS=$?
156                 if [ ${STATUS} -eq 0 ]; then
157                         mv -f ${TMP} ${FILE}
158                 fi
159         done
160 fi
161
162 # All was successful, so remove everything except the ppm files and the
163 # metrics file.
164 FILES=`ls ${BASE}* | sed -e "/${BASE}.metrics/d" -e "/${BASE}[0-9]\{3\}.ppm/d"`
165 rm -f ${FILES}