| 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.05'; | ||
| 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->_laid_out_insets | ||
| 54 | or die "Term::Fabulous::Render::Canvas: the frame did not lay out the canvas of a custom render command"; | ||
| 55 | my @content = ( $x0 + $left, $y0 + $top ); | ||
| 56 | push @content, max( $content[0], $x1 - $right ), max( $content[1], $y1 - $bottom ); | ||
| 57 | $canvas->set_content_origin( @content[ 0, 1 ] ); | ||
| 58 | $canvas->fit_to( $content[2] - $content[0], $content[3] - $content[1] ); | ||
| 59 | $canvas->refresh_for_frame; | ||
| 60 | |||
| 61 | my $visible = intersect_cell_rects( \@content, $clip ); | ||
| 62 | return undef unless rects_overlap( $visible, $visible ); | ||
| 63 | return { canvas => $canvas, origin => [ @content[ 0, 1 ] ], visible => $visible, background => _background_attr($canvas) }; | ||
| 64 | } | ||
| 65 | |||
| 66 | method plan_canvases ($frame) { | ||
| 67 | my $viewport = join 'x', $frame->width, $frame->height; | ||
| 68 | my %painted = $viewport eq $_painted_viewport ? %_painted_by_canvas : (); | ||
| 69 | %_painted_by_canvas = (); | ||
| 70 | %_plan_by_canvas = (); | ||
| 71 | $_painted_viewport = $viewport; | ||
| 72 | |||
| 73 | my @commands = $frame->commands; | ||
| 74 | foreach my $index ( grep { $commands[$_]{commandType} == CLAY_RENDER_COMMAND_TYPE_CUSTOM } 0 .. $#commands ) { | ||
| 75 | my $plan = $self->_plan_canvas( $commands[$index], $frame->clip_rect($index) ) // next; | ||
| 76 | my $before = $painted{ refaddr $plan->{canvas} }; | ||
| 77 | |||
| 78 | $plan->{index} = $index; | ||
| 79 | $plan->{covered} = $frame->painted_after( $index, $plan->{visible} ); | ||
| 80 | $plan->{intact} = !$plan->{covered} && defined $before && !$before->{covered} && _same_place( $before, $plan ); | ||
| 81 | $_plan_by_canvas{ refaddr $plan->{canvas} } = $plan; | ||
| 82 | } | ||
| 83 | return map { $_->{visible} } grep { $_->{intact} } values %_plan_by_canvas; | ||
| 84 | } | ||
| 85 | |||
| 86 | # The sixel pictures of the frame's canvases, in paint order: each | ||
| 87 | # covers the visible cells of its canvas the target can show, and is | ||
| 88 | # transparent where the frame paints over it. | ||
| 89 | method sixel_placements ( $frame, $target ) { | ||
| 90 | my @cell_size = $target->sixel_cell_size; | ||
| 91 | return () unless @cell_size; | ||
| 92 | my $area = $target->sixel_area( $frame->width, $frame->height ); | ||
| 93 | my @placements; | ||
| 94 | foreach my $plan ( sort { $a->{index} <=> $b->{index} } values %_plan_by_canvas ) { | ||
| 95 | my $shown = intersect_cell_rects( $plan->{visible}, $area ); | ||
| 96 | next unless rects_overlap( $shown, $shown ); | ||
| 97 | my $to_canvas = sub ($rect) { | ||
| 98 | [ map { $rect->[$_] - $plan->{origin}[ $_ % 2 ] } 0 .. 3 ] | ||
| 99 | }; | ||
| 100 | my $data = $plan->{canvas}->sixel_data( | ||
| 101 | shown => $to_canvas->($shown), | ||
| 102 | covered => [ map { $to_canvas->($_) } $frame->painted_over( $plan->{index}, $shown ) ], | ||
| 103 | cell_size => [@cell_size], | ||
| 104 | ) // next; | ||
| 105 | push @placements, { x => $shown->[0], y => $shown->[1], columns => $shown->[2] - $shown->[0], rows => $shown->[3] - $shown->[1], data => $data }; | ||
| 106 | } | ||
| 107 | return @placements; | ||
| 108 | } | ||
| 109 | |||
| 110 | method finish_canvases () { | ||
| 111 | %_painted_by_canvas = %_plan_by_canvas; | ||
| 112 | %_plan_by_canvas = (); | ||
| 113 | return; | ||
| 114 | } | ||
| 115 | |||
| 116 | method invalidate_canvases () { | ||
| 117 | %_painted_by_canvas = (); | ||
| 118 | $_painted_viewport = ''; | ||
| 119 | return; | ||
| 120 | } | ||
| 121 | |||
| 122 | method render_custom ( $command, $widget, $buffer ) { | ||
| 123 | my $plan = $_plan_by_canvas{ refaddr $widget } // return; | ||
| 124 | my $target = $self->cell_target; | ||
| 125 | $target->release_rect( $plan->{visible} ) if $plan->{intact}; | ||
| 126 | |||
| 127 | my $changed_spans = $widget->take_changed_spans; | ||
| 128 | my ( $origin_x, $origin_y ) = @{ $plan->{origin} }; | ||
| 129 | my ( $x0, $y0, $x1, $y1 ) = @{ $plan->{visible} }; | ||
| 130 | foreach my $y ( $y0 .. $y1 - 1 ) { | ||
| 131 | my ( $row, $from, $to ) = ( $y - $origin_y, $x0 - $origin_x, $x1 - $origin_x ); | ||
| 132 | if ( $plan->{intact} ) { | ||
| 133 | _shade_canvas_row( $plan, $row, $from, $to, $buffer ); | ||
| 134 | my $span = $changed_spans->[$row] // next; | ||
| 135 | ( $from, $to ) = ( max( $from, $span->[0] ), min( $to, $span->[1] ) ); | ||
| 136 | } | ||
| 137 | _paint_canvas_row( $target, $plan, $row, $from, $to, $buffer ); | ||
| 138 | } | ||
| 139 | return; | ||
| 140 | } | ||
| 141 | |||
| 142 | # Records the backgrounds of the canvas columns [from, to) of one row | ||
| 143 | # in the frame's background buffer without painting them: the cells of | ||
| 144 | # an intact canvas stay on screen, but what is drawn later over their | ||
| 145 | # neighbors (a border's outer half) still blends with them. | ||
| 146 | sub _shade_canvas_row ( $plan, $row, $from, $to, $buffer ) { | ||
| 147 | my ( undef, undef, $bgs ) = $plan->{canvas}->cell_row($row); | ||
| 148 | my $origin_x = $plan->{origin}[0]; | ||
| 149 | my $shade = $buffer->[ $plan->{origin}[1] + $row ] //= []; | ||
| 150 | $shade->[ $origin_x + $_ ] = $bgs->[$_] // $plan->{background} foreach $from .. $to - 1; | ||
| 151 | return; | ||
| 152 | } | ||
| 153 | |||
| 154 | # Paints the canvas columns [from, to) of one row. A glyph that would | ||
| 155 | # cross the visible right edge is painted as spaces, like the covered | ||
| 156 | # cells of a wide glyph whose first cell is not painted here. | ||
| 157 | sub _paint_canvas_row ( $target, $plan, $row, $from, $to, $buffer ) { | ||
| 158 | my ( $glyphs, $fgs, $bgs ) = $plan->{canvas}->cell_row($row); | ||
| 159 | my ( $origin_x, $origin_y ) = @{ $plan->{origin} }; | ||
| 160 | my $y = $origin_y + $row; | ||
| 161 | my $limit = $plan->{visible}[2] - $origin_x; | ||
| 162 | my $shade = $buffer->[$y] //= []; | ||
| 163 | my $drawn_until = $from; | ||
| 164 | |||
| 165 | foreach my $column ( $from .. $to - 1 ) { | ||
| 166 | my ( $glyph, $x ) = ( $glyphs->[$column], $origin_x + $column ); | ||
| 167 | my $bg = $bgs->[$column] // $plan->{background}; | ||
| 168 | $shade->[$x] = $bg; | ||
| 169 | |||
| 170 | if ( ref $glyph && $column + $glyph->[1] <= $limit ) { | ||
| 171 | my ( undef, $width, $base, @extenders ) = @$glyph; | ||
| 172 | $target->set_cell( $x, $y, $base, $fgs->[$column] // TB_DEFAULT, $bg ); | ||
| 173 | $target->extend_cell( $x, $y, $_ ) foreach @extenders; | ||
| 174 | $drawn_until = $column + $width; | ||
| 175 | next; | ||
| 176 | } | ||
| 177 | next if $column < $drawn_until; | ||
| 178 | $target->set_cell( $x, $y, ' ', TB_DEFAULT, $bg ); | ||
| 179 | } | ||
| 180 | return; | ||
| 181 | } | ||
| 182 | } | ||
| 183 | |||
| 184 | 1; | ||
| 185 | |||
| 186 | __END__ |