make_unicode 72.3 KB
Newer Older
1
#!/usr/bin/perl -w
2 3 4 5 6
#
# Generate code page .c files from ftp.unicode.org descriptions
#
# Copyright 2000 Alexandre Julliard
#
7 8 9 10 11 12 13 14 15 16 17 18
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
19
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20
#
21

22 23
use strict;

24 25
# base URLs for www.unicode.org files
my $MAPPINGS = "http://www.unicode.org/Public/MAPPINGS";
26
my $UNIDATA = "http://www.unicode.org/Public/12.1.0/ucd/UCD.zip";
27 28
my $REPORTS = "http://www.unicode.org/reports";
my $RFCS = "http://www.rfc-editor.org/rfc";
29 30
my $MSDATA = "https://download.microsoft.com/download/C/F/7/CF713A5E-9FBC-4FD6-9246-275F65C0E498";
my $MSCODEPAGES = "$MSDATA/Windows Supported Code Page Data Files.zip";
31

32
# Sort keys file
33
my $SORTKEYS = "tr10/allkeys.txt";
34

35
# RFC3454 (stringprep data)
36
my $STRINGPREP = "rfc3454.txt";
37

38
# Default char for undefined mappings
39
my $DEF_CHAR = ord '?';
40

41
my @allfiles =
42
(
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
    "CodpageFiles/037.txt",
    "CodpageFiles/437.txt",
    "CodpageFiles/500.txt",
    "CodpageFiles/737.txt",
    "CodpageFiles/775.txt",
    "CodpageFiles/850.txt",
    "CodpageFiles/852.txt",
    "CodpageFiles/855.txt",
    "CodpageFiles/857.txt",
    "CodpageFiles/860.txt",
    "CodpageFiles/861.txt",
    "CodpageFiles/862.txt",
    "CodpageFiles/863.txt",
    "CodpageFiles/864.txt",
    "CodpageFiles/865.txt",
    "CodpageFiles/866.txt",
    "CodpageFiles/869.txt",
    "CodpageFiles/874.txt",
    "CodpageFiles/875.txt",
    "CodpageFiles/932.txt",
    "CodpageFiles/936.txt",
    "CodpageFiles/949.txt",
    "CodpageFiles/950.txt",
    "CodpageFiles/1026.txt",
    "CodpageFiles/1250.txt",
    "CodpageFiles/1251.txt",
    "CodpageFiles/1252.txt",
    "CodpageFiles/1253.txt",
    "CodpageFiles/1254.txt",
    "CodpageFiles/1255.txt",
    "CodpageFiles/1256.txt",
    "CodpageFiles/1257.txt",
    "CodpageFiles/1258.txt",
    "CodpageFiles/1361.txt",
    "CodpageFiles/10000.txt",
    "CodpageFiles/10001.txt",
    "CodpageFiles/10002.txt",
    "CodpageFiles/10003.txt",
    "CodpageFiles/10004.txt",
    "CodpageFiles/10005.txt",
    "CodpageFiles/10006.txt",
    "CodpageFiles/10007.txt",
    "CodpageFiles/10008.txt",
    "CodpageFiles/10010.txt",
    "CodpageFiles/10017.txt",
    "CodpageFiles/10021.txt",
    "CodpageFiles/10029.txt",
    "CodpageFiles/10079.txt",
    "CodpageFiles/10081.txt",
    "CodpageFiles/10082.txt",
    "CodpageFiles/20127.txt",
    "CodpageFiles/20866.txt",
    "CodpageFiles/21866.txt",
    "CodpageFiles/28591.txt",
    "CodpageFiles/28592.txt",
    "CodpageFiles/28593.txt",
    "CodpageFiles/28594.txt",
    "CodpageFiles/28595.txt",
    "CodpageFiles/28596.txt",
    "CodpageFiles/28597.txt",
    "CodpageFiles/28598.txt",
    "CodpageFiles/28599.txt",
    "CodpageFiles/28603.txt",
    "CodpageFiles/28605.txt",
107 108
);

109

110
my %ctype =
111 112 113 114 115 116 117 118 119
(
    "upper"  => 0x0001,
    "lower"  => 0x0002,
    "digit"  => 0x0004,
    "space"  => 0x0008,
    "punct"  => 0x0010,
    "cntrl"  => 0x0020,
    "blank"  => 0x0040,
    "xdigit" => 0x0080,
120 121
    "alpha"  => 0x0100,
    "defin"  => 0x0200
122 123
);

124 125 126 127 128 129
my %bracket_types =
(
    "o" => 0x0000,
    "c" => 0x0001,
);

130 131 132 133 134 135 136 137 138 139 140 141 142 143
my %indic_types =
(
    "Other"    => 0x0000,
    "Bindu"    => 0x0001,
    "Visarga"  => 0x0002,
    "Avagraha" => 0x0003,
    "Nukta"    => 0x0004,
    "Virama"   => 0x0005,
    "Vowel_Independent"  => 0x0006,
    "Vowel_Dependent"  => 0x0007,
    "Vowel"  => 0x0008,
    "Consonant_Placeholder"  => 0x0009,
    "Consonant"  => 0x000a,
    "Consonant_Dead"  => 0x000b,
144
    "Consonant_Succeeding_Repha" => 0x000c,
145 146 147 148 149 150 151
    "Consonant_Subjoined"  => 0x000d,
    "Consonant_Medial"  => 0x000e,
    "Consonant_Final"  => 0x000f,
    "Consonant_Head_Letter"  => 0x0010,
    "Modifying_Letter"  => 0x0011,
    "Tone_Letter"  => 0x0012,
    "Tone_Mark"  => 0x0013,
152 153 154 155 156 157 158 159 160 161
    "Register_Shifter"  => 0x0014,
    "Consonant_Preceding_Repha" => 0x0015,
    "Pure_Killer" => 0x0016,
    "Invisible_Stacker" => 0x0017,
    "Gemination_Mark" => 0x0018,
    "Cantillation_Mark" => 0x0019,
    "Non_Joiner" => 0x001a,
    "Joiner" => 0x001b,
    "Number_Joiner" => 0x001c,
    "Number" => 0x001d,
162 163 164 165
    "Brahmi_Joining_Number" => 0x001e,
    "Consonant_With_Stacker" => 0x001f,
    "Consonant_Prefixed" => 0x0020,
    "Syllable_Modifier" => 0x0021,
166 167
    "Consonant_Killer" => 0x0022,
    "Consonant_Initial_Postfixed" => 0x0023,
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
);

my %matra_types =
(
    "Right"    => 0x01,
    "Left"  => 0x02,
    "Visual_Order_Left" => 0x03,
    "Left_And_Right"    => 0x04,
    "Top"   => 0x05,
    "Bottom"  => 0x06,
    "Top_And_Bottom"  => 0x07,
    "Top_And_Right"  => 0x08,
    "Top_And_Left"  => 0x09,
    "Top_And_Left_And_Right"  => 0x0a,
    "Bottom_And_Right"  => 0x0b,
    "Top_And_Bottom_And_Right"  => 0x0c,
    "Overstruck"  => 0x0d,
185 186
    "Invisible"  => 0x0e,
    "Bottom_And_Left"  => 0x0f
187 188
);

189 190 191 192 193 194 195 196
my %nameprep_flags =
(
    "unassigned" => 0x01,
    "prohibited" => 0x02,
    "bidi_ral"   => 0x04,
    "bidi_l"     => 0x08
);

197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
my %break_types =
(
    "BK"  => 0x0001,
    "CR"  => 0x0002,
    "LF"  => 0x0003,
    "CM"  => 0x0004,
    "SG"  => 0x0005,
    "GL"  => 0x0006,
    "CB"  => 0x0007,
    "SP"  => 0x0008,
    "ZW"  => 0x0009,
    "NL"  => 0x000a,
    "WJ"  => 0x000b,
    "JL"  => 0x000c,
    "JV"  => 0x000d,
    "JT"  => 0x000e,
    "H2"  => 0x000f,
    "H3"  => 0x0010,
    "XX"  => 0x0011,
    "OP"  => 0x0012,
    "CL"  => 0x0013,
    "CP"  => 0x0014,
    "QU"  => 0x0015,
    "NS"  => 0x0016,
    "EX"  => 0x0017,
    "SY"  => 0x0018,
    "IS"  => 0x0019,
    "PR"  => 0x001a,
    "PO"  => 0x001b,
    "NU"  => 0x001c,
    "AL"  => 0x001d,
    "ID"  => 0x001e,
    "IN"  => 0x001f,
    "HY"  => 0x0020,
    "BB"  => 0x0021,
    "BA"  => 0x0022,
    "SA"  => 0x0023,
    "AI"  => 0x0024,
235 236 237 238
    "B2"  => 0x0025,
    "HL"  => 0x0026,
    "CJ"  => 0x0027,
    "RI"  => 0x0028,
239 240 241
    "EB"  => 0x0029,
    "EM"  => 0x002a,
    "ZWJ" => 0x002b,
242 243
);

244 245 246 247 248 249 250 251
my %vertical_types =
(
    "R"  => 0x0000,
    "U"  => 0x0001,
    "Tr" => 0x0002,
    "Tu" => 0x0003,
);

252
my %categories =
253
(
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
    "Lu" => $ctype{"defin"}|$ctype{"alpha"}|$ctype{"upper"}, # Letter, Uppercase
    "Ll" => $ctype{"defin"}|$ctype{"alpha"}|$ctype{"lower"}, # Letter, Lowercase
    "Lt" => $ctype{"defin"}|$ctype{"alpha"}|$ctype{"upper"}|$ctype{"lower"},    # Letter, Titlecase
    "Mn" => $ctype{"defin"},                    # Mark, Non-Spacing
    "Mc" => $ctype{"defin"},                    # Mark, Spacing Combining
    "Me" => $ctype{"defin"},                    # Mark, Enclosing
    "Nd" => $ctype{"defin"}|$ctype{"digit"},    # Number, Decimal Digit
    "Nl" => $ctype{"defin"}|$ctype{"alpha"},    # Number, Letter
    "No" => $ctype{"defin"},                    # Number, Other
    "Zs" => $ctype{"defin"}|$ctype{"space"},    # Separator, Space
    "Zl" => $ctype{"defin"}|$ctype{"space"},    # Separator, Line
    "Zp" => $ctype{"defin"}|$ctype{"space"},    # Separator, Paragraph
    "Cc" => $ctype{"defin"}|$ctype{"cntrl"},    # Other, Control
    "Cf" => $ctype{"defin"}|$ctype{"cntrl"},    # Other, Format
    "Cs" => $ctype{"defin"},                    # Other, Surrogate
    "Co" => $ctype{"defin"},                    # Other, Private Use
    "Cn" => $ctype{"defin"},                    # Other, Not Assigned
    "Lm" => $ctype{"defin"}|$ctype{"alpha"},    # Letter, Modifier
    "Lo" => $ctype{"defin"}|$ctype{"alpha"},    # Letter, Other
    "Pc" => $ctype{"defin"}|$ctype{"punct"},    # Punctuation, Connector
    "Pd" => $ctype{"defin"}|$ctype{"punct"},    # Punctuation, Dash
    "Ps" => $ctype{"defin"}|$ctype{"punct"},    # Punctuation, Open
    "Pe" => $ctype{"defin"}|$ctype{"punct"},    # Punctuation, Close
    "Pi" => $ctype{"defin"}|$ctype{"punct"},    # Punctuation, Initial quote
    "Pf" => $ctype{"defin"}|$ctype{"punct"},    # Punctuation, Final quote
    "Po" => $ctype{"defin"}|$ctype{"punct"},    # Punctuation, Other
    "Sm" => $ctype{"defin"},                    # Symbol, Math
    "Sc" => $ctype{"defin"},                    # Symbol, Currency
    "Sk" => $ctype{"defin"},                    # Symbol, Modifier
    "So" => $ctype{"defin"}                     # Symbol, Other
284 285 286
);

# a few characters need additional categories that cannot be determined automatically
287
my %special_categories =
288 289 290
(
    "xdigit" => [ ord('0')..ord('9'),ord('A')..ord('F'),ord('a')..ord('f'),
                  0xff10..0xff19, 0xff21..0xff26, 0xff41..0xff46 ],
291 292
    "space"  => [ 0x09..0x0d, 0x85 ],
    "blank"  => [ 0x09, 0x20, 0xa0, 0x3000, 0xfeff ],
293
    "cntrl"  => [ 0x070f, 0x200c, 0x200d,
294 295
                  0x200e, 0x200f, 0x202a, 0x202b, 0x202c, 0x202d, 0x202e,
                  0x206a, 0x206b, 0x206c, 0x206d, 0x206e, 0x206f, 0xfeff,
296 297
                  0xfff9, 0xfffa, 0xfffb ],
    "punct"  => [ 0x24, 0x2b, 0x3c..0x3e, 0x5e, 0x60, 0x7c, 0x7e, 0xa2..0xbe,
298 299 300
                  0xd7, 0xf7 ],
    "digit"  => [ 0xb2, 0xb3, 0xb9 ],
    "lower"  => [ 0x2071, 0x207f ]
301 302
);

303
my %directions =
304 305 306
(
    "L"   => 1,    # Left-to-Right
    "R"   => 2,    # Right-to-Left
307
    "AL"  => 12,   # Right-to-Left Arabic
308 309 310 311 312
    "EN"  => 3,    # European Number
    "ES"  => 4,    # European Number Separator
    "ET"  => 5,    # European Number Terminator
    "AN"  => 6,    # Arabic Number
    "CS"  => 7,    # Common Number Separator
313 314
    "NSM" => 13,   # Non-Spacing Mark
    "BN"  => 14,   # Boundary Neutral
315 316 317
    "B"   => 8,    # Paragraph Separator
    "S"   => 9,    # Segment Separator
    "WS"  => 10,   # Whitespace
318 319 320 321 322 323 324 325 326 327
    "ON"  => 11,   # Other Neutrals
    "LRE" => 15,   # Left-to-Right Embedding
    "LRO" => 15,   # Left-to-Right Override
    "RLE" => 15,   # Right-to-Left Embedding
    "RLO" => 15,   # Right-to-Left Override
    "PDF" => 15,   # Pop Directional Format
    "LRI" => 15,   # Left-to-Right Isolate
    "RLI" => 15,   # Right-to-Left Isolate
    "FSI" => 15,   # First Strong Isolate
    "PDI" => 15    # Pop Directional Isolate
328 329
);

330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
my %c2_types =
(
    "L"   => 1,    # C2_LEFTTORIGHT
    "R"   => 2,    # C2_RIGHTTOLEFT
    "AL"  => 2,    # C2_RIGHTTOLEFT
    "EN"  => 3,    # C2_EUROPENUMBER
    "ES"  => 4,    # C2_EUROPESEPARATOR
    "ET"  => 5,    # C2_EUROPETERMINATOR
    "AN"  => 6,    # C2_ARABICNUMBER
    "CS"  => 7,    # C2_COMMONSEPARATOR
    "NSM" => 0,    # C2_NOTAPPLICABLE
    "BN"  => 0,    # C2_NOTAPPLICABLE
    "B"   => 8,    # C2_BLOCKSEPARATOR
    "S"   => 9,    # C2_SEGMENTSEPARATOR
    "WS"  => 10,   # C2_WHITESPACE
    "ON"  => 11,   # C2_OTHERNEUTRAL
    "LRE" => 11,   # C2_OTHERNEUTRAL
    "LRO" => 11,   # C2_OTHERNEUTRAL
    "RLE" => 11,   # C2_OTHERNEUTRAL
    "RLO" => 11,   # C2_OTHERNEUTRAL
    "PDF" => 11,   # C2_OTHERNEUTRAL
    "LRI" => 11,   # C2_OTHERNEUTRAL
    "RLI" => 11,   # C2_OTHERNEUTRAL
    "FSI" => 11,   # C2_OTHERNEUTRAL
    "PDI" => 11    # C2_OTHERNEUTRAL
);

357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
my %bidi_types =
(
    "ON"  => 0,    # Other Neutrals
    "L"   => 1,    # Left-to-Right
    "R"   => 2,    # Right-to-Left
    "AN"  => 3,    # Arabic Number
    "EN"  => 4,    # European Number
    "AL"  => 5,    # Right-to-Left Arabic
    "NSM" => 6,    # Non-Spacing Mark
    "CS"  => 7,    # Common Number Separator
    "ES"  => 8,    # European Number Separator
    "ET"  => 9,    # European Number Terminator
    "BN"  => 10,   # Boundary Neutral
    "S"   => 11,   # Segment Separator
    "WS"  => 12,   # Whitespace
    "B"   => 13,   # Paragraph Separator
    "RLO" => 14,   # Right-to-Left Override
    "RLE" => 15,   # Right-to-Left Embedding
    "LRO" => 16,   # Left-to-Right Override
    "LRE" => 17,   # Left-to-Right Embedding
    "PDF" => 18,   # Pop Directional Format
    "LRI" => 19,   # Left-to-Right Isolate
    "RLI" => 20,   # Right-to-Left Isolate
    "FSI" => 21,   # First Strong Isolate
    "PDI" => 22    # Pop Directional Isolate
);

384 385 386 387 388 389 390 391 392 393
my %joining_types =
(
   "U" => 0,    # Non_Joining
   "T" => 1,    # Transparent
   "R" => 2,    # Right_Joining
   "L" => 3,    # Left_Joining
   "D" => 4,    # Dual_Joining
   "C" => 5,    # Join_Causing
);

394
my @cp2uni = ();
395
my @glyph2uni = ();
396 397 398 399 400
my @lead_bytes = ();
my @uni2cp = ();
my @tolower_table = ();
my @toupper_table = ();
my @digitmap_table = ();
401 402
my @category_table = ();
my @joining_table = ();
403 404 405
my @direction_table = ();
my @decomp_table = ();
my @compose_table = ();
406
my @combining_class_table = ();
407
my @decomp_compat_table = ();
408 409
my $default_char;
my $default_wchar;
410

411 412 413 414 415 416 417
my %joining_forms =
(
   "isolated" => [],
   "final" => [],
   "initial" => [],
   "medial" => []
);
418

419 420 421 422 423 424 425 426
sub get_utf16($)
{
    my $ch = shift;
    return $ch if ($ch < 0x10000);
    $ch -= 0x10000;
    return ( 0xd800 | ($ch >> 10), 0xdc00 | ($ch & 0x3ff) );
}

427 428
################################################################
# fetch a unicode.org file and open it
429
sub open_data_file($$)
430
{
431 432
    my ($base, $name) = @_;
    (my $dir = "data/$name") =~ s/\/[^\/]+$//;
433 434 435
    local *FILE;
    unless (-f "data/$name")
    {
436
        system "mkdir", "-p", $dir;
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
        if ($base =~ /.*\/([^\/]+\.zip)$/)
        {
            my $zip = $1;
            unless (-f "data/$zip")
            {
                print "Fetching $base...\n";
                !system "wget", "-q", "-O", "data/$zip", "$base" or die "cannot fetch $base";
            }
            !system "unzip", "-q", "-d", "data", "data/$zip", $name or die "cannot extract $name from $zip";
        }
        else
        {
            print "Fetching $base/$name...\n";
            !system "wget", "-q", "-O", "data/$name", "$base/$name" or die "cannot fetch $base/$name";
        }
452 453 454 455 456
    }
    open FILE, "<data/$name" or die "cannot open data/$name";
    return *FILE;
}

457
################################################################
458 459 460 461 462 463 464
# recursively get the decomposition for a character
sub get_decomposition($$);
sub get_decomposition($$)
{
    my ($char, $table) = @_;
    my @ret;

465
    return get_utf16($char) unless defined ${$table}[$char];
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
    foreach my $ch (@{${$table}[$char]})
    {
        push @ret, get_decomposition( $ch, $table );
    }
    return @ret;
}

################################################################
# recursively build decompositions
sub build_decompositions(@)
{
    my @src = @_;
    my @dst;

    for (my $i = 0; $i < 65536; $i++)
    {
        next unless defined $src[$i];
        my @decomp = get_decomposition( $i, \@src );
        $dst[$i] = \@decomp;
    }
    return @dst;
}

################################################################
# read in the Unicode database files
491
sub load_data()
492
{
493
    my $start;
494 495 496

    # now build mappings from the decomposition field of the Unicode database

497
    my $UNICODE_DATA = open_data_file( $UNIDATA, "UnicodeData.txt" );
498
    while (<$UNICODE_DATA>)
499 500
    {
	# Decode the fields ...
501 502 503 504
	my ($code, $name, $cat, $comb, $bidi,
            $decomp, $dec, $dig, $num, $mirror,
            $oldname, $comment, $upper, $lower, $title) = split /;/;
        my $dst;
505
        my $src = hex $code;
506

507 508 509 510
        die "unknown category $cat" unless defined $categories{$cat};
        die "unknown directionality $bidi" unless defined $directions{$bidi};

        $category_table[$src] = $categories{$cat};
511
        $direction_table[$src] = $bidi;
512
        $joining_table[$src] = $joining_types{"T"} if $cat eq "Mn" || $cat eq "Me" || $cat eq "Cf";
513 514 515 516 517 518 519 520 521 522 523 524 525

        if ($lower ne "")
        {
            $tolower_table[$src] = hex $lower;
        }
        if ($upper ne "")
        {
            $toupper_table[$src] = hex $upper;
        }
        if ($dec ne "")
        {
            $category_table[$src] |= $ctype{"digit"};
        }
526 527 528 529
        if ($dig ne "")
        {
            $digitmap_table[$src] = ord $dig;
        }
530 531 532 533
        if ($comb ne "")
        {
            $combining_class_table[$src] = $comb;
        }
534

535 536 537 538 539 540 541 542 543 544 545
        # copy the category and direction for everything between First/Last pairs
        if ($name =~ /, First>/) { $start = $src; }
        if ($name =~ /, Last>/)
        {
            while ($start < $src)
            {
                $category_table[$start] = $category_table[$src];
                $direction_table[$start] = $direction_table[$src];
                $start++;
            }
        }
546

547
        next if $decomp eq "";  # no decomposition, skip it
548

549 550
        if ($decomp =~ /^<([a-zA-Z]+)>\s+([0-9a-fA-F]+)/)
        {
551 552
            my @seq = map { hex $_; } (split /\s+/, (split /\s+/, $decomp, 2)[1]);
            $decomp_compat_table[$src] = \@seq;
553 554
        }

555 556 557
        if ($decomp =~ /^<([a-zA-Z]+)>\s+([0-9a-fA-F]+)$/)
        {
            # decomposition of the form "<foo> 1234" -> use char if type is known
558 559 560 561 562
            if ($1 eq "isolated" || $1 eq "final" || $1 eq "initial" || $1 eq "medial")
            {
                ${joining_forms{$1}}[hex $2] = $src;
                next;
            }
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
            next unless ($1 eq "font" ||
                         $1 eq "noBreak" ||
                         $1 eq "circle" ||
                         $1 eq "super" ||
                         $1 eq "sub" ||
                         $1 eq "wide" ||
                         $1 eq "narrow" ||
                         $1 eq "compat" ||
                         $1 eq "small");
            $dst = hex $2;
        }
        elsif ($decomp =~ /^<compat>\s+0020\s+([0-9a-fA-F]+)/)
        {
            # decomposition "<compat> 0020 1234" -> combining accent
            $dst = hex $1;
        }
        elsif ($decomp =~ /^([0-9a-fA-F]+)/)
        {
            # decomposition contains only char values without prefix -> use first char
            $dst = hex $1;
583
            $category_table[$src] |= $category_table[$dst] if defined $category_table[$dst];
584 585 586
            # store decomposition if it contains two chars
            if ($decomp =~ /^([0-9a-fA-F]+)\s+([0-9a-fA-F]+)$/)
            {
587
                $decomp_table[$src] = $decomp_compat_table[$src] = [ hex $1, hex $2 ];
588 589
                push @compose_table, [ hex $1, hex $2, $src ];
            }
590
            elsif ($decomp =~ /^([0-9a-fA-F]+)$/)
591
            {
592
                # Single char decomposition
593
                $decomp_table[$src] = $decomp_compat_table[$src] = [ hex $1 ];
594
            }
595 596 597 598 599 600
        }
        else
        {
            next;
        }
    }
601
    close $UNICODE_DATA;
602 603 604

    # patch the category of some special characters

605
    foreach my $cat (keys %special_categories)
606 607
    {
        my $flag = $ctype{$cat};
608
        foreach my $i (@{$special_categories{$cat}}) { $category_table[$i] |= $flag; }
609
    }
610 611 612 613 614 615 616 617 618 619 620 621 622 623 624

    # remove case mappings that don't round-trip

    for (my $i = 0; $i < @toupper_table; $i++)
    {
        next unless defined $toupper_table[$i];
        my $ch = $toupper_table[$i];
        $toupper_table[$i] = undef unless defined $tolower_table[$ch] && $tolower_table[$ch] == $i;
    }
    for (my $i = 0; $i < @tolower_table; $i++)
    {
        next unless defined $tolower_table[$i];
        my $ch = $tolower_table[$i];
        $tolower_table[$i] = undef unless defined $toupper_table[$ch] && $toupper_table[$ch] == $i;
    }
625 626 627 628
}


################################################################
629 630 631 632
# define a new lead byte
sub add_lead_byte($)
{
    my $ch = shift;
633
    return if defined $cp2uni[$ch];
634 635 636 637 638 639 640 641 642 643 644
    push @lead_bytes, $ch;
    $cp2uni[$ch] = 0;
}

################################################################
# define a new char mapping
sub add_mapping($$)
{
    my ($cp, $uni) = @_;
    $cp2uni[$cp] = $uni unless defined($cp2uni[$cp]);
    $uni2cp[$uni] = $cp unless defined($uni2cp[$uni]);
645
    if ($cp > 0xff) { add_lead_byte( $cp >> 8 ); }
646 647
}

648 649 650 651
################################################################
# get a mapping including glyph chars for MB_USEGLYPHCHARS
sub get_glyphs_mapping(@)
{
652 653 654 655 656 657 658
    my @table = @_;

    for (my $i = 0; $i < @glyph2uni; $i++)
    {
        $table[$i] = $glyph2uni[$i] if defined $glyph2uni[$i];
    }
    return @table;
659
}
660

661
################################################################
662
# build EUC-JP table from the JIS 0208/0212 files
663
sub dump_eucjp_codepage()
664
{
665 666 667 668
    @cp2uni = ();
    @glyph2uni = ();
    @lead_bytes = ();
    @uni2cp = ();
669 670
    $default_char = $DEF_CHAR;
    $default_wchar = 0x30fb;
671 672

    # ASCII chars
673 674 675 676
    foreach my $i (0x00 .. 0x7f) { add_mapping( $i, $i ); }

    # lead bytes
    foreach my $i (0x8e, 0xa1 .. 0xfe) { add_lead_byte($i); }
677 678

    # JIS X 0201 right plane
679
    foreach my $i (0xa1 .. 0xdf) { add_mapping( 0x8e00 + $i, 0xfec0 + $i ); }
680

681 682 683 684 685 686 687 688 689 690 691
    # undefined chars
    foreach my $i (0x80 .. 0x8d, 0x8f .. 0x9f) { $cp2uni[$i] = $i; }
    $cp2uni[0xa0] = 0xf8f0;
    $cp2uni[0xff] = 0xf8f3;

    # Fix backslash conversion
    add_mapping( 0xa1c0, 0xff3c );

    # Add private mappings for rows undefined in JIS 0208/0212
    my $private = 0xe000;
    foreach my $hi (0xf5 .. 0xfe)
692
    {
693 694 695 696
        foreach my $lo (0xa1 .. 0xfe)
        {
            add_mapping( ($hi << 8) + $lo, $private++ );
        }
697
    }
698
    foreach my $hi (0xf5 .. 0xfe)
699
    {
700 701 702 703
        foreach my $lo (0x21 .. 0x7e)
        {
            add_mapping( ($hi << 8) + $lo, $private++ );
        }
704 705
    }

706
    my $INPUT = open_data_file( $MAPPINGS, "OBSOLETE/EASTASIA/JIS/JIS0208.TXT" );
707
    while (<$INPUT>)
708 709 710 711 712 713
    {
        next if /^\#/;  # skip comments
        next if /^$/;  # skip empty lines
        next if /\x1a/;  # skip ^Z
        if (/^0x[0-9a-fA-F]+\s+0x([0-9a-fA-F]+)\s+0x([0-9a-fA-F]+)\s+(\#.*)?/)
        {
714 715 716
            add_mapping( 0x8080 + hex $1, hex $2 );
            next;
        }
717
        die "Unrecognized line $_\n";
718 719 720
    }
    close $INPUT;

721
    $INPUT = open_data_file( $MAPPINGS, "OBSOLETE/EASTASIA/JIS/JIS0212.TXT" );
722 723 724 725 726 727 728 729
    while (<$INPUT>)
    {
        next if /^\#/;  # skip comments
        next if /^$/;  # skip empty lines
        next if /\x1a/;  # skip ^Z
        if (/^0x([0-9a-fA-F]+)\s+0x([0-9a-fA-F]+)\s+(\#.*)?/)
        {
            add_mapping( 0x8000 + hex $1, hex $2 );
730 731
            next;
        }
732
        die "Unrecognized line $_\n";
733
    }
734
    close $INPUT;
735

736
    output_codepage_file( 20932 );
737 738 739
}


740 741
################################################################
# build the sort keys table
742
sub dump_sortkeys($)
743
{
744
    my $filename = shift;
745 746
    my @sortkeys = ();

747
    my $INPUT = open_data_file( $REPORTS, $SORTKEYS );
748
    while (<$INPUT>)
749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767
    {
        next if /^\#/;  # skip comments
        next if /^$/;  # skip empty lines
        next if /\x1a/;  # skip ^Z
        next if /^\@version/;  # skip @version header
        if (/^([0-9a-fA-F]+)\s+;\s+\[([*.])([0-9a-fA-F]{4})\.([0-9a-fA-F]{4})\.([0-9a-fA-F]{4})\.([0-9a-fA-F]+)\]/)
        {
            my ($uni,$variable) = (hex $1, $2);
            next if $uni > 65535;
            $sortkeys[$uni] = [ $uni, hex $3, hex $4, hex $5, hex $6 ];
            next;
        }
        if (/^([0-9a-fA-F]+\s+)+;\s+\[[*.]([0-9a-fA-F]{4})\.([0-9a-fA-F]{4})\.([0-9a-fA-F]{4})\.([0-9a-fA-F]+)\]/)
        {
            # multiple character sequence, ignored for now
            next;
        }
        die "$SORTKEYS: Unrecognized line $_\n";
    }
768
    close $INPUT;
769 770 771 772 773 774 775 776 777 778 779 780 781 782

    # compress the keys to 32 bit:
    # key 1 to 16 bits, key 2 to 8 bits, key 3 to 4 bits, key 4 to 1 bit

    @sortkeys = sort { ${$a}[1] <=> ${$b}[1] or 
                       ${$a}[2] <=> ${$b}[2] or
                       ${$a}[3] <=> ${$b}[3] or
                       ${$a}[4] <=> ${$b}[4] or
                       $a cmp $b; } @sortkeys;

    my ($n2, $n3) = (1, 1);
    my @keys = (-1, -1, -1, -1, -1 );
    my @flatkeys = ();

783
    for (my $i = 0; $i < @sortkeys; $i++)
784
    {
785
        next unless defined $sortkeys[$i];
786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826
        my @current = @{$sortkeys[$i]};
        if ($current[1] == $keys[1])
        {
            if ($current[2] == $keys[2])
            {
                if ($current[3] == $keys[3])
                {
                    # nothing
                }
                else
                {
                    $keys[3] = $current[3];
                    $n3++;
                    die if ($n3 >= 16);
                }
            }
            else
            {
                $keys[2] = $current[2];
                $keys[3] = $current[3];
                $n2++;
                $n3 = 1;
                die if ($n2 >= 256);
            }
        }
        else
        {
            $keys[1] = $current[1];
            $keys[2] = $current[2];
            $keys[3] = $current[3];
            $n2 = 1;
            $n3 = 1;
        }

        if ($current[2]) { $current[2] = $n2; }
        if ($current[3]) { $current[3] = $n3; }
        if ($current[4]) { $current[4] = 1; }

        $flatkeys[$current[0]] = ($current[1] << 16) | ($current[2] << 8) | ($current[3] << 4) | $current[4];
    }

827 828
    open OUTPUT,">$filename.new" or die "Cannot create $filename";
    printf "Building $filename\n";
829
    printf OUTPUT "/* Unicode collation element table */\n";
830
    printf OUTPUT "/* generated from %s */\n", "$REPORTS/$SORTKEYS";
831
    printf OUTPUT "/* DO NOT EDIT!! */\n\n";
832
    print OUTPUT "#include \"windef.h\"\n\n";
833

834 835
    dump_two_level_mapping( "collation_table", 0xffffffff, 32, @flatkeys );

836
    close OUTPUT;
837
    save_file($filename);
838 839 840
}


841 842
################################################################
# dump an array of integers
843
sub dump_array($$@)
844
{
845 846
    my ($bit_width, $default, @array) = @_;
    my $format = sprintf "0x%%0%ux", $bit_width / 4;
847 848
    my $i;
    my $ret = "    ";
849 850 851 852 853 854 855 856
    for ($i = 0; $i < $#array; $i++)
    {
        $ret .= sprintf($format, defined $array[$i] ? $array[$i] : $default);
        $ret .= (($i % 8) != 7) ? ", " : ",\n    ";
    }
    $ret .= sprintf($format, defined $array[$i] ? $array[$i] : $default);
    return $ret;
}
857

858

859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920
################################################################
# dump an SBCS mapping table in binary format
sub dump_binary_sbcs_table($)
{
    my $codepage = shift;

    my @header = ( 13, $codepage, 1, $default_char, $default_wchar, $cp2uni[$default_char], $uni2cp[$default_wchar] );
    my $wc_offset = 256 + 3 + (@glyph2uni ? 256 : 0);

    print OUTPUT pack "S<*", @header;
    print OUTPUT pack "C12", (0) x 12;
    print OUTPUT pack "S<*", $wc_offset, map { $_ || 0; } @cp2uni[0 .. 255];

    if (@glyph2uni)
    {
        print OUTPUT pack "S<*", 256, get_glyphs_mapping(@cp2uni[0 .. 255]);
    }
    else
    {
        print OUTPUT pack "S<*", 0;
    }

    print OUTPUT pack "S<*", 0, 0;

    print OUTPUT pack "C*", map { defined $_ ? $_ : $default_char; } @uni2cp[0 .. 65535];
}


################################################################
# dump a DBCS mapping table in binary format
sub dump_binary_dbcs_table($)
{
    my $codepage = shift;
    my @lb_ranges = get_lb_ranges();
    my @header = ( 13, $codepage, 2, $default_char, $default_wchar, $cp2uni[$default_char], $uni2cp[$default_wchar] );

    my @offsets = (0) x 256;
    my $pos = 0;
    foreach my $i (@lead_bytes)
    {
        $offsets[$i] = ($pos += 256);
        $cp2uni[$i] = 0;
    }

    my $wc_offset = 256 + 3 + 256 * (1 + scalar @lead_bytes);

    print OUTPUT pack "S<*", @header;
    print OUTPUT pack "C12", @lb_ranges, 0 x 12;
    print OUTPUT pack "S<*", $wc_offset, map { $_ || 0; } @cp2uni[0 .. 255];
    print OUTPUT pack "S<*", 0, scalar @lb_ranges / 2, @offsets;

    foreach my $i (@lead_bytes)
    {
        my $base = $i << 8;
        print OUTPUT pack "S<*", map { defined $_ ? $_ : $default_wchar; } @cp2uni[$base .. $base + 255];
    }

    print OUTPUT pack "S<", 4;
    print OUTPUT pack "S<*", map { defined $_ ? $_ : $default_char; } @uni2cp[0 .. 65535];
}


921
################################################################
922 923
# get the list of defined lead byte ranges
sub get_lb_ranges()
924 925
{
    my @list = ();
926
    my @ranges = ();
927 928

    foreach my $i (@lead_bytes) { $list[$i] = 1; }
929
    my $on = 0;
930
    for (my $i = 0; $i < 256; $i++)
931 932 933
    {
        if ($on)
        {
934
            if (!defined $list[$i]) { push @ranges, $i-1; $on = 0; }
935 936 937
        }
        else
        {
938
            if ($list[$i]) { push @ranges, $i; $on = 1; }
939 940
        }
    }
941 942
    if ($on) { push @ranges, 0xff; }
    return @ranges;
943 944
}

945 946 947 948 949
################################################################
# dump the Indic Syllabic Category table
sub dump_indic($)
{
    my $filename = shift;
950
    my @indic_table;
951

952
    my $INPUT = open_data_file( $UNIDATA, "IndicSyllabicCategory.txt" );
953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971
    while (<$INPUT>)
    {
        next if /^\#/;  # skip comments
        next if /^\s*$/;  # skip empty lines
        next if /\x1a/;  # skip ^Z
        if (/^\s*([0-9a-fA-F]+)\s*;\s*([a-zA-Z_]+)\s*#/)
        {
            my $type = $2;
            die "unknown indic $type" unless defined $indic_types{$type};
            if (hex $1 < 65536)
            {
                $indic_table[hex $1] = $indic_types{$type};
            }
            next;
        }
        elsif (/^\s*([0-9a-fA-F]+)..\s*([0-9a-fA-F]+)\s*;\s*([A-Za-z_]+)\s*#/)
        {
            my $type = $3;
            die "unknown indic $type" unless defined $indic_types{$type};
972
            if (hex $1 < 65536 and hex $2 < 65536)
973 974 975 976 977 978 979 980 981 982 983 984
            {
                foreach my $i (hex $1 .. hex $2)
                {
                    $indic_table[$i] = $indic_types{$type};
                }
            }
            next;
        }
        die "malformed line $_";
    }
    close $INPUT;

985
    $INPUT = open_data_file( $UNIDATA, "IndicPositionalCategory.txt" );
986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
    while (<$INPUT>)
    {
        next if /^\#/;  # skip comments
        next if /^\s*$/;  # skip empty lines
        next if /\x1a/;  # skip ^Z
        if (/^\s*([0-9a-fA-F]+)\s*;\s*([a-zA-Z]+)\s*#/)
        {
            my $type = $2;
            die "unknown matra $type" unless defined $matra_types{$type};
            $indic_table[hex $1] += $matra_types{$type} << 8;
            next;
        }
        elsif (/^\s*([0-9a-fA-F]+)..\s*([0-9a-fA-F]+)\s*;\s*([A-Za-z_]+)\s*#/)
        {
            my $type = $3;
            die "unknown matra $type" unless defined $matra_types{$type};
            foreach my $i (hex $1 .. hex $2)
            {
                $indic_table[$i] += $matra_types{$type} << 8;
            }
            next;
        }
        die "malformed line $_";
    }
    close $INPUT;

    open OUTPUT,">$filename.new" or die "Cannot create $filename";
    print "Building $filename\n";
    print OUTPUT "/* Unicode Indic Syllabic Category */\n";
1015 1016
    print OUTPUT "/* generated from $UNIDATA:IndicSyllabicCategory.txt */\n";
    print OUTPUT "/*       and from $UNIDATA:IndicPositionalCategory.txt */\n";
1017
    print OUTPUT "/* DO NOT EDIT!! */\n\n";
1018
    print OUTPUT "#include \"windef.h\"\n\n";
1019

1020
    dump_two_level_mapping( "indic_syllabic_table", $indic_types{'Other'}, 16, @indic_table );
1021 1022 1023 1024 1025

    close OUTPUT;
    save_file($filename);
}

1026 1027 1028 1029 1030
################################################################
# dump the Line Break Properties table
sub dump_linebreak($)
{
    my $filename = shift;
1031
    my @break_table;
1032 1033
    my $next_group = 0;

1034
    my $INPUT = open_data_file( $UNIDATA, "LineBreak.txt" );
1035 1036 1037 1038 1039
    while (<$INPUT>)
    {
        next if /^\#/;  # skip comments
        next if /^\s*$/;  # skip empty lines
        next if /\x1a/;  # skip ^Z
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
        if (/^\s*([0-9a-fA-F]+)\s*;\s*([0-9A-Z][0-9A-Z][0-9A-Z])+\s*/)
        {
            my $type = $2;
            die "unknown breaktype $type" unless defined $break_types{$type};
            $break_table[hex $1] = $break_types{$type};
            next;
        }
        elsif (/^\s*([0-9a-fA-F]+)..\s*([0-9a-fA-F]+)\s*;\s*([0-9A-Z][0-9A-Z][0-9A-Z])+\s*/)
        {
            my $type = $3;
            die "unknown breaktype $type" unless defined $break_types{$type};
            foreach my $i (hex $1 .. hex $2)
            {
                $break_table[$i] = $break_types{$type};
            }
            next;
        }
        elsif (/^\s*([0-9a-fA-F]+)\s*;\s*([0-9A-Z][0-9A-Z])+\s*/)
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
        {
            my $type = $2;
            die "unknown breaktype $type" unless defined $break_types{$type};
            $break_table[hex $1] = $break_types{$type};
            next;
        }
        elsif (/^\s*([0-9a-fA-F]+)..\s*([0-9a-fA-F]+)\s*;\s*([0-9A-Z][0-9A-Z])+\s*/)
        {
            my $type = $3;
            die "unknown breaktype $type" unless defined $break_types{$type};
            foreach my $i (hex $1 .. hex $2)
            {
                $break_table[$i] = $break_types{$type};
            }
            next;
        }
        die "malformed line $_";
    }
    close $INPUT;

    open OUTPUT,">$filename.new" or die "Cannot create $filename";
    print "Building $filename\n";
    print OUTPUT "/* Unicode Line Break Properties */\n";
1081
    print OUTPUT "/* generated from $UNIDATA:LineBreak.txt */\n";
1082
    print OUTPUT "/* DO NOT EDIT!! */\n\n";
1083
    print OUTPUT "#include \"windef.h\"\n\n";
1084

1085
    dump_two_level_mapping( "wine_linebreak_table", $break_types{'XX'}, 16, @break_table );
1086 1087 1088 1089 1090

    close OUTPUT;
    save_file($filename);
}

1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220
my %scripts =
(
    "Unknown"                => 0,
    "Common"                 => 1,
    "Inherited"              => 2,
    "Arabic"                 => 3,
    "Armenian"               => 4,
    "Avestan"                => 5,
    "Balinese"               => 6,
    "Bamum"                  => 7,
    "Batak"                  => 8,
    "Bengali"                => 9,
    "Bopomofo"               => 10,
    "Brahmi"                 => 11,
    "Braille"                => 12,
    "Buginese"               => 13,
    "Buhid"                  => 14,
    "Canadian_Aboriginal"    => 15,
    "Carian"                 => 16,
    "Cham"                   => 17,
    "Cherokee"               => 18,
    "Coptic"                 => 19,
    "Cuneiform"              => 20,
    "Cypriot"                => 21,
    "Cyrillic"               => 22,
    "Deseret"                => 23,
    "Devanagari"             => 24,
    "Egyptian_Hieroglyphs"   => 25,
    "Ethiopic"               => 26,
    "Georgian"               => 27,
    "Glagolitic"             => 28,
    "Gothic"                 => 29,
    "Greek"                  => 30,
    "Gujarati"               => 31,
    "Gurmukhi"               => 32,
    "Han"                    => 33,
    "Hangul"                 => 34,
    "Hanunoo"                => 35,
    "Hebrew"                 => 36,
    "Hiragana"               => 37,
    "Imperial_Aramaic"       => 38,
    "Inscriptional_Pahlavi"  => 39,
    "Inscriptional_Parthian" => 40,
    "Javanese"               => 41,
    "Kaithi"                 => 42,
    "Kannada"                => 43,
    "Katakana"               => 44,
    "Kayah_Li"               => 45,
    "Kharoshthi"             => 46,
    "Khmer"                  => 47,
    "Lao"                    => 48,
    "Latin"                  => 49,
    "Lepcha"                 => 50,
    "Limbu"                  => 51,
    "Linear_B"               => 52,
    "Lisu"                   => 53,
    "Lycian"                 => 54,
    "Lydian"                 => 55,
    "Malayalam"              => 56,
    "Mandaic"                => 57,
    "Meetei_Mayek"           => 58,
    "Mongolian"              => 59,
    "Myanmar"                => 60,
    "New_Tai_Lue"            => 61,
    "Nko"                    => 62,
    "Ogham"                  => 63,
    "Ol_Chiki"               => 64,
    "Old_Italic"             => 65,
    "Old_Persian"            => 66,
    "Old_South_Arabian"      => 67,
    "Old_Turkic"             => 68,
    "Oriya"                  => 69,
    "Osmanya"                => 70,
    "Phags_Pa"               => 71,
    "Phoenician"             => 72,
    "Rejang"                 => 73,
    "Runic"                  => 74,
    "Samaritan"              => 75,
    "Saurashtra"             => 76,
    "Shavian"                => 77,
    "Sinhala"                => 78,
    "Sundanese"              => 79,
    "Syloti_Nagri"           => 80,
    "Syriac"                 => 81,
    "Tagalog"                => 82,
    "Tagbanwa"               => 83,
    "Tai_Le"                 => 84,
    "Tai_Tham"               => 85,
    "Tai_Viet"               => 86,
    "Tamil"                  => 87,
    "Telugu"                 => 88,
    "Thaana"                 => 89,
    "Thai"                   => 90,
    "Tibetan"                => 91,
    "Tifinagh"               => 92,
    "Ugaritic"               => 93,
    "Vai"                    => 94,
    "Yi"                     => 95,
    # Win8/Win8.1
    "Chakma"                 => 96,
    "Meroitic_Cursive"       => 97,
    "Meroitic_Hieroglyphs"   => 98,
    "Miao"                   => 99,
    "Sharada"                => 100,
    "Sora_Sompeng"           => 101,
    "Takri"                  => 102,
    # Win10
    "Bassa_Vah"              => 103,
    "Caucasian_Albanian"     => 104,
    "Duployan"               => 105,
    "Elbasan"                => 106,
    "Grantha"                => 107,
    "Khojki"                 => 108,
    "Khudawadi"              => 109,
    "Linear_A"               => 110,
    "Mahajani"               => 111,
    "Manichaean"             => 112,
    "Mende_Kikakui"          => 113,
    "Modi"                   => 114,
    "Mro"                    => 115,
    "Nabataean"              => 116,
    "Old_North_Arabian"      => 117,
    "Old_Permic"             => 118,
    "Pahawh_Hmong"           => 119,
    "Palmyrene"              => 120,
    "Pau_Cin_Hau"            => 121,
    "Psalter_Pahlavi"        => 122,
    "Siddham"                => 123,
    "Tirhuta"                => 124,
    "Warang_Citi"            => 125,
1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232
    # Win10 RS1
    "Adlam"                  => 126,
    "Ahom"                   => 127,
    "Anatolian_Hieroglyphs"  => 128,
    "Bhaiksuki"              => 129,
    "Hatran"                 => 130,
    "Marchen"                => 131,
    "Multani"                => 132,
    "Newa"                   => 133,
    "Old_Hungarian"          => 134,
    "Osage"                  => 135,
    "SignWriting"            => 136,
1233 1234 1235 1236 1237 1238
    "Tangut"                 => 137,
    # Win10 RS4
    "Masaram_Gondi"          => 138,
    "Nushu"                  => 139,
    "Soyombo"                => 140,
    "Zanabazar_Square"       => 141,
1239 1240 1241 1242 1243 1244 1245 1246
    # Win10 1903
    "Dogra"                  => 142,
    "Gunjala_Gondi"          => 143,
    "Hanifi_Rohingya"        => 144,
    "Makasar"                => 145,
    "Medefaidrin"            => 146,
    "Old_Sogdian"            => 147,
    "Sogdian"                => 148,
1247 1248
);

1249 1250 1251 1252 1253 1254
################################################################
# dump Script IDs table
sub dump_scripts($)
{
    my $filename = shift;
    my $header = $filename;
1255
    my @scripts_table;
1256
    my $script_index;
1257 1258 1259
    my $i;

    my $INPUT = open_data_file( $UNIDATA, "Scripts.txt" );
1260 1261
    # Fill the table
    # Unknown script id is always 0, so undefined scripts are automatically treated as such
1262 1263 1264 1265 1266 1267 1268
    while (<$INPUT>)
    {
        my $type = "";

        next if /^\#/;  # skip comments
        next if /^\s*$/;  # skip empty lines
        next if /\x1a/;  # skip ^Z
1269
        if (/^\s*([0-9a-fA-F]+)\s*;\s*([a-zA-Z_]+)\s*/)
1270 1271 1272 1273 1274 1275 1276 1277
        {
            $type = $2;
            if (defined $scripts{$type})
            {
                $scripts_table[hex $1] = $scripts{$type};
            }
            next;
        }
1278
        elsif (/^\s*([0-9a-fA-F]+)..\s*([0-9a-fA-F]+)\s*;\s*([a-zA-Z_]+)\s*/)
1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297
        {
            $type = $3;
            if (defined $scripts{$type})
            {
                foreach my $i (hex $1 .. hex $2)
                {
                    $scripts_table[$i] = $scripts{$type};
                }
            }
            next;
        }
    }

    close $INPUT;

    $header = "$filename.h";
    open OUTPUT,">$header.new" or die "Cannot create $header";
    print "Building $header\n";
    print OUTPUT "/* Unicode Script IDs */\n";
1298
    print OUTPUT "/* generated from $UNIDATA:Scripts.txt */\n";
1299 1300 1301
    print OUTPUT "/* DO NOT EDIT!! */\n\n";

    print OUTPUT "enum unicode_script_id {\n";
1302
    foreach my $script (sort { $scripts{$a} <=> $scripts{$b} } keys %scripts)
1303 1304 1305
    {
        print OUTPUT "    Script_$script = $scripts{$script},\n";
    }
1306
    print OUTPUT "    Script_LastId = ", (scalar keys %scripts) - 1, "\n";
1307 1308 1309 1310 1311 1312 1313 1314 1315
    print OUTPUT "};\n";

    close OUTPUT;
    save_file($header);

    $filename = "$filename.c";
    open OUTPUT,">$filename.new" or die "Cannot create $header";
    print "Building $filename\n";
    print OUTPUT "/* Unicode Script IDs */\n";
1316
    print OUTPUT "/* generated from $UNIDATA:Scripts.txt */\n";
1317
    print OUTPUT "/* DO NOT EDIT!! */\n\n";
1318
    print OUTPUT "#include \"windef.h\"\n\n";
1319

1320
    dump_two_level_mapping( "wine_scripts_table", 0, 16, @scripts_table );
1321 1322 1323
    close OUTPUT;
    save_file($filename);
}
1324

1325 1326 1327 1328 1329 1330 1331
################################################################
# dump the BiDi mirroring table
sub dump_mirroring($)
{
    my $filename = shift;
    my @mirror_table = ();

1332
    my $INPUT = open_data_file( $UNIDATA, "BidiMirroring.txt" );
1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349
    while (<$INPUT>)
    {
        next if /^\#/;  # skip comments
        next if /^$/;  # skip empty lines
        next if /\x1a/;  # skip ^Z
        if (/^\s*([0-9a-fA-F]+)\s*;\s*([0-9a-fA-F]+)/)
        {
            $mirror_table[hex $1] = hex $2;
            next;
        }
        die "malformed line $_";
    }
    close $INPUT;

    open OUTPUT,">$filename.new" or die "Cannot create $filename";
    print "Building $filename\n";
    print OUTPUT "/* Unicode BiDi mirroring */\n";
1350
    print OUTPUT "/* generated from $UNIDATA:BidiMirroring.txt */\n";
1351
    print OUTPUT "/* DO NOT EDIT!! */\n\n";
1352
    print OUTPUT "#include \"windef.h\"\n\n";
1353
    dump_two_level_mapping( "wine_mirror_map", 0, 16, @mirror_table );
1354 1355 1356 1357
    close OUTPUT;
    save_file($filename);
}

1358 1359 1360 1361 1362
################################################################
# dump the Bidi Brackets
sub dump_bracket($)
{
    my $filename = shift;
1363
    my @bracket_table;
1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374

    my $INPUT = open_data_file( $UNIDATA, "BidiBrackets.txt" );
    while (<$INPUT>)
    {
        next if /^\#/;  # skip comments
        next if /^\s*$/;  # skip empty lines
        next if /\x1a/;  # skip ^Z
        if (/^\s*([0-9a-fA-F]+)\s*;\s*([0-9a-fA-F]+);\s*([con])/)
        {
            my $type = $3;
            die "unknown bracket $type" unless defined $bracket_types{$type};
1375
            die "characters too distant $1 and $2" if abs(hex($2) - hex($1)) >= 128;
1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386
            $bracket_table[hex $1] = (hex($2) - hex($1)) % 255;
            $bracket_table[hex $1] += $bracket_types{$type} << 8;
            next;
        }
        die "malformed line $_";
    }
    close $INPUT;

    open OUTPUT,">$filename.new" or die "Cannot create $filename";
    print "Building $filename\n";
    print OUTPUT "/* Unicode Bidirectional Bracket table */\n";
1387
    print OUTPUT "/* generated from $UNIDATA:BidiBrackets.txt */\n";
1388
    print OUTPUT "/* DO NOT EDIT!! */\n\n";
1389
    print OUTPUT "#include \"windef.h\"\n\n";
1390

1391
    dump_two_level_mapping( "bidi_bracket_table", 0, 16, @bracket_table );
1392 1393 1394 1395

    close OUTPUT;
    save_file($filename);
}
1396

1397 1398 1399 1400 1401 1402 1403 1404 1405 1406
################################################################
# dump the Arabic shaping table
sub dump_shaping($)
{
    my $filename = shift;
    my %groups;
    my $next_group = 0;

    $groups{"No_Joining_Group"} = $next_group++;

1407
    my $INPUT = open_data_file( $UNIDATA, "ArabicShaping.txt" );
1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427
    while (<$INPUT>)
    {
        next if /^\#/;  # skip comments
        next if /^\s*$/;  # skip empty lines
        next if /\x1a/;  # skip ^Z
        if (/^\s*([0-9a-fA-F]+)\s*;.*;\s*([RLDCUT])\s*;\s*(\w+)/)
        {
            my $type = $2;
            my $group = $3;
            $groups{$group} = $next_group++ unless defined $groups{$group};
            $joining_table[hex $1] = $joining_types{$type} | ($groups{$group} << 8);
            next;
        }
        die "malformed line $_";
    }
    close $INPUT;

    open OUTPUT,">$filename.new" or die "Cannot create $filename";
    print "Building $filename\n";
    print OUTPUT "/* Unicode Arabic shaping */\n";
1428
    print OUTPUT "/* generated from $UNIDATA:ArabicShaping.txt */\n";
1429
    print OUTPUT "/* DO NOT EDIT!! */\n\n";
1430
    print OUTPUT "#include \"windef.h\"\n\n";
1431

1432
    dump_two_level_mapping( "wine_shaping_table", 0, 16, @joining_table );
1433

1434
    print OUTPUT "\nconst unsigned short DECLSPEC_HIDDEN wine_shaping_forms[256][4] =\n{\n";
1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448
    for (my $i = 0x600; $i <= 0x6ff; $i++)
    {
        printf OUTPUT "    { 0x%04x, 0x%04x, 0x%04x, 0x%04x },\n",
            ${joining_forms{"isolated"}}[$i] || $i,
            ${joining_forms{"final"}}[$i] || $i,
            ${joining_forms{"initial"}}[$i] || $i,
            ${joining_forms{"medial"}}[$i] || $i;
    }
    print OUTPUT "};\n";

    close OUTPUT;
    save_file($filename);
}

1449 1450 1451 1452 1453
################################################################
# dump the Vertical Orientation table
sub dump_vertical($)
{
    my $filename = shift;
1454
    my @vertical_table;
1455

1456
    my $INPUT = open_data_file( $UNIDATA, "VerticalOrientation.txt" );
1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488
    while (<$INPUT>)
    {
        next if /^\#/;  # skip comments
        next if /^\s*$/;  # skip empty lines
        next if /\x1a/;  # skip ^Z
        if (/^\s*([0-9a-fA-F]+)\s*;\s*([a-zA-Z_]+)\s*/)
        {
            my $type = $2;
            die "unknown vertical $type" unless defined $vertical_types{$type};
            if (hex $1 < 65536)
            {
                $vertical_table[hex $1] = $vertical_types{$type};
            }
            next;
        }
        elsif (/^\s*([0-9a-fA-F]+)..\s*([0-9a-fA-F]+)\s*;\s*([A-Za-z_]+)\s*/)
        {
            my $type = $3;
            die "unknown vertical $type" unless defined $vertical_types{$type};
            foreach my $i (hex $1 .. hex $2)
            {
                $vertical_table[$i] = $vertical_types{$type};
            }
            next;
        }
        die "malformed line $_";
    }
    close $INPUT;

    open OUTPUT,">$filename.new" or die "Cannot create $filename";
    print "Building $filename\n";
    print OUTPUT "/* Unicode Vertical Orientation */\n";
1489
    print OUTPUT "/* generated from $UNIDATA:VerticalOrientation.txt */\n";
1490
    print OUTPUT "/* DO NOT EDIT!! */\n\n";
1491
    print OUTPUT "#include \"windef.h\"\n\n";
1492

1493
    dump_two_level_mapping( "vertical_orientation_table", $vertical_types{'R'}, 16, @vertical_table );
1494 1495 1496 1497

    close OUTPUT;
    save_file($filename);
}
1498

1499 1500
################################################################
# dump the digit folding tables
1501
sub dump_digit_folding($)
1502
{
1503
    my ($filename) = shift;
1504 1505 1506
    open OUTPUT,">$filename.new" or die "Cannot create $filename";
    print "Building $filename\n";
    print OUTPUT "/* Unicode digit folding mappings */\n";
1507
    print OUTPUT "/* generated from $UNIDATA:UnicodeData.txt */\n";
1508
    print OUTPUT "/* DO NOT EDIT!! */\n\n";
1509
    print OUTPUT "#include \"windef.h\"\n\n";
1510

1511
    dump_two_level_mapping( "wine_digitmap", 0, 16, @digitmap_table );
1512 1513 1514 1515 1516
    close OUTPUT;
    save_file($filename);
}


1517 1518
################################################################
# dump the case mapping tables
1519
sub dump_case_mappings($)
1520
{
1521 1522
    my $filename = shift;
    open OUTPUT,">$filename.new" or die "Cannot create $filename";
1523 1524
    print "Building $filename\n";
    print OUTPUT "/* Unicode case mappings */\n";
1525
    print OUTPUT "/* generated from $UNIDATA:UnicodeData.txt */\n";
1526
    print OUTPUT "/* DO NOT EDIT!! */\n\n";
1527
    print OUTPUT "#include \"windef.h\"\n\n";
1528

1529
    dump_case_table( "wine_casemap_lower", @tolower_table );
1530
    print OUTPUT "\n";
1531
    dump_case_table( "wine_casemap_upper", @toupper_table );
1532
    close OUTPUT;
1533
    save_file($filename);
1534 1535 1536 1537 1538
}


################################################################
# dump a case mapping table
1539
sub dump_case_table($@)
1540 1541 1542 1543
{
    my ($name,@table) = @_;

    # count the number of sub tables that contain something
1544
    # also compute the low and upper populated bounds
1545

1546 1547 1548
    my @lowerbounds = ( 0, 0 );
    my @upperbounds = ( 0, 255 );
    my $index = 0;
1549
    my @filled = ();
1550
    for (my $i = 0; $i < 65536; $i++)
1551 1552
    {
        next unless defined $table[$i];
1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564
        if (!defined $filled[$i >> 8])
        {
          $lowerbounds[$index] = $i & 0xff;
          $upperbounds[$index] = 0xff - $lowerbounds[$index];
          $filled[$i >> 8] = $index * 256 + 512;
          $index++;
        }
        else
        {
          $upperbounds[$index-1] = 0xff - ($i & 0xff);
        }
        $table[$i] = ($table[$i] - $i) & 0xffff;
1565
    }
1566 1567 1568 1569

    # Collapse blocks upwards if possible
    my $removed = 0;
    $index = 0;
1570
    for (my $i = 0; $i < 256; $i++)
1571
    {
1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583
        next unless defined $filled[$i];
        if ($upperbounds[$index - 1] > $lowerbounds[$index])
        {
           $removed = $removed + $lowerbounds[$index];
        }
        else
        {
           $removed = $removed + $upperbounds[$index - 1];
           $lowerbounds[$index] = $upperbounds[$index - 1];
        }
        $filled[$i] = $filled[$i] - $removed;
        $index++;
1584 1585 1586 1587
    }

    # dump the table

1588
    printf OUTPUT "const WCHAR %s[%d] =\n", $name, $index * 256 + 512 - $removed;
1589
    printf OUTPUT "{\n    /* index */\n";
1590
    printf OUTPUT "%s,\n", dump_array( 16, 256, @filled );
1591
    printf OUTPUT "    /* defaults */\n";
1592
    printf OUTPUT "%s", dump_array( 16, 0, (0) x 256 );
1593
    $index = 0;
1594
    for (my $i = 0; $i < 256; $i++)
1595 1596
    {
        next unless $filled[$i];
1597
        printf OUTPUT ",\n    /* 0x%02x%02x .. 0x%02xff */\n", $i, $lowerbounds[$index], $i;
1598
        printf OUTPUT "%s", dump_array( 16, 0, @table[($i<<8) + $lowerbounds[$index] .. ($i<<8)+255] );
1599
        $index++;
1600 1601 1602 1603
    }
    printf OUTPUT "\n};\n";
}

1604
################################################################
1605
# compress a mapping table by removing identical rows
1606
sub compress_array($$@)
1607
{
1608
    my $rows = shift;
1609
    my $def = shift;
1610
    my @table = @_;
1611
    my $len = @table / $rows;
1612 1613
    my @array;
    my $data = "";
1614 1615

    # try to merge table rows
1616
    for (my $row = 0; $row < $rows; $row++)
1617
    {
1618 1619 1620
        my $rowtxt = pack "U*", map { defined($_) ? $_ : $def; } @table[($row * $len)..(($row + 1) * $len - 1)];
        my $pos = index $data, $rowtxt;
        if ($pos == -1)
1621
        {
1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634
            # check if the tail of the data can match the start of the new row
            my $first = substr( $rowtxt, 0, 1 );
            for (my $i = length($data) - 1; $i > 0; $i--)
            {
                $pos = index( substr( $data, -$i ), $first );
                last if $pos == -1;
                $i -= $pos;
                next unless substr( $data, -$i ) eq substr( $rowtxt, 0, $i );
                substr( $data, -$i ) = "";
                last;
            }
            $pos = length $data;
            $data .= $rowtxt;
1635
        }
1636
        $array[$row] = $rows + $pos;
1637
    }
1638
    return @array, unpack "U*", $data;
1639 1640 1641 1642
}

################################################################
# dump a simple char -> 16-bit value mapping table
1643
sub dump_simple_mapping($$@)
1644 1645
{
    my $name = shift;
1646 1647
    my $def = shift;
    my @array = compress_array( 256, $def, @_[0..65535] );
1648

1649
    printf OUTPUT "const unsigned short %s[%d] =\n{\n", $name, $#array+1;
1650 1651
    printf OUTPUT "    /* offsets */\n%s,\n", dump_array( 16, 0, @array[0..255] );
    printf OUTPUT "    /* values */\n%s\n};\n", dump_array( 16, 0, @array[256..$#array] );
1652
}
1653

1654
################################################################
1655
# dump a char -> 16-bit value mapping table using two-level tables
1656
sub dump_two_level_mapping($$@)
1657 1658
{
    my $name = shift;
1659
    my $def = shift;
1660 1661
    my $size = shift;
    my $type = $size == 16 ? "unsigned short" : "unsigned int";
1662 1663
    my @row_array = compress_array( 4096, $def, @_[0..65535] );
    my @array = compress_array( 256, 0, @row_array[0..4095] );
1664 1665 1666

    for (my $i = 256; $i < @array; $i++) { $array[$i] += @array - 4096; }

1667 1668 1669 1670
    printf OUTPUT "const %s DECLSPEC_HIDDEN %s[%d] =\n{\n", $type, $name, @array + @row_array - 4096;
    printf OUTPUT "    /* level 1 offsets */\n%s,\n", dump_array( $size, 0, @array[0..255] );
    printf OUTPUT "    /* level 2 offsets */\n%s,\n", dump_array( $size, 0, @array[256..$#array] );
    printf OUTPUT "    /* values */\n%s\n};\n", dump_array( $size, 0, @row_array[4096..$#row_array] );
1671 1672 1673
}

################################################################
1674 1675 1676 1677
# dump a binary case mapping table in l_intl.nls format
sub dump_binary_case_table(@)
{
    my (@table) = @_;
1678 1679 1680
    my $max_char = 0x10000;
    my $level1 = $max_char / 16;
    my $level2 = $level1 / 16;
1681

1682 1683
    my @difftable;
    for (my $i = 0; $i < @table; $i++)
1684
    {
1685 1686
        next unless defined $table[$i];
        $difftable[$i] = ($table[$i] - $i) & 0xffff;
1687 1688
    }

1689 1690 1691 1692
    my @row_array = compress_array( $level1, 0, @difftable[0..$max_char-1] );
    my @array = compress_array( $level2, 0, @row_array[0..$level1-1] );
    for (my $i = $level2; $i < @array; $i++) { $array[$i] += @array - $level1; }
    return @array, @row_array[$level1..$#row_array];
1693 1694 1695 1696 1697 1698 1699
}


################################################################
# dump case mappings for l_intl.nls
sub dump_intl_nls($)
{
1700 1701 1702
    my @upper = dump_binary_case_table( @toupper_table );
    my @lower = dump_binary_case_table( @tolower_table );

1703 1704 1705 1706 1707 1708
    my $filename = shift;
    open OUTPUT,">$filename.new" or die "Cannot create $filename";
    printf "Building $filename\n";

    binmode OUTPUT;
    print OUTPUT pack "S<", 1;  # version
1709 1710
    print OUTPUT pack "S<*", 1 + scalar @upper, @upper;
    print OUTPUT pack "S<*", 1 + scalar @lower, @lower;
1711 1712 1713 1714 1715
    close OUTPUT;
    save_file($filename);
}


1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783
sub load_nameprep_range_table($$$)
{
    my ($INPUT, $val, $table_ref) = @_;

    while (<$INPUT>)
    {
        if (/^\s*([0-9a-fA-F]+)-([0-9a-fA-F]+)/)
        {
            my $last = hex $2;
            $last = 65535 if($last >= 65536);
            foreach my $i (hex $1 .. $last)
            {
                $table_ref->[$i] |= $val;
            }
            next;
        }
        elsif (/^\s*([0-9a-fA-F]+)/)
        {
            if (hex $1 < 65536)
            {
                $table_ref->[hex $1] |= $val;
            }
            next;
        }

        return if (/End\sTable/);
    }
}

sub load_nameprep_map_table($$)
{
    my ($INPUT, $table_ref) = @_;

    while (<$INPUT>)
    {
        if (/^\s*([0-9a-fA-F]+);\s;/)
        {
            # special value for map to nothing
            $table_ref->[hex $1] = [0xffff, 0xffff, 0xffff];
            next;
        }
        elsif (/^\s*([0-9a-fA-F]+);\s([0-9a-fA-F]+);/)
        {
            $table_ref->[hex $1] = [hex $2, 0, 0];
            next;
        }
        elsif (/^\s*([0-9a-fA-F]+);\s([0-9a-fA-F]+)\s([0-9a-fA-F]+);/)
        {
            $table_ref->[hex $1] = [hex $2, hex $3, 0];
            next;
        }
        elsif (/^\s*([0-9a-fA-F]+);\s([0-9a-fA-F]+)\s([0-9a-fA-F]+)\s([0-9a-fA-F]+);/)
        {
            $table_ref->[hex $1] = [hex $2, hex $3, hex $4];
            next;
        }

        return if (/End\sTable/);
    }
}

################################################################
# dump mapping table, prohibited characters set, unassigned
# characters, bidirectional rules used by nameprep algorithm
sub dump_nameprep($)
{
    my $filename = shift;
    my @mapping_table = ();
1784
    my @flags_table;
1785

1786
    my $INPUT = open_data_file( $RFCS, $STRINGPREP );
1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811
    while (<$INPUT>)
    {
        next unless /Start\sTable/;

        load_nameprep_range_table($INPUT, $nameprep_flags{"unassigned"}, \@flags_table) if (/A.1/);
        load_nameprep_range_table($INPUT, $nameprep_flags{"prohibited"}, \@flags_table) if (/C.1.2/);
        load_nameprep_range_table($INPUT, $nameprep_flags{"prohibited"}, \@flags_table) if (/C.2.2/);
        load_nameprep_range_table($INPUT, $nameprep_flags{"prohibited"}, \@flags_table) if (/C.3/);
        load_nameprep_range_table($INPUT, $nameprep_flags{"prohibited"}, \@flags_table) if (/C.4/);
        load_nameprep_range_table($INPUT, $nameprep_flags{"prohibited"}, \@flags_table) if (/C.5/);
        load_nameprep_range_table($INPUT, $nameprep_flags{"prohibited"}, \@flags_table) if (/C.6/);
        load_nameprep_range_table($INPUT, $nameprep_flags{"prohibited"}, \@flags_table) if (/C.7/);
        load_nameprep_range_table($INPUT, $nameprep_flags{"prohibited"}, \@flags_table) if (/C.8/);
        load_nameprep_range_table($INPUT, $nameprep_flags{"prohibited"}, \@flags_table) if (/C.9/);
        load_nameprep_range_table($INPUT, $nameprep_flags{"bidi_ral"}, \@flags_table) if (/D.1/);
        load_nameprep_range_table($INPUT, $nameprep_flags{"bidi_l"}, \@flags_table) if (/D.2/);

        load_nameprep_map_table($INPUT, \@mapping_table) if (/B.1/);
        load_nameprep_map_table($INPUT, \@mapping_table) if (/B.2/);
    }
    close $INPUT;

    open OUTPUT,">$filename.new" or die "Cannot create $filename";
    print "Building $filename\n";
    print OUTPUT "/* Nameprep algorithm related data */\n";
1812
    print OUTPUT "/* generated from $RFCS/$STRINGPREP */\n";
1813
    print OUTPUT "/* DO NOT EDIT!! */\n\n";
1814
    print OUTPUT "#include \"windef.h\"\n\n";
1815

1816
    dump_two_level_mapping( "nameprep_char_type", 0, 16, @flags_table );
1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851

    ######### mapping table
    # first determine all the 16-char subsets that contain something
    my @filled = ();
    my $pos = 16*3;  # for the null subset
    for (my $i = 0; $i < 65536; $i++)
    {
        next unless defined $mapping_table[$i];
        $filled[$i >> 4] = $pos;
        $pos += 16*3;
        $i |= 15;
    }
    my $total = $pos;

    # now count the 256-char subsets that contain something
    my @filled_idx = (256) x 256;
    $pos = 256 + 16;
    for (my $i = 0; $i < 4096; $i++)
    {
        next unless $filled[$i];
        $filled_idx[$i >> 4] = $pos;
        $pos += 16;
        $i |= 15;
    }
    my $null_offset = $pos;
    $total += $pos;

    # add the index offsets to the subsets positions
    for (my $i = 0; $i < 4096; $i++)
    {
        next unless $filled[$i];
        $filled[$i] += $null_offset;
    }

    # dump the main index
1852
    printf OUTPUT "const unsigned short DECLSPEC_HIDDEN nameprep_mapping[%d] =\n", $total;
1853
    printf OUTPUT "{\n    /* index */\n";
1854 1855
    printf OUTPUT "%s", dump_array( 16, 0, @filled_idx );
    printf OUTPUT ",\n    /* null sub-index */\n%s", dump_array( 16, 0, ($null_offset) x 16 );
1856 1857 1858 1859 1860 1861 1862 1863

    # dump the second-level indexes
    for (my $i = 0; $i < 256; $i++)
    {
        next unless ($filled_idx[$i] > 256);
        my @table = @filled[($i<<4)..($i<<4)+15];
        for (my $j = 0; $j < 16; $j++) { $table[$j] ||= $null_offset; }
        printf OUTPUT ",\n    /* sub-index %02x */\n", $i;
1864
        printf OUTPUT "%s", dump_array( 16, 0, @table );
1865 1866 1867 1868
    }

    # dump the 16-char subsets
    printf OUTPUT ",\n    /* null mapping */\n";
1869
    printf OUTPUT "%s", dump_array( 16, 0, (0) x 48 );
1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884

    for (my $i = 0; $i < 4096; $i++)
    {
        next unless $filled[$i];
        my @table = (0) x 48;
        for (my $j = 0; $j < 16; $j++)
        {
            if (defined $mapping_table[($i<<4) + $j])
            {
                $table[3 * $j] = ${$mapping_table[($i << 4) + $j]}[0];
                $table[3 * $j + 1] = ${$mapping_table[($i << 4) + $j]}[1];
                $table[3 * $j + 2] = ${$mapping_table[($i << 4) + $j]}[2];
            }
        }
        printf OUTPUT ",\n    /* 0x%03x0 .. 0x%03xf */\n", $i, $i;
1885
        printf OUTPUT "%s", dump_array( 16, 0, @table );
1886 1887 1888 1889 1890 1891 1892 1893
    }

    printf OUTPUT "\n};\n";

    close OUTPUT;
    save_file($filename);
}

1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910

################################################################
# dump the GetStringTypeW table
sub dump_string_type_table($)
{
    my $filename = shift;
    open OUTPUT,">$filename.new" or die "Cannot create $filename";
    printf "Building $filename\n";
    printf OUTPUT "/* Unicode wctype table */\n";
    printf OUTPUT "/* Automatically generated; DO NOT EDIT!! */\n\n";
    printf OUTPUT "#include \"windef.h\"\n\n";

    my @table = @category_table;

    # add the direction in the high 4 bits of the category
    for (my $i = 0; $i < 65536; $i++)
    {
1911
        $table[$i] |= $c2_types{$direction_table[$i]} << 12 if defined $direction_table[$i];
1912 1913
    }

1914
    dump_two_level_mapping( "wctype_table", 0, 16, @table );
1915 1916 1917 1918 1919 1920

    close OUTPUT;
    save_file($filename);
}


1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935
################################################################
# dump the bidi direction table
sub dump_bidi_dir_table($)
{
    my $filename = shift;
    open OUTPUT,">$filename.new" or die "Cannot create $filename";
    printf "Building $filename\n";
    printf OUTPUT "/* Unicode BiDi direction table */\n";
    printf OUTPUT "/* Automatically generated; DO NOT EDIT!! */\n\n";
    printf OUTPUT "#include \"windef.h\"\n\n";

    my @table;

    for (my $i = 0; $i < 65536; $i++)
    {
1936
        $table[$i] = $bidi_types{$direction_table[$i]} if defined $direction_table[$i];
1937 1938
    }

1939
    dump_two_level_mapping( "bidi_direction_table", $bidi_types{"L"}, 16, @table );
1940 1941 1942 1943 1944 1945

    close OUTPUT;
    save_file($filename);
}


1946 1947
################################################################
# dump the ctype tables
1948
sub dump_ctype_tables($)
1949
{
1950 1951 1952
    my $filename = shift;
    open OUTPUT,">$filename.new" or die "Cannot create $filename";
    printf "Building $filename\n";
1953 1954
    printf OUTPUT "/* Unicode ctype tables */\n";
    printf OUTPUT "/* Automatically generated; DO NOT EDIT!! */\n\n";
1955
    printf OUTPUT "#include \"windef.h\"\n\n";
1956

1957 1958
    my @table = @category_table;

1959
    # add the direction in the high 4 bits of the category
1960
    for (my $i = 0; $i < 65536; $i++)
1961
    {
1962
        $table[$i] |= $directions{$direction_table[$i]} << 12 if defined $direction_table[$i];
1963 1964
    }

1965
    dump_simple_mapping( "wine_wctype_table", 0, @table );
1966 1967

    close OUTPUT;
1968
    save_file($filename);
1969 1970
}

1971 1972

################################################################
1973 1974
# dump the char composition table
sub dump_compose_table($)
1975
{
1976 1977 1978
    my $filename = shift;

    open OUTPUT,">$filename.new" or die "Cannot create $filename";
1979 1980
    print "Building $filename\n";
    print OUTPUT "/* Unicode char composition */\n";
1981
    print OUTPUT "/* generated from $UNIDATA:UnicodeData.txt */\n";
1982
    print OUTPUT "/* DO NOT EDIT!! */\n\n";
1983
    print OUTPUT "#include \"windef.h\"\n\n";
1984 1985

    my @filled = ();
1986
    foreach my $i (@compose_table)
1987 1988 1989 1990 1991 1992 1993
    {
        my @comp = @$i;
        push @{$filled[$comp[1]]}, [ $comp[0], $comp[2] ];
    }

    # count how many different second chars we have

1994 1995
    my $count = 0;
    for (my $i = 0; $i < 65536; $i++)
1996 1997 1998 1999 2000 2001 2002 2003
    {
        next unless defined $filled[$i];
        $count++;
    }

    # build the table of second chars and offsets

    my $pos = $count + 1;
2004 2005
    my @table = ();
    for (my $i = 0; $i < 65536; $i++)
2006 2007 2008 2009 2010 2011 2012
    {
        next unless defined $filled[$i];
        push @table, $i, $pos;
        $pos += @{$filled[$i]};
    }
    # terminator with last position
    push @table, 0, $pos;
2013
    printf OUTPUT "static const WCHAR table[0x%x] =\n{\n", 2*$pos;
2014
    printf OUTPUT "    /* second chars + offsets */\n%s", dump_array( 16, 0, @table );
2015 2016 2017

    # build the table of first chars and mappings

2018
    for (my $i = 0; $i < 65536; $i++)
2019 2020 2021 2022
    {
        next unless defined $filled[$i];
        my @table = ();
        my @list = sort { $a->[0] <=> $b->[0] } @{$filled[$i]};
2023
        for (my $j = 0; $j <= $#list; $j++)
2024 2025 2026
        {
            push @table, $list[$j][0], $list[$j][1];
        }
2027
        printf OUTPUT ",\n    /* 0x%04x */\n%s", $i, dump_array( 16, 0, @table );
2028
    }
2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042
    print OUTPUT "\n};\n\n";
    print OUTPUT <<"EOF";
static inline int binary_search( WCHAR ch, int low, int high )
{
    while (low <= high)
    {
        int pos = (low + high) / 2;
        if (table[2 * pos] < ch) low = pos + 1;
        else if (table[2 * pos] > ch) high = pos - 1;
        else return pos;
    }
    return -1;
}

2043
WCHAR DECLSPEC_HIDDEN wine_compose( const WCHAR *str )
2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054
{
    int pos, idx = 1, start = 0, count = $count;
    for (;;)
    {
        if ((pos = binary_search( str[idx], start, count - 1 )) == -1) return 0;
        if (!idx--) return table[2 * pos + 1];
        start = table[2 * pos + 1];
        count = table[2 * pos + 3];
    }
}
EOF
2055 2056 2057 2058
    close OUTPUT;
    save_file($filename);
}

2059 2060 2061
################################################################
# dump a decomposition table
sub dump_decompositions($@)
2062
{
2063 2064
    my ($name, @decomp) = @_;

2065 2066 2067 2068 2069 2070 2071
    # first determine all the 16-char subsets that contain something

    my @filled = (0) x 4096;
    my $pos = 16;  # for the null subset
    my $data_total = 0;
    for (my $i = 0; $i < 65536; $i++)
    {
2072
        next unless defined $decomp[$i];
2073 2074 2075 2076 2077
        if ($filled[$i >> 4] == 0)
        {
            $filled[$i >> 4] = $pos;
            $pos += 16;
        }
2078
        $data_total += @{$decomp[$i]};
2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105
    }
    my $total = $pos;

    # now count the 256-char subsets that contain something

    my @filled_idx = (256) x 256;
    $pos = 256 + 16;
    for (my $i = 0; $i < 4096; $i++)
    {
        next unless $filled[$i];
        $filled_idx[$i >> 4] = $pos;
        $pos += 16;
        $i |= 15;
    }
    my $null_offset = $pos;  # null mapping
    $total += $pos + 1; # add the offset sentinel

    # add the index offsets to the subsets positions

    for (my $i = 0; $i < 4096; $i++)
    {
        next unless $filled[$i];
        $filled[$i] += $null_offset;
    }

    # dump the main index

2106
    printf OUTPUT "\nconst WCHAR DECLSPEC_HIDDEN %s[%d] =\n", $name, $total + $data_total;
2107
    printf OUTPUT "{\n    /* index */\n";
2108 2109
    printf OUTPUT "%s", dump_array( 16, 0, @filled_idx );
    printf OUTPUT ",\n    /* null sub-index */\n%s", dump_array( 16, 0, ($null_offset) x 16 );
2110 2111 2112 2113 2114 2115 2116 2117 2118

    # dump the second-level indexes

    for (my $i = 0; $i < 256; $i++)
    {
        next unless ($filled_idx[$i] > 256);
        my @table = @filled[($i<<4)..($i<<4)+15];
        for (my $j = 0; $j < 16; $j++) { $table[$j] ||= $null_offset; }
        printf OUTPUT ",\n    /* sub-index %02x */\n", $i;
2119
        printf OUTPUT "%s", dump_array( 16, 0, @table );
2120 2121 2122 2123 2124
    }

    # dump the 16-char offsets

    printf OUTPUT ",\n    /* null offsets */\n";
2125
    printf OUTPUT "%s", dump_array( 16, 0, ($total) x (16) );
2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136

    $pos = $total;

    my @data;
    for (my $i = 0; $i < 4096; $i++)
    {
        next unless $filled[$i];
        my @table = (0) x (16);
        for (my $j = 0; $j < 16; $j++)
        {
            $table[$j] = $pos;
2137
            if (defined $decomp[($i<<4) + $j])
2138
            {
2139 2140
                $pos += $#{$decomp[($i<<4) + $j]} + 1;
                push @data, @{$decomp[($i<<4) + $j]};
2141 2142 2143
            }
        }
        printf OUTPUT ",\n    /* offsets 0x%03x0 .. 0x%03xf */\n", $i, $i;
2144
        printf OUTPUT "%s", dump_array( 16, 0, @table );
2145 2146 2147 2148
    }

    my @sentinel = $pos;
    printf OUTPUT ",\n    /* offset sentinel */\n";
2149
    printf OUTPUT "%s", dump_array( 16, 0, @sentinel );
2150 2151

    printf OUTPUT ",\n    /* data */\n";
2152
    printf OUTPUT "%s", dump_array( 16, 0, @data );
2153

2154
    printf OUTPUT "\n};\n";
2155 2156
}

2157 2158
################################################################
# dump the char decomposition table
2159
sub dump_decompose_table($$)
2160
{
2161
    my ($filename, $compat) = @_;
2162

2163 2164 2165
    open OUTPUT,">$filename.new" or die "Cannot create $filename";
    print "Building $filename\n";
    print OUTPUT "/* Unicode char composition */\n";
2166
    print OUTPUT "/* generated from $UNIDATA:UnicodeData.txt */\n";
2167
    print OUTPUT "/* DO NOT EDIT!! */\n\n";
2168
    print OUTPUT "#include \"windef.h\"\n";
2169

2170 2171
    dump_decompositions( "nfd_table", build_decompositions( @decomp_table ));
    dump_decompositions( "nfkd_table", build_decompositions( @decomp_compat_table )) if $compat;
2172

2173
    close OUTPUT;
2174
    save_file($filename);
2175 2176
}

2177 2178 2179 2180 2181 2182 2183 2184 2185
################################################################
# dump the combining class table
sub dump_combining_class($)
{
    my $filename = shift;

    open OUTPUT,">$filename.new" or die "Cannot create $filename";
    print "Building $filename\n";
    print OUTPUT "/* Unicode Combining Classes */\n";
2186
    print OUTPUT "/* generated from $UNIDATA:UnicodeData.txt */\n";
2187
    print OUTPUT "/* DO NOT EDIT!! */\n\n";
2188
    print OUTPUT "#include \"windef.h\"\n\n";
2189

2190
    dump_two_level_mapping( "combining_class_table", 0, 16, @combining_class_table );
2191 2192 2193 2194
    close OUTPUT;
    save_file($filename);
}

2195
################################################################
2196
# output a codepage definition file from the global tables
2197
sub output_codepage_file($)
2198
{
2199
    my $codepage = shift;
2200

2201
    my $output = sprintf "nls/c_%03d.nls", $codepage;
2202 2203 2204 2205 2206 2207 2208 2209
    open OUTPUT,">$output.new" or die "Cannot create $output";

    printf "Building %s\n", $output;
    if (!@lead_bytes) { dump_binary_sbcs_table( $codepage ); }
    else { dump_binary_dbcs_table( $codepage ); }

    close OUTPUT;
    save_file($output);
2210 2211 2212
}

################################################################
2213
# output a codepage table from a Microsoft-style mapping file
2214
sub dump_msdata_codepage($)
2215
{
2216 2217
    my $filename = shift;

2218
    my $state = "";
2219
    my ($codepage, $width, $count);
2220 2221
    my ($lb_cur, $lb_end);

2222 2223 2224 2225 2226 2227 2228
    @cp2uni = ();
    @glyph2uni = ();
    @lead_bytes = ();
    @uni2cp = ();
    $default_char = $DEF_CHAR;
    $default_wchar = $DEF_CHAR;

2229
    my $INPUT = open_data_file( $MSCODEPAGES, $filename ) or die "Cannot open $filename";
2230

2231
    while (<$INPUT>)
2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245
    {
        next if /^;/;  # skip comments
        next if /^\s*$/;  # skip empty lines
        next if /\x1a/;  # skip ^Z
        last if /^ENDCODEPAGE/;

        if (/^CODEPAGE\s+(\d+)/)
        {
            $codepage = $1;
            next;
        }
        if (/^CPINFO\s+(\d+)\s+0x([0-9a-fA-f]+)\s+0x([0-9a-fA-F]+)/)
        {
            $width = $1;
2246 2247
            $default_char = hex $2;
            $default_wchar = hex $3;
2248 2249
            next;
        }
2250
        if (/^(MBTABLE|GLYPHTABLE|WCTABLE|DBCSRANGE|DBCSTABLE)\s+(\d+)/)
2251 2252 2253 2254 2255 2256 2257 2258 2259
        {
            $state = $1;
            $count = $2;
            next;
        }
        if (/^0x([0-9a-fA-F]+)\s+0x([0-9a-fA-F]+)/)
        {
            if ($state eq "MBTABLE")
            {
2260 2261
                my $cp = hex $1;
                my $uni = hex $2;
2262 2263 2264
                $cp2uni[$cp] = $uni unless defined($cp2uni[$cp]);
                next;
            }
2265 2266 2267 2268 2269 2270 2271
            if ($state eq "GLYPHTABLE")
            {
                my $cp = hex $1;
                my $uni = hex $2;
                $glyph2uni[$cp] = $uni unless defined($glyph2uni[$cp]);
                next;
            }
2272 2273
            if ($state eq "WCTABLE")
            {
2274 2275
                my $uni = hex $1;
                my $cp = hex $2;
2276 2277 2278 2279 2280
                $uni2cp[$uni] = $cp unless defined($uni2cp[$uni]);
                next;
            }
            if ($state eq "DBCSRANGE")
            {
2281 2282
                my $start = hex $1;
                my $end = hex $2;
2283
                for (my $i = $start; $i <= $end; $i++) { add_lead_byte( $i ); }
2284 2285 2286 2287 2288 2289
                $lb_cur = $start;
                $lb_end = $end;
                next;
            }
            if ($state eq "DBCSTABLE")
            {
2290 2291 2292
                my $mb = hex $1;
                my $uni = hex $2;
                my $cp = ($lb_cur << 8) | $mb;
2293 2294 2295 2296 2297 2298 2299 2300
                $cp2uni[$cp] = $uni unless defined($cp2uni[$cp]);
                if (!--$count)
                {
                    if (++$lb_cur > $lb_end) { $state = "DBCSRANGE"; }
                }
                next;
            }
        }
2301
        die "$filename: Unrecognized line $_\n";
2302
    }
2303
    close $INPUT;
2304

2305
    output_codepage_file( $codepage );
2306 2307 2308 2309 2310 2311 2312 2313
}


################################################################
# save a file if modified
sub save_file($)
{
    my $file = shift;
2314
    if (-f $file && !system "cmp $file $file.new >/dev/null")
2315 2316 2317 2318 2319 2320 2321
    {
        unlink "$file.new";
    }
    else
    {
        rename "$file.new", "$file";
    }
2322 2323 2324 2325 2326 2327
}


################################################################
# replace the contents of a file between ### cpmap ### marks

2328
sub REPLACE_IN_FILE($@)
2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344
{
    my $name = shift;
    my @data = @_;
    my @lines = ();
    open(FILE,$name) or die "Can't open $name";
    while (<FILE>)
    {
	push @lines, $_;
	last if /\#\#\# cpmap begin \#\#\#/;
    }
    push @lines, @data;
    while (<FILE>)
    {
	if (/\#\#\# cpmap end \#\#\#/) { push @lines, "\n", $_; last; }
    }
    push @lines, <FILE>;
2345
    open(FILE,">$name.new") or die "Can't modify $name";
2346 2347
    print FILE @lines;
    close(FILE);
2348
    save_file($name);
2349
}
2350 2351 2352 2353

################################################################
# main routine

2354
chdir ".." if -f "./make_unicode";
2355
load_data();
2356
dump_case_mappings( "libs/port/casemap.c" );
2357
dump_sortkeys( "dlls/kernelbase/collation.c" );
2358
dump_compose_table( "libs/port/compose.c" );
2359
dump_compose_table( "dlls/kernelbase/compose.c" );
2360 2361
dump_decompose_table( "libs/port/decompose.c", 1 );
dump_decompose_table( "dlls/kernelbase/decompose.c", 0 );
2362
dump_ctype_tables( "libs/port/wctype.c" );
2363
dump_bidi_dir_table( "dlls/gdi32/direction.c" );
2364
dump_bidi_dir_table( "dlls/usp10/direction.c" );
2365
dump_bidi_dir_table( "dlls/dwrite/direction.c" );
2366
dump_string_type_table( "dlls/kernelbase/wctype.c" );
2367
dump_digit_folding( "dlls/kernelbase/digitmap.c" );
2368
dump_combining_class( "dlls/ntdll/combclass.c" );
2369
dump_mirroring( "dlls/usp10/mirror.c" );
2370
dump_mirroring( "dlls/dwrite/mirror.c" );
2371
dump_bracket( "dlls/usp10/bracket.c" );
2372
dump_bracket( "dlls/dwrite/bracket.c" );
2373 2374
dump_shaping( "dlls/usp10/shaping.c" );
dump_linebreak( "dlls/usp10/linebreak.c" );
2375
dump_linebreak( "dlls/dwrite/linebreak.c" );
2376
dump_scripts( "dlls/dwrite/scripts" );
2377
dump_indic( "dlls/usp10/indicsyllable.c" );
2378 2379
dump_vertical( "dlls/gdi32/vertical.c" );
dump_vertical( "dlls/wineps.drv/vertical.c" );
2380
dump_nameprep( "dlls/kernel32/nameprep.c" );
2381
dump_intl_nls("nls/l_intl.nls");
2382 2383
foreach my $file (@allfiles) { dump_msdata_codepage( $file ); }
dump_eucjp_codepage();
2384 2385 2386 2387

exit 0;

# Local Variables:
2388
# compile-command: "./make_unicode"
2389
# End: