/contrib/groff/contrib/eqn2graph/eqn2graph.sh

https://bitbucket.org/freebsd/freebsd-head/ · Shell · 91 lines · 41 code · 8 blank · 42 comment · 12 complexity · 1a18acc956f2267c898f83e9c26937a9 MD5 · raw file

  1. #! /bin/sh
  2. #
  3. # eqn2graph -- compile EQN equation 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 an eqn equation on stdin, emit cropped bitmap on stdout.
  10. # The pic markup should *not* be wrapped in .EQ/.EN, 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. # This is separate from pic2graph because pic processing has some weird
  17. # clipping effect on the output, mangling equations that are very wide
  18. # or deep. Besides, this tool can supply its own delimiters.
  19. #
  20. # Requires the groff suite and the ImageMagick tools. Both are open source.
  21. # This code is released to the public domain.
  22. #
  23. # Here are the assumptions behind the option processing:
  24. #
  25. # 1. None of the options of eqn(1) are relevant.
  26. #
  27. # 2. Only the -U option of groff(1) is relevant.
  28. #
  29. # 3. Many options of convert(1) are potentially relevant, (especially
  30. # -density, -interlace, -transparency, -border, and -comment).
  31. #
  32. # Thus, we pass -U to groff(1), and everything else to convert(1).
  33. #
  34. # $Id: eqn2graph.sh,v 1.5 2005/05/18 07:03:06 wl Exp $
  35. #
  36. groff_opts=""
  37. convert_opts=""
  38. format="png"
  39. while [ "$1" ]
  40. do
  41. case $1 in
  42. -unsafe)
  43. groff_opts="-U";;
  44. -format)
  45. format=$2
  46. shift;;
  47. -v | --version)
  48. echo "GNU eqn2graph (groff) version @VERSION@"
  49. exit 0;;
  50. --help)
  51. echo "usage: eqn2graph [ option ...] < in > out"
  52. exit 0;;
  53. *)
  54. convert_opts="$convert_opts $1";;
  55. esac
  56. shift
  57. done
  58. # create temporary directory
  59. tmp=
  60. for d in "$GROFF_TMPDIR" "$TMPDIR" "$TMP" "$TEMP" /tmp; do
  61. test -z "$d" && continue
  62. tmp=`(umask 077 && mktemp -d -q "$d/eqn2graph-XXXXXX") 2> /dev/null` \
  63. && test -n "$tmp" && test -d "$tmp" \
  64. && break
  65. tmp=$d/eqn2graph$$-$RANDOM
  66. (umask 077 && mkdir $tmp) 2> /dev/null && break
  67. done;
  68. if test -z "$tmp"; then
  69. echo "$0: cannot create temporary directory" >&2
  70. { (exit 1); exit 1; }
  71. fi
  72. trap 'exit_status=$?; rm -rf $tmp && exit $exit_status' 0 2 15
  73. # Here goes:
  74. # 1. Add .EQ/.EN.
  75. # 2. Process through eqn(1) to emit troff markup.
  76. # 3. Process through groff(1) to emit Postscript.
  77. # 4. Use convert(1) to crop the Postscript and turn it into a bitmap.
  78. read equation
  79. (echo ".EQ"; echo 'delim $$'; echo ".EN"; echo '$'"$equation"'$') | \
  80. groff -e $groff_opts -Tps -P-pletter > $tmp/eqn2graph.ps \
  81. && convert -trim -crop 0x0 $convert_opts $tmp/eqn2graph.ps $tmp/eqn2graph.$format \
  82. && cat $tmp/eqn2graph.$format
  83. # End