]> git.lyx.org Git - lyx.git/blob - lib/scripts/lyxpreview2ppm.sh
Activate the code using preview.sty 0.73. Ditch support for older versions.
[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 # with much help from David Kastrup, david.kastrup@t-online.de.
9 #
10 # This script takes a LaTeX file and generates PPM files, one per page.
11 # The idea is to use it with preview.sty from the preview-latex project
12 # (http://preview-latex.sourceforge.net/) to create small bitmap previews of
13 # things like math equations.
14
15 # preview.sty can be obtained from CTAN/macros/latex/contrib/supported/preview.
16
17 # This script, lyxpreview2ppm.sh, takes two arguments, the name of the file
18 # to be converted and a scale factor, used to ascertain the resolution of the
19 # generated image which is then passed to gs.
20
21 # If successful it will leave in dir ${DIR} a number of image files
22 # ${BASE}[0-9]\{3\}.ppm and a file ${BASE}.metrics containing info needed by
23 # LyX to position the images correctly on the screen. All other files ${BASE}*
24 # will be deleted.
25
26 # Three helper functions.
27 FIND_IT () {
28         which ${EXECUTABLE} > /dev/null
29         if [ $? -ne 0 ]; then
30                 echo "Unable to find \"${EXECUTABLE}\". Please install."
31                 exit 1
32         fi
33 }
34
35 BAIL_OUT () {
36         # Remove everything except the original .tex file.
37         FILES=`ls ${BASE}* | sed -e "/${BASE}.tex/d"`
38         rm -f ${FILES} texput.log
39         exit 1
40 }
41
42 REQUIRED_VERSION () {
43         echo "We require preview.sty version 0.73 or newer. You're using"
44         grep 'Package: preview' ${LOGFILE}
45 }
46
47 # Preliminary check
48 if [ $# -ne 2 ]; then
49         exit 1
50 fi
51
52 # We use latex, dvips and gs, so check that they're all there.
53 EXECUTABLE=latex; FIND_IT
54 EXECUTABLE=dvips; FIND_IT
55 EXECUTABLE=gs;    FIND_IT
56
57 # Initialise some variables.
58 DIR=`dirname $1`
59 BASE=`basename $1 .tex`
60
61 SCALEFACTOR=$2
62
63 TEXFILE=${BASE}.tex
64 LOGFILE=${BASE}.log
65 DVIFILE=${BASE}.dvi
66 PSFILE=${BASE}.ps
67 METRICSFILE=${BASE}.metrics
68
69 # LaTeX -> DVI.
70 cd ${DIR}
71 latex ${TEXFILE}
72 if [ $? -ne 0 ]; then
73         echo "Failed: latex ${TEXFILE}"
74         BAIL_OUT
75 fi
76
77 # Parse ${LOGFILE} to obtain bounding box info to output to ${METRICSFILE}.
78 # This extracts lines starting "Preview: Tightpage" and "Preview: Snippet".
79 grep -E 'Preview: [ST]' ${LOGFILE} > ${METRICSFILE}
80 if [ $? -ne 0 ]; then
81         echo "Failed: grep -E 'Preview: [ST]' ${LOGFILE}"
82         REQUIRED_VERSION
83         BAIL_OUT
84 fi
85
86 # Parse ${LOGFILE} to obtain ${RESOLUTION} for the gs process to follow.
87 # 1. Extract font size from a line like "Preview: Fontsize 20.74pt"
88 # Use grep for speed and because it gives an error if the line is not found.
89 LINE=`grep 'Preview: Fontsize' ${LOGFILE}`
90 if [ $? -ne 0 ]; then
91         echo "Failed: grep 'Preview: Fontsize' ${LOGFILE}"
92         REQUIRED_VERSION
93         BAIL_OUT
94 fi
95 # Use "" quotes in the echo to preserve newlines (technically IFS separators).
96 # The sed script strips out everything that won't form a decimal number from the
97 # line. It bails out after the first match has been made in case there are
98 # multiple lines "Preview: Fontsize". (There shouldn't be.)
99 LATEXFONT=`echo "${LINE}" | sed 's/[^0-9\.]//g; 1q'`
100
101 # 2. Extract magnification from a line like "Preview: Magnification 2074"
102 # If no such line found, default to MAGNIFICATION=1000.
103 LINE=`grep 'Preview: Magnification' ${LOGFILE}`
104 if [ $? -ne 0 ]; then
105         MAGNIFICATION=1000
106 else
107         # The sed script strips out everything that won't form an /integer/.
108         MAGNIFICATION=`echo "${LINE}" | sed 's/[^0-9]//g; 1q'`
109 fi
110
111 # 3. Compute resolution.
112 # "bc" allows floating-point arithmetic, unlike "expr" or "dc".
113 RESOLUTION=`echo "scale=2; \
114                 ${SCALEFACTOR} * (10/${LATEXFONT}) * (1000/${MAGNIFICATION})" \
115                 | bc`
116
117 # DVI -> PostScript
118 dvips -o ${PSFILE} ${DVIFILE}
119 if [ $? -ne 0 ]; then
120         echo "Failed: dvips -o ${PSFILE} ${DVIFILE}"
121         BAIL_OUT
122 fi
123
124 # PostScript -> Bitmap files
125 # Older versions of gs have problems with a large degree of anti-aliasing
126 # at high resolutions
127 ALPHA=4
128 if [ ${RESOLUTION} -gt 150 ]; then
129         ALPHA=2
130 fi
131
132 gs -q -dNOPAUSE -dBATCH -dSAFER -sDEVICE=pnmraw -sOutputFile=${BASE}%03d.ppm \
133     -dGraphicsAlphaBit=${ALPHA} -dTextAlphaBits=${ALPHA} -r${RESOLUTION} \
134     ${PSFILE}
135
136 if [ $? -ne 0 ]; then
137         echo "Failed: gs ${PSFILE}"
138         BAIL_OUT
139 fi
140
141 # All has been successful, so remove everything except the bitmap files
142 # and the metrics file.
143 FILES=`ls ${BASE}* | sed -e "/${BASE}.metrics/d" -e "/${BASE}[0-9]\{3\}.ppm/d"`
144 rm -f ${FILES}