1151497Sru#! /bin/sh
2104862Sru#
3104862Sru# eqn2graph -- compile EQN equation descriptions to bitmap images
4104862Sru#
5104862Sru# by Eric S. Raymond <esr@thyrsus.com>, July 2002
6104862Sru#
7104862Sru# In Unixland, the magic is in knowing what to string together...
8104862Sru#
9104862Sru# Take an eqn equation on stdin, emit cropped bitmap on stdout.
10104862Sru# The pic markup should *not* be wrapped in .EQ/.EN, this script will do that.
11104862Sru# A -U option on the command line enables gpic/groff "unsafe" mode.
12104862Sru# A -format FOO option changes the image output format to any format
13104862Sru# supported by convert(1).  All other options are passed to convert(1).
14104862Sru# The default format is PNG.
15104862Sru#
16104862Sru# This is separate from pic2graph because pic processing has some weird
17104862Sru# clipping effect on the output, mangling equations that are very wide 
18104862Sru# or deep.  Besides, this tool can supply its own delimiters.
19104862Sru#
20104862Sru
21104862Sru# Requires the groff suite and the ImageMagick tools.  Both are open source.
22104862Sru# This code is released to the public domain.
23104862Sru#
24104862Sru# Here are the assumptions behind the option processing:
25104862Sru#
26104862Sru# 1. None of the options of eqn(1) are relevant.
27104862Sru#
28104862Sru# 2. Only the -U option of groff(1) is relevant.
29104862Sru#
30104862Sru# 3. Many options of convert(1) are potentially relevant, (especially 
31104862Sru# -density, -interlace, -transparency, -border, and -comment).
32104862Sru#
33104862Sru# Thus, we pass -U to groff(1), and everything else to convert(1).
34104862Sru#
35151497Sru# $Id: eqn2graph.sh,v 1.5 2005/05/18 07:03:06 wl Exp $
36104862Sru#
37104862Srugroff_opts=""
38104862Sruconvert_opts=""
39104862Sruformat="png"
40104862Sru
41104862Sruwhile [ "$1" ]
42104862Srudo
43104862Sru    case $1 in
44104862Sru    -unsafe)
45104862Sru	groff_opts="-U";;
46104862Sru    -format)
47104862Sru	format=$2
48104862Sru	shift;;
49104862Sru    -v | --version)
50104862Sru	echo "GNU eqn2graph (groff) version @VERSION@"
51104862Sru	exit 0;;
52104862Sru    --help)
53104862Sru	echo "usage: eqn2graph [ option ...] < in > out"
54104862Sru	exit 0;;
55104862Sru    *)
56104862Sru	convert_opts="$convert_opts $1";;
57104862Sru    esac
58104862Sru    shift
59104862Srudone
60104862Sru
61151497Sru# create temporary directory
62151497Srutmp=
63151497Srufor d in "$GROFF_TMPDIR" "$TMPDIR" "$TMP" "$TEMP" /tmp; do
64151497Sru    test -z "$d" && continue
65151497Sru
66151497Sru    tmp=`(umask 077 && mktemp -d -q "$d/eqn2graph-XXXXXX") 2> /dev/null` \
67151497Sru    && test -n "$tmp" && test -d "$tmp" \
68151497Sru    && break
69151497Sru
70151497Sru    tmp=$d/eqn2graph$$-$RANDOM
71151497Sru    (umask 077 && mkdir $tmp) 2> /dev/null && break
72151497Srudone;
73151497Sruif test -z "$tmp"; then
74151497Sru    echo "$0: cannot create temporary directory" >&2
75151497Sru    { (exit 1); exit 1; }
76151497Srufi
77151497Sru
78151497Srutrap 'exit_status=$?; rm -rf $tmp && exit $exit_status' 0 2 15 
79151497Sru
80104862Sru# Here goes:
81104862Sru# 1. Add .EQ/.EN.
82104862Sru# 2. Process through eqn(1) to emit troff markup.
83104862Sru# 3. Process through groff(1) to emit Postscript.
84104862Sru# 4. Use convert(1) to crop the Postscript and turn it into a bitmap.
85104862Sruread equation
86151497Sru(echo ".EQ"; echo 'delim $$'; echo ".EN"; echo '$'"$equation"'$') | \
87151497Sru	groff -e $groff_opts -Tps -P-pletter > $tmp/eqn2graph.ps \
88151497Sru	&& convert -trim -crop 0x0 $convert_opts $tmp/eqn2graph.ps $tmp/eqn2graph.$format \
89151497Sru	&& cat $tmp/eqn2graph.$format
90104862Sru
91104862Sru# End
92