our $sharedir="/usr/share/$package";
our @rulespath;
our $mode;
+our %foundrymap;
+our $verbose=0;
+our $reportfh;
+
+sub reportloaded {
+ return unless $verbose;
+ print $reportfh @_,"\n" or die $!;
+}
sub loadrules ($) {
my ($key) = @_;
my $script="$path/$key.rules";
$!=0; $@=''; my $f = do $script;
if (defined $f) {
+ reportloaded("rules: loaded ",$script);
$cache{$key}=$f;
return $f;
}
return $cache{$key}=undef;
}
-sub processbdf ($$) {
- my ($inbdf,$outbdf) = @_;
+sub processbdf ($$$) {
+ my ($inbdf,$outbdf,$what) = @_;
my $state='idle';
- my ($donefoundry,$donefont);
+ my ($foundry,$font);
my ($w,$h,$xo,$yo,$y,$bitmap,$glyph);
+ my $modified=0;
while (<$inbdf>) {
if ($state eq 'bitmap' && $y==$h) {
- local ($_) = uc $glyph;
- my $key= sprintf "%d,%d,%d,%d", $w,$h,$xo,$yo;
+ $glyph = uc $glyph;
+ $glyph =~ s/\;$//;
+ local ($_) = $glyph;
+ my $key= sprintf "%s,%d,%d,%d,%d", $foundry,$w,$h,$xo,$yo;
my $rules= loadrules($key);
- return 0 if !$rules;
- s/\;$//;
+ return (0,'no rules') if !$rules;
$rules->();
+ $modified += ($_ ne $glyph);
print $outbdf $_,"\n" or die $!
foreach split /\;/, $_; # /;
$state='idle';
next;
}
if ($state eq 'idle' && m/^FOUNDRY\s+/) {
- return 0 unless m/^FOUNDRY\s+\"[Mm]isc\"\s+/;
- s/misc/Trad/i;
- $donefoundry=1;
+ die if defined $foundry;
+ return (0,'foundry syntax') unless m/^FOUNDRY\s+\"(\w+)\"\s+/;
+ $foundry = $foundrymap{lc $1};
+ return (0,'no foundry') unless defined $foundry;
+ $_ = "FOUNDRY \"$foundry\"\n";
}
if ($state eq 'idle' && m/^FONT\s+/) {
- return 0 unless s/^(FONT\s+)\-[Mm]isc\-/$1-Trad-/;
- $donefont=1;
+ die if defined $font;
+ return 0 unless m/^(FONT\s+)\-(\w+)\-/;
+ $font = $foundrymap{lc $2};
+ return (0,'no foundry') unless defined $font;
+ $_ = "FONT -$font-$'";
}
if ($state eq 'idle' && m/^STARTCHAR\s/) {
- die unless $donefoundry;
- die unless $donefont;
+ die unless defined $foundry;
+ die unless defined $font;
+ return (0,'foundry != font') unless $foundry eq $font;
$state='startchar';
$w=undef;
}
die $! if $inbdf->error;
die $! if $outbdf->error or !$outbdf->flush;
die unless $state eq 'idle';
+ if ($modified) {
+ printf $reportfh "%s: %d glyphs changed\n", $what, $modified
+ or die $!;
+ } else {
+ printf $reportfh "%s: unchanged - no rules matched\n", $what
+ or die $!;
+ }
+ return $modified;
}
our (@options)=(
'R|rules-include=s@' => \@rulespath,
'share-dir=s' => \$sharedir,
+ 'verbose|v+' => \$verbose,
);
sub define_mode ($$) {
};
}
-define_mode('bdf-filter', sub {
- die if @ARGV;
+sub loadfoundries () {
+ foreach my $path (@rulespath) {
+ my $p = "$path/foundries";
+ my $f = new IO::File $p;
+ if (!$f) {
+ die "$p $!" unless $!==&ENOENT;
+ print $reportfh "foundries: none in $p\n" or die $! if $verbose;
+ next;
+ }
+ while (<$f>) {
+ s/^\s*//; s/\s+$//;
+ next if m/^\#/;
+ m/^(\w+)\s+(\w+)$/ or die;
+ my $k = lc $1;
+ next if exists $foundrymap{$k};
+ $foundrymap{$k}=$2;
+ }
+ $f->error and die $!;
+ reportloaded('foundries: loaded ',$p);
+ }
+ die "no foundry maps\n" unless %foundrymap;
+}
- processbdf((new IO::File '<&STDIN'),
- (new IO::File '>&STDOUT'));
+our $stdin = new IO::File '<&STDIN' or die $!;
+our $stdout = new IO::File '>&STDOUT' or die $!;
+our $stderr = new IO::File '>&STDERR' or die $!;
+$reportfh = $stdout;
+
+define_mode('bdf-filter', sub {
+ die "no arguments allowed with --bdf-filter\n" if @ARGV;
+ $reportfh = $stderr;
+ loadfoundries();
+ my $r = processbdf($stdin,$stdout,'stdin');
+ if ($r !~ m/^\d/) {
+ print STDERR "stdin not processed: $r\n";
+ exit 2;
+ }
});
Getopt::Long::Configure(qw(bundling));