3 # Copyright (C) 1998, 1999 Tom Tromey
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2, or (at your option)
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 # gen-unicode-tables.pl - Generate tables for libunicode from Unicode data.
21 # See http://www.unicode.org/Public/UNIDATA/UnicodeCharacterDatabase.html
22 # Usage: gen-unicode-tables.pl [-decomp | -both] UNICODE-VERSION UnicodeData.txt LineBreak.txt
23 # I consider the output of this program to be unrestricted. Use it as
27 # * We could save even more space in the generated table by using
28 # indexes and not pointers.
29 # * For decomp table it might make sense to use a shift count other
30 # than 8. We could easily compute the perfect shift count.
32 use vars qw($CODE $NAME $CATEGORY $COMBINING_CLASSES $BIDI_CATEGORY $DECOMPOSITION $DECIMAL_VALUE $DIGIT_VALUE $NUMERIC_VALUE $MIRRORED $OLD_NAME $COMMENT $UPPER $LOWER $TITLE $BREAK_CODE $BREAK_CATEGORY $BREAK_NAME);
34 # Names of fields in Unicode data table.
38 $COMBINING_CLASSES = 3;
51 # Names of fields in the line break table
56 # Map general category code onto symbolic name.
60 'Lu' => "G_UNICODE_UPPERCASE_LETTER",
61 'Ll' => "G_UNICODE_LOWERCASE_LETTER",
62 'Lt' => "G_UNICODE_TITLECASE_LETTER",
63 'Mn' => "G_UNICODE_NON_SPACING_MARK",
64 'Mc' => "G_UNICODE_COMBINING_MARK",
65 'Me' => "G_UNICODE_ENCLOSING_MARK",
66 'Nd' => "G_UNICODE_DECIMAL_NUMBER",
67 'Nl' => "G_UNICODE_LETTER_NUMBER",
68 'No' => "G_UNICODE_OTHER_NUMBER",
69 'Zs' => "G_UNICODE_SPACE_SEPARATOR",
70 'Zl' => "G_UNICODE_LINE_SEPARATOR",
71 'Zp' => "G_UNICODE_PARAGRAPH_SEPARATOR",
72 'Cc' => "G_UNICODE_CONTROL",
73 'Cf' => "G_UNICODE_FORMAT",
74 'Cs' => "G_UNICODE_SURROGATE",
75 'Co' => "G_UNICODE_PRIVATE_USE",
76 'Cn' => "G_UNICODE_UNASSIGNED",
79 'Lm' => "G_UNICODE_MODIFIER_LETTER",
80 'Lo' => "G_UNICODE_OTHER_LETTER",
81 'Pc' => "G_UNICODE_CONNECT_PUNCTUATION",
82 'Pd' => "G_UNICODE_DASH_PUNCTUATION",
83 'Ps' => "G_UNICODE_OPEN_PUNCTUATION",
84 'Pe' => "G_UNICODE_CLOSE_PUNCTUATION",
85 'Pi' => "G_UNICODE_INITIAL_PUNCTUATION",
86 'Pf' => "G_UNICODE_FINAL_PUNCTUATION",
87 'Po' => "G_UNICODE_OTHER_PUNCTUATION",
88 'Sm' => "G_UNICODE_MATH_SYMBOL",
89 'Sc' => "G_UNICODE_CURRENCY_SYMBOL",
90 'Sk' => "G_UNICODE_MODIFIER_SYMBOL",
91 'So' => "G_UNICODE_OTHER_SYMBOL"
96 'BK' => "G_UNICODE_BREAK_MANDATORY",
97 'CR' => "G_UNICODE_BREAK_CARRIAGE_RETURN",
98 'LF' => "G_UNICODE_BREAK_LINE_FEED",
99 'CM' => "G_UNICODE_BREAK_COMBINING_MARK",
100 'SG' => "G_UNICODE_BREAK_SURROGATE",
101 'ZW' => "G_UNICODE_BREAK_ZERO_WIDTH_SPACE",
102 'IN' => "G_UNICODE_BREAK_INSEPARABLE",
103 'GL' => "G_UNICODE_BREAK_NON_BREAKING_GLUE",
104 'CB' => "G_UNICODE_BREAK_CONTINGENT",
105 'SP' => "G_UNICODE_BREAK_SPACE",
106 'BA' => "G_UNICODE_BREAK_AFTER",
107 'BB' => "G_UNICODE_BREAK_BEFORE",
108 'B2' => "G_UNICODE_BREAK_BEFORE_AND_AFTER",
109 'HY' => "G_UNICODE_BREAK_HYPHEN",
110 'NS' => "G_UNICODE_BREAK_NON_STARTER",
111 'OP' => "G_UNICODE_BREAK_OPEN_PUNCTUATION",
112 'CL' => "G_UNICODE_BREAK_CLOSE_PUNCTUATION",
113 'QU' => "G_UNICODE_BREAK_QUOTATION",
114 'EX' => "G_UNICODE_BREAK_EXCLAMATION",
115 'ID' => "G_UNICODE_BREAK_IDEOGRAPHIC",
116 'NU' => "G_UNICODE_BREAK_NUMERIC",
117 'IS' => "G_UNICODE_BREAK_INFIX_SEPARATOR",
118 'SY' => "G_UNICODE_BREAK_SYMBOL",
119 'AL' => "G_UNICODE_BREAK_ALPHABETIC",
120 'PR' => "G_UNICODE_BREAK_PREFIX",
121 'PO' => "G_UNICODE_BREAK_POSTFIX",
122 'SA' => "G_UNICODE_BREAK_COMPLEX_CONTEXT",
123 'AI' => "G_UNICODE_BREAK_AMBIGUOUS",
124 'XX' => "G_UNICODE_BREAK_UNKNOWN"
127 # Title case mappings.
128 %title_to_lower = ();
129 %title_to_upper = ();
133 if ($ARGV[0] eq '-decomp')
139 elsif ($ARGV[0] eq '-both')
145 print "Creating decomp table\n" if ($do_decomp);
146 print "Creating property table\n" if ($do_props);
148 print "Unicode data from $ARGV[1]\n";
150 open (INPUT, "< $ARGV[1]") || exit 1;
156 @fields = split (';', $_, 30);
159 printf STDERR ("Entry for $fields[$CODE] has wrong number of fields (%d)\n", $#fields);
162 $code = hex ($fields[$CODE]);
164 last if ($code > 0xFFFF); # ignore characters out of the basic plane
166 if ($code > $last_code + 1)
169 if ($fields[$NAME] =~ /Last>/)
171 # Fill the gap with the last character read,
172 # since this was a range specified in the char database
177 # The gap represents undefined characters. Only the type
179 @gfields = ('', '', 'Cn', '0', '', '', '', '', '', '', '',
182 for (++$last_code; $last_code < $code; ++$last_code)
184 $gfields{$CODE} = sprintf ("%04x", $last_code);
185 &process_one ($last_code, @gfields);
188 &process_one ($code, @fields);
192 @gfields = ('', '', 'Cn', '0', '', '', '', '', '', '', '',
194 for (++$last_code; $last_code < 0x10000; ++$last_code)
196 $gfields{$CODE} = sprintf ("%04x", $last_code);
197 &process_one ($last_code, @gfields);
199 --$last_code; # Want last to be 0xFFFF.
201 print "Creating line break table\n";
203 print "Line break data from $ARGV[2]\n";
205 open (INPUT, "< $ARGV[2]") || exit 1;
214 @fields = split (';', $_, 30);
217 printf STDERR ("Entry for $fields[$CODE] has wrong number of fields (%d)\n", $#fields);
220 $code = hex ($fields[$CODE]);
222 last if ($code > 0xFFFF); # ignore characters out of the basic plane
224 if ($code > $last_code + 1)
227 if ($fields[$NAME] =~ /Last>/)
229 # Fill the gap with the last character read,
230 # since this was a range specified in the char database
231 $gap_break_prop = $fields[$BREAK_PROPERTY];
232 for (++$last_code; $last_code < $code; ++$last_code)
234 $break_props[$last_code] = $gap_break_prop;
239 # The gap represents undefined characters. If assigned,
240 # they are AL, if not assigned, XX
241 for (++$last_code; $last_code < $code; ++$last_code)
243 if ($type[$last_code] eq 'Cn')
245 $break_props[$last_code] = 'XX';
249 $break_props[$last_code] = 'AL';
254 $break_props[$code] = $fields[$BREAK_PROPERTY];
258 for (++$last_code; $last_code < 0x10000; ++$last_code)
260 if ($type[$last_code] eq 'Cn')
262 $break_props[$last_code] = 'XX';
266 $break_props[$last_code] = 'AL';
269 --$last_code; # Want last to be 0xFFFF.
271 print STDERR "Last code is not 0xFFFF" if ($last_code != 0xFFFF);
273 &print_tables ($last_code)
275 &print_decomp ($last_code)
278 &print_line_break ($last_code);
282 # Process a single character.
285 my ($code, @fields) = @_;
287 $type[$code] = $fields[$CATEGORY];
288 if ($type[$code] eq 'Nd')
290 $value[$code] = int ($fields[$DECIMAL_VALUE]);
292 elsif ($type[$code] eq 'Ll')
294 $value[$code] = hex ($fields[$UPPER]);
296 elsif ($type[$code] eq 'Lu')
298 $value[$code] = hex ($fields[$LOWER]);
301 if ($type[$code] eq 'Lt')
303 $title_to_lower{$code} = hex ($fields[$LOWER]);
304 $title_to_upper{$code} = hex ($fields[$UPPER]);
307 $cclass[$code] = $fields[$COMBINING_CLASSES];
309 # Handle decompositions.
310 if ($fields[$DECOMPOSITION] ne ''
311 && $fields[$DECOMPOSITION] !~ /\<.*\>/)
313 $decompositions[$code] = $fields[$DECOMPOSITION];
320 my ($outfile) = "gunichartables.h";
322 local ($bytes_out) = 0;
324 print "Writing $outfile...\n";
326 open (OUT, "> $outfile");
328 print OUT "/* This file is automatically generated. DO NOT EDIT!\n";
329 print OUT " Instead, edit gen-unicode-tables.pl and re-run. */\n\n";
331 print OUT "#ifndef CHARTABLES_H\n";
332 print OUT "#define CHARTABLES_H\n\n";
334 print OUT "#define G_UNICODE_DATA_VERSION \"$ARGV[0]\"\n\n";
336 printf OUT "#define G_UNICODE_LAST_CHAR 0x%04x\n\n", $last;
338 for ($count = 0; $count <= $last; $count += 256)
340 $row[$count / 256] = &print_row ($count, '(char *) ', 'char', 1,
341 'page', \&fetch_type);
344 print OUT "static char *type_table[256] = {\n";
345 for ($count = 0; $count <= $last; $count += 256)
347 print OUT ",\n" if $count > 0;
348 print OUT " ", $row[$count / 256];
351 print OUT "\n};\n\n";
355 # Now print attribute table.
358 for ($count = 0; $count <= $last; $count += 256)
360 $row[$count / 256] = &print_row ($count, '', 'unsigned short', 2,
361 'attrpage', \&fetch_attr);
363 print OUT "static unsigned short *attr_table[256] = {\n";
364 for ($count = 0; $count <= $last; $count += 256)
366 print OUT ",\n" if $count > 0;
367 print OUT " ", $row[$count / 256];
370 print OUT "\n};\n\n";
373 print OUT "static unsigned short title_table[][3] = {\n";
376 foreach $item (sort keys %title_to_lower)
381 printf OUT " { 0x%04x, 0x%04x, 0x%04x }", $item, $title_to_upper{$item}, $title_to_lower{$item};
384 print OUT "\n};\n\n";
386 print OUT "#endif /* CHARTABLES_H */\n";
390 printf STDERR "Generated %d bytes in tables\n", $bytes_out;
393 # A fetch function for the type table.
397 return $mappings{$type[$index]};
400 # A fetch function for the attribute table.
404 if (defined $value[$index])
406 return sprintf ("0x%04x", $value[$index]);
414 # Print a single "row" of a two-level table.
417 my ($start, $def_pfx, $typname, $typsize, $name, $fetcher) = @_;
424 for ($off = 0; $off < 256; ++$off)
426 $values[$off] = $fetcher->($off + $start);
427 if ($values[$off] ne $values[0])
434 return $def_pfx . $values[0];
437 printf OUT "static %s %s%d[256] = {\n ", $typname, $name, $start / 256;
439 for ($i = $start; $i < $start + 256; ++$i)
443 my ($text) = $values[$i - $start];
444 if (length ($text) + $column + 2 > 78)
450 $column += length ($text) + 2;
452 print OUT "\n};\n\n";
454 $bytes_out += 256 * $typsize;
456 return sprintf "%s%d", $name, $start / 256;
459 # Generate the character decomposition header.
463 my ($outfile) = "gunidecomp.h";
465 local ($bytes_out) = 0;
467 print "Writing $outfile...\n";
469 open (OUT, "> $outfile") || exit 1;
471 print OUT "/* This file is automatically generated. DO NOT EDIT! */\n\n";
472 print OUT "#ifndef DECOMP_H\n";
473 print OUT "#define DECOMP_H\n\n";
475 printf OUT "#define G_UNICODE_LAST_CHAR 0x%04x\n\n", $last;
478 for ($count = 0; $count <= $last; $count += 256)
480 $row[$count / 256] = &print_row ($count, '(unsigned char *) ',
481 'unsigned char', 1, 'cclass',
485 print OUT "static unsigned char *combining_class_table[256] = {\n";
486 for ($count = 0; $count <= $last; $count += 256)
488 print OUT ",\n" if $count > 0;
489 print OUT " ", $row[$count / 256];
492 print OUT "\n};\n\n";
494 print OUT "typedef struct\n{\n";
496 print OUT " unsigned short ch;\n";
497 print OUT " unsigned char *expansion;\n";
498 print OUT "} decomposition;\n\n";
500 print OUT "static decomposition decomp_table[] =\n{\n";
503 for ($count = 0; $count <= $last; ++$count)
505 if (defined $decompositions[$count])
510 printf OUT " { 0x%04x, \"", $count;
512 foreach $iter (&expand_decomp ($count))
514 printf OUT "\\x%02x\\x%02x", $iter / 256, $iter & 0xff;
517 # Only a single terminator because one is implied in the string.
522 print OUT "\n};\n\n";
524 print OUT "#endif /* DECOMP_H */\n";
526 printf STDERR "Generated %d bytes in decomp tables\n", $bytes_out;
532 my ($outfile) = "gunibreak.h";
534 local ($bytes_out) = 0;
536 print "Writing $outfile...\n";
538 open (OUT, "> $outfile");
540 print OUT "/* This file is automatically generated. DO NOT EDIT!\n";
541 print OUT " Instead, edit gen-unicode-tables.pl and re-run. */\n\n";
543 print OUT "#ifndef BREAKTABLES_H\n";
544 print OUT "#define BREAKTABLES_H\n\n";
546 print OUT "#define G_UNICODE_DATA_VERSION \"$ARGV[0]\"\n\n";
548 printf OUT "#define G_UNICODE_LAST_CHAR 0x%04x\n\n", $last;
550 for ($count = 0; $count <= $last; $count += 256)
552 $row[$count / 256] = &print_row ($count, '(char *) ', 'char', 1,
557 print OUT "static char *break_property_table[256] = {\n";
558 for ($count = 0; $count <= $last; $count += 256)
560 print OUT ",\n" if $count > 0;
561 print OUT " ", $row[$count / 256];
564 print OUT "\n};\n\n";
566 print OUT "#endif /* BREAKTABLES_H */\n";
570 printf STDERR "Generated %d bytes in break tables\n", $bytes_out;
574 # A fetch function for the break properties table.
578 return $break_mappings{$break_props[$index]};
581 # Fetcher for combining class.
588 # Expand a character decomposition recursively.
595 foreach $iter (split (' ', $decompositions[$code]))
598 if (defined $decompositions[$val])
600 push (@result, &expand_decomp ($val));
604 push (@result, $val);