quoting of weird chars
[xfonts-traditional] / update-xfonts-traditional
1 #!/usr/bin/perl -w
2 use strict;
3 use POSIX;
4 use IO::File;
5 use Getopt::Long;
6
7 our $prefix="/usr/local";
8 our $package='xfonts-traditional';
9 our $sharedir="$prefix/share/$package";
10 our @fontsdirs=qw(/usr/share/fonts/X11 /usr/local/share/fonts/X11);
11 our @rulespath;
12 our $mode;
13 our %foundrymap;
14 our $verbose=0;
15 our $reportfh;
16
17 sub reportloaded {
18 return unless $verbose;
19 print $reportfh @_,"\n" or die $!;
20 }
21
22 sub loadrules ($) {
23 my ($key) = @_;
24 our %cache;
25 my $fc=$cache{$key};
26 return $fc if $fc;
27 foreach my $path (@rulespath) {
28 my $script="$path/$key.rules";
29 $!=0; $@=''; my $f = do $script;
30 if (defined $f) {
31 reportloaded("rules: loaded ",$script);
32 $cache{$key}=$f;
33 return $f;
34 }
35 die "$! $? $script" unless $! == &ENOENT;
36 }
37 return $cache{$key}=undef;
38 }
39
40 sub processbdf ($$$) {
41 my ($inbdf,$outbdf,$what) = @_;
42 my $state='idle';
43 my ($foundry,$font);
44 my ($w,$h,$xo,$yo,$y,$bitmap,$glyph);
45 my $modified=0;
46 while (<$inbdf>) {
47 if ($state eq 'bitmap' && $y==$h) {
48 $glyph = uc $glyph;
49 $glyph =~ s/\;$//;
50 local ($_) = $glyph;
51 my $key= sprintf "%s,%d,%d,%d,%d", $foundry,$w,$h,$xo,$yo;
52 my $rules= loadrules($key);
53 return (0,'no rules') if !$rules;
54 $rules->();
55 $modified += ($_ ne $glyph);
56 print $outbdf $_,"\n" or die $!
57 foreach split /\;/, $_; # /;
58 $state='idle';
59 }
60 if ($state eq 'bitmap') {
61 m/^([0-9a-fA-F]+)\s+$/ or die $y;
62 length($1) == (($w+7 >> 3) << 1) or die "$1 $w";
63 $glyph .= "$1;";
64 $y++;
65 next;
66 }
67 if ($state eq 'idle' && m/^FOUNDRY\s+/) {
68 die if defined $foundry;
69 return (0,'foundry syntax') unless m/^FOUNDRY\s+\"(\w+)\"\s+/;
70 $foundry = $foundrymap{lc $1};
71 return (0,'no foundry') unless defined $foundry;
72 $_ = "FOUNDRY \"$foundry\"\n";
73 }
74 if ($state eq 'idle' && m/^FONT\s+/) {
75 die if defined $font;
76 return 0 unless m/^(FONT\s+)\-(\w+)\-/;
77 $font = $foundrymap{lc $2};
78 return (0,'no foundry') unless defined $font;
79 $_ = "FONT -$font-$'";
80 }
81 if ($state eq 'idle' && m/^STARTCHAR\s/) {
82 die unless defined $foundry;
83 die unless defined $font;
84 return (0,'foundry != font') unless $foundry eq $font;
85 $state='startchar';
86 $w=undef;
87 }
88 if ($state eq 'startchar') {
89 if (m/^BBX\s+(\+?\d+)\s+(\+?\d+)\s+([-+]?\d+)\s+([-+]?\d+)\s+$/) {
90 ($w,$h,$xo,$yo) = ($1,$2,$3,$4);
91 }
92 if (m/^BITMAP\s+$/) {
93 die unless defined $w;
94 $y=0;
95 $glyph='';
96 $state='bitmap';
97 }
98 }
99 print $outbdf $_ or die $!;
100 }
101 die $! if $inbdf->error;
102 die $! if $outbdf->error or !$outbdf->flush;
103 die unless $state eq 'idle';
104 if ($modified) {
105 printf $reportfh "%s: %d glyphs changed\n", $what, $modified
106 or die $!;
107 } else {
108 printf $reportfh "%s: unchanged - no rules matched\n", $what
109 or die $!;
110 }
111 return $modified;
112 }
113
114 our (@options)=(
115 'R|rules-include=s@' => \@rulespath,
116 'share-dir=s' => \$sharedir,
117 'verbose|v+' => \$verbose,
118 );
119
120 sub define_mode ($$) {
121 my ($optname,$f) = @_;
122 push @options, $optname, sub {
123 die "only one mode may be specified\n" if defined $mode;
124 $mode=$f;
125 };
126 }
127
128 sub loadfoundries () {
129 foreach my $path (@rulespath) {
130 my $p = "$path/foundries";
131 my $f = new IO::File $p;
132 if (!$f) {
133 die "$p $!" unless $!==&ENOENT;
134 print $reportfh "foundries: none in $p\n" or die $! if $verbose;
135 next;
136 }
137 while (<$f>) {
138 s/^\s*//; s/\s+$//;
139 next if m/^\#/;
140 m/^(\w+)\s+(\w+)$/ or die;
141 my $k = lc $1;
142 next if exists $foundrymap{$k};
143 $foundrymap{$k}=$2;
144 }
145 $f->error and die $!;
146 reportloaded('foundries: loaded ',$p);
147 }
148 die "no foundry maps\n" unless %foundrymap;
149 }
150
151 our $stdin = new IO::File '<&STDIN' or die $!;
152 our $stdout = new IO::File '>&STDOUT' or die $!;
153 our $stderr = new IO::File '>&STDERR' or die $!;
154 $reportfh = $stdout;
155
156 define_mode('bdf-filter', sub {
157 die "no arguments allowed with --bdf-filter\n" if @ARGV;
158 $reportfh = $stderr;
159 loadfoundries();
160 my $r = processbdf($stdin,$stdout,'stdin');
161 if ($r !~ m/^\d/) {
162 print STDERR "stdin not processed: $r\n";
163 exit 2;
164 }
165 });
166
167 Getopt::Long::Configure(qw(bundling));
168 GetOptions(@options) or exit 127;
169
170 push @rulespath, "$sharedir/rules";
171
172 die "need a mode\n" unless $mode;
173
174 $mode->();
175
176 # 70 zcat /usr/share/fonts/X11/misc/6x13.pcf.gz |pcf2bdf >in.bdf
177 # 71 ./utility <in.bdf >out.bdf
178 # 83 bdftopcf out.bdf >out.pcf
179 # 84 gzip out.pcf
180 # 85 cp out.pcf.gz /usr/share/fonts/X11/misc/
181 # really mkfontdir /usr/share/fonts/X11/misc/
182 # xset fp rehash
183 # xfontsel