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