summaryrefslogtreecommitdiffstats
path: root/pscrop.pl
blob: 37b361375b94861119f7831d0f317de673f6785b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/usr/bin/perl -w

=head1 SYNOPSIS

psnup2.pl - a better psnup

For details, see http://consodoc.com/psnup2/

=head1 USAGE

psnup2.pl -d -m <margin> -w <page_width> -h <page_height>
          -l <n_long_edge> -s <n_short_edge> -r <reverse> -D
          SOURCE [DEST]

-d  If specified, only prints out the command.
-m  Margin, in centimeters, that should be kept around the page. Default to 1.
-w  Resulting page width, in centimeters. Default to 21.
-h  Resulting page height, in centimeters. Default to 29.7.
-l  Number of pages to fit on the long edge. Default to 2.
-s  Number of pages to fit on the short edge. Default to 1.
-r  If specified, the order is reversed on the long edge.
-D  Rotation direction, if needed ("L" or "R"). Default to "L".

=head1 REQUIRE

Requires psutils installed and available in the command line
http://www.tardis.ed.ac.uk/~ajcd/psutils/

=head1 AUTHOR

Public domain, (c) Oleg Parashchenko, Lionel Guy

=head1 VERSION

Version: 0.0.5, 23 October 2008

=cut

use Getopt::Long; 
use IPC::Open2;
use IPC::Open3;
use POSIX qw(floor);
use warnings;
use strict;

# TODO: units
# TODO: "-m h1cm:v3cm": horizontal, vertical
my $opt_m    = '1'; # in centimeters

my $papersize = 'a4';

my ($opt_w, $opt_h) = (21.0, 29.7); # in centimeters, A4

my $units_per_cm = 72 * .3937; # 1 centimeters = .393700787 inches, 1 inch = 72 PostScript units5

die "Too many arguments" if $#ARGV > 1;

my ($infile, $outfile) = @ARGV;


if (defined $infile && $infile ne "-") {
    open *FIN, '<', "$infile"  or  die "Can't read `$infile': $!";

} else {
    *FIN = *STDIN;
}

if (defined $outfile && $outfile ne "-") {
    open *FOUT, '>', "$outfile"  or  die "Can't create `$outfile': $!";
} else {
    *FOUT = *STDOUT;
}



#
# Calculate the maximal bounding box
#

my $pid;

# need to duplicate FIN, since it will be closed in the parent process
open *KIDFIN, "<&FIN"  or  die "Can't dup FIN: $!";

$pid = open3 "<&KIDFIN", ">&FINGS", *FINGS, 
             'gs', '-sDEVICE=bbox', '-dBATCH', '-dNOPAUSE', '-'
           or die "Can't run: `gs -sDEVICE=bbox -dBATCH -dNOPAUSE -'";
my $n = 0;
my ($x0, $y0, $x1, $y1) = (1<<16, 1<<16, -(1<<16), -(1<<16));
while (<FINGS>) {
    if ($_ =~ m/^\%\%BoundingBox: (\d+) (\d+) (\d+) (\d+)/) {
	    $x0 = $1 if $1 < $x0;
	    $y0 = $2 if $2 < $y0;
	    $x1 = $3 if $3 > $x1;
	    $y1 = $4 if $4 > $y1;
        print STDERR "[", ++$n, "] ";
    }
}
close FINGS;
print STDERR "\n";

# No zombie processes
waitpid $pid, 0;

die "Error when calculating bounding box" if ($x0 >= $x1 || $y0 >= $y1);
my @bbox = ($x0, $y0, $x1, $y1);


# Let's go back to the beginning of the input
seek FIN, 0, 0  or  die "$!";



#
# Calculate pstops specification
#
# (rounded) width, height, and margin
my ($w, $h, $margin) = map { &round ($_ * $units_per_cm) }
                           ($opt_w, $opt_h, $opt_m);

($x0,$x1) = &calculate_coordinates($w, $margin);
($y0,$y1) = &calculate_coordinates($h, $margin);

my $spec = 0 . &calc_pstops_page(@bbox, $x0, $y0, $x1, $y1);



#
# Run the program and filter the output
#

$pid = open2 *FINPS2PS, "<&FIN", 'pstops', "-w$w", "-h$h", "$spec"
           or die "Can't run `pstops -w$w -h$h $spec': $!\n";
my $l;
# Header and comments
while (defined ($l = <FINPS2PS>)  &&  $l ne "\%\%EndComments\n") {
    # 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: $!";
	    next;
    }
    print FOUT $l  or  die "Can't print: $!";
}

# Important to print the document right
# TODO: die "Can't print: $!"
print FOUT  <<  "EOF";
\%\%EndComments
\%\%BeginFeature: *PageSize ($w $h)
<< /PageSize [$w $h] >> setpagedevice
\%\%EndFeature
EOF


# Body
while (<FINPS2PS>) {
    print FOUT $_  or  die "Can't print: $!";

    # PStoPSclip hack: increase clipping box by 10
    if ($_ =~ m/^userdict\/PStoPSclip{0 0 moveto$/) {
        $l = <FINPS2PS>;
	    $l =~ s/\./0./g;
	    print FOUT $l  or  die "Can't print: $!";
    }
}

close FIN;
close FOUT;

# No zombie processes
waitpid $pid, 0;



# =========================================================

#
# Calculate an item of the pstops specification
#
sub calc_pstops_page {
    my ($fx0, $fy0, $fx1, $fy1,
        $tx0, $ty0, $tx1, $ty1) = @_;

    # From and to width / height
    my ($wf, $hf) = ($fx1 - $fx0,  $fy1 - $fy0);
    my ($wt, $ht) = ($tx1 - $tx0,  $ty1 - $ty0);

    # Scale factor width / height
    my ($sw, $sh) = ($wt / $wf,  $ht / $hf);

    # We take the smallest scale
    my $scale = ($sw > $sh) ? $sh : $sw;
    
    # Calculate the centers of the boxes
    my ($cxf, $cyf) = ( .5 * ($fx0 + $fx1),  .5 * ($fy0 + $fy1) );
    my ($cxt, $cyt) = ( .5 * ($tx0 + $tx1),  .5 * ($ty0 + $ty1) );

    # Fist, pstops scales, then moves

    my ($movex, $movey) = ($cxt - $cxf * $scale,  $cyt - $cyf * $scale);

    # Generate the summary
    return sprintf( '@%.3f(%.3f,%.3f)', $scale, $movex, $movey);
}

# =========================================================

# 
# Calculate coordinates of splitting the dimension on K chunks
# Returns an array, each item is a reference to an array of two
#   elements: the begin and end coordinates
#
sub calculate_coordinates {
    my ($length, $margin) = @_;
    my $skip   = $length - $margin;
    my $width  = $skip - $margin;
    my @coords = ( &round( &round($skip) - $width ), &round($skip) );
    return @coords;
}

# Round a float number
sub round {
    return floor ($_[0] + .5);
}