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,$logfh,$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 $logfh "%s: %d glyphs changed\n", $what, $modified
137 printf $logfh "%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;
176 sub filter_st_isok ($) {
179 return !$st || $ch->{SigOK}{($st & ~128)};
182 sub process_filter ($$$$$$$$) {
183 my ($rr, $input, $output,$what,$logfh,
184 $procs, $sigpipeok, $after_hook) = @_;
185 my ($reader,$writer);
187 my ($usread,$uswrite);
189 my $current = $input;
191 foreach my $proc (@$procs) {
192 my $isfinal = (@$proc && $proc->[0] eq '');
194 $reader = new IO::Handle or die $!;
195 $writer = new IO::Handle or die $!;
196 new IO::Pipe($reader,$writer) or die $!;
203 my $exe = $proc->[0];
204 my $child = fork; defined $child or die $!;
206 open STDIN, '<&', $current or die $!;
207 open STDOUT, '>&', $writer or die $!;
209 close $reader or die $!;
211 close $usread or die $! if $usread;
212 close $uswrite or die $! if $uswrite;
213 exec $exe @$proc or die "$exe $!";
218 Stage => (!$exe ? 'self' : defined $usread ? 'out' : 'in'),
223 close $current or die $!;
224 close $writer or die $!;
232 $$rr = processbdf($usread,$uswrite,$logfh,$what);
233 my $none = $$rr !~ m/^\d/;
235 $ch{$_}{SigOK}{13} = 1 foreach @$sigpipeok;
237 if ($none || !$$rr) {
238 # We're not going to install or use this so we can kill our
239 # input and output filters. We kill the input filters so that
240 # we don't risk waiting for them. (If the input filter died
241 # for some other reason then sending it a KILL now won't
242 # affect its exit status.) We kill the output filters (before
243 # we close the output pipe) so we don't produce messages from
244 # our output filters about corrupted data. And we tolerate
245 # SIGPIPE in all the input filters.
246 flush $uswrite or die $!;
248 my $filterkind = 'input';
249 foreach my $ch (@children) {
250 if ($ch->{Stage} ne 'self') {
251 kill 9, $ch->{Pid} or die "$ch->{Pid} $ch->{Exe} $!";
254 $filterkind = 'output';
257 if ($filterkind eq 'input') {
258 $ch->{SigOK}{13} = 1;
261 # ... we might not have read all the output from pcf2bdf, which is OK
264 close $uswrite or die $!;
265 close $usread or die $!;
267 foreach my $ch (@children) {
268 $!=0; waitpid($ch->{Pid}, 0) == $ch->{Pid} or
269 die "$ch->{Pid} $ch->{Exe} $!";
275 foreach my $ch (@children) {
276 if (!filter_st_isok($ch)) {
277 die "update-xfonts-traditional:".
278 " $ch->{Exe} [$ch->{Pid}] for $what".
280 " (".(join ' ', keys %{ $ch->{SigOK} })." ok)\n";
285 sub processpcfgz ($$$$) {
286 my ($inpcfgz,$outpcfgz,$logfh,$what) = @_;
287 print $reportfh "processing $inpcfgz to $outpcfgz\n" if $verbose>=2;
288 my $input = new IO::File $inpcfgz, '<' or die "$inpcfgz $!";
289 my $output = new IO::File $outpcfgz, '>' or die "$outpcfgz $!";
292 process_filter(\$r, $input, $output, $inpcfgz, $logfh,
294 ['gunzip'], ['pcf2bdf'],
296 ['bdftopcf'],['',qw(gzip -1 -n)]
299 # ... we never care if pcf2bdf didn't want all the data
301 if ($tolerate_bad_fonts &&
302 $r eq 'no bdf data' &&
303 filter_st_isok($ch{'gunzip'}) &&
304 ($ch{'pcf2bdf'}{St} & ~128) == 6)
306 $r = "pcf2bdf failed ($ch{'pcf2bdf'}{St})";
307 print STDERR "warning: $r: skipping $inpcfgz\n";
308 $ch{'pcf2bdf'}{SigOK}{6} = 1;
314 sub processfontdir ($) {
316 if (!opendir FD, $fontdir) {
317 die "$fontdir $!" unless $!==&ENOENT;
322 if (!$always_reprocess) {
323 $olddone = do "$fontdir/$donefile";
325 die "$fontdir $! $@ " unless $!==&ENOENT;
326 } elsif ($olddone->{''} ne $foundryinfo) {
328 print $reportfh "reprocessing fonts (rules updated)\n" or die $!
329 unless $repro_reported++;
337 my $newdone = { '' => $foundryinfo };
338 my %outfiles; # bitmask: 1 /*exists*/ | 2 /*wanted*/
343 my $logpath = "$fontdir/$logfile";
344 unlink "$logpath" or $!==&ENOENT or die "$logpath $!";
345 my $logfh = new IO::File $logpath, ">>" or die "$logpath $!";
347 if (!$wanted_parallel) {
348 $wanted_parallel = Sys::CPU::cpu_count();
349 printf $reportfh "parallelism: %d\n", $wanted_parallel if $verbose>=2;
351 $wanted_parallel = 1 if $wanted_parallel < 1;
357 die $! unless defined $child;
358 my $job = $inprogress{$child};
359 die $child unless $job;
361 my $dent = $job->{Dent};
362 my $outdent = $job->{Outdent};
363 my $stats = $job->{Stats};
366 $outfiles{$outdent} |= 3;
367 } elsif ($?==2*256) {
369 die "update-xfonts-traditional: processing of".
370 " $fontdir/$dent [$child] failed ($?)\n";
372 $newdone->{$dent} = $stats;
374 delete $inprogress{$child};
377 flush $reportfh or die $!;
378 while (my $dent = scalar readdir FD) {
379 if ($dent =~ m/^\Q$fontprefix\E.*\.new$/) {
380 unlink "$fontdir/$dent" or $!==&ENOENT or die "$fontdir $dent $!";
383 next unless $dent =~ m/^[^.\/].*\.pcf\.gz$/;
384 print $reportfh "processing $fontdir...\n" or die $!
386 if ($dent =~ m/^\Q$fontprefix/) {
387 $outfiles{$dent} |= 1;
390 if (!stat "$fontdir/$dent") {
391 die "$fontdir $dent $!" unless $!==&ENOENT;
394 die "$fontdir $dent" unless -f _;
397 my $stats = statsummary();
398 my $tdone = $olddone->{$dent};
399 my $outdent = $fontprefix.$dent;
400 if (defined $tdone && $tdone eq $stats) {
401 $outfiles{$outdent} |= 2;
402 $newdone->{$dent} = $stats;
406 $await->() while scalar keys %inprogress >= $wanted_parallel;
408 my $child = fork; die unless defined $child;
410 my $r = processpcfgz("$fontdir/$dent",
411 "$fontdir/$outdent.new",
415 printf $logfh "%s: unchanged - %s\n", $dent, $r;
416 unlink "$fontdir/$outdent.new" or die "$fontdir $outdent $!";
419 rename "$fontdir/$outdent.new", "$fontdir/$outdent"
420 or die "$fontdir $outdent $!";
423 $logfh->flush or die "$logpath $!";
426 $inprogress{$child} = {
432 $await->() while scalar keys %inprogress;
435 foreach my $olddent (keys %outfiles) {
436 my $state = $outfiles{$olddent};
438 $affected++ if $state & 1;
441 unlink "$fontdir/$olddent" or die "$fontdir $olddent $!";
445 if (!stat "$fontdir/fonts.dir") {
446 $!==&ENOENT or die "$fontdir $!";
448 $!=0; $?=0; system 'mkfontdir',$fontdir;
449 die "$fontdir $? $!" if $? or $!;
452 unlink "$logpath" or die "$fontdir $!";
453 unlink "$fontdir/$donefile" or $!==&ENOENT or die "$fontdir $!";
455 my $newdoneh = new IO::File "$fontdir/$donefile.new", 'w'
456 or die "$fontdir $!";
457 print $newdoneh Dumper($newdone) or die "$fontdir $!";
458 close $newdoneh or die "$fontdir $!";
459 rename "$fontdir/$donefile.new","$fontdir/$donefile"
460 or die "$fontdir $!";
462 if ($reported || %$newdone || $affected || $updated) {
463 printf " processed %s: %d pcfs, %d affected, %d updated.\n",
464 $fontdir, (scalar keys %$newdone), $affected, $updated;
468 sub processfonttree ($) {
471 dangling_symlinks => 0,
475 processfontdir($File::Find::name);
480 our $stdin = new IO::File '<&STDIN' or die $!;
481 our $stdout = new IO::File '>&STDOUT' or die $!;
482 our $stderr = new IO::File '>&STDERR' or die $!;
486 'R|rules-include=s@' => \@rulespath,
487 'share-dir=s' => \$sharedir,
488 'verbose|v+' => \$verbose,
489 'j|parallel=i' => \$wanted_parallel,
490 'always-reprocess!' => \$always_reprocess,
491 'tolerate-bad-fonts!' => \$tolerate_bad_fonts,
494 sub define_mode ($$) {
495 my ($optname,$f) = @_;
496 push @options, $optname, sub {
497 die "only one mode may be specified\n" if defined $mode;
502 define_mode('bdf-filter', sub {
503 die "no arguments allowed with --bdf-filter\n" if @ARGV;
506 my $r = processbdf($stdin,$stdout,$reportfh,'stdin');
508 print STDERR "stdin not processed: $r\n";
513 define_mode('process-pcf', sub {
514 die "need source and destination pcf.gz\n" if @ARGV!=2;
516 my $r = processpcfgz($ARGV[0],$ARGV[1],$reportfh,"pcf");
518 print STDERR "pcf not processed: $r\n";
523 define_mode('process-fontdirs', sub {
524 die "need font dir(s)\n" unless @ARGV;
526 foreach my $d (@ARGV) {
531 define_mode('process-fonttrees', sub {
532 die "need font tree(s)\n" unless @ARGV;
534 foreach my $d (@ARGV) {
539 define_mode('update', sub {
540 die "no arguments allowed with --postinst\n" unless !@ARGV;
542 foreach my $d (@fonttrees) {
547 Getopt::Long::Configure(qw(bundling));
548 GetOptions(@options) or exit 127;
550 push @rulespath, "$sharedir/rules";
552 die "need a mode\n" unless $mode;