Commit | Line | Data |
---|---|---|
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 | 46 | use strict; |
35b651f0 | 47 | use File::Basename; |
61507e3c RK |
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 | ||
e5a5a138 RK |
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 | ||
61507e3c RK |
65 | my %cats = (); # known general categories |
66 | my %data = (); # mapping of codepoints to information | |
61507e3c | 67 | my $max = 0; # maximum codepoint |
e5a5a138 RK |
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. | |
0b7052da | 77 | sub 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 | 89 | input("UnicodeData.txt"); |
61507e3c RK |
90 | while(<>) { |
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 |
131 | sub 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 | ||
349b7b74 | 155 | # Grapheme_Break etc |
0b7052da RK |
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. | |
158 | read_prop_with_ranges("auxiliary/GraphemeBreakProperty.txt", "gbreak"); | |
0b7052da | 159 | read_prop_with_ranges("auxiliary/WordBreakProperty.txt", "wbreak"); |
349b7b74 | 160 | read_prop_with_ranges("auxiliary/SentenceBreakProperty.txt", "sbreak"); |
0b7052da | 161 | |
349b7b74 RK |
162 | # Compute the full list and fill in the Extend category properly |
163 | my %gbreak = (); | |
164 | my %wbreak = (); | |
165 | my %sbreak = (); | |
0b7052da | 166 | for my $c (keys %data) { |
349b7b74 RK |
167 | if(!exists $data{$c}->{gbreak}) { |
168 | $data{$c}->{gbreak} = 'Other'; | |
169 | } | |
170 | $gbreak{$data{$c}->{gbreak}} = 1; | |
171 | ||
0b7052da | 172 | if(!exists $data{$c}->{wbreak}) { |
349b7b74 | 173 | if($data{$c}->{gbreak} eq 'Extend') { |
0b7052da RK |
174 | $data{$c}->{wbreak} = 'Extend'; |
175 | } else { | |
176 | $data{$c}->{wbreak} = 'Other'; | |
35b651f0 RK |
177 | } |
178 | } | |
349b7b74 RK |
179 | $wbreak{$data{$c}->{wbreak}} = 1; |
180 | ||
181 | if(!exists $data{$c}->{sbreak}) { | |
182 | if($data{$c}->{gbreak} eq 'Extend') { | |
183 | $data{$c}->{sbreak} = 'Extend'; | |
184 | } else { | |
185 | $data{$c}->{sbreak} = 'Other'; | |
186 | } | |
187 | } | |
188 | $sbreak{$data{$c}->{sbreak}} = 1; | |
35b651f0 RK |
189 | } |
190 | ||
e5a5a138 RK |
191 | # Round up the maximum value to a whole number of subtables |
192 | $max += ($modulus - 1) - ($max % $modulus); | |
61507e3c RK |
193 | |
194 | # Make sure there are no gaps | |
195 | for(my $c = 0; $c <= $max; ++$c) { | |
196 | if(!exists $data{$c}) { | |
197 | $data{$c} = { | |
198 | "gc" => "Cn", # not assigned | |
199 | "ccc" => 0, | |
200 | "ud" => 0, | |
0b7052da RK |
201 | "ld" => 0, |
202 | "wbreak" => 'Other', | |
349b7b74 RK |
203 | "gbreak" => 'Other', |
204 | "sbreak" => 'Other', | |
61507e3c RK |
205 | }; |
206 | } | |
207 | } | |
208 | $cats{'Cn'} = 1; | |
209 | ||
e5a5a138 | 210 | # Read the casefolding data too |
0b7052da | 211 | input("CaseFolding.txt"); |
e5a5a138 RK |
212 | while(<>) { |
213 | chomp; | |
214 | next if /^\#/ or $_ eq ''; | |
215 | my @f = split(/\s*;\s*/, $_); | |
216 | # Full case folding means use status C and F. | |
217 | # We discard status T, Turkish users may wish to change this. | |
218 | if($f[1] eq 'C' or $f[1] eq 'F') { | |
219 | my $c = hex($f[0]); | |
220 | $data{$c}->{casefold} = $f[2]; | |
221 | # We are particularly interest in combining characters that | |
222 | # case-fold to non-combining characters, or characters that | |
223 | # case-fold to sequences with combining characters in non-initial | |
224 | # positions, as these required decomposiiton before case-folding | |
225 | my @d = map(hex($_), split(/\s+/, $data{$c}->{casefold})); | |
226 | if($data{$c}->{ccc} != 0) { | |
227 | # This is a combining character | |
228 | if($data{$d[0]}->{ccc} == 0) { | |
229 | # The first character of its case-folded form is NOT | |
230 | # a combining character. The field name is the example | |
231 | # explicitly mentioned in the spec. | |
232 | $data{$c}->{ypogegrammeni} = 1; | |
233 | } | |
234 | } else { | |
235 | # This is a non-combining character; inspect the non-initial | |
236 | # code points of the case-folded sequence | |
237 | shift(@d); | |
238 | if(grep($data{$_}->{ccc} != 0, @d)) { | |
239 | # Some non-initial code point in the case-folded for is NOT a | |
240 | # a combining character. | |
241 | $data{$c}->{ypogegrammeni} = 1; | |
242 | } | |
243 | } | |
244 | } | |
245 | } | |
246 | ||
247 | # Generate the header file | |
61507e3c RK |
248 | open(STDOUT, ">unidata.h") or die "unidata.h: $!\n"; |
249 | ||
e5a5a138 RK |
250 | out("/* Automatically generated file, see scripts/make-unidata */\n", |
251 | "#ifndef UNIDATA_H\n", | |
61507e3c RK |
252 | "#define UNIDATA_H\n"); |
253 | ||
e5a5a138 | 254 | # TODO choose stable values for General_Category |
14523635 | 255 | out("enum unicode_General_Category {\n", |
61507e3c | 256 | join(",\n", |
14523635 | 257 | map(" unicode_General_Category_$_", sort keys %cats)), "\n};\n"); |
e5a5a138 | 258 | |
349b7b74 RK |
259 | out("enum unicode_Grapheme_Break {\n", |
260 | join(",\n", | |
261 | map(" unicode_Grapheme_Break_$_", sort keys %gbreak)), | |
262 | "\n};\n"); | |
263 | out("extern const char *const unicode_Grapheme_Break_names[];\n"); | |
264 | ||
0b7052da RK |
265 | out("enum unicode_Word_Break {\n", |
266 | join(",\n", | |
349b7b74 | 267 | map(" unicode_Word_Break_$_", sort keys %wbreak)), |
0b7052da | 268 | "\n};\n"); |
bb48024f | 269 | out("extern const char *const unicode_Word_Break_names[];\n"); |
0b7052da | 270 | |
349b7b74 RK |
271 | out("enum unicode_Sentence_Break {\n", |
272 | join(",\n", | |
273 | map(" unicode_Sentence_Break_$_", sort keys %sbreak)), | |
274 | "\n};\n"); | |
275 | out("extern const char *const unicode_Sentence_Break_names[];\n"); | |
276 | ||
e5a5a138 | 277 | out("enum unicode_flags {\n", |
0b7052da | 278 | " unicode_normalize_before_casefold = 1\n", |
e5a5a138 RK |
279 | "};\n", |
280 | "\n"); | |
281 | ||
282 | # Choose the narrowest type that will fit the required values | |
283 | sub choosetype { | |
284 | my ($min, $max) = @_; | |
285 | if($min >= 0) { | |
286 | return "char" if $max <= 127; | |
287 | return "unsigned char" if $max <= 255; | |
288 | return "int16_t" if $max < 32767; | |
289 | return "uint16_t" if $max < 65535; | |
290 | return "int32_t"; | |
291 | } else { | |
292 | return "char" if $min >= -127 && $max <= 127; | |
293 | return "int16_t" if $min >= -32767 && $max <= 32767; | |
294 | return "int32_t"; | |
295 | } | |
296 | } | |
297 | ||
61507e3c | 298 | out("struct unidata {\n", |
e5a5a138 RK |
299 | " const uint32_t *compat;\n", |
300 | " const uint32_t *canon;\n", | |
301 | " const uint32_t *casefold;\n", | |
302 | " ".choosetype($minud, $maxud)." upper_offset;\n", | |
303 | " ".choosetype($minld, $maxld)." lower_offset;\n", | |
304 | " ".choosetype(0, $maxccc)." ccc;\n", | |
14523635 | 305 | " char general_category;\n", |
e5a5a138 | 306 | " uint8_t flags;\n", |
349b7b74 | 307 | " char grapheme_break;\n", |
0b7052da | 308 | " char word_break;\n", |
349b7b74 | 309 | " char sentence_break;\n", |
61507e3c | 310 | "};\n"); |
e5a5a138 RK |
311 | # compat, canon and casefold do have have non-BMP characters, so we |
312 | # can't use a simple 16-bit table. We could use UTF-8 or UTF-16 | |
313 | # though, saving a bit of space (probably not that much...) at the | |
314 | # cost of marginally reduced performance and additional complexity | |
61507e3c RK |
315 | |
316 | out("extern const struct unidata *const unidata[];\n"); | |
317 | ||
318 | out("#define UNICODE_NCHARS ", ($max + 1), "\n"); | |
e5a5a138 | 319 | out("#define UNICODE_MODULUS $modulus\n"); |
61507e3c RK |
320 | |
321 | out("#endif\n"); | |
322 | ||
323 | close STDOUT or die "unidata.h: $!\n"; | |
324 | ||
325 | open(STDOUT, ">unidata.c") or die "unidata.c: $!\n"; | |
326 | ||
e5a5a138 RK |
327 | out("/* Automatically generated file, see scripts/make-unidata */\n", |
328 | "#include <config.h>\n", | |
329 | "#include \"types.h\"\n", | |
330 | "#include \"unidata.h\"\n"); | |
331 | ||
349b7b74 | 332 | # Short aliases to keep .c file small |
e5a5a138 | 333 | |
14523635 RK |
334 | out(map(sprintf("#define %s unicode_General_Category_%s\n", $_, $_), |
335 | sort keys %cats)); | |
336 | out(map(sprintf("#define GB%s unicode_Grapheme_Break_%s\n", $_, $_), | |
337 | sort keys %gbreak)); | |
338 | out(map(sprintf("#define WB%s unicode_Word_Break_%s\n", $_, $_), | |
339 | sort keys %wbreak)); | |
340 | out(map(sprintf("#define SB%s unicode_Sentence_Break_%s\n", $_, $_), | |
341 | sort keys %sbreak)); | |
e5a5a138 | 342 | |
349b7b74 RK |
343 | # Names for *_Break properties |
344 | out("const char *const unicode_Grapheme_Break_names[] = {\n", | |
345 | join(",\n", | |
346 | map(" \"$_\"", sort keys %gbreak)), | |
347 | "\n};\n"); | |
bb48024f RK |
348 | out("const char *const unicode_Word_Break_names[] = {\n", |
349 | join(",\n", | |
349b7b74 RK |
350 | map(" \"$_\"", sort keys %wbreak)), |
351 | "\n};\n"); | |
352 | out("const char *const unicode_Sentence_Break_names[] = {\n", | |
353 | join(",\n", | |
354 | map(" \"$_\"", sort keys %sbreak)), | |
bb48024f RK |
355 | "\n};\n"); |
356 | ||
e5a5a138 RK |
357 | # Generate the decomposition mapping tables. We look out for duplicates |
358 | # in order to save space and report this as decompsaved at the end. In | |
359 | # Unicode 5.0.0 this saves 1795 entries, which is at least 14Kbytes. | |
360 | my $decompnum = 0; | |
361 | my %decompnums = (); | |
362 | my $decompsaved = 0; | |
363 | out("static const uint32_t "); | |
364 | for(my $c = 0; $c <= $max; ++$c) { | |
365 | # If canon is set then compat will be too and will be identical. | |
366 | # If compat is set the canon might be clear. So we use the | |
367 | # compat version and fix up the symbols after. | |
368 | if(exists $data{$c}->{compat}) { | |
369 | my $s = join(",", | |
370 | (map(hex($_), split(/\s+/, $data{$c}->{compat})), 0)); | |
371 | if(!exists $decompnums{$s}) { | |
372 | out(",\n") if $decompnum != 0; | |
373 | out("cd$decompnum\[]={$s}"); | |
374 | $decompnums{$s} = $decompnum++; | |
375 | } else { | |
376 | ++$decompsaved; | |
377 | } | |
378 | $data{$c}->{compatsym} = "cd$decompnums{$s}"; | |
379 | if(exists $data{$c}->{canon}) { | |
380 | $data{$c}->{canonsym} = "cd$decompnums{$s}"; | |
381 | } | |
382 | } | |
383 | } | |
384 | out(";\n"); | |
385 | ||
386 | # ...and the case folding table. Again we compress equal entries to save | |
387 | # space. In Unicode 5.0.0 this saves 51 entries or at least 408 bytes. | |
388 | # This doesns't seem as worthwhile as the decomposition mapping saving above. | |
389 | my $cfnum = 0; | |
390 | my %cfnums = (); | |
391 | my $cfsaved = 0; | |
392 | out("static const uint32_t "); | |
393 | for(my $c = 0; $c <= $max; ++$c) { | |
394 | if(exists $data{$c}->{casefold}) { | |
395 | my $s = join(",", | |
396 | (map(hex($_), split(/\s+/, $data{$c}->{casefold})), 0)); | |
397 | if(!exists $cfnums{$s}) { | |
398 | out(",\n") if $cfnum != 0; | |
399 | out("cf$cfnum\[]={$s}"); | |
400 | $cfnums{$s} = $cfnum++; | |
401 | } else { | |
402 | ++$cfsaved; | |
403 | } | |
404 | $data{$c}->{cfsym} = "cf$cfnums{$s}"; | |
405 | } | |
406 | } | |
407 | out(";\n"); | |
408 | ||
409 | # Visit all the $modulus-character blocks in turn and generate the | |
410 | # required subtables. As above we spot duplicates to save space. In | |
411 | # Unicode 5.0.0 with $modulus=128 and current table data this saves | |
412 | # 1372 subtables or at least three and a half megabytes on 32-bit | |
413 | # platforms. | |
61507e3c | 414 | |
61507e3c RK |
415 | my %subtable = (); # base->subtable number |
416 | my %subtableno = (); # subtable number -> content | |
417 | my $subtablecounter = 0; # counter for subtable numbers | |
e5a5a138 RK |
418 | my $subtablessaved = 0; # number of tables saved |
419 | for(my $base = 0; $base <= $max; $base += $modulus) { | |
61507e3c | 420 | my @t; |
e5a5a138 | 421 | for(my $c = $base; $c < $base + $modulus; ++$c) { |
61507e3c | 422 | my $d = $data{$c}; |
e5a5a138 RK |
423 | my $canonsym = ($data{$c}->{canonsym} or "0"); |
424 | my $compatsym = ($data{$c}->{compatsym} or "0"); | |
425 | my $cfsym = ($data{$c}->{cfsym} or "0"); | |
35b651f0 RK |
426 | my @flags = (); |
427 | if($data{$c}->{ypogegrammeni}) { | |
428 | push(@flags, "unicode_normalize_before_casefold"); | |
429 | } | |
35b651f0 | 430 | my $flags = @flags ? join("|", @flags) : 0; |
e5a5a138 RK |
431 | push(@t, "{". |
432 | join(",", | |
433 | $compatsym, | |
434 | $canonsym, | |
435 | $cfsym, | |
0b7052da RK |
436 | $d->{ud}, |
437 | $d->{ld}, | |
438 | $d->{ccc}, | |
439 | $d->{gc}, | |
e5a5a138 | 440 | $flags, |
349b7b74 RK |
441 | "GB$d->{gbreak}", |
442 | "WB$d->{wbreak}", | |
443 | "SB$d->{sbreak}", | |
e5a5a138 | 444 | )."}"); |
61507e3c RK |
445 | } |
446 | my $t = join(",\n", @t); | |
447 | if(!exists $subtable{$t}) { | |
e5a5a138 | 448 | out("static const struct unidata st$subtablecounter\[] = {\n", |
61507e3c RK |
449 | "$t\n", |
450 | "};\n"); | |
451 | $subtable{$t} = $subtablecounter++; | |
e5a5a138 RK |
452 | } else { |
453 | ++$subtablessaved; | |
61507e3c RK |
454 | } |
455 | $subtableno{$base} = $subtable{$t}; | |
456 | } | |
457 | ||
e5a5a138 RK |
458 | out("const struct unidata*const unidata[]={\n"); |
459 | for(my $base = 0; $base <= $max; $base += $modulus) { | |
460 | out("st$subtableno{$base},\n"); | |
61507e3c RK |
461 | } |
462 | out("};\n"); | |
463 | ||
464 | close STDOUT or die "unidata.c: $!\n"; | |
465 | ||
e5a5a138 RK |
466 | print STDERR "max=$max, subtables=$subtablecounter, subtablessaved=$subtablessaved\n"; |
467 | print STDERR "decompsaved=$decompsaved cfsaved=$cfsaved\n"; |