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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 # base URLs for www.unicode.org files
25 my $MAPPINGS = "http://www.unicode.org/Public/MAPPINGS";
26 my $UNIDATA = "http://www.unicode.org/Public/5.2.0/ucd";
29 my $SORTKEYS = "http://www.unicode.org/reports/tr10/allkeys.txt";
32 my $DEFAULTS = "./defaults";
34 # Default char for undefined mappings
35 my $DEF_CHAR = ord '?';
39 [ 37, "VENDORS/MICSFT/EBCDIC/CP037.TXT", 0, "IBM EBCDIC US Canada" ],
40 [ 424, "VENDORS/MISC/CP424.TXT", 0, "IBM EBCDIC Hebrew" ],
41 [ 437, "VENDORS/MICSFT/PC/CP437.TXT", 1, "OEM United States" ],
42 [ 500, "VENDORS/MICSFT/EBCDIC/CP500.TXT", 0, "IBM EBCDIC International" ],
43 [ 737, "VENDORS/MICSFT/PC/CP737.TXT", 1, "OEM Greek 437G" ],
44 [ 775, "VENDORS/MICSFT/PC/CP775.TXT", 1, "OEM Baltic" ],
45 [ 850, "VENDORS/MICSFT/PC/CP850.TXT", 1, "OEM Multilingual Latin 1" ],
46 [ 852, "VENDORS/MICSFT/PC/CP852.TXT", 1, "OEM Slovak Latin 2" ],
47 [ 855, "VENDORS/MICSFT/PC/CP855.TXT", 1, "OEM Cyrillic" ],
48 [ 856, "VENDORS/MISC/CP856.TXT", 0, "Hebrew PC" ],
49 [ 857, "VENDORS/MICSFT/PC/CP857.TXT", 1, "OEM Turkish" ],
50 [ 860, "VENDORS/MICSFT/PC/CP860.TXT", 1, "OEM Portuguese" ],
51 [ 861, "VENDORS/MICSFT/PC/CP861.TXT", 1, "OEM Icelandic" ],
52 [ 862, "VENDORS/MICSFT/PC/CP862.TXT", 1, "OEM Hebrew" ],
53 [ 863, "VENDORS/MICSFT/PC/CP863.TXT", 1, "OEM Canadian French" ],
54 [ 864, "VENDORS/MICSFT/PC/CP864.TXT", 0, "OEM Arabic" ],
55 [ 865, "VENDORS/MICSFT/PC/CP865.TXT", 1, "OEM Nordic" ],
56 [ 866, "VENDORS/MICSFT/PC/CP866.TXT", 1, "OEM Russian" ],
57 [ 869, "VENDORS/MICSFT/PC/CP869.TXT", 1, "OEM Greek" ],
58 [ 874, "VENDORS/MICSFT/WindowsBestFit/bestfit874.txt", 1, "ANSI/OEM Thai" ],
59 [ 875, "VENDORS/MICSFT/EBCDIC/CP875.TXT", 0, "IBM EBCDIC Greek" ],
60 [ 878, "VENDORS/MISC/KOI8-R.TXT", 0, "Russian KOI8" ],
61 [ 932, "VENDORS/MICSFT/WindowsBestFit/bestfit932.txt", 0, "ANSI/OEM Japanese Shift-JIS" ],
62 [ 936, "VENDORS/MICSFT/WindowsBestFit/bestfit936.txt", 0, "ANSI/OEM Simplified Chinese GBK" ],
63 [ 949, "VENDORS/MICSFT/WindowsBestFit/bestfit949.txt", 0, "ANSI/OEM Korean Unified Hangul" ],
64 [ 950, "VENDORS/MICSFT/WindowsBestFit/bestfit950.txt", 0, "ANSI/OEM Traditional Chinese Big5" ],
65 [ 1006, "VENDORS/MISC/CP1006.TXT", 0, "IBM Arabic" ],
66 [ 1026, "VENDORS/MICSFT/EBCDIC/CP1026.TXT", 0, "IBM EBCDIC Latin 5 Turkish" ],
67 [ 1250, "VENDORS/MICSFT/WindowsBestFit/bestfit1250.txt", 0, "ANSI Eastern Europe" ],
68 [ 1251, "VENDORS/MICSFT/WindowsBestFit/bestfit1251.txt", 0, "ANSI Cyrillic" ],
69 [ 1252, "VENDORS/MICSFT/WindowsBestFit/bestfit1252.txt", 0, "ANSI Latin 1" ],
70 [ 1253, "VENDORS/MICSFT/WindowsBestFit/bestfit1253.txt", 0, "ANSI Greek" ],
71 [ 1254, "VENDORS/MICSFT/WindowsBestFit/bestfit1254.txt", 0, "ANSI Turkish" ],
72 [ 1255, "VENDORS/MICSFT/WindowsBestFit/bestfit1255.txt", 0, "ANSI Hebrew" ],
73 [ 1256, "VENDORS/MICSFT/WindowsBestFit/bestfit1256.txt", 0, "ANSI Arabic" ],
74 [ 1257, "VENDORS/MICSFT/WindowsBestFit/bestfit1257.txt", 0, "ANSI Baltic" ],
75 [ 1258, "VENDORS/MICSFT/WindowsBestFit/bestfit1258.txt", 0, "ANSI/OEM Viet Nam" ],
76 [ 1361, "OBSOLETE/EASTASIA/KSC/JOHAB.TXT", 0, "Korean Johab" ],
77 [ 10000, "VENDORS/MICSFT/MAC/ROMAN.TXT", 0, "Mac Roman" ],
78 [ 10006, "VENDORS/MICSFT/MAC/GREEK.TXT", 0, "Mac Greek" ],
79 [ 10007, "VENDORS/MICSFT/MAC/CYRILLIC.TXT", 0, "Mac Cyrillic" ],
80 [ 10029, "VENDORS/MICSFT/MAC/LATIN2.TXT", 0, "Mac Latin 2" ],
81 [ 10079, "VENDORS/MICSFT/MAC/ICELAND.TXT", 0, "Mac Icelandic" ],
82 [ 10081, "VENDORS/MICSFT/MAC/TURKISH.TXT", 0, "Mac Turkish" ],
83 [ 20127, undef, 0, "US-ASCII (7bit)" ],
84 [ 20866, "VENDORS/MISC/KOI8-R.TXT", 0, "Russian KOI8" ],
85 [ 20932, "OBSOLETE/EASTASIA/JIS/JIS0208.TXT", 0, "EUC-JP" ],
86 [ 21866, "VENDORS/MISC/KOI8-U.TXT", 0, "Ukrainian KOI8" ],
87 [ 28591, "ISO8859/8859-1.TXT", 0, "ISO 8859-1 Latin 1" ],
88 [ 28592, "ISO8859/8859-2.TXT", 0, "ISO 8859-2 Latin 2 (East European)" ],
89 [ 28593, "ISO8859/8859-3.TXT", 0, "ISO 8859-3 Latin 3 (South European)" ],
90 [ 28594, "ISO8859/8859-4.TXT", 0, "ISO 8859-4 Latin 4 (Baltic old)" ],
91 [ 28595, "ISO8859/8859-5.TXT", 0, "ISO 8859-5 Cyrillic" ],
92 [ 28596, "ISO8859/8859-6.TXT", 0, "ISO 8859-6 Arabic" ],
93 [ 28597, "ISO8859/8859-7.TXT", 0, "ISO 8859-7 Greek" ],
94 [ 28598, "ISO8859/8859-8.TXT", 0, "ISO 8859-8 Hebrew" ],
95 [ 28599, "ISO8859/8859-9.TXT", 0, "ISO 8859-9 Latin 5 (Turkish)" ],
96 [ 28600, "ISO8859/8859-10.TXT", 0, "ISO 8859-10 Latin 6 (Nordic)" ],
97 [ 28603, "ISO8859/8859-13.TXT", 0, "ISO 8859-13 Latin 7 (Baltic)" ],
98 [ 28604, "ISO8859/8859-14.TXT", 0, "ISO 8859-14 Latin 8 (Celtic)" ],
99 [ 28605, "ISO8859/8859-15.TXT", 0, "ISO 8859-15 Latin 9 (Euro)" ],
100 [ 28606, "ISO8859/8859-16.TXT", 0, "ISO 8859-16 Latin 10 (Balkan)" ]
120 "Lu" => $ctype{"defin"}|$ctype{"alpha"}|$ctype{"upper"}, # Letter, Uppercase
121 "Ll" => $ctype{"defin"}|$ctype{"alpha"}|$ctype{"lower"}, # Letter, Lowercase
122 "Lt" => $ctype{"defin"}|$ctype{"alpha"}|$ctype{"upper"}|$ctype{"lower"}, # Letter, Titlecase
123 "Mn" => $ctype{"defin"}, # Mark, Non-Spacing
124 "Mc" => $ctype{"defin"}, # Mark, Spacing Combining
125 "Me" => $ctype{"defin"}, # Mark, Enclosing
126 "Nd" => $ctype{"defin"}|$ctype{"digit"}, # Number, Decimal Digit
127 "Nl" => $ctype{"defin"}|$ctype{"alpha"}, # Number, Letter
128 "No" => $ctype{"defin"}, # Number, Other
129 "Zs" => $ctype{"defin"}|$ctype{"space"}, # Separator, Space
130 "Zl" => $ctype{"defin"}|$ctype{"space"}, # Separator, Line
131 "Zp" => $ctype{"defin"}|$ctype{"space"}, # Separator, Paragraph
132 "Cc" => $ctype{"defin"}|$ctype{"cntrl"}, # Other, Control
133 "Cf" => $ctype{"defin"}|$ctype{"cntrl"}, # Other, Format
134 "Cs" => $ctype{"defin"}, # Other, Surrogate
135 "Co" => $ctype{"defin"}, # Other, Private Use
136 "Cn" => $ctype{"defin"}, # Other, Not Assigned
137 "Lm" => $ctype{"defin"}|$ctype{"alpha"}, # Letter, Modifier
138 "Lo" => $ctype{"defin"}|$ctype{"alpha"}, # Letter, Other
139 "Pc" => $ctype{"defin"}|$ctype{"punct"}, # Punctuation, Connector
140 "Pd" => $ctype{"defin"}|$ctype{"punct"}, # Punctuation, Dash
141 "Ps" => $ctype{"defin"}|$ctype{"punct"}, # Punctuation, Open
142 "Pe" => $ctype{"defin"}|$ctype{"punct"}, # Punctuation, Close
143 "Pi" => $ctype{"defin"}|$ctype{"punct"}, # Punctuation, Initial quote
144 "Pf" => $ctype{"defin"}|$ctype{"punct"}, # Punctuation, Final quote
145 "Po" => $ctype{"defin"}|$ctype{"punct"}, # Punctuation, Other
146 "Sm" => $ctype{"defin"}, # Symbol, Math
147 "Sc" => $ctype{"defin"}, # Symbol, Currency
148 "Sk" => $ctype{"defin"}, # Symbol, Modifier
149 "So" => $ctype{"defin"} # Symbol, Other
152 # a few characters need additional categories that cannot be determined automatically
153 my %special_categories =
155 "xdigit" => [ ord('0')..ord('9'),ord('A')..ord('F'),ord('a')..ord('f'),
156 0xff10..0xff19, 0xff21..0xff26, 0xff41..0xff46 ],
157 "space" => [ 0x09..0x0d, 0x85 ],
158 "blank" => [ 0x09, 0x20, 0xa0, 0x3000, 0xfeff ],
159 "cntrl" => [ 0x070f, 0x200c, 0x200d,
160 0x200e, 0x200f, 0x202a, 0x202b, 0x202c, 0x202d, 0x202e,
161 0x206a, 0x206b, 0x206c, 0x206d, 0x206e, 0x206f, 0xfeff,
162 0xfff9, 0xfffa, 0xfffb ]
167 "L" => 1, # Left-to-Right
168 "LRE" => 15, # Left-to-Right Embedding
169 "LRO" => 15, # Left-to-Right Override
170 "R" => 2, # Right-to-Left
171 "AL" => 12, # Right-to-Left Arabic
172 "RLE" => 15, # Right-to-Left Embedding
173 "RLO" => 15, # Right-to-Left Override
174 "PDF" => 15, # Pop Directional Format
175 "EN" => 3, # European Number
176 "ES" => 4, # European Number Separator
177 "ET" => 5, # European Number Terminator
178 "AN" => 6, # Arabic Number
179 "CS" => 7, # Common Number Separator
180 "NSM" => 13, # Non-Spacing Mark
181 "BN" => 14, # Boundary Neutral
182 "B" => 8, # Paragraph Separator
183 "S" => 9, # Segment Separator
184 "WS" => 10, # Whitespace
185 "ON" => 11 # Other Neutrals
190 "U" => 0, # Non_Joining
191 "T" => 1, # Transparent
192 "R" => 2, # Right_Joining
193 "L" => 3, # Left_Joining
194 "D" => 4, # Dual_Joining
195 "C" => 5, # Join_Causing
201 my @unicode_defaults = ();
202 my @unicode_aliases = ();
203 my @tolower_table = ();
204 my @toupper_table = ();
205 my @digitmap_table = ();
206 my @compatmap_table = ();
207 my @category_table = (0) x 65536;
208 my @joining_table = (0) x 65536;
209 my @direction_table = ();
210 my @decomp_table = ();
211 my @compose_table = ();
221 ################################################################
222 # fetch a unicode.org file and open it
223 sub open_data_file($)
226 (my $name = $url) =~ s/^.*\///;
228 unless (-f "data/$name")
230 print "Fetching $url...\n";
232 !system "wget", "-q", "-O", "data/$name", $url or die "cannot fetch $url";
234 open FILE, "<data/$name" or die "cannot open data/$name";
238 ################################################################
239 # read in the defaults file
242 my $filename = shift;
245 # first setup a few default mappings
247 open DEFAULTS, "$filename" or die "Cannot open $filename";
248 print "Loading $filename\n";
251 next if /^\#/; # skip comments
252 next if /^$/; # skip empty lines
253 if (/^(([0-9a-fA-F]+)(,[0-9a-fA-F]+)*)\s+([0-9a-fA-F]+|'.'|none)\s+(\#.*)?/)
255 my @src = map hex, split /,/,$1;
258 if ($#src > 0) { push @unicode_aliases, \@src; }
259 next if ($dst eq "none");
260 $dst = ($dst =~ /\'.\'/) ? ord substr($dst,1,1) : hex $dst;
261 foreach my $src (@src)
263 die "Duplicate value" if defined($unicode_defaults[$src]);
264 $unicode_defaults[$src] = $dst;
268 die "Unrecognized line $_\n";
272 # now build mappings from the decomposition field of the Unicode database
274 my $UNICODE_DATA = open_data_file "$UNIDATA/UnicodeData.txt";
275 while (<$UNICODE_DATA>)
277 # Decode the fields ...
278 my ($code, $name, $cat, $comb, $bidi,
279 $decomp, $dec, $dig, $num, $mirror,
280 $oldname, $comment, $upper, $lower, $title) = split /;/;
284 die "unknown category $cat" unless defined $categories{$cat};
285 die "unknown directionality $bidi" unless defined $directions{$bidi};
287 $category_table[$src] = $categories{$cat};
288 $direction_table[$src] = $directions{$bidi};
289 $joining_table[$src] = $joining_types{"T"} if $cat eq "Mn" || $cat eq "Me" || $cat eq "Cf";
293 $tolower_table[$src] = hex $lower;
297 $toupper_table[$src] = hex $upper;
301 $category_table[$src] |= $ctype{"digit"};
305 $digitmap_table[$src] = ord $dig;
308 # copy the category and direction for everything between First/Last pairs
309 if ($name =~ /, First>/) { $start = $src; }
310 if ($name =~ /, Last>/)
312 while ($start < $src)
314 $category_table[$start] = $category_table[$src];
315 $direction_table[$start] = $direction_table[$src];
320 next if $decomp eq ""; # no decomposition, skip it
322 if ($decomp =~ /^<([a-zA-Z]+)>\s+([0-9a-fA-F]+)$/)
324 # decomposition of the form "<foo> 1234" -> use char if type is known
325 if (($src >= 0xf900 && $src < 0xfb00) || ($src >= 0xfe30 && $src < 0xfffd))
327 # Single char decomposition in the compatibility range
328 $compatmap_table[$src] = hex $2;
330 if ($1 eq "isolated" || $1 eq "final" || $1 eq "initial" || $1 eq "medial")
332 ${joining_forms{$1}}[hex $2] = $src;
335 next unless ($1 eq "font" ||
346 elsif ($decomp =~ /^<compat>\s+0020\s+([0-9a-fA-F]+)/)
348 # decomposition "<compat> 0020 1234" -> combining accent
351 elsif ($decomp =~ /^([0-9a-fA-F]+)/)
353 # decomposition contains only char values without prefix -> use first char
355 $category_table[$src] |= $category_table[$dst] if defined $category_table[$dst];
356 # store decomposition if it contains two chars
357 if ($decomp =~ /^([0-9a-fA-F]+)\s+([0-9a-fA-F]+)$/)
359 $decomp_table[$src] = [ hex $1, hex $2 ];
360 push @compose_table, [ hex $1, hex $2, $src ];
362 elsif ($decomp =~ /^(<[a-z]+>\s)*([0-9a-fA-F]+)$/ &&
363 (($src >= 0xf900 && $src < 0xfb00) || ($src >= 0xfe30 && $src < 0xfffd)))
365 # Single char decomposition in the compatibility range
366 $compatmap_table[$src] = hex $2;
374 next if defined($unicode_defaults[$src]); # may have been set in the defaults file
377 for (my $i = $dst; ; $i = $unicode_defaults[$i])
379 die sprintf("loop detected for %04x -> %04x",$src,$dst) if $i == $src;
380 last unless defined($unicode_defaults[$i]);
382 $unicode_defaults[$src] = $dst;
386 # patch the category of some special characters
388 foreach my $cat (keys %special_categories)
390 my $flag = $ctype{$cat};
391 foreach my $i (@{$special_categories{$cat}}) { $category_table[$i] |= $flag; }
396 ################################################################
397 # parse the input file
401 my $INPUT = open_data_file $name;
405 next if /^\#/; # skip comments
406 next if /^$/; # skip empty lines
407 next if /\x1a/; # skip ^Z
408 next if (/^0x([0-9a-fA-F]+)\s+\#UNDEFINED/); # undefined char
410 if (/^0x([0-9a-fA-F]+)\s+\#DBCS LEAD BYTE/)
413 push @lead_bytes,$cp;
417 if (/^0x([0-9a-fA-F]+)\s+0x([0-9a-fA-F]+)\s+(\#.*)?/)
421 $cp2uni[$cp] = $uni unless defined($cp2uni[$cp]);
422 $uni2cp[$uni] = $cp unless defined($uni2cp[$uni]);
423 if ($cp > 0xff && !defined($cp2uni[$cp >> 8]))
425 push @lead_bytes,$cp >> 8;
426 $cp2uni[$cp >> 8] = 0;
430 die "$name: Unrecognized line $_\n";
436 ################################################################
437 # fill input data for the 20127 (us-ascii) codepage
438 sub fill_20127_codepage()
440 for (my $i = 0; $i < 128; $i++) { $cp2uni[$i] = $uni2cp[$i] = $i; }
441 for (my $i = 128; $i < 256; $i++) { $cp2uni[$i] = $i & 0x7f; }
444 ################################################################
445 # get a mapping including glyph chars for MB_USEGLYPHCHARS
447 sub get_glyphs_mapping(@)
449 $_[0x01] = 0x263a; # (WHITE SMILING FACE)
450 $_[0x02] = 0x263b; # (BLACK SMILING FACE)
451 $_[0x03] = 0x2665; # (BLACK HEART SUIT)
452 $_[0x04] = 0x2666; # (BLACK DIAMOND SUIT)
453 $_[0x05] = 0x2663; # (BLACK CLUB SUIT)
454 $_[0x06] = 0x2660; # (BLACK SPADE SUIT)
455 $_[0x07] = 0x2022; # (BULLET)
456 $_[0x08] = 0x25d8; # (INVERSE BULLET)
457 $_[0x09] = 0x25cb; # (WHITE CIRCLE)
458 $_[0x0a] = 0x25d9; # (INVERSE WHITE CIRCLE)
459 $_[0x0b] = 0x2642; # (MALE SIGN)
460 $_[0x0c] = 0x2640; # (FEMALE SIGN)
461 $_[0x0d] = 0x266a; # (EIGHTH NOTE)
462 $_[0x0e] = 0x266b; # (BEAMED EIGHTH NOTES)
463 $_[0x0f] = 0x263c; # (WHITE SUN WITH RAYS)
464 $_[0x10] = 0x25ba; # (BLACK RIGHT-POINTING POINTER)
465 $_[0x11] = 0x25c4; # (BLACK LEFT-POINTING POINTER)
466 $_[0x12] = 0x2195; # (UP DOWN ARROW)
467 $_[0x13] = 0x203c; # (DOUBLE EXCLAMATION MARK)
468 $_[0x14] = 0x00b6; # (PILCROW SIGN)
469 $_[0x15] = 0x00a7; # (SECTION SIGN)
470 $_[0x16] = 0x25ac; # (BLACK RECTANGLE)
471 $_[0x17] = 0x21a8; # (UP DOWN ARROW WITH BASE)
472 $_[0x18] = 0x2191; # (UPWARDS ARROW)
473 $_[0x19] = 0x2193; # (DOWNWARDS ARROW)
474 $_[0x1a] = 0x2192; # (RIGHTWARDS ARROW)
475 $_[0x1b] = 0x2190; # (LEFTWARDS ARROW)
476 $_[0x1c] = 0x221f; # (RIGHT ANGLE)
477 $_[0x1d] = 0x2194; # (LEFT RIGHT ARROW)
478 $_[0x1e] = 0x25b2; # (BLACK UP-POINTING TRIANGLE)
479 $_[0x1f] = 0x25bc; # (BLACK DOWN-POINTING TRIANGLE)
480 $_[0x7f] = 0x2302; # (HOUSE)
484 ################################################################
485 # build EUC-JP table from the JIS 0208 file
486 # FIXME: for proper EUC-JP we should probably read JIS 0212 too
487 # but this would require 3-byte DBCS characters
488 sub READ_JIS0208_FILE($)
493 for (my $i = 0x00; $i <= 0x7f; $i++)
499 # JIS X 0201 right plane
500 for (my $i = 0xa1; $i <= 0xdf; $i++)
502 $cp2uni[0x8e00 + $i] = 0xfec0 + $i;
503 $uni2cp[0xfec0 + $i] = 0x8e00 + $i;
507 foreach my $i (0x8e, 0x8f, 0xa1 .. 0xfe)
514 foreach my $i (0x80 .. 0x8d, 0x90 .. 0xa0, 0xff)
516 $cp2uni[$i] = $DEF_CHAR;
519 # Shift-JIS compatibility
520 $uni2cp[0x00a5] = 0x5c;
521 $uni2cp[0x203e] = 0x7e;
523 # Fix backslash conversion
524 $cp2uni[0xa1c0] = 0xff3c;
525 $uni2cp[0xff3c] = 0xa1c0;
527 my $INPUT = open_data_file $name;
530 next if /^\#/; # skip comments
531 next if /^$/; # skip empty lines
532 next if /\x1a/; # skip ^Z
533 if (/^0x[0-9a-fA-F]+\s+0x([0-9a-fA-F]+)\s+0x([0-9a-fA-F]+)\s+(\#.*)?/)
535 my $cp = 0x8080 + hex $1;
537 $cp2uni[$cp] = $uni unless defined($cp2uni[$cp]);
538 $uni2cp[$uni] = $cp unless defined($uni2cp[$uni]);
541 die "$name: Unrecognized line $_\n";
547 ################################################################
548 # build the sort keys table
549 sub READ_SORTKEYS_FILE()
552 for (my $i = 0; $i < 65536; $i++) { $sortkeys[$i] = [ -1, 0, 0, 0, 0 ] };
554 my $INPUT = open_data_file $SORTKEYS;
557 next if /^\#/; # skip comments
558 next if /^$/; # skip empty lines
559 next if /\x1a/; # skip ^Z
560 next if /^\@version/; # skip @version header
561 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]+)\]/)
563 my ($uni,$variable) = (hex $1, $2);
564 next if $uni > 65535;
565 $sortkeys[$uni] = [ $uni, hex $3, hex $4, hex $5, hex $6 ];
568 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]+)\]/)
570 # multiple character sequence, ignored for now
573 die "$SORTKEYS: Unrecognized line $_\n";
577 # compress the keys to 32 bit:
578 # key 1 to 16 bits, key 2 to 8 bits, key 3 to 4 bits, key 4 to 1 bit
580 @sortkeys = sort { ${$a}[1] <=> ${$b}[1] or
581 ${$a}[2] <=> ${$b}[2] or
582 ${$a}[3] <=> ${$b}[3] or
583 ${$a}[4] <=> ${$b}[4] or
584 $a cmp $b; } @sortkeys;
586 my ($n2, $n3) = (1, 1);
587 my @keys = (-1, -1, -1, -1, -1 );
590 for (my $i = 0; $i < 65536; $i++)
592 my @current = @{$sortkeys[$i]};
593 next if $current[0] == -1;
594 if ($current[1] == $keys[1])
596 if ($current[2] == $keys[2])
598 if ($current[3] == $keys[3])
604 $keys[3] = $current[3];
611 $keys[2] = $current[2];
612 $keys[3] = $current[3];
620 $keys[1] = $current[1];
621 $keys[2] = $current[2];
622 $keys[3] = $current[3];
627 if ($current[2]) { $current[2] = $n2; }
628 if ($current[3]) { $current[3] = $n3; }
629 if ($current[4]) { $current[4] = 1; }
631 $flatkeys[$current[0]] = ($current[1] << 16) | ($current[2] << 8) | ($current[3] << 4) | $current[4];
637 ################################################################
638 # build the sort keys table
639 sub DUMP_SORTKEYS($@)
641 my ($filename, @keys) = @_;
643 # count the number of 256-key ranges that contain something
647 for (my $i = 0; $i < 256; $i++) { $offsets[$i] = 256; }
648 for (my $i = 0; $i < 65536; $i++)
650 next unless defined $keys[$i];
651 $offsets[$i >> 8] = $ranges * 256;
656 # output the range offsets
658 open OUTPUT,">$filename.new" or die "Cannot create $filename";
659 printf "Building $filename\n";
660 printf OUTPUT "/* Unicode collation element table */\n";
661 printf OUTPUT "/* generated from %s */\n", $SORTKEYS;
662 printf OUTPUT "/* DO NOT EDIT!! */\n\n";
664 printf OUTPUT "const unsigned int collation_table[%d] =\n{\n", $ranges*256;
665 printf OUTPUT " /* index */\n";
666 printf OUTPUT "%s,\n", DUMP_ARRAY( "0x%08x", 0, @offsets );
668 # output the default values
670 printf OUTPUT " /* defaults */\n";
671 printf OUTPUT "%s", DUMP_ARRAY( "0x%08x", 0, (0xffffffff) x 256 );
673 # output all the key ranges
675 for (my $i = 0; $i < 256; $i++)
677 next if $offsets[$i] == 256;
678 printf OUTPUT ",\n /* 0x%02x00 .. 0x%02xff */\n", $i, $i;
679 printf OUTPUT "%s", DUMP_ARRAY( "0x%08x", 0xffffffff, @keys[($i<<8) .. ($i<<8)+255] );
681 printf OUTPUT "\n};\n";
683 save_file($filename);
687 ################################################################
688 # add default mappings once the file had been read
689 sub ADD_DEFAULT_MAPPINGS()
693 foreach my $alias (@unicode_aliases)
696 foreach my $src (@$alias)
698 if (defined($uni2cp[$src]))
700 $target = $uni2cp[$src];
704 next unless defined($target);
706 # At least one char of the alias set is defined, set the others to the same value
707 foreach my $src (@$alias)
709 $uni2cp[$src] = $target unless defined($uni2cp[$src]);
713 # For every src -> target mapping in the defaults table,
714 # make uni2cp[src] = uni2cp[target] if uni2cp[target] is defined
716 for (my $src = 0; $src < 65536; $src++)
718 next if defined($uni2cp[$src]); # source has a definition already
719 next unless defined($unicode_defaults[$src]); # no default for this char
720 my $target = $unicode_defaults[$src];
722 # do a recursive mapping until we find a target char that is defined
723 while (!defined($uni2cp[$target]) &&
724 defined($unicode_defaults[$target])) { $target = $unicode_defaults[$target]; }
726 if (defined($uni2cp[$target])) { $uni2cp[$src] = $uni2cp[$target]; }
729 # Add an identity mapping for all undefined chars
731 for (my $i = 0; $i < 256; $i++)
733 next if defined($cp2uni[$i]);
734 next if defined($uni2cp[$i]);
735 $cp2uni[$i] = $uni2cp[$i] = $i;
739 ################################################################
740 # dump an array of integers
743 my ($format,$default,@array) = @_;
746 for ($i = 0; $i < $#array; $i++)
748 $ret .= sprintf($format, defined $array[$i] ? $array[$i] : $default);
749 $ret .= (($i % 8) != 7) ? ", " : ",\n ";
751 $ret .= sprintf($format, defined $array[$i] ? $array[$i] : $default);
755 ################################################################
756 # dump an SBCS mapping table
757 sub dump_sbcs_table($$$$$)
759 my ($codepage, $has_glyphs, $name, $def, $defw) = @_;
762 # output the ascii->unicode table
766 printf OUTPUT "static const WCHAR cp2uni[512] =\n";
767 printf OUTPUT "{\n%s", DUMP_ARRAY( "0x%04x", $defw, @cp2uni[0 .. 255] );
768 printf OUTPUT ",\n /* glyphs */\n%s\n};\n\n",
769 DUMP_ARRAY( "0x%04x", $defw, get_glyphs_mapping(@cp2uni[0 .. 255]) );
773 printf OUTPUT "static const WCHAR cp2uni[256] =\n";
774 printf OUTPUT "{\n%s\n};\n\n", DUMP_ARRAY( "0x%04x", $defw, @cp2uni[0 .. 255] );
777 # count the number of unicode->ascii subtables that contain something
781 for (my $i = 0; $i < 65536; $i++)
783 next unless defined $uni2cp[$i];
784 $filled[$i >> 8] = 1;
789 # output all the subtables into a single array
791 printf OUTPUT "static const unsigned char uni2cp_low[%d] =\n{\n", $subtables*256;
792 for (my $i = 0; $i < 256; $i++)
794 next unless $filled[$i];
795 printf OUTPUT " /* 0x%02x00 .. 0x%02xff */\n", $i, $i;
796 printf OUTPUT "%s,\n", DUMP_ARRAY( "0x%02x", $def, @uni2cp[($i<<8) .. ($i<<8)+255] );
798 printf OUTPUT " /* defaults */\n";
799 printf OUTPUT "%s\n};\n\n", DUMP_ARRAY( "0x%02x", 0, ($def) x 256 );
801 # output a table of the offsets of the subtables in the previous array
805 for (my $i = 0; $i < 256; $i++)
807 if ($filled[$i]) { push @offsets, $pos; $pos += 256; }
808 else { push @offsets, ($subtables-1) * 256; }
810 printf OUTPUT "static const unsigned short uni2cp_high[256] =\n";
811 printf OUTPUT "{\n%s\n};\n\n", DUMP_ARRAY( "0x%04x", 0, @offsets );
813 # output the code page descriptor
815 printf OUTPUT "const struct sbcs_table cptable_%03d =\n{\n", $codepage;
816 printf OUTPUT " { %d, 1, 0x%04x, 0x%04x, \"%s\" },\n",
817 $codepage, $def, $defw, $name;
818 printf OUTPUT " cp2uni,\n";
819 if ($has_glyphs) { printf OUTPUT " cp2uni + 256,\n"; }
820 else { printf OUTPUT " cp2uni,\n"; }
821 printf OUTPUT " uni2cp_low,\n";
822 printf OUTPUT " uni2cp_high\n};\n";
826 ################################################################
827 # dump a DBCS mapping table
828 sub dump_dbcs_table($$$$@)
830 my ($codepage, $name, $def, $defw, @lb_ranges) = @_;
832 # build a list of lead bytes that are actually used
835 LBLOOP: for (my $y = 0; $y <= $#lead_bytes; $y++)
837 my $base = $lead_bytes[$y] << 8;
838 for (my $x = 0; $x < 256; $x++)
840 if (defined $cp2uni[$base+$x])
842 push @lblist,$lead_bytes[$y];
847 my $unused = ($#lead_bytes > $#lblist);
849 # output the ascii->unicode table for the single byte chars
851 printf OUTPUT "static const WCHAR cp2uni[%d] =\n", 256 * ($#lblist + 2 + $unused);
852 printf OUTPUT "{\n%s,\n", DUMP_ARRAY( "0x%04x", $defw, @cp2uni[0 .. 255] );
854 # output the default table for unused lead bytes
858 printf OUTPUT " /* unused lead bytes */\n";
859 printf OUTPUT "%s,\n", DUMP_ARRAY( "0x%04x", 0, ($defw) x 256 );
862 # output the ascii->unicode table for each DBCS lead byte
864 for (my $y = 0; $y <= $#lblist; $y++)
866 my $base = $lblist[$y] << 8;
867 printf OUTPUT " /* lead byte %02x */\n", $lblist[$y];
868 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", $defw, @cp2uni[$base .. $base+255] );
869 printf OUTPUT ($y < $#lblist) ? ",\n" : "\n};\n\n";
872 # output the lead byte subtables offsets
875 for (my $x = 0; $x < 256; $x++) { $offsets[$x] = 0; }
876 for (my $x = 0; $x <= $#lblist; $x++) { $offsets[$lblist[$x]] = $x + 1; }
879 # increment all lead bytes offset to take into account the unused table
880 for (my $x = 0; $x <= $#lead_bytes; $x++) { $offsets[$lead_bytes[$x]]++; }
882 printf OUTPUT "static const unsigned char cp2uni_leadbytes[256] =\n";
883 printf OUTPUT "{\n%s\n};\n\n", DUMP_ARRAY( "0x%02x", 0, @offsets );
885 # count the number of unicode->ascii subtables that contain something
889 for (my $i = 0; $i < 65536; $i++)
891 next unless defined $uni2cp[$i];
892 $filled[$i >> 8] = 1;
897 # output all the subtables into a single array
899 printf OUTPUT "static const unsigned short uni2cp_low[%d] =\n{\n", $subtables*256;
900 for (my $y = 0; $y < 256; $y++)
902 next unless $filled[$y];
903 printf OUTPUT " /* 0x%02x00 .. 0x%02xff */\n", $y, $y;
904 printf OUTPUT "%s,\n", DUMP_ARRAY( "0x%04x", $def, @uni2cp[($y<<8) .. ($y<<8)+255] );
906 printf OUTPUT " /* defaults */\n";
907 printf OUTPUT "%s\n};\n\n", DUMP_ARRAY( "0x%04x", 0, ($def) x 256 );
909 # output a table of the offsets of the subtables in the previous array
913 for (my $y = 0; $y < 256; $y++)
915 if ($filled[$y]) { push @offsets, $pos; $pos += 256; }
916 else { push @offsets, ($subtables-1) * 256; }
918 printf OUTPUT "static const unsigned short uni2cp_high[256] =\n";
919 printf OUTPUT "{\n%s\n};\n\n", DUMP_ARRAY( "0x%04x", 0, @offsets );
921 # output the code page descriptor
923 printf OUTPUT "const struct dbcs_table cptable_%03d =\n{\n", $codepage;
924 printf OUTPUT " { %d, 2, 0x%04x, 0x%04x, \"%s\" },\n",
925 $codepage, $def, $defw, $name;
926 printf OUTPUT " cp2uni,\n";
927 printf OUTPUT " cp2uni_leadbytes,\n";
928 printf OUTPUT " uni2cp_low,\n";
929 printf OUTPUT " uni2cp_high,\n";
930 printf OUTPUT " {\n %s\n }\n", DUMP_ARRAY( "0x%02x", 0, @lb_ranges, 0, 0 );
931 printf OUTPUT "};\n";
935 ################################################################
936 # get the list of defined lead byte ranges
942 foreach $i (@lead_bytes) { $list[$i] = 1; }
944 for (my $i = 0; $i < 256; $i++)
948 if (!defined $list[$i]) { push @ranges, $i-1; $on = 0; }
952 if ($list[$i]) { push @ranges, $i; $on = 1; }
955 if ($on) { push @ranges, 0xff; }
960 ################################################################
961 # dump the BiDi mirroring table
962 sub dump_mirroring($)
964 my $filename = shift;
965 my @mirror_table = ();
967 my $INPUT = open_data_file "$UNIDATA/BidiMirroring.txt";
970 next if /^\#/; # skip comments
971 next if /^$/; # skip empty lines
972 next if /\x1a/; # skip ^Z
973 if (/^\s*([0-9a-fA-F]+)\s*;\s*([0-9a-fA-F]+)/)
975 $mirror_table[hex $1] = hex $2;
978 die "malformed line $_";
982 open OUTPUT,">$filename.new" or die "Cannot create $filename";
983 print "Building $filename\n";
984 print OUTPUT "/* Unicode BiDi mirroring */\n";
985 print OUTPUT "/* generated from $UNIDATA/BidiMirroring.txt */\n";
986 print OUTPUT "/* DO NOT EDIT!! */\n\n";
987 print OUTPUT "#include \"wine/unicode.h\"\n\n";
988 DUMP_CASE_TABLE( "wine_mirror_map", @mirror_table );
990 save_file($filename);
994 ################################################################
995 # dump the Arabic shaping table
998 my $filename = shift;
1002 $groups{"No_Joining_Group"} = $next_group++;
1004 my $INPUT = open_data_file "$UNIDATA/ArabicShaping.txt";
1007 next if /^\#/; # skip comments
1008 next if /^\s*$/; # skip empty lines
1009 next if /\x1a/; # skip ^Z
1010 if (/^\s*([0-9a-fA-F]+)\s*;.*;\s*([RLDCUT])\s*;\s*(\w+)/)
1014 $groups{$group} = $next_group++ unless defined $groups{$group};
1015 $joining_table[hex $1] = $joining_types{$type} | ($groups{$group} << 8);
1018 die "malformed line $_";
1022 open OUTPUT,">$filename.new" or die "Cannot create $filename";
1023 print "Building $filename\n";
1024 print OUTPUT "/* Unicode Arabic shaping */\n";
1025 print OUTPUT "/* generated from $UNIDATA/ArabicShaping.txt */\n";
1026 print OUTPUT "/* DO NOT EDIT!! */\n\n";
1027 print OUTPUT "#include \"wine/unicode.h\"\n\n";
1029 dump_simple_mapping( "wine_shaping_table", @joining_table );
1031 print OUTPUT "\nconst unsigned short wine_shaping_forms[256][4] =\n{\n";
1032 for (my $i = 0x600; $i <= 0x6ff; $i++)
1034 printf OUTPUT " { 0x%04x, 0x%04x, 0x%04x, 0x%04x },\n",
1035 ${joining_forms{"isolated"}}[$i] || $i,
1036 ${joining_forms{"final"}}[$i] || $i,
1037 ${joining_forms{"initial"}}[$i] || $i,
1038 ${joining_forms{"medial"}}[$i] || $i;
1040 print OUTPUT "};\n";
1043 save_file($filename);
1047 ################################################################
1048 # dump the case mapping tables
1049 sub DUMP_CASE_MAPPINGS($)
1051 my $filename = shift;
1052 open OUTPUT,">$filename.new" or die "Cannot create $filename";
1053 printf "Building $filename\n";
1054 printf OUTPUT "/* Unicode case mappings */\n";
1055 printf OUTPUT "/* Automatically generated; DO NOT EDIT!! */\n\n";
1056 printf OUTPUT "#include \"wine/unicode.h\"\n\n";
1058 DUMP_CASE_TABLE( "wine_casemap_lower", @tolower_table );
1059 DUMP_CASE_TABLE( "wine_casemap_upper", @toupper_table );
1060 DUMP_CASE_TABLE( "wine_digitmap", @digitmap_table );
1061 DUMP_CASE_TABLE( "wine_compatmap", @compatmap_table );
1063 save_file($filename);
1067 ################################################################
1068 # dump a case mapping table
1069 sub DUMP_CASE_TABLE($@)
1071 my ($name,@table) = @_;
1073 # count the number of sub tables that contain something
1074 # also compute the low and upper populated bounds
1076 my @lowerbounds = ( 0, 0 );
1077 my @upperbounds = ( 0, 255 );
1080 for (my $i = 0; $i < 65536; $i++)
1082 next unless defined $table[$i];
1083 if (!defined $filled[$i >> 8])
1085 $lowerbounds[$index] = $i & 0xff;
1086 $upperbounds[$index] = 0xff - $lowerbounds[$index];
1087 $filled[$i >> 8] = $index * 256 + 512;
1092 $upperbounds[$index-1] = 0xff - ($i & 0xff);
1094 $table[$i] = ($table[$i] - $i) & 0xffff;
1097 # Collapse blocks upwards if possible
1100 for (my $i = 0; $i < 256; $i++)
1102 next unless defined $filled[$i];
1103 if ($upperbounds[$index - 1] > $lowerbounds[$index])
1105 $removed = $removed + $lowerbounds[$index];
1109 $removed = $removed + $upperbounds[$index - 1];
1110 $lowerbounds[$index] = $upperbounds[$index - 1];
1112 $filled[$i] = $filled[$i] - $removed;
1118 printf OUTPUT "const WCHAR %s[%d] =\n", $name, $index * 256 + 512 - $removed;
1119 printf OUTPUT "{\n /* index */\n";
1120 printf OUTPUT "%s,\n", DUMP_ARRAY( "0x%04x", 256, @filled );
1121 printf OUTPUT " /* defaults */\n";
1122 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, (0) x 256 );
1124 for (my $i = 0; $i < 256; $i++)
1126 next unless $filled[$i];
1127 printf OUTPUT ",\n /* 0x%02x%02x .. 0x%02xff */\n", $i, $lowerbounds[$index], $i;
1128 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0,
1129 @table[($i<<8) + $lowerbounds[$index] .. ($i<<8)+255] );
1132 printf OUTPUT "\n};\n";
1135 ################################################################
1136 # dump a simple char -> 16-bit value mapping table
1137 sub dump_simple_mapping($@)
1141 my @array = (0) x 256;
1144 # try to merge table rows
1145 for (my $row = 0; $row < 256; $row++)
1147 my $rowtxt = sprintf "%04x" x 256, @table[($row<<8)..($row<<8)+255];
1148 if (defined($sequences{$rowtxt}))
1150 # reuse an existing row
1151 $array[$row] = $sequences{$rowtxt};
1156 $sequences{$rowtxt} = $array[$row] = $#array + 1;
1157 push @array, @table[($row<<8)..($row<<8)+255];
1161 printf OUTPUT "const unsigned short %s[%d] =\n{\n", $name, $#array+1;
1162 printf OUTPUT " /* offsets */\n%s,\n", DUMP_ARRAY( "0x%04x", 0, @array[0..255] );
1163 printf OUTPUT " /* values */\n%s\n};\n", DUMP_ARRAY( "0x%04x", 0, @array[256..$#array] );
1166 ################################################################
1167 # dump a binary case mapping table in l_intl.nls format
1168 sub dump_binary_case_table(@)
1172 my %difftables_hash = ();
1174 my %offtables2_hash = ();
1175 my @offtables2 = ();
1178 for (my $i = 0; $i < 256; $i++)
1181 for(my $j = 0; $j < 16; $j++) # offset table for xx00-xxFF characters
1184 for (my $k = 0; $k < 16; $k++) # case map table for xxx0-xxxF characters
1186 my $char = ($i<<8) + ($j<<4) + $k;
1187 $difftable[$k] = (defined $table[$char]) ? (($table[$char]-$char) & 0xffff) : 0;
1190 my $diff_key = pack "S*", @difftable;
1191 my $offset3 = $difftables_hash{$diff_key};
1192 if (!defined $offset3)
1194 $offset3 = scalar @difftables;
1195 $difftables_hash{$diff_key} = $offset3;
1196 push @difftables, @difftable;
1198 $offtable2[$j] = $offset3;
1201 my $offtable2_key = pack "S*", @offtable2;
1202 my $offset2 = $offtables2_hash{$offtable2_key};
1203 if (!defined $offset2)
1205 $offset2 = scalar @offtables2;
1206 $offtables2_hash{$offtable2_key} = $offset2;
1207 push @offtables2, \@offtable2;
1209 $offtable[$i] = $offset2;
1213 my $offset = 0x100; # offset of first subtable in words
1216 push @output, 0x10 * $_ + $offset; # offset of subtable in words
1219 $offset = 0x100 + 0x10 * scalar @offtables2; # offset of first difftable in words
1220 foreach(@offtables2)
1225 push @output, $_ + $offset; # offset of difftable in words
1229 my $len = 1 + scalar @output + scalar @difftables;
1230 return pack "S<*", $len, @output, @difftables;
1234 ################################################################
1235 # dump case mappings for l_intl.nls
1236 sub dump_intl_nls($)
1238 my $filename = shift;
1239 open OUTPUT,">$filename.new" or die "Cannot create $filename";
1240 printf "Building $filename\n";
1243 print OUTPUT pack "S<", 1; # version
1244 print OUTPUT dump_binary_case_table( @toupper_table );
1245 print OUTPUT dump_binary_case_table( @tolower_table );
1247 save_file($filename);
1251 ################################################################
1252 # dump the ctype tables
1253 sub DUMP_CTYPE_TABLES($)
1255 my $filename = shift;
1256 open OUTPUT,">$filename.new" or die "Cannot create $filename";
1257 printf "Building $filename\n";
1258 printf OUTPUT "/* Unicode ctype tables */\n";
1259 printf OUTPUT "/* Automatically generated; DO NOT EDIT!! */\n\n";
1260 printf OUTPUT "#include \"wine/unicode.h\"\n\n";
1262 # add the direction in the high 4 bits of the category
1263 for (my $i = 0; $i < 65536; $i++)
1265 $category_table[$i] |= $direction_table[$i] << 12 if defined $direction_table[$i];
1268 dump_simple_mapping( "wine_wctype_table", @category_table );
1271 save_file($filename);
1275 ################################################################
1276 # dump the char composition tables
1277 sub DUMP_COMPOSE_TABLES($)
1279 my $filename = shift;
1281 open OUTPUT,">$filename.new" or die "Cannot create $filename";
1282 printf "Building $filename\n";
1283 printf OUTPUT "/* Unicode char composition */\n";
1284 printf OUTPUT "/* Automatically generated; DO NOT EDIT!! */\n\n";
1285 printf OUTPUT "#include \"wine/unicode.h\"\n\n";
1287 ######### composition table
1290 foreach my $i (@compose_table)
1293 push @{$filled[$comp[1]]}, [ $comp[0], $comp[2] ];
1296 # count how many different second chars we have
1299 for (my $i = 0; $i < 65536; $i++)
1301 next unless defined $filled[$i];
1305 # build the table of second chars and offsets
1307 my $pos = $count + 1;
1309 for (my $i = 0; $i < 65536; $i++)
1311 next unless defined $filled[$i];
1312 push @table, $i, $pos;
1313 $pos += @{$filled[$i]};
1315 # terminator with last position
1316 push @table, 0, $pos;
1317 printf OUTPUT "const WCHAR unicode_compose_table[0x%x] =\n{\n", 2*$pos;
1318 printf OUTPUT " /* second chars + offsets */\n%s", DUMP_ARRAY( "0x%04x", 0, @table );
1320 # build the table of first chars and mappings
1322 for (my $i = 0; $i < 65536; $i++)
1324 next unless defined $filled[$i];
1326 my @list = sort { $a->[0] <=> $b->[0] } @{$filled[$i]};
1327 for (my $j = 0; $j <= $#list; $j++)
1329 push @table, $list[$j][0], $list[$j][1];
1331 printf OUTPUT ",\n /* 0x%04x */\n%s", $i, DUMP_ARRAY( "0x%04x", 0, @table );
1333 printf OUTPUT "\n};\n\nconst unsigned int unicode_compose_table_size = %d;\n\n", $count;
1335 ######### decomposition table
1337 # first determine all the 16-char subsets that contain something
1339 @filled = (0) x 4096;
1340 $pos = 16*2; # for the null subset
1341 for (my $i = 0; $i < 65536; $i++)
1343 next unless defined $decomp_table[$i];
1344 $filled[$i >> 4] = $pos;
1350 # now count the 256-char subsets that contain something
1352 my @filled_idx = (256) x 256;
1354 for (my $i = 0; $i < 4096; $i++)
1356 next unless $filled[$i];
1357 $filled_idx[$i >> 4] = $pos;
1361 my $null_offset = $pos; # null mapping
1364 # add the index offsets to the subsets positions
1366 for (my $i = 0; $i < 4096; $i++)
1368 next unless $filled[$i];
1369 $filled[$i] += $null_offset;
1372 # dump the main index
1374 printf OUTPUT "const WCHAR unicode_decompose_table[%d] =\n", $total;
1375 printf OUTPUT "{\n /* index */\n";
1376 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, @filled_idx );
1377 printf OUTPUT ",\n /* null sub-index */\n%s", DUMP_ARRAY( "0x%04x", 0, ($null_offset) x 16 );
1379 # dump the second-level indexes
1381 for (my $i = 0; $i < 256; $i++)
1383 next unless ($filled_idx[$i] > 256);
1384 my @table = @filled[($i<<4)..($i<<4)+15];
1385 for (my $j = 0; $j < 16; $j++) { $table[$j] ||= $null_offset; }
1386 printf OUTPUT ",\n /* sub-index %02x */\n", $i;
1387 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, @table );
1390 # dump the 16-char subsets
1392 printf OUTPUT ",\n /* null mapping */\n";
1393 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, (0) x 32 );
1395 for (my $i = 0; $i < 4096; $i++)
1397 next unless $filled[$i];
1398 my @table = (0) x 32;
1399 for (my $j = 0; $j < 16; $j++)
1401 if (defined $decomp_table[($i<<4) + $j])
1403 $table[2 * $j] = ${$decomp_table[($i << 4) + $j]}[0];
1404 $table[2 * $j + 1] = ${$decomp_table[($i << 4) + $j]}[1];
1407 printf OUTPUT ",\n /* 0x%03x0 .. 0x%03xf */\n", $i, $i;
1408 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, @table );
1411 printf OUTPUT "\n};\n";
1413 save_file($filename);
1417 ################################################################
1418 # handle a "bestfit" Windows mapping file
1420 sub handle_bestfit_file($$$)
1422 my ($filename, $has_glyphs, $comment) = @_;
1424 my ($codepage, $width, $def, $defw, $count);
1425 my ($lb_cur, $lb_end);
1428 my $INPUT = open_data_file "$MAPPINGS/$filename" or die "Cannot open $filename";
1432 next if /^;/; # skip comments
1433 next if /^\s*$/; # skip empty lines
1434 next if /\x1a/; # skip ^Z
1435 last if /^ENDCODEPAGE/;
1437 if (/^CODEPAGE\s+(\d+)/)
1442 if (/^CPINFO\s+(\d+)\s+0x([0-9a-fA-f]+)\s+0x([0-9a-fA-F]+)/)
1449 if (/^(MBTABLE|WCTABLE|DBCSRANGE|DBCSTABLE)\s+(\d+)/)
1455 if (/^0x([0-9a-fA-F]+)\s+0x([0-9a-fA-F]+)/)
1457 if ($state eq "MBTABLE")
1461 $cp2uni[$cp] = $uni unless defined($cp2uni[$cp]);
1464 if ($state eq "WCTABLE")
1468 $uni2cp[$uni] = $cp unless defined($uni2cp[$uni]);
1471 if ($state eq "DBCSRANGE")
1475 push @lb_ranges, $start, $end;
1476 for (my $i = $start; $i <= $end; $i++)
1478 push @lead_bytes, $i;
1485 if ($state eq "DBCSTABLE")
1489 my $cp = ($lb_cur << 8) | $mb;
1490 $cp2uni[$cp] = $uni unless defined($cp2uni[$cp]);
1493 if (++$lb_cur > $lb_end) { $state = "DBCSRANGE"; }
1498 die "$filename: Unrecognized line $_\n";
1502 my $output = sprintf "c_%03d.c", $codepage;
1503 open OUTPUT,">$output.new" or die "Cannot create $output";
1505 printf "Building %s from %s (%s)\n", $output, $filename, $comment;
1509 printf OUTPUT "/* code page %03d (%s) */\n", $codepage, $comment;
1510 printf OUTPUT "/* generated from $MAPPINGS/$filename */\n";
1511 printf OUTPUT "/* DO NOT EDIT!! */\n\n";
1512 printf OUTPUT "#include \"wine/unicode.h\"\n\n";
1514 if ($width == 1) { dump_sbcs_table( $codepage, $has_glyphs, $comment, $def, $defw ); }
1515 else { dump_dbcs_table( $codepage, $comment, $def, $defw, @lb_ranges ); }
1521 ################################################################
1522 # read an input file and generate the corresponding .c file
1525 my ($codepage,$filename,$has_glyphs,$comment) = @_;
1531 # symbol codepage file is special
1532 if ($codepage == 20932) { READ_JIS0208_FILE "$MAPPINGS/$filename"; }
1533 elsif ($codepage == 20127) { fill_20127_codepage(); }
1534 elsif ($filename =~ /\/bestfit/)
1536 handle_bestfit_file( $filename, $has_glyphs, $comment );
1539 else { READ_FILE "$MAPPINGS/$filename"; }
1541 ADD_DEFAULT_MAPPINGS();
1543 my $output = sprintf "c_%03d.c", $codepage;
1544 open OUTPUT,">$output.new" or die "Cannot create $output";
1546 printf "Building %s from %s (%s)\n", $output, $filename || "hardcoded data", $comment;
1550 printf OUTPUT "/* code page %03d (%s) */\n", $codepage, $comment;
1553 print OUTPUT "/* generated from $MAPPINGS/$filename */\n";
1554 print OUTPUT "/* DO NOT EDIT!! */\n\n";
1558 printf OUTPUT "/* Automatically generated; DO NOT EDIT!! */\n\n";
1560 printf OUTPUT "#include \"wine/unicode.h\"\n\n";
1562 if (!@lead_bytes) { dump_sbcs_table( $codepage, $has_glyphs, $comment, $DEF_CHAR, $DEF_CHAR ); }
1563 else { dump_dbcs_table( $codepage, $comment, $DEF_CHAR, $DEF_CHAR, get_lb_ranges() ); }
1569 ################################################################
1570 # save a file if modified
1574 if (-f $file && !system "cmp $file $file.new >/dev/null")
1580 rename "$file.new", "$file";
1585 ################################################################
1586 # output the list of codepage tables into the cptable.c file
1587 sub OUTPUT_CPTABLE()
1589 my @tables_decl = ();
1591 foreach my $file (@allfiles)
1593 my ($codepage,$filename,$comment) = @$file;
1594 push @tables_decl, sprintf("extern union cptable cptable_%03d;\n",$codepage);
1597 push @tables_decl, sprintf("\nstatic const union cptable * const cptables[%d] =\n{\n",$#allfiles+1);
1598 foreach my $file (@allfiles)
1600 my ($codepage,$filename,$comment) = @$file;
1601 push @tables_decl, sprintf(" &cptable_%03d,\n", $codepage);
1603 push @tables_decl, "};";
1604 REPLACE_IN_FILE( "cptable.c", @tables_decl );
1607 ################################################################
1608 # replace the contents of a file between ### cpmap ### marks
1610 sub REPLACE_IN_FILE($@)
1615 open(FILE,$name) or die "Can't open $name";
1619 last if /\#\#\# cpmap begin \#\#\#/;
1624 if (/\#\#\# cpmap end \#\#\#/) { push @lines, "\n", $_; last; }
1626 push @lines, <FILE>;
1627 open(FILE,">$name.new") or die "Can't modify $name";
1633 ################################################################
1636 READ_DEFAULTS( $DEFAULTS );
1637 DUMP_CASE_MAPPINGS( "casemap.c" );
1638 DUMP_SORTKEYS( "collation.c", READ_SORTKEYS_FILE() );
1639 DUMP_COMPOSE_TABLES( "compose.c" );
1640 DUMP_CTYPE_TABLES( "wctype.c" );
1641 dump_mirroring( "../../dlls/usp10/mirror.c" );
1642 dump_shaping( "../../dlls/usp10/shaping.c" );
1643 dump_intl_nls("../../tools/l_intl.nls");
1645 foreach my $file (@allfiles) { HANDLE_FILE( @{$file} ); }
1652 # compile-command: "./cpmap.pl && make -k"