3 # This auxiliary script makes five header files
4 # used for building XSUB of Unicode::Normalize.
7 # <do 'mkheader'> in perl, or <perl mkheader> in command line
10 # unicore/CombiningClass.pl (or unicode/CombiningClass.pl)
11 # unicore/Decomposition.pl (or unicode/Decomposition.pl)
12 # unicore/CompositionExclusions.txt (or unicode/CompExcl.txt)
28 unless ("A" eq pack('U', 0x41)) {
29 die "Unicode::Normalize cannot stringify a Unicode code point\n";
33 our $PACKAGE = 'Unicode::Normalize, mkheader';
35 our $Combin = do "unicore/CombiningClass.pl"
36 || do "unicode/CombiningClass.pl"
37 || croak "$PACKAGE: CombiningClass.pl not found";
39 our $Decomp = do "unicore/Decomposition.pl"
40 || do "unicode/Decomposition.pl"
41 || croak "$PACKAGE: Decomposition.pl not found";
43 our %Combin; # $codepoint => $number : combination class
44 our %Canon; # $codepoint => \@codepoints : canonical decomp.
45 our %Compat; # $codepoint => \@codepoints : compat. decomp.
46 # after _U_stringify(), ($codepoint => $hexstring) for %Canon and %Compat
47 our %Exclus; # $codepoint => 1 : composition exclusions
48 our %Single; # $codepoint => 1 : singletons
49 our %NonStD; # $codepoint => 1 : non-starter decompositions
51 our %Comp1st; # $codepoint => $listname : may be composed with a next char.
52 our %Comp2nd; # $codepoint => 1 : may be composed with a prev char.
53 our %CompList; # $listname,$2nd => $codepoint : composite
56 our $structname = "${prefix}complist";
58 ########## definition of Hangul constants ##########
59 use constant SBase => 0xAC00;
60 use constant SFinal => 0xD7A3; # SBase -1 + SCount
61 use constant SCount => 11172; # LCount * NCount
62 use constant NCount => 588; # VCount * TCount
63 use constant LBase => 0x1100;
64 use constant LFinal => 0x1112;
65 use constant LCount => 19;
66 use constant VBase => 0x1161;
67 use constant VFinal => 0x1175;
68 use constant VCount => 21;
69 use constant TBase => 0x11A7;
70 use constant TFinal => 0x11C2;
71 use constant TCount => 28;
74 my $SIndex = $_[0] - SBase;
75 my $LIndex = int( $SIndex / NCount);
76 my $VIndex = int(($SIndex % NCount) / TCount);
77 my $TIndex = $SIndex % TCount;
81 $TIndex ? (TBase + $TIndex) : (),
83 wantarray ? @ret : pack('U*', @ret);
84 # any element in @ret greater than 0xFF, so no need of u2n conversion.
87 ########## getting full decomposion ##########
90 foreach my $d (@INC) {
91 $f = File::Spec->catfile($d, "unicore", "CompositionExclusions.txt");
92 last if open($fh, $f);
93 $f = File::Spec->catfile($d, "unicode", "CompExcl.txt");
94 last if open($fh, $f);
97 croak "$PACKAGE: neither unicore/CompositionExclusions.txt "
98 . "nor unicode/CompExcl.txt is found in @INC" unless defined $f;
101 next if /^#/ or /^$/;
103 $Exclus{ hex($1) } = 1 if /([0-9A-Fa-f]+)/;
109 ## converts string "hhhh hhhh hhhh" to a numeric list
111 sub _getHexArray { map hex, $_[0] =~ /([0-9A-Fa-f]+)/g }
113 while ($Combin =~ /(.+)/g) {
114 my @tab = split /\t/, $1;
115 my $ini = hex $tab[0];
117 $Combin{ $ini } = $tab[2];
119 $Combin{ $_ } = $tab[2] foreach $ini .. hex($tab[1]);
123 while ($Decomp =~ /(.+)/g) {
124 my @tab = split /\t/, $1;
125 my $compat = $tab[2] =~ s/<[^>]+>//;
126 my $dec = [ _getHexArray($tab[2]) ]; # decomposition
127 my $ini = hex($tab[0]); # initial decomposable character
130 @$dec == 2 ? sprintf("${structname}_%06x", $dec->[0]) : 'USELESS';
131 # %04x is bad since it'd place _3046 after _1d157.
134 $Compat{ $ini } = $dec;
137 $Canon{ $ini } = $dec;
140 if ($Combin{ $dec->[0] }) {
143 $CompList{ $listname }{ $dec->[1] } = $ini;
144 $Comp1st{ $dec->[0] } = $listname;
145 $Comp2nd{ $dec->[1] } = 1 if ! $Exclus{$ini};
147 } elsif (@$dec == 1) {
150 croak("Weird Canonical Decomposition of U+$tab[0]");
154 foreach my $u ($ini .. hex($tab[1])) {
155 $Compat{ $u } = $dec;
161 if ($Combin{ $dec->[0] }) {
164 $CompList{ $listname }{ $dec->[1] } = $u;
165 $Comp1st{ $dec->[0] } = $listname;
166 $Comp2nd{ $dec->[1] } = 1 if ! $Exclus{$u};
168 } elsif (@$dec == 1) {
171 croak("Weird Canonical Decomposition of U+$tab[0]");
178 # modern HANGUL JUNGSEONG and HANGUL JONGSEONG jamo
179 foreach my $j (0x1161..0x1175, 0x11A8..0x11C2) {
186 (SBase <= $_ && $_ <= SFinal) ? decomposeHangul($_)
187 : $Canon{$_} ? @{ $Canon{$_} } : $_
189 return join(" ",@src) eq join(" ",@dec) ? @dec : getCanonList(@dec);
190 # condition @src == @dec is not ok.
196 (SBase <= $_ && $_ <= SFinal) ? decomposeHangul($_)
197 : $Compat{$_} ? @{ $Compat{$_} } : $_
199 return join(" ",@src) eq join(" ",@dec) ? @dec : getCompatList(@dec);
200 # condition @src == @dec is not ok.
203 # exhaustive decomposition
204 foreach my $key (keys %Canon) {
205 $Canon{$key} = [ getCanonList($key) ];
208 # exhaustive decomposition
209 foreach my $key (keys %Compat) {
210 $Compat{$key} = [ getCompatList($key) ];
214 return pack('U*', @_);
218 sprintf '"%s"', join '',
219 map sprintf("\\x%02x", $_), unpack 'C*', _pack_U(@_);
222 foreach my $hash (\%Canon, \%Compat) {
223 foreach my $key (keys %$hash) {
224 $hash->{$key} = _U_stringify( @{ $hash->{$key} } );
228 ########## writing header files ##########
242 name => "NonStDecomp",
253 my $file = "unfexc.h";
254 open FH, ">$file" or croak "$PACKAGE: $file can't be made";
255 binmode FH; select FH;
259 * This file is auto-generated by mkheader.
260 * Any changes here will be lost!
264 foreach my $tbl (@boolfunc) {
265 my @temp = sort {$a <=> $b} keys %{$tbl->{hash}};
266 my $type = $tbl->{type};
267 my $name = $tbl->{name};
268 print "$type is$name (UV uv)\n{\nreturn\n\t";
271 my $cur = shift @temp;
272 if (@temp && $cur + 1 == $temp[0]) {
273 print "($cur <= uv && uv <= ";
274 while (@temp && $cur + 1 == $temp[0]) {
278 print "\n\t|| " if @temp;
281 print "\n\t|| " if @temp;
284 print "\n\t? TRUE : FALSE;\n}\n\n";
289 ####################################
292 "typedef struct { UV nextchar; UV composite; } $structname;\n\n";
294 foreach my $i (sort keys %CompList) {
295 $compinit .= "$structname $i [] = {\n";
296 $compinit .= join ",\n",
297 map sprintf("\t{ %d, %d }", $_, $CompList{$i}{$_}),
298 sort {$a <=> $b } keys %{ $CompList{$i} };
299 $compinit .= ",\n{0,0}\n};\n\n"; # with sentinel
327 type => "$structname *",
334 foreach my $tbl (@tripletable) {
335 my $file = "$tbl->{file}.h";
336 my $head = "${prefix}$tbl->{name}";
337 my $type = $tbl->{type};
338 my $hash = $tbl->{hash};
339 my $null = $tbl->{null};
340 my $init = $tbl->{init};
342 open FH, ">$file" or croak "$PACKAGE: $file can't be made";
343 binmode FH; select FH;
348 * This file is auto-generated by mkheader.
349 * Any changes here will be lost!
353 print $init if defined $init;
355 foreach my $uv (keys %$hash) {
356 croak sprintf("a Unicode code point 0x%04X over 0x10FFFF.", $uv)
357 unless $uv <= 0x10FFFF;
358 my @c = unpack 'CCCC', pack 'N', $uv;
359 $val{ $c[1] }{ $c[2] }{ $c[3] } = $hash->{$uv};
362 foreach my $p (sort { $a <=> $b } keys %val) {
363 next if ! $val{ $p };
364 for (my $r = 0; $r < 256; $r++) {
365 next if ! $val{ $p }{ $r };
366 printf "static $type ${head}_%02x_%02x [256] = {\n", $p, $r;
367 for (my $c = 0; $c < 256; $c++) {
368 print "\t", defined $val{$p}{$r}{$c}
369 ? "($type)".$val{$p}{$r}{$c}
371 print ',' if $c != 255;
372 print "\n" if $c % 8 == 7;
377 foreach my $p (sort { $a <=> $b } keys %val) {
378 next if ! $val{ $p };
379 printf "static $type* ${head}_%02x [256] = {\n", $p;
380 for (my $r = 0; $r < 256; $r++) {
381 print $val{ $p }{ $r }
382 ? sprintf("${head}_%02x_%02x", $p, $r)
384 print ',' if $r != 255;
385 print "\n" if $val{ $p }{ $r } || ($r+1) % 8 == 0;
389 print "static $type** $head [] = {\n";
390 for (my $p = 0; $p <= 0x10; $p++) {
391 print $val{ $p } ? sprintf("${head}_%02x", $p) : "NULL";
392 print ',' if $p != 0x10;