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;
27 our $always_reprocess;
31 return unless $verbose;
32 print $reportfh @_,"\n" or die $!;
36 return join ' ', ((stat _)[1,7,9,10]);
44 foreach my $path (@rulespath) {
45 my $script="$path/$key.rules";
46 $!=0; $@=''; my $f = do $script;
48 reportloaded("rules: loaded ",$script);
52 die "$! $? $script" unless $! == &ENOENT;
54 return $cache{$key}=undef;
57 sub processbdf ($$$$) {
58 my ($inbdf,$outbdf,$logfile,$what) = @_;
61 my ($w,$h,$xo,$yo,$y,$bitmap,$glyph);
67 if ($state eq 'bitmap' && $y==$h) {
71 my $key= sprintf "%s,%d,%d,%d,%d", $foundry,$w,$h,$xo,$yo;
72 my $rules= loadrules($key);
73 return 'no rules' if !$rules;
75 $modified += ($_ ne $glyph);
76 print $outbdf $_,"\n" or die $!
77 foreach split /\;/, $_; # /;
80 if ($state eq 'bitmap') {
81 m/^([0-9a-fA-F]+)\s+$/ or die $y;
82 length($1) == (($w+7 >> 3) << 1) or die "$1 $w";
87 if ($state eq 'idle' && m/^FOUNDRY\s+/) {
88 die if defined $foundry;
89 return 'foundry syntax' unless m/^FOUNDRY\s+\"(\w+)\"\s+/;
90 $foundry = $foundrymap{lc $1};
91 return 'no foundry' unless defined $foundry;
92 $_ = "FOUNDRY \"$foundry\"\n";
94 if ($state eq 'idle' && m/^FONT\s+/) {
96 return 'simple font name' unless m/^(FONT\s+)\-(\w+)\-/;
97 $font = $foundrymap{lc $2};
98 return 'no foundry' unless defined $font;
99 $_ = "FONT -$font-$'";
101 if ($state eq 'idle' && m/^STARTCHAR\s/) {
102 die unless defined $foundry;
103 die unless defined $font;
104 return 'foundry != font' unless $foundry eq $font;
108 if (($state eq 'idle' || $state eq 'startchar') &&
109 m/^([A-Z_]+)\s+(.*\S)\s+$/) {
112 if ($state eq 'startchar') {
113 if (m/^BBX\s+(\+?\d+)\s+(\+?\d+)\s+([-+]?\d+)\s+([-+]?\d+)\s+$/) {
114 ($w,$h,$xo,$yo) = ($1,$2,$3,$4);
116 if (m/^BITMAP\s+$/) {
117 die unless defined $w;
122 ($props{'CHARSET_REGISTRY'} =~ m/iso8859|utf|iso10646/i &&
123 $props{'ENCODING'} <= 127);
126 print $outbdf $_ or die $!;
128 die $! if $inbdf->error;
129 die $! if $outbdf->error or !$outbdf->flush;
130 die unless $state eq 'idle';
131 return 'no bdf data' # also special cased in processpcfgz
134 printf $logfile "%s: %d glyphs changed\n", $what, $modified
137 printf $logfile "%s: unchanged - no rules matched\n", $what
143 sub loadfoundries () {
145 foreach my $path (@rulespath) {
147 die "$path $!" unless $!==&ENOENT;
150 $foundryinfo .= statsummary().' '.$path."\0\n";
152 my $p = "$path/foundries";
153 my $f = new IO::File $p;
155 die "$p $!" unless $!==&ENOENT;
156 print $reportfh "foundries: none in $p\n" or die $! if $verbose;
163 m/^([.0-9A-Za-z]+)\s+([.0-9A-Za-z]+)$/ or die;
165 next if exists $foundrymap{$k};
168 $f->error and die $!;
169 reportloaded('foundries: loaded ',$p);
171 die "no foundry maps\n" unless %foundrymap;
174 sub processpcfgz ($$$$) {
175 my ($inpcfgz,$outpcfgz,$logfile,$what) = @_;
176 print $reportfh "processing $inpcfgz to $outpcfgz\n" if $verbose>=2;
177 my $current = new IO::File $inpcfgz, '<' or die "$inpcfgz $!";
178 my ($usread,$uswrite);
179 my ($reader,$writer);
182 foreach my $proc (['gunzip'], ['pcf2bdf'], [],
183 ['bdftopcf'],['',qw(gzip -1 -n)]) {
184 my $isfinal = (@$proc && $proc->[0] eq '');
186 $reader = new IO::Handle or die $!;
187 $writer = new IO::Handle or die $!;
188 new IO::Pipe($reader,$writer) or die $!;
192 $writer = new IO::File $outpcfgz, '>' or die "$outpcfgz $!";
195 my $exe = $proc->[0];
196 my $child = fork; defined $child or die $!;
198 open STDIN, '<&', $current or die $!;
199 open STDOUT, '>&', $writer or die $!;
201 close $reader or die $!;
203 close $usread or die $! if $usread;
204 close $uswrite or die $! if $uswrite;
205 exec $exe @$proc or die "$exe $!";
210 Stage => (!$exe ? 'self' : defined $usread ? 'out' : 'in'),
215 close $current or die $!;
216 close $writer or die $!;
224 my $r = processbdf($usread,$uswrite,$logfile,$what);
225 my $none = $r !~ m/^\d/;
227 $ch{'gunzip'}{SigOK}{13} = 1;
228 # ... we never care if pcf2bdf didn't want all the output from gunzip
231 # We're not going to install or use this so we can kill our
232 # input and output filters. We kill the input filters so that
233 # we don't risk waiting for them. (If the input filter died
234 # for some other reason then sending it a KILL now won't
235 # affect its exit status.) We kill the output filters (before
236 # we close the output pipe) so we don't produce messages from
237 # our output filters about corrupted data.
238 flush $uswrite or die $!;
240 foreach my $ch (@children) {
241 if ($ch->{Stage} ne 'self') {
242 kill 9, $ch->{Pid} or die "$ch->{Pid} $ch->{Exe} $!";
246 $ch{'pcf2bdf'}{SigOK}{13} = 1;
247 # ... we might not have read all the output from pcf2bdf, which is OK
249 close $uswrite or die $!;
250 close $usread or die $!;
252 foreach my $ch (@children) {
253 $!=0; waitpid($ch->{Pid}, 0) == $ch->{Pid} or
254 die "$ch->{Pid} $ch->{Exe} $!";
261 return !$st || $ch->{SigOK}{($st & ~128)};
264 if ($tolerate_bad_fonts &&
265 $r eq 'no bdf data' &&
266 $st_isok->($ch{'gunzip'}) &&
267 ($ch{'pcf2bdf'}{St} & ~128) == 6)
269 $r = "pcf2bdf failed ($ch{'pcf2bdf'}{St})";
270 print STDERR "warning: $r: skipping $inpcfgz\n";
271 $ch{'pcf2bdf'}{SigOK}{6} = 1;
273 foreach my $ch (@children) {
274 if (!$st_isok->($ch)) {
275 die "update-xfonts-traditional:".
276 " $ch->{Exe} [$ch->{Pid}] for $inpcfgz".
278 " (".(join ' ', keys %{ $ch->{SigOK} })." ok)\n";
284 sub processfontdir ($) {
286 if (!opendir FD, $fontdir) {
287 die "$fontdir $!" unless $!==&ENOENT;
292 if (!$always_reprocess) {
293 $olddone = do "$fontdir/$donefile";
295 die "$fontdir $! $@ " unless $!==&ENOENT;
296 } elsif ($olddone->{''} ne $foundryinfo) {
298 print $reportfh "reprocessing fonts (rules updated)\n" or die $!
299 unless $repro_reported++;
307 my $newdone = { '' => $foundryinfo };
308 my %outfiles; # bitmask: 1 /*exists*/ | 2 /*wanted*/
313 my $logpath = "$fontdir/$logfile";
314 unlink "$logpath" or $!==&ENOENT or die "$logpath $!";
315 my $log = new IO::File $logpath, ">>" or die "$logpath $!";
317 if (!$wanted_parallel) {
318 $wanted_parallel = Sys::CPU::cpu_count();
319 printf $reportfh "parallelism: %d\n", $wanted_parallel if $verbose>=2;
321 $wanted_parallel = 1 if $wanted_parallel < 1;
327 die $! unless defined $child;
328 my $job = $inprogress{$child};
329 die $child unless $job;
331 my $dent = $job->{Dent};
332 my $outdent = $job->{Outdent};
333 my $stats = $job->{Stats};
336 $outfiles{$outdent} |= 3;
337 } elsif ($?==2*256) {
339 die "update-xfonts-traditional: processing of".
340 " $fontdir/$dent [$child] failed ($?)\n";
342 $newdone->{$dent} = $stats;
344 delete $inprogress{$child};
347 flush $reportfh or die $!;
348 while (my $dent = scalar readdir FD) {
349 if ($dent =~ m/^\Q$fontprefix\E.*\.new$/) {
350 unlink "$fontdir/$dent" or $!==&ENOENT or die "$fontdir $dent $!";
353 next unless $dent =~ m/^[^.\/].*\.pcf\.gz$/;
354 print $reportfh "processing $fontdir...\n" or die $!
356 if ($dent =~ m/^\Q$fontprefix/) {
357 $outfiles{$dent} |= 1;
360 if (!stat "$fontdir/$dent") {
361 die "$fontdir $dent $!" unless $!==&ENOENT;
364 die "$fontdir $dent" unless -f _;
367 my $stats = statsummary();
368 my $tdone = $olddone->{$dent};
369 my $outdent = $fontprefix.$dent;
370 if (defined $tdone && $tdone eq $stats) {
371 $outfiles{$outdent} |= 2;
372 $newdone->{$dent} = $stats;
376 $await->() while scalar keys %inprogress >= $wanted_parallel;
378 my $child = fork; die unless defined $child;
380 my $r = processpcfgz("$fontdir/$dent",
381 "$fontdir/$outdent.new",
385 printf $log "%s: unchanged - %s\n", $dent, $r;
386 unlink "$fontdir/$outdent.new" or die "$fontdir $outdent $!";
389 rename "$fontdir/$outdent.new", "$fontdir/$outdent"
390 or die "$fontdir $outdent $!";
393 $log->flush or die "$logpath $!";
396 $inprogress{$child} = {
402 $await->() while scalar keys %inprogress;
405 foreach my $olddent (keys %outfiles) {
406 my $state = $outfiles{$olddent};
408 $affected++ if $state & 1;
411 unlink "$fontdir/$olddent" or die "$fontdir $olddent $!";
415 if (!stat "$fontdir/fonts.dir") {
416 $!==&ENOENT or die "$fontdir $!";
418 $!=0; $?=0; system 'mkfontdir',$fontdir;
419 die "$fontdir $? $!" if $? or $!;
422 unlink "$logpath" or die "$fontdir $!";
423 unlink "$fontdir/$donefile" or $!==&ENOENT or die "$fontdir $!";
425 my $newdoneh = new IO::File "$fontdir/$donefile.new", 'w'
426 or die "$fontdir $!";
427 print $newdoneh Dumper($newdone) or die "$fontdir $!";
428 close $newdoneh or die "$fontdir $!";
429 rename "$fontdir/$donefile.new","$fontdir/$donefile"
430 or die "$fontdir $!";
432 if ($reported || %$newdone || $affected || $updated) {
433 printf " processed %s: %d pcfs, %d affected, %d updated.\n",
434 $fontdir, (scalar keys %$newdone), $affected, $updated;
438 sub processfonttree ($) {
441 dangling_symlinks => 0,
445 processfontdir($File::Find::name);
450 our $stdin = new IO::File '<&STDIN' or die $!;
451 our $stdout = new IO::File '>&STDOUT' or die $!;
452 our $stderr = new IO::File '>&STDERR' or die $!;
456 'R|rules-include=s@' => \@rulespath,
457 'share-dir=s' => \$sharedir,
458 'verbose|v+' => \$verbose,
459 'j|parallel=i' => \$wanted_parallel,
460 'always-reprocess!' => \$always_reprocess,
461 'tolerate-bad-fonts!' => \$tolerate_bad_fonts,
464 sub define_mode ($$) {
465 my ($optname,$f) = @_;
466 push @options, $optname, sub {
467 die "only one mode may be specified\n" if defined $mode;
472 define_mode('bdf-filter', sub {
473 die "no arguments allowed with --bdf-filter\n" if @ARGV;
476 my $r = processbdf($stdin,$stdout,$reportfh,'stdin');
478 print STDERR "stdin not processed: $r\n";
483 define_mode('process-pcf', sub {
484 die "need source and destination pcf.gz\n" if @ARGV!=2;
486 my $r = processpcfgz($ARGV[0],$ARGV[1],$reportfh,"pcf");
488 print STDERR "pcf not processed: $r\n";
493 define_mode('process-fontdirs', sub {
494 die "need font dir(s)\n" unless @ARGV;
496 foreach my $d (@ARGV) {
501 define_mode('process-fonttrees', sub {
502 die "need font tree(s)\n" unless @ARGV;
504 foreach my $d (@ARGV) {
509 define_mode('update', sub {
510 die "no arguments allowed with --postinst\n" unless !@ARGV;
512 foreach my $d (@fonttrees) {
517 Getopt::Long::Configure(qw(bundling));
518 GetOptions(@options) or exit 127;
520 push @rulespath, "$sharedir/rules";
522 die "need a mode\n" unless $mode;