3 our $us = $0; $us =~ s#.*/##;
8 our $papersize = 'creditcard';
9 our $fontname = 'Courier';
10 our $fontname_num = 'Courier';
15 our @lp_options = ( [ 'blank-below', 'blank-to-right', ],
16 [ 'landscape', 'portrait' ],
17 [ 'single-column', 'multi-column', ] );
21 usage: $us [<options>] <foo-mail-pregen> <foo-mail-pregen-opts>...
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)
31 open DEBUG, ">/dev/null" or die $!;
35 sub badusage () { die "bad usage\n\n$usage"; }
37 sub mm2pt { map { $_ * 72.0 / 25.4 } @_; }
42 $r = $_ if !defined $r or $_ > $r;
50 $r = $_ if !defined $r or $_ < $r;
55 my $fontname_re = '[^()\\,]+';
56 my $dbl_re = '(?:[0-9]+\.?|[0-9]*\.[0-9]+)';
59 badusage unless @ARGV;
60 last unless $ARGV[0] =~ m/^-/;
67 } elsif (s/^-p($dbl_re)x($dbl_re)$//o) {
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) {
76 } elsif (s/^-b($dbl_re)x($dbl_re)$//o) {
78 } elsif (s/^-g($dbl_re)$//o) {
80 } elsif (s/^-b($dbl_re)$//o) {
83 open DEBUG, ">&STDERR" or die $!;
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);
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 "$_ ?";
102 @borders = mm2pt @borders;
104 @ARGV >= 2 or badusage;
111 open P, "-|", @ARGV or die $!;
114 m/^(\d+) (\S+)$/ or die "$_ ?";
115 $nlen = length($1) if length($1) > $nlen;
116 push @strings, [ $1, $2 ];
118 $!=0; $?=0; close P or die "$us: generator failed: $! $?\n";
132 print DEBUG " NO @_\n";
146 sub prepare_metrics () {
147 print DEBUG " prepare_metrics\n";
148 my $pchild = open GI, "-|"; defined $pchild or die $!;
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 ],
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 $!
162 close STDOUT or die $!;
165 my $gchild = open GO, "-|"; defined $gchild or die $!;
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";
172 foreach my $set (@sets) {
173 my ($ra, $fn, @s) = @$set;
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, $_;
179 m/^\%\%HiResBoundingBox: ($dbl_re) ($dbl_re) ($dbl_re) ($dbl_re)$/
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];
187 print STDERR "$us: warning: gs: $_" or die;
190 print DEBUG " metrics $fn @bb\n";
191 @$ra = map { $_ * 0.1 } @bb;
193 $!=0; $?=0; close GO or die "gs $! $?";
194 $!=0; $?=0; close GI or die "gs paste $! $?";
197 sub do_layout_recursive_search ($);
198 sub do_layout_recursive_search ($) {
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;
209 print DEBUG " try", (map { sprintf " %-10.10s", $_ } @lp_values), ":";
212 $lp_y{$_} = 1 foreach @lp_values;
215 ($paperpts[0] > $paperpts[1] # paper looks like landscape
216 xor $lp_y{'landscape'});
217 @eff_paper_size = !$rotate_paper ? @paperpts : reverse @paperpts;
219 @item_size = @core_size;
220 my $blank_coord = !!$lp_y{'blank-below'};
221 $item_size[$blank_coord] *= (1.0 + $blankratio);
223 foreach my $coord (qw(0 1)) {
224 my $avail = $eff_paper_size[$coord] - $borders[$coord] * 2;
225 my $each = $item_size[$coord];
228 $avail += $gap_width;
230 $laycount[$coord] = floor($avail / $each);
231 $laycount[$coord] >= 1 or return wontfit "cannot fit even one $coord";
234 if ($lp_y{'single-column'}) {
238 or return wontfit "requested multi-column but only one";
241 $laycount[0] * $laycount[1] >= @strings
242 or return wontfit "layout fits too few @laycount";
244 print DEBUG " OK @laycount\n";
251 print DEBUG "layout $fontsize\n";
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;
259 ($numbers_bbox[2] - $numbers_bbox[0]) +
261 ($texts_bbox[2] - $texts_bbox[0]);
264 $numbers_bbox[3] - $numbers_bbox[1],
265 $texts_bbox[3] - $texts_bbox[1];
267 return do_layout_recursive_search 0;
272 sub determine_size_layout () {
278 !defined $minsz ? 1 :
279 !defined $maxsz ? $minsz * 4 :
280 sqrt($minsz * $maxsz);
282 my $ok = do_layout $trysz;
284 if ($ok) { $minsz = $trysz; } else { $maxsz = $trysz; }
286 defined $minsz or die "cannot fit at even at ${trysz}pt\n";
288 if (defined $maxsz && ($maxsz / $minsz) < 1.01) {
294 do_layout $usesz or die;
296 print DEBUG Dumper($usesz, \@lp_values, \@numbers_bbox,
297 \@texts_bbox, $gap_width, \@core_size,
298 \@item_size, $rotate_paper, \@eff_paper_size,
304 determine_size_layout();