msi/tests: Skip some package tests if in a non-administrator, non-power user account.
[wine/zf.git] / tools / make_unicode
blob66865ddb7a2ebad6637e723b6f6683a85e44ab80
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/6.3.0/ucd";
27 my $REPORTS = "http://www.unicode.org/reports";
28 my $RFCS = "http://www.rfc-editor.org/rfc";
29 my $VERTICALDATA = "http://www.unicode.org/Public/vertical/revision-11";
31 # Sort keys file
32 my $SORTKEYS = "tr10/allkeys.txt";
34 # RFC3454 (stringprep data)
35 my $STRINGPREP = "rfc3454.txt";
37 # Defaults mapping
38 my $DEFAULTS = "tools/unicode-defaults";
40 # Default char for undefined mappings
41 my $DEF_CHAR = ord '?';
43 my @allfiles =
45 [ 37, "VENDORS/MICSFT/EBCDIC/CP037.TXT", 0, "IBM EBCDIC US Canada", 0, 0x6f ],
46 [ 424, "VENDORS/MISC/CP424.TXT", 0, "IBM EBCDIC Hebrew" ],
47 [ 437, "VENDORS/MICSFT/PC/CP437.TXT", 1, "OEM United States" ],
48 [ 500, "VENDORS/MICSFT/EBCDIC/CP500.TXT", 0, "IBM EBCDIC International", 0, 0x6f ],
49 [ 737, "VENDORS/MICSFT/PC/CP737.TXT", 1, "OEM Greek 437G" ],
50 [ 775, "VENDORS/MICSFT/PC/CP775.TXT", 1, "OEM Baltic" ],
51 [ 850, "VENDORS/MICSFT/PC/CP850.TXT", 1, "OEM Multilingual Latin 1" ],
52 [ 852, "VENDORS/MICSFT/PC/CP852.TXT", 1, "OEM Slovak Latin 2" ],
53 [ 855, "VENDORS/MICSFT/PC/CP855.TXT", 1, "OEM Cyrillic" ],
54 [ 856, "VENDORS/MISC/CP856.TXT", 0, "Hebrew PC" ],
55 [ 857, "VENDORS/MICSFT/PC/CP857.TXT", 1, "OEM Turkish", 0xf8bb ],
56 [ 860, "VENDORS/MICSFT/PC/CP860.TXT", 1, "OEM Portuguese" ],
57 [ 861, "VENDORS/MICSFT/PC/CP861.TXT", 1, "OEM Icelandic" ],
58 [ 862, "VENDORS/MICSFT/PC/CP862.TXT", 1, "OEM Hebrew" ],
59 [ 863, "VENDORS/MICSFT/PC/CP863.TXT", 1, "OEM Canadian French" ],
60 [ 864, "VENDORS/MICSFT/PC/CP864.TXT", 0, "OEM Arabic", 0xf8bb ],
61 [ 865, "VENDORS/MICSFT/PC/CP865.TXT", 1, "OEM Nordic" ],
62 [ 866, "VENDORS/MICSFT/PC/CP866.TXT", 1, "OEM Russian" ],
63 [ 869, "VENDORS/MICSFT/PC/CP869.TXT", 1, "OEM Greek" ],
64 [ 874, "VENDORS/MICSFT/WindowsBestFit/bestfit874.txt", 1, "ANSI/OEM Thai" ],
65 [ 875, "VENDORS/MICSFT/EBCDIC/CP875.TXT", 0, "IBM EBCDIC Greek", 0, 0x6f ],
66 [ 878, "VENDORS/MISC/KOI8-R.TXT", 0, "Russian KOI8" ],
67 [ 932, "VENDORS/MICSFT/WindowsBestFit/bestfit932.txt", 0, "ANSI/OEM Japanese Shift-JIS" ],
68 [ 936, "VENDORS/MICSFT/WindowsBestFit/bestfit936.txt", 0, "ANSI/OEM Simplified Chinese GBK" ],
69 [ 949, "VENDORS/MICSFT/WindowsBestFit/bestfit949.txt", 0, "ANSI/OEM Korean Unified Hangul" ],
70 [ 950, "VENDORS/MICSFT/WindowsBestFit/bestfit950.txt", 0, "ANSI/OEM Traditional Chinese Big5" ],
71 [ 1006, "VENDORS/MISC/CP1006.TXT", 0, "IBM Arabic" ],
72 [ 1026, "VENDORS/MICSFT/EBCDIC/CP1026.TXT", 0, "IBM EBCDIC Latin 5 Turkish", 0, 0x6f ],
73 [ 1250, "VENDORS/MICSFT/WindowsBestFit/bestfit1250.txt", 0, "ANSI Eastern Europe" ],
74 [ 1251, "VENDORS/MICSFT/WindowsBestFit/bestfit1251.txt", 0, "ANSI Cyrillic" ],
75 [ 1252, "VENDORS/MICSFT/WindowsBestFit/bestfit1252.txt", 0, "ANSI Latin 1" ],
76 [ 1253, "VENDORS/MICSFT/WindowsBestFit/bestfit1253.txt", 0, "ANSI Greek" ],
77 [ 1254, "VENDORS/MICSFT/WindowsBestFit/bestfit1254.txt", 0, "ANSI Turkish" ],
78 [ 1255, "VENDORS/MICSFT/WindowsBestFit/bestfit1255.txt", 0, "ANSI Hebrew" ],
79 [ 1256, "VENDORS/MICSFT/WindowsBestFit/bestfit1256.txt", 0, "ANSI Arabic" ],
80 [ 1257, "VENDORS/MICSFT/WindowsBestFit/bestfit1257.txt", 0, "ANSI Baltic" ],
81 [ 1258, "VENDORS/MICSFT/WindowsBestFit/bestfit1258.txt", 0, "ANSI/OEM Viet Nam" ],
82 [ 1361, "VENDORS/MICSFT/WindowsBestFit/bestfit1361.txt", 0, "Korean Johab" ],
83 [ 10000, "VENDORS/MICSFT/MAC/ROMAN.TXT", 0, "Mac Roman" ],
84 [ 10001, "VENDORS/APPLE/JAPANESE.TXT", 0, "Mac Japanese" ],
85 [ 10002, "VENDORS/APPLE/CHINTRAD.TXT", 0, "Mac Traditional Chinese" ],
86 [ 10003, "VENDORS/APPLE/KOREAN.TXT", 0, "Mac Korean", 0xf8e5 ],
87 [ 10004, "VENDORS/APPLE/ARABIC.TXT", 0, "Mac Arabic", 0xf826 ],
88 [ 10005, "VENDORS/APPLE/HEBREW.TXT", 0, "Mac Hebrew", 0xf7fa ],
89 [ 10006, "VENDORS/MICSFT/MAC/GREEK.TXT", 0, "Mac Greek" ],
90 [ 10007, "VENDORS/MICSFT/MAC/CYRILLIC.TXT", 0, "Mac Cyrillic" ],
91 [ 10008, "VENDORS/APPLE/CHINSIMP.TXT", 0, "Mac Simplified Chinese" ],
92 [ 10010, "VENDORS/APPLE/ROMANIAN.TXT", 0, "Mac Romanian" ],
93 [ 10017, "VENDORS/APPLE/CYRILLIC.TXT", 0, "Mac Ukrainian" ],
94 [ 10021, "VENDORS/APPLE/THAI.TXT", 0, "Mac Thai" ],
95 [ 10029, "VENDORS/MICSFT/MAC/LATIN2.TXT", 0, "Mac Latin 2" ],
96 [ 10079, "VENDORS/MICSFT/MAC/ICELAND.TXT", 0, "Mac Icelandic" ],
97 [ 10081, "VENDORS/MICSFT/MAC/TURKISH.TXT", 0, "Mac Turkish", 0xf89f ],
98 [ 10082, "VENDORS/APPLE/CROATIAN.TXT", 0, "Mac Croatian" ],
99 [ 20127, undef, 0, "US-ASCII (7bit)" ],
100 [ 20866, "VENDORS/MISC/KOI8-R.TXT", 0, "Russian KOI8" ],
101 [ 20932, "OBSOLETE/EASTASIA/JIS/JIS0208.TXT", 0, "EUC-JP", 0, 0x3f, 0x30fb ],
102 [ 21866, "VENDORS/MISC/KOI8-U.TXT", 0, "Ukrainian KOI8" ],
103 [ 28591, "ISO8859/8859-1.TXT", 0, "ISO 8859-1 Latin 1" ],
104 [ 28592, "ISO8859/8859-2.TXT", 0, "ISO 8859-2 Latin 2 (East European)" ],
105 [ 28593, "ISO8859/8859-3.TXT", 0, "ISO 8859-3 Latin 3 (South European)", 0xf7f5 ],
106 [ 28594, "ISO8859/8859-4.TXT", 0, "ISO 8859-4 Latin 4 (Baltic old)" ],
107 [ 28595, "ISO8859/8859-5.TXT", 0, "ISO 8859-5 Cyrillic" ],
108 [ 28596, "ISO8859/8859-6.TXT", 0, "ISO 8859-6 Arabic", 0xf7c8 ],
109 [ 28597, "ISO8859/8859-7.TXT", 0, "ISO 8859-7 Greek", 0xf7c5 ],
110 [ 28598, "ISO8859/8859-8.TXT", 0, "ISO 8859-8 Hebrew", 0xf79c ],
111 [ 28599, "ISO8859/8859-9.TXT", 0, "ISO 8859-9 Latin 5 (Turkish)" ],
112 [ 28600, "ISO8859/8859-10.TXT", 0, "ISO 8859-10 Latin 6 (Nordic)" ],
113 [ 28603, "ISO8859/8859-13.TXT", 0, "ISO 8859-13 Latin 7 (Baltic)" ],
114 [ 28604, "ISO8859/8859-14.TXT", 0, "ISO 8859-14 Latin 8 (Celtic)" ],
115 [ 28605, "ISO8859/8859-15.TXT", 0, "ISO 8859-15 Latin 9 (Euro)" ],
116 [ 28606, "ISO8859/8859-16.TXT", 0, "ISO 8859-16 Latin 10 (Balkan)" ]
120 my %ctype =
122 "upper" => 0x0001,
123 "lower" => 0x0002,
124 "digit" => 0x0004,
125 "space" => 0x0008,
126 "punct" => 0x0010,
127 "cntrl" => 0x0020,
128 "blank" => 0x0040,
129 "xdigit" => 0x0080,
130 "alpha" => 0x0100,
131 "defin" => 0x0200
134 my %indic_types =
136 "Other" => 0x0000,
137 "Bindu" => 0x0001,
138 "Visarga" => 0x0002,
139 "Avagraha" => 0x0003,
140 "Nukta" => 0x0004,
141 "Virama" => 0x0005,
142 "Vowel_Independent" => 0x0006,
143 "Vowel_Dependent" => 0x0007,
144 "Vowel" => 0x0008,
145 "Consonant_Placeholder" => 0x0009,
146 "Consonant" => 0x000a,
147 "Consonant_Dead" => 0x000b,
148 "Consonant_Repha" => 0x000c,
149 "Consonant_Subjoined" => 0x000d,
150 "Consonant_Medial" => 0x000e,
151 "Consonant_Final" => 0x000f,
152 "Consonant_Head_Letter" => 0x0010,
153 "Modifying_Letter" => 0x0011,
154 "Tone_Letter" => 0x0012,
155 "Tone_Mark" => 0x0013,
156 "Register_Shifter" => 0x0014
159 my %matra_types =
161 "Right" => 0x01,
162 "Left" => 0x02,
163 "Visual_Order_Left" => 0x03,
164 "Left_And_Right" => 0x04,
165 "Top" => 0x05,
166 "Bottom" => 0x06,
167 "Top_And_Bottom" => 0x07,
168 "Top_And_Right" => 0x08,
169 "Top_And_Left" => 0x09,
170 "Top_And_Left_And_Right" => 0x0a,
171 "Bottom_And_Right" => 0x0b,
172 "Top_And_Bottom_And_Right" => 0x0c,
173 "Overstruck" => 0x0d,
174 "Invisible" => 0x0e
177 my %nameprep_flags =
179 "unassigned" => 0x01,
180 "prohibited" => 0x02,
181 "bidi_ral" => 0x04,
182 "bidi_l" => 0x08
185 my %break_types =
187 "BK" => 0x0001,
188 "CR" => 0x0002,
189 "LF" => 0x0003,
190 "CM" => 0x0004,
191 "SG" => 0x0005,
192 "GL" => 0x0006,
193 "CB" => 0x0007,
194 "SP" => 0x0008,
195 "ZW" => 0x0009,
196 "NL" => 0x000a,
197 "WJ" => 0x000b,
198 "JL" => 0x000c,
199 "JV" => 0x000d,
200 "JT" => 0x000e,
201 "H2" => 0x000f,
202 "H3" => 0x0010,
203 "XX" => 0x0011,
204 "OP" => 0x0012,
205 "CL" => 0x0013,
206 "CP" => 0x0014,
207 "QU" => 0x0015,
208 "NS" => 0x0016,
209 "EX" => 0x0017,
210 "SY" => 0x0018,
211 "IS" => 0x0019,
212 "PR" => 0x001a,
213 "PO" => 0x001b,
214 "NU" => 0x001c,
215 "AL" => 0x001d,
216 "ID" => 0x001e,
217 "IN" => 0x001f,
218 "HY" => 0x0020,
219 "BB" => 0x0021,
220 "BA" => 0x0022,
221 "SA" => 0x0023,
222 "AI" => 0x0024,
223 "B2" => 0x0025,
224 "HL" => 0x0026,
225 "CJ" => 0x0027,
226 "RI" => 0x0028,
229 my %vertical_types =
231 "R" => 0x0000,
232 "U" => 0x0001,
233 "Tr" => 0x0002,
234 "Tu" => 0x0003,
237 my %categories =
239 "Lu" => $ctype{"defin"}|$ctype{"alpha"}|$ctype{"upper"}, # Letter, Uppercase
240 "Ll" => $ctype{"defin"}|$ctype{"alpha"}|$ctype{"lower"}, # Letter, Lowercase
241 "Lt" => $ctype{"defin"}|$ctype{"alpha"}|$ctype{"upper"}|$ctype{"lower"}, # Letter, Titlecase
242 "Mn" => $ctype{"defin"}, # Mark, Non-Spacing
243 "Mc" => $ctype{"defin"}, # Mark, Spacing Combining
244 "Me" => $ctype{"defin"}, # Mark, Enclosing
245 "Nd" => $ctype{"defin"}|$ctype{"digit"}, # Number, Decimal Digit
246 "Nl" => $ctype{"defin"}|$ctype{"alpha"}, # Number, Letter
247 "No" => $ctype{"defin"}, # Number, Other
248 "Zs" => $ctype{"defin"}|$ctype{"space"}, # Separator, Space
249 "Zl" => $ctype{"defin"}|$ctype{"space"}, # Separator, Line
250 "Zp" => $ctype{"defin"}|$ctype{"space"}, # Separator, Paragraph
251 "Cc" => $ctype{"defin"}|$ctype{"cntrl"}, # Other, Control
252 "Cf" => $ctype{"defin"}|$ctype{"cntrl"}, # Other, Format
253 "Cs" => $ctype{"defin"}, # Other, Surrogate
254 "Co" => $ctype{"defin"}, # Other, Private Use
255 "Cn" => $ctype{"defin"}, # Other, Not Assigned
256 "Lm" => $ctype{"defin"}|$ctype{"alpha"}, # Letter, Modifier
257 "Lo" => $ctype{"defin"}|$ctype{"alpha"}, # Letter, Other
258 "Pc" => $ctype{"defin"}|$ctype{"punct"}, # Punctuation, Connector
259 "Pd" => $ctype{"defin"}|$ctype{"punct"}, # Punctuation, Dash
260 "Ps" => $ctype{"defin"}|$ctype{"punct"}, # Punctuation, Open
261 "Pe" => $ctype{"defin"}|$ctype{"punct"}, # Punctuation, Close
262 "Pi" => $ctype{"defin"}|$ctype{"punct"}, # Punctuation, Initial quote
263 "Pf" => $ctype{"defin"}|$ctype{"punct"}, # Punctuation, Final quote
264 "Po" => $ctype{"defin"}|$ctype{"punct"}, # Punctuation, Other
265 "Sm" => $ctype{"defin"}, # Symbol, Math
266 "Sc" => $ctype{"defin"}, # Symbol, Currency
267 "Sk" => $ctype{"defin"}, # Symbol, Modifier
268 "So" => $ctype{"defin"} # Symbol, Other
271 # a few characters need additional categories that cannot be determined automatically
272 my %special_categories =
274 "xdigit" => [ ord('0')..ord('9'),ord('A')..ord('F'),ord('a')..ord('f'),
275 0xff10..0xff19, 0xff21..0xff26, 0xff41..0xff46 ],
276 "space" => [ 0x09..0x0d, 0x85 ],
277 "blank" => [ 0x09, 0x20, 0xa0, 0x3000, 0xfeff ],
278 "cntrl" => [ 0x070f, 0x200c, 0x200d,
279 0x200e, 0x200f, 0x202a, 0x202b, 0x202c, 0x202d, 0x202e,
280 0x206a, 0x206b, 0x206c, 0x206d, 0x206e, 0x206f, 0xfeff,
281 0xfff9, 0xfffa, 0xfffb ],
282 "punct" => [ 0x24, 0x2b, 0x3c..0x3e, 0x5e, 0x60, 0x7c, 0x7e, 0xa2..0xbe,
283 0xd7, 0xf7 ],
284 "digit" => [ 0xb2, 0xb3, 0xb9 ],
285 "lower" => [ 0x2071, 0x207f ]
288 my %directions =
290 "L" => 1, # Left-to-Right
291 "R" => 2, # Right-to-Left
292 "AL" => 12, # Right-to-Left Arabic
293 "EN" => 3, # European Number
294 "ES" => 4, # European Number Separator
295 "ET" => 5, # European Number Terminator
296 "AN" => 6, # Arabic Number
297 "CS" => 7, # Common Number Separator
298 "NSM" => 13, # Non-Spacing Mark
299 "BN" => 14, # Boundary Neutral
300 "B" => 8, # Paragraph Separator
301 "S" => 9, # Segment Separator
302 "WS" => 10, # Whitespace
303 "ON" => 11, # Other Neutrals
304 "LRE" => 15, # Left-to-Right Embedding
305 "LRO" => 15, # Left-to-Right Override
306 "RLE" => 15, # Right-to-Left Embedding
307 "RLO" => 15, # Right-to-Left Override
308 "PDF" => 15, # Pop Directional Format
309 "LRI" => 15, # Left-to-Right Isolate
310 "RLI" => 15, # Right-to-Left Isolate
311 "FSI" => 15, # First Strong Isolate
312 "PDI" => 15 # Pop Directional Isolate
315 my %joining_types =
317 "U" => 0, # Non_Joining
318 "T" => 1, # Transparent
319 "R" => 2, # Right_Joining
320 "L" => 3, # Left_Joining
321 "D" => 4, # Dual_Joining
322 "C" => 5, # Join_Causing
325 my @cp2uni = ();
326 my @lead_bytes = ();
327 my @uni2cp = ();
328 my @unicode_defaults = ();
329 my @unicode_aliases = ();
330 my @tolower_table = ();
331 my @toupper_table = ();
332 my @digitmap_table = ();
333 my @compatmap_table = ();
334 my @category_table = (0) x 65536;
335 my @joining_table = (0) x 65536;
336 my @direction_table = ();
337 my @decomp_table = ();
338 my @compose_table = ();
339 my $default_char;
340 my $default_wchar;
342 my %joining_forms =
344 "isolated" => [],
345 "final" => [],
346 "initial" => [],
347 "medial" => []
350 ################################################################
351 # fetch a unicode.org file and open it
352 sub open_data_file($$)
354 my ($base, $name) = @_;
355 (my $dir = "data/$name") =~ s/\/[^\/]+$//;
356 local *FILE;
357 unless (-f "data/$name")
359 print "Fetching $base/$name...\n";
360 system "mkdir", "-p", $dir;
361 !system "wget", "-q", "-O", "data/$name", "$base/$name" or die "cannot fetch $base/$name";
363 open FILE, "<data/$name" or die "cannot open data/$name";
364 return *FILE;
367 ################################################################
368 # read in the defaults file
369 sub READ_DEFAULTS($)
371 my $filename = shift;
372 my $start;
374 # first setup a few default mappings
376 open DEFAULTS, "$filename" or die "Cannot open $filename";
377 print "Loading $filename\n";
378 while (<DEFAULTS>)
380 next if /^\#/; # skip comments
381 next if /^$/; # skip empty lines
382 if (/^(([0-9a-fA-F]+)(,[0-9a-fA-F]+)*)\s+([0-9a-fA-F]+|'.'|none)\s+(\#.*)?/)
384 my @src = map hex, split /,/,$1;
385 my $dst = $4;
386 my $comment = $5;
387 if ($#src > 0) { push @unicode_aliases, \@src; }
388 next if ($dst eq "none");
389 $dst = ($dst =~ /\'.\'/) ? ord substr($dst,1,1) : hex $dst;
390 foreach my $src (@src)
392 die "Duplicate value" if defined($unicode_defaults[$src]);
393 $unicode_defaults[$src] = $dst;
395 next;
397 die "Unrecognized line $_\n";
399 close DEFAULTS;
401 # now build mappings from the decomposition field of the Unicode database
403 my $UNICODE_DATA = open_data_file( $UNIDATA, "UnicodeData.txt" );
404 while (<$UNICODE_DATA>)
406 # Decode the fields ...
407 my ($code, $name, $cat, $comb, $bidi,
408 $decomp, $dec, $dig, $num, $mirror,
409 $oldname, $comment, $upper, $lower, $title) = split /;/;
410 my $dst;
411 my $src = hex $code;
413 die "unknown category $cat" unless defined $categories{$cat};
414 die "unknown directionality $bidi" unless defined $directions{$bidi};
416 $category_table[$src] = $categories{$cat};
417 $direction_table[$src] = $directions{$bidi};
418 $joining_table[$src] = $joining_types{"T"} if $cat eq "Mn" || $cat eq "Me" || $cat eq "Cf";
420 if ($lower ne "")
422 $tolower_table[$src] = hex $lower;
424 if ($upper ne "")
426 $toupper_table[$src] = hex $upper;
428 if ($dec ne "")
430 $category_table[$src] |= $ctype{"digit"};
432 if ($dig ne "")
434 $digitmap_table[$src] = ord $dig;
437 # copy the category and direction for everything between First/Last pairs
438 if ($name =~ /, First>/) { $start = $src; }
439 if ($name =~ /, Last>/)
441 while ($start < $src)
443 $category_table[$start] = $category_table[$src];
444 $direction_table[$start] = $direction_table[$src];
445 $start++;
449 next if $decomp eq ""; # no decomposition, skip it
451 if ($decomp =~ /^<([a-zA-Z]+)>\s+([0-9a-fA-F]+)$/)
453 # decomposition of the form "<foo> 1234" -> use char if type is known
454 if (($src >= 0xf900 && $src < 0xfb00) || ($src >= 0xfe30 && $src < 0xfffd))
456 # Single char decomposition in the compatibility range
457 $compatmap_table[$src] = hex $2;
459 if ($1 eq "isolated" || $1 eq "final" || $1 eq "initial" || $1 eq "medial")
461 ${joining_forms{$1}}[hex $2] = $src;
462 next;
464 next unless ($1 eq "font" ||
465 $1 eq "noBreak" ||
466 $1 eq "circle" ||
467 $1 eq "super" ||
468 $1 eq "sub" ||
469 $1 eq "wide" ||
470 $1 eq "narrow" ||
471 $1 eq "compat" ||
472 $1 eq "small");
473 $dst = hex $2;
475 elsif ($decomp =~ /^<compat>\s+0020\s+([0-9a-fA-F]+)/)
477 # decomposition "<compat> 0020 1234" -> combining accent
478 $dst = hex $1;
480 elsif ($decomp =~ /^([0-9a-fA-F]+)/)
482 # decomposition contains only char values without prefix -> use first char
483 $dst = hex $1;
484 $category_table[$src] |= $category_table[$dst] if defined $category_table[$dst];
485 # store decomposition if it contains two chars
486 if ($decomp =~ /^([0-9a-fA-F]+)\s+([0-9a-fA-F]+)$/)
488 $decomp_table[$src] = [ hex $1, hex $2 ];
489 push @compose_table, [ hex $1, hex $2, $src ];
491 elsif ($decomp =~ /^(<[a-z]+>\s)*([0-9a-fA-F]+)$/ &&
492 (($src >= 0xf900 && $src < 0xfb00) || ($src >= 0xfe30 && $src < 0xfffd)))
494 # Single char decomposition in the compatibility range
495 $compatmap_table[$src] = hex $2;
498 else
500 next;
503 next if defined($unicode_defaults[$src]); # may have been set in the defaults file
505 # check for loops
506 for (my $i = $dst; ; $i = $unicode_defaults[$i])
508 die sprintf("loop detected for %04x -> %04x",$src,$dst) if $i == $src;
509 last unless defined($unicode_defaults[$i]);
511 $unicode_defaults[$src] = $dst;
513 close $UNICODE_DATA;
515 # patch the category of some special characters
517 foreach my $cat (keys %special_categories)
519 my $flag = $ctype{$cat};
520 foreach my $i (@{$special_categories{$cat}}) { $category_table[$i] |= $flag; }
525 ################################################################
526 # define a new lead byte
527 sub add_lead_byte($)
529 my $ch = shift;
530 push @lead_bytes, $ch;
531 $cp2uni[$ch] = 0;
534 ################################################################
535 # define a new char mapping
536 sub add_mapping($$)
538 my ($cp, $uni) = @_;
539 $cp2uni[$cp] = $uni unless defined($cp2uni[$cp]);
540 $uni2cp[$uni] = $cp unless defined($uni2cp[$uni]);
541 if ($cp > 0xff && !defined($cp2uni[$cp >> 8]))
543 add_lead_byte( $cp >> 8 );
547 ################################################################
548 # parse a standard codepage file
549 sub read_codepage_file($)
551 my $name = shift;
552 my $INPUT = open_data_file( $MAPPINGS, $name );
554 while (<$INPUT>)
556 next if /^\#/; # skip comments
557 next if /^$/; # skip empty lines
558 next if /\x1a/; # skip ^Z
559 next if (/^0x([0-9a-fA-F]+)\s+\#UNDEFINED/); # undefined char
561 if (/^0x([0-9a-fA-F]+)\s+\#DBCS LEAD BYTE/)
563 add_lead_byte( hex $1 );
564 next;
566 # 0x12 0x3456
567 if (/^0x([0-9a-fA-F]+)\s+0x([0-9a-fA-F]+)\s+(\#.*)?/)
569 add_mapping( hex $1, hex $2 );
570 next;
572 # 0x12 0x3456+0x7890
573 if (/^0x([0-9a-fA-F]+)\s+0x([0-9a-fA-F]+)\+.*\s+(\#.*)?/)
575 add_mapping( hex $1, hex $2 );
576 next;
578 # 0x12 <LR>+0x3456
579 if (/^0x([0-9a-fA-F]+)\s+<LR>\+0x([0-9a-fA-F]+)\s+(\#.*)?/)
581 add_mapping( hex $1, hex $2 );
582 next;
584 # 0x12 <RL>+0x3456
585 if (/^0x([0-9a-fA-F]+)\s+<RL>\+0x([0-9a-fA-F]+)\s+(\#.*)?/)
587 my $uni = hex $2;
588 add_mapping( hex $1, $uni ) if $uni >= 0x80;
589 next;
591 die "$name: Unrecognized line $_\n";
593 close $INPUT;
597 ################################################################
598 # fill input data for the 20127 (us-ascii) codepage
599 sub fill_20127_codepage()
601 for (my $i = 0; $i < 128; $i++) { $cp2uni[$i] = $uni2cp[$i] = $i; }
602 for (my $i = 128; $i < 256; $i++) { $cp2uni[$i] = $i & 0x7f; }
605 ################################################################
606 # get a mapping including glyph chars for MB_USEGLYPHCHARS
608 sub get_glyphs_mapping(@)
610 $_[0x01] = 0x263a; # (WHITE SMILING FACE)
611 $_[0x02] = 0x263b; # (BLACK SMILING FACE)
612 $_[0x03] = 0x2665; # (BLACK HEART SUIT)
613 $_[0x04] = 0x2666; # (BLACK DIAMOND SUIT)
614 $_[0x05] = 0x2663; # (BLACK CLUB SUIT)
615 $_[0x06] = 0x2660; # (BLACK SPADE SUIT)
616 $_[0x07] = 0x2022; # (BULLET)
617 $_[0x08] = 0x25d8; # (INVERSE BULLET)
618 $_[0x09] = 0x25cb; # (WHITE CIRCLE)
619 $_[0x0a] = 0x25d9; # (INVERSE WHITE CIRCLE)
620 $_[0x0b] = 0x2642; # (MALE SIGN)
621 $_[0x0c] = 0x2640; # (FEMALE SIGN)
622 $_[0x0d] = 0x266a; # (EIGHTH NOTE)
623 $_[0x0e] = 0x266b; # (BEAMED EIGHTH NOTES)
624 $_[0x0f] = 0x263c; # (WHITE SUN WITH RAYS)
625 $_[0x10] = 0x25ba; # (BLACK RIGHT-POINTING POINTER)
626 $_[0x11] = 0x25c4; # (BLACK LEFT-POINTING POINTER)
627 $_[0x12] = 0x2195; # (UP DOWN ARROW)
628 $_[0x13] = 0x203c; # (DOUBLE EXCLAMATION MARK)
629 $_[0x14] = 0x00b6; # (PILCROW SIGN)
630 $_[0x15] = 0x00a7; # (SECTION SIGN)
631 $_[0x16] = 0x25ac; # (BLACK RECTANGLE)
632 $_[0x17] = 0x21a8; # (UP DOWN ARROW WITH BASE)
633 $_[0x18] = 0x2191; # (UPWARDS ARROW)
634 $_[0x19] = 0x2193; # (DOWNWARDS ARROW)
635 $_[0x1a] = 0x2192; # (RIGHTWARDS ARROW)
636 $_[0x1b] = 0x2190; # (LEFTWARDS ARROW)
637 $_[0x1c] = 0x221f; # (RIGHT ANGLE)
638 $_[0x1d] = 0x2194; # (LEFT RIGHT ARROW)
639 $_[0x1e] = 0x25b2; # (BLACK UP-POINTING TRIANGLE)
640 $_[0x1f] = 0x25bc; # (BLACK DOWN-POINTING TRIANGLE)
641 $_[0x7f] = 0x2302; # (HOUSE)
642 return @_;
645 ################################################################
646 # build EUC-JP table from the JIS 0208 file
647 # FIXME: for proper EUC-JP we should probably read JIS 0212 too
648 # but this would require 3-byte DBCS characters
649 sub READ_JIS0208_FILE($)
651 my $name = shift;
653 # ASCII chars
654 for (my $i = 0x00; $i <= 0x7f; $i++)
656 $cp2uni[$i] = $i;
657 $uni2cp[$i] = $i;
660 # JIS X 0201 right plane
661 for (my $i = 0xa1; $i <= 0xdf; $i++)
663 $cp2uni[0x8e00 + $i] = 0xfec0 + $i;
664 $uni2cp[0xfec0 + $i] = 0x8e00 + $i;
667 # lead bytes
668 foreach my $i (0x8e, 0x8f, 0xa1 .. 0xfe)
670 push @lead_bytes,$i;
671 $cp2uni[$i] = 0;
674 # undefined chars
675 foreach my $i (0x80 .. 0x8d, 0x90 .. 0xa0, 0xff)
677 $cp2uni[$i] = $DEF_CHAR;
680 # Shift-JIS compatibility
681 $uni2cp[0x00a5] = 0x5c;
682 $uni2cp[0x203e] = 0x7e;
684 # Fix backslash conversion
685 $cp2uni[0xa1c0] = 0xff3c;
686 $uni2cp[0xff3c] = 0xa1c0;
688 my $INPUT = open_data_file( $MAPPINGS, $name );
689 while (<$INPUT>)
691 next if /^\#/; # skip comments
692 next if /^$/; # skip empty lines
693 next if /\x1a/; # skip ^Z
694 if (/^0x[0-9a-fA-F]+\s+0x([0-9a-fA-F]+)\s+0x([0-9a-fA-F]+)\s+(\#.*)?/)
696 my $cp = 0x8080 + hex $1;
697 my $uni = hex $2;
698 $cp2uni[$cp] = $uni unless defined($cp2uni[$cp]);
699 $uni2cp[$uni] = $cp unless defined($uni2cp[$uni]);
700 next;
702 die "$name: Unrecognized line $_\n";
704 close $INPUT;
708 ################################################################
709 # build the sort keys table
710 sub READ_SORTKEYS_FILE()
712 my @sortkeys = ();
713 for (my $i = 0; $i < 65536; $i++) { $sortkeys[$i] = [ -1, 0, 0, 0, 0 ] };
715 my $INPUT = open_data_file( $REPORTS, $SORTKEYS );
716 while (<$INPUT>)
718 next if /^\#/; # skip comments
719 next if /^$/; # skip empty lines
720 next if /\x1a/; # skip ^Z
721 next if /^\@version/; # skip @version header
722 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]+)\]/)
724 my ($uni,$variable) = (hex $1, $2);
725 next if $uni > 65535;
726 $sortkeys[$uni] = [ $uni, hex $3, hex $4, hex $5, hex $6 ];
727 next;
729 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]+)\]/)
731 # multiple character sequence, ignored for now
732 next;
734 die "$SORTKEYS: Unrecognized line $_\n";
736 close $INPUT;
738 # compress the keys to 32 bit:
739 # key 1 to 16 bits, key 2 to 8 bits, key 3 to 4 bits, key 4 to 1 bit
741 @sortkeys = sort { ${$a}[1] <=> ${$b}[1] or
742 ${$a}[2] <=> ${$b}[2] or
743 ${$a}[3] <=> ${$b}[3] or
744 ${$a}[4] <=> ${$b}[4] or
745 $a cmp $b; } @sortkeys;
747 my ($n2, $n3) = (1, 1);
748 my @keys = (-1, -1, -1, -1, -1 );
749 my @flatkeys = ();
751 for (my $i = 0; $i < 65536; $i++)
753 my @current = @{$sortkeys[$i]};
754 next if $current[0] == -1;
755 if ($current[1] == $keys[1])
757 if ($current[2] == $keys[2])
759 if ($current[3] == $keys[3])
761 # nothing
763 else
765 $keys[3] = $current[3];
766 $n3++;
767 die if ($n3 >= 16);
770 else
772 $keys[2] = $current[2];
773 $keys[3] = $current[3];
774 $n2++;
775 $n3 = 1;
776 die if ($n2 >= 256);
779 else
781 $keys[1] = $current[1];
782 $keys[2] = $current[2];
783 $keys[3] = $current[3];
784 $n2 = 1;
785 $n3 = 1;
788 if ($current[2]) { $current[2] = $n2; }
789 if ($current[3]) { $current[3] = $n3; }
790 if ($current[4]) { $current[4] = 1; }
792 $flatkeys[$current[0]] = ($current[1] << 16) | ($current[2] << 8) | ($current[3] << 4) | $current[4];
794 return @flatkeys;
798 ################################################################
799 # build the sort keys table
800 sub DUMP_SORTKEYS($@)
802 my ($filename, @keys) = @_;
804 # count the number of 256-key ranges that contain something
806 my @offsets = ();
807 my $ranges = 2;
808 for (my $i = 0; $i < 256; $i++) { $offsets[$i] = 256; }
809 for (my $i = 0; $i < 65536; $i++)
811 next unless defined $keys[$i];
812 $offsets[$i >> 8] = $ranges * 256;
813 $ranges++;
814 $i |= 255;
817 # output the range offsets
819 open OUTPUT,">$filename.new" or die "Cannot create $filename";
820 printf "Building $filename\n";
821 printf OUTPUT "/* Unicode collation element table */\n";
822 printf OUTPUT "/* generated from %s */\n", "$REPORTS/$SORTKEYS";
823 printf OUTPUT "/* DO NOT EDIT!! */\n\n";
825 printf OUTPUT "const unsigned int collation_table[%d] =\n{\n", $ranges*256;
826 printf OUTPUT " /* index */\n";
827 printf OUTPUT "%s,\n", DUMP_ARRAY( "0x%08x", 0, @offsets );
829 # output the default values
831 printf OUTPUT " /* defaults */\n";
832 printf OUTPUT "%s", DUMP_ARRAY( "0x%08x", 0, (0xffffffff) x 256 );
834 # output all the key ranges
836 for (my $i = 0; $i < 256; $i++)
838 next if $offsets[$i] == 256;
839 printf OUTPUT ",\n /* 0x%02x00 .. 0x%02xff */\n", $i, $i;
840 printf OUTPUT "%s", DUMP_ARRAY( "0x%08x", 0xffffffff, @keys[($i<<8) .. ($i<<8)+255] );
842 printf OUTPUT "\n};\n";
843 close OUTPUT;
844 save_file($filename);
848 ################################################################
849 # add default mappings once the file had been read
850 sub add_default_mappings($)
852 my $first_private = shift;
854 # Apply aliases
856 foreach my $alias (@unicode_aliases)
858 my $target = undef;
859 foreach my $src (@$alias)
861 if (defined($uni2cp[$src]))
863 $target = $uni2cp[$src];
864 last;
867 next unless defined($target);
869 # At least one char of the alias set is defined, set the others to the same value
870 foreach my $src (@$alias)
872 $uni2cp[$src] = $target unless defined($uni2cp[$src]);
876 # For every src -> target mapping in the defaults table,
877 # make uni2cp[src] = uni2cp[target] if uni2cp[target] is defined
879 for (my $src = 0; $src < 65536; $src++)
881 next if defined($uni2cp[$src]); # source has a definition already
882 next unless defined($unicode_defaults[$src]); # no default for this char
883 my $target = $unicode_defaults[$src];
885 # do a recursive mapping until we find a target char that is defined
886 while (!defined($uni2cp[$target]) &&
887 defined($unicode_defaults[$target])) { $target = $unicode_defaults[$target]; }
889 if (defined($uni2cp[$target])) { $uni2cp[$src] = $uni2cp[$target]; }
892 # Add a private char mapping for undefined chars
894 if ($first_private)
896 for (my $i = 32; $i < 256; $i++)
898 next if defined($cp2uni[$i]);
899 $cp2uni[$i] = $first_private;
900 $uni2cp[$first_private] = $i;
901 $first_private++;
905 # Add an identity mapping for all undefined chars
907 for (my $i = 0; $i < 256; $i++)
909 next if defined($cp2uni[$i]);
910 next if defined($uni2cp[$i]);
911 $cp2uni[$i] = $uni2cp[$i] = $i;
915 ################################################################
916 # dump an array of integers
917 sub DUMP_ARRAY($$@)
919 my ($format,$default,@array) = @_;
920 my $i;
921 my $ret = " ";
922 for ($i = 0; $i < $#array; $i++)
924 $ret .= sprintf($format, defined $array[$i] ? $array[$i] : $default);
925 $ret .= (($i % 8) != 7) ? ", " : ",\n ";
927 $ret .= sprintf($format, defined $array[$i] ? $array[$i] : $default);
928 return $ret;
932 ################################################################
933 # dump a unicode->ascii mapping table
934 sub dump_uni2cp_table($$)
936 my ($width, $def) = @_;
938 # count the number of unicode->ascii subtables that contain something
940 my @lowerbounds;
941 my @upperbounds;
942 my $index = 0;
943 my @filled = (-1) x 256;
944 for (my $i = 0; $i < 65536; $i++)
946 next unless defined $uni2cp[$i];
947 if ($filled[$i >> 8] == -1)
949 $lowerbounds[$index] = $i & 0xff;
950 $upperbounds[$index] = 0xff - $lowerbounds[$index];
951 $filled[$i >> 8] = $index * 256;
952 $index++;
954 else
956 $upperbounds[$index-1] = 0xff - ($i & 0xff);
960 # add defaults mapping at the end
961 $filled[256] = $index * 256;
962 $lowerbounds[$index] = 255;
963 $upperbounds[$index] = 0;
964 $index++;
966 # collapse blocks upwards if possible
967 my $removed = 0;
968 $index = 0;
969 for (my $i = 0; $i < 257; $i++)
971 next if $filled[$i] == -1;
972 if ($upperbounds[$index - 1] > $lowerbounds[$index])
974 $removed += $lowerbounds[$index];
976 else
978 $removed += $upperbounds[$index - 1];
979 $lowerbounds[$index] = $upperbounds[$index - 1];
981 $filled[$i] -= $removed;
982 $index++;
985 # output all the subtables into a single array
987 printf OUTPUT "static const unsigned %s uni2cp_low[%d] =\n{\n",
988 $width == 8 ? "char" : "short", ($index + 1) * 256 - $removed;
990 my $format = $width == 8 ? "0x%02x" : "0x%04x";
991 for (my $i = $index = 0; $i < 257; $i++)
993 next if $filled[$i] == -1;
994 my $start = ($i << 8) + $lowerbounds[$index];
995 my $end = ($i << 8) + 255;
996 if ($i == 256)
998 print OUTPUT " /* defaults */\n";
999 printf OUTPUT "%s\n};\n\n", DUMP_ARRAY( $format, 0, ($def) x ($end - $start + 1) );
1001 else
1003 printf OUTPUT " /* 0x%04x .. 0x%04x */\n", $start, $end;
1004 printf OUTPUT "%s,\n", DUMP_ARRAY( $format, $def, @uni2cp[$start .. $end] );
1006 $index++;
1009 # output a table of the offsets of the subtables in the previous array
1011 for (my $i = 0; $i < 256; $i++)
1013 if ($filled[$i] == -1) { $filled[$i] = $filled[256]; }
1015 printf OUTPUT "static const unsigned short uni2cp_high[256] =\n";
1016 printf OUTPUT "{\n%s\n};\n\n", DUMP_ARRAY( "0x%04x", 0, @filled[0..255] );
1019 ################################################################
1020 # dump an SBCS mapping table
1021 sub dump_sbcs_table($$$$$)
1023 my ($codepage, $has_glyphs, $name, $def, $defw) = @_;
1024 my $i;
1026 # output the ascii->unicode table
1028 if ($has_glyphs)
1030 printf OUTPUT "static const WCHAR cp2uni[512] =\n";
1031 printf OUTPUT "{\n%s", DUMP_ARRAY( "0x%04x", $defw, @cp2uni[0 .. 255] );
1032 printf OUTPUT ",\n /* glyphs */\n%s\n};\n\n",
1033 DUMP_ARRAY( "0x%04x", $defw, get_glyphs_mapping(@cp2uni[0 .. 255]) );
1035 else
1037 printf OUTPUT "static const WCHAR cp2uni[256] =\n";
1038 printf OUTPUT "{\n%s\n};\n\n", DUMP_ARRAY( "0x%04x", $defw, @cp2uni[0 .. 255] );
1041 dump_uni2cp_table( 8, $def );
1043 # output the code page descriptor
1045 printf OUTPUT "const struct sbcs_table cptable_%03d =\n{\n", $codepage;
1046 printf OUTPUT " { %d, 1, 0x%04x, 0x%04x, \"%s\" },\n",
1047 $codepage, $def, $defw, $name;
1048 printf OUTPUT " cp2uni,\n";
1049 if ($has_glyphs) { printf OUTPUT " cp2uni + 256,\n"; }
1050 else { printf OUTPUT " cp2uni,\n"; }
1051 printf OUTPUT " uni2cp_low,\n";
1052 printf OUTPUT " uni2cp_high\n};\n";
1056 ################################################################
1057 # dump a DBCS mapping table
1058 sub dump_dbcs_table($$$$@)
1060 my ($codepage, $name, $def, $defw, @lb_ranges) = @_;
1062 # build a list of lead bytes that are actually used
1064 my @lblist = ();
1065 LBLOOP: for (my $y = 0; $y <= $#lead_bytes; $y++)
1067 my $base = $lead_bytes[$y] << 8;
1068 for (my $x = 0; $x < 256; $x++)
1070 if (defined $cp2uni[$base+$x])
1072 push @lblist,$lead_bytes[$y];
1073 next LBLOOP;
1077 my $unused = ($#lead_bytes > $#lblist);
1079 # output the ascii->unicode table for the single byte chars
1081 printf OUTPUT "static const WCHAR cp2uni[%d] =\n", 256 * ($#lblist + 2 + $unused);
1082 printf OUTPUT "{\n%s,\n", DUMP_ARRAY( "0x%04x", $defw, @cp2uni[0 .. 255] );
1084 # output the default table for unused lead bytes
1086 if ($unused)
1088 printf OUTPUT " /* unused lead bytes */\n";
1089 printf OUTPUT "%s,\n", DUMP_ARRAY( "0x%04x", 0, ($defw) x 256 );
1092 # output the ascii->unicode table for each DBCS lead byte
1094 for (my $y = 0; $y <= $#lblist; $y++)
1096 my $base = $lblist[$y] << 8;
1097 printf OUTPUT " /* lead byte %02x */\n", $lblist[$y];
1098 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", $defw, @cp2uni[$base .. $base+255] );
1099 printf OUTPUT ($y < $#lblist) ? ",\n" : "\n};\n\n";
1102 # output the lead byte subtables offsets
1104 my @offsets = (0) x 256;
1105 for (my $x = 0; $x <= $#lblist; $x++) { $offsets[$lblist[$x]] = $x + 1; }
1106 if ($unused)
1108 # increment all lead bytes offset to take into account the unused table
1109 for (my $x = 0; $x <= $#lead_bytes; $x++) { $offsets[$lead_bytes[$x]]++; }
1111 printf OUTPUT "static const unsigned char cp2uni_leadbytes[256] =\n";
1112 printf OUTPUT "{\n%s\n};\n\n", DUMP_ARRAY( "0x%02x", 0, @offsets );
1114 dump_uni2cp_table( 16, $def );
1116 # output the code page descriptor
1118 printf OUTPUT "const struct dbcs_table cptable_%03d =\n{\n", $codepage;
1119 printf OUTPUT " { %d, 2, 0x%04x, 0x%04x, \"%s\" },\n",
1120 $codepage, $def, $defw, $name;
1121 printf OUTPUT " cp2uni,\n";
1122 printf OUTPUT " cp2uni_leadbytes,\n";
1123 printf OUTPUT " uni2cp_low,\n";
1124 printf OUTPUT " uni2cp_high,\n";
1125 printf OUTPUT " {\n %s\n }\n", DUMP_ARRAY( "0x%02x", 0, @lb_ranges, 0, 0 );
1126 printf OUTPUT "};\n";
1130 ################################################################
1131 # get the list of defined lead byte ranges
1132 sub get_lb_ranges()
1134 my @list = ();
1135 my @ranges = ();
1136 my $i = 0;
1137 foreach $i (@lead_bytes) { $list[$i] = 1; }
1138 my $on = 0;
1139 for (my $i = 0; $i < 256; $i++)
1141 if ($on)
1143 if (!defined $list[$i]) { push @ranges, $i-1; $on = 0; }
1145 else
1147 if ($list[$i]) { push @ranges, $i; $on = 1; }
1150 if ($on) { push @ranges, 0xff; }
1151 return @ranges;
1154 ################################################################
1155 # dump the Indic Syllabic Category table
1156 sub dump_indic($)
1158 my $filename = shift;
1159 my @indic_table = ($indic_types{'Other'}) x 65536;;
1161 my $INPUT = open_data_file( $UNIDATA, "IndicSyllabicCategory.txt" );
1162 while (<$INPUT>)
1164 next if /^\#/; # skip comments
1165 next if /^\s*$/; # skip empty lines
1166 next if /\x1a/; # skip ^Z
1167 if (/^\s*([0-9a-fA-F]+)\s*;\s*([a-zA-Z_]+)\s*#/)
1169 my $type = $2;
1170 die "unknown indic $type" unless defined $indic_types{$type};
1171 if (hex $1 < 65536)
1173 $indic_table[hex $1] = $indic_types{$type};
1175 next;
1177 elsif (/^\s*([0-9a-fA-F]+)..\s*([0-9a-fA-F]+)\s*;\s*([A-Za-z_]+)\s*#/)
1179 my $type = $3;
1180 die "unknown indic $type" unless defined $indic_types{$type};
1181 if (hex $1 < 65536 and hex $2 < 6536)
1183 foreach my $i (hex $1 .. hex $2)
1185 $indic_table[$i] = $indic_types{$type};
1188 next;
1190 die "malformed line $_";
1192 close $INPUT;
1194 $INPUT = open_data_file( $UNIDATA, "IndicMatraCategory.txt" );
1195 while (<$INPUT>)
1197 next if /^\#/; # skip comments
1198 next if /^\s*$/; # skip empty lines
1199 next if /\x1a/; # skip ^Z
1200 if (/^\s*([0-9a-fA-F]+)\s*;\s*([a-zA-Z]+)\s*#/)
1202 my $type = $2;
1203 die "unknown matra $type" unless defined $matra_types{$type};
1204 $indic_table[hex $1] += $matra_types{$type} << 8;
1205 next;
1207 elsif (/^\s*([0-9a-fA-F]+)..\s*([0-9a-fA-F]+)\s*;\s*([A-Za-z_]+)\s*#/)
1209 my $type = $3;
1210 die "unknown matra $type" unless defined $matra_types{$type};
1211 foreach my $i (hex $1 .. hex $2)
1213 $indic_table[$i] += $matra_types{$type} << 8;
1215 next;
1217 die "malformed line $_";
1219 close $INPUT;
1221 open OUTPUT,">$filename.new" or die "Cannot create $filename";
1222 print "Building $filename\n";
1223 print OUTPUT "/* Unicode Indic Syllabic Category */\n";
1224 print OUTPUT "/* generated from $UNIDATA/IndicSyllabicCategory.txt */\n";
1225 print OUTPUT "/* and from $UNIDATA/IndicMatraCategory.txt */\n";
1226 print OUTPUT "/* DO NOT EDIT!! */\n\n";
1227 print OUTPUT "#include \"wine/unicode.h\"\n\n";
1229 dump_two_level_mapping( "indic_syllabic_table", @indic_table);
1231 close OUTPUT;
1232 save_file($filename);
1235 ################################################################
1236 # dump the Line Break Properties table
1237 sub dump_linebreak($)
1239 my $filename = shift;
1240 my @break_table = ($break_types{'XX'}) x 65536;;
1241 my $next_group = 0;
1243 my $INPUT = open_data_file( $UNIDATA, "LineBreak.txt" );
1244 while (<$INPUT>)
1246 next if /^\#/; # skip comments
1247 next if /^\s*$/; # skip empty lines
1248 next if /\x1a/; # skip ^Z
1249 if (/^\s*([0-9a-fA-F]+)\s*;\s*([0-9A-Z][0-9A-Z])+\s*/)
1251 my $type = $2;
1252 die "unknown breaktype $type" unless defined $break_types{$type};
1253 $break_table[hex $1] = $break_types{$type};
1254 next;
1256 elsif (/^\s*([0-9a-fA-F]+)..\s*([0-9a-fA-F]+)\s*;\s*([0-9A-Z][0-9A-Z])+\s*/)
1258 my $type = $3;
1259 die "unknown breaktype $type" unless defined $break_types{$type};
1260 foreach my $i (hex $1 .. hex $2)
1262 $break_table[$i] = $break_types{$type};
1264 next;
1266 die "malformed line $_";
1268 close $INPUT;
1270 open OUTPUT,">$filename.new" or die "Cannot create $filename";
1271 print "Building $filename\n";
1272 print OUTPUT "/* Unicode Line Break Properties */\n";
1273 print OUTPUT "/* generated from $UNIDATA/LineBreak.txt */\n";
1274 print OUTPUT "/* DO NOT EDIT!! */\n\n";
1275 print OUTPUT "#include \"wine/unicode.h\"\n\n";
1277 dump_two_level_mapping( "wine_linebreak_table", @break_table);
1279 close OUTPUT;
1280 save_file($filename);
1284 ################################################################
1285 # dump the BiDi mirroring table
1286 sub dump_mirroring($)
1288 my $filename = shift;
1289 my @mirror_table = ();
1291 my $INPUT = open_data_file( $UNIDATA, "BidiMirroring.txt" );
1292 while (<$INPUT>)
1294 next if /^\#/; # skip comments
1295 next if /^$/; # skip empty lines
1296 next if /\x1a/; # skip ^Z
1297 if (/^\s*([0-9a-fA-F]+)\s*;\s*([0-9a-fA-F]+)/)
1299 $mirror_table[hex $1] = hex $2;
1300 next;
1302 die "malformed line $_";
1304 close $INPUT;
1306 open OUTPUT,">$filename.new" or die "Cannot create $filename";
1307 print "Building $filename\n";
1308 print OUTPUT "/* Unicode BiDi mirroring */\n";
1309 print OUTPUT "/* generated from $UNIDATA/BidiMirroring.txt */\n";
1310 print OUTPUT "/* DO NOT EDIT!! */\n\n";
1311 print OUTPUT "#include \"wine/unicode.h\"\n\n";
1312 DUMP_CASE_TABLE( "wine_mirror_map", @mirror_table );
1313 close OUTPUT;
1314 save_file($filename);
1318 ################################################################
1319 # dump the Arabic shaping table
1320 sub dump_shaping($)
1322 my $filename = shift;
1323 my %groups;
1324 my $next_group = 0;
1326 $groups{"No_Joining_Group"} = $next_group++;
1328 my $INPUT = open_data_file( $UNIDATA, "ArabicShaping.txt" );
1329 while (<$INPUT>)
1331 next if /^\#/; # skip comments
1332 next if /^\s*$/; # skip empty lines
1333 next if /\x1a/; # skip ^Z
1334 if (/^\s*([0-9a-fA-F]+)\s*;.*;\s*([RLDCUT])\s*;\s*(\w+)/)
1336 my $type = $2;
1337 my $group = $3;
1338 $groups{$group} = $next_group++ unless defined $groups{$group};
1339 $joining_table[hex $1] = $joining_types{$type} | ($groups{$group} << 8);
1340 next;
1342 die "malformed line $_";
1344 close $INPUT;
1346 open OUTPUT,">$filename.new" or die "Cannot create $filename";
1347 print "Building $filename\n";
1348 print OUTPUT "/* Unicode Arabic shaping */\n";
1349 print OUTPUT "/* generated from $UNIDATA/ArabicShaping.txt */\n";
1350 print OUTPUT "/* DO NOT EDIT!! */\n\n";
1351 print OUTPUT "#include \"wine/unicode.h\"\n\n";
1353 dump_two_level_mapping( "wine_shaping_table", @joining_table );
1355 print OUTPUT "\nconst unsigned short wine_shaping_forms[256][4] =\n{\n";
1356 for (my $i = 0x600; $i <= 0x6ff; $i++)
1358 printf OUTPUT " { 0x%04x, 0x%04x, 0x%04x, 0x%04x },\n",
1359 ${joining_forms{"isolated"}}[$i] || $i,
1360 ${joining_forms{"final"}}[$i] || $i,
1361 ${joining_forms{"initial"}}[$i] || $i,
1362 ${joining_forms{"medial"}}[$i] || $i;
1364 print OUTPUT "};\n";
1366 close OUTPUT;
1367 save_file($filename);
1370 ################################################################
1371 # dump the Vertical Orientation table
1372 sub dump_vertical($)
1374 my $filename = shift;
1375 my @vertical_table = ($vertical_types{'R'}) x 65536;
1377 my $INPUT = open_data_file( $VERTICALDATA, "VerticalOrientation-11.txt" );
1378 while (<$INPUT>)
1380 next if /^\#/; # skip comments
1381 next if /^\s*$/; # skip empty lines
1382 next if /\x1a/; # skip ^Z
1383 if (/^\s*([0-9a-fA-F]+)\s*;\s*([a-zA-Z_]+)\s*/)
1385 my $type = $2;
1386 die "unknown vertical $type" unless defined $vertical_types{$type};
1387 if (hex $1 < 65536)
1389 $vertical_table[hex $1] = $vertical_types{$type};
1391 next;
1393 elsif (/^\s*([0-9a-fA-F]+)..\s*([0-9a-fA-F]+)\s*;\s*([A-Za-z_]+)\s*/)
1395 my $type = $3;
1396 die "unknown vertical $type" unless defined $vertical_types{$type};
1397 foreach my $i (hex $1 .. hex $2)
1399 $vertical_table[$i] = $vertical_types{$type};
1401 next;
1403 die "malformed line $_";
1405 close $INPUT;
1407 open OUTPUT,">$filename.new" or die "Cannot create $filename";
1408 print "Building $filename\n";
1409 print OUTPUT "/* Unicode Vertical Orientation */\n";
1410 print OUTPUT "/* generated from $VERTICALDATA/VerticalOrientation-11.txt */\n";
1411 print OUTPUT "/* DO NOT EDIT!! */\n\n";
1412 print OUTPUT "#include \"wine/unicode.h\"\n\n";
1414 dump_two_level_mapping( "vertical_orientation_table", @vertical_table);
1416 close OUTPUT;
1417 save_file($filename);
1420 ################################################################
1421 # dump the case mapping tables
1422 sub DUMP_CASE_MAPPINGS($)
1424 my $filename = shift;
1425 open OUTPUT,">$filename.new" or die "Cannot create $filename";
1426 printf "Building $filename\n";
1427 printf OUTPUT "/* Unicode case mappings */\n";
1428 printf OUTPUT "/* Automatically generated; DO NOT EDIT!! */\n\n";
1429 printf OUTPUT "#include \"wine/unicode.h\"\n\n";
1431 DUMP_CASE_TABLE( "wine_casemap_lower", @tolower_table );
1432 DUMP_CASE_TABLE( "wine_casemap_upper", @toupper_table );
1433 DUMP_CASE_TABLE( "wine_digitmap", @digitmap_table );
1434 DUMP_CASE_TABLE( "wine_compatmap", @compatmap_table );
1435 close OUTPUT;
1436 save_file($filename);
1440 ################################################################
1441 # dump a case mapping table
1442 sub DUMP_CASE_TABLE($@)
1444 my ($name,@table) = @_;
1446 # count the number of sub tables that contain something
1447 # also compute the low and upper populated bounds
1449 my @lowerbounds = ( 0, 0 );
1450 my @upperbounds = ( 0, 255 );
1451 my $index = 0;
1452 my @filled = ();
1453 for (my $i = 0; $i < 65536; $i++)
1455 next unless defined $table[$i];
1456 if (!defined $filled[$i >> 8])
1458 $lowerbounds[$index] = $i & 0xff;
1459 $upperbounds[$index] = 0xff - $lowerbounds[$index];
1460 $filled[$i >> 8] = $index * 256 + 512;
1461 $index++;
1463 else
1465 $upperbounds[$index-1] = 0xff - ($i & 0xff);
1467 $table[$i] = ($table[$i] - $i) & 0xffff;
1470 # Collapse blocks upwards if possible
1471 my $removed = 0;
1472 $index = 0;
1473 for (my $i = 0; $i < 256; $i++)
1475 next unless defined $filled[$i];
1476 if ($upperbounds[$index - 1] > $lowerbounds[$index])
1478 $removed = $removed + $lowerbounds[$index];
1480 else
1482 $removed = $removed + $upperbounds[$index - 1];
1483 $lowerbounds[$index] = $upperbounds[$index - 1];
1485 $filled[$i] = $filled[$i] - $removed;
1486 $index++;
1489 # dump the table
1491 printf OUTPUT "const WCHAR %s[%d] =\n", $name, $index * 256 + 512 - $removed;
1492 printf OUTPUT "{\n /* index */\n";
1493 printf OUTPUT "%s,\n", DUMP_ARRAY( "0x%04x", 256, @filled );
1494 printf OUTPUT " /* defaults */\n";
1495 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, (0) x 256 );
1496 $index = 0;
1497 for (my $i = 0; $i < 256; $i++)
1499 next unless $filled[$i];
1500 printf OUTPUT ",\n /* 0x%02x%02x .. 0x%02xff */\n", $i, $lowerbounds[$index], $i;
1501 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0,
1502 @table[($i<<8) + $lowerbounds[$index] .. ($i<<8)+255] );
1503 $index++;
1505 printf OUTPUT "\n};\n";
1508 ################################################################
1509 # compress a mapping table by removing identical rows
1510 sub compress_array($@)
1512 my $rows = shift;
1513 my @table = @_;
1514 my $len = @table / $rows;
1515 my @array = (0) x $rows;
1516 my %sequences;
1518 # try to merge table rows
1519 for (my $row = 0; $row < $rows; $row++)
1521 my $rowtxt = pack "S*", @table[($row * $len)..($row * $len + $len - 1)];
1522 if (defined($sequences{$rowtxt}))
1524 # reuse an existing row
1525 $array[$row] = $sequences{$rowtxt};
1527 else
1529 # create a new row
1530 $sequences{$rowtxt} = $array[$row] = $#array + 1;
1531 push @array, @table[$row * $len..$row * $len + $len - 1];
1534 return @array;
1537 ################################################################
1538 # dump a simple char -> 16-bit value mapping table
1539 sub dump_simple_mapping($@)
1541 my $name = shift;
1542 my @array = compress_array( 256, @_[0..65535] );
1544 printf OUTPUT "const unsigned short %s[%d] =\n{\n", $name, $#array+1;
1545 printf OUTPUT " /* offsets */\n%s,\n", DUMP_ARRAY( "0x%04x", 0, @array[0..255] );
1546 printf OUTPUT " /* values */\n%s\n};\n", DUMP_ARRAY( "0x%04x", 0, @array[256..$#array] );
1549 ################################################################
1550 # dump a char -> 16-bit value mapping table using two-level tables
1551 sub dump_two_level_mapping($@)
1553 my $name = shift;
1554 my @row_array = compress_array( 4096, @_[0..65535] );
1555 my @array = compress_array( 256, @row_array[0..4095] );
1557 for (my $i = 256; $i < @array; $i++) { $array[$i] += @array - 4096; }
1559 printf OUTPUT "const unsigned short %s[%d] =\n{\n", $name, @array + @row_array - 4096;
1560 printf OUTPUT " /* level 1 offsets */\n%s,\n", DUMP_ARRAY( "0x%04x", 0, @array[0..255] );
1561 printf OUTPUT " /* level 2 offsets */\n%s,\n", DUMP_ARRAY( "0x%04x", 0, @array[256..$#array] );
1562 printf OUTPUT " /* values */\n%s\n};\n", DUMP_ARRAY( "0x%04x", 0, @row_array[4096..$#row_array] );
1565 ################################################################
1566 # dump a binary case mapping table in l_intl.nls format
1567 sub dump_binary_case_table(@)
1569 my (@table) = @_;
1571 my %difftables_hash = ();
1572 my @difftables;
1573 my %offtables2_hash = ();
1574 my @offtables2 = ();
1576 my @offtable = ();
1577 for (my $i = 0; $i < 256; $i++)
1579 my @offtable2 = ();
1580 for(my $j = 0; $j < 16; $j++) # offset table for xx00-xxFF characters
1582 my @difftable;
1583 for (my $k = 0; $k < 16; $k++) # case map table for xxx0-xxxF characters
1585 my $char = ($i<<8) + ($j<<4) + $k;
1586 $difftable[$k] = (defined $table[$char]) ? (($table[$char]-$char) & 0xffff) : 0;
1589 my $diff_key = pack "S*", @difftable;
1590 my $offset3 = $difftables_hash{$diff_key};
1591 if (!defined $offset3)
1593 $offset3 = scalar @difftables;
1594 $difftables_hash{$diff_key} = $offset3;
1595 push @difftables, @difftable;
1597 $offtable2[$j] = $offset3;
1600 my $offtable2_key = pack "S*", @offtable2;
1601 my $offset2 = $offtables2_hash{$offtable2_key};
1602 if (!defined $offset2)
1604 $offset2 = scalar @offtables2;
1605 $offtables2_hash{$offtable2_key} = $offset2;
1606 push @offtables2, \@offtable2;
1608 $offtable[$i] = $offset2;
1611 my @output;
1612 my $offset = 0x100; # offset of first subtable in words
1613 foreach (@offtable)
1615 push @output, 0x10 * $_ + $offset; # offset of subtable in words
1618 $offset = 0x100 + 0x10 * scalar @offtables2; # offset of first difftable in words
1619 foreach(@offtables2)
1621 my $table = $_;
1622 foreach(@$table)
1624 push @output, $_ + $offset; # offset of difftable in words
1628 my $len = 1 + scalar @output + scalar @difftables;
1629 return pack "S<*", $len, @output, @difftables;
1633 ################################################################
1634 # dump case mappings for l_intl.nls
1635 sub dump_intl_nls($)
1637 my $filename = shift;
1638 open OUTPUT,">$filename.new" or die "Cannot create $filename";
1639 printf "Building $filename\n";
1641 binmode OUTPUT;
1642 print OUTPUT pack "S<", 1; # version
1643 print OUTPUT dump_binary_case_table( @toupper_table );
1644 print OUTPUT dump_binary_case_table( @tolower_table );
1645 close OUTPUT;
1646 save_file($filename);
1650 sub load_nameprep_range_table($$$)
1652 my ($INPUT, $val, $table_ref) = @_;
1654 while (<$INPUT>)
1656 if (/^\s*([0-9a-fA-F]+)-([0-9a-fA-F]+)/)
1658 my $last = hex $2;
1659 $last = 65535 if($last >= 65536);
1660 foreach my $i (hex $1 .. $last)
1662 $table_ref->[$i] |= $val;
1664 next;
1666 elsif (/^\s*([0-9a-fA-F]+)/)
1668 if (hex $1 < 65536)
1670 $table_ref->[hex $1] |= $val;
1672 next;
1675 return if (/End\sTable/);
1679 sub load_nameprep_map_table($$)
1681 my ($INPUT, $table_ref) = @_;
1683 while (<$INPUT>)
1685 if (/^\s*([0-9a-fA-F]+);\s;/)
1687 # special value for map to nothing
1688 $table_ref->[hex $1] = [0xffff, 0xffff, 0xffff];
1689 next;
1691 elsif (/^\s*([0-9a-fA-F]+);\s([0-9a-fA-F]+);/)
1693 $table_ref->[hex $1] = [hex $2, 0, 0];
1694 next;
1696 elsif (/^\s*([0-9a-fA-F]+);\s([0-9a-fA-F]+)\s([0-9a-fA-F]+);/)
1698 $table_ref->[hex $1] = [hex $2, hex $3, 0];
1699 next;
1701 elsif (/^\s*([0-9a-fA-F]+);\s([0-9a-fA-F]+)\s([0-9a-fA-F]+)\s([0-9a-fA-F]+);/)
1703 $table_ref->[hex $1] = [hex $2, hex $3, hex $4];
1704 next;
1707 return if (/End\sTable/);
1711 ################################################################
1712 # dump mapping table, prohibited characters set, unassigned
1713 # characters, bidirectional rules used by nameprep algorithm
1714 sub dump_nameprep($)
1716 my $filename = shift;
1717 my @mapping_table = ();
1718 my @flags_table = (0) x 65536;
1720 my $INPUT = open_data_file( $RFCS, $STRINGPREP );
1721 while (<$INPUT>)
1723 next unless /Start\sTable/;
1725 load_nameprep_range_table($INPUT, $nameprep_flags{"unassigned"}, \@flags_table) if (/A.1/);
1726 load_nameprep_range_table($INPUT, $nameprep_flags{"prohibited"}, \@flags_table) if (/C.1.2/);
1727 load_nameprep_range_table($INPUT, $nameprep_flags{"prohibited"}, \@flags_table) if (/C.2.2/);
1728 load_nameprep_range_table($INPUT, $nameprep_flags{"prohibited"}, \@flags_table) if (/C.3/);
1729 load_nameprep_range_table($INPUT, $nameprep_flags{"prohibited"}, \@flags_table) if (/C.4/);
1730 load_nameprep_range_table($INPUT, $nameprep_flags{"prohibited"}, \@flags_table) if (/C.5/);
1731 load_nameprep_range_table($INPUT, $nameprep_flags{"prohibited"}, \@flags_table) if (/C.6/);
1732 load_nameprep_range_table($INPUT, $nameprep_flags{"prohibited"}, \@flags_table) if (/C.7/);
1733 load_nameprep_range_table($INPUT, $nameprep_flags{"prohibited"}, \@flags_table) if (/C.8/);
1734 load_nameprep_range_table($INPUT, $nameprep_flags{"prohibited"}, \@flags_table) if (/C.9/);
1735 load_nameprep_range_table($INPUT, $nameprep_flags{"bidi_ral"}, \@flags_table) if (/D.1/);
1736 load_nameprep_range_table($INPUT, $nameprep_flags{"bidi_l"}, \@flags_table) if (/D.2/);
1738 load_nameprep_map_table($INPUT, \@mapping_table) if (/B.1/);
1739 load_nameprep_map_table($INPUT, \@mapping_table) if (/B.2/);
1741 close $INPUT;
1743 open OUTPUT,">$filename.new" or die "Cannot create $filename";
1744 print "Building $filename\n";
1745 print OUTPUT "/* Nameprep algorithm related data */\n";
1746 print OUTPUT "/* generated from $RFCS/$STRINGPREP */\n";
1747 print OUTPUT "/* DO NOT EDIT!! */\n\n";
1748 print OUTPUT "#include \"wine/unicode.h\"\n\n";
1750 dump_two_level_mapping( "nameprep_char_type", @flags_table );
1752 ######### mapping table
1753 # first determine all the 16-char subsets that contain something
1754 my @filled = ();
1755 my $pos = 16*3; # for the null subset
1756 for (my $i = 0; $i < 65536; $i++)
1758 next unless defined $mapping_table[$i];
1759 $filled[$i >> 4] = $pos;
1760 $pos += 16*3;
1761 $i |= 15;
1763 my $total = $pos;
1765 # now count the 256-char subsets that contain something
1766 my @filled_idx = (256) x 256;
1767 $pos = 256 + 16;
1768 for (my $i = 0; $i < 4096; $i++)
1770 next unless $filled[$i];
1771 $filled_idx[$i >> 4] = $pos;
1772 $pos += 16;
1773 $i |= 15;
1775 my $null_offset = $pos;
1776 $total += $pos;
1778 # add the index offsets to the subsets positions
1779 for (my $i = 0; $i < 4096; $i++)
1781 next unless $filled[$i];
1782 $filled[$i] += $null_offset;
1785 # dump the main index
1786 printf OUTPUT "const WCHAR nameprep_mapping[%d] =\n", $total;
1787 printf OUTPUT "{\n /* index */\n";
1788 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, @filled_idx );
1789 printf OUTPUT ",\n /* null sub-index */\n%s", DUMP_ARRAY( "0x%04x", 0, ($null_offset) x 16 );
1791 # dump the second-level indexes
1792 for (my $i = 0; $i < 256; $i++)
1794 next unless ($filled_idx[$i] > 256);
1795 my @table = @filled[($i<<4)..($i<<4)+15];
1796 for (my $j = 0; $j < 16; $j++) { $table[$j] ||= $null_offset; }
1797 printf OUTPUT ",\n /* sub-index %02x */\n", $i;
1798 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, @table );
1801 # dump the 16-char subsets
1802 printf OUTPUT ",\n /* null mapping */\n";
1803 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, (0) x 48 );
1805 for (my $i = 0; $i < 4096; $i++)
1807 next unless $filled[$i];
1808 my @table = (0) x 48;
1809 for (my $j = 0; $j < 16; $j++)
1811 if (defined $mapping_table[($i<<4) + $j])
1813 $table[3 * $j] = ${$mapping_table[($i << 4) + $j]}[0];
1814 $table[3 * $j + 1] = ${$mapping_table[($i << 4) + $j]}[1];
1815 $table[3 * $j + 2] = ${$mapping_table[($i << 4) + $j]}[2];
1818 printf OUTPUT ",\n /* 0x%03x0 .. 0x%03xf */\n", $i, $i;
1819 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, @table );
1822 printf OUTPUT "\n};\n";
1824 close OUTPUT;
1825 save_file($filename);
1828 ################################################################
1829 # dump the ctype tables
1830 sub DUMP_CTYPE_TABLES($)
1832 my $filename = shift;
1833 open OUTPUT,">$filename.new" or die "Cannot create $filename";
1834 printf "Building $filename\n";
1835 printf OUTPUT "/* Unicode ctype tables */\n";
1836 printf OUTPUT "/* Automatically generated; DO NOT EDIT!! */\n\n";
1837 printf OUTPUT "#include \"wine/unicode.h\"\n\n";
1839 # add the direction in the high 4 bits of the category
1840 for (my $i = 0; $i < 65536; $i++)
1842 $category_table[$i] |= $direction_table[$i] << 12 if defined $direction_table[$i];
1845 dump_simple_mapping( "wine_wctype_table", @category_table );
1847 close OUTPUT;
1848 save_file($filename);
1852 ################################################################
1853 # dump the char composition tables
1854 sub DUMP_COMPOSE_TABLES($)
1856 my $filename = shift;
1858 open OUTPUT,">$filename.new" or die "Cannot create $filename";
1859 printf "Building $filename\n";
1860 printf OUTPUT "/* Unicode char composition */\n";
1861 printf OUTPUT "/* Automatically generated; DO NOT EDIT!! */\n\n";
1862 printf OUTPUT "#include \"wine/unicode.h\"\n\n";
1864 ######### composition table
1866 my @filled = ();
1867 foreach my $i (@compose_table)
1869 my @comp = @$i;
1870 push @{$filled[$comp[1]]}, [ $comp[0], $comp[2] ];
1873 # count how many different second chars we have
1875 my $count = 0;
1876 for (my $i = 0; $i < 65536; $i++)
1878 next unless defined $filled[$i];
1879 $count++;
1882 # build the table of second chars and offsets
1884 my $pos = $count + 1;
1885 my @table = ();
1886 for (my $i = 0; $i < 65536; $i++)
1888 next unless defined $filled[$i];
1889 push @table, $i, $pos;
1890 $pos += @{$filled[$i]};
1892 # terminator with last position
1893 push @table, 0, $pos;
1894 printf OUTPUT "const WCHAR unicode_compose_table[0x%x] =\n{\n", 2*$pos;
1895 printf OUTPUT " /* second chars + offsets */\n%s", DUMP_ARRAY( "0x%04x", 0, @table );
1897 # build the table of first chars and mappings
1899 for (my $i = 0; $i < 65536; $i++)
1901 next unless defined $filled[$i];
1902 my @table = ();
1903 my @list = sort { $a->[0] <=> $b->[0] } @{$filled[$i]};
1904 for (my $j = 0; $j <= $#list; $j++)
1906 push @table, $list[$j][0], $list[$j][1];
1908 printf OUTPUT ",\n /* 0x%04x */\n%s", $i, DUMP_ARRAY( "0x%04x", 0, @table );
1910 printf OUTPUT "\n};\n\nconst unsigned int unicode_compose_table_size = %d;\n\n", $count;
1912 ######### decomposition table
1914 # first determine all the 16-char subsets that contain something
1916 @filled = (0) x 4096;
1917 $pos = 16*2; # for the null subset
1918 for (my $i = 0; $i < 65536; $i++)
1920 next unless defined $decomp_table[$i];
1921 $filled[$i >> 4] = $pos;
1922 $pos += 16*2;
1923 $i |= 15;
1925 my $total = $pos;
1927 # now count the 256-char subsets that contain something
1929 my @filled_idx = (256) x 256;
1930 $pos = 256 + 16;
1931 for (my $i = 0; $i < 4096; $i++)
1933 next unless $filled[$i];
1934 $filled_idx[$i >> 4] = $pos;
1935 $pos += 16;
1936 $i |= 15;
1938 my $null_offset = $pos; # null mapping
1939 $total += $pos;
1941 # add the index offsets to the subsets positions
1943 for (my $i = 0; $i < 4096; $i++)
1945 next unless $filled[$i];
1946 $filled[$i] += $null_offset;
1949 # dump the main index
1951 printf OUTPUT "const WCHAR unicode_decompose_table[%d] =\n", $total;
1952 printf OUTPUT "{\n /* index */\n";
1953 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, @filled_idx );
1954 printf OUTPUT ",\n /* null sub-index */\n%s", DUMP_ARRAY( "0x%04x", 0, ($null_offset) x 16 );
1956 # dump the second-level indexes
1958 for (my $i = 0; $i < 256; $i++)
1960 next unless ($filled_idx[$i] > 256);
1961 my @table = @filled[($i<<4)..($i<<4)+15];
1962 for (my $j = 0; $j < 16; $j++) { $table[$j] ||= $null_offset; }
1963 printf OUTPUT ",\n /* sub-index %02x */\n", $i;
1964 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, @table );
1967 # dump the 16-char subsets
1969 printf OUTPUT ",\n /* null mapping */\n";
1970 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, (0) x 32 );
1972 for (my $i = 0; $i < 4096; $i++)
1974 next unless $filled[$i];
1975 my @table = (0) x 32;
1976 for (my $j = 0; $j < 16; $j++)
1978 if (defined $decomp_table[($i<<4) + $j])
1980 $table[2 * $j] = ${$decomp_table[($i << 4) + $j]}[0];
1981 $table[2 * $j + 1] = ${$decomp_table[($i << 4) + $j]}[1];
1984 printf OUTPUT ",\n /* 0x%03x0 .. 0x%03xf */\n", $i, $i;
1985 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, @table );
1988 printf OUTPUT "\n};\n";
1989 close OUTPUT;
1990 save_file($filename);
1993 ################################################################
1994 # output a codepage definition file from the global tables
1995 sub output_codepage_file($$$$)
1997 my ($codepage, $filename, $comment, $has_glyphs) = @_;
1999 my $output = sprintf "libs/wine/c_%03d.c", $codepage;
2000 open OUTPUT,">$output.new" or die "Cannot create $output";
2002 printf "Building %s from %s (%s)\n", $output, $filename || "hardcoded data", $comment;
2004 # dump all tables
2006 printf OUTPUT "/* code page %03d (%s) */\n", $codepage, $comment;
2007 if ($filename)
2009 print OUTPUT "/* generated from $MAPPINGS/$filename */\n";
2010 print OUTPUT "/* DO NOT EDIT!! */\n\n";
2012 else
2014 printf OUTPUT "/* Automatically generated; DO NOT EDIT!! */\n\n";
2016 printf OUTPUT "#include \"wine/unicode.h\"\n\n";
2018 if (!@lead_bytes) { dump_sbcs_table( $codepage, $has_glyphs, $comment, $default_char, $default_wchar ); }
2019 else { dump_dbcs_table( $codepage, $comment, $default_char, $default_wchar, get_lb_ranges() ); }
2020 close OUTPUT;
2021 save_file($output);
2024 ################################################################
2025 # read a "bestfit" Windows mapping file
2026 sub read_bestfit_file($)
2028 my ($filename) = @_;
2029 my $state = "";
2030 my ($codepage, $width, $count);
2031 my ($lb_cur, $lb_end);
2033 my $INPUT = open_data_file( $MAPPINGS, $filename ) or die "Cannot open $filename";
2035 while (<$INPUT>)
2037 next if /^;/; # skip comments
2038 next if /^\s*$/; # skip empty lines
2039 next if /\x1a/; # skip ^Z
2040 last if /^ENDCODEPAGE/;
2042 if (/^CODEPAGE\s+(\d+)/)
2044 $codepage = $1;
2045 next;
2047 if (/^CPINFO\s+(\d+)\s+0x([0-9a-fA-f]+)\s+0x([0-9a-fA-F]+)/)
2049 $width = $1;
2050 $default_char = hex $2;
2051 $default_wchar = hex $3;
2052 next;
2054 if (/^(MBTABLE|WCTABLE|DBCSRANGE|DBCSTABLE)\s+(\d+)/)
2056 $state = $1;
2057 $count = $2;
2058 next;
2060 if (/^0x([0-9a-fA-F]+)\s+0x([0-9a-fA-F]+)/)
2062 if ($state eq "MBTABLE")
2064 my $cp = hex $1;
2065 my $uni = hex $2;
2066 $cp2uni[$cp] = $uni unless defined($cp2uni[$cp]);
2067 next;
2069 if ($state eq "WCTABLE")
2071 my $uni = hex $1;
2072 my $cp = hex $2;
2073 $uni2cp[$uni] = $cp unless defined($uni2cp[$uni]);
2074 next;
2076 if ($state eq "DBCSRANGE")
2078 my $start = hex $1;
2079 my $end = hex $2;
2080 for (my $i = $start; $i <= $end; $i++) { add_lead_byte( $i ); }
2081 $lb_cur = $start;
2082 $lb_end = $end;
2083 next;
2085 if ($state eq "DBCSTABLE")
2087 my $mb = hex $1;
2088 my $uni = hex $2;
2089 my $cp = ($lb_cur << 8) | $mb;
2090 $cp2uni[$cp] = $uni unless defined($cp2uni[$cp]);
2091 if (!--$count)
2093 if (++$lb_cur > $lb_end) { $state = "DBCSRANGE"; }
2095 next;
2098 die "$filename: Unrecognized line $_\n";
2100 close $INPUT;
2104 ################################################################
2105 # read an input file and generate the corresponding .c file
2106 sub HANDLE_FILE(@)
2108 my ($codepage,$filename,$has_glyphs,$comment,$first_private,$def,$defw) = @_;
2110 @cp2uni = ();
2111 @lead_bytes = ();
2112 @uni2cp = ();
2113 $default_char = $def || $DEF_CHAR;
2114 $default_wchar = $defw || $DEF_CHAR;
2116 # some codepage files are special
2117 if ($codepage == 20932)
2119 READ_JIS0208_FILE( $filename );
2120 add_default_mappings( $first_private );
2122 elsif ($codepage == 20127)
2124 fill_20127_codepage();
2125 add_default_mappings( $first_private );
2127 elsif ($filename =~ /\/bestfit/)
2129 read_bestfit_file( $filename );
2131 else
2133 read_codepage_file( $filename );
2134 if ($codepage == 10001)
2136 # add Shift-JIS mappings
2137 read_bestfit_file( "VENDORS/MICSFT/WindowsBestFit/bestfit932.txt" );
2139 else
2141 add_default_mappings( $first_private );
2145 output_codepage_file( $codepage, $filename, $comment, $has_glyphs );
2149 ################################################################
2150 # save a file if modified
2151 sub save_file($)
2153 my $file = shift;
2154 if (-f $file && !system "cmp $file $file.new >/dev/null")
2156 unlink "$file.new";
2158 else
2160 rename "$file.new", "$file";
2165 ################################################################
2166 # output the list of codepage tables into the cptable.c file
2167 sub output_cptable($)
2169 my $output = shift;
2170 my @tables_decl = ();
2172 printf "Building %s\n", $output;
2174 foreach my $file (@allfiles)
2176 my ($codepage,$filename) = @$file;
2177 push @tables_decl, sprintf("extern union cptable cptable_%03d;\n",$codepage);
2180 push @tables_decl, sprintf("\nstatic const union cptable * const cptables[%d] =\n{\n",$#allfiles+1);
2181 foreach my $file (@allfiles)
2183 my ($codepage,$filename) = @$file;
2184 push @tables_decl, sprintf(" &cptable_%03d,\n", $codepage);
2186 push @tables_decl, "};";
2187 REPLACE_IN_FILE( $output, @tables_decl );
2190 ################################################################
2191 # replace the contents of a file between ### cpmap ### marks
2193 sub REPLACE_IN_FILE($@)
2195 my $name = shift;
2196 my @data = @_;
2197 my @lines = ();
2198 open(FILE,$name) or die "Can't open $name";
2199 while (<FILE>)
2201 push @lines, $_;
2202 last if /\#\#\# cpmap begin \#\#\#/;
2204 push @lines, @data;
2205 while (<FILE>)
2207 if (/\#\#\# cpmap end \#\#\#/) { push @lines, "\n", $_; last; }
2209 push @lines, <FILE>;
2210 open(FILE,">$name.new") or die "Can't modify $name";
2211 print FILE @lines;
2212 close(FILE);
2213 save_file($name);
2216 ################################################################
2217 # main routine
2219 chdir ".." if -f "./make_unicode";
2220 READ_DEFAULTS( $DEFAULTS );
2221 DUMP_CASE_MAPPINGS( "libs/wine/casemap.c" );
2222 DUMP_SORTKEYS( "libs/wine/collation.c", READ_SORTKEYS_FILE() );
2223 DUMP_COMPOSE_TABLES( "libs/wine/compose.c" );
2224 DUMP_CTYPE_TABLES( "libs/wine/wctype.c" );
2225 dump_mirroring( "dlls/usp10/mirror.c" );
2226 dump_shaping( "dlls/usp10/shaping.c" );
2227 dump_linebreak( "dlls/usp10/linebreak.c" );
2228 dump_indic( "dlls/usp10/indicsyllable.c" );
2229 dump_intl_nls("loader/l_intl.nls");
2230 dump_vertical( "dlls/gdi32/vertical.c" );
2231 dump_vertical( "dlls/wineps.drv/vertical.c" );
2232 dump_nameprep( "dlls/kernel32/nameprep.c" );
2234 foreach my $file (@allfiles) { HANDLE_FILE( @{$file} ); }
2236 output_cptable("libs/wine/cptable.c");
2238 exit 0;
2240 # Local Variables:
2241 # compile-command: "./make_unicode"
2242 # End: