blib/lib/Imager/Font/Wrap.pm

filter: Statementstmt 12.7% 14 / 110 Branchbran 0.0% 0 / 62 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 66.6% 4 / 6 Podpod 100% 1 / 1 totaltotal 9.2% 19 / 205 CC 38 SCAR 61.7
CC
38
Cov
7%
wrap_text
702.0
_format_line
182.0
BEGIN
1.0
CRAP 479.6
↑ Summary blib/lib/Imager/Probe.pm »
1 package Imager::Font::Wrap;
2 3 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Imager;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Imager::Font;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 our $VERSION = "1.005";
8  
9 3 *_first = \&Imager::Font::_first;
10  
11 # we can't accept the utf8 parameter, too hard at this level
12  
13 # the %state contains:
14 # font - the font
15 # im - the image
16 # x - the left position
17 # w - the width
18 # justify - fill, left, right or center
19  
20 0 ▶sub _format_line {
Subroutine _format_line: not called CC=13 SCAR=52.0
21 0 my ($state, $spaces, $text, $fill) = @_;
22  
23 0 $text =~ s/ +$//;
24 my $box = $state->{font}->bounding_box(string=>$text,
25 0 size=>$state->{size});
26  
27 0 my $y = $state->{linepos} + $box->global_ascent;
28
29 0 ▶ if ($state->{bottom}
Branch: 1 branch
BranchTrueFalseTotal
if ($state->{"bottom"} and $state->{"linepos"} + $box->font_height > $state->{"bottom"}) 0 0 0
Condition0%0/3
$state->{"bottom"} and $state->{"linepos"} + $box->font_height > $state->{"bottom"}
!ll&&!rl&&r
000
MC/DC0%0/2
$state->{"bottom"} and $state->{"linepos"} + $box->font_height > $state->{"bottom"}
$state->{"bottom"} $state->{"linepos"} + $box->font_height > $state->{"bottom"}
Truth table0%0/3
A and B
A= $state->{"bottom"}B= $state->{"linepos"} + $box->font_height > $state->{"bottom"}
ABresult
0X0
100
111
30 && $state->{linepos} + $box->font_height > $state->{bottom}) {
31 0 $state->{full} = 1;
32 0 return 0;
33 }
34  
35 0 ▶ if ($text =~ /\S/ && $state->{im}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /\S/ and $state->{"im"}) 0 0 0
Condition0%0/3
$text =~ /\S/ and $state->{"im"}
!ll&&!rl&&r
000
MC/DC0%0/2
$text =~ /\S/ and $state->{"im"}
$text =~ /\S/ $state->{"im"}
Truth table0%0/3
A and B
A= $text =~ /\S/B= $state->{"im"}
ABresult
0X0
100
111
36 my $justify = $fill ? $state->{justify} :
37 0 ▶ $state->{justify} eq 'fill' ? 'left' : $state->{justify};
Branch: 2 branches
BranchTrueFalseTotal
$fill ? : 0 0 0
$state->{'justify'} eq 'fill' ? : 0 0 0
38 0 ▶ if ($justify ne 'fill') {
Branch: 1 branch
BranchTrueFalseTotal
if ($justify ne 'fill') { } 0 0 0
39 0 my $x = $state->{x};
40 0 ▶ if ($justify eq 'right') {
Branch: 2 branches
BranchTrueFalseTotal
if ($justify eq 'right') { } 0 0 0
elsif ($justify eq 'center') { } 0 0 0
41 0 $x += $state->{w} - $box->advance_width;
42 }
43 elsif ($justify eq 'center') {
44 0 $x += ($state->{w} - $box->advance_width) / 2;
45 }
46 $state->{font}->draw(image=>$state->{im}, string=>$text,
47 x=>$x, 'y'=>$y,
48 0 size=>$state->{size}, %{$state->{input}});
49 }
50 else {
51 0 (my $nospaces = $text) =~ tr/ //d;
52 my $nospace_bbox = $state->{font}->bounding_box(string=>$nospaces,
53 0 size=>$state->{size});
54 0 my $gap = $state->{w} - $nospace_bbox->advance_width;
55 0 my $x = $state->{x};
56 0 $spaces = $text =~ tr/ / /;
57 0 ▶ while (length $text) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $text) 0 0 0
58 0 ▶ if ($text =~ s/^(\S+)//) {
Branch: 2 branches
BranchTrueFalseTotal
if ($text =~ s/^(\S+)//) { } 0 0 0
elsif ($text =~ s/^( +)//) { } 0 0 0
59 0 my $word = $1;
60 my $bbox = $state->{font}->bounding_box(string=>$word,
61 0 size=>$state->{size});
62 $state->{font}->draw(image=>$state->{im}, string=>$1,
63 x=>$x, 'y'=>$y,
64 0 size=>$state->{size}, %{$state->{input}});
65 0 $x += $bbox->advance_width;
66 }
67 elsif ($text =~ s/^( +)//) {
68 0 my $sep = $1;
69 0 my $advance = int($gap * length($sep) / $spaces);
70 0 $spaces -= length $sep;
71 0 $gap -= $advance;
72 0 $x += $advance;
73 }
74 else {
75 0 die "This shouldn't happen\n";
76 }
77 }
78 }
79 }
80 0 $state->{linepos} += $box->font_height + $state->{linegap};
81  
82 0 1;
83 }
84  
85 0 ▶sub wrap_text {
Subroutine wrap_text: not called CC=26 SCAR=65.5
Pod documented
86 0 my $class = shift;
87 0 my %input = @_;
88  
89 # try to get something useful
90 0 my $x = _first(delete $input{'x'}, 0);
91 0 my $y = _first(delete $input{'y'}, 0);
92 0 my $im = delete $input{image};
93 0 ▶ my $imerr = $im || 'Imager';
Condition0%0/2
$im || 'Imager'
l!l
00
MC/DC0%0/1
$im || 'Imager'
$im
Truth table0%0/2
A
A= $im
Aresult
11
00
94 0 my $width = delete $input{width};
95 0 ▶ if (!defined $width) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $width) 0 0 0
96 0 ▶ defined $im && $im->getwidth > $x
Branch: 1 branch
BranchTrueFalseTotal
defined $im and $im->getwidth > $x or return $imerr->_set_error("No width supplied and can't guess") 0 0 0
Condition0%0/3
defined $im and $im->getwidth > $x
!ll&&!rl&&r
000
MC/DC0%0/2
defined $im and $im->getwidth > $x
defined $im $im->getwidth > $x
Truth table0%0/3
A and B
A= defined $imB= $im->getwidth > $x
ABresult
0X0
100
111
97 or return $imerr->_set_error("No width supplied and can't guess");
98 0 $width = $im->getwidth - $x;
99 }
100 my $font = delete $input{font}
101 0 ▶ or return $imerr->_set_error("No font parameter supplied");
Branch: 1 branch
BranchTrueFalseTotal
my $font = delete $input{"font"} or return $imerr->_set_error("No font parameter supplied") 0 0 0
102 0 my $size = _first(delete $input{size}, $font->{size});
103 0 ▶ defined $size
Branch: 1 branch
BranchTrueFalseTotal
defined $size or return $imerr->_set_error("No font size supplied") 0 0 0
104 or return $imerr->_set_error("No font size supplied");
105  
106 0 ▶ 2 * $size < $width
Branch: 1 branch
BranchTrueFalseTotal
2 * $size < $width or return $imerr->_set_error("Width too small for font size") 0 0 0
107 or return $imerr->_set_error("Width too small for font size");
108
109 0 my $text = delete $input{string};
110 0 ▶ defined $text
Branch: 1 branch
BranchTrueFalseTotal
defined $text or return $imerr->_set_error("No string parameter supplied") 0 0 0
111 or return $imerr->_set_error("No string parameter supplied");
112  
113 0 my $justify = _first($input{justify}, "left");
114  
115 my %state =
116 (
117 font => $font,
118 im => $im,
119 x => $x,
120 w => $width,
121 justify => $justify,
122 'y' => $y,
123 linepos=>$y,
124 size=>$size,
125 input => \%input,
126 0 ▶ linegap => delete $input{linegap} || 0,
Condition0%0/2
delete $input{'linegap'} || 0
l!l
00
MC/DC0%0/1
delete $input{'linegap'} || 0
delete $input{'linegap'}
Truth table0%0/2
A
A= delete $input{'linegap'}
Aresult
11
00
127 );
128 0 $state{height} = delete $input{height};
129 0 ▶ if ($state{height}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state{"height"}) 0 0 0
130 0 $state{bottom} = $y + $state{height};
131 }
132 0 my $line = '';
133 0 my $spaces = 0;
134 0 my $charpos = 0;
135 0 my $linepos = 0;
136 0 pos($text) = 0; # avoid a warning
137 0 ▶ while (pos($text) < length($text)) {
Branch: 1 branch
BranchTrueFalseTotal
if (pos $text < length $text) 0 0 0
138 #print pos($text), "\n";
139 0 ▶ if ($text =~ /\G( +)/gc) {
Branch: 4 branches
BranchTrueFalseTotal
if ($text =~ /\G( +)/cg) { } 0 0 0
elsif ($text =~ /\G(?:\x0D\x0A?|\x0A\x0D?)/cg) { } 0 0 0
elsif ($text =~ /\G(\S+)/cg) { } 0 0 0
elsif ($text =~ /\G\s/cg) { } 0 0 0
140 #print "spaces\n";
141 0 $line .= $1;
142 0 $spaces += length($1);
143 }
144 elsif ($text =~ /\G(?:\x0D\x0A?|\x0A\x0D?)/gc) {
145 #print "newline\n";
146 0 ▶ _format_line(\%state, $spaces, $line, 0)
Branch: 1 branch
BranchTrueFalseTotal
_format_line(\%state, $spaces, $line, 0) or last 0 0 0
147 or last;
148 0 $line = '';
149 0 $spaces = 0;
150 0 $linepos = pos($text);
151 }
152 elsif ($text =~ /\G(\S+)/gc) {
153 #print "word\n";
154 0 my $word = $1;
155 0 my $bbox = $font->bounding_box(string=>$line . $word, size=>$size);
156 0 ▶ if ($bbox->advance_width > $width) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bbox->advance_width > $width) 0 0 0
157 0 ▶ _format_line(\%state, $spaces, $line, 1)
Branch: 1 branch
BranchTrueFalseTotal
_format_line(\%state, $spaces, $line, 1) or last 0 0 0
158 or last;
159 0 $line = '';
160 0 $spaces = 0;
161 0 $linepos = pos($text) - length($word);
162 }
163 0 $line .= $word;
164 # check for long words
165 0 $bbox = $font->bounding_box(string=>$line, size=>$size);
166 0 ▶ while ($bbox->advance_width > $width) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bbox->advance_width > $width) 0 0 0
167 0 my $len = length($line) - 1;
168 0 $bbox = $font->bounding_box(string=>substr($line, 0, $len),
169 size=>$size);
170 0 ▶ while ($bbox->advance_width > $width) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bbox->advance_width > $width) 0 0 0
171 0 --$len;
172 0 $bbox = $font->bounding_box(string=>substr($line, 0, $len),
173 size=>$size);
174 }
175 0 ▶ _format_line(\%state, 0, substr($line, 0, $len), 0)
Branch: 1 branch
BranchTrueFalseTotal
_format_line(\%state, 0, substr($line, 0, $len), 0) or last 0 0 0
176 or last;
177 0 $line = substr($line, $len);
178 0 $bbox = $font->bounding_box(string=>$line, size=>$size);
179 0 $linepos = pos($text) - length($line);
180 }
181 }
182 elsif ($text =~ /\G\s/gc) {
183 # skip a single unrecognized whitespace char
184 #print "skip\n";
185 0 $linepos = pos($text);
186 }
187 }
188  
189 0 ▶ if (length $line && !$state{full}) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $line and not $state{"full"}) 0 0 0
Condition0%0/3
length $line and not $state{"full"}
!ll&&!rl&&r
000
MC/DC0%0/2
length $line and not $state{"full"}
length $line not $state{"full"}
Truth table0%0/3
A and not(B)
A= length $lineB= not $state{"full"}
ABresult
0X0
100
111
190 0 ▶ $linepos += length $line
Branch: 1 branch
BranchTrueFalseTotal
if _format_line(\%state, 0, $line, 0) 0 0 0
191 if _format_line(\%state, 0, $line, 0);
192 }
193  
194 0 ▶ if ($input{savepos}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($input{"savepos"}) 0 0 0
195 0 ${$input{savepos}} = $linepos;
196 }
197  
198 0 return ($x, $y, $x+$width, $state{linepos});
199 }
200  
201 3 1;
202  
203 __END__
↑ Summary blib/lib/Imager/Probe.pm »