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
26 which ${FIND_EXECUTABLE} > /dev/null
28 if [ ${STATUS} -ne 0 ]; then
29 echo "Unable to find \"${FIND_EXECUTABLE}\". Please install."
35 if [ ${STATUS} -ne 0 ]; then
36 echo "${EXECUTABLE} failed."
37 # Remove everything except the original .tex file.
38 FILES=`ls ${BASE}* | sed -e "/${BASE}.tex/d"`
44 # We use latex, dvips and gs, so check that they're all there.
45 FIND_EXECUTABLE=latex; FIND_IT
46 FIND_EXECUTABLE=dvips; FIND_IT
47 FIND_EXECUTABLE=gs; FIND_IT
49 # Initialise some variables.
53 DIR=`dirname ${TEXFILE}`
54 BASE=`basename ${TEXFILE} .tex`
57 METRICS=${BASE}.metrics
59 # Perform the conversion.
63 EXECUTABLE="latex ${TEXFILE}"; CHECK_STATUS
65 dvips -o ${PSFILE} ${DVIFILE}
68 EXECUTABLE="dvips ${DVIFILE}"; CHECK_STATUS
70 # Older versions of gs have problems with a large degree of anti-aliasing
73 if [ ${RESOLUTION} -gt 150 ]; then
77 gs -q -dNOPAUSE -dBATCH -dSAFER -sDEVICE=pnm -sOutputFile=${BASE}%03d.ppm \
78 -dGraphicsAlphaBit=${ALPHA} -dTextAlphaBits=${ALPHA} -r${RESOLUTION} \
82 EXECUTABLE="gs ${PSFILE}"; CHECK_STATUS
84 # Attempt to generate a file ${METRICS} that contains only the tightpage
85 # bounding box info, extract from ${PSFILE}
87 # 1. Create a file containing the sed instructions
89 cat - > ${SEDSCRIPT} <<EOF
90 # Delete everything that's enclosed between %%BeginDocument and %%EndDocument
91 /^\%\%BeginDocument/,/^\%\%EndDocument/d
93 # Extract the tightpage bounding box info.
97 # -32890 -32890 32890 32890 492688 0 744653
98 # The sed command gives this:
99 # %%Page 1: -32890 -32890 32890 32890 492688 0 744653
102 s/\: \(.*\) .*$/ \1: /;N;N
106 # Delete everything (so only the stuff that's printed, above, goes into the
112 sed -f ${SEDSCRIPT} < ${PSFILE} > ${METRICS}
115 EXECUTABLE="extracting metrics"; CHECK_STATUS
117 # The ppm files have spurious (?! say some !) white space on the left and right
118 # sides. If you want this removed set REMOVE_WS=1.
121 which pnmcrop > /dev/null
124 if [ ${STATUS} -ne 0 ]; then
128 if [ ${REMOVE_WS} -eq 1 ]; then
130 for FILE in `ls ${BASE}???.ppm`
132 pnmcrop -left ${FILE} | pnmcrop -right > ${TMP}
134 if [ ${STATUS} -eq 0 ]; then
140 # All was successful, so remove everything except the ppm files and the
142 FILES=`ls ${BASE}* | sed -e "/${BASE}.metrics/d" -e "/${BASE}.*.ppm/d"`