+sub computeboundings() {
+ $bb{Entire} = [ 0,0, $a4_h, $a4_w + $a3_h ];
+ my $submargins = sub {
+ my ($margin, $pr) = @_;
+ my @p = @$pr;
+ [ $p[0] + $margin,
+ $p[1] + $margin,
+ $p[2] - $margin,
+ $p[3] - $margin ];
+ };
+ my $somepage = sub {
+ my $id = shift @_;
+ print STDERR "defining page Page$id\n";
+ $bb{"Page$id"} = [ @_ ];
+ $bb{"Printable$id"} = $submargins->($max_printeredge, $bb{"Page$id"});
+ };
+ my $pageb_a4 = sub {
+ $somepage->('B', 0,0, $a4_h, $a4_w );
+ };
+ if ($maxpaper eq 'a3') {
+ $pageb_a4->();
+ my $mt_offset = $bb{PrintableB}[3] - $max_printeredge;
+ $somepage->('MT', 0, $mt_offset, $a4_h, $mt_offset + $a3_h );
+ $bb{PrintableAll} = [
+ @{ $bb{PrintableB} }[0..1],
+ @{ $bb{PrintableMT} }[2..3],
+ ];
+ } elsif ($maxpaper eq 'a4') {
+ $pageb_a4->();
+ my $m_offset = $bb{PrintableB}[3] - $max_printeredge;
+ $somepage->('M', 0, $m_offset, $a4_h, $m_offset + $a4_w );
+ my $t_offset = $bb{PrintableM}[3] - $max_printeredge;
+ $somepage->('T', 0, $t_offset, $a4_h, $t_offset + $a4_w );
+ $bb{PrintableAll} = [
+ @{ $bb{PrintableB} }[0..1],
+ @{ $bb{PrintableT} }[2..3],
+ ];
+ } elsif ($maxpaper =~ m/^a1/) {
+ my $offx = ($a1_w - $bb{Entire}[2] * (1 + $a1sfactor)) / 3;
+ my $offy = 0.5*($a1_h - $bb{Entire}[3]);
+ $somepage->('P', -$offx,-$offy, $a1_w-$offx, $a1_h-$offy);
+ my $hairs = 30 * MM2PT;
+ my $hairsw = $bb{Entire}[2];
+ my $hairsh = $bb{Entire}[3];
+ my $surround = 5 * MM2PT;
+ my $surroundw = $bb{Entire}[2] + $surround*2;
+ my $surroundh = $bb{Entire}[3] + $surround*2;
+ my @hrect = @{ $bb{Entire} };
+ if ($maxpaper eq 'a1m') {
+ @hrect = @{ $submargins->($a1m_pasteedge, \@hrect) };
+ $bb{Cutout} = \@hrect;
+ }
+ my $w_stroke = $xopts =~ m/W/ ?
+ ' gsave 3 setlinewidth stroke grestore '
+ : '';
+ $preamble_from_boundings = <<END;
+ 1 setlinewidth
+ 0 setgray
+ /a1hairs {
+ newpath moveto
+ $hairs 0 rmoveto
+ $hairs -2 mul 0 rlineto
+ $hairs $hairs rmoveto
+ 0 $hairs -2 mul rlineto
+ stroke
+ } def
+ $hrect[0] $hrect[1] a1hairs
+ $hrect[0] $hrect[3] a1hairs
+ $hrect[2] $hrect[3] a1hairs
+ $hrect[2] $hrect[1] a1hairs
+ newpath
+ -$surround -$surround moveto
+ $surroundw 0 rlineto
+ 0 $surroundh rlineto
+ -$surroundw 0 rlineto
+ closepath $w_stroke clip
+END
+ $adjuncts_dy_from_boundings{Top} = $c{PA}{BoundingsA1dy}[0];
+ $adjuncts_dy_from_boundings{Bottom} = $c{PA}{BoundingsA1dy}[1];
+ # ^ if we are doing it all in one go we lose less printable area
+ } else {
+ confess;
+ }
+}
+
+sub showboundings () {
+ return unless $xopts =~ m/B/;
+ o(" gsave\n");
+ my $bb = $bb{$bounding};
+ o(" $bb->[0] neg $bb->[1] neg translate\n");
+ my $i = 0;
+ my $on = 2;
+ my $off = 9;
+ foreach my $bname (sort keys %bb) {
+ o(sprintf " 1 %d %d setrgbcolor\n",
+ !!($bname =~ m/^Page/),
+ !!($bname =~ m/B$/),
+ );
+ o(" % $bname\n");
+ o(" [ $on $off ] ".($i * ($on+$off))," setdash newpath\n");
+ my @p = @{ $bb{$bname} };
+ o(" $p[0] $p[1] moveto\n");
+ o(" $p[2] $p[1] lineto\n");
+ o(" $p[2] $p[3] lineto\n");
+ o(" $p[0] $p[3] lineto\n");
+ o(" closepath stroke\n");
+ $i++;
+ }
+ o(" grestore");
+}
+
+sub o_amble (@) {
+ # CPerl-mode does a really awful thing with %s in the preamble
+ # and postamble, causing constant useless flashing
+ # So we write & in the here docs and transform them back:
+ my ($t) = join '', @_;
+ $t =~ s/^\&+/ '%' x length $& /mge;
+ o($t);