libport: Move the codepage table functions back to libwine and make them obsolete.
[wine.git] / tools / make_unicode
blob80bf6304c3912fd322056c6c18eb3f4a188eaf87
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/12.1.0/ucd/UCD.zip";
27 my $REPORTS = "http://www.unicode.org/reports";
28 my $RFCS = "http://www.rfc-editor.org/rfc";
29 my $MSDATA = "https://download.microsoft.com/download/C/F/7/CF713A5E-9FBC-4FD6-9246-275F65C0E498";
30 my $MSCODEPAGES = "$MSDATA/Windows Supported Code Page Data Files.zip";
32 # Sort keys file
33 my $SORTKEYS = "tr10/allkeys.txt";
35 # RFC3454 (stringprep data)
36 my $STRINGPREP = "rfc3454.txt";
38 # Default char for undefined mappings
39 my $DEF_CHAR = ord '?';
41 my @allfiles =
43 [ 37, "CodpageFiles/037.txt", "IBM EBCDIC US Canada" ],
44 [ 437, "CodpageFiles/437.txt", "OEM United States" ],
45 [ 500, "CodpageFiles/500.txt", "IBM EBCDIC International" ],
46 [ 737, "CodpageFiles/737.txt", "OEM Greek 437G" ],
47 [ 775, "CodpageFiles/775.txt", "OEM Baltic" ],
48 [ 850, "CodpageFiles/850.txt", "OEM Multilingual Latin 1" ],
49 [ 852, "CodpageFiles/852.txt", "OEM Slovak Latin 2" ],
50 [ 855, "CodpageFiles/855.txt", "OEM Cyrillic" ],
51 [ 857, "CodpageFiles/857.txt", "OEM Turkish" ],
52 [ 860, "CodpageFiles/860.txt", "OEM Portuguese" ],
53 [ 861, "CodpageFiles/861.txt", "OEM Icelandic" ],
54 [ 862, "CodpageFiles/862.txt", "OEM Hebrew" ],
55 [ 863, "CodpageFiles/863.txt", "OEM Canadian French" ],
56 [ 864, "CodpageFiles/864.txt", "OEM Arabic" ],
57 [ 865, "CodpageFiles/865.txt", "OEM Nordic" ],
58 [ 866, "CodpageFiles/866.txt", "OEM Russian" ],
59 [ 869, "CodpageFiles/869.txt", "OEM Greek" ],
60 [ 874, "CodpageFiles/874.txt", "ANSI/OEM Thai" ],
61 [ 875, "CodpageFiles/875.txt", "IBM EBCDIC Greek" ],
62 [ 932, "CodpageFiles/932.txt", "ANSI/OEM Japanese Shift-JIS" ],
63 [ 936, "CodpageFiles/936.txt", "ANSI/OEM Simplified Chinese GBK" ],
64 [ 949, "CodpageFiles/949.txt", "ANSI/OEM Korean Unified Hangul" ],
65 [ 950, "CodpageFiles/950.txt", "ANSI/OEM Traditional Chinese Big5" ],
66 [ 1026, "CodpageFiles/1026.txt", "IBM EBCDIC Latin 5 Turkish" ],
67 [ 1250, "CodpageFiles/1250.txt", "ANSI Eastern Europe" ],
68 [ 1251, "CodpageFiles/1251.txt", "ANSI Cyrillic" ],
69 [ 1252, "CodpageFiles/1252.txt", "ANSI Latin 1" ],
70 [ 1253, "CodpageFiles/1253.txt", "ANSI Greek" ],
71 [ 1254, "CodpageFiles/1254.txt", "ANSI Turkish" ],
72 [ 1255, "CodpageFiles/1255.txt", "ANSI Hebrew" ],
73 [ 1256, "CodpageFiles/1256.txt", "ANSI Arabic" ],
74 [ 1257, "CodpageFiles/1257.txt", "ANSI Baltic" ],
75 [ 1258, "CodpageFiles/1258.txt", "ANSI/OEM Viet Nam" ],
76 [ 1361, "CodpageFiles/1361.txt", "Korean Johab" ],
77 [ 10000, "CodpageFiles/10000.txt", "Mac Roman" ],
78 [ 10001, "CodpageFiles/10001.txt", "Mac Japanese" ],
79 [ 10002, "CodpageFiles/10002.txt", "Mac Traditional Chinese" ],
80 [ 10003, "CodpageFiles/10003.txt", "Mac Korean" ],
81 [ 10004, "CodpageFiles/10004.txt", "Mac Arabic" ],
82 [ 10005, "CodpageFiles/10005.txt", "Mac Hebrew" ],
83 [ 10006, "CodpageFiles/10006.txt", "Mac Greek" ],
84 [ 10007, "CodpageFiles/10007.txt", "Mac Cyrillic" ],
85 [ 10008, "CodpageFiles/10008.txt", "Mac Simplified Chinese" ],
86 [ 10010, "CodpageFiles/10010.txt", "Mac Romanian" ],
87 [ 10017, "CodpageFiles/10017.txt", "Mac Ukrainian" ],
88 [ 10021, "CodpageFiles/10021.txt", "Mac Thai" ],
89 [ 10029, "CodpageFiles/10029.txt", "Mac Latin 2" ],
90 [ 10079, "CodpageFiles/10079.txt", "Mac Icelandic" ],
91 [ 10081, "CodpageFiles/10081.txt", "Mac Turkish" ],
92 [ 10082, "CodpageFiles/10082.txt", "Mac Croatian" ],
93 [ 20127, "CodpageFiles/20127.txt", "US-ASCII (7bit)" ],
94 [ 20866, "CodpageFiles/20866.txt", "Russian KOI8" ],
95 [ 20932, "OBSOLETE/EASTASIA/JIS", "EUC-JP" ],
96 [ 21866, "CodpageFiles/21866.txt", "Ukrainian KOI8" ],
97 [ 28591, "CodpageFiles/28591.txt", "ISO 8859-1 Latin 1" ],
98 [ 28592, "CodpageFiles/28592.txt", "ISO 8859-2 Latin 2 (East European)" ],
99 [ 28593, "CodpageFiles/28593.txt", "ISO 8859-3 Latin 3 (South European)" ],
100 [ 28594, "CodpageFiles/28594.txt", "ISO 8859-4 Latin 4 (Baltic old)" ],
101 [ 28595, "CodpageFiles/28595.txt", "ISO 8859-5 Cyrillic" ],
102 [ 28596, "CodpageFiles/28596.txt", "ISO 8859-6 Arabic" ],
103 [ 28597, "CodpageFiles/28597.txt", "ISO 8859-7 Greek" ],
104 [ 28598, "CodpageFiles/28598.txt", "ISO 8859-8 Hebrew" ],
105 [ 28599, "CodpageFiles/28599.txt", "ISO 8859-9 Latin 5 (Turkish)" ],
106 [ 28603, "CodpageFiles/28603.txt", "ISO 8859-13 Latin 7 (Baltic)" ],
107 [ 28605, "CodpageFiles/28605.txt", "ISO 8859-15 Latin 9 (Euro)" ],
111 my %ctype =
113 "upper" => 0x0001,
114 "lower" => 0x0002,
115 "digit" => 0x0004,
116 "space" => 0x0008,
117 "punct" => 0x0010,
118 "cntrl" => 0x0020,
119 "blank" => 0x0040,
120 "xdigit" => 0x0080,
121 "alpha" => 0x0100,
122 "defin" => 0x0200
125 my %bracket_types =
127 "o" => 0x0000,
128 "c" => 0x0001,
131 my %indic_types =
133 "Other" => 0x0000,
134 "Bindu" => 0x0001,
135 "Visarga" => 0x0002,
136 "Avagraha" => 0x0003,
137 "Nukta" => 0x0004,
138 "Virama" => 0x0005,
139 "Vowel_Independent" => 0x0006,
140 "Vowel_Dependent" => 0x0007,
141 "Vowel" => 0x0008,
142 "Consonant_Placeholder" => 0x0009,
143 "Consonant" => 0x000a,
144 "Consonant_Dead" => 0x000b,
145 "Consonant_Succeeding_Repha" => 0x000c,
146 "Consonant_Subjoined" => 0x000d,
147 "Consonant_Medial" => 0x000e,
148 "Consonant_Final" => 0x000f,
149 "Consonant_Head_Letter" => 0x0010,
150 "Modifying_Letter" => 0x0011,
151 "Tone_Letter" => 0x0012,
152 "Tone_Mark" => 0x0013,
153 "Register_Shifter" => 0x0014,
154 "Consonant_Preceding_Repha" => 0x0015,
155 "Pure_Killer" => 0x0016,
156 "Invisible_Stacker" => 0x0017,
157 "Gemination_Mark" => 0x0018,
158 "Cantillation_Mark" => 0x0019,
159 "Non_Joiner" => 0x001a,
160 "Joiner" => 0x001b,
161 "Number_Joiner" => 0x001c,
162 "Number" => 0x001d,
163 "Brahmi_Joining_Number" => 0x001e,
164 "Consonant_With_Stacker" => 0x001f,
165 "Consonant_Prefixed" => 0x0020,
166 "Syllable_Modifier" => 0x0021,
167 "Consonant_Killer" => 0x0022,
168 "Consonant_Initial_Postfixed" => 0x0023,
171 my %matra_types =
173 "Right" => 0x01,
174 "Left" => 0x02,
175 "Visual_Order_Left" => 0x03,
176 "Left_And_Right" => 0x04,
177 "Top" => 0x05,
178 "Bottom" => 0x06,
179 "Top_And_Bottom" => 0x07,
180 "Top_And_Right" => 0x08,
181 "Top_And_Left" => 0x09,
182 "Top_And_Left_And_Right" => 0x0a,
183 "Bottom_And_Right" => 0x0b,
184 "Top_And_Bottom_And_Right" => 0x0c,
185 "Overstruck" => 0x0d,
186 "Invisible" => 0x0e,
187 "Bottom_And_Left" => 0x0f
190 my %nameprep_flags =
192 "unassigned" => 0x01,
193 "prohibited" => 0x02,
194 "bidi_ral" => 0x04,
195 "bidi_l" => 0x08
198 my %break_types =
200 "BK" => 0x0001,
201 "CR" => 0x0002,
202 "LF" => 0x0003,
203 "CM" => 0x0004,
204 "SG" => 0x0005,
205 "GL" => 0x0006,
206 "CB" => 0x0007,
207 "SP" => 0x0008,
208 "ZW" => 0x0009,
209 "NL" => 0x000a,
210 "WJ" => 0x000b,
211 "JL" => 0x000c,
212 "JV" => 0x000d,
213 "JT" => 0x000e,
214 "H2" => 0x000f,
215 "H3" => 0x0010,
216 "XX" => 0x0011,
217 "OP" => 0x0012,
218 "CL" => 0x0013,
219 "CP" => 0x0014,
220 "QU" => 0x0015,
221 "NS" => 0x0016,
222 "EX" => 0x0017,
223 "SY" => 0x0018,
224 "IS" => 0x0019,
225 "PR" => 0x001a,
226 "PO" => 0x001b,
227 "NU" => 0x001c,
228 "AL" => 0x001d,
229 "ID" => 0x001e,
230 "IN" => 0x001f,
231 "HY" => 0x0020,
232 "BB" => 0x0021,
233 "BA" => 0x0022,
234 "SA" => 0x0023,
235 "AI" => 0x0024,
236 "B2" => 0x0025,
237 "HL" => 0x0026,
238 "CJ" => 0x0027,
239 "RI" => 0x0028,
240 "EB" => 0x0029,
241 "EM" => 0x002a,
242 "ZWJ" => 0x002b,
245 my %vertical_types =
247 "R" => 0x0000,
248 "U" => 0x0001,
249 "Tr" => 0x0002,
250 "Tu" => 0x0003,
253 my %categories =
255 "Lu" => $ctype{"defin"}|$ctype{"alpha"}|$ctype{"upper"}, # Letter, Uppercase
256 "Ll" => $ctype{"defin"}|$ctype{"alpha"}|$ctype{"lower"}, # Letter, Lowercase
257 "Lt" => $ctype{"defin"}|$ctype{"alpha"}|$ctype{"upper"}|$ctype{"lower"}, # Letter, Titlecase
258 "Mn" => $ctype{"defin"}, # Mark, Non-Spacing
259 "Mc" => $ctype{"defin"}, # Mark, Spacing Combining
260 "Me" => $ctype{"defin"}, # Mark, Enclosing
261 "Nd" => $ctype{"defin"}|$ctype{"digit"}, # Number, Decimal Digit
262 "Nl" => $ctype{"defin"}|$ctype{"alpha"}, # Number, Letter
263 "No" => $ctype{"defin"}, # Number, Other
264 "Zs" => $ctype{"defin"}|$ctype{"space"}, # Separator, Space
265 "Zl" => $ctype{"defin"}|$ctype{"space"}, # Separator, Line
266 "Zp" => $ctype{"defin"}|$ctype{"space"}, # Separator, Paragraph
267 "Cc" => $ctype{"defin"}|$ctype{"cntrl"}, # Other, Control
268 "Cf" => $ctype{"defin"}|$ctype{"cntrl"}, # Other, Format
269 "Cs" => $ctype{"defin"}, # Other, Surrogate
270 "Co" => $ctype{"defin"}, # Other, Private Use
271 "Cn" => $ctype{"defin"}, # Other, Not Assigned
272 "Lm" => $ctype{"defin"}|$ctype{"alpha"}, # Letter, Modifier
273 "Lo" => $ctype{"defin"}|$ctype{"alpha"}, # Letter, Other
274 "Pc" => $ctype{"defin"}|$ctype{"punct"}, # Punctuation, Connector
275 "Pd" => $ctype{"defin"}|$ctype{"punct"}, # Punctuation, Dash
276 "Ps" => $ctype{"defin"}|$ctype{"punct"}, # Punctuation, Open
277 "Pe" => $ctype{"defin"}|$ctype{"punct"}, # Punctuation, Close
278 "Pi" => $ctype{"defin"}|$ctype{"punct"}, # Punctuation, Initial quote
279 "Pf" => $ctype{"defin"}|$ctype{"punct"}, # Punctuation, Final quote
280 "Po" => $ctype{"defin"}|$ctype{"punct"}, # Punctuation, Other
281 "Sm" => $ctype{"defin"}, # Symbol, Math
282 "Sc" => $ctype{"defin"}, # Symbol, Currency
283 "Sk" => $ctype{"defin"}, # Symbol, Modifier
284 "So" => $ctype{"defin"} # Symbol, Other
287 # a few characters need additional categories that cannot be determined automatically
288 my %special_categories =
290 "xdigit" => [ ord('0')..ord('9'),ord('A')..ord('F'),ord('a')..ord('f'),
291 0xff10..0xff19, 0xff21..0xff26, 0xff41..0xff46 ],
292 "space" => [ 0x09..0x0d, 0x85 ],
293 "blank" => [ 0x09, 0x20, 0xa0, 0x3000, 0xfeff ],
294 "cntrl" => [ 0x070f, 0x200c, 0x200d,
295 0x200e, 0x200f, 0x202a, 0x202b, 0x202c, 0x202d, 0x202e,
296 0x206a, 0x206b, 0x206c, 0x206d, 0x206e, 0x206f, 0xfeff,
297 0xfff9, 0xfffa, 0xfffb ],
298 "punct" => [ 0x24, 0x2b, 0x3c..0x3e, 0x5e, 0x60, 0x7c, 0x7e, 0xa2..0xbe,
299 0xd7, 0xf7 ],
300 "digit" => [ 0xb2, 0xb3, 0xb9 ],
301 "lower" => [ 0x2071, 0x207f ]
304 my %directions =
306 "L" => 1, # Left-to-Right
307 "R" => 2, # Right-to-Left
308 "AL" => 12, # Right-to-Left Arabic
309 "EN" => 3, # European Number
310 "ES" => 4, # European Number Separator
311 "ET" => 5, # European Number Terminator
312 "AN" => 6, # Arabic Number
313 "CS" => 7, # Common Number Separator
314 "NSM" => 13, # Non-Spacing Mark
315 "BN" => 14, # Boundary Neutral
316 "B" => 8, # Paragraph Separator
317 "S" => 9, # Segment Separator
318 "WS" => 10, # Whitespace
319 "ON" => 11, # Other Neutrals
320 "LRE" => 15, # Left-to-Right Embedding
321 "LRO" => 15, # Left-to-Right Override
322 "RLE" => 15, # Right-to-Left Embedding
323 "RLO" => 15, # Right-to-Left Override
324 "PDF" => 15, # Pop Directional Format
325 "LRI" => 15, # Left-to-Right Isolate
326 "RLI" => 15, # Right-to-Left Isolate
327 "FSI" => 15, # First Strong Isolate
328 "PDI" => 15 # Pop Directional Isolate
331 my %c2_types =
333 "L" => 1, # C2_LEFTTORIGHT
334 "R" => 2, # C2_RIGHTTOLEFT
335 "AL" => 2, # C2_RIGHTTOLEFT
336 "EN" => 3, # C2_EUROPENUMBER
337 "ES" => 4, # C2_EUROPESEPARATOR
338 "ET" => 5, # C2_EUROPETERMINATOR
339 "AN" => 6, # C2_ARABICNUMBER
340 "CS" => 7, # C2_COMMONSEPARATOR
341 "NSM" => 0, # C2_NOTAPPLICABLE
342 "BN" => 0, # C2_NOTAPPLICABLE
343 "B" => 8, # C2_BLOCKSEPARATOR
344 "S" => 9, # C2_SEGMENTSEPARATOR
345 "WS" => 10, # C2_WHITESPACE
346 "ON" => 11, # C2_OTHERNEUTRAL
347 "LRE" => 11, # C2_OTHERNEUTRAL
348 "LRO" => 11, # C2_OTHERNEUTRAL
349 "RLE" => 11, # C2_OTHERNEUTRAL
350 "RLO" => 11, # C2_OTHERNEUTRAL
351 "PDF" => 11, # C2_OTHERNEUTRAL
352 "LRI" => 11, # C2_OTHERNEUTRAL
353 "RLI" => 11, # C2_OTHERNEUTRAL
354 "FSI" => 11, # C2_OTHERNEUTRAL
355 "PDI" => 11 # C2_OTHERNEUTRAL
358 my %bidi_types =
360 "ON" => 0, # Other Neutrals
361 "L" => 1, # Left-to-Right
362 "R" => 2, # Right-to-Left
363 "AN" => 3, # Arabic Number
364 "EN" => 4, # European Number
365 "AL" => 5, # Right-to-Left Arabic
366 "NSM" => 6, # Non-Spacing Mark
367 "CS" => 7, # Common Number Separator
368 "ES" => 8, # European Number Separator
369 "ET" => 9, # European Number Terminator
370 "BN" => 10, # Boundary Neutral
371 "S" => 11, # Segment Separator
372 "WS" => 12, # Whitespace
373 "B" => 13, # Paragraph Separator
374 "RLO" => 14, # Right-to-Left Override
375 "RLE" => 15, # Right-to-Left Embedding
376 "LRO" => 16, # Left-to-Right Override
377 "LRE" => 17, # Left-to-Right Embedding
378 "PDF" => 18, # Pop Directional Format
379 "LRI" => 19, # Left-to-Right Isolate
380 "RLI" => 20, # Right-to-Left Isolate
381 "FSI" => 21, # First Strong Isolate
382 "PDI" => 22 # Pop Directional Isolate
385 my %joining_types =
387 "U" => 0, # Non_Joining
388 "T" => 1, # Transparent
389 "R" => 2, # Right_Joining
390 "L" => 3, # Left_Joining
391 "D" => 4, # Dual_Joining
392 "C" => 5, # Join_Causing
395 my @cp2uni = ();
396 my @glyph2uni = ();
397 my @lead_bytes = ();
398 my @uni2cp = ();
399 my @tolower_table = ();
400 my @toupper_table = ();
401 my @digitmap_table = ();
402 my @category_table = ();
403 my @joining_table = ();
404 my @direction_table = ();
405 my @decomp_table = ();
406 my @compose_table = ();
407 my @combining_class_table = ();
408 my @decomp_compat_table = ();
409 my $default_char;
410 my $default_wchar;
412 my %joining_forms =
414 "isolated" => [],
415 "final" => [],
416 "initial" => [],
417 "medial" => []
420 sub get_utf16($)
422 my $ch = shift;
423 return $ch if ($ch < 0x10000);
424 $ch -= 0x10000;
425 return ( 0xd800 | ($ch >> 10), 0xdc00 | ($ch & 0x3ff) );
428 ################################################################
429 # fetch a unicode.org file and open it
430 sub open_data_file($$)
432 my ($base, $name) = @_;
433 (my $dir = "data/$name") =~ s/\/[^\/]+$//;
434 local *FILE;
435 unless (-f "data/$name")
437 system "mkdir", "-p", $dir;
438 if ($base =~ /.*\/([^\/]+\.zip)$/)
440 my $zip = $1;
441 unless (-f "data/$zip")
443 print "Fetching $base...\n";
444 !system "wget", "-q", "-O", "data/$zip", "$base" or die "cannot fetch $base";
446 !system "unzip", "-q", "-d", "data", "data/$zip", $name or die "cannot extract $name from $zip";
448 else
450 print "Fetching $base/$name...\n";
451 !system "wget", "-q", "-O", "data/$name", "$base/$name" or die "cannot fetch $base/$name";
454 open FILE, "<data/$name" or die "cannot open data/$name";
455 return *FILE;
458 ################################################################
459 # recursively get the decomposition for a character
460 sub get_decomposition($$);
461 sub get_decomposition($$)
463 my ($char, $table) = @_;
464 my @ret;
466 return get_utf16($char) unless defined ${$table}[$char];
467 foreach my $ch (@{${$table}[$char]})
469 push @ret, get_decomposition( $ch, $table );
471 return @ret;
474 ################################################################
475 # recursively build decompositions
476 sub build_decompositions(@)
478 my @src = @_;
479 my @dst;
481 for (my $i = 0; $i < 65536; $i++)
483 next unless defined $src[$i];
484 my @decomp = get_decomposition( $i, \@src );
485 $dst[$i] = \@decomp;
487 return @dst;
490 ################################################################
491 # read in the Unicode database files
492 sub load_data()
494 my $start;
496 # now build mappings from the decomposition field of the Unicode database
498 my $UNICODE_DATA = open_data_file( $UNIDATA, "UnicodeData.txt" );
499 while (<$UNICODE_DATA>)
501 # Decode the fields ...
502 my ($code, $name, $cat, $comb, $bidi,
503 $decomp, $dec, $dig, $num, $mirror,
504 $oldname, $comment, $upper, $lower, $title) = split /;/;
505 my $dst;
506 my $src = hex $code;
508 die "unknown category $cat" unless defined $categories{$cat};
509 die "unknown directionality $bidi" unless defined $directions{$bidi};
511 $category_table[$src] = $categories{$cat};
512 $direction_table[$src] = $bidi;
513 $joining_table[$src] = $joining_types{"T"} if $cat eq "Mn" || $cat eq "Me" || $cat eq "Cf";
515 if ($lower ne "")
517 $tolower_table[$src] = hex $lower;
519 if ($upper ne "")
521 $toupper_table[$src] = hex $upper;
523 if ($dec ne "")
525 $category_table[$src] |= $ctype{"digit"};
527 if ($dig ne "")
529 $digitmap_table[$src] = ord $dig;
531 if ($comb ne "")
533 $combining_class_table[$src] = $comb;
536 # copy the category and direction for everything between First/Last pairs
537 if ($name =~ /, First>/) { $start = $src; }
538 if ($name =~ /, Last>/)
540 while ($start < $src)
542 $category_table[$start] = $category_table[$src];
543 $direction_table[$start] = $direction_table[$src];
544 $start++;
548 next if $decomp eq ""; # no decomposition, skip it
550 if ($decomp =~ /^<([a-zA-Z]+)>\s+([0-9a-fA-F]+)/)
552 my @seq = map { hex $_; } (split /\s+/, (split /\s+/, $decomp, 2)[1]);
553 $decomp_compat_table[$src] = \@seq;
556 if ($decomp =~ /^<([a-zA-Z]+)>\s+([0-9a-fA-F]+)$/)
558 # decomposition of the form "<foo> 1234" -> use char if type is known
559 if ($1 eq "isolated" || $1 eq "final" || $1 eq "initial" || $1 eq "medial")
561 ${joining_forms{$1}}[hex $2] = $src;
562 next;
564 next unless ($1 eq "font" ||
565 $1 eq "noBreak" ||
566 $1 eq "circle" ||
567 $1 eq "super" ||
568 $1 eq "sub" ||
569 $1 eq "wide" ||
570 $1 eq "narrow" ||
571 $1 eq "compat" ||
572 $1 eq "small");
573 $dst = hex $2;
575 elsif ($decomp =~ /^<compat>\s+0020\s+([0-9a-fA-F]+)/)
577 # decomposition "<compat> 0020 1234" -> combining accent
578 $dst = hex $1;
580 elsif ($decomp =~ /^([0-9a-fA-F]+)/)
582 # decomposition contains only char values without prefix -> use first char
583 $dst = hex $1;
584 $category_table[$src] |= $category_table[$dst] if defined $category_table[$dst];
585 # store decomposition if it contains two chars
586 if ($decomp =~ /^([0-9a-fA-F]+)\s+([0-9a-fA-F]+)$/)
588 $decomp_table[$src] = $decomp_compat_table[$src] = [ hex $1, hex $2 ];
589 push @compose_table, [ hex $1, hex $2, $src ];
591 elsif ($decomp =~ /^([0-9a-fA-F]+)$/)
593 # Single char decomposition
594 $decomp_table[$src] = $decomp_compat_table[$src] = [ hex $1 ];
597 else
599 next;
602 close $UNICODE_DATA;
604 # patch the category of some special characters
606 foreach my $cat (keys %special_categories)
608 my $flag = $ctype{$cat};
609 foreach my $i (@{$special_categories{$cat}}) { $category_table[$i] |= $flag; }
612 # remove case mappings that don't round-trip
614 for (my $i = 0; $i < @toupper_table; $i++)
616 next unless defined $toupper_table[$i];
617 my $ch = $toupper_table[$i];
618 $toupper_table[$i] = undef unless defined $tolower_table[$ch] && $tolower_table[$ch] == $i;
620 for (my $i = 0; $i < @tolower_table; $i++)
622 next unless defined $tolower_table[$i];
623 my $ch = $tolower_table[$i];
624 $tolower_table[$i] = undef unless defined $toupper_table[$ch] && $toupper_table[$ch] == $i;
629 ################################################################
630 # define a new lead byte
631 sub add_lead_byte($)
633 my $ch = shift;
634 return if defined $cp2uni[$ch];
635 push @lead_bytes, $ch;
636 $cp2uni[$ch] = 0;
639 ################################################################
640 # define a new char mapping
641 sub add_mapping($$)
643 my ($cp, $uni) = @_;
644 $cp2uni[$cp] = $uni unless defined($cp2uni[$cp]);
645 $uni2cp[$uni] = $cp unless defined($uni2cp[$uni]);
646 if ($cp > 0xff) { add_lead_byte( $cp >> 8 ); }
649 ################################################################
650 # get a mapping including glyph chars for MB_USEGLYPHCHARS
651 sub get_glyphs_mapping(@)
653 my @table = @_;
655 for (my $i = 0; $i < @glyph2uni; $i++)
657 $table[$i] = $glyph2uni[$i] if defined $glyph2uni[$i];
659 return @table;
662 ################################################################
663 # build EUC-JP table from the JIS 0208/0212 files
664 sub output_eucjp_codepage($$)
666 my ($dir, $comment) = @_;
668 $default_char = $DEF_CHAR;
669 $default_wchar = 0x30fb;
671 # ASCII chars
672 foreach my $i (0x00 .. 0x7f) { add_mapping( $i, $i ); }
674 # lead bytes
675 foreach my $i (0x8e, 0xa1 .. 0xfe) { add_lead_byte($i); }
677 # JIS X 0201 right plane
678 foreach my $i (0xa1 .. 0xdf) { add_mapping( 0x8e00 + $i, 0xfec0 + $i ); }
680 # undefined chars
681 foreach my $i (0x80 .. 0x8d, 0x8f .. 0x9f) { $cp2uni[$i] = $i; }
682 $cp2uni[0xa0] = 0xf8f0;
683 $cp2uni[0xff] = 0xf8f3;
685 # Fix backslash conversion
686 add_mapping( 0xa1c0, 0xff3c );
688 # Add private mappings for rows undefined in JIS 0208/0212
689 my $private = 0xe000;
690 foreach my $hi (0xf5 .. 0xfe)
692 foreach my $lo (0xa1 .. 0xfe)
694 add_mapping( ($hi << 8) + $lo, $private++ );
697 foreach my $hi (0xf5 .. 0xfe)
699 foreach my $lo (0x21 .. 0x7e)
701 add_mapping( ($hi << 8) + $lo, $private++ );
705 my $name = "$dir/JIS0208.TXT";
706 my $INPUT = open_data_file( $MAPPINGS, $name );
707 while (<$INPUT>)
709 next if /^\#/; # skip comments
710 next if /^$/; # skip empty lines
711 next if /\x1a/; # skip ^Z
712 if (/^0x[0-9a-fA-F]+\s+0x([0-9a-fA-F]+)\s+0x([0-9a-fA-F]+)\s+(\#.*)?/)
714 add_mapping( 0x8080 + hex $1, hex $2 );
715 next;
717 die "$name: Unrecognized line $_\n";
719 close $INPUT;
721 $name = "$dir/JIS0212.TXT";
722 $INPUT = open_data_file( $MAPPINGS, $name );
723 while (<$INPUT>)
725 next if /^\#/; # skip comments
726 next if /^$/; # skip empty lines
727 next if /\x1a/; # skip ^Z
728 if (/^0x([0-9a-fA-F]+)\s+0x([0-9a-fA-F]+)\s+(\#.*)?/)
730 add_mapping( 0x8000 + hex $1, hex $2 );
731 next;
733 die "$name: Unrecognized line $_\n";
735 close $INPUT;
737 output_codepage_file( 20932, "$MAPPINGS/$dir", $comment );
741 ################################################################
742 # build the sort keys table
743 sub dump_sortkeys($)
745 my $filename = shift;
746 my @sortkeys = ();
748 my $INPUT = open_data_file( $REPORTS, $SORTKEYS );
749 while (<$INPUT>)
751 next if /^\#/; # skip comments
752 next if /^$/; # skip empty lines
753 next if /\x1a/; # skip ^Z
754 next if /^\@version/; # skip @version header
755 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]+)\]/)
757 my ($uni,$variable) = (hex $1, $2);
758 next if $uni > 65535;
759 $sortkeys[$uni] = [ $uni, hex $3, hex $4, hex $5, hex $6 ];
760 next;
762 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]+)\]/)
764 # multiple character sequence, ignored for now
765 next;
767 die "$SORTKEYS: Unrecognized line $_\n";
769 close $INPUT;
771 # compress the keys to 32 bit:
772 # key 1 to 16 bits, key 2 to 8 bits, key 3 to 4 bits, key 4 to 1 bit
774 @sortkeys = sort { ${$a}[1] <=> ${$b}[1] or
775 ${$a}[2] <=> ${$b}[2] or
776 ${$a}[3] <=> ${$b}[3] or
777 ${$a}[4] <=> ${$b}[4] or
778 $a cmp $b; } @sortkeys;
780 my ($n2, $n3) = (1, 1);
781 my @keys = (-1, -1, -1, -1, -1 );
782 my @flatkeys = ();
784 for (my $i = 0; $i < @sortkeys; $i++)
786 next unless defined $sortkeys[$i];
787 my @current = @{$sortkeys[$i]};
788 if ($current[1] == $keys[1])
790 if ($current[2] == $keys[2])
792 if ($current[3] == $keys[3])
794 # nothing
796 else
798 $keys[3] = $current[3];
799 $n3++;
800 die if ($n3 >= 16);
803 else
805 $keys[2] = $current[2];
806 $keys[3] = $current[3];
807 $n2++;
808 $n3 = 1;
809 die if ($n2 >= 256);
812 else
814 $keys[1] = $current[1];
815 $keys[2] = $current[2];
816 $keys[3] = $current[3];
817 $n2 = 1;
818 $n3 = 1;
821 if ($current[2]) { $current[2] = $n2; }
822 if ($current[3]) { $current[3] = $n3; }
823 if ($current[4]) { $current[4] = 1; }
825 $flatkeys[$current[0]] = ($current[1] << 16) | ($current[2] << 8) | ($current[3] << 4) | $current[4];
828 open OUTPUT,">$filename.new" or die "Cannot create $filename";
829 printf "Building $filename\n";
830 printf OUTPUT "/* Unicode collation element table */\n";
831 printf OUTPUT "/* generated from %s */\n", "$REPORTS/$SORTKEYS";
832 printf OUTPUT "/* DO NOT EDIT!! */\n\n";
833 print OUTPUT "#include \"windef.h\"\n\n";
835 dump_two_level_mapping( "collation_table", 0xffffffff, 32, @flatkeys );
837 close OUTPUT;
838 save_file($filename);
842 ################################################################
843 # dump an array of integers
844 sub dump_array($$@)
846 my ($bit_width, $default, @array) = @_;
847 my $format = sprintf "0x%%0%ux", $bit_width / 4;
848 my $i;
849 my $ret = " ";
850 for ($i = 0; $i < $#array; $i++)
852 $ret .= sprintf($format, defined $array[$i] ? $array[$i] : $default);
853 $ret .= (($i % 8) != 7) ? ", " : ",\n ";
855 $ret .= sprintf($format, defined $array[$i] ? $array[$i] : $default);
856 return $ret;
860 ################################################################
861 # dump a unicode->ascii mapping table
862 sub dump_uni2cp_table($$)
864 my ($width, $def) = @_;
866 # count the number of unicode->ascii subtables that contain something
868 my @lowerbounds;
869 my @upperbounds;
870 my $index = 0;
871 my @filled = (-1) x 256;
872 for (my $i = 0; $i < 65536; $i++)
874 next unless defined $uni2cp[$i];
875 if ($filled[$i >> 8] == -1)
877 $lowerbounds[$index] = $i & 0xff;
878 $upperbounds[$index] = 0xff - $lowerbounds[$index];
879 $filled[$i >> 8] = $index * 256;
880 $index++;
882 else
884 $upperbounds[$index-1] = 0xff - ($i & 0xff);
888 # add defaults mapping at the end
889 $filled[256] = $index * 256;
890 $lowerbounds[$index] = 255;
891 $upperbounds[$index] = 0;
892 $index++;
894 # collapse blocks upwards if possible
895 my $removed = 0;
896 $index = 0;
897 for (my $i = 0; $i < 257; $i++)
899 next if $filled[$i] == -1;
900 if ($upperbounds[$index - 1] > $lowerbounds[$index])
902 $removed += $lowerbounds[$index];
904 else
906 $removed += $upperbounds[$index - 1];
907 $lowerbounds[$index] = $upperbounds[$index - 1];
909 $filled[$i] -= $removed;
910 $index++;
913 # output all the subtables into a single array
915 printf OUTPUT "static const unsigned %s uni2cp_low[] =\n{\n", $width == 8 ? "char" : "short";
917 for (my $i = $index = 0; $i < 257; $i++)
919 next if $filled[$i] == -1;
920 my $start = ($i << 8) + $lowerbounds[$index];
921 my $end = ($i << 8) + 255;
922 if ($i == 256)
924 print OUTPUT " /* defaults */\n";
925 printf OUTPUT "%s\n};\n\n", dump_array( $width, 0, ($def) x ($end - $start + 1) );
927 else
929 printf OUTPUT " /* 0x%04x .. 0x%04x */\n", $start, $end;
930 printf OUTPUT "%s,\n", dump_array( $width, $def, @uni2cp[$start .. $end] );
932 $index++;
935 # output a table of the offsets of the subtables in the previous array
937 for (my $i = 0; $i < 256; $i++)
939 if ($filled[$i] == -1) { $filled[$i] = $filled[256]; }
941 printf OUTPUT "static const unsigned short uni2cp_high[256] =\n";
942 printf OUTPUT "{\n%s\n};\n\n", dump_array( 16, 0, @filled[0..255] );
945 ################################################################
946 # dump an SBCS mapping table
947 sub dump_sbcs_table($$$$)
949 my ($codepage, $name, $def, $defw) = @_;
950 my $i;
952 # output the ascii->unicode table
954 if (@glyph2uni)
956 printf OUTPUT "static const WCHAR cp2uni[512] =\n";
957 printf OUTPUT "{\n%s", dump_array( 16, $defw, @cp2uni[0 .. 255] );
958 printf OUTPUT ",\n /* glyphs */\n%s\n};\n\n",
959 dump_array( 16, $defw, get_glyphs_mapping(@cp2uni[0 .. 255]) );
961 else
963 printf OUTPUT "static const WCHAR cp2uni[256] =\n";
964 printf OUTPUT "{\n%s\n};\n\n", dump_array( 16, $defw, @cp2uni[0 .. 255] );
967 dump_uni2cp_table( 8, $def );
969 # output the code page descriptor
971 printf OUTPUT "const struct sbcs_table DECLSPEC_HIDDEN cptable_%03d =\n{\n", $codepage;
972 printf OUTPUT " { %d, 1, 0x%04x, 0x%04x, \"%s\" },\n",
973 $codepage, $def, $defw, $name;
974 printf OUTPUT " cp2uni,\n";
975 if (@glyph2uni) { printf OUTPUT " cp2uni + 256,\n"; }
976 else { printf OUTPUT " cp2uni,\n"; }
977 printf OUTPUT " uni2cp_low,\n";
978 printf OUTPUT " uni2cp_high\n};\n";
982 ################################################################
983 # dump a DBCS mapping table
984 sub dump_dbcs_table($$$$@)
986 my ($codepage, $name, $def, $defw, @lb_ranges) = @_;
988 # build a list of lead bytes that are actually used
990 my @lblist = ();
991 LBLOOP: for (my $y = 0; $y <= $#lead_bytes; $y++)
993 $cp2uni[$lead_bytes[$y]] = $defw;
994 my $base = $lead_bytes[$y] << 8;
995 for (my $x = 0; $x < 256; $x++)
997 if (defined $cp2uni[$base+$x])
999 push @lblist,$lead_bytes[$y];
1000 next LBLOOP;
1004 my $unused = ($#lead_bytes > $#lblist);
1006 # output the ascii->unicode table for the single byte chars
1008 printf OUTPUT "static const WCHAR cp2uni[%d] =\n", 256 * ($#lblist + 2 + $unused);
1009 printf OUTPUT "{\n%s,\n", dump_array( 16, $defw, @cp2uni[0 .. 255] );
1011 # output the default table for unused lead bytes
1013 if ($unused)
1015 printf OUTPUT " /* unused lead bytes */\n";
1016 printf OUTPUT "%s,\n", dump_array( 16, 0, ($defw) x 256 );
1019 # output the ascii->unicode table for each DBCS lead byte
1021 for (my $y = 0; $y <= $#lblist; $y++)
1023 my $base = $lblist[$y] << 8;
1024 printf OUTPUT " /* lead byte %02x */\n", $lblist[$y];
1025 printf OUTPUT "%s", dump_array( 16, $defw, @cp2uni[$base .. $base+255] );
1026 printf OUTPUT ($y < $#lblist) ? ",\n" : "\n};\n\n";
1029 # output the lead byte subtables offsets
1031 my @offsets = (0) x 256;
1032 for (my $x = 0; $x <= $#lblist; $x++) { $offsets[$lblist[$x]] = $x + 1; }
1033 if ($unused)
1035 # increment all lead bytes offset to take into account the unused table
1036 for (my $x = 0; $x <= $#lead_bytes; $x++) { $offsets[$lead_bytes[$x]]++; }
1038 printf OUTPUT "static const unsigned char cp2uni_leadbytes[256] =\n";
1039 printf OUTPUT "{\n%s\n};\n\n", dump_array( 8, 0, @offsets );
1041 dump_uni2cp_table( 16, $def );
1043 # output the code page descriptor
1045 printf OUTPUT "const struct dbcs_table DECLSPEC_HIDDEN cptable_%03d =\n{\n", $codepage;
1046 printf OUTPUT " { %d, 2, 0x%04x, 0x%04x, \"%s\" },\n",
1047 $codepage, $def, $defw, $name;
1048 printf OUTPUT " cp2uni,\n";
1049 printf OUTPUT " cp2uni_leadbytes,\n";
1050 printf OUTPUT " uni2cp_low,\n";
1051 printf OUTPUT " uni2cp_high,\n";
1052 printf OUTPUT " {\n %s\n }\n", dump_array( 8, 0, @lb_ranges, 0, 0 );
1053 printf OUTPUT "};\n";
1057 ################################################################
1058 # dump an SBCS mapping table in binary format
1059 sub dump_binary_sbcs_table($)
1061 my $codepage = shift;
1063 my @header = ( 13, $codepage, 1, $default_char, $default_wchar, $cp2uni[$default_char], $uni2cp[$default_wchar] );
1064 my $wc_offset = 256 + 3 + (@glyph2uni ? 256 : 0);
1066 print OUTPUT pack "S<*", @header;
1067 print OUTPUT pack "C12", (0) x 12;
1068 print OUTPUT pack "S<*", $wc_offset, map { $_ || 0; } @cp2uni[0 .. 255];
1070 if (@glyph2uni)
1072 print OUTPUT pack "S<*", 256, get_glyphs_mapping(@cp2uni[0 .. 255]);
1074 else
1076 print OUTPUT pack "S<*", 0;
1079 print OUTPUT pack "S<*", 0, 0;
1081 print OUTPUT pack "C*", map { defined $_ ? $_ : $default_char; } @uni2cp[0 .. 65535];
1085 ################################################################
1086 # dump a DBCS mapping table in binary format
1087 sub dump_binary_dbcs_table($)
1089 my $codepage = shift;
1090 my @lb_ranges = get_lb_ranges();
1091 my @header = ( 13, $codepage, 2, $default_char, $default_wchar, $cp2uni[$default_char], $uni2cp[$default_wchar] );
1093 my @offsets = (0) x 256;
1094 my $pos = 0;
1095 foreach my $i (@lead_bytes)
1097 $offsets[$i] = ($pos += 256);
1098 $cp2uni[$i] = 0;
1101 my $wc_offset = 256 + 3 + 256 * (1 + scalar @lead_bytes);
1103 print OUTPUT pack "S<*", @header;
1104 print OUTPUT pack "C12", @lb_ranges, 0 x 12;
1105 print OUTPUT pack "S<*", $wc_offset, map { $_ || 0; } @cp2uni[0 .. 255];
1106 print OUTPUT pack "S<*", 0, scalar @lb_ranges / 2, @offsets;
1108 foreach my $i (@lead_bytes)
1110 my $base = $i << 8;
1111 print OUTPUT pack "S<*", map { defined $_ ? $_ : $default_wchar; } @cp2uni[$base .. $base + 255];
1114 print OUTPUT pack "S<", 4;
1115 print OUTPUT pack "S<*", map { defined $_ ? $_ : $default_char; } @uni2cp[0 .. 65535];
1119 ################################################################
1120 # get the list of defined lead byte ranges
1121 sub get_lb_ranges()
1123 my @list = ();
1124 my @ranges = ();
1126 foreach my $i (@lead_bytes) { $list[$i] = 1; }
1127 my $on = 0;
1128 for (my $i = 0; $i < 256; $i++)
1130 if ($on)
1132 if (!defined $list[$i]) { push @ranges, $i-1; $on = 0; }
1134 else
1136 if ($list[$i]) { push @ranges, $i; $on = 1; }
1139 if ($on) { push @ranges, 0xff; }
1140 return @ranges;
1143 ################################################################
1144 # dump the Indic Syllabic Category table
1145 sub dump_indic($)
1147 my $filename = shift;
1148 my @indic_table;
1150 my $INPUT = open_data_file( $UNIDATA, "IndicSyllabicCategory.txt" );
1151 while (<$INPUT>)
1153 next if /^\#/; # skip comments
1154 next if /^\s*$/; # skip empty lines
1155 next if /\x1a/; # skip ^Z
1156 if (/^\s*([0-9a-fA-F]+)\s*;\s*([a-zA-Z_]+)\s*#/)
1158 my $type = $2;
1159 die "unknown indic $type" unless defined $indic_types{$type};
1160 if (hex $1 < 65536)
1162 $indic_table[hex $1] = $indic_types{$type};
1164 next;
1166 elsif (/^\s*([0-9a-fA-F]+)..\s*([0-9a-fA-F]+)\s*;\s*([A-Za-z_]+)\s*#/)
1168 my $type = $3;
1169 die "unknown indic $type" unless defined $indic_types{$type};
1170 if (hex $1 < 65536 and hex $2 < 65536)
1172 foreach my $i (hex $1 .. hex $2)
1174 $indic_table[$i] = $indic_types{$type};
1177 next;
1179 die "malformed line $_";
1181 close $INPUT;
1183 $INPUT = open_data_file( $UNIDATA, "IndicPositionalCategory.txt" );
1184 while (<$INPUT>)
1186 next if /^\#/; # skip comments
1187 next if /^\s*$/; # skip empty lines
1188 next if /\x1a/; # skip ^Z
1189 if (/^\s*([0-9a-fA-F]+)\s*;\s*([a-zA-Z]+)\s*#/)
1191 my $type = $2;
1192 die "unknown matra $type" unless defined $matra_types{$type};
1193 $indic_table[hex $1] += $matra_types{$type} << 8;
1194 next;
1196 elsif (/^\s*([0-9a-fA-F]+)..\s*([0-9a-fA-F]+)\s*;\s*([A-Za-z_]+)\s*#/)
1198 my $type = $3;
1199 die "unknown matra $type" unless defined $matra_types{$type};
1200 foreach my $i (hex $1 .. hex $2)
1202 $indic_table[$i] += $matra_types{$type} << 8;
1204 next;
1206 die "malformed line $_";
1208 close $INPUT;
1210 open OUTPUT,">$filename.new" or die "Cannot create $filename";
1211 print "Building $filename\n";
1212 print OUTPUT "/* Unicode Indic Syllabic Category */\n";
1213 print OUTPUT "/* generated from $UNIDATA:IndicSyllabicCategory.txt */\n";
1214 print OUTPUT "/* and from $UNIDATA:IndicPositionalCategory.txt */\n";
1215 print OUTPUT "/* DO NOT EDIT!! */\n\n";
1216 print OUTPUT "#include \"windef.h\"\n\n";
1218 dump_two_level_mapping( "indic_syllabic_table", $indic_types{'Other'}, 16, @indic_table );
1220 close OUTPUT;
1221 save_file($filename);
1224 ################################################################
1225 # dump the Line Break Properties table
1226 sub dump_linebreak($)
1228 my $filename = shift;
1229 my @break_table;
1230 my $next_group = 0;
1232 my $INPUT = open_data_file( $UNIDATA, "LineBreak.txt" );
1233 while (<$INPUT>)
1235 next if /^\#/; # skip comments
1236 next if /^\s*$/; # skip empty lines
1237 next if /\x1a/; # skip ^Z
1238 if (/^\s*([0-9a-fA-F]+)\s*;\s*([0-9A-Z][0-9A-Z][0-9A-Z])+\s*/)
1240 my $type = $2;
1241 die "unknown breaktype $type" unless defined $break_types{$type};
1242 $break_table[hex $1] = $break_types{$type};
1243 next;
1245 elsif (/^\s*([0-9a-fA-F]+)..\s*([0-9a-fA-F]+)\s*;\s*([0-9A-Z][0-9A-Z][0-9A-Z])+\s*/)
1247 my $type = $3;
1248 die "unknown breaktype $type" unless defined $break_types{$type};
1249 foreach my $i (hex $1 .. hex $2)
1251 $break_table[$i] = $break_types{$type};
1253 next;
1255 elsif (/^\s*([0-9a-fA-F]+)\s*;\s*([0-9A-Z][0-9A-Z])+\s*/)
1257 my $type = $2;
1258 die "unknown breaktype $type" unless defined $break_types{$type};
1259 $break_table[hex $1] = $break_types{$type};
1260 next;
1262 elsif (/^\s*([0-9a-fA-F]+)..\s*([0-9a-fA-F]+)\s*;\s*([0-9A-Z][0-9A-Z])+\s*/)
1264 my $type = $3;
1265 die "unknown breaktype $type" unless defined $break_types{$type};
1266 foreach my $i (hex $1 .. hex $2)
1268 $break_table[$i] = $break_types{$type};
1270 next;
1272 die "malformed line $_";
1274 close $INPUT;
1276 open OUTPUT,">$filename.new" or die "Cannot create $filename";
1277 print "Building $filename\n";
1278 print OUTPUT "/* Unicode Line Break Properties */\n";
1279 print OUTPUT "/* generated from $UNIDATA:LineBreak.txt */\n";
1280 print OUTPUT "/* DO NOT EDIT!! */\n\n";
1281 print OUTPUT "#include \"windef.h\"\n\n";
1283 dump_two_level_mapping( "wine_linebreak_table", $break_types{'XX'}, 16, @break_table );
1285 close OUTPUT;
1286 save_file($filename);
1289 my %scripts =
1291 "Unknown" => 0,
1292 "Common" => 1,
1293 "Inherited" => 2,
1294 "Arabic" => 3,
1295 "Armenian" => 4,
1296 "Avestan" => 5,
1297 "Balinese" => 6,
1298 "Bamum" => 7,
1299 "Batak" => 8,
1300 "Bengali" => 9,
1301 "Bopomofo" => 10,
1302 "Brahmi" => 11,
1303 "Braille" => 12,
1304 "Buginese" => 13,
1305 "Buhid" => 14,
1306 "Canadian_Aboriginal" => 15,
1307 "Carian" => 16,
1308 "Cham" => 17,
1309 "Cherokee" => 18,
1310 "Coptic" => 19,
1311 "Cuneiform" => 20,
1312 "Cypriot" => 21,
1313 "Cyrillic" => 22,
1314 "Deseret" => 23,
1315 "Devanagari" => 24,
1316 "Egyptian_Hieroglyphs" => 25,
1317 "Ethiopic" => 26,
1318 "Georgian" => 27,
1319 "Glagolitic" => 28,
1320 "Gothic" => 29,
1321 "Greek" => 30,
1322 "Gujarati" => 31,
1323 "Gurmukhi" => 32,
1324 "Han" => 33,
1325 "Hangul" => 34,
1326 "Hanunoo" => 35,
1327 "Hebrew" => 36,
1328 "Hiragana" => 37,
1329 "Imperial_Aramaic" => 38,
1330 "Inscriptional_Pahlavi" => 39,
1331 "Inscriptional_Parthian" => 40,
1332 "Javanese" => 41,
1333 "Kaithi" => 42,
1334 "Kannada" => 43,
1335 "Katakana" => 44,
1336 "Kayah_Li" => 45,
1337 "Kharoshthi" => 46,
1338 "Khmer" => 47,
1339 "Lao" => 48,
1340 "Latin" => 49,
1341 "Lepcha" => 50,
1342 "Limbu" => 51,
1343 "Linear_B" => 52,
1344 "Lisu" => 53,
1345 "Lycian" => 54,
1346 "Lydian" => 55,
1347 "Malayalam" => 56,
1348 "Mandaic" => 57,
1349 "Meetei_Mayek" => 58,
1350 "Mongolian" => 59,
1351 "Myanmar" => 60,
1352 "New_Tai_Lue" => 61,
1353 "Nko" => 62,
1354 "Ogham" => 63,
1355 "Ol_Chiki" => 64,
1356 "Old_Italic" => 65,
1357 "Old_Persian" => 66,
1358 "Old_South_Arabian" => 67,
1359 "Old_Turkic" => 68,
1360 "Oriya" => 69,
1361 "Osmanya" => 70,
1362 "Phags_Pa" => 71,
1363 "Phoenician" => 72,
1364 "Rejang" => 73,
1365 "Runic" => 74,
1366 "Samaritan" => 75,
1367 "Saurashtra" => 76,
1368 "Shavian" => 77,
1369 "Sinhala" => 78,
1370 "Sundanese" => 79,
1371 "Syloti_Nagri" => 80,
1372 "Syriac" => 81,
1373 "Tagalog" => 82,
1374 "Tagbanwa" => 83,
1375 "Tai_Le" => 84,
1376 "Tai_Tham" => 85,
1377 "Tai_Viet" => 86,
1378 "Tamil" => 87,
1379 "Telugu" => 88,
1380 "Thaana" => 89,
1381 "Thai" => 90,
1382 "Tibetan" => 91,
1383 "Tifinagh" => 92,
1384 "Ugaritic" => 93,
1385 "Vai" => 94,
1386 "Yi" => 95,
1387 # Win8/Win8.1
1388 "Chakma" => 96,
1389 "Meroitic_Cursive" => 97,
1390 "Meroitic_Hieroglyphs" => 98,
1391 "Miao" => 99,
1392 "Sharada" => 100,
1393 "Sora_Sompeng" => 101,
1394 "Takri" => 102,
1395 # Win10
1396 "Bassa_Vah" => 103,
1397 "Caucasian_Albanian" => 104,
1398 "Duployan" => 105,
1399 "Elbasan" => 106,
1400 "Grantha" => 107,
1401 "Khojki" => 108,
1402 "Khudawadi" => 109,
1403 "Linear_A" => 110,
1404 "Mahajani" => 111,
1405 "Manichaean" => 112,
1406 "Mende_Kikakui" => 113,
1407 "Modi" => 114,
1408 "Mro" => 115,
1409 "Nabataean" => 116,
1410 "Old_North_Arabian" => 117,
1411 "Old_Permic" => 118,
1412 "Pahawh_Hmong" => 119,
1413 "Palmyrene" => 120,
1414 "Pau_Cin_Hau" => 121,
1415 "Psalter_Pahlavi" => 122,
1416 "Siddham" => 123,
1417 "Tirhuta" => 124,
1418 "Warang_Citi" => 125,
1419 # Win10 RS1
1420 "Adlam" => 126,
1421 "Ahom" => 127,
1422 "Anatolian_Hieroglyphs" => 128,
1423 "Bhaiksuki" => 129,
1424 "Hatran" => 130,
1425 "Marchen" => 131,
1426 "Multani" => 132,
1427 "Newa" => 133,
1428 "Old_Hungarian" => 134,
1429 "Osage" => 135,
1430 "SignWriting" => 136,
1431 "Tangut" => 137,
1432 # Win10 RS4
1433 "Masaram_Gondi" => 138,
1434 "Nushu" => 139,
1435 "Soyombo" => 140,
1436 "Zanabazar_Square" => 141,
1437 # Win10 1903
1438 "Dogra" => 142,
1439 "Gunjala_Gondi" => 143,
1440 "Hanifi_Rohingya" => 144,
1441 "Makasar" => 145,
1442 "Medefaidrin" => 146,
1443 "Old_Sogdian" => 147,
1444 "Sogdian" => 148,
1447 ################################################################
1448 # dump Script IDs table
1449 sub dump_scripts($)
1451 my $filename = shift;
1452 my $header = $filename;
1453 my @scripts_table;
1454 my $script_index;
1455 my $i;
1457 my $INPUT = open_data_file( $UNIDATA, "Scripts.txt" );
1458 # Fill the table
1459 # Unknown script id is always 0, so undefined scripts are automatically treated as such
1460 while (<$INPUT>)
1462 my $type = "";
1464 next if /^\#/; # skip comments
1465 next if /^\s*$/; # skip empty lines
1466 next if /\x1a/; # skip ^Z
1467 if (/^\s*([0-9a-fA-F]+)\s*;\s*([a-zA-Z_]+)\s*/)
1469 $type = $2;
1470 if (defined $scripts{$type})
1472 $scripts_table[hex $1] = $scripts{$type};
1474 next;
1476 elsif (/^\s*([0-9a-fA-F]+)..\s*([0-9a-fA-F]+)\s*;\s*([a-zA-Z_]+)\s*/)
1478 $type = $3;
1479 if (defined $scripts{$type})
1481 foreach my $i (hex $1 .. hex $2)
1483 $scripts_table[$i] = $scripts{$type};
1486 next;
1490 close $INPUT;
1492 $header = "$filename.h";
1493 open OUTPUT,">$header.new" or die "Cannot create $header";
1494 print "Building $header\n";
1495 print OUTPUT "/* Unicode Script IDs */\n";
1496 print OUTPUT "/* generated from $UNIDATA:Scripts.txt */\n";
1497 print OUTPUT "/* DO NOT EDIT!! */\n\n";
1499 print OUTPUT "enum unicode_script_id {\n";
1500 foreach my $script (sort { $scripts{$a} <=> $scripts{$b} } keys %scripts)
1502 print OUTPUT " Script_$script = $scripts{$script},\n";
1504 print OUTPUT " Script_LastId = ", (scalar keys %scripts) - 1, "\n";
1505 print OUTPUT "};\n";
1507 close OUTPUT;
1508 save_file($header);
1510 $filename = "$filename.c";
1511 open OUTPUT,">$filename.new" or die "Cannot create $header";
1512 print "Building $filename\n";
1513 print OUTPUT "/* Unicode Script IDs */\n";
1514 print OUTPUT "/* generated from $UNIDATA:Scripts.txt */\n";
1515 print OUTPUT "/* DO NOT EDIT!! */\n\n";
1516 print OUTPUT "#include \"windef.h\"\n\n";
1518 dump_two_level_mapping( "wine_scripts_table", 0, 16, @scripts_table );
1519 close OUTPUT;
1520 save_file($filename);
1523 ################################################################
1524 # dump the BiDi mirroring table
1525 sub dump_mirroring($)
1527 my $filename = shift;
1528 my @mirror_table = ();
1530 my $INPUT = open_data_file( $UNIDATA, "BidiMirroring.txt" );
1531 while (<$INPUT>)
1533 next if /^\#/; # skip comments
1534 next if /^$/; # skip empty lines
1535 next if /\x1a/; # skip ^Z
1536 if (/^\s*([0-9a-fA-F]+)\s*;\s*([0-9a-fA-F]+)/)
1538 $mirror_table[hex $1] = hex $2;
1539 next;
1541 die "malformed line $_";
1543 close $INPUT;
1545 open OUTPUT,">$filename.new" or die "Cannot create $filename";
1546 print "Building $filename\n";
1547 print OUTPUT "/* Unicode BiDi mirroring */\n";
1548 print OUTPUT "/* generated from $UNIDATA:BidiMirroring.txt */\n";
1549 print OUTPUT "/* DO NOT EDIT!! */\n\n";
1550 print OUTPUT "#include \"windef.h\"\n\n";
1551 dump_two_level_mapping( "wine_mirror_map", 0, 16, @mirror_table );
1552 close OUTPUT;
1553 save_file($filename);
1556 ################################################################
1557 # dump the Bidi Brackets
1558 sub dump_bracket($)
1560 my $filename = shift;
1561 my @bracket_table;
1563 my $INPUT = open_data_file( $UNIDATA, "BidiBrackets.txt" );
1564 while (<$INPUT>)
1566 next if /^\#/; # skip comments
1567 next if /^\s*$/; # skip empty lines
1568 next if /\x1a/; # skip ^Z
1569 if (/^\s*([0-9a-fA-F]+)\s*;\s*([0-9a-fA-F]+);\s*([con])/)
1571 my $type = $3;
1572 die "unknown bracket $type" unless defined $bracket_types{$type};
1573 die "characters too distant $1 and $2" if abs(hex($2) - hex($1)) >= 128;
1574 $bracket_table[hex $1] = (hex($2) - hex($1)) % 255;
1575 $bracket_table[hex $1] += $bracket_types{$type} << 8;
1576 next;
1578 die "malformed line $_";
1580 close $INPUT;
1582 open OUTPUT,">$filename.new" or die "Cannot create $filename";
1583 print "Building $filename\n";
1584 print OUTPUT "/* Unicode Bidirectional Bracket table */\n";
1585 print OUTPUT "/* generated from $UNIDATA:BidiBrackets.txt */\n";
1586 print OUTPUT "/* DO NOT EDIT!! */\n\n";
1587 print OUTPUT "#include \"windef.h\"\n\n";
1589 dump_two_level_mapping( "bidi_bracket_table", 0, 16, @bracket_table );
1591 close OUTPUT;
1592 save_file($filename);
1595 ################################################################
1596 # dump the Arabic shaping table
1597 sub dump_shaping($)
1599 my $filename = shift;
1600 my %groups;
1601 my $next_group = 0;
1603 $groups{"No_Joining_Group"} = $next_group++;
1605 my $INPUT = open_data_file( $UNIDATA, "ArabicShaping.txt" );
1606 while (<$INPUT>)
1608 next if /^\#/; # skip comments
1609 next if /^\s*$/; # skip empty lines
1610 next if /\x1a/; # skip ^Z
1611 if (/^\s*([0-9a-fA-F]+)\s*;.*;\s*([RLDCUT])\s*;\s*(\w+)/)
1613 my $type = $2;
1614 my $group = $3;
1615 $groups{$group} = $next_group++ unless defined $groups{$group};
1616 $joining_table[hex $1] = $joining_types{$type} | ($groups{$group} << 8);
1617 next;
1619 die "malformed line $_";
1621 close $INPUT;
1623 open OUTPUT,">$filename.new" or die "Cannot create $filename";
1624 print "Building $filename\n";
1625 print OUTPUT "/* Unicode Arabic shaping */\n";
1626 print OUTPUT "/* generated from $UNIDATA:ArabicShaping.txt */\n";
1627 print OUTPUT "/* DO NOT EDIT!! */\n\n";
1628 print OUTPUT "#include \"windef.h\"\n\n";
1630 dump_two_level_mapping( "wine_shaping_table", 0, 16, @joining_table );
1632 print OUTPUT "\nconst unsigned short DECLSPEC_HIDDEN wine_shaping_forms[256][4] =\n{\n";
1633 for (my $i = 0x600; $i <= 0x6ff; $i++)
1635 printf OUTPUT " { 0x%04x, 0x%04x, 0x%04x, 0x%04x },\n",
1636 ${joining_forms{"isolated"}}[$i] || $i,
1637 ${joining_forms{"final"}}[$i] || $i,
1638 ${joining_forms{"initial"}}[$i] || $i,
1639 ${joining_forms{"medial"}}[$i] || $i;
1641 print OUTPUT "};\n";
1643 close OUTPUT;
1644 save_file($filename);
1647 ################################################################
1648 # dump the Vertical Orientation table
1649 sub dump_vertical($)
1651 my $filename = shift;
1652 my @vertical_table;
1654 my $INPUT = open_data_file( $UNIDATA, "VerticalOrientation.txt" );
1655 while (<$INPUT>)
1657 next if /^\#/; # skip comments
1658 next if /^\s*$/; # skip empty lines
1659 next if /\x1a/; # skip ^Z
1660 if (/^\s*([0-9a-fA-F]+)\s*;\s*([a-zA-Z_]+)\s*/)
1662 my $type = $2;
1663 die "unknown vertical $type" unless defined $vertical_types{$type};
1664 if (hex $1 < 65536)
1666 $vertical_table[hex $1] = $vertical_types{$type};
1668 next;
1670 elsif (/^\s*([0-9a-fA-F]+)..\s*([0-9a-fA-F]+)\s*;\s*([A-Za-z_]+)\s*/)
1672 my $type = $3;
1673 die "unknown vertical $type" unless defined $vertical_types{$type};
1674 foreach my $i (hex $1 .. hex $2)
1676 $vertical_table[$i] = $vertical_types{$type};
1678 next;
1680 die "malformed line $_";
1682 close $INPUT;
1684 open OUTPUT,">$filename.new" or die "Cannot create $filename";
1685 print "Building $filename\n";
1686 print OUTPUT "/* Unicode Vertical Orientation */\n";
1687 print OUTPUT "/* generated from $UNIDATA:VerticalOrientation.txt */\n";
1688 print OUTPUT "/* DO NOT EDIT!! */\n\n";
1689 print OUTPUT "#include \"windef.h\"\n\n";
1691 dump_two_level_mapping( "vertical_orientation_table", $vertical_types{'R'}, 16, @vertical_table );
1693 close OUTPUT;
1694 save_file($filename);
1697 ################################################################
1698 # dump the digit folding tables
1699 sub dump_digit_folding($)
1701 my ($filename) = shift;
1702 open OUTPUT,">$filename.new" or die "Cannot create $filename";
1703 print "Building $filename\n";
1704 print OUTPUT "/* Unicode digit folding mappings */\n";
1705 print OUTPUT "/* generated from $UNIDATA:UnicodeData.txt */\n";
1706 print OUTPUT "/* DO NOT EDIT!! */\n\n";
1707 print OUTPUT "#include \"windef.h\"\n\n";
1709 dump_two_level_mapping( "wine_digitmap", 0, 16, @digitmap_table );
1710 close OUTPUT;
1711 save_file($filename);
1715 ################################################################
1716 # dump the case mapping tables
1717 sub dump_case_mappings($)
1719 my $filename = shift;
1720 open OUTPUT,">$filename.new" or die "Cannot create $filename";
1721 print "Building $filename\n";
1722 print OUTPUT "/* Unicode case mappings */\n";
1723 print OUTPUT "/* generated from $UNIDATA:UnicodeData.txt */\n";
1724 print OUTPUT "/* DO NOT EDIT!! */\n\n";
1725 print OUTPUT "#include \"windef.h\"\n\n";
1727 dump_case_table( "wine_casemap_lower", @tolower_table );
1728 print OUTPUT "\n";
1729 dump_case_table( "wine_casemap_upper", @toupper_table );
1730 close OUTPUT;
1731 save_file($filename);
1735 ################################################################
1736 # dump a case mapping table
1737 sub dump_case_table($@)
1739 my ($name,@table) = @_;
1741 # count the number of sub tables that contain something
1742 # also compute the low and upper populated bounds
1744 my @lowerbounds = ( 0, 0 );
1745 my @upperbounds = ( 0, 255 );
1746 my $index = 0;
1747 my @filled = ();
1748 for (my $i = 0; $i < 65536; $i++)
1750 next unless defined $table[$i];
1751 if (!defined $filled[$i >> 8])
1753 $lowerbounds[$index] = $i & 0xff;
1754 $upperbounds[$index] = 0xff - $lowerbounds[$index];
1755 $filled[$i >> 8] = $index * 256 + 512;
1756 $index++;
1758 else
1760 $upperbounds[$index-1] = 0xff - ($i & 0xff);
1762 $table[$i] = ($table[$i] - $i) & 0xffff;
1765 # Collapse blocks upwards if possible
1766 my $removed = 0;
1767 $index = 0;
1768 for (my $i = 0; $i < 256; $i++)
1770 next unless defined $filled[$i];
1771 if ($upperbounds[$index - 1] > $lowerbounds[$index])
1773 $removed = $removed + $lowerbounds[$index];
1775 else
1777 $removed = $removed + $upperbounds[$index - 1];
1778 $lowerbounds[$index] = $upperbounds[$index - 1];
1780 $filled[$i] = $filled[$i] - $removed;
1781 $index++;
1784 # dump the table
1786 printf OUTPUT "const WCHAR %s[%d] =\n", $name, $index * 256 + 512 - $removed;
1787 printf OUTPUT "{\n /* index */\n";
1788 printf OUTPUT "%s,\n", dump_array( 16, 256, @filled );
1789 printf OUTPUT " /* defaults */\n";
1790 printf OUTPUT "%s", dump_array( 16, 0, (0) x 256 );
1791 $index = 0;
1792 for (my $i = 0; $i < 256; $i++)
1794 next unless $filled[$i];
1795 printf OUTPUT ",\n /* 0x%02x%02x .. 0x%02xff */\n", $i, $lowerbounds[$index], $i;
1796 printf OUTPUT "%s", dump_array( 16, 0, @table[($i<<8) + $lowerbounds[$index] .. ($i<<8)+255] );
1797 $index++;
1799 printf OUTPUT "\n};\n";
1802 ################################################################
1803 # compress a mapping table by removing identical rows
1804 sub compress_array($$@)
1806 my $rows = shift;
1807 my $def = shift;
1808 my @table = @_;
1809 my $len = @table / $rows;
1810 my @array;
1811 my $data = "";
1813 # try to merge table rows
1814 for (my $row = 0; $row < $rows; $row++)
1816 my $rowtxt = pack "U*", map { defined($_) ? $_ : $def; } @table[($row * $len)..(($row + 1) * $len - 1)];
1817 my $pos = index $data, $rowtxt;
1818 if ($pos == -1)
1820 # check if the tail of the data can match the start of the new row
1821 my $first = substr( $rowtxt, 0, 1 );
1822 for (my $i = length($data) - 1; $i > 0; $i--)
1824 $pos = index( substr( $data, -$i ), $first );
1825 last if $pos == -1;
1826 $i -= $pos;
1827 next unless substr( $data, -$i ) eq substr( $rowtxt, 0, $i );
1828 substr( $data, -$i ) = "";
1829 last;
1831 $pos = length $data;
1832 $data .= $rowtxt;
1834 $array[$row] = $rows + $pos;
1836 return @array, unpack "U*", $data;
1839 ################################################################
1840 # dump a simple char -> 16-bit value mapping table
1841 sub dump_simple_mapping($$@)
1843 my $name = shift;
1844 my $def = shift;
1845 my @array = compress_array( 256, $def, @_[0..65535] );
1847 printf OUTPUT "const unsigned short %s[%d] =\n{\n", $name, $#array+1;
1848 printf OUTPUT " /* offsets */\n%s,\n", dump_array( 16, 0, @array[0..255] );
1849 printf OUTPUT " /* values */\n%s\n};\n", dump_array( 16, 0, @array[256..$#array] );
1852 ################################################################
1853 # dump a char -> 16-bit value mapping table using two-level tables
1854 sub dump_two_level_mapping($$@)
1856 my $name = shift;
1857 my $def = shift;
1858 my $size = shift;
1859 my $type = $size == 16 ? "unsigned short" : "unsigned int";
1860 my @row_array = compress_array( 4096, $def, @_[0..65535] );
1861 my @array = compress_array( 256, 0, @row_array[0..4095] );
1863 for (my $i = 256; $i < @array; $i++) { $array[$i] += @array - 4096; }
1865 printf OUTPUT "const %s DECLSPEC_HIDDEN %s[%d] =\n{\n", $type, $name, @array + @row_array - 4096;
1866 printf OUTPUT " /* level 1 offsets */\n%s,\n", dump_array( $size, 0, @array[0..255] );
1867 printf OUTPUT " /* level 2 offsets */\n%s,\n", dump_array( $size, 0, @array[256..$#array] );
1868 printf OUTPUT " /* values */\n%s\n};\n", dump_array( $size, 0, @row_array[4096..$#row_array] );
1871 ################################################################
1872 # dump a binary case mapping table in l_intl.nls format
1873 sub dump_binary_case_table(@)
1875 my (@table) = @_;
1876 my $max_char = 0x10000;
1877 my $level1 = $max_char / 16;
1878 my $level2 = $level1 / 16;
1880 my @difftable;
1881 for (my $i = 0; $i < @table; $i++)
1883 next unless defined $table[$i];
1884 $difftable[$i] = ($table[$i] - $i) & 0xffff;
1887 my @row_array = compress_array( $level1, 0, @difftable[0..$max_char-1] );
1888 my @array = compress_array( $level2, 0, @row_array[0..$level1-1] );
1889 for (my $i = $level2; $i < @array; $i++) { $array[$i] += @array - $level1; }
1890 return @array, @row_array[$level1..$#row_array];
1894 ################################################################
1895 # dump case mappings for l_intl.nls
1896 sub dump_intl_nls($)
1898 my @upper = dump_binary_case_table( @toupper_table );
1899 my @lower = dump_binary_case_table( @tolower_table );
1901 my $filename = shift;
1902 open OUTPUT,">$filename.new" or die "Cannot create $filename";
1903 printf "Building $filename\n";
1905 binmode OUTPUT;
1906 print OUTPUT pack "S<", 1; # version
1907 print OUTPUT pack "S<*", 1 + scalar @upper, @upper;
1908 print OUTPUT pack "S<*", 1 + scalar @lower, @lower;
1909 close OUTPUT;
1910 save_file($filename);
1914 sub load_nameprep_range_table($$$)
1916 my ($INPUT, $val, $table_ref) = @_;
1918 while (<$INPUT>)
1920 if (/^\s*([0-9a-fA-F]+)-([0-9a-fA-F]+)/)
1922 my $last = hex $2;
1923 $last = 65535 if($last >= 65536);
1924 foreach my $i (hex $1 .. $last)
1926 $table_ref->[$i] |= $val;
1928 next;
1930 elsif (/^\s*([0-9a-fA-F]+)/)
1932 if (hex $1 < 65536)
1934 $table_ref->[hex $1] |= $val;
1936 next;
1939 return if (/End\sTable/);
1943 sub load_nameprep_map_table($$)
1945 my ($INPUT, $table_ref) = @_;
1947 while (<$INPUT>)
1949 if (/^\s*([0-9a-fA-F]+);\s;/)
1951 # special value for map to nothing
1952 $table_ref->[hex $1] = [0xffff, 0xffff, 0xffff];
1953 next;
1955 elsif (/^\s*([0-9a-fA-F]+);\s([0-9a-fA-F]+);/)
1957 $table_ref->[hex $1] = [hex $2, 0, 0];
1958 next;
1960 elsif (/^\s*([0-9a-fA-F]+);\s([0-9a-fA-F]+)\s([0-9a-fA-F]+);/)
1962 $table_ref->[hex $1] = [hex $2, hex $3, 0];
1963 next;
1965 elsif (/^\s*([0-9a-fA-F]+);\s([0-9a-fA-F]+)\s([0-9a-fA-F]+)\s([0-9a-fA-F]+);/)
1967 $table_ref->[hex $1] = [hex $2, hex $3, hex $4];
1968 next;
1971 return if (/End\sTable/);
1975 ################################################################
1976 # dump mapping table, prohibited characters set, unassigned
1977 # characters, bidirectional rules used by nameprep algorithm
1978 sub dump_nameprep($)
1980 my $filename = shift;
1981 my @mapping_table = ();
1982 my @flags_table;
1984 my $INPUT = open_data_file( $RFCS, $STRINGPREP );
1985 while (<$INPUT>)
1987 next unless /Start\sTable/;
1989 load_nameprep_range_table($INPUT, $nameprep_flags{"unassigned"}, \@flags_table) if (/A.1/);
1990 load_nameprep_range_table($INPUT, $nameprep_flags{"prohibited"}, \@flags_table) if (/C.1.2/);
1991 load_nameprep_range_table($INPUT, $nameprep_flags{"prohibited"}, \@flags_table) if (/C.2.2/);
1992 load_nameprep_range_table($INPUT, $nameprep_flags{"prohibited"}, \@flags_table) if (/C.3/);
1993 load_nameprep_range_table($INPUT, $nameprep_flags{"prohibited"}, \@flags_table) if (/C.4/);
1994 load_nameprep_range_table($INPUT, $nameprep_flags{"prohibited"}, \@flags_table) if (/C.5/);
1995 load_nameprep_range_table($INPUT, $nameprep_flags{"prohibited"}, \@flags_table) if (/C.6/);
1996 load_nameprep_range_table($INPUT, $nameprep_flags{"prohibited"}, \@flags_table) if (/C.7/);
1997 load_nameprep_range_table($INPUT, $nameprep_flags{"prohibited"}, \@flags_table) if (/C.8/);
1998 load_nameprep_range_table($INPUT, $nameprep_flags{"prohibited"}, \@flags_table) if (/C.9/);
1999 load_nameprep_range_table($INPUT, $nameprep_flags{"bidi_ral"}, \@flags_table) if (/D.1/);
2000 load_nameprep_range_table($INPUT, $nameprep_flags{"bidi_l"}, \@flags_table) if (/D.2/);
2002 load_nameprep_map_table($INPUT, \@mapping_table) if (/B.1/);
2003 load_nameprep_map_table($INPUT, \@mapping_table) if (/B.2/);
2005 close $INPUT;
2007 open OUTPUT,">$filename.new" or die "Cannot create $filename";
2008 print "Building $filename\n";
2009 print OUTPUT "/* Nameprep algorithm related data */\n";
2010 print OUTPUT "/* generated from $RFCS/$STRINGPREP */\n";
2011 print OUTPUT "/* DO NOT EDIT!! */\n\n";
2012 print OUTPUT "#include \"windef.h\"\n\n";
2014 dump_two_level_mapping( "nameprep_char_type", 0, 16, @flags_table );
2016 ######### mapping table
2017 # first determine all the 16-char subsets that contain something
2018 my @filled = ();
2019 my $pos = 16*3; # for the null subset
2020 for (my $i = 0; $i < 65536; $i++)
2022 next unless defined $mapping_table[$i];
2023 $filled[$i >> 4] = $pos;
2024 $pos += 16*3;
2025 $i |= 15;
2027 my $total = $pos;
2029 # now count the 256-char subsets that contain something
2030 my @filled_idx = (256) x 256;
2031 $pos = 256 + 16;
2032 for (my $i = 0; $i < 4096; $i++)
2034 next unless $filled[$i];
2035 $filled_idx[$i >> 4] = $pos;
2036 $pos += 16;
2037 $i |= 15;
2039 my $null_offset = $pos;
2040 $total += $pos;
2042 # add the index offsets to the subsets positions
2043 for (my $i = 0; $i < 4096; $i++)
2045 next unless $filled[$i];
2046 $filled[$i] += $null_offset;
2049 # dump the main index
2050 printf OUTPUT "const unsigned short DECLSPEC_HIDDEN nameprep_mapping[%d] =\n", $total;
2051 printf OUTPUT "{\n /* index */\n";
2052 printf OUTPUT "%s", dump_array( 16, 0, @filled_idx );
2053 printf OUTPUT ",\n /* null sub-index */\n%s", dump_array( 16, 0, ($null_offset) x 16 );
2055 # dump the second-level indexes
2056 for (my $i = 0; $i < 256; $i++)
2058 next unless ($filled_idx[$i] > 256);
2059 my @table = @filled[($i<<4)..($i<<4)+15];
2060 for (my $j = 0; $j < 16; $j++) { $table[$j] ||= $null_offset; }
2061 printf OUTPUT ",\n /* sub-index %02x */\n", $i;
2062 printf OUTPUT "%s", dump_array( 16, 0, @table );
2065 # dump the 16-char subsets
2066 printf OUTPUT ",\n /* null mapping */\n";
2067 printf OUTPUT "%s", dump_array( 16, 0, (0) x 48 );
2069 for (my $i = 0; $i < 4096; $i++)
2071 next unless $filled[$i];
2072 my @table = (0) x 48;
2073 for (my $j = 0; $j < 16; $j++)
2075 if (defined $mapping_table[($i<<4) + $j])
2077 $table[3 * $j] = ${$mapping_table[($i << 4) + $j]}[0];
2078 $table[3 * $j + 1] = ${$mapping_table[($i << 4) + $j]}[1];
2079 $table[3 * $j + 2] = ${$mapping_table[($i << 4) + $j]}[2];
2082 printf OUTPUT ",\n /* 0x%03x0 .. 0x%03xf */\n", $i, $i;
2083 printf OUTPUT "%s", dump_array( 16, 0, @table );
2086 printf OUTPUT "\n};\n";
2088 close OUTPUT;
2089 save_file($filename);
2093 ################################################################
2094 # dump the GetStringTypeW table
2095 sub dump_string_type_table($)
2097 my $filename = shift;
2098 open OUTPUT,">$filename.new" or die "Cannot create $filename";
2099 printf "Building $filename\n";
2100 printf OUTPUT "/* Unicode wctype table */\n";
2101 printf OUTPUT "/* Automatically generated; DO NOT EDIT!! */\n\n";
2102 printf OUTPUT "#include \"windef.h\"\n\n";
2104 my @table = @category_table;
2106 # add the direction in the high 4 bits of the category
2107 for (my $i = 0; $i < 65536; $i++)
2109 $table[$i] |= $c2_types{$direction_table[$i]} << 12 if defined $direction_table[$i];
2112 dump_two_level_mapping( "wctype_table", 0, 16, @table );
2114 close OUTPUT;
2115 save_file($filename);
2119 ################################################################
2120 # dump the bidi direction table
2121 sub dump_bidi_dir_table($)
2123 my $filename = shift;
2124 open OUTPUT,">$filename.new" or die "Cannot create $filename";
2125 printf "Building $filename\n";
2126 printf OUTPUT "/* Unicode BiDi direction table */\n";
2127 printf OUTPUT "/* Automatically generated; DO NOT EDIT!! */\n\n";
2128 printf OUTPUT "#include \"windef.h\"\n\n";
2130 my @table;
2132 for (my $i = 0; $i < 65536; $i++)
2134 $table[$i] = $bidi_types{$direction_table[$i]} if defined $direction_table[$i];
2137 dump_two_level_mapping( "bidi_direction_table", $bidi_types{"L"}, 16, @table );
2139 close OUTPUT;
2140 save_file($filename);
2144 ################################################################
2145 # dump the ctype tables
2146 sub dump_ctype_tables($)
2148 my $filename = shift;
2149 open OUTPUT,">$filename.new" or die "Cannot create $filename";
2150 printf "Building $filename\n";
2151 printf OUTPUT "/* Unicode ctype tables */\n";
2152 printf OUTPUT "/* Automatically generated; DO NOT EDIT!! */\n\n";
2153 printf OUTPUT "#include \"windef.h\"\n\n";
2155 my @table = @category_table;
2157 # add the direction in the high 4 bits of the category
2158 for (my $i = 0; $i < 65536; $i++)
2160 $table[$i] |= $directions{$direction_table[$i]} << 12 if defined $direction_table[$i];
2163 dump_simple_mapping( "wine_wctype_table", 0, @table );
2165 close OUTPUT;
2166 save_file($filename);
2170 ################################################################
2171 # dump the char composition table
2172 sub dump_compose_table($)
2174 my $filename = shift;
2176 open OUTPUT,">$filename.new" or die "Cannot create $filename";
2177 print "Building $filename\n";
2178 print OUTPUT "/* Unicode char composition */\n";
2179 print OUTPUT "/* generated from $UNIDATA:UnicodeData.txt */\n";
2180 print OUTPUT "/* DO NOT EDIT!! */\n\n";
2181 print OUTPUT "#include \"windef.h\"\n\n";
2183 my @filled = ();
2184 foreach my $i (@compose_table)
2186 my @comp = @$i;
2187 push @{$filled[$comp[1]]}, [ $comp[0], $comp[2] ];
2190 # count how many different second chars we have
2192 my $count = 0;
2193 for (my $i = 0; $i < 65536; $i++)
2195 next unless defined $filled[$i];
2196 $count++;
2199 # build the table of second chars and offsets
2201 my $pos = $count + 1;
2202 my @table = ();
2203 for (my $i = 0; $i < 65536; $i++)
2205 next unless defined $filled[$i];
2206 push @table, $i, $pos;
2207 $pos += @{$filled[$i]};
2209 # terminator with last position
2210 push @table, 0, $pos;
2211 printf OUTPUT "static const WCHAR table[0x%x] =\n{\n", 2*$pos;
2212 printf OUTPUT " /* second chars + offsets */\n%s", dump_array( 16, 0, @table );
2214 # build the table of first chars and mappings
2216 for (my $i = 0; $i < 65536; $i++)
2218 next unless defined $filled[$i];
2219 my @table = ();
2220 my @list = sort { $a->[0] <=> $b->[0] } @{$filled[$i]};
2221 for (my $j = 0; $j <= $#list; $j++)
2223 push @table, $list[$j][0], $list[$j][1];
2225 printf OUTPUT ",\n /* 0x%04x */\n%s", $i, dump_array( 16, 0, @table );
2227 print OUTPUT "\n};\n\n";
2228 print OUTPUT <<"EOF";
2229 static inline int binary_search( WCHAR ch, int low, int high )
2231 while (low <= high)
2233 int pos = (low + high) / 2;
2234 if (table[2 * pos] < ch) low = pos + 1;
2235 else if (table[2 * pos] > ch) high = pos - 1;
2236 else return pos;
2238 return -1;
2241 WCHAR DECLSPEC_HIDDEN wine_compose( const WCHAR *str )
2243 int pos, idx = 1, start = 0, count = $count;
2244 for (;;)
2246 if ((pos = binary_search( str[idx], start, count - 1 )) == -1) return 0;
2247 if (!idx--) return table[2 * pos + 1];
2248 start = table[2 * pos + 1];
2249 count = table[2 * pos + 3];
2253 close OUTPUT;
2254 save_file($filename);
2257 ################################################################
2258 # dump a decomposition table
2259 sub dump_decompositions($@)
2261 my ($name, @decomp) = @_;
2263 # first determine all the 16-char subsets that contain something
2265 my @filled = (0) x 4096;
2266 my $pos = 16; # for the null subset
2267 my $data_total = 0;
2268 for (my $i = 0; $i < 65536; $i++)
2270 next unless defined $decomp[$i];
2271 if ($filled[$i >> 4] == 0)
2273 $filled[$i >> 4] = $pos;
2274 $pos += 16;
2276 $data_total += @{$decomp[$i]};
2278 my $total = $pos;
2280 # now count the 256-char subsets that contain something
2282 my @filled_idx = (256) x 256;
2283 $pos = 256 + 16;
2284 for (my $i = 0; $i < 4096; $i++)
2286 next unless $filled[$i];
2287 $filled_idx[$i >> 4] = $pos;
2288 $pos += 16;
2289 $i |= 15;
2291 my $null_offset = $pos; # null mapping
2292 $total += $pos + 1; # add the offset sentinel
2294 # add the index offsets to the subsets positions
2296 for (my $i = 0; $i < 4096; $i++)
2298 next unless $filled[$i];
2299 $filled[$i] += $null_offset;
2302 # dump the main index
2304 printf OUTPUT "\nconst WCHAR DECLSPEC_HIDDEN %s[%d] =\n", $name, $total + $data_total;
2305 printf OUTPUT "{\n /* index */\n";
2306 printf OUTPUT "%s", dump_array( 16, 0, @filled_idx );
2307 printf OUTPUT ",\n /* null sub-index */\n%s", dump_array( 16, 0, ($null_offset) x 16 );
2309 # dump the second-level indexes
2311 for (my $i = 0; $i < 256; $i++)
2313 next unless ($filled_idx[$i] > 256);
2314 my @table = @filled[($i<<4)..($i<<4)+15];
2315 for (my $j = 0; $j < 16; $j++) { $table[$j] ||= $null_offset; }
2316 printf OUTPUT ",\n /* sub-index %02x */\n", $i;
2317 printf OUTPUT "%s", dump_array( 16, 0, @table );
2320 # dump the 16-char offsets
2322 printf OUTPUT ",\n /* null offsets */\n";
2323 printf OUTPUT "%s", dump_array( 16, 0, ($total) x (16) );
2325 $pos = $total;
2327 my @data;
2328 for (my $i = 0; $i < 4096; $i++)
2330 next unless $filled[$i];
2331 my @table = (0) x (16);
2332 for (my $j = 0; $j < 16; $j++)
2334 $table[$j] = $pos;
2335 if (defined $decomp[($i<<4) + $j])
2337 $pos += $#{$decomp[($i<<4) + $j]} + 1;
2338 push @data, @{$decomp[($i<<4) + $j]};
2341 printf OUTPUT ",\n /* offsets 0x%03x0 .. 0x%03xf */\n", $i, $i;
2342 printf OUTPUT "%s", dump_array( 16, 0, @table );
2345 my @sentinel = $pos;
2346 printf OUTPUT ",\n /* offset sentinel */\n";
2347 printf OUTPUT "%s", dump_array( 16, 0, @sentinel );
2349 printf OUTPUT ",\n /* data */\n";
2350 printf OUTPUT "%s", dump_array( 16, 0, @data );
2352 printf OUTPUT "\n};\n";
2355 ################################################################
2356 # dump the char decomposition table
2357 sub dump_decompose_table($$)
2359 my ($filename, $compat) = @_;
2361 open OUTPUT,">$filename.new" or die "Cannot create $filename";
2362 print "Building $filename\n";
2363 print OUTPUT "/* Unicode char composition */\n";
2364 print OUTPUT "/* generated from $UNIDATA:UnicodeData.txt */\n";
2365 print OUTPUT "/* DO NOT EDIT!! */\n\n";
2366 print OUTPUT "#include \"windef.h\"\n";
2368 dump_decompositions( "nfd_table", build_decompositions( @decomp_table ));
2369 dump_decompositions( "nfkd_table", build_decompositions( @decomp_compat_table )) if $compat;
2371 close OUTPUT;
2372 save_file($filename);
2375 ################################################################
2376 # dump the combining class table
2377 sub dump_combining_class($)
2379 my $filename = shift;
2381 open OUTPUT,">$filename.new" or die "Cannot create $filename";
2382 print "Building $filename\n";
2383 print OUTPUT "/* Unicode Combining Classes */\n";
2384 print OUTPUT "/* generated from $UNIDATA:UnicodeData.txt */\n";
2385 print OUTPUT "/* DO NOT EDIT!! */\n\n";
2386 print OUTPUT "#include \"windef.h\"\n\n";
2388 dump_two_level_mapping( "combining_class_table", 0, 16, @combining_class_table );
2389 close OUTPUT;
2390 save_file($filename);
2393 ################################################################
2394 # output a codepage definition file from the global tables
2395 sub output_codepage_file($$$)
2397 my ($codepage, $filename, $comment) = @_;
2399 my $output = sprintf "libs/port/c_%03d.c", $codepage;
2400 open OUTPUT,">$output.new" or die "Cannot create $output";
2402 printf "Building %s (%s)\n", $output, $comment;
2404 # dump all tables
2406 printf OUTPUT "/* code page %03d (%s) */\n", $codepage, $comment;
2407 print OUTPUT "/* generated from $filename */\n";
2408 print OUTPUT "/* DO NOT EDIT!! */\n\n";
2409 printf OUTPUT "#include \"wine/unicode.h\"\n\n";
2411 if (!@lead_bytes) { dump_sbcs_table( $codepage, $comment, $default_char, $default_wchar ); }
2412 else { dump_dbcs_table( $codepage, $comment, $default_char, $default_wchar, get_lb_ranges() ); }
2413 close OUTPUT;
2414 save_file($output);
2416 # same thing in binary format
2418 $output = sprintf "nls/c_%03d.nls", $codepage;
2419 open OUTPUT,">$output.new" or die "Cannot create $output";
2421 printf "Building %s\n", $output;
2422 if (!@lead_bytes) { dump_binary_sbcs_table( $codepage ); }
2423 else { dump_binary_dbcs_table( $codepage ); }
2425 close OUTPUT;
2426 save_file($output);
2429 ################################################################
2430 # output a codepage table from a Microsoft-style mapping file
2431 sub output_msdata_codepage($$)
2433 my ($filename, $comment) = @_;
2434 my $state = "";
2435 my ($codepage, $width, $count);
2436 my ($lb_cur, $lb_end);
2438 my $INPUT = open_data_file( $MSCODEPAGES, $filename ) or die "Cannot open $filename";
2440 while (<$INPUT>)
2442 next if /^;/; # skip comments
2443 next if /^\s*$/; # skip empty lines
2444 next if /\x1a/; # skip ^Z
2445 last if /^ENDCODEPAGE/;
2447 if (/^CODEPAGE\s+(\d+)/)
2449 $codepage = $1;
2450 next;
2452 if (/^CPINFO\s+(\d+)\s+0x([0-9a-fA-f]+)\s+0x([0-9a-fA-F]+)/)
2454 $width = $1;
2455 $default_char = hex $2;
2456 $default_wchar = hex $3;
2457 next;
2459 if (/^(MBTABLE|GLYPHTABLE|WCTABLE|DBCSRANGE|DBCSTABLE)\s+(\d+)/)
2461 $state = $1;
2462 $count = $2;
2463 next;
2465 if (/^0x([0-9a-fA-F]+)\s+0x([0-9a-fA-F]+)/)
2467 if ($state eq "MBTABLE")
2469 my $cp = hex $1;
2470 my $uni = hex $2;
2471 $cp2uni[$cp] = $uni unless defined($cp2uni[$cp]);
2472 next;
2474 if ($state eq "GLYPHTABLE")
2476 my $cp = hex $1;
2477 my $uni = hex $2;
2478 $glyph2uni[$cp] = $uni unless defined($glyph2uni[$cp]);
2479 next;
2481 if ($state eq "WCTABLE")
2483 my $uni = hex $1;
2484 my $cp = hex $2;
2485 $uni2cp[$uni] = $cp unless defined($uni2cp[$uni]);
2486 next;
2488 if ($state eq "DBCSRANGE")
2490 my $start = hex $1;
2491 my $end = hex $2;
2492 for (my $i = $start; $i <= $end; $i++) { add_lead_byte( $i ); }
2493 $lb_cur = $start;
2494 $lb_end = $end;
2495 next;
2497 if ($state eq "DBCSTABLE")
2499 my $mb = hex $1;
2500 my $uni = hex $2;
2501 my $cp = ($lb_cur << 8) | $mb;
2502 $cp2uni[$cp] = $uni unless defined($cp2uni[$cp]);
2503 if (!--$count)
2505 if (++$lb_cur > $lb_end) { $state = "DBCSRANGE"; }
2507 next;
2510 die "$filename: Unrecognized line $_\n";
2512 close $INPUT;
2514 output_codepage_file( $codepage, "$MSCODEPAGES:$filename", $comment );
2518 ################################################################
2519 # read an input file and generate the corresponding .c file
2520 sub handle_codepage_file(@)
2522 my ($codepage, $filename, $comment) = @_;
2524 @cp2uni = ();
2525 @glyph2uni = ();
2526 @lead_bytes = ();
2527 @uni2cp = ();
2528 $default_char = $DEF_CHAR;
2529 $default_wchar = $DEF_CHAR;
2531 # some codepage files are special
2532 if ($codepage == 20932)
2534 output_eucjp_codepage( $filename, $comment );
2536 else
2538 output_msdata_codepage( $filename, $comment );
2543 ################################################################
2544 # save a file if modified
2545 sub save_file($)
2547 my $file = shift;
2548 if (-f $file && !system "cmp $file $file.new >/dev/null")
2550 unlink "$file.new";
2552 else
2554 rename "$file.new", "$file";
2559 ################################################################
2560 # replace the contents of a file between ### cpmap ### marks
2562 sub REPLACE_IN_FILE($@)
2564 my $name = shift;
2565 my @data = @_;
2566 my @lines = ();
2567 open(FILE,$name) or die "Can't open $name";
2568 while (<FILE>)
2570 push @lines, $_;
2571 last if /\#\#\# cpmap begin \#\#\#/;
2573 push @lines, @data;
2574 while (<FILE>)
2576 if (/\#\#\# cpmap end \#\#\#/) { push @lines, "\n", $_; last; }
2578 push @lines, <FILE>;
2579 open(FILE,">$name.new") or die "Can't modify $name";
2580 print FILE @lines;
2581 close(FILE);
2582 save_file($name);
2585 ################################################################
2586 # main routine
2588 chdir ".." if -f "./make_unicode";
2589 load_data();
2590 dump_case_mappings( "libs/port/casemap.c" );
2591 dump_sortkeys( "dlls/kernelbase/collation.c" );
2592 dump_compose_table( "libs/port/compose.c" );
2593 dump_compose_table( "dlls/kernelbase/compose.c" );
2594 dump_decompose_table( "libs/port/decompose.c", 1 );
2595 dump_decompose_table( "dlls/kernelbase/decompose.c", 0 );
2596 dump_ctype_tables( "libs/port/wctype.c" );
2597 dump_bidi_dir_table( "dlls/gdi32/direction.c" );
2598 dump_bidi_dir_table( "dlls/usp10/direction.c" );
2599 dump_bidi_dir_table( "dlls/dwrite/direction.c" );
2600 dump_string_type_table( "dlls/kernelbase/wctype.c" );
2601 dump_digit_folding( "dlls/kernelbase/digitmap.c" );
2602 dump_combining_class( "dlls/ntdll/combclass.c" );
2603 dump_mirroring( "dlls/usp10/mirror.c" );
2604 dump_mirroring( "dlls/dwrite/mirror.c" );
2605 dump_bracket( "dlls/usp10/bracket.c" );
2606 dump_bracket( "dlls/dwrite/bracket.c" );
2607 dump_shaping( "dlls/usp10/shaping.c" );
2608 dump_linebreak( "dlls/usp10/linebreak.c" );
2609 dump_linebreak( "dlls/dwrite/linebreak.c" );
2610 dump_scripts( "dlls/dwrite/scripts" );
2611 dump_indic( "dlls/usp10/indicsyllable.c" );
2612 dump_vertical( "dlls/gdi32/vertical.c" );
2613 dump_vertical( "dlls/wineps.drv/vertical.c" );
2614 dump_nameprep( "dlls/kernel32/nameprep.c" );
2615 dump_intl_nls("nls/l_intl.nls");
2617 foreach my $file (@allfiles) { handle_codepage_file( @{$file} ); }
2619 exit 0;
2621 # Local Variables:
2622 # compile-command: "./make_unicode"
2623 # End: