summaryrefslogtreecommitdiff
path: root/contrib/fixbb.m4
blob: 4e5ea739d2e81beb76942db864cc6191d4585479 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
include(shell.m4)
#! /bin/sh -e
# -*- ksh -*-

# Get the name of the program
program=`echo $0 | sed 's#.*/##g'`

# Look for a running ghostscript
gs=${GHOSTSCRIPT:-${GS:-gs}}
tmpdir=`mktemp -d -t fixbb.XXXXXX` || { echo "$program: Cannot create temporary dir!" >&2 ; exit 1; }

# fixbb -- Fix bounding box info in postscript files.
# Written 2/92 by ggh@cs.brown.edu,
# using an idea of Doug Crabill (dgc@cs.purdue.edu).
# Revised 9/30/92: Use GNU utilities, make backup copy of input.
# Revised 3/18/93: Set PATH explicitly.
# Revised 4/23/93: Hack FrameMaker header so that it doesn't set paper size.
# 04/21/99: Changed by hf@nike.phys.chemie.uni-muenchen.de to run with gs5.x
#           and some sort of eps-files.
#------------------------------------------------------------------------------

help="Try \`$program --help' for more information."

# Parse command line arguments.
option_without_arguments='vhsqDf'

GETOPT([[    -v | --v*) echo "$version"; exit 0;;
    -h | --h*) echo "$usage"; exit 0;;
    -q | -s | --s* | --q*) verbose=:;;
    # Delay debugging so that options parsing does not appear
    -D | --debug) debug=: ;;
    -o | --output) shift ; output=$1 ;;
    -f | --force)
     # Refuse if gs does not seem to work
     if test "x$gs" = x; then
       echo "$program: error: ghostscript does not work." >&2
       exit 1
     else
       run_gs=1
     fi
     ;;
    -) # We are working with stdin ;;
      set dummy "$@" "$1"; shift;;]])

# Check the number of arguments.
case $# in
  0)  file=-;;
  1)  file=$1;;
  *)  echo "$program: too many arguments" 1>&2
      echo "$help"  1>&2
      exit 1;;
esac

if test -n "$debug"; then
  # Set -x now if debugging
  set -x
else
  # Temp dir.  Get ready not to leave junk (if not debugging)
  trap "/bin/rm -rf $tmpdir" 0 1 2 3 13 15
fi

#------------------------------------------------------------------------------
# Render image using GhostScript.
#------------------------------------------------------------------------------

# Does ghostscript run?
# Run test in a subshell; some versions of sh will print an error if
# an executable is not found, even if stderr is redirected.
if ($gs -v) >/dev/null 2>&1; then :; else
  echo "Cannot not run $gs" >&2
  exit 1
fi

echo "Computing bounding box: ."

# If gs support the bbox device, then cool!
if ($gs -h | grep bbox) >/dev/null 2>&1; then
  bbox_awk='
/^%%BoundingBox:/ {
  if (bbox_llx < $1)
     bbox_llx = $1 ;
  if (bbox_lly < $2)
     bbox_lly = $2;
  if (bbox_urx > $3)
     bbox_urx = $3;
  if (bbox_ury > $4)
     bbox_urx = $4;
}

/^%%HiResBoundingBox:/ {
  if (hrbbox_llx < $1)
     hrbbox_llx = $1 ;
  if (hrbbox_lly < $2)
     hrbbox_lly = $2;
  if (hrbbox_urx > $3)
     hrbbox_urx = $3;
  if (hrbbox_ury > $4)
     hrbbox_urx = $4;
}

END {
 print "%%BoundingBox:" bbox_llx bbox_lly bbox_urx bbox_ury;
 print "%%HiResBoundingBox:" hrbbox_llx hrbbox_lly hrbbox_urx hrbbox_ury;
}
'
  # I don't know why, but the result is sent onto stderr.
  bbox=`$gs -dNOPAUSE -sDEVICE=bbox $file 2>&1 >/dev/null | awk "$bbox_awk"`
  echo $bbox
  exit 0
else
  echo "Cannot not run $gs -sDEVICE=bbox" >&2
  exit 1
fi

BBOX=`$GS -dNOPAUSE -dQUIET -dBATCH -sDEVICE=epswrite -sOutputFile=- $1 | \
grep "^%%BoundingBox: *[0-9]" | cut -d" " -f2- `

echo -n "."

if [ $? -ne 0 ]; then echo; echo "fixbb: Could not find bbox" >&2; exit 1; fi

echo "$BBOX"

echo -n "Editing $1: ."

#------------------------------------------------------------------------------
# Backup original file and replace with updated version.
#------------------------------------------------------------------------------

cp $1 $1.orig

if [ $? -ne 0 ]; then echo; echo "fixbb: Could not save original file" >&2; exit 1; fi

#------------------------------------------------------------------------------
# Update %%BoundingBox comment in source file, nuke /papersize in Frame output.
#------------------------------------------------------------------------------

awk '\
  /^%%BoundingBox/ {if (!done) print "%%BoundingBox: '"$BBOX"'"; done=1; next}\
  /FrameDict/   {frame=1}\
  /%%EndProlog/ {if (frame) print "FrameDict begin /papersize {false} def end"}\
  /.*/          {print}\
  END           {if (!done) print "%%BoundingBox: '"$BBOX"'"}' \
 $1.orig > $1

if [ $? -ne 0 ]; then
    echo; echo "fixbb: Edit of $1 failed" >&2;
    cp $1.orig $1;
    if [ $? -ne 0 ]; then
        echo;
        echo "fixbb: Could not write back original file saved in $1.orig" >&2;
        exit 1;
    fi
    exit 1;
fi

echo ".done"