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) : (),
86 ########## length of a character ##########
90 return $uv < 0x80 ? 1 :
94 croak "$PACKAGE: illegal char in the composite. codepoint max is 0x10ffff.";
99 return $uv < 0xA0 ? 1 :
104 croak "$PACKAGE: illegal char in the composite. codepoint max is 0x10ffff.";
107 my $errExpand = "$PACKAGE: Composition to U+%04X (from U+%04X and U+%04X) " .
108 "needs growing the string in %s! Quit. Please inform the author...";
110 ########## getting full decomposion ##########
113 foreach my $d (@INC) {
114 $f = File::Spec->catfile($d, "unicore", "CompositionExclusions.txt");
115 last if open($fh, $f);
116 $f = File::Spec->catfile($d, "unicode", "CompExcl.txt");
117 last if open($fh, $f);
120 croak "$PACKAGE: neither unicore/CompositionExclusions.txt "
121 . "nor unicode/CompExcl.txt is found in @INC" unless defined $f;
124 next if /^#/ or /^$/;
126 $Exclus{ hex($1) } = 1 if /([0-9A-Fa-f]+)/;
132 ## converts string "hhhh hhhh hhhh" to a numeric list
134 sub _getHexArray { map hex, $_[0] =~ /([0-9A-Fa-f]+)/g }
136 while ($Combin =~ /(.+)/g) {
137 my @tab = split /\t/, $1;
138 my $ini = hex $tab[0];
140 $Combin{$ini} = $tab[2];
142 $Combin{$_} = $tab[2] foreach $ini .. hex($tab[1]);
146 while ($Decomp =~ /(.+)/g) {
147 my @tab = split /\t/, $1;
148 my $compat = $tab[2] =~ s/<[^>]+>//;
149 my $dec = [ _getHexArray($tab[2]) ]; # decomposition
150 my $ini = hex($tab[0]);
151 my $end = $tab[1] eq '' ? $ini : hex($tab[1]);
152 # ($ini .. $end) is the range of decomposable characters.
155 @$dec == 2 ? sprintf("${structname}_%06x", $dec->[0]) : 'USELESS';
156 # %04x is bad since it'd place _3046 after _1d157.
158 foreach my $u ($ini .. $end) {
165 if (utf8len($dec->[0]) + utf8len($dec->[1]) < utf8len($u)) {
166 croak sprintf $errExpand, $u, $dec->[0], $dec->[1],
169 if (utfelen($dec->[0]) + utfelen($dec->[1]) < utfelen($u)) {
170 croak sprintf $errExpand, $u, $dec->[0], $dec->[1],
174 if ($Combin{ $dec->[0] }) {
177 $CompList{ $listname }{ $dec->[1] } = $u;
178 $Comp1st{ $dec->[0] } = $listname;
179 $Comp2nd{ $dec->[1] } = 1 if ! $Exclus{$u};
181 } elsif (@$dec == 1) {
184 croak("Weird Canonical Decomposition of U+$tab[0]");
190 # modern HANGUL JUNGSEONG and HANGUL JONGSEONG jamo
191 foreach my $j (0x1161..0x1175, 0x11A8..0x11C2) {
198 (SBase <= $_ && $_ <= SFinal) ? decomposeHangul($_)
199 : $Canon{$_} ? @{ $Canon{$_} } : $_
201 return join(" ",@src) eq join(" ",@dec) ? @dec : getCanonList(@dec);
202 # condition @src == @dec is not ok.
208 (SBase <= $_ && $_ <= SFinal) ? decomposeHangul($_)
209 : $Compat{$_} ? @{ $Compat{$_} } : $_
211 return join(" ",@src) eq join(" ",@dec) ? @dec : getCompatList(@dec);
212 # condition @src == @dec is not ok.
215 # exhaustive decomposition
216 foreach my $key (keys %Canon) {
217 $Canon{$key} = [ getCanonList($key) ];
220 # exhaustive decomposition
221 foreach my $key (keys %Compat) {
222 $Compat{$key} = [ getCompatList($key) ];
226 return pack('U*', @_);
229 sub split_into_char {
232 my $len = length($uni);
234 for(my $i = 0; $i < $len; ++$i) {
235 push @ary, ord(substr($uni,$i,1));
241 sprintf '"%s"', join '',
242 map sprintf("\\x%02x", $_), split_into_char(_pack_U(@_));
245 foreach my $hash (\%Canon, \%Compat) {
246 foreach my $key (keys %$hash) {
247 $hash->{$key} = _U_stringify( @{ $hash->{$key} } );
251 ########## writing header files ##########
265 name => "NonStDecomp",
276 my $file = "unfexc.h";
277 open FH, ">$file" or croak "$PACKAGE: $file can't be made";
278 binmode FH; select FH;
282 * This file is auto-generated by mkheader.
283 * Any changes here will be lost!
287 foreach my $tbl (@boolfunc) {
288 my @temp = sort {$a <=> $b} keys %{$tbl->{hash}};
289 my $type = $tbl->{type};
290 my $name = $tbl->{name};
291 print "$type is$name (UV uv)\n{\nreturn\n\t";
294 my $cur = shift @temp;
295 if (@temp && $cur + 1 == $temp[0]) {
296 print "($cur <= uv && uv <= ";
297 while (@temp && $cur + 1 == $temp[0]) {
301 print "\n\t|| " if @temp;
304 print "\n\t|| " if @temp;
307 print "\n\t? TRUE : FALSE;\n}\n\n";
312 ####################################
315 "typedef struct { UV nextchar; UV composite; } $structname;\n\n";
317 foreach my $i (sort keys %CompList) {
318 $compinit .= "$structname $i [] = {\n";
319 $compinit .= join ",\n",
320 map sprintf("\t{ %d, %d }", $_, $CompList{$i}{$_}),
321 sort {$a <=> $b } keys %{ $CompList{$i} };
322 $compinit .= ",\n{0,0}\n};\n\n"; # with sentinel
350 type => "$structname *",
357 foreach my $tbl (@tripletable) {
358 my $file = "$tbl->{file}.h";
359 my $head = "${prefix}$tbl->{name}";
360 my $type = $tbl->{type};
361 my $hash = $tbl->{hash};
362 my $null = $tbl->{null};
363 my $init = $tbl->{init};
365 open FH, ">$file" or croak "$PACKAGE: $file can't be made";
366 binmode FH; select FH;
371 * This file is auto-generated by mkheader.
372 * Any changes here will be lost!
376 print $init if defined $init;
378 foreach my $uv (keys %$hash) {
379 croak sprintf("a Unicode code point 0x%04X over 0x10FFFF.", $uv)
380 unless $uv <= 0x10FFFF;
381 my @c = unpack 'CCCC', pack 'N', $uv;
382 $val{ $c[1] }{ $c[2] }{ $c[3] } = $hash->{$uv};
385 foreach my $p (sort { $a <=> $b } keys %val) {
386 next if ! $val{ $p };
387 for (my $r = 0; $r < 256; $r++) {
388 next if ! $val{ $p }{ $r };
389 printf "static $type ${head}_%02x_%02x [256] = {\n", $p, $r;
390 for (my $c = 0; $c < 256; $c++) {
391 print "\t", defined $val{$p}{$r}{$c}
392 ? "($type)".$val{$p}{$r}{$c}
394 print ',' if $c != 255;
395 print "\n" if $c % 8 == 7;
400 foreach my $p (sort { $a <=> $b } keys %val) {
401 next if ! $val{ $p };
402 printf "static $type* ${head}_%02x [256] = {\n", $p;
403 for (my $r = 0; $r < 256; $r++) {
404 print $val{ $p }{ $r }
405 ? sprintf("${head}_%02x_%02x", $p, $r)
407 print ',' if $r != 255;
408 print "\n" if $val{ $p }{ $r } || ($r+1) % 8 == 0;
412 print "static $type** $head [] = {\n";
413 for (my $p = 0; $p <= 0x10; $p++) {
414 print $val{ $p } ? sprintf("${head}_%02x", $p) : "NULL";
415 print ',' if $p != 0x10;