chiark / gitweb /
numbered-alias-sheet: implement -g and -b
[evade-mail-usrlocal.git] / numbered-alias-sheet
1 #!/usr/bin/perl -w
2 use strict;
3 our $us = $0; $us =~ s#.*/##;
4
5 use POSIX;
6 use Data::Dumper;
7
8 our $papersize =  'creditcard';
9 our $fontname = 'Courier';
10 our $fontname_num = 'Courier';
11 our $gapratio = 1;
12 our $blankratio = 1;
13 our @borders = (4,4);
14
15 our @lp_options = ( [ 'blank-below', 'blank-to-right', ],
16                     [ 'landscape', 'portrait' ],
17                     [ 'single-column', 'multi-column', ] );
18 our @lp_fixed;
19
20 our $usage = <<END;
21 usage: $us [<options>] <foo-mail-pregen> <foo-mail-pregen-opts>...
22 options:
23   -p<papersize>           for libpaper, or "creditcard"  default is $papersize
24   -F[<numberfont>,]<font> font name                      default is $fontname
25   -b<border>|-b<bx>x<by>  all in mm
26   -g<gapratio>            number-to-addr gap adjustment
27   -b<blankratio>          (blank space size) / (text size)
28   -D                      debug
29 END
30
31 open DEBUG, ">/dev/null" or die $!;
32
33 our @paperpts;
34
35 sub badusage () { die "bad usage\n\n$usage"; }
36
37 sub mm2pt { map { $_ * 72.0 / 25.4 } @_; }
38
39 sub max {
40     my $r = undef;
41     foreach (@_) {
42         $r = $_ if !defined $r or $_ > $r;
43     }
44     return $r;
45 }
46
47 sub min {
48     my $r = undef;
49     foreach (@_) {
50         $r = $_ if !defined $r or $_ < $r;
51     }
52     return $r;
53 }
54
55 my $fontname_re = '[^()\\,]+';
56 my $dbl_re = '(?:[0-9]+\.?|[0-9]*\.[0-9]+)';
57
58 for (;;) {
59     badusage unless @ARGV;
60     last unless $ARGV[0] =~ m/^-/;
61     $_ = shift @ARGV;
62     last if m/^--?$/;
63     while (m/^-./) {
64         if (s/^-p(\w+)$//) {
65             $papersize = $1;
66             @paperpts = ();
67         } elsif (s/^-p($dbl_re)x($dbl_re)$//o) {
68             $papersize = undef;
69             @paperpts = mm2pt($1,$2);
70         } elsif (s/^-F($fontname_re)$//o) {
71             $fontname = $fontname_num = $1;
72         } elsif (s/^-F($fontname_re),($fontname_re)$//o) {
73             ($fontname_num, $fontname) = ($1, $2);
74         } elsif (s/^-b($dbl_re)$//o) {
75             @borders = ($1,$1);
76         } elsif (s/^-b($dbl_re)x($dbl_re)$//o) {
77             @borders = ($1,$1);
78         } elsif (s/^-g($dbl_re)$//o) {
79             $gapratio = $1;
80         } elsif (s/^-b($dbl_re)$//o) {
81             $blankratio = $1;
82         } elsif (s/^-D/-/) {
83             open DEBUG, ">&STDERR" or die $!;
84         } else {
85             badusage;
86         }
87     }
88 }
89
90 if (!@paperpts) {
91     if ($papersize eq 'creditcard') {
92         # ISO/IEC 7810 ID-1, from en.wikipedia.org/wiki/Payment_card
93         @paperpts = mm2pt qw(85.60 53.98);
94     } else {
95         $!=0; $?=0; my $r = `paperconf -sp $1`;
96         defined $r or die "paperconf failed: $? $!\n";
97         $r =~ m/^([0-9.]+) ([0-9.]+)$/ or die "$_ ?";
98         @paperpts = ($1,$2);
99     }
100 }
101
102 @borders = mm2pt @borders;
103
104 @ARGV >= 2 or badusage;
105
106 our @strings;
107
108 sub readstrings () {
109     my $nlen = 0;
110
111     open P, "-|", @ARGV or die $!;
112     while (<P>) {
113         chomp or die;
114         m/^(\d+) (\S+)$/ or die "$_ ?";
115         $nlen = length($1) if length($1) > $nlen;
116         push @strings, [ $1, $2 ];
117     }
118     $!=0; $?=0; close P or die "$us: generator failed: $! $?\n";
119 }
120
121 our @lp_values;
122 our @numbers_bbox;
123 our @texts_bbox;
124 our $gap_width;
125 our @core_size;
126 our @item_size;
127 our $rotate_paper;
128 our @eff_paper_size;
129 our @laycount;
130
131 sub wontfit ($) {
132     print DEBUG " NO @_\n";
133     return 0;
134 }
135
136 sub psstring ($) {
137     local ($_) = @_;
138     s/[()\\]/\\$&/g;
139     return "($_)";
140 }
141
142 our @numbers_1_bbox;
143 our @nom_gap_1_bbox;
144 our @texts_1_bbox;
145
146 sub prepare_metrics () {
147     print DEBUG " prepare_metrics\n";
148     my $pchild = open GI, "-|"; defined $pchild or die $!;
149     my @sets = 
150         ([ \@numbers_1_bbox, $fontname_num, map { $_->[0] } @strings ],
151          [ \@nom_gap_1_bbox, $fontname_num, 'x'                      ],
152          [ \@texts_1_bbox,   $fontname,     map { $_->[1] } @strings ],
153         );
154     if (!$pchild) {
155         foreach my $set (@sets) {
156             my ($ra, $fn, @s) = @$set;
157             print DEBUG "  want $fn ",scalar(@s),"\n";
158             print psstring($fn)," findfont 10 scalefont setfont\n" or die $!;
159             print "0 0 moveto ", psstring($_), " show showpage\n" or die $!
160                 foreach @s;
161         }
162         close STDOUT or die $!;
163         exit 0;
164     }
165     my $gchild = open GO, "-|"; defined $gchild or die $!;
166     if (!$gchild) {
167         open STDIN, "<&GI" or die $!;
168         open STDERR, ">&STDOUT" or die $!;
169         exec qw(gs -dSAFER -dNOPAUSE -q -dBATCH -sDEVICE=bbox -) 
170             or die "$us: exec gs: $!\n";
171     }
172     foreach my $set (@sets) {
173         my ($ra, $fn, @s) = @$set;
174         my @bb;
175         for (my $count=0; $count < @s; $count++) {
176             $_ = <GO>; defined or die "gs fail or eof";
177             printf DEBUG "    %s (%d) |%s", $fn, $count, $_;
178             if (my @tbb =
179  m/^\%\%HiResBoundingBox: ($dbl_re) ($dbl_re) ($dbl_re) ($dbl_re)$/
180                 ) {
181                 $bb[0] = min $bb[0], $tbb[0];
182                 $bb[1] = min $bb[1], $tbb[1];
183                 $bb[2] = max $bb[2], $tbb[2];
184                 $bb[3] = max $bb[3], $tbb[3];
185             } elsif (m/^\%\%/) {
186             } else {
187                 print STDERR "$us: warning: gs: $_" or die;
188             }
189         }
190         print DEBUG "  metrics $fn @bb\n";
191         @$ra = map { $_ * 0.1 } @bb;
192     }
193     $!=0; $?=0; close GO or die "gs $! $?";
194     $!=0; $?=0; close GI or die "gs paste $! $?";
195 }
196
197 sub do_layout_recursive_search ($);
198 sub do_layout_recursive_search ($) {
199     my ($lpi) = @_;
200
201     if ($lpi < @lp_options) {
202         foreach my $v ($lp_fixed[$lpi] or @{ $lp_options[$lpi] }) {
203             $lp_values[$lpi] = $v;
204             return 1 if do_layout_recursive_search $lpi+1;
205         }
206         return 0;
207     }
208
209     print DEBUG " try", (map { sprintf " %-10.10s", $_ } @lp_values), ":";
210
211     my %lp_y;
212     $lp_y{$_} = 1 foreach @lp_values;
213
214     $rotate_paper =
215         ($paperpts[0] > $paperpts[1] # paper looks like landscape
216          xor $lp_y{'landscape'});
217     @eff_paper_size = !$rotate_paper ? @paperpts : reverse @paperpts;
218
219     @item_size = @core_size;
220     my $blank_coord = !!$lp_y{'blank-below'};
221     $item_size[$blank_coord] *= (1.0 + $blankratio);
222
223     foreach my $coord (qw(0 1)) {
224         my $avail = $eff_paper_size[$coord] - $borders[$coord] * 2;
225         my $each = $item_size[$coord];
226         if (!$coord) {
227             $each += $gap_width;
228             $avail += $gap_width;
229         }
230         $laycount[$coord] = floor($avail / $each);
231         $laycount[$coord] >= 1 or return wontfit "cannot fit even one $coord";
232     }
233
234     if ($lp_y{'single-column'}) {
235         $laycount[0] = 1;
236     } else {
237         $laycount[0] >= 2 
238             or return wontfit "requested multi-column but only one";
239     }
240
241     $laycount[0] * $laycount[1] >= @strings
242         or return wontfit "layout fits too few @laycount";
243
244     print DEBUG " OK @laycount\n";
245     return 1;
246 }
247
248 sub do_layout ($) {
249     my ($fontsize) = @_;
250
251     print DEBUG "layout $fontsize\n";
252
253     @numbers_bbox = map { $_ * $fontsize } @numbers_1_bbox;
254     $gap_width = $gapratio * $fontsize *
255         ($nom_gap_1_bbox[2] - $nom_gap_1_bbox[0]);
256     @texts_bbox = map { $_ * $fontsize } @texts_1_bbox;
257
258     $core_size[0] =
259         ($numbers_bbox[2] - $numbers_bbox[0]) +
260         $gap_width +
261         ($texts_bbox[2] - $texts_bbox[0]);
262
263     $core_size[1] = max
264         $numbers_bbox[3] - $numbers_bbox[1],
265         $texts_bbox[3] - $texts_bbox[1];
266
267     return do_layout_recursive_search 0;
268 }
269
270 our $usesz;
271
272 sub determine_size_layout () {
273     my $minsz;
274     my $maxsz;
275
276     for (;;) {
277         my $trysz =
278             !defined $minsz ? 1 :
279             !defined $maxsz ? $minsz * 4 :
280             sqrt($minsz * $maxsz);
281
282         my $ok = do_layout $trysz;
283
284         if ($ok) { $minsz = $trysz; } else { $maxsz = $trysz; }
285
286         defined $minsz or die "cannot fit at even at ${trysz}pt\n";
287
288         if (defined $maxsz && ($maxsz / $minsz) < 1.01) {
289             $usesz = $minsz;
290             last;
291         }
292     }
293
294     do_layout $usesz or die;
295     
296     print DEBUG Dumper($usesz, \@lp_values, \@numbers_bbox,
297                        \@texts_bbox, $gap_width, \@core_size,
298                        \@item_size, $rotate_paper, \@eff_paper_size,
299                        \@laycount);
300 }
301
302 readstrings();
303 prepare_metrics();
304 determine_size_layout();