]> git.lyx.org Git - lyx.git/blob - lib/scripts/lyxpreview2ppm.sh
Preview code
[lyx.git] / lib / scripts / lyxpreview2ppm.sh
1 #!/bin/sh
2
3 # This script takes a LaTeX file and generates PPM files, one per page.
4 # The idea is to use it with preview.sty to create small bitmap previews of
5 # things like math equations.
6
7 # The script takes two arguments, the name of the file to be converted and
8 # the resolution of the generated image, to be passed to gs.
9 if [ $# -ne 2 ]; then
10         exit 1
11 fi
12
13 # A couple of helper functions
14 FIND_EXECUTABLE=""
15 FIND_IT () {
16         which ${FIND_EXECUTABLE} > /dev/null
17         STATUS=$?
18         if [ ${STATUS} -ne 0 ]; then
19                 echo "Unable to find \"${FIND_EXECUTABLE}\". Please install."
20                 exit 1
21         fi
22 }
23
24 CHECK_STATUS () {
25         if [ ${STATUS} -ne 0 ]; then
26                 echo "${EXECUTABLE} failed."
27                 # Remove everything except the original .tex file.
28                 FILES=`ls ${BASE}* | sed -e "/${BASE}.tex/d"`
29                 rm -f ${FILES}
30                 exit ${STATUS}
31         fi
32 }
33
34 # We use latex, dvips and gs, so check that they're all there.
35 FIND_EXECUTABLE=latex; FIND_IT
36 FIND_EXECUTABLE=dvips; FIND_IT
37 FIND_EXECUTABLE=gs;    FIND_IT
38
39 # Initialise some variables.
40 TEXFILE=$1
41 RESOLUTION=$2
42
43 DIR=`dirname ${TEXFILE}`
44 BASE=`basename ${TEXFILE} .tex`
45 DVIFILE=${BASE}.dvi
46 PSFILE=${BASE}.ps
47 METRICS=${BASE}.metrics
48
49 # Perform the conversion.
50 cd ${DIR}
51 latex -interaction=batchmode ${TEXFILE}
52
53 STATUS=$?
54 EXECUTABLE="latex ${TEXFILE}"; CHECK_STATUS
55
56 dvips -o ${PSFILE} ${DVIFILE}
57
58 STATUS=$?
59 EXECUTABLE="dvips ${DVIFILE}"; CHECK_STATUS
60
61 # Older versions of gs have problems with a large degree of anti-aliasing
62 # at high resolutions
63 ALPHA=4
64 if [ ${RESOLUTION} -gt 150 ]; then
65         ALPHA=2
66 fi
67
68 gs -q -dNOPAUSE -dBATCH -dSAFER -sDEVICE=pnm -sOutputFile=${BASE}%03d.ppm \
69     -dGraphicsAlphaBit=${ALPHA} -dTextAlphaBits=${ALPHA} -r${RESOLUTION} \
70     ${PSFILE}
71
72 STATUS=$?
73 EXECUTABLE="gs ${PSFILE}"; CHECK_STATUS
74
75 # Attempt to generate a file ${METRICS} that contains only the tightpage
76 # bounding box info, extract from ${PSFILE}
77
78 # 1. Create a file containing the sed instructions
79 SEDSCRIPT=bbox.sed
80 cat - > ${SEDSCRIPT} <<EOF
81 # Delete everything that's enclosed between %%BeginDocument and %%EndDocument
82 /^\%\%BeginDocument/,/^\%\%EndDocument/d
83
84 # Extract the tightpage bounding box info.
85 # Given this snippet:
86 # %%Page: 1 1
87 # 1 0 bop
88 # -32890 -32890 32890 32890 492688 0 744653
89 # The sed command gives this:
90 # %%Page 1: -32890 -32890 32890 32890 492688 0 744653
91
92 /^\%\%Page:/{
93   s/\: \(.*\) .*$/ \1: /;N;N
94   s/\n[^\n]*\n//p
95 }
96
97 # Delete everything (so only the stuff that's printed, above, goes into the
98 # metrics file).
99 d
100 EOF
101
102 # 2. Run sed!
103 sed -f ${SEDSCRIPT} < ${PSFILE} > ${METRICS}
104 STATUS=$?
105 rm -f ${SEDSCRIPT}
106 EXECUTABLE="extracting metrics"; CHECK_STATUS
107
108 # All was successful, so remove everything except the ppm files and the
109 # metrics file.
110 FILES=`ls ${BASE}* | sed -e "/${BASE}.metrics/d" -e "/${BASE}.*.ppm/d"`
111 rm -f ${FILES}