intl/lwbrk/tools/anzx4051.pl

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 #!/usr/bin/perl
michael@0 2 #
michael@0 3 # This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 # License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
michael@0 6
michael@0 7 ######################################################################
michael@0 8 #
michael@0 9 # Initial global variable
michael@0 10 #
michael@0 11 ######################################################################
michael@0 12 %utot = ();
michael@0 13 $ui=0;
michael@0 14 $li=0;
michael@0 15
michael@0 16 ######################################################################
michael@0 17 #
michael@0 18 # Open the unicode database file
michael@0 19 #
michael@0 20 ######################################################################
michael@0 21 open ( UNICODATA , "< ../../unicharutil/tools/UnicodeData-Latest.txt")
michael@0 22 || die "cannot find UnicodeData-Latest.txt";
michael@0 23
michael@0 24 ######################################################################
michael@0 25 #
michael@0 26 # Open the JIS X 4051 Class file
michael@0 27 #
michael@0 28 ######################################################################
michael@0 29 open ( CLASS , "< jisx4051class.txt")
michael@0 30 || die "cannot find jisx4051class.txt";
michael@0 31
michael@0 32 ######################################################################
michael@0 33 #
michael@0 34 # Open the JIS X 4051 Class simplified mapping
michael@0 35 #
michael@0 36 ######################################################################
michael@0 37 open ( SIMP , "< jisx4051simp.txt")
michael@0 38 || die "cannot find jisx4051simp.txt";
michael@0 39
michael@0 40 ######################################################################
michael@0 41 #
michael@0 42 # Open the output file
michael@0 43 #
michael@0 44 ######################################################################
michael@0 45 open ( OUT , "> anzx4051.html")
michael@0 46 || die "cannot open output anzx4051.html file";
michael@0 47
michael@0 48 ######################################################################
michael@0 49 #
michael@0 50 # Open the output file
michael@0 51 #
michael@0 52 ######################################################################
michael@0 53 open ( HEADER , "> ../src/jisx4051class.h")
michael@0 54 || die "cannot open output ../src/jisx4051class.h file";
michael@0 55
michael@0 56 ######################################################################
michael@0 57 #
michael@0 58 # Generate license and header
michael@0 59 #
michael@0 60 ######################################################################
michael@0 61 $hthmlheader = <<END_OF_HTML;
michael@0 62 <!-- This Source Code Form is subject to the terms of the Mozilla Public
michael@0 63 - License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 64 - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
michael@0 65
michael@0 66 <HTML>
michael@0 67 <HEAD>
michael@0 68 <TITLE>
michael@0 69 Analysis of JIS X 4051 to Unicode General Category Mapping
michael@0 70 </TITLE>
michael@0 71 </HEAD>
michael@0 72 <BODY>
michael@0 73 <H1>
michael@0 74 Analysis of JIS X 4051 to Unicode General Category Mapping
michael@0 75 </H1>
michael@0 76 END_OF_HTML
michael@0 77 print OUT $hthmlheader;
michael@0 78
michael@0 79 ######################################################################
michael@0 80 #
michael@0 81 # Generate license and header
michael@0 82 #
michael@0 83 ######################################################################
michael@0 84 $npl = <<END_OF_NPL;
michael@0 85 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 86 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 87 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 88 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 89 /*
michael@0 90 DO NOT EDIT THIS DOCUMENT !!! THIS DOCUMENT IS GENERATED BY
michael@0 91 mozilla/intl/lwbrk/tools/anzx4051.pl
michael@0 92 */
michael@0 93 END_OF_NPL
michael@0 94 print HEADER $npl;
michael@0 95
michael@0 96 %occ = ();
michael@0 97 %gcat = ();
michael@0 98 %dcat = ();
michael@0 99 %simp = ();
michael@0 100 %gcount = ();
michael@0 101 %dcount = ();
michael@0 102 %sccount = ();
michael@0 103 %rangecount = ();
michael@0 104
michael@0 105 ######################################################################
michael@0 106 #
michael@0 107 # Process the file line by line
michael@0 108 #
michael@0 109 ######################################################################
michael@0 110 while(<UNICODATA>) {
michael@0 111 chop;
michael@0 112 ######################################################################
michael@0 113 #
michael@0 114 # Get value from fields
michael@0 115 #
michael@0 116 ######################################################################
michael@0 117 @f = split(/;/ , $_);
michael@0 118 $c = $f[0]; # The unicode value
michael@0 119 $g = $f[2];
michael@0 120 $d = substr($g, 0, 1);
michael@0 121
michael@0 122 $gcat{$c} = $g;
michael@0 123 $dcat{$c} = $d;
michael@0 124 $gcount{$g}++;
michael@0 125 $dcount{$d}++;
michael@0 126 }
michael@0 127 close(UNIDATA);
michael@0 128
michael@0 129 while(<SIMP>) {
michael@0 130 chop;
michael@0 131 ######################################################################
michael@0 132 #
michael@0 133 # Get value from fields
michael@0 134 #
michael@0 135 ######################################################################
michael@0 136 @f = split(/;/ , $_);
michael@0 137
michael@0 138 $simp{$f[0]} = $f[1];
michael@0 139 $sccount{$f[1]}++;
michael@0 140 }
michael@0 141 close(SIMP);
michael@0 142
michael@0 143 sub GetClass{
michael@0 144 my ($u) = @_;
michael@0 145 my $hex = DecToHex($u);
michael@0 146 $g = $gcat{$hex};
michael@0 147 if($g ne "") {
michael@0 148 return $g;
michael@0 149 } elsif (( 0x3400 <= $u) && ( $u <= 0x9fa5 ) ) {
michael@0 150 return "Han";
michael@0 151 } elsif (( 0xac00 <= $u) && ( $u <= 0xd7a3 ) ) {
michael@0 152 return "Lo";
michael@0 153 } elsif (( 0xd800 <= $u) && ( $u <= 0xdb7f ) ) {
michael@0 154 return "Cs";
michael@0 155 } elsif (( 0xdb80 <= $u) && ( $u <= 0xdbff ) ) {
michael@0 156 return "Cs";
michael@0 157 } elsif (( 0xdc00 <= $u) && ( $u <= 0xdfff ) ) {
michael@0 158 return "Cs";
michael@0 159 } elsif (( 0xe000 <= $u) && ( $u <= 0xf8ff ) ) {
michael@0 160 return "Co";
michael@0 161 } else {
michael@0 162 printf "WARNING !!!! Cannot find General Category for U+%s \n" , $hex;
michael@0 163 }
michael@0 164 }
michael@0 165 sub GetDClass{
michael@0 166 my ($u) = @_;
michael@0 167 my $hex = DecToHex($u);
michael@0 168 $g = $dcat{$hex};
michael@0 169 if($g ne "") {
michael@0 170 return $g;
michael@0 171 } elsif (( 0x3400 <= $u) && ( $u <= 0x9fa5 ) ) {
michael@0 172 return "Han";
michael@0 173 } elsif (( 0xac00 <= $u) && ( $u <= 0xd7a3 ) ) {
michael@0 174 return "L";
michael@0 175 } elsif (( 0xd800 <= $u) && ( $u <= 0xdb7f ) ) {
michael@0 176 return "C";
michael@0 177 } elsif (( 0xdb80 <= $u) && ( $u <= 0xdbff ) ) {
michael@0 178 return "C";
michael@0 179 } elsif (( 0xdc00 <= $u) && ( $u <= 0xdfff ) ) {
michael@0 180 return "C";
michael@0 181 } elsif (( 0xe000 <= $u) && ( $u <= 0xf8ff ) ) {
michael@0 182 return "C";
michael@0 183 } else {
michael@0 184 printf "WARNING !!!! Cannot find Detailed General Category for U+%s \n" , $hex;
michael@0 185 }
michael@0 186 }
michael@0 187 sub DecToHex{
michael@0 188 my ($d) = @_;
michael@0 189 return sprintf("%04X", $d);
michael@0 190 }
michael@0 191 %gtotal = ();
michael@0 192 %dtotal = ();
michael@0 193 while(<CLASS>) {
michael@0 194 chop;
michael@0 195 ######################################################################
michael@0 196 #
michael@0 197 # Get value from fields
michael@0 198 #
michael@0 199 ######################################################################
michael@0 200 @f = split(/;/ , $_);
michael@0 201
michael@0 202 if( substr($f[2], 0, 1) ne "a")
michael@0 203 {
michael@0 204 $sc = $simp{$f[2]};
michael@0 205 $l = hex($f[0]);
michael@0 206 if($f[1] eq "")
michael@0 207 {
michael@0 208 $h = $l;
michael@0 209 } else {
michael@0 210 $h = hex($f[1]);
michael@0 211 }
michael@0 212 for($k = $l; $k <= $h ; $k++)
michael@0 213 {
michael@0 214 if( exists($occ{$k}))
michael@0 215 {
michael@0 216 # printf "WARNING !! Conflict defination!!! U+%s -> [%s] [%s | %s]\n",
michael@0 217 # DecToHex($k), $occ{$k} , $f[2] , $sc;
michael@0 218 }
michael@0 219 else
michael@0 220 {
michael@0 221 $occ{$k} = $sc . " | " . $f[2];
michael@0 222 $gclass = GetClass($k);
michael@0 223 $dclass = GetDClass($k);
michael@0 224 $gtotal{$sc . $gclass}++;
michael@0 225 $dtotal{$sc . $dclass}++;
michael@0 226 $u = DecToHex($k);
michael@0 227 $rk = " " . substr($u,0,2) . ":" . $sc;
michael@0 228 $rangecount{$rk}++;
michael@0 229 }
michael@0 230 }
michael@0 231 }
michael@0 232 }
michael@0 233
michael@0 234 #print %gtotal;
michael@0 235 #print %dtotal;
michael@0 236
michael@0 237 sub printreport
michael@0 238 {
michael@0 239 print OUT "<TABLE BORDER=3>\n";
michael@0 240 print OUT "<TR BGCOLOR=blue><TH><TH>\n";
michael@0 241
michael@0 242 foreach $d (sort(keys %dcount)) {
michael@0 243 print OUT "<TD BGCOLOR=red>$d</TD>\n";
michael@0 244 }
michael@0 245
michael@0 246 print OUT "<TD BGCOLOR=white>Total</TD>\n";
michael@0 247 foreach $g (sort(keys %gcount)) {
michael@0 248 print OUT "<TD BGCOLOR=yellow>$g</TD>\n";
michael@0 249 }
michael@0 250 print OUT "</TR>\n";
michael@0 251 foreach $sc (sort(keys %sccount)) {
michael@0 252
michael@0 253 print OUT "<TR><TH>$sc<TH>\n";
michael@0 254
michael@0 255 $total = 0;
michael@0 256 foreach $d (sort (keys %dcount)) {
michael@0 257 $count = $dtotal{$sc . $d};
michael@0 258 $total += $count;
michael@0 259 print OUT "<TD>$count</TD>\n";
michael@0 260 }
michael@0 261
michael@0 262 print OUT "<TD BGCOLOR=white>$total</TD>\n";
michael@0 263
michael@0 264 foreach $g (sort(keys %gcount)) {
michael@0 265 $count = $gtotal{$sc . $g};
michael@0 266 print OUT "<TD>$count</TD>\n";
michael@0 267 }
michael@0 268
michael@0 269
michael@0 270 print OUT "</TR>\n";
michael@0 271 }
michael@0 272 print OUT "</TABLE>\n";
michael@0 273
michael@0 274
michael@0 275 print OUT "<TABLE BORDER=3>\n";
michael@0 276 print OUT "<TR BGCOLOR=blue><TH><TH>\n";
michael@0 277
michael@0 278 foreach $sc (sort(keys %sccount))
michael@0 279 {
michael@0 280 print OUT "<TD BGCOLOR=red>$sc</TD>\n";
michael@0 281 }
michael@0 282
michael@0 283 print OUT "</TR>\n";
michael@0 284
michael@0 285
michael@0 286 for($rr = 0; $rr < 0x4f; $rr++)
michael@0 287 {
michael@0 288 $empty = 0;
michael@0 289 $r = sprintf("%02X" , $rr) ;
michael@0 290 $tmp = "<TR><TH>" . $r . "<TH>\n";
michael@0 291
michael@0 292 foreach $sc (sort(keys %sccount)) {
michael@0 293 $count = $rangecount{ " " .$r . ":" .$sc};
michael@0 294 $tmp .= sprintf("<TD>%s</TD>\n", $count);
michael@0 295 $empty += $count;
michael@0 296 }
michael@0 297
michael@0 298 $tmp .= "</TR>\n";
michael@0 299
michael@0 300 if($empty ne 0)
michael@0 301 {
michael@0 302 print OUT $tmp;
michael@0 303 }
michael@0 304 }
michael@0 305 print OUT "</TABLE>\n";
michael@0 306
michael@0 307 }
michael@0 308 printreport();
michael@0 309
michael@0 310 sub printarray
michael@0 311 {
michael@0 312 my($r, $def) = @_;
michael@0 313 printf "[%s || %s]\n", $r, $def;
michael@0 314 $k = hex($r) * 256;
michael@0 315 printf HEADER "static const uint32_t gLBClass%s[32] = {\n", $r;
michael@0 316 for($i = 0 ; $i < 256; $i+= 8)
michael@0 317 {
michael@0 318 for($j = 7 ; $j >= 0; $j-- )
michael@0 319 {
michael@0 320 $v = $k + $i + $j;
michael@0 321 if( exists($occ{$v}))
michael@0 322 {
michael@0 323 $p = substr($occ{$v}, 1,1);
michael@0 324 } else {
michael@0 325 $p = $def;
michael@0 326 }
michael@0 327
michael@0 328 if($j eq 7 )
michael@0 329 {
michael@0 330 printf HEADER "0x%s" , $p;
michael@0 331 } else {
michael@0 332 printf HEADER "%s", $p ;
michael@0 333 }
michael@0 334 }
michael@0 335 printf HEADER ", // U+%04X - U+%04X\n", $k + $i ,( $k + $i + 7);
michael@0 336 }
michael@0 337 print HEADER "};\n\n";
michael@0 338 }
michael@0 339 printarray("00", "7");
michael@0 340 printarray("20", "7");
michael@0 341 printarray("21", "7");
michael@0 342 printarray("30", "5");
michael@0 343 printarray("0E", "8");
michael@0 344 printarray("17", "7");
michael@0 345
michael@0 346 #print %rangecount;
michael@0 347
michael@0 348 ######################################################################
michael@0 349 #
michael@0 350 # Close files
michael@0 351 #
michael@0 352 ######################################################################
michael@0 353 close(HEADER);
michael@0 354 close(CLASS);
michael@0 355 close(OUT);
michael@0 356

mercurial