1 package Unicode::Normalize;
7 use Lingua::KO::Hangul::Util;
10 our $PACKAGE = __PACKAGE__;
13 our @ISA = qw(Exporter);
14 our @EXPORT = qw( NFC NFD NFKC NFKD );
15 our @EXPORT_OK = qw( normalize );
16 our %EXPORT_TAGS = ( all => [ @EXPORT, @EXPORT_OK ] );
18 our $Combin = do "unicore/CombiningClass.pl"
19 || do "unicode/CombiningClass.pl"
20 || croak "$PACKAGE: CombiningClass.pl not found";
22 our $Decomp = do "unicore/Decomposition.pl"
23 || do "unicode/Decomposition.pl"
24 || croak "$PACKAGE: Decomposition.pl not found";
26 our %Combin; # $codepoint => $number : combination class
27 our %Canon; # $codepoint => \@codepoints : canonical decomp.
28 our %Compat; # $codepoint => \@codepoints : compat. decomp.
29 our %Compos; # $string => $codepoint : composite
30 our %Exclus; # $codepoint => 1 : composition exclusions
34 foreach my $d (@INC) {
36 $f = File::Spec->catfile($d, "unicore", "CompExcl.txt");
37 last if open($fh, $f);
38 $f = File::Spec->catfile($d, "unicode", "CompExcl.txt");
39 last if open($fh, $f);
42 croak "$PACKAGE: CompExcl.txt not found in @INC" unless defined $f;
46 $Exclus{ hex($1) } =1 if /([0-9A-Fa-f]+)/;
51 while($Combin =~ /(.+)/g)
53 my @tab = split /\t/, $1;
54 my $ini = hex $tab[0];
57 $Combin{ $ini } = $tab[2];
61 $Combin{ $_ } = $tab[2] foreach $ini .. hex($tab[1]);
65 while($Decomp =~ /(.+)/g)
67 my @tab = split /\t/, $1;
68 my $compat = $tab[2] =~ s/<[^>]+>//;
69 my $dec = [ _getHexArray($tab[2]) ]; # decomposition
70 my $com = pack('U*', @$dec); # composable sequence
71 my $ini = hex($tab[0]);
74 $Compat{ $ini } = $dec;
76 $Canon{ $ini } = $dec;
77 $Compos{ $com } = $ini;
82 foreach my $u ($ini .. hex($tab[1])){
86 $Compos{ $com } = $ini;
92 foreach my $key (keys %Canon) # exhaustive decomposition
94 $Canon{$key} = [ getCanonList($key) ];
97 foreach my $key (keys %Compat) # exhaustive decomposition
99 $Compat{$key} = [ getCompatList($key) ];
105 my @dec = map $Canon{$_} ? @{ $Canon{$_} } : $_, @src;
106 join(" ",@src) eq join(" ",@dec) ? @dec : getCanonList(@dec);
107 # condition @src == @dec is not ok.
113 my @dec = map $Compat{$_} ? @{ $Compat{$_} } : $_, @src;
114 join(" ",@src) eq join(" ",@dec) ? @dec : getCompatList(@dec);
115 # condition @src == @dec is not ok.
118 sub NFD($){ _decompose(shift, 0) }
120 sub NFKD($){ _decompose(shift, 1) }
122 sub NFC($){ _compose(NFD(shift)) }
124 sub NFKC($){ _compose(NFKD(shift)) }
129 $form eq 'D' || $form eq 'NFD' ? NFD($str) :
130 $form eq 'C' || $form eq 'NFC' ? NFC($str) :
131 $form eq 'KD' || $form eq 'NFKD' ? NFKD($str) :
132 $form eq 'KC' || $form eq 'NFKC' ? NFKC($str) :
133 croak $PACKAGE."::normalize: invalid form name: $form";
138 ## string _decompose(string, compat?)
143 my $hash = $_[1] ? \%Compat : \%Canon;
146 foreach my $u (unpack 'U*', $str){
148 $hash->{ $u } ? @{ $hash->{ $u } } :
149 _isHangul($u) ? decomposeHangul($u) : $u;
151 for(my $i=0; $i<@ret;){
152 $retstr .= pack('U', $ret[$i++]), next
153 unless $Combin{ $ret[$i] } && $i+1 < @ret && $Combin{ $ret[$i+1] };
155 push(@tmp, $ret[$i++]) while $i < @ret && $Combin{ $ret[$i] };
156 $retstr .= pack 'U*', @tmp[
158 $Combin{ $tmp[$a] } <=> $Combin{ $tmp[$b] } || $a <=> $b
166 ## string _compose(string)
168 ## S : starter; NS : not starter;
170 ## composable sequence begins at S.
171 ## S + S or (S + S) + S may be composed.
172 ## NS + NS must not be composed.
176 my @src = unpack('U*', composeHangul shift); # get codepoints
177 for(my $s = 0; $s+1 < @src; $s++){
178 next unless defined $src[$s] && ! $Combin{ $src[$s] }; # S only
180 for(my $j = $s+1; $j < @src && !$blocked; $j++){
181 $blocked = 1 if ! $Combin{ $src[$j] };
183 next if $j != $s + 1 && defined $src[$j-1]
184 && $Combin{ $src[$j-1] } && $Combin{ $src[$j] }
185 && $Combin{ $src[$j-1] } == $Combin{ $src[$j] };
187 if( # $c != 0, maybe.
188 $c = $Compos{pack('U*', @src[$s,$j])} and ! $Exclus{$c}
191 $src[$s] = $c; $src[$j] = undef; $blocked = 0;
195 pack 'U*', grep defined(), @src;
199 ## "hhhh hhhh hhhh" to (dddd, dddd, dddd)
204 map hex(), $str =~ /([0-9A-Fa-f]+)/g;
213 return 0xAC00 <= $code && $code <= 0xD7A3;
219 sub _getCombin { wantarray ? %Combin : \%Combin }
220 sub _getCanon { wantarray ? %Canon : \%Canon }
221 sub _getCompat { wantarray ? %Compat : \%Compat }
222 sub _getCompos { wantarray ? %Compos : \%Compos }
223 sub _getExclus { wantarray ? %Exclus : \%Exclus }
229 Unicode::Normalize - normalized forms of Unicode text
233 use Unicode::Normalize;
235 $string_NFD = NFD($raw_string); # Normalization Form D
236 $string_NFC = NFC($raw_string); # Normalization Form C
237 $string_NFKD = NFKD($raw_string); # Normalization Form KD
238 $string_NFKC = NFKC($raw_string); # Normalization Form KC
242 use Unicode::Normalize 'normalize';
244 $string_NFD = normalize('D', $raw_string); # Normalization Form D
245 $string_NFC = normalize('C', $raw_string); # Normalization Form C
246 $string_NFKD = normalize('KD', $raw_string); # Normalization Form KD
247 $string_NFKC = normalize('KC', $raw_string); # Normalization Form KC
253 =item C<$string_NFD = NFD($raw_string)>
255 returns the Normalization Form D (formed by canonical decomposition).
258 =item C<$string_NFC = NFC($raw_string)>
260 returns the Normalization Form C (formed by canonical decomposition
261 followed by canonical composition).
263 =item C<$string_NFKD = NFKD($raw_string)>
265 returns the Normalization Form KD (formed by compatibility decomposition).
267 =item C<$string_NFKC = NFKC($raw_string)>
269 returns the Normalization Form KC (formed by compatibility decomposition
270 followed by B<canonical> composition).
272 =item C<$normalized_string = normalize($form_name, $raw_string)>
274 As C<$form_name>, one of the following names must be given.
276 'C' or 'NFC' for Normalization Form C
277 'D' or 'NFD' for Normalization Form D
278 'KC' or 'NFKC' for Normalization Form KC
279 'KD' or 'NFKD' for Normalization Form KD
285 C<NFC>, C<NFD>, C<NFKC>, C<NFKD>: by default.
287 C<normalize>: on request.
291 SADAHIRO Tomoyuki, E<lt>SADAHIRO@cpan.orgE<gt>
293 http://homepage1.nifty.com/nomenclator/perl/
295 Copyright(C) 2001, SADAHIRO Tomoyuki. Japan. All rights reserved.
297 This program is free software; you can redistribute it and/or
298 modify it under the same terms as Perl itself.
304 =item L<Lingua::KO::Hangul::Util>
306 utility functions for Hangul Syllables
308 =item http://www.unicode.org/unicode/reports/tr15/
310 Unicode Normalization Forms - UAX #15