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--";
29 return unless $verbose;
30 print $reportfh @_,"\n" or die $!;
34 return join ' ', ((stat _)[1,7,9,10]);
42 foreach my $path (@rulespath) {
43 my $script="$path/$key.rules";
44 $!=0; $@=''; my $f = do $script;
46 reportloaded("rules: loaded ",$script);
50 die "$! $? $script" unless $! == &ENOENT;
52 return $cache{$key}=undef;
55 sub processbdf ($$$$) {
56 my ($inbdf,$outbdf,$logfile,$what) = @_;
59 my ($w,$h,$xo,$yo,$y,$bitmap,$glyph);
65 if ($state eq 'bitmap' && $y==$h) {
69 my $key= sprintf "%s,%d,%d,%d,%d", $foundry,$w,$h,$xo,$yo;
70 my $rules= loadrules($key);
71 return 'no rules' if !$rules;
73 $modified += ($_ ne $glyph);
74 print $outbdf $_,"\n" or die $!
75 foreach split /\;/, $_; # /;
78 if ($state eq 'bitmap') {
79 m/^([0-9a-fA-F]+)\s+$/ or die $y;
80 length($1) == (($w+7 >> 3) << 1) or die "$1 $w";
85 if ($state eq 'idle' && m/^FOUNDRY\s+/) {
86 die if defined $foundry;
87 return 'foundry syntax' unless m/^FOUNDRY\s+\"(\w+)\"\s+/;
88 $foundry = $foundrymap{lc $1};
89 return 'no foundry' unless defined $foundry;
90 $_ = "FOUNDRY \"$foundry\"\n";
92 if ($state eq 'idle' && m/^FONT\s+/) {
94 return 'simple font name' unless m/^(FONT\s+)\-(\w+)\-/;
95 $font = $foundrymap{lc $2};
96 return 'no foundry' unless defined $font;
97 $_ = "FONT -$font-$'";
99 if ($state eq 'idle' && m/^STARTCHAR\s/) {
100 die unless defined $foundry;
101 die unless defined $font;
102 return 'foundry != font' unless $foundry eq $font;
106 if (($state eq 'idle' || $state eq 'startchar') &&
107 m/^([A-Z_]+)\s+(.*\S)\s+$/) {
110 if ($state eq 'startchar') {
111 if (m/^BBX\s+(\+?\d+)\s+(\+?\d+)\s+([-+]?\d+)\s+([-+]?\d+)\s+$/) {
112 ($w,$h,$xo,$yo) = ($1,$2,$3,$4);
114 if (m/^BITMAP\s+$/) {
115 die unless defined $w;
120 ($props{'CHARSET_REGISTRY'} =~ m/iso8859|utf|iso10646/i &&
121 $props{'ENCODING'} <= 127);
124 print $outbdf $_ or die $!;
126 die $! if $inbdf->error;
127 die $! if $outbdf->error or !$outbdf->flush;
128 die unless $state eq 'idle';
129 return 'no bdf data' # also special cased in processpcfgz
132 printf $logfile "%s: %d glyphs changed\n", $what, $modified
135 printf $logfile "%s: unchanged - no rules matched\n", $what
141 sub loadfoundries () {
143 foreach my $path (@rulespath) {
145 die "$path $!" unless $!==&ENOENT;
148 $foundryinfo .= statsummary().' '.$path."\0\n";
150 my $p = "$path/foundries";
151 my $f = new IO::File $p;
153 die "$p $!" unless $!==&ENOENT;
154 print $reportfh "foundries: none in $p\n" or die $! if $verbose;
161 m/^(\w+)\s+(\w+)$/ or die;
163 next if exists $foundrymap{$k};
166 $f->error and die $!;
167 reportloaded('foundries: loaded ',$p);
169 die "no foundry maps\n" unless %foundrymap;
172 sub processpcfgz ($$$$) {
173 my ($inpcfgz,$outpcfgz,$logfile,$what) = @_;
174 print $reportfh "processing $inpcfgz to $outpcfgz\n" if $verbose>=2;
175 my $current = new IO::File $inpcfgz, '<' or die "$inpcfgz $!";
176 my ($usread,$uswrite);
177 my ($reader,$writer);
180 foreach my $proc (['gunzip'], ['pcf2bdf'], [],
181 ['bdftopcf'],['',qw(gzip -1 -n)]) {
182 my $isfinal = (@$proc && $proc->[0] eq '');
184 $reader = new IO::Handle or die $!;
185 $writer = new IO::Handle or die $!;
186 new IO::Pipe($reader,$writer) or die $!;
190 $writer = new IO::File $outpcfgz, '>' or die "$outpcfgz $!";
193 my $exe = $proc->[0];
194 my $child = fork; defined $child or die $!;
196 open STDIN, '<&', $current or die $!;
197 open STDOUT, '>&', $writer or die $!;
199 close $reader or die $!;
201 close $usread or die $! if $usread;
202 close $uswrite or die $! if $uswrite;
203 exec $exe @$proc or die "$exe $!";
208 Stage => (!$exe ? 'self' : defined $usread ? 'out' : 'in'),
213 close $current or die $!;
214 close $writer or die $!;
222 my $r = processbdf($usread,$uswrite,$logfile,$what);
223 my $none = $r !~ m/^\d/;
225 $ch{'gunzip'}{SigOK}{13} = 1;
226 # ... we never care if pcf2bdf didn't want all the output from gunzip
229 # We're not going to install or use this so we can kill our
230 # input and output filters. We kill the input filters so that
231 # we don't risk waiting for them. (If the input filter died
232 # for some other reason then sending it a KILL now won't
233 # affect its exit status.) We kill the output filters (before
234 # we close the output pipe) so we don't produce messages from
235 # our output filters about corrupted data.
236 flush $uswrite or die $!;
238 foreach my $ch (@children) {
239 if ($ch->{Stage} ne 'self') {
240 kill 9, $ch->{Pid} or die "$ch->{Pid} $ch->{Exe} $!";
244 $ch{'pcf2bdf'}{SigOK}{13} = 1;
245 # ... we might not have read all the output from pcf2bdf, which is OK
247 close $uswrite or die $!;
248 close $usread or die $!;
250 foreach my $ch (@children) {
251 $!=0; waitpid($ch->{Pid}, 0) == $ch->{Pid} or
252 die "$ch->{Pid} $ch->{Exe} $!";
259 return !$st || $ch->{SigOK}{($st & ~128)};
262 foreach my $ch (@children) {
263 if (!$st_isok->($ch)) {
264 die "update-xfonts-traditional:".
265 " $ch->{Exe} [$ch->{Pid}] for $inpcfgz".
267 " (".(join ' ', keys %{ $ch->{SigOK} })." ok)\n";
273 sub processfontdir ($) {
275 if (!opendir FD, $fontdir) {
276 die "$fontdir $!" unless $!==&ENOENT;
280 my $olddone = do "$fontdir/$donefile";
282 die "$fontdir $! $@ " unless $!==&ENOENT;
283 } elsif ($olddone->{''} ne $foundryinfo) {
285 print $reportfh "reprocessing fonts (rules updated)\n" or die $!
286 unless $repro_reported++;
293 my $newdone = { '' => $foundryinfo };
294 my %outfiles; # bitmask: 1 /*exists*/ | 2 /*wanted*/
299 my $logpath = "$fontdir/$logfile";
300 unlink "$logpath" or $!==&ENOENT or die "$logpath $!";
301 my $log = new IO::File $logpath, ">>" or die "$logpath $!";
303 if (!$wanted_parallel) {
304 $wanted_parallel = Sys::CPU::cpu_count();
305 printf $reportfh "parallelism: %d\n", $wanted_parallel if $verbose>=2;
307 $wanted_parallel = 1 if $wanted_parallel < 1;
313 die $! unless defined $child;
314 my $job = $inprogress{$child};
315 die $child unless $job;
317 my $dent = $job->{Dent};
318 my $outdent = $job->{Outdent};
319 my $stats = $job->{Stats};
322 $outfiles{$outdent} |= 3;
323 } elsif ($?==2*256) {
325 die "update-xfonts-traditional: processing of".
326 " $fontdir/$dent [$child] failed ($?)\n";
328 $newdone->{$dent} = $stats;
330 delete $inprogress{$child};
333 flush $reportfh or die $!;
334 while (my $dent = scalar readdir FD) {
335 if ($dent =~ m/^\Q$fontprefix\E.*\.new$/) {
336 unlink "$fontdir/$dent" or $!==&ENOENT or die "$fontdir $dent $!";
339 next unless $dent =~ m/^[^.\/].*\.pcf\.gz$/;
340 print $reportfh "processing $fontdir...\n" or die $!
342 if ($dent =~ m/^\Q$fontprefix/) {
343 $outfiles{$dent} |= 1;
346 if (!stat "$fontdir/$dent") {
347 die "$fontdir $dent $!" unless $!==&ENOENT;
350 die "$fontdir $dent" unless -f _;
353 my $stats = statsummary();
354 my $tdone = $olddone->{$dent};
355 my $outdent = $fontprefix.$dent;
356 if (defined $tdone && $tdone eq $stats) {
357 $outfiles{$outdent} |= 2;
358 $newdone->{$dent} = $stats;
362 $await->() while scalar keys %inprogress >= $wanted_parallel;
364 my $child = fork; die unless defined $child;
366 my $r = processpcfgz("$fontdir/$dent",
367 "$fontdir/$outdent.new",
371 printf $log "%s: unchanged - %s\n", $dent, $r;
372 unlink "$fontdir/$outdent.new" or die "$fontdir $outdent $!";
375 rename "$fontdir/$outdent.new", "$fontdir/$outdent"
376 or die "$fontdir $outdent $!";
379 $log->flush or die "$logpath $!";
382 $inprogress{$child} = {
388 $await->() while scalar keys %inprogress;
391 foreach my $olddent (keys %outfiles) {
392 my $state = $outfiles{$olddent};
394 $affected++ if $state & 1;
397 unlink "$fontdir/$olddent" or die "$fontdir $olddent $!";
401 if (!stat "$fontdir/fonts.dir") {
402 $!==&ENOENT or die "$fontdir $!";
404 $!=0; $?=0; system 'mkfontdir',$fontdir;
405 die "$fontdir $? $!" if $? or $!;
408 unlink "$logpath" or die "$fontdir $!";
409 unlink "$fontdir/$donefile" or $!==&ENOENT or die "$fontdir $!";
411 my $newdoneh = new IO::File "$fontdir/$donefile.new", 'w'
412 or die "$fontdir $!";
413 print $newdoneh Dumper($newdone) or die "$fontdir $!";
414 close $newdoneh or die "$fontdir $!";
415 rename "$fontdir/$donefile.new","$fontdir/$donefile"
416 or die "$fontdir $!";
418 if ($reported || %$newdone || $affected || $updated) {
419 printf " processed %s: %d pcfs, %d affected, %d updated.\n",
420 $fontdir, (scalar keys %$newdone), $affected, $updated;
424 sub processfonttree ($) {
427 dangling_symlinks => 0,
431 processfontdir($File::Find::name);
436 our $stdin = new IO::File '<&STDIN' or die $!;
437 our $stdout = new IO::File '>&STDOUT' or die $!;
438 our $stderr = new IO::File '>&STDERR' or die $!;
442 'R|rules-include=s@' => \@rulespath,
443 'share-dir=s' => \$sharedir,
444 'verbose|v+' => \$verbose,
445 'j|parallel=i' => \$wanted_parallel,
448 sub define_mode ($$) {
449 my ($optname,$f) = @_;
450 push @options, $optname, sub {
451 die "only one mode may be specified\n" if defined $mode;
456 define_mode('bdf-filter', sub {
457 die "no arguments allowed with --bdf-filter\n" if @ARGV;
460 my $r = processbdf($stdin,$stdout,$reportfh,'stdin');
462 print STDERR "stdin not processed: $r\n";
467 define_mode('process-pcf', sub {
468 die "need source and destination pcf.gz\n" if @ARGV!=2;
470 my $r = processpcfgz($ARGV[0],$ARGV[1],$reportfh,"pcf");
472 print STDERR "pcf not processed: $r\n";
477 define_mode('process-fontdirs', sub {
478 die "need font dir(s)\n" unless @ARGV;
480 foreach my $d (@ARGV) {
485 define_mode('process-fonttrees', sub {
486 die "need font tree(s)\n" unless @ARGV;
488 foreach my $d (@ARGV) {
493 define_mode('update', sub {
494 die "no arguments allowed with --postinst\n" unless !@ARGV;
496 foreach my $d (@fonttrees) {
501 Getopt::Long::Configure(qw(bundling));
502 GetOptions(@options) or exit 127;
504 push @rulespath, "$sharedir/rules";
506 die "need a mode\n" unless $mode;