Imported Upstream version 1.22.4
[platform/upstream/groff.git] / contrib / grap2graph / grap2graph.sh
1 #! /bin/sh
2 #
3 # grap2graph -- compile graph description descriptions to bitmap images
4 #
5 # by Eric S. Raymond <esr@thyrsus.com>, May 2003
6 #
7 # In Unixland, the magic is in knowing what to string together...
8 #
9 # Take grap description on stdin, emit cropped bitmap on stdout.
10 # The grap markup should *not* be wrapped in .G1/.G2, 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
17 # Requires the groff suite and the ImageMagick tools.  Both are open source.
18 # This code is released to the public domain.
19 #
20 # Here are the assumptions behind the option processing:
21 #
22 # 1. None of the options of grap(1) are relevant.
23 #
24 # 2. Only the -U option of groff(1) is relevant.
25 #
26 # 3. Many options of convert(1) are potentially relevant, (especially 
27 # -density, -interlace, -transparency, -border, and -comment).
28 #
29 # Thus, we pass -U to groff(1), and everything else to convert(1).
30 #
31 groff_opts=""
32 convert_opts=""
33 convert_trim_arg="-trim"
34 format="png"
35
36 while [ "$1" ]
37 do
38     case $1 in
39     -unsafe)
40         groff_opts="-U";;
41     -format)
42         format=$2
43         shift;;
44     -v | --version)
45         echo "GNU grap2graph (groff) version @VERSION@"
46         exit 0;;
47     --help)
48         echo "usage: grap2graph [ option ...] < in > out"
49         exit 0;;
50     *)
51         convert_opts="$convert_opts $1";;
52     esac
53     shift
54 done
55
56 # create temporary directory
57 tmp=
58 for d in "$GROFF_TMPDIR" "$TMPDIR" "$TMP" "$TEMP" /tmp
59 do
60     test -n "$d" && break
61 done
62
63 if ! test -d "$d"
64 then
65     echo "$0: error: temporary directory \"$d\" does not exist or is" \
66         "not a directory" >&2
67     exit 1
68 fi
69
70 if ! tmp=`(umask 077 && mktemp -d -q "$d/grap2graph-XXXXXX") 2> /dev/null`
71 then
72     # mktemp failed--not installed or is a version that doesn't support those
73     # flags?  Fall back to older method which uses more predictable naming.
74     #
75     # $RANDOM is a Bashism.  The fallback of $PPID is not good pseudorandomness,
76     # but is supported by the stripped-down dash shell, for instance.
77     tmp="$d/grap2graph$$-${RANDOM:-$PPID}"
78     (umask 077 && mkdir "$tmp") 2> /dev/null
79 fi
80
81 if ! test -d "$tmp"
82 then
83     echo "$0: error: cannot create temporary directory \"$tmp\"" >&2
84     exit 1
85 fi
86
87 # See if the installed version of convert(1) is new enough to support the -trim
88 # option.  Versions that didn't were described as "old" as early as 2008.
89 is_convert_recent=`convert -help | grep -e -trim`
90 if test -z "$is_convert_recent"
91 then
92     echo "$0: warning: falling back to old '-crop 0x0' trim method" >&2
93     convert_trim_arg="-crop 0x0"
94 fi
95
96 trap 'exit_status=$?; rm -rf "$tmp" && exit $exit_status' EXIT INT TERM
97
98 # Here goes:
99 # 1. Add .G1/.G2.
100 # 2. Process through grap(1) to emit pic markup.
101 # 3. Process through groff(1) with pic preprocessing to emit Postscript.
102 # 4. Use convert(1) to crop the Postscript and turn it into a bitmap.
103 (echo ".G1"; cat; echo ".G2") | grap | groff -p $groff_opts -Tps -P-pletter | \
104     convert -trim $convert_opts - "$tmp"/grap2graph.$format \
105     && cat "$tmp"/grap2graph.$format
106
107 # End