From afb0888a13da8a67e0c659130fe9c525105d96dc Mon Sep 17 00:00:00 2001 From: Guilhem Moulin Date: Sun, 8 May 2011 00:05:47 +0200 Subject: tumble --- pdftool.pl | 70 ++++++++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 52 insertions(+), 18 deletions(-) diff --git a/pdftool.pl b/pdftool.pl index 8d6f577..8320747 100755 --- a/pdftool.pl +++ b/pdftool.pl @@ -18,7 +18,7 @@ pdftool.pl - a PDF swiss army knife B [-s I] [-w I ] [-h I] [-p I] [-W I] [-H I] [-P I] [-p I] [-m I] -[-b I] [-c] [--book] [--column] [-n I] [-q] +[-b I] [-c] [--book] [--column] [-n I] [-t] [-q] [I [I]] =head1 DESCRIPTION @@ -139,6 +139,13 @@ B<-b>), see B<-m> for details. If I is less than 10, the option B<->I may be used as an alternative. +=item B<-t, --tumble> + +By default, B ensures that your pdf will be printable using the +DuplexNoTumble mode, hence even pages of landscape files are rotated by +180°. This option removes this behavior; also, landscape files are rotated +in order to make it easy to read on the computer. + =item B<--column> Changes the order to `column-major', where successive pages are placed in @@ -205,6 +212,8 @@ my $nup = 1; my $column; my $quiet; my $man; +my $tumble; +my $landscape; GetOptions( "select|s=s" => \$select, "w|width=s" => \$outwidth, @@ -218,6 +227,7 @@ GetOptions( "select|s=s" => \$select, "crop|c" => \$crop, "book" => \$book, "nup|n=i" => \$nup, + "t|tumble" => \$tumble, "1" => sub { $nup = 1 }, "2" => sub { $nup = 2 }, "3" => sub { $nup = 3 }, @@ -387,7 +397,6 @@ if ($filetype eq "PDF") { $r .= $rmin if defined $rmin; $r .= "-"; $r .= $rmax if defined $rmax; - print "$r\n"; push @newselect, $r; } @@ -541,19 +550,33 @@ if (defined $book) { # # PSNup # -($outheight, $outwidth) = ($outwidth, $outheight) - if (($bbox[2]-$bbox[0] > $bbox[3]-$bbox[1]) - xor ($outwidth-2*$margin > $outheight-2*$margin)); +if ((($bbox[2]-$bbox[0] > $bbox[3]-$bbox[1]) + and not ($outwidth-2*$margin > $outheight-2*$margin)) + or + (defined $tumble + and not ($bbox[2]-$bbox[0] > $bbox[3]-$bbox[1]) + and ($outwidth-2*$margin > $outheight-2*$margin))) { + ($outheight, $outwidth) = ($outwidth, $outheight); + $landscape = 1; +} my ($horiz, $vert, $rotate, $scale, $hshift, $vshift) = &calc_layout ($nup, $border, \@bbox, $outwidth-2*$margin, $outheight-2*$margin); -my @specs = &calc_specs ($horiz, $vert, $rotate, $scale, - [$outwidth-2*$margin, $outheight-2*$margin, - $hshift, $vshift]); +my ($ospecs,$especs) = &calc_specs ($horiz, $vert, $rotate, $scale, + [$outwidth-2*$margin, $outheight-2*$margin, + $hshift, $vshift]); +my $land = ($outwidth > $outheight); +$land = not $land if ($rotate%2); + +my $pagespecs; +if (defined $tumble || not $land) { + $pagespecs = "$nup:" . join ('+', @$ospecs); +} else { + $pagespecs = 2*$nup . ':' . join ('+', @$ospecs) . ',' . join ('+', @$especs); +} -my $pagespecs = "$nup:" . join ('+', @specs); @cmd = ('pstops', '-w', $bbox[2], '-h', $bbox[3], $pagespecs); push @cmd, '-q' if defined $quiet; my $pid = open3 "<&IN", *OUT, ">&LOG", @cmd; @@ -561,13 +584,20 @@ open *IN, "<&OUT" or die "Can't dup: $!"; push @pids, [$pid, @cmd]; - # # Final file: Convert back to PDF # +my $pagedevice; +if (defined $tumble || $land || $landscape) { + $rotate = ($rotate+1)%4 if not (defined $tumble) and $land; + $pagedevice = "/Orientation $rotate /PageSize [$outwidth $outheight]"; +} else { + $pagedevice = "/PageSize [$outwidth $outheight]"; +} + @cmd = ('gs', "-sDEVICE=pdfwrite", "-sOutputFile=%stdout%", "-dBATCH", "-dNOPAUSE", "-dAutoRotatePages=/None", - "-c", "<< /Orientation $rotate /PageSize [$outwidth $outheight] >> setpagedevice", + "-c", "<< $pagedevice >> setpagedevice", "-f", "-"); $pid = open3 "<&IN", ">&FOUT", ">&LOG", @cmd; push @pids, [$pid, @cmd]; @@ -662,11 +692,13 @@ sub calc_specs { my ($horiz, $vert, $rotate, $scale, $bbox) = @_; my ($outwidth, $outheight, $hshift, $vshift) = @$bbox; - my @specs; + my $n = $horiz * $vert; + + my (@ospecs, @especs); # specs for odd and even pages - for (my $pageno = 0; $pageno < $horiz*$vert; $pageno++) { + for (my $pageno = 0; $pageno < $n; $pageno++) { my ($up, $across); # pageno index - my $rot = ""; + my ($orot,$erot) = ('','U'); my ($xoff, $yoff); @@ -680,7 +712,7 @@ sub calc_specs { $across = floor($pageno / $vert); $up = $pageno % $vert; } - $rot = 'L'; + ($orot,$erot) = ('L','R'); $xoff = $margin + ($across+1)*$outwidth/$horiz - $hshift; } else { if (defined $column) { @@ -696,10 +728,12 @@ sub calc_specs { } $yoff = $margin + $up*$outheight/$vert + $vshift; - push @specs, sprintf ("%d%s@%.3f(%.3f,%.3f)", - $pageno, $rot, $scale, $xoff, $yoff); + push @ospecs, sprintf ("%d%s@%.3f(%.3f,%.3f)", + $pageno, $orot, $scale, $xoff, $yoff); + push @especs, sprintf ("%d%s@%.3f(%.3f,%.3f)", + $n + $pageno, $erot, $scale, $outwidth-$xoff, $outheight-$yoff); } - return @specs; + return (\@ospecs,\@especs); } -- cgit v1.2.3