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;
175 if (@ARGV && $ARGV[0] eq '-decomp')
181 elsif (@ARGV && $ARGV[0] eq '-both')
189 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 Scripts.txt extracted/DerivedEastAsianWidth.txt \n\n";
192 my ($unicodedatatxt, $linebreaktxt, $specialcasingtxt, $casefoldingtxt, $compositionexclusionstxt,
193 $scriptstxt, $derivedeastasianwidth);
196 opendir (my $dir, $d) or die "Cannot open Unicode data dir $d: $!\n";
197 for my $f (readdir ($dir))
199 $unicodedatatxt = "$d/$f" if ($f =~ /^UnicodeData.*\.txt/);
200 $linebreaktxt = "$d/$f" if ($f =~ /^LineBreak.*\.txt/);
201 $specialcasingtxt = "$d/$f" if ($f =~ /^SpecialCasing.*\.txt/);
202 $casefoldingtxt = "$d/$f" if ($f =~ /^CaseFolding.*\.txt/);
203 $compositionexclusionstxt = "$d/$f" if ($f =~ /^CompositionExclusions.*\.txt/);
204 $scriptstxt = "$d/$f" if ($f =~ /^Scripts.*\.txt/);
207 my $extd = $ARGV[1] . "/extracted";
208 opendir (my $extdir, $extd) or die "Cannot open Unicode/extracted data dir $extd: $!\n";
209 for my $f (readdir ($extdir))
211 $derivedeastasianwidthtxt = "$extd/$f" if ($f =~ /^DerivedEastAsianWidth.*\.txt/);
214 defined $unicodedatatxt or die "Did not find UnicodeData file";
215 defined $linebreaktxt or die "Did not find LineBreak file";
216 defined $specialcasingtxt or die "Did not find SpecialCasing file";
217 defined $casefoldingtxt or die "Did not find CaseFolding file";
218 defined $compositionexclusionstxt or die "Did not find CompositionExclusions file";
219 defined $scriptstxt or die "Did not find Scripts file";
220 defined $derivedeastasianwidthtxt or die "Did not find DerivedEastAsianWidth file";
222 print "Creating decomp table\n" if ($do_decomp);
223 print "Creating property table\n" if ($do_props);
225 print "Composition exlusions from $compositionexclusionstxt\n";
227 open (INPUT, "< $compositionexclusionstxt") || exit 1;
241 $composition_exclusions{hex($_)} = 1;
246 print "Unicode data from $unicodedatatxt\n";
248 open (INPUT, "< $unicodedatatxt") || exit 1;
250 # we save memory by skipping the huge empty area before U+E0000
251 my $pages_before_e0000;
257 @fields = split (';', $_, 30);
260 printf STDERR ("Entry for $fields[$CODE] has wrong number of fields (%d)\n", $#fields);
263 $code = hex ($fields[$CODE]);
265 if ($code >= 0xE0000 and $last_code < 0xE0000)
267 $pages_before_e0000 = ($last_code >> 8) + 1;
270 if ($code > $last_code + 1)
273 if ($fields[$NAME] =~ /Last>/)
275 # Fill the gap with the last character read,
276 # since this was a range specified in the char database
281 # The gap represents undefined characters. Only the type
283 @gfields = ('', '', 'Cn', '0', '', '', '', '', '', '', '',
286 for (++$last_code; $last_code < $code; ++$last_code)
288 $gfields{$CODE} = sprintf ("%04x", $last_code);
289 &process_one ($last_code, @gfields);
292 &process_one ($code, @fields);
298 @gfields = ('', '', 'Cn', '0', '', '', '', '', '', '', '',
300 for (++$last_code; $last_code <= 0x10FFFF; ++$last_code)
302 $gfields{$CODE} = sprintf ("%04x", $last_code);
303 &process_one ($last_code, @gfields);
305 --$last_code; # Want last to be 0x10FFFF.
307 print "Creating line break table\n";
309 print "Line break data from $linebreaktxt\n";
311 open (INPUT, "< $linebreaktxt") || exit 1;
316 my ($start_code, $end_code);
325 @fields = split (';', $_, 30);
328 printf STDERR ("Entry for $fields[$CODE] has wrong number of fields (%d)\n", $#fields);
332 if ($fields[$CODE] =~ /([A-F0-9]{4,6})\.\.([A-F0-9]{4,6})/)
334 $start_code = hex ($1);
335 $end_code = hex ($2);
337 $start_code = $end_code = hex ($fields[$CODE]);
341 if ($start_code > $last_code + 1)
343 # The gap represents undefined characters. If assigned,
344 # they are AL, if not assigned, XX
345 for (++$last_code; $last_code < $start_code; ++$last_code)
347 if ($type[$last_code] eq 'Cn')
349 $break_props[$last_code] = 'XX';
353 $break_props[$last_code] = 'AL';
358 for ($last_code = $start_code; $last_code <= $end_code; $last_code++)
360 $break_props[$last_code] = $fields[$BREAK_PROPERTY];
363 $last_code = $end_code;
368 for (++$last_code; $last_code <= 0x10FFFF; ++$last_code)
370 if ($type[$last_code] eq 'Cn')
372 $break_props[$last_code] = 'XX';
376 $break_props[$last_code] = 'AL';
379 --$last_code; # Want last to be 0x10FFFF.
381 print STDERR "Last code is not 0x10FFFF" if ($last_code != 0x10FFFF);
383 print "Reading special-casing table for case conversion\n";
385 open (INPUT, "< $specialcasingtxt") || exit 1;
398 @fields = split ('\s*;\s*', $_, 30);
400 $raw_code = $fields[$CASE_CODE];
401 $code = hex ($raw_code);
403 if ($#fields != 4 && $#fields != 5)
405 printf STDERR ("Entry for $raw_code has wrong number of fields (%d)\n", $#fields);
409 if (!defined $type[$code])
411 printf STDERR "Special case for code point: $code, which has no defined type\n";
415 if (defined $fields[5]) {
416 # Ignore conditional special cases - we'll handle them in code
420 if ($type[$code] eq 'Lu')
422 (hex $fields[$CASE_UPPER] == $code) || die "$raw_code is Lu and UCD_Upper($raw_code) != $raw_code";
424 &add_special_case ($code, $value[$code], $fields[$CASE_LOWER], $fields[$CASE_TITLE]);
426 } elsif ($type[$code] eq 'Lt')
428 (hex $fields[$CASE_TITLE] == $code) || die "$raw_code is Lt and UCD_Title($raw_code) != $raw_code";
430 &add_special_case ($code, undef, $fields[$CASE_LOWER], $fields[$CASE_UPPER]);
431 } elsif ($type[$code] eq 'Ll')
433 (hex $fields[$CASE_LOWER] == $code) || die "$raw_code is Ll and UCD_Lower($raw_code) != $raw_code";
435 &add_special_case ($code, $value[$code], $fields[$CASE_UPPER], $fields[$CASE_TITLE]);
437 printf STDERR "Special case for non-alphabetic code point: $raw_code\n";
444 open (INPUT, "< $casefoldingtxt") || exit 1;
460 @fields = split ('\s*;\s*', $_, 30);
462 $raw_code = $fields[$FOLDING_CODE];
463 $code = hex ($raw_code);
467 printf STDERR ("Entry for $raw_code has wrong number of fields (%d)\n", $#fields);
471 # we don't use Simple or Turkic rules here
472 next if ($fields[$FOLDING_STATUS] =~ /^[ST]$/);
474 @values = map { hex ($_) } split /\s+/, $fields[$FOLDING_MAPPING];
479 !(defined $value[$code] && $value[$code] >= 0x1000000) &&
480 defined $type[$code]) {
483 if ($type[$code] eq 'Ll')
486 } elsif ($type[$code] eq 'Lt')
488 $lower = $title_to_lower{$code};
489 } elsif ($type[$code] eq 'Lu')
491 $lower = $value[$code];
496 if ($lower == $values[0]) {
501 my $string = pack ("U*", @values);
503 if (1 + &length_in_bytes ($string) > $casefoldlen) {
504 $casefoldlen = 1 + &length_in_bytes ($string);
507 push @casefold, [ $code, &escape ($string) ];
512 print "Reading scripts\n";
514 open (INPUT, "< $scriptstxt") || exit 1;
519 if (!/^([0-9A-F]+)(?:\.\.([0-9A-F]+))?\s*;\s*([A-Za-z_]+)\s*$/) {
520 die "Cannot parse line: '$_'\n";
524 push @scripts, [ hex $1, hex $2, uc $3 ];
526 push @scripts, [ hex $1, hex $1, uc $3 ];
532 print "Reading derived east asian widths\n";
534 open (INPUT, "< $derivedeastasianwidthtxt") || exit 1;
538 my ($start_code, $end_code);
544 if (!/^([0-9A-F]+)(?:\.\.([0-9A-F]+))?\s*;\s*([A-Za-z_]+)\s*$/) {
545 die "Cannot parse line: '$_'\n";
549 push @eawidths, [ hex $1, hex $2, $3 ];
551 push @eawidths, [ hex $1, hex $1, $3 ];
558 &print_tables ($last_code)
561 &print_decomp ($last_code);
562 &output_composition_table;
564 &print_line_break ($last_code);
573 # perl "length" returns the length in characters
578 return length $string;
581 # Process a single character.
584 my ($code, @fields) = @_;
586 $type[$code] = $fields[$CATEGORY];
587 if ($type[$code] eq 'Nd')
589 $value[$code] = int ($fields[$DECIMAL_VALUE]);
591 elsif ($type[$code] eq 'Ll')
593 $value[$code] = hex ($fields[$UPPER]);
595 elsif ($type[$code] eq 'Lu')
597 $value[$code] = hex ($fields[$LOWER]);
600 if ($type[$code] eq 'Lt')
602 $title_to_lower{$code} = hex ($fields[$LOWER]);
603 $title_to_upper{$code} = hex ($fields[$UPPER]);
606 $cclass[$code] = $fields[$COMBINING_CLASSES];
608 # Handle decompositions.
609 if ($fields[$DECOMPOSITION] ne '')
611 if ($fields[$DECOMPOSITION] =~ s/\<.*\>\s*//) {
612 $decompose_compat[$code] = 1;
614 $decompose_compat[$code] = 0;
616 if (!exists $composition_exclusions{$code}) {
617 $compositions{$code} = $fields[$DECOMPOSITION];
620 $decompositions[$code] = $fields[$DECOMPOSITION];
627 my ($outfile) = "gunichartables.h";
629 local ($bytes_out) = 0;
631 print "Writing $outfile...\n";
633 open (OUT, "> $outfile");
635 print OUT "/* This file is automatically generated. DO NOT EDIT!\n";
636 print OUT " Instead, edit gen-unicode-tables.pl and re-run. */\n\n";
638 print OUT "#ifndef CHARTABLES_H\n";
639 print OUT "#define CHARTABLES_H\n\n";
641 print OUT "#define G_UNICODE_DATA_VERSION \"$ARGV[0]\"\n\n";
643 printf OUT "#define G_UNICODE_LAST_CHAR 0x%04x\n\n", $last;
645 printf OUT "#define G_UNICODE_MAX_TABLE_INDEX 10000\n\n";
647 my $last_part1 = ($pages_before_e0000 * 256) - 1;
648 printf OUT "#define G_UNICODE_LAST_CHAR_PART1 0x%04X\n\n", $last_part1;
649 printf OUT "#define G_UNICODE_LAST_PAGE_PART1 %d\n\n", $pages_before_e0000 - 1;
652 printf OUT "static const char type_data[][256] = {\n";
653 for ($count = 0; $count <= $last; $count += 256)
655 $row[$count / 256] = &print_row ($count, 1, \&fetch_type);
657 printf OUT "\n};\n\n";
659 printf OUT "/* U+0000 through U+%04X */\n", $last_part1;
660 print OUT "static const gint16 type_table_part1[$pages_before_e0000] = {\n";
661 for ($count = 0; $count <= $last_part1; $count += 256)
663 print OUT ",\n" if $count > 0;
664 print OUT " ", $row[$count / 256];
667 print OUT "\n};\n\n";
669 printf OUT "/* U+E0000 through U+%04X */\n", $last;
670 print OUT "static const gint16 type_table_part2[768] = {\n";
671 for ($count = 0xE0000; $count <= $last; $count += 256)
673 print OUT ",\n" if $count > 0xE0000;
674 print OUT " ", $row[$count / 256];
677 print OUT "\n};\n\n";
681 # Now print attribute table.
685 printf OUT "static const gunichar attr_data[][256] = {\n";
686 for ($count = 0; $count <= $last; $count += 256)
688 $row[$count / 256] = &print_row ($count, 4, \&fetch_attr);
690 printf OUT "\n};\n\n";
692 printf OUT "/* U+0000 through U+%04X */\n", $last_part1;
693 print OUT "static const gint16 attr_table_part1[$pages_before_e0000] = {\n";
694 for ($count = 0; $count <= $last_part1; $count += 256)
696 print OUT ",\n" if $count > 0;
697 print OUT " ", $row[$count / 256];
700 print OUT "\n};\n\n";
702 printf OUT "/* U+E0000 through U+%04X */\n", $last;
703 print OUT "static const gint16 attr_table_part2[768] = {\n";
704 for ($count = 0xE0000; $count <= $last; $count += 256)
706 print OUT ",\n" if $count > 0xE0000;
707 print OUT " ", $row[$count / 256];
710 print OUT "\n};\n\n";
713 # print title case table
716 print OUT "static const gunichar title_table[][3] = {\n";
719 foreach $item (sort keys %title_to_lower)
724 printf OUT " { 0x%04x, 0x%04x, 0x%04x }", $item, $title_to_upper{$item}, $title_to_lower{$item};
727 print OUT "\n};\n\n";
730 # And special case conversion table -- conversions that change length
732 &output_special_case_table (\*OUT);
733 &output_casefold_table (\*OUT);
736 # And the widths tables
738 &output_width_tables (\*OUT);
740 print OUT "#endif /* CHARTABLES_H */\n";
744 printf STDERR "Generated %d bytes in tables\n", $bytes_out;
747 # A fetch function for the type table.
751 return $mappings{$type[$index]};
754 # A fetch function for the attribute table.
758 if (defined $value[$index])
760 return sprintf ("0x%04x", $value[$index]);
770 my ($start, $typsize, $fetcher) = @_;
777 for ($off = 0; $off < 256; ++$off)
779 $values[$off] = $fetcher->($off + $start);
780 if ($values[$off] ne $values[0])
787 return $values[0] . " + G_UNICODE_MAX_TABLE_INDEX";
790 printf OUT ",\n" if ($table_index != 0);
791 printf OUT " { /* page %d, index %d */\n ", $start / 256, $table_index;
793 for ($i = $start; $i < $start + 256; ++$i)
797 my ($text) = $values[$i - $start];
798 if (length ($text) + $column + 2 > 78)
804 $column += length ($text) + 2;
808 $bytes_out += 256 * $typsize;
810 return sprintf "%d /* page %d */", $table_index++, $start / 256;
817 my $escaped = unpack("H*", $string);
818 $escaped =~ s/(.{2})/\\x$1/g;
823 # Returns the offset of $decomp in the offset string. Updates the
824 # referenced variables as appropriate.
825 sub handle_decomp ($$$$)
827 my ($decomp, $decomp_offsets_ref, $decomp_string_ref, $decomp_string_offset_ref) = @_;
828 my $offset = "G_UNICODE_NOT_PRESENT_OFFSET";
832 if (defined $decomp_offsets_ref->{$decomp})
834 $offset = $decomp_offsets_ref->{$decomp};
838 $offset = ${$decomp_string_offset_ref};
839 $decomp_offsets_ref->{$decomp} = $offset;
840 ${$decomp_string_ref} .= "\n \"" . &escape ($decomp) . "\\0\" /* offset ${$decomp_string_offset_ref} */";
841 ${$decomp_string_offset_ref} += &length_in_bytes ($decomp) + 1;
848 # Generate the character decomposition header.
852 my ($outfile) = "gunidecomp.h";
854 local ($bytes_out) = 0;
856 print "Writing $outfile...\n";
858 open (OUT, "> $outfile") || exit 1;
860 print OUT "/* This file is automatically generated. DO NOT EDIT! */\n\n";
861 print OUT "#ifndef DECOMP_H\n";
862 print OUT "#define DECOMP_H\n\n";
864 printf OUT "#define G_UNICODE_LAST_CHAR 0x%04x\n\n", $last;
866 printf OUT "#define G_UNICODE_MAX_TABLE_INDEX (0x110000 / 256)\n\n";
868 my $last_part1 = ($pages_before_e0000 * 256) - 1;
869 printf OUT "#define G_UNICODE_LAST_CHAR_PART1 0x%04X\n\n", $last_part1;
870 printf OUT "#define G_UNICODE_LAST_PAGE_PART1 %d\n\n", $pages_before_e0000 - 1;
872 $NOT_PRESENT_OFFSET = 65535;
873 print OUT "#define G_UNICODE_NOT_PRESENT_OFFSET $NOT_PRESENT_OFFSET\n\n";
877 printf OUT "static const guchar cclass_data[][256] = {\n";
878 for ($count = 0; $count <= $last; $count += 256)
880 $row[$count / 256] = &print_row ($count, 1, \&fetch_cclass);
882 printf OUT "\n};\n\n";
884 print OUT "static const gint16 combining_class_table_part1[$pages_before_e0000] = {\n";
885 for ($count = 0; $count <= $last_part1; $count += 256)
887 print OUT ",\n" if $count > 0;
888 print OUT " ", $row[$count / 256];
891 print OUT "\n};\n\n";
893 print OUT "static const gint16 combining_class_table_part2[768] = {\n";
894 for ($count = 0xE0000; $count <= $last; $count += 256)
896 print OUT ",\n" if $count > 0xE0000;
897 print OUT " ", $row[$count / 256];
900 print OUT "\n};\n\n";
902 print OUT "typedef struct\n{\n";
903 print OUT " gunichar ch;\n";
904 print OUT " guint16 canon_offset;\n";
905 print OUT " guint16 compat_offset;\n";
906 print OUT "} decomposition;\n\n";
908 print OUT "static const decomposition decomp_table[] =\n{\n";
911 my ($decomp_string) = "";
912 my ($decomp_string_offset) = 0;
913 for ($count = 0; $count <= $last; ++$count)
915 if (defined $decompositions[$count])
924 if (!$decompose_compat[$count]) {
925 $canon_decomp = make_decomp ($count, 0);
927 $compat_decomp = make_decomp ($count, 1);
929 if (defined $canon_decomp && $compat_decomp eq $canon_decomp) {
930 undef $compat_decomp;
933 my $canon_offset = handle_decomp ($canon_decomp, \%decomp_offsets, \$decomp_string, \$decomp_string_offset);
934 my $compat_offset = handle_decomp ($compat_decomp, \%decomp_offsets, \$decomp_string, \$decomp_string_offset);
936 die if $decomp_string_offset > $NOT_PRESENT_OFFSET;
938 printf OUT qq( { 0x%04x, $canon_offset, $compat_offset }), $count;
942 print OUT "\n};\n\n";
943 $bytes_out += $decomp_string_offset + 1;
945 printf OUT "static const gchar decomp_expansion_string[] = %s;\n\n", $decomp_string;
947 print OUT "typedef struct\n{\n";
948 print OUT " gunichar ch;\n";
949 print OUT " gunichar a;\n";
950 print OUT " gunichar b;\n";
951 print OUT "} decomposition_step;\n\n";
953 # There's lots of room to optimize the following table...
954 print OUT "static const decomposition_step decomp_step_table[] =\n{\n";
957 for ($count = 0; $count <= $last; ++$count)
959 if ((defined $decompositions[$count]) && (!$decompose_compat[$count]))
965 @list = (split(' ', $decompositions[$count]), "0");
966 printf OUT qq( { 0x%05x, 0x%05x, 0x%05x }), $count, hex($list[0]), hex($list[1]);
967 # don't include 1:1 in the compose table
968 push @steps, [ ($count, hex($list[0]), hex($list[1])) ]
972 print OUT "\n};\n\n";
974 print OUT "#endif /* DECOMP_H */\n";
976 printf STDERR "Generated %d bytes in decomp tables\n", $bytes_out;
982 my ($outfile) = "gunibreak.h";
984 local ($bytes_out) = 0;
986 print "Writing $outfile...\n";
988 open (OUT, "> $outfile");
990 print OUT "/* This file is automatically generated. DO NOT EDIT!\n";
991 print OUT " Instead, edit gen-unicode-tables.pl and re-run. */\n\n";
993 print OUT "#ifndef BREAKTABLES_H\n";
994 print OUT "#define BREAKTABLES_H\n\n";
996 print OUT "#include <glib/gtypes.h>\n";
997 print OUT "#include <glib/gunicode.h>\n\n";
999 print OUT "#define G_UNICODE_DATA_VERSION \"$ARGV[0]\"\n\n";
1001 printf OUT "#define G_UNICODE_LAST_CHAR 0x%04X\n\n", $last;
1003 printf OUT "#define G_UNICODE_MAX_TABLE_INDEX 10000\n\n";
1005 my $last_part1 = ($pages_before_e0000 * 256) - 1;
1006 printf OUT "/* the last code point that should be looked up in break_property_table_part1 */\n";
1007 printf OUT "#define G_UNICODE_LAST_CHAR_PART1 0x%04X\n\n", $last_part1;
1010 printf OUT "static const gint8 break_property_data[][256] = {\n";
1011 for ($count = 0; $count <= $last; $count += 256)
1013 $row[$count / 256] = &print_row ($count, 1, \&fetch_break_type);
1015 printf OUT "\n};\n\n";
1017 printf OUT "/* U+0000 through U+%04X */\n", $last_part1;
1018 print OUT "static const gint16 break_property_table_part1[$pages_before_e0000] = {\n";
1019 for ($count = 0; $count <= $last_part1; $count += 256)
1021 print OUT ",\n" if $count > 0;
1022 print OUT " ", $row[$count / 256];
1025 print OUT "\n};\n\n";
1027 printf OUT "/* U+E0000 through U+%04X */\n", $last;
1028 print OUT "static const gint16 break_property_table_part2[768] = {\n";
1029 for ($count = 0xE0000; $count <= $last; $count += 256)
1031 print OUT ",\n" if $count > 0xE0000;
1032 print OUT " ", $row[$count / 256];
1035 print OUT "\n};\n\n";
1038 print OUT "#endif /* BREAKTABLES_H */\n";
1042 printf STDERR "Generated %d bytes in break tables\n", $bytes_out;
1046 # A fetch function for the break properties table.
1047 sub fetch_break_type
1050 return $break_mappings{$break_props[$index]};
1053 # Fetcher for combining class.
1060 # Expand a character decomposition recursively.
1063 my ($code, $compat) = @_;
1067 foreach $iter (split (' ', $decompositions[$code]))
1070 if (defined $decompositions[$val] &&
1071 ($compat || !$decompose_compat[$val]))
1073 push (@result, &expand_decomp ($val, $compat));
1077 push (@result, $val);
1086 my ($code, $compat) = @_;
1089 foreach $iter (&expand_decomp ($code, $compat))
1091 $result .= pack ("U", $iter); # to utf-8
1096 # Generate special case data string from two fields
1097 sub add_special_case
1099 my ($code, $single, $field1, $field2) = @_;
1101 @values = (defined $single ? $single : (),
1102 (map { hex ($_) } split /\s+/, $field1),
1104 (map { hex ($_) } split /\s+/, $field2));
1108 for $value (@values) {
1109 $result .= pack ("U", $value); # to utf-8
1112 push @special_case_offsets, $special_case_offset;
1114 # We encode special cases up in the 0x1000000 space
1115 $value[$code] = 0x1000000 + $special_case_offset;
1117 $special_case_offset += 1 + &length_in_bytes ($result);
1119 push @special_cases, &escape ($result);
1122 sub output_special_case_table
1128 /* Table of special cases for case conversion; each record contains
1129 * First, the best single character mapping to lowercase if Lu,
1130 * and to uppercase if Ll, followed by the output mapping for the two cases
1131 * other than the case of the codepoint, in the order [Ll],[Lu],[Lt],
1132 * encoded in UTF-8, separated and terminated by a null character.
1134 static const gchar special_case_table[] = {
1138 for $case (@special_cases) {
1139 print $out qq( "$case\\0" /* offset ${special_case_offsets[$i]} */\n);
1148 print STDERR "Generated " . ($special_case_offset + 1) . " bytes in special case table\n";
1151 sub enumerate_ordered
1156 for my $code (sort { $a <=> $b } keys %$array) {
1157 if ($array->{$code} == 1) {
1158 delete $array->{$code};
1161 $array->{$code} = $n++;
1167 sub output_composition_table
1169 print STDERR "Generating composition table\n";
1171 local ($bytes_out) = 0;
1176 # First we need to go through and remove decompositions
1177 # starting with a non-starter, and single-character
1178 # decompositions. At the same time, record
1179 # the first and second character of each decomposition
1181 for $code (keys %compositions)
1183 @values = map { hex ($_) } split /\s+/, $compositions{$code};
1186 if ($cclass[$code]) {
1187 delete $compositions{$code};
1190 if ($cclass[$values[0]]) {
1191 delete $compositions{$code};
1195 # single-character decompositions
1197 delete $compositions{$code};
1202 die "$code has more than two elements in its decomposition!\n";
1205 if (exists $first{$values[0]}) {
1206 $first{$values[0]}++;
1208 $first{$values[0]} = 1;
1212 # Assign integer indices, removing singletons
1213 my $n_first = enumerate_ordered (\%first);
1215 # Now record the second character of each (non-singleton) decomposition
1216 for $code (keys %compositions) {
1217 @values = map { hex ($_) } split /\s+/, $compositions{$code};
1219 if (exists $first{$values[0]}) {
1220 if (exists $second{$values[1]}) {
1221 $second{$values[1]}++;
1223 $second{$values[1]} = 1;
1228 # Assign integer indices, removing duplicate
1229 my $n_second = enumerate_ordered (\%second);
1231 # Build reverse table
1233 my @first_singletons;
1234 my @second_singletons;
1236 for $code (keys %compositions) {
1237 @values = map { hex ($_) } split /\s+/, $compositions{$code};
1239 my $first = $first{$values[0]};
1240 my $second = $second{$values[1]};
1242 if (defined $first && defined $second) {
1243 $reverse{"$first|$second"} = $code;
1244 } elsif (!defined $first) {
1245 push @first_singletons, [ $values[0], $values[1], $code ];
1247 push @second_singletons, [ $values[1], $values[0], $code ];
1251 @first_singletons = sort { $a->[0] <=> $b->[0] } @first_singletons;
1252 @second_singletons = sort { $a->[0] <=> $b->[0] } @second_singletons;
1256 open OUT, ">gunicomp.h" or die "Cannot open gunicomp.h: $!\n";
1258 # Assign values in lookup table for all code points involved
1262 printf OUT "#define COMPOSE_FIRST_START %d\n", $total;
1263 for $code (keys %first) {
1264 $vals{$code} = $first{$code} + $total;
1265 $last = $code if $code > $last;
1269 printf OUT "#define COMPOSE_FIRST_SINGLE_START %d\n", $total;
1270 for $record (@first_singletons) {
1271 my $code = $record->[0];
1272 $vals{$code} = $i++ + $total;
1273 $last = $code if $code > $last;
1275 $total += @first_singletons;
1276 printf OUT "#define COMPOSE_SECOND_START %d\n", $total;
1277 for $code (keys %second) {
1278 $vals{$code} = $second{$code} + $total;
1279 $last = $code if $code > $last;
1281 $total += $n_second;
1283 printf OUT "#define COMPOSE_SECOND_SINGLE_START %d\n\n", $total;
1284 for $record (@second_singletons) {
1285 my $code = $record->[0];
1286 $vals{$code} = $i++ + $total;
1287 $last = $code if $code > $last;
1290 printf OUT "#define COMPOSE_TABLE_LAST %d\n\n", $last / 256;
1292 # Output lookup table
1296 printf OUT "static const guint16 compose_data[][256] = {\n";
1297 for (my $count = 0; $count <= $last; $count += 256)
1299 $row[$count / 256] = &print_row ($count, 2, sub { exists $vals{$_[0]} ? $vals{$_[0]} : 0; });
1301 printf OUT "\n};\n\n";
1303 print OUT "static const gint16 compose_table[COMPOSE_TABLE_LAST + 1] = {\n";
1304 for (my $count = 0; $count <= $last; $count += 256)
1306 print OUT ",\n" if $count > 0;
1307 print OUT " ", $row[$count / 256];
1310 print OUT "\n};\n\n";
1312 # Output first singletons
1314 print OUT "static const gunichar compose_first_single[][2] = {\n";
1316 for $record (@first_singletons) {
1317 print OUT ",\n" if $i++ > 0;
1318 printf OUT " { %#06x, %#06x }", $record->[1], $record->[2];
1322 $bytes_out += @first_singletons * 4;
1324 # Output second singletons
1326 print OUT "static const gunichar compose_second_single[][2] = {\n";
1328 for $record (@second_singletons) {
1329 print OUT ",\n" if $i++ > 0;
1330 printf OUT " { %#06x, %#06x }", $record->[1], $record->[2];
1334 $bytes_out += @second_singletons * 4;
1336 # Output array of composition pairs
1339 static const guint16 compose_array[$n_first][$n_second] = {
1342 for (my $i = 0; $i < $n_first; $i++) {
1343 print OUT ",\n" if $i;
1345 for (my $j = 0; $j < $n_second; $j++) {
1346 print OUT ", " if $j;
1347 if (exists $reverse{"$i|$j"}) {
1348 if ($reverse{"$i|$j"} > 0xFFFF) {
1349 die "time to switch compose_array to gunichar" ;
1351 printf OUT "0x%04x", $reverse{"$i|$j"};
1364 $bytes_out += $n_first * $n_second * 2;
1366 printf STDERR "Generated %d bytes in compose tables\n", $bytes_out;
1369 sub output_casefold_table
1375 /* Table of casefolding cases that can't be derived by lowercasing
1377 static const struct {
1379 gchar data[$casefoldlen];
1380 } casefold_table[] = {
1383 @casefold = sort { $a->[0] <=> $b->[0] } @casefold;
1385 for $case (@casefold)
1388 $string = $case->[1];
1390 if ($code > 0xFFFF) {
1391 die "time to switch casefold_table to gunichar" ;
1394 print $out sprintf(qq( { 0x%04x, "$string" },\n), $code);
1403 my $recordlen = (2+$casefoldlen+1) & ~1;
1404 printf "Generated %d bytes for casefold table\n", $recordlen * @casefold;
1407 sub output_one_width_table
1409 my ($out, $name, $wpe) = @_;
1415 print $out "static const struct Interval g_unicode_width_table_${name}[] = {\n";
1419 for (my $i = 0; $i <= $#eawidths; $i++) {
1420 $start = $eawidths[$i]->[0];
1421 $end = $eawidths[$i]->[1];
1422 $wp = $eawidths[$i]->[2];
1424 next if ($wp !~ $rex);
1426 while ($i <= $#eawidths - 1 &&
1427 $eawidths[$i + 1]->[0] == $end + 1 &&
1428 ($eawidths[$i + 1]->[2] =~ $rex)) {
1430 $end = $eawidths[$i]->[1];
1433 printf $out "{0x%04X, 0x%04X},\n", $start, $end;
1436 printf $out "};\n\n";
1439 sub output_width_tables
1443 @eawidths = sort { $a->[0] <=> $b->[0] } @eawidths;
1449 gunichar start, end;
1454 &output_one_width_table ($out,"wide", "[FW]");
1455 &output_one_width_table ($out, "ambiguous", "[A]");
1466 print STDERR "Writing gscripttable.h\n";
1468 open OUT, ">gscripttable.h" or die "Cannot open gscripttable.h: $!\n";
1471 /* This file is automatically generated. DO NOT EDIT!
1472 Instead, edit gen-unicode-tables.pl and re-run. */
1474 #ifndef SCRIPTTABLES_H
1475 #define SCRIPTTABLES_H
1479 @scripts = sort { $a->[0] <=> $b->[0] } @scripts;
1481 $easy_range = 0x2000;
1484 #define G_EASY_SCRIPTS_RANGE $easy_range
1486 static const guchar g_script_easy_table[$easy_range] = {
1492 for (my $c = 0; $c < $easy_range; $c++) {
1499 $start = $scripts[$i]->[0];
1500 $end = $scripts[$i]->[1];
1501 $script = $scripts[$i]->[2];
1506 printf OUT " G_UNICODE_SCRIPT_UNKNOWN,";
1508 printf OUT " G_UNICODE_SCRIPT_%s,", $script;
1512 if ($end >= $easy_range) {
1514 $scripts[$i]->[0] = $easy_range;
1521 static const struct {
1525 } g_script_table[] = {
1528 for (; $i <= $#scripts; $i++) {
1529 $start = $scripts[$i]->[0];
1530 $end = $scripts[$i]->[1];
1531 $script = $scripts[$i]->[2];
1533 while ($i <= $#scripts - 1 &&
1534 $scripts[$i + 1]->[0] == $end + 1 &&
1535 $scripts[$i + 1]->[2] eq $script) {
1537 $end = $scripts[$i]->[1];
1539 printf OUT " { %#06x, %5d, G_UNICODE_SCRIPT_%s },\n", $start, $end - $start + 1, $script;
1545 #endif /* SCRIPTTABLES_H */