urlmon/tests: Use BOOL type where appropriate.
[wine.git] / tools / make_unicode
blob1acc424398f835dabb9ac4e50a0db1f02167a89c
1 #!/usr/bin/perl -w
3 # Generate code page .c files from ftp.unicode.org descriptions
5 # Copyright 2000 Alexandre Julliard
7 # This library is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation; either
10 # version 2.1 of the License, or (at your option) any later version.
12 # This library is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # Lesser General Public License for more details.
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with this library; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 use strict;
24 # base URLs for www.unicode.org files
25 my $MAPPINGS = "http://www.unicode.org/Public/MAPPINGS";
26 my $UNIDATA = "http://www.unicode.org/Public/6.2.0/ucd";
27 my $REPORTS = "http://www.unicode.org/reports";
28 my $RFCS = "http://www.rfc-editor.org/rfc";
30 # Sort keys file
31 my $SORTKEYS = "tr10/allkeys.txt";
33 # RFC3454 (stringprep data)
34 my $STRINGPREP = "rfc3454.txt";
36 # Defaults mapping
37 my $DEFAULTS = "tools/unicode-defaults";
39 # Default char for undefined mappings
40 my $DEF_CHAR = ord '?';
42 my @allfiles =
44 [ 37, "VENDORS/MICSFT/EBCDIC/CP037.TXT", 0, "IBM EBCDIC US Canada", 0, 0x6f ],
45 [ 424, "VENDORS/MISC/CP424.TXT", 0, "IBM EBCDIC Hebrew" ],
46 [ 437, "VENDORS/MICSFT/PC/CP437.TXT", 1, "OEM United States" ],
47 [ 500, "VENDORS/MICSFT/EBCDIC/CP500.TXT", 0, "IBM EBCDIC International", 0, 0x6f ],
48 [ 737, "VENDORS/MICSFT/PC/CP737.TXT", 1, "OEM Greek 437G" ],
49 [ 775, "VENDORS/MICSFT/PC/CP775.TXT", 1, "OEM Baltic" ],
50 [ 850, "VENDORS/MICSFT/PC/CP850.TXT", 1, "OEM Multilingual Latin 1" ],
51 [ 852, "VENDORS/MICSFT/PC/CP852.TXT", 1, "OEM Slovak Latin 2" ],
52 [ 855, "VENDORS/MICSFT/PC/CP855.TXT", 1, "OEM Cyrillic" ],
53 [ 856, "VENDORS/MISC/CP856.TXT", 0, "Hebrew PC" ],
54 [ 857, "VENDORS/MICSFT/PC/CP857.TXT", 1, "OEM Turkish", 0xf8bb ],
55 [ 860, "VENDORS/MICSFT/PC/CP860.TXT", 1, "OEM Portuguese" ],
56 [ 861, "VENDORS/MICSFT/PC/CP861.TXT", 1, "OEM Icelandic" ],
57 [ 862, "VENDORS/MICSFT/PC/CP862.TXT", 1, "OEM Hebrew" ],
58 [ 863, "VENDORS/MICSFT/PC/CP863.TXT", 1, "OEM Canadian French" ],
59 [ 864, "VENDORS/MICSFT/PC/CP864.TXT", 0, "OEM Arabic", 0xf8bb ],
60 [ 865, "VENDORS/MICSFT/PC/CP865.TXT", 1, "OEM Nordic" ],
61 [ 866, "VENDORS/MICSFT/PC/CP866.TXT", 1, "OEM Russian" ],
62 [ 869, "VENDORS/MICSFT/PC/CP869.TXT", 1, "OEM Greek" ],
63 [ 874, "VENDORS/MICSFT/WindowsBestFit/bestfit874.txt", 1, "ANSI/OEM Thai" ],
64 [ 875, "VENDORS/MICSFT/EBCDIC/CP875.TXT", 0, "IBM EBCDIC Greek", 0, 0x6f ],
65 [ 878, "VENDORS/MISC/KOI8-R.TXT", 0, "Russian KOI8" ],
66 [ 932, "VENDORS/MICSFT/WindowsBestFit/bestfit932.txt", 0, "ANSI/OEM Japanese Shift-JIS" ],
67 [ 936, "VENDORS/MICSFT/WindowsBestFit/bestfit936.txt", 0, "ANSI/OEM Simplified Chinese GBK" ],
68 [ 949, "VENDORS/MICSFT/WindowsBestFit/bestfit949.txt", 0, "ANSI/OEM Korean Unified Hangul" ],
69 [ 950, "VENDORS/MICSFT/WindowsBestFit/bestfit950.txt", 0, "ANSI/OEM Traditional Chinese Big5" ],
70 [ 1006, "VENDORS/MISC/CP1006.TXT", 0, "IBM Arabic" ],
71 [ 1026, "VENDORS/MICSFT/EBCDIC/CP1026.TXT", 0, "IBM EBCDIC Latin 5 Turkish", 0, 0x6f ],
72 [ 1250, "VENDORS/MICSFT/WindowsBestFit/bestfit1250.txt", 0, "ANSI Eastern Europe" ],
73 [ 1251, "VENDORS/MICSFT/WindowsBestFit/bestfit1251.txt", 0, "ANSI Cyrillic" ],
74 [ 1252, "VENDORS/MICSFT/WindowsBestFit/bestfit1252.txt", 0, "ANSI Latin 1" ],
75 [ 1253, "VENDORS/MICSFT/WindowsBestFit/bestfit1253.txt", 0, "ANSI Greek" ],
76 [ 1254, "VENDORS/MICSFT/WindowsBestFit/bestfit1254.txt", 0, "ANSI Turkish" ],
77 [ 1255, "VENDORS/MICSFT/WindowsBestFit/bestfit1255.txt", 0, "ANSI Hebrew" ],
78 [ 1256, "VENDORS/MICSFT/WindowsBestFit/bestfit1256.txt", 0, "ANSI Arabic" ],
79 [ 1257, "VENDORS/MICSFT/WindowsBestFit/bestfit1257.txt", 0, "ANSI Baltic" ],
80 [ 1258, "VENDORS/MICSFT/WindowsBestFit/bestfit1258.txt", 0, "ANSI/OEM Viet Nam" ],
81 [ 1361, "VENDORS/MICSFT/WindowsBestFit/bestfit1361.txt", 0, "Korean Johab" ],
82 [ 10000, "VENDORS/MICSFT/MAC/ROMAN.TXT", 0, "Mac Roman" ],
83 [ 10001, "VENDORS/APPLE/JAPANESE.TXT", 0, "Mac Japanese" ],
84 [ 10002, "VENDORS/APPLE/CHINTRAD.TXT", 0, "Mac Traditional Chinese" ],
85 [ 10003, "VENDORS/APPLE/KOREAN.TXT", 0, "Mac Korean", 0xf8e5 ],
86 [ 10004, "VENDORS/APPLE/ARABIC.TXT", 0, "Mac Arabic", 0xf826 ],
87 [ 10005, "VENDORS/APPLE/HEBREW.TXT", 0, "Mac Hebrew", 0xf7fa ],
88 [ 10006, "VENDORS/MICSFT/MAC/GREEK.TXT", 0, "Mac Greek" ],
89 [ 10007, "VENDORS/MICSFT/MAC/CYRILLIC.TXT", 0, "Mac Cyrillic" ],
90 [ 10008, "VENDORS/APPLE/CHINSIMP.TXT", 0, "Mac Simplified Chinese" ],
91 [ 10010, "VENDORS/APPLE/ROMANIAN.TXT", 0, "Mac Romanian" ],
92 [ 10017, "VENDORS/APPLE/CYRILLIC.TXT", 0, "Mac Ukrainian" ],
93 [ 10021, "VENDORS/APPLE/THAI.TXT", 0, "Mac Thai" ],
94 [ 10029, "VENDORS/MICSFT/MAC/LATIN2.TXT", 0, "Mac Latin 2" ],
95 [ 10079, "VENDORS/MICSFT/MAC/ICELAND.TXT", 0, "Mac Icelandic" ],
96 [ 10081, "VENDORS/MICSFT/MAC/TURKISH.TXT", 0, "Mac Turkish", 0xf89f ],
97 [ 10082, "VENDORS/APPLE/CROATIAN.TXT", 0, "Mac Croatian" ],
98 [ 20127, undef, 0, "US-ASCII (7bit)" ],
99 [ 20866, "VENDORS/MISC/KOI8-R.TXT", 0, "Russian KOI8" ],
100 [ 20932, "OBSOLETE/EASTASIA/JIS/JIS0208.TXT", 0, "EUC-JP", 0, 0x3f, 0x30fb ],
101 [ 21866, "VENDORS/MISC/KOI8-U.TXT", 0, "Ukrainian KOI8" ],
102 [ 28591, "ISO8859/8859-1.TXT", 0, "ISO 8859-1 Latin 1" ],
103 [ 28592, "ISO8859/8859-2.TXT", 0, "ISO 8859-2 Latin 2 (East European)" ],
104 [ 28593, "ISO8859/8859-3.TXT", 0, "ISO 8859-3 Latin 3 (South European)", 0xf7f5 ],
105 [ 28594, "ISO8859/8859-4.TXT", 0, "ISO 8859-4 Latin 4 (Baltic old)" ],
106 [ 28595, "ISO8859/8859-5.TXT", 0, "ISO 8859-5 Cyrillic" ],
107 [ 28596, "ISO8859/8859-6.TXT", 0, "ISO 8859-6 Arabic", 0xf7c8 ],
108 [ 28597, "ISO8859/8859-7.TXT", 0, "ISO 8859-7 Greek", 0xf7c5 ],
109 [ 28598, "ISO8859/8859-8.TXT", 0, "ISO 8859-8 Hebrew", 0xf79c ],
110 [ 28599, "ISO8859/8859-9.TXT", 0, "ISO 8859-9 Latin 5 (Turkish)" ],
111 [ 28600, "ISO8859/8859-10.TXT", 0, "ISO 8859-10 Latin 6 (Nordic)" ],
112 [ 28603, "ISO8859/8859-13.TXT", 0, "ISO 8859-13 Latin 7 (Baltic)" ],
113 [ 28604, "ISO8859/8859-14.TXT", 0, "ISO 8859-14 Latin 8 (Celtic)" ],
114 [ 28605, "ISO8859/8859-15.TXT", 0, "ISO 8859-15 Latin 9 (Euro)" ],
115 [ 28606, "ISO8859/8859-16.TXT", 0, "ISO 8859-16 Latin 10 (Balkan)" ]
119 my %ctype =
121 "upper" => 0x0001,
122 "lower" => 0x0002,
123 "digit" => 0x0004,
124 "space" => 0x0008,
125 "punct" => 0x0010,
126 "cntrl" => 0x0020,
127 "blank" => 0x0040,
128 "xdigit" => 0x0080,
129 "alpha" => 0x0100,
130 "defin" => 0x0200
133 my %indic_types =
135 "Other" => 0x0000,
136 "Bindu" => 0x0001,
137 "Visarga" => 0x0002,
138 "Avagraha" => 0x0003,
139 "Nukta" => 0x0004,
140 "Virama" => 0x0005,
141 "Vowel_Independent" => 0x0006,
142 "Vowel_Dependent" => 0x0007,
143 "Vowel" => 0x0008,
144 "Consonant_Placeholder" => 0x0009,
145 "Consonant" => 0x000a,
146 "Consonant_Dead" => 0x000b,
147 "Consonant_Repha" => 0x000c,
148 "Consonant_Subjoined" => 0x000d,
149 "Consonant_Medial" => 0x000e,
150 "Consonant_Final" => 0x000f,
151 "Consonant_Head_Letter" => 0x0010,
152 "Modifying_Letter" => 0x0011,
153 "Tone_Letter" => 0x0012,
154 "Tone_Mark" => 0x0013,
155 "Register_Shifter" => 0x0014
158 my %matra_types =
160 "Right" => 0x01,
161 "Left" => 0x02,
162 "Visual_Order_Left" => 0x03,
163 "Left_And_Right" => 0x04,
164 "Top" => 0x05,
165 "Bottom" => 0x06,
166 "Top_And_Bottom" => 0x07,
167 "Top_And_Right" => 0x08,
168 "Top_And_Left" => 0x09,
169 "Top_And_Left_And_Right" => 0x0a,
170 "Bottom_And_Right" => 0x0b,
171 "Top_And_Bottom_And_Right" => 0x0c,
172 "Overstruck" => 0x0d,
173 "Invisible" => 0x0e
176 my %nameprep_flags =
178 "unassigned" => 0x01,
179 "prohibited" => 0x02,
180 "bidi_ral" => 0x04,
181 "bidi_l" => 0x08
184 my %break_types =
186 "BK" => 0x0001,
187 "CR" => 0x0002,
188 "LF" => 0x0003,
189 "CM" => 0x0004,
190 "SG" => 0x0005,
191 "GL" => 0x0006,
192 "CB" => 0x0007,
193 "SP" => 0x0008,
194 "ZW" => 0x0009,
195 "NL" => 0x000a,
196 "WJ" => 0x000b,
197 "JL" => 0x000c,
198 "JV" => 0x000d,
199 "JT" => 0x000e,
200 "H2" => 0x000f,
201 "H3" => 0x0010,
202 "XX" => 0x0011,
203 "OP" => 0x0012,
204 "CL" => 0x0013,
205 "CP" => 0x0014,
206 "QU" => 0x0015,
207 "NS" => 0x0016,
208 "EX" => 0x0017,
209 "SY" => 0x0018,
210 "IS" => 0x0019,
211 "PR" => 0x001a,
212 "PO" => 0x001b,
213 "NU" => 0x001c,
214 "AL" => 0x001d,
215 "ID" => 0x001e,
216 "IN" => 0x001f,
217 "HY" => 0x0020,
218 "BB" => 0x0021,
219 "BA" => 0x0022,
220 "SA" => 0x0023,
221 "AI" => 0x0024,
222 "B2" => 0x0025,
223 "HL" => 0x0026,
224 "CJ" => 0x0027,
225 "RI" => 0x0028,
228 my %categories =
230 "Lu" => $ctype{"defin"}|$ctype{"alpha"}|$ctype{"upper"}, # Letter, Uppercase
231 "Ll" => $ctype{"defin"}|$ctype{"alpha"}|$ctype{"lower"}, # Letter, Lowercase
232 "Lt" => $ctype{"defin"}|$ctype{"alpha"}|$ctype{"upper"}|$ctype{"lower"}, # Letter, Titlecase
233 "Mn" => $ctype{"defin"}, # Mark, Non-Spacing
234 "Mc" => $ctype{"defin"}, # Mark, Spacing Combining
235 "Me" => $ctype{"defin"}, # Mark, Enclosing
236 "Nd" => $ctype{"defin"}|$ctype{"digit"}, # Number, Decimal Digit
237 "Nl" => $ctype{"defin"}|$ctype{"alpha"}, # Number, Letter
238 "No" => $ctype{"defin"}, # Number, Other
239 "Zs" => $ctype{"defin"}|$ctype{"space"}, # Separator, Space
240 "Zl" => $ctype{"defin"}|$ctype{"space"}, # Separator, Line
241 "Zp" => $ctype{"defin"}|$ctype{"space"}, # Separator, Paragraph
242 "Cc" => $ctype{"defin"}|$ctype{"cntrl"}, # Other, Control
243 "Cf" => $ctype{"defin"}|$ctype{"cntrl"}, # Other, Format
244 "Cs" => $ctype{"defin"}, # Other, Surrogate
245 "Co" => $ctype{"defin"}, # Other, Private Use
246 "Cn" => $ctype{"defin"}, # Other, Not Assigned
247 "Lm" => $ctype{"defin"}|$ctype{"alpha"}, # Letter, Modifier
248 "Lo" => $ctype{"defin"}|$ctype{"alpha"}, # Letter, Other
249 "Pc" => $ctype{"defin"}|$ctype{"punct"}, # Punctuation, Connector
250 "Pd" => $ctype{"defin"}|$ctype{"punct"}, # Punctuation, Dash
251 "Ps" => $ctype{"defin"}|$ctype{"punct"}, # Punctuation, Open
252 "Pe" => $ctype{"defin"}|$ctype{"punct"}, # Punctuation, Close
253 "Pi" => $ctype{"defin"}|$ctype{"punct"}, # Punctuation, Initial quote
254 "Pf" => $ctype{"defin"}|$ctype{"punct"}, # Punctuation, Final quote
255 "Po" => $ctype{"defin"}|$ctype{"punct"}, # Punctuation, Other
256 "Sm" => $ctype{"defin"}, # Symbol, Math
257 "Sc" => $ctype{"defin"}, # Symbol, Currency
258 "Sk" => $ctype{"defin"}, # Symbol, Modifier
259 "So" => $ctype{"defin"} # Symbol, Other
262 # a few characters need additional categories that cannot be determined automatically
263 my %special_categories =
265 "xdigit" => [ ord('0')..ord('9'),ord('A')..ord('F'),ord('a')..ord('f'),
266 0xff10..0xff19, 0xff21..0xff26, 0xff41..0xff46 ],
267 "space" => [ 0x09..0x0d, 0x85 ],
268 "blank" => [ 0x09, 0x20, 0xa0, 0x3000, 0xfeff ],
269 "cntrl" => [ 0x070f, 0x200c, 0x200d,
270 0x200e, 0x200f, 0x202a, 0x202b, 0x202c, 0x202d, 0x202e,
271 0x206a, 0x206b, 0x206c, 0x206d, 0x206e, 0x206f, 0xfeff,
272 0xfff9, 0xfffa, 0xfffb ],
273 "punct" => [ 0x24, 0x2b, 0x3c..0x3e, 0x5e, 0x60, 0x7c, 0x7e, 0xa2..0xbe,
274 0xd7, 0xf7 ],
275 "digit" => [ 0xb2, 0xb3, 0xb9 ],
276 "lower" => [ 0x2071, 0x207f ]
279 my %directions =
281 "L" => 1, # Left-to-Right
282 "LRE" => 15, # Left-to-Right Embedding
283 "LRO" => 15, # Left-to-Right Override
284 "R" => 2, # Right-to-Left
285 "AL" => 12, # Right-to-Left Arabic
286 "RLE" => 15, # Right-to-Left Embedding
287 "RLO" => 15, # Right-to-Left Override
288 "PDF" => 15, # Pop Directional Format
289 "EN" => 3, # European Number
290 "ES" => 4, # European Number Separator
291 "ET" => 5, # European Number Terminator
292 "AN" => 6, # Arabic Number
293 "CS" => 7, # Common Number Separator
294 "NSM" => 13, # Non-Spacing Mark
295 "BN" => 14, # Boundary Neutral
296 "B" => 8, # Paragraph Separator
297 "S" => 9, # Segment Separator
298 "WS" => 10, # Whitespace
299 "ON" => 11 # Other Neutrals
302 my %joining_types =
304 "U" => 0, # Non_Joining
305 "T" => 1, # Transparent
306 "R" => 2, # Right_Joining
307 "L" => 3, # Left_Joining
308 "D" => 4, # Dual_Joining
309 "C" => 5, # Join_Causing
312 my @cp2uni = ();
313 my @lead_bytes = ();
314 my @uni2cp = ();
315 my @unicode_defaults = ();
316 my @unicode_aliases = ();
317 my @tolower_table = ();
318 my @toupper_table = ();
319 my @digitmap_table = ();
320 my @compatmap_table = ();
321 my @category_table = (0) x 65536;
322 my @joining_table = (0) x 65536;
323 my @direction_table = ();
324 my @decomp_table = ();
325 my @compose_table = ();
326 my $default_char;
327 my $default_wchar;
329 my %joining_forms =
331 "isolated" => [],
332 "final" => [],
333 "initial" => [],
334 "medial" => []
337 ################################################################
338 # fetch a unicode.org file and open it
339 sub open_data_file($$)
341 my ($base, $name) = @_;
342 (my $dir = "data/$name") =~ s/\/[^\/]+$//;
343 local *FILE;
344 unless (-f "data/$name")
346 print "Fetching $base/$name...\n";
347 system "mkdir", "-p", $dir;
348 !system "wget", "-q", "-O", "data/$name", "$base/$name" or die "cannot fetch $base/$name";
350 open FILE, "<data/$name" or die "cannot open data/$name";
351 return *FILE;
354 ################################################################
355 # read in the defaults file
356 sub READ_DEFAULTS($)
358 my $filename = shift;
359 my $start;
361 # first setup a few default mappings
363 open DEFAULTS, "$filename" or die "Cannot open $filename";
364 print "Loading $filename\n";
365 while (<DEFAULTS>)
367 next if /^\#/; # skip comments
368 next if /^$/; # skip empty lines
369 if (/^(([0-9a-fA-F]+)(,[0-9a-fA-F]+)*)\s+([0-9a-fA-F]+|'.'|none)\s+(\#.*)?/)
371 my @src = map hex, split /,/,$1;
372 my $dst = $4;
373 my $comment = $5;
374 if ($#src > 0) { push @unicode_aliases, \@src; }
375 next if ($dst eq "none");
376 $dst = ($dst =~ /\'.\'/) ? ord substr($dst,1,1) : hex $dst;
377 foreach my $src (@src)
379 die "Duplicate value" if defined($unicode_defaults[$src]);
380 $unicode_defaults[$src] = $dst;
382 next;
384 die "Unrecognized line $_\n";
386 close DEFAULTS;
388 # now build mappings from the decomposition field of the Unicode database
390 my $UNICODE_DATA = open_data_file( $UNIDATA, "UnicodeData.txt" );
391 while (<$UNICODE_DATA>)
393 # Decode the fields ...
394 my ($code, $name, $cat, $comb, $bidi,
395 $decomp, $dec, $dig, $num, $mirror,
396 $oldname, $comment, $upper, $lower, $title) = split /;/;
397 my $dst;
398 my $src = hex $code;
400 die "unknown category $cat" unless defined $categories{$cat};
401 die "unknown directionality $bidi" unless defined $directions{$bidi};
403 $category_table[$src] = $categories{$cat};
404 $direction_table[$src] = $directions{$bidi};
405 $joining_table[$src] = $joining_types{"T"} if $cat eq "Mn" || $cat eq "Me" || $cat eq "Cf";
407 if ($lower ne "")
409 $tolower_table[$src] = hex $lower;
411 if ($upper ne "")
413 $toupper_table[$src] = hex $upper;
415 if ($dec ne "")
417 $category_table[$src] |= $ctype{"digit"};
419 if ($dig ne "")
421 $digitmap_table[$src] = ord $dig;
424 # copy the category and direction for everything between First/Last pairs
425 if ($name =~ /, First>/) { $start = $src; }
426 if ($name =~ /, Last>/)
428 while ($start < $src)
430 $category_table[$start] = $category_table[$src];
431 $direction_table[$start] = $direction_table[$src];
432 $start++;
436 next if $decomp eq ""; # no decomposition, skip it
438 if ($decomp =~ /^<([a-zA-Z]+)>\s+([0-9a-fA-F]+)$/)
440 # decomposition of the form "<foo> 1234" -> use char if type is known
441 if (($src >= 0xf900 && $src < 0xfb00) || ($src >= 0xfe30 && $src < 0xfffd))
443 # Single char decomposition in the compatibility range
444 $compatmap_table[$src] = hex $2;
446 if ($1 eq "isolated" || $1 eq "final" || $1 eq "initial" || $1 eq "medial")
448 ${joining_forms{$1}}[hex $2] = $src;
449 next;
451 next unless ($1 eq "font" ||
452 $1 eq "noBreak" ||
453 $1 eq "circle" ||
454 $1 eq "super" ||
455 $1 eq "sub" ||
456 $1 eq "wide" ||
457 $1 eq "narrow" ||
458 $1 eq "compat" ||
459 $1 eq "small");
460 $dst = hex $2;
462 elsif ($decomp =~ /^<compat>\s+0020\s+([0-9a-fA-F]+)/)
464 # decomposition "<compat> 0020 1234" -> combining accent
465 $dst = hex $1;
467 elsif ($decomp =~ /^([0-9a-fA-F]+)/)
469 # decomposition contains only char values without prefix -> use first char
470 $dst = hex $1;
471 $category_table[$src] |= $category_table[$dst] if defined $category_table[$dst];
472 # store decomposition if it contains two chars
473 if ($decomp =~ /^([0-9a-fA-F]+)\s+([0-9a-fA-F]+)$/)
475 $decomp_table[$src] = [ hex $1, hex $2 ];
476 push @compose_table, [ hex $1, hex $2, $src ];
478 elsif ($decomp =~ /^(<[a-z]+>\s)*([0-9a-fA-F]+)$/ &&
479 (($src >= 0xf900 && $src < 0xfb00) || ($src >= 0xfe30 && $src < 0xfffd)))
481 # Single char decomposition in the compatibility range
482 $compatmap_table[$src] = hex $2;
485 else
487 next;
490 next if defined($unicode_defaults[$src]); # may have been set in the defaults file
492 # check for loops
493 for (my $i = $dst; ; $i = $unicode_defaults[$i])
495 die sprintf("loop detected for %04x -> %04x",$src,$dst) if $i == $src;
496 last unless defined($unicode_defaults[$i]);
498 $unicode_defaults[$src] = $dst;
500 close $UNICODE_DATA;
502 # patch the category of some special characters
504 foreach my $cat (keys %special_categories)
506 my $flag = $ctype{$cat};
507 foreach my $i (@{$special_categories{$cat}}) { $category_table[$i] |= $flag; }
512 ################################################################
513 # define a new lead byte
514 sub add_lead_byte($)
516 my $ch = shift;
517 push @lead_bytes, $ch;
518 $cp2uni[$ch] = 0;
521 ################################################################
522 # define a new char mapping
523 sub add_mapping($$)
525 my ($cp, $uni) = @_;
526 $cp2uni[$cp] = $uni unless defined($cp2uni[$cp]);
527 $uni2cp[$uni] = $cp unless defined($uni2cp[$uni]);
528 if ($cp > 0xff && !defined($cp2uni[$cp >> 8]))
530 add_lead_byte( $cp >> 8 );
534 ################################################################
535 # parse a standard codepage file
536 sub read_codepage_file($)
538 my $name = shift;
539 my $INPUT = open_data_file( $MAPPINGS, $name );
541 while (<$INPUT>)
543 next if /^\#/; # skip comments
544 next if /^$/; # skip empty lines
545 next if /\x1a/; # skip ^Z
546 next if (/^0x([0-9a-fA-F]+)\s+\#UNDEFINED/); # undefined char
548 if (/^0x([0-9a-fA-F]+)\s+\#DBCS LEAD BYTE/)
550 add_lead_byte( hex $1 );
551 next;
553 # 0x12 0x3456
554 if (/^0x([0-9a-fA-F]+)\s+0x([0-9a-fA-F]+)\s+(\#.*)?/)
556 add_mapping( hex $1, hex $2 );
557 next;
559 # 0x12 0x3456+0x7890
560 if (/^0x([0-9a-fA-F]+)\s+0x([0-9a-fA-F]+)\+.*\s+(\#.*)?/)
562 add_mapping( hex $1, hex $2 );
563 next;
565 # 0x12 <LR>+0x3456
566 if (/^0x([0-9a-fA-F]+)\s+<LR>\+0x([0-9a-fA-F]+)\s+(\#.*)?/)
568 add_mapping( hex $1, hex $2 );
569 next;
571 # 0x12 <RL>+0x3456
572 if (/^0x([0-9a-fA-F]+)\s+<RL>\+0x([0-9a-fA-F]+)\s+(\#.*)?/)
574 my $uni = hex $2;
575 add_mapping( hex $1, $uni ) if $uni >= 0x80;
576 next;
578 die "$name: Unrecognized line $_\n";
580 close $INPUT;
584 ################################################################
585 # fill input data for the 20127 (us-ascii) codepage
586 sub fill_20127_codepage()
588 for (my $i = 0; $i < 128; $i++) { $cp2uni[$i] = $uni2cp[$i] = $i; }
589 for (my $i = 128; $i < 256; $i++) { $cp2uni[$i] = $i & 0x7f; }
592 ################################################################
593 # get a mapping including glyph chars for MB_USEGLYPHCHARS
595 sub get_glyphs_mapping(@)
597 $_[0x01] = 0x263a; # (WHITE SMILING FACE)
598 $_[0x02] = 0x263b; # (BLACK SMILING FACE)
599 $_[0x03] = 0x2665; # (BLACK HEART SUIT)
600 $_[0x04] = 0x2666; # (BLACK DIAMOND SUIT)
601 $_[0x05] = 0x2663; # (BLACK CLUB SUIT)
602 $_[0x06] = 0x2660; # (BLACK SPADE SUIT)
603 $_[0x07] = 0x2022; # (BULLET)
604 $_[0x08] = 0x25d8; # (INVERSE BULLET)
605 $_[0x09] = 0x25cb; # (WHITE CIRCLE)
606 $_[0x0a] = 0x25d9; # (INVERSE WHITE CIRCLE)
607 $_[0x0b] = 0x2642; # (MALE SIGN)
608 $_[0x0c] = 0x2640; # (FEMALE SIGN)
609 $_[0x0d] = 0x266a; # (EIGHTH NOTE)
610 $_[0x0e] = 0x266b; # (BEAMED EIGHTH NOTES)
611 $_[0x0f] = 0x263c; # (WHITE SUN WITH RAYS)
612 $_[0x10] = 0x25ba; # (BLACK RIGHT-POINTING POINTER)
613 $_[0x11] = 0x25c4; # (BLACK LEFT-POINTING POINTER)
614 $_[0x12] = 0x2195; # (UP DOWN ARROW)
615 $_[0x13] = 0x203c; # (DOUBLE EXCLAMATION MARK)
616 $_[0x14] = 0x00b6; # (PILCROW SIGN)
617 $_[0x15] = 0x00a7; # (SECTION SIGN)
618 $_[0x16] = 0x25ac; # (BLACK RECTANGLE)
619 $_[0x17] = 0x21a8; # (UP DOWN ARROW WITH BASE)
620 $_[0x18] = 0x2191; # (UPWARDS ARROW)
621 $_[0x19] = 0x2193; # (DOWNWARDS ARROW)
622 $_[0x1a] = 0x2192; # (RIGHTWARDS ARROW)
623 $_[0x1b] = 0x2190; # (LEFTWARDS ARROW)
624 $_[0x1c] = 0x221f; # (RIGHT ANGLE)
625 $_[0x1d] = 0x2194; # (LEFT RIGHT ARROW)
626 $_[0x1e] = 0x25b2; # (BLACK UP-POINTING TRIANGLE)
627 $_[0x1f] = 0x25bc; # (BLACK DOWN-POINTING TRIANGLE)
628 $_[0x7f] = 0x2302; # (HOUSE)
629 return @_;
632 ################################################################
633 # build EUC-JP table from the JIS 0208 file
634 # FIXME: for proper EUC-JP we should probably read JIS 0212 too
635 # but this would require 3-byte DBCS characters
636 sub READ_JIS0208_FILE($)
638 my $name = shift;
640 # ASCII chars
641 for (my $i = 0x00; $i <= 0x7f; $i++)
643 $cp2uni[$i] = $i;
644 $uni2cp[$i] = $i;
647 # JIS X 0201 right plane
648 for (my $i = 0xa1; $i <= 0xdf; $i++)
650 $cp2uni[0x8e00 + $i] = 0xfec0 + $i;
651 $uni2cp[0xfec0 + $i] = 0x8e00 + $i;
654 # lead bytes
655 foreach my $i (0x8e, 0x8f, 0xa1 .. 0xfe)
657 push @lead_bytes,$i;
658 $cp2uni[$i] = 0;
661 # undefined chars
662 foreach my $i (0x80 .. 0x8d, 0x90 .. 0xa0, 0xff)
664 $cp2uni[$i] = $DEF_CHAR;
667 # Shift-JIS compatibility
668 $uni2cp[0x00a5] = 0x5c;
669 $uni2cp[0x203e] = 0x7e;
671 # Fix backslash conversion
672 $cp2uni[0xa1c0] = 0xff3c;
673 $uni2cp[0xff3c] = 0xa1c0;
675 my $INPUT = open_data_file( $MAPPINGS, $name );
676 while (<$INPUT>)
678 next if /^\#/; # skip comments
679 next if /^$/; # skip empty lines
680 next if /\x1a/; # skip ^Z
681 if (/^0x[0-9a-fA-F]+\s+0x([0-9a-fA-F]+)\s+0x([0-9a-fA-F]+)\s+(\#.*)?/)
683 my $cp = 0x8080 + hex $1;
684 my $uni = hex $2;
685 $cp2uni[$cp] = $uni unless defined($cp2uni[$cp]);
686 $uni2cp[$uni] = $cp unless defined($uni2cp[$uni]);
687 next;
689 die "$name: Unrecognized line $_\n";
691 close $INPUT;
695 ################################################################
696 # build the sort keys table
697 sub READ_SORTKEYS_FILE()
699 my @sortkeys = ();
700 for (my $i = 0; $i < 65536; $i++) { $sortkeys[$i] = [ -1, 0, 0, 0, 0 ] };
702 my $INPUT = open_data_file( $REPORTS, $SORTKEYS );
703 while (<$INPUT>)
705 next if /^\#/; # skip comments
706 next if /^$/; # skip empty lines
707 next if /\x1a/; # skip ^Z
708 next if /^\@version/; # skip @version header
709 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]+)\]/)
711 my ($uni,$variable) = (hex $1, $2);
712 next if $uni > 65535;
713 $sortkeys[$uni] = [ $uni, hex $3, hex $4, hex $5, hex $6 ];
714 next;
716 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]+)\]/)
718 # multiple character sequence, ignored for now
719 next;
721 die "$SORTKEYS: Unrecognized line $_\n";
723 close $INPUT;
725 # compress the keys to 32 bit:
726 # key 1 to 16 bits, key 2 to 8 bits, key 3 to 4 bits, key 4 to 1 bit
728 @sortkeys = sort { ${$a}[1] <=> ${$b}[1] or
729 ${$a}[2] <=> ${$b}[2] or
730 ${$a}[3] <=> ${$b}[3] or
731 ${$a}[4] <=> ${$b}[4] or
732 $a cmp $b; } @sortkeys;
734 my ($n2, $n3) = (1, 1);
735 my @keys = (-1, -1, -1, -1, -1 );
736 my @flatkeys = ();
738 for (my $i = 0; $i < 65536; $i++)
740 my @current = @{$sortkeys[$i]};
741 next if $current[0] == -1;
742 if ($current[1] == $keys[1])
744 if ($current[2] == $keys[2])
746 if ($current[3] == $keys[3])
748 # nothing
750 else
752 $keys[3] = $current[3];
753 $n3++;
754 die if ($n3 >= 16);
757 else
759 $keys[2] = $current[2];
760 $keys[3] = $current[3];
761 $n2++;
762 $n3 = 1;
763 die if ($n2 >= 256);
766 else
768 $keys[1] = $current[1];
769 $keys[2] = $current[2];
770 $keys[3] = $current[3];
771 $n2 = 1;
772 $n3 = 1;
775 if ($current[2]) { $current[2] = $n2; }
776 if ($current[3]) { $current[3] = $n3; }
777 if ($current[4]) { $current[4] = 1; }
779 $flatkeys[$current[0]] = ($current[1] << 16) | ($current[2] << 8) | ($current[3] << 4) | $current[4];
781 return @flatkeys;
785 ################################################################
786 # build the sort keys table
787 sub DUMP_SORTKEYS($@)
789 my ($filename, @keys) = @_;
791 # count the number of 256-key ranges that contain something
793 my @offsets = ();
794 my $ranges = 2;
795 for (my $i = 0; $i < 256; $i++) { $offsets[$i] = 256; }
796 for (my $i = 0; $i < 65536; $i++)
798 next unless defined $keys[$i];
799 $offsets[$i >> 8] = $ranges * 256;
800 $ranges++;
801 $i |= 255;
804 # output the range offsets
806 open OUTPUT,">$filename.new" or die "Cannot create $filename";
807 printf "Building $filename\n";
808 printf OUTPUT "/* Unicode collation element table */\n";
809 printf OUTPUT "/* generated from %s */\n", "$REPORTS/$SORTKEYS";
810 printf OUTPUT "/* DO NOT EDIT!! */\n\n";
812 printf OUTPUT "const unsigned int collation_table[%d] =\n{\n", $ranges*256;
813 printf OUTPUT " /* index */\n";
814 printf OUTPUT "%s,\n", DUMP_ARRAY( "0x%08x", 0, @offsets );
816 # output the default values
818 printf OUTPUT " /* defaults */\n";
819 printf OUTPUT "%s", DUMP_ARRAY( "0x%08x", 0, (0xffffffff) x 256 );
821 # output all the key ranges
823 for (my $i = 0; $i < 256; $i++)
825 next if $offsets[$i] == 256;
826 printf OUTPUT ",\n /* 0x%02x00 .. 0x%02xff */\n", $i, $i;
827 printf OUTPUT "%s", DUMP_ARRAY( "0x%08x", 0xffffffff, @keys[($i<<8) .. ($i<<8)+255] );
829 printf OUTPUT "\n};\n";
830 close OUTPUT;
831 save_file($filename);
835 ################################################################
836 # add default mappings once the file had been read
837 sub add_default_mappings($)
839 my $first_private = shift;
841 # Apply aliases
843 foreach my $alias (@unicode_aliases)
845 my $target = undef;
846 foreach my $src (@$alias)
848 if (defined($uni2cp[$src]))
850 $target = $uni2cp[$src];
851 last;
854 next unless defined($target);
856 # At least one char of the alias set is defined, set the others to the same value
857 foreach my $src (@$alias)
859 $uni2cp[$src] = $target unless defined($uni2cp[$src]);
863 # For every src -> target mapping in the defaults table,
864 # make uni2cp[src] = uni2cp[target] if uni2cp[target] is defined
866 for (my $src = 0; $src < 65536; $src++)
868 next if defined($uni2cp[$src]); # source has a definition already
869 next unless defined($unicode_defaults[$src]); # no default for this char
870 my $target = $unicode_defaults[$src];
872 # do a recursive mapping until we find a target char that is defined
873 while (!defined($uni2cp[$target]) &&
874 defined($unicode_defaults[$target])) { $target = $unicode_defaults[$target]; }
876 if (defined($uni2cp[$target])) { $uni2cp[$src] = $uni2cp[$target]; }
879 # Add a private char mapping for undefined chars
881 if ($first_private)
883 for (my $i = 32; $i < 256; $i++)
885 next if defined($cp2uni[$i]);
886 $cp2uni[$i] = $first_private;
887 $uni2cp[$first_private] = $i;
888 $first_private++;
892 # Add an identity mapping for all undefined chars
894 for (my $i = 0; $i < 256; $i++)
896 next if defined($cp2uni[$i]);
897 next if defined($uni2cp[$i]);
898 $cp2uni[$i] = $uni2cp[$i] = $i;
902 ################################################################
903 # dump an array of integers
904 sub DUMP_ARRAY($$@)
906 my ($format,$default,@array) = @_;
907 my $i;
908 my $ret = " ";
909 for ($i = 0; $i < $#array; $i++)
911 $ret .= sprintf($format, defined $array[$i] ? $array[$i] : $default);
912 $ret .= (($i % 8) != 7) ? ", " : ",\n ";
914 $ret .= sprintf($format, defined $array[$i] ? $array[$i] : $default);
915 return $ret;
919 ################################################################
920 # dump a unicode->ascii mapping table
921 sub dump_uni2cp_table($$)
923 my ($width, $def) = @_;
925 # count the number of unicode->ascii subtables that contain something
927 my @lowerbounds;
928 my @upperbounds;
929 my $index = 0;
930 my @filled = (-1) x 256;
931 for (my $i = 0; $i < 65536; $i++)
933 next unless defined $uni2cp[$i];
934 if ($filled[$i >> 8] == -1)
936 $lowerbounds[$index] = $i & 0xff;
937 $upperbounds[$index] = 0xff - $lowerbounds[$index];
938 $filled[$i >> 8] = $index * 256;
939 $index++;
941 else
943 $upperbounds[$index-1] = 0xff - ($i & 0xff);
947 # add defaults mapping at the end
948 $filled[256] = $index * 256;
949 $lowerbounds[$index] = 255;
950 $upperbounds[$index] = 0;
951 $index++;
953 # collapse blocks upwards if possible
954 my $removed = 0;
955 $index = 0;
956 for (my $i = 0; $i < 257; $i++)
958 next if $filled[$i] == -1;
959 if ($upperbounds[$index - 1] > $lowerbounds[$index])
961 $removed += $lowerbounds[$index];
963 else
965 $removed += $upperbounds[$index - 1];
966 $lowerbounds[$index] = $upperbounds[$index - 1];
968 $filled[$i] -= $removed;
969 $index++;
972 # output all the subtables into a single array
974 printf OUTPUT "static const unsigned %s uni2cp_low[%d] =\n{\n",
975 $width == 8 ? "char" : "short", ($index + 1) * 256 - $removed;
977 my $format = $width == 8 ? "0x%02x" : "0x%04x";
978 for (my $i = $index = 0; $i < 257; $i++)
980 next if $filled[$i] == -1;
981 my $start = ($i << 8) + $lowerbounds[$index];
982 my $end = ($i << 8) + 255;
983 if ($i == 256)
985 print OUTPUT " /* defaults */\n";
986 printf OUTPUT "%s\n};\n\n", DUMP_ARRAY( $format, 0, ($def) x ($end - $start + 1) );
988 else
990 printf OUTPUT " /* 0x%04x .. 0x%04x */\n", $start, $end;
991 printf OUTPUT "%s,\n", DUMP_ARRAY( $format, $def, @uni2cp[$start .. $end] );
993 $index++;
996 # output a table of the offsets of the subtables in the previous array
998 for (my $i = 0; $i < 256; $i++)
1000 if ($filled[$i] == -1) { $filled[$i] = $filled[256]; }
1002 printf OUTPUT "static const unsigned short uni2cp_high[256] =\n";
1003 printf OUTPUT "{\n%s\n};\n\n", DUMP_ARRAY( "0x%04x", 0, @filled[0..255] );
1006 ################################################################
1007 # dump an SBCS mapping table
1008 sub dump_sbcs_table($$$$$)
1010 my ($codepage, $has_glyphs, $name, $def, $defw) = @_;
1011 my $i;
1013 # output the ascii->unicode table
1015 if ($has_glyphs)
1017 printf OUTPUT "static const WCHAR cp2uni[512] =\n";
1018 printf OUTPUT "{\n%s", DUMP_ARRAY( "0x%04x", $defw, @cp2uni[0 .. 255] );
1019 printf OUTPUT ",\n /* glyphs */\n%s\n};\n\n",
1020 DUMP_ARRAY( "0x%04x", $defw, get_glyphs_mapping(@cp2uni[0 .. 255]) );
1022 else
1024 printf OUTPUT "static const WCHAR cp2uni[256] =\n";
1025 printf OUTPUT "{\n%s\n};\n\n", DUMP_ARRAY( "0x%04x", $defw, @cp2uni[0 .. 255] );
1028 dump_uni2cp_table( 8, $def );
1030 # output the code page descriptor
1032 printf OUTPUT "const struct sbcs_table cptable_%03d =\n{\n", $codepage;
1033 printf OUTPUT " { %d, 1, 0x%04x, 0x%04x, \"%s\" },\n",
1034 $codepage, $def, $defw, $name;
1035 printf OUTPUT " cp2uni,\n";
1036 if ($has_glyphs) { printf OUTPUT " cp2uni + 256,\n"; }
1037 else { printf OUTPUT " cp2uni,\n"; }
1038 printf OUTPUT " uni2cp_low,\n";
1039 printf OUTPUT " uni2cp_high\n};\n";
1043 ################################################################
1044 # dump a DBCS mapping table
1045 sub dump_dbcs_table($$$$@)
1047 my ($codepage, $name, $def, $defw, @lb_ranges) = @_;
1049 # build a list of lead bytes that are actually used
1051 my @lblist = ();
1052 LBLOOP: for (my $y = 0; $y <= $#lead_bytes; $y++)
1054 my $base = $lead_bytes[$y] << 8;
1055 for (my $x = 0; $x < 256; $x++)
1057 if (defined $cp2uni[$base+$x])
1059 push @lblist,$lead_bytes[$y];
1060 next LBLOOP;
1064 my $unused = ($#lead_bytes > $#lblist);
1066 # output the ascii->unicode table for the single byte chars
1068 printf OUTPUT "static const WCHAR cp2uni[%d] =\n", 256 * ($#lblist + 2 + $unused);
1069 printf OUTPUT "{\n%s,\n", DUMP_ARRAY( "0x%04x", $defw, @cp2uni[0 .. 255] );
1071 # output the default table for unused lead bytes
1073 if ($unused)
1075 printf OUTPUT " /* unused lead bytes */\n";
1076 printf OUTPUT "%s,\n", DUMP_ARRAY( "0x%04x", 0, ($defw) x 256 );
1079 # output the ascii->unicode table for each DBCS lead byte
1081 for (my $y = 0; $y <= $#lblist; $y++)
1083 my $base = $lblist[$y] << 8;
1084 printf OUTPUT " /* lead byte %02x */\n", $lblist[$y];
1085 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", $defw, @cp2uni[$base .. $base+255] );
1086 printf OUTPUT ($y < $#lblist) ? ",\n" : "\n};\n\n";
1089 # output the lead byte subtables offsets
1091 my @offsets = (0) x 256;
1092 for (my $x = 0; $x <= $#lblist; $x++) { $offsets[$lblist[$x]] = $x + 1; }
1093 if ($unused)
1095 # increment all lead bytes offset to take into account the unused table
1096 for (my $x = 0; $x <= $#lead_bytes; $x++) { $offsets[$lead_bytes[$x]]++; }
1098 printf OUTPUT "static const unsigned char cp2uni_leadbytes[256] =\n";
1099 printf OUTPUT "{\n%s\n};\n\n", DUMP_ARRAY( "0x%02x", 0, @offsets );
1101 dump_uni2cp_table( 16, $def );
1103 # output the code page descriptor
1105 printf OUTPUT "const struct dbcs_table cptable_%03d =\n{\n", $codepage;
1106 printf OUTPUT " { %d, 2, 0x%04x, 0x%04x, \"%s\" },\n",
1107 $codepage, $def, $defw, $name;
1108 printf OUTPUT " cp2uni,\n";
1109 printf OUTPUT " cp2uni_leadbytes,\n";
1110 printf OUTPUT " uni2cp_low,\n";
1111 printf OUTPUT " uni2cp_high,\n";
1112 printf OUTPUT " {\n %s\n }\n", DUMP_ARRAY( "0x%02x", 0, @lb_ranges, 0, 0 );
1113 printf OUTPUT "};\n";
1117 ################################################################
1118 # get the list of defined lead byte ranges
1119 sub get_lb_ranges()
1121 my @list = ();
1122 my @ranges = ();
1123 my $i = 0;
1124 foreach $i (@lead_bytes) { $list[$i] = 1; }
1125 my $on = 0;
1126 for (my $i = 0; $i < 256; $i++)
1128 if ($on)
1130 if (!defined $list[$i]) { push @ranges, $i-1; $on = 0; }
1132 else
1134 if ($list[$i]) { push @ranges, $i; $on = 1; }
1137 if ($on) { push @ranges, 0xff; }
1138 return @ranges;
1141 ################################################################
1142 # dump the Indic Syllabic Category table
1143 sub dump_indic($)
1145 my $filename = shift;
1146 my @indic_table = ($indic_types{'Other'}) x 65536;;
1148 my $INPUT = open_data_file( $UNIDATA, "IndicSyllabicCategory.txt" );
1149 while (<$INPUT>)
1151 next if /^\#/; # skip comments
1152 next if /^\s*$/; # skip empty lines
1153 next if /\x1a/; # skip ^Z
1154 if (/^\s*([0-9a-fA-F]+)\s*;\s*([a-zA-Z_]+)\s*#/)
1156 my $type = $2;
1157 die "unknown indic $type" unless defined $indic_types{$type};
1158 if (hex $1 < 65536)
1160 $indic_table[hex $1] = $indic_types{$type};
1162 next;
1164 elsif (/^\s*([0-9a-fA-F]+)..\s*([0-9a-fA-F]+)\s*;\s*([A-Za-z_]+)\s*#/)
1166 my $type = $3;
1167 die "unknown indic $type" unless defined $indic_types{$type};
1168 if (hex $1 < 65536 and hex $2 < 6536)
1170 foreach my $i (hex $1 .. hex $2)
1172 $indic_table[$i] = $indic_types{$type};
1175 next;
1177 die "malformed line $_";
1179 close $INPUT;
1181 $INPUT = open_data_file( $UNIDATA, "IndicMatraCategory.txt" );
1182 while (<$INPUT>)
1184 next if /^\#/; # skip comments
1185 next if /^\s*$/; # skip empty lines
1186 next if /\x1a/; # skip ^Z
1187 if (/^\s*([0-9a-fA-F]+)\s*;\s*([a-zA-Z]+)\s*#/)
1189 my $type = $2;
1190 die "unknown matra $type" unless defined $matra_types{$type};
1191 $indic_table[hex $1] += $matra_types{$type} << 8;
1192 next;
1194 elsif (/^\s*([0-9a-fA-F]+)..\s*([0-9a-fA-F]+)\s*;\s*([A-Za-z_]+)\s*#/)
1196 my $type = $3;
1197 die "unknown matra $type" unless defined $matra_types{$type};
1198 foreach my $i (hex $1 .. hex $2)
1200 $indic_table[$i] += $matra_types{$type} << 8;
1202 next;
1204 die "malformed line $_";
1206 close $INPUT;
1208 open OUTPUT,">$filename.new" or die "Cannot create $filename";
1209 print "Building $filename\n";
1210 print OUTPUT "/* Unicode Indic Syllabic Category */\n";
1211 print OUTPUT "/* generated from $UNIDATA/IndicSyllabicCategory.txt */\n";
1212 print OUTPUT "/* and from $UNIDATA/IndicMatraCategory.txt */\n";
1213 print OUTPUT "/* DO NOT EDIT!! */\n\n";
1214 print OUTPUT "#include \"wine/unicode.h\"\n\n";
1216 dump_two_level_mapping( "indic_syllabic_table", @indic_table);
1218 close OUTPUT;
1219 save_file($filename);
1222 ################################################################
1223 # dump the Line Break Properties table
1224 sub dump_linebreak($)
1226 my $filename = shift;
1227 my @break_table = ($break_types{'XX'}) x 65536;;
1228 my $next_group = 0;
1230 my $INPUT = open_data_file( $UNIDATA, "LineBreak.txt" );
1231 while (<$INPUT>)
1233 next if /^\#/; # skip comments
1234 next if /^\s*$/; # skip empty lines
1235 next if /\x1a/; # skip ^Z
1236 if (/^\s*([0-9a-fA-F]+)\s*;\s*([0-9A-Z][0-9A-Z])+\s*/)
1238 my $type = $2;
1239 die "unknown breaktype $type" unless defined $break_types{$type};
1240 $break_table[hex $1] = $break_types{$type};
1241 next;
1243 elsif (/^\s*([0-9a-fA-F]+)..\s*([0-9a-fA-F]+)\s*;\s*([0-9A-Z][0-9A-Z])+\s*/)
1245 my $type = $3;
1246 die "unknown breaktype $type" unless defined $break_types{$type};
1247 foreach my $i (hex $1 .. hex $2)
1249 $break_table[$i] = $break_types{$type};
1251 next;
1253 die "malformed line $_";
1255 close $INPUT;
1257 open OUTPUT,">$filename.new" or die "Cannot create $filename";
1258 print "Building $filename\n";
1259 print OUTPUT "/* Unicode Line Break Properties */\n";
1260 print OUTPUT "/* generated from $UNIDATA/LineBreak.txt */\n";
1261 print OUTPUT "/* DO NOT EDIT!! */\n\n";
1262 print OUTPUT "#include \"wine/unicode.h\"\n\n";
1264 dump_two_level_mapping( "wine_linebreak_table", @break_table);
1266 close OUTPUT;
1267 save_file($filename);
1271 ################################################################
1272 # dump the BiDi mirroring table
1273 sub dump_mirroring($)
1275 my $filename = shift;
1276 my @mirror_table = ();
1278 my $INPUT = open_data_file( $UNIDATA, "BidiMirroring.txt" );
1279 while (<$INPUT>)
1281 next if /^\#/; # skip comments
1282 next if /^$/; # skip empty lines
1283 next if /\x1a/; # skip ^Z
1284 if (/^\s*([0-9a-fA-F]+)\s*;\s*([0-9a-fA-F]+)/)
1286 $mirror_table[hex $1] = hex $2;
1287 next;
1289 die "malformed line $_";
1291 close $INPUT;
1293 open OUTPUT,">$filename.new" or die "Cannot create $filename";
1294 print "Building $filename\n";
1295 print OUTPUT "/* Unicode BiDi mirroring */\n";
1296 print OUTPUT "/* generated from $UNIDATA/BidiMirroring.txt */\n";
1297 print OUTPUT "/* DO NOT EDIT!! */\n\n";
1298 print OUTPUT "#include \"wine/unicode.h\"\n\n";
1299 DUMP_CASE_TABLE( "wine_mirror_map", @mirror_table );
1300 close OUTPUT;
1301 save_file($filename);
1305 ################################################################
1306 # dump the Arabic shaping table
1307 sub dump_shaping($)
1309 my $filename = shift;
1310 my %groups;
1311 my $next_group = 0;
1313 $groups{"No_Joining_Group"} = $next_group++;
1315 my $INPUT = open_data_file( $UNIDATA, "ArabicShaping.txt" );
1316 while (<$INPUT>)
1318 next if /^\#/; # skip comments
1319 next if /^\s*$/; # skip empty lines
1320 next if /\x1a/; # skip ^Z
1321 if (/^\s*([0-9a-fA-F]+)\s*;.*;\s*([RLDCUT])\s*;\s*(\w+)/)
1323 my $type = $2;
1324 my $group = $3;
1325 $groups{$group} = $next_group++ unless defined $groups{$group};
1326 $joining_table[hex $1] = $joining_types{$type} | ($groups{$group} << 8);
1327 next;
1329 die "malformed line $_";
1331 close $INPUT;
1333 open OUTPUT,">$filename.new" or die "Cannot create $filename";
1334 print "Building $filename\n";
1335 print OUTPUT "/* Unicode Arabic shaping */\n";
1336 print OUTPUT "/* generated from $UNIDATA/ArabicShaping.txt */\n";
1337 print OUTPUT "/* DO NOT EDIT!! */\n\n";
1338 print OUTPUT "#include \"wine/unicode.h\"\n\n";
1340 dump_two_level_mapping( "wine_shaping_table", @joining_table );
1342 print OUTPUT "\nconst unsigned short wine_shaping_forms[256][4] =\n{\n";
1343 for (my $i = 0x600; $i <= 0x6ff; $i++)
1345 printf OUTPUT " { 0x%04x, 0x%04x, 0x%04x, 0x%04x },\n",
1346 ${joining_forms{"isolated"}}[$i] || $i,
1347 ${joining_forms{"final"}}[$i] || $i,
1348 ${joining_forms{"initial"}}[$i] || $i,
1349 ${joining_forms{"medial"}}[$i] || $i;
1351 print OUTPUT "};\n";
1353 close OUTPUT;
1354 save_file($filename);
1358 ################################################################
1359 # dump the case mapping tables
1360 sub DUMP_CASE_MAPPINGS($)
1362 my $filename = shift;
1363 open OUTPUT,">$filename.new" or die "Cannot create $filename";
1364 printf "Building $filename\n";
1365 printf OUTPUT "/* Unicode case mappings */\n";
1366 printf OUTPUT "/* Automatically generated; DO NOT EDIT!! */\n\n";
1367 printf OUTPUT "#include \"wine/unicode.h\"\n\n";
1369 DUMP_CASE_TABLE( "wine_casemap_lower", @tolower_table );
1370 DUMP_CASE_TABLE( "wine_casemap_upper", @toupper_table );
1371 DUMP_CASE_TABLE( "wine_digitmap", @digitmap_table );
1372 DUMP_CASE_TABLE( "wine_compatmap", @compatmap_table );
1373 close OUTPUT;
1374 save_file($filename);
1378 ################################################################
1379 # dump a case mapping table
1380 sub DUMP_CASE_TABLE($@)
1382 my ($name,@table) = @_;
1384 # count the number of sub tables that contain something
1385 # also compute the low and upper populated bounds
1387 my @lowerbounds = ( 0, 0 );
1388 my @upperbounds = ( 0, 255 );
1389 my $index = 0;
1390 my @filled = ();
1391 for (my $i = 0; $i < 65536; $i++)
1393 next unless defined $table[$i];
1394 if (!defined $filled[$i >> 8])
1396 $lowerbounds[$index] = $i & 0xff;
1397 $upperbounds[$index] = 0xff - $lowerbounds[$index];
1398 $filled[$i >> 8] = $index * 256 + 512;
1399 $index++;
1401 else
1403 $upperbounds[$index-1] = 0xff - ($i & 0xff);
1405 $table[$i] = ($table[$i] - $i) & 0xffff;
1408 # Collapse blocks upwards if possible
1409 my $removed = 0;
1410 $index = 0;
1411 for (my $i = 0; $i < 256; $i++)
1413 next unless defined $filled[$i];
1414 if ($upperbounds[$index - 1] > $lowerbounds[$index])
1416 $removed = $removed + $lowerbounds[$index];
1418 else
1420 $removed = $removed + $upperbounds[$index - 1];
1421 $lowerbounds[$index] = $upperbounds[$index - 1];
1423 $filled[$i] = $filled[$i] - $removed;
1424 $index++;
1427 # dump the table
1429 printf OUTPUT "const WCHAR %s[%d] =\n", $name, $index * 256 + 512 - $removed;
1430 printf OUTPUT "{\n /* index */\n";
1431 printf OUTPUT "%s,\n", DUMP_ARRAY( "0x%04x", 256, @filled );
1432 printf OUTPUT " /* defaults */\n";
1433 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, (0) x 256 );
1434 $index = 0;
1435 for (my $i = 0; $i < 256; $i++)
1437 next unless $filled[$i];
1438 printf OUTPUT ",\n /* 0x%02x%02x .. 0x%02xff */\n", $i, $lowerbounds[$index], $i;
1439 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0,
1440 @table[($i<<8) + $lowerbounds[$index] .. ($i<<8)+255] );
1441 $index++;
1443 printf OUTPUT "\n};\n";
1446 ################################################################
1447 # compress a mapping table by removing identical rows
1448 sub compress_array($@)
1450 my $rows = shift;
1451 my @table = @_;
1452 my $len = @table / $rows;
1453 my @array = (0) x $rows;
1454 my %sequences;
1456 # try to merge table rows
1457 for (my $row = 0; $row < $rows; $row++)
1459 my $rowtxt = pack "S*", @table[($row * $len)..($row * $len + $len - 1)];
1460 if (defined($sequences{$rowtxt}))
1462 # reuse an existing row
1463 $array[$row] = $sequences{$rowtxt};
1465 else
1467 # create a new row
1468 $sequences{$rowtxt} = $array[$row] = $#array + 1;
1469 push @array, @table[$row * $len..$row * $len + $len - 1];
1472 return @array;
1475 ################################################################
1476 # dump a simple char -> 16-bit value mapping table
1477 sub dump_simple_mapping($@)
1479 my $name = shift;
1480 my @array = compress_array( 256, @_[0..65535] );
1482 printf OUTPUT "const unsigned short %s[%d] =\n{\n", $name, $#array+1;
1483 printf OUTPUT " /* offsets */\n%s,\n", DUMP_ARRAY( "0x%04x", 0, @array[0..255] );
1484 printf OUTPUT " /* values */\n%s\n};\n", DUMP_ARRAY( "0x%04x", 0, @array[256..$#array] );
1487 ################################################################
1488 # dump a char -> 16-bit value mapping table using two-level tables
1489 sub dump_two_level_mapping($@)
1491 my $name = shift;
1492 my @row_array = compress_array( 4096, @_[0..65535] );
1493 my @array = compress_array( 256, @row_array[0..4095] );
1495 for (my $i = 256; $i < @array; $i++) { $array[$i] += @array - 4096; }
1497 printf OUTPUT "const unsigned short %s[%d] =\n{\n", $name, @array + @row_array - 4096;
1498 printf OUTPUT " /* level 1 offsets */\n%s,\n", DUMP_ARRAY( "0x%04x", 0, @array[0..255] );
1499 printf OUTPUT " /* level 2 offsets */\n%s,\n", DUMP_ARRAY( "0x%04x", 0, @array[256..$#array] );
1500 printf OUTPUT " /* values */\n%s\n};\n", DUMP_ARRAY( "0x%04x", 0, @row_array[4096..$#row_array] );
1503 ################################################################
1504 # dump a binary case mapping table in l_intl.nls format
1505 sub dump_binary_case_table(@)
1507 my (@table) = @_;
1509 my %difftables_hash = ();
1510 my @difftables;
1511 my %offtables2_hash = ();
1512 my @offtables2 = ();
1514 my @offtable = ();
1515 for (my $i = 0; $i < 256; $i++)
1517 my @offtable2 = ();
1518 for(my $j = 0; $j < 16; $j++) # offset table for xx00-xxFF characters
1520 my @difftable;
1521 for (my $k = 0; $k < 16; $k++) # case map table for xxx0-xxxF characters
1523 my $char = ($i<<8) + ($j<<4) + $k;
1524 $difftable[$k] = (defined $table[$char]) ? (($table[$char]-$char) & 0xffff) : 0;
1527 my $diff_key = pack "S*", @difftable;
1528 my $offset3 = $difftables_hash{$diff_key};
1529 if (!defined $offset3)
1531 $offset3 = scalar @difftables;
1532 $difftables_hash{$diff_key} = $offset3;
1533 push @difftables, @difftable;
1535 $offtable2[$j] = $offset3;
1538 my $offtable2_key = pack "S*", @offtable2;
1539 my $offset2 = $offtables2_hash{$offtable2_key};
1540 if (!defined $offset2)
1542 $offset2 = scalar @offtables2;
1543 $offtables2_hash{$offtable2_key} = $offset2;
1544 push @offtables2, \@offtable2;
1546 $offtable[$i] = $offset2;
1549 my @output;
1550 my $offset = 0x100; # offset of first subtable in words
1551 foreach (@offtable)
1553 push @output, 0x10 * $_ + $offset; # offset of subtable in words
1556 $offset = 0x100 + 0x10 * scalar @offtables2; # offset of first difftable in words
1557 foreach(@offtables2)
1559 my $table = $_;
1560 foreach(@$table)
1562 push @output, $_ + $offset; # offset of difftable in words
1566 my $len = 1 + scalar @output + scalar @difftables;
1567 return pack "S<*", $len, @output, @difftables;
1571 ################################################################
1572 # dump case mappings for l_intl.nls
1573 sub dump_intl_nls($)
1575 my $filename = shift;
1576 open OUTPUT,">$filename.new" or die "Cannot create $filename";
1577 printf "Building $filename\n";
1579 binmode OUTPUT;
1580 print OUTPUT pack "S<", 1; # version
1581 print OUTPUT dump_binary_case_table( @toupper_table );
1582 print OUTPUT dump_binary_case_table( @tolower_table );
1583 close OUTPUT;
1584 save_file($filename);
1588 sub load_nameprep_range_table($$$)
1590 my ($INPUT, $val, $table_ref) = @_;
1592 while (<$INPUT>)
1594 if (/^\s*([0-9a-fA-F]+)-([0-9a-fA-F]+)/)
1596 my $last = hex $2;
1597 $last = 65535 if($last >= 65536);
1598 foreach my $i (hex $1 .. $last)
1600 $table_ref->[$i] |= $val;
1602 next;
1604 elsif (/^\s*([0-9a-fA-F]+)/)
1606 if (hex $1 < 65536)
1608 $table_ref->[hex $1] |= $val;
1610 next;
1613 return if (/End\sTable/);
1617 sub load_nameprep_map_table($$)
1619 my ($INPUT, $table_ref) = @_;
1621 while (<$INPUT>)
1623 if (/^\s*([0-9a-fA-F]+);\s;/)
1625 # special value for map to nothing
1626 $table_ref->[hex $1] = [0xffff, 0xffff, 0xffff];
1627 next;
1629 elsif (/^\s*([0-9a-fA-F]+);\s([0-9a-fA-F]+);/)
1631 $table_ref->[hex $1] = [hex $2, 0, 0];
1632 next;
1634 elsif (/^\s*([0-9a-fA-F]+);\s([0-9a-fA-F]+)\s([0-9a-fA-F]+);/)
1636 $table_ref->[hex $1] = [hex $2, hex $3, 0];
1637 next;
1639 elsif (/^\s*([0-9a-fA-F]+);\s([0-9a-fA-F]+)\s([0-9a-fA-F]+)\s([0-9a-fA-F]+);/)
1641 $table_ref->[hex $1] = [hex $2, hex $3, hex $4];
1642 next;
1645 return if (/End\sTable/);
1649 ################################################################
1650 # dump mapping table, prohibited characters set, unassigned
1651 # characters, bidirectional rules used by nameprep algorithm
1652 sub dump_nameprep($)
1654 my $filename = shift;
1655 my @mapping_table = ();
1656 my @flags_table = (0) x 65536;
1658 my $INPUT = open_data_file( $RFCS, $STRINGPREP );
1659 while (<$INPUT>)
1661 next unless /Start\sTable/;
1663 load_nameprep_range_table($INPUT, $nameprep_flags{"unassigned"}, \@flags_table) if (/A.1/);
1664 load_nameprep_range_table($INPUT, $nameprep_flags{"prohibited"}, \@flags_table) if (/C.1.2/);
1665 load_nameprep_range_table($INPUT, $nameprep_flags{"prohibited"}, \@flags_table) if (/C.2.2/);
1666 load_nameprep_range_table($INPUT, $nameprep_flags{"prohibited"}, \@flags_table) if (/C.3/);
1667 load_nameprep_range_table($INPUT, $nameprep_flags{"prohibited"}, \@flags_table) if (/C.4/);
1668 load_nameprep_range_table($INPUT, $nameprep_flags{"prohibited"}, \@flags_table) if (/C.5/);
1669 load_nameprep_range_table($INPUT, $nameprep_flags{"prohibited"}, \@flags_table) if (/C.6/);
1670 load_nameprep_range_table($INPUT, $nameprep_flags{"prohibited"}, \@flags_table) if (/C.7/);
1671 load_nameprep_range_table($INPUT, $nameprep_flags{"prohibited"}, \@flags_table) if (/C.8/);
1672 load_nameprep_range_table($INPUT, $nameprep_flags{"prohibited"}, \@flags_table) if (/C.9/);
1673 load_nameprep_range_table($INPUT, $nameprep_flags{"bidi_ral"}, \@flags_table) if (/D.1/);
1674 load_nameprep_range_table($INPUT, $nameprep_flags{"bidi_l"}, \@flags_table) if (/D.2/);
1676 load_nameprep_map_table($INPUT, \@mapping_table) if (/B.1/);
1677 load_nameprep_map_table($INPUT, \@mapping_table) if (/B.2/);
1679 close $INPUT;
1681 open OUTPUT,">$filename.new" or die "Cannot create $filename";
1682 print "Building $filename\n";
1683 print OUTPUT "/* Nameprep algorithm related data */\n";
1684 print OUTPUT "/* generated from $RFCS/$STRINGPREP */\n";
1685 print OUTPUT "/* DO NOT EDIT!! */\n\n";
1686 print OUTPUT "#include \"wine/unicode.h\"\n\n";
1688 dump_two_level_mapping( "nameprep_char_type", @flags_table );
1690 ######### mapping table
1691 # first determine all the 16-char subsets that contain something
1692 my @filled = ();
1693 my $pos = 16*3; # for the null subset
1694 for (my $i = 0; $i < 65536; $i++)
1696 next unless defined $mapping_table[$i];
1697 $filled[$i >> 4] = $pos;
1698 $pos += 16*3;
1699 $i |= 15;
1701 my $total = $pos;
1703 # now count the 256-char subsets that contain something
1704 my @filled_idx = (256) x 256;
1705 $pos = 256 + 16;
1706 for (my $i = 0; $i < 4096; $i++)
1708 next unless $filled[$i];
1709 $filled_idx[$i >> 4] = $pos;
1710 $pos += 16;
1711 $i |= 15;
1713 my $null_offset = $pos;
1714 $total += $pos;
1716 # add the index offsets to the subsets positions
1717 for (my $i = 0; $i < 4096; $i++)
1719 next unless $filled[$i];
1720 $filled[$i] += $null_offset;
1723 # dump the main index
1724 printf OUTPUT "const WCHAR nameprep_mapping[%d] =\n", $total;
1725 printf OUTPUT "{\n /* index */\n";
1726 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, @filled_idx );
1727 printf OUTPUT ",\n /* null sub-index */\n%s", DUMP_ARRAY( "0x%04x", 0, ($null_offset) x 16 );
1729 # dump the second-level indexes
1730 for (my $i = 0; $i < 256; $i++)
1732 next unless ($filled_idx[$i] > 256);
1733 my @table = @filled[($i<<4)..($i<<4)+15];
1734 for (my $j = 0; $j < 16; $j++) { $table[$j] ||= $null_offset; }
1735 printf OUTPUT ",\n /* sub-index %02x */\n", $i;
1736 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, @table );
1739 # dump the 16-char subsets
1740 printf OUTPUT ",\n /* null mapping */\n";
1741 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, (0) x 48 );
1743 for (my $i = 0; $i < 4096; $i++)
1745 next unless $filled[$i];
1746 my @table = (0) x 48;
1747 for (my $j = 0; $j < 16; $j++)
1749 if (defined $mapping_table[($i<<4) + $j])
1751 $table[3 * $j] = ${$mapping_table[($i << 4) + $j]}[0];
1752 $table[3 * $j + 1] = ${$mapping_table[($i << 4) + $j]}[1];
1753 $table[3 * $j + 2] = ${$mapping_table[($i << 4) + $j]}[2];
1756 printf OUTPUT ",\n /* 0x%03x0 .. 0x%03xf */\n", $i, $i;
1757 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, @table );
1760 printf OUTPUT "\n};\n";
1762 close OUTPUT;
1763 save_file($filename);
1766 ################################################################
1767 # dump the ctype tables
1768 sub DUMP_CTYPE_TABLES($)
1770 my $filename = shift;
1771 open OUTPUT,">$filename.new" or die "Cannot create $filename";
1772 printf "Building $filename\n";
1773 printf OUTPUT "/* Unicode ctype tables */\n";
1774 printf OUTPUT "/* Automatically generated; DO NOT EDIT!! */\n\n";
1775 printf OUTPUT "#include \"wine/unicode.h\"\n\n";
1777 # add the direction in the high 4 bits of the category
1778 for (my $i = 0; $i < 65536; $i++)
1780 $category_table[$i] |= $direction_table[$i] << 12 if defined $direction_table[$i];
1783 dump_simple_mapping( "wine_wctype_table", @category_table );
1785 close OUTPUT;
1786 save_file($filename);
1790 ################################################################
1791 # dump the char composition tables
1792 sub DUMP_COMPOSE_TABLES($)
1794 my $filename = shift;
1796 open OUTPUT,">$filename.new" or die "Cannot create $filename";
1797 printf "Building $filename\n";
1798 printf OUTPUT "/* Unicode char composition */\n";
1799 printf OUTPUT "/* Automatically generated; DO NOT EDIT!! */\n\n";
1800 printf OUTPUT "#include \"wine/unicode.h\"\n\n";
1802 ######### composition table
1804 my @filled = ();
1805 foreach my $i (@compose_table)
1807 my @comp = @$i;
1808 push @{$filled[$comp[1]]}, [ $comp[0], $comp[2] ];
1811 # count how many different second chars we have
1813 my $count = 0;
1814 for (my $i = 0; $i < 65536; $i++)
1816 next unless defined $filled[$i];
1817 $count++;
1820 # build the table of second chars and offsets
1822 my $pos = $count + 1;
1823 my @table = ();
1824 for (my $i = 0; $i < 65536; $i++)
1826 next unless defined $filled[$i];
1827 push @table, $i, $pos;
1828 $pos += @{$filled[$i]};
1830 # terminator with last position
1831 push @table, 0, $pos;
1832 printf OUTPUT "const WCHAR unicode_compose_table[0x%x] =\n{\n", 2*$pos;
1833 printf OUTPUT " /* second chars + offsets */\n%s", DUMP_ARRAY( "0x%04x", 0, @table );
1835 # build the table of first chars and mappings
1837 for (my $i = 0; $i < 65536; $i++)
1839 next unless defined $filled[$i];
1840 my @table = ();
1841 my @list = sort { $a->[0] <=> $b->[0] } @{$filled[$i]};
1842 for (my $j = 0; $j <= $#list; $j++)
1844 push @table, $list[$j][0], $list[$j][1];
1846 printf OUTPUT ",\n /* 0x%04x */\n%s", $i, DUMP_ARRAY( "0x%04x", 0, @table );
1848 printf OUTPUT "\n};\n\nconst unsigned int unicode_compose_table_size = %d;\n\n", $count;
1850 ######### decomposition table
1852 # first determine all the 16-char subsets that contain something
1854 @filled = (0) x 4096;
1855 $pos = 16*2; # for the null subset
1856 for (my $i = 0; $i < 65536; $i++)
1858 next unless defined $decomp_table[$i];
1859 $filled[$i >> 4] = $pos;
1860 $pos += 16*2;
1861 $i |= 15;
1863 my $total = $pos;
1865 # now count the 256-char subsets that contain something
1867 my @filled_idx = (256) x 256;
1868 $pos = 256 + 16;
1869 for (my $i = 0; $i < 4096; $i++)
1871 next unless $filled[$i];
1872 $filled_idx[$i >> 4] = $pos;
1873 $pos += 16;
1874 $i |= 15;
1876 my $null_offset = $pos; # null mapping
1877 $total += $pos;
1879 # add the index offsets to the subsets positions
1881 for (my $i = 0; $i < 4096; $i++)
1883 next unless $filled[$i];
1884 $filled[$i] += $null_offset;
1887 # dump the main index
1889 printf OUTPUT "const WCHAR unicode_decompose_table[%d] =\n", $total;
1890 printf OUTPUT "{\n /* index */\n";
1891 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, @filled_idx );
1892 printf OUTPUT ",\n /* null sub-index */\n%s", DUMP_ARRAY( "0x%04x", 0, ($null_offset) x 16 );
1894 # dump the second-level indexes
1896 for (my $i = 0; $i < 256; $i++)
1898 next unless ($filled_idx[$i] > 256);
1899 my @table = @filled[($i<<4)..($i<<4)+15];
1900 for (my $j = 0; $j < 16; $j++) { $table[$j] ||= $null_offset; }
1901 printf OUTPUT ",\n /* sub-index %02x */\n", $i;
1902 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, @table );
1905 # dump the 16-char subsets
1907 printf OUTPUT ",\n /* null mapping */\n";
1908 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, (0) x 32 );
1910 for (my $i = 0; $i < 4096; $i++)
1912 next unless $filled[$i];
1913 my @table = (0) x 32;
1914 for (my $j = 0; $j < 16; $j++)
1916 if (defined $decomp_table[($i<<4) + $j])
1918 $table[2 * $j] = ${$decomp_table[($i << 4) + $j]}[0];
1919 $table[2 * $j + 1] = ${$decomp_table[($i << 4) + $j]}[1];
1922 printf OUTPUT ",\n /* 0x%03x0 .. 0x%03xf */\n", $i, $i;
1923 printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, @table );
1926 printf OUTPUT "\n};\n";
1927 close OUTPUT;
1928 save_file($filename);
1931 ################################################################
1932 # output a codepage definition file from the global tables
1933 sub output_codepage_file($$$$)
1935 my ($codepage, $filename, $comment, $has_glyphs) = @_;
1937 my $output = sprintf "libs/wine/c_%03d.c", $codepage;
1938 open OUTPUT,">$output.new" or die "Cannot create $output";
1940 printf "Building %s from %s (%s)\n", $output, $filename || "hardcoded data", $comment;
1942 # dump all tables
1944 printf OUTPUT "/* code page %03d (%s) */\n", $codepage, $comment;
1945 if ($filename)
1947 print OUTPUT "/* generated from $MAPPINGS/$filename */\n";
1948 print OUTPUT "/* DO NOT EDIT!! */\n\n";
1950 else
1952 printf OUTPUT "/* Automatically generated; DO NOT EDIT!! */\n\n";
1954 printf OUTPUT "#include \"wine/unicode.h\"\n\n";
1956 if (!@lead_bytes) { dump_sbcs_table( $codepage, $has_glyphs, $comment, $default_char, $default_wchar ); }
1957 else { dump_dbcs_table( $codepage, $comment, $default_char, $default_wchar, get_lb_ranges() ); }
1958 close OUTPUT;
1959 save_file($output);
1962 ################################################################
1963 # read a "bestfit" Windows mapping file
1964 sub read_bestfit_file($)
1966 my ($filename) = @_;
1967 my $state = "";
1968 my ($codepage, $width, $count);
1969 my ($lb_cur, $lb_end);
1971 my $INPUT = open_data_file( $MAPPINGS, $filename ) or die "Cannot open $filename";
1973 while (<$INPUT>)
1975 next if /^;/; # skip comments
1976 next if /^\s*$/; # skip empty lines
1977 next if /\x1a/; # skip ^Z
1978 last if /^ENDCODEPAGE/;
1980 if (/^CODEPAGE\s+(\d+)/)
1982 $codepage = $1;
1983 next;
1985 if (/^CPINFO\s+(\d+)\s+0x([0-9a-fA-f]+)\s+0x([0-9a-fA-F]+)/)
1987 $width = $1;
1988 $default_char = hex $2;
1989 $default_wchar = hex $3;
1990 next;
1992 if (/^(MBTABLE|WCTABLE|DBCSRANGE|DBCSTABLE)\s+(\d+)/)
1994 $state = $1;
1995 $count = $2;
1996 next;
1998 if (/^0x([0-9a-fA-F]+)\s+0x([0-9a-fA-F]+)/)
2000 if ($state eq "MBTABLE")
2002 my $cp = hex $1;
2003 my $uni = hex $2;
2004 $cp2uni[$cp] = $uni unless defined($cp2uni[$cp]);
2005 next;
2007 if ($state eq "WCTABLE")
2009 my $uni = hex $1;
2010 my $cp = hex $2;
2011 $uni2cp[$uni] = $cp unless defined($uni2cp[$uni]);
2012 next;
2014 if ($state eq "DBCSRANGE")
2016 my $start = hex $1;
2017 my $end = hex $2;
2018 for (my $i = $start; $i <= $end; $i++) { add_lead_byte( $i ); }
2019 $lb_cur = $start;
2020 $lb_end = $end;
2021 next;
2023 if ($state eq "DBCSTABLE")
2025 my $mb = hex $1;
2026 my $uni = hex $2;
2027 my $cp = ($lb_cur << 8) | $mb;
2028 $cp2uni[$cp] = $uni unless defined($cp2uni[$cp]);
2029 if (!--$count)
2031 if (++$lb_cur > $lb_end) { $state = "DBCSRANGE"; }
2033 next;
2036 die "$filename: Unrecognized line $_\n";
2038 close $INPUT;
2042 ################################################################
2043 # read an input file and generate the corresponding .c file
2044 sub HANDLE_FILE(@)
2046 my ($codepage,$filename,$has_glyphs,$comment,$first_private,$def,$defw) = @_;
2048 @cp2uni = ();
2049 @lead_bytes = ();
2050 @uni2cp = ();
2051 $default_char = $def || $DEF_CHAR;
2052 $default_wchar = $defw || $DEF_CHAR;
2054 # some codepage files are special
2055 if ($codepage == 20932)
2057 READ_JIS0208_FILE( $filename );
2058 add_default_mappings( $first_private );
2060 elsif ($codepage == 20127)
2062 fill_20127_codepage();
2063 add_default_mappings( $first_private );
2065 elsif ($filename =~ /\/bestfit/)
2067 read_bestfit_file( $filename );
2069 else
2071 read_codepage_file( $filename );
2072 if ($codepage == 10001)
2074 # add Shift-JIS mappings
2075 read_bestfit_file( "VENDORS/MICSFT/WindowsBestFit/bestfit932.txt" );
2077 else
2079 add_default_mappings( $first_private );
2083 output_codepage_file( $codepage, $filename, $comment, $has_glyphs );
2087 ################################################################
2088 # save a file if modified
2089 sub save_file($)
2091 my $file = shift;
2092 if (-f $file && !system "cmp $file $file.new >/dev/null")
2094 unlink "$file.new";
2096 else
2098 rename "$file.new", "$file";
2103 ################################################################
2104 # output the list of codepage tables into the cptable.c file
2105 sub output_cptable($)
2107 my $output = shift;
2108 my @tables_decl = ();
2110 printf "Building %s\n", $output;
2112 foreach my $file (@allfiles)
2114 my ($codepage,$filename) = @$file;
2115 push @tables_decl, sprintf("extern union cptable cptable_%03d;\n",$codepage);
2118 push @tables_decl, sprintf("\nstatic const union cptable * const cptables[%d] =\n{\n",$#allfiles+1);
2119 foreach my $file (@allfiles)
2121 my ($codepage,$filename) = @$file;
2122 push @tables_decl, sprintf(" &cptable_%03d,\n", $codepage);
2124 push @tables_decl, "};";
2125 REPLACE_IN_FILE( $output, @tables_decl );
2128 ################################################################
2129 # replace the contents of a file between ### cpmap ### marks
2131 sub REPLACE_IN_FILE($@)
2133 my $name = shift;
2134 my @data = @_;
2135 my @lines = ();
2136 open(FILE,$name) or die "Can't open $name";
2137 while (<FILE>)
2139 push @lines, $_;
2140 last if /\#\#\# cpmap begin \#\#\#/;
2142 push @lines, @data;
2143 while (<FILE>)
2145 if (/\#\#\# cpmap end \#\#\#/) { push @lines, "\n", $_; last; }
2147 push @lines, <FILE>;
2148 open(FILE,">$name.new") or die "Can't modify $name";
2149 print FILE @lines;
2150 close(FILE);
2151 save_file($name);
2154 ################################################################
2155 # main routine
2157 chdir ".." if -f "./make_unicode";
2158 READ_DEFAULTS( $DEFAULTS );
2159 DUMP_CASE_MAPPINGS( "libs/wine/casemap.c" );
2160 DUMP_SORTKEYS( "libs/wine/collation.c", READ_SORTKEYS_FILE() );
2161 DUMP_COMPOSE_TABLES( "libs/wine/compose.c" );
2162 DUMP_CTYPE_TABLES( "libs/wine/wctype.c" );
2163 dump_mirroring( "dlls/usp10/mirror.c" );
2164 dump_shaping( "dlls/usp10/shaping.c" );
2165 dump_linebreak( "dlls/usp10/linebreak.c" );
2166 dump_indic( "dlls/usp10/indicsyllable.c" );
2167 dump_intl_nls("loader/l_intl.nls");
2168 dump_nameprep( "dlls/kernel32/nameprep.c" );
2170 foreach my $file (@allfiles) { HANDLE_FILE( @{$file} ); }
2172 output_cptable("libs/wine/cptable.c");
2174 exit 0;
2176 # Local Variables:
2177 # compile-command: "./make_unicode"
2178 # End: