make third argument a gboolean. (g_scanner_add_symbol,
[glib.git] / glib / gen-unicode-tables.pl
blob47423b2d4b4ddd5ef2a27ede1f1b38cd0d708de5
1 #! /usr/bin/perl -w
3 # Copyright (C) 1998, 1999 Tom Tromey
4 # Copyright (C) 2001 Red Hat Software
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.
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.
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.
21 # gen-unicode-tables.pl - Generate tables for libunicode from Unicode data.
22 # See http://www.unicode.org/Public/UNIDATA/UnicodeCharacterDatabase.html
23 # Usage: gen-unicode-tables.pl [-decomp | -both] UNICODE-VERSION UnicodeData.txt LineBreak.txt SpecialCasing.txt CaseFolding.txt
24 # I consider the output of this program to be unrestricted. Use it as
25 # you will.
27 # FIXME:
28 # * We could save even more space in the generated table by using
29 # indexes and not pointers.
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.
33 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);
35 # Names of fields in Unicode data table.
36 $CODE = 0;
37 $NAME = 1;
38 $CATEGORY = 2;
39 $COMBINING_CLASSES = 3;
40 $BIDI_CATEGORY = 4;
41 $DECOMPOSITION = 5;
42 $DECIMAL_VALUE = 6;
43 $DIGIT_VALUE = 7;
44 $NUMERIC_VALUE = 8;
45 $MIRRORED = 9;
46 $OLD_NAME = 10;
47 $COMMENT = 11;
48 $UPPER = 12;
49 $LOWER = 13;
50 $TITLE = 14;
52 # Names of fields in the line break table
53 $BREAK_CODE = 0;
54 $BREAK_PROPERTY = 1;
56 # Names of fields in the SpecialCasing table
57 $CASE_CODE = 0;
58 $CASE_LOWER = 1;
59 $CASE_TITLE = 2;
60 $CASE_UPPER = 3;
61 $CASE_CONDITION = 4;
63 # Names of fields in the CaseFolding table
64 $FOLDING_CODE = 0;
65 $FOLDING_STATUS = 1;
66 $FOLDING_MAPPING = 2;
68 # Map general category code onto symbolic name.
69 %mappings =
71 # Normative.
72 'Lu' => "G_UNICODE_UPPERCASE_LETTER",
73 'Ll' => "G_UNICODE_LOWERCASE_LETTER",
74 'Lt' => "G_UNICODE_TITLECASE_LETTER",
75 'Mn' => "G_UNICODE_NON_SPACING_MARK",
76 'Mc' => "G_UNICODE_COMBINING_MARK",
77 'Me' => "G_UNICODE_ENCLOSING_MARK",
78 'Nd' => "G_UNICODE_DECIMAL_NUMBER",
79 'Nl' => "G_UNICODE_LETTER_NUMBER",
80 'No' => "G_UNICODE_OTHER_NUMBER",
81 'Zs' => "G_UNICODE_SPACE_SEPARATOR",
82 'Zl' => "G_UNICODE_LINE_SEPARATOR",
83 'Zp' => "G_UNICODE_PARAGRAPH_SEPARATOR",
84 'Cc' => "G_UNICODE_CONTROL",
85 'Cf' => "G_UNICODE_FORMAT",
86 'Cs' => "G_UNICODE_SURROGATE",
87 'Co' => "G_UNICODE_PRIVATE_USE",
88 'Cn' => "G_UNICODE_UNASSIGNED",
90 # Informative.
91 'Lm' => "G_UNICODE_MODIFIER_LETTER",
92 'Lo' => "G_UNICODE_OTHER_LETTER",
93 'Pc' => "G_UNICODE_CONNECT_PUNCTUATION",
94 'Pd' => "G_UNICODE_DASH_PUNCTUATION",
95 'Ps' => "G_UNICODE_OPEN_PUNCTUATION",
96 'Pe' => "G_UNICODE_CLOSE_PUNCTUATION",
97 'Pi' => "G_UNICODE_INITIAL_PUNCTUATION",
98 'Pf' => "G_UNICODE_FINAL_PUNCTUATION",
99 'Po' => "G_UNICODE_OTHER_PUNCTUATION",
100 'Sm' => "G_UNICODE_MATH_SYMBOL",
101 'Sc' => "G_UNICODE_CURRENCY_SYMBOL",
102 'Sk' => "G_UNICODE_MODIFIER_SYMBOL",
103 'So' => "G_UNICODE_OTHER_SYMBOL"
106 %break_mappings =
108 'BK' => "G_UNICODE_BREAK_MANDATORY",
109 'CR' => "G_UNICODE_BREAK_CARRIAGE_RETURN",
110 'LF' => "G_UNICODE_BREAK_LINE_FEED",
111 'CM' => "G_UNICODE_BREAK_COMBINING_MARK",
112 'SG' => "G_UNICODE_BREAK_SURROGATE",
113 'ZW' => "G_UNICODE_BREAK_ZERO_WIDTH_SPACE",
114 'IN' => "G_UNICODE_BREAK_INSEPARABLE",
115 'GL' => "G_UNICODE_BREAK_NON_BREAKING_GLUE",
116 'CB' => "G_UNICODE_BREAK_CONTINGENT",
117 'SP' => "G_UNICODE_BREAK_SPACE",
118 'BA' => "G_UNICODE_BREAK_AFTER",
119 'BB' => "G_UNICODE_BREAK_BEFORE",
120 'B2' => "G_UNICODE_BREAK_BEFORE_AND_AFTER",
121 'HY' => "G_UNICODE_BREAK_HYPHEN",
122 'NS' => "G_UNICODE_BREAK_NON_STARTER",
123 'OP' => "G_UNICODE_BREAK_OPEN_PUNCTUATION",
124 'CL' => "G_UNICODE_BREAK_CLOSE_PUNCTUATION",
125 'QU' => "G_UNICODE_BREAK_QUOTATION",
126 'EX' => "G_UNICODE_BREAK_EXCLAMATION",
127 'ID' => "G_UNICODE_BREAK_IDEOGRAPHIC",
128 'NU' => "G_UNICODE_BREAK_NUMERIC",
129 'IS' => "G_UNICODE_BREAK_INFIX_SEPARATOR",
130 'SY' => "G_UNICODE_BREAK_SYMBOL",
131 'AL' => "G_UNICODE_BREAK_ALPHABETIC",
132 'PR' => "G_UNICODE_BREAK_PREFIX",
133 'PO' => "G_UNICODE_BREAK_POSTFIX",
134 'SA' => "G_UNICODE_BREAK_COMPLEX_CONTEXT",
135 'AI' => "G_UNICODE_BREAK_AMBIGUOUS",
136 'XX' => "G_UNICODE_BREAK_UNKNOWN"
139 # Title case mappings.
140 %title_to_lower = ();
141 %title_to_upper = ();
143 # Maximum length of special-case strings
145 my $special_case_len = 0;
146 my @special_cases;
148 $do_decomp = 0;
149 $do_props = 1;
150 if (@ARGV && $ARGV[0] eq '-decomp')
152 $do_decomp = 1;
153 $do_props = 0;
154 shift @ARGV;
156 elsif (@ARGV && $ARGV[0] eq '-both')
158 $do_decomp = 1;
159 shift @ARGV;
162 if (@ARGV != 6) {
163 $0 =~ s@.*/@@;
164 die "Usage: $0 [-decomp | -both] UNICODE-VERSION UnicodeData.txt LineBreak.txt SpecialCasing.txt CaseFolding.txt CompositionExclusions.txt\n";
167 print "Creating decomp table\n" if ($do_decomp);
168 print "Creating property table\n" if ($do_props);
170 print "Composition exlusions from $ARGV[5]\n";
172 open (INPUT, "< $ARGV[5]") || exit 1;
174 while (<INPUT>) {
176 chop;
178 next if /^#/;
179 next if /^\s*$/;
181 s/\s*#.*//;
183 s/^\s*//;
184 s/\s*$//;
186 $composition_exclusions{hex($_)} = 1;
189 close INPUT;
191 print "Unicode data from $ARGV[1]\n";
193 open (INPUT, "< $ARGV[1]") || exit 1;
195 $last_code = -1;
196 while (<INPUT>)
198 chop;
199 @fields = split (';', $_, 30);
200 if ($#fields != 14)
202 printf STDERR ("Entry for $fields[$CODE] has wrong number of fields (%d)\n", $#fields);
205 $code = hex ($fields[$CODE]);
207 last if ($code > 0xFFFF); # ignore characters out of the basic plane
209 if ($code > $last_code + 1)
211 # Found a gap.
212 if ($fields[$NAME] =~ /Last>/)
214 # Fill the gap with the last character read,
215 # since this was a range specified in the char database
216 @gfields = @fields;
218 else
220 # The gap represents undefined characters. Only the type
221 # matters.
222 @gfields = ('', '', 'Cn', '0', '', '', '', '', '', '', '',
223 '', '', '', '');
225 for (++$last_code; $last_code < $code; ++$last_code)
227 $gfields{$CODE} = sprintf ("%04x", $last_code);
228 &process_one ($last_code, @gfields);
231 &process_one ($code, @fields);
232 $last_code = $code;
235 close INPUT;
237 @gfields = ('', '', 'Cn', '0', '', '', '', '', '', '', '',
238 '', '', '', '');
239 for (++$last_code; $last_code < 0x10000; ++$last_code)
241 $gfields{$CODE} = sprintf ("%04x", $last_code);
242 &process_one ($last_code, @gfields);
244 --$last_code; # Want last to be 0xFFFF.
246 print "Creating line break table\n";
248 print "Line break data from $ARGV[2]\n";
250 open (INPUT, "< $ARGV[2]") || exit 1;
252 $last_code = -1;
253 while (<INPUT>)
255 my ($start_code, $end_code);
257 chop;
259 next if /^#/;
261 s/\s*#.*//;
263 @fields = split (';', $_, 30);
264 if ($#fields != 1)
266 printf STDERR ("Entry for $fields[$CODE] has wrong number of fields (%d)\n", $#fields);
267 next;
270 if ($fields[$CODE] =~ /([A-F0-9]{4})..([A-F0-9]{4})/)
272 $start_code = hex ($1);
273 $end_code = hex ($2);
274 } else {
275 $start_code = $end_code = hex ($fields[$CODE]);
279 last if ($start_code > 0xFFFF); # FIXME ignore characters out of the basic plane
281 if ($start_code > $last_code + 1)
283 # The gap represents undefined characters. If assigned,
284 # they are AL, if not assigned, XX
285 for (++$last_code; $last_code < $start_code; ++$last_code)
287 if ($type[$last_code] eq 'Cn')
289 $break_props[$last_code] = 'XX';
291 else
293 $break_props[$last_code] = 'AL';
298 for ($last_code = $start_code; $last_code <= $end_code; $last_code++)
300 $break_props[$last_code] = $fields[$BREAK_PROPERTY];
303 $last_code = $end_code;
306 close INPUT;
308 for (++$last_code; $last_code < 0x10000; ++$last_code)
310 if ($type[$last_code] eq 'Cn')
312 $break_props[$last_code] = 'XX';
314 else
316 $break_props[$last_code] = 'AL';
319 --$last_code; # Want last to be 0xFFFF.
321 print STDERR "Last code is not 0xFFFF" if ($last_code != 0xFFFF);
323 print "Reading special-casing table for case conversion\n";
325 open (INPUT, "< $ARGV[3]") || exit 1;
327 while (<INPUT>)
329 my $code;
331 chop;
333 next if /^#/;
334 next if /^\s*$/;
336 s/\s*#.*//;
338 @fields = split ('\s*;\s*', $_, 30);
340 $raw_code = $fields[$CASE_CODE];
341 $code = hex ($raw_code);
343 if ($#fields != 4 && $#fields != 5)
345 printf STDERR ("Entry for $raw_code has wrong number of fields (%d)\n", $#fields);
346 next;
349 if (!defined $type[$code])
351 printf STDERR "Special case for code point: $code, which has no defined type\n";
352 next;
355 if (defined $fields[5]) {
356 # Ignore conditional special cases - we'll handle them in code
357 next;
360 if ($type[$code] eq 'Lu')
362 (hex $fields[$CASE_UPPER] == $code) || die "$raw_code is Lu and UCD_Upper($raw_code) != $raw_code";
364 &add_special_case ($code, $value[$code],$fields[$CASE_LOWER], $fields[$CASE_TITLE]);
366 } elsif ($type[$code] eq 'Lt')
368 (hex $fields[$CASE_TITLE] == $code) || die "$raw_code is Lt and UCD_Title($raw_code) != $raw_code";
370 &add_special_case ($code, undef,$fields[$CASE_LOWER], $fields[$CASE_UPPER]);
371 } elsif ($type[$code] eq 'Ll')
373 (hex $fields[$CASE_LOWER] == $code) || die "$raw_code is Ll and UCD_Lower($raw_code) != $raw_code";
375 &add_special_case ($code, $value[$code],$fields[$CASE_UPPER], $fields[$CASE_TITLE]);
376 } else {
377 printf STDERR "Special case for non-alphabetic code point: $raw_code\n";
378 next;
382 close INPUT;
384 open (INPUT, "< $ARGV[4]") || exit 1;
386 my $casefoldlen = 0;
387 my @casefold;
389 while (<INPUT>)
391 my $code;
393 chop;
395 next if /^#/;
396 next if /^\s*$/;
398 s/\s*#.*//;
400 @fields = split ('\s*;\s*', $_, 30);
402 $raw_code = $fields[$FOLDING_CODE];
403 $code = hex ($raw_code);
405 next if $code > 0xffff; # FIXME!
407 if ($#fields != 3)
409 printf STDERR ("Entry for $raw_code has wrong number of fields (%d)\n", $#fields);
410 next;
413 next if ($fields[$FOLDING_STATUS] eq 'S');
415 @values = map { hex ($_) } split /\s+/, $fields[$FOLDING_MAPPING];
417 # Check simple case
419 if (@values == 1 &&
420 !(defined $value[$code] && $value[$code] >= 0xd800 && $value[$code] < 0xdc00) &&
421 defined $type[$code]) {
423 my $lower;
424 if ($type[$code] eq 'Ll')
426 $lower = $code;
427 } elsif ($type[$code] eq 'Lt')
429 $lower = $title_to_lower{$code};
430 } elsif ($type[$code] eq 'Lu')
432 $lower = $value[$code];
433 } else {
434 $lower = $code;
437 if ($lower == $values[0]) {
438 next;
442 my $string = pack ("U*", @values);
443 if (1 + length $string > $casefoldlen) {
444 $casefoldlen = 1 + length $string;
447 push @casefold, [ $code, $string ];
450 close INPUT;
452 if ($do_props) {
453 &print_tables ($last_code)
455 if ($do_decomp) {
456 &print_decomp ($last_code);
457 &output_composition_table;
460 &print_line_break ($last_code);
462 exit 0;
464 # Process a single character.
465 sub process_one
467 my ($code, @fields) = @_;
469 $type[$code] = $fields[$CATEGORY];
470 if ($type[$code] eq 'Nd')
472 $value[$code] = int ($fields[$DECIMAL_VALUE]);
474 elsif ($type[$code] eq 'Ll')
476 $value[$code] = hex ($fields[$UPPER]);
478 elsif ($type[$code] eq 'Lu')
480 $value[$code] = hex ($fields[$LOWER]);
483 if ($type[$code] eq 'Lt')
485 $title_to_lower{$code} = hex ($fields[$LOWER]);
486 $title_to_upper{$code} = hex ($fields[$UPPER]);
489 $cclass[$code] = $fields[$COMBINING_CLASSES];
491 # Handle decompositions.
492 if ($fields[$DECOMPOSITION] ne '')
494 if ($fields[$DECOMPOSITION] =~ s/\<.*\>\s*//) {
495 $decompose_compat[$code] = 1;
496 } else {
497 $decompose_compat[$code] = 0;
499 if (!exists $composition_exclusions{$code}) {
500 $compositions{$code} = $fields[$DECOMPOSITION];
503 $decompositions[$code] = $fields[$DECOMPOSITION];
507 sub print_tables
509 my ($last) = @_;
510 my ($outfile) = "gunichartables.h";
512 local ($bytes_out) = 0;
514 print "Writing $outfile...\n";
516 open (OUT, "> $outfile");
518 print OUT "/* This file is automatically generated. DO NOT EDIT!\n";
519 print OUT " Instead, edit gen-unicode-tables.pl and re-run. */\n\n";
521 print OUT "#ifndef CHARTABLES_H\n";
522 print OUT "#define CHARTABLES_H\n\n";
524 print OUT "#define G_UNICODE_DATA_VERSION \"$ARGV[0]\"\n\n";
526 printf OUT "#define G_UNICODE_LAST_CHAR 0x%04x\n\n", $last;
528 for ($count = 0; $count <= $last; $count += 256)
530 $row[$count / 256] = &print_row ($count, '(char *) ', 'char', 1,
531 'page', \&fetch_type);
534 print OUT "static const char *type_table[256] = {\n";
535 for ($count = 0; $count <= $last; $count += 256)
537 print OUT ",\n" if $count > 0;
538 print OUT " ", $row[$count / 256];
539 $bytes_out += 4;
541 print OUT "\n};\n\n";
545 # Now print attribute table.
548 for ($count = 0; $count <= $last; $count += 256)
550 $row[$count / 256] = &print_row ($count, '', 'unsigned short', 2,
551 'attrpage', \&fetch_attr);
553 print OUT "static const unsigned short *attr_table[256] = {\n";
554 for ($count = 0; $count <= $last; $count += 256)
556 print OUT ",\n" if $count > 0;
557 print OUT " ", $row[$count / 256];
558 $bytes_out += 4;
560 print OUT "\n};\n\n";
563 # print title case table
566 # FIXME: type.
567 print OUT "static const unsigned short title_table[][3] = {\n";
568 my ($item);
569 my ($first) = 1;
570 foreach $item (sort keys %title_to_lower)
572 print OUT ",\n"
573 unless $first;
574 $first = 0;
575 printf OUT " { 0x%04x, 0x%04x, 0x%04x }", $item, $title_to_upper{$item}, $title_to_lower{$item};
576 $bytes_out += 6;
578 print OUT "\n};\n\n";
581 # And special case conversion table -- conversions that change length
583 &output_special_case_table (\*OUT);
584 &output_casefold_table (\*OUT);
586 print OUT "#endif /* CHARTABLES_H */\n";
588 close (OUT);
590 printf STDERR "Generated %d bytes in tables\n", $bytes_out;
593 # A fetch function for the type table.
594 sub fetch_type
596 my ($index) = @_;
597 return $mappings{$type[$index]};
600 # A fetch function for the attribute table.
601 sub fetch_attr
603 my ($index) = @_;
604 if (defined $value[$index])
606 return sprintf ("0x%04x", $value[$index]);
608 else
610 return "0x0000";
614 # Print a single "row" of a two-level table.
615 sub print_row
617 my ($start, $def_pfx, $typname, $typsize, $name, $fetcher) = @_;
619 my ($i);
620 my (@values);
621 my ($flag) = 1;
622 my ($off);
624 for ($off = 0; $off < 256; ++$off)
626 $values[$off] = $fetcher->($off + $start);
627 if ($values[$off] ne $values[0])
629 $flag = 0;
632 if ($flag)
634 return $def_pfx . $values[0];
637 printf OUT "static const %s %s%d[256] = {\n ", $typname, $name, $start / 256;
638 my ($column) = 2;
639 for ($i = $start; $i < $start + 256; ++$i)
641 print OUT ", "
642 if $i > $start;
643 my ($text) = $values[$i - $start];
644 if (length ($text) + $column + 2 > 78)
646 print OUT "\n ";
647 $column = 2;
649 print OUT $text;
650 $column += length ($text) + 2;
652 print OUT "\n};\n\n";
654 $bytes_out += 256 * $typsize;
656 return sprintf "%s%d", $name, $start / 256;
659 # Generate the character decomposition header.
660 sub print_decomp
662 my ($last) = @_;
663 my ($outfile) = "gunidecomp.h";
665 local ($bytes_out) = 0;
667 print "Writing $outfile...\n";
669 open (OUT, "> $outfile") || exit 1;
671 print OUT "/* This file is automatically generated. DO NOT EDIT! */\n\n";
672 print OUT "#ifndef DECOMP_H\n";
673 print OUT "#define DECOMP_H\n\n";
675 printf OUT "#define G_UNICODE_LAST_CHAR 0x%04x\n\n", $last;
677 my ($count, @row);
678 for ($count = 0; $count <= $last; $count += 256)
680 $row[$count / 256] = &print_row ($count, '(unsigned char *) ',
681 'unsigned char', 1, 'cclass',
682 \&fetch_cclass);
685 print OUT "static const unsigned char *combining_class_table[256] = {\n";
686 for ($count = 0; $count <= $last; $count += 256)
688 print OUT ",\n" if $count > 0;
689 print OUT " ", $row[$count / 256];
690 $bytes_out += 4;
692 print OUT "\n};\n\n";
694 print OUT "typedef struct\n{\n";
695 # FIXME: type.
696 print OUT " unsigned short ch;\n";
697 print OUT " unsigned char canon_offset;\n";
698 print OUT " unsigned char compat_offset;\n";
699 print OUT " unsigned char *expansion;\n";
700 print OUT "} decomposition;\n\n";
702 print OUT "static const decomposition decomp_table[] =\n{\n";
703 my ($iter);
704 my ($first) = 1;
705 for ($count = 0; $count <= $last; ++$count)
707 if (defined $decompositions[$count])
709 print OUT ",\n"
710 if ! $first;
711 $first = 0;
713 my $canon_decomp;
714 my $compat_decomp;
716 if (!$decompose_compat[$count]) {
717 $canon_decomp = make_decomp ($count, 0);
719 $compat_decomp = make_decomp ($count, 1);
721 if (defined $canon_decomp && $compat_decomp eq $canon_decomp) {
722 undef $compat_decomp;
725 my $string = "";
726 my $canon_offset = 0xff;
727 my $compat_offset = 0xff;
729 if (defined $canon_decomp) {
730 $canon_offset = 0;
731 $string .= $canon_decomp;
733 if (defined $compat_decomp) {
734 if (defined $canon_decomp) {
735 $string .= "\\x00\\x00";
737 $compat_offset = (length $string) / 4;
738 $string .= $compat_decomp;
741 $bytes_out += (length $string) / 4; # "\x20"
743 # Only a single terminator because one is implied in the string.
744 printf OUT qq( { 0x%04x, %u, %u, "%s\\0" }),
745 $count, $canon_offset, $compat_offset, $string;
748 $bytes_out += 6;
751 print OUT "\n};\n\n";
753 print OUT "#endif /* DECOMP_H */\n";
755 printf STDERR "Generated %d bytes in decomp tables\n", $bytes_out;
758 sub print_line_break
760 my ($last) = @_;
761 my ($outfile) = "gunibreak.h";
763 local ($bytes_out) = 0;
765 print "Writing $outfile...\n";
767 open (OUT, "> $outfile");
769 print OUT "/* This file is automatically generated. DO NOT EDIT!\n";
770 print OUT " Instead, edit gen-unicode-tables.pl and re-run. */\n\n";
772 print OUT "#ifndef BREAKTABLES_H\n";
773 print OUT "#define BREAKTABLES_H\n\n";
775 print OUT "#define G_UNICODE_DATA_VERSION \"$ARGV[0]\"\n\n";
777 printf OUT "#define G_UNICODE_LAST_CHAR 0x%04x\n\n", $last;
779 for ($count = 0; $count <= $last; $count += 256)
781 $row[$count / 256] = &print_row ($count, '(char *) ', 'char', 1,
782 'page',
783 \&fetch_break_type);
786 print OUT "static const char *break_property_table[256] = {\n";
787 for ($count = 0; $count <= $last; $count += 256)
789 print OUT ",\n" if $count > 0;
790 print OUT " ", $row[$count / 256];
791 $bytes_out += 4;
793 print OUT "\n};\n\n";
795 print OUT "#endif /* BREAKTABLES_H */\n";
797 close (OUT);
799 printf STDERR "Generated %d bytes in break tables\n", $bytes_out;
803 # A fetch function for the break properties table.
804 sub fetch_break_type
806 my ($index) = @_;
807 return $break_mappings{$break_props[$index]};
810 # Fetcher for combining class.
811 sub fetch_cclass
813 my ($i) = @_;
814 return $cclass[$i];
817 # Expand a character decomposition recursively.
818 sub expand_decomp
820 my ($code, $compat) = @_;
822 my ($iter, $val);
823 my (@result) = ();
824 foreach $iter (split (' ', $decompositions[$code]))
826 $val = hex ($iter);
827 if (defined $decompositions[$val] &&
828 ($compat || !$decompose_compat[$val]))
830 push (@result, &expand_decomp ($val, $compat));
832 else
834 push (@result, $val);
838 return @result;
841 sub make_decomp
843 my ($code, $compat) = @_;
845 my $result = "";
846 foreach $iter (&expand_decomp ($code, $compat))
848 $result .= sprintf "\\x%02x\\x%02x", $iter / 256, $iter & 0xff;
851 $result;
853 # Generate special case data string from two fields
854 sub add_special_case
856 my ($code, $single, $field1, $field2) = @_;
858 @values = (defined $single ? $single : (),
859 (map { hex ($_) } split /\s+/, $field1),
861 (map { hex ($_) } split /\s+/, $field2));
862 $result = "";
865 for $value (@values) {
866 $result .= sprintf ("\\x%02x\\x%02x", $value / 256, $value & 0xff);
869 $result .= "\\0";
871 if (2 * @values + 2 > $special_case_len) {
872 $special_case_len = 2 * @values + 2;
875 push @special_cases, $result;
878 # We encode special cases in the surrogate pair space
880 $value[$code] = 0xD800 + scalar(@special_cases) - 1;
883 sub output_special_case_table
885 my $out = shift;
887 print $out <<EOT;
889 /* Table of special cases for case conversion; each record contains
890 * First, the best single character mapping to lowercase if Lu,
891 * and to uppercase if Ll, followed by the output mapping for the two cases
892 * other than the case of the codepoint, in the order [Ll],[Lu],[Lt],
893 * separated and terminated by a double NUL.
895 static const guchar special_case_table[][$special_case_len] = {
898 for $case (@special_cases) {
899 print $out qq( "$case",\n);
902 print $out <<EOT;
907 print STDERR "Generated ", ($special_case_len * scalar @special_cases), " bytes in special case table\n";
910 sub enumerate_ordered
912 my ($array) = @_;
914 my $n = 0;
915 for my $code (sort { $a <=> $b } keys %$array) {
916 if ($array->{$code} == 1) {
917 delete $array->{$code};
918 next;
920 $array->{$code} = $n++;
923 return $n;
926 sub output_composition_table
928 print STDERR "Generating composition table\n";
930 local ($bytes_out) = 0;
932 my %first;
933 my %second;
935 # First we need to go through and remove decompositions
936 # starting with a non-starter, and single-character
937 # decompositions. At the same time, record
938 # the first and second character of each decomposition
940 for $code (keys %compositions) {
941 @values = map { hex ($_) } split /\s+/, $compositions{$code};
942 if ($cclass[$values[0]]) {
943 delete $compositions{$code};
944 next;
946 if (@values == 1) {
947 delete $compositions{$code};
948 next;
950 if (@values != 2) {
951 die "$code has more than two elements in its decomposition!\n";
954 if (exists $first{$values[0]}) {
955 $first{$values[0]}++;
956 } else {
957 $first{$values[0]} = 1;
961 # Assign integer indicices, removing singletons
962 my $n_first = enumerate_ordered (\%first);
964 # Now record the second character if each (non-singleton) decomposition
965 for $code (keys %compositions) {
966 @values = map { hex ($_) } split /\s+/, $compositions{$code};
968 if (exists $first{$values[0]}) {
969 if (exists $second{$values[1]}) {
970 $second{$values[1]}++;
971 } else {
972 $second{$values[1]} = 1;
977 # Assign integer indices, removing duplicate
978 my $n_second = enumerate_ordered (\%second);
980 # Build reverse table
982 my @first_singletons;
983 my @second_singletons;
984 my %reverse;
985 for $code (keys %compositions) {
986 @values = map { hex ($_) } split /\s+/, $compositions{$code};
988 my $first = $first{$values[0]};
989 my $second = $second{$values[1]};
991 if (defined $first && defined $second) {
992 $reverse{"$first|$second"} = $code;
993 } elsif (!defined $first) {
994 push @first_singletons, [ $values[0], $values[1], $code ];
995 } else {
996 push @second_singletons, [ $values[1], $values[0], $code ];
1000 @first_singletons = sort { $a->[0] <=> $b->[0] } @first_singletons;
1001 @second_singletons = sort { $a->[0] <=> $b->[0] } @second_singletons;
1003 my %vals;
1005 open OUT, ">gunicomp.h" or die "Cannot open gunicomp.h: $!\n";
1007 # Assign values in lookup table for all code points involved
1009 my $total = 1;
1010 my $last = 0;
1011 printf OUT "#define COMPOSE_FIRST_START %d\n", $total;
1012 for $code (keys %first) {
1013 $vals{$code} = $first{$code} + $total;
1014 $last = $code if $code > $last;
1016 $total += $n_first;
1017 $i = 0;
1018 printf OUT "#define COMPOSE_FIRST_SINGLE_START %d\n", $total;
1019 for $record (@first_singletons) {
1020 my $code = $record->[0];
1021 $vals{$code} = $i++ + $total;
1022 $last = $code if $code > $last;
1024 $total += @first_singletons;
1025 printf OUT "#define COMPOSE_SECOND_START %d\n", $total;
1026 for $code (keys %second) {
1027 $vals{$code} = $second{$code} + $total;
1028 $last = $code if $code > $last;
1030 $total += $n_second;
1031 $i = 0;
1032 printf OUT "#define COMPOSE_SECOND_SINGLE_START %d\n\n", $total;
1033 for $record (@second_singletons) {
1034 my $code = $record->[0];
1035 $vals{$code} = $i++ + $total;
1036 $last = $code if $code > $last;
1039 # Output lookup table
1041 my @row;
1042 for (my $count = 0; $count <= $last; $count += 256)
1044 $row[$count / 256] = &print_row ($count, '(gushort *) ', 'gushort', 2,
1045 'compose_page',
1046 sub { exists $vals{$_[0]} ? $vals{$_[0]} : 0; });
1049 print OUT "static const unsigned short *compose_table[256] = {\n";
1050 for (my $count = 0; $count <= $last; $count += 256)
1052 print OUT ",\n" if $count > 0;
1053 print OUT " ", $row[$count / 256];
1054 $bytes_out += 4;
1056 print OUT "\n};\n\n";
1058 # Output first singletons
1060 print OUT "static const gushort compose_first_single[][2] = {\n";
1061 $i = 0;
1062 for $record (@first_singletons) {
1063 print OUT ",\n" if $i++ > 0;
1064 printf OUT " { %#06x, %#06x }", $record->[1], $record->[2];
1066 print OUT "\n};\n";
1068 $bytes_out += @first_singletons * 4;
1070 # Output second singletons
1072 print OUT "static const gushort compose_second_single[][2] = {\n";
1073 $i = 0;
1074 for $record (@second_singletons) {
1075 print OUT ",\n" if $i++ > 0;
1076 printf OUT " { %#06x, %#06x }", $record->[1], $record->[2];
1078 print OUT "\n};\n";
1080 $bytes_out += @second_singletons * 4;
1082 # Output array of composition pairs
1084 print OUT <<EOT;
1085 static const gushort compose_array[$n_first][$n_second] = {
1088 for (my $i = 0; $i < $n_first; $i++) {
1089 print OUT ",\n" if $i;
1090 print OUT " { ";
1091 for (my $j = 0; $j < $n_second; $j++) {
1092 print OUT ", " if $j;
1093 if (exists $reverse{"$i|$j"}) {
1094 printf OUT "%#06x", $reverse{"$i|$j"};
1095 } else {
1096 print OUT " 0";
1099 print OUT " }";
1101 print OUT "\n";
1103 print OUT <<EOT;
1107 $bytes_out += $n_first * $n_second * 2;
1109 printf STDERR "Generated %d bytes in compose tables\n", $bytes_out;
1112 sub output_casefold_table
1114 my $out = shift;
1116 print $out <<EOT;
1118 /* Table of casefolding cases that can't be derived by lowercasing
1120 static const struct {
1121 guint16 ch;
1122 gchar data[$casefoldlen];
1123 } casefold_table[] = {
1126 @casefold = sort { $a->[0] <=> $b->[0] } @casefold;
1128 for $case (@casefold) {
1129 $code = $case->[0];
1130 $string = $case->[1];
1131 print $out sprintf(qq({ %#04x, "$string" },\n), $code);
1135 print $out <<EOT;
1140 my $recordlen = (2+$casefoldlen+1) & ~1;
1141 printf "Generated %d bytes for casefold table\n", $recordlen * @casefold;