chiark / gitweb /
distinguish the different decompositions via a flag bit
[disorder] / scripts / make-unidata
CommitLineData
61507e3c
RK
1#! /usr/bin/perl -w
2#
e5a5a138
RK
3# This file is part of DisOrder.
4# Copyright (C) 2007 Richard Kettlewell
5#
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.
10#
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.
15#
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
19# USA
20#
21#
22# Generate Unicode support tables
23#
24# This script will download data from unicode.org if the required files
25# aren't in the current directory.
26#
27# After modifying this script you should run:
28# make -C lib rebuild-unicode check
29#
30# Things not supported yet:
31# - SpecialCasing.txt data for case mapping
32# - Title case offsets
33# - Some kind of hinting for composition
e5a5a138
RK
34# - ...
35#
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
38# be done:
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
42# DisOrder needs.
43# - ...
44#
61507e3c 45use strict;
35b651f0 46use File::Basename;
61507e3c
RK
47
48sub out {
49 print @_ or die "$!\n";
50}
51
52sub key {
53 my $d = shift;
54 local $_;
55
56 return join("-", map($d->{$_}, sort keys %$d));
57}
58
e5a5a138
RK
59# Size of a subtable
60#
61# This can be varied to trade off the number of subtables against their size.
c2e01e0a
RK
62# 16 gave the smallest results last time I checked (on a Mac with a 32-bit
63# build).
64our $modulus = 16;
65
66if(@ARGV) {
67 $modulus = shift;
68}
e5a5a138 69
1a05e381
RK
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.
74our $break_start = 0x30000;
75our $break_end = 0xE0000;
76
77# Similarly we simply omit the very top of the table and sort it out in the
78# lookup function.
79our $break_top = 0xE0200;
80
61507e3c
RK
81my %cats = (); # known general categories
82my %data = (); # mapping of codepoints to information
61507e3c 83my $max = 0; # maximum codepoint
e5a5a138
RK
84my $maxccc = 0; # maximum combining class
85my $maxud = 0;
86my $minud = 0; # max/min upper case offset
87my $maxld = 0;
88my $minld = 0; # max/min lower case offset
89
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.
0b7052da 93sub input {
35b651f0
RK
94 my $path = shift;
95 my $lpath = basename($path);
96 if(!-e $lpath) {
97 system("wget http://www.unicode.org/Public/5.0.0/ucd/$path");
98 chmod(0444, $lpath) or die "$lpath: $!\n";
e5a5a138 99 }
0b7052da 100 open(STDIN, "<$lpath") or die "$lpath: $!\n";
bcf9ed7f 101 print STDERR "Reading $lpath...\n";
e5a5a138 102}
61507e3c 103
e5a5a138
RK
104
105# Read the main data file
0b7052da 106input("UnicodeData.txt");
1a05e381 107my ($start, $end);
61507e3c
RK
108while(<>) {
109 my @f = split(/;/, $_);
110 my $c = hex($f[0]); # codepoint
61507e3c 111 my $name = $f[1];
1a05e381
RK
112 die "$f[0] $name is in the break\n"
113 if $c >= $break_start && $c < $break_end;
e5a5a138 114 my $gc = $f[2]; # General_Category
bcf9ed7f 115 # Variuos GCs we don't expect to see in UnicodeData.txt
0a4af692 116 $cats{$gc} = 1; # always record all GCs
1a05e381
RK
117 if($name =~ /first>/i) {
118 $start = $c;
119 next;
120 } elsif($name =~ /last>/i) {
121 $end = $c;
122 } else {
123 $start = $end = $c;
124 }
bcf9ed7f 125 die "unexpected Cn" if $gc eq 'Cn';
e5a5a138
RK
126 my $ccc = $f[3]; # Canonical_Combining_Class
127 my $dm = $f[5]; # Decomposition_Type + Decomposition_Mapping
128 my $sum = hex($f[12]) || $c; # Simple_Uppercase_Mapping
129 my $slm = hex($f[13]) || $c; # Simple_Lowercase_Mapping
61507e3c
RK
130 # recalculate the upper/lower case mappings as offsets
131 my $ud = $sum - $c;
132 my $ld = $slm - $c;
e5a5a138
RK
133 # update bounds on various values
134 $maxccc = $ccc if $ccc > $maxccc; # assumed never to be -ve
135 $minud = $ud if $ud < $minud;
136 $maxud = $ud if $ud > $maxud;
137 $minld = $ld if $ld < $minld;
138 $maxld = $ld if $ld > $maxld;
1a05e381 139 if($start != $end) {
0e843521 140 printf STDERR "> range %04X-%04X is %s\n", $start, $end, $gc;
1a05e381
RK
141 }
142 for($c = $start; $c <= $end; ++$c) {
0e843521
RK
143 my $d = {
144 "gc" => $gc,
145 "ccc" => $ccc,
146 "ud" => $ud,
147 "ld" => $ld,
148 };
149 if($dm ne '') {
f98fcddb
RK
150 if($dm =~ /</) {
151 # This is a compatibility decomposition
0e843521 152 $dm =~ s/^<.*>\s*//;
f98fcddb 153 $d->{compat} = 1;
0e843521 154 }
f98fcddb 155 $d->{decomp} = $dm;
0e843521 156 }
1a05e381
RK
157 $data{$c} = $d;
158 }
61507e3c 159 $cats{$gc} = 1;
1a05e381 160 $max = $end if $end > $max;
61507e3c
RK
161}
162
0b7052da
RK
163sub read_prop_with_ranges {
164 my $path = shift;
165 my $propkey = shift;
166 input($path);
167 while(<>) {
168 chomp;
169 s/\s*\#.*//;
170 next if $_ eq '';
171 my ($range, $propval) = split(/\s*;\s*/, $_);
172 if($range =~ /(.*)\.\.(.*)/) {
173 for my $c (hex($1) .. hex($2)) {
0e843521
RK
174 die "($range)\n" if($c == 0xAC00 and $propkey eq 'gbreak');
175 $data{$c}->{$propkey} = $propval;
0b7052da
RK
176 }
177 } else {
178 my $c = hex($range);
0e843521 179 $data{$c}->{$propkey} = $propval;
35b651f0 180 }
0b7052da
RK
181 }
182}
183
349b7b74 184# Grapheme_Break etc
0b7052da 185read_prop_with_ranges("auxiliary/GraphemeBreakProperty.txt", "gbreak");
0b7052da 186read_prop_with_ranges("auxiliary/WordBreakProperty.txt", "wbreak");
349b7b74 187read_prop_with_ranges("auxiliary/SentenceBreakProperty.txt", "sbreak");
0b7052da 188
349b7b74
RK
189# Compute the full list and fill in the Extend category properly
190my %gbreak = ();
191my %wbreak = ();
192my %sbreak = ();
0b7052da 193for my $c (keys %data) {
349b7b74
RK
194 if(!exists $data{$c}->{gbreak}) {
195 $data{$c}->{gbreak} = 'Other';
196 }
197 $gbreak{$data{$c}->{gbreak}} = 1;
198
0b7052da 199 if(!exists $data{$c}->{wbreak}) {
349b7b74 200 if($data{$c}->{gbreak} eq 'Extend') {
0b7052da
RK
201 $data{$c}->{wbreak} = 'Extend';
202 } else {
203 $data{$c}->{wbreak} = 'Other';
35b651f0
RK
204 }
205 }
349b7b74
RK
206 $wbreak{$data{$c}->{wbreak}} = 1;
207
208 if(!exists $data{$c}->{sbreak}) {
209 if($data{$c}->{gbreak} eq 'Extend') {
210 $data{$c}->{sbreak} = 'Extend';
211 } else {
212 $data{$c}->{sbreak} = 'Other';
213 }
214 }
215 $sbreak{$data{$c}->{sbreak}} = 1;
35b651f0
RK
216}
217
e5a5a138
RK
218# Round up the maximum value to a whole number of subtables
219$max += ($modulus - 1) - ($max % $modulus);
61507e3c 220
bcf9ed7f
RK
221# Private use characters
222# We only fill in values below $max, utf32__unidata()
223my $Co = {
224 "gc" => "Co",
225 "ccc" => 0,
226 "ud" => 0,
227 "ld" => 0
228};
229for(my $c = 0xE000; $c <= 0xF8FF && $c <= $max; ++$c) {
230 $data{$c} = $Co;
231}
232for(my $c = 0xF0000; $c <= 0xFFFFD && $c <= $max; ++$c) {
233 $data{$c} = $Co;
234}
235for(my $c = 0x100000; $c <= 0x10FFFD && $c <= $max; ++$c) {
236 $data{$c} = $Co;
237}
238
239# Anything left is not assigned
240my $Cn = {
241 "gc" => "Cn", # not assigned
242 "ccc" => 0,
243 "ud" => 0,
244 "ld" => 0
245};
61507e3c
RK
246for(my $c = 0; $c <= $max; ++$c) {
247 if(!exists $data{$c}) {
bcf9ed7f
RK
248 $data{$c} = $Cn;
249 }
250 if(!exists $data{$c}->{wbreak}) {
251 $data{$c}->{wbreak} = 'Other';
252 }
253 if(!exists $data{$c}->{gbreak}) {
254 $data{$c}->{gbreak} = 'Other';
255 }
256 if(!exists $data{$c}->{sbreak}) {
257 $data{$c}->{sbreak} = 'Other';
61507e3c
RK
258 }
259}
260$cats{'Cn'} = 1;
261
e5a5a138 262# Read the casefolding data too
0b7052da 263input("CaseFolding.txt");
e5a5a138
RK
264while(<>) {
265 chomp;
266 next if /^\#/ or $_ eq '';
267 my @f = split(/\s*;\s*/, $_);
268 # Full case folding means use status C and F.
269 # We discard status T, Turkish users may wish to change this.
270 if($f[1] eq 'C' or $f[1] eq 'F') {
271 my $c = hex($f[0]);
272 $data{$c}->{casefold} = $f[2];
273 # We are particularly interest in combining characters that
274 # case-fold to non-combining characters, or characters that
275 # case-fold to sequences with combining characters in non-initial
276 # positions, as these required decomposiiton before case-folding
277 my @d = map(hex($_), split(/\s+/, $data{$c}->{casefold}));
278 if($data{$c}->{ccc} != 0) {
279 # This is a combining character
280 if($data{$d[0]}->{ccc} == 0) {
281 # The first character of its case-folded form is NOT
282 # a combining character. The field name is the example
283 # explicitly mentioned in the spec.
284 $data{$c}->{ypogegrammeni} = 1;
285 }
286 } else {
287 # This is a non-combining character; inspect the non-initial
288 # code points of the case-folded sequence
289 shift(@d);
290 if(grep($data{$_}->{ccc} != 0, @d)) {
291 # Some non-initial code point in the case-folded for is NOT a
292 # a combining character.
293 $data{$c}->{ypogegrammeni} = 1;
294 }
295 }
296 }
297}
298
299# Generate the header file
bcf9ed7f 300print STDERR "Generating unidata.h...\n";
61507e3c
RK
301open(STDOUT, ">unidata.h") or die "unidata.h: $!\n";
302
e5a5a138
RK
303out("/* Automatically generated file, see scripts/make-unidata */\n",
304 "#ifndef UNIDATA_H\n",
61507e3c
RK
305 "#define UNIDATA_H\n");
306
e5a5a138 307# TODO choose stable values for General_Category
14523635 308out("enum unicode_General_Category {\n",
61507e3c 309 join(",\n",
14523635 310 map(" unicode_General_Category_$_", sort keys %cats)), "\n};\n");
e5a5a138 311
349b7b74
RK
312out("enum unicode_Grapheme_Break {\n",
313 join(",\n",
314 map(" unicode_Grapheme_Break_$_", sort keys %gbreak)),
315 "\n};\n");
316out("extern const char *const unicode_Grapheme_Break_names[];\n");
317
0b7052da
RK
318out("enum unicode_Word_Break {\n",
319 join(",\n",
349b7b74 320 map(" unicode_Word_Break_$_", sort keys %wbreak)),
0b7052da 321 "\n};\n");
bb48024f 322out("extern const char *const unicode_Word_Break_names[];\n");
0b7052da 323
349b7b74
RK
324out("enum unicode_Sentence_Break {\n",
325 join(",\n",
326 map(" unicode_Sentence_Break_$_", sort keys %sbreak)),
327 "\n};\n");
328out("extern const char *const unicode_Sentence_Break_names[];\n");
329
e5a5a138 330out("enum unicode_flags {\n",
f98fcddb
RK
331 " unicode_normalize_before_casefold = 1,\n",
332 " unicode_compatibility_decomposition = 2\n",
e5a5a138
RK
333 "};\n",
334 "\n");
335
336# Choose the narrowest type that will fit the required values
337sub choosetype {
338 my ($min, $max) = @_;
339 if($min >= 0) {
340 return "char" if $max <= 127;
341 return "unsigned char" if $max <= 255;
342 return "int16_t" if $max < 32767;
343 return "uint16_t" if $max < 65535;
344 return "int32_t";
345 } else {
346 return "char" if $min >= -127 && $max <= 127;
347 return "int16_t" if $min >= -32767 && $max <= 32767;
348 return "int32_t";
349 }
350}
351
61507e3c 352out("struct unidata {\n",
f98fcddb 353 " const uint32_t *decomp;\n",
e5a5a138 354 " const uint32_t *casefold;\n",
1a05e381
RK
355# " ".choosetype($minud, $maxud)." upper_offset;\n",
356# " ".choosetype($minld, $maxld)." lower_offset;\n",
e5a5a138 357 " ".choosetype(0, $maxccc)." ccc;\n",
14523635 358 " char general_category;\n",
e5a5a138 359 " uint8_t flags;\n",
349b7b74 360 " char grapheme_break;\n",
0b7052da 361 " char word_break;\n",
349b7b74 362 " char sentence_break;\n",
61507e3c 363 "};\n");
f98fcddb 364# decomp and casefold do have have non-BMP characters, so we
e5a5a138
RK
365# can't use a simple 16-bit table. We could use UTF-8 or UTF-16
366# though, saving a bit of space (probably not that much...) at the
367# cost of marginally reduced performance and additional complexity
61507e3c
RK
368
369out("extern const struct unidata *const unidata[];\n");
370
18cda350
RK
371out("extern const struct unicode_utf8_row {\n",
372 " uint8_t count;\n",
373 " uint8_t min2, max2;\n",
374 "} unicode_utf8_valid[];\n");
375
61507e3c 376out("#define UNICODE_NCHARS ", ($max + 1), "\n");
e5a5a138 377out("#define UNICODE_MODULUS $modulus\n");
1a05e381
RK
378out("#define UNICODE_BREAK_START $break_start\n");
379out("#define UNICODE_BREAK_END $break_end\n");
380out("#define UNICODE_BREAK_TOP $break_top\n");
61507e3c
RK
381
382out("#endif\n");
383
384close STDOUT or die "unidata.h: $!\n";
385
bcf9ed7f 386print STDERR "Generating unidata.c...\n";
61507e3c
RK
387open(STDOUT, ">unidata.c") or die "unidata.c: $!\n";
388
e5a5a138
RK
389out("/* Automatically generated file, see scripts/make-unidata */\n",
390 "#include <config.h>\n",
391 "#include \"types.h\"\n",
392 "#include \"unidata.h\"\n");
393
349b7b74 394# Short aliases to keep .c file small
e5a5a138 395
14523635
RK
396out(map(sprintf("#define %s unicode_General_Category_%s\n", $_, $_),
397 sort keys %cats));
398out(map(sprintf("#define GB%s unicode_Grapheme_Break_%s\n", $_, $_),
399 sort keys %gbreak));
400out(map(sprintf("#define WB%s unicode_Word_Break_%s\n", $_, $_),
401 sort keys %wbreak));
402out(map(sprintf("#define SB%s unicode_Sentence_Break_%s\n", $_, $_),
403 sort keys %sbreak));
e5a5a138 404
349b7b74
RK
405# Names for *_Break properties
406out("const char *const unicode_Grapheme_Break_names[] = {\n",
407 join(",\n",
408 map(" \"$_\"", sort keys %gbreak)),
409 "\n};\n");
bb48024f
RK
410out("const char *const unicode_Word_Break_names[] = {\n",
411 join(",\n",
349b7b74
RK
412 map(" \"$_\"", sort keys %wbreak)),
413 "\n};\n");
414out("const char *const unicode_Sentence_Break_names[] = {\n",
415 join(",\n",
416 map(" \"$_\"", sort keys %sbreak)),
bb48024f
RK
417 "\n};\n");
418
e5a5a138
RK
419# Generate the decomposition mapping tables. We look out for duplicates
420# in order to save space and report this as decompsaved at the end. In
421# Unicode 5.0.0 this saves 1795 entries, which is at least 14Kbytes.
422my $decompnum = 0;
423my %decompnums = ();
424my $decompsaved = 0;
425out("static const uint32_t ");
426for(my $c = 0; $c <= $max; ++$c) {
f98fcddb 427 if(exists $data{$c} && exists $data{$c}->{decomp}) {
e5a5a138 428 my $s = join(",",
f98fcddb 429 (map(hex($_), split(/\s+/, $data{$c}->{decomp})), 0));
e5a5a138
RK
430 if(!exists $decompnums{$s}) {
431 out(",\n") if $decompnum != 0;
432 out("cd$decompnum\[]={$s}");
433 $decompnums{$s} = $decompnum++;
434 } else {
435 ++$decompsaved;
436 }
f98fcddb 437 $data{$c}->{decompsym} = "cd$decompnums{$s}";
e5a5a138
RK
438 }
439}
440out(";\n");
441
442# ...and the case folding table. Again we compress equal entries to save
443# space. In Unicode 5.0.0 this saves 51 entries or at least 408 bytes.
444# This doesns't seem as worthwhile as the decomposition mapping saving above.
445my $cfnum = 0;
446my %cfnums = ();
447my $cfsaved = 0;
448out("static const uint32_t ");
449for(my $c = 0; $c <= $max; ++$c) {
bcf9ed7f 450 if(exists $data{$c} && exists $data{$c}->{casefold}) {
e5a5a138
RK
451 my $s = join(",",
452 (map(hex($_), split(/\s+/, $data{$c}->{casefold})), 0));
453 if(!exists $cfnums{$s}) {
454 out(",\n") if $cfnum != 0;
455 out("cf$cfnum\[]={$s}");
456 $cfnums{$s} = $cfnum++;
457 } else {
458 ++$cfsaved;
459 }
460 $data{$c}->{cfsym} = "cf$cfnums{$s}";
461 }
462}
463out(";\n");
464
465# Visit all the $modulus-character blocks in turn and generate the
466# required subtables. As above we spot duplicates to save space. In
467# Unicode 5.0.0 with $modulus=128 and current table data this saves
468# 1372 subtables or at least three and a half megabytes on 32-bit
469# platforms.
61507e3c 470
61507e3c
RK
471my %subtable = (); # base->subtable number
472my %subtableno = (); # subtable number -> content
473my $subtablecounter = 0; # counter for subtable numbers
e5a5a138
RK
474my $subtablessaved = 0; # number of tables saved
475for(my $base = 0; $base <= $max; $base += $modulus) {
1a05e381
RK
476 next if $base >= $break_start && $base < $break_end;
477 next if $base >= $break_top;
61507e3c 478 my @t;
e5a5a138 479 for(my $c = $base; $c < $base + $modulus; ++$c) {
61507e3c 480 my $d = $data{$c};
f98fcddb 481 my $decompsym = ($data{$c}->{decompsym} or "0");
e5a5a138 482 my $cfsym = ($data{$c}->{cfsym} or "0");
35b651f0
RK
483 my @flags = ();
484 if($data{$c}->{ypogegrammeni}) {
485 push(@flags, "unicode_normalize_before_casefold");
486 }
f98fcddb
RK
487 if($data{$c}->{compat}) {
488 push(@flags, "unicode_compatibility_decomposition");
489 }
35b651f0 490 my $flags = @flags ? join("|", @flags) : 0;
e5a5a138
RK
491 push(@t, "{".
492 join(",",
f98fcddb 493 $decompsym,
e5a5a138 494 $cfsym,
1a05e381
RK
495# $d->{ud},
496# $d->{ld},
0b7052da
RK
497 $d->{ccc},
498 $d->{gc},
e5a5a138 499 $flags,
349b7b74
RK
500 "GB$d->{gbreak}",
501 "WB$d->{wbreak}",
502 "SB$d->{sbreak}",
e5a5a138 503 )."}");
61507e3c
RK
504 }
505 my $t = join(",\n", @t);
506 if(!exists $subtable{$t}) {
0e843521 507 out(sprintf("/* %04X-%04X */\n", $base, $base + $modulus - 1));
e5a5a138 508 out("static const struct unidata st$subtablecounter\[] = {\n",
61507e3c
RK
509 "$t\n",
510 "};\n");
511 $subtable{$t} = $subtablecounter++;
e5a5a138
RK
512 } else {
513 ++$subtablessaved;
61507e3c
RK
514 }
515 $subtableno{$base} = $subtable{$t};
516}
517
bcf9ed7f 518out("const struct unidata *const unidata[]={\n");
e5a5a138 519for(my $base = 0; $base <= $max; $base += $modulus) {
1a05e381
RK
520 next if $base >= $break_start && $base < $break_end;
521 next if $base >= $break_top;
0a4af692 522 #out("st$subtableno{$base} /* ".sprintf("%04x", $base)." */,\n");
e5a5a138 523 out("st$subtableno{$base},\n");
61507e3c
RK
524}
525out("};\n");
526
18cda350
RK
527out("const struct unicode_utf8_row unicode_utf8_valid[] = {\n");
528for(my $c = 0; $c <= 0x7F; ++$c) {
529 out(" { 1, 0, 0 }, /* $c */\n");
530}
531for(my $c = 0x80; $c < 0xC2; ++$c) {
532 out(" { 0, 0, 0 }, /* $c */\n");
533}
534for(my $c = 0xC2; $c <= 0xDF; ++$c) {
535 out(" { 2, 0x80, 0xBF }, /* $c */\n");
536}
537for(my $c = 0xE0; $c <= 0xE0; ++$c) {
538 out(" { 3, 0xA0, 0xBF }, /* $c */\n");
539}
540for(my $c = 0xE1; $c <= 0xEC; ++$c) {
541 out(" { 3, 0x80, 0xBF }, /* $c */\n");
542}
543for(my $c = 0xED; $c <= 0xED; ++$c) {
544 out(" { 3, 0x80, 0x9F }, /* $c */\n");
545}
546for(my $c = 0xEE; $c <= 0xEF; ++$c) {
547 out(" { 3, 0x80, 0xBF }, /* $c */\n");
548}
549for(my $c = 0xF0; $c <= 0xF0; ++$c) {
550 out(" { 4, 0x90, 0xBF }, /* $c */\n");
551}
552for(my $c = 0xF1; $c <= 0xF3; ++$c) {
553 out(" { 4, 0x80, 0xBF }, /* $c */\n");
554}
555for(my $c = 0xF4; $c <= 0xF4; ++$c) {
556 out(" { 4, 0x80, 0x8F }, /* $c */\n");
557}
558for(my $c = 0xF5; $c <= 0xFF; ++$c) {
559 out(" { 0, 0, 0 }, /* $c */\n");
560}
561out("};\n");
562
61507e3c
RK
563close STDOUT or die "unidata.c: $!\n";
564
c2e01e0a 565printf STDERR "modulus=%d\n", $modulus;
bcf9ed7f
RK
566printf STDERR "max=%04X\n", $max;
567print STDERR "subtables=$subtablecounter, subtablessaved=$subtablessaved\n";
e5a5a138 568print STDERR "decompsaved=$decompsaved cfsaved=$cfsaved\n";