]> git.lyx.org Git - lyx.git/blob - lib/scripts/lyxpreview2ppm.sh
Add an option to crop to the left and to the right of the images if so desired. Add...
[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_EXECUTABLE=""
25 FIND_IT () {
26         which ${FIND_EXECUTABLE} > /dev/null
27         STATUS=$?
28         if [ ${STATUS} -ne 0 ]; then
29                 echo "Unable to find \"${FIND_EXECUTABLE}\". Please install."
30                 exit 1
31         fi
32 }
33
34 CHECK_STATUS () {
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"`
39                 rm -f ${FILES}
40                 exit ${STATUS}
41         fi
42 }
43
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
48
49 # Initialise some variables.
50 TEXFILE=$1
51 RESOLUTION=$2
52
53 DIR=`dirname ${TEXFILE}`
54 BASE=`basename ${TEXFILE} .tex`
55 DVIFILE=${BASE}.dvi
56 PSFILE=${BASE}.ps
57 METRICS=${BASE}.metrics
58
59 # Perform the conversion.
60 cd ${DIR}
61 latex -interaction=batchmode ${TEXFILE}
62
63 STATUS=$?
64 EXECUTABLE="latex ${TEXFILE}"; CHECK_STATUS
65
66 dvips -o ${PSFILE} ${DVIFILE}
67
68 STATUS=$?
69 EXECUTABLE="dvips ${DVIFILE}"; CHECK_STATUS
70
71 # Older versions of gs have problems with a large degree of anti-aliasing
72 # at high resolutions
73 ALPHA=4
74 if [ ${RESOLUTION} -gt 150 ]; then
75         ALPHA=2
76 fi
77
78 gs -q -dNOPAUSE -dBATCH -dSAFER -sDEVICE=pnm -sOutputFile=${BASE}%03d.ppm \
79     -dGraphicsAlphaBit=${ALPHA} -dTextAlphaBits=${ALPHA} -r${RESOLUTION} \
80     ${PSFILE}
81
82 STATUS=$?
83 EXECUTABLE="gs ${PSFILE}"; CHECK_STATUS
84
85 # Attempt to generate a file ${METRICS} that contains only the tightpage
86 # bounding box info, extract from ${PSFILE}
87
88 # 1. Create a file containing the sed instructions
89 SEDSCRIPT=bbox.sed
90 cat - > ${SEDSCRIPT} <<EOF
91 # Delete everything that's enclosed between %%BeginDocument and %%EndDocument
92 /^\%\%BeginDocument/,/^\%\%EndDocument/d
93
94 # Extract the tightpage bounding box info.
95 # Given this snippet:
96 # %%Page: 1 1
97 # 1 0 bop
98 # -32890 -32890 32890 32890 492688 0 744653
99 # The sed command gives this:
100 # %%Page 1: -32890 -32890 32890 32890 492688 0 744653
101
102 /^\%\%Page:/{
103   s/\: \(.*\) .*$/ \1: /;N;N
104   s/\n[^\n]*\n//p
105 }
106
107 # Delete everything (so only the stuff that's printed, above, goes into the
108 # metrics file).
109 d
110 EOF
111
112 # 2. Run sed!
113 sed -f ${SEDSCRIPT} < ${PSFILE} > ${METRICS}
114 STATUS=$?
115 rm -f ${SEDSCRIPT}
116 EXECUTABLE="extracting metrics"; CHECK_STATUS
117
118 # The ppm files have spurious (?! say some !) white space on the left and right
119 # sides. If you don't want this set REMOVE_WS=0.
120 REMOVE_WS=1
121
122 which pnmcrop > /dev/null
123 STATUS=$?
124
125 if [ ${STATUS} -ne 0 ]; then
126         REMOVE_WS=0
127 fi
128
129 if [ REMOVE_WS -eq 1 ]; then
130         TMP=.${BASE}.ppm
131         for FILE=`ls ${BASE}???.ppm`
132         do
133                 pnmcrop -left -right ${FILE} > ${TMP}
134                 STATUS=$?
135                 if [ ${STATUS} -eq 0 ]; then
136                         mv -f ${TMP} ${FILE}
137                 fi
138         done
139         rm -f ${TMP}
140 fi
141
142 # All was successful, so remove everything except the ppm files and the
143 # metrics file.
144 FILES=`ls ${BASE}* | sed -e "/${BASE}.metrics/d" -e "/${BASE}.*.ppm/d"`
145 rm -f ${FILES}