option parsing etc
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 7 Jan 2012 17:37:47 +0000 (17:37 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 7 Jan 2012 17:37:47 +0000 (17:37 +0000)
Trad,6,13,0,-2.rules [moved from 6,13,0,-2.rules with 100% similarity]
foundries [new file with mode: 0644]
utility

similarity index 100%
rename from 6,13,0,-2.rules
rename to Trad,6,13,0,-2.rules
diff --git a/foundries b/foundries
new file mode 100644 (file)
index 0000000..78a533e
--- /dev/null
+++ b/foundries
@@ -0,0 +1 @@
+Misc   Trad
diff --git a/utility b/utility
index c9a420e..f03b771 100755 (executable)
--- a/utility
+++ b/utility
@@ -8,6 +8,14 @@ our $package='xfonts-traditional';
 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) = @_;
@@ -18,6 +26,7 @@ sub loadrules ($) {
        my $script="$path/$key.rules";
        $!=0; $@=''; my $f = do $script;
        if (defined $f) {
+           reportloaded("rules: loaded ",$script);
            $cache{$key}=$f;
            return $f;
        }
@@ -26,19 +35,22 @@ sub loadrules ($) {
     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';
@@ -51,17 +63,23 @@ sub processbdf ($$) {
            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;
        }
@@ -81,11 +99,20 @@ sub processbdf ($$) {
     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 ($$) {
@@ -96,11 +123,43 @@ 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));