epscrop 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #!/bin/bash
  2. # epscrop -- crop EPS files using ghostscript
  3. # Copyright (C) 2002, Maarten Ditzel
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. # process arguments
  19. infile=$1
  20. if [ "$1" == "-h" ]; then
  21. echo "synopsis: epscrop <file>"
  22. echo "author: Maarten Ditzel (February 2002)"
  23. echo "description: adjusts the bounding box of <file> to fit"
  24. exit 1
  25. fi
  26. if (( $# < 1 )); then
  27. echo "no input file specified"
  28. exit 2
  29. fi
  30. # create a temporary directory
  31. tmpdir=/tmp/epscrop-$$
  32. mkdir $tmpdir
  33. # check ghostscript version (both GNU and Aladdin should work)
  34. gstype=`gs -v | awk '/^GNU|AFPL/ { print $1; }'`
  35. version=`gs -v | awk '/^GNU|AFPL/ { print $3; }'`
  36. major=`echo $version | awk -F "." '{ print $1; }'`
  37. minor=`echo $version | awk -F "." '{ print $2; }'`
  38. if [[ (( $major > 5 )) && (( $minor > 50 )) ]]; then
  39. # retrieve the bounding box
  40. gs -dNOPAUSE -sDEVICE=bbox -q $infile quit.ps 2> $tmpdir/bbox
  41. else
  42. # bbox device doesn't seem to work for previous versions
  43. # thus we use epswrite to find the bounding box
  44. # Q: why don't we use epswrite all the time ?
  45. # A: it returns a poor quality eps
  46. # Q: why don't we use the psbb program ?
  47. # A: only returns the boundingbox already specified in the file
  48. # and the whole point is to replace it with a cropped version
  49. gs -dNOPAUSE -sDEVICE=epswrite -sOutputFile=$tmpdir/bbox.eps -q $infile quit.ps
  50. grep BoundingBox $tmpdir/bbox.eps > $tmpdir/bbox
  51. fi
  52. # get the cropped bounding box
  53. bbox=`awk '/^%%BoundingBox:/ { print $2,$3,$4,$5; }' $tmpdir/bbox`
  54. # create awk script
  55. cat <<EOF > $tmpdir/epscrop.awk
  56. # created by epscrop
  57. BEGIN {
  58. bbdone = 0;
  59. }
  60. /^%%BoundingBox/ {
  61. if (!bbdone) {
  62. print \$1, bbox;
  63. bbdone = 1;
  64. }
  65. else
  66. print;
  67. }
  68. !/^%%BoundingBox/ {
  69. print;
  70. }
  71. EOF
  72. # adjust bounding box
  73. awk -v bbox="$bbox" -f $tmpdir/epscrop.awk $infile
  74. # remove the temporary directory
  75. rm -r $tmpdir