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 # I consider the output of this program to be unrestricted. Use it as
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 # 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_COMBINING_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 'BK' => "G_UNICODE_BREAK_MANDATORY",
113 'CR' => "G_UNICODE_BREAK_CARRIAGE_RETURN",
114 'LF' => "G_UNICODE_BREAK_LINE_FEED",
115 'CM' => "G_UNICODE_BREAK_COMBINING_MARK",
116 'SG' => "G_UNICODE_BREAK_SURROGATE",
117 'ZW' => "G_UNICODE_BREAK_ZERO_WIDTH_SPACE",
118 'IN' => "G_UNICODE_BREAK_INSEPARABLE",
119 'GL' => "G_UNICODE_BREAK_NON_BREAKING_GLUE",
120 'CB' => "G_UNICODE_BREAK_CONTINGENT",
121 'SP' => "G_UNICODE_BREAK_SPACE",
122 'BA' => "G_UNICODE_BREAK_AFTER",
123 'BB' => "G_UNICODE_BREAK_BEFORE",
124 'B2' => "G_UNICODE_BREAK_BEFORE_AND_AFTER",
125 'HY' => "G_UNICODE_BREAK_HYPHEN",
126 'NS' => "G_UNICODE_BREAK_NON_STARTER",
127 'OP' => "G_UNICODE_BREAK_OPEN_PUNCTUATION",
128 'CL' => "G_UNICODE_BREAK_CLOSE_PUNCTUATION",
129 'QU' => "G_UNICODE_BREAK_QUOTATION",
130 'EX' => "G_UNICODE_BREAK_EXCLAMATION",
131 'ID' => "G_UNICODE_BREAK_IDEOGRAPHIC",
132 'NU' => "G_UNICODE_BREAK_NUMERIC",
133 'IS' => "G_UNICODE_BREAK_INFIX_SEPARATOR",
134 'SY' => "G_UNICODE_BREAK_SYMBOL",
135 'AL' => "G_UNICODE_BREAK_ALPHABETIC",
136 'PR' => "G_UNICODE_BREAK_PREFIX",
137 'PO' => "G_UNICODE_BREAK_POSTFIX",
138 'SA' => "G_UNICODE_BREAK_COMPLEX_CONTEXT",
139 'AI' => "G_UNICODE_BREAK_AMBIGUOUS",
140 'NL' => "G_UNICODE_BREAK_NEXT_LINE",
141 'WJ' => "G_UNICODE_BREAK_WORD_JOINER",
142 'XX' => "G_UNICODE_BREAK_UNKNOWN",
143 'JL' => "G_UNICODE_BREAK_HANGUL_L_JAMO",
144 'JV' => "G_UNICODE_BREAK_HANGUL_V_JAMO",
145 'JT' => "G_UNICODE_BREAK_HANGUL_T_JAMO",
146 'H2' => "G_UNICODE_BREAK_HANGUL_LV_SYLLABLE",
147 'H3' => "G_UNICODE_BREAK_HANGUL_LVT_SYLLABLE"
150 # Title case mappings.
151 %title_to_lower = ();
152 %title_to_upper = ();
154 # Maximum length of special-case strings
157 my @special_case_offsets;
158 my $special_case_offset = 0;
162 if (@ARGV && $ARGV[0] eq '-decomp')
168 elsif (@ARGV && $ARGV[0] eq '-both')
176 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, BidiMirroring.txt\n\n";
179 my ($unicodedatatxt, $linebreaktxt, $specialcasingtxt, $casefoldingtxt, $compositionexclusionstxt);
182 opendir (my $dir, $d) or die "Cannot open Unicode data dir $d: $!\n";
183 for my $f (readdir ($dir))
185 $unicodedatatxt = "$d/$f" if ($f =~ /UnicodeData.*\.txt/);
186 $linebreaktxt = "$d/$f" if ($f =~ /LineBreak.*\.txt/);
187 $specialcasingtxt = "$d/$f" if ($f =~ /SpecialCasing.*\.txt/);
188 $casefoldingtxt = "$d/$f" if ($f =~ /CaseFolding.*\.txt/);
189 $compositionexclusionstxt = "$d/$f" if ($f =~ /CompositionExclusions.*\.txt/);
192 defined $unicodedatatxt or die "Did not find UnicodeData file";
193 defined $linebreaktxt or die "Did not find LineBreak file";
194 defined $specialcasingtxt or die "Did not find SpecialCasing file";
195 defined $casefoldingtxt or die "Did not find CaseFolding file";
196 defined $compositionexclusionstxt or die "Did not find CompositionExclusions file";
198 print "Creating decomp table\n" if ($do_decomp);
199 print "Creating property table\n" if ($do_props);
201 print "Composition exlusions from $compositionexclusionstxt\n";
203 open (INPUT, "< $compositionexclusionstxt") || exit 1;
217 $composition_exclusions{hex($_)} = 1;
222 print "Unicode data from $unicodedatatxt\n";
224 open (INPUT, "< $unicodedatatxt") || exit 1;
226 # we save memory by skipping the huge empty area before U+E0000
227 my $pages_before_e0000;
233 @fields = split (';', $_, 30);
236 printf STDERR ("Entry for $fields[$CODE] has wrong number of fields (%d)\n", $#fields);
239 $code = hex ($fields[$CODE]);
241 if ($code >= 0xE0000 and $last_code < 0xE0000)
243 $pages_before_e0000 = ($last_code >> 8) + 1;
246 if ($code > $last_code + 1)
249 if ($fields[$NAME] =~ /Last>/)
251 # Fill the gap with the last character read,
252 # since this was a range specified in the char database
257 # The gap represents undefined characters. Only the type
259 @gfields = ('', '', 'Cn', '0', '', '', '', '', '', '', '',
262 for (++$last_code; $last_code < $code; ++$last_code)
264 $gfields{$CODE} = sprintf ("%04x", $last_code);
265 &process_one ($last_code, @gfields);
268 &process_one ($code, @fields);
274 @gfields = ('', '', 'Cn', '0', '', '', '', '', '', '', '',
276 for (++$last_code; $last_code <= 0x10FFFF; ++$last_code)
278 $gfields{$CODE} = sprintf ("%04x", $last_code);
279 &process_one ($last_code, @gfields);
281 --$last_code; # Want last to be 0x10FFFF.
283 print "Creating line break table\n";
285 print "Line break data from $linebreaktxt\n";
287 open (INPUT, "< $linebreaktxt") || exit 1;
292 my ($start_code, $end_code);
300 @fields = split (';', $_, 30);
303 printf STDERR ("Entry for $fields[$CODE] has wrong number of fields (%d)\n", $#fields);
307 if ($fields[$CODE] =~ /([A-F0-9]{4,6})\.\.([A-F0-9]{4,6})/)
309 $start_code = hex ($1);
310 $end_code = hex ($2);
312 $start_code = $end_code = hex ($fields[$CODE]);
316 if ($start_code > $last_code + 1)
318 # The gap represents undefined characters. If assigned,
319 # they are AL, if not assigned, XX
320 for (++$last_code; $last_code < $start_code; ++$last_code)
322 if ($type[$last_code] eq 'Cn')
324 $break_props[$last_code] = 'XX';
328 $break_props[$last_code] = 'AL';
333 for ($last_code = $start_code; $last_code <= $end_code; $last_code++)
335 $break_props[$last_code] = $fields[$BREAK_PROPERTY];
338 $last_code = $end_code;
343 for (++$last_code; $last_code <= 0x10FFFF; ++$last_code)
345 if ($type[$last_code] eq 'Cn')
347 $break_props[$last_code] = 'XX';
351 $break_props[$last_code] = 'AL';
354 --$last_code; # Want last to be 0x10FFFF.
356 print STDERR "Last code is not 0x10FFFF" if ($last_code != 0x10FFFF);
358 print "Reading special-casing table for case conversion\n";
360 open (INPUT, "< $specialcasingtxt") || exit 1;
373 @fields = split ('\s*;\s*', $_, 30);
375 $raw_code = $fields[$CASE_CODE];
376 $code = hex ($raw_code);
378 if ($#fields != 4 && $#fields != 5)
380 printf STDERR ("Entry for $raw_code has wrong number of fields (%d)\n", $#fields);
384 if (!defined $type[$code])
386 printf STDERR "Special case for code point: $code, which has no defined type\n";
390 if (defined $fields[5]) {
391 # Ignore conditional special cases - we'll handle them in code
395 if ($type[$code] eq 'Lu')
397 (hex $fields[$CASE_UPPER] == $code) || die "$raw_code is Lu and UCD_Upper($raw_code) != $raw_code";
399 &add_special_case ($code, $value[$code], $fields[$CASE_LOWER], $fields[$CASE_TITLE]);
401 } elsif ($type[$code] eq 'Lt')
403 (hex $fields[$CASE_TITLE] == $code) || die "$raw_code is Lt and UCD_Title($raw_code) != $raw_code";
405 &add_special_case ($code, undef, $fields[$CASE_LOWER], $fields[$CASE_UPPER]);
406 } elsif ($type[$code] eq 'Ll')
408 (hex $fields[$CASE_LOWER] == $code) || die "$raw_code is Ll and UCD_Lower($raw_code) != $raw_code";
410 &add_special_case ($code, $value[$code], $fields[$CASE_UPPER], $fields[$CASE_TITLE]);
412 printf STDERR "Special case for non-alphabetic code point: $raw_code\n";
419 open (INPUT, "< $casefoldingtxt") || exit 1;
435 @fields = split ('\s*;\s*', $_, 30);
437 $raw_code = $fields[$FOLDING_CODE];
438 $code = hex ($raw_code);
442 printf STDERR ("Entry for $raw_code has wrong number of fields (%d)\n", $#fields);
446 # we don't use Simple or Turkic rules here
447 next if ($fields[$FOLDING_STATUS] =~ /^[ST]$/);
449 @values = map { hex ($_) } split /\s+/, $fields[$FOLDING_MAPPING];
454 !(defined $value[$code] && $value[$code] >= 0x1000000) &&
455 defined $type[$code]) {
458 if ($type[$code] eq 'Ll')
461 } elsif ($type[$code] eq 'Lt')
463 $lower = $title_to_lower{$code};
464 } elsif ($type[$code] eq 'Lu')
466 $lower = $value[$code];
471 if ($lower == $values[0]) {
476 my $string = pack ("U*", @values);
478 if (1 + &length_in_bytes ($string) > $casefoldlen) {
479 $casefoldlen = 1 + &length_in_bytes ($string);
482 push @casefold, [ $code, &escape ($string) ];
488 &print_tables ($last_code)
491 &print_decomp ($last_code);
492 &output_composition_table;
495 &print_line_break ($last_code);
500 # perl "length" returns the length in characters
506 return length $string;
509 # Process a single character.
512 my ($code, @fields) = @_;
514 $type[$code] = $fields[$CATEGORY];
515 if ($type[$code] eq 'Nd')
517 $value[$code] = int ($fields[$DECIMAL_VALUE]);
519 elsif ($type[$code] eq 'Ll')
521 $value[$code] = hex ($fields[$UPPER]);
523 elsif ($type[$code] eq 'Lu')
525 $value[$code] = hex ($fields[$LOWER]);
528 if ($type[$code] eq 'Lt')
530 $title_to_lower{$code} = hex ($fields[$LOWER]);
531 $title_to_upper{$code} = hex ($fields[$UPPER]);
534 $cclass[$code] = $fields[$COMBINING_CLASSES];
536 # Handle decompositions.
537 if ($fields[$DECOMPOSITION] ne '')
539 if ($fields[$DECOMPOSITION] =~ s/\<.*\>\s*//) {
540 $decompose_compat[$code] = 1;
542 $decompose_compat[$code] = 0;
544 if (!exists $composition_exclusions{$code}) {
545 $compositions{$code} = $fields[$DECOMPOSITION];
548 $decompositions[$code] = $fields[$DECOMPOSITION];
555 my ($outfile) = "gunichartables.h";
557 local ($bytes_out) = 0;
559 print "Writing $outfile...\n";
561 open (OUT, "> $outfile");
563 print OUT "/* This file is automatically generated. DO NOT EDIT!\n";
564 print OUT " Instead, edit gen-unicode-tables.pl and re-run. */\n\n";
566 print OUT "#ifndef CHARTABLES_H\n";
567 print OUT "#define CHARTABLES_H\n\n";
569 print OUT "#define G_UNICODE_DATA_VERSION \"$ARGV[0]\"\n\n";
571 printf OUT "#define G_UNICODE_LAST_CHAR 0x%04x\n\n", $last;
573 printf OUT "#define G_UNICODE_MAX_TABLE_INDEX 10000\n\n";
575 my $last_part1 = ($pages_before_e0000 * 256) - 1;
576 printf OUT "#define G_UNICODE_LAST_CHAR_PART1 0x%04X\n\n", $last_part1;
577 printf OUT "#define G_UNICODE_LAST_PAGE_PART1 %d\n\n", $pages_before_e0000 - 1;
580 printf OUT "static const char type_data[][256] = {\n";
581 for ($count = 0; $count <= $last; $count += 256)
583 $row[$count / 256] = &print_row ($count, 1, \&fetch_type);
585 printf OUT "\n};\n\n";
587 printf OUT "/* U+0000 through U+%04X */\n", $last_part1;
588 print OUT "static const gint16 type_table_part1[$pages_before_e0000] = {\n";
589 for ($count = 0; $count <= $last_part1; $count += 256)
591 print OUT ",\n" if $count > 0;
592 print OUT " ", $row[$count / 256];
595 print OUT "\n};\n\n";
597 printf OUT "/* U+E0000 through U+%04X */\n", $last;
598 print OUT "static const gint16 type_table_part2[768] = {\n";
599 for ($count = 0xE0000; $count <= $last; $count += 256)
601 print OUT ",\n" if $count > 0xE0000;
602 print OUT " ", $row[$count / 256];
605 print OUT "\n};\n\n";
609 # Now print attribute table.
613 printf OUT "static const gunichar attr_data[][256] = {\n";
614 for ($count = 0; $count <= $last; $count += 256)
616 $row[$count / 256] = &print_row ($count, 4, \&fetch_attr);
618 printf OUT "\n};\n\n";
620 printf OUT "/* U+0000 through U+%04X */\n", $last_part1;
621 print OUT "static const gint16 attr_table_part1[$pages_before_e0000] = {\n";
622 for ($count = 0; $count <= $last_part1; $count += 256)
624 print OUT ",\n" if $count > 0;
625 print OUT " ", $row[$count / 256];
628 print OUT "\n};\n\n";
630 printf OUT "/* U+E0000 through U+%04X */\n", $last;
631 print OUT "static const gint16 attr_table_part2[768] = {\n";
632 for ($count = 0xE0000; $count <= $last; $count += 256)
634 print OUT ",\n" if $count > 0xE0000;
635 print OUT " ", $row[$count / 256];
638 print OUT "\n};\n\n";
641 # print title case table
644 print OUT "static const gunichar title_table[][3] = {\n";
647 foreach $item (sort keys %title_to_lower)
652 printf OUT " { 0x%04x, 0x%04x, 0x%04x }", $item, $title_to_upper{$item}, $title_to_lower{$item};
655 print OUT "\n};\n\n";
658 # And special case conversion table -- conversions that change length
660 &output_special_case_table (\*OUT);
661 &output_casefold_table (\*OUT);
663 print OUT "#endif /* CHARTABLES_H */\n";
667 printf STDERR "Generated %d bytes in tables\n", $bytes_out;
670 # A fetch function for the type table.
674 return $mappings{$type[$index]};
677 # A fetch function for the attribute table.
681 if (defined $value[$index])
683 return sprintf ("0x%04x", $value[$index]);
693 my ($start, $typsize, $fetcher) = @_;
700 for ($off = 0; $off < 256; ++$off)
702 $values[$off] = $fetcher->($off + $start);
703 if ($values[$off] ne $values[0])
710 return $values[0] . " + G_UNICODE_MAX_TABLE_INDEX";
713 printf OUT ",\n" if ($table_index != 0);
714 printf OUT " { /* page %d, index %d */\n ", $start / 256, $table_index;
716 for ($i = $start; $i < $start + 256; ++$i)
720 my ($text) = $values[$i - $start];
721 if (length ($text) + $column + 2 > 78)
727 $column += length ($text) + 2;
731 $bytes_out += 256 * $typsize;
733 return sprintf "%d /* page %d */", $table_index++, $start / 256;
740 my $escaped = unpack("H*", $string);
741 $escaped =~ s/(.{2})/\\x$1/g;
746 # Returns the offset of $decomp in the offset string. Updates the
747 # referenced variables as appropriate.
748 sub handle_decomp ($$$$)
750 my ($decomp, $decomp_offsets_ref, $decomp_string_ref, $decomp_string_offset_ref) = @_;
751 my $offset = "G_UNICODE_NOT_PRESENT_OFFSET";
755 if (defined $decomp_offsets_ref->{$decomp})
757 $offset = $decomp_offsets_ref->{$decomp};
761 $offset = ${$decomp_string_offset_ref};
762 $decomp_offsets_ref->{$decomp} = $offset;
763 ${$decomp_string_ref} .= "\n \"" . &escape ($decomp) . "\\0\" /* offset ${$decomp_string_offset_ref} */";
764 ${$decomp_string_offset_ref} += &length_in_bytes ($decomp) + 1;
771 # Generate the character decomposition header.
775 my ($outfile) = "gunidecomp.h";
777 local ($bytes_out) = 0;
779 print "Writing $outfile...\n";
781 open (OUT, "> $outfile") || exit 1;
783 print OUT "/* This file is automatically generated. DO NOT EDIT! */\n\n";
784 print OUT "#ifndef DECOMP_H\n";
785 print OUT "#define DECOMP_H\n\n";
787 printf OUT "#define G_UNICODE_LAST_CHAR 0x%04x\n\n", $last;
789 printf OUT "#define G_UNICODE_MAX_TABLE_INDEX (0x110000 / 256)\n\n";
791 my $last_part1 = ($pages_before_e0000 * 256) - 1;
792 printf OUT "#define G_UNICODE_LAST_CHAR_PART1 0x%04X\n\n", $last_part1;
793 printf OUT "#define G_UNICODE_LAST_PAGE_PART1 %d\n\n", $pages_before_e0000 - 1;
795 $NOT_PRESENT_OFFSET = 65535;
796 print OUT "#define G_UNICODE_NOT_PRESENT_OFFSET $NOT_PRESENT_OFFSET\n\n";
800 printf OUT "static const guchar cclass_data[][256] = {\n";
801 for ($count = 0; $count <= $last; $count += 256)
803 $row[$count / 256] = &print_row ($count, 1, \&fetch_cclass);
805 printf OUT "\n};\n\n";
807 print OUT "static const gint16 combining_class_table_part1[$pages_before_e0000] = {\n";
808 for ($count = 0; $count <= $last_part1; $count += 256)
810 print OUT ",\n" if $count > 0;
811 print OUT " ", $row[$count / 256];
814 print OUT "\n};\n\n";
816 print OUT "static const gint16 combining_class_table_part2[768] = {\n";
817 for ($count = 0xE0000; $count <= $last; $count += 256)
819 print OUT ",\n" if $count > 0xE0000;
820 print OUT " ", $row[$count / 256];
823 print OUT "\n};\n\n";
825 print OUT "typedef struct\n{\n";
826 print OUT " gunichar ch;\n";
827 print OUT " guint16 canon_offset;\n";
828 print OUT " guint16 compat_offset;\n";
829 print OUT "} decomposition;\n\n";
831 print OUT "static const decomposition decomp_table[] =\n{\n";
834 my ($decomp_string) = "";
835 my ($decomp_string_offset) = 0;
836 for ($count = 0; $count <= $last; ++$count)
838 if (defined $decompositions[$count])
847 if (!$decompose_compat[$count]) {
848 $canon_decomp = make_decomp ($count, 0);
850 $compat_decomp = make_decomp ($count, 1);
852 if (defined $canon_decomp && $compat_decomp eq $canon_decomp) {
853 undef $compat_decomp;
856 my $canon_offset = handle_decomp ($canon_decomp, \%decomp_offsets, \$decomp_string, \$decomp_string_offset);
857 my $compat_offset = handle_decomp ($compat_decomp, \%decomp_offsets, \$decomp_string, \$decomp_string_offset);
859 die if $decomp_string_offset > $NOT_PRESENT_OFFSET;
861 printf OUT qq( { 0x%04x, $canon_offset, $compat_offset }), $count;
865 print OUT "\n};\n\n";
866 $bytes_out += $decomp_string_offset + 1;
868 printf OUT "static const gchar decomp_expansion_string[] = %s;\n\n", $decomp_string;
870 print OUT "#endif /* DECOMP_H */\n";
872 printf STDERR "Generated %d bytes in decomp tables\n", $bytes_out;
878 my ($outfile) = "gunibreak.h";
880 local ($bytes_out) = 0;
882 print "Writing $outfile...\n";
884 open (OUT, "> $outfile");
886 print OUT "/* This file is automatically generated. DO NOT EDIT!\n";
887 print OUT " Instead, edit gen-unicode-tables.pl and re-run. */\n\n";
889 print OUT "#ifndef BREAKTABLES_H\n";
890 print OUT "#define BREAKTABLES_H\n\n";
892 print OUT "#define G_UNICODE_DATA_VERSION \"$ARGV[0]\"\n\n";
894 printf OUT "#define G_UNICODE_LAST_CHAR 0x%04X\n\n", $last;
896 printf OUT "#define G_UNICODE_MAX_TABLE_INDEX 10000\n\n";
898 my $last_part1 = ($pages_before_e0000 * 256) - 1;
899 printf OUT "/* the last code point that should be looked up in break_property_table_part1 */\n";
900 printf OUT "#define G_UNICODE_LAST_CHAR_PART1 0x%04X\n\n", $last_part1;
903 printf OUT "static const gint8 break_property_data[][256] = {\n";
904 for ($count = 0; $count <= $last; $count += 256)
906 $row[$count / 256] = &print_row ($count, 1, \&fetch_break_type);
908 printf OUT "\n};\n\n";
910 printf OUT "/* U+0000 through U+%04X */\n", $last_part1;
911 print OUT "static const gint16 break_property_table_part1[$pages_before_e0000] = {\n";
912 for ($count = 0; $count <= $last_part1; $count += 256)
914 print OUT ",\n" if $count > 0;
915 print OUT " ", $row[$count / 256];
918 print OUT "\n};\n\n";
920 printf OUT "/* U+E0000 through U+%04X */\n", $last;
921 print OUT "static const gint16 break_property_table_part2[768] = {\n";
922 for ($count = 0xE0000; $count <= $last; $count += 256)
924 print OUT ",\n" if $count > 0xE0000;
925 print OUT " ", $row[$count / 256];
928 print OUT "\n};\n\n";
931 print OUT "#endif /* BREAKTABLES_H */\n";
935 printf STDERR "Generated %d bytes in break tables\n", $bytes_out;
939 # A fetch function for the break properties table.
943 return $break_mappings{$break_props[$index]};
946 # Fetcher for combining class.
953 # Expand a character decomposition recursively.
956 my ($code, $compat) = @_;
960 foreach $iter (split (' ', $decompositions[$code]))
963 if (defined $decompositions[$val] &&
964 ($compat || !$decompose_compat[$val]))
966 push (@result, &expand_decomp ($val, $compat));
970 push (@result, $val);
979 my ($code, $compat) = @_;
982 foreach $iter (&expand_decomp ($code, $compat))
984 $result .= pack ("U", $iter); # to utf-8
989 # Generate special case data string from two fields
992 my ($code, $single, $field1, $field2) = @_;
994 @values = (defined $single ? $single : (),
995 (map { hex ($_) } split /\s+/, $field1),
997 (map { hex ($_) } split /\s+/, $field2));
1001 for $value (@values) {
1002 $result .= pack ("U", $value); # to utf-8
1005 push @special_case_offsets, $special_case_offset;
1007 # We encode special cases up in the 0x1000000 space
1008 $value[$code] = 0x1000000 + $special_case_offset;
1010 $special_case_offset += 1 + &length_in_bytes ($result);
1012 push @special_cases, &escape ($result);
1015 sub output_special_case_table
1021 /* Table of special cases for case conversion; each record contains
1022 * First, the best single character mapping to lowercase if Lu,
1023 * and to uppercase if Ll, followed by the output mapping for the two cases
1024 * other than the case of the codepoint, in the order [Ll],[Lu],[Lt],
1025 * encoded in UTF-8, separated and terminated by a null character.
1027 static const gchar special_case_table[] = {
1031 for $case (@special_cases) {
1032 print $out qq( "$case\\0" /* offset ${special_case_offsets[$i]} */\n);
1041 print STDERR "Generated " . ($special_case_offset + 1) . " bytes in special case table\n";
1044 sub enumerate_ordered
1049 for my $code (sort { $a <=> $b } keys %$array) {
1050 if ($array->{$code} == 1) {
1051 delete $array->{$code};
1054 $array->{$code} = $n++;
1060 sub output_composition_table
1062 print STDERR "Generating composition table\n";
1064 local ($bytes_out) = 0;
1069 # First we need to go through and remove decompositions
1070 # starting with a non-starter, and single-character
1071 # decompositions. At the same time, record
1072 # the first and second character of each decomposition
1074 for $code (keys %compositions)
1076 @values = map { hex ($_) } split /\s+/, $compositions{$code};
1079 if ($cclass[$values[0]]) {
1080 delete $compositions{$code};
1084 # single-character decompositions
1086 delete $compositions{$code};
1091 die "$code has more than two elements in its decomposition!\n";
1094 if (exists $first{$values[0]}) {
1095 $first{$values[0]}++;
1097 $first{$values[0]} = 1;
1101 # Assign integer indices, removing singletons
1102 my $n_first = enumerate_ordered (\%first);
1104 # Now record the second character of each (non-singleton) decomposition
1105 for $code (keys %compositions) {
1106 @values = map { hex ($_) } split /\s+/, $compositions{$code};
1108 if (exists $first{$values[0]}) {
1109 if (exists $second{$values[1]}) {
1110 $second{$values[1]}++;
1112 $second{$values[1]} = 1;
1117 # Assign integer indices, removing duplicate
1118 my $n_second = enumerate_ordered (\%second);
1120 # Build reverse table
1122 my @first_singletons;
1123 my @second_singletons;
1125 for $code (keys %compositions) {
1126 @values = map { hex ($_) } split /\s+/, $compositions{$code};
1128 my $first = $first{$values[0]};
1129 my $second = $second{$values[1]};
1131 if (defined $first && defined $second) {
1132 $reverse{"$first|$second"} = $code;
1133 } elsif (!defined $first) {
1134 push @first_singletons, [ $values[0], $values[1], $code ];
1136 push @second_singletons, [ $values[1], $values[0], $code ];
1140 @first_singletons = sort { $a->[0] <=> $b->[0] } @first_singletons;
1141 @second_singletons = sort { $a->[0] <=> $b->[0] } @second_singletons;
1145 open OUT, ">gunicomp.h" or die "Cannot open gunicomp.h: $!\n";
1147 # Assign values in lookup table for all code points involved
1151 printf OUT "#define COMPOSE_FIRST_START %d\n", $total;
1152 for $code (keys %first) {
1153 $vals{$code} = $first{$code} + $total;
1154 $last = $code if $code > $last;
1158 printf OUT "#define COMPOSE_FIRST_SINGLE_START %d\n", $total;
1159 for $record (@first_singletons) {
1160 my $code = $record->[0];
1161 $vals{$code} = $i++ + $total;
1162 $last = $code if $code > $last;
1164 $total += @first_singletons;
1165 printf OUT "#define COMPOSE_SECOND_START %d\n", $total;
1166 for $code (keys %second) {
1167 $vals{$code} = $second{$code} + $total;
1168 $last = $code if $code > $last;
1170 $total += $n_second;
1172 printf OUT "#define COMPOSE_SECOND_SINGLE_START %d\n\n", $total;
1173 for $record (@second_singletons) {
1174 my $code = $record->[0];
1175 $vals{$code} = $i++ + $total;
1176 $last = $code if $code > $last;
1179 printf OUT "#define COMPOSE_TABLE_LAST %d\n\n", $last / 256;
1181 # Output lookup table
1185 printf OUT "static const guint16 compose_data[][256] = {\n";
1186 for (my $count = 0; $count <= $last; $count += 256)
1188 $row[$count / 256] = &print_row ($count, 2, sub { exists $vals{$_[0]} ? $vals{$_[0]} : 0; });
1190 printf OUT "\n};\n\n";
1192 print OUT "static const gint16 compose_table[COMPOSE_TABLE_LAST + 1] = {\n";
1193 for (my $count = 0; $count <= $last; $count += 256)
1195 print OUT ",\n" if $count > 0;
1196 print OUT " ", $row[$count / 256];
1199 print OUT "\n};\n\n";
1201 # Output first singletons
1203 print OUT "static const guint16 compose_first_single[][2] = {\n";
1205 for $record (@first_singletons) {
1206 if ($record->[1] > 0xFFFF or $record->[2] > 0xFFFF) {
1207 die "time to switch compose_first_single to gunichar" ;
1209 print OUT ",\n" if $i++ > 0;
1210 printf OUT " { %#06x, %#06x }", $record->[1], $record->[2];
1214 $bytes_out += @first_singletons * 4;
1216 # Output second singletons
1218 print OUT "static const guint16 compose_second_single[][2] = {\n";
1220 for $record (@second_singletons) {
1221 if ($record->[1] > 0xFFFF or $record->[2] > 0xFFFF) {
1222 die "time to switch compose_second_single to gunichar";
1224 print OUT ",\n" if $i++ > 0;
1225 printf OUT " { %#06x, %#06x }", $record->[1], $record->[2];
1229 $bytes_out += @second_singletons * 4;
1231 # Output array of composition pairs
1234 static const guint16 compose_array[$n_first][$n_second] = {
1237 for (my $i = 0; $i < $n_first; $i++) {
1238 print OUT ",\n" if $i;
1240 for (my $j = 0; $j < $n_second; $j++) {
1241 print OUT ", " if $j;
1242 if (exists $reverse{"$i|$j"}) {
1243 if ($reverse{"$i|$j"} > 0xFFFF) {
1244 die "time to switch compose_array to gunichar" ;
1246 printf OUT "0x%04x", $reverse{"$i|$j"};
1259 $bytes_out += $n_first * $n_second * 2;
1261 printf STDERR "Generated %d bytes in compose tables\n", $bytes_out;
1264 sub output_casefold_table
1270 /* Table of casefolding cases that can't be derived by lowercasing
1272 static const struct {
1274 gchar data[$casefoldlen];
1275 } casefold_table[] = {
1278 @casefold = sort { $a->[0] <=> $b->[0] } @casefold;
1280 for $case (@casefold)
1283 $string = $case->[1];
1285 if ($code > 0xFFFF) {
1286 die "time to switch casefold_table to gunichar" ;
1289 print $out sprintf(qq( { 0x%04x, "$string" },\n), $code);
1298 my $recordlen = (2+$casefoldlen+1) & ~1;
1299 printf "Generated %d bytes for casefold table\n", $recordlen * @casefold;