blib/lib/Term/Fabulous/Render/Geometry.pm

filter: Statementstmt 57.3% 39 / 68 Branchbran 45.4% 10 / 22 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 64.7% 11 / 17 Podpod 100% 7 / 7 totaltotal 45.8% 67 / 146 CC 20 SCAR 30.1
CC
20
Cov
44%
row_spans_outside
56.0
cell_coordinate
42.0
visible_cell_rect
12.0
CRAP 20.2
« blib/lib/Term/Fabulous/Render/Attr.pm ↑ Summary blib/lib/Term/Fabulous/Chart/Scale/Linear.pm »
1 package Term::Fabulous::Render::Geometry;
2  
3 5 ▶use v5.32;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶no warnings 'experimental::signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 5 our $VERSION = '0.02';
9  
10 5 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 our @EXPORT_OK = qw(cell_rect intersect_cell_rects visible_cell_rect rects_overlap row_spans_outside cell_coordinate line_steps);
12  
13 5 ▶use List::Util qw(min max);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use POSIX qw(floor);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 0 ▶sub cell_rect ($bbox) {
Subroutine cell_rect: not called CC=1 SCAR=6.9
Pod documented
18 0 my ( $x, $y ) = @{$bbox}{qw(x y)};
19 0 return ( floor($x), floor($y), floor( $x + $bbox->{width} ), floor( $y + $bbox->{height} ) );
20 }
21  
22 0 ▶sub intersect_cell_rects ( $first, $second ) {
Subroutine intersect_cell_rects: not called CC=1 SCAR=6.9
Pod documented
23 0 my $x0 = max( $first->[0], $second->[0] );
24 0 my $y0 = max( $first->[1], $second->[1] );
25 0 my $x1 = max( $x0, min( $first->[2], $second->[2] ) );
26 0 my $y1 = max( $y0, min( $first->[3], $second->[3] ) );
27 0 return [ $x0, $y0, $x1, $y1 ];
28 }
29  
30 0 ▶sub visible_cell_rect ( $bbox, $clip ) {
Subroutine visible_cell_rect: not called CC=3 SCAR=24.8
Pod documented
31 0 my ( $x0, $y0, $x1, $y1 ) = @{ intersect_cell_rects( [ cell_rect($bbox) ], $clip ) };
32 0 ▶ return if $x0 >= $x1 || $y0 >= $y1;
Branch: 1 branch
BranchTrueFalseTotal
if $x0 >= $x1 or $y0 >= $y1 0 0 0
Condition0%0/3
$x0 >= $x1 or $y0 >= $y1
l!l&&r!l&&!r
000
MC/DC0%0/2
$x0 >= $x1 or $y0 >= $y1
$x0 >= $x1 $y0 >= $y1
Truth table0%0/3
A or B
A= $x0 >= $x1B= $y0 >= $y1
ABresult
1X1
011
000
33 0 return ( $x0, $y0, $x1, $y1 );
34 }
35  
36 # Rounds down; int() and a comparison are much cheaper than POSIX::floor.
37 0 ▶sub cell_coordinate ( $what, $value ) {
Subroutine cell_coordinate: not called CC=6 SCAR=37.4
Pod documented
38 0 ▶ die "Term::Fabulous::Render::Geometry: $what must be a finite number, got " . ( defined $value ? "'$value'" : 'undef' )
Branch: 2 branches
BranchTrueFalseTotal
unless &looks_like_number($value) and $value == $value and $value - $value == 0 0 0 0
defined $value ? : 0 0 0
Condition0%0/6
&looks_like_number($value) and $value == $value and $value - $value == 0
!ll&&!rl&&r
000
&looks_like_number($value) and $value == $value
!ll&&!rl&&r
000
MC/DC0%0/3
&looks_like_number($value) and $value == $value and $value - $value == 0
&looks_like_number($value) $value == $value $value - $value == 0
Truth table0%0/4
A and B and C
A= &looks_like_number($value)B= $value == $valueC= $value - $value == 0
ABCresult
0XX0
10X0
1100
1111
39 unless looks_like_number($value) && $value == $value && $value - $value == 0;
40 0 my $whole = int $value;
41 0 ▶ return $whole <= $value ? $whole : $whole - 1;
Branch: 1 branch
BranchTrueFalseTotal
$whole <= $value ? : 0 0 0
42 }
43  
44 # Bresenham: the line passes every pixel from one end to the other, both
45 # included. Along the longer (major) axis every step moves one pixel, and
46 # after $i steps the shorter axis has moved round($i * minor / major)
47 # pixels, halves rounded up. The steps whose major coordinate lies in the
48 # raster are computed directly, so a line far longer than the raster
49 # costs no more than one across it.
50 272 ▶sub line_steps ( $x0, $y0, $x1, $y1, $width, $height ) {
Subroutine line_steps: called CC=4 SCAR=13.9
Pod documented
51 272 my ( $dx, $dy ) = ( abs( $x1 - $x0 ), abs( $y1 - $y0 ) );
52 272 ▶ my ( $step_x, $step_y ) = ( $x0 < $x1 ? 1 : -1, $y0 < $y1 ? 1 : -1 );
Branch: 2 branches
BranchTrueFalseTotal
$x0 < $x1 ? : 155 117 272
$y0 < $y1 ? : 153 119 272
53 272 ▶ if ( $dx >= $dy ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dx >= $dy) 179 93 272
54 179 my ( $first, $last ) = _steps_inside( $x0, $step_x, $dx, $width );
55 179 return ( $first, map { [ $x0 + $step_x * $_, $y0 + $step_y * _minor_steps( $_, $dy, $dx ) ] } $first .. $last );
56 }
57 93 my ( $first, $last ) = _steps_inside( $y0, $step_y, $dy, $height );
58 93 return ( $first, map { [ $x0 + $step_x * _minor_steps( $_, $dx, $dy ), $y0 + $step_y * $_ ] } $first .. $last );
59 }
60  
61 # The first and last step $i of 0 .. $length at which $start + $step * $i
62 # lies in 0 .. $limit - 1 (first above last when there is none).
63 272 ▶sub _steps_inside ( $start, $step, $length, $limit ) {
Subroutine _steps_inside: called CC=2 SCAR=6.9
64 272 ▶ my ( $first, $last ) = $step > 0 ? ( -$start, $limit - 1 - $start ) : ( $start - $limit + 1, $start );
Branch: 1 branch
BranchTrueFalseTotal
$step > 0 ? : 159 113 272
65 272 return ( max( $first, 0 ), min( $last, $length ) );
66 }
67  
68 1207 ▶sub _minor_steps ( $i, $minor, $major ) {
Subroutine _minor_steps: called CC=2 SCAR=6.9
69 1207 ▶ return 0 unless $major;
Branch: 1 branch
BranchTrueFalseTotal
unless $major 1 1206 1207
70 1206 return int( ( 2 * $minor * $i + $major ) / ( 2 * $major ) );
71 }
72  
73 0 ▶sub rects_overlap ( $first, $second ) {
Subroutine rects_overlap: not called CC=2 SCAR=17.9
Pod documented
74 0 my ( $x0, $y0, $x1, $y1 ) = @{ intersect_cell_rects( $first, $second ) };
75 0 ▶ return $x0 < $x1 && $y0 < $y1;
Condition0%0/3
$x0 < $x1 && $y0 < $y1
!ll&&!rl&&r
000
MC/DC0%0/2
$x0 < $x1 && $y0 < $y1
$x0 < $x1 $y0 < $y1
Truth table0%0/3
A && B
A= $x0 < $x1B= $y0 < $y1
ABresult
0X0
100
111
76 }
77  
78 0 ▶sub row_spans_outside ( $y, $x0, $x1, @rects ) {
Subroutine row_spans_outside: not called CC=7 SCAR=40.3
Pod documented
79 0 ▶ my @holes = sort { $a->[0] <=> $b->[0] } map { [ max( $x0, $_->[0] ), min( $x1, $_->[2] ) ] } grep { $y >= $_->[1] && $y < $_->[3] && $_->[0] < $x1 && $_->[2] > $x0 } @rects;
Condition0%0/9
$y >= $_->[1] && $y < $_->[3] && $_->[0] < $x1 && $_->[2] > $x0
!ll&&!rl&&r
000
$y >= $_->[1] and $y < $_->[3] and $_->[0] < $x1
!ll&&!rl&&r
000
$y >= $_->[1] and $y < $_->[3]
!ll&&!rl&&r
000
MC/DC0%0/4
$y >= $_->[1] && $y < $_->[3] && $_->[0] < $x1 && $_->[2] > $x0
$y >= $_->[1] $y < $_->[3] $_->[0] < $x1 $_->[2] > $x0
Truth table0%0/5
A and B and C && D
A= $y >= $_->[1]B= $y < $_->[3]C= $_->[0] < $x1D= $_->[2] > $x0
ABCDresult
0XXX0
10XX0
110X0
11100
11111
80  
81 0 my ( @spans, $from );
82 0 $from = $x0;
83 0 foreach my $hole (@holes) {
84 0 ▶ push @spans, [ $from, $hole->[0] ] if $hole->[0] > $from;
Branch: 1 branch
BranchTrueFalseTotal
if $hole->[0] > $from 0 0 0
85 0 $from = max( $from, $hole->[1] );
86 }
87 0 ▶ push @spans, [ $from, $x1 ] if $x1 > $from;
Branch: 1 branch
BranchTrueFalseTotal
if $x1 > $from 0 0 0
88 0 return @spans;
89 }
90  
91 5 1;
92  
93 __END__
« blib/lib/Term/Fabulous/Render/Attr.pm ↑ Summary blib/lib/Term/Fabulous/Chart/Scale/Linear.pm »