Allow multiple -B options.
[wine.git] / libs / unicode / cpmap.pl
blob71967b5ab6babb598f7bfcc53f11396526a10061
1 #!/usr/bin/perl
3 # Generate code page .c files from ftp.unicode.org descriptions
5 # Copyright 2000 Alexandre Julliard
7 # This library is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation; either
10 # version 2.1 of the License, or (at your option) any later version.
12 # This library is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # Lesser General Public License for more details.
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with this library; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 # base directory for ftp.unicode.org files
23 $BASEDIR = "ftp.unicode.org/Public/";
24 $MAPPREFIX = $BASEDIR . "MAPPINGS/";
26 # UnicodeData file
27 $UNICODEDATA = $BASEDIR . "UNIDATA/UnicodeData.txt";
29 # Sort keys file
30 $SORTKEYS = "www.unicode.org/reports/tr10/allkeys.txt";
32 # Defaults mapping
33 $DEFAULTS = "./defaults";
35 # Default char for undefined mappings
36 $DEF_CHAR = ord '?';
38 @allfiles =
40 [ 37, "VENDORS/MICSFT/EBCDIC/CP037.TXT", "IBM EBCDIC US Canada" ],
41 [ 424, "VENDORS/MISC/CP424.TXT", "IBM EBCDIC Hebrew" ],
42 [ 437, "VENDORS/MICSFT/PC/CP437.TXT", "OEM United States" ],
43 [ 500, "VENDORS/MICSFT/EBCDIC/CP500.TXT", "IBM EBCDIC International" ],
44 [ 737, "VENDORS/MICSFT/PC/CP737.TXT", "OEM Greek 437G" ],
45 [ 775, "VENDORS/MICSFT/PC/CP775.TXT", "OEM Baltic" ],
46 [ 850, "VENDORS/MICSFT/PC/CP850.TXT", "OEM Multilingual Latin 1" ],
47 [ 852, "VENDORS/MICSFT/PC/CP852.TXT", "OEM Slovak Latin 2" ],
48 [ 855, "VENDORS/MICSFT/PC/CP855.TXT", "OEM Cyrillic" ],
49 [ 856, "VENDORS/MISC/CP856.TXT", "Hebrew PC" ],
50 [ 857, "VENDORS/MICSFT/PC/CP857.TXT", "OEM Turkish" ],
51 [ 860, "VENDORS/MICSFT/PC/CP860.TXT", "OEM Portuguese" ],
52 [ 861, "VENDORS/MICSFT/PC/CP861.TXT", "OEM Icelandic" ],
53 [ 862, "VENDORS/MICSFT/PC/CP862.TXT", "OEM Hebrew" ],
54 [ 863, "VENDORS/MICSFT/PC/CP863.TXT", "OEM Canadian French" ],
55 [ 864, "VENDORS/MICSFT/PC/CP864.TXT", "OEM Arabic" ],
56 [ 865, "VENDORS/MICSFT/PC/CP865.TXT", "OEM Nordic" ],
57 [ 866, "VENDORS/MICSFT/PC/CP866.TXT", "OEM Russian" ],
58 [ 869, "VENDORS/MICSFT/PC/CP869.TXT", "OEM Greek" ],
59 [ 874, "VENDORS/MICSFT/PC/CP874.TXT", "ANSI/OEM Thai" ],
60 [ 875, "VENDORS/MICSFT/EBCDIC/CP875.TXT", "IBM EBCDIC Greek" ],
61 [ 878, "VENDORS/MISC/KOI8-R.TXT", "Russian KOI8" ],
62 [ 932, "VENDORS/MICSFT/WINDOWS/CP932.TXT", "ANSI/OEM Japanese Shift-JIS" ],
63 [ 936, "VENDORS/MICSFT/WINDOWS/CP936.TXT", "ANSI/OEM Simplified Chinese GBK" ],
64 [ 949, "VENDORS/MICSFT/WINDOWS/CP949.TXT", "ANSI/OEM Korean Unified Hangul" ],
65 [ 950, "VENDORS/MICSFT/WINDOWS/CP950.TXT", "ANSI/OEM Traditional Chinese Big5" ],
66 [ 1006, "VENDORS/MISC/CP1006.TXT", "IBM Arabic" ],
67 [ 1026, "VENDORS/MICSFT/EBCDIC/CP1026.TXT", "IBM EBCDIC Latin 5 Turkish" ],
68 [ 1250, "VENDORS/MICSFT/WINDOWS/CP1250.TXT", "ANSI Eastern Europe" ],
69 [ 1251, "VENDORS/MICSFT/WINDOWS/CP1251.TXT", "ANSI Cyrillic" ],
70 [ 1252, "VENDORS/MICSFT/WINDOWS/CP1252.TXT", "ANSI Latin 1" ],
71 [ 1253, "VENDORS/MICSFT/WINDOWS/CP1253.TXT", "ANSI Greek" ],
72 [ 1254, "VENDORS/MICSFT/WINDOWS/CP1254.TXT", "ANSI Turkish" ],
73 [ 1255, "VENDORS/MICSFT/WINDOWS/CP1255.TXT", "ANSI Hebrew" ],
74 [ 1256, "VENDORS/MICSFT/WINDOWS/CP1256.TXT", "ANSI Arabic" ],
75 [ 1257, "VENDORS/MICSFT/WINDOWS/CP1257.TXT", "ANSI Baltic" ],
76 [ 1258, "VENDORS/MICSFT/WINDOWS/CP1258.TXT", "ANSI/OEM Viet Nam" ],
77 [ 10000, "VENDORS/MICSFT/MAC/ROMAN.TXT", "Mac Roman" ],
78 [ 10006, "VENDORS/MICSFT/MAC/GREEK.TXT", "Mac Greek" ],
79 [ 10007, "VENDORS/MICSFT/MAC/CYRILLIC.TXT", "Mac Cyrillic" ],
80 [ 10029, "VENDORS/MICSFT/MAC/LATIN2.TXT", "Mac Latin 2" ],
81 [ 10079, "VENDORS/MICSFT/MAC/ICELAND.TXT", "Mac Icelandic" ],
82 [ 10081, "VENDORS/MICSFT/MAC/TURKISH.TXT", "Mac Turkish" ],
83 [ 20866, "VENDORS/MISC/KOI8-R.TXT", "Russian KOI8" ],
84 [ 20932, "OBSOLETE/EASTASIA/JIS/JIS0208.TXT", "EUC-JP" ],
85 [ 28591, "ISO8859/8859-1.TXT", "ISO 8859-1 Latin 1" ],
86 [ 28592, "ISO8859/8859-2.TXT", "ISO 8859-2 Latin 2 (East European)" ],
87 [ 28593, "ISO8859/8859-3.TXT", "ISO 8859-3 Latin 3 (South European)" ],
88 [ 28594, "ISO8859/8859-4.TXT", "ISO 8859-4 Latin 4 (Baltic old)" ],
89 [ 28595, "ISO8859/8859-5.TXT", "ISO 8859-5 Cyrillic" ],
90 [ 28596, "ISO8859/8859-6.TXT", "ISO 8859-6 Arabic" ],
91 [ 28597, "ISO8859/8859-7.TXT", "ISO 8859-7 Greek" ],
92 [ 28598, "ISO8859/8859-8.TXT", "ISO 8859-8 Hebrew" ],
93 [ 28599, "ISO8859/8859-9.TXT", "ISO 8859-9 Latin 5 (Turkish)" ],
94 [ 28600, "ISO8859/8859-10.TXT", "ISO 8859-10 Latin 6 (Nordic)" ],
95 [ 28603, "ISO8859/8859-13.TXT", "ISO 8859-13 Latin 7 (Baltic)" ],
96 [ 28604, "ISO8859/8859-14.TXT", "ISO 8859-14 Latin 8 (Celtic)" ],
97 [ 28605, "ISO8859/8859-15.TXT", "ISO 8859-15 Latin 9 (Euro)" ],
98 [ 28606, "ISO8859/8859-16.TXT", "ISO 8859-16 Latin 10 (Balkan)" ]
102 %ctype =
104 "upper" => 0x0001,
105 "lower" => 0x0002,
106 "digit" => 0x0004,
107 "space" => 0x0008,
108 "punct" => 0x0010,
109 "cntrl" => 0x0020,
110 "blank" => 0x0040,
111 "xdigit" => 0x0080,
112 "alpha" => 0x0100
115 %categories =
117 "Lu" => $ctype{"alpha"}|$ctype{"upper"}, # Letter, Uppercase
118 "Ll" => $ctype{"alpha"}|$ctype{"lower"}, # Letter, Lowercase
119 "Lt" => $ctype{"alpha"}, # Letter, Titlecase
120 "Mn" => $ctype{"punct"}, # Mark, Non-Spacing
121 "Mc" => $ctype{"punct"}, # Mark, Spacing Combining
122 "Me" => $ctype{"punct"}, # Mark, Enclosing
123 "Nd" => $ctype{"digit"}, # Number, Decimal Digit
124 "Nl" => $ctype{"punct"}, # Number, Letter
125 "No" => $ctype{"punct"}, # Number, Other
126 "Zs" => $ctype{"space"}, # Separator, Space
127 "Zl" => 0, # Separator, Line
128 "Zp" => 0, # Separator, Paragraph
129 "Cc" => $ctype{"cntrl"}, # Other, Control
130 "Cf" => 0, # Other, Format
131 "Cs" => 0, # Other, Surrogate
132 "Co" => 0, # Other, Private Use
133 "Cn" => 0, # Other, Not Assigned
134 "Lm" => $ctype{"punct"}, # Letter, Modifier
135 "Lo" => $ctype{"alpha"}, # Letter, Other
136 "Pc" => $ctype{"punct"}, # Punctuation, Connector
137 "Pd" => $ctype{"punct"}, # Punctuation, Dash
138 "Ps" => $ctype{"punct"}, # Punctuation, Open
139 "Pe" => $ctype{"punct"}, # Punctuation, Close
140 "Pi" => $ctype{"punct"}, # Punctuation, Initial quote
141 "Pf" => $ctype{"punct"}, # Punctuation, Final quote
142 "Po" => $ctype{"punct"}, # Punctuation, Other
143 "Sm" => $ctype{"punct"}, # Symbol, Math
144 "Sc" => $ctype{"punct"}, # Symbol, Currency
145 "Sk" => $ctype{"punct"}, # Symbol, Modifier
146 "So" => $ctype{"punct"} # Symbol, Other
149 # a few characters need additional categories that cannot be determined automatically
150 %special_categories =
152 "xdigit" => [ ord('0')..ord('9'),ord('A')..ord('F'),ord('a')..ord('f'),
153 0xff10..0xff19, 0xff21..0xff26, 0xff41..0xff46 ],
154 "space" => [ 0x09..0x0d, 0xfeff ],
155 "blank" => [ 0x09, 0x20, 0xa0, 0xfeff ]
158 %directions =
160 "L" => 1, # Left-to-Right
161 "LRE" => 11, # Left-to-Right Embedding
162 "LRO" => 11, # Left-to-Right Override
163 "R" => 2, # Right-to-Left
164 "AL" => 2, # Right-to-Left Arabic
165 "RLE" => 11, # Right-to-Left Embedding
166 "RLO" => 11, # Right-to-Left Override
167 "PDF" => 11, # Pop Directional Format
168 "EN" => 3, # European Number
169 "ES" => 4, # European Number Separator
170 "ET" => 5, # European Number Terminator
171 "AN" => 6, # Arabic Number
172 "CS" => 7, # Common Number Separator
173 "NSM" => 0, # Non-Spacing Mark
174 "BN" => 0, # Boundary Neutral
175 "B" => 8, # Paragraph Separator
176 "S" => 9, # Segment Separator
177 "WS" => 10, # Whitespace
178 "ON" => 11 # Other Neutrals
182 ################################################################
183 # main routine
185 READ_DEFAULTS();
186 my @sortkeys = READ_SORTKEYS_FILE();
187 DUMP_CASE_MAPPINGS();
188 DUMP_SORTKEYS(@sortkeys);
189 DUMP_COMPOSE_TABLES();
190 DUMP_CTYPE_TABLES();
192 foreach $file (@allfiles) { HANDLE_FILE( @$file ); }
194 OUTPUT_CPTABLE();
196 exit(0);
199 ################################################################
200 # read in the defaults file
201 sub READ_DEFAULTS
203 @unicode_defaults = ();
204 @unicode_aliases = ();
205 @tolower_table = ();
206 @toupper_table = ();
207 @digitmap_table = ();
208 @compatmap_table = ();
209 @category_table = ();
210 @direction_table = ();
211 @decomp_table = ();
212 @compose_table = ();
214 # first setup a few default mappings
216 open DEFAULTS or die "Cannot open $DEFAULTS";
217 print "Loading $DEFAULTS\n";
218 while (<DEFAULTS>)
220 next if /^\#/; # skip comments
221 next if /^$/; # skip empty lines
222 if (/^(([0-9a-fA-F]+)(,[0-9a-fA-F]+)*)\s+([0-9a-fA-F]+|'.'|none)\s+(\#.*)?/)
224 my @src = map hex, split /,/,$1;
225 my $dst = $4;
226 my $comment = $5;
227 if ($#src > 0) { push @unicode_aliases, \@src; }
228 next if ($dst eq "none");
229 $dst = ($dst =~ /\'.\'/) ? ord substr($dst,1,1) : hex $dst;
230 foreach $src (@src)
232 die "Duplicate value" if defined($unicode_defaults[$src]);
233 $unicode_defaults[$src] = $dst;
235 next;
237 die "Unrecognized line $_\n";
240 # now build mappings from the decomposition field of the Unicode database
242 open UNICODEDATA or die "Cannot open $UNICODEDATA";
243 print "Loading $UNICODEDATA\n";
244 while (<UNICODEDATA>)
246 # Decode the fields ...
247 ($code, $name, $cat, $comb, $bidi,
248 $decomp, $dec, $dig, $num, $mirror,
249 $oldname, $comment, $upper, $lower, $title) = split /;/;
251 my $src = hex $code;
253 die "unknown category $cat" unless defined $categories{$cat};
254 die "unknown directionality $bidi" unless defined $directions{$bidi};
256 $uniname[$src] = $name;
257 $category_table[$src] = $categories{$cat};
258 $direction_table[$src] = $directions{$bidi};
260 if ($lower ne "")
262 $tolower_table[$src] = hex $lower;
263 $category_table[$src] |= $ctype{"upper"}|$ctype{"alpha"};
265 if ($upper ne "")
267 $toupper_table[$src] = hex $upper;
268 $category_table[$src] |= $ctype{"lower"}|$ctype{"alpha"};
270 if ($dec ne "")
272 $category_table[$src] |= $ctype{"digit"};
274 if ($dig ne "")
276 $digitmap_table[$src] = ord $dig;
279 # copy the category and direction for everything between First/Last pairs
280 if ($name =~ /, First>/) { $start = $src; }
281 if ($name =~ /, Last>/)
283 while ($start < $src)
285 $category_table[$start] = $category_table[$src];
286 $direction_table[$start] = $direction_table[$src];
287 $start++;
291 next if $decomp eq ""; # no decomposition, skip it
293 if ($decomp =~ /^<([a-zA-Z]+)>\s+([0-9a-fA-F]+)$/)
295 # decomposition of the form "<foo> 1234" -> use char if type is known
296 if (($src >= 0xf900 && $src < 0xfb00) || ($src >= 0xfe30 && $src < 0xfffd))
298 # Single char decomposition in the compatibility range
299 $compatmap_table[$src] = hex $2;
301 next unless ($1 eq "font" ||
302 $1 eq "noBreak" ||
303 $1 eq "circle" ||
304 $1 eq "super" ||
305 $1 eq "sub" ||
306 $1 eq "wide" ||
307 $1 eq "narrow" ||
308 $1 eq "compat" ||
309 $1 eq "small");
310 $dst = hex $2;
312 elsif ($decomp =~ /^<compat>\s+0020\s+([0-9a-fA-F]+)/)
314 # decomposition "<compat> 0020 1234" -> combining accent
315 $dst = hex $1;
317 elsif ($decomp =~ /^([0-9a-fA-F]+)/)
319 # decomposition contains only char values without prefix -> use first char
320 $dst = hex $1;
321 $category_table[$src] |= $category_table[$dst];
322 # store decomposition if it contains two chars
323 if ($decomp =~ /^([0-9a-fA-F]+)\s+([0-9a-fA-F]+)$/)
325 $decomp_table[$src] = [ hex $1, hex $2 ];
326 push @compose_table, [ hex $1, hex $2, $src ];
328 elsif ($decomp =~ /^(<[a-z]+>\s)*([0-9a-fA-F]+)$/ &&
329 (($src >= 0xf900 && $src < 0xfb00) || ($src >= 0xfe30 && $src < 0xfffd)))
331 # Single char decomposition in the compatibility range
332 $compatmap_table[$src] = hex $2;
335 else
337 next;
340 next if defined($unicode_defaults[$src]); # may have been set in the defaults file
342 # check for loops
343 for ($i = $dst; ; $i = $unicode_defaults[$i])
345 die sprintf("loop detected for %04x -> %04x",$src,$dst) if $i == $src;
346 last unless defined($unicode_defaults[$i]);
348 $unicode_defaults[$src] = $dst;
351 # patch the category of some special characters
353 foreach $cat (keys %special_categories)
355 my $flag = $ctype{$cat};
356 foreach $i (@{$special_categories{$cat}}) { $category_table[$i] |= $flag; }
361 ################################################################
362 # parse the input file
363 sub READ_FILE
365 my $name = shift;
366 open INPUT,$name or die "Cannot open $name";
367 @cp2uni = ();
368 @lead_bytes = ();
369 @uni2cp = ();
371 while (<INPUT>)
373 next if /^\#/; # skip comments
374 next if /^$/; # skip empty lines
375 next if /\x1a/; # skip ^Z
376 next if (/^0x([0-9a-fA-F]+)\s+\#UNDEFINED/); # undefined char
378 if (/^0x([0-9a-fA-F]+)\s+\#DBCS LEAD BYTE/)
380 $cp = hex $1;
381 push @lead_bytes,$cp;
382 $cp2uni[$cp] = 0;
383 next;
385 if (/^0x([0-9a-fA-F]+)\s+0x([0-9a-fA-F]+)\s+(\#.*)?/)
387 $cp = hex $1;
388 $uni = hex $2;
389 $cp2uni[$cp] = $uni unless defined($cp2uni[$cp]);
390 $uni2cp[$uni] = $cp unless defined($uni2cp[$uni]);
391 next;
393 die "$name: Unrecognized line $_\n";
398 ################################################################
399 # build EUC-JP table from the JIS 0208 file
400 # FIXME: for proper EUC-JP we should probably read JIS 0212 too
401 # but this would require 3-byte DBCS characters
402 sub READ_JIS0208_FILE
404 my $name = shift;
405 @cp2uni = ();
406 @lead_bytes = ();
407 @uni2cp = ();
409 # ASCII chars
410 for ($i = 0x00; $i <= 0x7f; $i++)
412 $cp2uni[$i] = $i;
413 $uni2cp[$i] = $i;
416 # JIS X 0201 right plane
417 for ($i = 0xa1; $i <= 0xdf; $i++)
419 $cp2uni[0x8e00 + $i] = 0xfec0 + $i;
420 $uni2cp[0xfec0 + $i] = 0x8e00 + $i;
423 # lead bytes
424 foreach $i (0x8e, 0x8f, 0xa1 .. 0xfe)
426 push @lead_bytes,$i;
427 $cp2uni[$i] = 0;
430 # undefined chars
431 foreach $i (0x80 .. 0x8d, 0x90 .. 0xa0, 0xff)
433 $cp2uni[$i] = $DEF_CHAR;
436 # Shift-JIS compatibility
437 $uni2cp[0x00a5] = 0x5c;
438 $uni2cp[0x203e] = 0x7e;
440 # Fix backslash conversion
441 $cp2uni[0xa1c0] = 0xff3c;
442 $uni2cp[0xff3c] = 0xa1c0;
444 open INPUT, "$name" or die "Cannot open $name";
445 while (<INPUT>)
447 next if /^\#/; # skip comments
448 next if /^$/; # skip empty lines
449 next if /\x1a/; # skip ^Z
450 if (/^0x[0-9a-fA-F]+\s+0x([0-9a-fA-F]+)\s+0x([0-9a-fA-F]+)\s+(\#.*)?/)
452 $cp = 0x8080 + hex $1;
453 $uni = hex $2;
454 $cp2uni[$cp] = $uni unless defined($cp2uni[$cp]);
455 $uni2cp[$uni] = $cp unless defined($uni2cp[$uni]);
456 next;
458 die "$name: Unrecognized line $_\n";
463 ################################################################
464 # build the sort keys table
465 sub READ_SORTKEYS_FILE
467 my @sortkeys = ();
468 for (my $i = 0; $i < 65536; $i++) { $sortkeys[$i] = [ -1, 0, 0, 0, 0 ] };
470 open INPUT, "$SORTKEYS" or die "Cannot open $SORTKEYS";
471 print "Loading $SORTKEYS\n";
472 while (<INPUT>)
474 next if /^\#/; # skip comments
475 next if /^$/; # skip empty lines
476 next if /\x1a/; # skip ^Z
477 next if /^\@version/; # skip @version header
478 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]+)\]/)
480 my ($uni,$variable) = (hex $1, $2);
481 next if $uni > 65535;
482 $sortkeys[$uni] = [ $uni, hex $3, hex $4, hex $5, hex $6 ];
483 next;
485 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]+)\]/)
487 # multiple character sequence, ignored for now
488 next;
490 die "$SORTKEYS: Unrecognized line $_\n";
492 close INPUT;
494 # compress the keys to 32 bit:
495 # key 1 to 16 bits, key 2 to 8 bits, key 3 to 4 bits, key 4 to 1 bit
497 @sortkeys = sort { ${$a}[1] <=> ${$b}[1] or
498 ${$a}[2] <=> ${$b}[2] or
499 ${$a}[3] <=> ${$b}[3] or
500 ${$a}[4] <=> ${$b}[4] or
501 $a cmp $b; } @sortkeys;
503 my ($n2, $n3) = (1, 1);
504 my @keys = (-1, -1, -1, -1, -1 );
505 my @flatkeys = ();
507 for (my $i = 0; $i < 65536; $i++)
509 my @current = @{$sortkeys[$i]};
510 next if $current[0] == -1;
511 if ($current[1] == $keys[1])
513 if ($current[2] == $keys[2])
515 if ($current[3] == $keys[3])
517 # nothing
519 else
521 $keys[3] = $current[3];
522 $n3++;
523 die if ($n3 >= 16);
526 else
528 $keys[2] = $current[2];
529 $keys[3] = $current[3];
530 $n2++;
531 $n3 = 1;
532 die if ($n2 >= 256);
535 else
537 $keys[1] = $current[1];
538 $keys[2] = $current[2];
539 $keys[3] = $current[3];
540 $n2 = 1;
541 $n3 = 1;
544 if ($current[2]) { $current[2] = $n2; }
545 if ($current[3]) { $current[3] = $n3; }
546 if ($current[4]) { $current[4] = 1; }
548 $flatkeys[$current[0]] = ($current[1] << 16) | ($current[2] << 8) | ($current[3] << 4) | $current[4];
550 return @flatkeys;
554 ################################################################
555 # build the sort keys table
556 sub DUMP_SORTKEYS
558 my @keys = @_;
560 # count the number of 256-key ranges that contain something
562 my @offsets = ();
563 my $ranges = 2;
564 for (my $i = 0; $i < 256; $i++) { $offsets[$i] = 256; }
565 for (my $i = 0; $i < 65536; $i++)
567 next unless defined $keys[$i];
568 $offsets[$i >> 8] = $ranges * 256;
569 $ranges++;
570 $i |= 255;
573 # output the range offsets
575 open OUTPUT,">collation.c" or die "Cannot create collation.c";
576 printf "Building collation.c\n";
577 printf OUTPUT "/* Unicode collation element table */\n";
578 printf OUTPUT "/* generated from %s */\n", $SORTKEYS;
579 printf OUTPUT "/* DO NOT EDIT!! */\n\n";
581 printf OUTPUT "const unsigned int collation_table[%d] =\n{\n", $ranges*256;
582 printf OUTPUT " /* index */\n";
583 printf OUTPUT "%s,\n", DUMP_ARRAY( "0x%08x", 0, @offsets );
585 # output the default values
587 printf OUTPUT " /* defaults */\n";
588 printf OUTPUT "%s", DUMP_ARRAY( "0x%08x", 0, (-1) x 256 );
590 # output all the key ranges
592 for (my $i = 0; $i < 256; $i++)
594 next if $offsets[$i] == 256;
595 printf OUTPUT ",\n /* 0x%02x00 .. 0x%02xff */\n", $i, $i;
596 printf OUTPUT "%s", DUMP_ARRAY( "0x%08x", -1, @keys[($i<<8) .. ($i<<8)+255] );
598 printf OUTPUT "\n};\n";
599 close OUTPUT;
603 ################################################################
604 # add default mappings once the file had been read
605 sub ADD_DEFAULT_MAPPINGS
607 # Apply aliases
609 foreach $alias (@unicode_aliases)
611 my $target = undef;
612 foreach $src (@$alias)
614 if (defined($uni2cp[$src]))
616 $target = $uni2cp[$src];
617 last;
620 next unless defined($target);
622 # At least one char of the alias set is defined, set the others to the same value
623 foreach $src (@$alias)
625 $uni2cp[$src] = $target unless defined($uni2cp[$src]);
629 # For every src -> target mapping in the defaults table,
630 # make uni2cp[src] = uni2cp[target] if uni2cp[target] is defined
632 for ($src = 0; $src < 65536; $src++)
634 next if defined($uni2cp[$src]); # source has a definition already
635 next unless defined($unicode_defaults[$src]); # no default for this char
636 my $target = $unicode_defaults[$src];
638 # do a recursive mapping until we find a target char that is defined
639 while (!defined($uni2cp[$target]) &&
640 defined($unicode_defaults[$target])) { $target = $unicode_defaults[$target]; }
642 if (defined($uni2cp[$target])) { $uni2cp[$src] = $uni2cp[$target]; }
645 # Add an identity mapping for all undefined chars
647 for ($i = 0; $i < 256; $i++)
649 next if defined($cp2uni[$i]);
650 next if defined($uni2cp[$i]);
651 $cp2uni[$i] = $uni2cp[$i] = $i;
655 ################################################################
656 # dump an array of integers
657 sub DUMP_ARRAY
659 my ($format,$default,@array) = @_;
660 my $i, $ret = " ";
661 for ($i = 0; $i < $#array; $i++)
663 $ret .= sprintf($format, defined $array[$i] ? $array[$i] : $default);
664 $ret .= (($i % 8) != 7) ? ", " : ",\n ";
666 $ret .= sprintf($format, defined $array[$i] ? $array[$i] : $default);
667 return $ret;
670 ################################################################
671 # dump an SBCS mapping table
672 sub DUMP_SBCS_TABLE
674 my ($codepage, $name) = @_;
675 my $i;
677 # output the ascii->unicode table
679 printf OUTPUT "static const WCHAR cp2uni[256] =\n";
680 printf OUTPUT "{\n%s\n};\n\n", DUMP_ARRAY( "0x%04x", $DEF_CHAR, @cp2uni[0 .. 255] );
682 # count the number of unicode->ascii subtables that contain something
684 my @filled = ();
685 my $subtables = 1;
686 for ($i = 0; $i < 65536; $i++)
688 next unless defined $uni2cp[$i];
689 $filled[$i >> 8] = 1;
690 $subtables++;
691 $i |= 255;
694 # output all the subtables into a single array
696 printf OUTPUT "static const unsigned char uni2cp_low[%d] =\n{\n", $subtables*256;
697 for ($i = 0; $i < 256; $i++)
699 next unless $filled[$i];
700 printf OUTPUT " /* 0x%02x00 .. 0x%02xff */\n", $i, $i;
701 printf OUTPUT "%s,\n", DUMP_ARRAY( "0x%02x", $DEF_CHAR, @uni2cp[($i<<8) .. ($i<<8)+255] );
703 printf OUTPUT " /* defaults */\n";
704 printf OUTPUT "%s\n};\n\n", DUMP_ARRAY( "0x%02x", 0, ($DEF_CHAR) x 256 );
706 # output a table of the offsets of the subtables in the previous array
708 my $pos = 0;
709 my @offsets = ();
710 for ($i = 0; $i < 256; $i++)
712 if ($filled[$i]) { push @offsets, $pos; $pos += 256; }
713 else { push @offsets, ($subtables-1) * 256; }
715 printf OUTPUT "static const unsigned short uni2cp_high[256] =\n";
716 printf OUTPUT "{\n%s\n};\n\n", DUMP_ARRAY( "0x%04x", 0, @offsets );
718 # output the code page descriptor
720 printf OUTPUT "const struct sbcs_table cptable_%03d =\n{\n", $codepage;
721 printf OUTPUT " { %d, 1, 0x%04x, 0x%04x, \"%s\" },\n",
722 $codepage, $DEF_CHAR, $DEF_CHAR, $name;
723 printf OUTPUT " cp2uni,\n";
724 printf OUTPUT " uni2cp_low,\n";
725 printf OUTPUT " uni2cp_high\n};\n";
729 ################################################################
730 # dump a DBCS mapping table
731 sub DUMP_DBCS_TABLE
733 my ($codepage, $name) = @_;
734 my $i, $x, $y;
736 # build a list of lead bytes that are actually used
738 my @lblist = ();
739 LBLOOP: for ($y = 0; $y <= $#lead_bytes; $y++)
741 my $base = $lead_bytes[$y] << 8;
742 for ($x = 0; $x < 256; $x++)
744 if (defined $cp2uni[$base+$x])
746 push @lblist,$lead_bytes[$y];
747 next LBLOOP;
751 my $unused = ($#lead_bytes > $#lblist);
753 # output the ascii->unicode table for the single byte chars
755 printf OUTPUT "static const WCHAR cp2uni[%d] =\n", 256 * ($#lblist + 2 + $unused);
756 printf OUTPUT "{\n%s,\n", DUMP_ARRAY( "0x%04x", $DEF_CHAR, @cp2uni[0 .. 255] );
758 # output the default table for unused lead bytes
760 if ($unused)
762 printf OUTPUT " /* unused lead bytes */\n";
763 printf OUTPUT "%s,\n", DUMP_ARRAY( "0x%04x", 0, ($DEF_CHAR) x 256 );
766 # output the ascii->unicode table for each DBCS lead byte
768 for ($y = 0; $y <= $#lblist; $y++)
770 my $base = $lblist[$y] << 8;
771 printf OUTPUT " /* lead byte %02x */\n", $lblist[$y];
772 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", $DEF_CHAR, @cp2uni[$base .. $base+255] );
773 printf OUTPUT ($y < $#lblist) ? ",\n" : "\n};\n\n";
776 # output the lead byte subtables offsets
778 my @offsets = ();
779 for ($x = 0; $x < 256; $x++) { $offsets[$x] = 0; }
780 for ($x = 0; $x <= $#lblist; $x++) { $offsets[$lblist[$x]] = $x + 1; }
781 if ($unused)
783 # increment all lead bytes offset to take into account the unused table
784 for ($x = 0; $x <= $#lead_bytes; $x++) { $offsets[$lead_bytes[$x]]++; }
786 printf OUTPUT "static const unsigned char cp2uni_leadbytes[256] =\n";
787 printf OUTPUT "{\n%s\n};\n\n", DUMP_ARRAY( "0x%02x", 0, @offsets );
789 # count the number of unicode->ascii subtables that contain something
791 my @filled = ();
792 my $subtables = 1;
793 for ($i = 0; $i < 65536; $i++)
795 next unless defined $uni2cp[$i];
796 $filled[$i >> 8] = 1;
797 $subtables++;
798 $i |= 255;
801 # output all the subtables into a single array
803 printf OUTPUT "static const unsigned short uni2cp_low[%d] =\n{\n", $subtables*256;
804 for ($y = 0; $y < 256; $y++)
806 next unless $filled[$y];
807 printf OUTPUT " /* 0x%02x00 .. 0x%02xff */\n", $y, $y;
808 printf OUTPUT "%s,\n", DUMP_ARRAY( "0x%04x", $DEF_CHAR, @uni2cp[($y<<8) .. ($y<<8)+255] );
810 printf OUTPUT " /* defaults */\n";
811 printf OUTPUT "%s\n};\n\n", DUMP_ARRAY( "0x%04x", 0, ($DEF_CHAR) x 256 );
813 # output a table of the offsets of the subtables in the previous array
815 my $pos = 0;
816 my @offsets = ();
817 for ($y = 0; $y < 256; $y++)
819 if ($filled[$y]) { push @offsets, $pos; $pos += 256; }
820 else { push @offsets, ($subtables-1) * 256; }
822 printf OUTPUT "static const unsigned short uni2cp_high[256] =\n";
823 printf OUTPUT "{\n%s\n};\n\n", DUMP_ARRAY( "0x%04x", 0, @offsets );
825 # output the code page descriptor
827 printf OUTPUT "const struct dbcs_table cptable_%03d =\n{\n", $codepage;
828 printf OUTPUT " { %d, 2, 0x%04x, 0x%04x, \"%s\" },\n",
829 $codepage, $DEF_CHAR, $DEF_CHAR, $name;
830 printf OUTPUT " cp2uni,\n";
831 printf OUTPUT " cp2uni_leadbytes,\n";
832 printf OUTPUT " uni2cp_low,\n";
833 printf OUTPUT " uni2cp_high,\n";
834 DUMP_LB_RANGES();
835 printf OUTPUT "};\n";
839 ################################################################
840 # dump the list of defined lead byte ranges
841 sub DUMP_LB_RANGES
843 my @list = ();
844 my $i = 0;
845 foreach $i (@lead_bytes) { $list[$i] = 1; }
846 my $on = 0;
847 printf OUTPUT " { ";
848 for ($i = 0; $i < 256; $i++)
850 if ($on)
852 if (!defined $list[$i]) { printf OUTPUT "0x%02x, ", $i-1; $on = 0; }
854 else
856 if ($list[$i]) { printf OUTPUT "0x%02x, ", $i; $on = 1; }
859 if ($on) { printf OUTPUT "0xff, "; }
860 printf OUTPUT "0x00, 0x00 }\n";
864 ################################################################
865 # dump the case mapping tables
866 sub DUMP_CASE_MAPPINGS
868 open OUTPUT,">casemap.c" or die "Cannot create casemap.c";
869 printf "Building casemap.c\n";
870 printf OUTPUT "/* Unicode case mappings */\n";
871 printf OUTPUT "/* Automatically generated; DO NOT EDIT!! */\n\n";
872 printf OUTPUT "#include \"wine/unicode.h\"\n\n";
874 DUMP_CASE_TABLE( "wine_casemap_lower", @tolower_table );
875 DUMP_CASE_TABLE( "wine_casemap_upper", @toupper_table );
876 DUMP_CASE_TABLE( "wine_digitmap", @digitmap_table );
877 DUMP_CASE_TABLE( "wine_compatmap", @compatmap_table );
878 close OUTPUT;
882 ################################################################
883 # dump a case mapping table
884 sub DUMP_CASE_TABLE
886 my ($name,@table) = @_;
888 # count the number of sub tables that contain something
889 # also compute the low and upper populated bounds
891 my @lowerbounds = ( 0, 0 );
892 my @upperbounds = ( 0, 255 );
893 my $index = 0;
894 my @filled = ();
895 for ($i = 0; $i < 65536; $i++)
897 next unless defined $table[$i];
898 if (!defined $filled[$i >> 8])
900 $lowerbounds[$index] = $i & 0xff;
901 $upperbounds[$index] = 0xff - $lowerbounds[$index];
902 $filled[$i >> 8] = $index * 256 + 512;
903 $index++;
905 else
907 $upperbounds[$index-1] = 0xff - ($i & 0xff);
909 $table[$i] = ($table[$i] - $i) & 0xffff;
912 # Collapse blocks upwards if possible
913 my $removed = 0;
914 $index = 0;
915 for ($i = 0; $i < 256; $i++)
917 next unless defined $filled[$i];
918 if ($upperbounds[$index - 1] > $lowerbounds[$index])
920 $removed = $removed + $lowerbounds[$index];
922 else
924 $removed = $removed + $upperbounds[$index - 1];
925 $lowerbounds[$index] = $upperbounds[$index - 1];
927 $filled[$i] = $filled[$i] - $removed;
928 $index++;
931 # dump the table
933 printf OUTPUT "const WCHAR %s[%d] =\n", $name, $index * 256 + 512 - $removed;
934 printf OUTPUT "{\n /* index */\n";
935 printf OUTPUT "%s,\n", DUMP_ARRAY( "0x%04x", 256, @filled );
936 printf OUTPUT " /* defaults */\n";
937 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, (0) x 256 );
938 $index = 0;
939 for ($i = 0; $i < 256; $i++)
941 next unless $filled[$i];
942 printf OUTPUT ",\n /* 0x%02x%02x .. 0x%02xff */\n", $i, $lowerbounds[$index], $i;
943 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0,
944 @table[($i<<8) + $lowerbounds[$index] .. ($i<<8)+255] );
945 $index++;
947 printf OUTPUT "\n};\n";
951 ################################################################
952 # dump the ctype tables
953 sub DUMP_CTYPE_TABLES
955 open OUTPUT,">wctype.c" or die "Cannot create wctype.c";
956 printf "Building wctype.c\n";
957 printf OUTPUT "/* Unicode ctype tables */\n";
958 printf OUTPUT "/* Automatically generated; DO NOT EDIT!! */\n\n";
959 printf OUTPUT "#include \"wine/unicode.h\"\n\n";
961 my $i;
962 my @array = (0) x 256;
964 # add the direction in the high 4 bits of the category
965 for ($i = 0; $i < 65536; $i++)
967 $category_table[$i] |= $direction_table[$i] << 12;
970 # try to merge table rows
971 for ($row = 0; $row < 256; $row++)
973 my $rowtxt = sprintf "%04x" x 256, @category_table[($row<<8)..($row<<8)+255];
974 if (defined($sequences{$rowtxt}))
976 # reuse an existing row
977 $array[$row] = $sequences{$rowtxt};
979 else
981 # create a new row
982 $sequences{$rowtxt} = $array[$row] = $#array + 1;
983 push @array, @category_table[($row<<8)..($row<<8)+255];
987 printf OUTPUT "const unsigned short wine_wctype_table[%d] =\n{\n", $#array+1;
988 printf OUTPUT " /* offsets */\n%s,\n", DUMP_ARRAY( "0x%04x", 0, @array[0..255] );
989 printf OUTPUT " /* values */\n%s\n};\n", DUMP_ARRAY( "0x%04x", 0, @array[256..$#array] );
991 close OUTPUT;
995 ################################################################
996 # dump the char composition tables
997 sub DUMP_COMPOSE_TABLES
999 open OUTPUT,">compose.c" or die "Cannot create compose.c";
1000 printf "Building compose.c\n";
1001 printf OUTPUT "/* Unicode char composition */\n";
1002 printf OUTPUT "/* Automatically generated; DO NOT EDIT!! */\n\n";
1003 printf OUTPUT "#include \"wine/unicode.h\"\n\n";
1005 ######### composition table
1007 my @filled = ();
1008 foreach $i (@compose_table)
1010 my @comp = @$i;
1011 push @{$filled[$comp[1]]}, [ $comp[0], $comp[2] ];
1014 # count how many different second chars we have
1016 for ($i = $count = 0; $i < 65536; $i++)
1018 next unless defined $filled[$i];
1019 $count++;
1022 # build the table of second chars and offsets
1024 my $pos = $count + 1;
1025 for ($i = 0; $i < 65536; $i++)
1027 next unless defined $filled[$i];
1028 push @table, $i, $pos;
1029 $pos += @{$filled[$i]};
1031 # terminator with last position
1032 push @table, 0, $pos;
1033 printf OUTPUT "const WCHAR unicode_compose_table[0x%x] =\n{\n", 2*$pos;
1034 printf OUTPUT " /* second chars + offsets */\n%s", DUMP_ARRAY( "0x%04x", 0, @table );
1036 # build the table of first chars and mappings
1038 for ($i = 0; $i < 65536; $i++)
1040 next unless defined $filled[$i];
1041 my @table = ();
1042 my @list = sort { $a->[0] <=> $b->[0] } @{$filled[$i]};
1043 for ($j = 0; $j <= $#list; $j++)
1045 push @table, $list[$j][0], $list[$j][1];
1047 printf OUTPUT ",\n /* 0x%04x */\n%s", $i, DUMP_ARRAY( "0x%04x", 0, @table );
1049 printf OUTPUT "\n};\n\nconst unsigned int unicode_compose_table_size = %d;\n\n", $count;
1051 ######### decomposition table
1053 # first determine all the 16-char subsets that contain something
1055 my @filled = (0) x 4096;
1056 my $pos = 16*2; # for the null subset
1057 for ($i = 0; $i < 65536; $i++)
1059 next unless defined $decomp_table[$i];
1060 $filled[$i >> 4] = $pos;
1061 $pos += 16*2;
1062 $i |= 15;
1064 my $total = $pos;
1066 # now count the 256-char subsets that contain something
1068 my @filled_idx = (256) x 256;
1069 $pos = 256 + 16;
1070 for ($i = 0; $i < 4096; $i++)
1072 next unless $filled[$i];
1073 $filled_idx[$i >> 4] = $pos;
1074 $pos += 16;
1075 $i |= 15;
1077 my $null_offset = $pos; # null mapping
1078 $total += $pos;
1080 # add the index offsets to the subsets positions
1082 for ($i = 0; $i < 4096; $i++)
1084 next unless $filled[$i];
1085 $filled[$i] += $null_offset;
1088 # dump the main index
1090 printf OUTPUT "const WCHAR unicode_decompose_table[%d] =\n", $total;
1091 printf OUTPUT "{\n /* index */\n";
1092 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, @filled_idx );
1093 printf OUTPUT ",\n /* null sub-index */\n%s", DUMP_ARRAY( "0x%04x", 0, ($null_offset) x 16 );
1095 # dump the second-level indexes
1097 for ($i = 0; $i < 256; $i++)
1099 next unless ($filled_idx[$i] > 256);
1100 my @table = @filled[($i<<4)..($i<<4)+15];
1101 for ($j = 0; $j < 16; $j++) { $table[$j] ||= $null_offset; }
1102 printf OUTPUT ",\n /* sub-index %02x */\n", $i;
1103 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, @table );
1106 # dump the 16-char subsets
1108 printf OUTPUT ",\n /* null mapping */\n";
1109 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, (0) x 32 );
1111 for ($i = 0; $i < 4096; $i++)
1113 next unless $filled[$i];
1114 my @table = (0) x 32;
1115 for ($j = 0; $j < 16; $j++)
1117 if (defined $decomp_table[($i<<4) + $j])
1119 $table[2 * $j] = ${$decomp_table[($i << 4) + $j]}[0];
1120 $table[2 * $j + 1] = ${$decomp_table[($i << 4) + $j]}[1];
1123 printf OUTPUT ",\n /* 0x%03x0 .. 0x%03xf */\n", $i, $i;
1124 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, @table );
1127 printf OUTPUT "\n};\n";
1128 close OUTPUT;
1132 ################################################################
1133 # read an input file and generate the corresponding .c file
1134 sub HANDLE_FILE
1136 my ($codepage,$filename,$comment) = @_;
1138 # symbol codepage file is special
1139 if ($codepage == 20932) { READ_JIS0208_FILE($MAPPREFIX . $filename); }
1140 else { READ_FILE($MAPPREFIX . $filename); }
1142 # hack: 0x00a5 must map to backslash in Shift-JIS
1143 if ($codepage == 932) { $uni2cp[0x00a5] = 0x5c; }
1145 ADD_DEFAULT_MAPPINGS();
1147 my $output = sprintf "c_%03d.c", $codepage;
1148 open OUTPUT,">$output" or die "Cannot create $output";
1150 printf "Building %s from %s (%s)\n", $output, $filename, $comment;
1152 # dump all tables
1154 printf OUTPUT "/* code page %03d (%s) */\n", $codepage, $comment;
1155 printf OUTPUT "/* generated from %s */\n", $MAPPREFIX . $filename;
1156 printf OUTPUT "/* DO NOT EDIT!! */\n\n";
1157 printf OUTPUT "#include \"wine/unicode.h\"\n\n";
1159 if ($#lead_bytes == -1) { DUMP_SBCS_TABLE( $codepage, $comment ); }
1160 else { DUMP_DBCS_TABLE( $codepage, $comment ); }
1161 close OUTPUT;
1165 ################################################################
1166 # output the list of codepage tables into the cptable.c file
1167 sub OUTPUT_CPTABLE
1169 @tables_decl = ();
1171 foreach $file (@allfiles)
1173 my ($codepage,$filename,$comment) = @$file;
1174 push @tables_decl, sprintf("extern union cptable cptable_%03d;\n",$codepage);
1177 push @tables_decl, sprintf("\nstatic const union cptable * const cptables[%d] =\n{\n",$#allfiles+1);
1178 foreach $file (@allfiles)
1180 my ($codepage,$filename,$comment) = @$file;
1181 push @tables_decl, sprintf(" &cptable_%03d,\n", $codepage);
1183 push @tables_decl, "};";
1184 REPLACE_IN_FILE( "cptable.c", @tables_decl );
1187 ################################################################
1188 # replace the contents of a file between ### cpmap ### marks
1190 sub REPLACE_IN_FILE
1192 my $name = shift;
1193 my @data = @_;
1194 my @lines = ();
1195 open(FILE,$name) or die "Can't open $name";
1196 while (<FILE>)
1198 push @lines, $_;
1199 last if /\#\#\# cpmap begin \#\#\#/;
1201 push @lines, @data;
1202 while (<FILE>)
1204 if (/\#\#\# cpmap end \#\#\#/) { push @lines, "\n", $_; last; }
1206 push @lines, <FILE>;
1207 open(FILE,">$name") or die "Can't modify $name";
1208 print FILE @lines;
1209 close(FILE);