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

filter: Statementstmt 61.7% 42 / 68 Branchbran 50.0% 12 / 24 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 70.5% 12 / 17 Podpod 100% 7 / 7 totaltotal 49.3% 73 / 148 CC 21 SCAR 29.8
CC
21
Cov
48%
row_spans_outside
56.0
cell_coordinate
42.0
visible_cell_rect
12.0
CRAP 19.7
« blib/lib/Term/Fabulous/Render/Attr.pm ↑ Summary blib/lib/Term/Fabulous/Chart/Scale/Linear.pm »
1 package Term::Fabulous::Render::Geometry;
2  
3 6 ▶use v5.32;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶no warnings 'experimental::signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 6 our $VERSION = '0.05';
9  
10 6 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 our @EXPORT_OK = qw(cell_rect intersect_cell_rects visible_cell_rect rects_overlap row_spans_outside cell_coordinate line_steps);
12  
13 6 ▶use List::Util qw(min max);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 6 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 # Every edge is rounded to the nearest cell on its own. Two boxes that
17 # meet share the same edge value up to float32 noise (one's x + width is
18 # the other's x), so they land on the same cell, while flooring would
19 # leave a blank column between an edge of 70.9999 and one of 71.
20 6 ▶sub cell_rect ($bbox) {
Subroutine cell_rect: called CC=1 SCAR=0.0
Pod documented
21 6 my ( $x, $y ) = @{$bbox}{qw(x y)};
22 6 return ( _cell_edge($x), _cell_edge($y), _cell_edge( $x + $bbox->{width} ), _cell_edge( $y + $bbox->{height} ) );
23 }
24  
25 # Rounds to the nearest cell, halves up; int() and a comparison are much
26 # cheaper than POSIX::floor.
27 24 ▶sub _cell_edge ($value) {
Subroutine _cell_edge: called CC=2 SCAR=6.9
28 24 my $shifted = $value + 0.5;
29 24 my $whole = int $shifted;
30 24 ▶ return $whole <= $shifted ? $whole : $whole - 1;
Branch: 1 branch
BranchTrueFalseTotal
$whole <= $shifted ? : 23 1 24
31 }
32  
33 0 ▶sub intersect_cell_rects ( $first, $second ) {
Subroutine intersect_cell_rects: not called CC=1 SCAR=6.9
Pod documented
34 0 my $x0 = max( $first->[0], $second->[0] );
35 0 my $y0 = max( $first->[1], $second->[1] );
36 0 my $x1 = max( $x0, min( $first->[2], $second->[2] ) );
37 0 my $y1 = max( $y0, min( $first->[3], $second->[3] ) );
38 0 return [ $x0, $y0, $x1, $y1 ];
39 }
40  
41 0 ▶sub visible_cell_rect ( $bbox, $clip ) {
Subroutine visible_cell_rect: not called CC=3 SCAR=24.8
Pod documented
42 0 my ( $x0, $y0, $x1, $y1 ) = @{ intersect_cell_rects( [ cell_rect($bbox) ], $clip ) };
43 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
44 0 return ( $x0, $y0, $x1, $y1 );
45 }
46  
47 # Rounds down; int() and a comparison are much cheaper than POSIX::floor.
48 0 ▶sub cell_coordinate ( $what, $value ) {
Subroutine cell_coordinate: not called CC=6 SCAR=37.4
Pod documented
49 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
50 unless looks_like_number($value) && $value == $value && $value - $value == 0;
51 0 my $whole = int $value;
52 0 ▶ return $whole <= $value ? $whole : $whole - 1;
Branch: 1 branch
BranchTrueFalseTotal
$whole <= $value ? : 0 0 0
53 }
54  
55 # Bresenham: the line passes every pixel from one end to the other, both
56 # included. Along the longer (major) axis every step moves one pixel, and
57 # after $i steps the shorter axis has moved round($i * minor / major)
58 # pixels, halves rounded up. The steps whose major coordinate lies in the
59 # raster are computed directly, so a line far longer than the raster
60 # costs no more than one across it.
61 272 ▶sub line_steps ( $x0, $y0, $x1, $y1, $width, $height ) {
Subroutine line_steps: called CC=4 SCAR=13.9
Pod documented
62 272 my ( $dx, $dy ) = ( abs( $x1 - $x0 ), abs( $y1 - $y0 ) );
63 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
64 272 ▶ if ( $dx >= $dy ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dx >= $dy) 179 93 272
65 179 my ( $first, $last ) = _steps_inside( $x0, $step_x, $dx, $width );
66 179 return ( $first, map { [ $x0 + $step_x * $_, $y0 + $step_y * _minor_steps( $_, $dy, $dx ) ] } $first .. $last );
67 }
68 93 my ( $first, $last ) = _steps_inside( $y0, $step_y, $dy, $height );
69 93 return ( $first, map { [ $x0 + $step_x * _minor_steps( $_, $dx, $dy ), $y0 + $step_y * $_ ] } $first .. $last );
70 }
71  
72 # The first and last step $i of 0 .. $length at which $start + $step * $i
73 # lies in 0 .. $limit - 1 (first above last when there is none).
74 272 ▶sub _steps_inside ( $start, $step, $length, $limit ) {
Subroutine _steps_inside: called CC=2 SCAR=6.9
75 272 ▶ my ( $first, $last ) = $step > 0 ? ( -$start, $limit - 1 - $start ) : ( $start - $limit + 1, $start );
Branch: 1 branch
BranchTrueFalseTotal
$step > 0 ? : 159 113 272
76 272 return ( max( $first, 0 ), min( $last, $length ) );
77 }
78  
79 1207 ▶sub _minor_steps ( $i, $minor, $major ) {
Subroutine _minor_steps: called CC=2 SCAR=6.9
80 1207 ▶ return 0 unless $major;
Branch: 1 branch
BranchTrueFalseTotal
unless $major 1 1206 1207
81 1206 return int( ( 2 * $minor * $i + $major ) / ( 2 * $major ) );
82 }
83  
84 0 ▶sub rects_overlap ( $first, $second ) {
Subroutine rects_overlap: not called CC=2 SCAR=17.9
Pod documented
85 0 my ( $x0, $y0, $x1, $y1 ) = @{ intersect_cell_rects( $first, $second ) };
86 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
87 }
88  
89 0 ▶sub row_spans_outside ( $y, $x0, $x1, @rects ) {
Subroutine row_spans_outside: not called CC=7 SCAR=40.3
Pod documented
90 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
91  
92 0 my ( @spans, $from );
93 0 $from = $x0;
94 0 foreach my $hole (@holes) {
95 0 ▶ push @spans, [ $from, $hole->[0] ] if $hole->[0] > $from;
Branch: 1 branch
BranchTrueFalseTotal
if $hole->[0] > $from 0 0 0
96 0 $from = max( $from, $hole->[1] );
97 }
98 0 ▶ push @spans, [ $from, $x1 ] if $x1 > $from;
Branch: 1 branch
BranchTrueFalseTotal
if $x1 > $from 0 0 0
99 0 return @spans;
100 }
101  
102 6 1;
103  
104 __END__
« blib/lib/Term/Fabulous/Render/Attr.pm ↑ Summary blib/lib/Term/Fabulous/Chart/Scale/Linear.pm »