summaryrefslogtreecommitdiff
path: root/tools/gen-script-table.pl
blob: 73cd72f9323076ee69a3b5f9d246ef31ccd21a3e (plain)
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
#!/usr/bin/perl -w 
#
# Script to convert http://www.unicode.org/Public/UNIDATA/Scripts.txt
# into a machine-readable table.
#
######################################################################

if (@ARGV != 1) {
    die "Usage: gen-script-table.pl Scripts.txt > pango-script-table.h\n";
}

open IN, $ARGV[0] || die "Cannot open $ARGV[0]: $!\n";

my @ranges;
my $file;
my $easy_range;
my $i;
my $start;
my $end;
my $script;


while (<IN>) {
    if (/^\#\s+(Scripts-.*.txt)/) {
	$file = $1;
    }
    
    s/#.*//;
    next if /^\s*$/;
    if (!/^([0-9A-F]+)(?:\.\.([0-9A-F]+))?\s*;\s*([A-Za-z_]+)\s*$/) {
	die "Cannot parse line: '$_'\n";
    }

    if (defined $2) {
	push @ranges, [ hex $1, hex $2, uc $3 ];
    } else {
	push @ranges, [ hex $1, hex $1, uc $3 ];
    }
}

@ranges = sort { $a->[0] <=> $b->[0] } @ranges;
$date = gmtime;

print <<"EOT";
/* pango-script-table.h: Generated by gen-script-table.pl
 *
 *  Date: $date
 *  Source: $file
 *
 * Do not edit.
 */

EOT

$easy_range = 0x2000;

print <<"EOT";
#define PANGO_EASY_SCRIPTS_RANGE $easy_range

static const guchar pango_script_easy_table[$easy_range] = {
EOT

$i = 0;
$end = -1;

for (my $c = 0; $c < $easy_range; $c++) {

    if ($c % 3 == 0) {
      printf "\n ";
    }

    if ($c > $end) {
        $start = $ranges[$i]->[0];
        $end = $ranges[$i]->[1];
        $script = $ranges[$i]->[2];
        $i++;
    }
    
    if ($c < $start) {
        printf " PANGO_SCRIPT_COMMON,";
    } else {
        printf " PANGO_SCRIPT_%s,", $script;
    }
}

if ($end >= $easy_range) {
    $i--;
    $ranges[$i]->[0] = $easy_range;
}


print <<"EOT";

};

static const struct {
    gunichar    start;
    guint16     chars;
    guint16     script;
} pango_script_table[] = { 
EOT

for (; $i <= $#ranges; $i++) {
    $start = $ranges[$i]->[0];
    $end = $ranges[$i]->[1];
    $script = $ranges[$i]->[2];

    while ($i <= $#ranges - 1 &&
	   $ranges[$i + 1]->[0] == $end + 1 &&
	   $ranges[$i + 1]->[2] eq $script) {
	$i++;
	$end = $ranges[$i]->[1];
    }

    if ($script ne "COMMON") {
        printf " { %#06x, %5d, PANGO_SCRIPT_%s },\n", $start, $end - $start + 1, $script;
    }
}

printf "};\n";