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