Merge remote-tracking branch 'gvdb/master'
[platform/upstream/glib.git] / glib / gen-unicode-tables.pl
1 #! /usr/bin/perl -w
2
3 #    Copyright (C) 1998, 1999 Tom Tromey
4 #    Copyright (C) 2001 Red Hat Software
5
6 #    This program is free software; you can redistribute it and/or modify
7 #    it under the terms of the GNU General Public License as published by
8 #    the Free Software Foundation; either version 2, or (at your option)
9 #    any later version.
10
11 #    This program is distributed in the hope that it will be useful,
12 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #    GNU General Public License for more details.
15
16 #    You should have received a copy of the GNU General Public License
17 #    along with this program; if not, write to the Free Software
18 #    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 #    02111-1307, USA.
20
21 # Contributer(s):
22 #   Andrew Taylor <andrew.taylor@montage.ca>
23
24 # gen-unicode-tables.pl - Generate tables for libunicode from Unicode data.
25 # See http://www.unicode.org/Public/UNIDATA/UnicodeCharacterDatabase.html
26 # I consider the output of this program to be unrestricted.  Use it as
27 # you will.
28
29 # FIXME:
30 # * For decomp table it might make sense to use a shift count other
31 #   than 8.  We could easily compute the perfect shift count.
32
33 # we use some perl unicode features
34 require 5.006;
35
36 use bytes;
37
38 use vars qw($CODE $NAME $CATEGORY $COMBINING_CLASSES $BIDI_CATEGORY $DECOMPOSITION $DECIMAL_VALUE $DIGIT_VALUE $NUMERIC_VALUE $MIRRORED $OLD_NAME $COMMENT $UPPER $LOWER $TITLE $BREAK_CODE $BREAK_CATEGORY $BREAK_NAME $CASE_CODE $CASE_LOWER $CASE_TITLE $CASE_UPPER $CASE_CONDITION);
39
40
41 # Names of fields in Unicode data table.
42 $CODE = 0;
43 $NAME = 1;
44 $CATEGORY = 2;
45 $COMBINING_CLASSES = 3;
46 $BIDI_CATEGORY = 4;
47 $DECOMPOSITION = 5;
48 $DECIMAL_VALUE = 6;
49 $DIGIT_VALUE = 7;
50 $NUMERIC_VALUE = 8;
51 $MIRRORED = 9;
52 $OLD_NAME = 10;
53 $COMMENT = 11;
54 $UPPER = 12;
55 $LOWER = 13;
56 $TITLE = 14;
57
58 # Names of fields in the line break table
59 $BREAK_CODE = 0;
60 $BREAK_PROPERTY = 1;
61
62 # Names of fields in the SpecialCasing table
63 $CASE_CODE = 0;
64 $CASE_LOWER = 1;
65 $CASE_TITLE = 2;
66 $CASE_UPPER = 3;
67 $CASE_CONDITION = 4;
68
69 # Names of fields in the CaseFolding table
70 $FOLDING_CODE = 0;
71 $FOLDING_STATUS = 1;
72 $FOLDING_MAPPING = 2;
73
74 # Map general category code onto symbolic name.
75 %mappings =
76     (
77      # Normative.
78      'Lu' => "G_UNICODE_UPPERCASE_LETTER",
79      'Ll' => "G_UNICODE_LOWERCASE_LETTER",
80      'Lt' => "G_UNICODE_TITLECASE_LETTER",
81      'Mn' => "G_UNICODE_NON_SPACING_MARK",
82      'Mc' => "G_UNICODE_COMBINING_MARK",
83      'Me' => "G_UNICODE_ENCLOSING_MARK",
84      'Nd' => "G_UNICODE_DECIMAL_NUMBER",
85      'Nl' => "G_UNICODE_LETTER_NUMBER",
86      'No' => "G_UNICODE_OTHER_NUMBER",
87      'Zs' => "G_UNICODE_SPACE_SEPARATOR",
88      'Zl' => "G_UNICODE_LINE_SEPARATOR",
89      'Zp' => "G_UNICODE_PARAGRAPH_SEPARATOR",
90      'Cc' => "G_UNICODE_CONTROL",
91      'Cf' => "G_UNICODE_FORMAT",
92      'Cs' => "G_UNICODE_SURROGATE",
93      'Co' => "G_UNICODE_PRIVATE_USE",
94      'Cn' => "G_UNICODE_UNASSIGNED",
95
96      # Informative.
97      'Lm' => "G_UNICODE_MODIFIER_LETTER",
98      'Lo' => "G_UNICODE_OTHER_LETTER",
99      'Pc' => "G_UNICODE_CONNECT_PUNCTUATION",
100      'Pd' => "G_UNICODE_DASH_PUNCTUATION",
101      'Ps' => "G_UNICODE_OPEN_PUNCTUATION",
102      'Pe' => "G_UNICODE_CLOSE_PUNCTUATION",
103      'Pi' => "G_UNICODE_INITIAL_PUNCTUATION",
104      'Pf' => "G_UNICODE_FINAL_PUNCTUATION",
105      'Po' => "G_UNICODE_OTHER_PUNCTUATION",
106      'Sm' => "G_UNICODE_MATH_SYMBOL",
107      'Sc' => "G_UNICODE_CURRENCY_SYMBOL",
108      'Sk' => "G_UNICODE_MODIFIER_SYMBOL",
109      'So' => "G_UNICODE_OTHER_SYMBOL"
110      );
111
112 %break_mappings =
113     (
114      'AI' => "G_UNICODE_BREAK_AMBIGUOUS",
115      'AL' => "G_UNICODE_BREAK_ALPHABETIC",
116      'B2' => "G_UNICODE_BREAK_BEFORE_AND_AFTER",
117      'BA' => "G_UNICODE_BREAK_AFTER",
118      'BB' => "G_UNICODE_BREAK_BEFORE",
119      'BK' => "G_UNICODE_BREAK_MANDATORY",
120      'CB' => "G_UNICODE_BREAK_CONTINGENT",
121      'CL' => "G_UNICODE_BREAK_CLOSE_PUNCTUATION",
122      'CM' => "G_UNICODE_BREAK_COMBINING_MARK",
123      'CP' => "G_UNICODE_BREAK_CLOSE_PARANTHESIS",
124      'CR' => "G_UNICODE_BREAK_CARRIAGE_RETURN",
125      'EX' => "G_UNICODE_BREAK_EXCLAMATION",
126      'GL' => "G_UNICODE_BREAK_NON_BREAKING_GLUE",
127      'H2' => "G_UNICODE_BREAK_HANGUL_LV_SYLLABLE",
128      'H3' => "G_UNICODE_BREAK_HANGUL_LVT_SYLLABLE",
129      'HY' => "G_UNICODE_BREAK_HYPHEN",
130      'ID' => "G_UNICODE_BREAK_IDEOGRAPHIC",
131      'IN' => "G_UNICODE_BREAK_INSEPARABLE",
132      'IS' => "G_UNICODE_BREAK_INFIX_SEPARATOR",
133      'JL' => "G_UNICODE_BREAK_HANGUL_L_JAMO",
134      'JT' => "G_UNICODE_BREAK_HANGUL_T_JAMO",
135      'JV' => "G_UNICODE_BREAK_HANGUL_V_JAMO",
136      'LF' => "G_UNICODE_BREAK_LINE_FEED",
137      'NL' => "G_UNICODE_BREAK_NEXT_LINE",
138      'NS' => "G_UNICODE_BREAK_NON_STARTER",
139      'NU' => "G_UNICODE_BREAK_NUMERIC",
140      'OP' => "G_UNICODE_BREAK_OPEN_PUNCTUATION",
141      'PO' => "G_UNICODE_BREAK_POSTFIX",
142      'PR' => "G_UNICODE_BREAK_PREFIX",
143      'QU' => "G_UNICODE_BREAK_QUOTATION",
144      'SA' => "G_UNICODE_BREAK_COMPLEX_CONTEXT",
145      'SG' => "G_UNICODE_BREAK_SURROGATE",
146      'SP' => "G_UNICODE_BREAK_SPACE",
147      'SY' => "G_UNICODE_BREAK_SYMBOL",
148      'WJ' => "G_UNICODE_BREAK_WORD_JOINER",
149      'XX' => "G_UNICODE_BREAK_UNKNOWN",
150      'ZW' => "G_UNICODE_BREAK_ZERO_WIDTH_SPACE"
151      );
152
153 # Title case mappings.
154 %title_to_lower = ();
155 %title_to_upper = ();
156
157 # Maximum length of special-case strings
158
159 my @special_cases;
160 my @special_case_offsets;
161 my $special_case_offset = 0;
162
163 $do_decomp = 0;
164 $do_props = 1;
165 if (@ARGV && $ARGV[0] eq '-decomp')
166 {
167     $do_decomp = 1;
168     $do_props = 0;
169     shift @ARGV;
170 }
171 elsif (@ARGV && $ARGV[0] eq '-both')
172 {
173     $do_decomp = 1;
174     shift @ARGV;
175 }
176
177 if (@ARGV != 2) {
178     $0 =~ s@.*/@@;
179     die "\nUsage: $0 [-decomp | -both] UNICODE-VERSION DIRECTORY\n\n       DIRECTORY should contain the following Unicode data files:\n       UnicodeData.txt, LineBreak.txt, SpecialCasing.txt, CaseFolding.txt,\n       CompositionExclusions.txt\n\n";
180 }
181
182 my ($unicodedatatxt, $linebreaktxt, $specialcasingtxt, $casefoldingtxt, $compositionexclusionstxt);
183
184 my $d = $ARGV[1];
185 opendir (my $dir, $d) or die "Cannot open Unicode data dir $d: $!\n";
186 for my $f (readdir ($dir))
187 {
188     $unicodedatatxt = "$d/$f" if ($f =~ /^UnicodeData.*\.txt/);
189     $linebreaktxt = "$d/$f" if ($f =~ /^LineBreak.*\.txt/);
190     $specialcasingtxt = "$d/$f" if ($f =~ /^SpecialCasing.*\.txt/);
191     $casefoldingtxt = "$d/$f" if ($f =~ /^CaseFolding.*\.txt/);
192     $compositionexclusionstxt = "$d/$f" if ($f =~ /^CompositionExclusions.*\.txt/);
193 }
194
195 defined $unicodedatatxt or die "Did not find UnicodeData file";
196 defined $linebreaktxt or die "Did not find LineBreak file";
197 defined $specialcasingtxt or die "Did not find SpecialCasing file";
198 defined $casefoldingtxt or die "Did not find CaseFolding file";
199 defined $compositionexclusionstxt or die "Did not find CompositionExclusions file";
200
201 print "Creating decomp table\n" if ($do_decomp);
202 print "Creating property table\n" if ($do_props);
203
204 print "Composition exlusions from $compositionexclusionstxt\n";
205
206 open (INPUT, "< $compositionexclusionstxt") || exit 1;
207
208 while (<INPUT>) {
209
210     chop;
211
212     next if /^#/;
213     next if /^\s*$/;
214
215     s/\s*#.*//;
216
217     s/^\s*//;
218     s/\s*$//;
219
220     $composition_exclusions{hex($_)} = 1;
221 }
222
223 close INPUT;
224
225 print "Unicode data from $unicodedatatxt\n";
226
227 open (INPUT, "< $unicodedatatxt") || exit 1;
228
229 # we save memory by skipping the huge empty area before U+E0000
230 my $pages_before_e0000;
231
232 $last_code = -1;
233 while (<INPUT>)
234 {
235     chop;
236     @fields = split (';', $_, 30);
237     if ($#fields != 14)
238     {
239         printf STDERR ("Entry for $fields[$CODE] has wrong number of fields (%d)\n", $#fields);
240     }
241
242     $code = hex ($fields[$CODE]);
243
244     if ($code >= 0xE0000 and $last_code < 0xE0000)
245     {
246         $pages_before_e0000 = ($last_code >> 8) + 1;
247     }
248
249     if ($code > $last_code + 1)
250     {
251         # Found a gap.
252         if ($fields[$NAME] =~ /Last>/)
253         {
254             # Fill the gap with the last character read,
255             # since this was a range specified in the char database
256             @gfields = @fields;
257         }
258         else
259         {
260             # The gap represents undefined characters.  Only the type
261             # matters.
262             @gfields = ('', '', 'Cn', '0', '', '', '', '', '', '', '',
263                         '', '', '', '');
264         }
265         for (++$last_code; $last_code < $code; ++$last_code)
266         {
267             $gfields{$CODE} = sprintf ("%04x", $last_code);
268             &process_one ($last_code, @gfields);
269         }
270     }
271     &process_one ($code, @fields);
272     $last_code = $code;
273 }
274
275 close INPUT;
276
277 @gfields = ('', '', 'Cn', '0', '', '', '', '', '', '', '',
278             '', '', '', '');
279 for (++$last_code; $last_code <= 0x10FFFF; ++$last_code)
280 {
281     $gfields{$CODE} = sprintf ("%04x", $last_code);
282     &process_one ($last_code, @gfields);
283 }
284 --$last_code;                   # Want last to be 0x10FFFF.
285
286 print "Creating line break table\n";
287
288 print "Line break data from $linebreaktxt\n";
289
290 open (INPUT, "< $linebreaktxt") || exit 1;
291
292 $last_code = -1;
293 while (<INPUT>)
294 {
295     my ($start_code, $end_code);
296     
297     chop;
298
299     next if /^#/;
300     next if /^$/;
301
302     s/\s*#.*//;
303     
304     @fields = split (';', $_, 30);
305     if ($#fields != 1)
306     {
307         printf STDERR ("Entry for $fields[$CODE] has wrong number of fields (%d)\n", $#fields);
308         next;
309     }
310
311     if ($fields[$CODE] =~ /([A-F0-9]{4,6})\.\.([A-F0-9]{4,6})/) 
312     {
313         $start_code = hex ($1);
314         $end_code = hex ($2);
315     } else {
316         $start_code = $end_code = hex ($fields[$CODE]);
317         
318     }
319
320     if ($start_code > $last_code + 1)
321     {
322         # The gap represents undefined characters. If assigned,
323         # they are AL, if not assigned, XX
324         for (++$last_code; $last_code < $start_code; ++$last_code)
325         {
326             if ($type[$last_code] eq 'Cn')
327             {
328                 $break_props[$last_code] = 'XX';
329             }
330             else
331             {
332                 $break_props[$last_code] = 'AL';
333             }
334         }
335     }
336
337     for ($last_code = $start_code; $last_code <= $end_code; $last_code++)
338     {
339         $break_props[$last_code] = $fields[$BREAK_PROPERTY];
340     }
341     
342     $last_code = $end_code;
343 }
344
345 close INPUT;
346
347 for (++$last_code; $last_code <= 0x10FFFF; ++$last_code)
348 {
349   if ($type[$last_code] eq 'Cn')
350     {
351       $break_props[$last_code] = 'XX';
352     }
353   else
354     {
355       $break_props[$last_code] = 'AL';
356     }
357 }
358 --$last_code;                   # Want last to be 0x10FFFF.
359
360 print STDERR "Last code is not 0x10FFFF" if ($last_code != 0x10FFFF);
361
362 print "Reading special-casing table for case conversion\n";
363
364 open (INPUT, "< $specialcasingtxt") || exit 1;
365
366 while (<INPUT>)
367 {
368     my $code;
369     
370     chop;
371
372     next if /^#/;
373     next if /^\s*$/;
374
375     s/\s*#.*//;
376
377     @fields = split ('\s*;\s*', $_, 30);
378
379     $raw_code = $fields[$CASE_CODE];
380     $code = hex ($raw_code);
381
382     if ($#fields != 4 && $#fields != 5)
383     {
384         printf STDERR ("Entry for $raw_code has wrong number of fields (%d)\n", $#fields);
385         next;
386     }
387
388     if (!defined $type[$code])
389     {
390         printf STDERR "Special case for code point: $code, which has no defined type\n";
391         next;
392     }
393
394     if (defined $fields[5]) {
395         # Ignore conditional special cases - we'll handle them in code
396         next;
397     }
398     
399     if ($type[$code] eq 'Lu') 
400     {
401         (hex $fields[$CASE_UPPER] == $code) || die "$raw_code is Lu and UCD_Upper($raw_code) != $raw_code";
402
403         &add_special_case ($code, $value[$code], $fields[$CASE_LOWER], $fields[$CASE_TITLE]);
404         
405     } elsif ($type[$code] eq 'Lt') 
406     {
407         (hex $fields[$CASE_TITLE] == $code) || die "$raw_code is Lt and UCD_Title($raw_code) != $raw_code";
408         
409         &add_special_case ($code, undef, $fields[$CASE_LOWER], $fields[$CASE_UPPER]);
410     } elsif ($type[$code] eq 'Ll') 
411     {
412         (hex $fields[$CASE_LOWER] == $code) || die "$raw_code is Ll and UCD_Lower($raw_code) != $raw_code";
413         
414         &add_special_case ($code, $value[$code], $fields[$CASE_UPPER], $fields[$CASE_TITLE]);
415     } else {
416         printf STDERR "Special case for non-alphabetic code point: $raw_code\n";
417         next;
418     }
419 }
420
421 close INPUT;
422
423 open (INPUT, "< $casefoldingtxt") || exit 1;
424
425 my $casefoldlen = 0;
426 my @casefold;
427  
428 while (<INPUT>)
429 {
430     my $code;
431     
432     chop;
433
434     next if /^#/;
435     next if /^\s*$/;
436
437     s/\s*#.*//;
438
439     @fields = split ('\s*;\s*', $_, 30);
440
441     $raw_code = $fields[$FOLDING_CODE];
442     $code = hex ($raw_code);
443
444     if ($#fields != 3)
445     {
446         printf STDERR ("Entry for $raw_code has wrong number of fields (%d)\n", $#fields);
447         next;
448     }
449
450     # we don't use Simple or Turkic rules here
451     next if ($fields[$FOLDING_STATUS] =~ /^[ST]$/);
452
453     @values = map { hex ($_) } split /\s+/, $fields[$FOLDING_MAPPING];
454
455     # Check simple case
456
457     if (@values == 1 && 
458         !(defined $value[$code] && $value[$code] >= 0x1000000) &&
459         defined $type[$code]) {
460
461         my $lower;
462         if ($type[$code] eq 'Ll') 
463         {
464             $lower = $code;
465         } elsif ($type[$code] eq 'Lt') 
466         {
467             $lower = $title_to_lower{$code};
468         } elsif ($type[$code] eq 'Lu') 
469         {
470             $lower = $value[$code];
471         } else {
472             $lower = $code;
473         }
474         
475         if ($lower == $values[0]) {
476             next;
477         }
478     }
479
480     my $string = pack ("U*", @values);
481
482     if (1 + &length_in_bytes ($string) > $casefoldlen) {
483         $casefoldlen = 1 + &length_in_bytes ($string);
484     }
485
486     push @casefold, [ $code, &escape ($string) ];
487 }
488
489 close INPUT;
490
491 if ($do_props) {
492     &print_tables ($last_code)
493 }
494 if ($do_decomp) {
495     &print_decomp ($last_code);
496     &output_composition_table;
497 }
498
499 &print_line_break ($last_code);
500
501 exit 0;
502
503
504 # perl "length" returns the length in characters
505 sub length_in_bytes
506 {
507     my ($string) = @_;
508
509     return length $string;
510 }
511
512 # Process a single character.
513 sub process_one
514 {
515     my ($code, @fields) = @_;
516
517     $type[$code] = $fields[$CATEGORY];
518     if ($type[$code] eq 'Nd')
519     {
520         $value[$code] = int ($fields[$DECIMAL_VALUE]);
521     }
522     elsif ($type[$code] eq 'Ll')
523     {
524         $value[$code] = hex ($fields[$UPPER]);
525     }
526     elsif ($type[$code] eq 'Lu')
527     {
528         $value[$code] = hex ($fields[$LOWER]);
529     }
530
531     if ($type[$code] eq 'Lt')
532     {
533         $title_to_lower{$code} = hex ($fields[$LOWER]);
534         $title_to_upper{$code} = hex ($fields[$UPPER]);
535     }
536
537     $cclass[$code] = $fields[$COMBINING_CLASSES];
538
539     # Handle decompositions.
540     if ($fields[$DECOMPOSITION] ne '')
541     {
542         if ($fields[$DECOMPOSITION] =~ s/\<.*\>\s*//) {
543            $decompose_compat[$code] = 1;
544         } else {
545            $decompose_compat[$code] = 0;
546
547            if (!exists $composition_exclusions{$code}) {
548                $compositions{$code} = $fields[$DECOMPOSITION];
549            }
550         }
551         $decompositions[$code] = $fields[$DECOMPOSITION];
552     }
553 }
554
555 sub print_tables
556 {
557     my ($last) = @_;
558     my ($outfile) = "gunichartables.h";
559
560     local ($bytes_out) = 0;
561
562     print "Writing $outfile...\n";
563
564     open (OUT, "> $outfile");
565
566     print OUT "/* This file is automatically generated.  DO NOT EDIT!\n";
567     print OUT "   Instead, edit gen-unicode-tables.pl and re-run.  */\n\n";
568
569     print OUT "#ifndef CHARTABLES_H\n";
570     print OUT "#define CHARTABLES_H\n\n";
571
572     print OUT "#define G_UNICODE_DATA_VERSION \"$ARGV[0]\"\n\n";
573
574     printf OUT "#define G_UNICODE_LAST_CHAR 0x%04x\n\n", $last;
575
576     printf OUT "#define G_UNICODE_MAX_TABLE_INDEX 10000\n\n";
577
578     my $last_part1 = ($pages_before_e0000 * 256) - 1;
579     printf OUT "#define G_UNICODE_LAST_CHAR_PART1 0x%04X\n\n", $last_part1;
580     printf OUT "#define G_UNICODE_LAST_PAGE_PART1 %d\n\n", $pages_before_e0000 - 1;
581
582     $table_index = 0;
583     printf OUT "static const char type_data[][256] = {\n";
584     for ($count = 0; $count <= $last; $count += 256)
585     {
586         $row[$count / 256] = &print_row ($count, 1, \&fetch_type);
587     }
588     printf OUT "\n};\n\n";
589
590     printf OUT "/* U+0000 through U+%04X */\n", $last_part1;
591     print OUT "static const gint16 type_table_part1[$pages_before_e0000] = {\n";
592     for ($count = 0; $count <= $last_part1; $count += 256)
593     {
594         print OUT ",\n" if $count > 0;
595         print OUT "  ", $row[$count / 256];
596         $bytes_out += 2;
597     }
598     print OUT "\n};\n\n";
599
600     printf OUT "/* U+E0000 through U+%04X */\n", $last;
601     print OUT "static const gint16 type_table_part2[768] = {\n";
602     for ($count = 0xE0000; $count <= $last; $count += 256)
603     {
604         print OUT ",\n" if $count > 0xE0000;
605         print OUT "  ", $row[$count / 256];
606         $bytes_out += 2;
607     }
608     print OUT "\n};\n\n";
609
610
611     #
612     # Now print attribute table.
613     #
614
615     $table_index = 0;
616     printf OUT "static const gunichar attr_data[][256] = {\n";
617     for ($count = 0; $count <= $last; $count += 256)
618     {
619         $row[$count / 256] = &print_row ($count, 4, \&fetch_attr);
620     }
621     printf OUT "\n};\n\n";
622
623     printf OUT "/* U+0000 through U+%04X */\n", $last_part1;
624     print OUT "static const gint16 attr_table_part1[$pages_before_e0000] = {\n";
625     for ($count = 0; $count <= $last_part1; $count += 256)
626     {
627         print OUT ",\n" if $count > 0;
628         print OUT "  ", $row[$count / 256];
629         $bytes_out += 2;
630     }
631     print OUT "\n};\n\n";
632
633     printf OUT "/* U+E0000 through U+%04X */\n", $last;
634     print OUT "static const gint16 attr_table_part2[768] = {\n";
635     for ($count = 0xE0000; $count <= $last; $count += 256)
636     {
637         print OUT ",\n" if $count > 0xE0000;
638         print OUT "  ", $row[$count / 256];
639         $bytes_out += 2;
640     }
641     print OUT "\n};\n\n";
642
643     #
644     # print title case table
645     #
646
647     print OUT "static const gunichar title_table[][3] = {\n";
648     my ($item);
649     my ($first) = 1;
650     foreach $item (sort keys %title_to_lower)
651     {
652         print OUT ",\n"
653             unless $first;
654         $first = 0;
655         printf OUT "  { 0x%04x, 0x%04x, 0x%04x }", $item, $title_to_upper{$item}, $title_to_lower{$item};
656         $bytes_out += 12;
657     }
658     print OUT "\n};\n\n";
659
660     #
661     # And special case conversion table -- conversions that change length
662     #
663     &output_special_case_table (\*OUT);
664     &output_casefold_table (\*OUT);
665
666     print OUT "#endif /* CHARTABLES_H */\n";
667
668     close (OUT);
669
670     printf STDERR "Generated %d bytes in tables\n", $bytes_out;
671 }
672
673 # A fetch function for the type table.
674 sub fetch_type
675 {
676     my ($index) = @_;
677     return $mappings{$type[$index]};
678 }
679
680 # A fetch function for the attribute table.
681 sub fetch_attr
682 {
683     my ($index) = @_;
684     if (defined $value[$index])
685       {
686         return sprintf ("0x%04x", $value[$index]);
687       }
688     else
689       {
690         return "0x0000";
691       }
692 }
693
694 sub print_row
695 {
696     my ($start, $typsize, $fetcher) = @_;
697
698     my ($i);
699     my (@values);
700     my ($flag) = 1;
701     my ($off);
702
703     for ($off = 0; $off < 256; ++$off)
704     {
705         $values[$off] = $fetcher->($off + $start);
706         if ($values[$off] ne $values[0])
707         {
708             $flag = 0;
709         }
710     }
711     if ($flag)
712     {
713         return $values[0] . " + G_UNICODE_MAX_TABLE_INDEX";
714     }
715
716     printf OUT ",\n" if ($table_index != 0);
717     printf OUT "  { /* page %d, index %d */\n    ", $start / 256, $table_index;
718     my ($column) = 4;
719     for ($i = $start; $i < $start + 256; ++$i)
720     {
721         print OUT ", "
722             if $i > $start;
723         my ($text) = $values[$i - $start];
724         if (length ($text) + $column + 2 > 78)
725         {
726             print OUT "\n    ";
727             $column = 4;
728         }
729         print OUT $text;
730         $column += length ($text) + 2;
731     }
732     print OUT "\n  }";
733
734     $bytes_out += 256 * $typsize;
735
736     return sprintf "%d /* page %d */", $table_index++, $start / 256;
737 }
738
739 sub escape
740 {
741     my ($string) = @_;
742
743     my $escaped = unpack("H*", $string);
744     $escaped =~ s/(.{2})/\\x$1/g;
745
746     return $escaped;
747 }
748
749 # Returns the offset of $decomp in the offset string. Updates the
750 # referenced variables as appropriate.
751 sub handle_decomp ($$$$)
752 {
753     my ($decomp, $decomp_offsets_ref, $decomp_string_ref, $decomp_string_offset_ref) = @_;
754     my $offset = "G_UNICODE_NOT_PRESENT_OFFSET";
755
756     if (defined $decomp)
757     {
758         if (defined $decomp_offsets_ref->{$decomp})
759         {
760             $offset = $decomp_offsets_ref->{$decomp};
761         }
762         else
763         {
764             $offset = ${$decomp_string_offset_ref};
765             $decomp_offsets_ref->{$decomp} = $offset;
766             ${$decomp_string_ref} .= "\n  \"" . &escape ($decomp) . "\\0\" /* offset ${$decomp_string_offset_ref} */";
767             ${$decomp_string_offset_ref} += &length_in_bytes ($decomp) + 1;
768         }
769     }
770
771     return $offset;
772 }
773
774 # Generate the character decomposition header.
775 sub print_decomp
776 {
777     my ($last) = @_;
778     my ($outfile) = "gunidecomp.h";
779
780     local ($bytes_out) = 0;
781
782     print "Writing $outfile...\n";
783
784     open (OUT, "> $outfile") || exit 1;
785
786     print OUT "/* This file is automatically generated.  DO NOT EDIT! */\n\n";
787     print OUT "#ifndef DECOMP_H\n";
788     print OUT "#define DECOMP_H\n\n";
789
790     printf OUT "#define G_UNICODE_LAST_CHAR 0x%04x\n\n", $last;
791
792     printf OUT "#define G_UNICODE_MAX_TABLE_INDEX (0x110000 / 256)\n\n";
793
794     my $last_part1 = ($pages_before_e0000 * 256) - 1;
795     printf OUT "#define G_UNICODE_LAST_CHAR_PART1 0x%04X\n\n", $last_part1;
796     printf OUT "#define G_UNICODE_LAST_PAGE_PART1 %d\n\n", $pages_before_e0000 - 1;
797
798     $NOT_PRESENT_OFFSET = 65535;
799     print OUT "#define G_UNICODE_NOT_PRESENT_OFFSET $NOT_PRESENT_OFFSET\n\n";
800
801     my ($count, @row);
802     $table_index = 0;
803     printf OUT "static const guchar cclass_data[][256] = {\n";
804     for ($count = 0; $count <= $last; $count += 256)
805     {
806         $row[$count / 256] = &print_row ($count, 1, \&fetch_cclass);
807     }
808     printf OUT "\n};\n\n";
809
810     print OUT "static const gint16 combining_class_table_part1[$pages_before_e0000] = {\n";
811     for ($count = 0; $count <= $last_part1; $count += 256)
812     {
813         print OUT ",\n" if $count > 0;
814         print OUT "  ", $row[$count / 256];
815         $bytes_out += 2;
816     }
817     print OUT "\n};\n\n";
818
819     print OUT "static const gint16 combining_class_table_part2[768] = {\n";
820     for ($count = 0xE0000; $count <= $last; $count += 256)
821     {
822         print OUT ",\n" if $count > 0xE0000;
823         print OUT "  ", $row[$count / 256];
824         $bytes_out += 2;
825     }
826     print OUT "\n};\n\n";
827
828     print OUT "typedef struct\n{\n";
829     print OUT "  gunichar ch;\n";
830     print OUT "  guint16 canon_offset;\n";
831     print OUT "  guint16 compat_offset;\n";
832     print OUT "} decomposition;\n\n";
833
834     print OUT "static const decomposition decomp_table[] =\n{\n";
835     my ($iter);
836     my ($first) = 1;
837     my ($decomp_string) = "";
838     my ($decomp_string_offset) = 0;
839     for ($count = 0; $count <= $last; ++$count)
840     {
841         if (defined $decompositions[$count])
842         {
843             print OUT ",\n"
844                 if ! $first;
845             $first = 0;
846
847             my $canon_decomp;
848             my $compat_decomp;
849
850             if (!$decompose_compat[$count]) {
851                 $canon_decomp = make_decomp ($count, 0);
852             }
853             $compat_decomp = make_decomp ($count, 1);
854
855             if (defined $canon_decomp && $compat_decomp eq $canon_decomp) {
856                 undef $compat_decomp; 
857             }
858
859             my $canon_offset = handle_decomp ($canon_decomp, \%decomp_offsets, \$decomp_string, \$decomp_string_offset);
860             my $compat_offset = handle_decomp ($compat_decomp, \%decomp_offsets, \$decomp_string, \$decomp_string_offset);
861
862             die if $decomp_string_offset > $NOT_PRESENT_OFFSET;
863
864             printf OUT qq(  { 0x%04x, $canon_offset, $compat_offset }), $count;
865             $bytes_out += 8;
866         }
867     }
868     print OUT "\n};\n\n";
869     $bytes_out += $decomp_string_offset + 1;
870
871     printf OUT "static const gchar decomp_expansion_string[] = %s;\n\n", $decomp_string;
872
873     print OUT "#endif /* DECOMP_H */\n";
874
875     printf STDERR "Generated %d bytes in decomp tables\n", $bytes_out;
876 }
877
878 sub print_line_break
879 {
880     my ($last) = @_;
881     my ($outfile) = "gunibreak.h";
882
883     local ($bytes_out) = 0;
884
885     print "Writing $outfile...\n";
886
887     open (OUT, "> $outfile");
888
889     print OUT "/* This file is automatically generated.  DO NOT EDIT!\n";
890     print OUT "   Instead, edit gen-unicode-tables.pl and re-run.  */\n\n";
891
892     print OUT "#ifndef BREAKTABLES_H\n";
893     print OUT "#define BREAKTABLES_H\n\n";
894
895     print OUT "#include <glib/gtypes.h>\n";
896     print OUT "#include <glib/gunicode.h>\n\n";
897
898     print OUT "#define G_UNICODE_DATA_VERSION \"$ARGV[0]\"\n\n";
899
900     printf OUT "#define G_UNICODE_LAST_CHAR 0x%04X\n\n", $last;
901
902     printf OUT "#define G_UNICODE_MAX_TABLE_INDEX 10000\n\n";
903
904     my $last_part1 = ($pages_before_e0000 * 256) - 1;
905     printf OUT "/* the last code point that should be looked up in break_property_table_part1 */\n";
906     printf OUT "#define G_UNICODE_LAST_CHAR_PART1 0x%04X\n\n", $last_part1;
907
908     $table_index = 0;
909     printf OUT "static const gint8 break_property_data[][256] = {\n";
910     for ($count = 0; $count <= $last; $count += 256)
911     {
912         $row[$count / 256] = &print_row ($count, 1, \&fetch_break_type);
913     }
914     printf OUT "\n};\n\n";
915
916     printf OUT "/* U+0000 through U+%04X */\n", $last_part1;
917     print OUT "static const gint16 break_property_table_part1[$pages_before_e0000] = {\n";
918     for ($count = 0; $count <= $last_part1; $count += 256)
919     {
920         print OUT ",\n" if $count > 0;
921         print OUT "  ", $row[$count / 256];
922         $bytes_out += 2;
923     }
924     print OUT "\n};\n\n";
925
926     printf OUT "/* U+E0000 through U+%04X */\n", $last;
927     print OUT "static const gint16 break_property_table_part2[768] = {\n";
928     for ($count = 0xE0000; $count <= $last; $count += 256)
929     {
930         print OUT ",\n" if $count > 0xE0000;
931         print OUT "  ", $row[$count / 256];
932         $bytes_out += 2;
933     }
934     print OUT "\n};\n\n";
935
936
937     print OUT "#endif /* BREAKTABLES_H */\n";
938
939     close (OUT);
940
941     printf STDERR "Generated %d bytes in break tables\n", $bytes_out;
942 }
943
944
945 # A fetch function for the break properties table.
946 sub fetch_break_type
947 {
948     my ($index) = @_;
949     return $break_mappings{$break_props[$index]};
950 }
951
952 # Fetcher for combining class.
953 sub fetch_cclass
954 {
955     my ($i) = @_;
956     return $cclass[$i];
957 }
958
959 # Expand a character decomposition recursively.
960 sub expand_decomp
961 {
962     my ($code, $compat) = @_;
963
964     my ($iter, $val);
965     my (@result) = ();
966     foreach $iter (split (' ', $decompositions[$code]))
967     {
968         $val = hex ($iter);
969         if (defined $decompositions[$val] && 
970             ($compat || !$decompose_compat[$val]))
971         {
972             push (@result, &expand_decomp ($val, $compat));
973         }
974         else
975         {
976             push (@result, $val);
977         }
978     }
979
980     return @result;
981 }
982
983 sub make_decomp
984 {
985     my ($code, $compat) = @_;
986
987     my $result = "";
988     foreach $iter (&expand_decomp ($code, $compat))
989     {
990         $result .= pack ("U", $iter);  # to utf-8
991     }
992
993     $result;
994 }
995 # Generate special case data string from two fields
996 sub add_special_case
997 {
998     my ($code, $single, $field1, $field2) = @_;
999
1000     @values = (defined $single ? $single : (),
1001                (map { hex ($_) } split /\s+/, $field1),
1002                0,
1003                (map { hex ($_) } split /\s+/, $field2));
1004
1005     $result = "";
1006
1007     for $value (@values) {
1008         $result .= pack ("U", $value);  # to utf-8
1009     }
1010     
1011     push @special_case_offsets, $special_case_offset;
1012
1013     # We encode special cases up in the 0x1000000 space
1014     $value[$code] = 0x1000000 + $special_case_offset;
1015
1016     $special_case_offset += 1 + &length_in_bytes ($result);
1017
1018     push @special_cases, &escape ($result);
1019 }
1020
1021 sub output_special_case_table
1022 {
1023     my $out = shift;
1024
1025     print $out <<EOT;
1026
1027 /* Table of special cases for case conversion; each record contains
1028  * First, the best single character mapping to lowercase if Lu, 
1029  * and to uppercase if Ll, followed by the output mapping for the two cases 
1030  * other than the case of the codepoint, in the order [Ll],[Lu],[Lt],
1031  * encoded in UTF-8, separated and terminated by a null character.
1032  */
1033 static const gchar special_case_table[] = {
1034 EOT
1035
1036     my $i = 0;
1037     for $case (@special_cases) {
1038         print $out qq( "$case\\0" /* offset ${special_case_offsets[$i]} */\n);
1039         $i++;
1040     }
1041
1042     print $out <<EOT;
1043 };
1044
1045 EOT
1046
1047     print STDERR "Generated " . ($special_case_offset + 1) . " bytes in special case table\n";
1048 }
1049
1050 sub enumerate_ordered
1051 {
1052     my ($array) = @_;
1053
1054     my $n = 0;
1055     for my $code (sort { $a <=> $b } keys %$array) {
1056         if ($array->{$code} == 1) {
1057             delete $array->{$code};
1058             next;
1059         }
1060         $array->{$code} = $n++;
1061     }
1062
1063     return $n;
1064 }
1065
1066 sub output_composition_table
1067 {
1068     print STDERR "Generating composition table\n";
1069     
1070     local ($bytes_out) = 0;
1071
1072     my %first;
1073     my %second;
1074
1075     # First we need to go through and remove decompositions
1076     # starting with a non-starter, and single-character 
1077     # decompositions. At the same time, record
1078     # the first and second character of each decomposition
1079     
1080     for $code (keys %compositions) 
1081     {
1082         @values = map { hex ($_) } split /\s+/, $compositions{$code};
1083
1084         # non-starters
1085         if ($cclass[$values[0]]) {
1086             delete $compositions{$code};
1087             next;
1088         }
1089
1090         # single-character decompositions
1091         if (@values == 1) {
1092             delete $compositions{$code};
1093             next;
1094         }
1095
1096         if (@values != 2) {
1097             die "$code has more than two elements in its decomposition!\n";
1098         }
1099
1100         if (exists $first{$values[0]}) {
1101             $first{$values[0]}++;
1102         } else {
1103             $first{$values[0]} = 1;
1104         }
1105     }
1106
1107     # Assign integer indices, removing singletons
1108     my $n_first = enumerate_ordered (\%first);
1109
1110     # Now record the second character of each (non-singleton) decomposition
1111     for $code (keys %compositions) {
1112         @values = map { hex ($_) } split /\s+/, $compositions{$code};
1113
1114         if (exists $first{$values[0]}) {
1115             if (exists $second{$values[1]}) {
1116                 $second{$values[1]}++;
1117             } else {
1118                 $second{$values[1]} = 1;
1119             }
1120         }
1121     }
1122
1123     # Assign integer indices, removing duplicate
1124     my $n_second = enumerate_ordered (\%second);
1125
1126     # Build reverse table
1127
1128     my @first_singletons;
1129     my @second_singletons;
1130     my %reverse;
1131     for $code (keys %compositions) {
1132         @values = map { hex ($_) } split /\s+/, $compositions{$code};
1133
1134         my $first = $first{$values[0]};
1135         my $second = $second{$values[1]};
1136
1137         if (defined $first && defined $second) {
1138             $reverse{"$first|$second"} = $code;
1139         } elsif (!defined $first) {
1140             push @first_singletons, [ $values[0], $values[1], $code ];
1141         } else {
1142             push @second_singletons, [ $values[1], $values[0], $code ];
1143         }
1144     }
1145
1146     @first_singletons = sort { $a->[0] <=> $b->[0] } @first_singletons;
1147     @second_singletons = sort { $a->[0] <=> $b->[0] } @second_singletons;
1148
1149     my %vals;
1150     
1151     open OUT, ">gunicomp.h" or die "Cannot open gunicomp.h: $!\n";
1152     
1153     # Assign values in lookup table for all code points involved
1154     
1155     my $total = 1;
1156     my $last = 0;
1157     printf OUT "#define COMPOSE_FIRST_START %d\n", $total;
1158     for $code (keys %first) {
1159         $vals{$code} = $first{$code} + $total;
1160         $last = $code if $code > $last;
1161     }
1162     $total += $n_first;
1163     $i = 0;
1164     printf OUT "#define COMPOSE_FIRST_SINGLE_START %d\n", $total;
1165     for $record (@first_singletons) {
1166         my $code = $record->[0];
1167         $vals{$code} = $i++ + $total;
1168         $last = $code if $code > $last;
1169     }
1170     $total += @first_singletons;
1171     printf OUT "#define COMPOSE_SECOND_START %d\n", $total;
1172     for $code (keys %second) {
1173         $vals{$code} = $second{$code} + $total;
1174         $last = $code if $code > $last;
1175     }
1176     $total += $n_second;
1177     $i = 0;
1178     printf OUT "#define COMPOSE_SECOND_SINGLE_START %d\n\n", $total;
1179     for $record (@second_singletons) {
1180         my $code = $record->[0];
1181         $vals{$code} = $i++ + $total;
1182         $last = $code if $code > $last;
1183     }
1184
1185     printf OUT "#define COMPOSE_TABLE_LAST %d\n\n", $last / 256;
1186
1187     # Output lookup table
1188
1189     my @row;                                              
1190     $table_index = 0;
1191     printf OUT "static const guint16 compose_data[][256] = {\n";
1192     for (my $count = 0; $count <= $last; $count += 256)
1193     {
1194         $row[$count / 256] = &print_row ($count, 2, sub { exists $vals{$_[0]} ? $vals{$_[0]} : 0; });
1195     }
1196     printf OUT "\n};\n\n";
1197
1198     print OUT "static const gint16 compose_table[COMPOSE_TABLE_LAST + 1] = {\n";
1199     for (my $count = 0; $count <= $last; $count += 256)
1200     {
1201         print OUT ",\n" if $count > 0;
1202         print OUT "  ", $row[$count / 256];
1203         $bytes_out += 2;
1204     }
1205     print OUT "\n};\n\n";
1206
1207     # Output first singletons
1208
1209     print OUT "static const gunichar compose_first_single[][2] = {\n";
1210     $i = 0;                                  
1211     for $record (@first_singletons) {
1212         print OUT ",\n" if $i++ > 0;
1213         printf OUT " { %#06x, %#06x }", $record->[1], $record->[2];
1214     }
1215     print OUT "\n};\n";
1216                                      
1217     $bytes_out += @first_singletons * 4;
1218                   
1219     # Output second singletons
1220
1221     print OUT "static const guint16 compose_second_single[][2] = {\n";
1222     $i = 0;                                  
1223     for $record (@second_singletons) {
1224         if ($record->[1] > 0xFFFF or $record->[2] > 0xFFFF) {
1225             die "time to switch compose_second_single to gunichar";
1226         }
1227         print OUT ",\n" if $i++ > 0;
1228         printf OUT " { %#06x, %#06x }", $record->[1], $record->[2];
1229     }
1230     print OUT "\n};\n";
1231                                      
1232     $bytes_out += @second_singletons * 4;                                    
1233                   
1234     # Output array of composition pairs
1235
1236     print OUT <<EOT;
1237 static const guint16 compose_array[$n_first][$n_second] = {
1238 EOT
1239                         
1240     for (my $i = 0; $i < $n_first; $i++) {
1241         print OUT ",\n" if $i;
1242         print OUT " { ";
1243         for (my $j = 0; $j < $n_second; $j++) {
1244             print OUT ", " if $j;
1245             if (exists $reverse{"$i|$j"}) {
1246                 if ($reverse{"$i|$j"} > 0xFFFF) {
1247                     die "time to switch compose_array to gunichar" ;
1248                 }
1249                 printf OUT "0x%04x", $reverse{"$i|$j"};
1250             } else {
1251                 print OUT "     0";
1252             }
1253         }
1254         print OUT " }";
1255     }
1256     print OUT "\n";
1257
1258     print OUT <<EOT;
1259 };
1260 EOT
1261
1262     $bytes_out += $n_first * $n_second * 2;
1263     
1264     printf STDERR "Generated %d bytes in compose tables\n", $bytes_out;
1265 }
1266
1267 sub output_casefold_table
1268 {
1269     my $out = shift;
1270
1271     print $out <<EOT;
1272
1273 /* Table of casefolding cases that can't be derived by lowercasing
1274  */
1275 static const struct {
1276   guint16 ch;
1277   gchar data[$casefoldlen];
1278 } casefold_table[] = {
1279 EOT
1280
1281    @casefold = sort { $a->[0] <=> $b->[0] } @casefold; 
1282     
1283    for $case (@casefold) 
1284    {
1285        $code = $case->[0];
1286        $string = $case->[1];
1287
1288        if ($code > 0xFFFF) {
1289            die "time to switch casefold_table to gunichar" ;
1290        }
1291
1292        print $out sprintf(qq(  { 0x%04x, "$string" },\n), $code);
1293     
1294    }
1295
1296     print $out <<EOT;
1297 };
1298
1299 EOT
1300
1301    my $recordlen = (2+$casefoldlen+1) & ~1;
1302    printf "Generated %d bytes for casefold table\n", $recordlen * @casefold;
1303 }
1304
1305                              
1306