3 # This file is part of DisOrder.
4 # Copyright (C) 2007 Richard Kettlewell
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 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 # Generate Unicode support tables
24 # This script will download data from unicode.org if the required files
25 # aren't in the current directory.
27 # After modifying this script you should run:
28 # make -C lib rebuild-unicode check
30 # Things not supported yet:
31 # - SpecialCasing.txt data for case mapping
32 # - Title case offsets
33 # - Some kind of hinting for composition
36 # NB the generated files DO NOT offer a stable ABI and so are not immediately
37 # suitable for use in a general-purpose library. Things that would need to
39 # - Hide unidata.h from applications; it will never be ABI- or even API-stable.
40 # - Stablized General_Category values
41 # - Extend the unicode.h API to general utility rather than just what
49 print @_ or die "$!\n";
56 return join("-", map($d->{$_}, sort keys %$d));
61 # This can be varied to trade off the number of subtables against their size.
62 # 16 gave the smallest results last time I checked (on a Mac with a 32-bit
70 # Where to break the table. There is a huge empty section of the Unicode
71 # code space and we deal with this by simply leaving it out of the table.
72 # This complicates the lookup function a little but should not affect
73 # performance in the cases we care about.
74 our $break_start = 0x30000;
75 our $break_end = 0xE0000;
77 # Similarly we simply omit the very top of the table and sort it out in the
79 our $break_top = 0xE0200;
81 my %cats = (); # known general categories
82 my %data = (); # mapping of codepoints to information
83 my $max = 0; # maximum codepoint
84 my $maxccc = 0; # maximum combining class
86 my $minud = 0; # max/min upper case offset
88 my $minld = 0; # max/min lower case offset
90 # Make sure we have our desired input files. We explicitly specify a
91 # Unicode standard version to make sure that a given version of DisOrder
92 # supports a given version of Unicode.
95 my $lpath = basename($path);
97 system("wget http://www.unicode.org/Public/5.0.0/ucd/$path");
98 chmod(0444, $lpath) or die "$lpath: $!\n";
100 open(STDIN, "<$lpath") or die "$lpath: $!\n";
101 print STDERR "Reading $lpath...\n";
105 # Read the main data file
106 input("UnicodeData.txt");
110 my $hangul_syllable_decomps = 0;
111 my $hangul_choseong_decomps = 0;
113 my @f = split(/;/, $_);
114 my $c = hex($f[0]); # codepoint
116 die "$f[0] $name is in the break\n"
117 if $c >= $break_start && $c < $break_end;
118 my $gc = $f[2]; # General_Category
119 # Variuos GCs we don't expect to see in UnicodeData.txt
120 $cats{$gc} = 1; # always record all GCs
121 if($name =~ /first>/i) {
124 } elsif($name =~ /last>/i) {
129 die "unexpected Cn" if $gc eq 'Cn';
130 my $ccc = $f[3]; # Canonical_Combining_Class
131 my $dm = $f[5]; # Decomposition_Type + Decomposition_Mapping
132 my $sum = hex($f[12]) || $c; # Simple_Uppercase_Mapping
133 my $slm = hex($f[13]) || $c; # Simple_Lowercase_Mapping
134 # recalculate the upper/lower case mappings as offsets
137 # update bounds on various values
138 $maxccc = $ccc if $ccc > $maxccc; # assumed never to be -ve
139 $minud = $ud if $ud < $minud;
140 $maxud = $ud if $ud > $maxud;
141 $minld = $ld if $ld < $minld;
142 $maxld = $ld if $ld > $maxld;
144 printf STDERR "> range %04X-%04X is %s\n", $start, $end, $gc;
146 for($c = $start; $c <= $end; ++$c) {
156 # This is a compatibility decomposition
159 $maxref = \$maxcompat;
161 $maxref = \$maxcanon;
163 $d->{decomp} = [map(hex($_), split(/\s+/, $dm))];
164 my $len = scalar @{$d->{decomp}};
165 $$maxref = $len if $len > $$maxref;
167 if(${$d->{decomp}}[0] >= 0xAC00 && ${$d->{decomp}}[0] <= 0xD7A3) {
168 ++$hangul_syllable_decomps;
170 if(${$d->{decomp}}[0] >= 0x1100 && ${$d->{decomp}}[0] <= 0x115F) {
171 ++$hangul_choseong_decomps;
178 $max = $end if $end > $max;
181 sub read_prop_with_ranges {
189 my ($range, $propval) = split(/\s*;\s*/, $_);
190 if($range =~ /(.*)\.\.(.*)/) {
191 for my $c (hex($1) .. hex($2)) {
192 $data{$c}->{$propkey} = $propval;
196 $data{$c}->{$propkey} = $propval;
202 read_prop_with_ranges("auxiliary/GraphemeBreakProperty.txt", "gbreak");
203 read_prop_with_ranges("auxiliary/WordBreakProperty.txt", "wbreak");
204 read_prop_with_ranges("auxiliary/SentenceBreakProperty.txt", "sbreak");
206 # Compute the full list and fill in the Extend category properly
210 for my $c (keys %data) {
211 if(!exists $data{$c}->{gbreak}) {
212 $data{$c}->{gbreak} = 'Other';
214 $gbreak{$data{$c}->{gbreak}} = 1;
216 if(!exists $data{$c}->{wbreak}) {
217 if($data{$c}->{gbreak} eq 'Extend') {
218 $data{$c}->{wbreak} = 'Extend';
220 $data{$c}->{wbreak} = 'Other';
223 $wbreak{$data{$c}->{wbreak}} = 1;
225 if(!exists $data{$c}->{sbreak}) {
226 if($data{$c}->{gbreak} eq 'Extend') {
227 $data{$c}->{sbreak} = 'Extend';
229 $data{$c}->{sbreak} = 'Other';
232 $sbreak{$data{$c}->{sbreak}} = 1;
235 # Various derived properties
236 input("DerivedNormalizationProps.txt");
241 my @f = split(/\s*;\s*/, $_);
245 my ($range, $propkey, $propval) = @f;
246 if($range =~ /(.*)\.\.(.*)/) {
247 for my $c (hex($1) .. hex($2)) {
248 $data{$c}->{$propkey} = $propval
252 $data{$c}->{$propkey} = $propval
256 # Round up the maximum value to a whole number of subtables
257 $max += ($modulus - 1) - ($max % $modulus);
259 # Private use characters
260 # We only fill in values below $max, utf32__unidata()
267 for(my $c = 0xE000; $c <= 0xF8FF && $c <= $max; ++$c) {
270 for(my $c = 0xF0000; $c <= 0xFFFFD && $c <= $max; ++$c) {
273 for(my $c = 0x100000; $c <= 0x10FFFD && $c <= $max; ++$c) {
277 # Anything left is not assigned
279 "gc" => "Cn", # not assigned
284 for(my $c = 0; $c <= $max; ++$c) {
285 if(!exists $data{$c}) {
288 if(!exists $data{$c}->{wbreak}) {
289 $data{$c}->{wbreak} = 'Other';
291 if(!exists $data{$c}->{gbreak}) {
292 $data{$c}->{gbreak} = 'Other';
294 if(!exists $data{$c}->{sbreak}) {
295 $data{$c}->{sbreak} = 'Other';
300 # Read the casefolding data too
301 input("CaseFolding.txt");
304 next if /^\#/ or $_ eq '';
305 my @f = split(/\s*;\s*/, $_);
306 # Full case folding means use status C and F.
307 # We discard status T, Turkish users may wish to change this.
308 if($f[1] eq 'C' or $f[1] eq 'F') {
310 $data{$c}->{casefold} = $f[2];
311 # We are particularly interest in combining characters that
312 # case-fold to non-combining characters, or characters that
313 # case-fold to sequences with combining characters in non-initial
314 # positions, as these required decomposiiton before case-folding
315 my @d = map(hex($_), split(/\s+/, $data{$c}->{casefold}));
316 if($data{$c}->{ccc} != 0) {
317 # This is a combining character
318 if($data{$d[0]}->{ccc} == 0) {
319 # The first character of its case-folded form is NOT
320 # a combining character. The field name is the example
321 # explicitly mentioned in the spec.
322 $data{$c}->{ypogegrammeni} = 1;
325 # This is a non-combining character; inspect the non-initial
326 # code points of the case-folded sequence
328 if(grep($data{$_}->{ccc} != 0, @d)) {
329 # Some non-initial code point in the case-folded for is NOT a
330 # a combining character.
331 $data{$c}->{ypogegrammeni} = 1;
337 # Generate the header file
338 print STDERR "Generating unidata.h...\n";
339 open(STDOUT, ">unidata.h") or die "unidata.h: $!\n";
341 out("/* Automatically generated file, see scripts/make-unidata */\n",
342 "#ifndef UNIDATA_H\n",
343 "#define UNIDATA_H\n");
345 # TODO choose stable values for General_Category
346 out("enum unicode_General_Category {\n",
348 map(" unicode_General_Category_$_", sort keys %cats)), "\n};\n");
350 out("enum unicode_Grapheme_Break {\n",
352 map(" unicode_Grapheme_Break_$_", sort keys %gbreak)),
354 out("extern const char *const unicode_Grapheme_Break_names[];\n");
356 out("enum unicode_Word_Break {\n",
358 map(" unicode_Word_Break_$_", sort keys %wbreak)),
360 out("extern const char *const unicode_Word_Break_names[];\n");
362 out("enum unicode_Sentence_Break {\n",
364 map(" unicode_Sentence_Break_$_", sort keys %sbreak)),
366 out("extern const char *const unicode_Sentence_Break_names[];\n");
368 out("enum unicode_flags {\n",
369 " unicode_normalize_before_casefold = 1,\n",
370 " unicode_compatibility_decomposition = 2\n",
374 # Choose the narrowest type that will fit the required values
376 my ($min, $max) = @_;
378 return "char" if $max <= 127;
379 return "unsigned char" if $max <= 255;
380 return "int16_t" if $max < 32767;
381 return "uint16_t" if $max < 65535;
384 return "char" if $min >= -127 && $max <= 127;
385 return "int16_t" if $min >= -32767 && $max <= 32767;
390 out("struct unidata {\n",
391 # decomposition (canonical or compatibility;
392 # unicode_compatibility_decomposition distinguishes) or NULL
393 " const uint32_t *decomp;\n",
395 # case-folded string or NULL
396 " const uint32_t *casefold;\n",
398 # composed characters that start with this code point. This only
399 # includes primary composites, i.e. the decomposition mapping is
400 # canonical and this code point is not in the exclusion table.
401 " const uint32_t *composed;\n",
403 # " ".choosetype($minud, $maxud)." upper_offset;\n",
404 # " ".choosetype($minld, $maxld)." lower_offset;\n",
406 # canonical combining class
407 " ".choosetype(0, $maxccc)." ccc;\n",
408 " char general_category;\n",
410 # see unicode_flags enum
412 " char grapheme_break;\n",
413 " char word_break;\n",
414 " char sentence_break;\n",
416 # decomp and casefold do have have non-BMP characters, so we
417 # can't use a simple 16-bit table. We could use UTF-8 or UTF-16
418 # though, saving a bit of space (probably not that much...) at the
419 # cost of marginally reduced performance and additional complexity
421 out("extern const struct unidata *const unidata[];\n");
423 out("extern const struct unicode_utf8_row {\n",
425 " uint8_t min2, max2;\n",
426 "} unicode_utf8_valid[];\n");
428 out("#define UNICODE_NCHARS ", ($max + 1), "\n");
429 out("#define UNICODE_MODULUS $modulus\n");
430 out("#define UNICODE_BREAK_START $break_start\n");
431 out("#define UNICODE_BREAK_END $break_end\n");
432 out("#define UNICODE_BREAK_TOP $break_top\n");
436 close STDOUT or die "unidata.h: $!\n";
438 print STDERR "Generating unidata.c...\n";
439 open(STDOUT, ">unidata.c") or die "unidata.c: $!\n";
441 out("/* Automatically generated file, see scripts/make-unidata */\n",
442 "#include <config.h>\n",
443 "#include \"types.h\"\n",
444 "#include \"unidata.h\"\n");
446 # Short aliases to keep .c file small
448 out(map(sprintf("#define %s unicode_General_Category_%s\n", $_, $_),
450 out(map(sprintf("#define GB%s unicode_Grapheme_Break_%s\n", $_, $_),
452 out(map(sprintf("#define WB%s unicode_Word_Break_%s\n", $_, $_),
454 out(map(sprintf("#define SB%s unicode_Sentence_Break_%s\n", $_, $_),
456 out("#define NBC unicode_normalize_before_casefold\n");
457 out("#define CD unicode_compatibility_decomposition\n");
459 # Names for *_Break properties
460 out("const char *const unicode_Grapheme_Break_names[] = {\n",
462 map(" \"$_\"", sort keys %gbreak)),
464 out("const char *const unicode_Word_Break_names[] = {\n",
466 map(" \"$_\"", sort keys %wbreak)),
468 out("const char *const unicode_Sentence_Break_names[] = {\n",
470 map(" \"$_\"", sort keys %sbreak)),
477 out("static const uint32_t ");
479 my $s = join(",", @_);
480 if(!exists $ddnums{$s}) {
486 out("dd$ddnum\[]={$s}");
487 $ddnums{$s} = $ddnum++;
491 return "dd$ddnums{$s}";
494 # Generate the decomposition mapping tables.
495 print STDERR "> decomposition mappings\n";
496 for(my $c = 0; $c <= $max; ++$c) {
497 if(exists $data{$c} && exists $data{$c}->{decomp}) {
498 $data{$c}->{decompsym} = dedupe(@{$data{$c}->{decomp}}, 0);
502 print STDERR "> composition mappings\n";
503 # First we must generate the mapping of each code point to possible
505 for(my $c = 0; $c <= $max; ++$c) {
507 && exists $data{$c}->{decomp}
508 && !exists $data{$c}->{compat}
509 && !$data{$c}->{Full_Composition_Exclusion}) {
510 # $c has a non-excluded canonical decomposition, i.e. it is
511 # a primary composite. Find the first code point of the decomposition
512 my $first = ${$data{$c}->{decomp}}[0];
513 if(!exists $data{$first}->{compose}) {
514 $data{$first}->{compose} = [$c];
516 push(@{$data{$first}->{compose}}, $c);
520 # Then we can generate the tables.
521 for(my $c = 0; $c <= $max; ++$c) {
522 if(exists $data{$c} && exists $data{$c}->{compose}) {
523 $data{$c}->{compsym} = dedupe(@{$data{$c}->{compose}}, 0);
527 # The case folding table.
528 print STDERR "> case-fold mappings\n";
529 for(my $c = 0; $c <= $max; ++$c) {
530 if(exists $data{$c} && exists $data{$c}->{casefold}) {
531 $data{$c}->{cfsym} = dedupe(map(hex($_), split(/\s+/,
532 $data{$c}->{casefold})),
537 # End of de-dupable arrays
540 # Visit all the $modulus-character blocks in turn and generate the
541 # required subtables. As above we spot duplicates to save space. In
542 # Unicode 5.0.0 with $modulus=128 and current table data this saves
543 # 1372 subtables or at least three and a half megabytes on 32-bit
545 print STDERR "> subtables\n";
546 my %subtable = (); # base->subtable number
547 my %subtableno = (); # subtable number -> content
548 my $subtablecounter = 0; # counter for subtable numbers
549 my $subtablessaved = 0; # number of tables saved
550 for(my $base = 0; $base <= $max; $base += $modulus) {
551 next if $base >= $break_start && $base < $break_end;
552 next if $base >= $break_top;
554 for(my $c = $base; $c < $base + $modulus; ++$c) {
556 my $decompsym = ($data{$c}->{decompsym} or "0");
557 my $cfsym = ($data{$c}->{cfsym} or "0");
558 my $compsym = ($data{$c}->{compsym} or "0");
560 if($data{$c}->{ypogegrammeni}) {
563 if($data{$c}->{compat}) {
566 my $flags = @flags ? join("|", @flags) : 0;
582 my $t = join(",\n", @t);
583 if(!exists $subtable{$t}) {
584 out(sprintf("/* %04X-%04X */\n", $base, $base + $modulus - 1));
585 out("static const struct unidata st$subtablecounter\[] = {\n",
588 $subtable{$t} = $subtablecounter++;
592 $subtableno{$base} = $subtable{$t};
595 print STDERR "> main table\n";
596 out("const struct unidata *const unidata[]={\n");
597 for(my $base = 0; $base <= $max; $base += $modulus) {
598 next if $base >= $break_start && $base < $break_end;
599 next if $base >= $break_top;
600 #out("st$subtableno{$base} /* ".sprintf("%04x", $base)." */,\n");
601 out("st$subtableno{$base},\n");
605 print STDERR "> UTF-8 table\n";
606 out("const struct unicode_utf8_row unicode_utf8_valid[] = {\n");
607 for(my $c = 0; $c <= 0x7F; ++$c) {
608 out(" { 1, 0, 0 }, /* $c */\n");
610 for(my $c = 0x80; $c < 0xC2; ++$c) {
611 out(" { 0, 0, 0 }, /* $c */\n");
613 for(my $c = 0xC2; $c <= 0xDF; ++$c) {
614 out(" { 2, 0x80, 0xBF }, /* $c */\n");
616 for(my $c = 0xE0; $c <= 0xE0; ++$c) {
617 out(" { 3, 0xA0, 0xBF }, /* $c */\n");
619 for(my $c = 0xE1; $c <= 0xEC; ++$c) {
620 out(" { 3, 0x80, 0xBF }, /* $c */\n");
622 for(my $c = 0xED; $c <= 0xED; ++$c) {
623 out(" { 3, 0x80, 0x9F }, /* $c */\n");
625 for(my $c = 0xEE; $c <= 0xEF; ++$c) {
626 out(" { 3, 0x80, 0xBF }, /* $c */\n");
628 for(my $c = 0xF0; $c <= 0xF0; ++$c) {
629 out(" { 4, 0x90, 0xBF }, /* $c */\n");
631 for(my $c = 0xF1; $c <= 0xF3; ++$c) {
632 out(" { 4, 0x80, 0xBF }, /* $c */\n");
634 for(my $c = 0xF4; $c <= 0xF4; ++$c) {
635 out(" { 4, 0x80, 0x8F }, /* $c */\n");
637 for(my $c = 0xF5; $c <= 0xFF; ++$c) {
638 out(" { 0, 0, 0 }, /* $c */\n");
642 close STDOUT or die "unidata.c: $!\n";
644 print STDERR "Done.\n\n";
645 printf STDERR "modulus=%d\n", $modulus;
646 printf STDERR "max=%04X\n", $max;
647 print STDERR "subtables=$subtablecounter, subtablessaved=$subtablessaved\n";
648 print STDERR "ddsaved=$ddsaved\n";
649 print STDERR "maxcompat=$maxcompat maxcanon=$maxcanon\n";
650 print STDERR "$hangul_syllable_decomps canonical decompositions to Hangul syllables\n";
651 print STDERR "$hangul_choseong_decomps canonical decompositions to Hangul Choseong\n";
653 die "We assumed that canonical decompositions were never more than 2 long!\n"
656 die "We assumed no canonical decompositions to Hangul syllables/Choseong!\n"
657 if $hangul_syllable_decomps || $hangul_choseong_decomps;