summaryrefslogtreecommitdiff
path: root/unidata/CreateGetType.pl
blob: 28aad0e1963e5820dca6eda9a519e1a8183e533a (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
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
#!/usr/bin/perl -w

# FriBidi - Library of BiDi algorithm
# Copyright (C) 1999 Dov Grobgeld
# Copyright (C) 2001 Roozbeh Pournader
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
# Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
#

######################################################################
#  This is a Perl program for automatically building the cfunction
#  fribidi_get_type() which returns the Bidi type of a unicode
#  character. To build this function the script parses the
#  PropList.txt, and BidiMirroring.txt files.
#
#  The latest version of these files are always available at:
#     http://www.unicode.org/Public/UNIDATA/
######################################################################

use strict;

#my $unicode_data_file = "UnicodeData.txt";
my $unicode_proplist_file = "PropList.txt";
my $unicode_mirroring_file = "BidiMirroring.txt";
my $proplist_version;
my $mirroring_version;
my @bidi_entities;
my @mirrors;

my %type_translate = (L=>'LTR',R=>'RTL','B'=>'BS','S'=>'SS');

my %type_names = ("0x10000090" => ["CTL", "Control units"],
		  "0x01000000" => ["LTR", "Strong left to right"],
		  "0x02000000" => ["RTL", "Strong right to left"],
		  "0x03000000" => ["EN",  "European digit"],
		  "0x04000000" => ["ES",  "European number separator"],
		  "0x05000000" => ["ET",  "European number terminator"],
		  "0x06000000" => ["AN",  "Arabic digit"],
		  "0x07000000" => ["CS",  "Common Separator"],
		  "0x08000000" => ["BS",  "Block separator"],
		  "0x09000000" => ["SS",  "Segment separator"],
		  "0x0A000000" => ["WS",  "Whitespace"],
		  "0x0B000000" => ["AL",  "Arabic right to left letter"],
		  "0x0C000000" => ["NSM", "Non-spacing mark"],
		  "0x0D000000" => ["BN",  "Boundary Neutral"],
		  "0x0E000000" => ["PDF", "Pop directional formatting"],
		  "0x0F000000" => ["EO",  "Embedding or override"],
		  "0x80000009" => ["ON",  "Other Neutral"],
		  "0x10000091" => ["LRE", "RLE"],		  
		  "0x10000092" => ["RLE", "RLE"],		  
		  "0x10000093" => ["LRO", "RLO"],		  
		  "0x10000094" => ["RLO", "LRO"],		  
		 );

open(PROP, $unicode_proplist_file)
   or die "Failed opening $unicode_proplist_file!\n";

#open(DATA, $unicode_data_file)
#   or die "Failed opening $unicode_data_file!\n";

open(MIRR, $unicode_mirroring_file)
   or die "Failed opening $unicode_mirroring_file!\n";

$_ = <PROP>;
if (/^Property dump: UnicodeData-(.*).txt/) {
   $proplist_version = $1;
}

$_ = <MIRR>;
if (/^# BidiMirroring-(.*).txt/) {
   $mirroring_version = $1;
}

#parse_unicode_data_for_bidi_entries();
parse_prop_for_bidi_entities();
find_bidi_controls();
#print_bidi_entities();
parse_for_mirror_chars();
#print_mirrored_chars();
create_c_file();

sub parse_unicode_data_for_bidi_entries {
    seek(DATA,0,0);
    my ($prev_type, $prev_num) = ('',0);
    while(<DATA>) {
	my ($num, $type) = (split(/;/))[0,4];
	my $tt;
	if ($tt = $type_translate{$type}) {
	    $type = $tt;
	}
	$num = hex($num);
	
	if ($prev_type eq $type && $num == $prev_num+1) {
	    $bidi_entities[-1][2]++;
	} else {
	    push(@bidi_entities, [$num, $type, 1]);
	}
	$prev_num = $num;
	$prev_type = $type;
    }
}

sub parse_prop_for_bidi_entities {
    my @property_bidi_entities;
    my ($type, $type_num, $type_descr);
    while(<PROP>) {
	tr/\r\n//d;
	if (/^Property dump for:\s*(.*bidi.*)/i) {
	    $type = $1;
	    $_=<PROP>;
	    $type=~ / /;
	    ($type_num, $type_descr) = ($`, $');
	    next;
	}
	if ($type && /^\s*$/) {
	    $type = "";
	}
	next unless $type;

	my($range) = split;
	my($first,$last,$len);
	if ($range =~ /\./) {
	   ($first,$last) = map(hex, split(/\.\./, $range));
	    $len = $last - $first +1;
	} else {
	    ($first, $len) = (hex($range), 1);
	}
#	print "$first $type_names{$type_num}->[0] $len\n";
	$type = $type_names{$type_num} || die "Undefined num $type_num!\n";

	push(@property_bidi_entities, [$first, $type_names{$type_num}->[0], $len]);
    }

    @bidi_entities = sort { $a->[0] <=> $b->[0] } @property_bidi_entities;
}

######################################################################
#  Manually change the bidi control characters that are not
#  explicitely listed in the PropList file to their corresponding
#  values.
#
#  This should be changed to be more generic, but I don't expect
#  this behaviour to change in the PropList file in the near
#  future.
######################################################################
sub find_bidi_controls {
    # Erase all bidi controls
    @bidi_entities = grep($_->[1] !~ /^(CTL|EO)/o, @bidi_entities);

    # Manually add the bidi controls that are not listed in PropList
    push(@bidi_entities,
	 [0x202a, 'LRE', 1],
	 [0x202b, 'RLE', 1],
	 [0x202d, 'LRO', 1],
	 [0x202e, 'RLO', 1]);

    # resort
    @bidi_entities = sort { $a->[0] <=> $b->[0] } @bidi_entities;
}

sub parse_for_mirror_chars {
    seek(MIRR,0,0);
    while(<MIRR>) {
	if (/^([0-9A-F]*); ([0-9A-F]*)/) {
	    push(@mirrors, [$1, $2]);
	}
    }
}

sub print_mirrored_chars {
    print STDERR "Mirrors:\n";
    for my $m (@mirrors) {
	my ($num, $mirror_num) = @$m;
	print STDERR "$num <=> $mirror_num\n";
    }
}

sub print_bidi_entities {
    for my $e (@bidi_entities) {
	print sprintf("%04x", $e->[0]), " $e->[1] $e->[2]\n";
    }
}

sub split_entity {
    my $e = shift;
    my ($start, $type, $len) = @$e;

    my ($first,$last) = ($start, $start+$len-1);

    return ($first, $last, $type);
};


sub create_block {
    my ($block,$name, $ranges) = @_;
    my($title) = <<__;
FriBidiCharType ${name}\[256\] = {
__
    my $result;
    my $last_type=$ranges->[0]->[2];
    
    my $ind = 0;
    for my $i ($block*256 .. $block*256 + 255) {
       my $found = 0;
           
       if ($i % 16 == 0) {
           $result .= "  ";
       }
       while ($ind < @$ranges) {
           if ($i > $ranges->[$ind]->[1]) {
               $ind++;
           } elsif ($i < $ranges->[$ind]->[0]) {
               last;
           } else {
               $found = 1;
               $result .= sprintf("%-3s,", $ranges->[$ind]->[2]);
	       $last_type=$ranges->[$ind]->[2];
               last;
           }
       }
       if (!$found) {
	   $result .= sprintf("%-3s,", $last_type);
       }
       if ($i % 16 == 15) {
           $result .= "\n";
       }
    }
    $result .= "};\n\n";
    return($title, $result);
};


sub create_c_file {
    # Create the h file
    my $num_used_blocks = 0;

    my $c_file =<<__;
/*========================================================================
//  This file was automatically created from $unicode_proplist_file, version $proplist_version,
//  and $unicode_mirroring_file, version $mirroring_version, by the perl script CreateGetType.pl.
//----------------------------------------------------------------------*/

#include "fribidi.h"

__

    for my $type (keys %type_names) {
       $c_file .= "#define $type_names{$type}->[0] FRIBIDI_TYPE_$type_names{$type}->[0]\n";
    }
    $c_file .= "\n\n";
    
    my $i = 0;
    my $block_array = <<__;
FriBidiCharType *FriBidiPropertyBlocks[256] = {
__

    my %seen_blocks;
    for my $block (0..255) {
       my @block_ranges;

       while ($i < @bidi_entities) {
           my ($first,$last,$type) = split_entity ($bidi_entities[$i]);

           if ($first > $block * 256 + 255) {
               last;
           } else {
               if ($last >= $block * 256) {
                   push @block_ranges, [ $first > $block * 256 ? $first : $block * 256,
                                         $last < $block * 256 + 255 ? $last : $block * 256 + 255, 
                                         $type ];
                                    
               }
               if ($last < ($block + 1) * 256) {
                   $i++;
               } else {
                   last;
               }
           }
       }

       if (@block_ranges == 0) {
           $block_array .= "  NULL,\n";
           
#       }
#       elsif (@block_ranges == 1
#		&& $block_ranges[0]->[1] - $block_ranges[0]->[0] >= 255
#		&& exists $solid_blocks{$block_ranges[0]->[2]}) {
#           my $name = $solid_blocks{$block_ranges[0]->[2]};
#           $block_array .= "  $name,\n";
           
       } else {
           my $name = sprintf ("FriBidiPropertyBlock%04x", $block * 256);

	   my($title, $block_ctx) = create_block ($block, $name, \@block_ranges);

	   # use the fact that a hash table uses a good hash function. ;-)
	   if (exists $seen_blocks{$block_ctx}) {
	       my $name = $seen_blocks{$block_ctx};
	       $block_array .= "  $name,\n";
	   } else {
	       $num_used_blocks++;
	       $block_array .= "  $name,\n";
	       $c_file .= $title . $block_ctx;
               $seen_blocks{$block_ctx} = $name;
	   }

#           if (@block_ranges == 1
#	       && $block_ranges[0]->[1] - $block_ranges[0]->[0] >= 255
#	      ) {
#               $solid_blocks{$block_ranges[0]->[2]} = $name;
#           }
       }
    }
   $c_file .= "/* $num_used_blocks blocks defined */\n\n";

   $c_file .= $block_array
             . "};\n\n";

    for my $type (keys %type_names) {
       $c_file .= "#undef $type_names{$type}->[0]\n";
    }

   $c_file .= "\n\n";

    ######################################################################
    #  Mirrored characters.
    ######################################################################

    $c_file .= <<__;
/*======================================================================
//  Mirrored characters include all the characters in the Unicode list
//  that have been declared as being mirrored and that have a mirrored
//  equivalent.
//
//  There are lots of characters that are designed as being mirrored
//  but do not have any mirrored glyph, e.g. the sign for there exist.
//  Are these used in Arabic? That is are all the mathematical signs
//  that are assigned to be mirrorable actually mirrored in Arabic?
//  If that is the case, we'll change the below code to include also
//  characters that mirror to themself. It will then be the responsibility
//  of the display engine to actually mirror these.
//----------------------------------------------------------------------*/

struct {
     FriBidiChar ch, mirrored_ch;
} FriBidiMirroredChars[] = {
__

    for my $i (0..@mirrors-1) {
	my($ch, $mirrored_ch) = @{$mirrors[$i]};
	$c_file .= "  {0x$ch, 0x$mirrored_ch}";
	$c_file .= "," unless $i == @mirrors-1;
	$c_file .= "\n";
    }
    $c_file .= "};\n\n";
    $c_file .= "gint nFriBidiMirroredChars = "
             . scalar(@mirrors) . ";\n";

    print $c_file;
}