fcc8eac3028d687e60ffaa5176c285c0091555b8
[p5sagit/p5-mst-13.2.git] / lib / unicode / mktables.PL
1 #!../../miniperl
2
3 $UnicodeData = "UnicodeData-Latest.txt";
4
5 # Note: we try to keep filenames unique within first 8 chars.  Using
6 # subdirectories for the following helps.
7 mkdir "In", 0777;
8 mkdir "Is", 0777;
9 mkdir "To", 0777;
10 mkdir "Eq", 0777;
11
12 @todo = (
13 # typical
14
15     ['IsWord',  '$cat =~ /^L[ulo]|^Nd/ or $code eq "005F"',     ''],
16     ['IsAlnum', '$cat =~ /^L[ulo]|^Nd/',        ''],
17     ['IsAlpha',  '$cat =~ /^L[ulo]/',   ''],
18     ['IsSpace',  '$cat =~ /^Z/ or $code lt "0020" and chr(hex $code) =~ /^\s/', ''],
19     ['IsDigit',  '$cat =~ /^Nd$/',      ''],
20     ['IsUpper',  '$cat =~ /^Lu$/',      ''],
21     ['IsLower',  '$cat =~ /^Ll$/',      ''],
22     ['IsASCII',  'hex $code <= 127',    ''],
23     ['IsCntrl',  '$cat =~ /^C/',        ''],
24     ['IsGraph',  '$cat =~ /^[^C]/ and $code ne "0020"', ''],
25     ['IsPrint',  '$cat =~ /^[^C]/',     ''],
26     ['IsPunct',  '$cat =~ /^P/',        ''],
27     ['IsXDigit', '$code =~ /^00(3[0-9]|[46][1-6])$/',   ''],
28     ['ToUpper',  '$up',                 '$up'],
29     ['ToLower',  '$down',               '$down'],
30     ['ToTitle',  '$title',              '$title'],
31     ['ToDigit',  '$dec ne ""',          '$dec'],
32
33 # Name
34
35     ['Name',    '$name',                '$name'],
36
37 # Category
38
39     ['Category', '$cat',                '$cat'],
40
41 # Normative
42
43     ['IsM',     '$cat =~ /^M/',         ''],    # Mark
44     ['IsMn',    '$cat eq "Mn"',         ''],    # Mark, Non-Spacing 
45     ['IsMc',    '$cat eq "Mc"',         ''],    # Mark, Combining
46
47     ['IsN',     '$cat =~ /^N/',         ''],    # Number
48     ['IsNd',    '$cat eq "Nd"',         ''],    # Number, Decimal Digit
49     ['IsNo',    '$cat eq "No"',         ''],    # Number, Other
50
51     ['IsZ',     '$cat =~ /^Z/',         ''],    # Zeparator
52     ['IsZs',    '$cat eq "Zs"',         ''],    # Separator, Space
53     ['IsZl',    '$cat eq "Zl"',         ''],    # Separator, Line
54     ['IsZp',    '$cat eq "Zp"',         ''],    # Separator, Paragraph
55
56     ['IsC',     '$cat =~ /^C/',         ''],    # Crazy
57     ['IsCc',    '$cat eq "Cc"',         ''],    # Other, Control or Format
58     ['IsCo',    '$cat eq "Co"',         ''],    # Other, Private Use
59     ['IsCn',    '$cat eq "Cn"',         ''],    # Other, Not Assigned
60  
61 # Informative
62
63     ['IsL',     '$cat =~ /^L/',         ''],    # Letter
64     ['IsLu',    '$cat eq "Lu"',         ''],    # Letter, Uppercase
65     ['IsLl',    '$cat eq "Ll"',         ''],    # Letter, Lowercase
66     ['IsLt',    '$cat eq "Lt"',         ''],    # Letter, Titlecase 
67     ['IsLm',    '$cat eq "Lm"',         ''],    # Letter, Modifier
68     ['IsLo',    '$cat eq "Lo"',         ''],    # Letter, Other 
69
70     ['IsP',     '$cat =~ /^P/',         ''],    # Punctuation
71     ['IsPd',    '$cat eq "Pd"',         ''],    # Punctuation, Dash
72     ['IsPs',    '$cat eq "Ps"',         ''],    # Punctuation, Open
73     ['IsPe',    '$cat eq "Pe"',         ''],    # Punctuation, Close
74     ['IsPo',    '$cat eq "Po"',         ''],    # Punctuation, Other
75
76     ['IsS',     '$cat =~ /^S/',         ''],    # Symbol
77     ['IsSm',    '$cat eq "Sm"',         ''],    # Symbol, Math
78     ['IsSc',    '$cat eq "Sc"',         ''],    # Symbol, Currency
79     ['IsSo',    '$cat eq "So"',         ''],    # Symbol, Other
80
81 # Combining class
82     ['CombiningClass', '$comb',         '$comb'],
83
84 # BIDIRECTIONAL PROPERTIES
85  
86     ['Bidirectional', '$bid',           '$bid'],
87
88 # Strong types:
89
90     ['IsBidiL', '$bid eq "L"',          ''],    # Left-Right; Most alphabetic,
91                                                 # syllabic, and logographic
92                                                 # characters (e.g., CJK
93                                                 # ideographs)
94     ['IsBidiR', '$bid eq "R"',          ''],    # Right-Left; Arabic, Hebrew,
95                                                 # and punctuation specific to
96                                                 # those scripts
97
98 # Weak types:
99
100     ['IsBidiEN','$bid eq "EN"',         ''],    # European Number
101     ['IsBidiES','$bid eq "ES"',         ''],    # European Number Separator
102     ['IsBidiET','$bid eq "ET"',         ''],    # European Number Terminator
103     ['IsBidiAN','$bid eq "AN"',         ''],    # Arabic Number
104     ['IsBidiCS','$bid eq "CS"',         ''],    # Common Number Separator
105
106 # Separators:
107
108     ['IsBidiB', '$bid eq "B"',          ''],    # Block Separator
109     ['IsBidiS', '$bid eq "S"',          ''],    # Segment Separator
110
111 # Neutrals:
112
113     ['IsBidiWS','$bid eq "WS"',         ''],    # Whitespace
114     ['IsBidiON','$bid eq "ON"',         ''],    # Other Neutrals ; All other
115                                                 # characters: punctuation,
116                                                 # symbols
117
118 # Decomposition
119
120     ['Decomposition',   '$decomp',      '$decomp'],
121     ['IsDecoCanon',     '$decomp && $decomp !~ /^</',   ''],
122     ['IsDecoCompat',    '$decomp =~ /^</',              ''],
123     ['IsDCfont',        '$decomp =~ /^<font>/',         ''],
124     ['IsDCnoBreak',     '$decomp =~ /^<noBreak>/',      ''],
125     ['IsDCinitial',     '$decomp =~ /^<initial>/',      ''],
126     ['IsDCinital',      '$decomp =~ /^<medial>/',       ''],
127     ['IsDCfinal',       '$decomp =~ /^<final>/',        ''],
128     ['IsDCisolated',    '$decomp =~ /^<isolated>/',     ''],
129     ['IsDCcircle',      '$decomp =~ /^<circle>/',       ''],
130     ['IsDCsuper',       '$decomp =~ /^<super>/',        ''],
131     ['IsDCsub',         '$decomp =~ /^<sub>/',          ''],
132     ['IsDCvertical',    '$decomp =~ /^<vertical>/',     ''],
133     ['IsDCwide',        '$decomp =~ /^<wide>/',         ''],
134     ['IsDCnarrow',      '$decomp =~ /^<narrow>/',       ''],
135     ['IsDCsmall',       '$decomp =~ /^<small>/',        ''],
136     ['IsDCsquare',      '$decomp =~ /^<square>/',       ''],
137     ['IsDCcompat',      '$decomp =~ /^<compat>/',       ''],
138
139 # Number
140
141     ['Number',  '$num',                 '$num'],
142
143 # Mirrored
144
145     ['IsMirrored', '$mir eq "Y"',       ''],
146
147 # Arabic
148
149     ['ArabLink',        '1',            '$link'],
150     ['ArabLnkGrp',      '1',            '$linkgroup'],
151
152 # Jamo
153
154     ['JamoShort',       '1',            '$short'],
155 );
156
157 # This is not written for speed...
158
159 foreach $file (@todo) {
160     my ($table, $wanted, $val) = @$file;
161     next if @ARGV and not grep { $_ eq $table } @ARGV;
162     print $table,"\n";
163     if ($table =~ /^(Is|In|To)(.*)/) {
164         open(OUT, ">$1/$2.pl") or die "Can't create $1/$2.pl: $!\n";
165     }
166     else {
167         open(OUT, ">$table.pl") or die "Can't create $table.pl: $!\n";
168     }
169     print OUT <<"END";
170 return <<'END';
171 END
172     print OUT proplist($table, $wanted, $val);
173     print OUT "END\n";
174     close OUT;
175 }
176
177 # Must treat blocks specially.
178
179 exit if @ARGV and not grep { $_ eq Block } @ARGV;
180 print "Block\n";
181 open(UD, 'blocks.txt') or die "Can't open blocks.txt: $!\n";
182 open(OUT, ">Block.pl") or die "Can't create $table.pl: $!\n";
183 print OUT <<"END";
184 return <<'END';
185 END
186
187 while (<UD>) {
188     next if /^#/;
189     next if /^$/;
190     chomp;
191     ($code, $last, $name) = split(/; */);
192     if ($name) {
193         print OUT "$code        $last   $name\n";
194         $name =~ s/\s+//g;
195         open(BLOCK, ">In/$name.pl");
196         print BLOCK <<"END2";
197 return <<'END';
198 $code   $last
199 END
200 END2
201         close BLOCK;
202     }
203 }
204
205 print OUT "END\n";
206 close OUT;
207
208 ##################################################
209
210 sub proplist {
211     my ($table, $wanted, $val) = @_;
212     my @wanted;
213     my $out;
214     my $split;
215
216     if ($table =~ /^Arab/) {
217         open(UD, "arabshp.txt") or warn "Can't open $table: $!";
218
219         $split = '($code, $name, $link, $linkgroup) = split(/; */);';
220     }
221     elsif ($table =~ /^Jamo/) {
222         open(UD, "jamo2.txt") or warn "Can't open $table: $!";
223
224         $split = '($code, $short, $name) = split(/; */); $code =~ s/^U\+//;';
225     }
226     else {
227         open(UD, $UnicodeData) or warn "Can't open $UnicodeData: $!";
228
229         $split = '($code, $name, $cat, $comb, $bid, $decomp, $dec, $dig, $num, $mir, $uni1,
230                 $comment, $up, $down, $title) = split(/;/);';
231     }
232
233     if ($table =~ /^(?:To|Is)[A-Z]/) {
234         eval <<"END";
235             while (<UD>) {
236                 next if /^#/;
237                 next if /^\s/;
238                 chop;
239                 $split
240                 if ($wanted) {
241                     push(\@wanted, [hex \$code, hex $val, \$name =~ /, First>\$/]);
242                 }
243             }
244 END
245         die $@ if $@;
246
247         while (@wanted) {
248             $beg = shift @wanted;
249             $last = $beg;
250             while (@wanted and $wanted[0]->[0] == $last->[0] + 1 and
251                 (not $val or $wanted[0]->[1] == $last->[1] + 1)) {
252                     $last = shift @wanted;
253             }
254             $out .= sprintf "%04x", $beg->[0];
255             if ($beg->[2]) {
256                 $last = shift @wanted;
257             }
258             if ($beg == $last) {
259                 $out .= "\t";
260             }
261             else {
262                 $out .= sprintf "\t%04x", $last->[0];
263             }
264             $out .= sprintf "\t%04x", $beg->[1] if $val;
265             $out .= "\n";
266         }
267     }
268     else {
269         eval <<"END";
270             while (<UD>) {
271                 next if /^#/;
272                 next if /^\s*\$/;
273                 chop;
274                 $split
275                 if ($wanted) {
276                     push(\@wanted, [hex \$code, $val, \$name =~ /, First>\$/]);
277                 }
278             }
279 END
280         die $@ if $@;
281
282         while (@wanted) {
283             $beg = shift @wanted;
284             $last = $beg;
285             while (@wanted and $wanted[0]->[0] == $last->[0] + 1 and
286                 ($wanted[0]->[1] eq $last->[1])) {
287                     $last = shift @wanted;
288             }
289             $out .= sprintf "%04x", $beg->[0];
290             if ($beg->[2]) {
291                 $last = shift @wanted;
292             }
293             if ($beg == $last) {
294                 $out .= "\t";
295             }
296             else {
297                 $out .= sprintf "\t%04x", $last->[0];
298             }
299             $out .= sprintf "\t%s\n", $beg->[1];
300         }
301     }
302     $out;
303 }
304
305 # Create the equivalence mappings.
306
307 open(UNICODEDATA, $UnicodeData) || die "$0: $UnicodeData: $!\n";
308
309 while (<UNICODEDATA>) {
310     ($code, $name) = split /;/;
311     
312     $code{$name} = $code;
313     $name{$code} = $name;
314
315     if ($name =~ /^((?:LATIN|GREEK|CYRILLIC|HEBREW|BENGALI) .+? LETTER .+?) WITH /) {
316         push @base, [ $code, $1 ];
317     } elsif ($name =~ /^(ARABIC LETTER \w+?) WITH .+ (\w+ FORM)$/) {
318         push @base, [ $code, "$1 $2" ];
319     } elsif ($name =~ /^(ARABIC LETTER \w+?) WITH /) {
320         push @base, [ $code, $1 ];
321 # Is the concept of turning ligatures into character classes sound?
322     } elsif ($name =~ /^(ARABIC) LIGATURE (.+?) (WITH .+ )+(\w+ FORM)$/) {
323         my $script = $1;
324         my $base   = $2;
325         my $with   = $3;
326         my $form   = $4;
327         push @base, [ $code, "$script LETTER $base" ];
328         push @base, [ $code, "$script LETTER $base $form" ];
329         my @with = split(/\bWITH\s+/, $with);
330         shift @with;
331         @with = grep { ! /^ (?:ABOVE|BELOW)/ } @with;
332         foreach my $base (@with) {
333             push @base, [ $code, "$script LETTER $base" ];
334             push @base, [ $code, "$script LETTER $base $form" ];
335         }
336     } elsif ($name =~ /^((?:ARMENIAN|CYRILLIC) .+) LIGATURE (\w+) (\w+)$/) {
337         push @base, [ $code, "$1 LETTER $2" ];
338         push @base, [ $code, "$1 LETTER $3" ];
339 # Latin ligatures (ae, oe, ij, ff, fi, fl, ffi, ffl, long st, st) ignored.
340 # Hebrew Yiddish ligatures (double vav, vav yod, double yod, yod yod patah,
341 # alef lamed) ignored.
342     } else {
343         next;
344     }
345
346 }
347
348 foreach my $b (@base) {
349     ($code, $base) = @$b;
350     next unless exists $code{$base};
351     push @{$unicode{$code{$base}}}, $code;
352     print "$code: $name{$code} -> $base\n",
353 }
354
355 @unicode = sort keys %unicode;
356
357 if (open(EQ_UNICODE, ">Eq/Unicode")) {
358     foreach my $c (@unicode) {
359         print EQ_UNICODE "$c @{$unicode{$c}}\n";
360     }
361     close EQ_UNICODE;
362 } else {
363     die "$0: failed to open Eq/Unicode for writing: $!\n";
364 }
365
366 if (open(EQ_LATIN1, ">Eq/Latin1")) {
367     foreach my $c (@unicode) {
368         last if hex($c) > 255;
369         my @c = grep { hex($_) <= 255 } @{$unicode{$c}};
370         next unless @c;
371         print EQ_LATIN1 "$c @c\n";
372     }
373     close EQ_LATIN1;
374 } else {
375     die "$0: failed to open Eq/Latin1 for writing: $!\n";
376 }
377