7 sub croak { require Carp; Carp::croak(@_) }
10 my ($class, $type, $list, $minbits, $none) = @_;
13 print STDERR "SWASHNEW @_\n" if DEBUG;
17 if ($type and ref ${"${class}::{$type}"} eq $class) {
18 warn qq/Found \${"${class}::{$type}"}\n/ if DEBUG;
19 return ${"${class}::{$type}"}; # Already there...
24 defined %utf8::In || do "unicore/In.pl";
26 $type =~ s/^In(?:[-_]|\s+)?(?!herited$)//i;
29 my $inprefix = substr(lc($type), 0, 3);
30 if (exists $utf8::InPat{$inprefix}) {
32 for my $k (keys %{$utf8::InPat{$inprefix}}) {
34 $In = $utf8::InPat{$inprefix}->{$k};
35 if (exists $utf8::In{$In}) {
36 $file = "unicore/In/$utf8::In{$In}";
37 print "inprefix = $inprefix, In = $In, k = $k, file = $file\n" if DEBUG;
44 unless (defined $file) {
45 # This is separate from 'To' in preparation of Is.pl (a la In.pl).
46 if ($type =~ /^Is([A-Z][A-Za-z]*|L&)$/ || $type =~ /^(L&)$/ ) {
47 my $cat = $1 eq 'L&' ? 'L' : $1;
48 $file = "unicore/Is/$cat";
49 } elsif ((not defined $file) && $type =~ /^To([A-Z][A-Za-z]*)$/) {
50 $file = "unicore/To/$1";
56 $list ||= do "$file.pl"
57 || do "unicore/Is/$type.pl"
58 || croak("Can't find Unicode character property \"$type\"");
65 my @tmp = split(/^/m, $list);
68 $extras = join '', grep /^[^0-9a-fA-F]/, @tmp;
70 sort { hex $a <=> hex $b }
71 grep {/^([0-9a-fA-F]+)/ and not $seen{$1}++} @tmp; # XXX doesn't do ranges right
75 my $hextra = sprintf "%04x", $none + 1;
76 $list =~ s/\tXXXX$/\t$hextra/mg;
81 while ($list =~ /^([0-9a-fA-F]+)(?:\t([0-9a-fA-F]+)?)(?:\t([0-9a-fA-F]+))?/mg) {
83 my $max = hex(defined $2 ? $2 : $1);
84 my $val = hex(defined $3 ? $3 : "");
85 $val += $max - $min if defined $3;
86 $top = $val if $val > $top;
93 $bits = $minbits if $bits < $minbits;
98 while ($x =~ /^([^0-9a-fA-F\n])(.*)/mg) {
101 # print STDERR "$1 => $2\n" if DEBUG;
102 if ($char =~ /[-+!]/) {
103 my ($c,$t) = split(/::/, $name, 2); # bogus use of ::, really
104 my $subobj = $c->SWASHNEW($t, "", 0, 0, 0);
105 push @extras, $name => $subobj;
106 $bits = $subobj->{BITS} if $bits < $subobj->{BITS};
111 print STDERR "CLASS = $class, TYPE => $type, BITS => $bits, NONE => $none\nEXTRAS =>\n$extras\nLIST =>\n$list\n" if DEBUG;
113 ${"${class}::{$type}"} = bless {
123 # NOTE: utf8.c:swash_init() assumes entries are never modified once generated.
126 my ($self, $start, $len) = @_;
127 local $^D = 0 if $^D;
128 my $type = $self->{TYPE};
129 my $bits = $self->{BITS};
130 my $none = $self->{NONE};
131 print STDERR "SWASHGET @_ [$type/$bits/$none]\n" if DEBUG;
132 my $end = $start + $len;
135 vec($swatch, $len - 1, $bits) = 0; # Extend to correct length.
137 for $key (0 .. $len - 1) { vec($swatch, $key, $bits) = $none }
140 for ($self->{LIST}) {
144 while (/^([0-9a-fA-F]+)(?:\t([0-9a-fA-F]+)?)(?:\t([0-9a-fA-F]+))?/mg) {
146 my $max = (defined $2 ? hex $2 : $min);
148 next if $max < $start;
149 # print "$min $max $val\n";
152 $val += $start - $min if $val < $none;
155 for ($key = $min; $key <= $max; $key++) {
156 last LINE if $key >= $end;
157 # print STDERR "$key => $val\n" if DEBUG;
158 vec($swatch, $key - $start, $bits) = $val;
159 ++$val if $val < $none;
164 $val += $start - $min;
167 for ($key = $min; $key <= $max; $key++, $val++) {
168 last LINE if $key >= $end;
169 # print STDERR "$key => $val\n" if DEBUG;
170 vec($swatch, $key - $start, $bits) = $val;
177 while (/^([0-9a-fA-F]+)(?:\t([0-9a-fA-F]+))?/mg) {
179 my $max = (defined $2 ? hex $2 : $min);
180 next if $max < $start;
184 for ($key = $min; $key <= $max; $key++) {
185 last LINE if $key >= $end;
186 # print STDERR "$key => 1\n" if DEBUG;
187 vec($swatch, $key - $start, 1) = 1;
192 for my $x ($self->{EXTRAS}) {
194 while ($x =~ /^([-+!])(.*)/mg) {
197 print STDERR "INDIRECT $1 $2\n" if DEBUG;
198 my $otherbits = $self->{$name}->{BITS};
199 croak("SWASHGET size mismatch") if $bits < $otherbits;
200 my $other = $self->{$name}->SWASHGET($start, $len);
202 if ($bits == 1 and $otherbits == 1) {
206 for ($key = 0; $key < $len; $key++) {
207 vec($swatch, $key, $bits) = vec($other, $key, $otherbits);
211 elsif ($char eq '!') {
212 if ($bits == 1 and $otherbits == 1) {
216 for ($key = 0; $key < $len; $key++) {
217 if (!vec($other, $key, $otherbits)) {
218 vec($swatch, $key, $bits) = 1;
223 elsif ($char eq '-') {
224 if ($bits == 1 and $otherbits == 1) {
228 for ($key = 0; $key < $len; $key++) {
229 if (vec($other, $key, $otherbits)) {
230 vec($swatch, $key, $bits) = 0;
238 print STDERR "CELLS ";
239 for ($key = 0; $key < $len; $key++) {
240 print STDERR vec($swatch, $key, $bits), " ";