eval '(exit $?0)' && eval 'exec perl -S $0 ${1+"$@"}' && eval 'exec perl -S $0 $argv:q'
if 0;
use Config ;
use Getopt::Long ;
use strict ;
Getopt::Long::Configure
("auto_abbrev",
"ignore_case",
"pass_through") ;
my $Crop = "0cm" ;
my $LeftCrop = "0cm" ;
my $RightCrop = "0cm" ;
my $TopCrop = "0cm" ;
my $BottomCrop = "0cm" ;
my $Offset = "0cm" ;
my $GSbin = "" ;
my $Verbose = 0 ;
my $Help = 0 ;
my $UsePlain = 0 ;
my $Page = 1 ;
my $pdfps = "pdftops" ;
my $gs = "gs" ;
my $thisisunix = $Config{'osname'} !~ /dos|mswin/i ;
my $figurefile = "" ;
my $resultfile = "" ;
my $tempfile = "" ;
my $programname = "pdftrimwhite" ;
my $results = "" ;
my $pipe = "" ;
my $result = "" ;
my $pwidth = 597 ;
my $pheight = 847 ;
my $hoffset = 0 ;
my $voffset = 0 ;
my $width = my $height = my $llx = my $lly = my $urx = my $ury = 0 ;
&GetOptions
( "leftcrop=s" => \$LeftCrop ,
"rightcrop=s" => \$RightCrop ,
"topcrop=s" => \$TopCrop ,
"bottomcrop=s" => \$BottomCrop,
"crop=s" => \$Crop ,
"offset=s" => \$Offset ,
"verbose" => \$Verbose ,
"gsbin=s" => \$GSbin ,
"plain" => \$UsePlain ,
"page=i" => \$Page ,
"help" => \$Help ) ;
sub PrintHelp
{ print "This is PdfTrimWhite\n\n" .
"usage:\n\n" .
"pdftrimwhite [switches] filename result\n\n" .
"switches:\n\n" .
"--crop=<dimen>\n" .
"--offset=<dimen>\n" .
"--leftcrop=<dimen>\n" .
"--rightcrop=<dimen>\n" .
"--topcrop=<dimen>\n" .
"--bottomcrop=<dimen>\n" .
"--gsbin=<string>\n" .
"--page=<number>\n" .
"--plain\n" .
"--verbose\n" }
sub GetItRight
{ if ($Help)
{ PrintHelp() ; exit }
$figurefile = $ARGV[0] ; $figurefile =~ s/\.pdf$//oi ;
$resultfile = $ARGV[1] ; $resultfile =~ s/\.pdf$//oi ;
$tempfile = "pdftrimwhite-$resultfile" ;
if ($figurefile eq '')
{ PrintHelp() ; exit }
unless ($thisisunix)
{ $gs = "gswin32c" }
if ($GSbin ne '')
{ $gs = $GSbin }
unless (-e "$figurefile.pdf")
{ print "Something is terribly wrong: no file found\n" ;
exit }
if (($resultfile eq '')||($resultfile=~/(^\-|\.)/io))
{ $resultfile = $programname }
$pipe = "2>&1" ;
if ($thisisunix)
{ $pipe = "2>&1" } }
sub SavePageData
{ return "% saving page data
\\immediate\\openout\\scratchwrite=$figurefile.tmp
\\immediate\\write\\scratchwrite
{\\HOffsetBP\\space\\VOffsetBP\\space
\\FigureWidthBP\\space\\FigureHeightBP}
\\immediate\\closeout\\scratchwrite\n" }
sub MakePageConTeXt
{ return "% the real work
\\definepapersize
[Crap]
[width=\\FigureWidth,
height=\\FigureHeight]
\\setuppapersize
[Crap][Crap]
\\setuplayout
[topspace=0cm,backspace=0pt,
height=middle,width=middle,
header=0pt,footer=0pt]
\\starttext
\\startstandardmakeup
\\clip
[voffset=\\VOffset,
hoffset=\\HOffset,
width=\\FigureWidth,
height=\\FigureHeight]
{\\externalfigure[$figurefile.pdf][page=$Page]\\hss}
\\stopstandardmakeup
\\stoptext\n" }
sub MakePagePlainTeX
{ return "% the real work
\\output{}
\\hoffset=-1in
\\voffset=\\hoffset
\\pdfpageheight=\\FigureHeight
\\pdfpagewidth=\\FigureWidth
\\vbox to \\pdfpageheight
{\\offinterlineskip
\\vskip-\\VOffset
\\hbox to \\pdfpagewidth{\\hskip-\\HOffset\\box0\\hss}
\\vss}
\\end\n" }
sub CalculateClip
{ return "% some calculations
\\dimen0=\\figurewidth
\\dimen2=\\figureheight
\\dimen4=$Crop
\\dimen6=$Crop
\\advance\\dimen4 by $LeftCrop
\\advance\\dimen6 by $TopCrop
\\advance\\dimen0 by -\\dimen4
\\advance\\dimen0 by -$Crop
\\advance\\dimen0 by -$RightCrop
\\advance\\dimen2 by -\\dimen6
\\advance\\dimen2 by -$Crop
\\advance\\dimen2 by -$BottomCrop
\\edef\\FigureWidth {\\the\\dimen0}
\\edef\\FigureHeight{\\the\\dimen2}
\\edef\\HOffset {\\the\\dimen4}
\\edef\\VOffset {\\the\\dimen6}
\\ScaledPointsToWholeBigPoints{\\number\\dimen0}\\FigureWidthBP
\\ScaledPointsToWholeBigPoints{\\number\\dimen2}\\FigureHeightBP
\\ScaledPointsToWholeBigPoints{\\number\\dimen4}\\HOffsetBP
\\ScaledPointsToWholeBigPoints{\\number\\dimen6}\\VOffsetBP\n" }
sub RecalculateClip
{ return "% some calculations
\\dimen0=${width}bp
\\dimen2=${height}bp
\\dimen4=${hoffset}bp
\\dimen6=${pheight}bp
\\advance\\dimen0 by $Offset
\\advance\\dimen0 by $Offset
\\advance\\dimen2 by $Offset
\\advance\\dimen2 by $Offset
\\advance\\dimen4 by ${llx}bp
\\advance\\dimen4 by -$Offset
\\advance\\dimen6 by -${lly}bp
\\advance\\dimen6 by $Offset
\\advance\\dimen6 by -\\dimen2
\\advance\\dimen6 by $TopCrop
\\edef\\FigureWidth {\\the\\dimen0}
\\edef\\FigureHeight{\\the\\dimen2}
\\edef\\HOffset {\\the\\dimen4}
\\edef\\VOffset {\\the\\dimen6}\n" }
sub PrepareConTeXt
{ return "% interface=en
\\setupoutput[pdftex]
\\getfiguredimensions[$figurefile.pdf][page=$Page]\n" }
sub PreparePlainTeX
{ return "% plain tex alternative, needs recent supp-mis
\\input supp-mis
\\pdfoutput=1
\\newdimen\\figurewidth
\\newdimen\\figureheight
\\setbox0=\\hbox
{\\immediate\\pdfximage page $Page {$figurefile.pdf}\\pdfrefximage\\pdflastximage}
\\figurewidth=\\wd0
\\figureheight=\\ht0\n" }
sub PrepareFirstPass
{ open (TEX, ">$tempfile.tex") ;
if ($UsePlain)
{ print TEX
PreparePlainTeX .
CalculateClip .
SavePageData .
MakePagePlainTeX }
else
{ print TEX
PrepareConTeXt .
CalculateClip .
SavePageData .
MakePageConTeXt }
close TEX }
sub SetupConTeXt
{ return "% interface=en
\\setupoutput[pdftex]\n" }
sub SetupPlainTeX
{ return "% plain tex alternative
\\pdfoutput=1
\\setbox0=\\hbox
{\\immediate\\pdfximage page $Page {$figurefile.pdf}\\pdfrefximage\\pdflastximage}\n" }
sub PrepareSecondPass
{ open (TEX, ">$tempfile.tex") ;
if ($UsePlain)
{ print TEX
SetupPlainTeX .
RecalculateClip .
MakePagePlainTeX }
else
{ print TEX
SetupConTeXt .
RecalculateClip .
MakePageConTeXt }
close TEX }
sub FetchPaperSize
{ open (TMP,"$figurefile.tmp") ;
while (<TMP>)
{ chomp ;
if (/^(\d+) (\d+) (\d+) (\d+) *$/oi)
{ $hoffset = $1 ;
$voffset = $2 ;
$pwidth = $3 ;
$pheight = $4 ;
last } }
close (TMP) }
sub RunTeX
{ if ($UsePlain)
{ $result = `pdftex -prog=pdftex -fmt=plain -int=batchmode $tempfile` }
else
{ $result = `texexec --batch --once --purge $tempfile` }
print $result if $Verbose ; $results .= "$result\n" }
sub FindBoundingBox
{ $result = `$gs -sDEVICE=bbox -dNOPAUSE -dBATCH $tempfile.pdf $pipe` ;
print $result if $Verbose ; $results .= "$result\n" }
sub IdentifyCropBox
{ RunTeX() ;
FetchPaperSize () ;
FindBoundingBox() }
my $digits = '([\-\d\.]+)' ;
sub ValidatedCropBox
{ if ($result =~ /BoundingBox:\s*$digits\s+$digits\s+$digits\s+$digits\s*/mois)
{ $llx = $1 ; $lly = $2 ; $urx = $3 ; $ury = $4 }
else
{ print "Something is terribly wrong: no boundingbox:\n$result\n" ; exit }
$width = abs($urx - $llx) ;
$height = abs($ury - $lly) ;
if ($width&&$height)
{ return 1 }
else
{ unless ($width)
{ print "Something seems wrong: no width\n" ;
$LeftCrop = "0cm" ; $RightCrop = "0cm" ; $Crop = "0cm" }
unless ($height)
{ print "Something seems wrong: no height\n" ;
$TopCrop = "0cm" ; $BottomCrop = "0cm" ; $Crop = "0cm" }
return 0 } }
sub FixCropBox
{ RunTeX() }
sub RenameResult
{ unlink "$resultfile.pdf" ;
rename "$tempfile.pdf", "$resultfile.pdf" }
sub SaveLogInfo
{ open (LOG, ">$resultfile.log") ;
print LOG $results ;
close (LOG) }
sub CleanUp
{ unless ($Verbose)
{ unlink "$tempfile.tex" ;
unlink "$tempfile.tuo" ;
unlink "$tempfile.tui" ;
unlink "$figurefile.tmp" } }
GetItRight() ;
PrepareFirstPass() ;
IdentifyCropBox () ;
unless (ValidatedCropBox())
{ PrepareFirstPass() ;
IdentifyCropBox () }
if (ValidatedCropBox())
{ PrepareSecondPass() ;
FixCropBox() }
RenameResult() ;
SaveLogInfo() ;
CleanUp () ;