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)
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
22 # Andrew Taylor <andrew.taylor@montage.ca>
24 # gen-unicode-tables.pl - Generate tables for libunicode from Unicode data.
25 # See http://www.unicode.org/Public/UNIDATA/UnicodeCharacterDatabase.html
26 # Usage: gen-unicode-tables.pl [-decomp | -both] UNICODE-VERSION UnicodeData.txt LineBreak.txt SpecialCasing.txt CaseFolding.txt
27 # I consider the output of this program to be unrestricted. Use it as
31 # * For decomp table it might make sense to use a shift count other
32 # than 8. We could easily compute the perfect shift count.
34 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);
36 # Names of fields in Unicode data table.
40 $COMBINING_CLASSES = 3;
53 # Names of fields in the line break table
57 # Names of fields in the SpecialCasing table
64 # Names of fields in the CaseFolding table
69 # Map general category code onto symbolic name.
73 'Lu' => "G_UNICODE_UPPERCASE_LETTER",
74 'Ll' => "G_UNICODE_LOWERCASE_LETTER",
75 'Lt' => "G_UNICODE_TITLECASE_LETTER",
76 'Mn' => "G_UNICODE_NON_SPACING_MARK",
77 'Mc' => "G_UNICODE_COMBINING_MARK",
78 'Me' => "G_UNICODE_ENCLOSING_MARK",
79 'Nd' => "G_UNICODE_DECIMAL_NUMBER",
80 'Nl' => "G_UNICODE_LETTER_NUMBER",
81 'No' => "G_UNICODE_OTHER_NUMBER",
82 'Zs' => "G_UNICODE_SPACE_SEPARATOR",
83 'Zl' => "G_UNICODE_LINE_SEPARATOR",
84 'Zp' => "G_UNICODE_PARAGRAPH_SEPARATOR",
85 'Cc' => "G_UNICODE_CONTROL",
86 'Cf' => "G_UNICODE_FORMAT",
87 'Cs' => "G_UNICODE_SURROGATE",
88 'Co' => "G_UNICODE_PRIVATE_USE",
89 'Cn' => "G_UNICODE_UNASSIGNED",
92 'Lm' => "G_UNICODE_MODIFIER_LETTER",
93 'Lo' => "G_UNICODE_OTHER_LETTER",
94 'Pc' => "G_UNICODE_CONNECT_PUNCTUATION",
95 'Pd' => "G_UNICODE_DASH_PUNCTUATION",
96 'Ps' => "G_UNICODE_OPEN_PUNCTUATION",
97 'Pe' => "G_UNICODE_CLOSE_PUNCTUATION",
98 'Pi' => "G_UNICODE_INITIAL_PUNCTUATION",
99 'Pf' => "G_UNICODE_FINAL_PUNCTUATION",
100 'Po' => "G_UNICODE_OTHER_PUNCTUATION",
101 'Sm' => "G_UNICODE_MATH_SYMBOL",
102 'Sc' => "G_UNICODE_CURRENCY_SYMBOL",
103 'Sk' => "G_UNICODE_MODIFIER_SYMBOL",
104 'So' => "G_UNICODE_OTHER_SYMBOL"
109 'BK' => "G_UNICODE_BREAK_MANDATORY",
110 'CR' => "G_UNICODE_BREAK_CARRIAGE_RETURN",
111 'LF' => "G_UNICODE_BREAK_LINE_FEED",
112 'CM' => "G_UNICODE_BREAK_COMBINING_MARK",
113 'SG' => "G_UNICODE_BREAK_SURROGATE",
114 'ZW' => "G_UNICODE_BREAK_ZERO_WIDTH_SPACE",
115 'IN' => "G_UNICODE_BREAK_INSEPARABLE",
116 'GL' => "G_UNICODE_BREAK_NON_BREAKING_GLUE",
117 'CB' => "G_UNICODE_BREAK_CONTINGENT",
118 'SP' => "G_UNICODE_BREAK_SPACE",
119 'BA' => "G_UNICODE_BREAK_AFTER",
120 'BB' => "G_UNICODE_BREAK_BEFORE",
121 'B2' => "G_UNICODE_BREAK_BEFORE_AND_AFTER",
122 'HY' => "G_UNICODE_BREAK_HYPHEN",
123 'NS' => "G_UNICODE_BREAK_NON_STARTER",
124 'OP' => "G_UNICODE_BREAK_OPEN_PUNCTUATION",
125 'CL' => "G_UNICODE_BREAK_CLOSE_PUNCTUATION",
126 'QU' => "G_UNICODE_BREAK_QUOTATION",
127 'EX' => "G_UNICODE_BREAK_EXCLAMATION",
128 'ID' => "G_UNICODE_BREAK_IDEOGRAPHIC",
129 'NU' => "G_UNICODE_BREAK_NUMERIC",
130 'IS' => "G_UNICODE_BREAK_INFIX_SEPARATOR",
131 'SY' => "G_UNICODE_BREAK_SYMBOL",
132 'AL' => "G_UNICODE_BREAK_ALPHABETIC",
133 'PR' => "G_UNICODE_BREAK_PREFIX",
134 'PO' => "G_UNICODE_BREAK_POSTFIX",
135 'SA' => "G_UNICODE_BREAK_COMPLEX_CONTEXT",
136 'AI' => "G_UNICODE_BREAK_AMBIGUOUS",
137 'XX' => "G_UNICODE_BREAK_UNKNOWN"
140 # Title case mappings.
141 %title_to_lower = ();
142 %title_to_upper = ();
144 # Maximum length of special-case strings
146 my $special_case_len = 0;
151 if (@ARGV && $ARGV[0] eq '-decomp')
157 elsif (@ARGV && $ARGV[0] eq '-both')
165 die "Usage: $0 [-decomp | -both] UNICODE-VERSION UnicodeData.txt LineBreak.txt SpecialCasing.txt CaseFolding.txt CompositionExclusions.txt\n";
168 print "Creating decomp table\n" if ($do_decomp);
169 print "Creating property table\n" if ($do_props);
171 print "Composition exlusions from $ARGV[5]\n";
173 open (INPUT, "< $ARGV[5]") || exit 1;
187 $composition_exclusions{hex($_)} = 1;
192 print "Unicode data from $ARGV[1]\n";
194 open (INPUT, "< $ARGV[1]") || exit 1;
200 @fields = split (';', $_, 30);
203 printf STDERR ("Entry for $fields[$CODE] has wrong number of fields (%d)\n", $#fields);
206 $code = hex ($fields[$CODE]);
208 last if ($code > 0xFFFF); # ignore characters out of the basic plane
210 if ($code > $last_code + 1)
213 if ($fields[$NAME] =~ /Last>/)
215 # Fill the gap with the last character read,
216 # since this was a range specified in the char database
221 # The gap represents undefined characters. Only the type
223 @gfields = ('', '', 'Cn', '0', '', '', '', '', '', '', '',
226 for (++$last_code; $last_code < $code; ++$last_code)
228 $gfields{$CODE} = sprintf ("%04x", $last_code);
229 &process_one ($last_code, @gfields);
232 &process_one ($code, @fields);
238 @gfields = ('', '', 'Cn', '0', '', '', '', '', '', '', '',
240 for (++$last_code; $last_code < 0x10000; ++$last_code)
242 $gfields{$CODE} = sprintf ("%04x", $last_code);
243 &process_one ($last_code, @gfields);
245 --$last_code; # Want last to be 0xFFFF.
247 print "Creating line break table\n";
249 print "Line break data from $ARGV[2]\n";
251 open (INPUT, "< $ARGV[2]") || exit 1;
256 my ($start_code, $end_code);
264 @fields = split (';', $_, 30);
267 printf STDERR ("Entry for $fields[$CODE] has wrong number of fields (%d)\n", $#fields);
271 if ($fields[$CODE] =~ /([A-F0-9]{4})..([A-F0-9]{4})/)
273 $start_code = hex ($1);
274 $end_code = hex ($2);
276 $start_code = $end_code = hex ($fields[$CODE]);
280 last if ($start_code > 0xFFFF); # FIXME ignore characters out of the basic plane
282 if ($start_code > $last_code + 1)
284 # The gap represents undefined characters. If assigned,
285 # they are AL, if not assigned, XX
286 for (++$last_code; $last_code < $start_code; ++$last_code)
288 if ($type[$last_code] eq 'Cn')
290 $break_props[$last_code] = 'XX';
294 $break_props[$last_code] = 'AL';
299 for ($last_code = $start_code; $last_code <= $end_code; $last_code++)
301 $break_props[$last_code] = $fields[$BREAK_PROPERTY];
304 $last_code = $end_code;
309 for (++$last_code; $last_code < 0x10000; ++$last_code)
311 if ($type[$last_code] eq 'Cn')
313 $break_props[$last_code] = 'XX';
317 $break_props[$last_code] = 'AL';
320 --$last_code; # Want last to be 0xFFFF.
322 print STDERR "Last code is not 0xFFFF" if ($last_code != 0xFFFF);
324 print "Reading special-casing table for case conversion\n";
326 open (INPUT, "< $ARGV[3]") || exit 1;
339 @fields = split ('\s*;\s*', $_, 30);
341 $raw_code = $fields[$CASE_CODE];
342 $code = hex ($raw_code);
344 if ($#fields != 4 && $#fields != 5)
346 printf STDERR ("Entry for $raw_code has wrong number of fields (%d)\n", $#fields);
350 if (!defined $type[$code])
352 printf STDERR "Special case for code point: $code, which has no defined type\n";
356 if (defined $fields[5]) {
357 # Ignore conditional special cases - we'll handle them in code
361 if ($type[$code] eq 'Lu')
363 (hex $fields[$CASE_UPPER] == $code) || die "$raw_code is Lu and UCD_Upper($raw_code) != $raw_code";
365 &add_special_case ($code, $value[$code],$fields[$CASE_LOWER], $fields[$CASE_TITLE]);
367 } elsif ($type[$code] eq 'Lt')
369 (hex $fields[$CASE_TITLE] == $code) || die "$raw_code is Lt and UCD_Title($raw_code) != $raw_code";
371 &add_special_case ($code, undef,$fields[$CASE_LOWER], $fields[$CASE_UPPER]);
372 } elsif ($type[$code] eq 'Ll')
374 (hex $fields[$CASE_LOWER] == $code) || die "$raw_code is Ll and UCD_Lower($raw_code) != $raw_code";
376 &add_special_case ($code, $value[$code],$fields[$CASE_UPPER], $fields[$CASE_TITLE]);
378 printf STDERR "Special case for non-alphabetic code point: $raw_code\n";
385 open (INPUT, "< $ARGV[4]") || exit 1;
401 @fields = split ('\s*;\s*', $_, 30);
403 $raw_code = $fields[$FOLDING_CODE];
404 $code = hex ($raw_code);
406 next if $code > 0xffff; # FIXME!
410 printf STDERR ("Entry for $raw_code has wrong number of fields (%d)\n", $#fields);
414 next if ($fields[$FOLDING_STATUS] eq 'S');
416 @values = map { hex ($_) } split /\s+/, $fields[$FOLDING_MAPPING];
421 !(defined $value[$code] && $value[$code] >= 0xd800 && $value[$code] < 0xdc00) &&
422 defined $type[$code]) {
425 if ($type[$code] eq 'Ll')
428 } elsif ($type[$code] eq 'Lt')
430 $lower = $title_to_lower{$code};
431 } elsif ($type[$code] eq 'Lu')
433 $lower = $value[$code];
438 if ($lower == $values[0]) {
443 my $string = pack ("U*", @values);
444 if (1 + length $string > $casefoldlen) {
445 $casefoldlen = 1 + length $string;
448 push @casefold, [ $code, $string ];
454 &print_tables ($last_code)
457 &print_decomp ($last_code);
458 &output_composition_table;
461 &print_line_break ($last_code);
465 # Process a single character.
468 my ($code, @fields) = @_;
470 $type[$code] = $fields[$CATEGORY];
471 if ($type[$code] eq 'Nd')
473 $value[$code] = int ($fields[$DECIMAL_VALUE]);
475 elsif ($type[$code] eq 'Ll')
477 $value[$code] = hex ($fields[$UPPER]);
479 elsif ($type[$code] eq 'Lu')
481 $value[$code] = hex ($fields[$LOWER]);
484 if ($type[$code] eq 'Lt')
486 $title_to_lower{$code} = hex ($fields[$LOWER]);
487 $title_to_upper{$code} = hex ($fields[$UPPER]);
490 $cclass[$code] = $fields[$COMBINING_CLASSES];
492 # Handle decompositions.
493 if ($fields[$DECOMPOSITION] ne '')
495 if ($fields[$DECOMPOSITION] =~ s/\<.*\>\s*//) {
496 $decompose_compat[$code] = 1;
498 $decompose_compat[$code] = 0;
500 if (!exists $composition_exclusions{$code}) {
501 $compositions{$code} = $fields[$DECOMPOSITION];
504 $decompositions[$code] = $fields[$DECOMPOSITION];
511 my ($outfile) = "gunichartables.h";
513 local ($bytes_out) = 0;
515 print "Writing $outfile...\n";
517 open (OUT, "> $outfile");
519 print OUT "/* This file is automatically generated. DO NOT EDIT!\n";
520 print OUT " Instead, edit gen-unicode-tables.pl and re-run. */\n\n";
522 print OUT "#ifndef CHARTABLES_H\n";
523 print OUT "#define CHARTABLES_H\n\n";
525 print OUT "#define G_UNICODE_DATA_VERSION \"$ARGV[0]\"\n\n";
527 printf OUT "#define G_UNICODE_LAST_CHAR 0x%04x\n\n", $last;
529 printf OUT "#define G_UNICODE_MAX_TABLE_INDEX 1000\n\n";
532 printf OUT "static const char type_data[][256] = {\n";
533 for ($count = 0; $count <= $last; $count += 256)
535 $row[$count / 256] = &print_row ($count, 1, \&fetch_type);
537 printf OUT "\n};\n\n";
539 print OUT "static const short type_table[256] = {\n";
540 for ($count = 0; $count <= $last; $count += 256)
542 print OUT ",\n" if $count > 0;
543 print OUT " ", $row[$count / 256];
546 print OUT "\n};\n\n";
550 # Now print attribute table.
554 printf OUT "static const unsigned short attr_data[][256] = {\n";
555 for ($count = 0; $count <= $last; $count += 256)
557 $row[$count / 256] = &print_row ($count, 2, \&fetch_attr);
559 printf OUT "\n};\n\n";
561 print OUT "static const short attr_table[256] = {\n";
562 for ($count = 0; $count <= $last; $count += 256)
564 print OUT ",\n" if $count > 0;
565 print OUT " ", $row[$count / 256];
568 print OUT "\n};\n\n";
571 # print title case table
575 print OUT "static const unsigned short title_table[][3] = {\n";
578 foreach $item (sort keys %title_to_lower)
583 printf OUT " { 0x%04x, 0x%04x, 0x%04x }", $item, $title_to_upper{$item}, $title_to_lower{$item};
586 print OUT "\n};\n\n";
589 # And special case conversion table -- conversions that change length
591 &output_special_case_table (\*OUT);
592 &output_casefold_table (\*OUT);
594 print OUT "#endif /* CHARTABLES_H */\n";
598 printf STDERR "Generated %d bytes in tables\n", $bytes_out;
601 # A fetch function for the type table.
605 return $mappings{$type[$index]};
608 # A fetch function for the attribute table.
612 if (defined $value[$index])
614 return sprintf ("0x%04x", $value[$index]);
624 my ($start, $typsize, $fetcher) = @_;
631 for ($off = 0; $off < 256; ++$off)
633 $values[$off] = $fetcher->($off + $start);
634 if ($values[$off] ne $values[0])
641 return $values[0] . " + G_UNICODE_MAX_TABLE_INDEX";
644 printf OUT ",\n" if ($table_index != 0);
645 printf OUT " { /* page %d, index %d */\n ", $start / 256, $table_index;
647 for ($i = $start; $i < $start + 256; ++$i)
651 my ($text) = $values[$i - $start];
652 if (length ($text) + $column + 2 > 78)
658 $column += length ($text) + 2;
662 $bytes_out += 256 * $typsize;
664 return sprintf "%d /* page %d */", $table_index++, $start / 256;
667 # Generate the character decomposition header.
671 my ($outfile) = "gunidecomp.h";
673 local ($bytes_out) = 0;
675 print "Writing $outfile...\n";
677 open (OUT, "> $outfile") || exit 1;
679 print OUT "/* This file is automatically generated. DO NOT EDIT! */\n\n";
680 print OUT "#ifndef DECOMP_H\n";
681 print OUT "#define DECOMP_H\n\n";
683 printf OUT "#define G_UNICODE_LAST_CHAR 0x%04x\n\n", $last;
685 printf OUT "#define G_UNICODE_MAX_TABLE_INDEX 1000\n\n";
689 printf OUT "static const unsigned char cclass_data[][256] = {\n";
690 for ($count = 0; $count <= $last; $count += 256)
692 $row[$count / 256] = &print_row ($count, 1, \&fetch_cclass);
694 printf OUT "\n};\n\n";
696 print OUT "static const short combining_class_table[256] = {\n";
697 for ($count = 0; $count <= $last; $count += 256)
699 print OUT ",\n" if $count > 0;
700 print OUT " ", $row[$count / 256];
703 print OUT "\n};\n\n";
705 print OUT "typedef struct\n{\n";
707 print OUT " unsigned short ch;\n";
708 print OUT " unsigned char canon_offset;\n";
709 print OUT " unsigned char compat_offset;\n";
710 print OUT " unsigned short expansion_offset;\n";
711 print OUT "} decomposition;\n\n";
713 print OUT "static const decomposition decomp_table[] =\n{\n";
716 my ($decomp_string) = "";
717 my ($decomp_string_offset) = 0;
718 for ($count = 0; $count <= $last; ++$count)
720 if (defined $decompositions[$count])
729 if (!$decompose_compat[$count]) {
730 $canon_decomp = make_decomp ($count, 0);
732 $compat_decomp = make_decomp ($count, 1);
734 if (defined $canon_decomp && $compat_decomp eq $canon_decomp) {
735 undef $compat_decomp;
739 my $canon_offset = 0xff;
740 my $compat_offset = 0xff;
742 if (defined $canon_decomp) {
744 $string .= $canon_decomp;
746 if (defined $compat_decomp) {
747 if (defined $canon_decomp) {
748 $string .= "\\x00\\x00";
750 $compat_offset = (length $string) / 4;
751 $string .= $compat_decomp;
754 if (!defined($decomp_offsets{$string})) {
755 $decomp_offsets{$string} = $decomp_string_offset;
756 $decomp_string .= "\n \"".$string."\\0\\0\" /* offset ".
757 $decomp_string_offset." */";
758 $decomp_string_offset += ((length $string) / 4) + 2;
760 $bytes_out += (length $string) / 4 + 2; # "\x20"
763 printf OUT qq( { 0x%04x, %u, %u, %d }),
764 $count, $canon_offset, $compat_offset, $decomp_offsets{$string};
769 print OUT "\n};\n\n";
771 printf OUT "static const char decomp_expansion_string[] = %s;\n\n", $decomp_string;
773 print OUT "#endif /* DECOMP_H */\n";
775 printf STDERR "Generated %d bytes in decomp tables\n", $bytes_out;
781 my ($outfile) = "gunibreak.h";
783 local ($bytes_out) = 0;
785 print "Writing $outfile...\n";
787 open (OUT, "> $outfile");
789 print OUT "/* This file is automatically generated. DO NOT EDIT!\n";
790 print OUT " Instead, edit gen-unicode-tables.pl and re-run. */\n\n";
792 print OUT "#ifndef BREAKTABLES_H\n";
793 print OUT "#define BREAKTABLES_H\n\n";
795 print OUT "#define G_UNICODE_DATA_VERSION \"$ARGV[0]\"\n\n";
797 printf OUT "#define G_UNICODE_LAST_CHAR 0x%04x\n\n", $last;
799 printf OUT "#define G_UNICODE_MAX_TABLE_INDEX 1000\n\n";
802 printf OUT "static const char break_property_data[][256] = {\n";
803 for ($count = 0; $count <= $last; $count += 256)
805 $row[$count / 256] = &print_row ($count, 1, \&fetch_break_type);
807 printf OUT "\n};\n\n";
809 print OUT "static const short break_property_table[256] = {\n";
810 for ($count = 0; $count <= $last; $count += 256)
812 print OUT ",\n" if $count > 0;
813 print OUT " ", $row[$count / 256];
816 print OUT "\n};\n\n";
818 print OUT "#endif /* BREAKTABLES_H */\n";
822 printf STDERR "Generated %d bytes in break tables\n", $bytes_out;
826 # A fetch function for the break properties table.
830 return $break_mappings{$break_props[$index]};
833 # Fetcher for combining class.
840 # Expand a character decomposition recursively.
843 my ($code, $compat) = @_;
847 foreach $iter (split (' ', $decompositions[$code]))
850 if (defined $decompositions[$val] &&
851 ($compat || !$decompose_compat[$val]))
853 push (@result, &expand_decomp ($val, $compat));
857 push (@result, $val);
866 my ($code, $compat) = @_;
869 foreach $iter (&expand_decomp ($code, $compat))
871 $result .= sprintf "\\x%02x\\x%02x", $iter / 256, $iter & 0xff;
876 # Generate special case data string from two fields
879 my ($code, $single, $field1, $field2) = @_;
881 @values = (defined $single ? $single : (),
882 (map { hex ($_) } split /\s+/, $field1),
884 (map { hex ($_) } split /\s+/, $field2));
888 for $value (@values) {
889 $result .= sprintf ("\\x%02x\\x%02x", $value / 256, $value & 0xff);
894 if (2 * @values + 2 > $special_case_len) {
895 $special_case_len = 2 * @values + 2;
898 push @special_cases, $result;
901 # We encode special cases in the surrogate pair space
903 $value[$code] = 0xD800 + scalar(@special_cases) - 1;
906 sub output_special_case_table
912 /* Table of special cases for case conversion; each record contains
913 * First, the best single character mapping to lowercase if Lu,
914 * and to uppercase if Ll, followed by the output mapping for the two cases
915 * other than the case of the codepoint, in the order [Ll],[Lu],[Lt],
916 * separated and terminated by a double NUL.
918 static const guchar special_case_table[][$special_case_len] = {
921 for $case (@special_cases) {
922 print $out qq( "$case",\n);
930 print STDERR "Generated ", ($special_case_len * scalar @special_cases), " bytes in special case table\n";
933 sub enumerate_ordered
938 for my $code (sort { $a <=> $b } keys %$array) {
939 if ($array->{$code} == 1) {
940 delete $array->{$code};
943 $array->{$code} = $n++;
949 sub output_composition_table
951 print STDERR "Generating composition table\n";
953 local ($bytes_out) = 0;
958 # First we need to go through and remove decompositions
959 # starting with a non-starter, and single-character
960 # decompositions. At the same time, record
961 # the first and second character of each decomposition
963 for $code (keys %compositions) {
964 @values = map { hex ($_) } split /\s+/, $compositions{$code};
965 if ($cclass[$values[0]]) {
966 delete $compositions{$code};
970 delete $compositions{$code};
974 die "$code has more than two elements in its decomposition!\n";
977 if (exists $first{$values[0]}) {
978 $first{$values[0]}++;
980 $first{$values[0]} = 1;
984 # Assign integer indicices, removing singletons
985 my $n_first = enumerate_ordered (\%first);
987 # Now record the second character if each (non-singleton) decomposition
988 for $code (keys %compositions) {
989 @values = map { hex ($_) } split /\s+/, $compositions{$code};
991 if (exists $first{$values[0]}) {
992 if (exists $second{$values[1]}) {
993 $second{$values[1]}++;
995 $second{$values[1]} = 1;
1000 # Assign integer indices, removing duplicate
1001 my $n_second = enumerate_ordered (\%second);
1003 # Build reverse table
1005 my @first_singletons;
1006 my @second_singletons;
1008 for $code (keys %compositions) {
1009 @values = map { hex ($_) } split /\s+/, $compositions{$code};
1011 my $first = $first{$values[0]};
1012 my $second = $second{$values[1]};
1014 if (defined $first && defined $second) {
1015 $reverse{"$first|$second"} = $code;
1016 } elsif (!defined $first) {
1017 push @first_singletons, [ $values[0], $values[1], $code ];
1019 push @second_singletons, [ $values[1], $values[0], $code ];
1023 @first_singletons = sort { $a->[0] <=> $b->[0] } @first_singletons;
1024 @second_singletons = sort { $a->[0] <=> $b->[0] } @second_singletons;
1028 open OUT, ">gunicomp.h" or die "Cannot open gunicomp.h: $!\n";
1030 # Assign values in lookup table for all code points involved
1034 printf OUT "#define COMPOSE_FIRST_START %d\n", $total;
1035 for $code (keys %first) {
1036 $vals{$code} = $first{$code} + $total;
1037 $last = $code if $code > $last;
1041 printf OUT "#define COMPOSE_FIRST_SINGLE_START %d\n", $total;
1042 for $record (@first_singletons) {
1043 my $code = $record->[0];
1044 $vals{$code} = $i++ + $total;
1045 $last = $code if $code > $last;
1047 $total += @first_singletons;
1048 printf OUT "#define COMPOSE_SECOND_START %d\n", $total;
1049 for $code (keys %second) {
1050 $vals{$code} = $second{$code} + $total;
1051 $last = $code if $code > $last;
1053 $total += $n_second;
1055 printf OUT "#define COMPOSE_SECOND_SINGLE_START %d\n\n", $total;
1056 for $record (@second_singletons) {
1057 my $code = $record->[0];
1058 $vals{$code} = $i++ + $total;
1059 $last = $code if $code > $last;
1062 # Output lookup table
1066 printf OUT "static const gushort compose_data[][256] = {\n";
1067 for (my $count = 0; $count <= $last; $count += 256)
1069 $row[$count / 256] = &print_row ($count, 2, sub { exists $vals{$_[0]} ? $vals{$_[0]} : 0; });
1071 printf OUT "\n};\n\n";
1073 print OUT "static const short compose_table[256] = {\n";
1074 for (my $count = 0; $count <= $last; $count += 256)
1076 print OUT ",\n" if $count > 0;
1077 print OUT " ", $row[$count / 256];
1080 print OUT "\n};\n\n";
1082 # Output first singletons
1084 print OUT "static const gushort compose_first_single[][2] = {\n";
1086 for $record (@first_singletons) {
1087 print OUT ",\n" if $i++ > 0;
1088 printf OUT " { %#06x, %#06x }", $record->[1], $record->[2];
1092 $bytes_out += @first_singletons * 4;
1094 # Output second singletons
1096 print OUT "static const gushort compose_second_single[][2] = {\n";
1098 for $record (@second_singletons) {
1099 print OUT ",\n" if $i++ > 0;
1100 printf OUT " { %#06x, %#06x }", $record->[1], $record->[2];
1104 $bytes_out += @second_singletons * 4;
1106 # Output array of composition pairs
1109 static const gushort compose_array[$n_first][$n_second] = {
1112 for (my $i = 0; $i < $n_first; $i++) {
1113 print OUT ",\n" if $i;
1115 for (my $j = 0; $j < $n_second; $j++) {
1116 print OUT ", " if $j;
1117 if (exists $reverse{"$i|$j"}) {
1118 printf OUT "%#06x", $reverse{"$i|$j"};
1131 $bytes_out += $n_first * $n_second * 2;
1133 printf STDERR "Generated %d bytes in compose tables\n", $bytes_out;
1136 sub output_casefold_table
1142 /* Table of casefolding cases that can't be derived by lowercasing
1144 static const struct {
1146 gchar data[$casefoldlen];
1147 } casefold_table[] = {
1150 @casefold = sort { $a->[0] <=> $b->[0] } @casefold;
1152 for $case (@casefold) {
1154 $string = $case->[1];
1155 print $out sprintf(qq({ %#04x, "$string" },\n), $code);
1164 my $recordlen = (2+$casefoldlen+1) & ~1;
1165 printf "Generated %d bytes for casefold table\n", $recordlen * @casefold;