blib/lib/Term/Fabulous/Widget/PixelCanvas.pm untested

filter: Statementstmt 0.0% 0 / 117 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 39 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 0.0% 0 / 11 Podpod n/a totaltotal 0.0% 0 / 215 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package Term::Fabulous::Widget::PixelCanvas;
2  
3 use v5.32;
4 use warnings;
5 use feature 'signatures';
6 no warnings 'experimental::signatures';
7  
8 use Object::Pad 0.825;
9  
10 use Term::Fabulous::Widget::Canvas;
11  
12 our $VERSION = '0.05';
13  
14 class Term::Fabulous::Widget::PixelCanvas
15 :isa(Term::Fabulous::Widget::Canvas)
16 :strict(params)
17 {
18 use List::Util qw(max min);
19 use Term::Fabulous::Render::Attr qw(cell_color_attr);
20 use Term::Fabulous::Render::Geometry qw(cell_coordinate line_steps);
21  
22 use constant UPPER_HALF => "\x{2580}";
23 use constant LOWER_HALF => "\x{2584}";
24  
25 method pixel_width () {
26 return $self->columns;
27 }
28  
29 method pixel_height () {
30 return 2 * $self->rows;
31 }
32  
33 # The (top, bottom) pixel attributes a cell shows; a cell that holds
34 # anything but a half block shows no pixels.
35 method _pixels_of_cell ( $x, $row ) {
36 my ( $glyphs, $fgs, $bgs ) = $self->cell_row($row);
37 my $glyph = $glyphs->[$x];
38 return ( undef, undef ) unless ref $glyph;
39 return ( $fgs->[$x], $bgs->[$x] ) if $glyph->[0] eq UPPER_HALF;
40 return ( $bgs->[$x], $fgs->[$x] ) if $glyph->[0] eq LOWER_HALF;
41 return ( undef, undef );
42 }
43  
44 # Sets one pixel of whole coordinates to an attribute (undef unsets
45 # it); pixels outside the buffer are dropped.
46 method _paint_pixel ( $x, $y, $attr ) {
47 return if $x < 0 || $y < 0 || $x >= $self->columns || $y >= 2 * $self->rows;
48  
49 my $row = int( $y / 2 );
50 my ( $top, $bottom ) = $self->_pixels_of_cell( $x, $row );
51 if ( $y % 2 ) { $bottom = $attr }
52 else { $top = $attr }
53  
54 return $self->put_attrs( $x, $row, undef, undef, undef ) if !defined $top && !defined $bottom;
55 return $self->put_attrs( $x, $row, LOWER_HALF, $bottom, undef ) if !defined $top;
56 return $self->put_attrs( $x, $row, UPPER_HALF, $top, $bottom );
57 }
58  
59 method set_pixel ( $x, $y, $color ) {
60 $self->_paint_pixel( cell_coordinate( x => $x ), cell_coordinate( y => $y ), cell_color_attr( color => $color ) );
61 return $self;
62 }
63  
64 method unset_pixel ( $x, $y ) {
65 $self->_paint_pixel( cell_coordinate( x => $x ), cell_coordinate( y => $y ), undef );
66 return $self;
67 }
68  
69 method pixel ( $x, $y ) {
70 my ( $column, $y_pixel ) = ( cell_coordinate( x => $x ), cell_coordinate( y => $y ) );
71 return undef if $column < 0 || $y_pixel < 0 || $column >= $self->columns || $y_pixel >= 2 * $self->rows;
72  
73 my ( $top, $bottom ) = $self->_pixels_of_cell( $column, int( $y_pixel / 2 ) );
74 return $y_pixel % 2 ? $bottom : $top;
75 }
76  
77 method fill_rect ( $x, $y, $width, $height, $color ) {
78 my ( $x0, $y0 ) = ( cell_coordinate( x => $x ), cell_coordinate( y => $y ) );
79 my ( $x1, $y1 ) = ( $x0 + cell_coordinate( width => $width ), $y0 + cell_coordinate( height => $height ) );
80 my $attr = cell_color_attr( color => $color );
81  
82 foreach my $y_pixel ( max( $y0, 0 ) .. min( $y1, $self->pixel_height ) - 1 ) {
83 $self->_paint_pixel( $_, $y_pixel, $attr ) foreach max( $x0, 0 ) .. min( $x1, $self->pixel_width ) - 1;
84 }
85 return $self;
86 }
87  
88 method draw_rect ( $x, $y, $width, $height, $color ) {
89 my ( $x0, $y0 ) = ( cell_coordinate( x => $x ), cell_coordinate( y => $y ) );
90 my ( $x1, $y1 ) = ( $x0 + cell_coordinate( width => $width ) - 1, $y0 + cell_coordinate( height => $height ) - 1 );
91 my $attr = cell_color_attr( color => $color );
92 return $self if $x1 < $x0 || $y1 < $y0;
93  
94 foreach my $column ( max( $x0, 0 ) .. min( $x1, $self->pixel_width - 1 ) ) {
95 $self->_paint_pixel( $column, $_, $attr ) foreach $y0, $y1;
96 }
97 foreach my $y_pixel ( max( $y0 + 1, 0 ) .. min( $y1 - 1, $self->pixel_height - 1 ) ) {
98 $self->_paint_pixel( $_, $y_pixel, $attr ) foreach $x0, $x1;
99 }
100 return $self;
101 }
102  
103 # Every pixel the line passes, both end points included
104 # (Term::Fabulous::Render::Geometry::line_steps).
105 method draw_line ( $from_x, $from_y, $to_x, $to_y, $color ) {
106 my ( $x0, $y0 ) = ( cell_coordinate( x => $from_x ), cell_coordinate( y => $from_y ) );
107 my ( $x1, $y1 ) = ( cell_coordinate( x => $to_x ), cell_coordinate( y => $to_y ) );
108 my $attr = cell_color_attr( color => $color );
109 my ( undef, @pixels ) = line_steps( $x0, $y0, $x1, $y1, $self->pixel_width, $self->pixel_height );
110 $self->_paint_pixel( @$_, $attr ) foreach @pixels;
111 return $self;
112 }
113  
114 # Midpoint circle: the outline, one pixel wide. In each octant the
115 # point of row $y lies round(sqrt(r^2 - y^2)) pixels out, so only the
116 # rows and columns inside the canvas are computed.
117 method draw_circle ( $center_x, $center_y, $radius, $color ) {
118 my ( $cx, $cy, $r ) = ( cell_coordinate( x => $center_x ), cell_coordinate( y => $center_y ), cell_coordinate( radius => $radius ) );
119 die "Term::Fabulous::Widget::PixelCanvas: radius must not be negative, got $radius" if $r < 0;
120 my $attr = cell_color_attr( color => $color );
121 my ( $width, $height ) = ( $self->pixel_width, $self->pixel_height );
122 return $self if $cx + $r < 0 || $cx - $r >= $width || $cy + $r < 0 || $cy - $r >= $height;
123  
124 # The octant runs while the point stays on or above the diagonal.
125 my $last = int( $r / sqrt 2 );
126 $last++ while _circle_offset( $r, $last + 1 ) >= $last + 1;
127 $last-- while $last > 0 && _circle_offset( $r, $last ) < $last;
128  
129 # A step $y paints rows $cy +- $y and columns $cx +- $y; any other
130 # step paints nothing inside the canvas.
131 my %visible;
132 foreach my $range ( [ $cy, $height ], [ $cx, $width ] ) {
133 my ( $center, $limit ) = @$range;
134 $visible{$_} = 1 foreach max( 0, -$center ) .. min( $last, $limit - 1 - $center ), max( 0, $center - $limit + 1 ) .. min( $last, $center );
135 }
136 foreach my $y ( keys %visible ) {
137 my $x = _circle_offset( $r, $y );
138 foreach my $offset ( [ $x, $y ], [ $y, $x ] ) {
139 my ( $ox, $oy ) = @$offset;
140 $self->_paint_pixel( $cx + $_->[0], $cy + $_->[1], $attr ) foreach [ $ox, $oy ], [ -$ox, $oy ], [ $ox, -$oy ], [ -$ox, -$oy ];
141 }
142 }
143 return $self;
144 }
145  
146 # round(sqrt(r^2 - y^2)), corrected to the exact integer answer.
147 sub _circle_offset ( $r, $y ) {
148 my $square = $r * $r - $y * $y;
149 return -1 if $square < 0;
150 my $x = int( sqrt($square) + 0.5 );
151 $x-- while $x > 0 && ( 2 * $x - 1 )**2 > 4 * $square;
152 $x++ while ( 2 * $x + 1 )**2 <= 4 * $square;
153 return $x;
154 }
155  
156 method pixel_at ($event) {
157 my ( $column, $row ) = $self->cell_at($event);
158 return () unless defined $column;
159 return ( $column, 2 * $row );
160 }
161 }
162  
163 1;
164  
165 __END__