Builder/UniWrap.pm

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