'used' should be set even if we are only computing the necessary
[wine/testsucceed.git] / unicode / cpmap.pl
blob83d4df46fa32380308bfe0017f7d37708cacbb46
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 [ 28591, "ISO8859/8859-1.TXT", "ISO 8859-1 Latin 1" ],
83 [ 28592, "ISO8859/8859-2.TXT", "ISO 8859-2 Latin 2 (East European)" ],
84 [ 28593, "ISO8859/8859-3.TXT", "ISO 8859-3 Latin 3 (South European)" ],
85 [ 28594, "ISO8859/8859-4.TXT", "ISO 8859-4 Latin 4 (Baltic old)" ],
86 [ 28595, "ISO8859/8859-5.TXT", "ISO 8859-5 Cyrillic" ],
87 [ 28596, "ISO8859/8859-6.TXT", "ISO 8859-6 Arabic" ],
88 [ 28597, "ISO8859/8859-7.TXT", "ISO 8859-7 Greek" ],
89 [ 28598, "ISO8859/8859-8.TXT", "ISO 8859-8 Hebrew" ],
90 [ 28599, "ISO8859/8859-9.TXT", "ISO 8859-9 Latin 5 (Turkish)" ],
91 [ 28600, "ISO8859/8859-10.TXT", "ISO 8859-10 Latin 6 (Nordic)" ],
92 [ 28603, "ISO8859/8859-13.TXT", "ISO 8859-13 Latin 7 (Baltic)" ],
93 [ 28604, "ISO8859/8859-14.TXT", "ISO 8859-14 Latin 8 (Celtic)" ],
94 [ 28605, "ISO8859/8859-15.TXT", "ISO 8859-15 Latin 9 (Euro)" ]
98 %ctype =
100 "upper" => 0x0001,
101 "lower" => 0x0002,
102 "digit" => 0x0004,
103 "space" => 0x0008,
104 "punct" => 0x0010,
105 "cntrl" => 0x0020,
106 "blank" => 0x0040,
107 "xdigit" => 0x0080,
108 "alpha" => 0x0100
111 %categories =
113 "Lu" => $ctype{"alpha"}|$ctype{"upper"}, # Letter, Uppercase
114 "Ll" => $ctype{"alpha"}|$ctype{"lower"}, # Letter, Lowercase
115 "Lt" => $ctype{"alpha"}, # Letter, Titlecase
116 "Mn" => $ctype{"punct"}, # Mark, Non-Spacing
117 "Mc" => $ctype{"punct"}, # Mark, Spacing Combining
118 "Me" => $ctype{"punct"}, # Mark, Enclosing
119 "Nd" => $ctype{"digit"}, # Number, Decimal Digit
120 "Nl" => $ctype{"punct"}, # Number, Letter
121 "No" => $ctype{"punct"}, # Number, Other
122 "Zs" => $ctype{"space"}, # Separator, Space
123 "Zl" => 0, # Separator, Line
124 "Zp" => 0, # Separator, Paragraph
125 "Cc" => $ctype{"cntrl"}, # Other, Control
126 "Cf" => 0, # Other, Format
127 "Cs" => 0, # Other, Surrogate
128 "Co" => 0, # Other, Private Use
129 "Cn" => 0, # Other, Not Assigned
130 "Lm" => $ctype{"punct"}, # Letter, Modifier
131 "Lo" => $ctype{"alpha"}, # Letter, Other
132 "Pc" => $ctype{"punct"}, # Punctuation, Connector
133 "Pd" => $ctype{"punct"}, # Punctuation, Dash
134 "Ps" => $ctype{"punct"}, # Punctuation, Open
135 "Pe" => $ctype{"punct"}, # Punctuation, Close
136 "Pi" => $ctype{"punct"}, # Punctuation, Initial quote
137 "Pf" => $ctype{"punct"}, # Punctuation, Final quote
138 "Po" => $ctype{"punct"}, # Punctuation, Other
139 "Sm" => $ctype{"punct"}, # Symbol, Math
140 "Sc" => $ctype{"punct"}, # Symbol, Currency
141 "Sk" => $ctype{"punct"}, # Symbol, Modifier
142 "So" => $ctype{"punct"} # Symbol, Other
145 # a few characters need additional categories that cannot be determined automatically
146 %special_categories =
148 "xdigit" => [ ord('0')..ord('9'),ord('A')..ord('F'),ord('a')..ord('f'),
149 0xff10..0xff19, 0xff21..0xff26, 0xff41..0xff46 ],
150 "space" => [ 0x09..0x0d, 0xfeff ],
151 "blank" => [ 0x09, 0x20, 0xa0, 0xfeff ]
154 %directions =
156 "L" => 1, # Left-to-Right
157 "LRE" => 11, # Left-to-Right Embedding
158 "LRO" => 11, # Left-to-Right Override
159 "R" => 2, # Right-to-Left
160 "AL" => 2, # Right-to-Left Arabic
161 "RLE" => 11, # Right-to-Left Embedding
162 "RLO" => 11, # Right-to-Left Override
163 "PDF" => 11, # Pop Directional Format
164 "EN" => 3, # European Number
165 "ES" => 4, # European Number Separator
166 "ET" => 5, # European Number Terminator
167 "AN" => 6, # Arabic Number
168 "CS" => 7, # Common Number Separator
169 "NSM" => 0, # Non-Spacing Mark
170 "BN" => 0, # Boundary Neutral
171 "B" => 8, # Paragraph Separator
172 "S" => 9, # Segment Separator
173 "WS" => 10, # Whitespace
174 "ON" => 11 # Other Neutrals
178 ################################################################
179 # main routine
181 READ_DEFAULTS();
182 DUMP_CASE_MAPPINGS();
183 DUMP_COMPOSE_TABLES();
184 DUMP_CTYPE_TABLES();
186 foreach $file (@allfiles) { HANDLE_FILE( @$file ); }
188 OUTPUT_CPTABLE();
190 exit(0);
193 ################################################################
194 # read in the defaults file
195 sub READ_DEFAULTS
197 @unicode_defaults = ();
198 @unicode_aliases = ();
199 @tolower_table = ();
200 @toupper_table = ();
201 @category_table = ();
202 @direction_table = ();
203 @decomp_table = ();
204 @compose_table = ();
206 # first setup a few default mappings
208 open DEFAULTS or die "Cannot open $DEFAULTS";
209 print "Loading $DEFAULTS\n";
210 while (<DEFAULTS>)
212 next if /^\#/; # skip comments
213 next if /^$/; # skip empty lines
214 if (/^(([0-9a-fA-F]+)(,[0-9a-fA-F]+)*)\s+([0-9a-fA-F]+|'.'|none)\s+(\#.*)?/)
216 my @src = map hex, split /,/,$1;
217 my $dst = $4;
218 my $comment = $5;
219 if ($#src > 0) { push @unicode_aliases, \@src; }
220 next if ($dst eq "none");
221 $dst = ($dst =~ /\'.\'/) ? ord substr($dst,1,1) : hex $dst;
222 foreach $src (@src)
224 die "Duplicate value" if defined($unicode_defaults[$src]);
225 $unicode_defaults[$src] = $dst;
227 next;
229 die "Unrecognized line $_\n";
232 # now build mappings from the decomposition field of the Unicode database
234 open UNICODEDATA or die "Cannot open $UNICODEDATA";
235 print "Loading $UNICODEDATA\n";
236 while (<UNICODEDATA>)
238 # Decode the fields ...
239 ($code, $name, $cat, $comb, $bidi,
240 $decomp, $dec, $dig, $num, $mirror,
241 $oldname, $comment, $upper, $lower, $title) = split /;/;
243 my $src = hex $code;
245 die "unknown category $cat" unless defined $categories{$cat};
246 die "unknown directionality $bidi" unless defined $directions{$bidi};
248 $uniname[$src] = $name;
249 $category_table[$src] = $categories{$cat};
250 $direction_table[$src] = $directions{$bidi};
252 if ($lower ne "")
254 $tolower_table[$src] = hex $lower;
255 $category_table[$src] |= $ctype{"upper"}|$ctype{"alpha"};
257 if ($upper ne "")
259 $toupper_table[$src] = hex $upper;
260 $category_table[$src] |= $ctype{"lower"}|$ctype{"alpha"};
262 if ($dec ne "")
264 $category_table[$src] |= $ctype{"digit"};
267 # copy the category and direction for everything between First/Last pairs
268 if ($name =~ /, First>/) { $start = $src; }
269 if ($name =~ /, Last>/)
271 while ($start < $src)
273 $category_table[$start] = $category_table[$src];
274 $direction_table[$start] = $direction_table[$src];
275 $start++;
279 next if $decomp eq ""; # no decomposition, skip it
281 if ($decomp =~ /^<([a-zA-Z]+)>\s+([0-9a-fA-F]+)$/)
283 # decomposition of the form "<foo> 1234" -> use char if type is known
284 next unless ($1 eq "font" ||
285 $1 eq "noBreak" ||
286 $1 eq "circle" ||
287 $1 eq "super" ||
288 $1 eq "sub" ||
289 $1 eq "wide" ||
290 $1 eq "narrow" ||
291 $1 eq "compat" ||
292 $1 eq "small");
293 $dst = hex $2;
295 elsif ($decomp =~ /^<compat>\s+0020\s+([0-9a-fA-F]+)/)
297 # decomposition "<compat> 0020 1234" -> combining accent
298 $dst = hex $1;
300 elsif ($decomp =~ /^([0-9a-fA-F]+)/)
302 # decomposition contains only char values without prefix -> use first char
303 $dst = hex $1;
304 $category_table[$src] |= $category_table[$dst];
305 # store decomposition if it contains two chars
306 if ($decomp =~ /^([0-9a-fA-F]+)\s+([0-9a-fA-F]+)$/)
308 $decomp_table[$src] = [ hex $1, hex $2 ];
309 push @compose_table, [ hex $1, hex $2, $src ];
312 else
314 next;
317 next if defined($unicode_defaults[$src]); # may have been set in the defaults file
319 # check for loops
320 for ($i = $dst; ; $i = $unicode_defaults[$i])
322 die sprintf("loop detected for %04x -> %04x",$src,$dst) if $i == $src;
323 last unless defined($unicode_defaults[$i]);
325 $unicode_defaults[$src] = $dst;
328 # patch the category of some special characters
330 foreach $cat (keys %special_categories)
332 my $flag = $ctype{$cat};
333 foreach $i (@{$special_categories{$cat}}) { $category_table[$i] |= $flag; }
338 ################################################################
339 # parse the input file
340 sub READ_FILE
342 my $name = shift;
343 open INPUT,$name or die "Cannot open $name";
344 @cp2uni = ();
345 @lead_bytes = ();
346 @uni2cp = ();
348 while (<INPUT>)
350 next if /^\#/; # skip comments
351 next if /^$/; # skip empty lines
352 next if /\x1a/; # skip ^Z
353 next if (/^0x([0-9a-fA-F]+)\s+\#UNDEFINED/); # undefined char
355 if (/^0x([0-9a-fA-F]+)\s+\#DBCS LEAD BYTE/)
357 $cp = hex $1;
358 push @lead_bytes,$cp;
359 $cp2uni[$cp] = 0;
360 next;
362 if (/^0x([0-9a-fA-F]+)\s+0x([0-9a-fA-F]+)\s+(\#.*)?/)
364 $cp = hex $1;
365 $uni = hex $2;
366 $cp2uni[$cp] = $uni unless defined($cp2uni[$cp]);
367 $uni2cp[$uni] = $cp unless defined($uni2cp[$uni]);
368 next;
370 die "$name: Unrecognized line $_\n";
375 ################################################################
376 # parse the symbol.txt file, since its syntax is different from the other ones
377 sub READ_SYMBOL_FILE
379 my $name = shift;
380 open INPUT,$name or die "Cannot open $name";
381 @cp2uni = ();
382 @lead_bytes = ();
383 @uni2cp = ();
385 while (<INPUT>)
387 next if /^\#/; # skip comments
388 next if /^$/; # skip empty lines
389 next if /\x1a/; # skip ^Z
390 if (/^([0-9a-fA-F]+)\s+([0-9a-fA-F]+)\s+(\#.*)?/)
392 $uni = hex $1;
393 $cp = hex $2;
394 $cp2uni[$cp] = $uni unless defined($cp2uni[$cp]);
395 $uni2cp[$uni] = $cp unless defined($uni2cp[$uni]);
396 next;
398 die "$name: Unrecognized line $_\n";
403 ################################################################
404 # add default mappings once the file had been read
405 sub ADD_DEFAULT_MAPPINGS
407 # Apply aliases
409 foreach $alias (@unicode_aliases)
411 my $target = undef;
412 foreach $src (@$alias)
414 if (defined($uni2cp[$src]))
416 $target = $uni2cp[$src];
417 last;
420 next unless defined($target);
422 # At least one char of the alias set is defined, set the others to the same value
423 foreach $src (@$alias)
425 $uni2cp[$src] = $target unless defined($uni2cp[$src]);
429 # For every src -> target mapping in the defaults table,
430 # make uni2cp[src] = uni2cp[target] if uni2cp[target] is defined
432 for ($src = 0; $src < 65536; $src++)
434 next if defined($uni2cp[$src]); # source has a definition already
435 next unless defined($unicode_defaults[$src]); # no default for this char
436 my $target = $unicode_defaults[$src];
438 # do a recursive mapping until we find a target char that is defined
439 while (!defined($uni2cp[$target]) &&
440 defined($unicode_defaults[$target])) { $target = $unicode_defaults[$target]; }
442 if (defined($uni2cp[$target])) { $uni2cp[$src] = $uni2cp[$target]; }
445 # Add an identity mapping for all undefined chars
447 for ($i = 0; $i < 256; $i++)
449 next if defined($cp2uni[$i]);
450 next if defined($uni2cp[$i]);
451 $cp2uni[$i] = $uni2cp[$i] = $i;
455 ################################################################
456 # dump an array of integers
457 sub DUMP_ARRAY
459 my ($format,$default,@array) = @_;
460 my $i, $ret = " ";
461 for ($i = 0; $i < $#array; $i++)
463 $ret .= sprintf($format, defined $array[$i] ? $array[$i] : $default);
464 $ret .= (($i % 8) != 7) ? ", " : ",\n ";
466 $ret .= sprintf($format, defined $array[$i] ? $array[$i] : $default);
467 return $ret;
470 ################################################################
471 # dump an SBCS mapping table
472 sub DUMP_SBCS_TABLE
474 my ($codepage, $name) = @_;
475 my $i;
477 # output the ascii->unicode table
479 printf OUTPUT "static const WCHAR cp2uni[256] =\n";
480 printf OUTPUT "{\n%s\n};\n\n", DUMP_ARRAY( "0x%04x", $DEF_CHAR, @cp2uni[0 .. 255] );
482 # count the number of unicode->ascii subtables that contain something
484 my @filled = ();
485 my $subtables = 1;
486 for ($i = 0; $i < 65536; $i++)
488 next unless defined $uni2cp[$i];
489 $filled[$i >> 8] = 1;
490 $subtables++;
491 $i |= 255;
494 # output all the subtables into a single array
496 printf OUTPUT "static const unsigned char uni2cp_low[%d] =\n{\n", $subtables*256;
497 for ($i = 0; $i < 256; $i++)
499 next unless $filled[$i];
500 printf OUTPUT " /* 0x%02x00 .. 0x%02xff */\n", $i, $i;
501 printf OUTPUT "%s,\n", DUMP_ARRAY( "0x%02x", $DEF_CHAR, @uni2cp[($i<<8) .. ($i<<8)+255] );
503 printf OUTPUT " /* defaults */\n";
504 printf OUTPUT "%s\n};\n\n", DUMP_ARRAY( "0x%02x", 0, ($DEF_CHAR) x 256 );
506 # output a table of the offsets of the subtables in the previous array
508 my $pos = 0;
509 my @offsets = ();
510 for ($i = 0; $i < 256; $i++)
512 if ($filled[$i]) { push @offsets, $pos; $pos += 256; }
513 else { push @offsets, ($subtables-1) * 256; }
515 printf OUTPUT "static const unsigned short uni2cp_high[256] =\n";
516 printf OUTPUT "{\n%s\n};\n\n", DUMP_ARRAY( "0x%04x", 0, @offsets );
518 # output the code page descriptor
520 printf OUTPUT "const struct sbcs_table cptable_%03d =\n{\n", $codepage;
521 printf OUTPUT " { %d, 1, 0x%04x, 0x%04x, \"%s\" },\n",
522 $codepage, $DEF_CHAR, $DEF_CHAR, $name;
523 printf OUTPUT " cp2uni,\n";
524 printf OUTPUT " uni2cp_low,\n";
525 printf OUTPUT " uni2cp_high\n};\n";
529 ################################################################
530 # dump a DBCS mapping table
531 sub DUMP_DBCS_TABLE
533 my ($codepage, $name) = @_;
534 my $i, $x, $y;
536 # build a list of lead bytes that are actually used
538 my @lblist = ();
539 LBLOOP: for ($y = 0; $y <= $#lead_bytes; $y++)
541 my $base = $lead_bytes[$y] << 8;
542 for ($x = 0; $x < 256; $x++)
544 if (defined $cp2uni[$base+$x])
546 push @lblist,$lead_bytes[$y];
547 next LBLOOP;
551 my $unused = ($#lead_bytes > $#lblist);
553 # output the ascii->unicode table for the single byte chars
555 printf OUTPUT "static const WCHAR cp2uni[%d] =\n", 256 * ($#lblist + 2 + $unused);
556 printf OUTPUT "{\n%s,\n", DUMP_ARRAY( "0x%04x", $DEF_CHAR, @cp2uni[0 .. 255] );
558 # output the default table for unused lead bytes
560 if ($unused)
562 printf OUTPUT " /* unused lead bytes */\n";
563 printf OUTPUT "%s,\n", DUMP_ARRAY( "0x%04x", 0, ($DEF_CHAR) x 256 );
566 # output the ascii->unicode table for each DBCS lead byte
568 for ($y = 0; $y <= $#lblist; $y++)
570 my $base = $lblist[$y] << 8;
571 printf OUTPUT " /* lead byte %02x */\n", $lblist[$y];
572 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", $DEF_CHAR, @cp2uni[$base .. $base+255] );
573 printf OUTPUT ($y < $#lblist) ? ",\n" : "\n};\n\n";
576 # output the lead byte subtables offsets
578 my @offsets = ();
579 for ($x = 0; $x < 256; $x++) { $offsets[$x] = 0; }
580 for ($x = 0; $x <= $#lblist; $x++) { $offsets[$lblist[$x]] = $x + 1; }
581 if ($unused)
583 # increment all lead bytes offset to take into account the unused table
584 for ($x = 0; $x <= $#lead_bytes; $x++) { $offsets[$lead_bytes[$x]]++; }
586 printf OUTPUT "static const unsigned char cp2uni_leadbytes[256] =\n";
587 printf OUTPUT "{\n%s\n};\n\n", DUMP_ARRAY( "0x%02x", 0, @offsets );
589 # count the number of unicode->ascii subtables that contain something
591 my @filled = ();
592 my $subtables = 1;
593 for ($i = 0; $i < 65536; $i++)
595 next unless defined $uni2cp[$i];
596 $filled[$i >> 8] = 1;
597 $subtables++;
598 $i |= 255;
601 # output all the subtables into a single array
603 printf OUTPUT "static const unsigned short uni2cp_low[%d] =\n{\n", $subtables*256;
604 for ($y = 0; $y < 256; $y++)
606 next unless $filled[$y];
607 printf OUTPUT " /* 0x%02x00 .. 0x%02xff */\n", $y, $y;
608 printf OUTPUT "%s,\n", DUMP_ARRAY( "0x%04x", $DEF_CHAR, @uni2cp[($y<<8) .. ($y<<8)+255] );
610 printf OUTPUT " /* defaults */\n";
611 printf OUTPUT "%s\n};\n\n", DUMP_ARRAY( "0x%04x", 0, ($DEF_CHAR) x 256 );
613 # output a table of the offsets of the subtables in the previous array
615 my $pos = 0;
616 my @offsets = ();
617 for ($y = 0; $y < 256; $y++)
619 if ($filled[$y]) { push @offsets, $pos; $pos += 256; }
620 else { push @offsets, ($subtables-1) * 256; }
622 printf OUTPUT "static const unsigned short uni2cp_high[256] =\n";
623 printf OUTPUT "{\n%s\n};\n\n", DUMP_ARRAY( "0x%04x", 0, @offsets );
625 # output the code page descriptor
627 printf OUTPUT "const struct dbcs_table cptable_%03d =\n{\n", $codepage;
628 printf OUTPUT " { %d, 2, 0x%04x, 0x%04x, \"%s\" },\n",
629 $codepage, $DEF_CHAR, $DEF_CHAR, $name;
630 printf OUTPUT " cp2uni,\n";
631 printf OUTPUT " cp2uni_leadbytes,\n";
632 printf OUTPUT " uni2cp_low,\n";
633 printf OUTPUT " uni2cp_high,\n";
634 DUMP_LB_RANGES();
635 printf OUTPUT "};\n";
639 ################################################################
640 # dump the list of defined lead byte ranges
641 sub DUMP_LB_RANGES
643 my @list = ();
644 my $i = 0;
645 foreach $i (@lead_bytes) { $list[$i] = 1; }
646 my $on = 0;
647 printf OUTPUT " { ";
648 for ($i = 0; $i < 256; $i++)
650 if ($on)
652 if (!defined $list[$i]) { printf OUTPUT "0x%02x, ", $i-1; $on = 0; }
654 else
656 if ($list[$i]) { printf OUTPUT "0x%02x, ", $i; $on = 1; }
659 if ($on) { printf OUTPUT "0xff, "; }
660 printf OUTPUT "0x00, 0x00 }\n";
664 ################################################################
665 # dump the case mapping tables
666 sub DUMP_CASE_MAPPINGS
668 open OUTPUT,">casemap.c" or die "Cannot create casemap.c";
669 printf "Building casemap.c\n";
670 printf OUTPUT "/* Unicode case mappings */\n";
671 printf OUTPUT "/* Automatically generated; DO NOT EDIT!! */\n\n";
672 printf OUTPUT "#include \"wine/unicode.h\"\n\n";
674 DUMP_CASE_TABLE( "casemap_lower", @tolower_table );
675 DUMP_CASE_TABLE( "casemap_upper", @toupper_table );
676 close OUTPUT;
680 ################################################################
681 # dump a case mapping table
682 sub DUMP_CASE_TABLE
684 my ($name,@table) = @_;
686 # count the number of sub tables that contain something
688 my @filled = ();
689 my $pos = 512;
690 for ($i = 0; $i < 65536; $i++)
692 next unless defined $table[$i];
693 $filled[$i >> 8] = $pos;
694 $pos += 256;
695 $i |= 255;
697 for ($i = 0; $i < 65536; $i++)
699 next unless defined $table[$i];
700 $table[$i] = ($table[$i] - $i) & 0xffff;
703 # dump the table
705 printf OUTPUT "const WCHAR %s[%d] =\n", $name, $pos;
706 printf OUTPUT "{\n /* index */\n";
707 printf OUTPUT "%s,\n", DUMP_ARRAY( "0x%04x", 256, @filled );
708 printf OUTPUT " /* defaults */\n";
709 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, (0) x 256 );
710 for ($i = 0; $i < 256; $i++)
712 next unless $filled[$i];
713 printf OUTPUT ",\n /* 0x%02x00 .. 0x%02xff */\n", $i, $i;
714 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, @table[($i<<8) .. ($i<<8)+255] );
716 printf OUTPUT "\n};\n";
720 ################################################################
721 # dump the ctype tables
722 sub DUMP_CTYPE_TABLES
724 open OUTPUT,">wctype.c" or die "Cannot create casemap.c";
725 printf "Building wctype.c\n";
726 printf OUTPUT "/* Unicode ctype tables */\n";
727 printf OUTPUT "/* Automatically generated; DO NOT EDIT!! */\n\n";
728 printf OUTPUT "#include \"wine/unicode.h\"\n\n";
730 my $i;
731 my @array = (0) x 256;
733 # add the direction in the high 4 bits of the category
734 for ($i = 0; $i < 65536; $i++)
736 $category_table[$i] |= $direction_table[$i] << 12;
739 # try to merge table rows
740 for ($row = 0; $row < 256; $row++)
742 my $rowtxt = sprintf "%04x" x 256, @category_table[($row<<8)..($row<<8)+255];
743 if (defined($sequences{$rowtxt}))
745 # reuse an existing row
746 $array[$row] = $sequences{$rowtxt};
748 else
750 # create a new row
751 $sequences{$rowtxt} = $array[$row] = $#array + 1;
752 push @array, @category_table[($row<<8)..($row<<8)+255];
756 printf OUTPUT "const unsigned short wctype_table[%d] =\n{\n", $#array+1;
757 printf OUTPUT " /* offsets */\n%s,\n", DUMP_ARRAY( "0x%04x", 0, @array[0..255] );
758 printf OUTPUT " /* values */\n%s\n};\n", DUMP_ARRAY( "0x%04x", 0, @array[256..$#array] );
760 close OUTPUT;
764 ################################################################
765 # dump the char composition tables
766 sub DUMP_COMPOSE_TABLES
768 open OUTPUT,">compose.c" or die "Cannot create compose.c";
769 printf "Building compose.c\n";
770 printf OUTPUT "/* Unicode char composition */\n";
771 printf OUTPUT "/* Automatically generated; DO NOT EDIT!! */\n\n";
772 printf OUTPUT "#include \"wine/unicode.h\"\n\n";
774 ######### composition table
776 my @filled = ();
777 foreach $i (@compose_table)
779 my @comp = @$i;
780 push @{$filled[$comp[1]]}, [ $comp[0], $comp[2] ];
783 # count how many different second chars we have
785 for ($i = $count = 0; $i < 65536; $i++)
787 next unless defined $filled[$i];
788 $count++;
791 # build the table of second chars and offsets
793 my $pos = $count + 1;
794 for ($i = 0; $i < 65536; $i++)
796 next unless defined $filled[$i];
797 push @table, $i, $pos;
798 $pos += @{$filled[$i]};
800 # terminator with last position
801 push @table, 0, $pos;
802 printf OUTPUT "const WCHAR unicode_compose_table[0x%x] =\n{\n", 2*$pos;
803 printf OUTPUT " /* second chars + offsets */\n%s", DUMP_ARRAY( "0x%04x", 0, @table );
805 # build the table of first chars and mappings
807 for ($i = 0; $i < 65536; $i++)
809 next unless defined $filled[$i];
810 my @table = ();
811 my @list = sort { $a->[0] <=> $b->[0] } @{$filled[$i]};
812 for ($j = 0; $j <= $#list; $j++)
814 push @table, $list[$j][0], $list[$j][1];
816 printf OUTPUT ",\n /* 0x%04x */\n%s", $i, DUMP_ARRAY( "0x%04x", 0, @table );
818 printf OUTPUT "\n};\n\nconst unsigned int unicode_compose_table_size = %d;\n\n", $count;
820 ######### decomposition table
822 # first determine all the 16-char subsets that contain something
824 my @filled = (0) x 4096;
825 my $pos = 16*2; # for the null subset
826 for ($i = 0; $i < 65536; $i++)
828 next unless defined $decomp_table[$i];
829 $filled[$i >> 4] = $pos;
830 $pos += 16*2;
831 $i |= 15;
833 my $total = $pos;
835 # now count the 256-char subsets that contain something
837 my @filled_idx = (256) x 256;
838 $pos = 256 + 16;
839 for ($i = 0; $i < 4096; $i++)
841 next unless $filled[$i];
842 $filled_idx[$i >> 4] = $pos;
843 $pos += 16;
844 $i |= 15;
846 my $null_offset = $pos; # null mapping
847 $total += $pos;
849 # add the index offsets to the subsets positions
851 for ($i = 0; $i < 4096; $i++)
853 next unless $filled[$i];
854 $filled[$i] += $null_offset;
857 # dump the main index
859 printf OUTPUT "const WCHAR unicode_decompose_table[%d] =\n", $total;
860 printf OUTPUT "{\n /* index */\n";
861 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, @filled_idx );
862 printf OUTPUT ",\n /* null sub-index */\n%s", DUMP_ARRAY( "0x%04x", 0, ($null_offset) x 16 );
864 # dump the second-level indexes
866 for ($i = 0; $i < 256; $i++)
868 next unless ($filled_idx[$i] > 256);
869 my @table = @filled[($i<<4)..($i<<4)+15];
870 for ($j = 0; $j < 16; $j++) { $table[$j] ||= $null_offset; }
871 printf OUTPUT ",\n /* sub-index %02x */\n", $i;
872 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, @table );
875 # dump the 16-char subsets
877 printf OUTPUT ",\n /* null mapping */\n";
878 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, (0) x 32 );
880 for ($i = 0; $i < 4096; $i++)
882 next unless $filled[$i];
883 my @table = (0) x 32;
884 for ($j = 0; $j < 16; $j++)
886 if (defined $decomp_table[($i<<4) + $j])
888 $table[2 * $j] = ${$decomp_table[($i << 4) + $j]}[0];
889 $table[2 * $j + 1] = ${$decomp_table[($i << 4) + $j]}[1];
892 printf OUTPUT ",\n /* 0x%03x0 .. 0x%03xf */\n", $i, $i;
893 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, @table );
896 printf OUTPUT "\n};\n";
897 close OUTPUT;
901 ################################################################
902 # read an input file and generate the corresponding .c file
903 sub HANDLE_FILE
905 my ($codepage,$filename,$comment) = @_;
907 # symbol codepage file is special
908 if ($codepage == 42) { READ_SYMBOL_FILE($MAPPREFIX . $filename); }
909 else { READ_FILE($MAPPREFIX . $filename); }
911 ADD_DEFAULT_MAPPINGS();
913 my $output = sprintf "c_%03d.c", $codepage;
914 open OUTPUT,">$output" or die "Cannot create $output";
916 printf "Building %s from %s (%s)\n", $output, $filename, $comment;
918 # dump all tables
920 printf OUTPUT "/* code page %03d (%s) */\n", $codepage, $comment;
921 printf OUTPUT "/* generated from %s */\n", $MAPPREFIX . $filename;
922 printf OUTPUT "/* DO NOT EDIT!! */\n\n";
923 printf OUTPUT "#include \"wine/unicode.h\"\n\n";
925 if ($#lead_bytes == -1) { DUMP_SBCS_TABLE( $codepage, $comment ); }
926 else { DUMP_DBCS_TABLE( $codepage, $comment ); }
927 close OUTPUT;
931 ################################################################
932 # output the list of codepage tables into the cptable.c file
933 sub OUTPUT_CPTABLE
935 @tables_decl = ();
937 foreach $file (@allfiles)
939 my ($codepage,$filename,$comment) = @$file;
940 push @tables_decl, sprintf("extern union cptable cptable_%03d;\n",$codepage);
943 push @tables_decl, sprintf("\nstatic const union cptable * const cptables[%d] =\n{\n",$#allfiles+1);
944 foreach $file (@allfiles)
946 my ($codepage,$filename,$comment) = @$file;
947 push @tables_decl, sprintf(" &cptable_%03d,\n", $codepage);
949 push @tables_decl, "};";
950 REPLACE_IN_FILE( "cptable.c", @tables_decl );
953 ################################################################
954 # replace the contents of a file between ### cpmap ### marks
956 sub REPLACE_IN_FILE
958 my $name = shift;
959 my @data = @_;
960 my @lines = ();
961 open(FILE,$name) or die "Can't open $name";
962 while (<FILE>)
964 push @lines, $_;
965 last if /\#\#\# cpmap begin \#\#\#/;
967 push @lines, @data;
968 while (<FILE>)
970 if (/\#\#\# cpmap end \#\#\#/) { push @lines, "\n", $_; last; }
972 push @lines, <FILE>;
973 open(FILE,">$name") or die "Can't modify $name";
974 print FILE @lines;
975 close(FILE);