]> git.lyx.org Git - lyx.git/blob - lib/scripts/lyxpreview2ppm.sh
fix font info
[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 METRICSFILE=${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         # This "fix" should be removed once preview-latex 0.73 is released.
64         sed -e '/^\\usepackage/,/{preview}$/s/,showlabels//' \
65                 < ${TEXFILE} > .${TEXFILE}
66         cmp -s ${TEXFILE} .${TEXFILE}
67         STATUS=$?
68         if [ ${STATUS} -eq 0 ]; then
69                 rm -f .${TEXFILE}
70                 echo "Failed: latex ${TEXFILE}"
71                 BAIL_OUT
72         fi
73
74         mv -f .${TEXFILE} ${TEXFILE}
75         latex ${TEXFILE}
76         STATUS=$?
77         if [ ${STATUS} -ne 0 ]; then
78                 echo "Failed: latex ${TEXFILE}"
79                 BAIL_OUT
80         fi
81 fi
82
83 # DVI -> PostScript
84 dvips -o ${PSFILE} ${DVIFILE}
85 STATUS=$?
86 if [ ${STATUS} -ne 0 ]; then
87         echo "Failed: dvips -o ${PSFILE} ${DVIFILE}"
88         BAIL_OUT
89 fi
90
91 # PostScript -> Bitmap files
92 # Older versions of gs have problems with a large degree of anti-aliasing
93 # at high resolutions
94 ALPHA=4
95 if [ ${RESOLUTION} -gt 150 ]; then
96         ALPHA=2
97 fi
98
99 gs -q -dNOPAUSE -dBATCH -dSAFER -sDEVICE=pnmraw -sOutputFile=${BASE}%03d.ppm \
100     -dGraphicsAlphaBit=${ALPHA} -dTextAlphaBits=${ALPHA} -r${RESOLUTION} \
101     ${PSFILE}
102
103 STATUS=$?
104 if [ ${STATUS} -ne 0 ]; then
105         echo "Failed: gs ${PSFILE}"
106         BAIL_OUT
107 fi
108
109 # Attempt to generate a file ${METRICSFILE} that contains only the tightpage
110 # bounding box info, extract from ${PSFILE}
111
112 # 1. Create a file containing the sed instructions
113 SEDFILE=${BASE}.sed
114 cat - > ${SEDFILE} <<EOF
115 # Delete everything that's enclosed between %%BeginDocument and %%EndDocument
116 /^\%\%BeginDocument/,/^\%\%EndDocument/d
117
118 # Extract the tightpage bounding box info.
119 # Given this snippet:
120 # %%Page: 1 1
121 # 1 0 bop
122 # -32890 -32890 32890 32890 492688 0 744653
123 # The sed command gives this:
124 # %%Page 1: -32890 -32890 32890 32890 492688 0 744653
125
126 /^\%\%Page:/{
127   s/\: \(.*\) .*$/ \1: /;N;N
128   s/\n[^\n]*\n//p
129 }
130
131 # Delete everything (so only the stuff that's printed, above, goes into the
132 # metrics file).
133 d
134 EOF
135
136 # 2. Run sed!
137 sed -f ${SEDFILE} < ${PSFILE} > ${METRICSFILE}
138 rm -f ${SEDFILE}
139
140 # The ppm files have spurious (?! say some !) white space on the left and right
141 # sides. If you want this removed set REMOVE_WS=1.
142 REMOVE_WS=0
143
144 which pnmcrop > /dev/null
145 STATUS=$?
146
147 if [ ${STATUS} -ne 0 ]; then
148         REMOVE_WS=0
149 fi
150
151 if [ ${REMOVE_WS} -eq 1 ]; then
152         TMP=.${BASE}.ppm
153         for FILE in `ls ${BASE}???.ppm`
154         do
155                 pnmcrop -left ${FILE} | pnmcrop -right > ${TMP}
156                 STATUS=$?
157                 if [ ${STATUS} -eq 0 ]; then
158                         mv -f ${TMP} ${FILE}
159                 fi
160         done
161 fi
162
163 # All was successful, so remove everything except the ppm files and the
164 # metrics file.
165 FILES=`ls ${BASE}* | sed -e "/${BASE}.metrics/d" -e "/${BASE}[0-9]\{3\}.ppm/d"`
166 rm -f ${FILES}