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