1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001
|
#!/usr/bin/perl -w
#
# $Id$
#
# generates in the current directory:
# - unicode-blocks.h
# - unicode-names.h
# - unicode-nameslist.h
# - unicode-unihan.h
# - unicode-categories.h
# - unicode-scripts.h
# - unicode-versions.h
#
# usage: ./gen-guch-unicode-tables.pl UNICODE-VERSION DIRECTORY
# where DIRECTORY contains UnicodeData.txt Unihan_Readings.txt.bz2 NamesList.txt Blocks.txt Scripts.txt
#
# NOTE! Some code copied from glib/glib/gen-unicode-tables.pl; keep in sync!
use strict;
use vars ('$UNZIP', '$ICONV');
# if these things aren't in your path you can put full paths to them here
$UNZIP = 'bunzip2';
$ICONV = 'iconv';
sub process_unicode_data_txt ($);
sub process_unihan_zip ($);
sub process_nameslist_txt ($);
sub process_blocks_txt ($);
sub process_scripts_txt ($);
sub process_versions_txt ($);
$| = 1; # flush stdout buffer
if (@ARGV != 2)
{
$0 =~ s@.*/@@;
die <<EOF
Usage: $0 UNICODE-VERSION DIRECTORY
DIRECTORY should contain the following Unicode data files:
UnicodeData.txt Unihan_Readings.txt.bz2 NamesList.txt Blocks.txt Scripts.txt
which can be found at http://www.unicode.org/Public/UNIDATA/
EOF
}
my ($unicodedata_txt, $unihan_zip, $nameslist_txt, $blocks_txt, $scripts_txt, $versions_txt);
my $v = $ARGV[0];
my $d = $ARGV[1];
opendir (my $dir, $d) or die "Cannot open Unicode data dir $d: $!\n";
for my $f (readdir ($dir))
{
$unicodedata_txt = "$d/$f" if ($f =~ /UnicodeData.*\.txt/);
$unihan_zip = "$d/$f" if ($f =~ /Unihan_Readings\.txt\.bz2/);
$nameslist_txt = "$d/$f" if ($f =~ /NamesList.*\.txt/);
$blocks_txt = "$d/$f" if ($f =~ /Blocks.*\.txt/);
$scripts_txt = "$d/$f" if ($f =~ /Scripts.*\.txt/);
$versions_txt = "$d/$f" if ($f =~ /DerivedAge.*\.txt/);
}
defined $unicodedata_txt or die "Did not find $d/UnicodeData.txt";
defined $unihan_zip or die "Did not find $d/Unihan_Readings.txt.bz2";
defined $nameslist_txt or die "Did not find $d/NamesList.txt";
defined $blocks_txt or die "Did not find $d/Blocks.txt";
defined $scripts_txt or die "Did not find $d/Scripts.txt";
defined $versions_txt or die "Did not find $d/DerivedAge.txt";
process_unicode_data_txt ($unicodedata_txt);
process_nameslist_txt ($nameslist_txt);
process_blocks_txt ($blocks_txt);
process_scripts_txt ($scripts_txt);
process_versions_txt ($versions_txt);
process_unihan_zip ($unihan_zip);
exit;
#------------------------#
sub process_unicode_data_txt ($)
{
my ($unicodedata_txt) = @_;
# part 1: names
open (my $unicodedata, $unicodedata_txt) or die;
open (my $out, "> unicode-names.h") or die;
print "processing $unicodedata_txt...";
print $out "/* unicode-names.h */\n";
print $out "/* THIS IS A GENERATED FILE. CHANGES WILL BE OVERWRITTEN. */\n";
print $out "/* Generated by $0 */\n";
print $out "/* Generated from UCD version $v */\n\n";
print $out "#ifndef UNICODE_NAMES_H\n";
print $out "#define UNICODE_NAMES_H\n\n";
print $out "#include <glib.h>\n\n";
print $out "#include <glib/gi18n-lib.h>\n\n";
my @unicode_pairs;
my %names;
while (my $line = <$unicodedata>)
{
chomp $line;
$line =~ /^([^;]+);([^;]+)/ or die;
my $hex = $1;
my $name = $2;
$names{$name} = 1;
push @unicode_pairs, [$hex, $name];
}
print $out "static const char unicode_names_strings[] = \\\n";
my $offset = 0;
foreach my $name (sort keys %names) {
print $out " \"$name\\0\"\n";
$names{$name} = $offset;
$offset += length($name) + 1;
}
undef $offset;
print $out ";\n";
print $out "typedef struct _UnicodeName UnicodeName;\n\n";
print $out "static const struct _UnicodeName\n";
print $out "{\n";
print $out " gunichar index;\n";
print $out " guint32 name_offset;\n";
print $out "} \n";
print $out "unicode_names[] =\n";
print $out "{\n";
my $first_line = 1;
foreach my $pair (@unicode_pairs) {
if (!$first_line) {
print $out ",\n";
} else {
$first_line = 0;
}
my ($hex, $name) = @{$pair};
my $offset = $names{$name};
print $out " {0x$hex, $offset}";
}
print $out "\n};\n\n";
print $out <<EOT;
static inline const char * unicode_name_get_name(const UnicodeName *entry)
{
guint32 offset = entry->name_offset;
return unicode_names_strings + offset;
}
EOT
print $out "#endif /* #ifndef UNICODE_NAMES_H */\n";
undef %names;
undef @unicode_pairs;
close ($unicodedata);
close ($out);
# part 2: categories
open ($unicodedata, $unicodedata_txt) or die;
open ($out, "> unicode-categories.h") or die;
# Map general category code onto symbolic name.
my %mappings =
(
# Normative.
'Lu' => "G_UNICODE_UPPERCASE_LETTER",
'Ll' => "G_UNICODE_LOWERCASE_LETTER",
'Lt' => "G_UNICODE_TITLECASE_LETTER",
'Mn' => "G_UNICODE_NON_SPACING_MARK",
'Mc' => "G_UNICODE_COMBINING_MARK",
'Me' => "G_UNICODE_ENCLOSING_MARK",
'Nd' => "G_UNICODE_DECIMAL_NUMBER",
'Nl' => "G_UNICODE_LETTER_NUMBER",
'No' => "G_UNICODE_OTHER_NUMBER",
'Zs' => "G_UNICODE_SPACE_SEPARATOR",
'Zl' => "G_UNICODE_LINE_SEPARATOR",
'Zp' => "G_UNICODE_PARAGRAPH_SEPARATOR",
'Cc' => "G_UNICODE_CONTROL",
'Cf' => "G_UNICODE_FORMAT",
'Cs' => "G_UNICODE_SURROGATE",
'Co' => "G_UNICODE_PRIVATE_USE",
'Cn' => "G_UNICODE_UNASSIGNED",
# Informative.
'Lm' => "G_UNICODE_MODIFIER_LETTER",
'Lo' => "G_UNICODE_OTHER_LETTER",
'Pc' => "G_UNICODE_CONNECT_PUNCTUATION",
'Pd' => "G_UNICODE_DASH_PUNCTUATION",
'Ps' => "G_UNICODE_OPEN_PUNCTUATION",
'Pe' => "G_UNICODE_CLOSE_PUNCTUATION",
'Pi' => "G_UNICODE_INITIAL_PUNCTUATION",
'Pf' => "G_UNICODE_FINAL_PUNCTUATION",
'Po' => "G_UNICODE_OTHER_PUNCTUATION",
'Sm' => "G_UNICODE_MATH_SYMBOL",
'Sc' => "G_UNICODE_CURRENCY_SYMBOL",
'Sk' => "G_UNICODE_MODIFIER_SYMBOL",
'So' => "G_UNICODE_OTHER_SYMBOL"
);
# these shouldn't be -1
my ($codepoint, $last_codepoint, $start_codepoint) = (-999, -999, -999);
my ($category, $last_category) = ("G_FAKE1", "G_FAKE2");
my ($started_range, $finished_range) = (undef, undef);
print $out "/* unicode-categories.h */\n";
print $out "/* THIS IS A GENERATED FILE. CHANGES WILL BE OVERWRITTEN. */\n";
print $out "/* Generated by $0 */\n";
print $out "/* Generated from UCD version $v */\n\n";
print $out "#ifndef UNICODE_CATEGORIES_H\n";
print $out "#define UNICODE_CATEGORIES_H\n\n";
print $out "#include <glib.h>\n\n";
print $out "typedef struct _UnicodeCategory UnicodeCategory;\n\n";
print $out "static const struct _UnicodeCategory\n";
print $out "{\n";
print $out " gunichar start;\n";
print $out " gunichar end;\n";
print $out " GUnicodeType category;\n";
print $out "}\n";
print $out "unicode_categories[] =\n";
print $out "{\n";
while (my $line = <$unicodedata>)
{
$line =~ /^([0-9A-F]*);([^;]*);([^;]*);/ or die;
my $codepoint = hex ($1);
my $name = $2;
my $category = $mappings{$3};
if ($finished_range
or ($category ne $last_category)
or (not $started_range and $codepoint != $last_codepoint + 1))
{
if ($last_codepoint >= 0) {
printf $out (" { 0x%4.4X, 0x%4.4X, \%s },\n", $start_codepoint, $last_codepoint, $last_category);
}
$start_codepoint = $codepoint;
}
if ($name =~ /^<.*First>$/) {
$started_range = 1;
$finished_range = undef;
}
elsif ($name =~ /^<.*Last>$/) {
$started_range = undef;
$finished_range = 1;
}
elsif ($finished_range) {
$finished_range = undef;
}
$last_codepoint = $codepoint;
$last_category = $category;
}
printf $out (" { 0x%4.4X, 0x%4.4X, \%s },\n", $start_codepoint, $last_codepoint, $last_category);
print $out "};\n\n";
print $out "#endif /* #ifndef UNICODE_CATEGORIES_H */\n";
close ($out);
print " done.\n";
}
#------------------------#
# XXX should do kFrequency too
sub process_unihan_zip ($)
{
my ($unihan_zip) = @_;
open (my $unihan, "$UNZIP -c $unihan_zip |") or die;
open (my $out, "> unicode-unihan.h") or die;
print "processing $unihan_zip";
print $out "/* unicode-unihan.h */\n";
print $out "/* THIS IS A GENERATED FILE. CHANGES WILL BE OVERWRITTEN. */\n";
print $out "/* Generated by $0 */\n";
print $out "/* Generated from UCD version $v */\n\n";
print $out "#ifndef UNICODE_UNIHAN_H\n";
print $out "#define UNICODE_UNIHAN_H\n\n";
print $out "#include <glib.h>\n\n";
print $out "typedef struct _Unihan Unihan;\n\n";
print $out "static const struct _Unihan\n";
print $out "{\n";
print $out " gunichar index;\n";
print $out " gint32 kDefinition;\n";
print $out " gint32 kCantonese;\n";
print $out " gint32 kMandarin;\n";
print $out " gint32 kTang;\n";
print $out " gint32 kKorean;\n";
print $out " gint32 kJapaneseKun;\n";
print $out " gint32 kJapaneseOn;\n";
print $out "} \n";
print $out "unihan[] =\n";
print $out "{\n";
my @strings;
my $offset = 0;
my $wc = 0;
my ($kDefinition, $kCantonese, $kMandarin, $kTang, $kKorean, $kJapaneseKun, $kJapaneseOn);
my $i = 0;
while (my $line = <$unihan>)
{
chomp $line;
$line =~ /^U\+([0-9A-F]+)\s+([^\s]+)\s+(.+)$/ or next;
my $new_wc = hex ($1);
my $field = $2;
my $value = $3;
$value =~ s/\\/\\\\/g;
$value =~ s/\"/\\"/g;
if ($new_wc != $wc)
{
if (defined $kDefinition or defined $kCantonese or defined $kMandarin
or defined $kTang or defined $kKorean or defined $kJapaneseKun
or defined $kJapaneseOn)
{
printf $out (" { 0x%04X, \%d, \%d, \%d, \%d, \%d, \%d, \%d },\n",
$wc,
(defined($kDefinition) ? $kDefinition : -1),
(defined($kCantonese) ? $kCantonese: -1),
(defined($kMandarin) ? $kMandarin : -1),
(defined($kTang) ? $kTang : -1),
(defined($kKorean) ? $kKorean : -1),
(defined($kJapaneseKun) ? $kJapaneseKun : -1),
(defined($kJapaneseOn) ? $kJapaneseOn : -1));
}
$wc = $new_wc;
undef $kDefinition;
undef $kCantonese;
undef $kMandarin;
undef $kTang;
undef $kKorean;
undef $kJapaneseKun;
undef $kJapaneseOn;
}
for my $f qw(kDefinition kCantonese kMandarin
kTang kKorean kJapaneseKun kJapaneseOn) {
if ($field eq $f) {
push @strings, $value;
my $last_offset = $offset;
$offset += length($value) + 1;
$value = $last_offset;
last;
}
}
if ($field eq "kDefinition") {
$kDefinition = $value;
}
elsif ($field eq "kCantonese") {
$kCantonese = $value;
}
elsif ($field eq "kMandarin") {
$kMandarin = $value;
}
elsif ($field eq "kTang") {
$kTang = $value;
}
elsif ($field eq "kKorean") {
$kKorean = $value;
}
elsif ($field eq "kJapaneseKun") {
$kJapaneseKun = $value;
}
elsif ($field eq "kJapaneseOn") {
$kJapaneseOn = $value;
}
if ($i++ % 32768 == 0) {
print ".";
}
}
print $out "};\n\n";
print $out "static const char unihan_strings[] = \\\n";
for my $s (@strings) {
print $out " \"$s\\0\"\n";
}
print $out ";\n\n";
print $out "static const Unihan *_get_unihan (gunichar uc)\n;";
for my $name qw(kDefinition kCantonese kMandarin
kTang kKorean kJapaneseKun kJapaneseOn) {
print $out <<EOT;
static inline const char * unihan_get_$name (const Unihan *uh)
{
gint32 offset = uh->$name;
if (offset == -1)
return NULL;
return unihan_strings + offset;
}
G_CONST_RETURN gchar *
gucharmap_get_unicode_$name (gunichar uc)
{
const Unihan *uh = _get_unihan (uc);
if (uh == NULL)
return NULL;
else
return unihan_get_$name (uh);
}
EOT
}
print $out "#endif /* #ifndef UNICODE_UNIHAN_H */\n";
close ($unihan);
close ($out);
print " done.\n";
}
#------------------------#
# $nameslist_hash =
# {
# 0x0027 => { '=' => {
# 'index' => 30,
# 'values' => [ 'APOSTROPHE-QUOTE', 'APL quote' ]
# }
# '*' => {
# 'index' => 50,
# 'values' => [ 'neutral (vertical) glyph with mixed usage',
# '2019 is preferred for apostrophe',
# 'preferred characters in English for paired quotation marks are 2018 & 2019'
# ]
# }
# # etc
# }
# # etc
# };
#
sub print_names_list
{
my ($out, $nameslist_hash, $token, $variable_name) = @_;
print $out "static const char ", $variable_name, "_strings[] = \n";
my @names_pairs;
my %names_offsets;
my $offset = 0;
for my $wc (sort {$a <=> $b} keys %{$nameslist_hash})
{
next if not exists $nameslist_hash->{$wc}->{$token};
for my $value (@{$nameslist_hash->{$wc}->{$token}->{'values'}}) {
push @names_pairs, [$wc, $value];
next if exists $names_offsets{$value};
$names_offsets{$value} = $offset;
$offset += length($value) + 1;
my $printvalue = $value;
$printvalue =~ s/\\/\\\\/g;
$printvalue =~ s/\"/\\"/g;
printf $out (qq/ "\%s\\0"\n/, $printvalue);
}
}
print $out " ;\n\n";
print $out "static const UnicharStringIndex ", $variable_name, "[] = \n";
print $out "{\n";
foreach my $pair (@names_pairs) {
my ($wc, $value) = @{$pair};
printf $out (qq/ { 0x%04X, %d },\n/, $wc, $names_offsets{$value});
}
print $out " { (gunichar)(-1), 0 } /* end marker */ \n";
print $out "};\n\n";
}
sub process_nameslist_txt ($)
{
my ($nameslist_txt) = @_;
open (my $nameslist, "$ICONV -f 'ISO8859-1' -t 'UTF-8' $nameslist_txt |") or die;
print "processing $nameslist_txt...";
my ($equal_i, $ex_i, $star_i, $pound_i, $colon_i) = (0, 0, 0, 0, 0);
my $wc = 0;
my $nameslist_hash;
my $in_multiline_comment = 0;
while (my $line = <$nameslist>)
{
if ($in_multiline_comment && $line =~ /^\t/)
{
next;
}
chomp ($line);
$in_multiline_comment = 0;
if ($line =~ /^@\+/)
{
$in_multiline_comment = 1;
next;
}
elsif ($line =~ /^@/)
{
next;
}
elsif ($line =~ /^([0-9A-F]+)/)
{
$wc = hex ($1);
}
elsif ($line =~ /^\s+=\s+(.+)$/)
{
my $value = $1;
if (not defined $nameslist_hash->{$wc}->{'='}->{'index'}) {
$nameslist_hash->{$wc}->{'='}->{'index'} = $equal_i;
}
push (@{$nameslist_hash->{$wc}->{'='}->{'values'}}, $value);
$equal_i++;
}
elsif ($line =~ /^\s+\*\s+(.+)$/)
{
my $value = $1;
if (not defined $nameslist_hash->{$wc}->{'*'}->{'index'}) {
$nameslist_hash->{$wc}->{'*'}->{'index'} = $star_i;
}
push (@{$nameslist_hash->{$wc}->{'*'}->{'values'}}, $value);
$star_i++;
}
elsif ($line =~ /^\s+#\s+(.+)$/)
{
my $value = $1;
if (not defined $nameslist_hash->{$wc}->{'#'}->{'index'}) {
$nameslist_hash->{$wc}->{'#'}->{'index'} = $pound_i;
}
push (@{$nameslist_hash->{$wc}->{'#'}->{'values'}}, $value);
$pound_i++;
}
elsif ($line =~ /^\s+:\s+(.+)$/)
{
my $value = $1;
if (not defined $nameslist_hash->{$wc}->{':'}->{'index'}) {
$nameslist_hash->{$wc}->{':'}->{'index'} = $colon_i;
}
push (@{$nameslist_hash->{$wc}->{':'}->{'values'}}, $value);
$colon_i++;
}
elsif ($line =~ /^\s+x\s+.*?([0-9A-F]{4,6})\)$/) # this one is different
{
my $value = hex ($1);
if (not defined $nameslist_hash->{$wc}->{'x'}->{'index'}) {
$nameslist_hash->{$wc}->{'x'}->{'index'} = $ex_i;
}
push (@{$nameslist_hash->{$wc}->{'x'}->{'values'}}, $value);
$ex_i++;
}
}
close ($nameslist);
open (my $out, "> unicode-nameslist.h") or die;
print $out "/* unicode-nameslist.h */\n";
print $out "/* THIS IS A GENERATED FILE. CHANGES WILL BE OVERWRITTEN. */\n";
print $out "/* Generated by $0 */\n";
print $out "/* Generated from UCD version $v */\n\n";
print $out "#ifndef UNICODE_NAMESLIST_H\n";
print $out "#define UNICODE_NAMESLIST_H\n\n";
print $out "#include <glib.h>\n\n";
print $out "typedef struct _UnicharStringIndex UnicharStringIndex;\n";
print $out "typedef struct _UnicharUnichar UnicharUnichar;\n";
print $out "typedef struct _NamesList NamesList;\n\n";
print $out "struct _UnicharStringIndex\n";
print $out "{\n";
print $out " gunichar index;\n";
print $out " guint32 string_index;\n";
print $out "}; \n\n";
print $out "struct _UnicharUnichar\n";
print $out "{\n";
print $out " gunichar index;\n";
print $out " gunichar value;\n";
print $out "}; \n\n";
print $out "struct _NamesList\n";
print $out "{\n";
print $out " gunichar index;\n";
print $out " gint16 equals_index; /* -1 means */\n";
print $out " gint16 stars_index; /* this character */\n";
print $out " gint16 exes_index; /* doesn't */\n";
print $out " gint16 pounds_index; /* have any */\n";
print $out " gint16 colons_index;\n";
print $out "};\n\n";
print_names_list($out, $nameslist_hash, '=', "names_list_equals");
print_names_list($out, $nameslist_hash, '*', "names_list_stars");
print_names_list($out, $nameslist_hash, '#', "names_list_pounds");
print_names_list($out, $nameslist_hash, ':', "names_list_colons");
print $out "static const UnicharUnichar names_list_exes[] = \n";
print $out "{\n";
for $wc (sort {$a <=> $b} keys %{$nameslist_hash})
{
next if not exists $nameslist_hash->{$wc}->{'x'};
for my $value (@{$nameslist_hash->{$wc}->{'x'}->{'values'}}) {
printf $out (qq/ { 0x%04X, 0x%04X },\n/, $wc, $value);
}
}
print $out " { (gunichar)(-1), 0 }\n";
print $out "};\n\n";
print $out "static const NamesList names_list[] =\n";
print $out "{\n";
for $wc (sort {$a <=> $b} keys %{$nameslist_hash})
{
my $eq = exists $nameslist_hash->{$wc}->{'='}->{'index'} ? $nameslist_hash->{$wc}->{'='}->{'index'} : -1;
my $star = exists $nameslist_hash->{$wc}->{'*'}->{'index'} ? $nameslist_hash->{$wc}->{'*'}->{'index'} : -1;
my $ex = exists $nameslist_hash->{$wc}->{'x'}->{'index'} ? $nameslist_hash->{$wc}->{'x'}->{'index'} : -1;
my $pound = exists $nameslist_hash->{$wc}->{'#'}->{'index'} ? $nameslist_hash->{$wc}->{'#'}->{'index'} : -1;
my $colon = exists $nameslist_hash->{$wc}->{':'}->{'index'} ? $nameslist_hash->{$wc}->{':'}->{'index'} : -1;
printf $out (" { 0x%04X, \%d, \%d, \%d, \%d, \%d },\n", $wc, $eq, $star, $ex, $pound, $colon);
}
print $out "};\n\n";
print $out "#endif /* #ifndef UNICODE_NAMESLIST_H */\n";
close ($out);
print " done.\n";
}
#------------------------#
sub process_blocks_txt ($)
{
my ($blocks_txt) = @_;
# Override script names
my %block_overrides =
(
"NKo" => "N\'Ko"
);
open (my $blocks, $blocks_txt) or die;
open (my $out, "> unicode-blocks.h") or die;
print "processing $blocks_txt...";
print $out "/* unicode-blocks.h */\n";
print $out "/* THIS IS A GENERATED FILE. CHANGES WILL BE OVERWRITTEN. */\n";
print $out "/* Generated by $0 */\n";
print $out "/* Generated from UCD version $v */\n\n";
print $out "#ifndef UNICODE_BLOCKS_H\n";
print $out "#define UNICODE_BLOCKS_H\n\n";
print $out "#include <glib.h>\n";
print $out "#include <glib/gi18n-lib.h>\n\n";
my @blocks;
my $offset = 0;
while (my $line = <$blocks>)
{
$line =~ /^([0-9A-F]+)\.\.([0-9A-F]+); (.+)$/ or next;
my ($start,$end,$block) = ($1, $2, $3);
if (exists $block_overrides{$block}) {
$block = $block_overrides{$block};
}
push @blocks, [$start, $end, $block, $offset];
$offset += length($block) + 1;
}
print $out "/* for extraction by intltool */\n";
print $out "#if 0\n";
foreach my $block (@blocks)
{
my ($start, $end, $name, $offset) = @{$block};
print $out qq/ N_("$name"),\n/;
}
print $out "#endif /* 0 */\n\n";
print $out "static const char unicode_blocks_strings[] =\n";
foreach my $block (@blocks)
{
my ($start, $end, $name, $offset) = @{$block};
print $out qq/ "$name\\0"\n/;
}
print $out " ;\n\n";
print $out "typedef struct _UnicodeBlock UnicodeBlock;\n";
print $out "\n";
print $out "static const struct _UnicodeBlock\n";
print $out "{\n";
print $out " gunichar start;\n";
print $out " gunichar end;\n";
print $out " guint16 block_name_index;\n";
print $out "}\n";
print $out "unicode_blocks[] =\n";
print $out "{\n";
foreach my $block (@blocks)
{
my ($start, $end, $name, $offset) = @{$block};
print $out qq/ { 0x$start, 0x$end, $offset },\n/;
}
print $out "};\n\n";
print $out "#endif /* #ifndef UNICODE_BLOCKS_H */\n";
close ($blocks);
close ($out);
print " done.\n";
}
#------------------------#
sub process_scripts_txt ($)
{
my ($scripts_txt) = @_;
# Override script names
my %script_overrides =
(
"Nko" => "N\'Ko"
);
my %script_hash;
my %scripts;
open (my $scripts_file, $scripts_txt) or die;
open (my $out, "> unicode-scripts.h") or die;
print "processing $scripts_txt...";
while (my $line = <$scripts_file>)
{
my ($start, $end, $raw_script);
if ($line =~ /^([0-9A-F]+)\.\.([0-9A-F]+)\s+;\s+(\S+)/)
{
$start = hex ($1);
$end = hex ($2);
$raw_script = $3;
}
elsif ($line =~ /^([0-9A-F]+)\s+;\s+(\S+)/)
{
$start = hex ($1);
$end = $start;
$raw_script = $2;
}
else
{
next;
}
my $script = $raw_script;
$script =~ tr/_/ /;
$script =~ s/(\w+)/\u\L$1/g;
if (exists $script_overrides{$script}) {
$script = $script_overrides{$script};
}
$script_hash{$start} = { 'end' => $end, 'script' => $script };
$scripts{$script} = 1;
}
close ($scripts_file);
# Adds Common to make sure works with UCD <= 4.0.0
$scripts{"Common"} = 1;
print $out "/* unicode-scripts.h */\n";
print $out "/* THIS IS A GENERATED FILE. CHANGES WILL BE OVERWRITTEN. */\n";
print $out "/* Generated by $0 */\n";
print $out "/* Generated from UCD version $v */\n\n";
print $out "#ifndef UNICODE_SCRIPTS_H\n";
print $out "#define UNICODE_SCRIPTS_H\n\n";
print $out "#include <glib.h>\n";
print $out "#include <glib/gi18n-lib.h>\n\n";
print $out "typedef struct _UnicodeScript UnicodeScript;\n\n";
print $out "/* for extraction by intltool */\n";
print $out "#if 0\n";
my $i = 0;
for my $script (sort keys %scripts)
{
$scripts{$script} = $i;
$i++;
print $out qq/ N_("$script"),\n/;
}
print $out "#endif /* 0 */\n\n";
print $out "static const gchar unicode_script_list_strings[] =\n";
my $offset = 0;
my %script_offsets;
for my $script (sort keys %scripts)
{
printf $out (qq/ "\%s\\0"\n/, $script);
$script_offsets{$script} = $offset;
$offset += length($script) + 1;
}
print $out " ;\n\n";
undef $offset;
print $out "static const guint16 unicode_script_list_offsets[] =\n";
print $out "{\n";
for my $script (sort keys %scripts)
{
printf $out (qq/ \%d,\n/, $script_offsets{$script});
}
print $out "};\n\n";
print $out "static const struct _UnicodeScript\n";
print $out "{\n";
print $out " gunichar start;\n";
print $out " gunichar end;\n";
print $out " guint8 script_index; /* index into unicode_script_list_offsets */\n";
print $out "}\n";
print $out "unicode_scripts[] =\n";
print $out "{\n";
for my $start (sort { $a <=> $b } keys %script_hash)
{
printf $out (qq/ { 0x%04X, 0x%04X, \%2d },\n/,
$start, $script_hash{$start}->{'end'}, $scripts{$script_hash{$start}->{'script'}});
}
print $out "};\n\n";
print $out "#endif /* #ifndef UNICODE_SCRIPTS_H */\n";
close ($out);
print " done.\n";
}
#------------------------#
sub process_versions_txt ($)
{
my ($versions_txt) = @_;
my %version_hash;
my %versions;
open (my $versions, $versions_txt) or die;
open (my $out, "> unicode-versions.h") or die;
print "processing $versions_txt...";
while (my $line = <$versions>)
{
my ($start, $end, $raw_version);
if ($line =~ /^([0-9A-F]+)\.\.([0-9A-F]+)\s+;\s+(\S+)/)
{
$start = hex ($1);
$end = hex ($2);
$raw_version = $3;
}
elsif ($line =~ /^([0-9A-F]+)\s+;\s+(\S+)/)
{
$start = hex ($1);
$end = $start;
$raw_version = $2;
}
else
{
next;
}
my $version = $raw_version;
$version =~ tr/_/ /;
$version =~ s/(\w+)/\u\L$1/g;
$versions{$version} = 1;
$version =~ s/\./_/g;
$version_hash{$start} = { 'end' => $end, 'version' => $version };
}
close ($versions);
print $out "/* unicode-versions.h */\n";
print $out "/* THIS IS A GENERATED FILE. CHANGES WILL BE OVERWRITTEN. */\n";
print $out "/* Generated by $0 */\n";
print $out "/* Generated from UCD version $v */\n\n";
print $out "#ifndef UNICODE_VERSIONS_H\n";
print $out "#define UNICODE_VERSIONS_H\n\n";
print $out "#include <glib.h>\n";
print $out "#include <glib/gi18n-lib.h>\n\n";
print $out "typedef struct {\n";
print $out " gunichar start;\n";
print $out " gunichar end;\n";
print $out " GucharmapUnicodeVersion version;\n";
print $out "} UnicodeVersion;\n\n";
print $out "static const UnicodeVersion unicode_versions[] =\n";
print $out "{\n";
for my $start (sort { $a <=> $b } keys %version_hash)
{
printf $out (qq/ { 0x%04X, 0x%04X, GUCHARMAP_UNICODE_VERSION_\%s },\n/,
$start, $version_hash{$start}->{'end'}, $version_hash{$start}->{'version'});
}
print $out "};\n\n";
print $out "static const gchar unicode_version_strings[] =\n";
my $offset = 0;
my %version_offsets;
for my $version (sort keys %versions)
{
printf $out (qq/ "\%s\\0"\n/, $version);
$version_offsets{$version} = $offset;
$offset += length($version) + 1;
}
print $out " ;\n\n";
undef $offset;
print $out "static const guint16 unicode_version_string_offsets[] =\n";
print $out "{\n";
for my $version (sort keys %versions)
{
printf $out (qq/ \%d,\n/, $version_offsets{$version});
}
print $out "};\n\n";
print $out "#endif /* #ifndef UNICODE_VERSIONS_H */\n";
close ($out);
print " done.\n";
}
|