/contrib/groff/contrib/grap2graph/grap2graph.sh

https://bitbucket.org/freebsd/freebsd-head/ · Shell · 85 lines · 39 code · 8 blank · 38 comment · 11 complexity · 12bd52f3df18445e2397216b4ea54770 MD5 · raw file

  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 pic 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. # 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. None of the options of grap(1) are relevant.
  22. #
  23. # 2. Only the -U option of groff(1) is relevant.
  24. #
  25. # 3. Many options of convert(1) are potentially relevant, (especially
  26. # -density, -interlace, -transparency, -border, and -comment).
  27. #
  28. # Thus, we pass -U to groff(1), and everything else to convert(1).
  29. #
  30. # $Id: grap2graph.sh,v 1.4 2005/05/18 07:03:06 wl Exp $
  31. #
  32. groff_opts=""
  33. convert_opts=""
  34. format="png"
  35. while [ "$1" ]
  36. do
  37. case $1 in
  38. -unsafe)
  39. groff_opts="-U";;
  40. -format)
  41. format=$2
  42. shift;;
  43. -v | --version)
  44. echo "GNU grap2graph (groff) version @VERSION@"
  45. exit 0;;
  46. --help)
  47. echo "usage: grap2graph [ option ...] < in > out"
  48. exit 0;;
  49. *)
  50. convert_opts="$convert_opts $1";;
  51. esac
  52. shift
  53. done
  54. # create temporary directory
  55. tmp=
  56. for d in "$GROFF_TMPDIR" "$TMPDIR" "$TMP" "$TEMP" /tmp; do
  57. test -z "$d" && continue
  58. tmp=`(umask 077 && mktemp -d -q "$d/grap2graph-XXXXXX") 2> /dev/null` \
  59. && test -n "$tmp" && test -d "$tmp" \
  60. && break
  61. tmp=$d/grap2graph$$-$RANDOM
  62. (umask 077 && mkdir $tmp) 2> /dev/null && break
  63. done;
  64. if test -z "$tmp"; then
  65. echo "$0: cannot create temporary directory" >&2
  66. { (exit 1); exit 1; }
  67. fi
  68. trap 'exit_status=$?; rm -rf $tmp && exit $exit_status' 0 2 15
  69. # Here goes:
  70. # 1. Add .G1/.G2.
  71. # 2. Process through grap(1) to emit pic markup.
  72. # 3. Process through groff(1) with pic preprocessing to emit Postscript.
  73. # 4. Use convert(1) to crop the Postscript and turn it into a bitmap.
  74. (echo ".G1"; cat; echo ".G2") | grap | groff -p $groff_opts -Tps -P-pletter | \
  75. convert -trim -crop 0x0 $convert_opts - $tmp/grap2graph.$format \
  76. && cat $tmp/grap2graph.$format
  77. # End