chiark / gitweb /
--process-fonttree
[xfonts-traditional] / update-xfonts-traditional
CommitLineData
18d2cc90
IJ
1#!/usr/bin/perl -w
2use strict;
3use POSIX;
4use IO::File;
a2d36db5 5use Getopt::Long;
baaae949 6use File::Glob qw(:glob);
a94959e6 7use Data::Dumper;
71e1fff3 8use IO::Pipe;
1f0fee99 9use File::Find;
18d2cc90 10
1b17f3ec 11our $prefix="/usr/local";
a2d36db5 12our $package='xfonts-traditional';
1b17f3ec 13our $sharedir="$prefix/share/$package";
1f0fee99 14our @fonttrees=qw(/usr/share/fonts/X11 /usr/local/share/fonts/X11);
baaae949 15our $donefile="$package.done";
a94959e6 16our $logfile="$package.log";
baaae949 17our $fontprefix="trad--";
a2d36db5
IJ
18our @rulespath;
19our $mode;
148a1d85
IJ
20our %foundrymap;
21our $verbose=0;
22our $reportfh;
23
24sub reportloaded {
25 return unless $verbose;
26 print $reportfh @_,"\n" or die $!;
27}
18d2cc90 28
4ee8efe0
IJ
29sub loadrules ($) {
30 my ($key) = @_;
18d2cc90 31 our %cache;
18d2cc90
IJ
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) {
148a1d85 38 reportloaded("rules: loaded ",$script);
18d2cc90
IJ
39 $cache{$key}=$f;
40 return $f;
41 }
42 die "$! $? $script" unless $! == &ENOENT;
43 }
44 return $cache{$key}=undef;
45}
46
a94959e6
IJ
47sub processbdf ($$$$) {
48 my ($inbdf,$outbdf,$logfile,$what) = @_;
4ee8efe0 49 my $state='idle';
148a1d85 50 my ($foundry,$font);
4ee8efe0 51 my ($w,$h,$xo,$yo,$y,$bitmap,$glyph);
148a1d85 52 my $modified=0;
18d2cc90
IJ
53 while (<$inbdf>) {
54 if ($state eq 'bitmap' && $y==$h) {
148a1d85
IJ
55 $glyph = uc $glyph;
56 $glyph =~ s/\;$//;
57 local ($_) = $glyph;
58 my $key= sprintf "%s,%d,%d,%d,%d", $foundry,$w,$h,$xo,$yo;
4ee8efe0 59 my $rules= loadrules($key);
148a1d85 60 return (0,'no rules') if !$rules;
18d2cc90 61 $rules->();
148a1d85 62 $modified += ($_ ne $glyph);
18d2cc90 63 print $outbdf $_,"\n" or die $!
4ee8efe0 64 foreach split /\;/, $_; # /;
18d2cc90
IJ
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;";
4ee8efe0
IJ
71 $y++;
72 next;
18d2cc90 73 }
4ee8efe0 74 if ($state eq 'idle' && m/^FOUNDRY\s+/) {
148a1d85
IJ
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";
4ee8efe0
IJ
80 }
81 if ($state eq 'idle' && m/^FONT\s+/) {
148a1d85 82 die if defined $font;
71e1fff3 83 return (0,'simple font name') unless m/^(FONT\s+)\-(\w+)\-/;
148a1d85
IJ
84 $font = $foundrymap{lc $2};
85 return (0,'no foundry') unless defined $font;
86 $_ = "FONT -$font-$'";
18d2cc90
IJ
87 }
88 if ($state eq 'idle' && m/^STARTCHAR\s/) {
148a1d85
IJ
89 die unless defined $foundry;
90 die unless defined $font;
91 return (0,'foundry != font') unless $foundry eq $font;
18d2cc90
IJ
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 }
4ee8efe0 106 print $outbdf $_ or die $!;
18d2cc90
IJ
107 }
108 die $! if $inbdf->error;
4ee8efe0 109 die $! if $outbdf->error or !$outbdf->flush;
18d2cc90 110 die unless $state eq 'idle';
148a1d85 111 if ($modified) {
a94959e6 112 printf $logfile "%s: %d glyphs changed\n", $what, $modified
148a1d85
IJ
113 or die $!;
114 } else {
a94959e6 115 printf $logfile "%s: unchanged - no rules matched\n", $what
148a1d85
IJ
116 or die $!;
117 }
118 return $modified;
18d2cc90
IJ
119}
120
148a1d85
IJ
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}
a2d36db5 143
71e1fff3
IJ
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}
30c6d632 210
baaae949
IJ
211sub processfontdir ($) {
212 my ($fontdir) = @_;
213 if (!opendir FD, $fontdir) {
214 die "$fontdir $!" unless $!==&ENOENT;
215 return;
216 }
a94959e6 217 my $olddone = do "$fontdir/$donefile";
30c6d632 218 if (!$olddone) {
baaae949 219 die "$fontdir $! $@ " unless $!==&ENOENT;
30c6d632 220 $olddone = { };
baaae949 221 }
a94959e6
IJ
222 my $newdone = { };
223 my $log = new IO::File "$fontdir/$logfile", "w"
224 or die "$fontdir/$logfile $!";
71e1fff3 225 my %outfiles; # bitmask: 1 /*exists*/ | 2 /*wanted*/
a94959e6 226 my $changed;
71e1fff3 227 my $updated=0;
1f0fee99 228 my $reported=0;
71e1fff3 229
71e1fff3
IJ
230 flush $reportfh or die $!;
231 while (my $dent = scalar readdir FD) {
a94959e6
IJ
232 if ($dent =~ m/^\Q$fontprefix\E.*\.new$/) {
233 unlink "$fontdir/$dent" or $!==&ENOENT or die "$fontdir $dent $!";
234 next;
235 }
baaae949 236 next unless $dent =~ m/^[^.\/].*\.pcf\.gz$/;
1f0fee99
IJ
237 print $reportfh "processing $fontdir...\n" or die $!
238 unless $reported++;
baaae949 239 if ($dent =~ m/^\Q$fontprefix/) {
71e1fff3 240 $outfiles{$dent} |= 1;
baaae949
IJ
241 next;
242 }
71e1fff3 243 if (!stat "$fontdir/$dent") {
baaae949
IJ
244 die "$fontdir $dent $!" unless $!==&ENOENT;
245 next;
246 }
247 die "$fontdir $dent" unless -f _;
248 my $stats = join ' ', ((stat _)[1,7,9,10]);
30c6d632 249 my $tdone = $olddone->{$dent};
71e1fff3 250 my $outdent = $fontprefix.$dent;
baaae949 251 if (defined $tdone && $tdone eq $stats) {
71e1fff3 252 $outfiles{$outdent} |= 2;
a94959e6 253 $newdone->{$dent} = $stats;
baaae949
IJ
254 next;
255 }
a94959e6 256
30c6d632
IJ
257 my $r = processpcfgz("$fontdir/$dent",
258 "$fontdir/$outdent.new",
71e1fff3 259 $log, $dent);
a94959e6 260 if ($r !~ m/^\d/) {
71e1fff3 261 printf $log "%s: unchanged - %s\n", $dent, $r;
a94959e6
IJ
262 unlink "$fontdir/$outdent.new" or die "$fontdir $outdent $!";
263 } else {
264 rename "$fontdir/$outdent.new", "$fontdir/$outdent"
265 or die "$fontdir $outdent $!";
71e1fff3
IJ
266 $updated++;
267 $outfiles{$outdent} |= 3;
a94959e6
IJ
268 $changed = 1;
269 }
71e1fff3 270 $newdone->{$dent} = $stats;
a94959e6 271 }
71e1fff3
IJ
272 my $affected=0;
273 foreach my $olddent (keys %outfiles) {
274 my $state = $outfiles{$olddent};
275 if ($state & 2) {
276 $affected++ if $state & 1;
277 next;
278 }
a94959e6
IJ
279 unlink "$fontdir/$olddent" or die "$fontdir $olddent $!";
280 $changed = 1;
281 }
71e1fff3
IJ
282 if (!%$newdone) {
283 unlink "$fontdir/$logfile" or die "$fontdir $!";
284 unlink "$fontdir/$donefile" or $!==&ENOENT or die "$fontdir $!";
285 } elsif ($changed) {
a94959e6
IJ
286 $!=0; $?=0; system 'mkfontdir',$fontdir;
287 die "$fontdir $? $!" if $? or $!;
288 my $newdoneh = new IO::File "$fontdir/$donefile.new", 'w'
289 or die "$fontdir $!";
290 print $newdoneh Dumper($newdone) or die "$fontdir $!";
291 close $newdoneh or die "$fontdir $!";
292 rename "$fontdir/$donefile.new","$fontdir/$donefile"
293 or die "$fontdir $!";
294 }
1f0fee99
IJ
295 if ($reported || %$newdone || $affected || $updated) {
296 printf " processed %s: %d pcfs, %d affected, %d updated.\n",
297 $fontdir, (scalar keys %$newdone), $affected, $updated;
298 }
299}
300
301sub processfonttree ($) {
302 my ($tree) = @_;
303 find({ follow => 1,
304 dangling_symlinks => 0,
305 no_chdir => 1,
306 wanted => sub {
307 return unless -d _;
308 processfontdir($File::Find::name);
309 }},
310 $tree);
a94959e6 311}
baaae949 312
148a1d85
IJ
313our $stdin = new IO::File '<&STDIN' or die $!;
314our $stdout = new IO::File '>&STDOUT' or die $!;
315our $stderr = new IO::File '>&STDERR' or die $!;
316$reportfh = $stdout;
317
1f0fee99
IJ
318our (@options)=(
319 'R|rules-include=s@' => \@rulespath,
320 'share-dir=s' => \$sharedir,
321 'verbose|v+' => \$verbose,
322 );
323
324sub define_mode ($$) {
325 my ($optname,$f) = @_;
326 push @options, $optname, sub {
327 die "only one mode may be specified\n" if defined $mode;
328 $mode=$f;
329 };
330}
331
148a1d85
IJ
332define_mode('bdf-filter', sub {
333 die "no arguments allowed with --bdf-filter\n" if @ARGV;
334 $reportfh = $stderr;
335 loadfoundries();
a94959e6 336 my $r = processbdf($stdin,$stdout,$reportfh,'stdin');
148a1d85
IJ
337 if ($r !~ m/^\d/) {
338 print STDERR "stdin not processed: $r\n";
339 exit 2;
340 }
a2d36db5
IJ
341});
342
71e1fff3
IJ
343define_mode('process-pcf', sub {
344 die "need source and destination pcf.gz\n" if @ARGV!=2;
345 loadfoundries();
346 my $r = processpcfgz($ARGV[0],$ARGV[1],$reportfh,"pcf");
347 if ($r !~ m/^\d/) {
348 print STDERR "pcf not processed: $r\n";
349 exit 2;
350 }
351});
352
1f0fee99 353define_mode('process-fontdirs', sub {
71e1fff3
IJ
354 die "need font dir(s)\n" unless @ARGV;
355 loadfoundries();
356 foreach my $d (@ARGV) {
357 processfontdir($d);
358 }
359});
360
1f0fee99
IJ
361define_mode('process-fonttrees', sub {
362 die "need font tree(s)\n" unless @ARGV;
363 loadfoundries();
364 foreach my $d (@ARGV) {
365 processfonttree($d);
366 }
367});
368
a2d36db5
IJ
369Getopt::Long::Configure(qw(bundling));
370GetOptions(@options) or exit 127;
371
db16069b 372push @rulespath, "$sharedir/rules";
a2d36db5
IJ
373
374die "need a mode\n" unless $mode;
375
376$mode->();