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 # we use some perl unicode features
37 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);
40 # Names of fields in Unicode data table.
44 $COMBINING_CLASSES = 3;
57 # Names of fields in the line break table
61 # Names of fields in the SpecialCasing table
68 # Names of fields in the CaseFolding table
73 # Map general category code onto symbolic name.
77 'Lu' => "G_UNICODE_UPPERCASE_LETTER",
78 'Ll' => "G_UNICODE_LOWERCASE_LETTER",
79 'Lt' => "G_UNICODE_TITLECASE_LETTER",
80 'Mn' => "G_UNICODE_NON_SPACING_MARK",
81 'Mc' => "G_UNICODE_COMBINING_MARK",
82 'Me' => "G_UNICODE_ENCLOSING_MARK",
83 'Nd' => "G_UNICODE_DECIMAL_NUMBER",
84 'Nl' => "G_UNICODE_LETTER_NUMBER",
85 'No' => "G_UNICODE_OTHER_NUMBER",
86 'Zs' => "G_UNICODE_SPACE_SEPARATOR",
87 'Zl' => "G_UNICODE_LINE_SEPARATOR",
88 'Zp' => "G_UNICODE_PARAGRAPH_SEPARATOR",
89 'Cc' => "G_UNICODE_CONTROL",
90 'Cf' => "G_UNICODE_FORMAT",
91 'Cs' => "G_UNICODE_SURROGATE",
92 'Co' => "G_UNICODE_PRIVATE_USE",
93 'Cn' => "G_UNICODE_UNASSIGNED",
96 'Lm' => "G_UNICODE_MODIFIER_LETTER",
97 'Lo' => "G_UNICODE_OTHER_LETTER",
98 'Pc' => "G_UNICODE_CONNECT_PUNCTUATION",
99 'Pd' => "G_UNICODE_DASH_PUNCTUATION",
100 'Ps' => "G_UNICODE_OPEN_PUNCTUATION",
101 'Pe' => "G_UNICODE_CLOSE_PUNCTUATION",
102 'Pi' => "G_UNICODE_INITIAL_PUNCTUATION",
103 'Pf' => "G_UNICODE_FINAL_PUNCTUATION",
104 'Po' => "G_UNICODE_OTHER_PUNCTUATION",
105 'Sm' => "G_UNICODE_MATH_SYMBOL",
106 'Sc' => "G_UNICODE_CURRENCY_SYMBOL",
107 'Sk' => "G_UNICODE_MODIFIER_SYMBOL",
108 'So' => "G_UNICODE_OTHER_SYMBOL"
113 'BK' => "G_UNICODE_BREAK_MANDATORY",
114 'CR' => "G_UNICODE_BREAK_CARRIAGE_RETURN",
115 'LF' => "G_UNICODE_BREAK_LINE_FEED",
116 'CM' => "G_UNICODE_BREAK_COMBINING_MARK",
117 'SG' => "G_UNICODE_BREAK_SURROGATE",
118 'ZW' => "G_UNICODE_BREAK_ZERO_WIDTH_SPACE",
119 'IN' => "G_UNICODE_BREAK_INSEPARABLE",
120 'GL' => "G_UNICODE_BREAK_NON_BREAKING_GLUE",
121 'CB' => "G_UNICODE_BREAK_CONTINGENT",
122 'SP' => "G_UNICODE_BREAK_SPACE",
123 'BA' => "G_UNICODE_BREAK_AFTER",
124 'BB' => "G_UNICODE_BREAK_BEFORE",
125 'B2' => "G_UNICODE_BREAK_BEFORE_AND_AFTER",
126 'HY' => "G_UNICODE_BREAK_HYPHEN",
127 'NS' => "G_UNICODE_BREAK_NON_STARTER",
128 'OP' => "G_UNICODE_BREAK_OPEN_PUNCTUATION",
129 'CL' => "G_UNICODE_BREAK_CLOSE_PUNCTUATION",
130 'QU' => "G_UNICODE_BREAK_QUOTATION",
131 'EX' => "G_UNICODE_BREAK_EXCLAMATION",
132 'ID' => "G_UNICODE_BREAK_IDEOGRAPHIC",
133 'NU' => "G_UNICODE_BREAK_NUMERIC",
134 'IS' => "G_UNICODE_BREAK_INFIX_SEPARATOR",
135 'SY' => "G_UNICODE_BREAK_SYMBOL",
136 'AL' => "G_UNICODE_BREAK_ALPHABETIC",
137 'PR' => "G_UNICODE_BREAK_PREFIX",
138 'PO' => "G_UNICODE_BREAK_POSTFIX",
139 'SA' => "G_UNICODE_BREAK_COMPLEX_CONTEXT",
140 'AI' => "G_UNICODE_BREAK_AMBIGUOUS",
141 'NL' => "G_UNICODE_BREAK_NEXT_LINE",
142 'WJ' => "G_UNICODE_BREAK_WORD_JOINER",
143 'XX' => "G_UNICODE_BREAK_UNKNOWN"
146 # Title case mappings.
147 %title_to_lower = ();
148 %title_to_upper = ();
150 # Maximum length of special-case strings
153 my @special_case_offsets;
154 my $special_case_offset = 0;
158 if (@ARGV && $ARGV[0] eq '-decomp')
164 elsif (@ARGV && $ARGV[0] eq '-both')
172 die "Usage: $0 [-decomp | -both] UNICODE-VERSION UnicodeData.txt LineBreak.txt SpecialCasing.txt CaseFolding.txt CompositionExclusions.txt\n";
175 print "Creating decomp table\n" if ($do_decomp);
176 print "Creating property table\n" if ($do_props);
178 print "Composition exlusions from $ARGV[5]\n";
180 open (INPUT, "< $ARGV[5]") || exit 1;
194 $composition_exclusions{hex($_)} = 1;
199 print "Unicode data from $ARGV[1]\n";
201 open (INPUT, "< $ARGV[1]") || exit 1;
203 # we save memory by skipping the huge empty area before U+E0000
204 my $pages_before_e0000;
210 @fields = split (';', $_, 30);
213 printf STDERR ("Entry for $fields[$CODE] has wrong number of fields (%d)\n", $#fields);
216 $code = hex ($fields[$CODE]);
218 if ($code >= 0xE0000 and $last_code < 0xE0000)
220 $pages_before_e0000 = ($last_code >> 8) + 1;
223 if ($code > $last_code + 1)
226 if ($fields[$NAME] =~ /Last>/)
228 # Fill the gap with the last character read,
229 # since this was a range specified in the char database
234 # The gap represents undefined characters. Only the type
236 @gfields = ('', '', 'Cn', '0', '', '', '', '', '', '', '',
239 for (++$last_code; $last_code < $code; ++$last_code)
241 $gfields{$CODE} = sprintf ("%04x", $last_code);
242 &process_one ($last_code, @gfields);
245 &process_one ($code, @fields);
251 @gfields = ('', '', 'Cn', '0', '', '', '', '', '', '', '',
253 for (++$last_code; $last_code <= 0x10FFFF; ++$last_code)
255 $gfields{$CODE} = sprintf ("%04x", $last_code);
256 &process_one ($last_code, @gfields);
258 --$last_code; # Want last to be 0x10FFFF.
260 print "Creating line break table\n";
262 print "Line break data from $ARGV[2]\n";
264 open (INPUT, "< $ARGV[2]") || exit 1;
269 my ($start_code, $end_code);
277 @fields = split (';', $_, 30);
280 printf STDERR ("Entry for $fields[$CODE] has wrong number of fields (%d)\n", $#fields);
284 if ($fields[$CODE] =~ /([A-F0-9]{4,6})\.\.([A-F0-9]{4,6})/)
286 $start_code = hex ($1);
287 $end_code = hex ($2);
289 $start_code = $end_code = hex ($fields[$CODE]);
293 if ($start_code > $last_code + 1)
295 # The gap represents undefined characters. If assigned,
296 # they are AL, if not assigned, XX
297 for (++$last_code; $last_code < $start_code; ++$last_code)
299 if ($type[$last_code] eq 'Cn')
301 $break_props[$last_code] = 'XX';
305 $break_props[$last_code] = 'AL';
310 for ($last_code = $start_code; $last_code <= $end_code; $last_code++)
312 $break_props[$last_code] = $fields[$BREAK_PROPERTY];
315 $last_code = $end_code;
320 for (++$last_code; $last_code <= 0x10FFFF; ++$last_code)
322 if ($type[$last_code] eq 'Cn')
324 $break_props[$last_code] = 'XX';
328 $break_props[$last_code] = 'AL';
331 --$last_code; # Want last to be 0x10FFFF.
333 print STDERR "Last code is not 0x10FFFF" if ($last_code != 0x10FFFF);
335 print "Reading special-casing table for case conversion\n";
337 open (INPUT, "< $ARGV[3]") || exit 1;
350 @fields = split ('\s*;\s*', $_, 30);
352 $raw_code = $fields[$CASE_CODE];
353 $code = hex ($raw_code);
355 if ($#fields != 4 && $#fields != 5)
357 printf STDERR ("Entry for $raw_code has wrong number of fields (%d)\n", $#fields);
361 if (!defined $type[$code])
363 printf STDERR "Special case for code point: $code, which has no defined type\n";
367 if (defined $fields[5]) {
368 # Ignore conditional special cases - we'll handle them in code
372 if ($type[$code] eq 'Lu')
374 (hex $fields[$CASE_UPPER] == $code) || die "$raw_code is Lu and UCD_Upper($raw_code) != $raw_code";
376 &add_special_case ($code, $value[$code], $fields[$CASE_LOWER], $fields[$CASE_TITLE]);
378 } elsif ($type[$code] eq 'Lt')
380 (hex $fields[$CASE_TITLE] == $code) || die "$raw_code is Lt and UCD_Title($raw_code) != $raw_code";
382 &add_special_case ($code, undef, $fields[$CASE_LOWER], $fields[$CASE_UPPER]);
383 } elsif ($type[$code] eq 'Ll')
385 (hex $fields[$CASE_LOWER] == $code) || die "$raw_code is Ll and UCD_Lower($raw_code) != $raw_code";
387 &add_special_case ($code, $value[$code], $fields[$CASE_UPPER], $fields[$CASE_TITLE]);
389 printf STDERR "Special case for non-alphabetic code point: $raw_code\n";
396 open (INPUT, "< $ARGV[4]") || exit 1;
412 @fields = split ('\s*;\s*', $_, 30);
414 $raw_code = $fields[$FOLDING_CODE];
415 $code = hex ($raw_code);
419 printf STDERR ("Entry for $raw_code has wrong number of fields (%d)\n", $#fields);
423 # we don't use Simple or Turkic rules here
424 next if ($fields[$FOLDING_STATUS] =~ /^[ST]$/);
426 @values = map { hex ($_) } split /\s+/, $fields[$FOLDING_MAPPING];
431 !(defined $value[$code] && $value[$code] >= 0x1000000) &&
432 defined $type[$code]) {
435 if ($type[$code] eq 'Ll')
438 } elsif ($type[$code] eq 'Lt')
440 $lower = $title_to_lower{$code};
441 } elsif ($type[$code] eq 'Lu')
443 $lower = $value[$code];
448 if ($lower == $values[0]) {
453 my $string = pack ("U*", @values);
455 if (1 + &length_in_bytes ($string) > $casefoldlen) {
456 $casefoldlen = 1 + &length_in_bytes ($string);
459 push @casefold, [ $code, &escape ($string) ];
465 &print_tables ($last_code)
468 &print_decomp ($last_code);
469 &output_composition_table;
472 &print_line_break ($last_code);
477 # perl "length" returns the length in characters
483 return length $string;
486 # Process a single character.
489 my ($code, @fields) = @_;
491 $type[$code] = $fields[$CATEGORY];
492 if ($type[$code] eq 'Nd')
494 $value[$code] = int ($fields[$DECIMAL_VALUE]);
496 elsif ($type[$code] eq 'Ll')
498 $value[$code] = hex ($fields[$UPPER]);
500 elsif ($type[$code] eq 'Lu')
502 $value[$code] = hex ($fields[$LOWER]);
505 if ($type[$code] eq 'Lt')
507 $title_to_lower{$code} = hex ($fields[$LOWER]);
508 $title_to_upper{$code} = hex ($fields[$UPPER]);
511 $cclass[$code] = $fields[$COMBINING_CLASSES];
513 # Handle decompositions.
514 if ($fields[$DECOMPOSITION] ne '')
516 if ($fields[$DECOMPOSITION] =~ s/\<.*\>\s*//) {
517 $decompose_compat[$code] = 1;
519 $decompose_compat[$code] = 0;
521 if (!exists $composition_exclusions{$code}) {
522 $compositions{$code} = $fields[$DECOMPOSITION];
525 $decompositions[$code] = $fields[$DECOMPOSITION];
532 my ($outfile) = "gunichartables.h";
534 local ($bytes_out) = 0;
536 print "Writing $outfile...\n";
538 open (OUT, "> $outfile");
540 print OUT "/* This file is automatically generated. DO NOT EDIT!\n";
541 print OUT " Instead, edit gen-unicode-tables.pl and re-run. */\n\n";
543 print OUT "#ifndef CHARTABLES_H\n";
544 print OUT "#define CHARTABLES_H\n\n";
546 print OUT "#define G_UNICODE_DATA_VERSION \"$ARGV[0]\"\n\n";
548 printf OUT "#define G_UNICODE_LAST_CHAR 0x%04x\n\n", $last;
550 printf OUT "#define G_UNICODE_MAX_TABLE_INDEX 10000\n\n";
552 my $last_part1 = ($pages_before_e0000 * 256) - 1;
553 printf OUT "#define G_UNICODE_LAST_CHAR_PART1 0x%04X\n\n", $last_part1;
554 printf OUT "#define G_UNICODE_LAST_PAGE_PART1 %d\n\n", $pages_before_e0000 - 1;
557 printf OUT "static const char type_data[][256] = {\n";
558 for ($count = 0; $count <= $last; $count += 256)
560 $row[$count / 256] = &print_row ($count, 1, \&fetch_type);
562 printf OUT "\n};\n\n";
564 printf OUT "/* U+0000 through U+%04X */\n", $last_part1;
565 print OUT "static const gint16 type_table_part1[$pages_before_e0000] = {\n";
566 for ($count = 0; $count <= $last_part1; $count += 256)
568 print OUT ",\n" if $count > 0;
569 print OUT " ", $row[$count / 256];
572 print OUT "\n};\n\n";
574 printf OUT "/* U+E0000 through U+%04X */\n", $last;
575 print OUT "static const gint16 type_table_part2[768] = {\n";
576 for ($count = 0xE0000; $count <= $last; $count += 256)
578 print OUT ",\n" if $count > 0xE0000;
579 print OUT " ", $row[$count / 256];
582 print OUT "\n};\n\n";
586 # Now print attribute table.
590 printf OUT "static const gunichar attr_data[][256] = {\n";
591 for ($count = 0; $count <= $last; $count += 256)
593 $row[$count / 256] = &print_row ($count, 4, \&fetch_attr);
595 printf OUT "\n};\n\n";
597 printf OUT "/* U+0000 through U+%04X */\n", $last_part1;
598 print OUT "static const gint16 attr_table_part1[$pages_before_e0000] = {\n";
599 for ($count = 0; $count <= $last_part1; $count += 256)
601 print OUT ",\n" if $count > 0;
602 print OUT " ", $row[$count / 256];
605 print OUT "\n};\n\n";
607 printf OUT "/* U+E0000 through U+%04X */\n", $last;
608 print OUT "static const gint16 attr_table_part2[768] = {\n";
609 for ($count = 0xE0000; $count <= $last; $count += 256)
611 print OUT ",\n" if $count > 0xE0000;
612 print OUT " ", $row[$count / 256];
615 print OUT "\n};\n\n";
618 # print title case table
621 print OUT "static const gunichar title_table[][3] = {\n";
624 foreach $item (sort keys %title_to_lower)
629 printf OUT " { 0x%04x, 0x%04x, 0x%04x }", $item, $title_to_upper{$item}, $title_to_lower{$item};
632 print OUT "\n};\n\n";
635 # And special case conversion table -- conversions that change length
637 &output_special_case_table (\*OUT);
638 &output_casefold_table (\*OUT);
640 print OUT "#endif /* CHARTABLES_H */\n";
644 printf STDERR "Generated %d bytes in tables\n", $bytes_out;
647 # A fetch function for the type table.
651 return $mappings{$type[$index]};
654 # A fetch function for the attribute table.
658 if (defined $value[$index])
660 return sprintf ("0x%04x", $value[$index]);
670 my ($start, $typsize, $fetcher) = @_;
677 for ($off = 0; $off < 256; ++$off)
679 $values[$off] = $fetcher->($off + $start);
680 if ($values[$off] ne $values[0])
687 return $values[0] . " + G_UNICODE_MAX_TABLE_INDEX";
690 printf OUT ",\n" if ($table_index != 0);
691 printf OUT " { /* page %d, index %d */\n ", $start / 256, $table_index;
693 for ($i = $start; $i < $start + 256; ++$i)
697 my ($text) = $values[$i - $start];
698 if (length ($text) + $column + 2 > 78)
704 $column += length ($text) + 2;
708 $bytes_out += 256 * $typsize;
710 return sprintf "%d /* page %d */", $table_index++, $start / 256;
717 $string =~ s/(\C)/sprintf "\\x%02x",ord($1)/eg;
722 # Returns the offset of $decomp in the offset string. Updates the
723 # referenced variables as appropriate.
724 sub handle_decomp ($$$$)
726 my ($decomp, $decomp_offsets_ref, $decomp_string_ref, $decomp_string_offset_ref) = @_;
727 my $offset = "G_UNICODE_NOT_PRESENT_OFFSET";
731 if (defined $decomp_offsets_ref->{$decomp})
733 $offset = $decomp_offsets_ref->{$decomp};
737 $offset = ${$decomp_string_offset_ref};
738 $decomp_offsets_ref->{$decomp} = $offset;
739 ${$decomp_string_ref} .= "\n \"" . &escape ($decomp) . "\\0\" /* offset ${$decomp_string_offset_ref} */";
740 ${$decomp_string_offset_ref} += &length_in_bytes ($decomp) + 1;
747 # Generate the character decomposition header.
751 my ($outfile) = "gunidecomp.h";
753 local ($bytes_out) = 0;
755 print "Writing $outfile...\n";
757 open (OUT, "> $outfile") || exit 1;
759 print OUT "/* This file is automatically generated. DO NOT EDIT! */\n\n";
760 print OUT "#ifndef DECOMP_H\n";
761 print OUT "#define DECOMP_H\n\n";
763 printf OUT "#define G_UNICODE_LAST_CHAR 0x%04x\n\n", $last;
765 printf OUT "#define G_UNICODE_MAX_TABLE_INDEX (0x110000 / 256)\n\n";
767 my $last_part1 = ($pages_before_e0000 * 256) - 1;
768 printf OUT "#define G_UNICODE_LAST_CHAR_PART1 0x%04X\n\n", $last_part1;
769 printf OUT "#define G_UNICODE_LAST_PAGE_PART1 %d\n\n", $pages_before_e0000 - 1;
771 $NOT_PRESENT_OFFSET = 65535;
772 print OUT "#define G_UNICODE_NOT_PRESENT_OFFSET $NOT_PRESENT_OFFSET\n\n";
776 printf OUT "static const guchar cclass_data[][256] = {\n";
777 for ($count = 0; $count <= $last; $count += 256)
779 $row[$count / 256] = &print_row ($count, 1, \&fetch_cclass);
781 printf OUT "\n};\n\n";
783 print OUT "static const gint16 combining_class_table_part1[$pages_before_e0000] = {\n";
784 for ($count = 0; $count <= $last_part1; $count += 256)
786 print OUT ",\n" if $count > 0;
787 print OUT " ", $row[$count / 256];
790 print OUT "\n};\n\n";
792 print OUT "static const gint16 combining_class_table_part2[768] = {\n";
793 for ($count = 0xE0000; $count <= $last; $count += 256)
795 print OUT ",\n" if $count > 0xE0000;
796 print OUT " ", $row[$count / 256];
799 print OUT "\n};\n\n";
801 print OUT "typedef struct\n{\n";
802 print OUT " gunichar ch;\n";
803 print OUT " guint16 canon_offset;\n";
804 print OUT " guint16 compat_offset;\n";
805 print OUT "} decomposition;\n\n";
807 print OUT "static const decomposition decomp_table[] =\n{\n";
810 my ($decomp_string) = "";
811 my ($decomp_string_offset) = 0;
812 for ($count = 0; $count <= $last; ++$count)
814 if (defined $decompositions[$count])
823 if (!$decompose_compat[$count]) {
824 $canon_decomp = make_decomp ($count, 0);
826 $compat_decomp = make_decomp ($count, 1);
828 if (defined $canon_decomp && $compat_decomp eq $canon_decomp) {
829 undef $compat_decomp;
832 my $canon_offset = handle_decomp ($canon_decomp, \%decomp_offsets, \$decomp_string, \$decomp_string_offset);
833 my $compat_offset = handle_decomp ($compat_decomp, \%decomp_offsets, \$decomp_string, \$decomp_string_offset);
835 die if $decomp_string_offset > $NOT_PRESENT_OFFSET;
837 printf OUT qq( { 0x%04x, $canon_offset, $compat_offset }), $count;
841 print OUT "\n};\n\n";
842 $bytes_out += $decomp_string_offset + 1;
844 printf OUT "static const gchar decomp_expansion_string[] = %s;\n\n", $decomp_string;
846 print OUT "#endif /* DECOMP_H */\n";
848 printf STDERR "Generated %d bytes in decomp tables\n", $bytes_out;
854 my ($outfile) = "gunibreak.h";
856 local ($bytes_out) = 0;
858 print "Writing $outfile...\n";
860 open (OUT, "> $outfile");
862 print OUT "/* This file is automatically generated. DO NOT EDIT!\n";
863 print OUT " Instead, edit gen-unicode-tables.pl and re-run. */\n\n";
865 print OUT "#ifndef BREAKTABLES_H\n";
866 print OUT "#define BREAKTABLES_H\n\n";
868 print OUT "#define G_UNICODE_DATA_VERSION \"$ARGV[0]\"\n\n";
870 printf OUT "#define G_UNICODE_LAST_CHAR 0x%04X\n\n", $last;
872 printf OUT "#define G_UNICODE_MAX_TABLE_INDEX 10000\n\n";
874 my $last_part1 = ($pages_before_e0000 * 256) - 1;
875 printf OUT "/* the last code point that should be looked up in break_property_table_part1 */\n";
876 printf OUT "#define G_UNICODE_LAST_CHAR_PART1 0x%04X\n\n", $last_part1;
879 printf OUT "static const gint8 break_property_data[][256] = {\n";
880 for ($count = 0; $count <= $last; $count += 256)
882 $row[$count / 256] = &print_row ($count, 1, \&fetch_break_type);
884 printf OUT "\n};\n\n";
886 printf OUT "/* U+0000 through U+%04X */\n", $last_part1;
887 print OUT "static const gint16 break_property_table_part1[$pages_before_e0000] = {\n";
888 for ($count = 0; $count <= $last_part1; $count += 256)
890 print OUT ",\n" if $count > 0;
891 print OUT " ", $row[$count / 256];
894 print OUT "\n};\n\n";
896 printf OUT "/* U+E0000 through U+%04X */\n", $last;
897 print OUT "static const gint16 break_property_table_part2[768] = {\n";
898 for ($count = 0xE0000; $count <= $last; $count += 256)
900 print OUT ",\n" if $count > 0xE0000;
901 print OUT " ", $row[$count / 256];
904 print OUT "\n};\n\n";
907 print OUT "#endif /* BREAKTABLES_H */\n";
911 printf STDERR "Generated %d bytes in break tables\n", $bytes_out;
915 # A fetch function for the break properties table.
919 return $break_mappings{$break_props[$index]};
922 # Fetcher for combining class.
929 # Expand a character decomposition recursively.
932 my ($code, $compat) = @_;
936 foreach $iter (split (' ', $decompositions[$code]))
939 if (defined $decompositions[$val] &&
940 ($compat || !$decompose_compat[$val]))
942 push (@result, &expand_decomp ($val, $compat));
946 push (@result, $val);
955 my ($code, $compat) = @_;
958 foreach $iter (&expand_decomp ($code, $compat))
960 $result .= pack ("U", $iter); # to utf-8
965 # Generate special case data string from two fields
968 my ($code, $single, $field1, $field2) = @_;
970 @values = (defined $single ? $single : (),
971 (map { hex ($_) } split /\s+/, $field1),
973 (map { hex ($_) } split /\s+/, $field2));
977 for $value (@values) {
978 $result .= pack ("U", $value); # to utf-8
981 push @special_case_offsets, $special_case_offset;
983 # We encode special cases up in the 0x1000000 space
984 $value[$code] = 0x1000000 + $special_case_offset;
986 $special_case_offset += 1 + &length_in_bytes ($result);
988 push @special_cases, &escape ($result);
991 sub output_special_case_table
997 /* Table of special cases for case conversion; each record contains
998 * First, the best single character mapping to lowercase if Lu,
999 * and to uppercase if Ll, followed by the output mapping for the two cases
1000 * other than the case of the codepoint, in the order [Ll],[Lu],[Lt],
1001 * encoded in UTF-8, separated and terminated by a null character.
1003 static const gchar special_case_table[] = {
1007 for $case (@special_cases) {
1008 print $out qq( "$case\\0" /* offset ${special_case_offsets[$i]} */\n);
1017 print STDERR "Generated " . ($special_case_offset + 1) . " bytes in special case table\n";
1020 sub enumerate_ordered
1025 for my $code (sort { $a <=> $b } keys %$array) {
1026 if ($array->{$code} == 1) {
1027 delete $array->{$code};
1030 $array->{$code} = $n++;
1036 sub output_composition_table
1038 print STDERR "Generating composition table\n";
1040 local ($bytes_out) = 0;
1045 # First we need to go through and remove decompositions
1046 # starting with a non-starter, and single-character
1047 # decompositions. At the same time, record
1048 # the first and second character of each decomposition
1050 for $code (keys %compositions)
1052 @values = map { hex ($_) } split /\s+/, $compositions{$code};
1055 if ($cclass[$values[0]]) {
1056 delete $compositions{$code};
1060 # single-character decompositions
1062 delete $compositions{$code};
1067 die "$code has more than two elements in its decomposition!\n";
1070 if (exists $first{$values[0]}) {
1071 $first{$values[0]}++;
1073 $first{$values[0]} = 1;
1077 # Assign integer indices, removing singletons
1078 my $n_first = enumerate_ordered (\%first);
1080 # Now record the second character of each (non-singleton) decomposition
1081 for $code (keys %compositions) {
1082 @values = map { hex ($_) } split /\s+/, $compositions{$code};
1084 if (exists $first{$values[0]}) {
1085 if (exists $second{$values[1]}) {
1086 $second{$values[1]}++;
1088 $second{$values[1]} = 1;
1093 # Assign integer indices, removing duplicate
1094 my $n_second = enumerate_ordered (\%second);
1096 # Build reverse table
1098 my @first_singletons;
1099 my @second_singletons;
1101 for $code (keys %compositions) {
1102 @values = map { hex ($_) } split /\s+/, $compositions{$code};
1104 my $first = $first{$values[0]};
1105 my $second = $second{$values[1]};
1107 if (defined $first && defined $second) {
1108 $reverse{"$first|$second"} = $code;
1109 } elsif (!defined $first) {
1110 push @first_singletons, [ $values[0], $values[1], $code ];
1112 push @second_singletons, [ $values[1], $values[0], $code ];
1116 @first_singletons = sort { $a->[0] <=> $b->[0] } @first_singletons;
1117 @second_singletons = sort { $a->[0] <=> $b->[0] } @second_singletons;
1121 open OUT, ">gunicomp.h" or die "Cannot open gunicomp.h: $!\n";
1123 # Assign values in lookup table for all code points involved
1127 printf OUT "#define COMPOSE_FIRST_START %d\n", $total;
1128 for $code (keys %first) {
1129 $vals{$code} = $first{$code} + $total;
1130 $last = $code if $code > $last;
1134 printf OUT "#define COMPOSE_FIRST_SINGLE_START %d\n", $total;
1135 for $record (@first_singletons) {
1136 my $code = $record->[0];
1137 $vals{$code} = $i++ + $total;
1138 $last = $code if $code > $last;
1140 $total += @first_singletons;
1141 printf OUT "#define COMPOSE_SECOND_START %d\n", $total;
1142 for $code (keys %second) {
1143 $vals{$code} = $second{$code} + $total;
1144 $last = $code if $code > $last;
1146 $total += $n_second;
1148 printf OUT "#define COMPOSE_SECOND_SINGLE_START %d\n\n", $total;
1149 for $record (@second_singletons) {
1150 my $code = $record->[0];
1151 $vals{$code} = $i++ + $total;
1152 $last = $code if $code > $last;
1155 # Output lookup table
1159 printf OUT "static const guint16 compose_data[][256] = {\n";
1160 for (my $count = 0; $count <= $last; $count += 256)
1162 $row[$count / 256] = &print_row ($count, 2, sub { exists $vals{$_[0]} ? $vals{$_[0]} : 0; });
1164 printf OUT "\n};\n\n";
1166 print OUT "static const gint16 compose_table[256] = {\n";
1167 for (my $count = 0; $count <= $last; $count += 256)
1169 print OUT ",\n" if $count > 0;
1170 print OUT " ", $row[$count / 256];
1172 print OUT "\n};\n\n";
1174 $bytes_out += 256 * 2;
1176 # Output first singletons
1178 print OUT "static const guint16 compose_first_single[][2] = {\n";
1180 for $record (@first_singletons) {
1181 if ($record->[1] > 0xFFFF or $record->[2] > 0xFFFF) {
1182 die "time to switch compose_first_single to gunichar" ;
1184 print OUT ",\n" if $i++ > 0;
1185 printf OUT " { %#06x, %#06x }", $record->[1], $record->[2];
1189 $bytes_out += @first_singletons * 4;
1191 # Output second singletons
1193 print OUT "static const guint16 compose_second_single[][2] = {\n";
1195 for $record (@second_singletons) {
1196 if ($record->[1] > 0xFFFF or $record->[2] > 0xFFFF) {
1197 die "time to switch compose_second_single to gunichar";
1199 print OUT ",\n" if $i++ > 0;
1200 printf OUT " { %#06x, %#06x }", $record->[1], $record->[2];
1204 $bytes_out += @second_singletons * 4;
1206 # Output array of composition pairs
1209 static const guint16 compose_array[$n_first][$n_second] = {
1212 for (my $i = 0; $i < $n_first; $i++) {
1213 print OUT ",\n" if $i;
1215 for (my $j = 0; $j < $n_second; $j++) {
1216 print OUT ", " if $j;
1217 if (exists $reverse{"$i|$j"}) {
1218 if ($reverse{"$i|$j"} > 0xFFFF) {
1219 die "time to switch compose_array to gunichar" ;
1221 printf OUT "0x%04x", $reverse{"$i|$j"};
1234 $bytes_out += $n_first * $n_second * 2;
1236 printf STDERR "Generated %d bytes in compose tables\n", $bytes_out;
1239 sub output_casefold_table
1245 /* Table of casefolding cases that can't be derived by lowercasing
1247 static const struct {
1249 gchar data[$casefoldlen];
1250 } casefold_table[] = {
1253 @casefold = sort { $a->[0] <=> $b->[0] } @casefold;
1255 for $case (@casefold)
1258 $string = $case->[1];
1260 if ($code > 0xFFFF) {
1261 die "time to switch casefold_table to gunichar" ;
1264 print $out sprintf(qq( { 0x%04x, "$string" },\n), $code);
1273 my $recordlen = (2+$casefoldlen+1) & ~1;
1274 printf "Generated %d bytes for casefold table\n", $recordlen * @casefold;