Initial import from FreeBSD RELENG_4:
[dragonfly.git] / contrib / groff / contrib / eqn2graph / eqn2graph.sh
1 #!/bin/sh
2 #
3 # eqn2graph -- compile EQN equation descriptions to bitmap images
4 #
5 # by Eric S. Raymond <esr@thyrsus.com>, July 2002
6 #
7 # In Unixland, the magic is in knowing what to string together...
8 #
9 # Take an eqn equation on stdin, emit cropped bitmap on stdout.
10 # The pic markup should *not* be wrapped in .EQ/.EN, this script will do that.
11 # A -U option on the command line enables gpic/groff "unsafe" mode.
12 # A -format FOO option changes the image output format to any format
13 # supported by convert(1).  All other options are passed to convert(1).
14 # The default format is PNG.
15 #
16 # This is separate from pic2graph because pic processing has some weird
17 # clipping effect on the output, mangling equations that are very wide 
18 # or deep.  Besides, this tool can supply its own delimiters.
19 #
20
21 # Requires the groff suite and the ImageMagick tools.  Both are open source.
22 # This code is released to the public domain.
23 #
24 # Here are the assumptions behind the option processing:
25 #
26 # 1. None of the options of eqn(1) are relevant.
27 #
28 # 2. Only the -U option of groff(1) is relevant.
29 #
30 # 3. Many options of convert(1) are potentially relevant, (especially 
31 # -density, -interlace, -transparency, -border, and -comment).
32 #
33 # Thus, we pass -U to groff(1), and everything else to convert(1).
34 #
35 # $Id: eqn2graph.sh,v 1.2 2002/07/17 04:55:46 wlemb Exp $
36 #
37 groff_opts=""
38 convert_opts=""
39 format="png"
40
41 while [ "$1" ]
42 do
43     case $1 in
44     -unsafe)
45         groff_opts="-U";;
46     -format)
47         format=$2
48         shift;;
49     -v | --version)
50         echo "GNU eqn2graph (groff) version @VERSION@"
51         exit 0;;
52     --help)
53         echo "usage: eqn2graph [ option ...] < in > out"
54         exit 0;;
55     *)
56         convert_opts="$convert_opts $1";;
57     esac
58     shift
59 done
60
61 # Here goes:
62 # 1. Add .EQ/.EN.
63 # 2. Process through eqn(1) to emit troff markup.
64 # 3. Process through groff(1) to emit Postscript.
65 # 4. Use convert(1) to crop the Postscript and turn it into a bitmap.
66 tmp=/usr/tmp/eqn2graph-$$
67 trap "rm ${tmp}.*" 0 2 15 
68 read equation
69 (echo ".EQ"; echo 'delim $$'; echo ".EN"; echo '$'"${equation}"'$') | \
70         groff -e $groff_opts -Tps >${tmp}.ps \
71         && convert -crop 0x0 $convert_opts ${tmp}.ps ${tmp}.${format} \
72         && cat ${tmp}.${format}
73
74 # End