blib/lib/Text/Bidi/private.pm

filter: Statementstmt 30.7% 35 / 114 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 42.8% 12 / 28 Podpod n/a totaltotal 27.9% 47 / 168 CC 9 SCAR 22.4
CC
9
Cov
26%
reorder_map
42.0
_count
12.0
_same_length
6.0
CRAP 9.4
« blib/lib/Text/Bidi/Selection.pm ↑ Summary blib/lib/Text/Bidi/Array/Long.pm »
1 package Text::Bidi::private;
2 $Text::Bidi::private::VERSION = '3.01';
3 12 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 12 ▶use FFI::CheckLib 0.28 qw(find_lib_or_die);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 12 ▶use FFI::Platypus 2.00;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 12 ▶use FFI::Platypus::Buffer qw(scalar_to_pointer);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 12 ▶use version 0.77;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # ABSTRACT: Private FFI bindings for FriBidi
13  
14 # These are FriBidi's ABI constants, rather than Unicode property tables.
15 # Keep their scalar interface for Text::Bidi::Constants and existing callers.
16 # Values follow fribidi-{unicode,bidi-types,flags,joining-types,brackets,
17 # deprecated}.h, including the former wrapper's signed bracket-open mask.
18 while (<DATA>) {
19 my ($name, $value) = split;
20 12 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
21 ${__PACKAGE__ . '::' . $name} = 0 + $value;
22 Internals::SvREADONLY(${__PACKAGE__ . '::' . $name}, 1);
23 }
24 close DATA;
25  
26 my $ffi = FFI::Platypus->new(
27 api => 2,
28 lib => $ENV{TEXT_BIDI_FRIBIDI_LIB} || find_lib_or_die(lib => 'fribidi'),
29 );
30  
31 for my $name (qw(version_info unicode_version)) {
32 my $address = $ffi->find_symbol("fribidi_$name")
33 or croak "Cannot find fribidi_$name in the FriBidi library";
34 my $pointer = $ffi->cast('opaque' => 'opaque*', $address);
35 12 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
36 ${__PACKAGE__ . '::' . $name} = $ffi->cast('opaque' => 'string', $$pointer);
37 Internals::SvREADONLY(${__PACKAGE__ . '::' . $name}, 1);
38 }
39  
40 our $version_info;
41 my ($library_version) = $version_info =~ /\(GNU FriBidi\) ([0-9.]+)/;
42 croak 'Text::Bidi requires FriBidi 1.0.0 or newer'
43 unless defined $library_version
44 && version->parse($library_version) >= version->parse('1.0.0');
45  
46 # FriBidiChar/CharType/BracketType/ParType/Flags are uint32_t;
47 # StrIndex is int32_t, Level is signed char, and ArabicProp is uint8_t.
48 # Packed arrays are passed as opaque pointers, with lengths checked below.
49 my @bindings = (
50 [get_bidi_type => ['uint32'] => 'uint32'],
51 [get_bidi_type_name => ['uint32'] => 'string'],
52 [get_joining_type => ['uint32'] => 'uint8'],
53 [get_joining_type_name => ['uint8'] => 'string'],
54 [get_bracket => ['uint32'] => 'uint32'],
55 [get_bidi_types => ['opaque', 'sint32', 'opaque'] => 'void'],
56 [get_joining_types => ['opaque', 'sint32', 'opaque'] => 'void'],
57 [get_bracket_types => ['opaque', 'sint32', 'opaque', 'opaque'] => 'void'],
58 [get_par_direction => ['opaque', 'sint32'] => 'uint32'],
59 [get_par_embedding_levels_ex =>
60 ['opaque', 'opaque', 'sint32', 'uint32*', 'opaque'] => 'sint8'],
61 [get_mirror_char => ['uint32', 'uint32*'] => 'int'],
62 [shape_mirroring => ['opaque', 'sint32', 'opaque'] => 'void'],
63 [join_arabic => ['opaque', 'sint32', 'opaque', 'opaque'] => 'void'],
64 [shape_arabic => ['uint32', 'opaque', 'sint32', 'opaque', 'opaque'] => 'void'],
65 [shape => ['uint32', 'opaque', 'sint32', 'opaque', 'opaque'] => 'void'],
66 [reorder_line =>
67 ['uint32', 'opaque', 'sint32', 'sint32', 'uint32', 'opaque', 'opaque', 'opaque']
68 => 'sint8'],
69 [parse_charset => ['string'] => 'int'],
70 [charset_to_unicode => ['int', 'opaque', 'sint32', 'opaque'] => 'sint32'],
71 [unicode_to_charset => ['int', 'opaque', 'sint32', 'opaque'] => 'sint32'],
72 );
73 for my $binding (@bindings) {
74 my ($name, $args, $return) = @$binding;
75 $ffi->attach(["fribidi_$name" => "_c_$name"] => $args => $return);
76 }
77  
78 # Simple scalar lookups need no packed-array adapter.
79 for my $name (qw(get_bidi_type get_bidi_type_name get_joining_type
80 get_joining_type_name get_bracket)) {
81 12 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
82 *{$name} = \&{"_c_$name"};
83 }
84  
85 my $utf8 = _c_parse_charset('UTF-8') or croak 'FriBidi has no UTF-8 charset';
86  
87 0 ▶sub _count {
Subroutine _count: not called CC=3 SCAR=24.8
88 # SvPVbyte in the former wrapper also rejects wide-character buffers.
89 0 utf8::downgrade($_[0]);
90 0 my $size = length($_[0]);
91 0 ▶ croak 'Invalid packed array length'
Branch: 1 branch
BranchTrueFalseTotal
if $size % $_[1] or $size / $_[1] > 2147483647 0 0 0
Condition0%0/3
$size % $_[1] or $size / $_[1] > 2147483647
l!l&&r!l&&!r
000
MC/DC0%0/2
$size % $_[1] or $size / $_[1] > 2147483647
$size % $_[1] $size / $_[1] > 2147483647
Truth table0%0/3
A or B
A= $size % $_[1]B= $size / $_[1] > 2147483647
ABresult
1X1
011
000
92 if $size % $_[1] || $size / $_[1] > 2147483647;
93 0 return $size / $_[1];
94 }
95  
96 0 ▶sub _same_length {
Subroutine _same_length: not called CC=2 SCAR=17.9
97 0 my ($expected, $width) = @_[1, 2];
98 0 ▶ croak 'Packed arrays must have equal lengths'
Branch: 1 branch
BranchTrueFalseTotal
if _count($_[0], $width) != $expected 0 0 0
99 if _count($_[0], $width) != $expected;
100 }
101  
102 0 ▶sub utf8_to_internal {
Subroutine utf8_to_internal: not called CC=1 SCAR=6.9
103 0 my ($input) = @_;
104 0 my $len = _count($input, 1);
105 0 my $out = "\0" x (4 * ($len + 1));
106 0 my $count = _c_charset_to_unicode(
107 $utf8, scalar_to_pointer($input), $len, scalar_to_pointer($out));
108 0 return ($count, substr($out, 0, 4 * $count));
109 }
110  
111 0 ▶sub internal_to_utf8 {
Subroutine internal_to_utf8: not called CC=1 SCAR=6.9
112 0 my ($input) = @_;
113 0 my $len = _count($input, 4);
114 0 my $out = "\0" x (4 * ($len + 1));
115 0 my $count = _c_unicode_to_charset(
116 $utf8, scalar_to_pointer($input), $len, scalar_to_pointer($out));
117 0 return ($count, substr($out, 0, $count));
118 }
119  
120 0 ▶sub get_bidi_types {
Subroutine get_bidi_types: not called CC=1 SCAR=6.9
121 0 my ($chars) = @_;
122 0 my $len = _count($chars, 4);
123 0 my $out = "\0" x (4 * $len);
124 0 _c_get_bidi_types(scalar_to_pointer($chars), $len, scalar_to_pointer($out));
125 0 return [unpack 'L*', $out];
126 }
127  
128 0 ▶sub get_joining_types {
Subroutine get_joining_types: not called CC=1 SCAR=6.9
129 0 my ($chars) = @_;
130 0 my $len = _count($chars, 4);
131 0 my $out = "\0" x $len;
132 0 _c_get_joining_types(scalar_to_pointer($chars), $len, scalar_to_pointer($out));
133 0 return [unpack 'C*', $out];
134 }
135  
136 0 ▶sub get_bracket_types {
Subroutine get_bracket_types: not called CC=1 SCAR=6.9
137 0 my ($chars, $types) = @_;
138 0 my $len = _count($chars, 4);
139 0 _same_length($types, $len, 4);
140 0 my $out = "\0" x (4 * $len);
141 0 _c_get_bracket_types(scalar_to_pointer($chars), $len,
142 scalar_to_pointer($types), scalar_to_pointer($out));
143 0 return [unpack 'L*', $out];
144 }
145  
146 0 ▶sub get_par_direction {
Subroutine get_par_direction: not called CC=1 SCAR=6.9
147 0 my ($types) = @_;
148 0 my $len = _count($types, 4);
149 0 return _c_get_par_direction(scalar_to_pointer($types), $len);
150 }
151  
152 0 ▶sub get_par_embedding_levels_ex {
Subroutine get_par_embedding_levels_ex: not called CC=1 SCAR=6.9
153 0 my ($types, $brackets, $dir) = @_;
154 0 my $len = _count($types, 4);
155 0 _same_length($brackets, $len, 4);
156 0 my $out = "\0" x $len;
157 0 my $max = _c_get_par_embedding_levels_ex(scalar_to_pointer($types),
158 scalar_to_pointer($brackets), $len, \$dir, scalar_to_pointer($out));
159 0 return ($max, $dir, [unpack 'C*', $out]);
160 }
161  
162 0 ▶sub get_mirror_char {
Subroutine get_mirror_char: not called CC=1 SCAR=6.9
163 0 my ($ch) = @_;
164 0 my $mirror = $ch;
165 0 my $found = _c_get_mirror_char($ch, \$mirror);
166 0 return ($found, $mirror);
167 }
168  
169 0 ▶sub shape_mirroring {
Subroutine shape_mirroring: not called CC=1 SCAR=6.9
170 0 my ($levels, $chars) = @_;
171 0 my $len = _count($levels, 1);
172 0 _same_length($chars, $len, 4);
173 # pack makes a fresh writable allocation, including for COW/literal inputs.
174 0 my $out = pack 'a*', $chars;
175 0 _c_shape_mirroring(scalar_to_pointer($levels), $len, scalar_to_pointer($out));
176 0 return $out;
177 }
178  
179 0 ▶sub join_arabic {
Subroutine join_arabic: not called CC=1 SCAR=6.9
180 0 my ($types, $levels, $props) = @_;
181 0 my $len = _count($types, 4);
182 0 _same_length($levels, $len, 1);
183 0 _same_length($props, $len, 1);
184 0 my $out = pack 'a*', $props;
185 0 _c_join_arabic(scalar_to_pointer($types), $len,
186 scalar_to_pointer($levels), scalar_to_pointer($out));
187 0 return [unpack 'C*', $out];
188 }
189  
190 0 ▶sub _shape {
Subroutine _shape: not called CC=1 SCAR=6.9
191 0 my ($call, $flags, $levels, $props, $chars) = @_;
192 0 my $len = _count($levels, 1);
193 0 _same_length($props, $len, 1);
194 0 _same_length($chars, $len, 4);
195 0 my $out_props = pack 'a*', $props;
196 0 my $out_chars = pack 'a*', $chars;
197 0 $call->($flags, scalar_to_pointer($levels), $len,
198 scalar_to_pointer($out_props), scalar_to_pointer($out_chars));
199 0 return ([unpack 'C*', $out_props], $out_chars);
200 }
201  
202 0 ▶sub shape_arabic { return _shape(\&_c_shape_arabic, @_) }
Subroutine shape_arabic: not called CC=1 SCAR=6.9
203 0 ▶sub shape { return _shape(\&_c_shape, @_) }
Subroutine shape: not called CC=1 SCAR=6.9
204  
205 0 ▶sub reorder_map {
Subroutine reorder_map: not called CC=6 SCAR=37.4
206 0 my ($flags, $types, $off, $length, $dir, $levels, $map) = @_;
207 0 my $len = _count($types, 4);
208 0 _same_length($levels, $len, 1);
209 0 _same_length($map, $len, 4);
210 0 ▶ croak 'Line range is outside the paragraph'
Branch: 1 branch
BranchTrueFalseTotal
if $off < 0 or $length < 0 or $off > $len or $length > $len - $off 0 0 0
Condition0%0/9
$off < 0 or $length < 0 or $off > $len or $length > $len - $off
l!l&&r!l&&!r
000
$off < 0 or $length < 0 or $off > $len
l!l&&r!l&&!r
000
$off < 0 or $length < 0
l!l&&r!l&&!r
000
MC/DC0%0/4
$off < 0 or $length < 0 or $off > $len or $length > $len - $off
$off < 0 $length < 0 $off > $len $length > $len - $off
Truth table0%0/5
A or B or C or D
A= $off < 0B= $length < 0C= $off > $lenD= $length > $len - $off
ABCDresult
1XXX1
01XX1
001X1
00011
00000
211 if $off < 0 || $length < 0 || $off > $len || $length > $len - $off;
212 0 my $out_levels = pack 'a*', $levels;
213 0 my $out_map = pack 'a*', $map;
214 0 my $max = _c_reorder_line($flags, scalar_to_pointer($types), $length, $off,
215 $dir, scalar_to_pointer($out_levels), undef, scalar_to_pointer($out_map));
216 # The original wrapper promoted signed indices to Perl unsigned integers.
217 # Preserve that convention for caller-supplied sentinel values as well.
218 0 ▶ my @indices = map { $_ < 0 ? ~(-1 - $_) : $_ } unpack 'l*', $out_map;
Branch: 1 branch
BranchTrueFalseTotal
$_ < 0 ? : 0 0 0
219 0 return ($max, [unpack 'C*', $out_levels], \@indices);
220 }
221  
222 # Retain the deprecated private entry points for existing consumers. Bind them
223 # directly to FriBidi so their legacy status flags retain the same semantics.
224 for my $name (qw(mirroring_status reorder_nsm_status)) {
225 $ffi->attach(["fribidi_$name" => $name] => [] => 'int');
226 }
227 for my $name (qw(set_mirroring set_reorder_nsm)) {
228 $ffi->attach(["fribidi_$name" => $name] => ['int'] => 'int');
229 }
230 for my $name (qw(get_type get_type_internal)) {
231 $ffi->attach(["fribidi_$name" => $name] => ['uint32'] => 'uint32');
232 }
233 for my $name (qw(get_par_embedding_levels log2vis_get_embedding_levels)) {
234 $ffi->attach(["fribidi_$name" => "_c_$name"] =>
235 ['opaque', 'sint32', 'uint32*', 'opaque'] => 'sint8');
236 12 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
237 my $call = \&{"_c_$name"};
238 *{$name} = sub {
239 my ($types, $dir) = @_;
240 my $len = _count($types, 4);
241 my $out = "\0" x $len;
242 my $max = $call->(scalar_to_pointer($types), $len,
243 \$dir, scalar_to_pointer($out));
244 return ($max, $dir, [unpack 'C*', $out]);
245 };
246 }
247  
248 1;
249  
250 =pod
251  
252 =head1 NAME
253  
254 Text::Bidi::private - Private FFI bindings for FriBidi
255  
256 =head1 VERSION
257  
258 version 3.01
259  
260 =head1 DESCRIPTION
261  
262 This is the internal FriBidi adapter used by L<Text::Bidi>. It loads the
263 system shared library using L<FFI::Platypus>, and preserves the packed-buffer
264 and return-value conventions of the original SWIG adapter. Applications
265 should use the public modules instead of calling this package directly.
266  
267 C<TEXT_BIDI_FRIBIDI_LIB> can specify a shared library path when FriBidi is
268 installed outside the system library search paths. FriBidi 1.0.0 or newer
269 is required. No FriBidi headers or generated wrapper are needed.
270  
271 =head1 AUTHOR
272  
273 Moshe Kamensky <kamensky@cpan.org>
274  
275 =head1 COPYRIGHT AND LICENSE
276  
277 This software is copyright (c) 2015 by Moshe Kamensky.
278  
279 This is free software; you can redistribute it and/or modify it under
280 the same terms as the Perl 5 programming language system itself.
281  
282 =cut
283  
284 __DATA__
« blib/lib/Text/Bidi/Selection.pm ↑ Summary blib/lib/Text/Bidi/Array/Long.pm »