3 # Copyright (C) 2004, 2005 Simon Josefsson
4 # Copyright (C) 1998, 1999 Tom Tromey
5 # Copyright (C) 2001 Red Hat Software
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2, or (at your option)
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, you can either send email to this
19 # program's maintainer or write to: The Free Software Foundation,
20 # Inc.; 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 # Andrew Taylor <andrew.taylor@montage.ca>
25 # gen-unicode-tables.pl - Generate tables for libunicode from Unicode data.
26 # See http://www.unicode.org/Public/UNIDATA/UnicodeCharacterDatabase.html
27 # I consider the output of this program to be unrestricted. Use it as
31 # * For decomp table it might make sense to use a shift count other
32 # than 8. We could easily compute the perfect shift count.
34 # we use some perl unicode features
37 use vars
qw($CODE $NAME $CATEGORY $COMBINING_CLASSES $BIDI_CATEGORY $DECOMPOSITION $DECIMAL_VALUE $DIGIT_VALUE $NUMERIC_VALUE $MIRRORED $OLD_NAME $COMMENT $UPPER $LOWER $TITLE $BREAK_CODE $BREAK_CATEGORY $BREAK_NAME $CASE_CODE $CASE_LOWER $CASE_TITLE $CASE_UPPER $CASE_CONDITION);
40 # Names of fields in Unicode data table.
44 $COMBINING_CLASSES = 3;
57 # Names of fields in the line break table
61 # Names of fields in the SpecialCasing table
68 # Names of fields in the CaseFolding table
73 # Map general category code onto symbolic name.
77 'Lu' => "G_UNICODE_UPPERCASE_LETTER",
78 'Ll' => "G_UNICODE_LOWERCASE_LETTER",
79 'Lt' => "G_UNICODE_TITLECASE_LETTER",
80 'Mn' => "G_UNICODE_NON_SPACING_MARK",
81 'Mc' => "G_UNICODE_COMBINING_MARK",
82 'Me' => "G_UNICODE_ENCLOSING_MARK",
83 'Nd' => "G_UNICODE_DECIMAL_NUMBER",
84 'Nl' => "G_UNICODE_LETTER_NUMBER",
85 'No' => "G_UNICODE_OTHER_NUMBER",
86 'Zs' => "G_UNICODE_SPACE_SEPARATOR",
87 'Zl' => "G_UNICODE_LINE_SEPARATOR",
88 'Zp' => "G_UNICODE_PARAGRAPH_SEPARATOR",
89 'Cc' => "G_UNICODE_CONTROL",
90 'Cf' => "G_UNICODE_FORMAT",
91 'Cs' => "G_UNICODE_SURROGATE",
92 'Co' => "G_UNICODE_PRIVATE_USE",
93 'Cn' => "G_UNICODE_UNASSIGNED",
96 'Lm' => "G_UNICODE_MODIFIER_LETTER",
97 'Lo' => "G_UNICODE_OTHER_LETTER",
98 'Pc' => "G_UNICODE_CONNECT_PUNCTUATION",
99 'Pd' => "G_UNICODE_DASH_PUNCTUATION",
100 'Ps' => "G_UNICODE_OPEN_PUNCTUATION",
101 'Pe' => "G_UNICODE_CLOSE_PUNCTUATION",
102 'Pi' => "G_UNICODE_INITIAL_PUNCTUATION",
103 'Pf' => "G_UNICODE_FINAL_PUNCTUATION",
104 'Po' => "G_UNICODE_OTHER_PUNCTUATION",
105 'Sm' => "G_UNICODE_MATH_SYMBOL",
106 'Sc' => "G_UNICODE_CURRENCY_SYMBOL",
107 'Sk' => "G_UNICODE_MODIFIER_SYMBOL",
108 'So' => "G_UNICODE_OTHER_SYMBOL"
113 'BK' => "G_UNICODE_BREAK_MANDATORY",
114 'CR' => "G_UNICODE_BREAK_CARRIAGE_RETURN",
115 'LF' => "G_UNICODE_BREAK_LINE_FEED",
116 'CM' => "G_UNICODE_BREAK_COMBINING_MARK",
117 'SG' => "G_UNICODE_BREAK_SURROGATE",
118 'ZW' => "G_UNICODE_BREAK_ZERO_WIDTH_SPACE",
119 'IN' => "G_UNICODE_BREAK_INSEPARABLE",
120 'GL' => "G_UNICODE_BREAK_NON_BREAKING_GLUE",
121 'CB' => "G_UNICODE_BREAK_CONTINGENT",
122 'SP' => "G_UNICODE_BREAK_SPACE",
123 'BA' => "G_UNICODE_BREAK_AFTER",
124 'BB' => "G_UNICODE_BREAK_BEFORE",
125 'B2' => "G_UNICODE_BREAK_BEFORE_AND_AFTER",
126 'HY' => "G_UNICODE_BREAK_HYPHEN",
127 'NS' => "G_UNICODE_BREAK_NON_STARTER",
128 'OP' => "G_UNICODE_BREAK_OPEN_PUNCTUATION",
129 'CL' => "G_UNICODE_BREAK_CLOSE_PUNCTUATION",
130 'QU' => "G_UNICODE_BREAK_QUOTATION",
131 'EX' => "G_UNICODE_BREAK_EXCLAMATION",
132 'ID' => "G_UNICODE_BREAK_IDEOGRAPHIC",
133 'NU' => "G_UNICODE_BREAK_NUMERIC",
134 'IS' => "G_UNICODE_BREAK_INFIX_SEPARATOR",
135 'SY' => "G_UNICODE_BREAK_SYMBOL",
136 'AL' => "G_UNICODE_BREAK_ALPHABETIC",
137 'PR' => "G_UNICODE_BREAK_PREFIX",
138 'PO' => "G_UNICODE_BREAK_POSTFIX",
139 'SA' => "G_UNICODE_BREAK_COMPLEX_CONTEXT",
140 'AI' => "G_UNICODE_BREAK_AMBIGUOUS",
141 'NL' => "G_UNICODE_BREAK_NEXT_LINE",
142 'WJ' => "G_UNICODE_BREAK_WORD_JOINER",
143 'XX' => "G_UNICODE_BREAK_UNKNOWN"
146 # Title case mappings.
147 %title_to_lower = ();
148 %title_to_upper = ();
150 # Maximum length of special-case strings
153 my @special_case_offsets;
154 my $special_case_offset = 0;
158 if (@ARGV && $ARGV[0] eq '-decomp')
164 elsif (@ARGV && $ARGV[0] eq '-both')
172 die "\nUsage: $0 [-decomp | -both] UNICODE-VERSION DIRECTORY\n\n DIRECTORY should contain the following Unicode data files:\n UnicodeData.txt, LineBreak.txt, SpecialCasing.txt, CaseFolding.txt,\n CompositionExclusions.txt, BidiMirroring.txt\n\n";
175 my ($unicodedatatxt, $linebreaktxt, $specialcasingtxt, $casefoldingtxt, $compositionexclusionstxt, $bidimirroringtxt);
178 opendir (my $dir, $d) or die "Cannot open Unicode data dir $d: $!\n";
179 for my $f (readdir ($dir))
181 $unicodedatatxt = "$d/$f" if ($f =~ /UnicodeData.*\.txt/);
182 $linebreaktxt = "$d/$f" if ($f =~ /LineBreak.*\.txt/);
183 $specialcasingtxt = "$d/$f" if ($f =~ /SpecialCasing.*\.txt/);
184 $casefoldingtxt = "$d/$f" if ($f =~ /CaseFolding.*\.txt/);
185 $compositionexclusionstxt = "$d/$f" if ($f =~ /CompositionExclusions.*\.txt/);
186 $bidimirroringtxt = "$d/$f" if ($f =~ /BidiMirroring.*\.txt/);
189 defined $unicodedatatxt or die "Did not find UnicodeData file";
190 defined $linebreaktxt or die "Did not find LineBreak file";
191 defined $specialcasingtxt or die "Did not find SpecialCasing file";
192 defined $casefoldingtxt or die "Did not find CaseFolding file";
193 defined $compositionexclusionstxt or die "Did not find CompositionExclusions file";
194 defined $bidimirroringtxt or die "Did not find BidiMirroring file";
196 print "Creating decomp table\n" if ($do_decomp);
197 print "Creating property table\n" if ($do_props);
199 print "Composition exlusions from $compositionexclusionstxt\n";
201 open (INPUT, "< $compositionexclusionstxt") || exit 1;
215 $composition_exclusions{hex($_)} = 1;
220 print "Unicode data from $unicodedatatxt\n";
222 open (INPUT, "< $unicodedatatxt") || exit 1;
224 # we save memory by skipping the huge empty area before U+E0000
225 my $pages_before_e0000;
231 @fields = split (';', $_, 30);
234 printf STDERR ("Entry for $fields[$CODE] has wrong number of fields (%d)\n", $#fields);
237 $code = hex ($fields[$CODE]);
239 if ($code >= 0xE0000 and $last_code < 0xE0000)
241 $pages_before_e0000 = ($last_code >> 8) + 1;
244 if ($code > $last_code + 1)
247 if ($fields[$NAME] =~ /Last>/)
249 # Fill the gap with the last character read,
250 # since this was a range specified in the char database
255 # The gap represents undefined characters. Only the type
257 @gfields = ('', '', 'Cn', '0', '', '', '', '', '', '', '',
260 for (++$last_code; $last_code < $code; ++$last_code)
262 $gfields{$CODE} = sprintf ("%04x", $last_code);
263 &process_one ($last_code, @gfields);
266 &process_one ($code, @fields);
272 @gfields = ('', '', 'Cn', '0', '', '', '', '', '', '', '',
274 for (++$last_code; $last_code <= 0x10FFFF; ++$last_code)
276 $gfields{$CODE} = sprintf ("%04x", $last_code);
277 &process_one ($last_code, @gfields);
279 --$last_code; # Want last to be 0x10FFFF.
281 print "Creating line break table\n";
283 print "Line break data from $linebreaktxt\n";
285 open (INPUT, "< $linebreaktxt") || exit 1;
290 my ($start_code, $end_code);
298 @fields = split (';', $_, 30);
301 printf STDERR ("Entry for $fields[$CODE] has wrong number of fields (%d)\n", $#fields);
305 if ($fields[$CODE] =~ /([A-F0-9]{4,6})\.\.([A-F0-9]{4,6})/)
307 $start_code = hex ($1);
308 $end_code = hex ($2);
310 $start_code = $end_code = hex ($fields[$CODE]);
314 if ($start_code > $last_code + 1)
316 # The gap represents undefined characters. If assigned,
317 # they are AL, if not assigned, XX
318 for (++$last_code; $last_code < $start_code; ++$last_code)
320 if ($type[$last_code] eq 'Cn')
322 $break_props[$last_code] = 'XX';
326 $break_props[$last_code] = 'AL';
331 for ($last_code = $start_code; $last_code <= $end_code; $last_code++)
333 $break_props[$last_code] = $fields[$BREAK_PROPERTY];
336 $last_code = $end_code;
341 for (++$last_code; $last_code <= 0x10FFFF; ++$last_code)
343 if ($type[$last_code] eq 'Cn')
345 $break_props[$last_code] = 'XX';
349 $break_props[$last_code] = 'AL';
352 --$last_code; # Want last to be 0x10FFFF.
354 print STDERR "Last code is not 0x10FFFF" if ($last_code != 0x10FFFF);
356 print "Reading special-casing table for case conversion\n";
358 open (INPUT, "< $specialcasingtxt") || exit 1;
371 @fields = split ('\s*;\s*', $_, 30);
373 $raw_code = $fields[$CASE_CODE];
374 $code = hex ($raw_code);
376 if ($#fields != 4 && $#fields != 5)
378 printf STDERR ("Entry for $raw_code has wrong number of fields (%d)\n", $#fields);
382 if (!defined $type[$code])
384 printf STDERR "Special case for code point: $code, which has no defined type\n";
388 if (defined $fields[5]) {
389 # Ignore conditional special cases - we'll handle them in code
393 if ($type[$code] eq 'Lu')
395 (hex $fields[$CASE_UPPER] == $code) || die "$raw_code is Lu and UCD_Upper($raw_code) != $raw_code";
397 &add_special_case ($code, $value[$code], $fields[$CASE_LOWER], $fields[$CASE_TITLE]);
399 } elsif ($type[$code] eq 'Lt')
401 (hex $fields[$CASE_TITLE] == $code) || die "$raw_code is Lt and UCD_Title($raw_code) != $raw_code";
403 &add_special_case ($code, undef, $fields[$CASE_LOWER], $fields[$CASE_UPPER]);
404 } elsif ($type[$code] eq 'Ll')
406 (hex $fields[$CASE_LOWER] == $code) || die "$raw_code is Ll and UCD_Lower($raw_code) != $raw_code";
408 &add_special_case ($code, $value[$code], $fields[$CASE_UPPER], $fields[$CASE_TITLE]);
410 printf STDERR "Special case for non-alphabetic code point: $raw_code\n";
417 open (INPUT, "< $casefoldingtxt") || exit 1;
433 @fields = split ('\s*;\s*', $_, 30);
435 $raw_code = $fields[$FOLDING_CODE];
436 $code = hex ($raw_code);
440 printf STDERR ("Entry for $raw_code has wrong number of fields (%d)\n", $#fields);
444 # we don't use Simple or Turkic rules here
445 next if ($fields[$FOLDING_STATUS] =~ /^[ST]$/);
447 @values = map { hex ($_) } split /\s+/, $fields[$FOLDING_MAPPING];
452 !(defined $value[$code] && $value[$code] >= 0x1000000) &&
453 defined $type[$code]) {
456 if ($type[$code] eq 'Ll')
459 } elsif ($type[$code] eq 'Lt')
461 $lower = $title_to_lower{$code};
462 } elsif ($type[$code] eq 'Lu')
464 $lower = $value[$code];
469 if ($lower == $values[0]) {
474 my $string = pack ("U*", @values);
476 if (1 + &length_in_bytes ($string) > $casefoldlen) {
477 $casefoldlen = 1 + &length_in_bytes ($string);
480 push @casefold, [ $code, &escape ($string) ];
485 open (INPUT, "< $bidimirroringtxt") || exit 1;
497 @fields = split ('\s*;\s*', $_, 30);
499 push @bidimirror, [hex ($fields[0]), hex ($fields[1])];
503 &print_tables ($last_code)
506 &print_decomp ($last_code);
507 &output_composition_table;
510 &print_line_break ($last_code);
515 # perl "length" returns the length in characters
521 return length $string;
524 # Process a single character.
527 my ($code, @fields) = @_;
529 $type[$code] = $fields[$CATEGORY];
530 if ($type[$code] eq 'Nd')
532 $value[$code] = int ($fields[$DECIMAL_VALUE]);
534 elsif ($type[$code] eq 'Ll')
536 $value[$code] = hex ($fields[$UPPER]);
538 elsif ($type[$code] eq 'Lu')
540 $value[$code] = hex ($fields[$LOWER]);
543 if ($type[$code] eq 'Lt')
545 $title_to_lower{$code} = hex ($fields[$LOWER]);
546 $title_to_upper{$code} = hex ($fields[$UPPER]);
549 $cclass[$code] = $fields[$COMBINING_CLASSES];
551 # Handle decompositions.
552 if ($fields[$DECOMPOSITION] ne '')
554 if ($fields[$DECOMPOSITION] =~ s/\<.*\>\s*//) {
555 $decompose_compat[$code] = 1;
557 $decompose_compat[$code] = 0;
559 if (!exists $composition_exclusions{$code}) {
560 $compositions{$code} = $fields[$DECOMPOSITION];
563 $decompositions[$code] = $fields[$DECOMPOSITION];
570 my ($outfile) = "gunichartables.h";
572 local ($bytes_out) = 0;
574 print "Writing $outfile...\n";
576 open (OUT, "> $outfile");
578 print OUT "/* This file is automatically generated. DO NOT EDIT!\n";
579 print OUT " Instead, edit gen-unicode-tables.pl and re-run. */\n\n";
581 print OUT "#ifndef CHARTABLES_H\n";
582 print OUT "#define CHARTABLES_H\n\n";
584 print OUT "#define G_UNICODE_DATA_VERSION \"$ARGV[0]\"\n\n";
586 printf OUT "#define G_UNICODE_LAST_CHAR 0x%04x\n\n", $last;
588 printf OUT "#define G_UNICODE_MAX_TABLE_INDEX 10000\n\n";
590 my $last_part1 = ($pages_before_e0000 * 256) - 1;
591 printf OUT "#define G_UNICODE_LAST_CHAR_PART1 0x%04X\n\n", $last_part1;
592 printf OUT "#define G_UNICODE_LAST_PAGE_PART1 %d\n\n", $pages_before_e0000 - 1;
595 printf OUT "static const char type_data[][256] = {\n";
596 for ($count = 0; $count <= $last; $count += 256)
598 $row[$count / 256] = &print_row ($count, 1, \&fetch_type);
600 printf OUT "\n};\n\n";
602 printf OUT "/* U+0000 through U+%04X */\n", $last_part1;
603 print OUT "static const gint16 type_table_part1[$pages_before_e0000] = {\n";
604 for ($count = 0; $count <= $last_part1; $count += 256)
606 print OUT ",\n" if $count > 0;
607 print OUT " ", $row[$count / 256];
610 print OUT "\n};\n\n";
612 printf OUT "/* U+E0000 through U+%04X */\n", $last;
613 print OUT "static const gint16 type_table_part2[768] = {\n";
614 for ($count = 0xE0000; $count <= $last; $count += 256)
616 print OUT ",\n" if $count > 0xE0000;
617 print OUT " ", $row[$count / 256];
620 print OUT "\n};\n\n";
624 # Now print attribute table.
628 printf OUT "static const gunichar attr_data[][256] = {\n";
629 for ($count = 0; $count <= $last; $count += 256)
631 $row[$count / 256] = &print_row ($count, 4, \&fetch_attr);
633 printf OUT "\n};\n\n";
635 printf OUT "/* U+0000 through U+%04X */\n", $last_part1;
636 print OUT "static const gint16 attr_table_part1[$pages_before_e0000] = {\n";
637 for ($count = 0; $count <= $last_part1; $count += 256)
639 print OUT ",\n" if $count > 0;
640 print OUT " ", $row[$count / 256];
643 print OUT "\n};\n\n";
645 printf OUT "/* U+E0000 through U+%04X */\n", $last;
646 print OUT "static const gint16 attr_table_part2[768] = {\n";
647 for ($count = 0xE0000; $count <= $last; $count += 256)
649 print OUT ",\n" if $count > 0xE0000;
650 print OUT " ", $row[$count / 256];
653 print OUT "\n};\n\n";
656 # print title case table
659 print OUT "static const gunichar title_table[][3] = {\n";
662 foreach $item (sort keys %title_to_lower)
667 printf OUT " { 0x%04x, 0x%04x, 0x%04x }", $item, $title_to_upper{$item}, $title_to_lower{$item};
670 print OUT "\n};\n\n";
673 # And special case conversion table -- conversions that change length
675 &output_special_case_table (\*OUT);
676 &output_casefold_table (\*OUT);
678 print OUT "static const struct {\n";
679 print OUT " gunichar ch;\n";
680 print OUT " gunichar mirrored_ch;\n";
681 print OUT "} bidi_mirroring_table[] =\n";
684 foreach $item (@bidimirror)
686 print OUT ",\n" unless $first;
688 printf OUT " { 0x%04x, 0x%04x }", $item->[0], $item->[1];
691 print OUT "\n};\n\n";
693 print OUT "#endif /* CHARTABLES_H */\n";
697 printf STDERR "Generated %d bytes in tables\n", $bytes_out;
700 # A fetch function for the type table.
704 return $mappings{$type[$index]};
707 # A fetch function for the attribute table.
711 if (defined $value[$index])
713 return sprintf ("0x%04x", $value[$index]);
723 my ($start, $typsize, $fetcher) = @_;
730 for ($off = 0; $off < 256; ++$off)
732 $values[$off] = $fetcher->($off + $start);
733 if ($values[$off] ne $values[0])
740 return $values[0] . " + G_UNICODE_MAX_TABLE_INDEX";
743 printf OUT ",\n" if ($table_index != 0);
744 printf OUT " { /* page %d, index %d */\n ", $start / 256, $table_index;
746 for ($i = $start; $i < $start + 256; ++$i)
750 my ($text) = $values[$i - $start];
751 if (length ($text) + $column + 2 > 78)
757 $column += length ($text) + 2;
761 $bytes_out += 256 * $typsize;
763 return sprintf "%d /* page %d */", $table_index++, $start / 256;
770 my $escaped = unpack("H*", $string);
771 $escaped =~ s/(.{2})/\\x$1/g;
776 # Returns the offset of $decomp in the offset string. Updates the
777 # referenced variables as appropriate.
778 sub handle_decomp ($$$$)
780 my ($decomp, $decomp_offsets_ref, $decomp_string_ref, $decomp_string_offset_ref) = @_;
781 my $offset = "G_UNICODE_NOT_PRESENT_OFFSET";
785 if (defined $decomp_offsets_ref->{$decomp})
787 $offset = $decomp_offsets_ref->{$decomp};
791 $offset = ${$decomp_string_offset_ref};
792 $decomp_offsets_ref->{$decomp} = $offset;
793 ${$decomp_string_ref} .= "\n \"" . &escape ($decomp) . "\\0\" /* offset ${$decomp_string_offset_ref} */";
794 ${$decomp_string_offset_ref} += &length_in_bytes ($decomp) + 1;
801 # Generate the character decomposition header.
805 my ($outfile) = "gunidecomp.h";
807 local ($bytes_out) = 0;
809 print "Writing $outfile...\n";
811 open (OUT, "> $outfile") || exit 1;
813 print OUT "/* This file is automatically generated. DO NOT EDIT!\n";
814 print OUT " Instead, edit gen-unicode-tables.pl and re-run. */\n\n";
816 print OUT "#ifndef DECOMP_H\n";
817 print OUT "#define DECOMP_H\n\n";
819 printf OUT "#define G_UNICODE_LAST_CHAR 0x%04x\n\n", $last;
821 printf OUT "#define G_UNICODE_MAX_TABLE_INDEX (0x110000 / 256)\n\n";
823 my $last_part1 = ($pages_before_e0000 * 256) - 1;
824 printf OUT "#define G_UNICODE_LAST_CHAR_PART1 0x%04X\n\n", $last_part1;
825 printf OUT "#define G_UNICODE_LAST_PAGE_PART1 %d\n\n", $pages_before_e0000 - 1;
827 $NOT_PRESENT_OFFSET = 65535;
828 print OUT "#define G_UNICODE_NOT_PRESENT_OFFSET $NOT_PRESENT_OFFSET\n\n";
832 printf OUT "static const guchar cclass_data[][256] = {\n";
833 for ($count = 0; $count <= $last; $count += 256)
835 $row[$count / 256] = &print_row ($count, 1, \&fetch_cclass);
837 printf OUT "\n};\n\n";
839 print OUT "static const gint16 combining_class_table_part1[$pages_before_e0000] = {\n";
840 for ($count = 0; $count <= $last_part1; $count += 256)
842 print OUT ",\n" if $count > 0;
843 print OUT " ", $row[$count / 256];
846 print OUT "\n};\n\n";
848 print OUT "static const gint16 combining_class_table_part2[768] = {\n";
849 for ($count = 0xE0000; $count <= $last; $count += 256)
851 print OUT ",\n" if $count > 0xE0000;
852 print OUT " ", $row[$count / 256];
855 print OUT "\n};\n\n";
857 print OUT "typedef struct\n{\n";
858 print OUT " gunichar ch;\n";
859 print OUT " guint16 canon_offset;\n";
860 print OUT " guint16 compat_offset;\n";
861 print OUT "} decomposition;\n\n";
863 print OUT "static const decomposition decomp_table[] =\n{\n";
866 my ($decomp_string) = "";
867 my ($decomp_string_offset) = 0;
868 for ($count = 0; $count <= $last; ++$count)
870 if (defined $decompositions[$count])
879 if (!$decompose_compat[$count]) {
880 $canon_decomp = make_decomp ($count, 0);
882 $compat_decomp = make_decomp ($count, 1);
884 if (defined $canon_decomp && $compat_decomp eq $canon_decomp) {
885 undef $compat_decomp;
888 my $canon_offset = handle_decomp ($canon_decomp, \%decomp_offsets, \$decomp_string, \$decomp_string_offset);
889 my $compat_offset = handle_decomp ($compat_decomp, \%decomp_offsets, \$decomp_string, \$decomp_string_offset);
891 die if $decomp_string_offset > $NOT_PRESENT_OFFSET;
893 printf OUT qq( { 0x%04x, $canon_offset, $compat_offset }), $count;
897 print OUT "\n};\n\n";
898 $bytes_out += $decomp_string_offset + 1;
900 printf OUT "static const gchar decomp_expansion_string[] = %s;\n\n", $decomp_string;
902 print OUT "#endif /* DECOMP_H */\n";
904 printf STDERR "Generated %d bytes in decomp tables\n", $bytes_out;
910 my ($outfile) = "gunibreak.h";
912 local ($bytes_out) = 0;
914 print "Writing $outfile...\n";
916 open (OUT, "> $outfile");
918 print OUT "/* This file is automatically generated. DO NOT EDIT!\n";
919 print OUT " Instead, edit gen-unicode-tables.pl and re-run. */\n\n";
921 print OUT "#ifndef BREAKTABLES_H\n";
922 print OUT "#define BREAKTABLES_H\n\n";
924 print OUT "#define G_UNICODE_DATA_VERSION \"$ARGV[0]\"\n\n";
926 printf OUT "#define G_UNICODE_LAST_CHAR 0x%04X\n\n", $last;
928 printf OUT "#define G_UNICODE_MAX_TABLE_INDEX 10000\n\n";
930 my $last_part1 = ($pages_before_e0000 * 256) - 1;
931 printf OUT "/* the last code point that should be looked up in break_property_table_part1 */\n";
932 printf OUT "#define G_UNICODE_LAST_CHAR_PART1 0x%04X\n\n", $last_part1;
935 printf OUT "static const gint8 break_property_data[][256] = {\n";
936 for ($count = 0; $count <= $last; $count += 256)
938 $row[$count / 256] = &print_row ($count, 1, \&fetch_break_type);
940 printf OUT "\n};\n\n";
942 printf OUT "/* U+0000 through U+%04X */\n", $last_part1;
943 print OUT "static const gint16 break_property_table_part1[$pages_before_e0000] = {\n";
944 for ($count = 0; $count <= $last_part1; $count += 256)
946 print OUT ",\n" if $count > 0;
947 print OUT " ", $row[$count / 256];
950 print OUT "\n};\n\n";
952 printf OUT "/* U+E0000 through U+%04X */\n", $last;
953 print OUT "static const gint16 break_property_table_part2[768] = {\n";
954 for ($count = 0xE0000; $count <= $last; $count += 256)
956 print OUT ",\n" if $count > 0xE0000;
957 print OUT " ", $row[$count / 256];
960 print OUT "\n};\n\n";
963 print OUT "#endif /* BREAKTABLES_H */\n";
967 printf STDERR "Generated %d bytes in break tables\n", $bytes_out;
971 # A fetch function for the break properties table.
975 return $break_mappings{$break_props[$index]};
978 # Fetcher for combining class.
985 # Expand a character decomposition recursively.
988 my ($code, $compat) = @_;
992 foreach $iter (split (' ', $decompositions[$code]))
995 if (defined $decompositions[$val] &&
996 ($compat || !$decompose_compat[$val]))
998 push (@result, &expand_decomp ($val, $compat));
1002 push (@result, $val);
1011 my ($code, $compat) = @_;
1014 foreach $iter (&expand_decomp ($code, $compat))
1016 $result .= pack ("U", $iter); # to utf-8
1021 # Generate special case data string from two fields
1022 sub add_special_case
1024 my ($code, $single, $field1, $field2) = @_;
1026 @values = (defined $single ? $single : (),
1027 (map { hex ($_) } split /\s+/, $field1),
1029 (map { hex ($_) } split /\s+/, $field2));
1033 for $value (@values) {
1034 $result .= pack ("U", $value); # to utf-8
1037 push @special_case_offsets, $special_case_offset;
1039 # We encode special cases up in the 0x1000000 space
1040 $value[$code] = 0x1000000 + $special_case_offset;
1042 $special_case_offset += 1 + &length_in_bytes ($result);
1044 push @special_cases, &escape ($result);
1047 sub output_special_case_table
1053 /* Table of special cases for case conversion; each record contains
1054 * First, the best single character mapping to lowercase if Lu,
1055 * and to uppercase if Ll, followed by the output mapping for the two cases
1056 * other than the case of the codepoint, in the order [Ll],[Lu],[Lt],
1057 * encoded in UTF-8, separated and terminated by a null character.
1059 static const gchar special_case_table[] = {
1063 for $case (@special_cases) {
1064 print $out qq( "$case\\0" /* offset ${special_case_offsets[$i]} */\n);
1073 print STDERR
"Generated " . ($special_case_offset + 1) . " bytes in special case table\n";
1076 sub enumerate_ordered
1081 for my $code (sort { $a <=> $b } keys %$array) {
1082 if ($array->{$code} == 1) {
1083 delete $array->{$code};
1086 $array->{$code} = $n++;
1092 sub output_composition_table
1094 print STDERR
"Generating composition table\n";
1096 local ($bytes_out) = 0;
1101 # First we need to go through and remove decompositions
1102 # starting with a non-starter, and single-character
1103 # decompositions. At the same time, record
1104 # the first and second character of each decomposition
1106 for $code (keys %compositions)
1108 @values = map { hex ($_) } split /\s+/, $compositions{$code};
1111 if ($cclass[$values[0]]) {
1112 delete $compositions{$code};
1116 # single-character decompositions
1118 delete $compositions{$code};
1123 die "$code has more than two elements in its decomposition!\n";
1126 if (exists $first{$values[0]}) {
1127 $first{$values[0]}++;
1129 $first{$values[0]} = 1;
1133 # Assign integer indices, removing singletons
1134 my $n_first = enumerate_ordered
(\
%first);
1136 # Now record the second character of each (non-singleton) decomposition
1137 for $code (keys %compositions) {
1138 @values = map { hex ($_) } split /\s+/, $compositions{$code};
1140 if (exists $first{$values[0]}) {
1141 if (exists $second{$values[1]}) {
1142 $second{$values[1]}++;
1144 $second{$values[1]} = 1;
1149 # Assign integer indices, removing duplicate
1150 my $n_second = enumerate_ordered
(\
%second);
1152 # Build reverse table
1154 my @first_singletons;
1155 my @second_singletons;
1157 for $code (keys %compositions) {
1158 @values = map { hex ($_) } split /\s+/, $compositions{$code};
1160 my $first = $first{$values[0]};
1161 my $second = $second{$values[1]};
1163 if (defined $first && defined $second) {
1164 $reverse{"$first|$second"} = $code;
1165 } elsif (!defined $first) {
1166 push @first_singletons, [ $values[0], $values[1], $code ];
1168 push @second_singletons, [ $values[1], $values[0], $code ];
1172 @first_singletons = sort { $a->[0] <=> $b->[0] } @first_singletons;
1173 @second_singletons = sort { $a->[0] <=> $b->[0] } @second_singletons;
1177 open OUT
, ">gunicomp.h" or die "Cannot open gunicomp.h: $!\n";
1179 print OUT
"/* This file is automatically generated. DO NOT EDIT!\n";
1180 print OUT
" Instead, edit gen-unicode-tables.pl and re-run. */\n\n";
1182 # Assign values in lookup table for all code points involved
1186 printf OUT
"#define COMPOSE_FIRST_START %d\n", $total;
1187 for $code (keys %first) {
1188 $vals{$code} = $first{$code} + $total;
1189 $last = $code if $code > $last;
1193 printf OUT
"#define COMPOSE_FIRST_SINGLE_START %d\n", $total;
1194 for $record (@first_singletons) {
1195 my $code = $record->[0];
1196 $vals{$code} = $i++ + $total;
1197 $last = $code if $code > $last;
1199 $total += @first_singletons;
1200 printf OUT
"#define COMPOSE_SECOND_START %d\n", $total;
1201 for $code (keys %second) {
1202 $vals{$code} = $second{$code} + $total;
1203 $last = $code if $code > $last;
1205 $total += $n_second;
1207 printf OUT
"#define COMPOSE_SECOND_SINGLE_START %d\n\n", $total;
1208 for $record (@second_singletons) {
1209 my $code = $record->[0];
1210 $vals{$code} = $i++ + $total;
1211 $last = $code if $code > $last;
1214 printf OUT
"#define COMPOSE_TABLE_LAST %d\n\n", $last / 256;
1216 # Output lookup table
1220 printf OUT
"static const guint16 compose_data[][256] = {\n";
1221 for (my $count = 0; $count <= $last; $count += 256)
1223 $row[$count / 256] = &print_row
($count, 2, sub { exists $vals{$_[0]} ?
$vals{$_[0]} : 0; });
1225 printf OUT
"\n};\n\n";
1227 print OUT
"static const gint16 compose_table[COMPOSE_TABLE_LAST + 1] = {\n";
1228 for (my $count = 0; $count <= $last; $count += 256)
1230 print OUT
",\n" if $count > 0;
1231 print OUT
" ", $row[$count / 256];
1234 print OUT
"\n};\n\n";
1236 # Output first singletons
1238 print OUT
"static const guint16 compose_first_single[][2] = {\n";
1240 for $record (@first_singletons) {
1241 if ($record->[1] > 0xFFFF or $record->[2] > 0xFFFF) {
1242 die "time to switch compose_first_single to gunichar" ;
1244 print OUT
",\n" if $i++ > 0;
1245 printf OUT
" { %#06x, %#06x }", $record->[1], $record->[2];
1249 $bytes_out += @first_singletons * 4;
1251 # Output second singletons
1253 print OUT
"static const guint16 compose_second_single[][2] = {\n";
1255 for $record (@second_singletons) {
1256 if ($record->[1] > 0xFFFF or $record->[2] > 0xFFFF) {
1257 die "time to switch compose_second_single to gunichar";
1259 print OUT
",\n" if $i++ > 0;
1260 printf OUT
" { %#06x, %#06x }", $record->[1], $record->[2];
1264 $bytes_out += @second_singletons * 4;
1266 # Output array of composition pairs
1269 static const guint16 compose_array[$n_first][$n_second] = {
1272 for (my $i = 0; $i < $n_first; $i++) {
1273 print OUT
",\n" if $i;
1275 for (my $j = 0; $j < $n_second; $j++) {
1276 print OUT
", " if $j;
1277 if (exists $reverse{"$i|$j"}) {
1278 if ($reverse{"$i|$j"} > 0xFFFF) {
1279 die "time to switch compose_array to gunichar" ;
1281 printf OUT
"0x%04x", $reverse{"$i|$j"};
1294 $bytes_out += $n_first * $n_second * 2;
1296 printf STDERR
"Generated %d bytes in compose tables\n", $bytes_out;
1299 sub output_casefold_table
1305 /* Table of casefolding cases that can't be derived by lowercasing
1307 static const struct {
1309 gchar data[$casefoldlen];
1310 } casefold_table[] = {
1313 @casefold = sort { $a->[0] <=> $b->[0] } @casefold;
1315 for $case (@casefold)
1318 $string = $case->[1];
1320 if ($code > 0xFFFF) {
1321 die "time to switch casefold_table to gunichar" ;
1324 print $out sprintf(qq( { 0x
%04x, "$string" },\n), $code);
1333 my $recordlen = (2+$casefoldlen+1) & ~1;
1334 printf "Generated %d bytes for casefold table\n", $recordlen * @casefold;