blib/lib/Term/Fabulous/Render/Canvas.pm untested

filter: Statementstmt 0.0% 0 / 142 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 40 MC/DCmcdc 0.0% 0 / 22 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 238 CC 1 SCAR 6.9
CC
1
Cov
0%
_background_attr
2.0
_same_place
2.0
BEGIN
2.0
CRAP 2.0
1 package Term::Fabulous::Render::Canvas;
2  
3 use v5.32;
4 use warnings;
5 use feature 'signatures';
6 no warnings 'experimental::signatures';
7  
8 our $VERSION = '0.02';
9  
10 use Object::Pad 0.825;
11  
12 role Term::Fabulous::Render::Canvas {
13 use Clay::XS qw(CLAY_RENDER_COMMAND_TYPE_CUSTOM);
14 use List::Util qw(max min);
15 use Scalar::Util qw(refaddr);
16 use Term::Fabulous::Termbox qw(TB_DEFAULT);
17 use Term::Fabulous::Color;
18 use Term::Fabulous::Render::Attr qw(color_attr);
19 use Term::Fabulous::Render::Geometry qw(cell_rect intersect_cell_rects rects_overlap);
20  
21 method widget_for;
22  
23 # The cell target the frame is painted into (Term::Fabulous::Render).
24 method cell_target;
25  
26 # By canvas refaddr: { canvas, origin => [x, y], visible => [x0, y0, x1, y1],
27 # background, covered, intact } for the frame being painted, and for the
28 # last frame that was painted completely.
29 field %_plan_by_canvas;
30 field %_painted_by_canvas;
31 field $_painted_viewport = '';
32  
33 # What the unset cells of a canvas show: the first background with an
34 # alpha above 0 at or above it (its cells are then blended over that),
35 # else the screen background of its UI, else the terminal's.
36 sub _background_attr ($canvas) {
37 my $rgba = $canvas->background_below( translucent => 1 ) // return TB_DEFAULT;
38 return color_attr( Term::Fabulous::Color->new( color => $rgba ) );
39 }
40  
41 sub _same_place ( $before, $now ) {
42 return "@{ $before->{origin} } @{ $before->{visible} } $before->{background}" eq "@{ $now->{origin} } @{ $now->{visible} } $now->{background}";
43 }
44  
45 # Sizes the canvas to its content box and lets it bring its cells up to
46 # date; undef when nothing of it is visible.
47 method _plan_canvas ( $command, $clip ) {
48 my $canvas = $self->widget_for( $command->{userData} );
49 die "Term::Fabulous::Render::Canvas: a custom render command needs a Term::Fabulous::Widget::Canvas, got " . ( ref $canvas || 'no widget' )
50 unless defined $canvas && $canvas->isa('Term::Fabulous::Widget::Canvas');
51  
52 my ( $x0, $y0, $x1, $y1 ) = cell_rect( $command->{boundingBox} );
53 my ( $left, $top, $right, $bottom ) = $canvas->content_insets;
54 my @content = ( $x0 + $left, $y0 + $top );
55 push @content, max( $content[0], $x1 - $right ), max( $content[1], $y1 - $bottom );
56 $canvas->set_content_origin( @content[ 0, 1 ] );
57 $canvas->fit_to( $content[2] - $content[0], $content[3] - $content[1] );
58 $canvas->refresh_for_frame;
59  
60 my $visible = intersect_cell_rects( \@content, $clip );
61 return undef unless rects_overlap( $visible, $visible );
62 return { canvas => $canvas, origin => [ @content[ 0, 1 ] ], visible => $visible, background => _background_attr($canvas) };
63 }
64  
65 method plan_canvases ($frame) {
66 my $viewport = join 'x', $frame->width, $frame->height;
67 my %painted = $viewport eq $_painted_viewport ? %_painted_by_canvas : ();
68 %_painted_by_canvas = ();
69 %_plan_by_canvas = ();
70 $_painted_viewport = $viewport;
71  
72 my @commands = $frame->commands;
73 foreach my $index ( grep { $commands[$_]{commandType} == CLAY_RENDER_COMMAND_TYPE_CUSTOM } 0 .. $#commands ) {
74 my $plan = $self->_plan_canvas( $commands[$index], $frame->clip_rect($index) ) // next;
75 my $before = $painted{ refaddr $plan->{canvas} };
76  
77 $plan->{index} = $index;
78 $plan->{covered} = $frame->painted_after( $index, $plan->{visible} );
79 $plan->{intact} = !$plan->{covered} && defined $before && !$before->{covered} && _same_place( $before, $plan );
80 $_plan_by_canvas{ refaddr $plan->{canvas} } = $plan;
81 }
82 return map { $_->{visible} } grep { $_->{intact} } values %_plan_by_canvas;
83 }
84  
85 # The sixel pictures of the frame's canvases, in paint order: each
86 # covers the visible cells of its canvas the target can show, and is
87 # transparent where the frame paints over it.
88 method sixel_placements ( $frame, $target ) {
89 my @cell_size = $target->sixel_cell_size;
90 return () unless @cell_size;
91 my $area = $target->sixel_area( $frame->width, $frame->height );
92 my @placements;
93 foreach my $plan ( sort { $a->{index} <=> $b->{index} } values %_plan_by_canvas ) {
94 my $shown = intersect_cell_rects( $plan->{visible}, $area );
95 next unless rects_overlap( $shown, $shown );
96 my $to_canvas = sub ($rect) {
97 [ map { $rect->[$_] - $plan->{origin}[ $_ % 2 ] } 0 .. 3 ]
98 };
99 my $data = $plan->{canvas}->sixel_data(
100 shown => $to_canvas->($shown),
101 covered => [ map { $to_canvas->($_) } $frame->painted_over( $plan->{index}, $shown ) ],
102 cell_size => [@cell_size],
103 ) // next;
104 push @placements, { x => $shown->[0], y => $shown->[1], columns => $shown->[2] - $shown->[0], rows => $shown->[3] - $shown->[1], data => $data };
105 }
106 return @placements;
107 }
108  
109 method finish_canvases () {
110 %_painted_by_canvas = %_plan_by_canvas;
111 %_plan_by_canvas = ();
112 return;
113 }
114  
115 method invalidate_canvases () {
116 %_painted_by_canvas = ();
117 $_painted_viewport = '';
118 return;
119 }
120  
121 method render_custom ( $command, $widget, $buffer ) {
122 my $plan = $_plan_by_canvas{ refaddr $widget } // return;
123 my $target = $self->cell_target;
124 $target->release_rect( $plan->{visible} ) if $plan->{intact};
125  
126 my $changed_spans = $widget->take_changed_spans;
127 my ( $origin_x, $origin_y ) = @{ $plan->{origin} };
128 my ( $x0, $y0, $x1, $y1 ) = @{ $plan->{visible} };
129 foreach my $y ( $y0 .. $y1 - 1 ) {
130 my ( $row, $from, $to ) = ( $y - $origin_y, $x0 - $origin_x, $x1 - $origin_x );
131 if ( $plan->{intact} ) {
132 _shade_canvas_row( $plan, $row, $from, $to, $buffer );
133 my $span = $changed_spans->[$row] // next;
134 ( $from, $to ) = ( max( $from, $span->[0] ), min( $to, $span->[1] ) );
135 }
136 _paint_canvas_row( $target, $plan, $row, $from, $to, $buffer );
137 }
138 return;
139 }
140  
141 # Records the backgrounds of the canvas columns [from, to) of one row
142 # in the frame's background buffer without painting them: the cells of
143 # an intact canvas stay on screen, but what is drawn later over their
144 # neighbors (a border's outer half) still blends with them.
145 sub _shade_canvas_row ( $plan, $row, $from, $to, $buffer ) {
146 my ( undef, undef, $bgs ) = $plan->{canvas}->cell_row($row);
147 my $origin_x = $plan->{origin}[0];
148 my $shade = $buffer->[ $plan->{origin}[1] + $row ] //= [];
149 $shade->[ $origin_x + $_ ] = $bgs->[$_] // $plan->{background} foreach $from .. $to - 1;
150 return;
151 }
152  
153 # Paints the canvas columns [from, to) of one row. A glyph that would
154 # cross the visible right edge is painted as spaces, like the covered
155 # cells of a wide glyph whose first cell is not painted here.
156 sub _paint_canvas_row ( $target, $plan, $row, $from, $to, $buffer ) {
157 my ( $glyphs, $fgs, $bgs ) = $plan->{canvas}->cell_row($row);
158 my ( $origin_x, $origin_y ) = @{ $plan->{origin} };
159 my $y = $origin_y + $row;
160 my $limit = $plan->{visible}[2] - $origin_x;
161 my $shade = $buffer->[$y] //= [];
162 my $drawn_until = $from;
163  
164 foreach my $column ( $from .. $to - 1 ) {
165 my ( $glyph, $x ) = ( $glyphs->[$column], $origin_x + $column );
166 my $bg = $bgs->[$column] // $plan->{background};
167 $shade->[$x] = $bg;
168  
169 if ( ref $glyph && $column + $glyph->[1] <= $limit ) {
170 my ( undef, $width, $base, @extenders ) = @$glyph;
171 $target->set_cell( $x, $y, $base, $fgs->[$column] // TB_DEFAULT, $bg );
172 $target->extend_cell( $x, $y, $_ ) foreach @extenders;
173 $drawn_until = $column + $width;
174 next;
175 }
176 next if $column < $drawn_until;
177 $target->set_cell( $x, $y, ' ', TB_DEFAULT, $bg );
178 }
179 return;
180 }
181 }
182  
183 1;
184  
185 __END__