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