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_PARENTHESIS",
123 'CR' => "G_UNICODE_BREAK_CARRIAGE_RETURN",
124 'EB' => "G_UNICODE_BREAK_EMOJI_BASE",
125 'EM' => "G_UNICODE_BREAK_EMOJI_MODIFIER",
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",
154 'ZWJ' => "G_UNICODE_BREAK_ZERO_WIDTH_JOINER"
157 # Title case mappings.
158 %title_to_lower = ();
159 %title_to_upper = ();
161 # Maximum length of special-case strings
164 my @special_case_offsets;
165 my $special_case_offset = 0;
178 if (@ARGV && $ARGV[0] eq '-decomp')
184 elsif (@ARGV && $ARGV[0] eq '-both')
192 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";
195 my ($unicodedatatxt, $linebreaktxt, $specialcasingtxt, $casefoldingtxt, $compositionexclusionstxt,
196 $scriptstxt, $derivedeastasianwidth);
199 opendir (my $dir, $d) or die "Cannot open Unicode data dir $d: $!\n";
200 for my $f (readdir ($dir))
202 $unicodedatatxt = "$d/$f" if ($f =~ /^UnicodeData.*\.txt/);
203 $linebreaktxt = "$d/$f" if ($f =~ /^LineBreak.*\.txt/);
204 $specialcasingtxt = "$d/$f" if ($f =~ /^SpecialCasing.*\.txt/);
205 $casefoldingtxt = "$d/$f" if ($f =~ /^CaseFolding.*\.txt/);
206 $compositionexclusionstxt = "$d/$f" if ($f =~ /^CompositionExclusions.*\.txt/);
207 $scriptstxt = "$d/$f" if ($f =~ /^Scripts.*\.txt/);
210 my $extd = $ARGV[1] . "/extracted";
211 opendir (my $extdir, $extd) or die "Cannot open Unicode/extracted data dir $extd: $!\n";
212 for my $f (readdir ($extdir))
214 $derivedeastasianwidthtxt = "$extd/$f" if ($f =~ /^DerivedEastAsianWidth.*\.txt/);
217 defined $unicodedatatxt or die "Did not find UnicodeData file";
218 defined $linebreaktxt or die "Did not find LineBreak file";
219 defined $specialcasingtxt or die "Did not find SpecialCasing file";
220 defined $casefoldingtxt or die "Did not find CaseFolding file";
221 defined $compositionexclusionstxt or die "Did not find CompositionExclusions file";
222 defined $scriptstxt or die "Did not find Scripts file";
223 defined $derivedeastasianwidthtxt or die "Did not find DerivedEastAsianWidth file";
225 print "Creating decomp table\n" if ($do_decomp);
226 print "Creating property table\n" if ($do_props);
228 print "Composition exclusions from $compositionexclusionstxt\n";
230 open (INPUT, "< $compositionexclusionstxt") || exit 1;
244 $composition_exclusions{hex($_)} = 1;
249 print "Unicode data from $unicodedatatxt\n";
251 open (INPUT, "< $unicodedatatxt") || exit 1;
253 # we save memory by skipping the huge empty area before U+E0000
254 my $pages_before_e0000;
260 @fields = split (';', $_, 30);
263 printf STDERR ("Entry for $fields[$CODE] has wrong number of fields (%d)\n", $#fields);
266 $code = hex ($fields[$CODE]);
268 if ($code >= 0xE0000 and $last_code < 0xE0000)
270 $pages_before_e0000 = ($last_code >> 8) + 1;
273 if ($code > $last_code + 1)
276 if ($fields[$NAME] =~ /Last>/)
278 # Fill the gap with the last character read,
279 # since this was a range specified in the char database
284 # The gap represents undefined characters. Only the type
286 @gfields = ('', '', 'Cn', '0', '', '', '', '', '', '', '',
289 for (++$last_code; $last_code < $code; ++$last_code)
291 $gfields{$CODE} = sprintf ("%04x", $last_code);
292 &process_one ($last_code, @gfields);
295 &process_one ($code, @fields);
301 @gfields = ('', '', 'Cn', '0', '', '', '', '', '', '', '',
303 for (++$last_code; $last_code <= 0x10FFFF; ++$last_code)
305 $gfields{$CODE} = sprintf ("%04x", $last_code);
306 &process_one ($last_code, @gfields);
308 --$last_code; # Want last to be 0x10FFFF.
310 print "Creating line break table\n";
312 print "Line break data from $linebreaktxt\n";
314 open (INPUT, "< $linebreaktxt") || exit 1;
319 my ($start_code, $end_code);
328 @fields = split (';', $_, 30);
331 printf STDERR ("Entry for $fields[$CODE] has wrong number of fields (%d)\n", $#fields);
335 if ($fields[$CODE] =~ /([A-F0-9]{4,6})\.\.([A-F0-9]{4,6})/)
337 $start_code = hex ($1);
338 $end_code = hex ($2);
340 $start_code = $end_code = hex ($fields[$CODE]);
344 if ($start_code > $last_code + 1)
346 # The gap represents undefined characters. If assigned,
347 # they are AL, if not assigned, XX
348 for (++$last_code; $last_code < $start_code; ++$last_code)
350 if ($type[$last_code] eq 'Cn')
352 $break_props[$last_code] = 'XX';
356 $break_props[$last_code] = 'AL';
361 for ($last_code = $start_code; $last_code <= $end_code; $last_code++)
363 $break_props[$last_code] = $fields[$BREAK_PROPERTY];
366 $last_code = $end_code;
371 for (++$last_code; $last_code <= 0x10FFFF; ++$last_code)
373 if ($type[$last_code] eq 'Cn')
375 $break_props[$last_code] = 'XX';
379 $break_props[$last_code] = 'AL';
382 --$last_code; # Want last to be 0x10FFFF.
384 print STDERR "Last code is not 0x10FFFF" if ($last_code != 0x10FFFF);
386 print "Reading special-casing table for case conversion\n";
388 open (INPUT, "< $specialcasingtxt") || exit 1;
401 @fields = split ('\s*;\s*', $_, 30);
403 $raw_code = $fields[$CASE_CODE];
404 $code = hex ($raw_code);
406 if ($#fields != 4 && $#fields != 5)
408 printf STDERR ("Entry for $raw_code has wrong number of fields (%d)\n", $#fields);
412 if (!defined $type[$code])
414 printf STDERR "Special case for code point: $code, which has no defined type\n";
418 if (defined $fields[5]) {
419 # Ignore conditional special cases - we'll handle them in code
423 if ($type[$code] eq 'Lu')
425 (hex $fields[$CASE_UPPER] == $code) || die "$raw_code is Lu and UCD_Upper($raw_code) != $raw_code";
427 &add_special_case ($code, $value[$code], $fields[$CASE_LOWER], $fields[$CASE_TITLE]);
429 } elsif ($type[$code] eq 'Lt')
431 (hex $fields[$CASE_TITLE] == $code) || die "$raw_code is Lt and UCD_Title($raw_code) != $raw_code";
433 &add_special_case ($code, undef, $fields[$CASE_LOWER], $fields[$CASE_UPPER]);
434 } elsif ($type[$code] eq 'Ll')
436 (hex $fields[$CASE_LOWER] == $code) || die "$raw_code is Ll and UCD_Lower($raw_code) != $raw_code";
438 &add_special_case ($code, $value[$code], $fields[$CASE_UPPER], $fields[$CASE_TITLE]);
440 printf STDERR "Special case for non-alphabetic code point: $raw_code\n";
447 open (INPUT, "< $casefoldingtxt") || exit 1;
463 @fields = split ('\s*;\s*', $_, 30);
465 $raw_code = $fields[$FOLDING_CODE];
466 $code = hex ($raw_code);
470 printf STDERR ("Entry for $raw_code has wrong number of fields (%d)\n", $#fields);
474 # we don't use Simple or Turkic rules here
475 next if ($fields[$FOLDING_STATUS] =~ /^[ST]$/);
477 @values = map { hex ($_) } split /\s+/, $fields[$FOLDING_MAPPING];
482 !(defined $value[$code] && $value[$code] >= 0x1000000) &&
483 defined $type[$code]) {
486 if ($type[$code] eq 'Ll')
489 } elsif ($type[$code] eq 'Lt')
491 $lower = $title_to_lower{$code};
492 } elsif ($type[$code] eq 'Lu')
494 $lower = $value[$code];
499 if ($lower == $values[0]) {
504 my $string = pack ("U*", @values);
506 if (1 + &length_in_bytes ($string) > $casefoldlen) {
507 $casefoldlen = 1 + &length_in_bytes ($string);
510 push @casefold, [ $code, &escape ($string) ];
515 print "Reading scripts\n";
517 open (INPUT, "< $scriptstxt") || exit 1;
522 if (!/^([0-9A-F]+)(?:\.\.([0-9A-F]+))?\s*;\s*([A-Za-z_]+)\s*$/) {
523 die "Cannot parse line: '$_'\n";
527 push @scripts, [ hex $1, hex $2, uc $3 ];
529 push @scripts, [ hex $1, hex $1, uc $3 ];
535 print "Reading derived east asian widths\n";
537 open (INPUT, "< $derivedeastasianwidthtxt") || exit 1;
541 my ($start_code, $end_code);
547 if (!/^([0-9A-F]+)(?:\.\.([0-9A-F]+))?\s*;\s*([A-Za-z_]+)\s*$/) {
548 die "Cannot parse line: '$_'\n";
552 push @eawidths, [ hex $1, hex $2, $3 ];
554 push @eawidths, [ hex $1, hex $1, $3 ];
561 &print_tables ($last_code)
564 &print_decomp ($last_code);
565 &output_composition_table;
567 &print_line_break ($last_code);
576 # perl "length" returns the length in characters
581 return length $string;
584 # Process a single character.
587 my ($code, @fields) = @_;
589 $type[$code] = $fields[$CATEGORY];
590 if ($type[$code] eq 'Nd')
592 $value[$code] = int ($fields[$DECIMAL_VALUE]);
594 elsif ($type[$code] eq 'Ll')
596 $value[$code] = hex ($fields[$UPPER]);
598 elsif ($type[$code] eq 'Lu')
600 $value[$code] = hex ($fields[$LOWER]);
603 if ($type[$code] eq 'Lt')
605 $title_to_lower{$code} = hex ($fields[$LOWER]);
606 $title_to_upper{$code} = hex ($fields[$UPPER]);
609 $cclass[$code] = $fields[$COMBINING_CLASSES];
611 # Handle decompositions.
612 if ($fields[$DECOMPOSITION] ne '')
614 if ($fields[$DECOMPOSITION] =~ s/\<.*\>\s*//) {
615 $decompose_compat[$code] = 1;
617 $decompose_compat[$code] = 0;
619 if (!exists $composition_exclusions{$code}) {
620 $compositions{$code} = $fields[$DECOMPOSITION];
623 $decompositions[$code] = $fields[$DECOMPOSITION];
630 my ($outfile) = "gunichartables.h";
632 local ($bytes_out) = 0;
634 print "Writing $outfile...\n";
636 open (OUT, "> $outfile");
638 print OUT "/* This file is automatically generated. DO NOT EDIT!\n";
639 print OUT " Instead, edit gen-unicode-tables.pl and re-run. */\n\n";
641 print OUT "#ifndef CHARTABLES_H\n";
642 print OUT "#define CHARTABLES_H\n\n";
644 print OUT "#define G_UNICODE_DATA_VERSION \"$ARGV[0]\"\n\n";
646 printf OUT "#define G_UNICODE_LAST_CHAR 0x%04x\n\n", $last;
648 printf OUT "#define G_UNICODE_MAX_TABLE_INDEX 10000\n\n";
650 my $last_part1 = ($pages_before_e0000 * 256) - 1;
651 printf OUT "#define G_UNICODE_LAST_CHAR_PART1 0x%04X\n\n", $last_part1;
652 printf OUT "#define G_UNICODE_LAST_PAGE_PART1 %d\n\n", $pages_before_e0000 - 1;
655 printf OUT "static const char type_data[][256] = {\n";
656 for ($count = 0; $count <= $last; $count += 256)
658 $row[$count / 256] = &print_row ($count, 1, \&fetch_type);
660 printf OUT "\n};\n\n";
662 printf OUT "/* U+0000 through U+%04X */\n", $last_part1;
663 print OUT "static const gint16 type_table_part1[$pages_before_e0000] = {\n";
664 for ($count = 0; $count <= $last_part1; $count += 256)
666 print OUT ",\n" if $count > 0;
667 print OUT " ", $row[$count / 256];
670 print OUT "\n};\n\n";
672 printf OUT "/* U+E0000 through U+%04X */\n", $last;
673 print OUT "static const gint16 type_table_part2[768] = {\n";
674 for ($count = 0xE0000; $count <= $last; $count += 256)
676 print OUT ",\n" if $count > 0xE0000;
677 print OUT " ", $row[$count / 256];
680 print OUT "\n};\n\n";
684 # Now print attribute table.
688 printf OUT "static const gunichar attr_data[][256] = {\n";
689 for ($count = 0; $count <= $last; $count += 256)
691 $row[$count / 256] = &print_row ($count, 4, \&fetch_attr);
693 printf OUT "\n};\n\n";
695 printf OUT "/* U+0000 through U+%04X */\n", $last_part1;
696 print OUT "static const gint16 attr_table_part1[$pages_before_e0000] = {\n";
697 for ($count = 0; $count <= $last_part1; $count += 256)
699 print OUT ",\n" if $count > 0;
700 print OUT " ", $row[$count / 256];
703 print OUT "\n};\n\n";
705 printf OUT "/* U+E0000 through U+%04X */\n", $last;
706 print OUT "static const gint16 attr_table_part2[768] = {\n";
707 for ($count = 0xE0000; $count <= $last; $count += 256)
709 print OUT ",\n" if $count > 0xE0000;
710 print OUT " ", $row[$count / 256];
713 print OUT "\n};\n\n";
716 # print title case table
719 print OUT "static const gunichar title_table[][3] = {\n";
722 foreach $item (sort keys %title_to_lower)
727 printf OUT " { 0x%04x, 0x%04x, 0x%04x }", $item, $title_to_upper{$item}, $title_to_lower{$item};
730 print OUT "\n};\n\n";
733 # And special case conversion table -- conversions that change length
735 &output_special_case_table (\*OUT);
736 &output_casefold_table (\*OUT);
739 # And the widths tables
741 &output_width_tables (\*OUT);
743 print OUT "#endif /* CHARTABLES_H */\n";
747 printf STDERR "Generated %d bytes in tables\n", $bytes_out;
750 # A fetch function for the type table.
754 return $mappings{$type[$index]};
757 # A fetch function for the attribute table.
761 if (defined $value[$index])
763 return sprintf ("0x%04x", $value[$index]);
773 my ($start, $typsize, $fetcher) = @_;
780 for ($off = 0; $off < 256; ++$off)
782 $values[$off] = $fetcher->($off + $start);
783 if ($values[$off] ne $values[0])
790 return $values[0] . " + G_UNICODE_MAX_TABLE_INDEX";
793 printf OUT ",\n" if ($table_index != 0);
794 printf OUT " { /* page %d, index %d */\n ", $start / 256, $table_index;
796 for ($i = $start; $i < $start + 256; ++$i)
800 my ($text) = $values[$i - $start];
801 if (length ($text) + $column + 2 > 78)
811 $column += length ($text) + 2;
815 $bytes_out += 256 * $typsize;
817 return sprintf "%d /* page %d */", $table_index++, $start / 256;
824 my $escaped = unpack("H*", $string);
825 $escaped =~ s/(.{2})/\\x$1/g;
830 # Returns the offset of $decomp in the offset string. Updates the
831 # referenced variables as appropriate.
832 sub handle_decomp ($$$$)
834 my ($decomp, $decomp_offsets_ref, $decomp_string_ref, $decomp_string_offset_ref) = @_;
835 my $offset = "G_UNICODE_NOT_PRESENT_OFFSET";
839 if (defined $decomp_offsets_ref->{$decomp})
841 $offset = $decomp_offsets_ref->{$decomp};
845 $offset = ${$decomp_string_offset_ref};
846 $decomp_offsets_ref->{$decomp} = $offset;
847 ${$decomp_string_ref} .= "\n \"" . &escape ($decomp) . "\\0\" /* offset ${$decomp_string_offset_ref} */";
848 ${$decomp_string_offset_ref} += &length_in_bytes ($decomp) + 1;
855 # Generate the character decomposition header.
859 my ($outfile) = "gunidecomp.h";
861 local ($bytes_out) = 0;
863 print "Writing $outfile...\n";
865 open (OUT, "> $outfile") || exit 1;
867 print OUT "/* This file is automatically generated. DO NOT EDIT! */\n\n";
868 print OUT "#ifndef DECOMP_H\n";
869 print OUT "#define DECOMP_H\n\n";
871 printf OUT "#define G_UNICODE_LAST_CHAR 0x%04x\n\n", $last;
873 printf OUT "#define G_UNICODE_MAX_TABLE_INDEX (0x110000 / 256)\n\n";
875 my $last_part1 = ($pages_before_e0000 * 256) - 1;
876 printf OUT "#define G_UNICODE_LAST_CHAR_PART1 0x%04X\n\n", $last_part1;
877 printf OUT "#define G_UNICODE_LAST_PAGE_PART1 %d\n\n", $pages_before_e0000 - 1;
879 $NOT_PRESENT_OFFSET = 65535;
880 print OUT "#define G_UNICODE_NOT_PRESENT_OFFSET $NOT_PRESENT_OFFSET\n\n";
884 printf OUT "static const guchar cclass_data[][256] = {\n";
885 for ($count = 0; $count <= $last; $count += 256)
887 $row[$count / 256] = &print_row ($count, 1, \&fetch_cclass);
889 printf OUT "\n};\n\n";
891 print OUT "static const gint16 combining_class_table_part1[$pages_before_e0000] = {\n";
892 for ($count = 0; $count <= $last_part1; $count += 256)
894 print OUT ",\n" if $count > 0;
895 print OUT " ", $row[$count / 256];
898 print OUT "\n};\n\n";
900 print OUT "static const gint16 combining_class_table_part2[768] = {\n";
901 for ($count = 0xE0000; $count <= $last; $count += 256)
903 print OUT ",\n" if $count > 0xE0000;
904 print OUT " ", $row[$count / 256];
907 print OUT "\n};\n\n";
909 print OUT "typedef struct\n{\n";
910 print OUT " gunichar ch;\n";
911 print OUT " guint16 canon_offset;\n";
912 print OUT " guint16 compat_offset;\n";
913 print OUT "} decomposition;\n\n";
915 print OUT "static const decomposition decomp_table[] =\n{\n";
918 my ($decomp_string) = "";
919 my ($decomp_string_offset) = 0;
920 for ($count = 0; $count <= $last; ++$count)
922 if (defined $decompositions[$count])
931 if (!$decompose_compat[$count]) {
932 $canon_decomp = make_decomp ($count, 0);
934 $compat_decomp = make_decomp ($count, 1);
936 if (defined $canon_decomp && $compat_decomp eq $canon_decomp) {
937 undef $compat_decomp;
940 my $canon_offset = handle_decomp ($canon_decomp, \%decomp_offsets, \$decomp_string, \$decomp_string_offset);
941 my $compat_offset = handle_decomp ($compat_decomp, \%decomp_offsets, \$decomp_string, \$decomp_string_offset);
943 die if $decomp_string_offset > $NOT_PRESENT_OFFSET;
945 printf OUT qq( { 0x%04x, $canon_offset, $compat_offset }), $count;
949 print OUT "\n};\n\n";
950 $bytes_out += $decomp_string_offset + 1;
952 printf OUT "static const gchar decomp_expansion_string[] = %s;\n\n", $decomp_string;
954 print OUT "typedef struct\n{\n";
955 print OUT " gunichar ch;\n";
956 print OUT " gunichar a;\n";
957 print OUT " gunichar b;\n";
958 print OUT "} decomposition_step;\n\n";
960 # There's lots of room to optimize the following table...
961 print OUT "static const decomposition_step decomp_step_table[] =\n{\n";
964 for ($count = 0; $count <= $last; ++$count)
966 if ((defined $decompositions[$count]) && (!$decompose_compat[$count]))
972 @list = (split(' ', $decompositions[$count]), "0");
973 printf OUT qq( { 0x%05x, 0x%05x, 0x%05x }), $count, hex($list[0]), hex($list[1]);
974 # don't include 1:1 in the compose table
975 push @steps, [ ($count, hex($list[0]), hex($list[1])) ]
979 print OUT "\n};\n\n";
981 print OUT "#endif /* DECOMP_H */\n";
983 printf STDERR "Generated %d bytes in decomp tables\n", $bytes_out;
989 my ($outfile) = "gunibreak.h";
991 local ($bytes_out) = 0;
993 print "Writing $outfile...\n";
995 open (OUT, "> $outfile");
997 print OUT "/* This file is automatically generated. DO NOT EDIT!\n";
998 print OUT " Instead, edit gen-unicode-tables.pl and re-run. */\n\n";
1000 print OUT "#ifndef BREAKTABLES_H\n";
1001 print OUT "#define BREAKTABLES_H\n\n";
1003 print OUT "#include <glib/gtypes.h>\n";
1004 print OUT "#include <glib/gunicode.h>\n\n";
1006 print OUT "#define G_UNICODE_DATA_VERSION \"$ARGV[0]\"\n\n";
1008 printf OUT "#define G_UNICODE_LAST_CHAR 0x%04X\n\n", $last;
1010 printf OUT "#define G_UNICODE_MAX_TABLE_INDEX 10000\n\n";
1012 my $last_part1 = ($pages_before_e0000 * 256) - 1;
1013 printf OUT "/* the last code point that should be looked up in break_property_table_part1 */\n";
1014 printf OUT "#define G_UNICODE_LAST_CHAR_PART1 0x%04X\n\n", $last_part1;
1017 printf OUT "static const gint8 break_property_data[][256] = {\n";
1018 for ($count = 0; $count <= $last; $count += 256)
1020 $row[$count / 256] = &print_row ($count, 1, \&fetch_break_type);
1022 printf OUT "\n};\n\n";
1024 printf OUT "/* U+0000 through U+%04X */\n", $last_part1;
1025 print OUT "static const gint16 break_property_table_part1[$pages_before_e0000] = {\n";
1026 for ($count = 0; $count <= $last_part1; $count += 256)
1028 print OUT ",\n" if $count > 0;
1029 print OUT " ", $row[$count / 256];
1032 print OUT "\n};\n\n";
1034 printf OUT "/* U+E0000 through U+%04X */\n", $last;
1035 print OUT "static const gint16 break_property_table_part2[768] = {\n";
1036 for ($count = 0xE0000; $count <= $last; $count += 256)
1038 print OUT ",\n" if $count > 0xE0000;
1039 print OUT " ", $row[$count / 256];
1042 print OUT "\n};\n\n";
1045 print OUT "#endif /* BREAKTABLES_H */\n";
1049 printf STDERR "Generated %d bytes in break tables\n", $bytes_out;
1053 # A fetch function for the break properties table.
1054 sub fetch_break_type
1057 return $break_mappings{$break_props[$index]};
1060 # Fetcher for combining class.
1067 # Expand a character decomposition recursively.
1070 my ($code, $compat) = @_;
1074 foreach $iter (split (' ', $decompositions[$code]))
1077 if (defined $decompositions[$val] &&
1078 ($compat || !$decompose_compat[$val]))
1080 push (@result, &expand_decomp ($val, $compat));
1084 push (@result, $val);
1093 my ($code, $compat) = @_;
1096 foreach $iter (&expand_decomp ($code, $compat))
1098 $result .= pack ("U", $iter); # to utf-8
1103 # Generate special case data string from two fields
1104 sub add_special_case
1106 my ($code, $single, $field1, $field2) = @_;
1108 @values = (defined $single ? $single : (),
1109 (map { hex ($_) } split /\s+/, $field1),
1111 (map { hex ($_) } split /\s+/, $field2));
1115 for $value (@values) {
1116 $result .= pack ("U", $value); # to utf-8
1119 push @special_case_offsets, $special_case_offset;
1121 # We encode special cases up in the 0x1000000 space
1122 $value[$code] = 0x1000000 + $special_case_offset;
1124 $special_case_offset += 1 + &length_in_bytes ($result);
1126 push @special_cases, &escape ($result);
1129 sub output_special_case_table
1135 /* Table of special cases for case conversion; each record contains
1136 * First, the best single character mapping to lowercase if Lu,
1137 * and to uppercase if Ll, followed by the output mapping for the two cases
1138 * other than the case of the codepoint, in the order [Ll],[Lu],[Lt],
1139 * encoded in UTF-8, separated and terminated by a null character.
1141 static const gchar special_case_table[] = {
1145 for $case (@special_cases) {
1146 print $out qq( "$case\\0" /* offset ${special_case_offsets[$i]} */\n);
1155 print STDERR "Generated " . ($special_case_offset + 1) . " bytes in special case table\n";
1158 sub enumerate_ordered
1163 for my $code (sort { $a <=> $b } keys %$array) {
1164 if ($array->{$code} == 1) {
1165 delete $array->{$code};
1168 $array->{$code} = $n++;
1174 sub output_composition_table
1176 print STDERR "Generating composition table\n";
1178 local ($bytes_out) = 0;
1183 # First we need to go through and remove decompositions
1184 # starting with a non-starter, and single-character
1185 # decompositions. At the same time, record
1186 # the first and second character of each decomposition
1188 for $code (keys %compositions)
1190 @values = map { hex ($_) } split /\s+/, $compositions{$code};
1193 if ($cclass[$code]) {
1194 delete $compositions{$code};
1197 if ($cclass[$values[0]]) {
1198 delete $compositions{$code};
1202 # single-character decompositions
1204 delete $compositions{$code};
1209 die "$code has more than two elements in its decomposition!\n";
1212 if (exists $first{$values[0]}) {
1213 $first{$values[0]}++;
1215 $first{$values[0]} = 1;
1219 # Assign integer indices, removing singletons
1220 my $n_first = enumerate_ordered (\%first);
1222 # Now record the second character of each (non-singleton) decomposition
1223 for $code (keys %compositions) {
1224 @values = map { hex ($_) } split /\s+/, $compositions{$code};
1226 if (exists $first{$values[0]}) {
1227 if (exists $second{$values[1]}) {
1228 $second{$values[1]}++;
1230 $second{$values[1]} = 1;
1235 # Assign integer indices, removing duplicate
1236 my $n_second = enumerate_ordered (\%second);
1238 # Build reverse table
1240 my @first_singletons;
1241 my @second_singletons;
1243 for $code (keys %compositions) {
1244 @values = map { hex ($_) } split /\s+/, $compositions{$code};
1246 my $first = $first{$values[0]};
1247 my $second = $second{$values[1]};
1249 if (defined $first && defined $second) {
1250 $reverse{"$first|$second"} = $code;
1251 } elsif (!defined $first) {
1252 push @first_singletons, [ $values[0], $values[1], $code ];
1254 push @second_singletons, [ $values[1], $values[0], $code ];
1258 @first_singletons = sort { $a->[0] <=> $b->[0] } @first_singletons;
1259 @second_singletons = sort { $a->[0] <=> $b->[0] } @second_singletons;
1263 open OUT, ">gunicomp.h" or die "Cannot open gunicomp.h: $!\n";
1265 # Assign values in lookup table for all code points involved
1269 printf OUT "#define COMPOSE_FIRST_START %d\n", $total;
1270 for $code (keys %first) {
1271 $vals{$code} = $first{$code} + $total;
1272 $last = $code if $code > $last;
1276 printf OUT "#define COMPOSE_FIRST_SINGLE_START %d\n", $total;
1277 for $record (@first_singletons) {
1278 my $code = $record->[0];
1279 $vals{$code} = $i++ + $total;
1280 $last = $code if $code > $last;
1282 $total += @first_singletons;
1283 printf OUT "#define COMPOSE_SECOND_START %d\n", $total;
1284 for $code (keys %second) {
1285 $vals{$code} = $second{$code} + $total;
1286 $last = $code if $code > $last;
1288 $total += $n_second;
1290 printf OUT "#define COMPOSE_SECOND_SINGLE_START %d\n\n", $total;
1291 for $record (@second_singletons) {
1292 my $code = $record->[0];
1293 $vals{$code} = $i++ + $total;
1294 $last = $code if $code > $last;
1297 printf OUT "#define COMPOSE_TABLE_LAST %d\n\n", $last / 256;
1299 # Output lookup table
1303 printf OUT "static const guint16 compose_data[][256] = {\n";
1304 for (my $count = 0; $count <= $last; $count += 256)
1306 $row[$count / 256] = &print_row ($count, 2, sub { exists $vals{$_[0]} ? $vals{$_[0]} : 0; });
1308 printf OUT "\n};\n\n";
1310 print OUT "static const gint16 compose_table[COMPOSE_TABLE_LAST + 1] = {\n";
1311 for (my $count = 0; $count <= $last; $count += 256)
1313 print OUT ",\n" if $count > 0;
1314 print OUT " ", $row[$count / 256];
1317 print OUT "\n};\n\n";
1319 # Output first singletons
1321 print OUT "static const gunichar compose_first_single[][2] = {\n";
1323 for $record (@first_singletons) {
1324 print OUT ",\n" if $i++ > 0;
1325 printf OUT " { %#06x, %#06x }", $record->[1], $record->[2];
1329 $bytes_out += @first_singletons * 4;
1331 # Output second singletons
1333 print OUT "static const gunichar compose_second_single[][2] = {\n";
1335 for $record (@second_singletons) {
1336 print OUT ",\n" if $i++ > 0;
1337 printf OUT " { %#06x, %#06x }", $record->[1], $record->[2];
1341 $bytes_out += @second_singletons * 4;
1343 # Output array of composition pairs
1346 static const guint16 compose_array[$n_first][$n_second] = {
1349 for (my $i = 0; $i < $n_first; $i++) {
1350 print OUT ",\n" if $i;
1352 for (my $j = 0; $j < $n_second; $j++) {
1353 print OUT ", " if $j;
1354 if (exists $reverse{"$i|$j"}) {
1355 if ($reverse{"$i|$j"} > 0xFFFF) {
1356 die "time to switch compose_array to gunichar" ;
1358 printf OUT "0x%04x", $reverse{"$i|$j"};
1371 $bytes_out += $n_first * $n_second * 2;
1373 printf STDERR "Generated %d bytes in compose tables\n", $bytes_out;
1376 sub output_casefold_table
1382 /* Table of casefolding cases that can't be derived by lowercasing
1384 static const struct {
1386 gchar data[$casefoldlen];
1387 } casefold_table[] = {
1390 @casefold = sort { $a->[0] <=> $b->[0] } @casefold;
1392 for $case (@casefold)
1395 $string = $case->[1];
1397 if ($code > 0xFFFF) {
1398 die "time to switch casefold_table to gunichar" ;
1401 print $out sprintf(qq( { 0x%04x, "$string" },\n), $code);
1410 my $recordlen = (2+$casefoldlen+1) & ~1;
1411 printf "Generated %d bytes for casefold table\n", $recordlen * @casefold;
1414 sub output_one_width_table
1416 my ($out, $name, $wpe) = @_;
1422 print $out "static const struct Interval g_unicode_width_table_${name}[] = {\n";
1426 for (my $i = 0; $i <= $#eawidths; $i++) {
1427 $start = $eawidths[$i]->[0];
1428 $end = $eawidths[$i]->[1];
1429 $wp = $eawidths[$i]->[2];
1431 next if ($wp !~ $rex);
1433 while ($i <= $#eawidths - 1 &&
1434 $eawidths[$i + 1]->[0] == $end + 1 &&
1435 ($eawidths[$i + 1]->[2] =~ $rex)) {
1437 $end = $eawidths[$i]->[1];
1440 printf $out "{0x%04X, 0x%04X},\n", $start, $end;
1443 printf $out "};\n\n";
1446 sub output_width_tables
1450 @eawidths = sort { $a->[0] <=> $b->[0] } @eawidths;
1456 gunichar start, end;
1461 &output_one_width_table ($out,"wide", "[FW]");
1462 &output_one_width_table ($out, "ambiguous", "[A]");
1473 print STDERR "Writing gscripttable.h\n";
1475 open OUT, ">gscripttable.h" or die "Cannot open gscripttable.h: $!\n";
1478 /* This file is automatically generated. DO NOT EDIT!
1479 Instead, edit gen-unicode-tables.pl and re-run. */
1481 #ifndef SCRIPTTABLES_H
1482 #define SCRIPTTABLES_H
1486 @scripts = sort { $a->[0] <=> $b->[0] } @scripts;
1488 $easy_range = 0x2000;
1491 #define G_EASY_SCRIPTS_RANGE $easy_range
1493 static const guchar g_script_easy_table[$easy_range] = {
1499 for (my $c = 0; $c < $easy_range; $c++) {
1506 $start = $scripts[$i]->[0];
1507 $end = $scripts[$i]->[1];
1508 $script = $scripts[$i]->[2];
1513 printf OUT " G_UNICODE_SCRIPT_UNKNOWN,";
1515 printf OUT " G_UNICODE_SCRIPT_%s,", $script;
1519 if ($end >= $easy_range) {
1521 $scripts[$i]->[0] = $easy_range;
1528 static const struct {
1532 } g_script_table[] = {
1535 for (; $i <= $#scripts; $i++) {
1536 $start = $scripts[$i]->[0];
1537 $end = $scripts[$i]->[1];
1538 $script = $scripts[$i]->[2];
1540 while ($i <= $#scripts - 1 &&
1541 $scripts[$i + 1]->[0] == $end + 1 &&
1542 $scripts[$i + 1]->[2] eq $script) {
1544 $end = $scripts[$i]->[1];
1546 printf OUT " { %#06x, %5d, G_UNICODE_SCRIPT_%s },\n", $start, $end - $start + 1, $script;
1552 #endif /* SCRIPTTABLES_H */