From 82960edaf52ac576ed1956cad82749ba3aad8681 Mon Sep 17 00:00:00 2001 From: Guilhem Moulin Date: Thu, 23 Sep 2010 18:51:43 +0200 Subject: pipable --- pscrop.pl | 92 ++++++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 53 insertions(+), 39 deletions(-) diff --git a/pscrop.pl b/pscrop.pl index 6b61df4..796bdbb 100755 --- a/pscrop.pl +++ b/pscrop.pl @@ -36,12 +36,16 @@ Version: 0.0.5, 23 October 2008 =cut +use Getopt::Long; +use IPC::Open2; +use IPC::Open3; +use warnings; use strict; -use Getopt::Std; -# -# Set default options -# +my $margin = '1'; +my $papersize = 'a4'; + + our ($opt_d, $opt_r); our $opt_m = 1; # in centimeters our ($opt_w, $opt_h) = (21.0, 29.7); # in centimeters, A4 @@ -51,30 +55,36 @@ our $opt_D = 'L'; # 'L' or 'R'. How to rotate, if required. my $units_per_cm = 72 * .3937; # 1 centimeters = .393700787 inches, 1 inch = 72 PostScript units5 -# -# Parse command line -# -getopts("dm:w:h:l:s:r"); - -my ($in_file, $out_file) = @ARGV; -die "No input file" unless $in_file; - -# # Calculate the maximal bounding box # -my ($x1, $y1, $x2, $y2) = (32000, 32000, -32000, -32000); -my $cmdline = "gs -sDEVICE=bbox -dBATCH -dNOPAUSE $in_file 2>&1 |"; -(open FIN, $cmdline) or die "Can't run: '$cmdline'"; -while (defined (my $l = )) { - #print $l; - if ($l =~ m/^\%\%BoundingBox: (\d+) (\d+) (\d+) (\d+)/) { - $x1 = $1 if $1 < $x1; - $y1 = $2 if $2 < $y1; - $x2 = $3 if $3 > $x2; - $y2 = $4 if $4 > $y2; +my $pid; +my ($x1, $y1, $x2, $y2) = (2**16, 2**16, -2**16, -2**16); +my $cmdline = "gs -sDEVICE=bbox -dBATCH -dNOPAUSE - 2>&1"; + + +open *KIDIN, "<&STDIN"; + +#open *FIN, '-|', 'gs', '-sDEVICE=bbox', '-dBATCH', '-dNOPAUSE', '-' +#open *FIN, '-|', 'gs -sDEVICE=bbox -dBATCH -dNOPAUSE - 2>&1' +open3 "<&KIDIN", ">&FIN", *FIN, + 'gs', '-sDEVICE=bbox', '-dBATCH', '-dNOPAUSE', '-' + or die "Can't run: '$cmdline'"; +my $n = 0; +while () { + if ($_ =~ m/^\%\%BoundingBox: (\d+) (\d+) (\d+) (\d+)/) { + $x1 = $1 if $1 < $x1; + $y1 = $2 if $2 < $y1; + $x2 = $3 if $3 > $x2; + $y2 = $4 if $4 > $y2; + print STDERR "[", ++$n, "] "; } } close FIN; + +print STDERR "\n"; + +seek STDIN, 0, 0; + die "Error calculating bounding box" if (($x1 >= $x2) || ($y1 >= $y2)); #print "Bounding box: ($x1,$y1), ($x2,$y2)\n"; my @bbox = ($x1, $y1, $x2, $y2); @@ -82,7 +92,8 @@ my @bbox = ($x1, $y1, $x2, $y2); # # Calculate pstops specification # -my @views = &calculate_views_short_x_long($opt_s, $opt_l, $opt_r); +#my @views = &calculate_views_short_x_long($opt_s, $opt_l, $opt_r); +my @views = &calculate_views_short_x_long(1, 1, 3); my $spec = scalar(@views); for (my $i = 0; $i < scalar(@views); $i++) { @@ -97,44 +108,47 @@ my $ps_size_spec = "\%\%BeginFeature: *PageSize ($w $h)\n<< /PageSize [$w $h] >> # # Dry run? Only print the specification # -$cmdline = "pstops -w$w -h$h '$spec' $in_file"; -if ($opt_d) { - print "$cmdline $out_file\nAnd add after the first '\%\%EndComments':\n$ps_size_spec"; - exit(0); -} +#$cmdline = "pstops -w$w -h$h '$spec' $in_file"; +#if ($opt_d) { +# print "$cmdline $out_file\nAnd add after the first '\%\%EndComments':\n$ps_size_spec"; +# exit(0); +#} # # Run the program and filter the output # + my $PStoPSclip_hack = 1; -$out_file = '-' unless $out_file; -(open FOUT, ">$out_file") or die "Can't create '$out_file': $!\n"; -(open FIN, "$cmdline|") or die "Can't run '$cmdline': $!\n"; +#$out_file = '-' unless $out_file; +#(open FOUT, ">$out_file") or die "Can't create '$out_file': $!\n"; +open2 *FIN, "<&STDIN", 'pstops', "-w$w", "-h$h", "$spec" + or die "Can't run '$cmdline': $!\n"; +#(open FIN, "$cmdline|") or die "Can't run '$cmdline': $!\n"; while (my $l = ) { # Optional, but nice: tune how "gv" will show the document next if $l =~ m/^\%\%DocumentMedia:/; if ($l =~ m/^\%\%BoundingBox:/) { - (print FOUT "\%\%BoundingBox: 0 0 $w $h\n") or die "Can't print: $!"; + (print "\%\%BoundingBox: 0 0 $w $h\n") or die "Can't print: $!"; next; } - (print FOUT $l) or die "Can't print: $!"; + (print $l) or die "Can't print: $!"; chomp $l; # Important to print the document right if ('%%EndComments' eq $l) { - (print FOUT $ps_size_spec) or die "Can't print: $!"; + (print $ps_size_spec) or die "Can't print: $!"; last; } } while (my $l = ) { - (print FOUT $l) or die "Can't print: $!"; + (print $l) or die "Can't print: $!"; if ($PStoPSclip_hack && ($l =~ m/^userdict\/PStoPSclip{0 0 moveto$/)) { $l = ; $l =~ s/\./0./g; # Increase clipping box by 10 - (print FOUT $l) or die "Can't print: $!"; + (print $l) or die "Can't print: $!"; } } -(close FIN) or die "Can't close '$cmdline': $!"; -(close FOUT) or die "Can't close '$out_file': $!"; +#(close FIN) or die "Can't close '$cmdline': $!"; +#(close FOUT) or die "Can't close '$out_file': $!"; # ========================================================= -- cgit v1.2.3