Text.pm

filter: Statementstmt 95.5% 149 / 156 Branchbran 87.1% 61 / 70 Conditioncond 78.7% 26 / 33 MC/DCmcdc 64.7% 11 / 17 Subroutinesub 100% 23 / 23 Podpod 50.0% 7 / 14 totaltotal 88.5% 277 / 313 CC 52 SCAR 18.4
CC
52
Cov
91%
rectangle
15.0
line
7.0
set
6.0
CRAP 6.3
« Multiplex.pm ↑ Summary
1 # Copyright 2010, 2011, 2012 Kevin Ryde
2  
3 # This file is part of Image-Base-Other.
4 #
5 # Image-Base-Other is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by the
7 # Free Software Foundation; either version 3, or (at your option) any later
8 # version.
9 #
10 # Image-Base-Other is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 # for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with Image-Base-Other. If not, see <http://www.gnu.org/licenses/>.
17  
18  
19 package Image::Base::Text;
20 # maybe one day 5.005 for 4-arg substr() replacing
21 # 5.6 for easier clean open()
22 4 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 4 ▶use Text::Tabs ();
Subroutine BEGIN: called CC=1 SCAR=0.0
26 4 ▶use List::Util 'min','max';
Subroutine BEGIN: called CC=1 SCAR=0.0
27 4 ▶use vars '$VERSION', '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 4 $VERSION = 9;
30  
31 4 ▶use Image::Base 1.12; # version 1.12 for ellipse() $fill
Subroutine BEGIN: called CC=1 SCAR=0.0
32 4 @ISA = ('Image::Base');
33  
34 # uncomment this to run the ### lines
35 #use Smart::Comments;
36  
37  
38 4 ▶use constant default_colour_to_character => { 'black' => ' ',
Subroutine BEGIN: called CC=1 SCAR=0.0
39 'clear' => ' ',
40 '#000000' => ' ',
41 '#000000000000' => ' ',
42 other => '*',
43 4 };
44  
45 53 ▶sub new {
Subroutine new: called CC=3 SCAR=12.0
Pod documented
46 53 my ($class, %param) = @_;
47  
48 53 ▶ if (ref $class) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $class) 0 53 53
49 # clone by copying fields and data array
50 0 my $self = bless { %$class }, $class;
51 0 $self->{'-rows_array'} = [ @{$class->{'-rows_array'}} ];
52 0 return $self;
53 }
54  
55 53 my $self = bless
56 { -rows_array => [],
57 -width => 0,
58 -colour_to_character => $class->default_colour_to_character,
59 }, $class;
60  
61 53 ▶ if (defined (my $filename = delete $param{'-file'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $filename = delete $param{"-file"})) 2 51 53
62 2 $self->load($filename);
63 }
64 53 $self->set (%param);
65 53 return $self;
66 }
67  
68 1996 ▶sub _get {
Subroutine _get: called CC=2 SCAR=6.9
69 1996 my ($self, $key) = @_;
70 # ### Image-Base-Text _get(): $key
71  
72 1996 ▶ if ($key eq '-height') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "-height") 936 1060 1996
73 936 return scalar @{$self->{'-rows_array'}};
74 }
75 1060 return $self->SUPER::_get ($key);
76 }
77  
78 81 ▶sub set {
Subroutine set: called CC=6 SCAR=17.9
Pod documented
79 81 my ($self, %param) = @_;
80 ### set(): \%param
81  
82 81 ▶ if (defined (my $width = delete $param{'-width'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $width = delete $param{"-width"})) 56 25 81
83 56 foreach my $row (@{$self->{'-rows_array'}}) {
84 60 ▶ if (length($row) < $width) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $row < $width) { } 20 40 60
85 20 $row .= ' ' x ($width - length($row));
86 } else {
87 40 substr($row,$width) = '';
88 }
89 }
90 # ready for -height to use
91 56 $self->{'-width'} = $width;
92 }
93  
94 81 ▶ if (defined (my $height = delete $param{'-height'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $height = delete $param{"-height"})) 41 40 81
95 41 my $rows_array = $self->{'-rows_array'};
96 41 ▶ if (@$rows_array >= $height) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$rows_array >= $height) { } 4 37 41
97 ### rows_array shorten
98 4 splice @$rows_array, $height;
99 } else {
100 ### rows_array extend by: ($height - scalar(@$rows_array))
101 37 my $row = ' ' x $self->{'-width'};
102 37 push @$rows_array, ($row) x ($height - scalar(@$rows_array));
103 }
104 }
105  
106 81 %$self = (%$self, %param);
107 }
108  
109 3 ▶sub load {
Subroutine load: called CC=4 SCAR=15.0
Pod documented
110 3 my ($self, $filename) = @_;
111 ### Image-Base-Text load()
112 3 ▶ if (@_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 0 3 3
113 0 $filename = $self->get('-file');
114 } else {
115 3 $self->set('-file', $filename);
116 }
117 ### $filename
118  
119 3 ▶ open my $fh, '<', $filename or croak "Cannot open $filename: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $filename or croak("Cannot open ${filename}: $!") 3 0 3
120 3 $self->load_fh ($fh);
121 3 ▶ close $fh or croak "Error closing $filename: $!";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or croak("Error closing ${filename}: $!") 3 0 3
122 }
123  
124 # these undocumented yet ...
125 3 ▶sub load_fh {
Subroutine load_fh: called CC=1 SCAR=0.0
Pod undocumented
126 3 my ($self, $fh) = @_;
127 ### Image-Base-Text load_fh(): $fh
128 3 $self->load_lines (map {chomp; $_} <$fh>);
129 }
130 12 ▶sub load_string {
Subroutine load_string: called CC=3 SCAR=11.0
Pod undocumented
131 12 my ($self, $str) = @_;
132 ### Image-Base-Text load_string(): $str
133 # split
134 12 my @lines = split /\n/, $str, -1;
135 12 ▶ if (@lines && $lines[-1] eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (@lines and $lines[-1] eq "") 9 3 12
Condition100%3/3
@lines and $lines[-1] eq ""
!ll&&!rl&&r
129
MC/DC100%2/2
@lines and $lines[-1] eq ""
@lines $lines[-1] eq ""
Truth table100%3/3
A and B
A= @linesB= $lines[-1] eq ""
ABresult
0X0
100
111
136 # drop the empty element after the last newline, but keep a non-empty
137 # final element from chars without a final newline
138 9 pop @lines;
139 }
140 12 $self->load_lines (@lines);
141 }
142 16 ▶sub load_lines {
Subroutine load_lines: called CC=3 SCAR=11.0
Pod undocumented
143 16 my ($self, @rows_array) = @_;
144 ### load_lines: @rows_array
145  
146 16 my $width = 0;
147 16 foreach my $row (@rows_array) {
148 29 $row = Text::Tabs::expand ($row);
149 29 ▶ if ($width < length($row)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($width < length $row) 16 13 29
150 16 $width = length($row);
151 }
152 }
153  
154 16 $self->{'-rows_array'} = \@rows_array;
155 16 $self->set (-width => $width); # pad out shorter lines
156 }
157  
158 2 ▶sub save {
Subroutine save: called CC=5 SCAR=17.5
Pod documented
159 2 my ($self, $filename) = @_;
160 ### Image-Base-Text save(): @_
161 2 ▶ if (@_ == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) { } 1 1 2
162 1 $self->set('-file', $filename);
163 } else {
164 1 $filename = $self->get('-file');
165 }
166 ### $filename
167  
168 2 my $fh;
169 2 ▶ (open $fh, '>', $filename
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">", $filename and $self->save_fh($fh) and close $fh or croak("Error writing ${filename}: $!") 2 0 2
Condition33%2/6
open $fh, ">", $filename and $self->save_fh($fh) and close $fh
!ll&&!rl&&r
002
open $fh, ">", $filename and $self->save_fh($fh)
!ll&&!rl&&r
002
MC/DC0%0/3
open $fh, ">", $filename and $self->save_fh($fh) and close $fh
open $fh, ">", $filename $self->save_fh($fh) close $fh
Truth table25%1/4
A and B and C
A= open $fh, ">", $filenameB= $self->save_fh($fh)C= close $fh
ABCresult
0XX0
10X0
1100
1111
170 and $self->save_fh($fh)
171 and close $fh)
172 or croak "Error writing $filename: $!";
173 }
174  
175 # these undocumented yet ...
176 2 ▶sub save_fh {
Subroutine save_fh: called CC=2 SCAR=7.0
Pod undocumented
177 2 my ($self, $fh) = @_;
178 2 my $rows_array = $self->{'-rows_array'};
179 2 local $, = "\n";
180 2 ▶ return print $fh @$rows_array,(@$rows_array ? '' : ());
Branch: 1 branch
BranchTrueFalseTotal
@$rows_array ? : 2 0 2
181 }
182 17 ▶sub save_string {
Subroutine save_string: called CC=2 SCAR=7.1
Pod undocumented
183 17 my ($self) = @_;
184 17 my $rows_array = $self->{'-rows_array'};
185 17 ▶ return join ("\n", @$rows_array, (@$rows_array ? '' : ()));
Branch: 1 branch
BranchTrueFalseTotal
@$rows_array ? : 17 0 17
186 }
187  
188 #------------------------------------------------------------------------------
189 # drawing
190  
191 5320 ▶sub xy {
Subroutine xy: called CC=6 SCAR=17.9
Pod documented
192 5320 my ($self, $x, $y, $colour) = @_;
193 ### Image-Base-Text xy(): @_[1 .. $#_]
194  
195 # clip to width,height
196 return if ($x < 0 || $x >= $self->{'-width'}
197 5320 ▶ || $y < 0 || $y >= @{$self->{'-rows_array'}});
Branch: 1 branch
BranchTrueFalseTotal
if $x < 0 or $x >= $self->{"-width"} or $y < 0 or $y >= @{$self->{"-rows_array"};} 25 5295 5320
Condition100%9/9
$x < 0 or $x >= $self->{"-width"} or $y < 0 or $y >= @{$self->{"-rows_array"};}
l!l&&r!l&&!r
2145295
$x < 0 or $x >= $self->{"-width"} or $y < 0
l!l&&r!l&&!r
1745299
$x < 0 or $x >= $self->{"-width"}
l!l&&r!l&&!r
985303
MC/DC100%4/4
$x < 0 or $x >= $self->{"-width"} or $y < 0 or $y >= @{$self->{"-rows_array"};}
$x < 0 $x >= $self->{"-width"} $y < 0 $y >= @{$self->{"-rows_array"};}
Truth table100%5/5
A or B or C or D
A= $x < 0B= $x >= $self->{"-width"}C= $y < 0D= $y >= @{$self->{"-rows_array"};}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
198  
199 5295 my $rows_array = $self->{'-rows_array'};
200 5295 ▶ if (@_ == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 3) { } 5023 272 5295
201 5023 return $self->character_to_colour (substr ($rows_array->[$y], $x, 1));
202 } else {
203 272 substr ($rows_array->[$y], $x, 1) = $self->colour_to_character($colour);
204 }
205 }
206  
207 79 ▶sub line {
Subroutine line: called CC=7 SCAR=19.5
Pod documented
208 79 my ($self, $x1,$y1, $x2,$y2, $colour) = @_;
209  
210 79 ▶ if ($y1 == $y2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y1 == $y2) { } 48 31 79
211 ### horizontal line by substr() block store ...
212  
213 48 my $rows_array = $self->{'-rows_array'};
214 48 ▶ return if $y1 < 0 || $y1 > $#$rows_array; # entirely outside
Branch: 1 branch
BranchTrueFalseTotal
if $y1 < 0 or $y1 > $rows_array->$#* 4 44 48
Condition100%3/3
$y1 < 0 or $y1 > $rows_array->$#*
l!l&&r!l&&!r
2244
MC/DC100%2/2
$y1 < 0 or $y1 > $rows_array->$#*
$y1 < 0 $y1 > $rows_array->$#*
Truth table100%3/3
A or B
A= $y1 < 0B= $y1 > $rows_array->$#*
ABresult
1X1
011
000
215  
216 44 ▶ if ($x1 > $x2) { ($x1,$x2) = ($x2,$x1) } # x1 smaller
Branch: 1 branch
BranchTrueFalseTotal
if ($x1 > $x2) 9 35 44
217 44 my $xmax = $self->{'-width'}-1;
218 44 ▶ return if $x2 < 0 || $x1 > $xmax; # entirely outside
Branch: 1 branch
BranchTrueFalseTotal
if $x2 < 0 or $x1 > $xmax 2 42 44
Condition100%3/3
$x2 < 0 or $x1 > $xmax
l!l&&r!l&&!r
1142
MC/DC100%2/2
$x2 < 0 or $x1 > $xmax
$x2 < 0 $x1 > $xmax
Truth table100%3/3
A or B
A= $x2 < 0B= $x1 > $xmax
ABresult
1X1
011
000
219  
220 42 $x1 = max($x1,0);
221 42 $x2 = min($x2,$xmax);
222 42 my $x_width = $x2-$x1+1;
223 42 substr($rows_array->[$y1], $x1, $x_width,
224 $self->colour_to_character($colour) x $x_width);
225  
226 } else {
227 31 shift->SUPER::line(@_);
228 }
229 }
230  
231 # rectangle() can do a substr() block store on each filled row (either all
232 # if $fill, or the top and bottom if not).
233 #
234 369 ▶sub rectangle {
Subroutine rectangle: called CC=15 SCAR=27.1
Pod documented
235 369 my ($self, $x1,$y1, $x2,$y2, $colour, $fill) = @_;
236 ### Image-Base-Text xy(): @_[1,$#_]
237  
238 369 my $rows_array = $self->{'-rows_array'};
239  
240 369 ▶ unless ($x2 >= 0
Branch: 1 branch
BranchTrueFalseTotal
unless ($x2 >= 0 and $y2 >= 0 and $x1 < $self->{"-width"} and $y1 <= $rows_array->$#*) 6 363 369
Condition66%6/9
$x2 >= 0 and $y2 >= 0 and $x1 < $self->{"-width"} and $y1 <= $rows_array->$#*
!ll&&!rl&&r
60363
$x2 >= 0 and $y2 >= 0 and $x1 < $self->{"-width"}
!ll&&!rl&&r
60363
$x2 >= 0 and $y2 >= 0
!ll&&!rl&&r
60363
MC/DC25%1/4
$x2 >= 0 and $y2 >= 0 and $x1 < $self->{"-width"} and $y1 <= $rows_array->$#*
$x2 >= 0 $y2 >= 0 $x1 < $self->{"-width"} $y1 <= $rows_array->$#*
Truth table40%2/5
A and B and C and D
A= $x2 >= 0B= $y2 >= 0C= $x1 < $self->{"-width"}D= $y1 <= $rows_array->$#*
ABCDresult
0XXX0
10XX0
110X0
11100
11111
241 && $y2 >= 0
242 && $x1 < $self->{'-width'}
243 && $y1 <= $#$rows_array) {
244 ### entirely outside 0,0,width,height ...
245 6 return;
246 }
247  
248 363 my $x1_clip = max($x1,0);
249 363 my $x2_clip = min($x2,$self->{'-width'}-1);
250  
251 363 my $char = $self->colour_to_character($colour);
252 363 my $x_width = $x2_clip - $x1_clip + 1;
253 363 my $repl = $char x $x_width;
254  
255 363 ▶ if ($fill) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fill) { } 266 97 363
256 266 foreach my $y (max($y1,0) .. min($y2,$#$rows_array)) {
257 2129 substr ($rows_array->[$y], $x1_clip, $x_width) = $repl;
258 }
259  
260 } else {
261 ### top, if in range ...
262 97 ▶ if ($y1 >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y1 >= 0) 91 6 97
263 91 substr ($rows_array->[$y1], $x1_clip, $x_width) = $repl;
264 }
265  
266 97 $y1++;
267 97 ▶ if ($y2 >= $y1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y2 >= $y1) 53 44 97
268 53 ▶ if ($y2 <= $#$rows_array) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y2 <= $rows_array->$#*) 49 4 53
269 ### bottom in range and not same as top ...
270 49 substr ($rows_array->[$y2], $x1_clip, $x_width) = $repl;
271 }
272  
273 ### sides, if any ...
274 53 $y2--;
275 53 ▶ if ($y2 >= $y1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y2 >= $y1) 32 21 53
276 32 my $y1_clip = max($y1,0);
277 32 my $y2_clip = min($y2,$#$rows_array);
278  
279 32 ▶ if ($x1 == $x1_clip) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x1 == $x1_clip) 27 5 32
280 27 foreach my $y ($y1_clip .. $y2_clip) {
281 126 substr ($rows_array->[$y], $x1, 1) = $char;
282 }
283 }
284 32 ▶ if ($x2 == $x2_clip) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x2 == $x2_clip) 28 4 32
285 28 foreach my $y ($y1_clip .. $y2_clip) {
286 129 substr ($rows_array->[$y], $x2, 1) = $char;
287 }
288 }
289 }
290 }
291 }
292 }
293  
294 681 ▶sub colour_to_character {
Subroutine colour_to_character: called CC=4 SCAR=14.0
Pod undocumented
295 681 my ($self, $colour) = @_;
296 ### colour_to_character(): $colour
297 681 ▶ if (defined (my $char = $self->{'-colour_to_character'}->{$colour})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $char = $self->{"-colour_to_character"}{$colour})) 182 499 681
298 182 return $char;
299 }
300 499 ▶ if (length($colour) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $colour == 1) 69 430 499
301 69 return $colour;
302 }
303 430 ▶ if (defined (my $char = $self->{'-colour_to_character'}->{'other'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $char = $self->{"-colour_to_character"}{"other"})) 430 0 430
304 430 return $char;
305 }
306 0 croak "Unknown colour: $colour";
307 }
308 5023 ▶sub character_to_colour {
Subroutine character_to_colour: called CC=3 SCAR=11.2
Pod undocumented
309 5023 my ($self, $char) = @_;
310 5023 ▶ if (length ($char) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $char == 0) 0 5023 5023
311 0 return undef;
312 }
313 5023 ▶ if (defined (my $colour = $self->{'-character_to_colour'}->{$char})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $colour = $self->{"-character_to_colour"}{$char})) 4988 35 5023
314 4988 return $colour;
315 }
316 35 return $char;
317 }
318  
319 4 1;
320 __END__
« Multiplex.pm ↑ Summary