| 1 | 10 | ▶ | use 5.10.0; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 2 | 10 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | #no warnings 'experimental'; | ||
| 4 | 10 | ▶ | use strict 'vars'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | #use Carp::Always; | ||
| 6 | |||
| 7 | package Text::Bidi; | ||
| 8 | # ABSTRACT: Unicode bidi algorithm using libfribidi | ||
| 9 | $Text::Bidi::VERSION = '3.0'; | ||
| 10 | 10 | ▶ | use Exporter; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 10 | ▶ | use base qw(Exporter); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 10 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | |||
| 14 | 0 | ▶ | use Text::Bidi::private; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 15 | use Text::Bidi::Array::Byte; | ||
| 16 | use Text::Bidi::Array::Long; | ||
| 17 | use Encode qw(encode decode); | ||
| 18 | |||
| 19 | |||
| 20 | BEGIN { | ||
| 21 | our %EXPORT_TAGS = ( | ||
| 22 | 'all' => [ qw( | ||
| 23 | log2vis | ||
| 24 | is_bidi | ||
| 25 | get_mirror_char | ||
| 26 | get_bidi_type_name | ||
| 27 | fribidi_version | ||
| 28 | fribidi_version_num | ||
| 29 | unicode_version | ||
| 30 | ) ], | ||
| 31 | ); | ||
| 32 | our @EXPORT_OK = ( @{$EXPORT_TAGS{'all'}} ); | ||
| 33 | } | ||
| 34 | |||
| 35 | |||
| 36 | # The following mechanism is used to provide both kinds of interface: Every | ||
| 37 | # method starts with 'my $self = S(@_)' instead of 'my $self = shift'. S | ||
| 38 | # shifts and returns the object if there is one, or returns a global object, | ||
| 39 | # stored in $Global, if there is in @_. The first time $Global is needed, it | ||
| 40 | # is created with type $GlobalClass. | ||
| 41 | |||
| 42 | my $Global; | ||
| 43 | our $GlobalClass = __PACKAGE__; | ||
| 44 | |||
| 45 | |||
| 46 | sub S(\@) { | ||
| 47 | my $l = shift; | ||
| 48 | my $s = $l->[0]; | ||
| 49 | return shift @$l if eval { $s->isa('Text::Bidi') }; | ||
| 50 | $Global = new $GlobalClass unless $Global; | ||
| 51 | $Global | ||
| 52 | } | ||
| 53 | |||
| 54 | |||
| 55 | sub new { | ||
| 56 | my $class = shift; | ||
| 57 | my $self = { | ||
| 58 | tie_byte => 'Text::Bidi::Array::Byte', | ||
| 59 | tie_long => 'Text::Bidi::Array::Long', | ||
| 60 | @_ | ||
| 61 | }; | ||
| 62 | bless $self => $class | ||
| 63 | } | ||
| 64 | |||
| 65 | |||
| 66 | sub tie_byte { | ||
| 67 | my $self = shift; | ||
| 68 | return \undef unless defined $_[0]; | ||
| 69 | $self->{'tie_byte'}->new(@_) | ||
| 70 | } | ||
| 71 | |||
| 72 | sub tie_long { | ||
| 73 | my $self = shift; | ||
| 74 | return \undef unless defined $_[0]; | ||
| 75 | $self->{'tie_long'}->new(@_) | ||
| 76 | } | ||
| 77 | |||
| 78 | |||
| 79 | sub utf8_to_internal { | ||
| 80 | my $self = S(@_); | ||
| 81 | my $str = shift; | ||
| 82 | my ($i, $res) = | ||
| 83 | Text::Bidi::private::utf8_to_internal(encode('utf8', $str)); | ||
| 84 | $self->tie_long($res) | ||
| 85 | } | ||
| 86 | |||
| 87 | |||
| 88 | sub internal_to_utf8 { | ||
| 89 | my $self = S(@_); | ||
| 90 | my $u = shift; | ||
| 91 | $u = $self->tie_long($u) unless eval { defined $$u }; | ||
| 92 | my $r = Text::Bidi::private::internal_to_utf8($$u); | ||
| 93 | decode('utf8', $r) | ||
| 94 | } | ||
| 95 | |||
| 96 | |||
| 97 | sub get_bidi_types { | ||
| 98 | my $self = S(@_); | ||
| 99 | my $u = shift; | ||
| 100 | my $t = Text::Bidi::private::get_bidi_types($$u); | ||
| 101 | $self->tie_long($t) | ||
| 102 | } | ||
| 103 | |||
| 104 | |||
| 105 | sub get_bracket_types { | ||
| 106 | my $self = S(@_); | ||
| 107 | my ($u, $types) = @_; | ||
| 108 | $types //= $self->get_bidi_types($u); | ||
| 109 | $self->tie_long(Text::Bidi::private::get_bracket_types($$u, $$types)) | ||
| 110 | } | ||
| 111 | |||
| 112 | |||
| 113 | sub get_bidi_type_name { | ||
| 114 | my $self = S(@_); | ||
| 115 | Text::Bidi::private::get_bidi_type_name(@_) | ||
| 116 | } | ||
| 117 | |||
| 118 | |||
| 119 | sub get_joining_types { | ||
| 120 | my $self = S(@_); | ||
| 121 | my $u = shift; | ||
| 122 | $self->tie_byte(Text::Bidi::private::get_joining_types($$u)) | ||
| 123 | } | ||
| 124 | |||
| 125 | |||
| 126 | sub get_joining_type_name { | ||
| 127 | my $self = S(@_); | ||
| 128 | Text::Bidi::private::get_joining_type_name(@_) | ||
| 129 | } | ||
| 130 | |||
| 131 | |||
| 132 | sub get_par_embedding_levels { | ||
| 133 | my $self = S(@_); | ||
| 134 | my $bt = shift; | ||
| 135 | my $p = shift // $Text::Bidi::private::FRIBIDI_PAR_ON; | ||
| 136 | my $brackets = shift // $self->tie_long([(0) x @$bt]); | ||
| 137 | my ($lev, $par, $out) = | ||
| 138 | Text::Bidi::private::get_par_embedding_levels_ex($$bt, $$brackets, $p); | ||
| 139 | croak 'FriBidi could not compute paragraph embedding levels' if !$lev && @$bt; | ||
| 140 | my $res = $self->tie_byte($out); | ||
| 141 | ($par, $res) | ||
| 142 | } | ||
| 143 | |||
| 144 | |||
| 145 | sub join_arabic { | ||
| 146 | my $self = S(@_); | ||
| 147 | my ($t, $l, $j) = @_; | ||
| 148 | $self->tie_byte(Text::Bidi::private::join_arabic($$t, $$l, $$j)); | ||
| 149 | } | ||
| 150 | |||
| 151 | |||
| 152 | sub shaped { | ||
| 153 | my $self = S(@_); | ||
| 154 | my ($flags, $el, $prop, $u) = @_; | ||
| 155 | return ($prop, $u) unless defined $flags; | ||
| 156 | $flags ||= $Text::Bidi::private::FRIBIDI_FLAGS_ARABIC; | ||
| 157 | my ($p, $v) =Text::Bidi::private::shape_arabic($flags, $$el, $$prop, | ||
| 158 | $$u); | ||
| 159 | ($self->tie_byte($p), $self->tie_long($v)) | ||
| 160 | } | ||
| 161 | |||
| 162 | |||
| 163 | |||
| 164 | sub mirrored { | ||
| 165 | my $self = S(@_); | ||
| 166 | my ($el, $u) = @_; | ||
| 167 | my $r =Text::Bidi::private::shape_mirroring($$el, $$u); | ||
| 168 | my $res = $self->tie_long($r) | ||
| 169 | } | ||
| 170 | |||
| 171 | |||
| 172 | sub hash2flags { | ||
| 173 | my ($self, $flags) = @_; | ||
| 174 | my $res = 0; | ||
| 175 | foreach ( keys %$flags ) { | ||
| 176 | next unless $flags->{$_}; | ||
| 177 | next unless $_ eq uc; | ||
| 178 | $res |= ${"Text::Bidi::private::FRIBIDI_FLAG_$_"}; | ||
| 179 | } | ||
| 180 | $res | ||
| 181 | } | ||
| 182 | |||
| 183 | |||
| 184 | sub reorder { | ||
| 185 | my $self = S(@_); | ||
| 186 | my ($str, $map, $off, $len) = @_; | ||
| 187 | $off //= 0; | ||
| 188 | $len //= @$str - $off; | ||
| 189 | join('', (@$str)[@$map[$off..$off+$len-1]]) | ||
| 190 | } | ||
| 191 | |||
| 192 | |||
| 193 | sub reorder_map { | ||
| 194 | my $self = S(@_); | ||
| 195 | my ($bt, $off, $len, $par, $map, $el, $flags) = @_; | ||
| 196 | unless ( defined $el ) { | ||
| 197 | (my $p, $el) = $self->get_par_embedding_levels($bt, $par); | ||
| 198 | $par //= $p; | ||
| 199 | } | ||
| 200 | if ( defined $flags ) { | ||
| 201 | $flags = $self->hash2flags($flags) if ref $flags; | ||
| 202 | } else { | ||
| 203 | $flags = $Text::Bidi::private::FRIBIDI_FLAGS_DEFAULT; | ||
| 204 | } | ||
| 205 | $map //= [0..$#$bt]; | ||
| 206 | |||
| 207 | $map = $self->tie_long($map) unless eval {defined $$map}; | ||
| 208 | |||
| 209 | |||
| 210 | my ($lev, $elout, $mout) = Text::Bidi::private::reorder_map( | ||
| 211 | $flags, $$bt, $off, $len, $par, $$el, $$map); | ||
| 212 | croak 'FriBidi could not reorder the line' if !$lev && $len; | ||
| 213 | |||
| 214 | ($elout, $mout) | ||
| 215 | } | ||
| 216 | |||
| 217 | # TODO this doesn't work | ||
| 218 | |||
| 219 | |||
| 220 | sub remove_bidi_marks { | ||
| 221 | my $self = S(@_); | ||
| 222 | my ($v, $to, $from, $levels) = @_; | ||
| 223 | $to = $self->tie_long($to) unless eval {defined $$to}; | ||
| 224 | if ( defined($from) ) { | ||
| 225 | $from = $self->tie_long($from) unless eval {defined $$from}; | ||
| 226 | } else { | ||
| 227 | $from = \undef; | ||
| 228 | } | ||
| 229 | $levels = $self->tie_byte($levels) unless eval {defined $$levels}; | ||
| 230 | no warnings 'uninitialized'; | ||
| 231 | my ($len, $vout, $toout, $fromout, $levelsout) = | ||
| 232 | Text::Bidi::private::remove_bidi_marks($v, $$to, $$from, $$levels); | ||
| 233 | ($vout, $toout, $fromout, $levelsout) | ||
| 234 | } | ||
| 235 | |||
| 236 | |||
| 237 | sub log2vis { | ||
| 238 | require Text::Bidi::Paragraph; | ||
| 239 | my ($log, $width, $dir, $flags) = @_; | ||
| 240 | my $p = new Text::Bidi::Paragraph $log, dir => $dir; | ||
| 241 | $width //= $p->len; | ||
| 242 | my $off = 0; | ||
| 243 | my @visual; | ||
| 244 | while ( $off < $p->len ) { | ||
| 245 | my $v = $p->visual($off, $width, $flags); | ||
| 246 | my $l = length($v); | ||
| 247 | $off += $l; | ||
| 248 | $v = (' ' x ($width - $l)) . $v if $p->is_rtl; | ||
| 249 | push @visual, $v; | ||
| 250 | } | ||
| 251 | ($p, join("\n", @visual)) | ||
| 252 | } | ||
| 253 | |||
| 254 | |||
| 255 | sub is_bidi { $_[0] =~ /\p{bc=R}|\p{bc=AL}/ } | ||
| 256 | |||
| 257 | |||
| 258 | sub get_mirror_char { | ||
| 259 | my $self = S(@_); | ||
| 260 | my $u = shift; | ||
| 261 | $u = $self->utf8_to_internal($u) unless ref($u); | ||
| 262 | my $r = Text::Bidi::private::get_mirror_char($u->[0]); | ||
| 263 | my $res = $self->tie_long([$r]); | ||
| 264 | wantarray ? ($res) : $self->internal_to_utf8($res) | ||
| 265 | } | ||
| 266 | |||
| 267 | |||
| 268 | sub fribidi_version { | ||
| 269 | $Text::Bidi::private::version_info | ||
| 270 | } | ||
| 271 | |||
| 272 | |||
| 273 | sub fribidi_version_num { | ||
| 274 | fribidi_version =~ /\(GNU FriBidi\) ([0-9.]*)/ ? $1 : () | ||
| 275 | } | ||
| 276 | |||
| 277 | |||
| 278 | sub unicode_version { | ||
| 279 | $Text::Bidi::private::unicode_version | ||
| 280 | } | ||
| 281 | |||
| 282 | |||
| 283 | 1; # End of Text::Bidi | ||
| 284 | |||
| 285 | __END__ | ||