Removed some unused types.
[wine.git] / unicode / cpmap.pl
blob9582623af7a462dc287e211cf6282f8c38c89edb
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 # Defaults mapping
30 $DEFAULTS = "./defaults";
32 # Default char for undefined mappings
33 $DEF_CHAR = ord '?';
35 @allfiles =
37 [ 37, "VENDORS/MICSFT/EBCDIC/CP037.TXT", "IBM EBCDIC US Canada" ],
38 [ 42, "VENDORS/ADOBE/symbol.txt", "Symbol" ],
39 [ 424, "VENDORS/MISC/CP424.TXT", "IBM EBCDIC Hebrew" ],
40 [ 437, "VENDORS/MICSFT/PC/CP437.TXT", "OEM United States" ],
41 [ 500, "VENDORS/MICSFT/EBCDIC/CP500.TXT", "IBM EBCDIC International" ],
42 [ 737, "VENDORS/MICSFT/PC/CP737.TXT", "OEM Greek 437G" ],
43 [ 775, "VENDORS/MICSFT/PC/CP775.TXT", "OEM Baltic" ],
44 [ 850, "VENDORS/MICSFT/PC/CP850.TXT", "OEM Multilingual Latin 1" ],
45 [ 852, "VENDORS/MICSFT/PC/CP852.TXT", "OEM Slovak Latin 2" ],
46 [ 855, "VENDORS/MICSFT/PC/CP855.TXT", "OEM Cyrillic" ],
47 [ 856, "VENDORS/MISC/CP856.TXT", "Hebrew PC" ],
48 [ 857, "VENDORS/MICSFT/PC/CP857.TXT", "OEM Turkish" ],
49 [ 860, "VENDORS/MICSFT/PC/CP860.TXT", "OEM Portuguese" ],
50 [ 861, "VENDORS/MICSFT/PC/CP861.TXT", "OEM Icelandic" ],
51 [ 862, "VENDORS/MICSFT/PC/CP862.TXT", "OEM Hebrew" ],
52 [ 863, "VENDORS/MICSFT/PC/CP863.TXT", "OEM Canadian French" ],
53 [ 864, "VENDORS/MICSFT/PC/CP864.TXT", "OEM Arabic" ],
54 [ 865, "VENDORS/MICSFT/PC/CP865.TXT", "OEM Nordic" ],
55 [ 866, "VENDORS/MICSFT/PC/CP866.TXT", "OEM Russian" ],
56 [ 869, "VENDORS/MICSFT/PC/CP869.TXT", "OEM Greek" ],
57 [ 874, "VENDORS/MICSFT/PC/CP874.TXT", "ANSI/OEM Thai" ],
58 [ 875, "VENDORS/MICSFT/EBCDIC/CP875.TXT", "IBM EBCDIC Greek" ],
59 [ 878, "VENDORS/MISC/KOI8-R.TXT", "Russian KOI8" ],
60 [ 932, "VENDORS/MICSFT/WINDOWS/CP932.TXT", "ANSI/OEM Japanese Shift-JIS" ],
61 [ 936, "VENDORS/MICSFT/WINDOWS/CP936.TXT", "ANSI/OEM Simplified Chinese GBK" ],
62 [ 949, "VENDORS/MICSFT/WINDOWS/CP949.TXT", "ANSI/OEM Korean Unified Hangul" ],
63 [ 950, "VENDORS/MICSFT/WINDOWS/CP950.TXT", "ANSI/OEM Traditional Chinese Big5" ],
64 [ 1006, "VENDORS/MISC/CP1006.TXT", "IBM Arabic" ],
65 [ 1026, "VENDORS/MICSFT/EBCDIC/CP1026.TXT", "IBM EBCDIC Latin 5 Turkish" ],
66 [ 1250, "VENDORS/MICSFT/WINDOWS/CP1250.TXT", "ANSI Eastern Europe" ],
67 [ 1251, "VENDORS/MICSFT/WINDOWS/CP1251.TXT", "ANSI Cyrillic" ],
68 [ 1252, "VENDORS/MICSFT/WINDOWS/CP1252.TXT", "ANSI Latin 1" ],
69 [ 1253, "VENDORS/MICSFT/WINDOWS/CP1253.TXT", "ANSI Greek" ],
70 [ 1254, "VENDORS/MICSFT/WINDOWS/CP1254.TXT", "ANSI Turkish" ],
71 [ 1255, "VENDORS/MICSFT/WINDOWS/CP1255.TXT", "ANSI Hebrew" ],
72 [ 1256, "VENDORS/MICSFT/WINDOWS/CP1256.TXT", "ANSI Arabic" ],
73 [ 1257, "VENDORS/MICSFT/WINDOWS/CP1257.TXT", "ANSI Baltic" ],
74 [ 1258, "VENDORS/MICSFT/WINDOWS/CP1258.TXT", "ANSI/OEM Viet Nam" ],
75 [ 10000, "VENDORS/MICSFT/MAC/ROMAN.TXT", "Mac Roman" ],
76 [ 10006, "VENDORS/MICSFT/MAC/GREEK.TXT", "Mac Greek" ],
77 [ 10007, "VENDORS/MICSFT/MAC/CYRILLIC.TXT", "Mac Cyrillic" ],
78 [ 10029, "VENDORS/MICSFT/MAC/LATIN2.TXT", "Mac Latin 2" ],
79 [ 10079, "VENDORS/MICSFT/MAC/ICELAND.TXT", "Mac Icelandic" ],
80 [ 10081, "VENDORS/MICSFT/MAC/TURKISH.TXT", "Mac Turkish" ],
81 [ 20866, "VENDORS/MISC/KOI8-R.TXT", "Russian KOI8" ],
82 [ 20932, "OBSOLETE/EASTASIA/JIS/JIS0208.TXT", "EUC-JP" ],
83 [ 28591, "ISO8859/8859-1.TXT", "ISO 8859-1 Latin 1" ],
84 [ 28592, "ISO8859/8859-2.TXT", "ISO 8859-2 Latin 2 (East European)" ],
85 [ 28593, "ISO8859/8859-3.TXT", "ISO 8859-3 Latin 3 (South European)" ],
86 [ 28594, "ISO8859/8859-4.TXT", "ISO 8859-4 Latin 4 (Baltic old)" ],
87 [ 28595, "ISO8859/8859-5.TXT", "ISO 8859-5 Cyrillic" ],
88 [ 28596, "ISO8859/8859-6.TXT", "ISO 8859-6 Arabic" ],
89 [ 28597, "ISO8859/8859-7.TXT", "ISO 8859-7 Greek" ],
90 [ 28598, "ISO8859/8859-8.TXT", "ISO 8859-8 Hebrew" ],
91 [ 28599, "ISO8859/8859-9.TXT", "ISO 8859-9 Latin 5 (Turkish)" ],
92 [ 28600, "ISO8859/8859-10.TXT", "ISO 8859-10 Latin 6 (Nordic)" ],
93 [ 28603, "ISO8859/8859-13.TXT", "ISO 8859-13 Latin 7 (Baltic)" ],
94 [ 28604, "ISO8859/8859-14.TXT", "ISO 8859-14 Latin 8 (Celtic)" ],
95 [ 28605, "ISO8859/8859-15.TXT", "ISO 8859-15 Latin 9 (Euro)" ],
96 [ 28606, "ISO8859/8859-16.TXT", "ISO 8859-16 Latin 10 (Balkan)" ]
100 %ctype =
102 "upper" => 0x0001,
103 "lower" => 0x0002,
104 "digit" => 0x0004,
105 "space" => 0x0008,
106 "punct" => 0x0010,
107 "cntrl" => 0x0020,
108 "blank" => 0x0040,
109 "xdigit" => 0x0080,
110 "alpha" => 0x0100
113 %categories =
115 "Lu" => $ctype{"alpha"}|$ctype{"upper"}, # Letter, Uppercase
116 "Ll" => $ctype{"alpha"}|$ctype{"lower"}, # Letter, Lowercase
117 "Lt" => $ctype{"alpha"}, # Letter, Titlecase
118 "Mn" => $ctype{"punct"}, # Mark, Non-Spacing
119 "Mc" => $ctype{"punct"}, # Mark, Spacing Combining
120 "Me" => $ctype{"punct"}, # Mark, Enclosing
121 "Nd" => $ctype{"digit"}, # Number, Decimal Digit
122 "Nl" => $ctype{"punct"}, # Number, Letter
123 "No" => $ctype{"punct"}, # Number, Other
124 "Zs" => $ctype{"space"}, # Separator, Space
125 "Zl" => 0, # Separator, Line
126 "Zp" => 0, # Separator, Paragraph
127 "Cc" => $ctype{"cntrl"}, # Other, Control
128 "Cf" => 0, # Other, Format
129 "Cs" => 0, # Other, Surrogate
130 "Co" => 0, # Other, Private Use
131 "Cn" => 0, # Other, Not Assigned
132 "Lm" => $ctype{"punct"}, # Letter, Modifier
133 "Lo" => $ctype{"alpha"}, # Letter, Other
134 "Pc" => $ctype{"punct"}, # Punctuation, Connector
135 "Pd" => $ctype{"punct"}, # Punctuation, Dash
136 "Ps" => $ctype{"punct"}, # Punctuation, Open
137 "Pe" => $ctype{"punct"}, # Punctuation, Close
138 "Pi" => $ctype{"punct"}, # Punctuation, Initial quote
139 "Pf" => $ctype{"punct"}, # Punctuation, Final quote
140 "Po" => $ctype{"punct"}, # Punctuation, Other
141 "Sm" => $ctype{"punct"}, # Symbol, Math
142 "Sc" => $ctype{"punct"}, # Symbol, Currency
143 "Sk" => $ctype{"punct"}, # Symbol, Modifier
144 "So" => $ctype{"punct"} # Symbol, Other
147 # a few characters need additional categories that cannot be determined automatically
148 %special_categories =
150 "xdigit" => [ ord('0')..ord('9'),ord('A')..ord('F'),ord('a')..ord('f'),
151 0xff10..0xff19, 0xff21..0xff26, 0xff41..0xff46 ],
152 "space" => [ 0x09..0x0d, 0xfeff ],
153 "blank" => [ 0x09, 0x20, 0xa0, 0xfeff ]
156 %directions =
158 "L" => 1, # Left-to-Right
159 "LRE" => 11, # Left-to-Right Embedding
160 "LRO" => 11, # Left-to-Right Override
161 "R" => 2, # Right-to-Left
162 "AL" => 2, # Right-to-Left Arabic
163 "RLE" => 11, # Right-to-Left Embedding
164 "RLO" => 11, # Right-to-Left Override
165 "PDF" => 11, # Pop Directional Format
166 "EN" => 3, # European Number
167 "ES" => 4, # European Number Separator
168 "ET" => 5, # European Number Terminator
169 "AN" => 6, # Arabic Number
170 "CS" => 7, # Common Number Separator
171 "NSM" => 0, # Non-Spacing Mark
172 "BN" => 0, # Boundary Neutral
173 "B" => 8, # Paragraph Separator
174 "S" => 9, # Segment Separator
175 "WS" => 10, # Whitespace
176 "ON" => 11 # Other Neutrals
180 ################################################################
181 # main routine
183 READ_DEFAULTS();
184 DUMP_CASE_MAPPINGS();
185 DUMP_COMPOSE_TABLES();
186 DUMP_CTYPE_TABLES();
188 foreach $file (@allfiles) { HANDLE_FILE( @$file ); }
190 OUTPUT_CPTABLE();
192 exit(0);
195 ################################################################
196 # read in the defaults file
197 sub READ_DEFAULTS
199 @unicode_defaults = ();
200 @unicode_aliases = ();
201 @tolower_table = ();
202 @toupper_table = ();
203 @category_table = ();
204 @direction_table = ();
205 @decomp_table = ();
206 @compose_table = ();
208 # first setup a few default mappings
210 open DEFAULTS or die "Cannot open $DEFAULTS";
211 print "Loading $DEFAULTS\n";
212 while (<DEFAULTS>)
214 next if /^\#/; # skip comments
215 next if /^$/; # skip empty lines
216 if (/^(([0-9a-fA-F]+)(,[0-9a-fA-F]+)*)\s+([0-9a-fA-F]+|'.'|none)\s+(\#.*)?/)
218 my @src = map hex, split /,/,$1;
219 my $dst = $4;
220 my $comment = $5;
221 if ($#src > 0) { push @unicode_aliases, \@src; }
222 next if ($dst eq "none");
223 $dst = ($dst =~ /\'.\'/) ? ord substr($dst,1,1) : hex $dst;
224 foreach $src (@src)
226 die "Duplicate value" if defined($unicode_defaults[$src]);
227 $unicode_defaults[$src] = $dst;
229 next;
231 die "Unrecognized line $_\n";
234 # now build mappings from the decomposition field of the Unicode database
236 open UNICODEDATA or die "Cannot open $UNICODEDATA";
237 print "Loading $UNICODEDATA\n";
238 while (<UNICODEDATA>)
240 # Decode the fields ...
241 ($code, $name, $cat, $comb, $bidi,
242 $decomp, $dec, $dig, $num, $mirror,
243 $oldname, $comment, $upper, $lower, $title) = split /;/;
245 my $src = hex $code;
247 die "unknown category $cat" unless defined $categories{$cat};
248 die "unknown directionality $bidi" unless defined $directions{$bidi};
250 $uniname[$src] = $name;
251 $category_table[$src] = $categories{$cat};
252 $direction_table[$src] = $directions{$bidi};
254 if ($lower ne "")
256 $tolower_table[$src] = hex $lower;
257 $category_table[$src] |= $ctype{"upper"}|$ctype{"alpha"};
259 if ($upper ne "")
261 $toupper_table[$src] = hex $upper;
262 $category_table[$src] |= $ctype{"lower"}|$ctype{"alpha"};
264 if ($dec ne "")
266 $category_table[$src] |= $ctype{"digit"};
269 # copy the category and direction for everything between First/Last pairs
270 if ($name =~ /, First>/) { $start = $src; }
271 if ($name =~ /, Last>/)
273 while ($start < $src)
275 $category_table[$start] = $category_table[$src];
276 $direction_table[$start] = $direction_table[$src];
277 $start++;
281 next if $decomp eq ""; # no decomposition, skip it
283 if ($decomp =~ /^<([a-zA-Z]+)>\s+([0-9a-fA-F]+)$/)
285 # decomposition of the form "<foo> 1234" -> use char if type is known
286 next unless ($1 eq "font" ||
287 $1 eq "noBreak" ||
288 $1 eq "circle" ||
289 $1 eq "super" ||
290 $1 eq "sub" ||
291 $1 eq "wide" ||
292 $1 eq "narrow" ||
293 $1 eq "compat" ||
294 $1 eq "small");
295 $dst = hex $2;
297 elsif ($decomp =~ /^<compat>\s+0020\s+([0-9a-fA-F]+)/)
299 # decomposition "<compat> 0020 1234" -> combining accent
300 $dst = hex $1;
302 elsif ($decomp =~ /^([0-9a-fA-F]+)/)
304 # decomposition contains only char values without prefix -> use first char
305 $dst = hex $1;
306 $category_table[$src] |= $category_table[$dst];
307 # store decomposition if it contains two chars
308 if ($decomp =~ /^([0-9a-fA-F]+)\s+([0-9a-fA-F]+)$/)
310 $decomp_table[$src] = [ hex $1, hex $2 ];
311 push @compose_table, [ hex $1, hex $2, $src ];
314 else
316 next;
319 next if defined($unicode_defaults[$src]); # may have been set in the defaults file
321 # check for loops
322 for ($i = $dst; ; $i = $unicode_defaults[$i])
324 die sprintf("loop detected for %04x -> %04x",$src,$dst) if $i == $src;
325 last unless defined($unicode_defaults[$i]);
327 $unicode_defaults[$src] = $dst;
330 # patch the category of some special characters
332 foreach $cat (keys %special_categories)
334 my $flag = $ctype{$cat};
335 foreach $i (@{$special_categories{$cat}}) { $category_table[$i] |= $flag; }
340 ################################################################
341 # parse the input file
342 sub READ_FILE
344 my $name = shift;
345 open INPUT,$name or die "Cannot open $name";
346 @cp2uni = ();
347 @lead_bytes = ();
348 @uni2cp = ();
350 while (<INPUT>)
352 next if /^\#/; # skip comments
353 next if /^$/; # skip empty lines
354 next if /\x1a/; # skip ^Z
355 next if (/^0x([0-9a-fA-F]+)\s+\#UNDEFINED/); # undefined char
357 if (/^0x([0-9a-fA-F]+)\s+\#DBCS LEAD BYTE/)
359 $cp = hex $1;
360 push @lead_bytes,$cp;
361 $cp2uni[$cp] = 0;
362 next;
364 if (/^0x([0-9a-fA-F]+)\s+0x([0-9a-fA-F]+)\s+(\#.*)?/)
366 $cp = hex $1;
367 $uni = hex $2;
368 $cp2uni[$cp] = $uni unless defined($cp2uni[$cp]);
369 $uni2cp[$uni] = $cp unless defined($uni2cp[$uni]);
370 next;
372 die "$name: Unrecognized line $_\n";
377 ################################################################
378 # parse the symbol.txt file, since its syntax is different from the other ones
379 sub READ_SYMBOL_FILE
381 my $name = shift;
382 open INPUT,$name or die "Cannot open $name";
383 @cp2uni = ();
384 @lead_bytes = ();
385 @uni2cp = ();
387 while (<INPUT>)
389 next if /^\#/; # skip comments
390 next if /^$/; # skip empty lines
391 next if /\x1a/; # skip ^Z
392 if (/^([0-9a-fA-F]+)\s+([0-9a-fA-F]+)\s+(\#.*)?/)
394 $uni = hex $1;
395 $cp = hex $2;
396 $cp2uni[$cp] = $uni unless defined($cp2uni[$cp]);
397 $uni2cp[$uni] = $cp unless defined($uni2cp[$uni]);
398 next;
400 die "$name: Unrecognized line $_\n";
405 ################################################################
406 # build EUC-JP table from the JIS 0208 file
407 # FIXME: for proper EUC-JP we should probably read JIS 0212 too
408 # but this would require 3-byte DBCS characters
409 sub READ_JIS0208_FILE
411 my $name = shift;
412 @cp2uni = ();
413 @lead_bytes = ();
414 @uni2cp = ();
416 # ASCII chars
417 for ($i = 0x00; $i <= 0x7f; $i++)
419 $cp2uni[$i] = $i;
420 $uni2cp[$i] = $i;
423 # JIS X 0201 right plane
424 for ($i = 0xa1; $i <= 0xdf; $i++)
426 $cp2uni[0x8e00 + $i] = 0xfec0 + $i;
427 $uni2cp[0xfec0 + $i] = 0x8e00 + $i;
430 # lead bytes
431 foreach $i (0x8e, 0x8f, 0xa1 .. 0xfe)
433 push @lead_bytes,$i;
434 $cp2uni[$i] = 0;
437 # undefined chars
438 foreach $i (0x80 .. 0x8d, 0x90 .. 0xa0, 0xff)
440 $cp2uni[$i] = $DEF_CHAR;
443 # Shift-JIS compatibility
444 $uni2cp[0x00a5] = 0x5c;
445 $uni2cp[0x203e] = 0x7e;
447 # Fix backslash conversion
448 $cp2uni[0xa1c0] = 0xff3c;
449 $uni2cp[0xff3c] = 0xa1c0;
451 open INPUT, "$name" or die "Cannot open $name";
452 while (<INPUT>)
454 next if /^\#/; # skip comments
455 next if /^$/; # skip empty lines
456 next if /\x1a/; # skip ^Z
457 if (/^0x[0-9a-fA-F]+\s+0x([0-9a-fA-F]+)\s+0x([0-9a-fA-F]+)\s+(\#.*)?/)
459 $cp = 0x8080 + hex $1;
460 $uni = hex $2;
461 $cp2uni[$cp] = $uni unless defined($cp2uni[$cp]);
462 $uni2cp[$uni] = $cp unless defined($uni2cp[$uni]);
463 next;
465 die "$name: Unrecognized line $_\n";
470 ################################################################
471 # add default mappings once the file had been read
472 sub ADD_DEFAULT_MAPPINGS
474 # Apply aliases
476 foreach $alias (@unicode_aliases)
478 my $target = undef;
479 foreach $src (@$alias)
481 if (defined($uni2cp[$src]))
483 $target = $uni2cp[$src];
484 last;
487 next unless defined($target);
489 # At least one char of the alias set is defined, set the others to the same value
490 foreach $src (@$alias)
492 $uni2cp[$src] = $target unless defined($uni2cp[$src]);
496 # For every src -> target mapping in the defaults table,
497 # make uni2cp[src] = uni2cp[target] if uni2cp[target] is defined
499 for ($src = 0; $src < 65536; $src++)
501 next if defined($uni2cp[$src]); # source has a definition already
502 next unless defined($unicode_defaults[$src]); # no default for this char
503 my $target = $unicode_defaults[$src];
505 # do a recursive mapping until we find a target char that is defined
506 while (!defined($uni2cp[$target]) &&
507 defined($unicode_defaults[$target])) { $target = $unicode_defaults[$target]; }
509 if (defined($uni2cp[$target])) { $uni2cp[$src] = $uni2cp[$target]; }
512 # Add an identity mapping for all undefined chars
514 for ($i = 0; $i < 256; $i++)
516 next if defined($cp2uni[$i]);
517 next if defined($uni2cp[$i]);
518 $cp2uni[$i] = $uni2cp[$i] = $i;
522 ################################################################
523 # dump an array of integers
524 sub DUMP_ARRAY
526 my ($format,$default,@array) = @_;
527 my $i, $ret = " ";
528 for ($i = 0; $i < $#array; $i++)
530 $ret .= sprintf($format, defined $array[$i] ? $array[$i] : $default);
531 $ret .= (($i % 8) != 7) ? ", " : ",\n ";
533 $ret .= sprintf($format, defined $array[$i] ? $array[$i] : $default);
534 return $ret;
537 ################################################################
538 # dump an SBCS mapping table
539 sub DUMP_SBCS_TABLE
541 my ($codepage, $name) = @_;
542 my $i;
544 # output the ascii->unicode table
546 printf OUTPUT "static const WCHAR cp2uni[256] =\n";
547 printf OUTPUT "{\n%s\n};\n\n", DUMP_ARRAY( "0x%04x", $DEF_CHAR, @cp2uni[0 .. 255] );
549 # count the number of unicode->ascii subtables that contain something
551 my @filled = ();
552 my $subtables = 1;
553 for ($i = 0; $i < 65536; $i++)
555 next unless defined $uni2cp[$i];
556 $filled[$i >> 8] = 1;
557 $subtables++;
558 $i |= 255;
561 # output all the subtables into a single array
563 printf OUTPUT "static const unsigned char uni2cp_low[%d] =\n{\n", $subtables*256;
564 for ($i = 0; $i < 256; $i++)
566 next unless $filled[$i];
567 printf OUTPUT " /* 0x%02x00 .. 0x%02xff */\n", $i, $i;
568 printf OUTPUT "%s,\n", DUMP_ARRAY( "0x%02x", $DEF_CHAR, @uni2cp[($i<<8) .. ($i<<8)+255] );
570 printf OUTPUT " /* defaults */\n";
571 printf OUTPUT "%s\n};\n\n", DUMP_ARRAY( "0x%02x", 0, ($DEF_CHAR) x 256 );
573 # output a table of the offsets of the subtables in the previous array
575 my $pos = 0;
576 my @offsets = ();
577 for ($i = 0; $i < 256; $i++)
579 if ($filled[$i]) { push @offsets, $pos; $pos += 256; }
580 else { push @offsets, ($subtables-1) * 256; }
582 printf OUTPUT "static const unsigned short uni2cp_high[256] =\n";
583 printf OUTPUT "{\n%s\n};\n\n", DUMP_ARRAY( "0x%04x", 0, @offsets );
585 # output the code page descriptor
587 printf OUTPUT "const struct sbcs_table cptable_%03d =\n{\n", $codepage;
588 printf OUTPUT " { %d, 1, 0x%04x, 0x%04x, \"%s\" },\n",
589 $codepage, $DEF_CHAR, $DEF_CHAR, $name;
590 printf OUTPUT " cp2uni,\n";
591 printf OUTPUT " uni2cp_low,\n";
592 printf OUTPUT " uni2cp_high\n};\n";
596 ################################################################
597 # dump a DBCS mapping table
598 sub DUMP_DBCS_TABLE
600 my ($codepage, $name) = @_;
601 my $i, $x, $y;
603 # build a list of lead bytes that are actually used
605 my @lblist = ();
606 LBLOOP: for ($y = 0; $y <= $#lead_bytes; $y++)
608 my $base = $lead_bytes[$y] << 8;
609 for ($x = 0; $x < 256; $x++)
611 if (defined $cp2uni[$base+$x])
613 push @lblist,$lead_bytes[$y];
614 next LBLOOP;
618 my $unused = ($#lead_bytes > $#lblist);
620 # output the ascii->unicode table for the single byte chars
622 printf OUTPUT "static const WCHAR cp2uni[%d] =\n", 256 * ($#lblist + 2 + $unused);
623 printf OUTPUT "{\n%s,\n", DUMP_ARRAY( "0x%04x", $DEF_CHAR, @cp2uni[0 .. 255] );
625 # output the default table for unused lead bytes
627 if ($unused)
629 printf OUTPUT " /* unused lead bytes */\n";
630 printf OUTPUT "%s,\n", DUMP_ARRAY( "0x%04x", 0, ($DEF_CHAR) x 256 );
633 # output the ascii->unicode table for each DBCS lead byte
635 for ($y = 0; $y <= $#lblist; $y++)
637 my $base = $lblist[$y] << 8;
638 printf OUTPUT " /* lead byte %02x */\n", $lblist[$y];
639 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", $DEF_CHAR, @cp2uni[$base .. $base+255] );
640 printf OUTPUT ($y < $#lblist) ? ",\n" : "\n};\n\n";
643 # output the lead byte subtables offsets
645 my @offsets = ();
646 for ($x = 0; $x < 256; $x++) { $offsets[$x] = 0; }
647 for ($x = 0; $x <= $#lblist; $x++) { $offsets[$lblist[$x]] = $x + 1; }
648 if ($unused)
650 # increment all lead bytes offset to take into account the unused table
651 for ($x = 0; $x <= $#lead_bytes; $x++) { $offsets[$lead_bytes[$x]]++; }
653 printf OUTPUT "static const unsigned char cp2uni_leadbytes[256] =\n";
654 printf OUTPUT "{\n%s\n};\n\n", DUMP_ARRAY( "0x%02x", 0, @offsets );
656 # count the number of unicode->ascii subtables that contain something
658 my @filled = ();
659 my $subtables = 1;
660 for ($i = 0; $i < 65536; $i++)
662 next unless defined $uni2cp[$i];
663 $filled[$i >> 8] = 1;
664 $subtables++;
665 $i |= 255;
668 # output all the subtables into a single array
670 printf OUTPUT "static const unsigned short uni2cp_low[%d] =\n{\n", $subtables*256;
671 for ($y = 0; $y < 256; $y++)
673 next unless $filled[$y];
674 printf OUTPUT " /* 0x%02x00 .. 0x%02xff */\n", $y, $y;
675 printf OUTPUT "%s,\n", DUMP_ARRAY( "0x%04x", $DEF_CHAR, @uni2cp[($y<<8) .. ($y<<8)+255] );
677 printf OUTPUT " /* defaults */\n";
678 printf OUTPUT "%s\n};\n\n", DUMP_ARRAY( "0x%04x", 0, ($DEF_CHAR) x 256 );
680 # output a table of the offsets of the subtables in the previous array
682 my $pos = 0;
683 my @offsets = ();
684 for ($y = 0; $y < 256; $y++)
686 if ($filled[$y]) { push @offsets, $pos; $pos += 256; }
687 else { push @offsets, ($subtables-1) * 256; }
689 printf OUTPUT "static const unsigned short uni2cp_high[256] =\n";
690 printf OUTPUT "{\n%s\n};\n\n", DUMP_ARRAY( "0x%04x", 0, @offsets );
692 # output the code page descriptor
694 printf OUTPUT "const struct dbcs_table cptable_%03d =\n{\n", $codepage;
695 printf OUTPUT " { %d, 2, 0x%04x, 0x%04x, \"%s\" },\n",
696 $codepage, $DEF_CHAR, $DEF_CHAR, $name;
697 printf OUTPUT " cp2uni,\n";
698 printf OUTPUT " cp2uni_leadbytes,\n";
699 printf OUTPUT " uni2cp_low,\n";
700 printf OUTPUT " uni2cp_high,\n";
701 DUMP_LB_RANGES();
702 printf OUTPUT "};\n";
706 ################################################################
707 # dump the list of defined lead byte ranges
708 sub DUMP_LB_RANGES
710 my @list = ();
711 my $i = 0;
712 foreach $i (@lead_bytes) { $list[$i] = 1; }
713 my $on = 0;
714 printf OUTPUT " { ";
715 for ($i = 0; $i < 256; $i++)
717 if ($on)
719 if (!defined $list[$i]) { printf OUTPUT "0x%02x, ", $i-1; $on = 0; }
721 else
723 if ($list[$i]) { printf OUTPUT "0x%02x, ", $i; $on = 1; }
726 if ($on) { printf OUTPUT "0xff, "; }
727 printf OUTPUT "0x00, 0x00 }\n";
731 ################################################################
732 # dump the case mapping tables
733 sub DUMP_CASE_MAPPINGS
735 open OUTPUT,">casemap.c" or die "Cannot create casemap.c";
736 printf "Building casemap.c\n";
737 printf OUTPUT "/* Unicode case mappings */\n";
738 printf OUTPUT "/* Automatically generated; DO NOT EDIT!! */\n\n";
739 printf OUTPUT "#include \"wine/unicode.h\"\n\n";
741 DUMP_CASE_TABLE( "casemap_lower", @tolower_table );
742 DUMP_CASE_TABLE( "casemap_upper", @toupper_table );
743 close OUTPUT;
747 ################################################################
748 # dump a case mapping table
749 sub DUMP_CASE_TABLE
751 my ($name,@table) = @_;
753 # count the number of sub tables that contain something
755 my @filled = ();
756 my $pos = 512;
757 for ($i = 0; $i < 65536; $i++)
759 next unless defined $table[$i];
760 $filled[$i >> 8] = $pos;
761 $pos += 256;
762 $i |= 255;
764 for ($i = 0; $i < 65536; $i++)
766 next unless defined $table[$i];
767 $table[$i] = ($table[$i] - $i) & 0xffff;
770 # dump the table
772 printf OUTPUT "const WCHAR %s[%d] =\n", $name, $pos;
773 printf OUTPUT "{\n /* index */\n";
774 printf OUTPUT "%s,\n", DUMP_ARRAY( "0x%04x", 256, @filled );
775 printf OUTPUT " /* defaults */\n";
776 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, (0) x 256 );
777 for ($i = 0; $i < 256; $i++)
779 next unless $filled[$i];
780 printf OUTPUT ",\n /* 0x%02x00 .. 0x%02xff */\n", $i, $i;
781 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, @table[($i<<8) .. ($i<<8)+255] );
783 printf OUTPUT "\n};\n";
787 ################################################################
788 # dump the ctype tables
789 sub DUMP_CTYPE_TABLES
791 open OUTPUT,">wctype.c" or die "Cannot create casemap.c";
792 printf "Building wctype.c\n";
793 printf OUTPUT "/* Unicode ctype tables */\n";
794 printf OUTPUT "/* Automatically generated; DO NOT EDIT!! */\n\n";
795 printf OUTPUT "#include \"wine/unicode.h\"\n\n";
797 my $i;
798 my @array = (0) x 256;
800 # add the direction in the high 4 bits of the category
801 for ($i = 0; $i < 65536; $i++)
803 $category_table[$i] |= $direction_table[$i] << 12;
806 # try to merge table rows
807 for ($row = 0; $row < 256; $row++)
809 my $rowtxt = sprintf "%04x" x 256, @category_table[($row<<8)..($row<<8)+255];
810 if (defined($sequences{$rowtxt}))
812 # reuse an existing row
813 $array[$row] = $sequences{$rowtxt};
815 else
817 # create a new row
818 $sequences{$rowtxt} = $array[$row] = $#array + 1;
819 push @array, @category_table[($row<<8)..($row<<8)+255];
823 printf OUTPUT "const unsigned short wctype_table[%d] =\n{\n", $#array+1;
824 printf OUTPUT " /* offsets */\n%s,\n", DUMP_ARRAY( "0x%04x", 0, @array[0..255] );
825 printf OUTPUT " /* values */\n%s\n};\n", DUMP_ARRAY( "0x%04x", 0, @array[256..$#array] );
827 close OUTPUT;
831 ################################################################
832 # dump the char composition tables
833 sub DUMP_COMPOSE_TABLES
835 open OUTPUT,">compose.c" or die "Cannot create compose.c";
836 printf "Building compose.c\n";
837 printf OUTPUT "/* Unicode char composition */\n";
838 printf OUTPUT "/* Automatically generated; DO NOT EDIT!! */\n\n";
839 printf OUTPUT "#include \"wine/unicode.h\"\n\n";
841 ######### composition table
843 my @filled = ();
844 foreach $i (@compose_table)
846 my @comp = @$i;
847 push @{$filled[$comp[1]]}, [ $comp[0], $comp[2] ];
850 # count how many different second chars we have
852 for ($i = $count = 0; $i < 65536; $i++)
854 next unless defined $filled[$i];
855 $count++;
858 # build the table of second chars and offsets
860 my $pos = $count + 1;
861 for ($i = 0; $i < 65536; $i++)
863 next unless defined $filled[$i];
864 push @table, $i, $pos;
865 $pos += @{$filled[$i]};
867 # terminator with last position
868 push @table, 0, $pos;
869 printf OUTPUT "const WCHAR unicode_compose_table[0x%x] =\n{\n", 2*$pos;
870 printf OUTPUT " /* second chars + offsets */\n%s", DUMP_ARRAY( "0x%04x", 0, @table );
872 # build the table of first chars and mappings
874 for ($i = 0; $i < 65536; $i++)
876 next unless defined $filled[$i];
877 my @table = ();
878 my @list = sort { $a->[0] <=> $b->[0] } @{$filled[$i]};
879 for ($j = 0; $j <= $#list; $j++)
881 push @table, $list[$j][0], $list[$j][1];
883 printf OUTPUT ",\n /* 0x%04x */\n%s", $i, DUMP_ARRAY( "0x%04x", 0, @table );
885 printf OUTPUT "\n};\n\nconst unsigned int unicode_compose_table_size = %d;\n\n", $count;
887 ######### decomposition table
889 # first determine all the 16-char subsets that contain something
891 my @filled = (0) x 4096;
892 my $pos = 16*2; # for the null subset
893 for ($i = 0; $i < 65536; $i++)
895 next unless defined $decomp_table[$i];
896 $filled[$i >> 4] = $pos;
897 $pos += 16*2;
898 $i |= 15;
900 my $total = $pos;
902 # now count the 256-char subsets that contain something
904 my @filled_idx = (256) x 256;
905 $pos = 256 + 16;
906 for ($i = 0; $i < 4096; $i++)
908 next unless $filled[$i];
909 $filled_idx[$i >> 4] = $pos;
910 $pos += 16;
911 $i |= 15;
913 my $null_offset = $pos; # null mapping
914 $total += $pos;
916 # add the index offsets to the subsets positions
918 for ($i = 0; $i < 4096; $i++)
920 next unless $filled[$i];
921 $filled[$i] += $null_offset;
924 # dump the main index
926 printf OUTPUT "const WCHAR unicode_decompose_table[%d] =\n", $total;
927 printf OUTPUT "{\n /* index */\n";
928 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, @filled_idx );
929 printf OUTPUT ",\n /* null sub-index */\n%s", DUMP_ARRAY( "0x%04x", 0, ($null_offset) x 16 );
931 # dump the second-level indexes
933 for ($i = 0; $i < 256; $i++)
935 next unless ($filled_idx[$i] > 256);
936 my @table = @filled[($i<<4)..($i<<4)+15];
937 for ($j = 0; $j < 16; $j++) { $table[$j] ||= $null_offset; }
938 printf OUTPUT ",\n /* sub-index %02x */\n", $i;
939 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, @table );
942 # dump the 16-char subsets
944 printf OUTPUT ",\n /* null mapping */\n";
945 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, (0) x 32 );
947 for ($i = 0; $i < 4096; $i++)
949 next unless $filled[$i];
950 my @table = (0) x 32;
951 for ($j = 0; $j < 16; $j++)
953 if (defined $decomp_table[($i<<4) + $j])
955 $table[2 * $j] = ${$decomp_table[($i << 4) + $j]}[0];
956 $table[2 * $j + 1] = ${$decomp_table[($i << 4) + $j]}[1];
959 printf OUTPUT ",\n /* 0x%03x0 .. 0x%03xf */\n", $i, $i;
960 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, @table );
963 printf OUTPUT "\n};\n";
964 close OUTPUT;
968 ################################################################
969 # read an input file and generate the corresponding .c file
970 sub HANDLE_FILE
972 my ($codepage,$filename,$comment) = @_;
974 # symbol codepage file is special
975 if ($codepage == 42) { READ_SYMBOL_FILE($MAPPREFIX . $filename); }
976 elsif ($codepage == 20932) { READ_JIS0208_FILE($MAPPREFIX . $filename); }
977 else { READ_FILE($MAPPREFIX . $filename); }
979 ADD_DEFAULT_MAPPINGS();
981 my $output = sprintf "c_%03d.c", $codepage;
982 open OUTPUT,">$output" or die "Cannot create $output";
984 printf "Building %s from %s (%s)\n", $output, $filename, $comment;
986 # dump all tables
988 printf OUTPUT "/* code page %03d (%s) */\n", $codepage, $comment;
989 printf OUTPUT "/* generated from %s */\n", $MAPPREFIX . $filename;
990 printf OUTPUT "/* DO NOT EDIT!! */\n\n";
991 printf OUTPUT "#include \"wine/unicode.h\"\n\n";
993 if ($#lead_bytes == -1) { DUMP_SBCS_TABLE( $codepage, $comment ); }
994 else { DUMP_DBCS_TABLE( $codepage, $comment ); }
995 close OUTPUT;
999 ################################################################
1000 # output the list of codepage tables into the cptable.c file
1001 sub OUTPUT_CPTABLE
1003 @tables_decl = ();
1005 foreach $file (@allfiles)
1007 my ($codepage,$filename,$comment) = @$file;
1008 push @tables_decl, sprintf("extern union cptable cptable_%03d;\n",$codepage);
1011 push @tables_decl, sprintf("\nstatic const union cptable * const cptables[%d] =\n{\n",$#allfiles+1);
1012 foreach $file (@allfiles)
1014 my ($codepage,$filename,$comment) = @$file;
1015 push @tables_decl, sprintf(" &cptable_%03d,\n", $codepage);
1017 push @tables_decl, "};";
1018 REPLACE_IN_FILE( "cptable.c", @tables_decl );
1021 ################################################################
1022 # replace the contents of a file between ### cpmap ### marks
1024 sub REPLACE_IN_FILE
1026 my $name = shift;
1027 my @data = @_;
1028 my @lines = ();
1029 open(FILE,$name) or die "Can't open $name";
1030 while (<FILE>)
1032 push @lines, $_;
1033 last if /\#\#\# cpmap begin \#\#\#/;
1035 push @lines, @data;
1036 while (<FILE>)
1038 if (/\#\#\# cpmap end \#\#\#/) { push @lines, "\n", $_; last; }
1040 push @lines, <FILE>;
1041 open(FILE,">$name") or die "Can't modify $name";
1042 print FILE @lines;
1043 close(FILE);