libwine: Generate Unicode data for Arabic shaping.
[wine/hacks.git] / libs / wine / cpmap.pl
blob89773e8c9e20c1338b526136ead1dd72cbf7d1a6
1 #!/usr/bin/perl -w
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
22 use strict;
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";
28 # Sort keys file
29 my $SORTKEYS = "http://www.unicode.org/reports/tr10/allkeys.txt";
31 # Defaults mapping
32 my $DEFAULTS = "./defaults";
34 # Default char for undefined mappings
35 my $DEF_CHAR = ord '?';
37 my @allfiles =
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)" ]
104 my %ctype =
106 "upper" => 0x0001,
107 "lower" => 0x0002,
108 "digit" => 0x0004,
109 "space" => 0x0008,
110 "punct" => 0x0010,
111 "cntrl" => 0x0020,
112 "blank" => 0x0040,
113 "xdigit" => 0x0080,
114 "alpha" => 0x0100
117 my %categories =
119 "Lu" => $ctype{"alpha"}|$ctype{"upper"}, # Letter, Uppercase
120 "Ll" => $ctype{"alpha"}|$ctype{"lower"}, # Letter, Lowercase
121 "Lt" => $ctype{"alpha"}, # Letter, Titlecase
122 "Mn" => $ctype{"punct"}, # Mark, Non-Spacing
123 "Mc" => $ctype{"punct"}, # Mark, Spacing Combining
124 "Me" => $ctype{"punct"}, # Mark, Enclosing
125 "Nd" => $ctype{"digit"}, # Number, Decimal Digit
126 "Nl" => $ctype{"punct"}, # Number, Letter
127 "No" => $ctype{"punct"}, # Number, Other
128 "Zs" => $ctype{"space"}, # Separator, Space
129 "Zl" => $ctype{"space"}, # Separator, Line
130 "Zp" => $ctype{"space"}, # Separator, Paragraph
131 "Cc" => $ctype{"cntrl"}, # Other, Control
132 "Cf" => 0, # Other, Format
133 "Cs" => 0, # Other, Surrogate
134 "Co" => 0, # Other, Private Use
135 "Cn" => 0, # Other, Not Assigned
136 "Lm" => $ctype{"punct"}, # Letter, Modifier
137 "Lo" => $ctype{"alpha"}, # Letter, Other
138 "Pc" => $ctype{"punct"}, # Punctuation, Connector
139 "Pd" => $ctype{"punct"}, # Punctuation, Dash
140 "Ps" => $ctype{"punct"}, # Punctuation, Open
141 "Pe" => $ctype{"punct"}, # Punctuation, Close
142 "Pi" => $ctype{"punct"}, # Punctuation, Initial quote
143 "Pf" => $ctype{"punct"}, # Punctuation, Final quote
144 "Po" => $ctype{"punct"}, # Punctuation, Other
145 "Sm" => $ctype{"punct"}, # Symbol, Math
146 "Sc" => $ctype{"punct"}, # Symbol, Currency
147 "Sk" => $ctype{"punct"}, # Symbol, Modifier
148 "So" => $ctype{"punct"} # Symbol, Other
151 # a few characters need additional categories that cannot be determined automatically
152 my %special_categories =
154 "xdigit" => [ ord('0')..ord('9'),ord('A')..ord('F'),ord('a')..ord('f'),
155 0xff10..0xff19, 0xff21..0xff26, 0xff41..0xff46 ],
156 "space" => [ 0x09..0x0d, 0x85 ],
157 "blank" => [ 0x09, 0x20, 0xa0, 0x3000, 0xfeff ],
158 "cntrl" => [ 0x070f, 0x180b, 0x180c, 0x180d, 0x180e, 0x200c, 0x200d,
159 0x200e, 0x200f, 0x202a, 0x202b, 0x202c, 0x202d, 0x202e,
160 0x206a, 0x206b, 0x206c, 0x206d, 0x206e, 0x206f, 0xfeff,
161 0xfff9, 0xfffa, 0xfffb ]
164 my %directions =
166 "L" => 1, # Left-to-Right
167 "LRE" => 15, # Left-to-Right Embedding
168 "LRO" => 15, # Left-to-Right Override
169 "R" => 2, # Right-to-Left
170 "AL" => 12, # Right-to-Left Arabic
171 "RLE" => 15, # Right-to-Left Embedding
172 "RLO" => 15, # Right-to-Left Override
173 "PDF" => 15, # Pop Directional Format
174 "EN" => 3, # European Number
175 "ES" => 4, # European Number Separator
176 "ET" => 5, # European Number Terminator
177 "AN" => 6, # Arabic Number
178 "CS" => 7, # Common Number Separator
179 "NSM" => 13, # Non-Spacing Mark
180 "BN" => 14, # Boundary Neutral
181 "B" => 8, # Paragraph Separator
182 "S" => 9, # Segment Separator
183 "WS" => 10, # Whitespace
184 "ON" => 11 # Other Neutrals
187 my %joining_types =
189 "U" => 0, # Non_Joining
190 "T" => 1, # Transparent
191 "R" => 2, # Right_Joining
192 "L" => 3, # Left_Joining
193 "D" => 4, # Dual_Joining
194 "C" => 5, # Join_Causing
197 my @cp2uni = ();
198 my @lead_bytes = ();
199 my @uni2cp = ();
200 my @unicode_defaults = ();
201 my @unicode_aliases = ();
202 my @tolower_table = ();
203 my @toupper_table = ();
204 my @digitmap_table = ();
205 my @compatmap_table = ();
206 my @category_table = (0) x 65536;
207 my @joining_table = (0) x 65536;
208 my @direction_table = ();
209 my @decomp_table = ();
210 my @compose_table = ();
212 my %joining_forms =
214 "isolated" => [],
215 "final" => [],
216 "initial" => [],
217 "medial" => []
220 ################################################################
221 # fetch a unicode.org file and open it
222 sub open_data_file($)
224 my $url = shift;
225 (my $name = $url) =~ s/^.*\///;
226 local *FILE;
227 unless (-f "data/$name")
229 print "Fetching $url...\n";
230 mkdir "data";
231 !system "wget", "-q", "-O", "data/$name", $url or die "cannot fetch $url";
233 open FILE, "<data/$name" or die "cannot open data/$name";
234 return *FILE;
237 ################################################################
238 # read in the defaults file
239 sub READ_DEFAULTS($)
241 my $filename = shift;
242 my $start;
244 # first setup a few default mappings
246 open DEFAULTS, "$filename" or die "Cannot open $filename";
247 print "Loading $filename\n";
248 while (<DEFAULTS>)
250 next if /^\#/; # skip comments
251 next if /^$/; # skip empty lines
252 if (/^(([0-9a-fA-F]+)(,[0-9a-fA-F]+)*)\s+([0-9a-fA-F]+|'.'|none)\s+(\#.*)?/)
254 my @src = map hex, split /,/,$1;
255 my $dst = $4;
256 my $comment = $5;
257 if ($#src > 0) { push @unicode_aliases, \@src; }
258 next if ($dst eq "none");
259 $dst = ($dst =~ /\'.\'/) ? ord substr($dst,1,1) : hex $dst;
260 foreach my $src (@src)
262 die "Duplicate value" if defined($unicode_defaults[$src]);
263 $unicode_defaults[$src] = $dst;
265 next;
267 die "Unrecognized line $_\n";
269 close DEFAULTS;
271 # now build mappings from the decomposition field of the Unicode database
273 my $UNICODE_DATA = open_data_file "$UNIDATA/UnicodeData.txt";
274 while (<$UNICODE_DATA>)
276 # Decode the fields ...
277 my ($code, $name, $cat, $comb, $bidi,
278 $decomp, $dec, $dig, $num, $mirror,
279 $oldname, $comment, $upper, $lower, $title) = split /;/;
280 my $dst;
281 my $src = hex $code;
283 die "unknown category $cat" unless defined $categories{$cat};
284 die "unknown directionality $bidi" unless defined $directions{$bidi};
286 $category_table[$src] = $categories{$cat};
287 $direction_table[$src] = $directions{$bidi};
288 $joining_table[$src] = $joining_types{"T"} if $cat eq "Mn" || $cat eq "Me" || $cat eq "Cf";
290 if ($lower ne "")
292 $tolower_table[$src] = hex $lower;
293 $category_table[$src] |= $ctype{"upper"}|$ctype{"alpha"};
295 if ($upper ne "")
297 $toupper_table[$src] = hex $upper;
298 $category_table[$src] |= $ctype{"lower"}|$ctype{"alpha"};
300 if ($dec ne "")
302 $category_table[$src] |= $ctype{"digit"};
304 if ($dig ne "")
306 $digitmap_table[$src] = ord $dig;
309 # copy the category and direction for everything between First/Last pairs
310 if ($name =~ /, First>/) { $start = $src; }
311 if ($name =~ /, Last>/)
313 while ($start < $src)
315 $category_table[$start] = $category_table[$src];
316 $direction_table[$start] = $direction_table[$src];
317 $start++;
321 next if $decomp eq ""; # no decomposition, skip it
323 if ($decomp =~ /^<([a-zA-Z]+)>\s+([0-9a-fA-F]+)$/)
325 # decomposition of the form "<foo> 1234" -> use char if type is known
326 if (($src >= 0xf900 && $src < 0xfb00) || ($src >= 0xfe30 && $src < 0xfffd))
328 # Single char decomposition in the compatibility range
329 $compatmap_table[$src] = hex $2;
331 if ($1 eq "isolated" || $1 eq "final" || $1 eq "initial" || $1 eq "medial")
333 ${joining_forms{$1}}[hex $2] = $src;
334 next;
336 next unless ($1 eq "font" ||
337 $1 eq "noBreak" ||
338 $1 eq "circle" ||
339 $1 eq "super" ||
340 $1 eq "sub" ||
341 $1 eq "wide" ||
342 $1 eq "narrow" ||
343 $1 eq "compat" ||
344 $1 eq "small");
345 $dst = hex $2;
347 elsif ($decomp =~ /^<compat>\s+0020\s+([0-9a-fA-F]+)/)
349 # decomposition "<compat> 0020 1234" -> combining accent
350 $dst = hex $1;
352 elsif ($decomp =~ /^([0-9a-fA-F]+)/)
354 # decomposition contains only char values without prefix -> use first char
355 $dst = hex $1;
356 $category_table[$src] |= $category_table[$dst] if defined $category_table[$dst];
357 # store decomposition if it contains two chars
358 if ($decomp =~ /^([0-9a-fA-F]+)\s+([0-9a-fA-F]+)$/)
360 $decomp_table[$src] = [ hex $1, hex $2 ];
361 push @compose_table, [ hex $1, hex $2, $src ];
363 elsif ($decomp =~ /^(<[a-z]+>\s)*([0-9a-fA-F]+)$/ &&
364 (($src >= 0xf900 && $src < 0xfb00) || ($src >= 0xfe30 && $src < 0xfffd)))
366 # Single char decomposition in the compatibility range
367 $compatmap_table[$src] = hex $2;
370 else
372 next;
375 next if defined($unicode_defaults[$src]); # may have been set in the defaults file
377 # check for loops
378 for (my $i = $dst; ; $i = $unicode_defaults[$i])
380 die sprintf("loop detected for %04x -> %04x",$src,$dst) if $i == $src;
381 last unless defined($unicode_defaults[$i]);
383 $unicode_defaults[$src] = $dst;
385 close $UNICODE_DATA;
387 # patch the category of some special characters
389 foreach my $cat (keys %special_categories)
391 my $flag = $ctype{$cat};
392 foreach my $i (@{$special_categories{$cat}}) { $category_table[$i] |= $flag; }
397 ################################################################
398 # parse the input file
399 sub READ_FILE($)
401 my $name = shift;
402 my $INPUT = open_data_file $name;
404 while (<$INPUT>)
406 next if /^\#/; # skip comments
407 next if /^$/; # skip empty lines
408 next if /\x1a/; # skip ^Z
409 next if (/^0x([0-9a-fA-F]+)\s+\#UNDEFINED/); # undefined char
411 if (/^0x([0-9a-fA-F]+)\s+\#DBCS LEAD BYTE/)
413 my $cp = hex $1;
414 push @lead_bytes,$cp;
415 $cp2uni[$cp] = 0;
416 next;
418 if (/^0x([0-9a-fA-F]+)\s+0x([0-9a-fA-F]+)\s+(\#.*)?/)
420 my $cp = hex $1;
421 my $uni = hex $2;
422 $cp2uni[$cp] = $uni unless defined($cp2uni[$cp]);
423 $uni2cp[$uni] = $cp unless defined($uni2cp[$uni]);
424 if ($cp > 0xff && !defined($cp2uni[$cp >> 8]))
426 push @lead_bytes,$cp >> 8;
427 $cp2uni[$cp >> 8] = 0;
429 next;
431 die "$name: Unrecognized line $_\n";
433 close $INPUT;
437 ################################################################
438 # fill input data for the 20127 (us-ascii) codepage
439 sub fill_20127_codepage()
441 for (my $i = 0; $i < 128; $i++) { $cp2uni[$i] = $uni2cp[$i] = $i; }
442 for (my $i = 128; $i < 256; $i++) { $cp2uni[$i] = $i & 0x7f; }
445 ################################################################
446 # get a mapping including glyph chars for MB_USEGLYPHCHARS
448 sub get_glyphs_mapping(@)
450 $_[0x01] = 0x263a; # (WHITE SMILING FACE)
451 $_[0x02] = 0x263b; # (BLACK SMILING FACE)
452 $_[0x03] = 0x2665; # (BLACK HEART SUIT)
453 $_[0x04] = 0x2666; # (BLACK DIAMOND SUIT)
454 $_[0x05] = 0x2663; # (BLACK CLUB SUIT)
455 $_[0x06] = 0x2660; # (BLACK SPADE SUIT)
456 $_[0x07] = 0x2022; # (BULLET)
457 $_[0x08] = 0x25d8; # (INVERSE BULLET)
458 $_[0x09] = 0x25cb; # (WHITE CIRCLE)
459 $_[0x0a] = 0x25d9; # (INVERSE WHITE CIRCLE)
460 $_[0x0b] = 0x2642; # (MALE SIGN)
461 $_[0x0c] = 0x2640; # (FEMALE SIGN)
462 $_[0x0d] = 0x266a; # (EIGHTH NOTE)
463 $_[0x0e] = 0x266b; # (BEAMED EIGHTH NOTES)
464 $_[0x0f] = 0x263c; # (WHITE SUN WITH RAYS)
465 $_[0x10] = 0x25ba; # (BLACK RIGHT-POINTING POINTER)
466 $_[0x11] = 0x25c4; # (BLACK LEFT-POINTING POINTER)
467 $_[0x12] = 0x2195; # (UP DOWN ARROW)
468 $_[0x13] = 0x203c; # (DOUBLE EXCLAMATION MARK)
469 $_[0x14] = 0x00b6; # (PILCROW SIGN)
470 $_[0x15] = 0x00a7; # (SECTION SIGN)
471 $_[0x16] = 0x25ac; # (BLACK RECTANGLE)
472 $_[0x17] = 0x21a8; # (UP DOWN ARROW WITH BASE)
473 $_[0x18] = 0x2191; # (UPWARDS ARROW)
474 $_[0x19] = 0x2193; # (DOWNWARDS ARROW)
475 $_[0x1a] = 0x2192; # (RIGHTWARDS ARROW)
476 $_[0x1b] = 0x2190; # (LEFTWARDS ARROW)
477 $_[0x1c] = 0x221f; # (RIGHT ANGLE)
478 $_[0x1d] = 0x2194; # (LEFT RIGHT ARROW)
479 $_[0x1e] = 0x25b2; # (BLACK UP-POINTING TRIANGLE)
480 $_[0x1f] = 0x25bc; # (BLACK DOWN-POINTING TRIANGLE)
481 $_[0x7f] = 0x2302; # (HOUSE)
482 return @_;
485 ################################################################
486 # build EUC-JP table from the JIS 0208 file
487 # FIXME: for proper EUC-JP we should probably read JIS 0212 too
488 # but this would require 3-byte DBCS characters
489 sub READ_JIS0208_FILE($)
491 my $name = shift;
493 # ASCII chars
494 for (my $i = 0x00; $i <= 0x7f; $i++)
496 $cp2uni[$i] = $i;
497 $uni2cp[$i] = $i;
500 # JIS X 0201 right plane
501 for (my $i = 0xa1; $i <= 0xdf; $i++)
503 $cp2uni[0x8e00 + $i] = 0xfec0 + $i;
504 $uni2cp[0xfec0 + $i] = 0x8e00 + $i;
507 # lead bytes
508 foreach my $i (0x8e, 0x8f, 0xa1 .. 0xfe)
510 push @lead_bytes,$i;
511 $cp2uni[$i] = 0;
514 # undefined chars
515 foreach my $i (0x80 .. 0x8d, 0x90 .. 0xa0, 0xff)
517 $cp2uni[$i] = $DEF_CHAR;
520 # Shift-JIS compatibility
521 $uni2cp[0x00a5] = 0x5c;
522 $uni2cp[0x203e] = 0x7e;
524 # Fix backslash conversion
525 $cp2uni[0xa1c0] = 0xff3c;
526 $uni2cp[0xff3c] = 0xa1c0;
528 my $INPUT = open_data_file $name;
529 while (<$INPUT>)
531 next if /^\#/; # skip comments
532 next if /^$/; # skip empty lines
533 next if /\x1a/; # skip ^Z
534 if (/^0x[0-9a-fA-F]+\s+0x([0-9a-fA-F]+)\s+0x([0-9a-fA-F]+)\s+(\#.*)?/)
536 my $cp = 0x8080 + hex $1;
537 my $uni = hex $2;
538 $cp2uni[$cp] = $uni unless defined($cp2uni[$cp]);
539 $uni2cp[$uni] = $cp unless defined($uni2cp[$uni]);
540 next;
542 die "$name: Unrecognized line $_\n";
544 close $INPUT;
548 ################################################################
549 # build the sort keys table
550 sub READ_SORTKEYS_FILE()
552 my @sortkeys = ();
553 for (my $i = 0; $i < 65536; $i++) { $sortkeys[$i] = [ -1, 0, 0, 0, 0 ] };
555 my $INPUT = open_data_file $SORTKEYS;
556 while (<$INPUT>)
558 next if /^\#/; # skip comments
559 next if /^$/; # skip empty lines
560 next if /\x1a/; # skip ^Z
561 next if /^\@version/; # skip @version header
562 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]+)\]/)
564 my ($uni,$variable) = (hex $1, $2);
565 next if $uni > 65535;
566 $sortkeys[$uni] = [ $uni, hex $3, hex $4, hex $5, hex $6 ];
567 next;
569 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]+)\]/)
571 # multiple character sequence, ignored for now
572 next;
574 die "$SORTKEYS: Unrecognized line $_\n";
576 close $INPUT;
578 # compress the keys to 32 bit:
579 # key 1 to 16 bits, key 2 to 8 bits, key 3 to 4 bits, key 4 to 1 bit
581 @sortkeys = sort { ${$a}[1] <=> ${$b}[1] or
582 ${$a}[2] <=> ${$b}[2] or
583 ${$a}[3] <=> ${$b}[3] or
584 ${$a}[4] <=> ${$b}[4] or
585 $a cmp $b; } @sortkeys;
587 my ($n2, $n3) = (1, 1);
588 my @keys = (-1, -1, -1, -1, -1 );
589 my @flatkeys = ();
591 for (my $i = 0; $i < 65536; $i++)
593 my @current = @{$sortkeys[$i]};
594 next if $current[0] == -1;
595 if ($current[1] == $keys[1])
597 if ($current[2] == $keys[2])
599 if ($current[3] == $keys[3])
601 # nothing
603 else
605 $keys[3] = $current[3];
606 $n3++;
607 die if ($n3 >= 16);
610 else
612 $keys[2] = $current[2];
613 $keys[3] = $current[3];
614 $n2++;
615 $n3 = 1;
616 die if ($n2 >= 256);
619 else
621 $keys[1] = $current[1];
622 $keys[2] = $current[2];
623 $keys[3] = $current[3];
624 $n2 = 1;
625 $n3 = 1;
628 if ($current[2]) { $current[2] = $n2; }
629 if ($current[3]) { $current[3] = $n3; }
630 if ($current[4]) { $current[4] = 1; }
632 $flatkeys[$current[0]] = ($current[1] << 16) | ($current[2] << 8) | ($current[3] << 4) | $current[4];
634 return @flatkeys;
638 ################################################################
639 # build the sort keys table
640 sub DUMP_SORTKEYS($@)
642 my ($filename, @keys) = @_;
644 # count the number of 256-key ranges that contain something
646 my @offsets = ();
647 my $ranges = 2;
648 for (my $i = 0; $i < 256; $i++) { $offsets[$i] = 256; }
649 for (my $i = 0; $i < 65536; $i++)
651 next unless defined $keys[$i];
652 $offsets[$i >> 8] = $ranges * 256;
653 $ranges++;
654 $i |= 255;
657 # output the range offsets
659 open OUTPUT,">$filename.new" or die "Cannot create $filename";
660 printf "Building $filename\n";
661 printf OUTPUT "/* Unicode collation element table */\n";
662 printf OUTPUT "/* generated from %s */\n", $SORTKEYS;
663 printf OUTPUT "/* DO NOT EDIT!! */\n\n";
665 printf OUTPUT "const unsigned int collation_table[%d] =\n{\n", $ranges*256;
666 printf OUTPUT " /* index */\n";
667 printf OUTPUT "%s,\n", DUMP_ARRAY( "0x%08x", 0, @offsets );
669 # output the default values
671 printf OUTPUT " /* defaults */\n";
672 printf OUTPUT "%s", DUMP_ARRAY( "0x%08x", 0, (0xffffffff) x 256 );
674 # output all the key ranges
676 for (my $i = 0; $i < 256; $i++)
678 next if $offsets[$i] == 256;
679 printf OUTPUT ",\n /* 0x%02x00 .. 0x%02xff */\n", $i, $i;
680 printf OUTPUT "%s", DUMP_ARRAY( "0x%08x", 0xffffffff, @keys[($i<<8) .. ($i<<8)+255] );
682 printf OUTPUT "\n};\n";
683 close OUTPUT;
684 save_file($filename);
688 ################################################################
689 # add default mappings once the file had been read
690 sub ADD_DEFAULT_MAPPINGS()
692 # Apply aliases
694 foreach my $alias (@unicode_aliases)
696 my $target = undef;
697 foreach my $src (@$alias)
699 if (defined($uni2cp[$src]))
701 $target = $uni2cp[$src];
702 last;
705 next unless defined($target);
707 # At least one char of the alias set is defined, set the others to the same value
708 foreach my $src (@$alias)
710 $uni2cp[$src] = $target unless defined($uni2cp[$src]);
714 # For every src -> target mapping in the defaults table,
715 # make uni2cp[src] = uni2cp[target] if uni2cp[target] is defined
717 for (my $src = 0; $src < 65536; $src++)
719 next if defined($uni2cp[$src]); # source has a definition already
720 next unless defined($unicode_defaults[$src]); # no default for this char
721 my $target = $unicode_defaults[$src];
723 # do a recursive mapping until we find a target char that is defined
724 while (!defined($uni2cp[$target]) &&
725 defined($unicode_defaults[$target])) { $target = $unicode_defaults[$target]; }
727 if (defined($uni2cp[$target])) { $uni2cp[$src] = $uni2cp[$target]; }
730 # Add an identity mapping for all undefined chars
732 for (my $i = 0; $i < 256; $i++)
734 next if defined($cp2uni[$i]);
735 next if defined($uni2cp[$i]);
736 $cp2uni[$i] = $uni2cp[$i] = $i;
740 ################################################################
741 # dump an array of integers
742 sub DUMP_ARRAY($$@)
744 my ($format,$default,@array) = @_;
745 my $i;
746 my $ret = " ";
747 for ($i = 0; $i < $#array; $i++)
749 $ret .= sprintf($format, defined $array[$i] ? $array[$i] : $default);
750 $ret .= (($i % 8) != 7) ? ", " : ",\n ";
752 $ret .= sprintf($format, defined $array[$i] ? $array[$i] : $default);
753 return $ret;
756 ################################################################
757 # dump an SBCS mapping table
758 sub dump_sbcs_table($$$$$)
760 my ($codepage, $has_glyphs, $name, $def, $defw) = @_;
761 my $i;
763 # output the ascii->unicode table
765 if ($has_glyphs)
767 printf OUTPUT "static const WCHAR cp2uni[512] =\n";
768 printf OUTPUT "{\n%s", DUMP_ARRAY( "0x%04x", $defw, @cp2uni[0 .. 255] );
769 printf OUTPUT ",\n /* glyphs */\n%s\n};\n\n",
770 DUMP_ARRAY( "0x%04x", $defw, get_glyphs_mapping(@cp2uni[0 .. 255]) );
772 else
774 printf OUTPUT "static const WCHAR cp2uni[256] =\n";
775 printf OUTPUT "{\n%s\n};\n\n", DUMP_ARRAY( "0x%04x", $defw, @cp2uni[0 .. 255] );
778 # count the number of unicode->ascii subtables that contain something
780 my @filled = ();
781 my $subtables = 1;
782 for (my $i = 0; $i < 65536; $i++)
784 next unless defined $uni2cp[$i];
785 $filled[$i >> 8] = 1;
786 $subtables++;
787 $i |= 255;
790 # output all the subtables into a single array
792 printf OUTPUT "static const unsigned char uni2cp_low[%d] =\n{\n", $subtables*256;
793 for (my $i = 0; $i < 256; $i++)
795 next unless $filled[$i];
796 printf OUTPUT " /* 0x%02x00 .. 0x%02xff */\n", $i, $i;
797 printf OUTPUT "%s,\n", DUMP_ARRAY( "0x%02x", $def, @uni2cp[($i<<8) .. ($i<<8)+255] );
799 printf OUTPUT " /* defaults */\n";
800 printf OUTPUT "%s\n};\n\n", DUMP_ARRAY( "0x%02x", 0, ($def) x 256 );
802 # output a table of the offsets of the subtables in the previous array
804 my $pos = 0;
805 my @offsets = ();
806 for (my $i = 0; $i < 256; $i++)
808 if ($filled[$i]) { push @offsets, $pos; $pos += 256; }
809 else { push @offsets, ($subtables-1) * 256; }
811 printf OUTPUT "static const unsigned short uni2cp_high[256] =\n";
812 printf OUTPUT "{\n%s\n};\n\n", DUMP_ARRAY( "0x%04x", 0, @offsets );
814 # output the code page descriptor
816 printf OUTPUT "const struct sbcs_table cptable_%03d =\n{\n", $codepage;
817 printf OUTPUT " { %d, 1, 0x%04x, 0x%04x, \"%s\" },\n",
818 $codepage, $def, $defw, $name;
819 printf OUTPUT " cp2uni,\n";
820 if ($has_glyphs) { printf OUTPUT " cp2uni + 256,\n"; }
821 else { printf OUTPUT " cp2uni,\n"; }
822 printf OUTPUT " uni2cp_low,\n";
823 printf OUTPUT " uni2cp_high\n};\n";
827 ################################################################
828 # dump a DBCS mapping table
829 sub dump_dbcs_table($$$$@)
831 my ($codepage, $name, $def, $defw, @lb_ranges) = @_;
833 # build a list of lead bytes that are actually used
835 my @lblist = ();
836 LBLOOP: for (my $y = 0; $y <= $#lead_bytes; $y++)
838 my $base = $lead_bytes[$y] << 8;
839 for (my $x = 0; $x < 256; $x++)
841 if (defined $cp2uni[$base+$x])
843 push @lblist,$lead_bytes[$y];
844 next LBLOOP;
848 my $unused = ($#lead_bytes > $#lblist);
850 # output the ascii->unicode table for the single byte chars
852 printf OUTPUT "static const WCHAR cp2uni[%d] =\n", 256 * ($#lblist + 2 + $unused);
853 printf OUTPUT "{\n%s,\n", DUMP_ARRAY( "0x%04x", $defw, @cp2uni[0 .. 255] );
855 # output the default table for unused lead bytes
857 if ($unused)
859 printf OUTPUT " /* unused lead bytes */\n";
860 printf OUTPUT "%s,\n", DUMP_ARRAY( "0x%04x", 0, ($defw) x 256 );
863 # output the ascii->unicode table for each DBCS lead byte
865 for (my $y = 0; $y <= $#lblist; $y++)
867 my $base = $lblist[$y] << 8;
868 printf OUTPUT " /* lead byte %02x */\n", $lblist[$y];
869 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", $defw, @cp2uni[$base .. $base+255] );
870 printf OUTPUT ($y < $#lblist) ? ",\n" : "\n};\n\n";
873 # output the lead byte subtables offsets
875 my @offsets = ();
876 for (my $x = 0; $x < 256; $x++) { $offsets[$x] = 0; }
877 for (my $x = 0; $x <= $#lblist; $x++) { $offsets[$lblist[$x]] = $x + 1; }
878 if ($unused)
880 # increment all lead bytes offset to take into account the unused table
881 for (my $x = 0; $x <= $#lead_bytes; $x++) { $offsets[$lead_bytes[$x]]++; }
883 printf OUTPUT "static const unsigned char cp2uni_leadbytes[256] =\n";
884 printf OUTPUT "{\n%s\n};\n\n", DUMP_ARRAY( "0x%02x", 0, @offsets );
886 # count the number of unicode->ascii subtables that contain something
888 my @filled = ();
889 my $subtables = 1;
890 for (my $i = 0; $i < 65536; $i++)
892 next unless defined $uni2cp[$i];
893 $filled[$i >> 8] = 1;
894 $subtables++;
895 $i |= 255;
898 # output all the subtables into a single array
900 printf OUTPUT "static const unsigned short uni2cp_low[%d] =\n{\n", $subtables*256;
901 for (my $y = 0; $y < 256; $y++)
903 next unless $filled[$y];
904 printf OUTPUT " /* 0x%02x00 .. 0x%02xff */\n", $y, $y;
905 printf OUTPUT "%s,\n", DUMP_ARRAY( "0x%04x", $def, @uni2cp[($y<<8) .. ($y<<8)+255] );
907 printf OUTPUT " /* defaults */\n";
908 printf OUTPUT "%s\n};\n\n", DUMP_ARRAY( "0x%04x", 0, ($def) x 256 );
910 # output a table of the offsets of the subtables in the previous array
912 my $pos = 0;
913 @offsets = ();
914 for (my $y = 0; $y < 256; $y++)
916 if ($filled[$y]) { push @offsets, $pos; $pos += 256; }
917 else { push @offsets, ($subtables-1) * 256; }
919 printf OUTPUT "static const unsigned short uni2cp_high[256] =\n";
920 printf OUTPUT "{\n%s\n};\n\n", DUMP_ARRAY( "0x%04x", 0, @offsets );
922 # output the code page descriptor
924 printf OUTPUT "const struct dbcs_table cptable_%03d =\n{\n", $codepage;
925 printf OUTPUT " { %d, 2, 0x%04x, 0x%04x, \"%s\" },\n",
926 $codepage, $def, $defw, $name;
927 printf OUTPUT " cp2uni,\n";
928 printf OUTPUT " cp2uni_leadbytes,\n";
929 printf OUTPUT " uni2cp_low,\n";
930 printf OUTPUT " uni2cp_high,\n";
931 printf OUTPUT " {\n %s\n }\n", DUMP_ARRAY( "0x%02x", 0, @lb_ranges, 0, 0 );
932 printf OUTPUT "};\n";
936 ################################################################
937 # get the list of defined lead byte ranges
938 sub get_lb_ranges()
940 my @list = ();
941 my @ranges = ();
942 my $i = 0;
943 foreach $i (@lead_bytes) { $list[$i] = 1; }
944 my $on = 0;
945 for (my $i = 0; $i < 256; $i++)
947 if ($on)
949 if (!defined $list[$i]) { push @ranges, $i-1; $on = 0; }
951 else
953 if ($list[$i]) { push @ranges, $i; $on = 1; }
956 if ($on) { push @ranges, 0xff; }
957 return @ranges;
961 ################################################################
962 # dump the BiDi mirroring table
963 sub dump_mirroring($)
965 my $filename = shift;
966 my @mirror_table = ();
968 my $INPUT = open_data_file "$UNIDATA/BidiMirroring.txt";
969 while (<$INPUT>)
971 next if /^\#/; # skip comments
972 next if /^$/; # skip empty lines
973 next if /\x1a/; # skip ^Z
974 if (/^\s*([0-9a-fA-F]+)\s*;\s*([0-9a-fA-F]+)/)
976 $mirror_table[hex $1] = hex $2;
977 next;
979 die "malformed line $_";
981 close $INPUT;
983 open OUTPUT,">$filename.new" or die "Cannot create $filename";
984 print "Building $filename\n";
985 print OUTPUT "/* Unicode BiDi mirroring */\n";
986 print OUTPUT "/* generated from $UNIDATA/BidiMirroring.txt */\n";
987 print OUTPUT "/* DO NOT EDIT!! */\n\n";
988 print OUTPUT "#include \"wine/unicode.h\"\n\n";
989 DUMP_CASE_TABLE( "wine_mirror_map", @mirror_table );
990 close OUTPUT;
991 save_file($filename);
995 ################################################################
996 # dump the Arabic shaping table
997 sub dump_shaping($)
999 my $filename = shift;
1000 my %groups;
1001 my $next_group = 0;
1003 $groups{"No_Joining_Group"} = $next_group++;
1005 my $INPUT = open_data_file "$UNIDATA/ArabicShaping.txt";
1006 while (<$INPUT>)
1008 next if /^\#/; # skip comments
1009 next if /^\s*$/; # skip empty lines
1010 next if /\x1a/; # skip ^Z
1011 if (/^\s*([0-9a-fA-F]+)\s*;.*;\s*([RLDCUT])\s*;\s*(\w+)/)
1013 my $type = $2;
1014 my $group = $3;
1015 $groups{$group} = $next_group++ unless defined $groups{$group};
1016 $joining_table[hex $1] = $joining_types{$type} | ($groups{$group} << 8);
1017 next;
1019 die "malformed line $_";
1021 close $INPUT;
1023 open OUTPUT,">$filename.new" or die "Cannot create $filename";
1024 print "Building $filename\n";
1025 print OUTPUT "/* Unicode Arabic shaping */\n";
1026 print OUTPUT "/* generated from $UNIDATA/ArabicShaping.txt */\n";
1027 print OUTPUT "/* DO NOT EDIT!! */\n\n";
1028 print OUTPUT "#include \"wine/unicode.h\"\n\n";
1030 dump_simple_mapping( "wine_shaping_table", @joining_table );
1032 print OUTPUT "\nconst unsigned short wine_shaping_forms[256][4] =\n{\n";
1033 for (my $i = 0x600; $i <= 0x6ff; $i++)
1035 printf OUTPUT " { 0x%04x, 0x%04x, 0x%04x, 0x%04x },\n",
1036 ${joining_forms{"isolated"}}[$i] || $i,
1037 ${joining_forms{"final"}}[$i] || $i,
1038 ${joining_forms{"initial"}}[$i] || $i,
1039 ${joining_forms{"medial"}}[$i] || $i;
1041 print OUTPUT "};\n";
1043 close OUTPUT;
1044 save_file($filename);
1048 ################################################################
1049 # dump the case mapping tables
1050 sub DUMP_CASE_MAPPINGS($)
1052 my $filename = shift;
1053 open OUTPUT,">$filename.new" or die "Cannot create $filename";
1054 printf "Building $filename\n";
1055 printf OUTPUT "/* Unicode case mappings */\n";
1056 printf OUTPUT "/* Automatically generated; DO NOT EDIT!! */\n\n";
1057 printf OUTPUT "#include \"wine/unicode.h\"\n\n";
1059 DUMP_CASE_TABLE( "wine_casemap_lower", @tolower_table );
1060 DUMP_CASE_TABLE( "wine_casemap_upper", @toupper_table );
1061 DUMP_CASE_TABLE( "wine_digitmap", @digitmap_table );
1062 DUMP_CASE_TABLE( "wine_compatmap", @compatmap_table );
1063 close OUTPUT;
1064 save_file($filename);
1068 ################################################################
1069 # dump a case mapping table
1070 sub DUMP_CASE_TABLE($@)
1072 my ($name,@table) = @_;
1074 # count the number of sub tables that contain something
1075 # also compute the low and upper populated bounds
1077 my @lowerbounds = ( 0, 0 );
1078 my @upperbounds = ( 0, 255 );
1079 my $index = 0;
1080 my @filled = ();
1081 for (my $i = 0; $i < 65536; $i++)
1083 next unless defined $table[$i];
1084 if (!defined $filled[$i >> 8])
1086 $lowerbounds[$index] = $i & 0xff;
1087 $upperbounds[$index] = 0xff - $lowerbounds[$index];
1088 $filled[$i >> 8] = $index * 256 + 512;
1089 $index++;
1091 else
1093 $upperbounds[$index-1] = 0xff - ($i & 0xff);
1095 $table[$i] = ($table[$i] - $i) & 0xffff;
1098 # Collapse blocks upwards if possible
1099 my $removed = 0;
1100 $index = 0;
1101 for (my $i = 0; $i < 256; $i++)
1103 next unless defined $filled[$i];
1104 if ($upperbounds[$index - 1] > $lowerbounds[$index])
1106 $removed = $removed + $lowerbounds[$index];
1108 else
1110 $removed = $removed + $upperbounds[$index - 1];
1111 $lowerbounds[$index] = $upperbounds[$index - 1];
1113 $filled[$i] = $filled[$i] - $removed;
1114 $index++;
1117 # dump the table
1119 printf OUTPUT "const WCHAR %s[%d] =\n", $name, $index * 256 + 512 - $removed;
1120 printf OUTPUT "{\n /* index */\n";
1121 printf OUTPUT "%s,\n", DUMP_ARRAY( "0x%04x", 256, @filled );
1122 printf OUTPUT " /* defaults */\n";
1123 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, (0) x 256 );
1124 $index = 0;
1125 for (my $i = 0; $i < 256; $i++)
1127 next unless $filled[$i];
1128 printf OUTPUT ",\n /* 0x%02x%02x .. 0x%02xff */\n", $i, $lowerbounds[$index], $i;
1129 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0,
1130 @table[($i<<8) + $lowerbounds[$index] .. ($i<<8)+255] );
1131 $index++;
1133 printf OUTPUT "\n};\n";
1136 ################################################################
1137 # dump a simple char -> 16-bit value mapping table
1138 sub dump_simple_mapping($@)
1140 my $name = shift;
1141 my @table = @_;
1142 my @array = (0) x 256;
1143 my %sequences;
1145 # try to merge table rows
1146 for (my $row = 0; $row < 256; $row++)
1148 my $rowtxt = sprintf "%04x" x 256, @table[($row<<8)..($row<<8)+255];
1149 if (defined($sequences{$rowtxt}))
1151 # reuse an existing row
1152 $array[$row] = $sequences{$rowtxt};
1154 else
1156 # create a new row
1157 $sequences{$rowtxt} = $array[$row] = $#array + 1;
1158 push @array, @table[($row<<8)..($row<<8)+255];
1162 printf OUTPUT "const unsigned short %s[%d] =\n{\n", $name, $#array+1;
1163 printf OUTPUT " /* offsets */\n%s,\n", DUMP_ARRAY( "0x%04x", 0, @array[0..255] );
1164 printf OUTPUT " /* values */\n%s\n};\n", DUMP_ARRAY( "0x%04x", 0, @array[256..$#array] );
1167 ################################################################
1168 # dump a binary case mapping table in l_intl.nls format
1169 sub dump_binary_case_table(@)
1171 my (@table) = @_;
1173 my %difftables_hash = ();
1174 my @difftables;
1175 my %offtables2_hash = ();
1176 my @offtables2 = ();
1178 my @offtable = ();
1179 for (my $i = 0; $i < 256; $i++)
1181 my @offtable2 = ();
1182 for(my $j = 0; $j < 16; $j++) # offset table for xx00-xxFF characters
1184 my @difftable;
1185 for (my $k = 0; $k < 16; $k++) # case map table for xxx0-xxxF characters
1187 my $char = ($i<<8) + ($j<<4) + $k;
1188 $difftable[$k] = (defined $table[$char]) ? (($table[$char]-$char) & 0xffff) : 0;
1191 my $diff_key = pack "S*", @difftable;
1192 my $offset3 = $difftables_hash{$diff_key};
1193 if (!defined $offset3)
1195 $offset3 = scalar @difftables;
1196 $difftables_hash{$diff_key} = $offset3;
1197 push @difftables, @difftable;
1199 $offtable2[$j] = $offset3;
1202 my $offtable2_key = pack "S*", @offtable2;
1203 my $offset2 = $offtables2_hash{$offtable2_key};
1204 if (!defined $offset2)
1206 $offset2 = scalar @offtables2;
1207 $offtables2_hash{$offtable2_key} = $offset2;
1208 push @offtables2, \@offtable2;
1210 $offtable[$i] = $offset2;
1213 my @output;
1214 my $offset = 0x100; # offset of first subtable in words
1215 foreach (@offtable)
1217 push @output, 0x10 * $_ + $offset; # offset of subtable in words
1220 $offset = 0x100 + 0x10 * scalar @offtables2; # offset of first difftable in words
1221 foreach(@offtables2)
1223 my $table = $_;
1224 foreach(@$table)
1226 push @output, $_ + $offset; # offset of difftable in words
1230 my $len = 1 + scalar @output + scalar @difftables;
1231 return pack "S<*", $len, @output, @difftables;
1235 ################################################################
1236 # dump case mappings for l_intl.nls
1237 sub dump_intl_nls($)
1239 my $filename = shift;
1240 open OUTPUT,">$filename.new" or die "Cannot create $filename";
1241 printf "Building $filename\n";
1243 binmode OUTPUT;
1244 print OUTPUT pack "S<", 1; # version
1245 print OUTPUT dump_binary_case_table( @toupper_table );
1246 print OUTPUT dump_binary_case_table( @tolower_table );
1247 close OUTPUT;
1248 save_file($filename);
1252 ################################################################
1253 # dump the ctype tables
1254 sub DUMP_CTYPE_TABLES($)
1256 my $filename = shift;
1257 open OUTPUT,">$filename.new" or die "Cannot create $filename";
1258 printf "Building $filename\n";
1259 printf OUTPUT "/* Unicode ctype tables */\n";
1260 printf OUTPUT "/* Automatically generated; DO NOT EDIT!! */\n\n";
1261 printf OUTPUT "#include \"wine/unicode.h\"\n\n";
1263 # add the direction in the high 4 bits of the category
1264 for (my $i = 0; $i < 65536; $i++)
1266 $category_table[$i] |= $direction_table[$i] << 12 if defined $direction_table[$i];
1269 dump_simple_mapping( "wine_wctype_table", @category_table );
1271 close OUTPUT;
1272 save_file($filename);
1276 ################################################################
1277 # dump the char composition tables
1278 sub DUMP_COMPOSE_TABLES($)
1280 my $filename = shift;
1282 open OUTPUT,">$filename.new" or die "Cannot create $filename";
1283 printf "Building $filename\n";
1284 printf OUTPUT "/* Unicode char composition */\n";
1285 printf OUTPUT "/* Automatically generated; DO NOT EDIT!! */\n\n";
1286 printf OUTPUT "#include \"wine/unicode.h\"\n\n";
1288 ######### composition table
1290 my @filled = ();
1291 foreach my $i (@compose_table)
1293 my @comp = @$i;
1294 push @{$filled[$comp[1]]}, [ $comp[0], $comp[2] ];
1297 # count how many different second chars we have
1299 my $count = 0;
1300 for (my $i = 0; $i < 65536; $i++)
1302 next unless defined $filled[$i];
1303 $count++;
1306 # build the table of second chars and offsets
1308 my $pos = $count + 1;
1309 my @table = ();
1310 for (my $i = 0; $i < 65536; $i++)
1312 next unless defined $filled[$i];
1313 push @table, $i, $pos;
1314 $pos += @{$filled[$i]};
1316 # terminator with last position
1317 push @table, 0, $pos;
1318 printf OUTPUT "const WCHAR unicode_compose_table[0x%x] =\n{\n", 2*$pos;
1319 printf OUTPUT " /* second chars + offsets */\n%s", DUMP_ARRAY( "0x%04x", 0, @table );
1321 # build the table of first chars and mappings
1323 for (my $i = 0; $i < 65536; $i++)
1325 next unless defined $filled[$i];
1326 my @table = ();
1327 my @list = sort { $a->[0] <=> $b->[0] } @{$filled[$i]};
1328 for (my $j = 0; $j <= $#list; $j++)
1330 push @table, $list[$j][0], $list[$j][1];
1332 printf OUTPUT ",\n /* 0x%04x */\n%s", $i, DUMP_ARRAY( "0x%04x", 0, @table );
1334 printf OUTPUT "\n};\n\nconst unsigned int unicode_compose_table_size = %d;\n\n", $count;
1336 ######### decomposition table
1338 # first determine all the 16-char subsets that contain something
1340 @filled = (0) x 4096;
1341 $pos = 16*2; # for the null subset
1342 for (my $i = 0; $i < 65536; $i++)
1344 next unless defined $decomp_table[$i];
1345 $filled[$i >> 4] = $pos;
1346 $pos += 16*2;
1347 $i |= 15;
1349 my $total = $pos;
1351 # now count the 256-char subsets that contain something
1353 my @filled_idx = (256) x 256;
1354 $pos = 256 + 16;
1355 for (my $i = 0; $i < 4096; $i++)
1357 next unless $filled[$i];
1358 $filled_idx[$i >> 4] = $pos;
1359 $pos += 16;
1360 $i |= 15;
1362 my $null_offset = $pos; # null mapping
1363 $total += $pos;
1365 # add the index offsets to the subsets positions
1367 for (my $i = 0; $i < 4096; $i++)
1369 next unless $filled[$i];
1370 $filled[$i] += $null_offset;
1373 # dump the main index
1375 printf OUTPUT "const WCHAR unicode_decompose_table[%d] =\n", $total;
1376 printf OUTPUT "{\n /* index */\n";
1377 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, @filled_idx );
1378 printf OUTPUT ",\n /* null sub-index */\n%s", DUMP_ARRAY( "0x%04x", 0, ($null_offset) x 16 );
1380 # dump the second-level indexes
1382 for (my $i = 0; $i < 256; $i++)
1384 next unless ($filled_idx[$i] > 256);
1385 my @table = @filled[($i<<4)..($i<<4)+15];
1386 for (my $j = 0; $j < 16; $j++) { $table[$j] ||= $null_offset; }
1387 printf OUTPUT ",\n /* sub-index %02x */\n", $i;
1388 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, @table );
1391 # dump the 16-char subsets
1393 printf OUTPUT ",\n /* null mapping */\n";
1394 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, (0) x 32 );
1396 for (my $i = 0; $i < 4096; $i++)
1398 next unless $filled[$i];
1399 my @table = (0) x 32;
1400 for (my $j = 0; $j < 16; $j++)
1402 if (defined $decomp_table[($i<<4) + $j])
1404 $table[2 * $j] = ${$decomp_table[($i << 4) + $j]}[0];
1405 $table[2 * $j + 1] = ${$decomp_table[($i << 4) + $j]}[1];
1408 printf OUTPUT ",\n /* 0x%03x0 .. 0x%03xf */\n", $i, $i;
1409 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, @table );
1412 printf OUTPUT "\n};\n";
1413 close OUTPUT;
1414 save_file($filename);
1418 ################################################################
1419 # handle a "bestfit" Windows mapping file
1421 sub handle_bestfit_file($$$)
1423 my ($filename, $has_glyphs, $comment) = @_;
1424 my $state = "";
1425 my ($codepage, $width, $def, $defw, $count);
1426 my ($lb_cur, $lb_end);
1427 my @lb_ranges = ();
1429 my $INPUT = open_data_file "$MAPPINGS/$filename" or die "Cannot open $filename";
1431 while (<$INPUT>)
1433 next if /^;/; # skip comments
1434 next if /^\s*$/; # skip empty lines
1435 next if /\x1a/; # skip ^Z
1436 last if /^ENDCODEPAGE/;
1438 if (/^CODEPAGE\s+(\d+)/)
1440 $codepage = $1;
1441 next;
1443 if (/^CPINFO\s+(\d+)\s+0x([0-9a-fA-f]+)\s+0x([0-9a-fA-F]+)/)
1445 $width = $1;
1446 $def = hex $2;
1447 $defw = hex $3;
1448 next;
1450 if (/^(MBTABLE|WCTABLE|DBCSRANGE|DBCSTABLE)\s+(\d+)/)
1452 $state = $1;
1453 $count = $2;
1454 next;
1456 if (/^0x([0-9a-fA-F]+)\s+0x([0-9a-fA-F]+)/)
1458 if ($state eq "MBTABLE")
1460 my $cp = hex $1;
1461 my $uni = hex $2;
1462 $cp2uni[$cp] = $uni unless defined($cp2uni[$cp]);
1463 next;
1465 if ($state eq "WCTABLE")
1467 my $uni = hex $1;
1468 my $cp = hex $2;
1469 $uni2cp[$uni] = $cp unless defined($uni2cp[$uni]);
1470 next;
1472 if ($state eq "DBCSRANGE")
1474 my $start = hex $1;
1475 my $end = hex $2;
1476 push @lb_ranges, $start, $end;
1477 for (my $i = $start; $i <= $end; $i++)
1479 push @lead_bytes, $i;
1480 $cp2uni[$i] = 0;
1482 $lb_cur = $start;
1483 $lb_end = $end;
1484 next;
1486 if ($state eq "DBCSTABLE")
1488 my $mb = hex $1;
1489 my $uni = hex $2;
1490 my $cp = ($lb_cur << 8) | $mb;
1491 $cp2uni[$cp] = $uni unless defined($cp2uni[$cp]);
1492 if (!--$count)
1494 if (++$lb_cur > $lb_end) { $state = "DBCSRANGE"; }
1496 next;
1499 die "$filename: Unrecognized line $_\n";
1501 close $INPUT;
1503 my $output = sprintf "c_%03d.c", $codepage;
1504 open OUTPUT,">$output.new" or die "Cannot create $output";
1506 printf "Building %s from %s (%s)\n", $output, $filename, $comment;
1508 # dump all tables
1510 printf OUTPUT "/* code page %03d (%s) */\n", $codepage, $comment;
1511 printf OUTPUT "/* generated from $MAPPINGS/$filename */\n";
1512 printf OUTPUT "/* DO NOT EDIT!! */\n\n";
1513 printf OUTPUT "#include \"wine/unicode.h\"\n\n";
1515 if ($width == 1) { dump_sbcs_table( $codepage, $has_glyphs, $comment, $def, $defw ); }
1516 else { dump_dbcs_table( $codepage, $comment, $def, $defw, @lb_ranges ); }
1517 close OUTPUT;
1518 save_file($output);
1522 ################################################################
1523 # read an input file and generate the corresponding .c file
1524 sub HANDLE_FILE(@)
1526 my ($codepage,$filename,$has_glyphs,$comment) = @_;
1528 @cp2uni = ();
1529 @lead_bytes = ();
1530 @uni2cp = ();
1532 # symbol codepage file is special
1533 if ($codepage == 20932) { READ_JIS0208_FILE "$MAPPINGS/$filename"; }
1534 elsif ($codepage == 20127) { fill_20127_codepage(); }
1535 elsif ($filename =~ /\/bestfit/)
1537 handle_bestfit_file( $filename, $has_glyphs, $comment );
1538 return;
1540 else { READ_FILE "$MAPPINGS/$filename"; }
1542 ADD_DEFAULT_MAPPINGS();
1544 my $output = sprintf "c_%03d.c", $codepage;
1545 open OUTPUT,">$output.new" or die "Cannot create $output";
1547 printf "Building %s from %s (%s)\n", $output, $filename || "hardcoded data", $comment;
1549 # dump all tables
1551 printf OUTPUT "/* code page %03d (%s) */\n", $codepage, $comment;
1552 if ($filename)
1554 print OUTPUT "/* generated from $MAPPINGS/$filename */\n";
1555 print OUTPUT "/* DO NOT EDIT!! */\n\n";
1557 else
1559 printf OUTPUT "/* Automatically generated; DO NOT EDIT!! */\n\n";
1561 printf OUTPUT "#include \"wine/unicode.h\"\n\n";
1563 if (!@lead_bytes) { dump_sbcs_table( $codepage, $has_glyphs, $comment, $DEF_CHAR, $DEF_CHAR ); }
1564 else { dump_dbcs_table( $codepage, $comment, $DEF_CHAR, $DEF_CHAR, get_lb_ranges() ); }
1565 close OUTPUT;
1566 save_file($output);
1570 ################################################################
1571 # save a file if modified
1572 sub save_file($)
1574 my $file = shift;
1575 if (-f $file && !system "cmp $file $file.new >/dev/null")
1577 unlink "$file.new";
1579 else
1581 rename "$file.new", "$file";
1586 ################################################################
1587 # output the list of codepage tables into the cptable.c file
1588 sub OUTPUT_CPTABLE()
1590 my @tables_decl = ();
1592 foreach my $file (@allfiles)
1594 my ($codepage,$filename,$comment) = @$file;
1595 push @tables_decl, sprintf("extern union cptable cptable_%03d;\n",$codepage);
1598 push @tables_decl, sprintf("\nstatic const union cptable * const cptables[%d] =\n{\n",$#allfiles+1);
1599 foreach my $file (@allfiles)
1601 my ($codepage,$filename,$comment) = @$file;
1602 push @tables_decl, sprintf(" &cptable_%03d,\n", $codepage);
1604 push @tables_decl, "};";
1605 REPLACE_IN_FILE( "cptable.c", @tables_decl );
1608 ################################################################
1609 # replace the contents of a file between ### cpmap ### marks
1611 sub REPLACE_IN_FILE($@)
1613 my $name = shift;
1614 my @data = @_;
1615 my @lines = ();
1616 open(FILE,$name) or die "Can't open $name";
1617 while (<FILE>)
1619 push @lines, $_;
1620 last if /\#\#\# cpmap begin \#\#\#/;
1622 push @lines, @data;
1623 while (<FILE>)
1625 if (/\#\#\# cpmap end \#\#\#/) { push @lines, "\n", $_; last; }
1627 push @lines, <FILE>;
1628 open(FILE,">$name.new") or die "Can't modify $name";
1629 print FILE @lines;
1630 close(FILE);
1631 save_file($name);
1634 ################################################################
1635 # main routine
1637 READ_DEFAULTS( $DEFAULTS );
1638 DUMP_CASE_MAPPINGS( "casemap.c" );
1639 DUMP_SORTKEYS( "collation.c", READ_SORTKEYS_FILE() );
1640 DUMP_COMPOSE_TABLES( "compose.c" );
1641 DUMP_CTYPE_TABLES( "wctype.c" );
1642 dump_mirroring( "../../dlls/usp10/mirror.c" );
1643 dump_shaping( "../../dlls/usp10/shaping.c" );
1644 dump_intl_nls("../../tools/l_intl.nls");
1646 foreach my $file (@allfiles) { HANDLE_FILE( @{$file} ); }
1648 OUTPUT_CPTABLE();
1650 exit 0;
1652 # Local Variables:
1653 # compile-command: "./cpmap.pl && make -k"
1654 # End: