libwine: There are a few supercript characters (No, Lm types) that Windows marks...
[wine.git] / libs / wine / cpmap.pl
blob79f5fd575b9528472cb1087534e402ab070eb2d8
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,
115 "defin" => 0x0200
118 my %categories =
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 ],
163 "punct" => [ 0x24, 0x2b, 0x3c..0x3e, 0x5e, 0x60, 0x7c, 0x7e, 0xa2..0xbe,
164 0xd7, 0xf7 ],
165 "digit" => [ 0xb2, 0xb3, 0xb9 ],
166 "lower" => [ 0x2071, 0x207f ]
169 my %directions =
171 "L" => 1, # Left-to-Right
172 "LRE" => 15, # Left-to-Right Embedding
173 "LRO" => 15, # Left-to-Right Override
174 "R" => 2, # Right-to-Left
175 "AL" => 12, # Right-to-Left Arabic
176 "RLE" => 15, # Right-to-Left Embedding
177 "RLO" => 15, # Right-to-Left Override
178 "PDF" => 15, # Pop Directional Format
179 "EN" => 3, # European Number
180 "ES" => 4, # European Number Separator
181 "ET" => 5, # European Number Terminator
182 "AN" => 6, # Arabic Number
183 "CS" => 7, # Common Number Separator
184 "NSM" => 13, # Non-Spacing Mark
185 "BN" => 14, # Boundary Neutral
186 "B" => 8, # Paragraph Separator
187 "S" => 9, # Segment Separator
188 "WS" => 10, # Whitespace
189 "ON" => 11 # Other Neutrals
192 my %joining_types =
194 "U" => 0, # Non_Joining
195 "T" => 1, # Transparent
196 "R" => 2, # Right_Joining
197 "L" => 3, # Left_Joining
198 "D" => 4, # Dual_Joining
199 "C" => 5, # Join_Causing
202 my @cp2uni = ();
203 my @lead_bytes = ();
204 my @uni2cp = ();
205 my @unicode_defaults = ();
206 my @unicode_aliases = ();
207 my @tolower_table = ();
208 my @toupper_table = ();
209 my @digitmap_table = ();
210 my @compatmap_table = ();
211 my @category_table = (0) x 65536;
212 my @joining_table = (0) x 65536;
213 my @direction_table = ();
214 my @decomp_table = ();
215 my @compose_table = ();
217 my %joining_forms =
219 "isolated" => [],
220 "final" => [],
221 "initial" => [],
222 "medial" => []
225 ################################################################
226 # fetch a unicode.org file and open it
227 sub open_data_file($)
229 my $url = shift;
230 (my $name = $url) =~ s/^.*\///;
231 local *FILE;
232 unless (-f "data/$name")
234 print "Fetching $url...\n";
235 mkdir "data";
236 !system "wget", "-q", "-O", "data/$name", $url or die "cannot fetch $url";
238 open FILE, "<data/$name" or die "cannot open data/$name";
239 return *FILE;
242 ################################################################
243 # read in the defaults file
244 sub READ_DEFAULTS($)
246 my $filename = shift;
247 my $start;
249 # first setup a few default mappings
251 open DEFAULTS, "$filename" or die "Cannot open $filename";
252 print "Loading $filename\n";
253 while (<DEFAULTS>)
255 next if /^\#/; # skip comments
256 next if /^$/; # skip empty lines
257 if (/^(([0-9a-fA-F]+)(,[0-9a-fA-F]+)*)\s+([0-9a-fA-F]+|'.'|none)\s+(\#.*)?/)
259 my @src = map hex, split /,/,$1;
260 my $dst = $4;
261 my $comment = $5;
262 if ($#src > 0) { push @unicode_aliases, \@src; }
263 next if ($dst eq "none");
264 $dst = ($dst =~ /\'.\'/) ? ord substr($dst,1,1) : hex $dst;
265 foreach my $src (@src)
267 die "Duplicate value" if defined($unicode_defaults[$src]);
268 $unicode_defaults[$src] = $dst;
270 next;
272 die "Unrecognized line $_\n";
274 close DEFAULTS;
276 # now build mappings from the decomposition field of the Unicode database
278 my $UNICODE_DATA = open_data_file "$UNIDATA/UnicodeData.txt";
279 while (<$UNICODE_DATA>)
281 # Decode the fields ...
282 my ($code, $name, $cat, $comb, $bidi,
283 $decomp, $dec, $dig, $num, $mirror,
284 $oldname, $comment, $upper, $lower, $title) = split /;/;
285 my $dst;
286 my $src = hex $code;
288 die "unknown category $cat" unless defined $categories{$cat};
289 die "unknown directionality $bidi" unless defined $directions{$bidi};
291 $category_table[$src] = $categories{$cat};
292 $direction_table[$src] = $directions{$bidi};
293 $joining_table[$src] = $joining_types{"T"} if $cat eq "Mn" || $cat eq "Me" || $cat eq "Cf";
295 if ($lower ne "")
297 $tolower_table[$src] = hex $lower;
299 if ($upper ne "")
301 $toupper_table[$src] = hex $upper;
303 if ($dec ne "")
305 $category_table[$src] |= $ctype{"digit"};
307 if ($dig ne "")
309 $digitmap_table[$src] = ord $dig;
312 # copy the category and direction for everything between First/Last pairs
313 if ($name =~ /, First>/) { $start = $src; }
314 if ($name =~ /, Last>/)
316 while ($start < $src)
318 $category_table[$start] = $category_table[$src];
319 $direction_table[$start] = $direction_table[$src];
320 $start++;
324 next if $decomp eq ""; # no decomposition, skip it
326 if ($decomp =~ /^<([a-zA-Z]+)>\s+([0-9a-fA-F]+)$/)
328 # decomposition of the form "<foo> 1234" -> use char if type is known
329 if (($src >= 0xf900 && $src < 0xfb00) || ($src >= 0xfe30 && $src < 0xfffd))
331 # Single char decomposition in the compatibility range
332 $compatmap_table[$src] = hex $2;
334 if ($1 eq "isolated" || $1 eq "final" || $1 eq "initial" || $1 eq "medial")
336 ${joining_forms{$1}}[hex $2] = $src;
337 next;
339 next unless ($1 eq "font" ||
340 $1 eq "noBreak" ||
341 $1 eq "circle" ||
342 $1 eq "super" ||
343 $1 eq "sub" ||
344 $1 eq "wide" ||
345 $1 eq "narrow" ||
346 $1 eq "compat" ||
347 $1 eq "small");
348 $dst = hex $2;
350 elsif ($decomp =~ /^<compat>\s+0020\s+([0-9a-fA-F]+)/)
352 # decomposition "<compat> 0020 1234" -> combining accent
353 $dst = hex $1;
355 elsif ($decomp =~ /^([0-9a-fA-F]+)/)
357 # decomposition contains only char values without prefix -> use first char
358 $dst = hex $1;
359 $category_table[$src] |= $category_table[$dst] if defined $category_table[$dst];
360 # store decomposition if it contains two chars
361 if ($decomp =~ /^([0-9a-fA-F]+)\s+([0-9a-fA-F]+)$/)
363 $decomp_table[$src] = [ hex $1, hex $2 ];
364 push @compose_table, [ hex $1, hex $2, $src ];
366 elsif ($decomp =~ /^(<[a-z]+>\s)*([0-9a-fA-F]+)$/ &&
367 (($src >= 0xf900 && $src < 0xfb00) || ($src >= 0xfe30 && $src < 0xfffd)))
369 # Single char decomposition in the compatibility range
370 $compatmap_table[$src] = hex $2;
373 else
375 next;
378 next if defined($unicode_defaults[$src]); # may have been set in the defaults file
380 # check for loops
381 for (my $i = $dst; ; $i = $unicode_defaults[$i])
383 die sprintf("loop detected for %04x -> %04x",$src,$dst) if $i == $src;
384 last unless defined($unicode_defaults[$i]);
386 $unicode_defaults[$src] = $dst;
388 close $UNICODE_DATA;
390 # patch the category of some special characters
392 foreach my $cat (keys %special_categories)
394 my $flag = $ctype{$cat};
395 foreach my $i (@{$special_categories{$cat}}) { $category_table[$i] |= $flag; }
400 ################################################################
401 # parse the input file
402 sub READ_FILE($)
404 my $name = shift;
405 my $INPUT = open_data_file $name;
407 while (<$INPUT>)
409 next if /^\#/; # skip comments
410 next if /^$/; # skip empty lines
411 next if /\x1a/; # skip ^Z
412 next if (/^0x([0-9a-fA-F]+)\s+\#UNDEFINED/); # undefined char
414 if (/^0x([0-9a-fA-F]+)\s+\#DBCS LEAD BYTE/)
416 my $cp = hex $1;
417 push @lead_bytes,$cp;
418 $cp2uni[$cp] = 0;
419 next;
421 if (/^0x([0-9a-fA-F]+)\s+0x([0-9a-fA-F]+)\s+(\#.*)?/)
423 my $cp = hex $1;
424 my $uni = hex $2;
425 $cp2uni[$cp] = $uni unless defined($cp2uni[$cp]);
426 $uni2cp[$uni] = $cp unless defined($uni2cp[$uni]);
427 if ($cp > 0xff && !defined($cp2uni[$cp >> 8]))
429 push @lead_bytes,$cp >> 8;
430 $cp2uni[$cp >> 8] = 0;
432 next;
434 die "$name: Unrecognized line $_\n";
436 close $INPUT;
440 ################################################################
441 # fill input data for the 20127 (us-ascii) codepage
442 sub fill_20127_codepage()
444 for (my $i = 0; $i < 128; $i++) { $cp2uni[$i] = $uni2cp[$i] = $i; }
445 for (my $i = 128; $i < 256; $i++) { $cp2uni[$i] = $i & 0x7f; }
448 ################################################################
449 # get a mapping including glyph chars for MB_USEGLYPHCHARS
451 sub get_glyphs_mapping(@)
453 $_[0x01] = 0x263a; # (WHITE SMILING FACE)
454 $_[0x02] = 0x263b; # (BLACK SMILING FACE)
455 $_[0x03] = 0x2665; # (BLACK HEART SUIT)
456 $_[0x04] = 0x2666; # (BLACK DIAMOND SUIT)
457 $_[0x05] = 0x2663; # (BLACK CLUB SUIT)
458 $_[0x06] = 0x2660; # (BLACK SPADE SUIT)
459 $_[0x07] = 0x2022; # (BULLET)
460 $_[0x08] = 0x25d8; # (INVERSE BULLET)
461 $_[0x09] = 0x25cb; # (WHITE CIRCLE)
462 $_[0x0a] = 0x25d9; # (INVERSE WHITE CIRCLE)
463 $_[0x0b] = 0x2642; # (MALE SIGN)
464 $_[0x0c] = 0x2640; # (FEMALE SIGN)
465 $_[0x0d] = 0x266a; # (EIGHTH NOTE)
466 $_[0x0e] = 0x266b; # (BEAMED EIGHTH NOTES)
467 $_[0x0f] = 0x263c; # (WHITE SUN WITH RAYS)
468 $_[0x10] = 0x25ba; # (BLACK RIGHT-POINTING POINTER)
469 $_[0x11] = 0x25c4; # (BLACK LEFT-POINTING POINTER)
470 $_[0x12] = 0x2195; # (UP DOWN ARROW)
471 $_[0x13] = 0x203c; # (DOUBLE EXCLAMATION MARK)
472 $_[0x14] = 0x00b6; # (PILCROW SIGN)
473 $_[0x15] = 0x00a7; # (SECTION SIGN)
474 $_[0x16] = 0x25ac; # (BLACK RECTANGLE)
475 $_[0x17] = 0x21a8; # (UP DOWN ARROW WITH BASE)
476 $_[0x18] = 0x2191; # (UPWARDS ARROW)
477 $_[0x19] = 0x2193; # (DOWNWARDS ARROW)
478 $_[0x1a] = 0x2192; # (RIGHTWARDS ARROW)
479 $_[0x1b] = 0x2190; # (LEFTWARDS ARROW)
480 $_[0x1c] = 0x221f; # (RIGHT ANGLE)
481 $_[0x1d] = 0x2194; # (LEFT RIGHT ARROW)
482 $_[0x1e] = 0x25b2; # (BLACK UP-POINTING TRIANGLE)
483 $_[0x1f] = 0x25bc; # (BLACK DOWN-POINTING TRIANGLE)
484 $_[0x7f] = 0x2302; # (HOUSE)
485 return @_;
488 ################################################################
489 # build EUC-JP table from the JIS 0208 file
490 # FIXME: for proper EUC-JP we should probably read JIS 0212 too
491 # but this would require 3-byte DBCS characters
492 sub READ_JIS0208_FILE($)
494 my $name = shift;
496 # ASCII chars
497 for (my $i = 0x00; $i <= 0x7f; $i++)
499 $cp2uni[$i] = $i;
500 $uni2cp[$i] = $i;
503 # JIS X 0201 right plane
504 for (my $i = 0xa1; $i <= 0xdf; $i++)
506 $cp2uni[0x8e00 + $i] = 0xfec0 + $i;
507 $uni2cp[0xfec0 + $i] = 0x8e00 + $i;
510 # lead bytes
511 foreach my $i (0x8e, 0x8f, 0xa1 .. 0xfe)
513 push @lead_bytes,$i;
514 $cp2uni[$i] = 0;
517 # undefined chars
518 foreach my $i (0x80 .. 0x8d, 0x90 .. 0xa0, 0xff)
520 $cp2uni[$i] = $DEF_CHAR;
523 # Shift-JIS compatibility
524 $uni2cp[0x00a5] = 0x5c;
525 $uni2cp[0x203e] = 0x7e;
527 # Fix backslash conversion
528 $cp2uni[0xa1c0] = 0xff3c;
529 $uni2cp[0xff3c] = 0xa1c0;
531 my $INPUT = open_data_file $name;
532 while (<$INPUT>)
534 next if /^\#/; # skip comments
535 next if /^$/; # skip empty lines
536 next if /\x1a/; # skip ^Z
537 if (/^0x[0-9a-fA-F]+\s+0x([0-9a-fA-F]+)\s+0x([0-9a-fA-F]+)\s+(\#.*)?/)
539 my $cp = 0x8080 + hex $1;
540 my $uni = hex $2;
541 $cp2uni[$cp] = $uni unless defined($cp2uni[$cp]);
542 $uni2cp[$uni] = $cp unless defined($uni2cp[$uni]);
543 next;
545 die "$name: Unrecognized line $_\n";
547 close $INPUT;
551 ################################################################
552 # build the sort keys table
553 sub READ_SORTKEYS_FILE()
555 my @sortkeys = ();
556 for (my $i = 0; $i < 65536; $i++) { $sortkeys[$i] = [ -1, 0, 0, 0, 0 ] };
558 my $INPUT = open_data_file $SORTKEYS;
559 while (<$INPUT>)
561 next if /^\#/; # skip comments
562 next if /^$/; # skip empty lines
563 next if /\x1a/; # skip ^Z
564 next if /^\@version/; # skip @version header
565 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]+)\]/)
567 my ($uni,$variable) = (hex $1, $2);
568 next if $uni > 65535;
569 $sortkeys[$uni] = [ $uni, hex $3, hex $4, hex $5, hex $6 ];
570 next;
572 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]+)\]/)
574 # multiple character sequence, ignored for now
575 next;
577 die "$SORTKEYS: Unrecognized line $_\n";
579 close $INPUT;
581 # compress the keys to 32 bit:
582 # key 1 to 16 bits, key 2 to 8 bits, key 3 to 4 bits, key 4 to 1 bit
584 @sortkeys = sort { ${$a}[1] <=> ${$b}[1] or
585 ${$a}[2] <=> ${$b}[2] or
586 ${$a}[3] <=> ${$b}[3] or
587 ${$a}[4] <=> ${$b}[4] or
588 $a cmp $b; } @sortkeys;
590 my ($n2, $n3) = (1, 1);
591 my @keys = (-1, -1, -1, -1, -1 );
592 my @flatkeys = ();
594 for (my $i = 0; $i < 65536; $i++)
596 my @current = @{$sortkeys[$i]};
597 next if $current[0] == -1;
598 if ($current[1] == $keys[1])
600 if ($current[2] == $keys[2])
602 if ($current[3] == $keys[3])
604 # nothing
606 else
608 $keys[3] = $current[3];
609 $n3++;
610 die if ($n3 >= 16);
613 else
615 $keys[2] = $current[2];
616 $keys[3] = $current[3];
617 $n2++;
618 $n3 = 1;
619 die if ($n2 >= 256);
622 else
624 $keys[1] = $current[1];
625 $keys[2] = $current[2];
626 $keys[3] = $current[3];
627 $n2 = 1;
628 $n3 = 1;
631 if ($current[2]) { $current[2] = $n2; }
632 if ($current[3]) { $current[3] = $n3; }
633 if ($current[4]) { $current[4] = 1; }
635 $flatkeys[$current[0]] = ($current[1] << 16) | ($current[2] << 8) | ($current[3] << 4) | $current[4];
637 return @flatkeys;
641 ################################################################
642 # build the sort keys table
643 sub DUMP_SORTKEYS($@)
645 my ($filename, @keys) = @_;
647 # count the number of 256-key ranges that contain something
649 my @offsets = ();
650 my $ranges = 2;
651 for (my $i = 0; $i < 256; $i++) { $offsets[$i] = 256; }
652 for (my $i = 0; $i < 65536; $i++)
654 next unless defined $keys[$i];
655 $offsets[$i >> 8] = $ranges * 256;
656 $ranges++;
657 $i |= 255;
660 # output the range offsets
662 open OUTPUT,">$filename.new" or die "Cannot create $filename";
663 printf "Building $filename\n";
664 printf OUTPUT "/* Unicode collation element table */\n";
665 printf OUTPUT "/* generated from %s */\n", $SORTKEYS;
666 printf OUTPUT "/* DO NOT EDIT!! */\n\n";
668 printf OUTPUT "const unsigned int collation_table[%d] =\n{\n", $ranges*256;
669 printf OUTPUT " /* index */\n";
670 printf OUTPUT "%s,\n", DUMP_ARRAY( "0x%08x", 0, @offsets );
672 # output the default values
674 printf OUTPUT " /* defaults */\n";
675 printf OUTPUT "%s", DUMP_ARRAY( "0x%08x", 0, (0xffffffff) x 256 );
677 # output all the key ranges
679 for (my $i = 0; $i < 256; $i++)
681 next if $offsets[$i] == 256;
682 printf OUTPUT ",\n /* 0x%02x00 .. 0x%02xff */\n", $i, $i;
683 printf OUTPUT "%s", DUMP_ARRAY( "0x%08x", 0xffffffff, @keys[($i<<8) .. ($i<<8)+255] );
685 printf OUTPUT "\n};\n";
686 close OUTPUT;
687 save_file($filename);
691 ################################################################
692 # add default mappings once the file had been read
693 sub ADD_DEFAULT_MAPPINGS()
695 # Apply aliases
697 foreach my $alias (@unicode_aliases)
699 my $target = undef;
700 foreach my $src (@$alias)
702 if (defined($uni2cp[$src]))
704 $target = $uni2cp[$src];
705 last;
708 next unless defined($target);
710 # At least one char of the alias set is defined, set the others to the same value
711 foreach my $src (@$alias)
713 $uni2cp[$src] = $target unless defined($uni2cp[$src]);
717 # For every src -> target mapping in the defaults table,
718 # make uni2cp[src] = uni2cp[target] if uni2cp[target] is defined
720 for (my $src = 0; $src < 65536; $src++)
722 next if defined($uni2cp[$src]); # source has a definition already
723 next unless defined($unicode_defaults[$src]); # no default for this char
724 my $target = $unicode_defaults[$src];
726 # do a recursive mapping until we find a target char that is defined
727 while (!defined($uni2cp[$target]) &&
728 defined($unicode_defaults[$target])) { $target = $unicode_defaults[$target]; }
730 if (defined($uni2cp[$target])) { $uni2cp[$src] = $uni2cp[$target]; }
733 # Add an identity mapping for all undefined chars
735 for (my $i = 0; $i < 256; $i++)
737 next if defined($cp2uni[$i]);
738 next if defined($uni2cp[$i]);
739 $cp2uni[$i] = $uni2cp[$i] = $i;
743 ################################################################
744 # dump an array of integers
745 sub DUMP_ARRAY($$@)
747 my ($format,$default,@array) = @_;
748 my $i;
749 my $ret = " ";
750 for ($i = 0; $i < $#array; $i++)
752 $ret .= sprintf($format, defined $array[$i] ? $array[$i] : $default);
753 $ret .= (($i % 8) != 7) ? ", " : ",\n ";
755 $ret .= sprintf($format, defined $array[$i] ? $array[$i] : $default);
756 return $ret;
759 ################################################################
760 # dump an SBCS mapping table
761 sub dump_sbcs_table($$$$$)
763 my ($codepage, $has_glyphs, $name, $def, $defw) = @_;
764 my $i;
766 # output the ascii->unicode table
768 if ($has_glyphs)
770 printf OUTPUT "static const WCHAR cp2uni[512] =\n";
771 printf OUTPUT "{\n%s", DUMP_ARRAY( "0x%04x", $defw, @cp2uni[0 .. 255] );
772 printf OUTPUT ",\n /* glyphs */\n%s\n};\n\n",
773 DUMP_ARRAY( "0x%04x", $defw, get_glyphs_mapping(@cp2uni[0 .. 255]) );
775 else
777 printf OUTPUT "static const WCHAR cp2uni[256] =\n";
778 printf OUTPUT "{\n%s\n};\n\n", DUMP_ARRAY( "0x%04x", $defw, @cp2uni[0 .. 255] );
781 # count the number of unicode->ascii subtables that contain something
783 my @filled = ();
784 my $subtables = 1;
785 for (my $i = 0; $i < 65536; $i++)
787 next unless defined $uni2cp[$i];
788 $filled[$i >> 8] = 1;
789 $subtables++;
790 $i |= 255;
793 # output all the subtables into a single array
795 printf OUTPUT "static const unsigned char uni2cp_low[%d] =\n{\n", $subtables*256;
796 for (my $i = 0; $i < 256; $i++)
798 next unless $filled[$i];
799 printf OUTPUT " /* 0x%02x00 .. 0x%02xff */\n", $i, $i;
800 printf OUTPUT "%s,\n", DUMP_ARRAY( "0x%02x", $def, @uni2cp[($i<<8) .. ($i<<8)+255] );
802 printf OUTPUT " /* defaults */\n";
803 printf OUTPUT "%s\n};\n\n", DUMP_ARRAY( "0x%02x", 0, ($def) x 256 );
805 # output a table of the offsets of the subtables in the previous array
807 my $pos = 0;
808 my @offsets = ();
809 for (my $i = 0; $i < 256; $i++)
811 if ($filled[$i]) { push @offsets, $pos; $pos += 256; }
812 else { push @offsets, ($subtables-1) * 256; }
814 printf OUTPUT "static const unsigned short uni2cp_high[256] =\n";
815 printf OUTPUT "{\n%s\n};\n\n", DUMP_ARRAY( "0x%04x", 0, @offsets );
817 # output the code page descriptor
819 printf OUTPUT "const struct sbcs_table cptable_%03d =\n{\n", $codepage;
820 printf OUTPUT " { %d, 1, 0x%04x, 0x%04x, \"%s\" },\n",
821 $codepage, $def, $defw, $name;
822 printf OUTPUT " cp2uni,\n";
823 if ($has_glyphs) { printf OUTPUT " cp2uni + 256,\n"; }
824 else { printf OUTPUT " cp2uni,\n"; }
825 printf OUTPUT " uni2cp_low,\n";
826 printf OUTPUT " uni2cp_high\n};\n";
830 ################################################################
831 # dump a DBCS mapping table
832 sub dump_dbcs_table($$$$@)
834 my ($codepage, $name, $def, $defw, @lb_ranges) = @_;
836 # build a list of lead bytes that are actually used
838 my @lblist = ();
839 LBLOOP: for (my $y = 0; $y <= $#lead_bytes; $y++)
841 my $base = $lead_bytes[$y] << 8;
842 for (my $x = 0; $x < 256; $x++)
844 if (defined $cp2uni[$base+$x])
846 push @lblist,$lead_bytes[$y];
847 next LBLOOP;
851 my $unused = ($#lead_bytes > $#lblist);
853 # output the ascii->unicode table for the single byte chars
855 printf OUTPUT "static const WCHAR cp2uni[%d] =\n", 256 * ($#lblist + 2 + $unused);
856 printf OUTPUT "{\n%s,\n", DUMP_ARRAY( "0x%04x", $defw, @cp2uni[0 .. 255] );
858 # output the default table for unused lead bytes
860 if ($unused)
862 printf OUTPUT " /* unused lead bytes */\n";
863 printf OUTPUT "%s,\n", DUMP_ARRAY( "0x%04x", 0, ($defw) x 256 );
866 # output the ascii->unicode table for each DBCS lead byte
868 for (my $y = 0; $y <= $#lblist; $y++)
870 my $base = $lblist[$y] << 8;
871 printf OUTPUT " /* lead byte %02x */\n", $lblist[$y];
872 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", $defw, @cp2uni[$base .. $base+255] );
873 printf OUTPUT ($y < $#lblist) ? ",\n" : "\n};\n\n";
876 # output the lead byte subtables offsets
878 my @offsets = ();
879 for (my $x = 0; $x < 256; $x++) { $offsets[$x] = 0; }
880 for (my $x = 0; $x <= $#lblist; $x++) { $offsets[$lblist[$x]] = $x + 1; }
881 if ($unused)
883 # increment all lead bytes offset to take into account the unused table
884 for (my $x = 0; $x <= $#lead_bytes; $x++) { $offsets[$lead_bytes[$x]]++; }
886 printf OUTPUT "static const unsigned char cp2uni_leadbytes[256] =\n";
887 printf OUTPUT "{\n%s\n};\n\n", DUMP_ARRAY( "0x%02x", 0, @offsets );
889 # count the number of unicode->ascii subtables that contain something
891 my @filled = ();
892 my $subtables = 1;
893 for (my $i = 0; $i < 65536; $i++)
895 next unless defined $uni2cp[$i];
896 $filled[$i >> 8] = 1;
897 $subtables++;
898 $i |= 255;
901 # output all the subtables into a single array
903 printf OUTPUT "static const unsigned short uni2cp_low[%d] =\n{\n", $subtables*256;
904 for (my $y = 0; $y < 256; $y++)
906 next unless $filled[$y];
907 printf OUTPUT " /* 0x%02x00 .. 0x%02xff */\n", $y, $y;
908 printf OUTPUT "%s,\n", DUMP_ARRAY( "0x%04x", $def, @uni2cp[($y<<8) .. ($y<<8)+255] );
910 printf OUTPUT " /* defaults */\n";
911 printf OUTPUT "%s\n};\n\n", DUMP_ARRAY( "0x%04x", 0, ($def) x 256 );
913 # output a table of the offsets of the subtables in the previous array
915 my $pos = 0;
916 @offsets = ();
917 for (my $y = 0; $y < 256; $y++)
919 if ($filled[$y]) { push @offsets, $pos; $pos += 256; }
920 else { push @offsets, ($subtables-1) * 256; }
922 printf OUTPUT "static const unsigned short uni2cp_high[256] =\n";
923 printf OUTPUT "{\n%s\n};\n\n", DUMP_ARRAY( "0x%04x", 0, @offsets );
925 # output the code page descriptor
927 printf OUTPUT "const struct dbcs_table cptable_%03d =\n{\n", $codepage;
928 printf OUTPUT " { %d, 2, 0x%04x, 0x%04x, \"%s\" },\n",
929 $codepage, $def, $defw, $name;
930 printf OUTPUT " cp2uni,\n";
931 printf OUTPUT " cp2uni_leadbytes,\n";
932 printf OUTPUT " uni2cp_low,\n";
933 printf OUTPUT " uni2cp_high,\n";
934 printf OUTPUT " {\n %s\n }\n", DUMP_ARRAY( "0x%02x", 0, @lb_ranges, 0, 0 );
935 printf OUTPUT "};\n";
939 ################################################################
940 # get the list of defined lead byte ranges
941 sub get_lb_ranges()
943 my @list = ();
944 my @ranges = ();
945 my $i = 0;
946 foreach $i (@lead_bytes) { $list[$i] = 1; }
947 my $on = 0;
948 for (my $i = 0; $i < 256; $i++)
950 if ($on)
952 if (!defined $list[$i]) { push @ranges, $i-1; $on = 0; }
954 else
956 if ($list[$i]) { push @ranges, $i; $on = 1; }
959 if ($on) { push @ranges, 0xff; }
960 return @ranges;
964 ################################################################
965 # dump the BiDi mirroring table
966 sub dump_mirroring($)
968 my $filename = shift;
969 my @mirror_table = ();
971 my $INPUT = open_data_file "$UNIDATA/BidiMirroring.txt";
972 while (<$INPUT>)
974 next if /^\#/; # skip comments
975 next if /^$/; # skip empty lines
976 next if /\x1a/; # skip ^Z
977 if (/^\s*([0-9a-fA-F]+)\s*;\s*([0-9a-fA-F]+)/)
979 $mirror_table[hex $1] = hex $2;
980 next;
982 die "malformed line $_";
984 close $INPUT;
986 open OUTPUT,">$filename.new" or die "Cannot create $filename";
987 print "Building $filename\n";
988 print OUTPUT "/* Unicode BiDi mirroring */\n";
989 print OUTPUT "/* generated from $UNIDATA/BidiMirroring.txt */\n";
990 print OUTPUT "/* DO NOT EDIT!! */\n\n";
991 print OUTPUT "#include \"wine/unicode.h\"\n\n";
992 DUMP_CASE_TABLE( "wine_mirror_map", @mirror_table );
993 close OUTPUT;
994 save_file($filename);
998 ################################################################
999 # dump the Arabic shaping table
1000 sub dump_shaping($)
1002 my $filename = shift;
1003 my %groups;
1004 my $next_group = 0;
1006 $groups{"No_Joining_Group"} = $next_group++;
1008 my $INPUT = open_data_file "$UNIDATA/ArabicShaping.txt";
1009 while (<$INPUT>)
1011 next if /^\#/; # skip comments
1012 next if /^\s*$/; # skip empty lines
1013 next if /\x1a/; # skip ^Z
1014 if (/^\s*([0-9a-fA-F]+)\s*;.*;\s*([RLDCUT])\s*;\s*(\w+)/)
1016 my $type = $2;
1017 my $group = $3;
1018 $groups{$group} = $next_group++ unless defined $groups{$group};
1019 $joining_table[hex $1] = $joining_types{$type} | ($groups{$group} << 8);
1020 next;
1022 die "malformed line $_";
1024 close $INPUT;
1026 open OUTPUT,">$filename.new" or die "Cannot create $filename";
1027 print "Building $filename\n";
1028 print OUTPUT "/* Unicode Arabic shaping */\n";
1029 print OUTPUT "/* generated from $UNIDATA/ArabicShaping.txt */\n";
1030 print OUTPUT "/* DO NOT EDIT!! */\n\n";
1031 print OUTPUT "#include \"wine/unicode.h\"\n\n";
1033 dump_simple_mapping( "wine_shaping_table", @joining_table );
1035 print OUTPUT "\nconst unsigned short wine_shaping_forms[256][4] =\n{\n";
1036 for (my $i = 0x600; $i <= 0x6ff; $i++)
1038 printf OUTPUT " { 0x%04x, 0x%04x, 0x%04x, 0x%04x },\n",
1039 ${joining_forms{"isolated"}}[$i] || $i,
1040 ${joining_forms{"final"}}[$i] || $i,
1041 ${joining_forms{"initial"}}[$i] || $i,
1042 ${joining_forms{"medial"}}[$i] || $i;
1044 print OUTPUT "};\n";
1046 close OUTPUT;
1047 save_file($filename);
1051 ################################################################
1052 # dump the case mapping tables
1053 sub DUMP_CASE_MAPPINGS($)
1055 my $filename = shift;
1056 open OUTPUT,">$filename.new" or die "Cannot create $filename";
1057 printf "Building $filename\n";
1058 printf OUTPUT "/* Unicode case mappings */\n";
1059 printf OUTPUT "/* Automatically generated; DO NOT EDIT!! */\n\n";
1060 printf OUTPUT "#include \"wine/unicode.h\"\n\n";
1062 DUMP_CASE_TABLE( "wine_casemap_lower", @tolower_table );
1063 DUMP_CASE_TABLE( "wine_casemap_upper", @toupper_table );
1064 DUMP_CASE_TABLE( "wine_digitmap", @digitmap_table );
1065 DUMP_CASE_TABLE( "wine_compatmap", @compatmap_table );
1066 close OUTPUT;
1067 save_file($filename);
1071 ################################################################
1072 # dump a case mapping table
1073 sub DUMP_CASE_TABLE($@)
1075 my ($name,@table) = @_;
1077 # count the number of sub tables that contain something
1078 # also compute the low and upper populated bounds
1080 my @lowerbounds = ( 0, 0 );
1081 my @upperbounds = ( 0, 255 );
1082 my $index = 0;
1083 my @filled = ();
1084 for (my $i = 0; $i < 65536; $i++)
1086 next unless defined $table[$i];
1087 if (!defined $filled[$i >> 8])
1089 $lowerbounds[$index] = $i & 0xff;
1090 $upperbounds[$index] = 0xff - $lowerbounds[$index];
1091 $filled[$i >> 8] = $index * 256 + 512;
1092 $index++;
1094 else
1096 $upperbounds[$index-1] = 0xff - ($i & 0xff);
1098 $table[$i] = ($table[$i] - $i) & 0xffff;
1101 # Collapse blocks upwards if possible
1102 my $removed = 0;
1103 $index = 0;
1104 for (my $i = 0; $i < 256; $i++)
1106 next unless defined $filled[$i];
1107 if ($upperbounds[$index - 1] > $lowerbounds[$index])
1109 $removed = $removed + $lowerbounds[$index];
1111 else
1113 $removed = $removed + $upperbounds[$index - 1];
1114 $lowerbounds[$index] = $upperbounds[$index - 1];
1116 $filled[$i] = $filled[$i] - $removed;
1117 $index++;
1120 # dump the table
1122 printf OUTPUT "const WCHAR %s[%d] =\n", $name, $index * 256 + 512 - $removed;
1123 printf OUTPUT "{\n /* index */\n";
1124 printf OUTPUT "%s,\n", DUMP_ARRAY( "0x%04x", 256, @filled );
1125 printf OUTPUT " /* defaults */\n";
1126 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, (0) x 256 );
1127 $index = 0;
1128 for (my $i = 0; $i < 256; $i++)
1130 next unless $filled[$i];
1131 printf OUTPUT ",\n /* 0x%02x%02x .. 0x%02xff */\n", $i, $lowerbounds[$index], $i;
1132 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0,
1133 @table[($i<<8) + $lowerbounds[$index] .. ($i<<8)+255] );
1134 $index++;
1136 printf OUTPUT "\n};\n";
1139 ################################################################
1140 # dump a simple char -> 16-bit value mapping table
1141 sub dump_simple_mapping($@)
1143 my $name = shift;
1144 my @table = @_;
1145 my @array = (0) x 256;
1146 my %sequences;
1148 # try to merge table rows
1149 for (my $row = 0; $row < 256; $row++)
1151 my $rowtxt = sprintf "%04x" x 256, @table[($row<<8)..($row<<8)+255];
1152 if (defined($sequences{$rowtxt}))
1154 # reuse an existing row
1155 $array[$row] = $sequences{$rowtxt};
1157 else
1159 # create a new row
1160 $sequences{$rowtxt} = $array[$row] = $#array + 1;
1161 push @array, @table[($row<<8)..($row<<8)+255];
1165 printf OUTPUT "const unsigned short %s[%d] =\n{\n", $name, $#array+1;
1166 printf OUTPUT " /* offsets */\n%s,\n", DUMP_ARRAY( "0x%04x", 0, @array[0..255] );
1167 printf OUTPUT " /* values */\n%s\n};\n", DUMP_ARRAY( "0x%04x", 0, @array[256..$#array] );
1170 ################################################################
1171 # dump a binary case mapping table in l_intl.nls format
1172 sub dump_binary_case_table(@)
1174 my (@table) = @_;
1176 my %difftables_hash = ();
1177 my @difftables;
1178 my %offtables2_hash = ();
1179 my @offtables2 = ();
1181 my @offtable = ();
1182 for (my $i = 0; $i < 256; $i++)
1184 my @offtable2 = ();
1185 for(my $j = 0; $j < 16; $j++) # offset table for xx00-xxFF characters
1187 my @difftable;
1188 for (my $k = 0; $k < 16; $k++) # case map table for xxx0-xxxF characters
1190 my $char = ($i<<8) + ($j<<4) + $k;
1191 $difftable[$k] = (defined $table[$char]) ? (($table[$char]-$char) & 0xffff) : 0;
1194 my $diff_key = pack "S*", @difftable;
1195 my $offset3 = $difftables_hash{$diff_key};
1196 if (!defined $offset3)
1198 $offset3 = scalar @difftables;
1199 $difftables_hash{$diff_key} = $offset3;
1200 push @difftables, @difftable;
1202 $offtable2[$j] = $offset3;
1205 my $offtable2_key = pack "S*", @offtable2;
1206 my $offset2 = $offtables2_hash{$offtable2_key};
1207 if (!defined $offset2)
1209 $offset2 = scalar @offtables2;
1210 $offtables2_hash{$offtable2_key} = $offset2;
1211 push @offtables2, \@offtable2;
1213 $offtable[$i] = $offset2;
1216 my @output;
1217 my $offset = 0x100; # offset of first subtable in words
1218 foreach (@offtable)
1220 push @output, 0x10 * $_ + $offset; # offset of subtable in words
1223 $offset = 0x100 + 0x10 * scalar @offtables2; # offset of first difftable in words
1224 foreach(@offtables2)
1226 my $table = $_;
1227 foreach(@$table)
1229 push @output, $_ + $offset; # offset of difftable in words
1233 my $len = 1 + scalar @output + scalar @difftables;
1234 return pack "S<*", $len, @output, @difftables;
1238 ################################################################
1239 # dump case mappings for l_intl.nls
1240 sub dump_intl_nls($)
1242 my $filename = shift;
1243 open OUTPUT,">$filename.new" or die "Cannot create $filename";
1244 printf "Building $filename\n";
1246 binmode OUTPUT;
1247 print OUTPUT pack "S<", 1; # version
1248 print OUTPUT dump_binary_case_table( @toupper_table );
1249 print OUTPUT dump_binary_case_table( @tolower_table );
1250 close OUTPUT;
1251 save_file($filename);
1255 ################################################################
1256 # dump the ctype tables
1257 sub DUMP_CTYPE_TABLES($)
1259 my $filename = shift;
1260 open OUTPUT,">$filename.new" or die "Cannot create $filename";
1261 printf "Building $filename\n";
1262 printf OUTPUT "/* Unicode ctype tables */\n";
1263 printf OUTPUT "/* Automatically generated; DO NOT EDIT!! */\n\n";
1264 printf OUTPUT "#include \"wine/unicode.h\"\n\n";
1266 # add the direction in the high 4 bits of the category
1267 for (my $i = 0; $i < 65536; $i++)
1269 $category_table[$i] |= $direction_table[$i] << 12 if defined $direction_table[$i];
1272 dump_simple_mapping( "wine_wctype_table", @category_table );
1274 close OUTPUT;
1275 save_file($filename);
1279 ################################################################
1280 # dump the char composition tables
1281 sub DUMP_COMPOSE_TABLES($)
1283 my $filename = shift;
1285 open OUTPUT,">$filename.new" or die "Cannot create $filename";
1286 printf "Building $filename\n";
1287 printf OUTPUT "/* Unicode char composition */\n";
1288 printf OUTPUT "/* Automatically generated; DO NOT EDIT!! */\n\n";
1289 printf OUTPUT "#include \"wine/unicode.h\"\n\n";
1291 ######### composition table
1293 my @filled = ();
1294 foreach my $i (@compose_table)
1296 my @comp = @$i;
1297 push @{$filled[$comp[1]]}, [ $comp[0], $comp[2] ];
1300 # count how many different second chars we have
1302 my $count = 0;
1303 for (my $i = 0; $i < 65536; $i++)
1305 next unless defined $filled[$i];
1306 $count++;
1309 # build the table of second chars and offsets
1311 my $pos = $count + 1;
1312 my @table = ();
1313 for (my $i = 0; $i < 65536; $i++)
1315 next unless defined $filled[$i];
1316 push @table, $i, $pos;
1317 $pos += @{$filled[$i]};
1319 # terminator with last position
1320 push @table, 0, $pos;
1321 printf OUTPUT "const WCHAR unicode_compose_table[0x%x] =\n{\n", 2*$pos;
1322 printf OUTPUT " /* second chars + offsets */\n%s", DUMP_ARRAY( "0x%04x", 0, @table );
1324 # build the table of first chars and mappings
1326 for (my $i = 0; $i < 65536; $i++)
1328 next unless defined $filled[$i];
1329 my @table = ();
1330 my @list = sort { $a->[0] <=> $b->[0] } @{$filled[$i]};
1331 for (my $j = 0; $j <= $#list; $j++)
1333 push @table, $list[$j][0], $list[$j][1];
1335 printf OUTPUT ",\n /* 0x%04x */\n%s", $i, DUMP_ARRAY( "0x%04x", 0, @table );
1337 printf OUTPUT "\n};\n\nconst unsigned int unicode_compose_table_size = %d;\n\n", $count;
1339 ######### decomposition table
1341 # first determine all the 16-char subsets that contain something
1343 @filled = (0) x 4096;
1344 $pos = 16*2; # for the null subset
1345 for (my $i = 0; $i < 65536; $i++)
1347 next unless defined $decomp_table[$i];
1348 $filled[$i >> 4] = $pos;
1349 $pos += 16*2;
1350 $i |= 15;
1352 my $total = $pos;
1354 # now count the 256-char subsets that contain something
1356 my @filled_idx = (256) x 256;
1357 $pos = 256 + 16;
1358 for (my $i = 0; $i < 4096; $i++)
1360 next unless $filled[$i];
1361 $filled_idx[$i >> 4] = $pos;
1362 $pos += 16;
1363 $i |= 15;
1365 my $null_offset = $pos; # null mapping
1366 $total += $pos;
1368 # add the index offsets to the subsets positions
1370 for (my $i = 0; $i < 4096; $i++)
1372 next unless $filled[$i];
1373 $filled[$i] += $null_offset;
1376 # dump the main index
1378 printf OUTPUT "const WCHAR unicode_decompose_table[%d] =\n", $total;
1379 printf OUTPUT "{\n /* index */\n";
1380 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, @filled_idx );
1381 printf OUTPUT ",\n /* null sub-index */\n%s", DUMP_ARRAY( "0x%04x", 0, ($null_offset) x 16 );
1383 # dump the second-level indexes
1385 for (my $i = 0; $i < 256; $i++)
1387 next unless ($filled_idx[$i] > 256);
1388 my @table = @filled[($i<<4)..($i<<4)+15];
1389 for (my $j = 0; $j < 16; $j++) { $table[$j] ||= $null_offset; }
1390 printf OUTPUT ",\n /* sub-index %02x */\n", $i;
1391 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, @table );
1394 # dump the 16-char subsets
1396 printf OUTPUT ",\n /* null mapping */\n";
1397 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, (0) x 32 );
1399 for (my $i = 0; $i < 4096; $i++)
1401 next unless $filled[$i];
1402 my @table = (0) x 32;
1403 for (my $j = 0; $j < 16; $j++)
1405 if (defined $decomp_table[($i<<4) + $j])
1407 $table[2 * $j] = ${$decomp_table[($i << 4) + $j]}[0];
1408 $table[2 * $j + 1] = ${$decomp_table[($i << 4) + $j]}[1];
1411 printf OUTPUT ",\n /* 0x%03x0 .. 0x%03xf */\n", $i, $i;
1412 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, @table );
1415 printf OUTPUT "\n};\n";
1416 close OUTPUT;
1417 save_file($filename);
1421 ################################################################
1422 # handle a "bestfit" Windows mapping file
1424 sub handle_bestfit_file($$$)
1426 my ($filename, $has_glyphs, $comment) = @_;
1427 my $state = "";
1428 my ($codepage, $width, $def, $defw, $count);
1429 my ($lb_cur, $lb_end);
1430 my @lb_ranges = ();
1432 my $INPUT = open_data_file "$MAPPINGS/$filename" or die "Cannot open $filename";
1434 while (<$INPUT>)
1436 next if /^;/; # skip comments
1437 next if /^\s*$/; # skip empty lines
1438 next if /\x1a/; # skip ^Z
1439 last if /^ENDCODEPAGE/;
1441 if (/^CODEPAGE\s+(\d+)/)
1443 $codepage = $1;
1444 next;
1446 if (/^CPINFO\s+(\d+)\s+0x([0-9a-fA-f]+)\s+0x([0-9a-fA-F]+)/)
1448 $width = $1;
1449 $def = hex $2;
1450 $defw = hex $3;
1451 next;
1453 if (/^(MBTABLE|WCTABLE|DBCSRANGE|DBCSTABLE)\s+(\d+)/)
1455 $state = $1;
1456 $count = $2;
1457 next;
1459 if (/^0x([0-9a-fA-F]+)\s+0x([0-9a-fA-F]+)/)
1461 if ($state eq "MBTABLE")
1463 my $cp = hex $1;
1464 my $uni = hex $2;
1465 $cp2uni[$cp] = $uni unless defined($cp2uni[$cp]);
1466 next;
1468 if ($state eq "WCTABLE")
1470 my $uni = hex $1;
1471 my $cp = hex $2;
1472 $uni2cp[$uni] = $cp unless defined($uni2cp[$uni]);
1473 next;
1475 if ($state eq "DBCSRANGE")
1477 my $start = hex $1;
1478 my $end = hex $2;
1479 push @lb_ranges, $start, $end;
1480 for (my $i = $start; $i <= $end; $i++)
1482 push @lead_bytes, $i;
1483 $cp2uni[$i] = 0;
1485 $lb_cur = $start;
1486 $lb_end = $end;
1487 next;
1489 if ($state eq "DBCSTABLE")
1491 my $mb = hex $1;
1492 my $uni = hex $2;
1493 my $cp = ($lb_cur << 8) | $mb;
1494 $cp2uni[$cp] = $uni unless defined($cp2uni[$cp]);
1495 if (!--$count)
1497 if (++$lb_cur > $lb_end) { $state = "DBCSRANGE"; }
1499 next;
1502 die "$filename: Unrecognized line $_\n";
1504 close $INPUT;
1506 my $output = sprintf "c_%03d.c", $codepage;
1507 open OUTPUT,">$output.new" or die "Cannot create $output";
1509 printf "Building %s from %s (%s)\n", $output, $filename, $comment;
1511 # dump all tables
1513 printf OUTPUT "/* code page %03d (%s) */\n", $codepage, $comment;
1514 printf OUTPUT "/* generated from $MAPPINGS/$filename */\n";
1515 printf OUTPUT "/* DO NOT EDIT!! */\n\n";
1516 printf OUTPUT "#include \"wine/unicode.h\"\n\n";
1518 if ($width == 1) { dump_sbcs_table( $codepage, $has_glyphs, $comment, $def, $defw ); }
1519 else { dump_dbcs_table( $codepage, $comment, $def, $defw, @lb_ranges ); }
1520 close OUTPUT;
1521 save_file($output);
1525 ################################################################
1526 # read an input file and generate the corresponding .c file
1527 sub HANDLE_FILE(@)
1529 my ($codepage,$filename,$has_glyphs,$comment) = @_;
1531 @cp2uni = ();
1532 @lead_bytes = ();
1533 @uni2cp = ();
1535 # symbol codepage file is special
1536 if ($codepage == 20932) { READ_JIS0208_FILE "$MAPPINGS/$filename"; }
1537 elsif ($codepage == 20127) { fill_20127_codepage(); }
1538 elsif ($filename =~ /\/bestfit/)
1540 handle_bestfit_file( $filename, $has_glyphs, $comment );
1541 return;
1543 else { READ_FILE "$MAPPINGS/$filename"; }
1545 ADD_DEFAULT_MAPPINGS();
1547 my $output = sprintf "c_%03d.c", $codepage;
1548 open OUTPUT,">$output.new" or die "Cannot create $output";
1550 printf "Building %s from %s (%s)\n", $output, $filename || "hardcoded data", $comment;
1552 # dump all tables
1554 printf OUTPUT "/* code page %03d (%s) */\n", $codepage, $comment;
1555 if ($filename)
1557 print OUTPUT "/* generated from $MAPPINGS/$filename */\n";
1558 print OUTPUT "/* DO NOT EDIT!! */\n\n";
1560 else
1562 printf OUTPUT "/* Automatically generated; DO NOT EDIT!! */\n\n";
1564 printf OUTPUT "#include \"wine/unicode.h\"\n\n";
1566 if (!@lead_bytes) { dump_sbcs_table( $codepage, $has_glyphs, $comment, $DEF_CHAR, $DEF_CHAR ); }
1567 else { dump_dbcs_table( $codepage, $comment, $DEF_CHAR, $DEF_CHAR, get_lb_ranges() ); }
1568 close OUTPUT;
1569 save_file($output);
1573 ################################################################
1574 # save a file if modified
1575 sub save_file($)
1577 my $file = shift;
1578 if (-f $file && !system "cmp $file $file.new >/dev/null")
1580 unlink "$file.new";
1582 else
1584 rename "$file.new", "$file";
1589 ################################################################
1590 # output the list of codepage tables into the cptable.c file
1591 sub OUTPUT_CPTABLE()
1593 my @tables_decl = ();
1595 foreach my $file (@allfiles)
1597 my ($codepage,$filename,$comment) = @$file;
1598 push @tables_decl, sprintf("extern union cptable cptable_%03d;\n",$codepage);
1601 push @tables_decl, sprintf("\nstatic const union cptable * const cptables[%d] =\n{\n",$#allfiles+1);
1602 foreach my $file (@allfiles)
1604 my ($codepage,$filename,$comment) = @$file;
1605 push @tables_decl, sprintf(" &cptable_%03d,\n", $codepage);
1607 push @tables_decl, "};";
1608 REPLACE_IN_FILE( "cptable.c", @tables_decl );
1611 ################################################################
1612 # replace the contents of a file between ### cpmap ### marks
1614 sub REPLACE_IN_FILE($@)
1616 my $name = shift;
1617 my @data = @_;
1618 my @lines = ();
1619 open(FILE,$name) or die "Can't open $name";
1620 while (<FILE>)
1622 push @lines, $_;
1623 last if /\#\#\# cpmap begin \#\#\#/;
1625 push @lines, @data;
1626 while (<FILE>)
1628 if (/\#\#\# cpmap end \#\#\#/) { push @lines, "\n", $_; last; }
1630 push @lines, <FILE>;
1631 open(FILE,">$name.new") or die "Can't modify $name";
1632 print FILE @lines;
1633 close(FILE);
1634 save_file($name);
1637 ################################################################
1638 # main routine
1640 READ_DEFAULTS( $DEFAULTS );
1641 DUMP_CASE_MAPPINGS( "casemap.c" );
1642 DUMP_SORTKEYS( "collation.c", READ_SORTKEYS_FILE() );
1643 DUMP_COMPOSE_TABLES( "compose.c" );
1644 DUMP_CTYPE_TABLES( "wctype.c" );
1645 dump_mirroring( "../../dlls/usp10/mirror.c" );
1646 dump_shaping( "../../dlls/usp10/shaping.c" );
1647 dump_intl_nls("../../tools/l_intl.nls");
1649 foreach my $file (@allfiles) { HANDLE_FILE( @{$file} ); }
1651 OUTPUT_CPTABLE();
1653 exit 0;
1655 # Local Variables:
1656 # compile-command: "./cpmap.pl && make -k"
1657 # End: