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