/contrib/groff/contrib/pic2graph/pic2graph.sh

https://bitbucket.org/freebsd/freebsd-head/ · Shell · 101 lines · 50 code · 9 blank · 42 comment · 13 complexity · c86fcee0318dffa38aafa74190d516fe MD5 · raw file

  1. #! /bin/sh
  2. #
  3. # pic2graph -- compile PIC image descriptions to bitmap images
  4. #
  5. # by Eric S. Raymond <esr@thyrsus.com>, July 2002
  6. # In Unixland, the magic is in knowing what to string together...
  7. #
  8. # Take a pic/eqn diagram on stdin, emit cropped bitmap on stdout.
  9. # The pic markup should *not* be wrapped in .PS/.PE, this script will do that.
  10. # An -unsafe option on the command line enables gpic/groff "unsafe" mode.
  11. # A -format FOO option changes the image output format to any format
  12. # supported by convert(1). An -eqn option changes the eqn delimiters.
  13. # All other options are passed to convert(1). The default format in PNG.
  14. #
  15. # Requires the groff suite and the ImageMagick tools. Both are open source.
  16. # This code is released to the public domain.
  17. #
  18. # Here are the assumptions behind the option processing:
  19. #
  20. # 1. Only the -U option of gpic(1) is relevant. -C doesn't matter because
  21. # we're generating our own .PS/.PE, -[ntcz] are irrelevant because we're
  22. # generating Postscript.
  23. #
  24. # 2. Ditto for groff(1), though it's a longer and more tedious demonstration.
  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 gpic and groff, and everything else to convert(1).
  30. #
  31. # We don't have complete option coverage on eqn because this is primarily
  32. # intended as a pic translator; we can live with eqn defaults.
  33. #
  34. # $Id: pic2graph.sh,v 1.7 2005/05/18 07:03:07 wl Exp $
  35. #
  36. groffpic_opts=""
  37. gs_opts=""
  38. convert_opts=""
  39. format="png"
  40. eqndelim='$$'
  41. while [ "$1" ]
  42. do
  43. case $1 in
  44. -unsafe)
  45. groffpic_opts="-U";;
  46. -format)
  47. format=$2
  48. shift;;
  49. -eqn)
  50. eqndelim=$2
  51. shift;;
  52. -v | --version)
  53. echo "GNU pic2graph (groff) version @VERSION@"
  54. exit 0;;
  55. --help)
  56. echo "usage: pic2graph [ option ...] < in > out"
  57. exit 0;;
  58. *)
  59. convert_opts="$convert_opts $1";;
  60. esac
  61. shift
  62. done
  63. if [ "$eqndelim" ]
  64. then
  65. eqndelim="delim $eqndelim"
  66. fi
  67. # create temporary directory
  68. tmp=
  69. for d in "$GROFF_TMPDIR" "$TMPDIR" "$TMP" "$TEMP" /tmp; do
  70. test -z "$d" && continue
  71. tmp=`(umask 077 && mktemp -d -q "$d/pic2graph-XXXXXX") 2> /dev/null` \
  72. && test -n "$tmp" && test -d "$tmp" \
  73. && break
  74. tmp=$d/pic2graph$$-$RANDOM
  75. (umask 077 && mkdir $tmp) 2> /dev/null \
  76. && break
  77. done;
  78. if test -z "$tmp"; then
  79. echo "$0: cannot create temporary directory" >&2
  80. { (exit 1); exit 1; }
  81. fi
  82. trap 'exit_status=$?; rm -rf $tmp && exit $exit_status' 0 2 15
  83. # Here goes:
  84. # 1. Wrap the input in dummy .PS/PE macros (and add possibly null .EQ/.EN)
  85. # 2. Process through eqn and pic to emit troff markup.
  86. # 3. Process through groff to emit Postscript.
  87. # 4. Use convert(1) to crop the PostScript and turn it into a bitmap.
  88. (echo ".EQ"; echo $eqndelim; echo ".EN"; echo ".PS"; cat; echo ".PE") | \
  89. groff -e -p $groffpic_opts -Tps -P-pletter > $tmp/pic2graph.ps \
  90. && convert -trim -crop 0x0 $convert_opts $tmp/pic2graph.ps $tmp/pic2graph.$format \
  91. && cat $tmp/pic2graph.$format
  92. # End