#! /bin/ksh # findbb -- write PostScript BoundingBox to stdout # Notes: # 0. The '-b' flag can be used with .pdf files. # 1. (e)ps files may contain binary preview images and may # use non-native newlines, so must be handled as binary. # Our approach is to grab the first 5000 bytes # of the file, then convert \012 and \015 to \012 for # processing with tools intended for use with text files. # 2. if the file does not provide a BoundingBox line, use # the GhostScript bbox device (-b flag) to generate one. # 3. if the first entry is %%BoundingBox: (atend), examine # the last 5000 bytes of the file # Bugs: # Does not normalize the line by adding a space after the ':' # Does not handle large preview images # Author: George N. White III # pgm="$(basename $0)" usage="usage: $pgm [-b] filename" TMPDIR=${TMPDIR:-/usr/tmp} GS_PROG=${GS_PROG:-gsc} tempnm=$(mktemp $TMPDIR/$pgm-temp.XXXXXXXXXX ) || exit 1 pipenm=$(mktemp $TMPDIR/$pgm-pipe.XXXXXXXXXX ) || exit 1 /sbin/rm -f "$pipenm" && /sbin/mknod "$pipenm" p trap "rm -f $tempnm $pipenm ; exit \$rc" 0 flags="" case $# in 2) case $1 in -b) flags="b$flags" ; shift ;; *) print -u2 $usage ; exit 1 ;; esac ;; 1) ;; *) print -u2 $usage ; exit 1 ;; esac # can we read the file? if [ -r "$1" ] ; then fnm="$1" else print -u2 "$pgm: unable to read file <$1>." exit 1 fi case $flags in *b*) $GS_PROG -q -dBATCH -dNOPAUSE -sDEVICE=bbox $fnm > $tempnm 2>&1 set $(grep --max-count=1 '^%%BoundingBox:' $tempnm ) || exit 1 ;; *) set $(head --bytes=5000 $fnm | tr '[\012\015]' '\012' | grep --max-count=1 '^%%BoundingBox:') || exit 1 ;; esac case "$#" in 1|2) # handle '^%%BoundingBox:(atend)' and '^%%BoundingBox: (atend)' case "$1$2" in %%BoundingBox:\(atend\)) print -u2 "$pgm -- looking 'atend'" bbox=$(tail --bytes=5000 $fnm | tr '[\012\015]' '\012' \ | grep '^%%BoundingBox:'|tail -1) ;; *) print -u2 "$pgm failed to find BoundingBox" ; exit 1 ;; esac ;; 4|5) # handle BoundingBox:NN ... and BoundingBox: NN ... case "$1$2" in %%BoundingBox:*) bbox="$@" ;; *) ;; esac ;; *) print -u2 "$pgm failed to find BoundingBox, try setting '-b' flag." ; exit 1 esac print "$bbox"