6 use File::Glob qw(:glob);
12 our $prefix="/usr/local";
13 our $package='xfonts-traditional';
14 our $sharedir="$prefix/share/$package";
15 our @fonttrees=qw(/usr/share/fonts/X11 /usr/local/share/fonts/X11);
16 our $donefile="$package.done";
17 our $logfile="$package.log";
18 our $fontprefix="trad--";
26 our $tolerate_bad_fonts=1;
30 return unless $verbose;
31 print $reportfh @_,"\n" or die $!;
35 return join ' ', ((stat _)[1,7,9,10]);
43 foreach my $path (@rulespath) {
44 my $script="$path/$key.rules";
45 $!=0; $@=''; my $f = do $script;
47 reportloaded("rules: loaded ",$script);
51 die "$! $? $script" unless $! == &ENOENT;
53 return $cache{$key}=undef;
56 sub processbdf ($$$$) {
57 my ($inbdf,$outbdf,$logfile,$what) = @_;
60 my ($w,$h,$xo,$yo,$y,$bitmap,$glyph);
66 if ($state eq 'bitmap' && $y==$h) {
70 my $key= sprintf "%s,%d,%d,%d,%d", $foundry,$w,$h,$xo,$yo;
71 my $rules= loadrules($key);
72 return 'no rules' if !$rules;
74 $modified += ($_ ne $glyph);
75 print $outbdf $_,"\n" or die $!
76 foreach split /\;/, $_; # /;
79 if ($state eq 'bitmap') {
80 m/^([0-9a-fA-F]+)\s+$/ or die $y;
81 length($1) == (($w+7 >> 3) << 1) or die "$1 $w";
86 if ($state eq 'idle' && m/^FOUNDRY\s+/) {
87 die if defined $foundry;
88 return 'foundry syntax' unless m/^FOUNDRY\s+\"(\w+)\"\s+/;
89 $foundry = $foundrymap{lc $1};
90 return 'no foundry' unless defined $foundry;
91 $_ = "FOUNDRY \"$foundry\"\n";
93 if ($state eq 'idle' && m/^FONT\s+/) {
95 return 'simple font name' unless m/^(FONT\s+)\-(\w+)\-/;
96 $font = $foundrymap{lc $2};
97 return 'no foundry' unless defined $font;
98 $_ = "FONT -$font-$'";
100 if ($state eq 'idle' && m/^STARTCHAR\s/) {
101 die unless defined $foundry;
102 die unless defined $font;
103 return 'foundry != font' unless $foundry eq $font;
107 if (($state eq 'idle' || $state eq 'startchar') &&
108 m/^([A-Z_]+)\s+(.*\S)\s+$/) {
111 if ($state eq 'startchar') {
112 if (m/^BBX\s+(\+?\d+)\s+(\+?\d+)\s+([-+]?\d+)\s+([-+]?\d+)\s+$/) {
113 ($w,$h,$xo,$yo) = ($1,$2,$3,$4);
115 if (m/^BITMAP\s+$/) {
116 die unless defined $w;
121 ($props{'CHARSET_REGISTRY'} =~ m/iso8859|utf|iso10646/i &&
122 $props{'ENCODING'} <= 127);
125 print $outbdf $_ or die $!;
127 die $! if $inbdf->error;
128 die $! if $outbdf->error or !$outbdf->flush;
129 die unless $state eq 'idle';
130 return 'no bdf data' # also special cased in processpcfgz
133 printf $logfile "%s: %d glyphs changed\n", $what, $modified
136 printf $logfile "%s: unchanged - no rules matched\n", $what
142 sub loadfoundries () {
144 foreach my $path (@rulespath) {
146 die "$path $!" unless $!==&ENOENT;
149 $foundryinfo .= statsummary().' '.$path."\0\n";
151 my $p = "$path/foundries";
152 my $f = new IO::File $p;
154 die "$p $!" unless $!==&ENOENT;
155 print $reportfh "foundries: none in $p\n" or die $! if $verbose;
162 m/^(\w+)\s+(\w+)$/ or die;
164 next if exists $foundrymap{$k};
167 $f->error and die $!;
168 reportloaded('foundries: loaded ',$p);
170 die "no foundry maps\n" unless %foundrymap;
173 sub processpcfgz ($$$$) {
174 my ($inpcfgz,$outpcfgz,$logfile,$what) = @_;
175 print $reportfh "processing $inpcfgz to $outpcfgz\n" if $verbose>=2;
176 my $current = new IO::File $inpcfgz, '<' or die "$inpcfgz $!";
177 my ($usread,$uswrite);
178 my ($reader,$writer);
181 foreach my $proc (['gunzip'], ['pcf2bdf'], [],
182 ['bdftopcf'],['',qw(gzip -1 -n)]) {
183 my $isfinal = (@$proc && $proc->[0] eq '');
185 $reader = new IO::Handle or die $!;
186 $writer = new IO::Handle or die $!;
187 new IO::Pipe($reader,$writer) or die $!;
191 $writer = new IO::File $outpcfgz, '>' or die "$outpcfgz $!";
194 my $exe = $proc->[0];
195 my $child = fork; defined $child or die $!;
197 open STDIN, '<&', $current or die $!;
198 open STDOUT, '>&', $writer or die $!;
200 close $reader or die $!;
202 close $usread or die $! if $usread;
203 close $uswrite or die $! if $uswrite;
204 exec $exe @$proc or die "$exe $!";
209 Stage => (!$exe ? 'self' : defined $usread ? 'out' : 'in'),
214 close $current or die $!;
215 close $writer or die $!;
223 my $r = processbdf($usread,$uswrite,$logfile,$what);
224 my $none = $r !~ m/^\d/;
226 $ch{'gunzip'}{SigOK}{13} = 1;
227 # ... we never care if pcf2bdf didn't want all the output from gunzip
230 # We're not going to install or use this so we can kill our
231 # input and output filters. We kill the input filters so that
232 # we don't risk waiting for them. (If the input filter died
233 # for some other reason then sending it a KILL now won't
234 # affect its exit status.) We kill the output filters (before
235 # we close the output pipe) so we don't produce messages from
236 # our output filters about corrupted data.
237 flush $uswrite or die $!;
239 foreach my $ch (@children) {
240 if ($ch->{Stage} ne 'self') {
241 kill 9, $ch->{Pid} or die "$ch->{Pid} $ch->{Exe} $!";
245 $ch{'pcf2bdf'}{SigOK}{13} = 1;
246 # ... we might not have read all the output from pcf2bdf, which is OK
248 close $uswrite or die $!;
249 close $usread or die $!;
251 foreach my $ch (@children) {
252 $!=0; waitpid($ch->{Pid}, 0) == $ch->{Pid} or
253 die "$ch->{Pid} $ch->{Exe} $!";
260 return !$st || $ch->{SigOK}{($st & ~128)};
263 if ($tolerate_bad_fonts &&
264 $r eq 'no bdf data' &&
265 $st_isok->($ch{'gunzip'}) &&
266 ($ch{'pcf2bdf'}{St} & ~128) == 6)
268 $r = "pcf2bdf failed ($ch{'pcf2bdf'}{St})";
269 print STDERR "warning: $r: skipping $inpcfgz\n";
270 $ch{'pcf2bdf'}{SigOK}{6} = 1;
272 foreach my $ch (@children) {
273 if (!$st_isok->($ch)) {
274 die "update-xfonts-traditional:".
275 " $ch->{Exe} [$ch->{Pid}] for $inpcfgz".
277 " (".(join ' ', keys %{ $ch->{SigOK} })." ok)\n";
283 sub processfontdir ($) {
285 if (!opendir FD, $fontdir) {
286 die "$fontdir $!" unless $!==&ENOENT;
290 my $olddone = do "$fontdir/$donefile";
292 die "$fontdir $! $@ " unless $!==&ENOENT;
293 } elsif ($olddone->{''} ne $foundryinfo) {
295 print $reportfh "reprocessing fonts (rules updated)\n" or die $!
296 unless $repro_reported++;
303 my $newdone = { '' => $foundryinfo };
304 my %outfiles; # bitmask: 1 /*exists*/ | 2 /*wanted*/
309 my $logpath = "$fontdir/$logfile";
310 unlink "$logpath" or $!==&ENOENT or die "$logpath $!";
311 my $log = new IO::File $logpath, ">>" or die "$logpath $!";
313 if (!$wanted_parallel) {
314 $wanted_parallel = Sys::CPU::cpu_count();
315 printf $reportfh "parallelism: %d\n", $wanted_parallel if $verbose>=2;
317 $wanted_parallel = 1 if $wanted_parallel < 1;
323 die $! unless defined $child;
324 my $job = $inprogress{$child};
325 die $child unless $job;
327 my $dent = $job->{Dent};
328 my $outdent = $job->{Outdent};
329 my $stats = $job->{Stats};
332 $outfiles{$outdent} |= 3;
333 } elsif ($?==2*256) {
335 die "update-xfonts-traditional: processing of".
336 " $fontdir/$dent [$child] failed ($?)\n";
338 $newdone->{$dent} = $stats;
340 delete $inprogress{$child};
343 flush $reportfh or die $!;
344 while (my $dent = scalar readdir FD) {
345 if ($dent =~ m/^\Q$fontprefix\E.*\.new$/) {
346 unlink "$fontdir/$dent" or $!==&ENOENT or die "$fontdir $dent $!";
349 next unless $dent =~ m/^[^.\/].*\.pcf\.gz$/;
350 print $reportfh "processing $fontdir...\n" or die $!
352 if ($dent =~ m/^\Q$fontprefix/) {
353 $outfiles{$dent} |= 1;
356 if (!stat "$fontdir/$dent") {
357 die "$fontdir $dent $!" unless $!==&ENOENT;
360 die "$fontdir $dent" unless -f _;
363 my $stats = statsummary();
364 my $tdone = $olddone->{$dent};
365 my $outdent = $fontprefix.$dent;
366 if (defined $tdone && $tdone eq $stats) {
367 $outfiles{$outdent} |= 2;
368 $newdone->{$dent} = $stats;
372 $await->() while scalar keys %inprogress >= $wanted_parallel;
374 my $child = fork; die unless defined $child;
376 my $r = processpcfgz("$fontdir/$dent",
377 "$fontdir/$outdent.new",
381 printf $log "%s: unchanged - %s\n", $dent, $r;
382 unlink "$fontdir/$outdent.new" or die "$fontdir $outdent $!";
385 rename "$fontdir/$outdent.new", "$fontdir/$outdent"
386 or die "$fontdir $outdent $!";
389 $log->flush or die "$logpath $!";
392 $inprogress{$child} = {
398 $await->() while scalar keys %inprogress;
401 foreach my $olddent (keys %outfiles) {
402 my $state = $outfiles{$olddent};
404 $affected++ if $state & 1;
407 unlink "$fontdir/$olddent" or die "$fontdir $olddent $!";
411 if (!stat "$fontdir/fonts.dir") {
412 $!==&ENOENT or die "$fontdir $!";
414 $!=0; $?=0; system 'mkfontdir',$fontdir;
415 die "$fontdir $? $!" if $? or $!;
418 unlink "$logpath" or die "$fontdir $!";
419 unlink "$fontdir/$donefile" or $!==&ENOENT or die "$fontdir $!";
421 my $newdoneh = new IO::File "$fontdir/$donefile.new", 'w'
422 or die "$fontdir $!";
423 print $newdoneh Dumper($newdone) or die "$fontdir $!";
424 close $newdoneh or die "$fontdir $!";
425 rename "$fontdir/$donefile.new","$fontdir/$donefile"
426 or die "$fontdir $!";
428 if ($reported || %$newdone || $affected || $updated) {
429 printf " processed %s: %d pcfs, %d affected, %d updated.\n",
430 $fontdir, (scalar keys %$newdone), $affected, $updated;
434 sub processfonttree ($) {
437 dangling_symlinks => 0,
441 processfontdir($File::Find::name);
446 our $stdin = new IO::File '<&STDIN' or die $!;
447 our $stdout = new IO::File '>&STDOUT' or die $!;
448 our $stderr = new IO::File '>&STDERR' or die $!;
452 'R|rules-include=s@' => \@rulespath,
453 'share-dir=s' => \$sharedir,
454 'verbose|v+' => \$verbose,
455 'j|parallel=i' => \$wanted_parallel,
456 'tolerate-bad-fonts!' => \$tolerate_bad_fonts,
459 sub define_mode ($$) {
460 my ($optname,$f) = @_;
461 push @options, $optname, sub {
462 die "only one mode may be specified\n" if defined $mode;
467 define_mode('bdf-filter', sub {
468 die "no arguments allowed with --bdf-filter\n" if @ARGV;
471 my $r = processbdf($stdin,$stdout,$reportfh,'stdin');
473 print STDERR "stdin not processed: $r\n";
478 define_mode('process-pcf', sub {
479 die "need source and destination pcf.gz\n" if @ARGV!=2;
481 my $r = processpcfgz($ARGV[0],$ARGV[1],$reportfh,"pcf");
483 print STDERR "pcf not processed: $r\n";
488 define_mode('process-fontdirs', sub {
489 die "need font dir(s)\n" unless @ARGV;
491 foreach my $d (@ARGV) {
496 define_mode('process-fonttrees', sub {
497 die "need font tree(s)\n" unless @ARGV;
499 foreach my $d (@ARGV) {
504 define_mode('update', sub {
505 die "no arguments allowed with --postinst\n" unless !@ARGV;
507 foreach my $d (@fonttrees) {
512 Getopt::Long::Configure(qw(bundling));
513 GetOptions(@options) or exit 127;
515 push @rulespath, "$sharedir/rules";
517 die "need a mode\n" unless $mode;