msxml3/saxreader: Fix attributes array growing size.
[wine.git] / tools / make_unicode
blobc8365cd9212c8cea6384d0225429499777b24e3e
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/7.0.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 %bracket_types =
136 "o" => 0x0000,
137 "c" => 0x0001,
140 my %indic_types =
142 "Other" => 0x0000,
143 "Bindu" => 0x0001,
144 "Visarga" => 0x0002,
145 "Avagraha" => 0x0003,
146 "Nukta" => 0x0004,
147 "Virama" => 0x0005,
148 "Vowel_Independent" => 0x0006,
149 "Vowel_Dependent" => 0x0007,
150 "Vowel" => 0x0008,
151 "Consonant_Placeholder" => 0x0009,
152 "Consonant" => 0x000a,
153 "Consonant_Dead" => 0x000b,
154 "Consonant_Succeeding_Repha" => 0x000c,
155 "Consonant_Subjoined" => 0x000d,
156 "Consonant_Medial" => 0x000e,
157 "Consonant_Final" => 0x000f,
158 "Consonant_Head_Letter" => 0x0010,
159 "Modifying_Letter" => 0x0011,
160 "Tone_Letter" => 0x0012,
161 "Tone_Mark" => 0x0013,
162 "Register_Shifter" => 0x0014,
163 "Consonant_Preceding_Repha" => 0x0015,
164 "Pure_Killer" => 0x0016,
165 "Invisible_Stacker" => 0x0017,
166 "Gemination_Mark" => 0x0018,
167 "Cantillation_Mark" => 0x0019,
168 "Non_Joiner" => 0x001a,
169 "Joiner" => 0x001b,
170 "Number_Joiner" => 0x001c,
171 "Number" => 0x001d,
172 "Brahmi_Joining_Number" => 0x001e
175 my %matra_types =
177 "Right" => 0x01,
178 "Left" => 0x02,
179 "Visual_Order_Left" => 0x03,
180 "Left_And_Right" => 0x04,
181 "Top" => 0x05,
182 "Bottom" => 0x06,
183 "Top_And_Bottom" => 0x07,
184 "Top_And_Right" => 0x08,
185 "Top_And_Left" => 0x09,
186 "Top_And_Left_And_Right" => 0x0a,
187 "Bottom_And_Right" => 0x0b,
188 "Top_And_Bottom_And_Right" => 0x0c,
189 "Overstruck" => 0x0d,
190 "Invisible" => 0x0e
193 my %nameprep_flags =
195 "unassigned" => 0x01,
196 "prohibited" => 0x02,
197 "bidi_ral" => 0x04,
198 "bidi_l" => 0x08
201 my %break_types =
203 "BK" => 0x0001,
204 "CR" => 0x0002,
205 "LF" => 0x0003,
206 "CM" => 0x0004,
207 "SG" => 0x0005,
208 "GL" => 0x0006,
209 "CB" => 0x0007,
210 "SP" => 0x0008,
211 "ZW" => 0x0009,
212 "NL" => 0x000a,
213 "WJ" => 0x000b,
214 "JL" => 0x000c,
215 "JV" => 0x000d,
216 "JT" => 0x000e,
217 "H2" => 0x000f,
218 "H3" => 0x0010,
219 "XX" => 0x0011,
220 "OP" => 0x0012,
221 "CL" => 0x0013,
222 "CP" => 0x0014,
223 "QU" => 0x0015,
224 "NS" => 0x0016,
225 "EX" => 0x0017,
226 "SY" => 0x0018,
227 "IS" => 0x0019,
228 "PR" => 0x001a,
229 "PO" => 0x001b,
230 "NU" => 0x001c,
231 "AL" => 0x001d,
232 "ID" => 0x001e,
233 "IN" => 0x001f,
234 "HY" => 0x0020,
235 "BB" => 0x0021,
236 "BA" => 0x0022,
237 "SA" => 0x0023,
238 "AI" => 0x0024,
239 "B2" => 0x0025,
240 "HL" => 0x0026,
241 "CJ" => 0x0027,
242 "RI" => 0x0028,
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 %joining_types =
333 "U" => 0, # Non_Joining
334 "T" => 1, # Transparent
335 "R" => 2, # Right_Joining
336 "L" => 3, # Left_Joining
337 "D" => 4, # Dual_Joining
338 "C" => 5, # Join_Causing
341 my @cp2uni = ();
342 my @lead_bytes = ();
343 my @uni2cp = ();
344 my @unicode_defaults = ();
345 my @unicode_aliases = ();
346 my @tolower_table = ();
347 my @toupper_table = ();
348 my @digitmap_table = ();
349 my @compatmap_table = ();
350 my @category_table = (0) x 65536;
351 my @joining_table = (0) x 65536;
352 my @direction_table = ();
353 my @decomp_table = ();
354 my @compose_table = ();
355 my $default_char;
356 my $default_wchar;
358 my %joining_forms =
360 "isolated" => [],
361 "final" => [],
362 "initial" => [],
363 "medial" => []
366 ################################################################
367 # fetch a unicode.org file and open it
368 sub open_data_file($$)
370 my ($base, $name) = @_;
371 (my $dir = "data/$name") =~ s/\/[^\/]+$//;
372 local *FILE;
373 unless (-f "data/$name")
375 print "Fetching $base/$name...\n";
376 system "mkdir", "-p", $dir;
377 !system "wget", "-q", "-O", "data/$name", "$base/$name" or die "cannot fetch $base/$name";
379 open FILE, "<data/$name" or die "cannot open data/$name";
380 return *FILE;
383 ################################################################
384 # read in the defaults file
385 sub READ_DEFAULTS($)
387 my $filename = shift;
388 my $start;
390 # first setup a few default mappings
392 open DEFAULTS, "$filename" or die "Cannot open $filename";
393 print "Loading $filename\n";
394 while (<DEFAULTS>)
396 next if /^\#/; # skip comments
397 next if /^$/; # skip empty lines
398 if (/^(([0-9a-fA-F]+)(,[0-9a-fA-F]+)*)\s+([0-9a-fA-F]+|'.'|none)\s+(\#.*)?/)
400 my @src = map hex, split /,/,$1;
401 my $dst = $4;
402 my $comment = $5;
403 if ($#src > 0) { push @unicode_aliases, \@src; }
404 next if ($dst eq "none");
405 $dst = ($dst =~ /\'.\'/) ? ord substr($dst,1,1) : hex $dst;
406 foreach my $src (@src)
408 die "Duplicate value" if defined($unicode_defaults[$src]);
409 $unicode_defaults[$src] = $dst;
411 next;
413 die "Unrecognized line $_\n";
415 close DEFAULTS;
417 # now build mappings from the decomposition field of the Unicode database
419 my $UNICODE_DATA = open_data_file( $UNIDATA, "UnicodeData.txt" );
420 while (<$UNICODE_DATA>)
422 # Decode the fields ...
423 my ($code, $name, $cat, $comb, $bidi,
424 $decomp, $dec, $dig, $num, $mirror,
425 $oldname, $comment, $upper, $lower, $title) = split /;/;
426 my $dst;
427 my $src = hex $code;
429 die "unknown category $cat" unless defined $categories{$cat};
430 die "unknown directionality $bidi" unless defined $directions{$bidi};
432 $category_table[$src] = $categories{$cat};
433 $direction_table[$src] = $directions{$bidi};
434 $joining_table[$src] = $joining_types{"T"} if $cat eq "Mn" || $cat eq "Me" || $cat eq "Cf";
436 if ($lower ne "")
438 $tolower_table[$src] = hex $lower;
440 if ($upper ne "")
442 $toupper_table[$src] = hex $upper;
444 if ($dec ne "")
446 $category_table[$src] |= $ctype{"digit"};
448 if ($dig ne "")
450 $digitmap_table[$src] = ord $dig;
453 # copy the category and direction for everything between First/Last pairs
454 if ($name =~ /, First>/) { $start = $src; }
455 if ($name =~ /, Last>/)
457 while ($start < $src)
459 $category_table[$start] = $category_table[$src];
460 $direction_table[$start] = $direction_table[$src];
461 $start++;
465 next if $decomp eq ""; # no decomposition, skip it
467 if ($decomp =~ /^<([a-zA-Z]+)>\s+([0-9a-fA-F]+)$/)
469 # decomposition of the form "<foo> 1234" -> use char if type is known
470 if (($src >= 0xf900 && $src < 0xfb00) || ($src >= 0xfe30 && $src < 0xfffd))
472 # Single char decomposition in the compatibility range
473 $compatmap_table[$src] = hex $2;
475 if ($1 eq "isolated" || $1 eq "final" || $1 eq "initial" || $1 eq "medial")
477 ${joining_forms{$1}}[hex $2] = $src;
478 next;
480 next unless ($1 eq "font" ||
481 $1 eq "noBreak" ||
482 $1 eq "circle" ||
483 $1 eq "super" ||
484 $1 eq "sub" ||
485 $1 eq "wide" ||
486 $1 eq "narrow" ||
487 $1 eq "compat" ||
488 $1 eq "small");
489 $dst = hex $2;
491 elsif ($decomp =~ /^<compat>\s+0020\s+([0-9a-fA-F]+)/)
493 # decomposition "<compat> 0020 1234" -> combining accent
494 $dst = hex $1;
496 elsif ($decomp =~ /^([0-9a-fA-F]+)/)
498 # decomposition contains only char values without prefix -> use first char
499 $dst = hex $1;
500 $category_table[$src] |= $category_table[$dst] if defined $category_table[$dst];
501 # store decomposition if it contains two chars
502 if ($decomp =~ /^([0-9a-fA-F]+)\s+([0-9a-fA-F]+)$/)
504 $decomp_table[$src] = [ hex $1, hex $2 ];
505 push @compose_table, [ hex $1, hex $2, $src ];
507 elsif ($decomp =~ /^(<[a-z]+>\s)*([0-9a-fA-F]+)$/ &&
508 (($src >= 0xf900 && $src < 0xfb00) || ($src >= 0xfe30 && $src < 0xfffd)))
510 # Single char decomposition in the compatibility range
511 $compatmap_table[$src] = hex $2;
514 else
516 next;
519 next if defined($unicode_defaults[$src]); # may have been set in the defaults file
521 # check for loops
522 for (my $i = $dst; ; $i = $unicode_defaults[$i])
524 die sprintf("loop detected for %04x -> %04x",$src,$dst) if $i == $src;
525 last unless defined($unicode_defaults[$i]);
527 $unicode_defaults[$src] = $dst;
529 close $UNICODE_DATA;
531 # patch the category of some special characters
533 foreach my $cat (keys %special_categories)
535 my $flag = $ctype{$cat};
536 foreach my $i (@{$special_categories{$cat}}) { $category_table[$i] |= $flag; }
541 ################################################################
542 # define a new lead byte
543 sub add_lead_byte($)
545 my $ch = shift;
546 push @lead_bytes, $ch;
547 $cp2uni[$ch] = 0;
550 ################################################################
551 # define a new char mapping
552 sub add_mapping($$)
554 my ($cp, $uni) = @_;
555 $cp2uni[$cp] = $uni unless defined($cp2uni[$cp]);
556 $uni2cp[$uni] = $cp unless defined($uni2cp[$uni]);
557 if ($cp > 0xff && !defined($cp2uni[$cp >> 8]))
559 add_lead_byte( $cp >> 8 );
563 ################################################################
564 # parse a standard codepage file
565 sub read_codepage_file($)
567 my $name = shift;
568 my $INPUT = open_data_file( $MAPPINGS, $name );
570 while (<$INPUT>)
572 next if /^\#/; # skip comments
573 next if /^$/; # skip empty lines
574 next if /\x1a/; # skip ^Z
575 next if (/^0x([0-9a-fA-F]+)\s+\#UNDEFINED/); # undefined char
577 if (/^0x([0-9a-fA-F]+)\s+\#DBCS LEAD BYTE/)
579 add_lead_byte( hex $1 );
580 next;
582 # 0x12 0x3456
583 if (/^0x([0-9a-fA-F]+)\s+0x([0-9a-fA-F]+)\s+(\#.*)?/)
585 add_mapping( hex $1, hex $2 );
586 next;
588 # 0x12 0x3456+0x7890
589 if (/^0x([0-9a-fA-F]+)\s+0x([0-9a-fA-F]+)\+.*\s+(\#.*)?/)
591 add_mapping( hex $1, hex $2 );
592 next;
594 # 0x12 <LR>+0x3456
595 if (/^0x([0-9a-fA-F]+)\s+<LR>\+0x([0-9a-fA-F]+)\s+(\#.*)?/)
597 add_mapping( hex $1, hex $2 );
598 next;
600 # 0x12 <RL>+0x3456
601 if (/^0x([0-9a-fA-F]+)\s+<RL>\+0x([0-9a-fA-F]+)\s+(\#.*)?/)
603 my $uni = hex $2;
604 add_mapping( hex $1, $uni ) if $uni >= 0x80;
605 next;
607 die "$name: Unrecognized line $_\n";
609 close $INPUT;
613 ################################################################
614 # fill input data for the 20127 (us-ascii) codepage
615 sub fill_20127_codepage()
617 for (my $i = 0; $i < 128; $i++) { $cp2uni[$i] = $uni2cp[$i] = $i; }
618 for (my $i = 128; $i < 256; $i++) { $cp2uni[$i] = $i & 0x7f; }
621 ################################################################
622 # get a mapping including glyph chars for MB_USEGLYPHCHARS
624 sub get_glyphs_mapping(@)
626 $_[0x01] = 0x263a; # (WHITE SMILING FACE)
627 $_[0x02] = 0x263b; # (BLACK SMILING FACE)
628 $_[0x03] = 0x2665; # (BLACK HEART SUIT)
629 $_[0x04] = 0x2666; # (BLACK DIAMOND SUIT)
630 $_[0x05] = 0x2663; # (BLACK CLUB SUIT)
631 $_[0x06] = 0x2660; # (BLACK SPADE SUIT)
632 $_[0x07] = 0x2022; # (BULLET)
633 $_[0x08] = 0x25d8; # (INVERSE BULLET)
634 $_[0x09] = 0x25cb; # (WHITE CIRCLE)
635 $_[0x0a] = 0x25d9; # (INVERSE WHITE CIRCLE)
636 $_[0x0b] = 0x2642; # (MALE SIGN)
637 $_[0x0c] = 0x2640; # (FEMALE SIGN)
638 $_[0x0d] = 0x266a; # (EIGHTH NOTE)
639 $_[0x0e] = 0x266b; # (BEAMED EIGHTH NOTES)
640 $_[0x0f] = 0x263c; # (WHITE SUN WITH RAYS)
641 $_[0x10] = 0x25ba; # (BLACK RIGHT-POINTING POINTER)
642 $_[0x11] = 0x25c4; # (BLACK LEFT-POINTING POINTER)
643 $_[0x12] = 0x2195; # (UP DOWN ARROW)
644 $_[0x13] = 0x203c; # (DOUBLE EXCLAMATION MARK)
645 $_[0x14] = 0x00b6; # (PILCROW SIGN)
646 $_[0x15] = 0x00a7; # (SECTION SIGN)
647 $_[0x16] = 0x25ac; # (BLACK RECTANGLE)
648 $_[0x17] = 0x21a8; # (UP DOWN ARROW WITH BASE)
649 $_[0x18] = 0x2191; # (UPWARDS ARROW)
650 $_[0x19] = 0x2193; # (DOWNWARDS ARROW)
651 $_[0x1a] = 0x2192; # (RIGHTWARDS ARROW)
652 $_[0x1b] = 0x2190; # (LEFTWARDS ARROW)
653 $_[0x1c] = 0x221f; # (RIGHT ANGLE)
654 $_[0x1d] = 0x2194; # (LEFT RIGHT ARROW)
655 $_[0x1e] = 0x25b2; # (BLACK UP-POINTING TRIANGLE)
656 $_[0x1f] = 0x25bc; # (BLACK DOWN-POINTING TRIANGLE)
657 $_[0x7f] = 0x2302; # (HOUSE)
658 return @_;
661 ################################################################
662 # build EUC-JP table from the JIS 0208 file
663 # FIXME: for proper EUC-JP we should probably read JIS 0212 too
664 # but this would require 3-byte DBCS characters
665 sub READ_JIS0208_FILE($)
667 my $name = shift;
669 # ASCII chars
670 for (my $i = 0x00; $i <= 0x7f; $i++)
672 $cp2uni[$i] = $i;
673 $uni2cp[$i] = $i;
676 # JIS X 0201 right plane
677 for (my $i = 0xa1; $i <= 0xdf; $i++)
679 $cp2uni[0x8e00 + $i] = 0xfec0 + $i;
680 $uni2cp[0xfec0 + $i] = 0x8e00 + $i;
683 # lead bytes
684 foreach my $i (0x8e, 0x8f, 0xa1 .. 0xfe)
686 push @lead_bytes,$i;
687 $cp2uni[$i] = 0;
690 # undefined chars
691 foreach my $i (0x80 .. 0x8d, 0x90 .. 0xa0, 0xff)
693 $cp2uni[$i] = $DEF_CHAR;
696 # Shift-JIS compatibility
697 $uni2cp[0x00a5] = 0x5c;
698 $uni2cp[0x203e] = 0x7e;
700 # Fix backslash conversion
701 $cp2uni[0xa1c0] = 0xff3c;
702 $uni2cp[0xff3c] = 0xa1c0;
704 my $INPUT = open_data_file( $MAPPINGS, $name );
705 while (<$INPUT>)
707 next if /^\#/; # skip comments
708 next if /^$/; # skip empty lines
709 next if /\x1a/; # skip ^Z
710 if (/^0x[0-9a-fA-F]+\s+0x([0-9a-fA-F]+)\s+0x([0-9a-fA-F]+)\s+(\#.*)?/)
712 my $cp = 0x8080 + hex $1;
713 my $uni = hex $2;
714 $cp2uni[$cp] = $uni unless defined($cp2uni[$cp]);
715 $uni2cp[$uni] = $cp unless defined($uni2cp[$uni]);
716 next;
718 die "$name: Unrecognized line $_\n";
720 close $INPUT;
724 ################################################################
725 # build the sort keys table
726 sub READ_SORTKEYS_FILE()
728 my @sortkeys = ();
729 for (my $i = 0; $i < 65536; $i++) { $sortkeys[$i] = [ -1, 0, 0, 0, 0 ] };
731 my $INPUT = open_data_file( $REPORTS, $SORTKEYS );
732 while (<$INPUT>)
734 next if /^\#/; # skip comments
735 next if /^$/; # skip empty lines
736 next if /\x1a/; # skip ^Z
737 next if /^\@version/; # skip @version header
738 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]+)\]/)
740 my ($uni,$variable) = (hex $1, $2);
741 next if $uni > 65535;
742 $sortkeys[$uni] = [ $uni, hex $3, hex $4, hex $5, hex $6 ];
743 next;
745 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]+)\]/)
747 # multiple character sequence, ignored for now
748 next;
750 die "$SORTKEYS: Unrecognized line $_\n";
752 close $INPUT;
754 # compress the keys to 32 bit:
755 # key 1 to 16 bits, key 2 to 8 bits, key 3 to 4 bits, key 4 to 1 bit
757 @sortkeys = sort { ${$a}[1] <=> ${$b}[1] or
758 ${$a}[2] <=> ${$b}[2] or
759 ${$a}[3] <=> ${$b}[3] or
760 ${$a}[4] <=> ${$b}[4] or
761 $a cmp $b; } @sortkeys;
763 my ($n2, $n3) = (1, 1);
764 my @keys = (-1, -1, -1, -1, -1 );
765 my @flatkeys = ();
767 for (my $i = 0; $i < 65536; $i++)
769 my @current = @{$sortkeys[$i]};
770 next if $current[0] == -1;
771 if ($current[1] == $keys[1])
773 if ($current[2] == $keys[2])
775 if ($current[3] == $keys[3])
777 # nothing
779 else
781 $keys[3] = $current[3];
782 $n3++;
783 die if ($n3 >= 16);
786 else
788 $keys[2] = $current[2];
789 $keys[3] = $current[3];
790 $n2++;
791 $n3 = 1;
792 die if ($n2 >= 256);
795 else
797 $keys[1] = $current[1];
798 $keys[2] = $current[2];
799 $keys[3] = $current[3];
800 $n2 = 1;
801 $n3 = 1;
804 if ($current[2]) { $current[2] = $n2; }
805 if ($current[3]) { $current[3] = $n3; }
806 if ($current[4]) { $current[4] = 1; }
808 $flatkeys[$current[0]] = ($current[1] << 16) | ($current[2] << 8) | ($current[3] << 4) | $current[4];
810 return @flatkeys;
814 ################################################################
815 # build the sort keys table
816 sub DUMP_SORTKEYS($@)
818 my ($filename, @keys) = @_;
820 # count the number of 256-key ranges that contain something
822 my @offsets = ();
823 my $ranges = 2;
824 for (my $i = 0; $i < 256; $i++) { $offsets[$i] = 256; }
825 for (my $i = 0; $i < 65536; $i++)
827 next unless defined $keys[$i];
828 $offsets[$i >> 8] = $ranges * 256;
829 $ranges++;
830 $i |= 255;
833 # output the range offsets
835 open OUTPUT,">$filename.new" or die "Cannot create $filename";
836 printf "Building $filename\n";
837 printf OUTPUT "/* Unicode collation element table */\n";
838 printf OUTPUT "/* generated from %s */\n", "$REPORTS/$SORTKEYS";
839 printf OUTPUT "/* DO NOT EDIT!! */\n\n";
841 printf OUTPUT "const unsigned int collation_table[%d] =\n{\n", $ranges*256;
842 printf OUTPUT " /* index */\n";
843 printf OUTPUT "%s,\n", DUMP_ARRAY( "0x%08x", 0, @offsets );
845 # output the default values
847 printf OUTPUT " /* defaults */\n";
848 printf OUTPUT "%s", DUMP_ARRAY( "0x%08x", 0, (0xffffffff) x 256 );
850 # output all the key ranges
852 for (my $i = 0; $i < 256; $i++)
854 next if $offsets[$i] == 256;
855 printf OUTPUT ",\n /* 0x%02x00 .. 0x%02xff */\n", $i, $i;
856 printf OUTPUT "%s", DUMP_ARRAY( "0x%08x", 0xffffffff, @keys[($i<<8) .. ($i<<8)+255] );
858 printf OUTPUT "\n};\n";
859 close OUTPUT;
860 save_file($filename);
864 ################################################################
865 # add default mappings once the file had been read
866 sub add_default_mappings($)
868 my $first_private = shift;
870 # Apply aliases
872 foreach my $alias (@unicode_aliases)
874 my $target = undef;
875 foreach my $src (@$alias)
877 if (defined($uni2cp[$src]))
879 $target = $uni2cp[$src];
880 last;
883 next unless defined($target);
885 # At least one char of the alias set is defined, set the others to the same value
886 foreach my $src (@$alias)
888 $uni2cp[$src] = $target unless defined($uni2cp[$src]);
892 # For every src -> target mapping in the defaults table,
893 # make uni2cp[src] = uni2cp[target] if uni2cp[target] is defined
895 for (my $src = 0; $src < 65536; $src++)
897 next if defined($uni2cp[$src]); # source has a definition already
898 next unless defined($unicode_defaults[$src]); # no default for this char
899 my $target = $unicode_defaults[$src];
901 # do a recursive mapping until we find a target char that is defined
902 while (!defined($uni2cp[$target]) &&
903 defined($unicode_defaults[$target])) { $target = $unicode_defaults[$target]; }
905 if (defined($uni2cp[$target])) { $uni2cp[$src] = $uni2cp[$target]; }
908 # Add a private char mapping for undefined chars
910 if ($first_private)
912 for (my $i = 32; $i < 256; $i++)
914 next if defined($cp2uni[$i]);
915 $cp2uni[$i] = $first_private;
916 $uni2cp[$first_private] = $i;
917 $first_private++;
921 # Add an identity mapping for all undefined chars
923 for (my $i = 0; $i < 256; $i++)
925 next if defined($cp2uni[$i]);
926 next if defined($uni2cp[$i]);
927 $cp2uni[$i] = $uni2cp[$i] = $i;
931 ################################################################
932 # dump an array of integers
933 sub DUMP_ARRAY($$@)
935 my ($format,$default,@array) = @_;
936 my $i;
937 my $ret = " ";
938 for ($i = 0; $i < $#array; $i++)
940 $ret .= sprintf($format, defined $array[$i] ? $array[$i] : $default);
941 $ret .= (($i % 8) != 7) ? ", " : ",\n ";
943 $ret .= sprintf($format, defined $array[$i] ? $array[$i] : $default);
944 return $ret;
948 ################################################################
949 # dump a unicode->ascii mapping table
950 sub dump_uni2cp_table($$)
952 my ($width, $def) = @_;
954 # count the number of unicode->ascii subtables that contain something
956 my @lowerbounds;
957 my @upperbounds;
958 my $index = 0;
959 my @filled = (-1) x 256;
960 for (my $i = 0; $i < 65536; $i++)
962 next unless defined $uni2cp[$i];
963 if ($filled[$i >> 8] == -1)
965 $lowerbounds[$index] = $i & 0xff;
966 $upperbounds[$index] = 0xff - $lowerbounds[$index];
967 $filled[$i >> 8] = $index * 256;
968 $index++;
970 else
972 $upperbounds[$index-1] = 0xff - ($i & 0xff);
976 # add defaults mapping at the end
977 $filled[256] = $index * 256;
978 $lowerbounds[$index] = 255;
979 $upperbounds[$index] = 0;
980 $index++;
982 # collapse blocks upwards if possible
983 my $removed = 0;
984 $index = 0;
985 for (my $i = 0; $i < 257; $i++)
987 next if $filled[$i] == -1;
988 if ($upperbounds[$index - 1] > $lowerbounds[$index])
990 $removed += $lowerbounds[$index];
992 else
994 $removed += $upperbounds[$index - 1];
995 $lowerbounds[$index] = $upperbounds[$index - 1];
997 $filled[$i] -= $removed;
998 $index++;
1001 # output all the subtables into a single array
1003 printf OUTPUT "static const unsigned %s uni2cp_low[%d] =\n{\n",
1004 $width == 8 ? "char" : "short", ($index + 1) * 256 - $removed;
1006 my $format = $width == 8 ? "0x%02x" : "0x%04x";
1007 for (my $i = $index = 0; $i < 257; $i++)
1009 next if $filled[$i] == -1;
1010 my $start = ($i << 8) + $lowerbounds[$index];
1011 my $end = ($i << 8) + 255;
1012 if ($i == 256)
1014 print OUTPUT " /* defaults */\n";
1015 printf OUTPUT "%s\n};\n\n", DUMP_ARRAY( $format, 0, ($def) x ($end - $start + 1) );
1017 else
1019 printf OUTPUT " /* 0x%04x .. 0x%04x */\n", $start, $end;
1020 printf OUTPUT "%s,\n", DUMP_ARRAY( $format, $def, @uni2cp[$start .. $end] );
1022 $index++;
1025 # output a table of the offsets of the subtables in the previous array
1027 for (my $i = 0; $i < 256; $i++)
1029 if ($filled[$i] == -1) { $filled[$i] = $filled[256]; }
1031 printf OUTPUT "static const unsigned short uni2cp_high[256] =\n";
1032 printf OUTPUT "{\n%s\n};\n\n", DUMP_ARRAY( "0x%04x", 0, @filled[0..255] );
1035 ################################################################
1036 # dump an SBCS mapping table
1037 sub dump_sbcs_table($$$$$)
1039 my ($codepage, $has_glyphs, $name, $def, $defw) = @_;
1040 my $i;
1042 # output the ascii->unicode table
1044 if ($has_glyphs)
1046 printf OUTPUT "static const WCHAR cp2uni[512] =\n";
1047 printf OUTPUT "{\n%s", DUMP_ARRAY( "0x%04x", $defw, @cp2uni[0 .. 255] );
1048 printf OUTPUT ",\n /* glyphs */\n%s\n};\n\n",
1049 DUMP_ARRAY( "0x%04x", $defw, get_glyphs_mapping(@cp2uni[0 .. 255]) );
1051 else
1053 printf OUTPUT "static const WCHAR cp2uni[256] =\n";
1054 printf OUTPUT "{\n%s\n};\n\n", DUMP_ARRAY( "0x%04x", $defw, @cp2uni[0 .. 255] );
1057 dump_uni2cp_table( 8, $def );
1059 # output the code page descriptor
1061 printf OUTPUT "const struct sbcs_table cptable_%03d =\n{\n", $codepage;
1062 printf OUTPUT " { %d, 1, 0x%04x, 0x%04x, \"%s\" },\n",
1063 $codepage, $def, $defw, $name;
1064 printf OUTPUT " cp2uni,\n";
1065 if ($has_glyphs) { printf OUTPUT " cp2uni + 256,\n"; }
1066 else { printf OUTPUT " cp2uni,\n"; }
1067 printf OUTPUT " uni2cp_low,\n";
1068 printf OUTPUT " uni2cp_high\n};\n";
1072 ################################################################
1073 # dump a DBCS mapping table
1074 sub dump_dbcs_table($$$$@)
1076 my ($codepage, $name, $def, $defw, @lb_ranges) = @_;
1078 # build a list of lead bytes that are actually used
1080 my @lblist = ();
1081 LBLOOP: for (my $y = 0; $y <= $#lead_bytes; $y++)
1083 my $base = $lead_bytes[$y] << 8;
1084 for (my $x = 0; $x < 256; $x++)
1086 if (defined $cp2uni[$base+$x])
1088 push @lblist,$lead_bytes[$y];
1089 next LBLOOP;
1093 my $unused = ($#lead_bytes > $#lblist);
1095 # output the ascii->unicode table for the single byte chars
1097 printf OUTPUT "static const WCHAR cp2uni[%d] =\n", 256 * ($#lblist + 2 + $unused);
1098 printf OUTPUT "{\n%s,\n", DUMP_ARRAY( "0x%04x", $defw, @cp2uni[0 .. 255] );
1100 # output the default table for unused lead bytes
1102 if ($unused)
1104 printf OUTPUT " /* unused lead bytes */\n";
1105 printf OUTPUT "%s,\n", DUMP_ARRAY( "0x%04x", 0, ($defw) x 256 );
1108 # output the ascii->unicode table for each DBCS lead byte
1110 for (my $y = 0; $y <= $#lblist; $y++)
1112 my $base = $lblist[$y] << 8;
1113 printf OUTPUT " /* lead byte %02x */\n", $lblist[$y];
1114 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", $defw, @cp2uni[$base .. $base+255] );
1115 printf OUTPUT ($y < $#lblist) ? ",\n" : "\n};\n\n";
1118 # output the lead byte subtables offsets
1120 my @offsets = (0) x 256;
1121 for (my $x = 0; $x <= $#lblist; $x++) { $offsets[$lblist[$x]] = $x + 1; }
1122 if ($unused)
1124 # increment all lead bytes offset to take into account the unused table
1125 for (my $x = 0; $x <= $#lead_bytes; $x++) { $offsets[$lead_bytes[$x]]++; }
1127 printf OUTPUT "static const unsigned char cp2uni_leadbytes[256] =\n";
1128 printf OUTPUT "{\n%s\n};\n\n", DUMP_ARRAY( "0x%02x", 0, @offsets );
1130 dump_uni2cp_table( 16, $def );
1132 # output the code page descriptor
1134 printf OUTPUT "const struct dbcs_table cptable_%03d =\n{\n", $codepage;
1135 printf OUTPUT " { %d, 2, 0x%04x, 0x%04x, \"%s\" },\n",
1136 $codepage, $def, $defw, $name;
1137 printf OUTPUT " cp2uni,\n";
1138 printf OUTPUT " cp2uni_leadbytes,\n";
1139 printf OUTPUT " uni2cp_low,\n";
1140 printf OUTPUT " uni2cp_high,\n";
1141 printf OUTPUT " {\n %s\n }\n", DUMP_ARRAY( "0x%02x", 0, @lb_ranges, 0, 0 );
1142 printf OUTPUT "};\n";
1146 ################################################################
1147 # get the list of defined lead byte ranges
1148 sub get_lb_ranges()
1150 my @list = ();
1151 my @ranges = ();
1152 my $i = 0;
1153 foreach $i (@lead_bytes) { $list[$i] = 1; }
1154 my $on = 0;
1155 for (my $i = 0; $i < 256; $i++)
1157 if ($on)
1159 if (!defined $list[$i]) { push @ranges, $i-1; $on = 0; }
1161 else
1163 if ($list[$i]) { push @ranges, $i; $on = 1; }
1166 if ($on) { push @ranges, 0xff; }
1167 return @ranges;
1170 ################################################################
1171 # dump the Indic Syllabic Category table
1172 sub dump_indic($)
1174 my $filename = shift;
1175 my @indic_table = ($indic_types{'Other'}) x 65536;;
1177 my $INPUT = open_data_file( $UNIDATA, "IndicSyllabicCategory.txt" );
1178 while (<$INPUT>)
1180 next if /^\#/; # skip comments
1181 next if /^\s*$/; # skip empty lines
1182 next if /\x1a/; # skip ^Z
1183 if (/^\s*([0-9a-fA-F]+)\s*;\s*([a-zA-Z_]+)\s*#/)
1185 my $type = $2;
1186 die "unknown indic $type" unless defined $indic_types{$type};
1187 if (hex $1 < 65536)
1189 $indic_table[hex $1] = $indic_types{$type};
1191 next;
1193 elsif (/^\s*([0-9a-fA-F]+)..\s*([0-9a-fA-F]+)\s*;\s*([A-Za-z_]+)\s*#/)
1195 my $type = $3;
1196 die "unknown indic $type" unless defined $indic_types{$type};
1197 if (hex $1 < 65536 and hex $2 < 6536)
1199 foreach my $i (hex $1 .. hex $2)
1201 $indic_table[$i] = $indic_types{$type};
1204 next;
1206 die "malformed line $_";
1208 close $INPUT;
1210 $INPUT = open_data_file( $UNIDATA, "IndicMatraCategory.txt" );
1211 while (<$INPUT>)
1213 next if /^\#/; # skip comments
1214 next if /^\s*$/; # skip empty lines
1215 next if /\x1a/; # skip ^Z
1216 if (/^\s*([0-9a-fA-F]+)\s*;\s*([a-zA-Z]+)\s*#/)
1218 my $type = $2;
1219 die "unknown matra $type" unless defined $matra_types{$type};
1220 $indic_table[hex $1] += $matra_types{$type} << 8;
1221 next;
1223 elsif (/^\s*([0-9a-fA-F]+)..\s*([0-9a-fA-F]+)\s*;\s*([A-Za-z_]+)\s*#/)
1225 my $type = $3;
1226 die "unknown matra $type" unless defined $matra_types{$type};
1227 foreach my $i (hex $1 .. hex $2)
1229 $indic_table[$i] += $matra_types{$type} << 8;
1231 next;
1233 die "malformed line $_";
1235 close $INPUT;
1237 open OUTPUT,">$filename.new" or die "Cannot create $filename";
1238 print "Building $filename\n";
1239 print OUTPUT "/* Unicode Indic Syllabic Category */\n";
1240 print OUTPUT "/* generated from $UNIDATA/IndicSyllabicCategory.txt */\n";
1241 print OUTPUT "/* and from $UNIDATA/IndicMatraCategory.txt */\n";
1242 print OUTPUT "/* DO NOT EDIT!! */\n\n";
1244 dump_two_level_mapping( "indic_syllabic_table", @indic_table);
1246 close OUTPUT;
1247 save_file($filename);
1250 ################################################################
1251 # dump the Line Break Properties table
1252 sub dump_linebreak($)
1254 my $filename = shift;
1255 my @break_table = ($break_types{'XX'}) x 65536;;
1256 my $next_group = 0;
1258 my $INPUT = open_data_file( $UNIDATA, "LineBreak.txt" );
1259 while (<$INPUT>)
1261 next if /^\#/; # skip comments
1262 next if /^\s*$/; # skip empty lines
1263 next if /\x1a/; # skip ^Z
1264 if (/^\s*([0-9a-fA-F]+)\s*;\s*([0-9A-Z][0-9A-Z])+\s*/)
1266 my $type = $2;
1267 die "unknown breaktype $type" unless defined $break_types{$type};
1268 $break_table[hex $1] = $break_types{$type};
1269 next;
1271 elsif (/^\s*([0-9a-fA-F]+)..\s*([0-9a-fA-F]+)\s*;\s*([0-9A-Z][0-9A-Z])+\s*/)
1273 my $type = $3;
1274 die "unknown breaktype $type" unless defined $break_types{$type};
1275 foreach my $i (hex $1 .. hex $2)
1277 $break_table[$i] = $break_types{$type};
1279 next;
1281 die "malformed line $_";
1283 close $INPUT;
1285 open OUTPUT,">$filename.new" or die "Cannot create $filename";
1286 print "Building $filename\n";
1287 print OUTPUT "/* Unicode Line Break Properties */\n";
1288 print OUTPUT "/* generated from $UNIDATA/LineBreak.txt */\n";
1289 print OUTPUT "/* DO NOT EDIT!! */\n\n";
1291 dump_two_level_mapping( "wine_linebreak_table", @break_table);
1293 close OUTPUT;
1294 save_file($filename);
1298 ################################################################
1299 # dump the BiDi mirroring table
1300 sub dump_mirroring($)
1302 my $filename = shift;
1303 my @mirror_table = ();
1305 my $INPUT = open_data_file( $UNIDATA, "BidiMirroring.txt" );
1306 while (<$INPUT>)
1308 next if /^\#/; # skip comments
1309 next if /^$/; # skip empty lines
1310 next if /\x1a/; # skip ^Z
1311 if (/^\s*([0-9a-fA-F]+)\s*;\s*([0-9a-fA-F]+)/)
1313 $mirror_table[hex $1] = hex $2;
1314 next;
1316 die "malformed line $_";
1318 close $INPUT;
1320 open OUTPUT,">$filename.new" or die "Cannot create $filename";
1321 print "Building $filename\n";
1322 print OUTPUT "/* Unicode BiDi mirroring */\n";
1323 print OUTPUT "/* generated from $UNIDATA/BidiMirroring.txt */\n";
1324 print OUTPUT "/* DO NOT EDIT!! */\n\n";
1325 print OUTPUT "#include \"wine/unicode.h\"\n\n";
1326 DUMP_CASE_TABLE( "wine_mirror_map", @mirror_table );
1327 close OUTPUT;
1328 save_file($filename);
1331 ################################################################
1332 # dump the Bidi Brackets
1333 sub dump_bracket($)
1335 my $filename = shift;
1336 my @bracket_table = (0) x 65536;;
1338 my $INPUT = open_data_file( $UNIDATA, "BidiBrackets.txt" );
1339 while (<$INPUT>)
1341 next if /^\#/; # skip comments
1342 next if /^\s*$/; # skip empty lines
1343 next if /\x1a/; # skip ^Z
1344 if (/^\s*([0-9a-fA-F]+)\s*;\s*([0-9a-fA-F]+);\s*([con])/)
1346 my $type = $3;
1347 die "unknown bracket $type" unless defined $bracket_types{$type};
1348 die "characters too distant $1 and $2" if abs(hex($2) - hex($1)) >= 128;
1349 $bracket_table[hex $1] = (hex($2) - hex($1)) % 255;
1350 $bracket_table[hex $1] += $bracket_types{$type} << 8;
1351 next;
1353 die "malformed line $_";
1355 close $INPUT;
1357 open OUTPUT,">$filename.new" or die "Cannot create $filename";
1358 print "Building $filename\n";
1359 print OUTPUT "/* Unicode Bidirectional Bracket table */\n";
1360 print OUTPUT "/* generated from $UNIDATA/BidiBrackets.txt */\n";
1361 print OUTPUT "/* DO NOT EDIT!! */\n\n";
1363 dump_two_level_mapping( "bidi_bracket_table", @bracket_table);
1365 close OUTPUT;
1366 save_file($filename);
1369 ################################################################
1370 # dump the Arabic shaping table
1371 sub dump_shaping($)
1373 my $filename = shift;
1374 my %groups;
1375 my $next_group = 0;
1377 $groups{"No_Joining_Group"} = $next_group++;
1379 my $INPUT = open_data_file( $UNIDATA, "ArabicShaping.txt" );
1380 while (<$INPUT>)
1382 next if /^\#/; # skip comments
1383 next if /^\s*$/; # skip empty lines
1384 next if /\x1a/; # skip ^Z
1385 if (/^\s*([0-9a-fA-F]+)\s*;.*;\s*([RLDCUT])\s*;\s*(\w+)/)
1387 my $type = $2;
1388 my $group = $3;
1389 $groups{$group} = $next_group++ unless defined $groups{$group};
1390 $joining_table[hex $1] = $joining_types{$type} | ($groups{$group} << 8);
1391 next;
1393 die "malformed line $_";
1395 close $INPUT;
1397 open OUTPUT,">$filename.new" or die "Cannot create $filename";
1398 print "Building $filename\n";
1399 print OUTPUT "/* Unicode Arabic shaping */\n";
1400 print OUTPUT "/* generated from $UNIDATA/ArabicShaping.txt */\n";
1401 print OUTPUT "/* DO NOT EDIT!! */\n\n";
1403 dump_two_level_mapping( "wine_shaping_table", @joining_table );
1405 print OUTPUT "\nconst unsigned short wine_shaping_forms[256][4] =\n{\n";
1406 for (my $i = 0x600; $i <= 0x6ff; $i++)
1408 printf OUTPUT " { 0x%04x, 0x%04x, 0x%04x, 0x%04x },\n",
1409 ${joining_forms{"isolated"}}[$i] || $i,
1410 ${joining_forms{"final"}}[$i] || $i,
1411 ${joining_forms{"initial"}}[$i] || $i,
1412 ${joining_forms{"medial"}}[$i] || $i;
1414 print OUTPUT "};\n";
1416 close OUTPUT;
1417 save_file($filename);
1420 ################################################################
1421 # dump the Vertical Orientation table
1422 sub dump_vertical($)
1424 my $filename = shift;
1425 my @vertical_table = ($vertical_types{'R'}) x 65536;
1427 my $INPUT = open_data_file( $VERTICALDATA, "VerticalOrientation-11.txt" );
1428 while (<$INPUT>)
1430 next if /^\#/; # skip comments
1431 next if /^\s*$/; # skip empty lines
1432 next if /\x1a/; # skip ^Z
1433 if (/^\s*([0-9a-fA-F]+)\s*;\s*([a-zA-Z_]+)\s*/)
1435 my $type = $2;
1436 die "unknown vertical $type" unless defined $vertical_types{$type};
1437 if (hex $1 < 65536)
1439 $vertical_table[hex $1] = $vertical_types{$type};
1441 next;
1443 elsif (/^\s*([0-9a-fA-F]+)..\s*([0-9a-fA-F]+)\s*;\s*([A-Za-z_]+)\s*/)
1445 my $type = $3;
1446 die "unknown vertical $type" unless defined $vertical_types{$type};
1447 foreach my $i (hex $1 .. hex $2)
1449 $vertical_table[$i] = $vertical_types{$type};
1451 next;
1453 die "malformed line $_";
1455 close $INPUT;
1457 open OUTPUT,">$filename.new" or die "Cannot create $filename";
1458 print "Building $filename\n";
1459 print OUTPUT "/* Unicode Vertical Orientation */\n";
1460 print OUTPUT "/* generated from $VERTICALDATA/VerticalOrientation-11.txt */\n";
1461 print OUTPUT "/* DO NOT EDIT!! */\n\n";
1463 dump_two_level_mapping( "vertical_orientation_table", @vertical_table);
1465 close OUTPUT;
1466 save_file($filename);
1469 ################################################################
1470 # dump the case mapping tables
1471 sub DUMP_CASE_MAPPINGS($)
1473 my $filename = shift;
1474 open OUTPUT,">$filename.new" or die "Cannot create $filename";
1475 printf "Building $filename\n";
1476 printf OUTPUT "/* Unicode case mappings */\n";
1477 printf OUTPUT "/* Automatically generated; DO NOT EDIT!! */\n\n";
1478 printf OUTPUT "#include \"wine/unicode.h\"\n\n";
1480 DUMP_CASE_TABLE( "wine_casemap_lower", @tolower_table );
1481 DUMP_CASE_TABLE( "wine_casemap_upper", @toupper_table );
1482 DUMP_CASE_TABLE( "wine_digitmap", @digitmap_table );
1483 DUMP_CASE_TABLE( "wine_compatmap", @compatmap_table );
1484 close OUTPUT;
1485 save_file($filename);
1489 ################################################################
1490 # dump a case mapping table
1491 sub DUMP_CASE_TABLE($@)
1493 my ($name,@table) = @_;
1495 # count the number of sub tables that contain something
1496 # also compute the low and upper populated bounds
1498 my @lowerbounds = ( 0, 0 );
1499 my @upperbounds = ( 0, 255 );
1500 my $index = 0;
1501 my @filled = ();
1502 for (my $i = 0; $i < 65536; $i++)
1504 next unless defined $table[$i];
1505 if (!defined $filled[$i >> 8])
1507 $lowerbounds[$index] = $i & 0xff;
1508 $upperbounds[$index] = 0xff - $lowerbounds[$index];
1509 $filled[$i >> 8] = $index * 256 + 512;
1510 $index++;
1512 else
1514 $upperbounds[$index-1] = 0xff - ($i & 0xff);
1516 $table[$i] = ($table[$i] - $i) & 0xffff;
1519 # Collapse blocks upwards if possible
1520 my $removed = 0;
1521 $index = 0;
1522 for (my $i = 0; $i < 256; $i++)
1524 next unless defined $filled[$i];
1525 if ($upperbounds[$index - 1] > $lowerbounds[$index])
1527 $removed = $removed + $lowerbounds[$index];
1529 else
1531 $removed = $removed + $upperbounds[$index - 1];
1532 $lowerbounds[$index] = $upperbounds[$index - 1];
1534 $filled[$i] = $filled[$i] - $removed;
1535 $index++;
1538 # dump the table
1540 printf OUTPUT "const WCHAR %s[%d] =\n", $name, $index * 256 + 512 - $removed;
1541 printf OUTPUT "{\n /* index */\n";
1542 printf OUTPUT "%s,\n", DUMP_ARRAY( "0x%04x", 256, @filled );
1543 printf OUTPUT " /* defaults */\n";
1544 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, (0) x 256 );
1545 $index = 0;
1546 for (my $i = 0; $i < 256; $i++)
1548 next unless $filled[$i];
1549 printf OUTPUT ",\n /* 0x%02x%02x .. 0x%02xff */\n", $i, $lowerbounds[$index], $i;
1550 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0,
1551 @table[($i<<8) + $lowerbounds[$index] .. ($i<<8)+255] );
1552 $index++;
1554 printf OUTPUT "\n};\n";
1557 ################################################################
1558 # compress a mapping table by removing identical rows
1559 sub compress_array($@)
1561 my $rows = shift;
1562 my @table = @_;
1563 my $len = @table / $rows;
1564 my @array = (0) x $rows;
1565 my %sequences;
1567 # try to merge table rows
1568 for (my $row = 0; $row < $rows; $row++)
1570 my $rowtxt = pack "S*", @table[($row * $len)..($row * $len + $len - 1)];
1571 if (defined($sequences{$rowtxt}))
1573 # reuse an existing row
1574 $array[$row] = $sequences{$rowtxt};
1576 else
1578 # create a new row
1579 $sequences{$rowtxt} = $array[$row] = $#array + 1;
1580 push @array, @table[$row * $len..$row * $len + $len - 1];
1583 return @array;
1586 ################################################################
1587 # dump a simple char -> 16-bit value mapping table
1588 sub dump_simple_mapping($@)
1590 my $name = shift;
1591 my @array = compress_array( 256, @_[0..65535] );
1593 printf OUTPUT "const unsigned short %s[%d] =\n{\n", $name, $#array+1;
1594 printf OUTPUT " /* offsets */\n%s,\n", DUMP_ARRAY( "0x%04x", 0, @array[0..255] );
1595 printf OUTPUT " /* values */\n%s\n};\n", DUMP_ARRAY( "0x%04x", 0, @array[256..$#array] );
1598 ################################################################
1599 # dump a char -> 16-bit value mapping table using two-level tables
1600 sub dump_two_level_mapping($@)
1602 my $name = shift;
1603 my @row_array = compress_array( 4096, @_[0..65535] );
1604 my @array = compress_array( 256, @row_array[0..4095] );
1606 for (my $i = 256; $i < @array; $i++) { $array[$i] += @array - 4096; }
1608 printf OUTPUT "const unsigned short %s[%d] =\n{\n", $name, @array + @row_array - 4096;
1609 printf OUTPUT " /* level 1 offsets */\n%s,\n", DUMP_ARRAY( "0x%04x", 0, @array[0..255] );
1610 printf OUTPUT " /* level 2 offsets */\n%s,\n", DUMP_ARRAY( "0x%04x", 0, @array[256..$#array] );
1611 printf OUTPUT " /* values */\n%s\n};\n", DUMP_ARRAY( "0x%04x", 0, @row_array[4096..$#row_array] );
1614 ################################################################
1615 # dump a binary case mapping table in l_intl.nls format
1616 sub dump_binary_case_table(@)
1618 my (@table) = @_;
1620 my %difftables_hash = ();
1621 my @difftables;
1622 my %offtables2_hash = ();
1623 my @offtables2 = ();
1625 my @offtable = ();
1626 for (my $i = 0; $i < 256; $i++)
1628 my @offtable2 = ();
1629 for(my $j = 0; $j < 16; $j++) # offset table for xx00-xxFF characters
1631 my @difftable;
1632 for (my $k = 0; $k < 16; $k++) # case map table for xxx0-xxxF characters
1634 my $char = ($i<<8) + ($j<<4) + $k;
1635 $difftable[$k] = (defined $table[$char]) ? (($table[$char]-$char) & 0xffff) : 0;
1638 my $diff_key = pack "S*", @difftable;
1639 my $offset3 = $difftables_hash{$diff_key};
1640 if (!defined $offset3)
1642 $offset3 = scalar @difftables;
1643 $difftables_hash{$diff_key} = $offset3;
1644 push @difftables, @difftable;
1646 $offtable2[$j] = $offset3;
1649 my $offtable2_key = pack "S*", @offtable2;
1650 my $offset2 = $offtables2_hash{$offtable2_key};
1651 if (!defined $offset2)
1653 $offset2 = scalar @offtables2;
1654 $offtables2_hash{$offtable2_key} = $offset2;
1655 push @offtables2, \@offtable2;
1657 $offtable[$i] = $offset2;
1660 my @output;
1661 my $offset = 0x100; # offset of first subtable in words
1662 foreach (@offtable)
1664 push @output, 0x10 * $_ + $offset; # offset of subtable in words
1667 $offset = 0x100 + 0x10 * scalar @offtables2; # offset of first difftable in words
1668 foreach(@offtables2)
1670 my $table = $_;
1671 foreach(@$table)
1673 push @output, $_ + $offset; # offset of difftable in words
1677 my $len = 1 + scalar @output + scalar @difftables;
1678 return pack "S<*", $len, @output, @difftables;
1682 ################################################################
1683 # dump case mappings for l_intl.nls
1684 sub dump_intl_nls($)
1686 my $filename = shift;
1687 open OUTPUT,">$filename.new" or die "Cannot create $filename";
1688 printf "Building $filename\n";
1690 binmode OUTPUT;
1691 print OUTPUT pack "S<", 1; # version
1692 print OUTPUT dump_binary_case_table( @toupper_table );
1693 print OUTPUT dump_binary_case_table( @tolower_table );
1694 close OUTPUT;
1695 save_file($filename);
1699 sub load_nameprep_range_table($$$)
1701 my ($INPUT, $val, $table_ref) = @_;
1703 while (<$INPUT>)
1705 if (/^\s*([0-9a-fA-F]+)-([0-9a-fA-F]+)/)
1707 my $last = hex $2;
1708 $last = 65535 if($last >= 65536);
1709 foreach my $i (hex $1 .. $last)
1711 $table_ref->[$i] |= $val;
1713 next;
1715 elsif (/^\s*([0-9a-fA-F]+)/)
1717 if (hex $1 < 65536)
1719 $table_ref->[hex $1] |= $val;
1721 next;
1724 return if (/End\sTable/);
1728 sub load_nameprep_map_table($$)
1730 my ($INPUT, $table_ref) = @_;
1732 while (<$INPUT>)
1734 if (/^\s*([0-9a-fA-F]+);\s;/)
1736 # special value for map to nothing
1737 $table_ref->[hex $1] = [0xffff, 0xffff, 0xffff];
1738 next;
1740 elsif (/^\s*([0-9a-fA-F]+);\s([0-9a-fA-F]+);/)
1742 $table_ref->[hex $1] = [hex $2, 0, 0];
1743 next;
1745 elsif (/^\s*([0-9a-fA-F]+);\s([0-9a-fA-F]+)\s([0-9a-fA-F]+);/)
1747 $table_ref->[hex $1] = [hex $2, hex $3, 0];
1748 next;
1750 elsif (/^\s*([0-9a-fA-F]+);\s([0-9a-fA-F]+)\s([0-9a-fA-F]+)\s([0-9a-fA-F]+);/)
1752 $table_ref->[hex $1] = [hex $2, hex $3, hex $4];
1753 next;
1756 return if (/End\sTable/);
1760 ################################################################
1761 # dump mapping table, prohibited characters set, unassigned
1762 # characters, bidirectional rules used by nameprep algorithm
1763 sub dump_nameprep($)
1765 my $filename = shift;
1766 my @mapping_table = ();
1767 my @flags_table = (0) x 65536;
1769 my $INPUT = open_data_file( $RFCS, $STRINGPREP );
1770 while (<$INPUT>)
1772 next unless /Start\sTable/;
1774 load_nameprep_range_table($INPUT, $nameprep_flags{"unassigned"}, \@flags_table) if (/A.1/);
1775 load_nameprep_range_table($INPUT, $nameprep_flags{"prohibited"}, \@flags_table) if (/C.1.2/);
1776 load_nameprep_range_table($INPUT, $nameprep_flags{"prohibited"}, \@flags_table) if (/C.2.2/);
1777 load_nameprep_range_table($INPUT, $nameprep_flags{"prohibited"}, \@flags_table) if (/C.3/);
1778 load_nameprep_range_table($INPUT, $nameprep_flags{"prohibited"}, \@flags_table) if (/C.4/);
1779 load_nameprep_range_table($INPUT, $nameprep_flags{"prohibited"}, \@flags_table) if (/C.5/);
1780 load_nameprep_range_table($INPUT, $nameprep_flags{"prohibited"}, \@flags_table) if (/C.6/);
1781 load_nameprep_range_table($INPUT, $nameprep_flags{"prohibited"}, \@flags_table) if (/C.7/);
1782 load_nameprep_range_table($INPUT, $nameprep_flags{"prohibited"}, \@flags_table) if (/C.8/);
1783 load_nameprep_range_table($INPUT, $nameprep_flags{"prohibited"}, \@flags_table) if (/C.9/);
1784 load_nameprep_range_table($INPUT, $nameprep_flags{"bidi_ral"}, \@flags_table) if (/D.1/);
1785 load_nameprep_range_table($INPUT, $nameprep_flags{"bidi_l"}, \@flags_table) if (/D.2/);
1787 load_nameprep_map_table($INPUT, \@mapping_table) if (/B.1/);
1788 load_nameprep_map_table($INPUT, \@mapping_table) if (/B.2/);
1790 close $INPUT;
1792 open OUTPUT,">$filename.new" or die "Cannot create $filename";
1793 print "Building $filename\n";
1794 print OUTPUT "/* Nameprep algorithm related data */\n";
1795 print OUTPUT "/* generated from $RFCS/$STRINGPREP */\n";
1796 print OUTPUT "/* DO NOT EDIT!! */\n\n";
1798 dump_two_level_mapping( "nameprep_char_type", @flags_table );
1800 ######### mapping table
1801 # first determine all the 16-char subsets that contain something
1802 my @filled = ();
1803 my $pos = 16*3; # for the null subset
1804 for (my $i = 0; $i < 65536; $i++)
1806 next unless defined $mapping_table[$i];
1807 $filled[$i >> 4] = $pos;
1808 $pos += 16*3;
1809 $i |= 15;
1811 my $total = $pos;
1813 # now count the 256-char subsets that contain something
1814 my @filled_idx = (256) x 256;
1815 $pos = 256 + 16;
1816 for (my $i = 0; $i < 4096; $i++)
1818 next unless $filled[$i];
1819 $filled_idx[$i >> 4] = $pos;
1820 $pos += 16;
1821 $i |= 15;
1823 my $null_offset = $pos;
1824 $total += $pos;
1826 # add the index offsets to the subsets positions
1827 for (my $i = 0; $i < 4096; $i++)
1829 next unless $filled[$i];
1830 $filled[$i] += $null_offset;
1833 # dump the main index
1834 printf OUTPUT "const unsigned short nameprep_mapping[%d] =\n", $total;
1835 printf OUTPUT "{\n /* index */\n";
1836 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, @filled_idx );
1837 printf OUTPUT ",\n /* null sub-index */\n%s", DUMP_ARRAY( "0x%04x", 0, ($null_offset) x 16 );
1839 # dump the second-level indexes
1840 for (my $i = 0; $i < 256; $i++)
1842 next unless ($filled_idx[$i] > 256);
1843 my @table = @filled[($i<<4)..($i<<4)+15];
1844 for (my $j = 0; $j < 16; $j++) { $table[$j] ||= $null_offset; }
1845 printf OUTPUT ",\n /* sub-index %02x */\n", $i;
1846 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, @table );
1849 # dump the 16-char subsets
1850 printf OUTPUT ",\n /* null mapping */\n";
1851 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, (0) x 48 );
1853 for (my $i = 0; $i < 4096; $i++)
1855 next unless $filled[$i];
1856 my @table = (0) x 48;
1857 for (my $j = 0; $j < 16; $j++)
1859 if (defined $mapping_table[($i<<4) + $j])
1861 $table[3 * $j] = ${$mapping_table[($i << 4) + $j]}[0];
1862 $table[3 * $j + 1] = ${$mapping_table[($i << 4) + $j]}[1];
1863 $table[3 * $j + 2] = ${$mapping_table[($i << 4) + $j]}[2];
1866 printf OUTPUT ",\n /* 0x%03x0 .. 0x%03xf */\n", $i, $i;
1867 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, @table );
1870 printf OUTPUT "\n};\n";
1872 close OUTPUT;
1873 save_file($filename);
1876 ################################################################
1877 # dump the ctype tables
1878 sub DUMP_CTYPE_TABLES($)
1880 my $filename = shift;
1881 open OUTPUT,">$filename.new" or die "Cannot create $filename";
1882 printf "Building $filename\n";
1883 printf OUTPUT "/* Unicode ctype tables */\n";
1884 printf OUTPUT "/* Automatically generated; DO NOT EDIT!! */\n\n";
1885 printf OUTPUT "#include \"wine/unicode.h\"\n\n";
1887 # add the direction in the high 4 bits of the category
1888 for (my $i = 0; $i < 65536; $i++)
1890 $category_table[$i] |= $direction_table[$i] << 12 if defined $direction_table[$i];
1893 dump_simple_mapping( "wine_wctype_table", @category_table );
1895 close OUTPUT;
1896 save_file($filename);
1900 ################################################################
1901 # dump the char composition tables
1902 sub DUMP_COMPOSE_TABLES($)
1904 my $filename = shift;
1906 open OUTPUT,">$filename.new" or die "Cannot create $filename";
1907 printf "Building $filename\n";
1908 printf OUTPUT "/* Unicode char composition */\n";
1909 printf OUTPUT "/* Automatically generated; DO NOT EDIT!! */\n\n";
1910 printf OUTPUT "#include \"wine/unicode.h\"\n\n";
1912 ######### composition table
1914 my @filled = ();
1915 foreach my $i (@compose_table)
1917 my @comp = @$i;
1918 push @{$filled[$comp[1]]}, [ $comp[0], $comp[2] ];
1921 # count how many different second chars we have
1923 my $count = 0;
1924 for (my $i = 0; $i < 65536; $i++)
1926 next unless defined $filled[$i];
1927 $count++;
1930 # build the table of second chars and offsets
1932 my $pos = $count + 1;
1933 my @table = ();
1934 for (my $i = 0; $i < 65536; $i++)
1936 next unless defined $filled[$i];
1937 push @table, $i, $pos;
1938 $pos += @{$filled[$i]};
1940 # terminator with last position
1941 push @table, 0, $pos;
1942 printf OUTPUT "const WCHAR unicode_compose_table[0x%x] =\n{\n", 2*$pos;
1943 printf OUTPUT " /* second chars + offsets */\n%s", DUMP_ARRAY( "0x%04x", 0, @table );
1945 # build the table of first chars and mappings
1947 for (my $i = 0; $i < 65536; $i++)
1949 next unless defined $filled[$i];
1950 my @table = ();
1951 my @list = sort { $a->[0] <=> $b->[0] } @{$filled[$i]};
1952 for (my $j = 0; $j <= $#list; $j++)
1954 push @table, $list[$j][0], $list[$j][1];
1956 printf OUTPUT ",\n /* 0x%04x */\n%s", $i, DUMP_ARRAY( "0x%04x", 0, @table );
1958 printf OUTPUT "\n};\n\nconst unsigned int unicode_compose_table_size = %d;\n\n", $count;
1960 ######### decomposition table
1962 # first determine all the 16-char subsets that contain something
1964 @filled = (0) x 4096;
1965 $pos = 16*2; # for the null subset
1966 for (my $i = 0; $i < 65536; $i++)
1968 next unless defined $decomp_table[$i];
1969 $filled[$i >> 4] = $pos;
1970 $pos += 16*2;
1971 $i |= 15;
1973 my $total = $pos;
1975 # now count the 256-char subsets that contain something
1977 my @filled_idx = (256) x 256;
1978 $pos = 256 + 16;
1979 for (my $i = 0; $i < 4096; $i++)
1981 next unless $filled[$i];
1982 $filled_idx[$i >> 4] = $pos;
1983 $pos += 16;
1984 $i |= 15;
1986 my $null_offset = $pos; # null mapping
1987 $total += $pos;
1989 # add the index offsets to the subsets positions
1991 for (my $i = 0; $i < 4096; $i++)
1993 next unless $filled[$i];
1994 $filled[$i] += $null_offset;
1997 # dump the main index
1999 printf OUTPUT "const WCHAR unicode_decompose_table[%d] =\n", $total;
2000 printf OUTPUT "{\n /* index */\n";
2001 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, @filled_idx );
2002 printf OUTPUT ",\n /* null sub-index */\n%s", DUMP_ARRAY( "0x%04x", 0, ($null_offset) x 16 );
2004 # dump the second-level indexes
2006 for (my $i = 0; $i < 256; $i++)
2008 next unless ($filled_idx[$i] > 256);
2009 my @table = @filled[($i<<4)..($i<<4)+15];
2010 for (my $j = 0; $j < 16; $j++) { $table[$j] ||= $null_offset; }
2011 printf OUTPUT ",\n /* sub-index %02x */\n", $i;
2012 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, @table );
2015 # dump the 16-char subsets
2017 printf OUTPUT ",\n /* null mapping */\n";
2018 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, (0) x 32 );
2020 for (my $i = 0; $i < 4096; $i++)
2022 next unless $filled[$i];
2023 my @table = (0) x 32;
2024 for (my $j = 0; $j < 16; $j++)
2026 if (defined $decomp_table[($i<<4) + $j])
2028 $table[2 * $j] = ${$decomp_table[($i << 4) + $j]}[0];
2029 $table[2 * $j + 1] = ${$decomp_table[($i << 4) + $j]}[1];
2032 printf OUTPUT ",\n /* 0x%03x0 .. 0x%03xf */\n", $i, $i;
2033 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, @table );
2036 printf OUTPUT "\n};\n";
2037 close OUTPUT;
2038 save_file($filename);
2041 ################################################################
2042 # output a codepage definition file from the global tables
2043 sub output_codepage_file($$$$)
2045 my ($codepage, $filename, $comment, $has_glyphs) = @_;
2047 my $output = sprintf "libs/wine/c_%03d.c", $codepage;
2048 open OUTPUT,">$output.new" or die "Cannot create $output";
2050 printf "Building %s from %s (%s)\n", $output, $filename || "hardcoded data", $comment;
2052 # dump all tables
2054 printf OUTPUT "/* code page %03d (%s) */\n", $codepage, $comment;
2055 if ($filename)
2057 print OUTPUT "/* generated from $MAPPINGS/$filename */\n";
2058 print OUTPUT "/* DO NOT EDIT!! */\n\n";
2060 else
2062 printf OUTPUT "/* Automatically generated; DO NOT EDIT!! */\n\n";
2064 printf OUTPUT "#include \"wine/unicode.h\"\n\n";
2066 if (!@lead_bytes) { dump_sbcs_table( $codepage, $has_glyphs, $comment, $default_char, $default_wchar ); }
2067 else { dump_dbcs_table( $codepage, $comment, $default_char, $default_wchar, get_lb_ranges() ); }
2068 close OUTPUT;
2069 save_file($output);
2072 ################################################################
2073 # read a "bestfit" Windows mapping file
2074 sub read_bestfit_file($)
2076 my ($filename) = @_;
2077 my $state = "";
2078 my ($codepage, $width, $count);
2079 my ($lb_cur, $lb_end);
2081 my $INPUT = open_data_file( $MAPPINGS, $filename ) or die "Cannot open $filename";
2083 while (<$INPUT>)
2085 next if /^;/; # skip comments
2086 next if /^\s*$/; # skip empty lines
2087 next if /\x1a/; # skip ^Z
2088 last if /^ENDCODEPAGE/;
2090 if (/^CODEPAGE\s+(\d+)/)
2092 $codepage = $1;
2093 next;
2095 if (/^CPINFO\s+(\d+)\s+0x([0-9a-fA-f]+)\s+0x([0-9a-fA-F]+)/)
2097 $width = $1;
2098 $default_char = hex $2;
2099 $default_wchar = hex $3;
2100 next;
2102 if (/^(MBTABLE|WCTABLE|DBCSRANGE|DBCSTABLE)\s+(\d+)/)
2104 $state = $1;
2105 $count = $2;
2106 next;
2108 if (/^0x([0-9a-fA-F]+)\s+0x([0-9a-fA-F]+)/)
2110 if ($state eq "MBTABLE")
2112 my $cp = hex $1;
2113 my $uni = hex $2;
2114 $cp2uni[$cp] = $uni unless defined($cp2uni[$cp]);
2115 next;
2117 if ($state eq "WCTABLE")
2119 my $uni = hex $1;
2120 my $cp = hex $2;
2121 $uni2cp[$uni] = $cp unless defined($uni2cp[$uni]);
2122 next;
2124 if ($state eq "DBCSRANGE")
2126 my $start = hex $1;
2127 my $end = hex $2;
2128 for (my $i = $start; $i <= $end; $i++) { add_lead_byte( $i ); }
2129 $lb_cur = $start;
2130 $lb_end = $end;
2131 next;
2133 if ($state eq "DBCSTABLE")
2135 my $mb = hex $1;
2136 my $uni = hex $2;
2137 my $cp = ($lb_cur << 8) | $mb;
2138 $cp2uni[$cp] = $uni unless defined($cp2uni[$cp]);
2139 if (!--$count)
2141 if (++$lb_cur > $lb_end) { $state = "DBCSRANGE"; }
2143 next;
2146 die "$filename: Unrecognized line $_\n";
2148 close $INPUT;
2152 ################################################################
2153 # read an input file and generate the corresponding .c file
2154 sub HANDLE_FILE(@)
2156 my ($codepage,$filename,$has_glyphs,$comment,$first_private,$def,$defw) = @_;
2158 @cp2uni = ();
2159 @lead_bytes = ();
2160 @uni2cp = ();
2161 $default_char = $def || $DEF_CHAR;
2162 $default_wchar = $defw || $DEF_CHAR;
2164 # some codepage files are special
2165 if ($codepage == 20932)
2167 READ_JIS0208_FILE( $filename );
2168 add_default_mappings( $first_private );
2170 elsif ($codepage == 20127)
2172 fill_20127_codepage();
2173 add_default_mappings( $first_private );
2175 elsif ($filename =~ /\/bestfit/)
2177 read_bestfit_file( $filename );
2179 else
2181 read_codepage_file( $filename );
2182 if ($codepage == 10001)
2184 # add Shift-JIS mappings
2185 read_bestfit_file( "VENDORS/MICSFT/WindowsBestFit/bestfit932.txt" );
2187 else
2189 add_default_mappings( $first_private );
2193 output_codepage_file( $codepage, $filename, $comment, $has_glyphs );
2197 ################################################################
2198 # save a file if modified
2199 sub save_file($)
2201 my $file = shift;
2202 if (-f $file && !system "cmp $file $file.new >/dev/null")
2204 unlink "$file.new";
2206 else
2208 rename "$file.new", "$file";
2213 ################################################################
2214 # output the list of codepage tables into the cptable.c file
2215 sub output_cptable($)
2217 my $output = shift;
2218 my @tables_decl = ();
2220 printf "Building %s\n", $output;
2222 foreach my $file (@allfiles)
2224 my ($codepage,$filename) = @$file;
2225 push @tables_decl, sprintf("extern union cptable cptable_%03d;\n",$codepage);
2228 push @tables_decl, sprintf("\nstatic const union cptable * const cptables[%d] =\n{\n",$#allfiles+1);
2229 foreach my $file (@allfiles)
2231 my ($codepage,$filename) = @$file;
2232 push @tables_decl, sprintf(" &cptable_%03d,\n", $codepage);
2234 push @tables_decl, "};";
2235 REPLACE_IN_FILE( $output, @tables_decl );
2238 ################################################################
2239 # replace the contents of a file between ### cpmap ### marks
2241 sub REPLACE_IN_FILE($@)
2243 my $name = shift;
2244 my @data = @_;
2245 my @lines = ();
2246 open(FILE,$name) or die "Can't open $name";
2247 while (<FILE>)
2249 push @lines, $_;
2250 last if /\#\#\# cpmap begin \#\#\#/;
2252 push @lines, @data;
2253 while (<FILE>)
2255 if (/\#\#\# cpmap end \#\#\#/) { push @lines, "\n", $_; last; }
2257 push @lines, <FILE>;
2258 open(FILE,">$name.new") or die "Can't modify $name";
2259 print FILE @lines;
2260 close(FILE);
2261 save_file($name);
2264 ################################################################
2265 # main routine
2267 chdir ".." if -f "./make_unicode";
2268 READ_DEFAULTS( $DEFAULTS );
2269 DUMP_CASE_MAPPINGS( "libs/wine/casemap.c" );
2270 DUMP_SORTKEYS( "libs/wine/collation.c", READ_SORTKEYS_FILE() );
2271 DUMP_COMPOSE_TABLES( "libs/wine/compose.c" );
2272 DUMP_CTYPE_TABLES( "libs/wine/wctype.c" );
2273 dump_mirroring( "dlls/usp10/mirror.c" );
2274 dump_bracket( "dlls/usp10/bracket.c" );
2275 dump_shaping( "dlls/usp10/shaping.c" );
2276 dump_linebreak( "dlls/usp10/linebreak.c" );
2277 dump_indic( "dlls/usp10/indicsyllable.c" );
2278 dump_intl_nls("loader/l_intl.nls");
2279 dump_vertical( "dlls/gdi32/vertical.c" );
2280 dump_vertical( "dlls/wineps.drv/vertical.c" );
2281 dump_nameprep( "dlls/kernel32/nameprep.c" );
2283 foreach my $file (@allfiles) { HANDLE_FILE( @{$file} ); }
2285 output_cptable("libs/wine/cptable.c");
2287 exit 0;
2289 # Local Variables:
2290 # compile-command: "./make_unicode"
2291 # End: