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, see <http://www.gnu.org/licenses/>.
20 # Andrew Taylor <andrew.taylor@montage.ca>
22 # gen-unicode-tables.pl - Generate tables for libunicode from Unicode data.
23 # See http://www.unicode.org/Public/UNIDATA/UnicodeCharacterDatabase.html
24 # I consider the output of this program to be unrestricted. Use it as
28 # * For decomp table it might make sense to use a shift count other
29 # than 8. We could easily compute the perfect shift count.
31 # we use some perl unicode features
36 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 # Names of fields in Unicode data table.
43 $COMBINING_CLASSES = 3;
56 # Names of fields in the line break table
60 # Names of fields in the SpecialCasing table
67 # Names of fields in the CaseFolding table
72 # Map general category code onto symbolic name.
76 'Lu' => "G_UNICODE_UPPERCASE_LETTER",
77 'Ll' => "G_UNICODE_LOWERCASE_LETTER",
78 'Lt' => "G_UNICODE_TITLECASE_LETTER",
79 'Mn' => "G_UNICODE_NON_SPACING_MARK",
80 'Mc' => "G_UNICODE_SPACING_MARK",
81 'Me' => "G_UNICODE_ENCLOSING_MARK",
82 'Nd' => "G_UNICODE_DECIMAL_NUMBER",
83 'Nl' => "G_UNICODE_LETTER_NUMBER",
84 'No' => "G_UNICODE_OTHER_NUMBER",
85 'Zs' => "G_UNICODE_SPACE_SEPARATOR",
86 'Zl' => "G_UNICODE_LINE_SEPARATOR",
87 'Zp' => "G_UNICODE_PARAGRAPH_SEPARATOR",
88 'Cc' => "G_UNICODE_CONTROL",
89 'Cf' => "G_UNICODE_FORMAT",
90 'Cs' => "G_UNICODE_SURROGATE",
91 'Co' => "G_UNICODE_PRIVATE_USE",
92 'Cn' => "G_UNICODE_UNASSIGNED",
95 'Lm' => "G_UNICODE_MODIFIER_LETTER",
96 'Lo' => "G_UNICODE_OTHER_LETTER",
97 'Pc' => "G_UNICODE_CONNECT_PUNCTUATION",
98 'Pd' => "G_UNICODE_DASH_PUNCTUATION",
99 'Ps' => "G_UNICODE_OPEN_PUNCTUATION",
100 'Pe' => "G_UNICODE_CLOSE_PUNCTUATION",
101 'Pi' => "G_UNICODE_INITIAL_PUNCTUATION",
102 'Pf' => "G_UNICODE_FINAL_PUNCTUATION",
103 'Po' => "G_UNICODE_OTHER_PUNCTUATION",
104 'Sm' => "G_UNICODE_MATH_SYMBOL",
105 'Sc' => "G_UNICODE_CURRENCY_SYMBOL",
106 'Sk' => "G_UNICODE_MODIFIER_SYMBOL",
107 'So' => "G_UNICODE_OTHER_SYMBOL"
112 'AI' => "G_UNICODE_BREAK_AMBIGUOUS",
113 'AL' => "G_UNICODE_BREAK_ALPHABETIC",
114 'B2' => "G_UNICODE_BREAK_BEFORE_AND_AFTER",
115 'BA' => "G_UNICODE_BREAK_AFTER",
116 'BB' => "G_UNICODE_BREAK_BEFORE",
117 'BK' => "G_UNICODE_BREAK_MANDATORY",
118 'CB' => "G_UNICODE_BREAK_CONTINGENT",
119 'CJ' => "G_UNICODE_BREAK_CONDITIONAL_JAPANESE_STARTER",
120 'CL' => "G_UNICODE_BREAK_CLOSE_PUNCTUATION",
121 'CM' => "G_UNICODE_BREAK_COMBINING_MARK",
122 'CP' => "G_UNICODE_BREAK_CLOSE_PARANTHESIS",
123 'CR' => "G_UNICODE_BREAK_CARRIAGE_RETURN",
124 'EX' => "G_UNICODE_BREAK_EXCLAMATION",
125 'GL' => "G_UNICODE_BREAK_NON_BREAKING_GLUE",
126 'H2' => "G_UNICODE_BREAK_HANGUL_LV_SYLLABLE",
127 'H3' => "G_UNICODE_BREAK_HANGUL_LVT_SYLLABLE",
128 'HL' => "G_UNICODE_BREAK_HEBREW_LETTER",
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 'RI' => "G_UNICODE_BREAK_REGIONAL_INDICATOR",
145 'SA' => "G_UNICODE_BREAK_COMPLEX_CONTEXT",
146 'SG' => "G_UNICODE_BREAK_SURROGATE",
147 'SP' => "G_UNICODE_BREAK_SPACE",
148 'SY' => "G_UNICODE_BREAK_SYMBOL",
149 'WJ' => "G_UNICODE_BREAK_WORD_JOINER",
150 'XX' => "G_UNICODE_BREAK_UNKNOWN",
151 'ZW' => "G_UNICODE_BREAK_ZERO_WIDTH_SPACE"
154 # Title case mappings.
155 %title_to_lower = ();
156 %title_to_upper = ();
158 # Maximum length of special-case strings
161 my @special_case_offsets;
162 my $special_case_offset = 0;
166 if (@ARGV && $ARGV[0] eq '-decomp')
172 elsif (@ARGV && $ARGV[0] eq '-both')
180 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";
183 my ($unicodedatatxt, $linebreaktxt, $specialcasingtxt, $casefoldingtxt, $compositionexclusionstxt);
186 opendir (my $dir, $d) or die "Cannot open Unicode data dir $d: $!\n";
187 for my $f (readdir ($dir))
189 $unicodedatatxt = "$d/$f" if ($f =~ /^UnicodeData.*\.txt/);
190 $linebreaktxt = "$d/$f" if ($f =~ /^LineBreak.*\.txt/);
191 $specialcasingtxt = "$d/$f" if ($f =~ /^SpecialCasing.*\.txt/);
192 $casefoldingtxt = "$d/$f" if ($f =~ /^CaseFolding.*\.txt/);
193 $compositionexclusionstxt = "$d/$f" if ($f =~ /^CompositionExclusions.*\.txt/);
196 defined $unicodedatatxt or die "Did not find UnicodeData file";
197 defined $linebreaktxt or die "Did not find LineBreak file";
198 defined $specialcasingtxt or die "Did not find SpecialCasing file";
199 defined $casefoldingtxt or die "Did not find CaseFolding file";
200 defined $compositionexclusionstxt or die "Did not find CompositionExclusions file";
202 print "Creating decomp table\n" if ($do_decomp);
203 print "Creating property table\n" if ($do_props);
205 print "Composition exlusions from $compositionexclusionstxt\n";
207 open (INPUT, "< $compositionexclusionstxt") || exit 1;
221 $composition_exclusions{hex($_)} = 1;
226 print "Unicode data from $unicodedatatxt\n";
228 open (INPUT, "< $unicodedatatxt") || exit 1;
230 # we save memory by skipping the huge empty area before U+E0000
231 my $pages_before_e0000;
237 @fields = split (';', $_, 30);
240 printf STDERR ("Entry for $fields[$CODE] has wrong number of fields (%d)\n", $#fields);
243 $code = hex ($fields[$CODE]);
245 if ($code >= 0xE0000 and $last_code < 0xE0000)
247 $pages_before_e0000 = ($last_code >> 8) + 1;
250 if ($code > $last_code + 1)
253 if ($fields[$NAME] =~ /Last>/)
255 # Fill the gap with the last character read,
256 # since this was a range specified in the char database
261 # The gap represents undefined characters. Only the type
263 @gfields = ('', '', 'Cn', '0', '', '', '', '', '', '', '',
266 for (++$last_code; $last_code < $code; ++$last_code)
268 $gfields{$CODE} = sprintf ("%04x", $last_code);
269 &process_one ($last_code, @gfields);
272 &process_one ($code, @fields);
278 @gfields = ('', '', 'Cn', '0', '', '', '', '', '', '', '',
280 for (++$last_code; $last_code <= 0x10FFFF; ++$last_code)
282 $gfields{$CODE} = sprintf ("%04x", $last_code);
283 &process_one ($last_code, @gfields);
285 --$last_code; # Want last to be 0x10FFFF.
287 print "Creating line break table\n";
289 print "Line break data from $linebreaktxt\n";
291 open (INPUT, "< $linebreaktxt") || exit 1;
296 my ($start_code, $end_code);
305 @fields = split (';', $_, 30);
308 printf STDERR ("Entry for $fields[$CODE] has wrong number of fields (%d)\n", $#fields);
312 if ($fields[$CODE] =~ /([A-F0-9]{4,6})\.\.([A-F0-9]{4,6})/)
314 $start_code = hex ($1);
315 $end_code = hex ($2);
317 $start_code = $end_code = hex ($fields[$CODE]);
321 if ($start_code > $last_code + 1)
323 # The gap represents undefined characters. If assigned,
324 # they are AL, if not assigned, XX
325 for (++$last_code; $last_code < $start_code; ++$last_code)
327 if ($type[$last_code] eq 'Cn')
329 $break_props[$last_code] = 'XX';
333 $break_props[$last_code] = 'AL';
338 for ($last_code = $start_code; $last_code <= $end_code; $last_code++)
340 $break_props[$last_code] = $fields[$BREAK_PROPERTY];
343 $last_code = $end_code;
348 for (++$last_code; $last_code <= 0x10FFFF; ++$last_code)
350 if ($type[$last_code] eq 'Cn')
352 $break_props[$last_code] = 'XX';
356 $break_props[$last_code] = 'AL';
359 --$last_code; # Want last to be 0x10FFFF.
361 print STDERR "Last code is not 0x10FFFF" if ($last_code != 0x10FFFF);
363 print "Reading special-casing table for case conversion\n";
365 open (INPUT, "< $specialcasingtxt") || exit 1;
378 @fields = split ('\s*;\s*', $_, 30);
380 $raw_code = $fields[$CASE_CODE];
381 $code = hex ($raw_code);
383 if ($#fields != 4 && $#fields != 5)
385 printf STDERR ("Entry for $raw_code has wrong number of fields (%d)\n", $#fields);
389 if (!defined $type[$code])
391 printf STDERR "Special case for code point: $code, which has no defined type\n";
395 if (defined $fields[5]) {
396 # Ignore conditional special cases - we'll handle them in code
400 if ($type[$code] eq 'Lu')
402 (hex $fields[$CASE_UPPER] == $code) || die "$raw_code is Lu and UCD_Upper($raw_code) != $raw_code";
404 &add_special_case ($code, $value[$code], $fields[$CASE_LOWER], $fields[$CASE_TITLE]);
406 } elsif ($type[$code] eq 'Lt')
408 (hex $fields[$CASE_TITLE] == $code) || die "$raw_code is Lt and UCD_Title($raw_code) != $raw_code";
410 &add_special_case ($code, undef, $fields[$CASE_LOWER], $fields[$CASE_UPPER]);
411 } elsif ($type[$code] eq 'Ll')
413 (hex $fields[$CASE_LOWER] == $code) || die "$raw_code is Ll and UCD_Lower($raw_code) != $raw_code";
415 &add_special_case ($code, $value[$code], $fields[$CASE_UPPER], $fields[$CASE_TITLE]);
417 printf STDERR "Special case for non-alphabetic code point: $raw_code\n";
424 open (INPUT, "< $casefoldingtxt") || exit 1;
440 @fields = split ('\s*;\s*', $_, 30);
442 $raw_code = $fields[$FOLDING_CODE];
443 $code = hex ($raw_code);
447 printf STDERR ("Entry for $raw_code has wrong number of fields (%d)\n", $#fields);
451 # we don't use Simple or Turkic rules here
452 next if ($fields[$FOLDING_STATUS] =~ /^[ST]$/);
454 @values = map { hex ($_) } split /\s+/, $fields[$FOLDING_MAPPING];
459 !(defined $value[$code] && $value[$code] >= 0x1000000) &&
460 defined $type[$code]) {
463 if ($type[$code] eq 'Ll')
466 } elsif ($type[$code] eq 'Lt')
468 $lower = $title_to_lower{$code};
469 } elsif ($type[$code] eq 'Lu')
471 $lower = $value[$code];
476 if ($lower == $values[0]) {
481 my $string = pack ("U*", @values);
483 if (1 + &length_in_bytes ($string) > $casefoldlen) {
484 $casefoldlen = 1 + &length_in_bytes ($string);
487 push @casefold, [ $code, &escape ($string) ];
493 &print_tables ($last_code)
496 &print_decomp ($last_code);
497 &output_composition_table;
500 &print_line_break ($last_code);
505 # perl "length" returns the length in characters
510 return length $string;
513 # Process a single character.
516 my ($code, @fields) = @_;
518 $type[$code] = $fields[$CATEGORY];
519 if ($type[$code] eq 'Nd')
521 $value[$code] = int ($fields[$DECIMAL_VALUE]);
523 elsif ($type[$code] eq 'Ll')
525 $value[$code] = hex ($fields[$UPPER]);
527 elsif ($type[$code] eq 'Lu')
529 $value[$code] = hex ($fields[$LOWER]);
532 if ($type[$code] eq 'Lt')
534 $title_to_lower{$code} = hex ($fields[$LOWER]);
535 $title_to_upper{$code} = hex ($fields[$UPPER]);
538 $cclass[$code] = $fields[$COMBINING_CLASSES];
540 # Handle decompositions.
541 if ($fields[$DECOMPOSITION] ne '')
543 if ($fields[$DECOMPOSITION] =~ s/\<.*\>\s*//) {
544 $decompose_compat[$code] = 1;
546 $decompose_compat[$code] = 0;
548 if (!exists $composition_exclusions{$code}) {
549 $compositions{$code} = $fields[$DECOMPOSITION];
552 $decompositions[$code] = $fields[$DECOMPOSITION];
559 my ($outfile) = "gunichartables.h";
561 local ($bytes_out) = 0;
563 print "Writing $outfile...\n";
565 open (OUT, "> $outfile");
567 print OUT "/* This file is automatically generated. DO NOT EDIT!\n";
568 print OUT " Instead, edit gen-unicode-tables.pl and re-run. */\n\n";
570 print OUT "#ifndef CHARTABLES_H\n";
571 print OUT "#define CHARTABLES_H\n\n";
573 print OUT "#define G_UNICODE_DATA_VERSION \"$ARGV[0]\"\n\n";
575 printf OUT "#define G_UNICODE_LAST_CHAR 0x%04x\n\n", $last;
577 printf OUT "#define G_UNICODE_MAX_TABLE_INDEX 10000\n\n";
579 my $last_part1 = ($pages_before_e0000 * 256) - 1;
580 printf OUT "#define G_UNICODE_LAST_CHAR_PART1 0x%04X\n\n", $last_part1;
581 printf OUT "#define G_UNICODE_LAST_PAGE_PART1 %d\n\n", $pages_before_e0000 - 1;
584 printf OUT "static const char type_data[][256] = {\n";
585 for ($count = 0; $count <= $last; $count += 256)
587 $row[$count / 256] = &print_row ($count, 1, \&fetch_type);
589 printf OUT "\n};\n\n";
591 printf OUT "/* U+0000 through U+%04X */\n", $last_part1;
592 print OUT "static const gint16 type_table_part1[$pages_before_e0000] = {\n";
593 for ($count = 0; $count <= $last_part1; $count += 256)
595 print OUT ",\n" if $count > 0;
596 print OUT " ", $row[$count / 256];
599 print OUT "\n};\n\n";
601 printf OUT "/* U+E0000 through U+%04X */\n", $last;
602 print OUT "static const gint16 type_table_part2[768] = {\n";
603 for ($count = 0xE0000; $count <= $last; $count += 256)
605 print OUT ",\n" if $count > 0xE0000;
606 print OUT " ", $row[$count / 256];
609 print OUT "\n};\n\n";
613 # Now print attribute table.
617 printf OUT "static const gunichar attr_data[][256] = {\n";
618 for ($count = 0; $count <= $last; $count += 256)
620 $row[$count / 256] = &print_row ($count, 4, \&fetch_attr);
622 printf OUT "\n};\n\n";
624 printf OUT "/* U+0000 through U+%04X */\n", $last_part1;
625 print OUT "static const gint16 attr_table_part1[$pages_before_e0000] = {\n";
626 for ($count = 0; $count <= $last_part1; $count += 256)
628 print OUT ",\n" if $count > 0;
629 print OUT " ", $row[$count / 256];
632 print OUT "\n};\n\n";
634 printf OUT "/* U+E0000 through U+%04X */\n", $last;
635 print OUT "static const gint16 attr_table_part2[768] = {\n";
636 for ($count = 0xE0000; $count <= $last; $count += 256)
638 print OUT ",\n" if $count > 0xE0000;
639 print OUT " ", $row[$count / 256];
642 print OUT "\n};\n\n";
645 # print title case table
648 print OUT "static const gunichar title_table[][3] = {\n";
651 foreach $item (sort keys %title_to_lower)
656 printf OUT " { 0x%04x, 0x%04x, 0x%04x }", $item, $title_to_upper{$item}, $title_to_lower{$item};
659 print OUT "\n};\n\n";
662 # And special case conversion table -- conversions that change length
664 &output_special_case_table (\*OUT);
665 &output_casefold_table (\*OUT);
667 print OUT "#endif /* CHARTABLES_H */\n";
671 printf STDERR "Generated %d bytes in tables\n", $bytes_out;
674 # A fetch function for the type table.
678 return $mappings{$type[$index]};
681 # A fetch function for the attribute table.
685 if (defined $value[$index])
687 return sprintf ("0x%04x", $value[$index]);
697 my ($start, $typsize, $fetcher) = @_;
704 for ($off = 0; $off < 256; ++$off)
706 $values[$off] = $fetcher->($off + $start);
707 if ($values[$off] ne $values[0])
714 return $values[0] . " + G_UNICODE_MAX_TABLE_INDEX";
717 printf OUT ",\n" if ($table_index != 0);
718 printf OUT " { /* page %d, index %d */\n ", $start / 256, $table_index;
720 for ($i = $start; $i < $start + 256; ++$i)
724 my ($text) = $values[$i - $start];
725 if (length ($text) + $column + 2 > 78)
731 $column += length ($text) + 2;
735 $bytes_out += 256 * $typsize;
737 return sprintf "%d /* page %d */", $table_index++, $start / 256;
744 my $escaped = unpack("H*", $string);
745 $escaped =~ s/(.{2})/\\x$1/g;
750 # Returns the offset of $decomp in the offset string. Updates the
751 # referenced variables as appropriate.
752 sub handle_decomp ($$$$)
754 my ($decomp, $decomp_offsets_ref, $decomp_string_ref, $decomp_string_offset_ref) = @_;
755 my $offset = "G_UNICODE_NOT_PRESENT_OFFSET";
759 if (defined $decomp_offsets_ref->{$decomp})
761 $offset = $decomp_offsets_ref->{$decomp};
765 $offset = ${$decomp_string_offset_ref};
766 $decomp_offsets_ref->{$decomp} = $offset;
767 ${$decomp_string_ref} .= "\n \"" . &escape ($decomp) . "\\0\" /* offset ${$decomp_string_offset_ref} */";
768 ${$decomp_string_offset_ref} += &length_in_bytes ($decomp) + 1;
775 # Generate the character decomposition header.
779 my ($outfile) = "gunidecomp.h";
781 local ($bytes_out) = 0;
783 print "Writing $outfile...\n";
785 open (OUT, "> $outfile") || exit 1;
787 print OUT "/* This file is automatically generated. DO NOT EDIT! */\n\n";
788 print OUT "#ifndef DECOMP_H\n";
789 print OUT "#define DECOMP_H\n\n";
791 printf OUT "#define G_UNICODE_LAST_CHAR 0x%04x\n\n", $last;
793 printf OUT "#define G_UNICODE_MAX_TABLE_INDEX (0x110000 / 256)\n\n";
795 my $last_part1 = ($pages_before_e0000 * 256) - 1;
796 printf OUT "#define G_UNICODE_LAST_CHAR_PART1 0x%04X\n\n", $last_part1;
797 printf OUT "#define G_UNICODE_LAST_PAGE_PART1 %d\n\n", $pages_before_e0000 - 1;
799 $NOT_PRESENT_OFFSET = 65535;
800 print OUT "#define G_UNICODE_NOT_PRESENT_OFFSET $NOT_PRESENT_OFFSET\n\n";
804 printf OUT "static const guchar cclass_data[][256] = {\n";
805 for ($count = 0; $count <= $last; $count += 256)
807 $row[$count / 256] = &print_row ($count, 1, \&fetch_cclass);
809 printf OUT "\n};\n\n";
811 print OUT "static const gint16 combining_class_table_part1[$pages_before_e0000] = {\n";
812 for ($count = 0; $count <= $last_part1; $count += 256)
814 print OUT ",\n" if $count > 0;
815 print OUT " ", $row[$count / 256];
818 print OUT "\n};\n\n";
820 print OUT "static const gint16 combining_class_table_part2[768] = {\n";
821 for ($count = 0xE0000; $count <= $last; $count += 256)
823 print OUT ",\n" if $count > 0xE0000;
824 print OUT " ", $row[$count / 256];
827 print OUT "\n};\n\n";
829 print OUT "typedef struct\n{\n";
830 print OUT " gunichar ch;\n";
831 print OUT " guint16 canon_offset;\n";
832 print OUT " guint16 compat_offset;\n";
833 print OUT "} decomposition;\n\n";
835 print OUT "static const decomposition decomp_table[] =\n{\n";
838 my ($decomp_string) = "";
839 my ($decomp_string_offset) = 0;
840 for ($count = 0; $count <= $last; ++$count)
842 if (defined $decompositions[$count])
851 if (!$decompose_compat[$count]) {
852 $canon_decomp = make_decomp ($count, 0);
854 $compat_decomp = make_decomp ($count, 1);
856 if (defined $canon_decomp && $compat_decomp eq $canon_decomp) {
857 undef $compat_decomp;
860 my $canon_offset = handle_decomp ($canon_decomp, \%decomp_offsets, \$decomp_string, \$decomp_string_offset);
861 my $compat_offset = handle_decomp ($compat_decomp, \%decomp_offsets, \$decomp_string, \$decomp_string_offset);
863 die if $decomp_string_offset > $NOT_PRESENT_OFFSET;
865 printf OUT qq( { 0x%04x, $canon_offset, $compat_offset }), $count;
869 print OUT "\n};\n\n";
870 $bytes_out += $decomp_string_offset + 1;
872 printf OUT "static const gchar decomp_expansion_string[] = %s;\n\n", $decomp_string;
874 print OUT "typedef struct\n{\n";
875 print OUT " gunichar ch;\n";
876 print OUT " gunichar a;\n";
877 print OUT " gunichar b;\n";
878 print OUT "} decomposition_step;\n\n";
880 # There's lots of room to optimize the following table...
881 print OUT "static const decomposition_step decomp_step_table[] =\n{\n";
884 for ($count = 0; $count <= $last; ++$count)
886 if ((defined $decompositions[$count]) && (!$decompose_compat[$count]))
892 @list = (split(' ', $decompositions[$count]), "0");
893 printf OUT qq( { 0x%05x, 0x%05x, 0x%05x }), $count, hex($list[0]), hex($list[1]);
894 # don't include 1:1 in the compose table
895 push @steps, [ ($count, hex($list[0]), hex($list[1])) ]
899 print OUT "\n};\n\n";
901 print OUT "#endif /* DECOMP_H */\n";
903 printf STDERR "Generated %d bytes in decomp tables\n", $bytes_out;
909 my ($outfile) = "gunibreak.h";
911 local ($bytes_out) = 0;
913 print "Writing $outfile...\n";
915 open (OUT, "> $outfile");
917 print OUT "/* This file is automatically generated. DO NOT EDIT!\n";
918 print OUT " Instead, edit gen-unicode-tables.pl and re-run. */\n\n";
920 print OUT "#ifndef BREAKTABLES_H\n";
921 print OUT "#define BREAKTABLES_H\n\n";
923 print OUT "#include <glib/gtypes.h>\n";
924 print OUT "#include <glib/gunicode.h>\n\n";
926 print OUT "#define G_UNICODE_DATA_VERSION \"$ARGV[0]\"\n\n";
928 printf OUT "#define G_UNICODE_LAST_CHAR 0x%04X\n\n", $last;
930 printf OUT "#define G_UNICODE_MAX_TABLE_INDEX 10000\n\n";
932 my $last_part1 = ($pages_before_e0000 * 256) - 1;
933 printf OUT "/* the last code point that should be looked up in break_property_table_part1 */\n";
934 printf OUT "#define G_UNICODE_LAST_CHAR_PART1 0x%04X\n\n", $last_part1;
937 printf OUT "static const gint8 break_property_data[][256] = {\n";
938 for ($count = 0; $count <= $last; $count += 256)
940 $row[$count / 256] = &print_row ($count, 1, \&fetch_break_type);
942 printf OUT "\n};\n\n";
944 printf OUT "/* U+0000 through U+%04X */\n", $last_part1;
945 print OUT "static const gint16 break_property_table_part1[$pages_before_e0000] = {\n";
946 for ($count = 0; $count <= $last_part1; $count += 256)
948 print OUT ",\n" if $count > 0;
949 print OUT " ", $row[$count / 256];
952 print OUT "\n};\n\n";
954 printf OUT "/* U+E0000 through U+%04X */\n", $last;
955 print OUT "static const gint16 break_property_table_part2[768] = {\n";
956 for ($count = 0xE0000; $count <= $last; $count += 256)
958 print OUT ",\n" if $count > 0xE0000;
959 print OUT " ", $row[$count / 256];
962 print OUT "\n};\n\n";
965 print OUT "#endif /* BREAKTABLES_H */\n";
969 printf STDERR "Generated %d bytes in break tables\n", $bytes_out;
973 # A fetch function for the break properties table.
977 return $break_mappings{$break_props[$index]};
980 # Fetcher for combining class.
987 # Expand a character decomposition recursively.
990 my ($code, $compat) = @_;
994 foreach $iter (split (' ', $decompositions[$code]))
997 if (defined $decompositions[$val] &&
998 ($compat || !$decompose_compat[$val]))
1000 push (@result, &expand_decomp ($val, $compat));
1004 push (@result, $val);
1013 my ($code, $compat) = @_;
1016 foreach $iter (&expand_decomp ($code, $compat))
1018 $result .= pack ("U", $iter); # to utf-8
1023 # Generate special case data string from two fields
1024 sub add_special_case
1026 my ($code, $single, $field1, $field2) = @_;
1028 @values = (defined $single ? $single : (),
1029 (map { hex ($_) } split /\s+/, $field1),
1031 (map { hex ($_) } split /\s+/, $field2));
1035 for $value (@values) {
1036 $result .= pack ("U", $value); # to utf-8
1039 push @special_case_offsets, $special_case_offset;
1041 # We encode special cases up in the 0x1000000 space
1042 $value[$code] = 0x1000000 + $special_case_offset;
1044 $special_case_offset += 1 + &length_in_bytes ($result);
1046 push @special_cases, &escape ($result);
1049 sub output_special_case_table
1055 /* Table of special cases for case conversion; each record contains
1056 * First, the best single character mapping to lowercase if Lu,
1057 * and to uppercase if Ll, followed by the output mapping for the two cases
1058 * other than the case of the codepoint, in the order [Ll],[Lu],[Lt],
1059 * encoded in UTF-8, separated and terminated by a null character.
1061 static const gchar special_case_table[] = {
1065 for $case (@special_cases) {
1066 print $out qq( "$case\\0" /* offset ${special_case_offsets[$i]} */\n);
1075 print STDERR "Generated " . ($special_case_offset + 1) . " bytes in special case table\n";
1078 sub enumerate_ordered
1083 for my $code (sort { $a <=> $b } keys %$array) {
1084 if ($array->{$code} == 1) {
1085 delete $array->{$code};
1088 $array->{$code} = $n++;
1094 sub output_composition_table
1096 print STDERR "Generating composition table\n";
1098 local ($bytes_out) = 0;
1103 # First we need to go through and remove decompositions
1104 # starting with a non-starter, and single-character
1105 # decompositions. At the same time, record
1106 # the first and second character of each decomposition
1108 for $code (keys %compositions)
1110 @values = map { hex ($_) } split /\s+/, $compositions{$code};
1113 if ($cclass[$code]) {
1114 delete $compositions{$code};
1117 if ($cclass[$values[0]]) {
1118 delete $compositions{$code};
1122 # single-character decompositions
1124 delete $compositions{$code};
1129 die "$code has more than two elements in its decomposition!\n";
1132 if (exists $first{$values[0]}) {
1133 $first{$values[0]}++;
1135 $first{$values[0]} = 1;
1139 # Assign integer indices, removing singletons
1140 my $n_first = enumerate_ordered (\%first);
1142 # Now record the second character of each (non-singleton) decomposition
1143 for $code (keys %compositions) {
1144 @values = map { hex ($_) } split /\s+/, $compositions{$code};
1146 if (exists $first{$values[0]}) {
1147 if (exists $second{$values[1]}) {
1148 $second{$values[1]}++;
1150 $second{$values[1]} = 1;
1155 # Assign integer indices, removing duplicate
1156 my $n_second = enumerate_ordered (\%second);
1158 # Build reverse table
1160 my @first_singletons;
1161 my @second_singletons;
1163 for $code (keys %compositions) {
1164 @values = map { hex ($_) } split /\s+/, $compositions{$code};
1166 my $first = $first{$values[0]};
1167 my $second = $second{$values[1]};
1169 if (defined $first && defined $second) {
1170 $reverse{"$first|$second"} = $code;
1171 } elsif (!defined $first) {
1172 push @first_singletons, [ $values[0], $values[1], $code ];
1174 push @second_singletons, [ $values[1], $values[0], $code ];
1178 @first_singletons = sort { $a->[0] <=> $b->[0] } @first_singletons;
1179 @second_singletons = sort { $a->[0] <=> $b->[0] } @second_singletons;
1183 open OUT, ">gunicomp.h" or die "Cannot open gunicomp.h: $!\n";
1185 # Assign values in lookup table for all code points involved
1189 printf OUT "#define COMPOSE_FIRST_START %d\n", $total;
1190 for $code (keys %first) {
1191 $vals{$code} = $first{$code} + $total;
1192 $last = $code if $code > $last;
1196 printf OUT "#define COMPOSE_FIRST_SINGLE_START %d\n", $total;
1197 for $record (@first_singletons) {
1198 my $code = $record->[0];
1199 $vals{$code} = $i++ + $total;
1200 $last = $code if $code > $last;
1202 $total += @first_singletons;
1203 printf OUT "#define COMPOSE_SECOND_START %d\n", $total;
1204 for $code (keys %second) {
1205 $vals{$code} = $second{$code} + $total;
1206 $last = $code if $code > $last;
1208 $total += $n_second;
1210 printf OUT "#define COMPOSE_SECOND_SINGLE_START %d\n\n", $total;
1211 for $record (@second_singletons) {
1212 my $code = $record->[0];
1213 $vals{$code} = $i++ + $total;
1214 $last = $code if $code > $last;
1217 printf OUT "#define COMPOSE_TABLE_LAST %d\n\n", $last / 256;
1219 # Output lookup table
1223 printf OUT "static const guint16 compose_data[][256] = {\n";
1224 for (my $count = 0; $count <= $last; $count += 256)
1226 $row[$count / 256] = &print_row ($count, 2, sub { exists $vals{$_[0]} ? $vals{$_[0]} : 0; });
1228 printf OUT "\n};\n\n";
1230 print OUT "static const gint16 compose_table[COMPOSE_TABLE_LAST + 1] = {\n";
1231 for (my $count = 0; $count <= $last; $count += 256)
1233 print OUT ",\n" if $count > 0;
1234 print OUT " ", $row[$count / 256];
1237 print OUT "\n};\n\n";
1239 # Output first singletons
1241 print OUT "static const gunichar compose_first_single[][2] = {\n";
1243 for $record (@first_singletons) {
1244 print OUT ",\n" if $i++ > 0;
1245 printf OUT " { %#06x, %#06x }", $record->[1], $record->[2];
1249 $bytes_out += @first_singletons * 4;
1251 # Output second singletons
1253 print OUT "static const guint16 compose_second_single[][2] = {\n";
1255 for $record (@second_singletons) {
1256 if ($record->[1] > 0xFFFF or $record->[2] > 0xFFFF) {
1257 die "time to switch compose_second_single to gunichar";
1259 print OUT ",\n" if $i++ > 0;
1260 printf OUT " { %#06x, %#06x }", $record->[1], $record->[2];
1264 $bytes_out += @second_singletons * 4;
1266 # Output array of composition pairs
1269 static const guint16 compose_array[$n_first][$n_second] = {
1272 for (my $i = 0; $i < $n_first; $i++) {
1273 print OUT ",\n" if $i;
1275 for (my $j = 0; $j < $n_second; $j++) {
1276 print OUT ", " if $j;
1277 if (exists $reverse{"$i|$j"}) {
1278 if ($reverse{"$i|$j"} > 0xFFFF) {
1279 die "time to switch compose_array to gunichar" ;
1281 printf OUT "0x%04x", $reverse{"$i|$j"};
1294 $bytes_out += $n_first * $n_second * 2;
1296 printf STDERR "Generated %d bytes in compose tables\n", $bytes_out;
1299 sub output_casefold_table
1305 /* Table of casefolding cases that can't be derived by lowercasing
1307 static const struct {
1309 gchar data[$casefoldlen];
1310 } casefold_table[] = {
1313 @casefold = sort { $a->[0] <=> $b->[0] } @casefold;
1315 for $case (@casefold)
1318 $string = $case->[1];
1320 if ($code > 0xFFFF) {
1321 die "time to switch casefold_table to gunichar" ;
1324 print $out sprintf(qq( { 0x%04x, "$string" },\n), $code);
1333 my $recordlen = (2+$casefoldlen+1) & ~1;
1334 printf "Generated %d bytes for casefold table\n", $recordlen * @casefold;