Debian Codegraph @

impose

raw ◀ ▶
1 Debian patch touch this file
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 my($verbose);
36
37 while($_=$ARGV[0],/^-/) {
38 shift;
39
40 /^-help/ && do { print <<EOHELP; exit; };
41 impose - create 2-up files
42
43 Syntax:
44 impose {options} filename
45
46 Description:
47 Produces a new file filename.imposed that is a 2-up version of the
48 file filename.
49
50 Options:
51 -letter Use letter size paper (sets width and height).
52 -height h Set paper height h.
53 -width w Set paper width w.
54 -scale s Force scale of imposed pages to be s.
55 -noscale Equal to -scale 1.0
56 -pages p How many pages bboxx should probe to find document bbox.
57 -xm Left and right margins along the long side of the page.
58 Default 30.
59 -xrm Separate right x-margin.
60 -xlm Separate left x-margin.
61 -ym Margin at the top of the page. Default is 40.
62 -gutter Gutter space between the two columns.
63 -noglue Don\'t rubber band extra white space.
64 -bbox Manually set bbox so bboxx won\'t be run.
65 -noevenodd Don\'t use different bounding boxes for even and odd pages.
66 -evenbbox Set even page bbox.
67 -oddbbox Set odd page bbox.
68 -lastbbox Use bbox information of last run of $0.
69 -stdout Print output to stdout.
70 -rtl Do RTL layout of pages
•71 -v Verbose operation
72
73 Author:
74 Dov Grobgeld
75 EOHELP
76
77 /^-letter/ && do { $height = 792; $width=612; next; };
78 /^-height/ && do { $height = shift; next; };
79 /^-rtl/ && do { $rtl++; next; };
80 /^-width/ && do { $width = shift; next; };
81 /^-scale/ && do { $scaling = shift; next; };
82 /^-noscal/ && do { $scaling=1.0; next; };
83 /^-pages/ && do { $pages = shift; next; };
84 /^-ud/ && do { $rotdir='R'; next; };
85 /^-xm/ && do { $xright = $xleft = shift; next; };
86 /^-xrm/ && do { $xright = shift; next; };
87 /^-xlm/ && do { $xleft = shift; next; };
88 /^-ym/ && do { $ytop = $ybot = shift; next; };
89 /^-tmarg/ && do { $ytop = shift; next; };
90 /^-bmarg/ && do { $ybot = shift; next; };
91 /^-notumble/ && do { $no_tumble++; next; };
92 /^-ybot/ && do { $ybot = shift; next; };
93 /^-gutter/ && do { $gutter = shift; next; };
94 /^-stdout/ && do { $do_stdout++; next; };
95 /^-noglue/ && do { $noglue++; next; };
96 /^-noevenodd/ && do { $noevenodd++; next };
97 /^-bboxfile/ && do { $bboxfile=shift; next; };
98 /^-bbox/ && do { $bbox[0]=shift; $bbox[1]=shift; $bbox[2]=shift; $bbox[3]=shift; next; };
99 /^-evenbbox/ && do { $evenbbox[0]=shift; $evenbbox[1]=shift; $evenbbox[2]=shift; $evenbbox[3]=shift; next; };
100 /^-oddbbox/ && do { $oddbbox[0]=shift; $oddbbox[1]=shift; $oddbbox[2]=shift; $oddbbox[3]=shift; next; };
101 /^-lastbbox/ && do { $lastbbox++; next };
•102 /^-v/ && do {$verbose++; next; };
103 die "Unknown option $_!\n";
104 }
105
106 # defaults
107 $height=842 unless $height;
108 $width=595 unless $width;
109 $xright=30 unless $xright;
110 $xleft=30 unless $xleft;
111 $gutter=40 unless $gutter;
112 $ytop=30 unless $ytop;
113 $ybot=30 unless $ybot;
114 $pages=6 unless defined $pages;
115 $rotdir="L" unless $rotdir;
116
117 $filename = shift or do {
118 $filename = "/tmp/$ENV{USER}-tmp.ps";
119 open(TMP, ">$filename");
120 while(<>) { print TMP; }
121 close(TMP);
122 $do_stdout++;
123 $do_erase_tmp++;
124 };
125 #die "Couldn't get filename!\n";
126 -f $filename || die "No such file $filename!\n";
127
128 if ($filename =~ m|/([^/]*)$|) {
129 $filename = $1;
130 $path = $`;
131 }
132 else {
133 $path = ".";
134 }
135
136 # get the bounding box
137 if ($bboxfile) {
138 open(BB,"$bboxfile");
139 $_=<BB>; @oddbbox=split;
140 $_=<BB>; @evenbbox=split;
141 @oddbbox = @evenbbox unless @evenbbox;
142 close(BB);
143 }
144 elsif ($lastbbox && -e "/tmp/bboxx-$ENV{USER}") {
145 open(BB,"/tmp/bboxx-$ENV{USER}");
146 $_=<BB>; @oddbbox=split;
147 $_=<BB>; @evenbbox=split;
148 close(BB);
149 # print "oddbbox = @oddbbox\n";
150 # print "evenbbox = @evenbbox\n";
151 }
152 elsif (@bbox) {
153 @oddbbox=@bbox;
154 @evenbbox=@bbox;
155 }
156 elsif (!@oddbbox || !@evenbbox) {
157 @oddbbox=(0,0,0,0);
158 @evenbbox=(0,0,0,0);
159 &getboundingboxes("$path/$filename", @oddbbox, @evenbbox);
160 }
161
162 @evenbbox = @oddbbox if $noevenodd;
163
164 # record the bboxx information
165 open(BB, ">/tmp/bboxx-$ENV{USER}");
166 print BB "@oddbbox\n@evenbbox\n";
167 close(BB);
168
169 # Do page calculations
170 $oddwidth = $oddbbox[2]-$oddbbox[0];
171 $oddheight = $oddbbox[3]-$oddbbox[1];
172 $evenwidth = $evenbbox[2]-$evenbbox[0];
173 $evenheight = $evenbbox[3]-$evenbbox[1];
174
175 # Check for lone page...
176 if ($evenwidth <= 0) {
177 $evenwidth = $oddwidth;
178 $evenheight = $oddheight;
179 }
180
181 unless ($scaling) {
182 $scaling = ($height/2 - $gutter/2 - $xleft) / $oddwidth;
183 if ($oddheight*$scaling > $width - $ytop - $ybot) {
184 $scaling = ($width - $ytop - $ybot) / $oddheight;
185 }
186 }
187
188 # for ($oddheight,$oddweight,$evenheight, $evenwidth) { $_*= $scaling } # perl5
189
190 $oddheight *= $scaling;
191 $oddwidth *= $scaling;
192 $evenheight *= $scaling;
193 $evenwidth *= $scaling;
194
195 # scale bboxes
196 foreach (@oddbbox) { $_ = sprintf("%.1f",$_*$scaling); }
197 foreach (@evenbbox) { $_ = sprintf("%.1f",$_*$scaling); }
198
199 # readjust xleft, ytop, and gutter in a TeX glue like manner
200 unless ($noglue) {
201 my $mpg = $height - $oddwidth - $evenwidth; # xmargins + gutter
202 my $r = $xleft/($gutter+$xleft+$xright);
203 my $r1= $xright/($gutter+$xleft+$xright);
204 $xleft= $mpg*$r;
205 $xright= $mpg*$r1;
206 $gutter= $mpg*(1-$r-$r1);
207 my $ym = $width - $oddheight; # ymargins
208 my $s = $ytop / ($ytop + $ybot);
209 $ytop = $ym * $s;
210 $ybot = $ym * (1-$s);
211 }
212
213 my $shifty = sprintf("%.1f", $ytop+$oddbbox[3]);
214 my $shiftoddx = sprintf("%.1f",$xleft-$oddbbox[0]);
215 my $shiftevenx = sprintf("%.1f", $height-$xright-$evenwidth - $evenbbox[0]);
216 $scaling= sprintf("%.4f", $scaling);
217
•218 my($pstops_prm, $pstops_swtch);
219 if ($rtl) {
220 $pstops_prm = "2:1${rotdir}\@${scaling}($shifty,$shiftoddx)+0${rotdir}\@${scaling}($shifty,$shiftevenx)";
221 }
222 else {
223 $pstops_prm = "2:0${rotdir}\@${scaling}($shifty,$shiftoddx)+1${rotdir}\@${scaling}($shifty,$shiftevenx)";
224 }
225
•226 $pstops_swtch = " -q " unless $verbose;
227
228 my $out = "|fixtd -tumble" unless $no_tumble;
•229 $out .= "> '$filename.imposed'" unless $do_stdout;
230 my $cmd="pstops $pstops_swtch '$pstops_prm' '$path/$filename' $out";
231 print STDERR $cmd,"\n" if $verbose;
232 system $cmd;
233 unlink "$path/$filename" if $do_erase_tmp;
234
235 sub getboundingboxes {
236 my ($filename) = $_[0];
237 my($bboxinfo);
238
239 # This should really only look for some reasonable amount of pages...
240 $pages = "-pages $pages" if $pages;
•241 my $cmd= "bboxx -evenodd $pages '$filename'";
242 print STDERR "$cmd\n" if $verbose;
243 $_ = `$cmd`;
•244 print STDERR if $verbose;
245
246 # Get the odd pages bounding box
247 my ($llx,$lly,$urx,$ury) = /Odd: \s*(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/;
248
249 $_[1] = $llx;
250 $_[2] = $lly;
251 $_[3] = $urx;
252 $_[4] = $ury;
253
254 # Get the even pages bounding box
255 ($llx,$lly,$urx,$ury) = /Even: \s*(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/;
256 $_[5] = $llx;
257 $_[6] = $lly;
258 $_[7] = $urx;
259 $_[8] = $ury;
260 }