#!/usr/bin/perl ###################################################################### # An imposition program for doing automatic balanced twoup # imposition. # # Copyright (C) 1999 Dov Grobgeld # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # # For more details see the file COPYING. ###################################################################### my $VERSION = 0.2; use strict; my($height,$width,$rtl,$scaling,$pages,$rotdir,$xright,$xleft,$ytop,$ybot); my($no_tumble, $gutter, $do_stdout, $noglue, $noevenodd, $bboxfile); my(@bbox,@oddbbox, @evenbbox, $lastbbox); my($filename, $path, $do_erase_tmp); my($evenwidth, $evenheight); my($oddwidth, $oddheight); my($verbose); while($_=$ARGV[0],/^-/) { shift; /^-help/ && do { print <$filename"); while(<>) { print TMP; } close(TMP); $do_stdout++; $do_erase_tmp++; }; #die "Couldn't get filename!\n"; -f $filename || die "No such file $filename!\n"; if ($filename =~ m|/([^/]*)$|) { $filename = $1; $path = $`; } else { $path = "."; } # get the bounding box if ($bboxfile) { open(BB,"$bboxfile"); $_=; @oddbbox=split; $_=; @evenbbox=split; @oddbbox = @evenbbox unless @evenbbox; close(BB); } elsif ($lastbbox && -e "/tmp/bboxx-$ENV{USER}") { open(BB,"/tmp/bboxx-$ENV{USER}"); $_=; @oddbbox=split; $_=; @evenbbox=split; close(BB); # print "oddbbox = @oddbbox\n"; # print "evenbbox = @evenbbox\n"; } elsif (@bbox) { @oddbbox=@bbox; @evenbbox=@bbox; } elsif (!@oddbbox || !@evenbbox) { @oddbbox=(0,0,0,0); @evenbbox=(0,0,0,0); &getboundingboxes("$path/$filename", @oddbbox, @evenbbox); } @evenbbox = @oddbbox if $noevenodd; # record the bboxx information open(BB, ">/tmp/bboxx-$ENV{USER}"); print BB "@oddbbox\n@evenbbox\n"; close(BB); # Do page calculations $oddwidth = $oddbbox[2]-$oddbbox[0]; $oddheight = $oddbbox[3]-$oddbbox[1]; $evenwidth = $evenbbox[2]-$evenbbox[0]; $evenheight = $evenbbox[3]-$evenbbox[1]; # Check for lone page... if ($evenwidth <= 0) { $evenwidth = $oddwidth; $evenheight = $oddheight; } unless ($scaling) { $scaling = ($height/2 - $gutter/2 - $xleft) / $oddwidth; if ($oddheight*$scaling > $width - $ytop - $ybot) { $scaling = ($width - $ytop - $ybot) / $oddheight; } } # for ($oddheight,$oddweight,$evenheight, $evenwidth) { $_*= $scaling } # perl5 $oddheight *= $scaling; $oddwidth *= $scaling; $evenheight *= $scaling; $evenwidth *= $scaling; # scale bboxes foreach (@oddbbox) { $_ = sprintf("%.1f",$_*$scaling); } foreach (@evenbbox) { $_ = sprintf("%.1f",$_*$scaling); } # readjust xleft, ytop, and gutter in a TeX glue like manner unless ($noglue) { my $mpg = $height - $oddwidth - $evenwidth; # xmargins + gutter my $r = $xleft/($gutter+$xleft+$xright); my $r1= $xright/($gutter+$xleft+$xright); $xleft= $mpg*$r; $xright= $mpg*$r1; $gutter= $mpg*(1-$r-$r1); my $ym = $width - $oddheight; # ymargins my $s = $ytop / ($ytop + $ybot); $ytop = $ym * $s; $ybot = $ym * (1-$s); } my $shifty = sprintf("%.1f", $ytop+$oddbbox[3]); my $shiftoddx = sprintf("%.1f",$xleft-$oddbbox[0]); my $shiftevenx = sprintf("%.1f", $height-$xright-$evenwidth - $evenbbox[0]); $scaling= sprintf("%.4f", $scaling); my($pstops_prm, $pstops_swtch); if ($rtl) { $pstops_prm = "2:1${rotdir}\@${scaling}($shifty,$shiftoddx)+0${rotdir}\@${scaling}($shifty,$shiftevenx)"; } else { $pstops_prm = "2:0${rotdir}\@${scaling}($shifty,$shiftoddx)+1${rotdir}\@${scaling}($shifty,$shiftevenx)"; } $pstops_swtch = " -q " unless $verbose; my $out = "|fixtd -tumble" unless $no_tumble; $out .= "> '$filename.imposed'" unless $do_stdout; my $cmd="pstops $pstops_swtch '$pstops_prm' '$path/$filename' $out"; print STDERR $cmd,"\n" if $verbose; system $cmd; unlink "$path/$filename" if $do_erase_tmp; sub getboundingboxes { my ($filename) = $_[0]; my($bboxinfo); # This should really only look for some reasonable amount of pages... $pages = "-pages $pages" if $pages; my $cmd= "bboxx -evenodd $pages '$filename'"; print STDERR "$cmd\n" if $verbose; $_ = `$cmd`; print STDERR if $verbose; # Get the odd pages bounding box my ($llx,$lly,$urx,$ury) = /Odd: \s*(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/; $_[1] = $llx; $_[2] = $lly; $_[3] = $urx; $_[4] = $ury; # Get the even pages bounding box ($llx,$lly,$urx,$ury) = /Even: \s*(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/; $_[5] = $llx; $_[6] = $lly; $_[7] = $urx; $_[8] = $ury; }