1151497Sru#! /bin/sh
2104862Sru#
3104862Sru# pic2graph -- compile PIC image 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 a pic/eqn diagram on stdin, emit cropped bitmap on stdout.
10104862Sru# The pic markup should *not* be wrapped in .PS/.PE, this script will do that.
11104862Sru# An -unsafe 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).  An -eqn option changes the eqn delimiters.
14104862Sru# All other options are passed to convert(1).  The default format in PNG.
15104862Sru#
16104862Sru# Requires the groff suite and the ImageMagick tools.  Both are open source.
17104862Sru# This code is released to the public domain.
18104862Sru#
19104862Sru# Here are the assumptions behind the option processing:
20104862Sru#
21104862Sru# 1. Only the -U option of gpic(1) is relevant.  -C doesn't matter because
22104862Sru#    we're generating our own .PS/.PE, -[ntcz] are irrelevant because we're
23104862Sru#    generating Postscript.
24104862Sru#
25104862Sru# 2. Ditto for groff(1), though it's a longer and more tedious demonstration.
26104862Sru#
27104862Sru# 3. Many options of convert(1) are potentially relevant (especially 
28104862Sru#    -density, -interlace, -transparency, -border, and -comment).
29104862Sru#
30104862Sru# Thus, we pass -U to gpic and groff, and everything else to convert(1).
31104862Sru#
32104862Sru# We don't have complete option coverage on eqn because this is primarily
33104862Sru# intended as a pic translator; we can live with eqn defaults. 
34104862Sru#
35151497Sru# $Id: pic2graph.sh,v 1.7 2005/05/18 07:03:07 wl Exp $
36104862Sru#
37104862Srugroffpic_opts=""
38104862Srugs_opts=""
39104862Sruconvert_opts=""
40104862Sruformat="png"
41104862Srueqndelim='$$'
42104862Sru
43104862Sruwhile [ "$1" ]
44104862Srudo
45104862Sru    case $1 in
46104862Sru    -unsafe)
47114402Sru	groffpic_opts="-U";;
48104862Sru    -format)
49104862Sru	format=$2
50104862Sru	shift;;
51104862Sru    -eqn)
52104862Sru	eqndelim=$2
53104862Sru	shift;;
54104862Sru    -v | --version)
55104862Sru	echo "GNU pic2graph (groff) version @VERSION@"
56104862Sru	exit 0;;
57104862Sru    --help)
58104862Sru	echo "usage: pic2graph [ option ...] < in > out"
59104862Sru	exit 0;;
60104862Sru    *)
61104862Sru	convert_opts="$convert_opts $1";;
62104862Sru    esac
63104862Sru    shift
64104862Srudone
65104862Sru
66104862Sruif [ "$eqndelim" ]
67104862Sruthen
68104862Sru    eqndelim="delim $eqndelim"
69104862Srufi
70104862Sru
71151497Sru# create temporary directory
72151497Srutmp=
73151497Srufor d in "$GROFF_TMPDIR" "$TMPDIR" "$TMP" "$TEMP" /tmp; do
74151497Sru    test -z "$d" && continue
75151497Sru
76151497Sru    tmp=`(umask 077 && mktemp -d -q "$d/pic2graph-XXXXXX") 2> /dev/null` \
77151497Sru    && test -n "$tmp" && test -d "$tmp" \
78151497Sru    && break
79151497Sru
80151497Sru    tmp=$d/pic2graph$$-$RANDOM
81151497Sru    (umask 077 && mkdir $tmp) 2> /dev/null \
82151497Sru    && break
83151497Srudone;
84151497Sruif test -z "$tmp"; then
85151497Sru    echo "$0: cannot create temporary directory" >&2
86151497Sru    { (exit 1); exit 1; }
87151497Srufi
88151497Sru
89151497Srutrap 'exit_status=$?; rm -rf $tmp && exit $exit_status' 0 2 15 
90151497Sru
91104862Sru# Here goes:
92104862Sru# 1. Wrap the input in dummy .PS/PE macros (and add possibly null .EQ/.EN)
93104862Sru# 2. Process through eqn and pic to emit troff markup.
94104862Sru# 3. Process through groff to emit Postscript.
95104862Sru# 4. Use convert(1) to crop the PostScript and turn it into a bitmap.
96104862Sru(echo ".EQ"; echo $eqndelim; echo ".EN"; echo ".PS"; cat; echo ".PE") | \
97151497Sru    groff -e -p $groffpic_opts -Tps -P-pletter > $tmp/pic2graph.ps \
98151497Sru    && convert -trim -crop 0x0 $convert_opts $tmp/pic2graph.ps $tmp/pic2graph.$format \
99151497Sru    && cat $tmp/pic2graph.$format
100104862Sru
101104862Sru# End
102