| 1 |
#!/usr/bin/perl |
| 2 |
######################################################################## |
| 3 |
# This program uses ghostscript and its pbm driver to extract absolute |
| 4 |
# bounding box information for any postscript file. |
| 5 |
# |
| 6 |
# The output of the program is the bounding box for every page in the |
| 7 |
# document. |
| 8 |
# |
| 9 |
# This program could certainly be made faster if written in a compiled |
| 10 |
# language, but the time spent within it is probably neglible compared |
| 11 |
# to the time spent by ghostscript. |
| 12 |
# |
| 13 |
# Bugs: |
| 14 |
# The program may be confused by files that directly set the page |
| 15 |
# size. |
| 16 |
# |
| 17 |
# Copyright (C) 2003 Dov Grobgeld <[email protected]> |
| 18 |
# |
| 19 |
# This program is free software; you can redistribute it and/or modify |
| 20 |
# it under the terms of the GNU General Public License as published by |
| 21 |
# the Free Software Foundation; either version 2 of the License, or |
| 22 |
# (at your option) any later version. |
| 23 |
# |
| 24 |
# This program is distributed in the hope that it will be useful, |
| 25 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 26 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 27 |
# GNU General Public License for more details. |
| 28 |
# |
| 29 |
# You should have received a copy of the GNU General Public License |
| 30 |
# along with this program; if not, write to the Free Software |
| 31 |
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 32 |
# |
| 33 |
# For more details see the file COPYING. |
| 34 |
######################################################################## |
| 35 |
|
| 36 |
my $VERSION = "0.2"; |
| 37 |
|
| 38 |
use strict; |
| 39 |
no utf8; |
| 40 |
|
| 41 |
my $do_through_pbm = undef; # Turn this on for old gs compatibility |
| 42 |
my $GSPATH = "gs"; # Your path to gs |
| 43 |
my($baseres, $pagesize, $maxpages, $evenodd, $insert, $pad); |
| 44 |
|
| 45 |
while( $_ = $ARGV[0], /^-/) { |
| 46 |
shift; |
| 47 |
/-res/ && do { $baseres = shift; next }; |
| 48 |
/-letter/ && do { $pagesize = 'letter'; next }; |
| 49 |
/-a4/ && do { $pagesize = 'a4'; next; }; |
| 50 |
/-pages/ && do { $maxpages = shift; next; }; |
| 51 |
/-evenodd/ && do { $evenodd++; next; }; |
| 52 |
/-insert/ && do { $insert++; next; }; |
| 53 |
/-bypbm/ && do { $do_through_pbm++; next; }; |
| 54 |
/-pad/ && do { $pad = shift; next; }; |
| 55 |
/-help/ && do { print <<EOHELP; exit; }; |
| 56 |
$0 - Calculate the bounding boxes of all pages in a PostScript file |
| 57 |
|
| 58 |
Syntax: |
| 59 |
$0 [-res r] [-letter|-a4] filename |
| 60 |
|
| 61 |
Description: |
| 62 |
The program outputs the page number and the Bounding Box parameters |
| 63 |
for every page in the file filename, by postprocessing the output |
| 64 |
from the gs bbox device. |
| 65 |
|
| 66 |
Options: |
| 67 |
-res res Choose a resolution for ghostscript other than the default |
| 68 |
72 lpi. |
| 69 |
-letter Choose paper size. Default is a4. |
| 70 |
-evenodd Report separate document bounding boxes for odd and even pages |
| 71 |
-pages n Set max number of pages to analyze to n. |
| 72 |
-insert Edit the postscript file and change/insert a EPSF directive |
| 73 |
and a BoundingBox statement. |
| 74 |
-bypbm Calculate bounding box by pbm device (obsolete). |
| 75 |
-pad p Padd bounding box on all sides with p postscript points. |
| 76 |
|
| 77 |
Requirements: |
| 78 |
Ghostscript in path with bbox or pbmraw support compiled in. |
| 79 |
|
| 80 |
Author: |
| 81 |
Dov Grobgeld, Rehovot, Israel, 2003 |
| 82 |
EOHELP |
| 83 |
die "Unknown option $_!\n"; |
| 84 |
} |
| 85 |
|
| 86 |
# defaults |
| 87 |
$pagesize = 'a4' unless $pagesize; |
| 88 |
$baseres = 72 unless $baseres; |
| 89 |
$maxpages = 1_000_000 unless $maxpages; |
| 90 |
|
| 91 |
# table of known paper sizes. |
| 92 |
# perl5 |
| 93 |
# %papersizes = ( 'a4'=> [595,842], 'letter', [612,792]); |
| 94 |
# ($pw,$ph) = @{$papersize{$pagesize}}; |
| 95 |
|
| 96 |
my %paperwidth = ( 'a4', 595, 'letter', 612 ); |
| 97 |
my %paperheight = ( 'a4', 842, 'letter', 792 ); |
| 98 |
|
| 99 |
my $pw = $paperwidth{$pagesize}; |
| 100 |
my $ph = $paperheight{$pagesize}; |
| 101 |
|
| 102 |
# Calculate a resolution of about $baseres that gives the pixelwidth |
| 103 |
|
| 104 |
# document bounding box initialization |
| 105 |
my $docllx=$pw; |
| 106 |
my $doclly=$ph; |
| 107 |
my $docurx=0; |
| 108 |
my $docury=0; |
| 109 |
my $odddocllx=$pw; |
| 110 |
my $odddoclly=$ph; |
| 111 |
my $odddocurx=0; |
| 112 |
my $odddocury=0; |
| 113 |
my $evendocllx=$pw; |
| 114 |
my $evendoclly=$ph; |
| 115 |
my $evendocurx=0; |
| 116 |
my $evendocury=0; |
| 117 |
|
| 118 |
# get filename |
| 119 |
my $fn = shift || die "Expected postscript file name!\n"; |
| 120 |
-f $fn || die "No such file $fn!\n"; |
| 121 |
|
| 122 |
# Print header |
| 123 |
print " Page: llx lly urx ury\n"; |
| 124 |
|
| 125 |
$|++; # auto flush output |
| 126 |
# Use the new ghostscript bbox device |
| 127 |
if (!$do_through_pbm) { |
| 128 |
my $gsopt = "-q -dNOPAUSE -sPAPERSIZE=$pagesize -sDEVICE=bbox -sOutputFile=-"; |
| 129 |
my $gscmd = "$GSPATH -r72 $gsopt -- $fn"; |
| 130 |
open(GS, "$gscmd 2>&1|"); |
| 131 |
my $page = 0; |
| 132 |
|
| 133 |
while(<GS>) { |
| 134 |
# Check if we are using an old ghostscript version |
| 135 |
if (/^Unknown device:/) { |
| 136 |
$do_through_pbm++; |
| 137 |
} |
| 138 |
next unless /^\%\%BoundingBox:/; |
| 139 |
my($llx,$lly,$urx,$ury) = split(' ', $'); |
| 140 |
|
| 141 |
$page++; |
| 142 |
printf " %4d %5.f %5.f %5.f %5.f\n", $page, $llx, $lly, $urx, $ury; |
| 143 |
next if $llx == 0 && $urx == 0 && $lly == 0 && $ury == 0; |
| 144 |
|
| 145 |
if ($pad) { |
| 146 |
$llx-=$pad; |
| 147 |
$lly-=$pad; |
| 148 |
$urx+=$pad; |
| 149 |
$ury+=$pad; |
| 150 |
} |
| 151 |
|
| 152 |
if ($evenodd) { |
| 153 |
if ($page % 2) { # odd |
| 154 |
$odddocllx=$llx if $llx<$odddocllx; |
| 155 |
$odddoclly=$lly if $lly<$odddoclly; |
| 156 |
$odddocurx=$urx if $urx>$odddocurx; |
| 157 |
$odddocury=$ury if $ury>$odddocury; |
| 158 |
} |
| 159 |
else { |
| 160 |
$evendocllx=$llx if $llx<$evendocllx; |
| 161 |
$evendoclly=$lly if $lly<$evendoclly; |
| 162 |
$evendocurx=$urx if $urx>$evendocurx; |
| 163 |
$evendocury=$ury if $ury>$evendocury; |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
$docllx=$llx if $llx<$docllx; |
| 168 |
$doclly=$lly if $lly<$doclly; |
| 169 |
$docurx=$urx if $urx>$docurx; |
| 170 |
$docury=$ury if $ury>$docury; |
| 171 |
|
| 172 |
if ($page >= $maxpages) { |
| 173 |
print "skipping rest of file...\n"; |
| 174 |
last; |
| 175 |
} |
| 176 |
|
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
###################################################################### |
| 181 |
# This is the old pbm routine that renders pages in pbm and then |
| 182 |
# parses the binary output. |
| 183 |
###################################################################### |
| 184 |
if ($do_through_pbm) { |
| 185 |
# divisible by 8. |
| 186 |
my $w8 = int($pw / 72 * $baseres /8 + 0.5) * 8; |
| 187 |
my $res = 72.0*$w8/$pw; |
| 188 |
|
| 189 |
# create an empty row for optimization |
| 190 |
my $emptyrow = "\0" x ($w8/8); |
| 191 |
|
| 192 |
my $gsopt = "-q -dNOPAUSE -sPAPERSIZE=$pagesize -sDEVICE=pbmraw -sOutputFile=-"; |
| 193 |
open(GS, "$GSPATH -r$res $gsopt -- $fn|"); |
| 194 |
|
| 195 |
my $page=0; |
| 196 |
my $bot; |
| 197 |
my ($lmcand, $rmcand); |
| 198 |
while(!eof(GS)) { |
| 199 |
chop($_=<GS>); |
| 200 |
die "Expected P4 but got '$_'. \nProbably a PostScript error...\n" unless /P4/; |
| 201 |
while(<GS>) { last unless /^\#/; } |
| 202 |
my ($w,$h)=split(" ", $_); |
| 203 |
unless ($w8 == $w) { |
| 204 |
warn "Warning! Expected bitmap width $w8 but got $w. Adjusting resolution...\n"; |
| 205 |
$res *= $w8 / $w; |
| 206 |
close(GS); |
| 207 |
open(GS, "$GSPATH -r$res $gsopt -- $fn|"); |
| 208 |
next; |
| 209 |
} |
| 210 |
$page++; |
| 211 |
printf " %4d ", $page; |
| 212 |
my $topmarg = 0; |
| 213 |
my $botmarg = 0; |
| 214 |
my $leftmarg = $w; |
| 215 |
my $rightmarg = $w; |
| 216 |
my $top=1; |
| 217 |
for my $i (1..$h) { |
| 218 |
read(GS, $_, $w/8 ); |
| 219 |
|
| 220 |
# Check for an empty row |
| 221 |
#if ($_ eq $emptyrow) |
| 222 |
if ($_=~ /^\00*$/) { |
| 223 |
if ($top) { # Still scanning top margin? |
| 224 |
$topmarg++; |
| 225 |
} |
| 226 |
else { |
| 227 |
$botmarg = 0 unless $bot; # reset the bottom margin |
| 228 |
$bot=1; |
| 229 |
$botmarg++; |
| 230 |
} |
| 231 |
} |
| 232 |
# Otherwise get the left and right margins of the row |
| 233 |
else { |
| 234 |
$bot=0; $top=0; # Not counting top and bottom margins anymore |
| 235 |
|
| 236 |
# Get left margin of line |
| 237 |
/^(\00*)([^\00])/; |
| 238 |
$lmcand=length($1)*8; |
| 239 |
if ($lmcand < $leftmarg) { |
| 240 |
($b=unpack('B*',$2))=~ /^0*/; |
| 241 |
$lmcand+= length($&); |
| 242 |
$leftmarg = $lmcand if $lmcand < $leftmarg; |
| 243 |
} |
| 244 |
|
| 245 |
# Get right margin of line |
| 246 |
/([^\00])(\00*)$/; |
| 247 |
$rmcand=length($2)*8; |
| 248 |
if ($rmcand < $rightmarg) { |
| 249 |
($b = unpack('B*', $1))=~ /0*$/; |
| 250 |
$rmcand+= length($&); |
| 251 |
$rightmarg = $rmcand if ($rmcand < $rightmarg); |
| 252 |
} |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
# scale and translate to postscript points |
| 257 |
my $scale = 72/$res; |
| 258 |
my $ury= ($h-$topmarg)*$scale; |
| 259 |
my $lly= $botmarg*$scale; |
| 260 |
my $llx= $leftmarg*$scale; |
| 261 |
my $urx= ($w-$rightmarg)*$scale; |
| 262 |
$lly= $h*$scale if $lly==$ury && $lly==0; |
| 263 |
printf "%5.f %5.f %5.f %5.f\n", $llx, $lly, $urx, $ury; |
| 264 |
|
| 265 |
if ($pad) { |
| 266 |
$llx-=$pad; |
| 267 |
$lly-=$pad; |
| 268 |
$urx+=$pad; |
| 269 |
$ury+=$pad; |
| 270 |
} |
| 271 |
|
| 272 |
if ($evenodd) { |
| 273 |
if ($page % 2) { # odd |
| 274 |
$odddocllx=$llx if $llx<$odddocllx; |
| 275 |
$odddoclly=$lly if $lly<$odddoclly; |
| 276 |
$odddocurx=$urx if $urx>$odddocurx; |
| 277 |
$odddocury=$ury if $ury>$odddocury; |
| 278 |
} |
| 279 |
else { |
| 280 |
$evendocllx=$llx if $llx<$evendocllx; |
| 281 |
$evendoclly=$lly if $lly<$evendoclly; |
| 282 |
$evendocurx=$urx if $urx>$evendocurx; |
| 283 |
$evendocury=$ury if $ury>$evendocury; |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
$docllx=$llx if $llx<$docllx; |
| 288 |
$doclly=$lly if $lly<$doclly; |
| 289 |
$docurx=$urx if $urx>$docurx; |
| 290 |
$docury=$ury if $ury>$docury; |
| 291 |
|
| 292 |
if ($page >= $maxpages) { |
| 293 |
print "skipping rest of file...\n"; |
| 294 |
last; |
| 295 |
} |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
print "Document: "; |
| 300 |
printf "%5.f %5.f %5.f %5.f\n", $docllx, $doclly, $docurx, $docury; |
| 301 |
if ($evenodd) { |
| 302 |
print "Odd: "; |
| 303 |
printf "%5.f %5.f %5.f %5.f\n", $odddocllx, $odddoclly, $odddocurx, $odddocury; |
| 304 |
print "Even: "; |
| 305 |
printf "%5.f %5.f %5.f %5.f\n", $evendocllx, $evendoclly, $evendocurx, $evendocury; |
| 306 |
} |
| 307 |
|
| 308 |
if ($insert) { |
| 309 |
rename($fn, "$fn~"); |
| 310 |
open(PSIN, "$fn~"); open(PSOUT, ">$fn"); |
| 311 |
print PSOUT "%!PS-Adobe-1.0 EPSF-1.0\n"; |
| 312 |
print PSOUT "%%BoundingBox: "; |
| 313 |
printf PSOUT "%5.f %5.f %5.f %5.f\n", $docllx, $doclly, $docurx, $docury; |
| 314 |
print PSOUT "%%Comment: Bounding box extracted by bboxx\n"; |
| 315 |
print PSOUT "%%+: A program by Dov Grobgeld 2003\n"; |
| 316 |
|
| 317 |
while(<PSIN>) { |
| 318 |
next if $.==1 && /^%!/; |
| 319 |
next if /^%%BoundingBox: /; |
| 320 |
print PSOUT; |
| 321 |
} |
| 322 |
} |