blib/lib/Text/Bidi/Paragraph.pm

filter: Statementstmt 85.7% 12 / 14 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 89.4% 17 / 19 CC 1 SCAR 0.6
CC
1
Cov
86%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« blib/lib/Text/Bidi/Constants.pm ↑ Summary blib/lib/Text/Bidi.pm »
1 # Created: Tue 27 Aug 2013 04:10:03 PM IDT
2 # Last Changed: Sat 07 Nov 2015 10:27:08 PM IST
3  
4 1 ▶use 5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 #no warnings 'experimental';
7 1 ▶use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 package Text::Bidi::Paragraph;
11 # ABSTRACT: Run the bidi algorithm on one paragraph
12 $Text::Bidi::Paragraph::VERSION = '3.0';
13  
14 0 ▶use Text::Bidi;
Subroutine BEGIN: called CC=1 SCAR=2.6
15  
16  
17 sub new {
18 my $class = shift;
19 my $par = shift;
20 my $self = { @_ };
21 my @bd = ($self->{'bd'});
22 $self->{'bd'} = Text::Bidi::S(@bd);
23 $self->{'par'} = $par;
24 $self->{'shape'} = 0
25 if $par =~ /[\p{bc=AL}\p{bc=AN}]/ and not exists $self->{'shape'};
26 bless $self => $class;
27 $self->_init
28 }
29  
30  
31 for my $f ( qw(par bd dir _par _mirpar _unicode _mirrored )) {
32 no strict 'refs';
33 *$f = sub { $_[0]->{$f} };
34 }
35  
36 for my $f ( qw(len types levels map) ) {
37 no strict 'refs';
38 *$f = sub { $_[0]->{"_$f"} };
39 }
40  
41  
42 sub type_names {
43 my $self = shift;
44 my $bd = $self->bd;
45 map { $bd->get_bidi_type_name($_) } @{$self->types}
46 }
47  
48  
49 sub is_rtl { $_[0]->dir == $Text::Bidi::private::FRIBIDI_PAR_RTL }
50  
51 sub _init {
52 my ($self) = (@_);
53 my $par = $self->par;
54 $self->{'_len'} = length($par);
55 my $bd = $self->bd;
56 $self->{'_unicode'} = $bd->utf8_to_internal($par);
57 #$self->{'_par'} = [split '', $par];
58 $self->{'_types'} = $bd->get_bidi_types($self->_unicode);
59 my $brackets = $bd->get_bracket_types($self->_unicode, $self->types);
60 ($self->{'dir'}, $self->{'_levels'}) =
61 $bd->get_par_embedding_levels($self->types, $self->dir, $brackets);
62 $self->{'_map'} = [0..$#{$self->_unicode}];
63 $self->{'_unicode'} = $self->shaped($self->{'shape'})
64 if defined $self->{'shape'};
65 $self->{'_mirrored'} = $bd->mirrored($self->levels, $self->_unicode);
66 $self->{'_mirpar'} = $bd->internal_to_utf8($self->_mirrored);
67 $self->{'_par'} = [split '', $self->_mirpar ];
68 $self
69 }
70  
71  
72 sub ar_props {
73 my ($self) = (@_);
74 $self->{'_ar_props'} //= $self->bd->join_arabic($self->types, $self->levels, $self->bd->get_joining_types($self->_unicode))
75 }
76  
77  
78 sub shaped {
79 my ($self, $flags) = (@_);
80 ($self->{'_ar_props'}, $self->{'_shaped'}) = $self->bd->shaped(
81 $flags, $self->levels, $self->ar_props, $self->_unicode)
82 unless defined $self->{'_shaped'};
83 $self->{'_shaped'}
84 }
85  
86  
87 sub visual {
88 my ($self, $off, $len, $flags) = @_;
89 $off //= 0;
90 $len //= $self->len;
91 my $mlen = $self->len - $off;
92 $mlen = $len if $len < $mlen;
93 if (defined($flags) and my $break = eval { $flags->{'break'} } ) {
94 my $lb = length($break);
95 my $nlen = rindex($self->par, $break, $off + $mlen - $lb) - $off + $lb;
96 $mlen = $nlen if $nlen > 0;
97 }
98 my $bd = $self->bd;
99 (my $levels, $self->{'_map'}) =
100 $bd->reorder_map($self->types, $off, $mlen, $self->dir,
101 $self->map, $self->levels, $flags);
102 my $visual = $bd->reorder($self->_par, $self->map, $off, $mlen);
103 # TODO This does not currently work
104 if (defined($flags) and eval { $flags->{'remove_marks'} } ) {
105 ($visual, $self->{'_map'}, undef, $levels) =
106 $bd->remove_bidi_marks($visual, $self->map, undef, $levels);
107 }
108 $self->{'_levels'} = $bd->tie_byte($levels);
109
110 $visual
111 }
112  
113 1;
114  
115 __END__
« blib/lib/Text/Bidi/Constants.pm ↑ Summary blib/lib/Text/Bidi.pm »