Text-Bidi

 view release on metacpan or  search on metacpan

lib/Text/Bidi.pm  view on Meta::CPAN

use warnings;
#no warnings 'experimental';
use strict 'vars';
#use Carp::Always;

package Text::Bidi;
# ABSTRACT: Unicode bidi algorithm using libfribidi
$Text::Bidi::VERSION = '2.18';
use Exporter;
use base qw(Exporter);
use Carp;

use Text::Bidi::private;
use Text::Bidi::Array::Byte;
use Text::Bidi::Array::Long;
use Encode qw(encode decode);


BEGIN {
    our %EXPORT_TAGS = (
        'all' => [ qw(
            log2vis
            is_bidi
            get_mirror_char
            get_bidi_type_name
            fribidi_version
            fribidi_version_num
            unicode_version
        ) ],
    );
    our @EXPORT_OK = ( @{$EXPORT_TAGS{'all'}} );
}


# The following mechanism is used to provide both kinds of interface: Every 
# method starts with 'my $self = S(@_)' instead of 'my $self = shift'. S 
# shifts and returns the object if there is one, or returns a global object, 
# stored in $Global, if there is in @_. The first time $Global is needed, it 
# is created with type $GlobalClass.

my $Global;
our $GlobalClass = __PACKAGE__;


sub S(\@) {
    my $l = shift;
    my $s = $l->[0];
    return shift @$l if eval { $s->isa('Text::Bidi') };
    $Global = new $GlobalClass unless $Global;
    $Global
}


sub new {
    my $class = shift;
    my $self = {
        tie_byte => 'Text::Bidi::Array::Byte',
        tie_long => 'Text::Bidi::Array::Long',
        @_
    };
    bless $self => $class
}


sub tie_byte {
    my $self = shift;
    return \undef unless defined $_[0];
    $self->{'tie_byte'}->new(@_)
}

sub tie_long {
    my $self = shift;
    return \undef unless defined $_[0];
    $self->{'tie_long'}->new(@_)
}


sub utf8_to_internal {
    my $self = S(@_);
    my $str = shift;
    my ($i, $res) = 
      Text::Bidi::private::utf8_to_internal(encode('utf8', $str));
    $self->tie_long($res)
}


sub internal_to_utf8 {
    my $self = S(@_);
    my $u = shift;
    $u = $self->tie_long($u) unless eval { defined $$u };
    my $r = Text::Bidi::private::internal_to_utf8($$u);
    decode('utf8', $r)
}


sub get_bidi_types {
    my $self = S(@_);
    my $u = shift;
    my $t = Text::Bidi::private::get_bidi_types($$u);
    $self->tie_long($t)
}


sub get_bidi_type_name {
    my $self = S(@_);
    Text::Bidi::private::get_bidi_type_name(@_)
}


sub get_joining_types {
    my $self = S(@_);
    my $u = shift;
    $self->tie_byte(Text::Bidi::private::get_joining_types($$u))
}


sub get_joining_type_name {
    my $self = S(@_);
    Text::Bidi::private::get_joining_type_name(@_)
}

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 1.571 second using v1.00-cache-2.02-grep-82fe00e-cpan-d29e8ade9f55 )