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 3 of the License, or
9 # (at your option) any later version.
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 # Generate Unicode support tables
22 # This script will download data from unicode.org if the required files
23 # aren't in the current directory.
25 # After modifying this script you should run:
26 # make -C lib rebuild-unicode check
28 # Things not supported yet:
29 # - SpecialCasing.txt data for case mapping
30 # - Title case offsets
31 # - Some kind of hinting for composition
34 # NB the generated files DO NOT offer a stable ABI and so are not immediately
35 # suitable for use in a general-purpose library. Things that would need to
37 # - Hide unidata.h from applications; it will never be ABI- or even API-stable.
38 # - Stablized General_Category values
39 # - Extend the unicode.h API to general utility rather than just what
47 print @_ or die "$!\n";
54 return join("-", map($d->{$_}, sort keys %$d));
59 # This can be varied to trade off the number of subtables against their size.
60 # 16 gave the smallest results last time I checked (on a Mac with a 32-bit
68 # Where to break the table. There is a huge empty section of the Unicode
69 # code space and we deal with this by simply leaving it out of the table.
70 # This complicates the lookup function a little but should not affect
71 # performance in the cases we care about.
72 our $break_start = 0x30000;
73 our $break_end = 0xE0000;
75 # Similarly we simply omit the very top of the table and sort it out in the
77 our $break_top = 0xE0200;
79 my %cats = (); # known general categories
80 my %data = (); # mapping of codepoints to information
81 my $max = 0; # maximum codepoint
82 my $maxccc = 0; # maximum combining class
84 my $minud = 0; # max/min upper case offset
86 my $minld = 0; # max/min lower case offset
88 # Make sure we have our desired input files. We explicitly specify a
89 # Unicode standard version to make sure that a given version of DisOrder
90 # supports a given version of Unicode.
93 my $lpath = basename($path);
95 system("wget http://www.unicode.org/Public/5.0.0/ucd/$path");
96 chmod(0444, $lpath) or die "$lpath: $!\n";
98 open(STDIN, "<$lpath") or die "$lpath: $!\n";
99 print STDERR "Reading $lpath...\n";
103 # Read the main data file
104 input("UnicodeData.txt");
108 my $hangul_syllable_decomps = 0;
109 my $hangul_choseong_decomps = 0;
111 my @f = split(/;/, $_);
112 my $c = hex($f[0]); # codepoint
114 die "$f[0] $name is in the break\n"
115 if $c >= $break_start && $c < $break_end;
116 my $gc = $f[2]; # General_Category
117 # Variuos GCs we don't expect to see in UnicodeData.txt
118 $cats{$gc} = 1; # always record all GCs
119 if($name =~ /first>/i) {
122 } elsif($name =~ /last>/i) {
127 die "unexpected Cn" if $gc eq 'Cn';
128 my $ccc = $f[3]; # Canonical_Combining_Class
129 my $dm = $f[5]; # Decomposition_Type + Decomposition_Mapping
130 my $sum = hex($f[12]) || $c; # Simple_Uppercase_Mapping
131 my $slm = hex($f[13]) || $c; # Simple_Lowercase_Mapping
132 # recalculate the upper/lower case mappings as offsets
135 # update bounds on various values
136 $maxccc = $ccc if $ccc > $maxccc; # assumed never to be -ve
137 $minud = $ud if $ud < $minud;
138 $maxud = $ud if $ud > $maxud;
139 $minld = $ld if $ld < $minld;
140 $maxld = $ld if $ld > $maxld;
142 printf STDERR "> range %04X-%04X is %s\n", $start, $end, $gc;
144 for($c = $start; $c <= $end; ++$c) {
154 # This is a compatibility decomposition
157 $maxref = \$maxcompat;
159 $maxref = \$maxcanon;
161 $d->{decomp} = [map(hex($_), split(/\s+/, $dm))];
162 my $len = scalar @{$d->{decomp}};
163 $$maxref = $len if $len > $$maxref;
165 if(${$d->{decomp}}[0] >= 0xAC00 && ${$d->{decomp}}[0] <= 0xD7A3) {
166 ++$hangul_syllable_decomps;
168 if(${$d->{decomp}}[0] >= 0x1100 && ${$d->{decomp}}[0] <= 0x115F) {
169 ++$hangul_choseong_decomps;
176 $max = $end if $end > $max;
179 sub read_prop_with_ranges {
187 my ($range, $propval) = split(/\s*;\s*/, $_);
188 if($range =~ /(.*)\.\.(.*)/) {
189 for my $c (hex($1) .. hex($2)) {
190 $data{$c}->{$propkey} = $propval;
194 $data{$c}->{$propkey} = $propval;
200 read_prop_with_ranges("auxiliary/GraphemeBreakProperty.txt", "gbreak");
201 read_prop_with_ranges("auxiliary/WordBreakProperty.txt", "wbreak");
202 read_prop_with_ranges("auxiliary/SentenceBreakProperty.txt", "sbreak");
204 # Compute the full list and fill in the Extend category properly
208 for my $c (keys %data) {
209 if(!exists $data{$c}->{gbreak}) {
210 $data{$c}->{gbreak} = 'Other';
212 $gbreak{$data{$c}->{gbreak}} = 1;
214 if(!exists $data{$c}->{wbreak}) {
215 if($data{$c}->{gbreak} eq 'Extend') {
216 $data{$c}->{wbreak} = 'Extend';
218 $data{$c}->{wbreak} = 'Other';
221 $wbreak{$data{$c}->{wbreak}} = 1;
223 if(!exists $data{$c}->{sbreak}) {
224 if($data{$c}->{gbreak} eq 'Extend') {
225 $data{$c}->{sbreak} = 'Extend';
227 $data{$c}->{sbreak} = 'Other';
230 $sbreak{$data{$c}->{sbreak}} = 1;
233 # Various derived properties
234 input("DerivedNormalizationProps.txt");
239 my @f = split(/\s*;\s*/, $_);
243 my ($range, $propkey, $propval) = @f;
244 if($range =~ /(.*)\.\.(.*)/) {
245 for my $c (hex($1) .. hex($2)) {
246 $data{$c}->{$propkey} = $propval
250 $data{$c}->{$propkey} = $propval
254 # Round up the maximum value to a whole number of subtables
255 $max += ($modulus - 1) - ($max % $modulus);
257 # Private use characters
258 # We only fill in values below $max, utf32__unidata()
265 for(my $c = 0xE000; $c <= 0xF8FF && $c <= $max; ++$c) {
268 for(my $c = 0xF0000; $c <= 0xFFFFD && $c <= $max; ++$c) {
271 for(my $c = 0x100000; $c <= 0x10FFFD && $c <= $max; ++$c) {
275 # Anything left is not assigned
277 "gc" => "Cn", # not assigned
282 for(my $c = 0; $c <= $max; ++$c) {
283 if(!exists $data{$c}) {
286 if(!exists $data{$c}->{wbreak}) {
287 $data{$c}->{wbreak} = 'Other';
289 if(!exists $data{$c}->{gbreak}) {
290 $data{$c}->{gbreak} = 'Other';
292 if(!exists $data{$c}->{sbreak}) {
293 $data{$c}->{sbreak} = 'Other';
298 # Read the casefolding data too
299 input("CaseFolding.txt");
302 next if /^\#/ or $_ eq '';
303 my @f = split(/\s*;\s*/, $_);
304 # Full case folding means use status C and F.
305 # We discard status T, Turkish users may wish to change this.
306 if($f[1] eq 'C' or $f[1] eq 'F') {
308 $data{$c}->{casefold} = $f[2];
309 # We are particularly interest in combining characters that
310 # case-fold to non-combining characters, or characters that
311 # case-fold to sequences with combining characters in non-initial
312 # positions, as these required decomposiiton before case-folding
313 my @d = map(hex($_), split(/\s+/, $data{$c}->{casefold}));
314 if($data{$c}->{ccc} != 0) {
315 # This is a combining character
316 if($data{$d[0]}->{ccc} == 0) {
317 # The first character of its case-folded form is NOT
318 # a combining character. The field name is the example
319 # explicitly mentioned in the spec.
320 $data{$c}->{ypogegrammeni} = 1;
323 # This is a non-combining character; inspect the non-initial
324 # code points of the case-folded sequence
326 if(grep($data{$_}->{ccc} != 0, @d)) {
327 # Some non-initial code point in the case-folded for is NOT a
328 # a combining character.
329 $data{$c}->{ypogegrammeni} = 1;
335 # Generate the header file
336 print STDERR "Generating unidata.h...\n";
337 open(STDOUT, ">unidata.h") or die "unidata.h: $!\n";
339 out("/* Automatically generated file, see scripts/make-unidata */\n",
340 "#ifndef UNIDATA_H\n",
341 "#define UNIDATA_H\n");
343 # TODO choose stable values for General_Category
344 out("enum unicode_General_Category {\n",
346 map(" unicode_General_Category_$_", sort keys %cats)), "\n};\n");
348 out("enum unicode_Grapheme_Break {\n",
350 map(" unicode_Grapheme_Break_$_", sort keys %gbreak)),
352 out("extern const char *const unicode_Grapheme_Break_names[];\n");
354 out("enum unicode_Word_Break {\n",
356 map(" unicode_Word_Break_$_", sort keys %wbreak)),
358 out("extern const char *const unicode_Word_Break_names[];\n");
360 out("enum unicode_Sentence_Break {\n",
362 map(" unicode_Sentence_Break_$_", sort keys %sbreak)),
364 out("extern const char *const unicode_Sentence_Break_names[];\n");
366 out("enum unicode_flags {\n",
367 " unicode_normalize_before_casefold = 1,\n",
368 " unicode_compatibility_decomposition = 2\n",
372 # Choose the narrowest type that will fit the required values
374 my ($min, $max) = @_;
376 return "char" if $max <= 127;
377 return "unsigned char" if $max <= 255;
378 return "int16_t" if $max < 32767;
379 return "uint16_t" if $max < 65535;
382 return "char" if $min >= -127 && $max <= 127;
383 return "int16_t" if $min >= -32767 && $max <= 32767;
388 out("struct unidata {\n",
389 # decomposition (canonical or compatibility;
390 # unicode_compatibility_decomposition distinguishes) or NULL
391 " const uint32_t *decomp;\n",
393 # case-folded string or NULL
394 " const uint32_t *casefold;\n",
396 # composed characters that start with this code point. This only
397 # includes primary composites, i.e. the decomposition mapping is
398 # canonical and this code point is not in the exclusion table.
399 " const uint32_t *composed;\n",
401 # " ".choosetype($minud, $maxud)." upper_offset;\n",
402 # " ".choosetype($minld, $maxld)." lower_offset;\n",
404 # canonical combining class
405 " ".choosetype(0, $maxccc)." ccc;\n",
406 " char general_category;\n",
408 # see unicode_flags enum
410 " char grapheme_break;\n",
411 " char word_break;\n",
412 " char sentence_break;\n",
414 # decomp and casefold do have have non-BMP characters, so we
415 # can't use a simple 16-bit table. We could use UTF-8 or UTF-16
416 # though, saving a bit of space (probably not that much...) at the
417 # cost of marginally reduced performance and additional complexity
419 out("extern const struct unidata *const unidata[];\n");
421 out("extern const struct unicode_utf8_row {\n",
423 " uint8_t min2, max2;\n",
424 "} unicode_utf8_valid[];\n");
426 out("#define UNICODE_NCHARS ", ($max + 1), "\n");
427 out("#define UNICODE_MODULUS $modulus\n");
428 out("#define UNICODE_BREAK_START $break_start\n");
429 out("#define UNICODE_BREAK_END $break_end\n");
430 out("#define UNICODE_BREAK_TOP $break_top\n");
434 close STDOUT or die "unidata.h: $!\n";
436 print STDERR "Generating unidata.c...\n";
437 open(STDOUT, ">unidata.c") or die "unidata.c: $!\n";
439 out("/* Automatically generated file, see scripts/make-unidata */\n",
440 "#include \"common.h\"\n",
441 "#include \"unidata.h\"\n");
443 # Short aliases to keep .c file small
445 out(map(sprintf("#define %s unicode_General_Category_%s\n", $_, $_),
447 out(map(sprintf("#define GB%s unicode_Grapheme_Break_%s\n", $_, $_),
449 out(map(sprintf("#define WB%s unicode_Word_Break_%s\n", $_, $_),
451 out(map(sprintf("#define SB%s unicode_Sentence_Break_%s\n", $_, $_),
453 out("#define NBC unicode_normalize_before_casefold\n");
454 out("#define CD unicode_compatibility_decomposition\n");
456 # Names for *_Break properties
457 out("const char *const unicode_Grapheme_Break_names[] = {\n",
459 map(" \"$_\"", sort keys %gbreak)),
461 out("const char *const unicode_Word_Break_names[] = {\n",
463 map(" \"$_\"", sort keys %wbreak)),
465 out("const char *const unicode_Sentence_Break_names[] = {\n",
467 map(" \"$_\"", sort keys %sbreak)),
474 out("static const uint32_t ");
476 my $s = join(",", @_);
477 if(!exists $ddnums{$s}) {
483 out("dd$ddnum\[]={$s}");
484 $ddnums{$s} = $ddnum++;
488 return "dd$ddnums{$s}";
491 # Generate the decomposition mapping tables.
492 print STDERR "> decomposition mappings\n";
493 for(my $c = 0; $c <= $max; ++$c) {
494 if(exists $data{$c} && exists $data{$c}->{decomp}) {
495 $data{$c}->{decompsym} = dedupe(@{$data{$c}->{decomp}}, 0);
499 print STDERR "> composition mappings\n";
500 # First we must generate the mapping of each code point to possible
502 for(my $c = 0; $c <= $max; ++$c) {
504 && exists $data{$c}->{decomp}
505 && !exists $data{$c}->{compat}
506 && !$data{$c}->{Full_Composition_Exclusion}) {
507 # $c has a non-excluded canonical decomposition, i.e. it is
508 # a primary composite. Find the first code point of the decomposition
509 my $first = ${$data{$c}->{decomp}}[0];
510 if(!exists $data{$first}->{compose}) {
511 $data{$first}->{compose} = [$c];
513 push(@{$data{$first}->{compose}}, $c);
517 # Then we can generate the tables.
518 for(my $c = 0; $c <= $max; ++$c) {
519 if(exists $data{$c} && exists $data{$c}->{compose}) {
520 $data{$c}->{compsym} = dedupe(@{$data{$c}->{compose}}, 0);
524 # The case folding table.
525 print STDERR "> case-fold mappings\n";
526 for(my $c = 0; $c <= $max; ++$c) {
527 if(exists $data{$c} && exists $data{$c}->{casefold}) {
528 $data{$c}->{cfsym} = dedupe(map(hex($_), split(/\s+/,
529 $data{$c}->{casefold})),
534 # End of de-dupable arrays
537 # Visit all the $modulus-character blocks in turn and generate the
538 # required subtables. As above we spot duplicates to save space. In
539 # Unicode 5.0.0 with $modulus=128 and current table data this saves
540 # 1372 subtables or at least three and a half megabytes on 32-bit
542 print STDERR "> subtables\n";
543 my %subtable = (); # base->subtable number
544 my %subtableno = (); # subtable number -> content
545 my $subtablecounter = 0; # counter for subtable numbers
546 my $subtablessaved = 0; # number of tables saved
547 for(my $base = 0; $base <= $max; $base += $modulus) {
548 next if $base >= $break_start && $base < $break_end;
549 next if $base >= $break_top;
551 for(my $c = $base; $c < $base + $modulus; ++$c) {
553 my $decompsym = ($data{$c}->{decompsym} or "0");
554 my $cfsym = ($data{$c}->{cfsym} or "0");
555 my $compsym = ($data{$c}->{compsym} or "0");
557 if($data{$c}->{ypogegrammeni}) {
560 if($data{$c}->{compat}) {
563 my $flags = @flags ? join("|", @flags) : 0;
579 my $t = join(",\n", @t);
580 if(!exists $subtable{$t}) {
581 out(sprintf("/* %04X-%04X */\n", $base, $base + $modulus - 1));
582 out("static const struct unidata st$subtablecounter\[] = {\n",
585 $subtable{$t} = $subtablecounter++;
589 $subtableno{$base} = $subtable{$t};
592 print STDERR "> main table\n";
593 out("const struct unidata *const unidata[]={\n");
594 for(my $base = 0; $base <= $max; $base += $modulus) {
595 next if $base >= $break_start && $base < $break_end;
596 next if $base >= $break_top;
597 #out("st$subtableno{$base} /* ".sprintf("%04x", $base)." */,\n");
598 out("st$subtableno{$base},\n");
602 print STDERR "> UTF-8 table\n";
603 out("const struct unicode_utf8_row unicode_utf8_valid[] = {\n");
604 for(my $c = 0; $c <= 0x7F; ++$c) {
605 out(" { 1, 0, 0 }, /* $c */\n");
607 for(my $c = 0x80; $c < 0xC2; ++$c) {
608 out(" { 0, 0, 0 }, /* $c */\n");
610 for(my $c = 0xC2; $c <= 0xDF; ++$c) {
611 out(" { 2, 0x80, 0xBF }, /* $c */\n");
613 for(my $c = 0xE0; $c <= 0xE0; ++$c) {
614 out(" { 3, 0xA0, 0xBF }, /* $c */\n");
616 for(my $c = 0xE1; $c <= 0xEC; ++$c) {
617 out(" { 3, 0x80, 0xBF }, /* $c */\n");
619 for(my $c = 0xED; $c <= 0xED; ++$c) {
620 out(" { 3, 0x80, 0x9F }, /* $c */\n");
622 for(my $c = 0xEE; $c <= 0xEF; ++$c) {
623 out(" { 3, 0x80, 0xBF }, /* $c */\n");
625 for(my $c = 0xF0; $c <= 0xF0; ++$c) {
626 out(" { 4, 0x90, 0xBF }, /* $c */\n");
628 for(my $c = 0xF1; $c <= 0xF3; ++$c) {
629 out(" { 4, 0x80, 0xBF }, /* $c */\n");
631 for(my $c = 0xF4; $c <= 0xF4; ++$c) {
632 out(" { 4, 0x80, 0x8F }, /* $c */\n");
634 for(my $c = 0xF5; $c <= 0xFF; ++$c) {
635 out(" { 0, 0, 0 }, /* $c */\n");
639 close STDOUT or die "unidata.c: $!\n";
641 print STDERR "Done.\n\n";
642 printf STDERR "modulus=%d\n", $modulus;
643 printf STDERR "max=%04X\n", $max;
644 print STDERR "subtables=$subtablecounter, subtablessaved=$subtablessaved\n";
645 print STDERR "ddsaved=$ddsaved\n";
646 print STDERR "maxcompat=$maxcompat maxcanon=$maxcanon\n";
647 print STDERR "$hangul_syllable_decomps canonical decompositions to Hangul syllables\n";
648 print STDERR "$hangul_choseong_decomps canonical decompositions to Hangul Choseong\n";
650 die "We assumed that canonical decompositions were never more than 2 long!\n"
653 die "We assumed no canonical decompositions to Hangul syllables/Choseong!\n"
654 if $hangul_syllable_decomps || $hangul_choseong_decomps;