summaryrefslogtreecommitdiff
path: root/support/impose/psbl
blob: 239b350fca2737d41e7b1f24335eed385f0c4063 (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
#!/usr/local/bin/perl

######################################################################
#  Perl font split impose
#
#  Copyright (C) 1999 Dov Grobgeld <dov@imagic.weizmann.ac.il>
#  
#  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.
######################################################################

# Get options
while($_ = $ARGV[0], /^-/) {
    shift;
    /^-lastbbox/ and do { $lastbbox="-lastbbox"; next; };
    /^-lpi/      and do { $lpi = shift; next; };
    /^-impose_options/ and do { $impose_options = shift; next; };
    /^-max(_pages)?/ and do { $max_pages_per_signature = shift; next; };
    /^-help/ and print<<HELP and exit;
psbl -- A program for automatic booklet formatting of postscript files

SYNTAX:
    psbl [-lastbbox] [-lpi] [-impose_options io] [-max_pages mp]
         postscript.file

SEEALSO:
    impose 
HELP
    die "Unknown option $_!\n";
}

$lpi = 80 unless $lpi;

# Defaults
$max_pages_per_signature = 60 unless $max_pages_per_signature;

# Get the name of the orginial file
$psname = shift || die "Need name of ps file!\n";

# Get number of pages in file
$pages = get_num_pages($psname);

# Split it nicely
$num_signatures = int($pages/$max_pages_per_signature + 0.9999999);

$q_per_signature = int($pages/4/$num_signatures+0.999);
print "num_signatures = $num_signatures\n";
print "q_per_signature = $q_per_signature\n";

for $i (0..$num_signatures-1) {
    my $start = $i * $q_per_signature * 4 + 1;
    my $end = ($i+1) * $q_per_signature * 4;
    if ($end > $pages) {
	$end = $pages;
    }
    $cmd = "psselect $start-$end $psname | fixtd -tumble | psbook > $psname.$start-$end";
    print "$cmd\n";
    system $cmd;

    $lastbbox = "-lastbbox" if $i > 0;
    $cmd = "impose $impose_options $lastbbox $psname.$start-$end";
    print "$cmd\n";
    system $cmd;

    unlink "$psname.$start-$end";
}
print "Done!\n";

sub get_num_pages {
    my $fn = shift;
    my $pages;
    open(PS, $fn) || die "Couldn't open $fn: $!\n";
    while(<PS>) {
	if ($. == 1) {
	    die "Not a Postscript file!\n" unless /^%!/;
	}
	$pages = $_ and last if /^%%Pages:/;
    }
    die "Couldn't find %%Pages!\n" unless $pages;

    if ($pages =~ /atend/) {
	seek(PS, -2000, 2);
	read(PS, $_, 2000);
	/^%%Pages:.*$/m or die "Couldn't find %%Pages at end of file!\n";
	$pages = $&;
    }

    $pages=~ /(\d+)\S*(\d*)/;
    return $1;
}