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
38 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);
41 # Names of fields in Unicode data table.
45 $COMBINING_CLASSES = 3;
58 # Names of fields in the line break table
62 # Names of fields in the SpecialCasing table
69 # Names of fields in the CaseFolding table
74 # Map general category code onto symbolic name.
78 'Lu' => "G_UNICODE_UPPERCASE_LETTER",
79 'Ll' => "G_UNICODE_LOWERCASE_LETTER",
80 'Lt' => "G_UNICODE_TITLECASE_LETTER",
81 'Mn' => "G_UNICODE_NON_SPACING_MARK",
82 'Mc' => "G_UNICODE_SPACING_MARK",
83 'Me' => "G_UNICODE_ENCLOSING_MARK",
84 'Nd' => "G_UNICODE_DECIMAL_NUMBER",
85 'Nl' => "G_UNICODE_LETTER_NUMBER",
86 'No' => "G_UNICODE_OTHER_NUMBER",
87 'Zs' => "G_UNICODE_SPACE_SEPARATOR",
88 'Zl' => "G_UNICODE_LINE_SEPARATOR",
89 'Zp' => "G_UNICODE_PARAGRAPH_SEPARATOR",
90 'Cc' => "G_UNICODE_CONTROL",
91 'Cf' => "G_UNICODE_FORMAT",
92 'Cs' => "G_UNICODE_SURROGATE",
93 'Co' => "G_UNICODE_PRIVATE_USE",
94 'Cn' => "G_UNICODE_UNASSIGNED",
97 'Lm' => "G_UNICODE_MODIFIER_LETTER",
98 'Lo' => "G_UNICODE_OTHER_LETTER",
99 'Pc' => "G_UNICODE_CONNECT_PUNCTUATION",
100 'Pd' => "G_UNICODE_DASH_PUNCTUATION",
101 'Ps' => "G_UNICODE_OPEN_PUNCTUATION",
102 'Pe' => "G_UNICODE_CLOSE_PUNCTUATION",
103 'Pi' => "G_UNICODE_INITIAL_PUNCTUATION",
104 'Pf' => "G_UNICODE_FINAL_PUNCTUATION",
105 'Po' => "G_UNICODE_OTHER_PUNCTUATION",
106 'Sm' => "G_UNICODE_MATH_SYMBOL",
107 'Sc' => "G_UNICODE_CURRENCY_SYMBOL",
108 'Sk' => "G_UNICODE_MODIFIER_SYMBOL",
109 'So' => "G_UNICODE_OTHER_SYMBOL"
114 'AI' => "G_UNICODE_BREAK_AMBIGUOUS",
115 'AL' => "G_UNICODE_BREAK_ALPHABETIC",
116 'B2' => "G_UNICODE_BREAK_BEFORE_AND_AFTER",
117 'BA' => "G_UNICODE_BREAK_AFTER",
118 'BB' => "G_UNICODE_BREAK_BEFORE",
119 'BK' => "G_UNICODE_BREAK_MANDATORY",
120 'CB' => "G_UNICODE_BREAK_CONTINGENT",
121 'CJ' => "G_UNICODE_BREAK_CONDITIONAL_JAPANESE_STARTER",
122 'CL' => "G_UNICODE_BREAK_CLOSE_PUNCTUATION",
123 'CM' => "G_UNICODE_BREAK_COMBINING_MARK",
124 'CP' => "G_UNICODE_BREAK_CLOSE_PARANTHESIS",
125 'CR' => "G_UNICODE_BREAK_CARRIAGE_RETURN",
126 'EX' => "G_UNICODE_BREAK_EXCLAMATION",
127 'GL' => "G_UNICODE_BREAK_NON_BREAKING_GLUE",
128 'H2' => "G_UNICODE_BREAK_HANGUL_LV_SYLLABLE",
129 'H3' => "G_UNICODE_BREAK_HANGUL_LVT_SYLLABLE",
130 'HL' => "G_UNICODE_BREAK_HEBREW_LETTER",
131 'HY' => "G_UNICODE_BREAK_HYPHEN",
132 'ID' => "G_UNICODE_BREAK_IDEOGRAPHIC",
133 'IN' => "G_UNICODE_BREAK_INSEPARABLE",
134 'IS' => "G_UNICODE_BREAK_INFIX_SEPARATOR",
135 'JL' => "G_UNICODE_BREAK_HANGUL_L_JAMO",
136 'JT' => "G_UNICODE_BREAK_HANGUL_T_JAMO",
137 'JV' => "G_UNICODE_BREAK_HANGUL_V_JAMO",
138 'LF' => "G_UNICODE_BREAK_LINE_FEED",
139 'NL' => "G_UNICODE_BREAK_NEXT_LINE",
140 'NS' => "G_UNICODE_BREAK_NON_STARTER",
141 'NU' => "G_UNICODE_BREAK_NUMERIC",
142 'OP' => "G_UNICODE_BREAK_OPEN_PUNCTUATION",
143 'PO' => "G_UNICODE_BREAK_POSTFIX",
144 'PR' => "G_UNICODE_BREAK_PREFIX",
145 'QU' => "G_UNICODE_BREAK_QUOTATION",
146 'RI' => "G_UNICODE_BREAK_REGIONAL_INDICATOR",
147 'SA' => "G_UNICODE_BREAK_COMPLEX_CONTEXT",
148 'SG' => "G_UNICODE_BREAK_SURROGATE",
149 'SP' => "G_UNICODE_BREAK_SPACE",
150 'SY' => "G_UNICODE_BREAK_SYMBOL",
151 'WJ' => "G_UNICODE_BREAK_WORD_JOINER",
152 'XX' => "G_UNICODE_BREAK_UNKNOWN",
153 'ZW' => "G_UNICODE_BREAK_ZERO_WIDTH_SPACE"
156 # Title case mappings.
157 %title_to_lower = ();
158 %title_to_upper = ();
160 # Maximum length of special-case strings
163 my @special_case_offsets;
164 my $special_case_offset = 0;
168 if (@ARGV && $ARGV[0] eq '-decomp')
174 elsif (@ARGV && $ARGV[0] eq '-both')
182 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";
185 my ($unicodedatatxt, $linebreaktxt, $specialcasingtxt, $casefoldingtxt, $compositionexclusionstxt);
188 opendir (my $dir, $d) or die "Cannot open Unicode data dir $d: $!\n";
189 for my $f (readdir ($dir))
191 $unicodedatatxt = "$d/$f" if ($f =~ /^UnicodeData.*\.txt/);
192 $linebreaktxt = "$d/$f" if ($f =~ /^LineBreak.*\.txt/);
193 $specialcasingtxt = "$d/$f" if ($f =~ /^SpecialCasing.*\.txt/);
194 $casefoldingtxt = "$d/$f" if ($f =~ /^CaseFolding.*\.txt/);
195 $compositionexclusionstxt = "$d/$f" if ($f =~ /^CompositionExclusions.*\.txt/);
198 defined $unicodedatatxt or die "Did not find UnicodeData file";
199 defined $linebreaktxt or die "Did not find LineBreak file";
200 defined $specialcasingtxt or die "Did not find SpecialCasing file";
201 defined $casefoldingtxt or die "Did not find CaseFolding file";
202 defined $compositionexclusionstxt or die "Did not find CompositionExclusions file";
204 print "Creating decomp table\n" if ($do_decomp);
205 print "Creating property table\n" if ($do_props);
207 print "Composition exlusions from $compositionexclusionstxt\n";
209 open (INPUT, "< $compositionexclusionstxt") || exit 1;
223 $composition_exclusions{hex($_)} = 1;
228 print "Unicode data from $unicodedatatxt\n";
230 open (INPUT, "< $unicodedatatxt") || exit 1;
232 # we save memory by skipping the huge empty area before U+E0000
233 my $pages_before_e0000;
239 @fields = split (';', $_, 30);
242 printf STDERR ("Entry for $fields[$CODE] has wrong number of fields (%d)\n", $#fields);
245 $code = hex ($fields[$CODE]);
247 if ($code >= 0xE0000 and $last_code < 0xE0000)
249 $pages_before_e0000 = ($last_code >> 8) + 1;
252 if ($code > $last_code + 1)
255 if ($fields[$NAME] =~ /Last>/)
257 # Fill the gap with the last character read,
258 # since this was a range specified in the char database
263 # The gap represents undefined characters. Only the type
265 @gfields = ('', '', 'Cn', '0', '', '', '', '', '', '', '',
268 for (++$last_code; $last_code < $code; ++$last_code)
270 $gfields{$CODE} = sprintf ("%04x", $last_code);
271 &process_one ($last_code, @gfields);
274 &process_one ($code, @fields);
280 @gfields = ('', '', 'Cn', '0', '', '', '', '', '', '', '',
282 for (++$last_code; $last_code <= 0x10FFFF; ++$last_code)
284 $gfields{$CODE} = sprintf ("%04x", $last_code);
285 &process_one ($last_code, @gfields);
287 --$last_code; # Want last to be 0x10FFFF.
289 print "Creating line break table\n";
291 print "Line break data from $linebreaktxt\n";
293 open (INPUT, "< $linebreaktxt") || exit 1;
298 my ($start_code, $end_code);
307 @fields = split (';', $_, 30);
310 printf STDERR ("Entry for $fields[$CODE] has wrong number of fields (%d)\n", $#fields);
314 if ($fields[$CODE] =~ /([A-F0-9]{4,6})\.\.([A-F0-9]{4,6})/)
316 $start_code = hex ($1);
317 $end_code = hex ($2);
319 $start_code = $end_code = hex ($fields[$CODE]);
323 if ($start_code > $last_code + 1)
325 # The gap represents undefined characters. If assigned,
326 # they are AL, if not assigned, XX
327 for (++$last_code; $last_code < $start_code; ++$last_code)
329 if ($type[$last_code] eq 'Cn')
331 $break_props[$last_code] = 'XX';
335 $break_props[$last_code] = 'AL';
340 for ($last_code = $start_code; $last_code <= $end_code; $last_code++)
342 $break_props[$last_code] = $fields[$BREAK_PROPERTY];
345 $last_code = $end_code;
350 for (++$last_code; $last_code <= 0x10FFFF; ++$last_code)
352 if ($type[$last_code] eq 'Cn')
354 $break_props[$last_code] = 'XX';
358 $break_props[$last_code] = 'AL';
361 --$last_code; # Want last to be 0x10FFFF.
363 print STDERR "Last code is not 0x10FFFF" if ($last_code != 0x10FFFF);
365 print "Reading special-casing table for case conversion\n";
367 open (INPUT, "< $specialcasingtxt") || exit 1;
380 @fields = split ('\s*;\s*', $_, 30);
382 $raw_code = $fields[$CASE_CODE];
383 $code = hex ($raw_code);
385 if ($#fields != 4 && $#fields != 5)
387 printf STDERR ("Entry for $raw_code has wrong number of fields (%d)\n", $#fields);
391 if (!defined $type[$code])
393 printf STDERR "Special case for code point: $code, which has no defined type\n";
397 if (defined $fields[5]) {
398 # Ignore conditional special cases - we'll handle them in code
402 if ($type[$code] eq 'Lu')
404 (hex $fields[$CASE_UPPER] == $code) || die "$raw_code is Lu and UCD_Upper($raw_code) != $raw_code";
406 &add_special_case ($code, $value[$code], $fields[$CASE_LOWER], $fields[$CASE_TITLE]);
408 } elsif ($type[$code] eq 'Lt')
410 (hex $fields[$CASE_TITLE] == $code) || die "$raw_code is Lt and UCD_Title($raw_code) != $raw_code";
412 &add_special_case ($code, undef, $fields[$CASE_LOWER], $fields[$CASE_UPPER]);
413 } elsif ($type[$code] eq 'Ll')
415 (hex $fields[$CASE_LOWER] == $code) || die "$raw_code is Ll and UCD_Lower($raw_code) != $raw_code";
417 &add_special_case ($code, $value[$code], $fields[$CASE_UPPER], $fields[$CASE_TITLE]);
419 printf STDERR "Special case for non-alphabetic code point: $raw_code\n";
426 open (INPUT, "< $casefoldingtxt") || exit 1;
442 @fields = split ('\s*;\s*', $_, 30);
444 $raw_code = $fields[$FOLDING_CODE];
445 $code = hex ($raw_code);
449 printf STDERR ("Entry for $raw_code has wrong number of fields (%d)\n", $#fields);
453 # we don't use Simple or Turkic rules here
454 next if ($fields[$FOLDING_STATUS] =~ /^[ST]$/);
456 @values = map { hex ($_) } split /\s+/, $fields[$FOLDING_MAPPING];
461 !(defined $value[$code] && $value[$code] >= 0x1000000) &&
462 defined $type[$code]) {
465 if ($type[$code] eq 'Ll')
468 } elsif ($type[$code] eq 'Lt')
470 $lower = $title_to_lower{$code};
471 } elsif ($type[$code] eq 'Lu')
473 $lower = $value[$code];
478 if ($lower == $values[0]) {
483 my $string = pack ("U*", @values);
485 if (1 + &length_in_bytes ($string) > $casefoldlen) {
486 $casefoldlen = 1 + &length_in_bytes ($string);
489 push @casefold, [ $code, &escape ($string) ];
495 &print_tables ($last_code)
498 &print_decomp ($last_code);
499 &output_composition_table;
502 &print_line_break ($last_code);
507 # perl "length" returns the length in characters
512 return length $string;
515 # Process a single character.
518 my ($code, @fields) = @_;
520 $type[$code] = $fields[$CATEGORY];
521 if ($type[$code] eq 'Nd')
523 $value[$code] = int ($fields[$DECIMAL_VALUE]);
525 elsif ($type[$code] eq 'Ll')
527 $value[$code] = hex ($fields[$UPPER]);
529 elsif ($type[$code] eq 'Lu')
531 $value[$code] = hex ($fields[$LOWER]);
534 if ($type[$code] eq 'Lt')
536 $title_to_lower{$code} = hex ($fields[$LOWER]);
537 $title_to_upper{$code} = hex ($fields[$UPPER]);
540 $cclass[$code] = $fields[$COMBINING_CLASSES];
542 # Handle decompositions.
543 if ($fields[$DECOMPOSITION] ne '')
545 if ($fields[$DECOMPOSITION] =~ s/\<.*\>\s*//) {
546 $decompose_compat[$code] = 1;
548 $decompose_compat[$code] = 0;
550 if (!exists $composition_exclusions{$code}) {
551 $compositions{$code} = $fields[$DECOMPOSITION];
554 $decompositions[$code] = $fields[$DECOMPOSITION];
561 my ($outfile) = "gunichartables.h";
563 local ($bytes_out) = 0;
565 print "Writing $outfile...\n";
567 open (OUT, "> $outfile");
569 print OUT "/* This file is automatically generated. DO NOT EDIT!\n";
570 print OUT " Instead, edit gen-unicode-tables.pl and re-run. */\n\n";
572 print OUT "#ifndef CHARTABLES_H\n";
573 print OUT "#define CHARTABLES_H\n\n";
575 print OUT "#define G_UNICODE_DATA_VERSION \"$ARGV[0]\"\n\n";
577 printf OUT "#define G_UNICODE_LAST_CHAR 0x%04x\n\n", $last;
579 printf OUT "#define G_UNICODE_MAX_TABLE_INDEX 10000\n\n";
581 my $last_part1 = ($pages_before_e0000 * 256) - 1;
582 printf OUT "#define G_UNICODE_LAST_CHAR_PART1 0x%04X\n\n", $last_part1;
583 printf OUT "#define G_UNICODE_LAST_PAGE_PART1 %d\n\n", $pages_before_e0000 - 1;
586 printf OUT "static const char type_data[][256] = {\n";
587 for ($count = 0; $count <= $last; $count += 256)
589 $row[$count / 256] = &print_row ($count, 1, \&fetch_type);
591 printf OUT "\n};\n\n";
593 printf OUT "/* U+0000 through U+%04X */\n", $last_part1;
594 print OUT "static const gint16 type_table_part1[$pages_before_e0000] = {\n";
595 for ($count = 0; $count <= $last_part1; $count += 256)
597 print OUT ",\n" if $count > 0;
598 print OUT " ", $row[$count / 256];
601 print OUT "\n};\n\n";
603 printf OUT "/* U+E0000 through U+%04X */\n", $last;
604 print OUT "static const gint16 type_table_part2[768] = {\n";
605 for ($count = 0xE0000; $count <= $last; $count += 256)
607 print OUT ",\n" if $count > 0xE0000;
608 print OUT " ", $row[$count / 256];
611 print OUT "\n};\n\n";
615 # Now print attribute table.
619 printf OUT "static const gunichar attr_data[][256] = {\n";
620 for ($count = 0; $count <= $last; $count += 256)
622 $row[$count / 256] = &print_row ($count, 4, \&fetch_attr);
624 printf OUT "\n};\n\n";
626 printf OUT "/* U+0000 through U+%04X */\n", $last_part1;
627 print OUT "static const gint16 attr_table_part1[$pages_before_e0000] = {\n";
628 for ($count = 0; $count <= $last_part1; $count += 256)
630 print OUT ",\n" if $count > 0;
631 print OUT " ", $row[$count / 256];
634 print OUT "\n};\n\n";
636 printf OUT "/* U+E0000 through U+%04X */\n", $last;
637 print OUT "static const gint16 attr_table_part2[768] = {\n";
638 for ($count = 0xE0000; $count <= $last; $count += 256)
640 print OUT ",\n" if $count > 0xE0000;
641 print OUT " ", $row[$count / 256];
644 print OUT "\n};\n\n";
647 # print title case table
650 print OUT "static const gunichar title_table[][3] = {\n";
653 foreach $item (sort keys %title_to_lower)
658 printf OUT " { 0x%04x, 0x%04x, 0x%04x }", $item, $title_to_upper{$item}, $title_to_lower{$item};
661 print OUT "\n};\n\n";
664 # And special case conversion table -- conversions that change length
666 &output_special_case_table (\*OUT);
667 &output_casefold_table (\*OUT);
669 print OUT "#endif /* CHARTABLES_H */\n";
673 printf STDERR "Generated %d bytes in tables\n", $bytes_out;
676 # A fetch function for the type table.
680 return $mappings{$type[$index]};
683 # A fetch function for the attribute table.
687 if (defined $value[$index])
689 return sprintf ("0x%04x", $value[$index]);
699 my ($start, $typsize, $fetcher) = @_;
706 for ($off = 0; $off < 256; ++$off)
708 $values[$off] = $fetcher->($off + $start);
709 if ($values[$off] ne $values[0])
716 return $values[0] . " + G_UNICODE_MAX_TABLE_INDEX";
719 printf OUT ",\n" if ($table_index != 0);
720 printf OUT " { /* page %d, index %d */\n ", $start / 256, $table_index;
722 for ($i = $start; $i < $start + 256; ++$i)
726 my ($text) = $values[$i - $start];
727 if (length ($text) + $column + 2 > 78)
733 $column += length ($text) + 2;
737 $bytes_out += 256 * $typsize;
739 return sprintf "%d /* page %d */", $table_index++, $start / 256;
746 my $escaped = unpack("H*", $string);
747 $escaped =~ s/(.{2})/\\x$1/g;
752 # Returns the offset of $decomp in the offset string. Updates the
753 # referenced variables as appropriate.
754 sub handle_decomp ($$$$)
756 my ($decomp, $decomp_offsets_ref, $decomp_string_ref, $decomp_string_offset_ref) = @_;
757 my $offset = "G_UNICODE_NOT_PRESENT_OFFSET";
761 if (defined $decomp_offsets_ref->{$decomp})
763 $offset = $decomp_offsets_ref->{$decomp};
767 $offset = ${$decomp_string_offset_ref};
768 $decomp_offsets_ref->{$decomp} = $offset;
769 ${$decomp_string_ref} .= "\n \"" . &escape ($decomp) . "\\0\" /* offset ${$decomp_string_offset_ref} */";
770 ${$decomp_string_offset_ref} += &length_in_bytes ($decomp) + 1;
777 # Generate the character decomposition header.
781 my ($outfile) = "gunidecomp.h";
783 local ($bytes_out) = 0;
785 print "Writing $outfile...\n";
787 open (OUT, "> $outfile") || exit 1;
789 print OUT "/* This file is automatically generated. DO NOT EDIT! */\n\n";
790 print OUT "#ifndef DECOMP_H\n";
791 print OUT "#define DECOMP_H\n\n";
793 printf OUT "#define G_UNICODE_LAST_CHAR 0x%04x\n\n", $last;
795 printf OUT "#define G_UNICODE_MAX_TABLE_INDEX (0x110000 / 256)\n\n";
797 my $last_part1 = ($pages_before_e0000 * 256) - 1;
798 printf OUT "#define G_UNICODE_LAST_CHAR_PART1 0x%04X\n\n", $last_part1;
799 printf OUT "#define G_UNICODE_LAST_PAGE_PART1 %d\n\n", $pages_before_e0000 - 1;
801 $NOT_PRESENT_OFFSET = 65535;
802 print OUT "#define G_UNICODE_NOT_PRESENT_OFFSET $NOT_PRESENT_OFFSET\n\n";
806 printf OUT "static const guchar cclass_data[][256] = {\n";
807 for ($count = 0; $count <= $last; $count += 256)
809 $row[$count / 256] = &print_row ($count, 1, \&fetch_cclass);
811 printf OUT "\n};\n\n";
813 print OUT "static const gint16 combining_class_table_part1[$pages_before_e0000] = {\n";
814 for ($count = 0; $count <= $last_part1; $count += 256)
816 print OUT ",\n" if $count > 0;
817 print OUT " ", $row[$count / 256];
820 print OUT "\n};\n\n";
822 print OUT "static const gint16 combining_class_table_part2[768] = {\n";
823 for ($count = 0xE0000; $count <= $last; $count += 256)
825 print OUT ",\n" if $count > 0xE0000;
826 print OUT " ", $row[$count / 256];
829 print OUT "\n};\n\n";
831 print OUT "typedef struct\n{\n";
832 print OUT " gunichar ch;\n";
833 print OUT " guint16 canon_offset;\n";
834 print OUT " guint16 compat_offset;\n";
835 print OUT "} decomposition;\n\n";
837 print OUT "static const decomposition decomp_table[] =\n{\n";
840 my ($decomp_string) = "";
841 my ($decomp_string_offset) = 0;
842 for ($count = 0; $count <= $last; ++$count)
844 if (defined $decompositions[$count])
853 if (!$decompose_compat[$count]) {
854 $canon_decomp = make_decomp ($count, 0);
856 $compat_decomp = make_decomp ($count, 1);
858 if (defined $canon_decomp && $compat_decomp eq $canon_decomp) {
859 undef $compat_decomp;
862 my $canon_offset = handle_decomp ($canon_decomp, \%decomp_offsets, \$decomp_string, \$decomp_string_offset);
863 my $compat_offset = handle_decomp ($compat_decomp, \%decomp_offsets, \$decomp_string, \$decomp_string_offset);
865 die if $decomp_string_offset > $NOT_PRESENT_OFFSET;
867 printf OUT qq( { 0x%04x, $canon_offset, $compat_offset }), $count;
871 print OUT "\n};\n\n";
872 $bytes_out += $decomp_string_offset + 1;
874 printf OUT "static const gchar decomp_expansion_string[] = %s;\n\n", $decomp_string;
876 print OUT "typedef struct\n{\n";
877 print OUT " gunichar ch;\n";
878 print OUT " gunichar a;\n";
879 print OUT " gunichar b;\n";
880 print OUT "} decomposition_step;\n\n";
882 # There's lots of room to optimize the following table...
883 print OUT "static const decomposition_step decomp_step_table[] =\n{\n";
886 for ($count = 0; $count <= $last; ++$count)
888 if ((defined $decompositions[$count]) && (!$decompose_compat[$count]))
894 @list = (split(' ', $decompositions[$count]), "0");
895 printf OUT qq( { 0x%05x, 0x%05x, 0x%05x }), $count, hex($list[0]), hex($list[1]);
896 # don't include 1:1 in the compose table
897 push @steps, [ ($count, hex($list[0]), hex($list[1])) ]
901 print OUT "\n};\n\n";
903 print OUT "#endif /* DECOMP_H */\n";
905 printf STDERR "Generated %d bytes in decomp tables\n", $bytes_out;
911 my ($outfile) = "gunibreak.h";
913 local ($bytes_out) = 0;
915 print "Writing $outfile...\n";
917 open (OUT, "> $outfile");
919 print OUT "/* This file is automatically generated. DO NOT EDIT!\n";
920 print OUT " Instead, edit gen-unicode-tables.pl and re-run. */\n\n";
922 print OUT "#ifndef BREAKTABLES_H\n";
923 print OUT "#define BREAKTABLES_H\n\n";
925 print OUT "#include <glib/gtypes.h>\n";
926 print OUT "#include <glib/gunicode.h>\n\n";
928 print OUT "#define G_UNICODE_DATA_VERSION \"$ARGV[0]\"\n\n";
930 printf OUT "#define G_UNICODE_LAST_CHAR 0x%04X\n\n", $last;
932 printf OUT "#define G_UNICODE_MAX_TABLE_INDEX 10000\n\n";
934 my $last_part1 = ($pages_before_e0000 * 256) - 1;
935 printf OUT "/* the last code point that should be looked up in break_property_table_part1 */\n";
936 printf OUT "#define G_UNICODE_LAST_CHAR_PART1 0x%04X\n\n", $last_part1;
939 printf OUT "static const gint8 break_property_data[][256] = {\n";
940 for ($count = 0; $count <= $last; $count += 256)
942 $row[$count / 256] = &print_row ($count, 1, \&fetch_break_type);
944 printf OUT "\n};\n\n";
946 printf OUT "/* U+0000 through U+%04X */\n", $last_part1;
947 print OUT "static const gint16 break_property_table_part1[$pages_before_e0000] = {\n";
948 for ($count = 0; $count <= $last_part1; $count += 256)
950 print OUT ",\n" if $count > 0;
951 print OUT " ", $row[$count / 256];
954 print OUT "\n};\n\n";
956 printf OUT "/* U+E0000 through U+%04X */\n", $last;
957 print OUT "static const gint16 break_property_table_part2[768] = {\n";
958 for ($count = 0xE0000; $count <= $last; $count += 256)
960 print OUT ",\n" if $count > 0xE0000;
961 print OUT " ", $row[$count / 256];
964 print OUT "\n};\n\n";
967 print OUT "#endif /* BREAKTABLES_H */\n";
971 printf STDERR "Generated %d bytes in break tables\n", $bytes_out;
975 # A fetch function for the break properties table.
979 return $break_mappings{$break_props[$index]};
982 # Fetcher for combining class.
989 # Expand a character decomposition recursively.
992 my ($code, $compat) = @_;
996 foreach $iter (split (' ', $decompositions[$code]))
999 if (defined $decompositions[$val] &&
1000 ($compat || !$decompose_compat[$val]))
1002 push (@result, &expand_decomp ($val, $compat));
1006 push (@result, $val);
1015 my ($code, $compat) = @_;
1018 foreach $iter (&expand_decomp ($code, $compat))
1020 $result .= pack ("U", $iter); # to utf-8
1025 # Generate special case data string from two fields
1026 sub add_special_case
1028 my ($code, $single, $field1, $field2) = @_;
1030 @values = (defined $single ? $single : (),
1031 (map { hex ($_) } split /\s+/, $field1),
1033 (map { hex ($_) } split /\s+/, $field2));
1037 for $value (@values) {
1038 $result .= pack ("U", $value); # to utf-8
1041 push @special_case_offsets, $special_case_offset;
1043 # We encode special cases up in the 0x1000000 space
1044 $value[$code] = 0x1000000 + $special_case_offset;
1046 $special_case_offset += 1 + &length_in_bytes ($result);
1048 push @special_cases, &escape ($result);
1051 sub output_special_case_table
1057 /* Table of special cases for case conversion; each record contains
1058 * First, the best single character mapping to lowercase if Lu,
1059 * and to uppercase if Ll, followed by the output mapping for the two cases
1060 * other than the case of the codepoint, in the order [Ll],[Lu],[Lt],
1061 * encoded in UTF-8, separated and terminated by a null character.
1063 static const gchar special_case_table[] = {
1067 for $case (@special_cases) {
1068 print $out qq( "$case\\0" /* offset ${special_case_offsets[$i]} */\n);
1077 print STDERR "Generated " . ($special_case_offset + 1) . " bytes in special case table\n";
1080 sub enumerate_ordered
1085 for my $code (sort { $a <=> $b } keys %$array) {
1086 if ($array->{$code} == 1) {
1087 delete $array->{$code};
1090 $array->{$code} = $n++;
1096 sub output_composition_table
1098 print STDERR "Generating composition table\n";
1100 local ($bytes_out) = 0;
1105 # First we need to go through and remove decompositions
1106 # starting with a non-starter, and single-character
1107 # decompositions. At the same time, record
1108 # the first and second character of each decomposition
1110 for $code (keys %compositions)
1112 @values = map { hex ($_) } split /\s+/, $compositions{$code};
1115 if ($cclass[$code]) {
1116 delete $compositions{$code};
1119 if ($cclass[$values[0]]) {
1120 delete $compositions{$code};
1124 # single-character decompositions
1126 delete $compositions{$code};
1131 die "$code has more than two elements in its decomposition!\n";
1134 if (exists $first{$values[0]}) {
1135 $first{$values[0]}++;
1137 $first{$values[0]} = 1;
1141 # Assign integer indices, removing singletons
1142 my $n_first = enumerate_ordered (\%first);
1144 # Now record the second character of each (non-singleton) decomposition
1145 for $code (keys %compositions) {
1146 @values = map { hex ($_) } split /\s+/, $compositions{$code};
1148 if (exists $first{$values[0]}) {
1149 if (exists $second{$values[1]}) {
1150 $second{$values[1]}++;
1152 $second{$values[1]} = 1;
1157 # Assign integer indices, removing duplicate
1158 my $n_second = enumerate_ordered (\%second);
1160 # Build reverse table
1162 my @first_singletons;
1163 my @second_singletons;
1165 for $code (keys %compositions) {
1166 @values = map { hex ($_) } split /\s+/, $compositions{$code};
1168 my $first = $first{$values[0]};
1169 my $second = $second{$values[1]};
1171 if (defined $first && defined $second) {
1172 $reverse{"$first|$second"} = $code;
1173 } elsif (!defined $first) {
1174 push @first_singletons, [ $values[0], $values[1], $code ];
1176 push @second_singletons, [ $values[1], $values[0], $code ];
1180 @first_singletons = sort { $a->[0] <=> $b->[0] } @first_singletons;
1181 @second_singletons = sort { $a->[0] <=> $b->[0] } @second_singletons;
1185 open OUT, ">gunicomp.h" or die "Cannot open gunicomp.h: $!\n";
1187 # Assign values in lookup table for all code points involved
1191 printf OUT "#define COMPOSE_FIRST_START %d\n", $total;
1192 for $code (keys %first) {
1193 $vals{$code} = $first{$code} + $total;
1194 $last = $code if $code > $last;
1198 printf OUT "#define COMPOSE_FIRST_SINGLE_START %d\n", $total;
1199 for $record (@first_singletons) {
1200 my $code = $record->[0];
1201 $vals{$code} = $i++ + $total;
1202 $last = $code if $code > $last;
1204 $total += @first_singletons;
1205 printf OUT "#define COMPOSE_SECOND_START %d\n", $total;
1206 for $code (keys %second) {
1207 $vals{$code} = $second{$code} + $total;
1208 $last = $code if $code > $last;
1210 $total += $n_second;
1212 printf OUT "#define COMPOSE_SECOND_SINGLE_START %d\n\n", $total;
1213 for $record (@second_singletons) {
1214 my $code = $record->[0];
1215 $vals{$code} = $i++ + $total;
1216 $last = $code if $code > $last;
1219 printf OUT "#define COMPOSE_TABLE_LAST %d\n\n", $last / 256;
1221 # Output lookup table
1225 printf OUT "static const guint16 compose_data[][256] = {\n";
1226 for (my $count = 0; $count <= $last; $count += 256)
1228 $row[$count / 256] = &print_row ($count, 2, sub { exists $vals{$_[0]} ? $vals{$_[0]} : 0; });
1230 printf OUT "\n};\n\n";
1232 print OUT "static const gint16 compose_table[COMPOSE_TABLE_LAST + 1] = {\n";
1233 for (my $count = 0; $count <= $last; $count += 256)
1235 print OUT ",\n" if $count > 0;
1236 print OUT " ", $row[$count / 256];
1239 print OUT "\n};\n\n";
1241 # Output first singletons
1243 print OUT "static const gunichar compose_first_single[][2] = {\n";
1245 for $record (@first_singletons) {
1246 print OUT ",\n" if $i++ > 0;
1247 printf OUT " { %#06x, %#06x }", $record->[1], $record->[2];
1251 $bytes_out += @first_singletons * 4;
1253 # Output second singletons
1255 print OUT "static const guint16 compose_second_single[][2] = {\n";
1257 for $record (@second_singletons) {
1258 if ($record->[1] > 0xFFFF or $record->[2] > 0xFFFF) {
1259 die "time to switch compose_second_single to gunichar";
1261 print OUT ",\n" if $i++ > 0;
1262 printf OUT " { %#06x, %#06x }", $record->[1], $record->[2];
1266 $bytes_out += @second_singletons * 4;
1268 # Output array of composition pairs
1271 static const guint16 compose_array[$n_first][$n_second] = {
1274 for (my $i = 0; $i < $n_first; $i++) {
1275 print OUT ",\n" if $i;
1277 for (my $j = 0; $j < $n_second; $j++) {
1278 print OUT ", " if $j;
1279 if (exists $reverse{"$i|$j"}) {
1280 if ($reverse{"$i|$j"} > 0xFFFF) {
1281 die "time to switch compose_array to gunichar" ;
1283 printf OUT "0x%04x", $reverse{"$i|$j"};
1296 $bytes_out += $n_first * $n_second * 2;
1298 printf STDERR "Generated %d bytes in compose tables\n", $bytes_out;
1301 sub output_casefold_table
1307 /* Table of casefolding cases that can't be derived by lowercasing
1309 static const struct {
1311 gchar data[$casefoldlen];
1312 } casefold_table[] = {
1315 @casefold = sort { $a->[0] <=> $b->[0] } @casefold;
1317 for $case (@casefold)
1320 $string = $case->[1];
1322 if ($code > 0xFFFF) {
1323 die "time to switch casefold_table to gunichar" ;
1326 print $out sprintf(qq( { 0x%04x, "$string" },\n), $code);
1335 my $recordlen = (2+$casefoldlen+1) & ~1;
1336 printf "Generated %d bytes for casefold table\n", $recordlen * @casefold;