chiark / gitweb /
test and corrections for utf32_is_word_boundary()
[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
34# - Word boundary support
35# - ...
36#
37# NB the generated files DO NOT offer a stable ABI and so are not immediately
38# suitable for use in a general-purpose library. Things that would need to
39# be done:
40# - Hide unidata.h from applications; it will never be ABI- or even API-stable.
41# - Stablized General_Category values
42# - Extend the unicode.h API to general utility rather than just what
43# DisOrder needs.
44# - ...
45#
61507e3c 46use strict;
35b651f0 47use File::Basename;
61507e3c
RK
48
49sub out {
50 print @_ or die "$!\n";
51}
52
53sub key {
54 my $d = shift;
55 local $_;
56
57 return join("-", map($d->{$_}, sort keys %$d));
58}
59
e5a5a138
RK
60# Size of a subtable
61#
62# This can be varied to trade off the number of subtables against their size.
63our $modulus = 128;
64
61507e3c
RK
65my %cats = (); # known general categories
66my %data = (); # mapping of codepoints to information
61507e3c 67my $max = 0; # maximum codepoint
e5a5a138
RK
68my $maxccc = 0; # maximum combining class
69my $maxud = 0;
70my $minud = 0; # max/min upper case offset
71my $maxld = 0;
72my $minld = 0; # max/min lower case offset
73
74# Make sure we have our desired input files. We explicitly specify a
75# Unicode standard version to make sure that a given version of DisOrder
76# supports a given version of Unicode.
0b7052da 77sub input {
35b651f0
RK
78 my $path = shift;
79 my $lpath = basename($path);
80 if(!-e $lpath) {
81 system("wget http://www.unicode.org/Public/5.0.0/ucd/$path");
82 chmod(0444, $lpath) or die "$lpath: $!\n";
e5a5a138 83 }
0b7052da 84 open(STDIN, "<$lpath") or die "$lpath: $!\n";
e5a5a138 85}
61507e3c 86
e5a5a138
RK
87
88# Read the main data file
0b7052da 89input("UnicodeData.txt");
61507e3c
RK
90while(<>) {
91 my @f = split(/;/, $_);
92 my $c = hex($f[0]); # codepoint
93 next if $c >= 0xE0000; # ignore various high-numbered stuff
e5a5a138 94 # TODO justify this exclusion!
61507e3c 95 my $name = $f[1];
e5a5a138
RK
96 my $gc = $f[2]; # General_Category
97 my $ccc = $f[3]; # Canonical_Combining_Class
98 my $dm = $f[5]; # Decomposition_Type + Decomposition_Mapping
99 my $sum = hex($f[12]) || $c; # Simple_Uppercase_Mapping
100 my $slm = hex($f[13]) || $c; # Simple_Lowercase_Mapping
61507e3c
RK
101 # recalculate the upper/lower case mappings as offsets
102 my $ud = $sum - $c;
103 my $ld = $slm - $c;
e5a5a138
RK
104 # update bounds on various values
105 $maxccc = $ccc if $ccc > $maxccc; # assumed never to be -ve
106 $minud = $ud if $ud < $minud;
107 $maxud = $ud if $ud > $maxud;
108 $minld = $ld if $ld < $minld;
109 $maxld = $ld if $ld > $maxld;
61507e3c
RK
110 $data{$c} = {
111 "gc" => $gc,
112 "ccc" => $ccc,
113 "ud" => $ud,
e5a5a138 114 "ld" => $ld,
61507e3c 115 };
e5a5a138
RK
116 if($dm ne '') {
117 if($dm !~ /</) {
118 # This is a canonical decomposition
119 $data{$c}->{canon} = $dm;
120 $data{$c}->{compat} = $dm;
121 } else {
122 # This is only a compatibility decomposition
123 $dm =~ s/^<.*>\s*//;
124 $data{$c}->{compat} = $dm;
125 }
126 }
61507e3c
RK
127 $cats{$gc} = 1;
128 $max = $c if $c > $max;
129}
130
0b7052da
RK
131sub read_prop_with_ranges {
132 my $path = shift;
133 my $propkey = shift;
134 input($path);
135 while(<>) {
136 chomp;
137 s/\s*\#.*//;
138 next if $_ eq '';
139 my ($range, $propval) = split(/\s*;\s*/, $_);
140 if($range =~ /(.*)\.\.(.*)/) {
141 for my $c (hex($1) .. hex($2)) {
142 if(exists $data{$c}) {
143 $data{$c}->{$propkey} = $propval;
144 }
145 }
146 } else {
147 my $c = hex($range);
35b651f0 148 if(exists $data{$c}) {
0b7052da 149 $data{$c}->{$propkey} = $propval;
35b651f0
RK
150 }
151 }
0b7052da
RK
152 }
153}
154
155# Grapheme_Break
156# NB we do this BEFORE filling in blanks so that the Hangul characters
157# don't get filled in; we can compute their properties mechanically.
158read_prop_with_ranges("auxiliary/GraphemeBreakProperty.txt", "gbreak");
159
160# Word_Break
161# Same remarks about Hangul as above. This one currently seems just too
162# complicated to do programmatically so we'll take a byte to store it.
163read_prop_with_ranges("auxiliary/WordBreakProperty.txt", "wbreak");
164
165# Make the list of Word_Break values
166my %wbpropvals = ();
167for my $c (keys %data) {
168 if(!exists $data{$c}->{wbreak}) {
169 if(exists $data{$c}->{gbreak} && $data{$c}->{gbreak} eq 'Extend') {
170 $data{$c}->{wbreak} = 'Extend';
171 } else {
172 $data{$c}->{wbreak} = 'Other';
35b651f0
RK
173 }
174 }
0b7052da 175 $wbpropvals{$data{$c}->{wbreak}} = 1;
35b651f0
RK
176}
177
e5a5a138
RK
178# Round up the maximum value to a whole number of subtables
179$max += ($modulus - 1) - ($max % $modulus);
61507e3c
RK
180
181# Make sure there are no gaps
182for(my $c = 0; $c <= $max; ++$c) {
183 if(!exists $data{$c}) {
184 $data{$c} = {
185 "gc" => "Cn", # not assigned
186 "ccc" => 0,
187 "ud" => 0,
0b7052da
RK
188 "ld" => 0,
189 "wbreak" => 'Other',
61507e3c
RK
190 };
191 }
192}
193$cats{'Cn'} = 1;
194
e5a5a138 195# Read the casefolding data too
0b7052da 196input("CaseFolding.txt");
e5a5a138
RK
197while(<>) {
198 chomp;
199 next if /^\#/ or $_ eq '';
200 my @f = split(/\s*;\s*/, $_);
201 # Full case folding means use status C and F.
202 # We discard status T, Turkish users may wish to change this.
203 if($f[1] eq 'C' or $f[1] eq 'F') {
204 my $c = hex($f[0]);
205 $data{$c}->{casefold} = $f[2];
206 # We are particularly interest in combining characters that
207 # case-fold to non-combining characters, or characters that
208 # case-fold to sequences with combining characters in non-initial
209 # positions, as these required decomposiiton before case-folding
210 my @d = map(hex($_), split(/\s+/, $data{$c}->{casefold}));
211 if($data{$c}->{ccc} != 0) {
212 # This is a combining character
213 if($data{$d[0]}->{ccc} == 0) {
214 # The first character of its case-folded form is NOT
215 # a combining character. The field name is the example
216 # explicitly mentioned in the spec.
217 $data{$c}->{ypogegrammeni} = 1;
218 }
219 } else {
220 # This is a non-combining character; inspect the non-initial
221 # code points of the case-folded sequence
222 shift(@d);
223 if(grep($data{$_}->{ccc} != 0, @d)) {
224 # Some non-initial code point in the case-folded for is NOT a
225 # a combining character.
226 $data{$c}->{ypogegrammeni} = 1;
227 }
228 }
229 }
230}
231
232# Generate the header file
61507e3c
RK
233open(STDOUT, ">unidata.h") or die "unidata.h: $!\n";
234
e5a5a138
RK
235out("/* Automatically generated file, see scripts/make-unidata */\n",
236 "#ifndef UNIDATA_H\n",
61507e3c
RK
237 "#define UNIDATA_H\n");
238
e5a5a138 239# TODO choose stable values for General_Category
61507e3c
RK
240out("enum unicode_gc_cat {\n",
241 join(",\n",
242 map(" unicode_gc_$_", sort keys %cats)), "\n};\n");
e5a5a138 243
0b7052da
RK
244out("enum unicode_Word_Break {\n",
245 join(",\n",
246 map(" unicode_Word_Break_$_", sort keys %wbpropvals)),
247 "\n};\n");
bb48024f 248out("extern const char *const unicode_Word_Break_names[];\n");
0b7052da 249
e5a5a138 250out("enum unicode_flags {\n",
0b7052da 251 " unicode_normalize_before_casefold = 1\n",
e5a5a138
RK
252 "};\n",
253 "\n");
254
255# Choose the narrowest type that will fit the required values
256sub choosetype {
257 my ($min, $max) = @_;
258 if($min >= 0) {
259 return "char" if $max <= 127;
260 return "unsigned char" if $max <= 255;
261 return "int16_t" if $max < 32767;
262 return "uint16_t" if $max < 65535;
263 return "int32_t";
264 } else {
265 return "char" if $min >= -127 && $max <= 127;
266 return "int16_t" if $min >= -32767 && $max <= 32767;
267 return "int32_t";
268 }
269}
270
61507e3c 271out("struct unidata {\n",
e5a5a138
RK
272 " const uint32_t *compat;\n",
273 " const uint32_t *canon;\n",
274 " const uint32_t *casefold;\n",
275 " ".choosetype($minud, $maxud)." upper_offset;\n",
276 " ".choosetype($minld, $maxld)." lower_offset;\n",
277 " ".choosetype(0, $maxccc)." ccc;\n",
278 " char gc;\n",
279 " uint8_t flags;\n",
0b7052da 280 " char word_break;\n",
61507e3c 281 "};\n");
e5a5a138
RK
282# compat, canon and casefold do have have non-BMP characters, so we
283# can't use a simple 16-bit table. We could use UTF-8 or UTF-16
284# though, saving a bit of space (probably not that much...) at the
285# cost of marginally reduced performance and additional complexity
61507e3c
RK
286
287out("extern const struct unidata *const unidata[];\n");
288
289out("#define UNICODE_NCHARS ", ($max + 1), "\n");
e5a5a138 290out("#define UNICODE_MODULUS $modulus\n");
61507e3c
RK
291
292out("#endif\n");
293
294close STDOUT or die "unidata.h: $!\n";
295
296open(STDOUT, ">unidata.c") or die "unidata.c: $!\n";
297
e5a5a138
RK
298out("/* Automatically generated file, see scripts/make-unidata */\n",
299 "#include <config.h>\n",
300 "#include \"types.h\"\n",
301 "#include \"unidata.h\"\n");
302
303# Short aliases for general category codes
304
305out(map(sprintf("#define %s unicode_gc_%s\n", $_, $_), sort keys %cats));
306
bb48024f
RK
307# Names for Word_Break property
308
309out("const char *const unicode_Word_Break_names[] = {\n",
310 join(",\n",
311 map(" \"$_\"", sort keys %wbpropvals)),
312 "\n};\n");
313
e5a5a138
RK
314# Generate the decomposition mapping tables. We look out for duplicates
315# in order to save space and report this as decompsaved at the end. In
316# Unicode 5.0.0 this saves 1795 entries, which is at least 14Kbytes.
317my $decompnum = 0;
318my %decompnums = ();
319my $decompsaved = 0;
320out("static const uint32_t ");
321for(my $c = 0; $c <= $max; ++$c) {
322 # If canon is set then compat will be too and will be identical.
323 # If compat is set the canon might be clear. So we use the
324 # compat version and fix up the symbols after.
325 if(exists $data{$c}->{compat}) {
326 my $s = join(",",
327 (map(hex($_), split(/\s+/, $data{$c}->{compat})), 0));
328 if(!exists $decompnums{$s}) {
329 out(",\n") if $decompnum != 0;
330 out("cd$decompnum\[]={$s}");
331 $decompnums{$s} = $decompnum++;
332 } else {
333 ++$decompsaved;
334 }
335 $data{$c}->{compatsym} = "cd$decompnums{$s}";
336 if(exists $data{$c}->{canon}) {
337 $data{$c}->{canonsym} = "cd$decompnums{$s}";
338 }
339 }
340}
341out(";\n");
342
343# ...and the case folding table. Again we compress equal entries to save
344# space. In Unicode 5.0.0 this saves 51 entries or at least 408 bytes.
345# This doesns't seem as worthwhile as the decomposition mapping saving above.
346my $cfnum = 0;
347my %cfnums = ();
348my $cfsaved = 0;
349out("static const uint32_t ");
350for(my $c = 0; $c <= $max; ++$c) {
351 if(exists $data{$c}->{casefold}) {
352 my $s = join(",",
353 (map(hex($_), split(/\s+/, $data{$c}->{casefold})), 0));
354 if(!exists $cfnums{$s}) {
355 out(",\n") if $cfnum != 0;
356 out("cf$cfnum\[]={$s}");
357 $cfnums{$s} = $cfnum++;
358 } else {
359 ++$cfsaved;
360 }
361 $data{$c}->{cfsym} = "cf$cfnums{$s}";
362 }
363}
364out(";\n");
365
366# Visit all the $modulus-character blocks in turn and generate the
367# required subtables. As above we spot duplicates to save space. In
368# Unicode 5.0.0 with $modulus=128 and current table data this saves
369# 1372 subtables or at least three and a half megabytes on 32-bit
370# platforms.
61507e3c 371
61507e3c
RK
372my %subtable = (); # base->subtable number
373my %subtableno = (); # subtable number -> content
374my $subtablecounter = 0; # counter for subtable numbers
e5a5a138
RK
375my $subtablessaved = 0; # number of tables saved
376for(my $base = 0; $base <= $max; $base += $modulus) {
61507e3c 377 my @t;
e5a5a138 378 for(my $c = $base; $c < $base + $modulus; ++$c) {
61507e3c 379 my $d = $data{$c};
e5a5a138
RK
380 my $canonsym = ($data{$c}->{canonsym} or "0");
381 my $compatsym = ($data{$c}->{compatsym} or "0");
382 my $cfsym = ($data{$c}->{cfsym} or "0");
35b651f0
RK
383 my @flags = ();
384 if($data{$c}->{ypogegrammeni}) {
385 push(@flags, "unicode_normalize_before_casefold");
386 }
35b651f0 387 my $flags = @flags ? join("|", @flags) : 0;
e5a5a138
RK
388 push(@t, "{".
389 join(",",
390 $compatsym,
391 $canonsym,
392 $cfsym,
0b7052da
RK
393 $d->{ud},
394 $d->{ld},
395 $d->{ccc},
396 $d->{gc},
e5a5a138 397 $flags,
0b7052da 398 "unicode_Word_Break_$d->{wbreak}",
e5a5a138 399 )."}");
61507e3c
RK
400 }
401 my $t = join(",\n", @t);
402 if(!exists $subtable{$t}) {
e5a5a138 403 out("static const struct unidata st$subtablecounter\[] = {\n",
61507e3c
RK
404 "$t\n",
405 "};\n");
406 $subtable{$t} = $subtablecounter++;
e5a5a138
RK
407 } else {
408 ++$subtablessaved;
61507e3c
RK
409 }
410 $subtableno{$base} = $subtable{$t};
411}
412
e5a5a138
RK
413out("const struct unidata*const unidata[]={\n");
414for(my $base = 0; $base <= $max; $base += $modulus) {
415 out("st$subtableno{$base},\n");
61507e3c
RK
416}
417out("};\n");
418
419close STDOUT or die "unidata.c: $!\n";
420
e5a5a138
RK
421print STDERR "max=$max, subtables=$subtablecounter, subtablessaved=$subtablessaved\n";
422print STDERR "decompsaved=$decompsaved cfsaved=$cfsaved\n";