From 6612d5beec5300d2c9ea807dfb156e8722ca0df2 Mon Sep 17 00:00:00 2001 From: Guilhem Moulin Date: Sun, 15 May 2011 04:06:05 +0200 Subject: fixed a bug with page selection; remove the (ignored) _ caracter --- pdftool.pl | 57 +++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 35 insertions(+), 22 deletions(-) diff --git a/pdftool.pl b/pdftool.pl index 11083d4..ed6b887 100755 --- a/pdftool.pl +++ b/pdftool.pl @@ -58,7 +58,7 @@ The document will be treated as follows: =item * Convert from PDF to PostScript (if necessary, and only for smallest interval that contains all the selected pages), -=item * Select of the page range, +=item * Select the page range, =item * Calculate the minimal bounding box @@ -82,10 +82,6 @@ may be a page number, or a page range of the form I-I. If I is omitted, the first page is assumed, and if I is omitted, the last page is assumed. -The prefix character `_' indicates that the page number is relative -to the end of the document, counting backwards. If just this -character with no page number is used, a blank page will be inserted. - =item B<-w, --width=>I Specify the width of the output file. If the height is not specified as @@ -198,7 +194,7 @@ C<< ssh remote pdftool.pl -cpA4 --book -2 -b2cm -m-1cm < in.pdf | \ =head1 REQUIREMENTS -Requires PSUtils installed and available in the command line +Requires PSUtils installed and available via the command line http://www.tardis.ed.ac.uk/~ajcd/psutils/. =head1 AUTHOR @@ -287,7 +283,24 @@ map {&topoints ($_)} ( \$outwidth, \$outheight, die "Margins are too big" if $outwidth <= $margin*2 or $outheight <= $margin*2; +# +# Check options +# +die "Bad page range: `$select'" if + defined $select && not $select =~ /^(\s*\d*-?\d*\s*,)*\s*\d*-?\d*\s*$/; +for (split /\s*,\s*/, $select) { + $_ =~ /^(\d*)\s*-?\s*(\d*)$/; + die "Bad page range: `$select'" if + ($1 ne "" && $1 <= 0) or ($2 ne "" && $2 <=0); +} + +die "Bad nup: `$nup'" if + defined $nup && not ($nup =~ /^\d+$/ && $nup > 0); + + +# # Open input and output files +# my $infile_display; if (defined $infile && $infile ne "-") { open FIN, '<', "$infile" or die "Can't read `$infile': $!"; @@ -385,15 +398,16 @@ if ($filetype eq "PDF") { if (defined $select) { # Convert to PS only the pages we are interested in ($first, $last) = (1<<16,-(1<<16)); - for (split / *, */, $select) { - my ($rmin, $rmax) = split (/ *- */, $_); - undef $first if defined $rmin && not $rmin; - undef $last if defined $rmax && not $rmax; - if (defined $rmin && $rmin) { + for (split /\s*,\s*/, $select) { + $_ =~ /^(\d*)\s*(-?)\s*(\d*)$/; + my ($rmin,$sep,$rmax) = ($1,$2,$3); + undef $first if $sep && not $rmin; + undef $last if $sep && not $rmax; + if ($rmin) { $first = $rmin if defined $first && $rmin < $first; $last = $rmin if defined $last && $rmin > $last; } - if (defined $rmax && $rmax) { + if ($rmax) { $first = $rmax if defined $first && $rmax < $first; $last = $rmax if defined $last && $rmax > $last; } @@ -401,15 +415,14 @@ if ($filetype eq "PDF") { # Calculate the new page range my @newselect; - for (split / *, */, $select) { - my ($rmin, $rmax) = split / *- */, $_; - $rmin -= $first-1 if defined $first && defined $rmin && $rmin; - $rmax -= $first-1 if defined $first && defined $rmax && $rmax; - my $r = ""; - $r .= $rmin if defined $rmin; - $r .= "-"; - $r .= $rmax if defined $rmax; - push @newselect, $r; + for (split /\s*,\s*/, $select) { + $_ =~ /^(\d*)\s*(-?)\s*(\d*)$/; + my ($rmin,$sep,$rmax) = ($1,$2,$3); + if (defined $first) { + $rmin -= $first-1 if $rmin; + $rmax -= $first-1 if $rmax; + } + push @newselect, "$rmin$sep$rmax"; } $select = join ',', @newselect; @@ -435,7 +448,7 @@ open *IN, "<&PSIN" or die "Can't dup: $!"; # Select, if necessary # if (defined $select) { - @cmd = ('psselect', "-p$select"); + @cmd = ('psselect', $select); push @cmd, '-q' if defined $quiet; my $pid = open3 "<&IN", *OUT, ">&LOG", @cmd; push @pids, [$pid, @cmd]; -- cgit v1.2.3