]> git.lyx.org Git - lyx.git/blob - lib/scripts/lyxpreview2ppm.sh
Add paragraph dialog to qt2 frontend. First commit, I am sure it can be done better...
[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 ${TEXFILE}
62 STATUS=$?
63 EXECUTABLE="latex ${TEXFILE}"; CHECK_STATUS
64
65 dvips -o ${PSFILE} ${DVIFILE}
66
67 STATUS=$?
68 EXECUTABLE="dvips ${DVIFILE}"; CHECK_STATUS
69
70 # Older versions of gs have problems with a large degree of anti-aliasing
71 # at high resolutions
72 ALPHA=4
73 if [ ${RESOLUTION} -gt 150 ]; then
74         ALPHA=2
75 fi
76
77 gs -q -dNOPAUSE -dBATCH -dSAFER -sDEVICE=pnmraw -sOutputFile=${BASE}%03d.ppm \
78     -dGraphicsAlphaBit=${ALPHA} -dTextAlphaBits=${ALPHA} -r${RESOLUTION} \
79     ${PSFILE}
80
81 STATUS=$?
82 EXECUTABLE="gs ${PSFILE}"; CHECK_STATUS
83
84 # Attempt to generate a file ${METRICS} that contains only the tightpage
85 # bounding box info, extract from ${PSFILE}
86
87 # 1. Create a file containing the sed instructions
88 SEDSCRIPT=bbox.sed
89 cat - > ${SEDSCRIPT} <<EOF
90 # Delete everything that's enclosed between %%BeginDocument and %%EndDocument
91 /^\%\%BeginDocument/,/^\%\%EndDocument/d
92
93 # Extract the tightpage bounding box info.
94 # Given this snippet:
95 # %%Page: 1 1
96 # 1 0 bop
97 # -32890 -32890 32890 32890 492688 0 744653
98 # The sed command gives this:
99 # %%Page 1: -32890 -32890 32890 32890 492688 0 744653
100
101 /^\%\%Page:/{
102   s/\: \(.*\) .*$/ \1: /;N;N
103   s/\n[^\n]*\n//p
104 }
105
106 # Delete everything (so only the stuff that's printed, above, goes into the
107 # metrics file).
108 d
109 EOF
110
111 # 2. Run sed!
112 sed -f ${SEDSCRIPT} < ${PSFILE} > ${METRICS}
113 STATUS=$?
114 rm -f ${SEDSCRIPT}
115 EXECUTABLE="extracting metrics"; CHECK_STATUS
116
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.
119 REMOVE_WS=0
120
121 which pnmcrop > /dev/null
122 STATUS=$?
123
124 if [ ${STATUS} -ne 0 ]; then
125         REMOVE_WS=0
126 fi
127
128 if [ ${REMOVE_WS} -eq 1 ]; then
129         TMP=.${BASE}.ppm
130         for FILE in `ls ${BASE}???.ppm`
131         do
132                 pnmcrop -left ${FILE} | pnmcrop -right > ${TMP}
133                 STATUS=$?
134                 if [ ${STATUS} -eq 0 ]; then
135                         mv -f ${TMP} ${FILE}
136                 fi
137         done
138 fi
139
140 # All was successful, so remove everything except the ppm files and the
141 # metrics file.
142 FILES=`ls ${BASE}* | sed -e "/${BASE}.metrics/d" -e "/${BASE}.*.ppm/d"`
143 rm -f ${FILES}