Subject: | pdfstamp patch for use strict |
Can you explain what sub proc_pages does?
--
Alexandr Ciornii, http://chorny.net
Subject: | pdfstamp.patch |
--- pdfstamp.dist Fri Mar 17 11:39:18 2006
+++ pdfstamp Wed Jan 9 21:20:39 2008
@@ -1,14 +1,18 @@
#!/usr/bin/perl
+use 5.006;
+use strict;
+use warnings;
+
use Text::PDF::File;
use Text::PDF::SFont;
use Text::PDF::Utils;
use Getopt::Std;
-
+our ($opt_t,$opt_f,$opt_s,$opt_l);
getopts('f:l:s:t:');
-$VERSION = 0.02; # MJPH 23-JUL-2001 Re-order to stamp on the top
+our $VERSION = 0.02; # MJPH 23-JUL-2001 Re-order to stamp on the top
unless (defined $ARGV[1] && -f $ARGV[0])
{
@@ -31,41 +35,42 @@
$opt_l =~ s/,\s*/ /o;
$opt_l = "0 0" unless $opt_l;
-$pdf = Text::PDF::File->open($ARGV[0], 1);
-$root = $pdf->{'Root'}->realise;
-$pgs = $root->{'Pages'}->realise;
+my $pdf = Text::PDF::File->open($ARGV[0], 1);
+my $root = $pdf->{'Root'}->realise;
+my $pgs = $root->{'Pages'}->realise;
-$fpgins = PDFDict(); $pdf->new_obj($fpgins);
-$spgins = PDFDict(); $pdf->new_obj($spgins);
+my $fpgins = PDFDict(); $pdf->new_obj($fpgins); #PDFDict generates new Text::PDF::Dict
+my $spgins = PDFDict(); $pdf->new_obj($spgins);
$fpgins->{' stream'} = "q";
$spgins->{' stream'} = "Q";
-@pglist = proc_pages($pdf, $pgs);
+my @pglist = proc_pages($pdf, $pgs);
-$max = 0;
-foreach $p (@pglist)
+my $max = 0;
+foreach my $p (@pglist)
{
- $dict = $p->find_prop('Resources');
+ my $dict = $p->find_prop('Resources');
if (defined $dict && defined $dict->{'Font'})
{
- foreach $k (keys %{$dict->{'Font'}})
+ foreach my $k (keys %{$dict->{'Font'}})
{
- next unless $k =~ m/^ap([0-9]+)/o;
- $val = $1;
+ next unless $k =~ m/^ap([0-9]+)/;
+ my $val = $1;
$max = $val if $val > $max;
}
}
}
$max++;
+my $font;
if ($opt_t)
{ $font = Text::PDF::TTFont->new($pdf, $opt_t, "ap$max", -subset => 1) || die "Can't work with font $opt_t"; }
else
{ $font = Text::PDF::SFont->new($pdf, $opt_f, "ap$max") || die "Can't create font $opt_f"; }
-$stream = PDFDict();
+my $stream = PDFDict();
$stream->{' stream'} = "BT 1 0 0 1 $opt_l Tm /ap$max $opt_s Tf " . $font->out_text($ARGV[1]) . " Tj ET";
$pdf->new_obj($stream);
-foreach $p (@pglist)
+foreach my $p (@pglist)
{
$p->add_font($font, $pdf);
$p->{Contents} = PDFArray($fpgins, $p->{Contents}->elementsof, $spgins, $stream);
@@ -77,11 +82,11 @@
sub proc_pages
{
my ($pdf, $pgs) = @_;
- my ($pg, $pgref, @pglist);
-
- foreach $pgref ($pgs->{'Kids'}->elementsof)
+ my @pglist;
+ my $pcount;
+ foreach my $pgref ($pgs->{'Kids'}->elementsof)
{
- $pg = $pdf->read_obj($pgref);
+ my $pg = $pdf->read_obj($pgref);
if ($pg->{'Type'}->val =~ m/^Pages$/oi)
{ push(@pglist, proc_pages($pdf, $pg)); }
else
@@ -90,6 +95,6 @@
push (@pglist, $pgref);
}
}
- (@pglist);
+ return (@pglist);
}