| 1 |
#!/usr/bin/perl |
| 2 |
###################################################################### |
| 3 |
# An imposition program for doing automatic balanced twoup |
| 4 |
# imposition. |
| 5 |
# |
| 6 |
# Copyright (C) 1999 Dov Grobgeld <[email protected]> |
| 7 |
# |
| 8 |
# This program is free software; you can redistribute it and/or modify |
| 9 |
# it under the terms of the GNU General Public License as published by |
| 10 |
# the Free Software Foundation; either version 2 of the License, or |
| 11 |
# (at your option) any later version. |
| 12 |
# |
| 13 |
# This program is distributed in the hope that it will be useful, |
| 14 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
# GNU General Public License for more details. |
| 17 |
# |
| 18 |
# You should have received a copy of the GNU General Public License |
| 19 |
# along with this program; if not, write to the Free Software |
| 20 |
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 |
# |
| 22 |
# For more details see the file COPYING. |
| 23 |
###################################################################### |
| 24 |
|
| 25 |
my $VERSION = 0.2; |
| 26 |
|
| 27 |
use strict; |
| 28 |
|
| 29 |
my($height,$width,$rtl,$scaling,$pages,$rotdir,$xright,$xleft,$ytop,$ybot); |
| 30 |
my($no_tumble, $gutter, $do_stdout, $noglue, $noevenodd, $bboxfile); |
| 31 |
my(@bbox,@oddbbox, @evenbbox, $lastbbox); |
| 32 |
my($filename, $path, $do_erase_tmp); |
| 33 |
my($evenwidth, $evenheight); |
| 34 |
my($oddwidth, $oddheight); |
| 35 |
|
| 36 |
while($_=$ARGV[0],/^-/) { |
| 37 |
shift; |
| 38 |
|
| 39 |
/^-help/ && do { print <<EOHELP; exit; }; |
| 40 |
impose - create 2-up files |
| 41 |
|
| 42 |
Syntax: |
| 43 |
impose {options} filename |
| 44 |
|
| 45 |
Description: |
| 46 |
Produces a new file filename.imposed that is a 2-up version of the |
| 47 |
file filename. |
| 48 |
|
| 49 |
Options: |
| 50 |
-letter Use letter size paper (sets width and height). |
| 51 |
-height h Set paper height h. |
| 52 |
-width w Set paper width w. |
| 53 |
-scale s Force scale of imposed pages to be s. |
| 54 |
-noscale Equal to -scale 1.0 |
| 55 |
-pages p How many pages bboxx should probe to find document bbox. |
| 56 |
-xm Left and right margins along the long side of the page. |
| 57 |
Default 30. |
| 58 |
-xrm Separate right x-margin. |
| 59 |
-xlm Separate left x-margin. |
| 60 |
-ym Margin at the top of the page. Default is 40. |
| 61 |
-gutter Gutter space between the two columns. |
| 62 |
-noglue Don\'t rubber band extra white space. |
| 63 |
-bbox Manually set bbox so bboxx won\'t be run. |
| 64 |
-noevenodd Don\'t use different bounding boxes for even and odd pages. |
| 65 |
-evenbbox Set even page bbox. |
| 66 |
-oddbbox Set odd page bbox. |
| 67 |
-lastbbox Use bbox information of last run of $0. |
| 68 |
-stdout Print output to stdout. |
| 69 |
-rtl Do RTL layout of pages |
| 70 |
|
| 71 |
Author: |
| 72 |
Dov Grobgeld |
| 73 |
EOHELP |
| 74 |
|
| 75 |
/^-letter/ && do { $height = 792; $width=612; next; }; |
| 76 |
/^-height/ && do { $height = shift; next; }; |
| 77 |
/^-rtl/ && do { $rtl++; next; }; |
| 78 |
/^-width/ && do { $width = shift; next; }; |
| 79 |
/^-scale/ && do { $scaling = shift; next; }; |
| 80 |
/^-noscal/ && do { $scaling=1.0; next; }; |
| 81 |
/^-pages/ && do { $pages = shift; next; }; |
| 82 |
/^-ud/ && do { $rotdir='R'; next; }; |
| 83 |
/^-xm/ && do { $xright = $xleft = shift; next; }; |
| 84 |
/^-xrm/ && do { $xright = shift; next; }; |
| 85 |
/^-xlm/ && do { $xleft = shift; next; }; |
| 86 |
/^-ym/ && do { $ytop = $ybot = shift; next; }; |
| 87 |
/^-tmarg/ && do { $ytop = shift; next; }; |
| 88 |
/^-bmarg/ && do { $ybot = shift; next; }; |
| 89 |
/^-notumble/ && do { $no_tumble++; next; }; |
| 90 |
/^-ybot/ && do { $ybot = shift; next; }; |
| 91 |
/^-gutter/ && do { $gutter = shift; next; }; |
| 92 |
/^-stdout/ && do { $do_stdout++; next; }; |
| 93 |
/^-noglue/ && do { $noglue++; next; }; |
| 94 |
/^-noevenodd/ && do { $noevenodd++; next }; |
| 95 |
/^-bboxfile/ && do { $bboxfile=shift; next; }; |
| 96 |
/^-bbox/ && do { $bbox[0]=shift; $bbox[1]=shift; $bbox[2]=shift; $bbox[3]=shift; next; }; |
| 97 |
/^-evenbbox/ && do { $evenbbox[0]=shift; $evenbbox[1]=shift; $evenbbox[2]=shift; $evenbbox[3]=shift; next; }; |
| 98 |
/^-oddbbox/ && do { $oddbbox[0]=shift; $oddbbox[1]=shift; $oddbbox[2]=shift; $oddbbox[3]=shift; next; }; |
| 99 |
/^-lastbbox/ && do { $lastbbox++; next }; |
| 100 |
die "Unknown option $_!\n"; |
| 101 |
} |
| 102 |
|
| 103 |
# defaults |
| 104 |
$height=842 unless $height; |
| 105 |
$width=595 unless $width; |
| 106 |
$xright=30 unless $xright; |
| 107 |
$xleft=30 unless $xleft; |
| 108 |
$gutter=40 unless $gutter; |
| 109 |
$ytop=30 unless $ytop; |
| 110 |
$ybot=30 unless $ybot; |
| 111 |
$pages=6 unless defined $pages; |
| 112 |
$rotdir="L" unless $rotdir; |
| 113 |
|
| 114 |
$filename = shift or do { |
| 115 |
$filename = "/tmp/$ENV{USER}-tmp.ps"; |
| 116 |
open(TMP, ">$filename"); |
| 117 |
while(<>) { print TMP; } |
| 118 |
close(TMP); |
| 119 |
$do_stdout++; |
| 120 |
$do_erase_tmp++; |
| 121 |
}; |
| 122 |
#die "Couldn't get filename!\n"; |
| 123 |
-f $filename || die "No such file $filename!\n"; |
| 124 |
|
| 125 |
if ($filename =~ m|/([^/]*)$|) { |
| 126 |
$filename = $1; |
| 127 |
$path = $`; |
| 128 |
} |
| 129 |
else { |
| 130 |
$path = "."; |
| 131 |
} |
| 132 |
|
| 133 |
# get the bounding box |
| 134 |
if ($bboxfile) { |
| 135 |
open(BB,"$bboxfile"); |
| 136 |
$_=<BB>; @oddbbox=split; |
| 137 |
$_=<BB>; @evenbbox=split; |
| 138 |
@oddbbox = @evenbbox unless @evenbbox; |
| 139 |
close(BB); |
| 140 |
} |
| 141 |
elsif ($lastbbox && -e "/tmp/bboxx-$ENV{USER}") { |
| 142 |
open(BB,"/tmp/bboxx-$ENV{USER}"); |
| 143 |
$_=<BB>; @oddbbox=split; |
| 144 |
$_=<BB>; @evenbbox=split; |
| 145 |
close(BB); |
| 146 |
# print "oddbbox = @oddbbox\n"; |
| 147 |
# print "evenbbox = @evenbbox\n"; |
| 148 |
} |
| 149 |
elsif (@bbox) { |
| 150 |
@oddbbox=@bbox; |
| 151 |
@evenbbox=@bbox; |
| 152 |
} |
| 153 |
elsif (!@oddbbox || !@evenbbox) { |
| 154 |
@oddbbox=(0,0,0,0); |
| 155 |
@evenbbox=(0,0,0,0); |
| 156 |
&getboundingboxes("$path/$filename", @oddbbox, @evenbbox); |
| 157 |
} |
| 158 |
|
| 159 |
@evenbbox = @oddbbox if $noevenodd; |
| 160 |
|
| 161 |
# record the bboxx information |
| 162 |
open(BB, ">/tmp/bboxx-$ENV{USER}"); |
| 163 |
print BB "@oddbbox\n@evenbbox\n"; |
| 164 |
close(BB); |
| 165 |
|
| 166 |
# Do page calculations |
| 167 |
$oddwidth = $oddbbox[2]-$oddbbox[0]; |
| 168 |
$oddheight = $oddbbox[3]-$oddbbox[1]; |
| 169 |
$evenwidth = $evenbbox[2]-$evenbbox[0]; |
| 170 |
$evenheight = $evenbbox[3]-$evenbbox[1]; |
| 171 |
|
| 172 |
# Check for lone page... |
| 173 |
if ($evenwidth <= 0) { |
| 174 |
$evenwidth = $oddwidth; |
| 175 |
$evenheight = $oddheight; |
| 176 |
} |
| 177 |
|
| 178 |
unless ($scaling) { |
| 179 |
$scaling = ($height/2 - $gutter/2 - $xleft) / $oddwidth; |
| 180 |
if ($oddheight*$scaling > $width - $ytop - $ybot) { |
| 181 |
$scaling = ($width - $ytop - $ybot) / $oddheight; |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
# for ($oddheight,$oddweight,$evenheight, $evenwidth) { $_*= $scaling } # perl5 |
| 186 |
|
| 187 |
$oddheight *= $scaling; |
| 188 |
$oddwidth *= $scaling; |
| 189 |
$evenheight *= $scaling; |
| 190 |
$evenwidth *= $scaling; |
| 191 |
|
| 192 |
# scale bboxes |
| 193 |
foreach (@oddbbox) { $_ = sprintf("%.1f",$_*$scaling); } |
| 194 |
foreach (@evenbbox) { $_ = sprintf("%.1f",$_*$scaling); } |
| 195 |
|
| 196 |
# readjust xleft, ytop, and gutter in a TeX glue like manner |
| 197 |
unless ($noglue) { |
| 198 |
my $mpg = $height - $oddwidth - $evenwidth; # xmargins + gutter |
| 199 |
my $r = $xleft/($gutter+$xleft+$xright); |
| 200 |
my $r1= $xright/($gutter+$xleft+$xright); |
| 201 |
$xleft= $mpg*$r; |
| 202 |
$xright= $mpg*$r1; |
| 203 |
$gutter= $mpg*(1-$r-$r1); |
| 204 |
my $ym = $width - $oddheight; # ymargins |
| 205 |
my $s = $ytop / ($ytop + $ybot); |
| 206 |
$ytop = $ym * $s; |
| 207 |
$ybot = $ym * (1-$s); |
| 208 |
} |
| 209 |
|
| 210 |
my $shifty = sprintf("%.1f", $ytop+$oddbbox[3]); |
| 211 |
my $shiftoddx = sprintf("%.1f",$xleft-$oddbbox[0]); |
| 212 |
my $shiftevenx = sprintf("%.1f", $height-$xright-$evenwidth - $evenbbox[0]); |
| 213 |
$scaling= sprintf("%.4f", $scaling); |
| 214 |
|
| 215 |
my $pstops_prm; |
| 216 |
if ($rtl) { |
| 217 |
$pstops_prm = "2:1${rotdir}\@${scaling}($shifty,$shiftoddx)+0${rotdir}\@${scaling}($shifty,$shiftevenx)"; |
| 218 |
} |
| 219 |
else { |
| 220 |
$pstops_prm = "2:0${rotdir}\@${scaling}($shifty,$shiftoddx)+1${rotdir}\@${scaling}($shifty,$shiftevenx)"; |
| 221 |
} |
| 222 |
|
| 223 |
my $out = "|fixtd -tumble" unless $no_tumble; |
| 224 |
$out .= "> $filename.imposed" unless $do_stdout; |
| 225 |
my $cmd="pstops '$pstops_prm' $path/$filename $out"; |
| 226 |
print STDERR $cmd,"\n"; |
| 227 |
system $cmd; |
| 228 |
unlink "$path/$filename" if $do_erase_tmp; |
| 229 |
|
| 230 |
sub getboundingboxes { |
| 231 |
my ($filename) = $_[0]; |
| 232 |
my($bboxinfo); |
| 233 |
|
| 234 |
# This should really only look for some reasonable amount of pages... |
| 235 |
$pages = "-pages $pages" if $pages; |
| 236 |
my $cmd= "bboxx -evenodd $pages $filename"; |
| 237 |
print "$cmd\n"; |
| 238 |
$_ = `$cmd`; |
| 239 |
print; |
| 240 |
|
| 241 |
# Get the odd pages bounding box |
| 242 |
my ($llx,$lly,$urx,$ury) = /Odd: \s*(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/; |
| 243 |
|
| 244 |
$_[1] = $llx; |
| 245 |
$_[2] = $lly; |
| 246 |
$_[3] = $urx; |
| 247 |
$_[4] = $ury; |
| 248 |
|
| 249 |
# Get the even pages bounding box |
| 250 |
($llx,$lly,$urx,$ury) = /Even: \s*(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/; |
| 251 |
$_[5] = $llx; |
| 252 |
$_[6] = $lly; |
| 253 |
$_[7] = $urx; |
| 254 |
$_[8] = $ury; |
| 255 |
} |