API2/UniWrap.pm

filter: Statementstmt 29.8% 51 / 171 Branchbran 0.0% 0 / 110 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 61.9% 13 / 21 Podpod 0.0% 0 / 7 totaltotal 18.6% 64 / 343 CC 65 SCAR 56.1
CC
65
Cov
17%
class_properties
538.3
find_breaks
378.7
lb_class
182.0
CRAP 272.4
« API2.pm ↑ Summary API2/Basic/PDF/Filter/FlateDecode.pm »
1 package PDF::API2::UniWrap;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶no warnings qw[ deprecated recursion uninitialized ];
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION = '2.049'; # VERSION
7  
8 # Implements UAX#14: Line Breaking Properties
9 # David Nesting <david@fastolfe.net>
10  
11 BEGIN {
12  
13 1 ▶ use Encode qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 ▶ use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶ use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 ▶ use Unicode::UCD;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶ use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: not called CC=1 SCAR=0.0
20  
21 }
22  
23 1 our $DEBUG = 0;
24 1 our $columns = 75;
25  
26 1 my %classified;
27 my $procedural_self;
28 1 my $txt;
29  
30 1 ▶use constant PROHIBITED => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 1 ▶use constant INDIRECT => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 1 ▶use constant DIRECT => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 1 ▶use constant REQUIRED => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 1 my @CLASSES = qw{ OP CL QU GL NS EX SY IS PR PO NU AL ID IN HY BA BB B2 ZW CM };
36 1 my %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 2 2 2 2 2 2 2 2 2 2 2 2 2 0 1 ]],
56 CM => [qw[ 2 0 1 1 1 0 0 0 2 2 1 1 2 1 1 1 2 2 0 1 ]],
57 );
58  
59 # Convert the table above into a hash that we can use for speedier lookups
60  
61 1 foreach (keys %BREAK_TABLE) {
62 20 my @t = @CLASSES;
63 20 $BREAK_TABLE{$_} = { map { shift(@t) => $_ } @{$BREAK_TABLE{$_}} };
64 }
65  
66 0 ▶sub new {
Subroutine new: not called CC=5 SCAR=34.0
Pod undocumented
67 0 my $pkg = shift;
68 0 my $self = { @_ };
69 0 ▶ $self->{line_length} ||= $columns;
Condition0%0/3
$self->{"line_length"} ||= $PDF::API2::UniWrap::columns
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"line_length"} ||= $PDF::API2::UniWrap::columns
$self->{"line_length"} $PDF::API2::UniWrap::columns
Truth table0%0/3
A ||= B
A= $self->{"line_length"}B= $PDF::API2::UniWrap::columns
ABresult
1X1
011
000
70 0 ▶ $self->{break_table} ||= \%BREAK_TABLE;
Condition0%0/2
$self->{"break_table"} ||= \%BREAK_TABLE
l!l
00
MC/DC0%0/1
$self->{"break_table"} ||= \%BREAK_TABLE
$self->{"break_table"}
Truth table0%0/2
A
A= $self->{"break_table"}
Aresult
11
00
71  
72 0 ▶ $self->{widthfunc} ||= 1;
Condition0%0/2
$self->{"widthfunc"} ||= 1
l!l
00
MC/DC0%0/1
$self->{"widthfunc"} ||= 1
$self->{"widthfunc"}
Truth table0%0/2
A
A= $self->{"widthfunc"}
Aresult
11
00
73  
74 0 ▶ bless($self, ref($pkg) || $pkg);
Condition0%0/3
ref $pkg || $pkg
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $pkg || $pkg
ref $pkg $pkg
Truth table0%0/3
A || B
A= ref $pkgB= $pkg
ABresult
1X1
011
000
75 }
76  
77  
78 # This attempts to identify the on-screen length of a given character.
79 # For normal displays, you can generally assume the character has a
80 # length of 1, but some terminals may expand the width of certain
81 # characters, so that extra space needs to be taken into consideration
82 # here so the wrapping occurs at the proper place.
83  
84 0 ▶sub char_length {
Subroutine char_length: not called CC=4 SCAR=30.0
Pod undocumented
85 0 ▶ shift if ref($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] 0 0 0
86 0 my ($c) = @_;
87  
88 0 ▶ if ($c eq 'CM' || $c eq 'ZW') {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "CM" or $c eq "ZW") 0 0 0
Condition0%0/3
$c eq "CM" or $c eq "ZW"
l!l&&r!l&&!r
000
MC/DC0%0/2
$c eq "CM" or $c eq "ZW"
$c eq "CM" $c eq "ZW"
Truth table0%0/3
A or B
A= $c eq "CM"B= $c eq "ZW"
ABresult
1X1
011
000
89 0 return 0;
90 }
91  
92 0 return 1;
93 }
94  
95 0 ▶sub lb_class {
Subroutine lb_class: not called CC=13 SCAR=52.0
Pod undocumented
96 0 ▶ my $self = ref($_[0]) ? shift() : self();
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
97 0 my $code = Unicode::UCD::_getcode(ord $_[0]);
98 0 my $hex;
99  
100 0 ▶ if (defined $code) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $code) { } 0 0 0
101 0 $hex = sprintf "%04X", $code;
102 } else {
103 0 carp("unexpected arg \"$_[1]\" to Text::Wrap::lb_class()");
104 0 return;
105 }
106  
107 0 ▶ return $classified{$hex} if $classified{$hex};
Branch: 1 branch
BranchTrueFalseTotal
if $classified{$hex} 0 0 0
108  
109 0 ▶ $txt = do "unicore/Lbrk.pl" unless $txt;
Branch: 1 branch
BranchTrueFalseTotal
unless $txt 0 0 0
110  
111 0 ▶ if ($txt =~ m/^$hex\t\t(.+)/m) {
Branch: 1 branch
BranchTrueFalseTotal
if ($txt =~ /^$hex\t\t(.+)/m) { } 0 0 0
112 0 ▶ print STDERR "< found direct match for $hex = $1 >\n" if $DEBUG > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $PDF::API2::UniWrap::DEBUG > 1 0 0 0
113 0 return $classified{$hex} = $1;
114 } else {
115 0 ▶ print STDERR "< no direct match $hex >\n" if $DEBUG > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $PDF::API2::UniWrap::DEBUG > 1 0 0 0
116 0 pos($txt) = 0;
117  
118 0 ▶ while ($txt =~ m/^([0-9A-F]+)\t([0-9A-F]+)\t(.+)/mg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($txt =~ /^([0-9A-F]+)\t([0-9A-F]+)\t(.+)/gm) 0 0 0
119 0 ▶ print STDERR "< examining $1 -> $2 >\n" if $DEBUG > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $PDF::API2::UniWrap::DEBUG > 1 0 0 0
120 0 ▶ if (hex($1) <= $code && hex($2) >= $code) {
Branch: 1 branch
BranchTrueFalseTotal
if (hex $1 <= $code and hex $2 >= $code) 0 0 0
Condition0%0/3
hex $1 <= $code and hex $2 >= $code
!ll&&!rl&&r
000
MC/DC0%0/2
hex $1 <= $code and hex $2 >= $code
hex $1 <= $code hex $2 >= $code
Truth table0%0/3
A and B
A= hex $1 <= $codeB= hex $2 >= $code
ABresult
0X0
100
111
121 0 ▶ print STDERR "< found range match for $hex = $3 between $1 and $2 >\n" if $DEBUG > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $PDF::API2::UniWrap::DEBUG > 1 0 0 0
122 0 return $classified{$hex} = $3;
123 }
124 }
125 0 return 'XX';
126 }
127 }
128  
129 # Returns a list of breaking properties for the given text
130 0 ▶sub text_properties {
Subroutine text_properties: not called CC=2 SCAR=17.9
Pod undocumented
131 0 ▶ my $self = ref($_[0]) ? shift() : self();
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
132 0 my ($text) = @_;
133  
134 0 my @characters = split(//, $text);
135 0 my @classifications = map { $self->lb_class($_) } @characters;
136  
137 0 class_properties(@classifications);
138 }
139  
140 # Returns a list of breaking properties for the provided breaking classes
141 0 ▶sub class_properties {
Subroutine class_properties: not called CC=24 SCAR=62.9
Pod undocumented
142 0 ▶ my $self = ref($_[0]) ? shift() : self();
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
143 1 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
144  
145 0 my @breaks;
146 0 my $last_class = $_[0];
147  
148 0 ▶ $last_class = 'ID' if $last_class eq 'CM'; # broken combining mark
Branch: 1 branch
BranchTrueFalseTotal
if $last_class eq "CM" 0 0 0
149  
150 0 ▶ print STDERR "find_breaks: first class=$last_class\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $PDF::API2::UniWrap::DEBUG 0 0 0
151  
152 0 ▶ for (my $i = 1; $i <= $#_; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#_) 0 0 0
153 0 ▶ print STDERR "find_breaks: i=$i class=$_[$i] prev=$last_class breaks[i-1]=$breaks[$i-1]\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $PDF::API2::UniWrap::DEBUG 0 0 0
154 0 ▶ $breaks[$i-1] ||= 0;
Condition0%0/2
$breaks[$i - 1] ||= 0
l!l
00
MC/DC0%0/1
$breaks[$i - 1] ||= 0
$breaks[$i - 1]
Truth table0%0/2
A
A= $breaks[$i - 1]
Aresult
11
00
155  
156 0 ▶ $_[$i] = 'ID' if $_[$i] eq 'XX'; # we want as few of these as possible!
Branch: 1 branch
BranchTrueFalseTotal
if $_[$i] eq "XX" 0 0 0
157  
158 0 ▶ if ($_[$i] eq 'SA') {
Branch: 7 branches
BranchTrueFalseTotal
if ($_[$i] eq 'SA') { } 0 0 0
elsif ($_[$i] eq 'CR') { } 0 0 0
elsif ($_[$i] eq 'LF') { } 0 0 0
elsif ($_[$i] eq 'BK') { } 0 0 0
elsif ($_[$i] eq 'SP') { } 0 0 0
elsif ($_[$i] eq 'CM') { } 0 0 0
elsif ($last_class ne 'SP') { } 0 0 0
159 # TODO: Need a classifiation system for complex characters
160 }
161  
162 elsif ($_[$i] eq 'CR') {
163 0 $breaks[$i] = REQUIRED;
164 }
165  
166 elsif ($_[$i] eq 'LF') {
167 0 ▶ if ($_[$i-1] eq 'CR') {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[$i - 1] eq "CR") 0 0 0
168 0 $breaks[$i-1] = PROHIBITED;
169 }
170 0 $breaks[$i] = REQUIRED;
171 }
172  
173 elsif ($_[$i] eq 'BK') {
174 0 $breaks[$i] = REQUIRED;
175 }
176  
177 elsif ($_[$i] eq 'SP') {
178 0 $breaks[$i-1] = PROHIBITED;
179 0 next;
180 }
181  
182 elsif ($_[$i] eq 'CM') {
183 0 ▶ if ($_[$i-1] eq 'SP') {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[$i - 1] eq "SP") 0 0 0
184 0 $last_class = 'ID';
185 0 ▶ if ($i > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i > 1) 0 0 0
186 $breaks[$i-2] = $self->{break_table}->{$_[$i-2]}->{ID} ==
187 0 ▶ DIRECT ? DIRECT : PROHIBITED;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'break_table'}{$_[$i - 2]}{'ID'} == 2 ? : 0 0 0
188 }
189 }
190 }
191  
192 elsif ($last_class ne 'SP') {
193 0 ▶ if ($breaks[$i-1] != REQUIRED) {
Branch: 1 branch
BranchTrueFalseTotal
if ($breaks[$i - 1] != 3) 0 0 0
194 0 my $this_break = $self->{break_table}->{$last_class}->{$_[$i]};
195  
196 0 ▶ if ($this_break == INDIRECT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this_break == 1) { } 0 0 0
197 0 ▶ $breaks[$i-1] = $_[$i-1] eq 'SP' ? INDIRECT : PROHIBITED;
Branch: 1 branch
BranchTrueFalseTotal
$_[$i - 1] eq 'SP' ? : 0 0 0
198 } else {
199 # die "internal error: no table mapping between '$last_class' and '$_[$i]'\n"
200 # unless defined $this_break;
201 0 ▶ if(defined $this_break)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $this_break) { } 0 0 0
202 {
203 0 $breaks[$i-1] = $this_break;
204 }
205 else
206 {
207 0 $breaks[$i-1] = DIRECT;
208 }
209 }
210 }
211 }
212  
213 0 $last_class = $_[$i];
214 }
215  
216 # $breaks[$#breaks] = DIRECT;
217 0 push(@breaks, REQUIRED);
218  
219 0 ▶ print STDERR "find_breaks: returning " . join(":", @breaks) . "\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $PDF::API2::UniWrap::DEBUG 0 0 0
220 0 return @breaks;
221 }
222  
223 # Returns a list of break points in the provided text, based on
224 # the line length
225 0 ▶sub find_breaks {
Subroutine find_breaks: not called CC=20 SCAR=59.4
Pod undocumented
226 0 ▶ my $self = ref($_[0]) ? shift() : self();
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
227 0 my $text = shift;
228  
229 1 ▶ no warnings 'uninitialized'; # since we do a lot of subscript +/- 1 checks
Subroutine BEGIN: called CC=1 SCAR=0.0
230  
231 0 my @characters = split //, $text;
232  
233 0 my @classifications = map { $self->lb_class($_) } @characters;
234 0 my @lengths = map { $self->char_length($_) } @characters;
235  
236 0 my @breaks = $self->class_properties(@classifications);
237 0 my @breakpoints;
238  
239 0 my $last_start = 0;
240 0 my $last_break;
241 my $last_length;
242 0 my $pos = 0;
243  
244 0 ▶ for (my $i = 0; $i <= $#lengths; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#lengths) 0 0 0
245  
246 0 ▶ print STDERR "[i=$i '$characters[$i]' $classifications[$i] $breaks[$i]] " if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $PDF::API2::UniWrap::DEBUG 0 0 0
247 0 ▶ if ($breaks[$i] == REQUIRED) {
Branch: 1 branch
BranchTrueFalseTotal
if ($breaks[$i] == 3) 0 0 0
248 0 ▶ print STDERR "required breakpoint\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $PDF::API2::UniWrap::DEBUG 0 0 0
249 0 push(@breakpoints, $i+1);
250 0 $last_start = $i+1;
251 0 $pos = 0;
252 0 next;
253 }
254  
255 0 my $c = $pos + $lengths[$i];
256  
257 0 ▶ if ($c > $self->{line_length}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c > $self->{"line_length"}) 0 0 0
258 0 ▶ print STDERR "want to break " if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $PDF::API2::UniWrap::DEBUG 0 0 0
259 0 ▶ if (defined $last_break) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $last_break) { } 0 0 0
elsif (defined $self->{'emergency_break'} and $c > $self->{'emergency_break'}) { } 0 0 0
260 0 ▶ print STDERR "at $last_break\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $PDF::API2::UniWrap::DEBUG 0 0 0
261 0 push(@breakpoints, $last_break + 1);
262 0 $last_start = $last_break + 1;
263 0 undef $last_break;
264 0 $pos -= $last_length - 1;
265 0 ▶ print STDERR "[pos now $pos]\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $PDF::API2::UniWrap::DEBUG 0 0 0
266 0 next;
267 ▶ } elsif (defined $self->{emergency_break} && $c > $self->{emergency_break}) {
Condition0%0/3
defined $self->{"emergency_break"} and $c > $self->{"emergency_break"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"emergency_break"} and $c > $self->{"emergency_break"}
defined $self->{"emergency_break"} $c > $self->{"emergency_break"}
Truth table0%0/3
A and B
A= defined $self->{"emergency_break"}B= $c > $self->{"emergency_break"}
ABresult
0X0
100
111
268 0 ▶ print STDERR "NOW\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $PDF::API2::UniWrap::DEBUG 0 0 0
269 0 push(@breakpoints, $i+1);
270 0 $pos = 0;
271 } else {
272 0 ▶ print STDERR "but can't" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $PDF::API2::UniWrap::DEBUG 0 0 0
273 }
274 }
275 0 ▶ print STDERR "\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $PDF::API2::UniWrap::DEBUG 0 0 0
276  
277 0 ▶ $last_break = $i if $breaks[$i];
Branch: 1 branch
BranchTrueFalseTotal
if $breaks[$i] 0 0 0
278 0 ▶ $last_length = $pos if $breaks[$i];
Branch: 1 branch
BranchTrueFalseTotal
if $breaks[$i] 0 0 0
279  
280 0 $pos += $lengths[$i];
281 }
282  
283 0 ▶ push(@breakpoints, $#lengths) if $breakpoints[$#breakpoints] < $#lengths;
Branch: 1 branch
BranchTrueFalseTotal
if $breakpoints[$#breakpoints] < $#lengths 0 0 0
284  
285 0 ▶ print STDERR "find_breaks: returning breakpoints " . join(":", @breakpoints) . "\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $PDF::API2::UniWrap::DEBUG 0 0 0
286  
287 0 return @breakpoints;
288 }
289  
290 # Returns a list of lines, broken up with find_breaks
291 0 ▶sub break_lines {
Subroutine break_lines: not called CC=3 SCAR=24.8
Pod undocumented
292 0 ▶ my $self = ref($_[0]) ? shift() : self();
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
293 0 my $text = shift;
294  
295 0 my @breaks = $self->find_breaks($text);
296 0 my @lines;
297  
298 0 my $last = 0;
299 0 foreach (@breaks) {
300 0 push(@lines, substr($text, $last, $_-$last));
301 0 $last = $_;
302 }
303  
304 0 return @lines;
305 }
306  
307 1 1;
308  
309 __END__
« API2.pm ↑ Summary API2/Basic/PDF/Filter/FlateDecode.pm »