3 # \file lyxpreview2ppm.sh
4 # Copyright 2002 the LyX Team
5 # Read the file COPYING
7 # \author Angus Leeming, leeming@lyx.org
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.
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.
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.
23 # A couple of helper functions
25 which ${EXECUTABLE} > /dev/null
27 if [ ${STATUS} -ne 0 ]; then
28 echo "Unable to find \"${EXECUTABLE}\". Please install."
34 # Remove everything except the original .tex file.
35 FILES=`ls ${BASE}* | sed -e "/${BASE}.tex/d"`
36 rm -f ${FILES} texput.log
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
45 # Initialise some variables.
50 BASE=`basename $1 .tex`
53 METRICSFILE=${BASE}.metrics
59 if [ ${STATUS} -ne 0 ]; then
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}
68 if [ ${STATUS} -eq 0 ]; then
70 echo "Failed: latex ${TEXFILE}"
74 mv -f .${TEXFILE} ${TEXFILE}
77 if [ ${STATUS} -ne 0 ]; then
78 echo "Failed: latex ${TEXFILE}"
84 dvips -o ${PSFILE} ${DVIFILE}
86 if [ ${STATUS} -ne 0 ]; then
87 echo "Failed: dvips -o ${PSFILE} ${DVIFILE}"
91 # PostScript -> Bitmap files
92 # Older versions of gs have problems with a large degree of anti-aliasing
95 if [ ${RESOLUTION} -gt 150 ]; then
99 gs -q -dNOPAUSE -dBATCH -dSAFER -sDEVICE=pnmraw -sOutputFile=${BASE}%03d.ppm \
100 -dGraphicsAlphaBit=${ALPHA} -dTextAlphaBits=${ALPHA} -r${RESOLUTION} \
104 if [ ${STATUS} -ne 0 ]; then
105 echo "Failed: gs ${PSFILE}"
109 # Attempt to generate a file ${METRICSFILE} that contains only the tightpage
110 # bounding box info, extract from ${PSFILE}
112 # 1. Create a file containing the sed instructions
114 cat - > ${SEDFILE} <<EOF
115 # Delete everything that's enclosed between %%BeginDocument and %%EndDocument
116 /^\%\%BeginDocument/,/^\%\%EndDocument/d
118 # Extract the tightpage bounding box info.
119 # Given this snippet:
122 # -32890 -32890 32890 32890 492688 0 744653
123 # The sed command gives this:
124 # %%Page 1: -32890 -32890 32890 32890 492688 0 744653
127 s/\: \(.*\) .*$/ \1: /;N;N
131 # Delete everything (so only the stuff that's printed, above, goes into the
137 sed -f ${SEDFILE} < ${PSFILE} > ${METRICSFILE}
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.
144 which pnmcrop > /dev/null
147 if [ ${STATUS} -ne 0 ]; then
151 if [ ${REMOVE_WS} -eq 1 ]; then
153 for FILE in `ls ${BASE}???.ppm`
155 pnmcrop -left ${FILE} | pnmcrop -right > ${TMP}
157 if [ ${STATUS} -eq 0 ]; then
163 # All was successful, so remove everything except the ppm files and the
165 FILES=`ls ${BASE}* | sed -e "/${BASE}.metrics/d" -e "/${BASE}[0-9]\{3\}.ppm/d"`