| Commit | Line | Data |
|---|---|---|
| 465b256c | 1 | #! /bin/sh |
| 92d0a6a6 JR |
2 | # |
| 3 | # pic2graph -- compile PIC image 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 a pic/eqn diagram on stdin, emit cropped bitmap on stdout. | |
| 10 | # The pic markup should *not* be wrapped in .PS/.PE, this script will do that. | |
| 11 | # An -unsafe 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). An -eqn option changes the eqn delimiters. | |
| 14 | # All other options are passed to convert(1). The default format in PNG. | |
| 15 | # | |
| 16 | # Requires the groff suite and the ImageMagick tools. Both are open source. | |
| 17 | # This code is released to the public domain. | |
| 18 | # | |
| 19 | # Here are the assumptions behind the option processing: | |
| 20 | # | |
| 21 | # 1. Only the -U option of gpic(1) is relevant. -C doesn't matter because | |
| 22 | # we're generating our own .PS/.PE, -[ntcz] are irrelevant because we're | |
| 23 | # generating Postscript. | |
| 24 | # | |
| 25 | # 2. Ditto for groff(1), though it's a longer and more tedious demonstration. | |
| 26 | # | |
| 27 | # 3. Many options of convert(1) are potentially relevant (especially | |
| 28 | # -density, -interlace, -transparency, -border, and -comment). | |
| 29 | # | |
| 30 | # Thus, we pass -U to gpic and groff, and everything else to convert(1). | |
| 31 | # | |
| 32 | # We don't have complete option coverage on eqn because this is primarily | |
| 33 | # intended as a pic translator; we can live with eqn defaults. | |
| 34 | # | |
| 4d3e9548 | 35 | # $Id: pic2graph.sh,v 1.8 2008/09/29 00:53:29 esr Exp $ |
| 92d0a6a6 JR |
36 | # |
| 37 | groffpic_opts="" | |
| 38 | gs_opts="" | |
| 39 | convert_opts="" | |
| 40 | format="png" | |
| 41 | eqndelim='$$' | |
| 42 | ||
| 43 | while [ "$1" ] | |
| 44 | do | |
| 45 | case $1 in | |
| 46 | -unsafe) | |
| 47 | groffpic_opts="-U";; | |
| 48 | -format) | |
| 49 | format=$2 | |
| 50 | shift;; | |
| 51 | -eqn) | |
| 52 | eqndelim=$2 | |
| 53 | shift;; | |
| 54 | -v | --version) | |
| 55 | echo "GNU pic2graph (groff) version @VERSION@" | |
| 56 | exit 0;; | |
| 57 | --help) | |
| 58 | echo "usage: pic2graph [ option ...] < in > out" | |
| 59 | exit 0;; | |
| 60 | *) | |
| 61 | convert_opts="$convert_opts $1";; | |
| 62 | esac | |
| 63 | shift | |
| 64 | done | |
| 65 | ||
| 66 | if [ "$eqndelim" ] | |
| 67 | then | |
| 68 | eqndelim="delim $eqndelim" | |
| 69 | fi | |
| 70 | ||
| 71 | # create temporary directory | |
| 72 | tmp= | |
| 73 | for d in "$GROFF_TMPDIR" "$TMPDIR" "$TMP" "$TEMP" /tmp; do | |
| 74 | test -z "$d" && continue | |
| 75 | ||
| 76 | tmp=`(umask 077 && mktemp -d -q "$d/pic2graph-XXXXXX") 2> /dev/null` \ | |
| 77 | && test -n "$tmp" && test -d "$tmp" \ | |
| 78 | && break | |
| 79 | ||
| 80 | tmp=$d/pic2graph$$-$RANDOM | |
| 81 | (umask 077 && mkdir $tmp) 2> /dev/null \ | |
| 82 | && break | |
| 83 | done; | |
| 84 | if test -z "$tmp"; then | |
| 85 | echo "$0: cannot create temporary directory" >&2 | |
| 86 | { (exit 1); exit 1; } | |
| 87 | fi | |
| 88 | ||
| 89 | trap 'exit_status=$?; rm -rf $tmp && exit $exit_status' 0 2 15 | |
| 90 | ||
| 91 | # Here goes: | |
| 92 | # 1. Wrap the input in dummy .PS/PE macros (and add possibly null .EQ/.EN) | |
| 93 | # 2. Process through eqn and pic to emit troff markup. | |
| 94 | # 3. Process through groff to emit Postscript. | |
| 95 | # 4. Use convert(1) to crop the PostScript and turn it into a bitmap. | |
| 96 | (echo ".EQ"; echo $eqndelim; echo ".EN"; echo ".PS"; cat; echo ".PE") | \ | |
| 97 | groff -e -p $groffpic_opts -Tps -P-pletter > $tmp/pic2graph.ps \ | |
| 4d3e9548 | 98 | && convert -trim $convert_opts $tmp/pic2graph.ps $tmp/pic2graph.$format \ |
| 92d0a6a6 JR |
99 | && cat $tmp/pic2graph.$format |
| 100 | ||
| 101 | # End |