widl: Produce __uuidof-compatible headers.
[wine] / tools / make_unicode
1 #!/usr/bin/perl -w
2 #
3 # Generate code page .c files from ftp.unicode.org descriptions
4 #
5 # Copyright 2000 Alexandre Julliard
6 #
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.
11 #
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.
16 #
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
20 #
21
22 use strict;
23
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.0.0/ucd";
27
28 # Sort keys file
29 my $SORTKEYS = "http://www.unicode.org/reports/tr10/allkeys.txt";
30
31 # Defaults mapping
32 my $DEFAULTS = "tools/unicode-defaults";
33
34 # Default char for undefined mappings
35 my $DEF_CHAR = ord '?';
36
37 my @allfiles =
38 (
39     [ 37,    "VENDORS/MICSFT/EBCDIC/CP037.TXT",   0, "IBM EBCDIC US Canada" ],
40     [ 424,   "VENDORS/MISC/CP424.TXT",            0, "IBM EBCDIC Hebrew" ],
41     [ 437,   "VENDORS/MICSFT/PC/CP437.TXT",       1, "OEM United States" ],
42     [ 500,   "VENDORS/MICSFT/EBCDIC/CP500.TXT",   0, "IBM EBCDIC International" ],
43     [ 737,   "VENDORS/MICSFT/PC/CP737.TXT",       1, "OEM Greek 437G" ],
44     [ 775,   "VENDORS/MICSFT/PC/CP775.TXT",       1, "OEM Baltic" ],
45     [ 850,   "VENDORS/MICSFT/PC/CP850.TXT",       1, "OEM Multilingual Latin 1" ],
46     [ 852,   "VENDORS/MICSFT/PC/CP852.TXT",       1, "OEM Slovak Latin 2" ],
47     [ 855,   "VENDORS/MICSFT/PC/CP855.TXT",       1, "OEM Cyrillic" ],
48     [ 856,   "VENDORS/MISC/CP856.TXT",            0, "Hebrew PC" ],
49     [ 857,   "VENDORS/MICSFT/PC/CP857.TXT",       1, "OEM Turkish" ],
50     [ 860,   "VENDORS/MICSFT/PC/CP860.TXT",       1, "OEM Portuguese" ],
51     [ 861,   "VENDORS/MICSFT/PC/CP861.TXT",       1, "OEM Icelandic" ],
52     [ 862,   "VENDORS/MICSFT/PC/CP862.TXT",       1, "OEM Hebrew" ],
53     [ 863,   "VENDORS/MICSFT/PC/CP863.TXT",       1, "OEM Canadian French" ],
54     [ 864,   "VENDORS/MICSFT/PC/CP864.TXT",       0, "OEM Arabic" ],
55     [ 865,   "VENDORS/MICSFT/PC/CP865.TXT",       1, "OEM Nordic" ],
56     [ 866,   "VENDORS/MICSFT/PC/CP866.TXT",       1, "OEM Russian" ],
57     [ 869,   "VENDORS/MICSFT/PC/CP869.TXT",       1, "OEM Greek" ],
58     [ 874,   "VENDORS/MICSFT/WindowsBestFit/bestfit874.txt",  1, "ANSI/OEM Thai" ],
59     [ 875,   "VENDORS/MICSFT/EBCDIC/CP875.TXT",               0, "IBM EBCDIC Greek" ],
60     [ 878,   "VENDORS/MISC/KOI8-R.TXT",                       0, "Russian KOI8" ],
61     [ 932,   "VENDORS/MICSFT/WindowsBestFit/bestfit932.txt",  0, "ANSI/OEM Japanese Shift-JIS" ],
62     [ 936,   "VENDORS/MICSFT/WindowsBestFit/bestfit936.txt",  0, "ANSI/OEM Simplified Chinese GBK" ],
63     [ 949,   "VENDORS/MICSFT/WindowsBestFit/bestfit949.txt",  0, "ANSI/OEM Korean Unified Hangul" ],
64     [ 950,   "VENDORS/MICSFT/WindowsBestFit/bestfit950.txt",  0, "ANSI/OEM Traditional Chinese Big5" ],
65     [ 1006,  "VENDORS/MISC/CP1006.TXT",                       0, "IBM Arabic" ],
66     [ 1026,  "VENDORS/MICSFT/EBCDIC/CP1026.TXT",              0, "IBM EBCDIC Latin 5 Turkish" ],
67     [ 1250,  "VENDORS/MICSFT/WindowsBestFit/bestfit1250.txt", 0, "ANSI Eastern Europe" ],
68     [ 1251,  "VENDORS/MICSFT/WindowsBestFit/bestfit1251.txt", 0, "ANSI Cyrillic" ],
69     [ 1252,  "VENDORS/MICSFT/WindowsBestFit/bestfit1252.txt", 0, "ANSI Latin 1" ],
70     [ 1253,  "VENDORS/MICSFT/WindowsBestFit/bestfit1253.txt", 0, "ANSI Greek" ],
71     [ 1254,  "VENDORS/MICSFT/WindowsBestFit/bestfit1254.txt", 0, "ANSI Turkish" ],
72     [ 1255,  "VENDORS/MICSFT/WindowsBestFit/bestfit1255.txt", 0, "ANSI Hebrew" ],
73     [ 1256,  "VENDORS/MICSFT/WindowsBestFit/bestfit1256.txt", 0, "ANSI Arabic" ],
74     [ 1257,  "VENDORS/MICSFT/WindowsBestFit/bestfit1257.txt", 0, "ANSI Baltic" ],
75     [ 1258,  "VENDORS/MICSFT/WindowsBestFit/bestfit1258.txt", 0, "ANSI/OEM Viet Nam" ],
76     [ 1361,  "OBSOLETE/EASTASIA/KSC/JOHAB.TXT",   0, "Korean Johab" ],
77     [ 10000, "VENDORS/MICSFT/MAC/ROMAN.TXT",      0, "Mac Roman" ],
78     [ 10006, "VENDORS/MICSFT/MAC/GREEK.TXT",      0, "Mac Greek" ],
79     [ 10007, "VENDORS/MICSFT/MAC/CYRILLIC.TXT",   0, "Mac Cyrillic" ],
80     [ 10029, "VENDORS/MICSFT/MAC/LATIN2.TXT",     0, "Mac Latin 2" ],
81     [ 10079, "VENDORS/MICSFT/MAC/ICELAND.TXT",    0, "Mac Icelandic" ],
82     [ 10081, "VENDORS/MICSFT/MAC/TURKISH.TXT",    0, "Mac Turkish" ],
83     [ 20127, undef,                               0, "US-ASCII (7bit)" ],
84     [ 20866, "VENDORS/MISC/KOI8-R.TXT",           0, "Russian KOI8" ],
85     [ 20932, "OBSOLETE/EASTASIA/JIS/JIS0208.TXT", 0, "EUC-JP" ],
86     [ 21866, "VENDORS/MISC/KOI8-U.TXT",           0, "Ukrainian KOI8" ],
87     [ 28591, "ISO8859/8859-1.TXT",                0, "ISO 8859-1 Latin 1" ],
88     [ 28592, "ISO8859/8859-2.TXT",                0, "ISO 8859-2 Latin 2 (East European)" ],
89     [ 28593, "ISO8859/8859-3.TXT",                0, "ISO 8859-3 Latin 3 (South European)" ],
90     [ 28594, "ISO8859/8859-4.TXT",                0, "ISO 8859-4 Latin 4 (Baltic old)" ],
91     [ 28595, "ISO8859/8859-5.TXT",                0, "ISO 8859-5 Cyrillic" ],
92     [ 28596, "ISO8859/8859-6.TXT",                0, "ISO 8859-6 Arabic" ],
93     [ 28597, "ISO8859/8859-7.TXT",                0, "ISO 8859-7 Greek" ],
94     [ 28598, "ISO8859/8859-8.TXT",                0, "ISO 8859-8 Hebrew" ],
95     [ 28599, "ISO8859/8859-9.TXT",                0, "ISO 8859-9 Latin 5 (Turkish)" ],
96     [ 28600, "ISO8859/8859-10.TXT",               0, "ISO 8859-10 Latin 6 (Nordic)" ],
97     [ 28603, "ISO8859/8859-13.TXT",               0, "ISO 8859-13 Latin 7 (Baltic)" ],
98     [ 28604, "ISO8859/8859-14.TXT",               0, "ISO 8859-14 Latin 8 (Celtic)" ],
99     [ 28605, "ISO8859/8859-15.TXT",               0, "ISO 8859-15 Latin 9 (Euro)" ],
100     [ 28606, "ISO8859/8859-16.TXT",               0, "ISO 8859-16 Latin 10 (Balkan)" ]
101 );
102
103
104 my %ctype =
105 (
106     "upper"  => 0x0001,
107     "lower"  => 0x0002,
108     "digit"  => 0x0004,
109     "space"  => 0x0008,
110     "punct"  => 0x0010,
111     "cntrl"  => 0x0020,
112     "blank"  => 0x0040,
113     "xdigit" => 0x0080,
114     "alpha"  => 0x0100,
115     "defin"  => 0x0200
116 );
117
118 my %indic_types =
119 (
120     "Other"    => 0x0000,
121     "Bindu"    => 0x0001,
122     "Visarga"  => 0x0002,
123     "Avagraha" => 0x0003,
124     "Nukta"    => 0x0004,
125     "Virama"   => 0x0005,
126     "Vowel_Independent"  => 0x0006,
127     "Vowel_Dependent"  => 0x0007,
128     "Vowel"  => 0x0008,
129     "Consonant_Placeholder"  => 0x0009,
130     "Consonant"  => 0x000a,
131     "Consonant_Dead"  => 0x000b,
132     "Consonant_Repha"  => 0x000c,
133     "Consonant_Subjoined"  => 0x000d,
134     "Consonant_Medial"  => 0x000e,
135     "Consonant_Final"  => 0x000f,
136     "Consonant_Head_Letter"  => 0x0010,
137     "Modifying_Letter"  => 0x0011,
138     "Tone_Letter"  => 0x0012,
139     "Tone_Mark"  => 0x0013,
140     "Register_Shifter"  => 0x0014
141 );
142
143 my %matra_types =
144 (
145     "Right"    => 0x01,
146     "Left"  => 0x02,
147     "Visual_Order_Left" => 0x03,
148     "Left_And_Right"    => 0x04,
149     "Top"   => 0x05,
150     "Bottom"  => 0x06,
151     "Top_And_Bottom"  => 0x07,
152     "Top_And_Right"  => 0x08,
153     "Top_And_Left"  => 0x09,
154     "Top_And_Left_And_Right"  => 0x0a,
155     "Bottom_And_Right"  => 0x0b,
156     "Top_And_Bottom_And_Right"  => 0x0c,
157     "Overstruck"  => 0x0d,
158     "Invisible"  => 0x0e
159 );
160
161 my %break_types =
162 (
163     "BK"  => 0x0001,
164     "CR"  => 0x0002,
165     "LF"  => 0x0003,
166     "CM"  => 0x0004,
167     "SG"  => 0x0005,
168     "GL"  => 0x0006,
169     "CB"  => 0x0007,
170     "SP"  => 0x0008,
171     "ZW"  => 0x0009,
172     "NL"  => 0x000a,
173     "WJ"  => 0x000b,
174     "JL"  => 0x000c,
175     "JV"  => 0x000d,
176     "JT"  => 0x000e,
177     "H2"  => 0x000f,
178     "H3"  => 0x0010,
179     "XX"  => 0x0011,
180     "OP"  => 0x0012,
181     "CL"  => 0x0013,
182     "CP"  => 0x0014,
183     "QU"  => 0x0015,
184     "NS"  => 0x0016,
185     "EX"  => 0x0017,
186     "SY"  => 0x0018,
187     "IS"  => 0x0019,
188     "PR"  => 0x001a,
189     "PO"  => 0x001b,
190     "NU"  => 0x001c,
191     "AL"  => 0x001d,
192     "ID"  => 0x001e,
193     "IN"  => 0x001f,
194     "HY"  => 0x0020,
195     "BB"  => 0x0021,
196     "BA"  => 0x0022,
197     "SA"  => 0x0023,
198     "AI"  => 0x0024,
199     "B2"  => 0x0025
200 );
201
202 my %categories =
203 (
204     "Lu" => $ctype{"defin"}|$ctype{"alpha"}|$ctype{"upper"}, # Letter, Uppercase
205     "Ll" => $ctype{"defin"}|$ctype{"alpha"}|$ctype{"lower"}, # Letter, Lowercase
206     "Lt" => $ctype{"defin"}|$ctype{"alpha"}|$ctype{"upper"}|$ctype{"lower"},    # Letter, Titlecase
207     "Mn" => $ctype{"defin"},                    # Mark, Non-Spacing
208     "Mc" => $ctype{"defin"},                    # Mark, Spacing Combining
209     "Me" => $ctype{"defin"},                    # Mark, Enclosing
210     "Nd" => $ctype{"defin"}|$ctype{"digit"},    # Number, Decimal Digit
211     "Nl" => $ctype{"defin"}|$ctype{"alpha"},    # Number, Letter
212     "No" => $ctype{"defin"},                    # Number, Other
213     "Zs" => $ctype{"defin"}|$ctype{"space"},    # Separator, Space
214     "Zl" => $ctype{"defin"}|$ctype{"space"},    # Separator, Line
215     "Zp" => $ctype{"defin"}|$ctype{"space"},    # Separator, Paragraph
216     "Cc" => $ctype{"defin"}|$ctype{"cntrl"},    # Other, Control
217     "Cf" => $ctype{"defin"}|$ctype{"cntrl"},    # Other, Format
218     "Cs" => $ctype{"defin"},                    # Other, Surrogate
219     "Co" => $ctype{"defin"},                    # Other, Private Use
220     "Cn" => $ctype{"defin"},                    # Other, Not Assigned
221     "Lm" => $ctype{"defin"}|$ctype{"alpha"},    # Letter, Modifier
222     "Lo" => $ctype{"defin"}|$ctype{"alpha"},    # Letter, Other
223     "Pc" => $ctype{"defin"}|$ctype{"punct"},    # Punctuation, Connector
224     "Pd" => $ctype{"defin"}|$ctype{"punct"},    # Punctuation, Dash
225     "Ps" => $ctype{"defin"}|$ctype{"punct"},    # Punctuation, Open
226     "Pe" => $ctype{"defin"}|$ctype{"punct"},    # Punctuation, Close
227     "Pi" => $ctype{"defin"}|$ctype{"punct"},    # Punctuation, Initial quote
228     "Pf" => $ctype{"defin"}|$ctype{"punct"},    # Punctuation, Final quote
229     "Po" => $ctype{"defin"}|$ctype{"punct"},    # Punctuation, Other
230     "Sm" => $ctype{"defin"},                    # Symbol, Math
231     "Sc" => $ctype{"defin"},                    # Symbol, Currency
232     "Sk" => $ctype{"defin"},                    # Symbol, Modifier
233     "So" => $ctype{"defin"}                     # Symbol, Other
234 );
235
236 # a few characters need additional categories that cannot be determined automatically
237 my %special_categories =
238 (
239     "xdigit" => [ ord('0')..ord('9'),ord('A')..ord('F'),ord('a')..ord('f'),
240                   0xff10..0xff19, 0xff21..0xff26, 0xff41..0xff46 ],
241     "space"  => [ 0x09..0x0d, 0x85 ],
242     "blank"  => [ 0x09, 0x20, 0xa0, 0x3000, 0xfeff ],
243     "cntrl"  => [ 0x070f, 0x200c, 0x200d,
244                   0x200e, 0x200f, 0x202a, 0x202b, 0x202c, 0x202d, 0x202e,
245                   0x206a, 0x206b, 0x206c, 0x206d, 0x206e, 0x206f, 0xfeff,
246                   0xfff9, 0xfffa, 0xfffb ],
247     "punct"  => [ 0x24, 0x2b, 0x3c..0x3e, 0x5e, 0x60, 0x7c, 0x7e, 0xa2..0xbe,
248                   0xd7, 0xf7 ],
249     "digit"  => [ 0xb2, 0xb3, 0xb9 ],
250     "lower"  => [ 0x2071, 0x207f ]
251 );
252
253 my %directions =
254 (
255     "L"   => 1,    # Left-to-Right
256     "LRE" => 15,   # Left-to-Right Embedding
257     "LRO" => 15,   # Left-to-Right Override
258     "R"   => 2,    # Right-to-Left
259     "AL"  => 12,   # Right-to-Left Arabic
260     "RLE" => 15,   # Right-to-Left Embedding
261     "RLO" => 15,   # Right-to-Left Override
262     "PDF" => 15,   # Pop Directional Format
263     "EN"  => 3,    # European Number
264     "ES"  => 4,    # European Number Separator
265     "ET"  => 5,    # European Number Terminator
266     "AN"  => 6,    # Arabic Number
267     "CS"  => 7,    # Common Number Separator
268     "NSM" => 13,   # Non-Spacing Mark
269     "BN"  => 14,   # Boundary Neutral
270     "B"   => 8,    # Paragraph Separator
271     "S"   => 9,    # Segment Separator
272     "WS"  => 10,   # Whitespace
273     "ON"  => 11    # Other Neutrals
274 );
275
276 my %joining_types =
277 (
278    "U" => 0,    # Non_Joining
279    "T" => 1,    # Transparent
280    "R" => 2,    # Right_Joining
281    "L" => 3,    # Left_Joining
282    "D" => 4,    # Dual_Joining
283    "C" => 5,    # Join_Causing
284 );
285
286 my @cp2uni = ();
287 my @lead_bytes = ();
288 my @uni2cp = ();
289 my @unicode_defaults = ();
290 my @unicode_aliases = ();
291 my @tolower_table = ();
292 my @toupper_table = ();
293 my @digitmap_table = ();
294 my @compatmap_table = ();
295 my @category_table = (0) x 65536;
296 my @joining_table = (0) x 65536;
297 my @direction_table = ();
298 my @decomp_table = ();
299 my @compose_table = ();
300
301 my %joining_forms =
302 (
303    "isolated" => [],
304    "final" => [],
305    "initial" => [],
306    "medial" => []
307 );
308
309 ################################################################
310 # fetch a unicode.org file and open it
311 sub open_data_file($)
312 {
313     my $url = shift;
314     (my $name = $url) =~ s/^.*\///;
315     local *FILE;
316     unless (-f "data/$name")
317     {
318         print "Fetching $url...\n";
319         mkdir "data";
320         !system "wget", "-q", "-O", "data/$name", $url or die "cannot fetch $url";
321     }
322     open FILE, "<data/$name" or die "cannot open data/$name";
323     return *FILE;
324 }
325
326 ################################################################
327 # read in the defaults file
328 sub READ_DEFAULTS($)
329 {
330     my $filename = shift;
331     my $start;
332
333     # first setup a few default mappings
334
335     open DEFAULTS, "$filename" or die "Cannot open $filename";
336     print "Loading $filename\n";
337     while (<DEFAULTS>)
338     {
339         next if /^\#/;  # skip comments
340         next if /^$/;  # skip empty lines
341         if (/^(([0-9a-fA-F]+)(,[0-9a-fA-F]+)*)\s+([0-9a-fA-F]+|'.'|none)\s+(\#.*)?/)
342         {
343             my @src = map hex, split /,/,$1;
344             my $dst = $4;
345             my $comment = $5;
346             if ($#src > 0) { push @unicode_aliases, \@src; }
347             next if ($dst eq "none");
348             $dst = ($dst =~ /\'.\'/) ? ord substr($dst,1,1) : hex $dst;
349             foreach my $src (@src)
350             {
351                 die "Duplicate value" if defined($unicode_defaults[$src]);
352                 $unicode_defaults[$src] = $dst;
353             }
354             next;
355         }
356         die "Unrecognized line $_\n";
357     }
358     close DEFAULTS;
359
360     # now build mappings from the decomposition field of the Unicode database
361
362     my $UNICODE_DATA = open_data_file "$UNIDATA/UnicodeData.txt";
363     while (<$UNICODE_DATA>)
364     {
365         # Decode the fields ...
366         my ($code, $name, $cat, $comb, $bidi,
367             $decomp, $dec, $dig, $num, $mirror,
368             $oldname, $comment, $upper, $lower, $title) = split /;/;
369         my $dst;
370         my $src = hex $code;
371
372         die "unknown category $cat" unless defined $categories{$cat};
373         die "unknown directionality $bidi" unless defined $directions{$bidi};
374
375         $category_table[$src] = $categories{$cat};
376         $direction_table[$src] = $directions{$bidi};
377         $joining_table[$src] = $joining_types{"T"} if $cat eq "Mn" || $cat eq "Me" || $cat eq "Cf";
378
379         if ($lower ne "")
380         {
381             $tolower_table[$src] = hex $lower;
382         }
383         if ($upper ne "")
384         {
385             $toupper_table[$src] = hex $upper;
386         }
387         if ($dec ne "")
388         {
389             $category_table[$src] |= $ctype{"digit"};
390         }
391         if ($dig ne "")
392         {
393             $digitmap_table[$src] = ord $dig;
394         }
395
396         # copy the category and direction for everything between First/Last pairs
397         if ($name =~ /, First>/) { $start = $src; }
398         if ($name =~ /, Last>/)
399         {
400             while ($start < $src)
401             {
402                 $category_table[$start] = $category_table[$src];
403                 $direction_table[$start] = $direction_table[$src];
404                 $start++;
405             }
406         }
407
408         next if $decomp eq "";  # no decomposition, skip it
409
410         if ($decomp =~ /^<([a-zA-Z]+)>\s+([0-9a-fA-F]+)$/)
411         {
412             # decomposition of the form "<foo> 1234" -> use char if type is known
413             if (($src >= 0xf900 && $src < 0xfb00) || ($src >= 0xfe30 && $src < 0xfffd))
414             {
415                 # Single char decomposition in the compatibility range
416                 $compatmap_table[$src] = hex $2;
417             }
418             if ($1 eq "isolated" || $1 eq "final" || $1 eq "initial" || $1 eq "medial")
419             {
420                 ${joining_forms{$1}}[hex $2] = $src;
421                 next;
422             }
423             next unless ($1 eq "font" ||
424                          $1 eq "noBreak" ||
425                          $1 eq "circle" ||
426                          $1 eq "super" ||
427                          $1 eq "sub" ||
428                          $1 eq "wide" ||
429                          $1 eq "narrow" ||
430                          $1 eq "compat" ||
431                          $1 eq "small");
432             $dst = hex $2;
433         }
434         elsif ($decomp =~ /^<compat>\s+0020\s+([0-9a-fA-F]+)/)
435         {
436             # decomposition "<compat> 0020 1234" -> combining accent
437             $dst = hex $1;
438         }
439         elsif ($decomp =~ /^([0-9a-fA-F]+)/)
440         {
441             # decomposition contains only char values without prefix -> use first char
442             $dst = hex $1;
443             $category_table[$src] |= $category_table[$dst] if defined $category_table[$dst];
444             # store decomposition if it contains two chars
445             if ($decomp =~ /^([0-9a-fA-F]+)\s+([0-9a-fA-F]+)$/)
446             {
447                 $decomp_table[$src] = [ hex $1, hex $2 ];
448                 push @compose_table, [ hex $1, hex $2, $src ];
449             }
450             elsif ($decomp =~ /^(<[a-z]+>\s)*([0-9a-fA-F]+)$/ &&
451                    (($src >= 0xf900 && $src < 0xfb00) || ($src >= 0xfe30 && $src < 0xfffd)))
452             {
453                 # Single char decomposition in the compatibility range
454                 $compatmap_table[$src] = hex $2;
455             }
456         }
457         else
458         {
459             next;
460         }
461
462         next if defined($unicode_defaults[$src]);  # may have been set in the defaults file
463
464         # check for loops
465         for (my $i = $dst; ; $i = $unicode_defaults[$i])
466         {
467             die sprintf("loop detected for %04x -> %04x",$src,$dst) if $i == $src;
468             last unless defined($unicode_defaults[$i]);
469         }
470         $unicode_defaults[$src] = $dst;
471     }
472     close $UNICODE_DATA;
473
474     # patch the category of some special characters
475
476     foreach my $cat (keys %special_categories)
477     {
478         my $flag = $ctype{$cat};
479         foreach my $i (@{$special_categories{$cat}}) { $category_table[$i] |= $flag; }
480     }
481 }
482
483
484 ################################################################
485 # parse the input file
486 sub READ_FILE($)
487 {
488     my $name = shift;
489     my $INPUT = open_data_file $name;
490
491     while (<$INPUT>)
492     {
493         next if /^\#/;  # skip comments
494         next if /^$/;  # skip empty lines
495         next if /\x1a/;  # skip ^Z
496         next if (/^0x([0-9a-fA-F]+)\s+\#UNDEFINED/);  # undefined char
497
498         if (/^0x([0-9a-fA-F]+)\s+\#DBCS LEAD BYTE/)
499         {
500             my $cp = hex $1;
501             push @lead_bytes,$cp;
502             $cp2uni[$cp] = 0;
503             next;
504         }
505         if (/^0x([0-9a-fA-F]+)\s+0x([0-9a-fA-F]+)\s+(\#.*)?/)
506         {
507             my $cp = hex $1;
508             my $uni = hex $2;
509             $cp2uni[$cp] = $uni unless defined($cp2uni[$cp]);
510             $uni2cp[$uni] = $cp unless defined($uni2cp[$uni]);
511             if ($cp > 0xff && !defined($cp2uni[$cp >> 8]))
512             {
513                 push @lead_bytes,$cp >> 8;
514                 $cp2uni[$cp >> 8] = 0;
515             }
516             next;
517         }
518         die "$name: Unrecognized line $_\n";
519     }
520     close $INPUT;
521 }
522
523
524 ################################################################
525 # fill input data for the 20127 (us-ascii) codepage
526 sub fill_20127_codepage()
527 {
528     for (my $i = 0; $i < 128; $i++) { $cp2uni[$i] = $uni2cp[$i] = $i; }
529     for (my $i = 128; $i < 256; $i++) { $cp2uni[$i] = $i & 0x7f; }
530 }
531
532 ################################################################
533 # get a mapping including glyph chars for MB_USEGLYPHCHARS
534
535 sub get_glyphs_mapping(@)
536 {
537     $_[0x01] = 0x263a;  # (WHITE SMILING FACE)
538     $_[0x02] = 0x263b;  # (BLACK SMILING FACE)
539     $_[0x03] = 0x2665;  # (BLACK HEART SUIT)
540     $_[0x04] = 0x2666;  # (BLACK DIAMOND SUIT)
541     $_[0x05] = 0x2663;  # (BLACK CLUB SUIT)
542     $_[0x06] = 0x2660;  # (BLACK SPADE SUIT)
543     $_[0x07] = 0x2022;  # (BULLET)
544     $_[0x08] = 0x25d8;  # (INVERSE BULLET)
545     $_[0x09] = 0x25cb;  # (WHITE CIRCLE)
546     $_[0x0a] = 0x25d9;  # (INVERSE WHITE CIRCLE)
547     $_[0x0b] = 0x2642;  # (MALE SIGN)
548     $_[0x0c] = 0x2640;  # (FEMALE SIGN)
549     $_[0x0d] = 0x266a;  # (EIGHTH NOTE)
550     $_[0x0e] = 0x266b;  # (BEAMED EIGHTH NOTES)
551     $_[0x0f] = 0x263c;  # (WHITE SUN WITH RAYS)
552     $_[0x10] = 0x25ba;  # (BLACK RIGHT-POINTING POINTER)
553     $_[0x11] = 0x25c4;  # (BLACK LEFT-POINTING POINTER)
554     $_[0x12] = 0x2195;  # (UP DOWN ARROW)
555     $_[0x13] = 0x203c;  # (DOUBLE EXCLAMATION MARK)
556     $_[0x14] = 0x00b6;  # (PILCROW SIGN)
557     $_[0x15] = 0x00a7;  # (SECTION SIGN)
558     $_[0x16] = 0x25ac;  # (BLACK RECTANGLE)
559     $_[0x17] = 0x21a8;  # (UP DOWN ARROW WITH BASE)
560     $_[0x18] = 0x2191;  # (UPWARDS ARROW)
561     $_[0x19] = 0x2193;  # (DOWNWARDS ARROW)
562     $_[0x1a] = 0x2192;  # (RIGHTWARDS ARROW)
563     $_[0x1b] = 0x2190;  # (LEFTWARDS ARROW)
564     $_[0x1c] = 0x221f;  # (RIGHT ANGLE)
565     $_[0x1d] = 0x2194;  # (LEFT RIGHT ARROW)
566     $_[0x1e] = 0x25b2;  # (BLACK UP-POINTING TRIANGLE)
567     $_[0x1f] = 0x25bc;  # (BLACK DOWN-POINTING TRIANGLE)
568     $_[0x7f] = 0x2302;  # (HOUSE)
569     return @_;
570 }
571
572 ################################################################
573 # build EUC-JP table from the JIS 0208 file
574 # FIXME: for proper EUC-JP we should probably read JIS 0212 too
575 # but this would require 3-byte DBCS characters
576 sub READ_JIS0208_FILE($)
577 {
578     my $name = shift;
579
580     # ASCII chars
581     for (my $i = 0x00; $i <= 0x7f; $i++)
582     {
583         $cp2uni[$i] = $i;
584         $uni2cp[$i] = $i;
585     }
586
587     # JIS X 0201 right plane
588     for (my $i = 0xa1; $i <= 0xdf; $i++)
589     {
590         $cp2uni[0x8e00 + $i] = 0xfec0 + $i;
591         $uni2cp[0xfec0 + $i] = 0x8e00 + $i;
592     }
593
594     # lead bytes
595     foreach my $i (0x8e, 0x8f, 0xa1 .. 0xfe)
596     {
597         push @lead_bytes,$i;
598         $cp2uni[$i] = 0;
599     }
600
601     # undefined chars
602     foreach my $i (0x80 .. 0x8d, 0x90 .. 0xa0, 0xff)
603     {
604         $cp2uni[$i] = $DEF_CHAR;
605     }
606
607     # Shift-JIS compatibility
608     $uni2cp[0x00a5] = 0x5c;
609     $uni2cp[0x203e] = 0x7e;
610
611     # Fix backslash conversion
612     $cp2uni[0xa1c0] = 0xff3c;
613     $uni2cp[0xff3c] = 0xa1c0;
614
615     my $INPUT = open_data_file $name;
616     while (<$INPUT>)
617     {
618         next if /^\#/;  # skip comments
619         next if /^$/;  # skip empty lines
620         next if /\x1a/;  # skip ^Z
621         if (/^0x[0-9a-fA-F]+\s+0x([0-9a-fA-F]+)\s+0x([0-9a-fA-F]+)\s+(\#.*)?/)
622         {
623             my $cp = 0x8080 + hex $1;
624             my $uni = hex $2;
625             $cp2uni[$cp] = $uni unless defined($cp2uni[$cp]);
626             $uni2cp[$uni] = $cp unless defined($uni2cp[$uni]);
627             next;
628         }
629         die "$name: Unrecognized line $_\n";
630     }
631     close $INPUT;
632 }
633
634
635 ################################################################
636 # build the sort keys table
637 sub READ_SORTKEYS_FILE()
638 {
639     my @sortkeys = ();
640     for (my $i = 0; $i < 65536; $i++) { $sortkeys[$i] = [ -1, 0, 0, 0, 0 ] };
641
642     my $INPUT = open_data_file $SORTKEYS;
643     while (<$INPUT>)
644     {
645         next if /^\#/;  # skip comments
646         next if /^$/;  # skip empty lines
647         next if /\x1a/;  # skip ^Z
648         next if /^\@version/;  # skip @version header
649         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]+)\]/)
650         {
651             my ($uni,$variable) = (hex $1, $2);
652             next if $uni > 65535;
653             $sortkeys[$uni] = [ $uni, hex $3, hex $4, hex $5, hex $6 ];
654             next;
655         }
656         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]+)\]/)
657         {
658             # multiple character sequence, ignored for now
659             next;
660         }
661         die "$SORTKEYS: Unrecognized line $_\n";
662     }
663     close $INPUT;
664
665     # compress the keys to 32 bit:
666     # key 1 to 16 bits, key 2 to 8 bits, key 3 to 4 bits, key 4 to 1 bit
667
668     @sortkeys = sort { ${$a}[1] <=> ${$b}[1] or 
669                        ${$a}[2] <=> ${$b}[2] or
670                        ${$a}[3] <=> ${$b}[3] or
671                        ${$a}[4] <=> ${$b}[4] or
672                        $a cmp $b; } @sortkeys;
673
674     my ($n2, $n3) = (1, 1);
675     my @keys = (-1, -1, -1, -1, -1 );
676     my @flatkeys = ();
677
678     for (my $i = 0; $i < 65536; $i++)
679     {
680         my @current = @{$sortkeys[$i]};
681         next if $current[0] == -1;
682         if ($current[1] == $keys[1])
683         {
684             if ($current[2] == $keys[2])
685             {
686                 if ($current[3] == $keys[3])
687                 {
688                     # nothing
689                 }
690                 else
691                 {
692                     $keys[3] = $current[3];
693                     $n3++;
694                     die if ($n3 >= 16);
695                 }
696             }
697             else
698             {
699                 $keys[2] = $current[2];
700                 $keys[3] = $current[3];
701                 $n2++;
702                 $n3 = 1;
703                 die if ($n2 >= 256);
704             }
705         }
706         else
707         {
708             $keys[1] = $current[1];
709             $keys[2] = $current[2];
710             $keys[3] = $current[3];
711             $n2 = 1;
712             $n3 = 1;
713         }
714
715         if ($current[2]) { $current[2] = $n2; }
716         if ($current[3]) { $current[3] = $n3; }
717         if ($current[4]) { $current[4] = 1; }
718
719         $flatkeys[$current[0]] = ($current[1] << 16) | ($current[2] << 8) | ($current[3] << 4) | $current[4];
720     }
721     return @flatkeys;
722 }
723
724
725 ################################################################
726 # build the sort keys table
727 sub DUMP_SORTKEYS($@)
728 {
729     my ($filename, @keys) = @_;
730
731     # count the number of 256-key ranges that contain something
732
733     my @offsets = ();
734     my $ranges = 2;
735     for (my $i = 0; $i < 256; $i++) { $offsets[$i] = 256; }
736     for (my $i = 0; $i < 65536; $i++)
737     {
738         next unless defined $keys[$i];
739         $offsets[$i >> 8] = $ranges * 256;
740         $ranges++;
741         $i |= 255;
742     }
743
744     # output the range offsets
745
746     open OUTPUT,">$filename.new" or die "Cannot create $filename";
747     printf "Building $filename\n";
748     printf OUTPUT "/* Unicode collation element table */\n";
749     printf OUTPUT "/* generated from %s */\n", $SORTKEYS;
750     printf OUTPUT "/* DO NOT EDIT!! */\n\n";
751
752     printf OUTPUT "const unsigned int collation_table[%d] =\n{\n", $ranges*256;
753     printf OUTPUT "    /* index */\n";
754     printf OUTPUT "%s,\n", DUMP_ARRAY( "0x%08x", 0, @offsets );
755
756     # output the default values
757
758     printf OUTPUT "    /* defaults */\n";
759     printf OUTPUT "%s", DUMP_ARRAY( "0x%08x", 0, (0xffffffff) x 256 );
760
761     # output all the key ranges
762
763     for (my $i = 0; $i < 256; $i++)
764     {
765         next if $offsets[$i] == 256;
766         printf OUTPUT ",\n    /* 0x%02x00 .. 0x%02xff */\n", $i, $i;
767         printf OUTPUT "%s", DUMP_ARRAY( "0x%08x", 0xffffffff, @keys[($i<<8) .. ($i<<8)+255] );
768     }
769     printf OUTPUT "\n};\n";
770     close OUTPUT;
771     save_file($filename);
772 }
773
774
775 ################################################################
776 # add default mappings once the file had been read
777 sub ADD_DEFAULT_MAPPINGS()
778 {
779     # Apply aliases
780
781     foreach my $alias (@unicode_aliases)
782     {
783         my $target = undef;
784         foreach my $src (@$alias)
785         {
786             if (defined($uni2cp[$src]))
787             {
788                 $target = $uni2cp[$src];
789                 last;
790             }
791         }
792         next unless defined($target);
793
794         # At least one char of the alias set is defined, set the others to the same value
795         foreach my $src (@$alias)
796         {
797             $uni2cp[$src] = $target unless defined($uni2cp[$src]);
798         }
799     }
800
801     # For every src -> target mapping in the defaults table,
802     # make uni2cp[src] = uni2cp[target] if uni2cp[target] is defined
803
804     for (my $src = 0; $src < 65536; $src++)
805     {
806         next if defined($uni2cp[$src]);  # source has a definition already
807         next unless defined($unicode_defaults[$src]);  # no default for this char
808         my $target = $unicode_defaults[$src];
809
810         # do a recursive mapping until we find a target char that is defined
811         while (!defined($uni2cp[$target]) &&
812                defined($unicode_defaults[$target])) { $target = $unicode_defaults[$target]; }
813
814         if (defined($uni2cp[$target])) { $uni2cp[$src] = $uni2cp[$target]; }
815     }
816
817     # Add an identity mapping for all undefined chars
818
819     for (my $i = 0; $i < 256; $i++)
820     {
821         next if defined($cp2uni[$i]);
822         next if defined($uni2cp[$i]);
823         $cp2uni[$i] = $uni2cp[$i] = $i;
824     }
825 }
826
827 ################################################################
828 # dump an array of integers
829 sub DUMP_ARRAY($$@)
830 {
831     my ($format,$default,@array) = @_;
832     my $i;
833     my $ret = "    ";
834     for ($i = 0; $i < $#array; $i++)
835     {
836         $ret .= sprintf($format, defined $array[$i] ? $array[$i] : $default);
837         $ret .= (($i % 8) != 7) ? ", " : ",\n    ";
838     }
839     $ret .= sprintf($format, defined $array[$i] ? $array[$i] : $default);
840     return $ret;
841 }
842
843 ################################################################
844 # dump an SBCS mapping table
845 sub dump_sbcs_table($$$$$)
846 {
847     my ($codepage, $has_glyphs, $name, $def, $defw) = @_;
848     my $i;
849
850     # output the ascii->unicode table
851
852     if ($has_glyphs)
853     {
854         printf OUTPUT "static const WCHAR cp2uni[512] =\n";
855         printf OUTPUT "{\n%s", DUMP_ARRAY( "0x%04x", $defw, @cp2uni[0 .. 255] );
856         printf OUTPUT ",\n    /* glyphs */\n%s\n};\n\n",
857                       DUMP_ARRAY( "0x%04x", $defw, get_glyphs_mapping(@cp2uni[0 .. 255]) );
858     }
859     else
860     {
861         printf OUTPUT "static const WCHAR cp2uni[256] =\n";
862         printf OUTPUT "{\n%s\n};\n\n", DUMP_ARRAY( "0x%04x", $defw, @cp2uni[0 .. 255] );
863     }
864
865     # count the number of unicode->ascii subtables that contain something
866
867     my @filled = ();
868     my $subtables = 1;
869     for (my $i = 0; $i < 65536; $i++)
870     {
871         next unless defined $uni2cp[$i];
872         $filled[$i >> 8] = 1;
873         $subtables++;
874         $i |= 255;
875     }
876
877     # output all the subtables into a single array
878
879     printf OUTPUT "static const unsigned char uni2cp_low[%d] =\n{\n", $subtables*256;
880     for (my $i = 0; $i < 256; $i++)
881     {
882         next unless $filled[$i];
883         printf OUTPUT "    /* 0x%02x00 .. 0x%02xff */\n", $i, $i;
884         printf OUTPUT "%s,\n", DUMP_ARRAY( "0x%02x", $def, @uni2cp[($i<<8) .. ($i<<8)+255] );
885     }
886     printf OUTPUT "    /* defaults */\n";
887     printf OUTPUT "%s\n};\n\n", DUMP_ARRAY( "0x%02x", 0, ($def) x 256 );
888
889     # output a table of the offsets of the subtables in the previous array
890
891     my $pos = 0;
892     my @offsets = ();
893     for (my $i = 0; $i < 256; $i++)
894     {
895         if ($filled[$i]) { push @offsets, $pos; $pos += 256; }
896         else { push @offsets, ($subtables-1) * 256; }
897     }
898     printf OUTPUT "static const unsigned short uni2cp_high[256] =\n";
899     printf OUTPUT "{\n%s\n};\n\n", DUMP_ARRAY( "0x%04x", 0, @offsets );
900
901     # output the code page descriptor
902
903     printf OUTPUT "const struct sbcs_table cptable_%03d =\n{\n", $codepage;
904     printf OUTPUT "    { %d, 1, 0x%04x, 0x%04x, \"%s\" },\n",
905                   $codepage, $def, $defw, $name;
906     printf OUTPUT "    cp2uni,\n";
907     if ($has_glyphs) { printf OUTPUT "    cp2uni + 256,\n"; }
908     else { printf OUTPUT "    cp2uni,\n"; }
909     printf OUTPUT "    uni2cp_low,\n";
910     printf OUTPUT "    uni2cp_high\n};\n";
911 }
912
913
914 ################################################################
915 # dump a DBCS mapping table
916 sub dump_dbcs_table($$$$@)
917 {
918     my ($codepage, $name, $def, $defw, @lb_ranges) = @_;
919
920     # build a list of lead bytes that are actually used
921
922     my @lblist = ();
923     LBLOOP: for (my $y = 0; $y <= $#lead_bytes; $y++)
924     {
925         my $base = $lead_bytes[$y] << 8;
926         for (my $x = 0; $x < 256; $x++)
927         {
928             if (defined $cp2uni[$base+$x])
929             {
930                 push @lblist,$lead_bytes[$y];
931                 next LBLOOP;
932             }
933         }
934     }
935     my $unused = ($#lead_bytes > $#lblist);
936
937     # output the ascii->unicode table for the single byte chars
938
939     printf OUTPUT "static const WCHAR cp2uni[%d] =\n", 256 * ($#lblist + 2 + $unused);
940     printf OUTPUT "{\n%s,\n", DUMP_ARRAY( "0x%04x", $defw, @cp2uni[0 .. 255] );
941
942     # output the default table for unused lead bytes
943
944     if ($unused)
945     {
946         printf OUTPUT "    /* unused lead bytes */\n";
947         printf OUTPUT "%s,\n", DUMP_ARRAY( "0x%04x", 0, ($defw) x 256 );
948     }
949
950     # output the ascii->unicode table for each DBCS lead byte
951
952     for (my $y = 0; $y <= $#lblist; $y++)
953     {
954         my $base = $lblist[$y] << 8;
955         printf OUTPUT "    /* lead byte %02x */\n", $lblist[$y];
956         printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", $defw, @cp2uni[$base .. $base+255] );
957         printf OUTPUT ($y < $#lblist) ? ",\n" : "\n};\n\n";
958     }
959
960     # output the lead byte subtables offsets
961
962     my @offsets = ();
963     for (my $x = 0; $x < 256; $x++) { $offsets[$x] = 0; }
964     for (my $x = 0; $x <= $#lblist; $x++) { $offsets[$lblist[$x]] = $x + 1; }
965     if ($unused)
966     {
967         # increment all lead bytes offset to take into account the unused table
968         for (my $x = 0; $x <= $#lead_bytes; $x++) { $offsets[$lead_bytes[$x]]++; }
969     }
970     printf OUTPUT "static const unsigned char cp2uni_leadbytes[256] =\n";
971     printf OUTPUT "{\n%s\n};\n\n", DUMP_ARRAY( "0x%02x", 0, @offsets );
972
973     # count the number of unicode->ascii subtables that contain something
974
975     my @filled = ();
976     my $subtables = 1;
977     for (my $i = 0; $i < 65536; $i++)
978     {
979         next unless defined $uni2cp[$i];
980         $filled[$i >> 8] = 1;
981         $subtables++;
982         $i |= 255;
983     }
984
985     # output all the subtables into a single array
986
987     printf OUTPUT "static const unsigned short uni2cp_low[%d] =\n{\n", $subtables*256;
988     for (my $y = 0; $y < 256; $y++)
989     {
990         next unless $filled[$y];
991         printf OUTPUT "    /* 0x%02x00 .. 0x%02xff */\n", $y, $y;
992         printf OUTPUT "%s,\n", DUMP_ARRAY( "0x%04x", $def, @uni2cp[($y<<8) .. ($y<<8)+255] );
993     }
994     printf OUTPUT "    /* defaults */\n";
995     printf OUTPUT "%s\n};\n\n", DUMP_ARRAY( "0x%04x", 0, ($def) x 256 );
996
997     # output a table of the offsets of the subtables in the previous array
998
999     my $pos = 0;
1000     @offsets = ();
1001     for (my $y = 0; $y < 256; $y++)
1002     {
1003         if ($filled[$y]) { push @offsets, $pos; $pos += 256; }
1004         else { push @offsets, ($subtables-1) * 256; }
1005     }
1006     printf OUTPUT "static const unsigned short uni2cp_high[256] =\n";
1007     printf OUTPUT "{\n%s\n};\n\n", DUMP_ARRAY( "0x%04x", 0, @offsets );
1008
1009     # output the code page descriptor
1010
1011     printf OUTPUT "const struct dbcs_table cptable_%03d =\n{\n", $codepage;
1012     printf OUTPUT "    { %d, 2, 0x%04x, 0x%04x, \"%s\" },\n",
1013                   $codepage, $def, $defw, $name;
1014     printf OUTPUT "    cp2uni,\n";
1015     printf OUTPUT "    cp2uni_leadbytes,\n";
1016     printf OUTPUT "    uni2cp_low,\n";
1017     printf OUTPUT "    uni2cp_high,\n";
1018     printf OUTPUT "    {\n    %s\n    }\n", DUMP_ARRAY( "0x%02x", 0, @lb_ranges, 0, 0 );
1019     printf OUTPUT "};\n";
1020 }
1021
1022
1023 ################################################################
1024 # get the list of defined lead byte ranges
1025 sub get_lb_ranges()
1026 {
1027     my @list = ();
1028     my @ranges = ();
1029     my $i = 0;
1030     foreach $i (@lead_bytes) { $list[$i] = 1; }
1031     my $on = 0;
1032     for (my $i = 0; $i < 256; $i++)
1033     {
1034         if ($on)
1035         {
1036             if (!defined $list[$i]) { push @ranges, $i-1; $on = 0; }
1037         }
1038         else
1039         {
1040             if ($list[$i]) { push @ranges, $i; $on = 1; }
1041         }
1042     }
1043     if ($on) { push @ranges, 0xff; }
1044     return @ranges;
1045 }
1046
1047 ################################################################
1048 # dump the Indic Syllabic Category table
1049 sub dump_indic($)
1050 {
1051     my $filename = shift;
1052     my @indic_table = ($indic_types{'Other'}) x 65536;;
1053
1054     my $INPUT = open_data_file "$UNIDATA/IndicSyllabicCategory.txt";
1055     while (<$INPUT>)
1056     {
1057         next if /^\#/;  # skip comments
1058         next if /^\s*$/;  # skip empty lines
1059         next if /\x1a/;  # skip ^Z
1060         if (/^\s*([0-9a-fA-F]+)\s*;\s*([a-zA-Z_]+)\s*#/)
1061         {
1062             my $type = $2;
1063             die "unknown indic $type" unless defined $indic_types{$type};
1064             if (hex $1 < 65536)
1065             {
1066                 $indic_table[hex $1] = $indic_types{$type};
1067             }
1068             next;
1069         }
1070         elsif (/^\s*([0-9a-fA-F]+)..\s*([0-9a-fA-F]+)\s*;\s*([A-Za-z_]+)\s*#/)
1071         {
1072             my $type = $3;
1073             die "unknown indic $type" unless defined $indic_types{$type};
1074             if (hex $1 < 65536 and hex $2 < 6536)
1075             {
1076                 foreach my $i (hex $1 .. hex $2)
1077                 {
1078                     $indic_table[$i] = $indic_types{$type};
1079                 }
1080             }
1081             next;
1082         }
1083         die "malformed line $_";
1084     }
1085     close $INPUT;
1086
1087     $INPUT = open_data_file "$UNIDATA/IndicMatraCategory.txt";
1088     while (<$INPUT>)
1089     {
1090         next if /^\#/;  # skip comments
1091         next if /^\s*$/;  # skip empty lines
1092         next if /\x1a/;  # skip ^Z
1093         if (/^\s*([0-9a-fA-F]+)\s*;\s*([a-zA-Z]+)\s*#/)
1094         {
1095             my $type = $2;
1096             die "unknown matra $type" unless defined $matra_types{$type};
1097             $indic_table[hex $1] += $matra_types{$type} << 8;
1098             next;
1099         }
1100         elsif (/^\s*([0-9a-fA-F]+)..\s*([0-9a-fA-F]+)\s*;\s*([A-Za-z_]+)\s*#/)
1101         {
1102             my $type = $3;
1103             die "unknown matra $type" unless defined $matra_types{$type};
1104             foreach my $i (hex $1 .. hex $2)
1105             {
1106                 $indic_table[$i] += $matra_types{$type} << 8;
1107             }
1108             next;
1109         }
1110         die "malformed line $_";
1111     }
1112     close $INPUT;
1113
1114     open OUTPUT,">$filename.new" or die "Cannot create $filename";
1115     print "Building $filename\n";
1116     print OUTPUT "/* Unicode Indic Syllabic Category */\n";
1117     print OUTPUT "/* generated from $UNIDATA/IndicSyllabicCategory.txt */\n";
1118     print OUTPUT "/*       and from $UNIDATA/IndicMatraCategory.txt */\n";
1119     print OUTPUT "/* DO NOT EDIT!! */\n\n";
1120     print OUTPUT "#include \"wine/unicode.h\"\n\n";
1121
1122     dump_two_level_mapping( "indic_syllabic_table", @indic_table);
1123
1124     close OUTPUT;
1125     save_file($filename);
1126 }
1127
1128 ################################################################
1129 # dump the Line Break Properties table
1130 sub dump_linebreak($)
1131 {
1132     my $filename = shift;
1133     my @break_table = ($break_types{'XX'}) x 65536;;
1134     my $next_group = 0;
1135
1136     my $INPUT = open_data_file "$UNIDATA/LineBreak.txt";
1137     while (<$INPUT>)
1138     {
1139         next if /^\#/;  # skip comments
1140         next if /^\s*$/;  # skip empty lines
1141         next if /\x1a/;  # skip ^Z
1142         if (/^\s*([0-9a-fA-F]+)\s*;\s*([0-9A-Z][0-9A-Z])+\s*/)
1143         {
1144             my $type = $2;
1145             die "unknown breaktype $type" unless defined $break_types{$type};
1146             $break_table[hex $1] = $break_types{$type};
1147             next;
1148         }
1149         elsif (/^\s*([0-9a-fA-F]+)..\s*([0-9a-fA-F]+)\s*;\s*([0-9A-Z][0-9A-Z])+\s*/)
1150         {
1151             my $type = $3;
1152             die "unknown breaktype $type" unless defined $break_types{$type};
1153             foreach my $i (hex $1 .. hex $2)
1154             {
1155                 $break_table[$i] = $break_types{$type};
1156             }
1157             next;
1158         }
1159         die "malformed line $_";
1160     }
1161     close $INPUT;
1162
1163     open OUTPUT,">$filename.new" or die "Cannot create $filename";
1164     print "Building $filename\n";
1165     print OUTPUT "/* Unicode Line Break Properties */\n";
1166     print OUTPUT "/* generated from $UNIDATA/LineBreak.txt */\n";
1167     print OUTPUT "/* DO NOT EDIT!! */\n\n";
1168     print OUTPUT "#include \"wine/unicode.h\"\n\n";
1169
1170     dump_two_level_mapping( "wine_linebreak_table", @break_table);
1171
1172     close OUTPUT;
1173     save_file($filename);
1174 }
1175
1176
1177 ################################################################
1178 # dump the BiDi mirroring table
1179 sub dump_mirroring($)
1180 {
1181     my $filename = shift;
1182     my @mirror_table = ();
1183
1184     my $INPUT = open_data_file "$UNIDATA/BidiMirroring.txt";
1185     while (<$INPUT>)
1186     {
1187         next if /^\#/;  # skip comments
1188         next if /^$/;  # skip empty lines
1189         next if /\x1a/;  # skip ^Z
1190         if (/^\s*([0-9a-fA-F]+)\s*;\s*([0-9a-fA-F]+)/)
1191         {
1192             $mirror_table[hex $1] = hex $2;
1193             next;
1194         }
1195         die "malformed line $_";
1196     }
1197     close $INPUT;
1198
1199     open OUTPUT,">$filename.new" or die "Cannot create $filename";
1200     print "Building $filename\n";
1201     print OUTPUT "/* Unicode BiDi mirroring */\n";
1202     print OUTPUT "/* generated from $UNIDATA/BidiMirroring.txt */\n";
1203     print OUTPUT "/* DO NOT EDIT!! */\n\n";
1204     print OUTPUT "#include \"wine/unicode.h\"\n\n";
1205     DUMP_CASE_TABLE( "wine_mirror_map", @mirror_table );
1206     close OUTPUT;
1207     save_file($filename);
1208 }
1209
1210
1211 ################################################################
1212 # dump the Arabic shaping table
1213 sub dump_shaping($)
1214 {
1215     my $filename = shift;
1216     my %groups;
1217     my $next_group = 0;
1218
1219     $groups{"No_Joining_Group"} = $next_group++;
1220
1221     my $INPUT = open_data_file "$UNIDATA/ArabicShaping.txt";
1222     while (<$INPUT>)
1223     {
1224         next if /^\#/;  # skip comments
1225         next if /^\s*$/;  # skip empty lines
1226         next if /\x1a/;  # skip ^Z
1227         if (/^\s*([0-9a-fA-F]+)\s*;.*;\s*([RLDCUT])\s*;\s*(\w+)/)
1228         {
1229             my $type = $2;
1230             my $group = $3;
1231             $groups{$group} = $next_group++ unless defined $groups{$group};
1232             $joining_table[hex $1] = $joining_types{$type} | ($groups{$group} << 8);
1233             next;
1234         }
1235         die "malformed line $_";
1236     }
1237     close $INPUT;
1238
1239     open OUTPUT,">$filename.new" or die "Cannot create $filename";
1240     print "Building $filename\n";
1241     print OUTPUT "/* Unicode Arabic shaping */\n";
1242     print OUTPUT "/* generated from $UNIDATA/ArabicShaping.txt */\n";
1243     print OUTPUT "/* DO NOT EDIT!! */\n\n";
1244     print OUTPUT "#include \"wine/unicode.h\"\n\n";
1245
1246     dump_two_level_mapping( "wine_shaping_table", @joining_table );
1247
1248     print OUTPUT "\nconst unsigned short wine_shaping_forms[256][4] =\n{\n";
1249     for (my $i = 0x600; $i <= 0x6ff; $i++)
1250     {
1251         printf OUTPUT "    { 0x%04x, 0x%04x, 0x%04x, 0x%04x },\n",
1252             ${joining_forms{"isolated"}}[$i] || $i,
1253             ${joining_forms{"final"}}[$i] || $i,
1254             ${joining_forms{"initial"}}[$i] || $i,
1255             ${joining_forms{"medial"}}[$i] || $i;
1256     }
1257     print OUTPUT "};\n";
1258
1259     close OUTPUT;
1260     save_file($filename);
1261 }
1262
1263
1264 ################################################################
1265 # dump the case mapping tables
1266 sub DUMP_CASE_MAPPINGS($)
1267 {
1268     my $filename = shift;
1269     open OUTPUT,">$filename.new" or die "Cannot create $filename";
1270     printf "Building $filename\n";
1271     printf OUTPUT "/* Unicode case mappings */\n";
1272     printf OUTPUT "/* Automatically generated; DO NOT EDIT!! */\n\n";
1273     printf OUTPUT "#include \"wine/unicode.h\"\n\n";
1274
1275     DUMP_CASE_TABLE( "wine_casemap_lower", @tolower_table );
1276     DUMP_CASE_TABLE( "wine_casemap_upper", @toupper_table );
1277     DUMP_CASE_TABLE( "wine_digitmap",  @digitmap_table );
1278     DUMP_CASE_TABLE( "wine_compatmap", @compatmap_table );
1279     close OUTPUT;
1280     save_file($filename);
1281 }
1282
1283
1284 ################################################################
1285 # dump a case mapping table
1286 sub DUMP_CASE_TABLE($@)
1287 {
1288     my ($name,@table) = @_;
1289
1290     # count the number of sub tables that contain something
1291     # also compute the low and upper populated bounds
1292
1293     my @lowerbounds = ( 0, 0 );
1294     my @upperbounds = ( 0, 255 );
1295     my $index = 0;
1296     my @filled = ();
1297     for (my $i = 0; $i < 65536; $i++)
1298     {
1299         next unless defined $table[$i];
1300         if (!defined $filled[$i >> 8])
1301         {
1302           $lowerbounds[$index] = $i & 0xff;
1303           $upperbounds[$index] = 0xff - $lowerbounds[$index];
1304           $filled[$i >> 8] = $index * 256 + 512;
1305           $index++;
1306         }
1307         else
1308         {
1309           $upperbounds[$index-1] = 0xff - ($i & 0xff);
1310         }
1311         $table[$i] = ($table[$i] - $i) & 0xffff;
1312     }
1313
1314     # Collapse blocks upwards if possible
1315     my $removed = 0;
1316     $index = 0;
1317     for (my $i = 0; $i < 256; $i++)
1318     {
1319         next unless defined $filled[$i];
1320         if ($upperbounds[$index - 1] > $lowerbounds[$index])
1321         {
1322            $removed = $removed + $lowerbounds[$index];
1323         }
1324         else
1325         {
1326            $removed = $removed + $upperbounds[$index - 1];
1327            $lowerbounds[$index] = $upperbounds[$index - 1];
1328         }
1329         $filled[$i] = $filled[$i] - $removed;
1330         $index++;
1331     }
1332
1333     # dump the table
1334
1335     printf OUTPUT "const WCHAR %s[%d] =\n", $name, $index * 256 + 512 - $removed;
1336     printf OUTPUT "{\n    /* index */\n";
1337     printf OUTPUT "%s,\n", DUMP_ARRAY( "0x%04x", 256, @filled );
1338     printf OUTPUT "    /* defaults */\n";
1339     printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, (0) x 256 );
1340     $index = 0;
1341     for (my $i = 0; $i < 256; $i++)
1342     {
1343         next unless $filled[$i];
1344         printf OUTPUT ",\n    /* 0x%02x%02x .. 0x%02xff */\n", $i, $lowerbounds[$index], $i;
1345         printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0,
1346                       @table[($i<<8) + $lowerbounds[$index] .. ($i<<8)+255] );
1347         $index++;
1348     }
1349     printf OUTPUT "\n};\n";
1350 }
1351
1352 ################################################################
1353 # compress a mapping table by removing identical rows
1354 sub compress_array($@)
1355 {
1356     my $rows = shift;
1357     my @table = @_;
1358     my $len = @table / $rows;
1359     my @array = (0) x $rows;
1360     my %sequences;
1361
1362     # try to merge table rows
1363     for (my $row = 0; $row < $rows; $row++)
1364     {
1365         my $rowtxt = pack "S*", @table[($row * $len)..($row * $len + $len - 1)];
1366         if (defined($sequences{$rowtxt}))
1367         {
1368             # reuse an existing row
1369             $array[$row] = $sequences{$rowtxt};
1370         }
1371         else
1372         {
1373             # create a new row
1374             $sequences{$rowtxt} = $array[$row] = $#array + 1;
1375             push @array, @table[$row * $len..$row * $len + $len - 1];
1376         }
1377     }
1378     return @array;
1379 }
1380
1381 ################################################################
1382 # dump a simple char -> 16-bit value mapping table
1383 sub dump_simple_mapping($@)
1384 {
1385     my $name = shift;
1386     my @array = compress_array( 256, @_[0..65535] );
1387
1388     printf OUTPUT "const unsigned short %s[%d] =\n{\n", $name, $#array+1;
1389     printf OUTPUT "    /* offsets */\n%s,\n", DUMP_ARRAY( "0x%04x", 0, @array[0..255] );
1390     printf OUTPUT "    /* values */\n%s\n};\n", DUMP_ARRAY( "0x%04x", 0, @array[256..$#array] );
1391 }
1392
1393 ################################################################
1394 # dump a char -> 16-bit value mapping table using two-level tables
1395 sub dump_two_level_mapping($@)
1396 {
1397     my $name = shift;
1398     my @row_array = compress_array( 4096, @_[0..65535] );
1399     my @array = compress_array( 256, @row_array[0..4095] );
1400
1401     for (my $i = 256; $i < @array; $i++) { $array[$i] += @array - 4096; }
1402
1403     printf OUTPUT "const unsigned short %s[%d] =\n{\n", $name, @array + @row_array - 4096;
1404     printf OUTPUT "    /* level 1 offsets */\n%s,\n", DUMP_ARRAY( "0x%04x", 0, @array[0..255] );
1405     printf OUTPUT "    /* level 2 offsets */\n%s,\n", DUMP_ARRAY( "0x%04x", 0, @array[256..$#array] );
1406     printf OUTPUT "    /* values */\n%s\n};\n", DUMP_ARRAY( "0x%04x", 0, @row_array[4096..$#row_array] );
1407 }
1408
1409 ################################################################
1410 # dump a binary case mapping table in l_intl.nls format
1411 sub dump_binary_case_table(@)
1412 {
1413     my (@table) = @_;
1414
1415     my %difftables_hash = ();
1416     my @difftables;
1417     my %offtables2_hash = ();
1418     my @offtables2 = ();
1419     
1420     my @offtable = ();
1421     for (my $i = 0; $i < 256; $i++)
1422     {
1423         my @offtable2 = ();
1424         for(my $j = 0; $j < 16; $j++) # offset table for xx00-xxFF characters
1425         {
1426             my @difftable;
1427             for (my $k = 0; $k < 16; $k++) # case map table for xxx0-xxxF characters
1428             {
1429                 my $char = ($i<<8) + ($j<<4) + $k;
1430                 $difftable[$k] = (defined $table[$char]) ? (($table[$char]-$char) & 0xffff) : 0;
1431             }
1432
1433             my $diff_key = pack "S*", @difftable;
1434             my $offset3 = $difftables_hash{$diff_key};
1435             if (!defined $offset3)
1436             {
1437                 $offset3 = scalar @difftables;
1438                 $difftables_hash{$diff_key} = $offset3;
1439                 push @difftables, @difftable;
1440             }
1441             $offtable2[$j] = $offset3;
1442         }
1443
1444         my $offtable2_key = pack "S*", @offtable2;
1445         my $offset2 = $offtables2_hash{$offtable2_key};
1446         if (!defined $offset2)
1447         {
1448             $offset2 = scalar @offtables2;
1449             $offtables2_hash{$offtable2_key} = $offset2;
1450             push @offtables2, \@offtable2;
1451         }
1452         $offtable[$i] = $offset2;
1453     }
1454
1455     my @output;
1456     my $offset = 0x100; # offset of first subtable in words
1457     foreach (@offtable)
1458     {
1459         push @output, 0x10 * $_ + $offset; # offset of subtable in words
1460     }
1461
1462     $offset = 0x100 + 0x10 * scalar @offtables2; # offset of first difftable in words
1463     foreach(@offtables2)
1464     {
1465         my $table = $_;
1466         foreach(@$table)
1467         {
1468             push @output, $_ + $offset; # offset of difftable in words
1469         }
1470     }
1471
1472     my $len = 1 + scalar @output + scalar @difftables;
1473     return pack "S<*", $len, @output, @difftables;
1474 }
1475
1476
1477 ################################################################
1478 # dump case mappings for l_intl.nls
1479 sub dump_intl_nls($)
1480 {
1481     my $filename = shift;
1482     open OUTPUT,">$filename.new" or die "Cannot create $filename";
1483     printf "Building $filename\n";
1484
1485     binmode OUTPUT;
1486     print OUTPUT pack "S<", 1;  # version
1487     print OUTPUT dump_binary_case_table( @toupper_table );
1488     print OUTPUT dump_binary_case_table( @tolower_table );
1489     close OUTPUT;
1490     save_file($filename);
1491 }
1492
1493
1494 ################################################################
1495 # dump the ctype tables
1496 sub DUMP_CTYPE_TABLES($)
1497 {
1498     my $filename = shift;
1499     open OUTPUT,">$filename.new" or die "Cannot create $filename";
1500     printf "Building $filename\n";
1501     printf OUTPUT "/* Unicode ctype tables */\n";
1502     printf OUTPUT "/* Automatically generated; DO NOT EDIT!! */\n\n";
1503     printf OUTPUT "#include \"wine/unicode.h\"\n\n";
1504
1505     # add the direction in the high 4 bits of the category
1506     for (my $i = 0; $i < 65536; $i++)
1507     {
1508         $category_table[$i] |= $direction_table[$i] << 12 if defined $direction_table[$i];
1509     }
1510
1511     dump_simple_mapping( "wine_wctype_table", @category_table );
1512
1513     close OUTPUT;
1514     save_file($filename);
1515 }
1516
1517
1518 ################################################################
1519 # dump the char composition tables
1520 sub DUMP_COMPOSE_TABLES($)
1521 {
1522     my $filename = shift;
1523
1524     open OUTPUT,">$filename.new" or die "Cannot create $filename";
1525     printf "Building $filename\n";
1526     printf OUTPUT "/* Unicode char composition */\n";
1527     printf OUTPUT "/* Automatically generated; DO NOT EDIT!! */\n\n";
1528     printf OUTPUT "#include \"wine/unicode.h\"\n\n";
1529
1530     ######### composition table
1531
1532     my @filled = ();
1533     foreach my $i (@compose_table)
1534     {
1535         my @comp = @$i;
1536         push @{$filled[$comp[1]]}, [ $comp[0], $comp[2] ];
1537     }
1538
1539     # count how many different second chars we have
1540
1541     my $count = 0;
1542     for (my $i = 0; $i < 65536; $i++)
1543     {
1544         next unless defined $filled[$i];
1545         $count++;
1546     }
1547
1548     # build the table of second chars and offsets
1549
1550     my $pos = $count + 1;
1551     my @table = ();
1552     for (my $i = 0; $i < 65536; $i++)
1553     {
1554         next unless defined $filled[$i];
1555         push @table, $i, $pos;
1556         $pos += @{$filled[$i]};
1557     }
1558     # terminator with last position
1559     push @table, 0, $pos;
1560     printf OUTPUT "const WCHAR unicode_compose_table[0x%x] =\n{\n", 2*$pos;
1561     printf OUTPUT "    /* second chars + offsets */\n%s", DUMP_ARRAY( "0x%04x", 0, @table );
1562
1563     # build the table of first chars and mappings
1564
1565     for (my $i = 0; $i < 65536; $i++)
1566     {
1567         next unless defined $filled[$i];
1568         my @table = ();
1569         my @list = sort { $a->[0] <=> $b->[0] } @{$filled[$i]};
1570         for (my $j = 0; $j <= $#list; $j++)
1571         {
1572             push @table, $list[$j][0], $list[$j][1];
1573         }
1574         printf OUTPUT ",\n    /* 0x%04x */\n%s", $i, DUMP_ARRAY( "0x%04x", 0, @table );
1575     }
1576     printf OUTPUT "\n};\n\nconst unsigned int unicode_compose_table_size = %d;\n\n", $count;
1577
1578     ######### decomposition table
1579
1580     # first determine all the 16-char subsets that contain something
1581
1582     @filled = (0) x 4096;
1583     $pos = 16*2;  # for the null subset
1584     for (my $i = 0; $i < 65536; $i++)
1585     {
1586         next unless defined $decomp_table[$i];
1587         $filled[$i >> 4] = $pos;
1588         $pos += 16*2;
1589         $i |= 15;
1590     }
1591     my $total = $pos;
1592
1593     # now count the 256-char subsets that contain something
1594
1595     my @filled_idx = (256) x 256;
1596     $pos = 256 + 16;
1597     for (my $i = 0; $i < 4096; $i++)
1598     {
1599         next unless $filled[$i];
1600         $filled_idx[$i >> 4] = $pos;
1601         $pos += 16;
1602         $i |= 15;
1603     }
1604     my $null_offset = $pos;  # null mapping
1605     $total += $pos;
1606
1607     # add the index offsets to the subsets positions
1608
1609     for (my $i = 0; $i < 4096; $i++)
1610     {
1611         next unless $filled[$i];
1612         $filled[$i] += $null_offset;
1613     }
1614
1615     # dump the main index
1616
1617     printf OUTPUT "const WCHAR unicode_decompose_table[%d] =\n", $total;
1618     printf OUTPUT "{\n    /* index */\n";
1619     printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, @filled_idx );
1620     printf OUTPUT ",\n    /* null sub-index */\n%s", DUMP_ARRAY( "0x%04x", 0, ($null_offset) x 16 );
1621
1622     # dump the second-level indexes
1623
1624     for (my $i = 0; $i < 256; $i++)
1625     {
1626         next unless ($filled_idx[$i] > 256);
1627         my @table = @filled[($i<<4)..($i<<4)+15];
1628         for (my $j = 0; $j < 16; $j++) { $table[$j] ||= $null_offset; }
1629         printf OUTPUT ",\n    /* sub-index %02x */\n", $i;
1630         printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, @table );
1631     }
1632
1633     # dump the 16-char subsets
1634
1635     printf OUTPUT ",\n    /* null mapping */\n";
1636     printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, (0) x 32 );
1637
1638     for (my $i = 0; $i < 4096; $i++)
1639     {
1640         next unless $filled[$i];
1641         my @table = (0) x 32;
1642         for (my $j = 0; $j < 16; $j++)
1643         {
1644             if (defined $decomp_table[($i<<4) + $j])
1645             {
1646                 $table[2 * $j] = ${$decomp_table[($i << 4) + $j]}[0];
1647                 $table[2 * $j + 1] = ${$decomp_table[($i << 4) + $j]}[1];
1648             }
1649         }
1650         printf OUTPUT ",\n    /* 0x%03x0 .. 0x%03xf */\n", $i, $i;
1651         printf OUTPUT "%s", DUMP_ARRAY( "0x%04x", 0, @table );
1652     }
1653
1654     printf OUTPUT "\n};\n";
1655     close OUTPUT;
1656     save_file($filename);
1657 }
1658
1659
1660 ################################################################
1661 # handle a "bestfit" Windows mapping file
1662
1663 sub handle_bestfit_file($$$)
1664 {
1665     my ($filename, $has_glyphs, $comment) = @_;
1666     my $state = "";
1667     my ($codepage, $width, $def, $defw, $count);
1668     my ($lb_cur, $lb_end);
1669     my @lb_ranges = ();
1670
1671     my $INPUT = open_data_file "$MAPPINGS/$filename" or die "Cannot open $filename";
1672
1673     while (<$INPUT>)
1674     {
1675         next if /^;/;  # skip comments
1676         next if /^\s*$/;  # skip empty lines
1677         next if /\x1a/;  # skip ^Z
1678         last if /^ENDCODEPAGE/;
1679
1680         if (/^CODEPAGE\s+(\d+)/)
1681         {
1682             $codepage = $1;
1683             next;
1684         }
1685         if (/^CPINFO\s+(\d+)\s+0x([0-9a-fA-f]+)\s+0x([0-9a-fA-F]+)/)
1686         {
1687             $width = $1;
1688             $def = hex $2;
1689             $defw = hex $3;
1690             next;
1691         }
1692         if (/^(MBTABLE|WCTABLE|DBCSRANGE|DBCSTABLE)\s+(\d+)/)
1693         {
1694             $state = $1;
1695             $count = $2;
1696             next;
1697         }
1698         if (/^0x([0-9a-fA-F]+)\s+0x([0-9a-fA-F]+)/)
1699         {
1700             if ($state eq "MBTABLE")
1701             {
1702                 my $cp = hex $1;
1703                 my $uni = hex $2;
1704                 $cp2uni[$cp] = $uni unless defined($cp2uni[$cp]);
1705                 next;
1706             }
1707             if ($state eq "WCTABLE")
1708             {
1709                 my $uni = hex $1;
1710                 my $cp = hex $2;
1711                 $uni2cp[$uni] = $cp unless defined($uni2cp[$uni]);
1712                 next;
1713             }
1714             if ($state eq "DBCSRANGE")
1715             {
1716                 my $start = hex $1;
1717                 my $end = hex $2;
1718                 push @lb_ranges, $start, $end;
1719                 for (my $i = $start; $i <= $end; $i++)
1720                 {
1721                     push @lead_bytes, $i;
1722                     $cp2uni[$i] = 0;
1723                 }
1724                 $lb_cur = $start;
1725                 $lb_end = $end;
1726                 next;
1727             }
1728             if ($state eq "DBCSTABLE")
1729             {
1730                 my $mb = hex $1;
1731                 my $uni = hex $2;
1732                 my $cp = ($lb_cur << 8) | $mb;
1733                 $cp2uni[$cp] = $uni unless defined($cp2uni[$cp]);
1734                 if (!--$count)
1735                 {
1736                     if (++$lb_cur > $lb_end) { $state = "DBCSRANGE"; }
1737                 }
1738                 next;
1739             }
1740         }
1741         die "$filename: Unrecognized line $_\n";
1742     }
1743     close $INPUT;
1744
1745     my $output = sprintf "libs/wine/c_%03d.c", $codepage;
1746     open OUTPUT,">$output.new" or die "Cannot create $output";
1747
1748     printf "Building %s from %s (%s)\n", $output, $filename, $comment;
1749
1750     # dump all tables
1751
1752     printf OUTPUT "/* code page %03d (%s) */\n", $codepage, $comment;
1753     printf OUTPUT "/* generated from $MAPPINGS/$filename */\n";
1754     printf OUTPUT "/* DO NOT EDIT!! */\n\n";
1755     printf OUTPUT "#include \"wine/unicode.h\"\n\n";
1756
1757     if ($width == 1) { dump_sbcs_table( $codepage, $has_glyphs, $comment, $def, $defw ); }
1758     else { dump_dbcs_table( $codepage, $comment, $def, $defw, @lb_ranges ); }
1759     close OUTPUT;
1760     save_file($output);
1761 }
1762
1763
1764 ################################################################
1765 # read an input file and generate the corresponding .c file
1766 sub HANDLE_FILE(@)
1767 {
1768     my ($codepage,$filename,$has_glyphs,$comment) = @_;
1769
1770     @cp2uni = ();
1771     @lead_bytes = ();
1772     @uni2cp = ();
1773
1774     # symbol codepage file is special
1775     if ($codepage == 20932) { READ_JIS0208_FILE "$MAPPINGS/$filename"; }
1776     elsif ($codepage == 20127) { fill_20127_codepage(); }
1777     elsif ($filename =~ /\/bestfit/)
1778     {
1779         handle_bestfit_file( $filename, $has_glyphs, $comment );
1780         return;
1781     }
1782     else { READ_FILE "$MAPPINGS/$filename"; }
1783
1784     ADD_DEFAULT_MAPPINGS();
1785
1786     my $output = sprintf "libs/wine/c_%03d.c", $codepage;
1787     open OUTPUT,">$output.new" or die "Cannot create $output";
1788
1789     printf "Building %s from %s (%s)\n", $output, $filename || "hardcoded data", $comment;
1790
1791     # dump all tables
1792
1793     printf OUTPUT "/* code page %03d (%s) */\n", $codepage, $comment;
1794     if ($filename)
1795     {
1796         print OUTPUT "/* generated from $MAPPINGS/$filename */\n";
1797         print OUTPUT "/* DO NOT EDIT!! */\n\n";
1798     }
1799     else
1800     {
1801         printf OUTPUT "/* Automatically generated; DO NOT EDIT!! */\n\n";
1802     }
1803     printf OUTPUT "#include \"wine/unicode.h\"\n\n";
1804
1805     if (!@lead_bytes) { dump_sbcs_table( $codepage, $has_glyphs, $comment, $DEF_CHAR, $DEF_CHAR ); }
1806     else { dump_dbcs_table( $codepage, $comment, $DEF_CHAR, $DEF_CHAR, get_lb_ranges() ); }
1807     close OUTPUT;
1808     save_file($output);
1809 }
1810
1811
1812 ################################################################
1813 # save a file if modified
1814 sub save_file($)
1815 {
1816     my $file = shift;
1817     if (-f $file && !system "cmp $file $file.new >/dev/null")
1818     {
1819         unlink "$file.new";
1820     }
1821     else
1822     {
1823         rename "$file.new", "$file";
1824     }
1825 }
1826
1827
1828 ################################################################
1829 # output the list of codepage tables into the cptable.c file
1830 sub output_cptable($)
1831 {
1832     my $output = shift;
1833     my @tables_decl = ();
1834
1835     printf "Building %s\n", $output;
1836
1837     foreach my $file (@allfiles)
1838     {
1839         my ($codepage,$filename,$comment) = @$file;
1840         push @tables_decl, sprintf("extern union cptable cptable_%03d;\n",$codepage);
1841     }
1842
1843     push @tables_decl, sprintf("\nstatic const union cptable * const cptables[%d] =\n{\n",$#allfiles+1);
1844     foreach my $file (@allfiles)
1845     {
1846         my ($codepage,$filename,$comment) = @$file;
1847         push @tables_decl, sprintf("    &cptable_%03d,\n", $codepage);
1848     }
1849     push @tables_decl, "};";
1850     REPLACE_IN_FILE( $output, @tables_decl );
1851 }
1852
1853 ################################################################
1854 # replace the contents of a file between ### cpmap ### marks
1855
1856 sub REPLACE_IN_FILE($@)
1857 {
1858     my $name = shift;
1859     my @data = @_;
1860     my @lines = ();
1861     open(FILE,$name) or die "Can't open $name";
1862     while (<FILE>)
1863     {
1864         push @lines, $_;
1865         last if /\#\#\# cpmap begin \#\#\#/;
1866     }
1867     push @lines, @data;
1868     while (<FILE>)
1869     {
1870         if (/\#\#\# cpmap end \#\#\#/) { push @lines, "\n", $_; last; }
1871     }
1872     push @lines, <FILE>;
1873     open(FILE,">$name.new") or die "Can't modify $name";
1874     print FILE @lines;
1875     close(FILE);
1876     save_file($name);
1877 }
1878
1879 ################################################################
1880 # main routine
1881
1882 chdir ".." if -f "./make_unicode";
1883 READ_DEFAULTS( $DEFAULTS );
1884 DUMP_CASE_MAPPINGS( "libs/wine/casemap.c" );
1885 DUMP_SORTKEYS( "libs/wine/collation.c", READ_SORTKEYS_FILE() );
1886 DUMP_COMPOSE_TABLES( "libs/wine/compose.c" );
1887 DUMP_CTYPE_TABLES( "libs/wine/wctype.c" );
1888 dump_mirroring( "dlls/usp10/mirror.c" );
1889 dump_shaping( "dlls/usp10/shaping.c" );
1890 dump_linebreak( "dlls/usp10/linebreak.c" );
1891 dump_indic( "dlls/usp10/indicsyllable.c" );
1892 dump_intl_nls("tools/l_intl.nls");
1893
1894 foreach my $file (@allfiles) { HANDLE_FILE( @{$file} ); }
1895
1896 output_cptable("libs/wine/cptable.c");
1897
1898 exit 0;
1899
1900 # Local Variables:
1901 # compile-command: "./make_unicode"
1902 # End: