6 use File
::Glob
qw(:glob);
11 our $prefix="/usr/local";
12 our $package='xfonts-traditional';
13 our $sharedir="$prefix/share/$package";
14 our @fonttrees=qw(/usr/share
/fonts/X11
/usr/local/share/fonts
/X11
);
15 our $donefile="$package.done";
16 our $logfile="$package.log";
17 our $fontprefix="trad--";
27 return unless $verbose;
28 print $reportfh @_,"\n" or die $!;
32 return join ' ', ((stat _
)[1,7,9,10]);
40 foreach my $path (@rulespath) {
41 my $script="$path/$key.rules";
42 $!=0; $@
=''; my $f = do $script;
44 reportloaded
("rules: loaded ",$script);
48 die "$! $? $script" unless $! == &ENOENT
;
50 return $cache{$key}=undef;
53 sub processbdf
($$$$) {
54 my ($inbdf,$outbdf,$logfile,$what) = @_;
57 my ($w,$h,$xo,$yo,$y,$bitmap,$glyph);
61 if ($state eq 'bitmap' && $y==$h) {
65 my $key= sprintf "%s,%d,%d,%d,%d", $foundry,$w,$h,$xo,$yo;
66 my $rules= loadrules
($key);
67 return (0,'no rules') if !$rules;
69 $modified += ($_ ne $glyph);
70 print $outbdf $_,"\n" or die $!
71 foreach split /\;/, $_; # /;
74 if ($state eq 'bitmap') {
75 m/^([0-9a-fA-F]+)\s+$/ or die $y;
76 length($1) == (($w+7 >> 3) << 1) or die "$1 $w";
81 if ($state eq 'idle' && m/^FOUNDRY\s+/) {
82 die if defined $foundry;
83 return (0,'foundry syntax') unless m/^FOUNDRY\s+\"(\w+)\"\s+/;
84 $foundry = $foundrymap{lc $1};
85 return (0,'no foundry') unless defined $foundry;
86 $_ = "FOUNDRY \"$foundry\"\n";
88 if ($state eq 'idle' && m/^FONT\s+/) {
90 return (0,'simple font name') unless m/^(FONT\s+)\-(\w+)\-/;
91 $font = $foundrymap{lc $2};
92 return (0,'no foundry') unless defined $font;
93 $_ = "FONT -$font-$'";
95 if ($state eq 'idle' && m/^STARTCHAR\s/) {
96 die unless defined $foundry;
97 die unless defined $font;
98 return (0,'foundry != font') unless $foundry eq $font;
102 if (($state eq 'idle' || $state eq 'startchar') &&
103 m/^([A-Z_]+)\s+(.*\S)\s+$/) {
106 if ($state eq 'startchar') {
107 if (m/^BBX\s+(\+?\d+)\s+(\+?\d+)\s+([-+]?\d+)\s+([-+]?\d+)\s+$/) {
108 ($w,$h,$xo,$yo) = ($1,$2,$3,$4);
110 if (m/^BITMAP\s+$/) {
111 die unless defined $w;
116 ($props{'CHARSET_REGISTRY'} =~ m/iso8859|utf|iso10646/i &&
117 $props{'ENCODING'} <= 127);
120 print $outbdf $_ or die $!;
122 die $! if $inbdf->error;
123 die $! if $outbdf->error or !$outbdf->flush;
124 die unless $state eq 'idle';
126 printf $logfile "%s: %d glyphs changed\n", $what, $modified
129 printf $logfile "%s: unchanged - no rules matched\n", $what
135 sub loadfoundries
() {
137 foreach my $path (@rulespath) {
139 die "$path $!" unless $!==&ENOENT
;
142 $foundryinfo .= statsummary
().' '.$path."\0\n";
144 my $p = "$path/foundries";
145 my $f = new IO
::File
$p;
147 die "$p $!" unless $!==&ENOENT
;
148 print $reportfh "foundries: none in $p\n" or die $! if $verbose;
155 m/^(\w+)\s+(\w+)$/ or die;
157 next if exists $foundrymap{$k};
160 $f->error and die $!;
161 reportloaded
('foundries: loaded ',$p);
163 die "no foundry maps\n" unless %foundrymap;
166 sub processpcfgz
($$$$) {
167 my ($inpcfgz,$outpcfgz,$logfile,$what) = @_;
168 print $reportfh "processing $inpcfgz to $outpcfgz\n" if $verbose>=2;
169 my $current = new IO
::File
$inpcfgz, '<' or die "$inpcfgz $!";
170 my ($usread,$uswrite);
171 my ($reader,$writer);
173 foreach my $proc (['gunzip'], ['pcf2bdf'], [],
174 ['bdftopcf'],['',qw(gzip
-1 -n
)]) {
175 my $isfinal = (@
$proc && $proc->[0] eq '');
177 $reader = new IO
::Handle
or die $!;
178 $writer = new IO
::Handle
or die $!;
179 new IO
::Pipe
($reader,$writer) or die $!;
183 $writer = new IO
::File
$outpcfgz, '>' or die "$outpcfgz $!";
186 my $exe = $proc->[0];
187 my $child = fork; defined $child or die $!;
189 open STDIN
, '<&', $current or die $!;
190 open STDOUT
, '>&', $writer or die $!;
192 close $reader or die $!;
194 close $usread or die $! if $usread;
195 close $uswrite or die $! if $uswrite;
196 exec $exe @
$proc or die "$exe $!";
198 push @children, [ $child, $exe, defined $usread ];
199 close $current or die $!;
200 close $writer or die $!;
208 my $r = processbdf
($usread,$uswrite,$logfile,$what);
209 my $none = $r !~ m/^\d/;
211 flush
$uswrite or die $!;
213 close $uswrite or die $!;
215 close $usread or die $!;
216 foreach my $chinfo (@children) {
217 my ($child,$exe,$isoutput)=@
$chinfo;
222 kill 9, $child or die "$child $!";
227 $!=0; waitpid($child, 0) == $child or die "$child $!";
228 !$?
or ($?
&~128)==$sigok or die "$exe [$child] $sigok $?";
233 sub processfontdir
($) {
235 if (!opendir FD
, $fontdir) {
236 die "$fontdir $!" unless $!==&ENOENT
;
240 my $olddone = do "$fontdir/$donefile";
242 die "$fontdir $! $@ " unless $!==&ENOENT
;
243 } elsif ($olddone->{''} ne $foundryinfo) {
245 print $reportfh "reprocessing fonts (rules updated)\n" or die $!
246 unless $repro_reported++;
253 my $newdone = { '' => $foundryinfo };
254 my $log = new IO
::File
"$fontdir/$logfile", "w"
255 or die "$fontdir/$logfile $!";
256 my %outfiles; # bitmask: 1 /*exists*/ | 2 /*wanted*/
261 flush
$reportfh or die $!;
262 while (my $dent = scalar readdir FD
) {
263 if ($dent =~ m/^\Q$fontprefix\E.*\.new$/) {
264 unlink "$fontdir/$dent" or $!==&ENOENT
or die "$fontdir $dent $!";
267 next unless $dent =~ m/^[^.\/].*\
.pcf\
.gz
$/;
268 print $reportfh "processing $fontdir...\n" or die $!
270 if ($dent =~ m/^\Q$fontprefix/) {
271 $outfiles{$dent} |= 1;
274 if (!stat "$fontdir/$dent") {
275 die "$fontdir $dent $!" unless $!==&ENOENT
;
278 die "$fontdir $dent" unless -f _
;
281 my $stats = statsummary
();
282 my $tdone = $olddone->{$dent};
283 my $outdent = $fontprefix.$dent;
284 if (defined $tdone && $tdone eq $stats) {
285 $outfiles{$outdent} |= 2;
286 $newdone->{$dent} = $stats;
290 my $r = processpcfgz
("$fontdir/$dent",
291 "$fontdir/$outdent.new",
294 printf $log "%s: unchanged - %s\n", $dent, $r;
295 unlink "$fontdir/$outdent.new" or die "$fontdir $outdent $!";
297 rename "$fontdir/$outdent.new", "$fontdir/$outdent"
298 or die "$fontdir $outdent $!";
300 $outfiles{$outdent} |= 3;
302 $newdone->{$dent} = $stats;
306 foreach my $olddent (keys %outfiles) {
307 my $state = $outfiles{$olddent};
309 $affected++ if $state & 1;
312 unlink "$fontdir/$olddent" or die "$fontdir $olddent $!";
316 if (!stat "$fontdir/fonts.dir") {
317 $!==&ENOENT
or die "$fontdir $!";
319 $!=0; $?
=0; system 'mkfontdir',$fontdir;
320 die "$fontdir $? $!" if $?
or $!;
323 unlink "$fontdir/$logfile" or die "$fontdir $!";
324 unlink "$fontdir/$donefile" or $!==&ENOENT
or die "$fontdir $!";
326 my $newdoneh = new IO
::File
"$fontdir/$donefile.new", 'w'
327 or die "$fontdir $!";
328 print $newdoneh Dumper
($newdone) or die "$fontdir $!";
329 close $newdoneh or die "$fontdir $!";
330 rename "$fontdir/$donefile.new","$fontdir/$donefile"
331 or die "$fontdir $!";
333 if ($reported || %$newdone || $affected || $updated) {
334 printf " processed %s: %d pcfs, %d affected, %d updated.\n",
335 $fontdir, (scalar keys %$newdone), $affected, $updated;
339 sub processfonttree
($) {
342 dangling_symlinks
=> 0,
346 processfontdir
($File::Find
::name
);
351 our $stdin = new IO
::File
'<&STDIN' or die $!;
352 our $stdout = new IO
::File
'>&STDOUT' or die $!;
353 our $stderr = new IO
::File
'>&STDERR' or die $!;
357 'R|rules-include=s@' => \
@rulespath,
358 'share-dir=s' => \
$sharedir,
359 'verbose|v+' => \
$verbose,
362 sub define_mode
($$) {
363 my ($optname,$f) = @_;
364 push @options, $optname, sub {
365 die "only one mode may be specified\n" if defined $mode;
370 define_mode
('bdf-filter', sub {
371 die "no arguments allowed with --bdf-filter\n" if @ARGV;
374 my $r = processbdf
($stdin,$stdout,$reportfh,'stdin');
376 print STDERR
"stdin not processed: $r\n";
381 define_mode
('process-pcf', sub {
382 die "need source and destination pcf.gz\n" if @ARGV!=2;
384 my $r = processpcfgz
($ARGV[0],$ARGV[1],$reportfh,"pcf");
386 print STDERR
"pcf not processed: $r\n";
391 define_mode
('process-fontdirs', sub {
392 die "need font dir(s)\n" unless @ARGV;
394 foreach my $d (@ARGV) {
399 define_mode
('process-fonttrees', sub {
400 die "need font tree(s)\n" unless @ARGV;
402 foreach my $d (@ARGV) {
407 define_mode
('update', sub {
408 die "no arguments allowed with --postinst\n" unless !@ARGV;
410 foreach my $d (@fonttrees) {
415 Getopt
::Long
::Configure
(qw(bundling
));
416 GetOptions
(@options) or exit 127;
418 push @rulespath, "$sharedir/rules";
420 die "need a mode\n" unless $mode;