chiark / gitweb /
better about when to update fonts.dir
[xfonts-traditional] / update-xfonts-traditional
... / ...
CommitLineData
1#!/usr/bin/perl -w
2use strict;
3use POSIX;
4use IO::File;
5use Getopt::Long;
6use File::Glob qw(:glob);
7use Data::Dumper;
8use IO::Pipe;
9use File::Find;
10
11our $prefix="/usr/local";
12our $package='xfonts-traditional';
13our $sharedir="$prefix/share/$package";
14our @fonttrees=qw(/usr/share/fonts/X11 /usr/local/share/fonts/X11);
15our $donefile="$package.done";
16our $logfile="$package.log";
17our $fontprefix="trad--";
18our @rulespath;
19our $mode;
20our %foundrymap;
21our $verbose=0;
22our $reportfh;
23
24sub reportloaded {
25 return unless $verbose;
26 print $reportfh @_,"\n" or die $!;
27}
28
29sub loadrules ($) {
30 my ($key) = @_;
31 our %cache;
32 my $fc=$cache{$key};
33 return $fc if $fc;
34 foreach my $path (@rulespath) {
35 my $script="$path/$key.rules";
36 $!=0; $@=''; my $f = do $script;
37 if (defined $f) {
38 reportloaded("rules: loaded ",$script);
39 $cache{$key}=$f;
40 return $f;
41 }
42 die "$! $? $script" unless $! == &ENOENT;
43 }
44 return $cache{$key}=undef;
45}
46
47sub processbdf ($$$$) {
48 my ($inbdf,$outbdf,$logfile,$what) = @_;
49 my $state='idle';
50 my ($foundry,$font);
51 my ($w,$h,$xo,$yo,$y,$bitmap,$glyph);
52 my $modified=0;
53 while (<$inbdf>) {
54 if ($state eq 'bitmap' && $y==$h) {
55 $glyph = uc $glyph;
56 $glyph =~ s/\;$//;
57 local ($_) = $glyph;
58 my $key= sprintf "%s,%d,%d,%d,%d", $foundry,$w,$h,$xo,$yo;
59 my $rules= loadrules($key);
60 return (0,'no rules') if !$rules;
61 $rules->();
62 $modified += ($_ ne $glyph);
63 print $outbdf $_,"\n" or die $!
64 foreach split /\;/, $_; # /;
65 $state='idle';
66 }
67 if ($state eq 'bitmap') {
68 m/^([0-9a-fA-F]+)\s+$/ or die $y;
69 length($1) == (($w+7 >> 3) << 1) or die "$1 $w";
70 $glyph .= "$1;";
71 $y++;
72 next;
73 }
74 if ($state eq 'idle' && m/^FOUNDRY\s+/) {
75 die if defined $foundry;
76 return (0,'foundry syntax') unless m/^FOUNDRY\s+\"(\w+)\"\s+/;
77 $foundry = $foundrymap{lc $1};
78 return (0,'no foundry') unless defined $foundry;
79 $_ = "FOUNDRY \"$foundry\"\n";
80 }
81 if ($state eq 'idle' && m/^FONT\s+/) {
82 die if defined $font;
83 return (0,'simple font name') unless m/^(FONT\s+)\-(\w+)\-/;
84 $font = $foundrymap{lc $2};
85 return (0,'no foundry') unless defined $font;
86 $_ = "FONT -$font-$'";
87 }
88 if ($state eq 'idle' && m/^STARTCHAR\s/) {
89 die unless defined $foundry;
90 die unless defined $font;
91 return (0,'foundry != font') unless $foundry eq $font;
92 $state='startchar';
93 $w=undef;
94 }
95 if ($state eq 'startchar') {
96 if (m/^BBX\s+(\+?\d+)\s+(\+?\d+)\s+([-+]?\d+)\s+([-+]?\d+)\s+$/) {
97 ($w,$h,$xo,$yo) = ($1,$2,$3,$4);
98 }
99 if (m/^BITMAP\s+$/) {
100 die unless defined $w;
101 $y=0;
102 $glyph='';
103 $state='bitmap';
104 }
105 }
106 print $outbdf $_ or die $!;
107 }
108 die $! if $inbdf->error;
109 die $! if $outbdf->error or !$outbdf->flush;
110 die unless $state eq 'idle';
111 if ($modified) {
112 printf $logfile "%s: %d glyphs changed\n", $what, $modified
113 or die $!;
114 } else {
115 printf $logfile "%s: unchanged - no rules matched\n", $what
116 or die $!;
117 }
118 return $modified;
119}
120
121sub loadfoundries () {
122 foreach my $path (@rulespath) {
123 my $p = "$path/foundries";
124 my $f = new IO::File $p;
125 if (!$f) {
126 die "$p $!" unless $!==&ENOENT;
127 print $reportfh "foundries: none in $p\n" or die $! if $verbose;
128 next;
129 }
130 while (<$f>) {
131 s/^\s*//; s/\s+$//;
132 next if m/^\#/;
133 m/^(\w+)\s+(\w+)$/ or die;
134 my $k = lc $1;
135 next if exists $foundrymap{$k};
136 $foundrymap{$k}=$2;
137 }
138 $f->error and die $!;
139 reportloaded('foundries: loaded ',$p);
140 }
141 die "no foundry maps\n" unless %foundrymap;
142}
143
144sub processpcfgz ($$$$) {
145 my ($inpcfgz,$outpcfgz,$logfile,$what) = @_;
146
147 my $current = new IO::File $inpcfgz, '<' or die "$inpcfgz $!";
148 my ($usread,$uswrite);
149 my ($reader,$writer);
150 my @children;
151 foreach my $proc (['zcat'], ['pcf2bdf'], [],
152 ['bdftopcf'],['',qw(gzip -1)]) {
153 my $isfinal = (@$proc && $proc->[0] eq '');
154 if (!$isfinal) {
155 $reader = new IO::Handle or die $!;
156 $writer = new IO::Handle or die $!;
157 new IO::Pipe($reader,$writer) or die $!;
158 } else {
159 shift @$proc;
160 $reader = undef;
161 $writer = new IO::File $outpcfgz, '>' or die "$outpcfgz $!";
162 }
163 if (@$proc) {
164 my $exe = $proc->[0];
165 my $child = fork; defined $child or die $!;
166 if (!$child) {
167 open STDIN, '<&', $current or die $!;
168 open STDOUT, '>&', $writer or die $!;
169 if (!$isfinal) {
170 close $reader or die $!;
171 }
172 close $usread or die $! if $usread;
173 close $uswrite or die $! if $uswrite;
174 exec $exe @$proc or die "$exe $!";
175 }
176 push @children, [ $child, $exe, defined $usread ];
177 close $current or die $!;
178 close $writer or die $!;
179 $current = $reader;
180 } else {
181 $usread = $current;
182 $uswrite = $writer;
183 $current = $reader;
184 }
185 }
186 my $r = processbdf($usread,$uswrite,$logfile,$what);
187 my $none = $r !~ m/^\d/;
188 if ($none) {
189 flush $uswrite or die $!;
190 } else {
191 close $uswrite or die $!;
192 }
193 close $usread or die $!;
194 foreach my $chinfo (@children) {
195 my ($child,$exe,$isoutput)=@$chinfo;
196 my $sigok = 0;
197 if ($none) {
198 if ($isoutput) {
199 $sigok = 9;
200 kill 9, $child or die "$child $!";
201 } else {
202 $sigok = 13;
203 }
204 }
205 $!=0; waitpid($child, 0) == $child or die "$child $!";
206 !$? or ($?&~128)==$sigok or die "$exe [$child] $sigok $?";
207 }
208 return $r;
209}
210
211sub processfontdir ($) {
212 my ($fontdir) = @_;
213 if (!opendir FD, $fontdir) {
214 die "$fontdir $!" unless $!==&ENOENT;
215 return;
216 }
217 my $changed = 0;
218 my $olddone = do "$fontdir/$donefile";
219 if (!$olddone) {
220 die "$fontdir $! $@ " unless $!==&ENOENT;
221 $olddone = { };
222 $changed = 1;
223 }
224 my $newdone = { };
225 my $log = new IO::File "$fontdir/$logfile", "w"
226 or die "$fontdir/$logfile $!";
227 my %outfiles; # bitmask: 1 /*exists*/ | 2 /*wanted*/
228 my $updated=0;
229 my $reported=0;
230 my $anypcfs=0;
231
232 flush $reportfh or die $!;
233 while (my $dent = scalar readdir FD) {
234 if ($dent =~ m/^\Q$fontprefix\E.*\.new$/) {
235 unlink "$fontdir/$dent" or $!==&ENOENT or die "$fontdir $dent $!";
236 next;
237 }
238 next unless $dent =~ m/^[^.\/].*\.pcf\.gz$/;
239 print $reportfh "processing $fontdir...\n" or die $!
240 unless $reported++;
241 if ($dent =~ m/^\Q$fontprefix/) {
242 $outfiles{$dent} |= 1;
243 next;
244 }
245 if (!stat "$fontdir/$dent") {
246 die "$fontdir $dent $!" unless $!==&ENOENT;
247 next;
248 }
249 die "$fontdir $dent" unless -f _;
250 $anypcfs++;
251
252 my $stats = join ' ', ((stat _)[1,7,9,10]);
253 my $tdone = $olddone->{$dent};
254 my $outdent = $fontprefix.$dent;
255 if (defined $tdone && $tdone eq $stats) {
256 $outfiles{$outdent} |= 2;
257 $newdone->{$dent} = $stats;
258 next;
259 }
260
261 my $r = processpcfgz("$fontdir/$dent",
262 "$fontdir/$outdent.new",
263 $log, $dent);
264 if ($r !~ m/^\d/) {
265 printf $log "%s: unchanged - %s\n", $dent, $r;
266 unlink "$fontdir/$outdent.new" or die "$fontdir $outdent $!";
267 } else {
268 rename "$fontdir/$outdent.new", "$fontdir/$outdent"
269 or die "$fontdir $outdent $!";
270 $updated++;
271 $outfiles{$outdent} |= 3;
272 }
273 $newdone->{$dent} = $stats;
274 $changed = 1;
275 }
276 my $affected=0;
277 foreach my $olddent (keys %outfiles) {
278 my $state = $outfiles{$olddent};
279 if ($state & 2) {
280 $affected++ if $state & 1;
281 next;
282 }
283 unlink "$fontdir/$olddent" or die "$fontdir $olddent $!";
284 $changed = 1;
285 $updated++;
286 }
287 if (!stat "$fontdir/fonts.dir") {
288 $!==&ENOENT or die "$fontdir $!";
289 } else {
290 $!=0; $?=0; system 'mkfontdir',$fontdir;
291 die "$fontdir $? $!" if $? or $!;
292 }
293 if (!$anypcfs) {
294 unlink "$fontdir/$logfile" or die "$fontdir $!";
295 unlink "$fontdir/$donefile" or $!==&ENOENT or die "$fontdir $!";
296 } elsif ($changed) {
297 my $newdoneh = new IO::File "$fontdir/$donefile.new", 'w'
298 or die "$fontdir $!";
299 print $newdoneh Dumper($newdone) or die "$fontdir $!";
300 close $newdoneh or die "$fontdir $!";
301 rename "$fontdir/$donefile.new","$fontdir/$donefile"
302 or die "$fontdir $!";
303 }
304 if ($reported || %$newdone || $affected || $updated) {
305 printf " processed %s: %d pcfs, %d affected, %d updated.\n",
306 $fontdir, (scalar keys %$newdone), $affected, $updated;
307 }
308}
309
310sub processfonttree ($) {
311 my ($tree) = @_;
312 find({ follow => 1,
313 dangling_symlinks => 0,
314 no_chdir => 1,
315 wanted => sub {
316 return unless -d _;
317 processfontdir($File::Find::name);
318 }},
319 $tree);
320}
321
322our $stdin = new IO::File '<&STDIN' or die $!;
323our $stdout = new IO::File '>&STDOUT' or die $!;
324our $stderr = new IO::File '>&STDERR' or die $!;
325$reportfh = $stdout;
326
327our (@options)=(
328 'R|rules-include=s@' => \@rulespath,
329 'share-dir=s' => \$sharedir,
330 'verbose|v+' => \$verbose,
331 );
332
333sub define_mode ($$) {
334 my ($optname,$f) = @_;
335 push @options, $optname, sub {
336 die "only one mode may be specified\n" if defined $mode;
337 $mode=$f;
338 };
339}
340
341define_mode('bdf-filter', sub {
342 die "no arguments allowed with --bdf-filter\n" if @ARGV;
343 $reportfh = $stderr;
344 loadfoundries();
345 my $r = processbdf($stdin,$stdout,$reportfh,'stdin');
346 if ($r !~ m/^\d/) {
347 print STDERR "stdin not processed: $r\n";
348 exit 2;
349 }
350});
351
352define_mode('process-pcf', sub {
353 die "need source and destination pcf.gz\n" if @ARGV!=2;
354 loadfoundries();
355 my $r = processpcfgz($ARGV[0],$ARGV[1],$reportfh,"pcf");
356 if ($r !~ m/^\d/) {
357 print STDERR "pcf not processed: $r\n";
358 exit 2;
359 }
360});
361
362define_mode('process-fontdirs', sub {
363 die "need font dir(s)\n" unless @ARGV;
364 loadfoundries();
365 foreach my $d (@ARGV) {
366 processfontdir($d);
367 }
368});
369
370define_mode('process-fonttrees', sub {
371 die "need font tree(s)\n" unless @ARGV;
372 loadfoundries();
373 foreach my $d (@ARGV) {
374 processfonttree($d);
375 }
376});
377
378define_mode('update', sub {
379 die "no arguments allowed with --postinst\n" unless !@ARGV;
380 loadfoundries();
381 foreach my $d (@fonttrees) {
382 processfonttree($d);
383 }
384});
385
386Getopt::Long::Configure(qw(bundling));
387GetOptions(@options) or exit 127;
388
389push @rulespath, "$sharedir/rules";
390
391die "need a mode\n" unless $mode;
392
393$mode->();