blib/lib/Term/Fabulous/Widget/Canvas.pm

filter: Statementstmt 88.2% 15 / 17 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 91.3% 21 / 23 CC 1 SCAR 0.5
CC
1
Cov
88%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/lib/Term/Fabulous/Widget/Box.pm ↑ Summary blib/lib/Term/Fabulous/Widget/Text.pm »
1 package Term::Fabulous::Widget::Canvas;
2  
3 1 ▶use v5.32;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶no warnings 'experimental::signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Object::Pad 0.825;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 0 ▶use Term::Fabulous::Widget::Box;
Subroutine BEGIN: called CC=1 SCAR=2.6
11  
12 our $VERSION = '0.05';
13  
14 class Term::Fabulous::Widget::Canvas
15 :isa(Term::Fabulous::Widget::Box)
16 :strict(params)
17 {
18 use Feature::Compat::Try;
19 use List::Util qw(max min);
20 use POSIX qw(ceil);
21 use Term::Fabulous::Check qw(describe);
22 use Term::Fabulous::Event::CanvasResize;
23 use Term::Fabulous::Render::Attr qw(cell_color_attr);
24 use Term::Fabulous::Render::Geometry qw(cell_coordinate);
25 use Term::Fabulous::Unicode qw(grapheme_clusters cluster_columns);
26  
27 use constant GLYPH_CACHE_LIMIT => 4096;
28  
29 # Stored in the cells a wide glyph covers to the right of its own cell.
30 use constant TAIL => '';
31  
32 # Glyph records, shared by every cell showing the glyph:
33 # [ $cluster, $columns, $base_character, @extending_characters ].
34 my %glyph_by_string;
35 my $SPACE = [ ' ', 1, ' ' ];
36  
37 field $columns :reader = 0;
38 field $rows :reader = 0;
39  
40 # Viewport cell of buffer cell (0, 0) in the last frame that laid it out.
41 field @content_origin;
42  
43 # Cells by [y][x]: a glyph record, TAIL, or undef for an unset cell; the
44 # attributes are undef where the cell has none of its own.
45 field @glyph_rows;
46 field @fg_rows;
47 field @bg_rows;
48  
49 # The cells as take_changed_spans last handed them out, by [y][x], and
50 # per row the [from, to) columns written since: only those can differ.
51 field @taken_glyph_rows;
52 field @taken_fg_rows;
53 field @taken_bg_rows;
54 field @written_spans;
55 field $everything_changed = 1;
56  
57 # Set while the renderer runs refresh: those writes belong to the frame
58 # being drawn and make no further frame due.
59 field $_refreshing = 0;
60  
61 sub _is_tail ($cell) {
62 return defined $cell && !ref $cell;
63 }
64  
65 sub _glyph_of_cluster ($cluster) {
66 my $glyph = $glyph_by_string{$cluster};
67 return $glyph if defined $glyph;
68  
69 %glyph_by_string = () if keys(%glyph_by_string) >= GLYPH_CACHE_LIMIT;
70 my ( $base, @extenders ) = split //, $cluster;
71 return $glyph_by_string{$cluster} = [ $cluster, cluster_columns($cluster), $base, @extenders ];
72 }
73  
74 sub _glyph ($string) {
75 die "Term::Fabulous::Widget::Canvas: a glyph must be a non-empty string, got " . describe($string)
76 unless defined $string && !ref $string && length $string;
77 my $glyph = $glyph_by_string{$string};
78 return $glyph if defined $glyph;
79  
80 my @clusters = grapheme_clusters($string);
81 die "Term::Fabulous::Widget::Canvas: a glyph must be exactly one grapheme cluster, got " . scalar(@clusters) . " in '$string'"
82 unless @clusters == 1;
83 return _glyph_of_cluster( $clusters[0] );
84 }
85  
86 sub _same_glyph ( $glyph, $other ) {
87 return !defined $other unless defined $glyph;
88 return 0 unless defined $other;
89 return ref $glyph ? ref $other && $glyph->[0] eq $other->[0] : !ref $other;
90 }
91  
92 sub _same_attr ( $attr, $other ) {
93 return defined $attr ? defined $other && $attr == $other : !defined $other;
94 }
95  
96 method contribute_custom ($config) {
97 $config->{custom} = { custom_data => 1 };
98 return;
99 }
100  
101 method put ( $x, $y, $glyph, $fg = undef, $bg = undef ) {
102 $self->_store( cell_coordinate( x => $x ), cell_coordinate( y => $y ), _glyph($glyph), cell_color_attr( fg => $fg ), cell_color_attr( bg => $bg ) );
103 return $self->_cells_changed;
104 }
105  
106 method put_text ( $x, $y, $text, $fg = undef, $bg = undef ) {
107 die "Term::Fabulous::Widget::Canvas: text must be a string, got " . describe($text) unless defined $text && !ref $text;
108 my ( $column, $row ) = ( cell_coordinate( x => $x ), cell_coordinate( y => $y ) );
109 my ( $fg_attr, $bg_attr ) = ( cell_color_attr( fg => $fg ), cell_color_attr( bg => $bg ) );
110  
111 foreach my $cluster ( grapheme_clusters($text) ) {
112 last if $column >= $columns;
113 my $glyph = _glyph_of_cluster($cluster);
114 $self->_store( $column, $row, $glyph, $fg_attr, $bg_attr );
115 $column += $glyph->[1];
116 }
117 return $self->_cells_changed;
118 }
119  
120 method fill ( $x, $y, $width, $height, $glyph, $fg = undef, $bg = undef ) {
121 my ( $x0, $y0 ) = ( cell_coordinate( x => $x ), cell_coordinate( y => $y ) );
122 my ( $x1, $y1 ) = ( $x0 + cell_coordinate( width => $width ), $y0 + cell_coordinate( height => $height ) );
123 my $record = _glyph($glyph);
124 my ( $fg_attr, $bg_attr ) = ( cell_color_attr( fg => $fg ), cell_color_attr( bg => $bg ) );
125  
126 # Glyphs repeat from x0 on; skip the repeats left of the buffer.
127 my $step = $record->[1];
128 my $first = $x0 < 0 ? $x0 + $step * ceil( -$x0 / $step ) : $x0;
129 foreach my $row ( max( $y0, 0 ) .. min( $y1, $rows ) - 1 ) {
130 for ( my $column = $first; $column + $step <= min( $x1, $columns ); $column += $step ) {
131 $self->_store( $column, $row, $record, $fg_attr, $bg_attr );
132 }
133 }
134 return $self->_cells_changed;
135 }
136  
137 method put_attrs ( $x, $y, $glyph, $fg_attr, $bg_attr ) {
138 $self->_store( $x, $y, defined $glyph ? _glyph($glyph) : undef, $fg_attr, $bg_attr );
139 return $self->_cells_changed;
140 }
141  
142 method erase ( $x, $y ) {
143 $self->_store( cell_coordinate( x => $x ), cell_coordinate( y => $y ), undef, undef, undef );
144 return $self->_cells_changed;
145 }
146  
147 # Every cell may differ now; take_changed_spans finds the ones that do.
148 method clear () {
149 @$_ = () foreach @glyph_rows, @fg_rows, @bg_rows;
150 @written_spans = map { [ 0, $columns ] } 1 .. $rows;
151 return $self->_cells_changed;
152 }
153  
154 method _cells_changed () {
155 return $self if $_refreshing;
156 return $self->mark_changed;
157 }
158  
159 # What the cells show, from the widget's own state; called by the
160 # renderer before it paints them. A plain canvas is drawn into by its
161 # users and has nothing to do.
162 method refresh () {
163 return;
164 }
165  
166 # A sixel picture the terminal draws over the cells; called by the
167 # renderer after it painted them, on a target that shows sixel. A
168 # plain canvas has none.
169 method sixel_data (%frame) {
170 return undef;
171 }
172  
173 method refresh_for_frame () {
174 $_refreshing = 1;
175 try {
176 $self->refresh;
177 }
178 catch ($error) {
179 $_refreshing = 0;
180 die $error;
181 }
182 $_refreshing = 0;
183 return;
184 }
185  
186 method cell ( $x, $y ) {
187 my ( $column, $row ) = ( cell_coordinate( x => $x ), cell_coordinate( y => $y ) );
188 return undef if $row < 0 || $row >= $rows || $column < 0 || $column >= $columns;
189  
190 my $glyph = $glyph_rows[$row][$column];
191 return undef unless ref $glyph;
192 return [ $glyph->[0], $fg_rows[$row][$column], $bg_rows[$row][$column] ];
193 }
194  
195 method cell_at ($event) {
196 return () unless @content_origin;
197 my ( $column, $row ) = ( $event->x - $content_origin[0], $event->y - $content_origin[1] );
198 return () if $row < 0 || $row >= $rows || $column < 0 || $column >= $columns;
199 return ( $column, $row );
200 }
201  
202 # Records where the frame being drawn puts the buffer. Nothing is drawn
203 # from it, and the renderer calls this every frame, so it does not mark
204 # the canvas changed.
205 method set_content_origin ( $x, $y ) {
206 @content_origin = ( $x, $y );
207 return;
208 }
209  
210 method content_origin () {
211 return @content_origin;
212 }
213  
214 method cell_row ($y) {
215 return ( $glyph_rows[$y], $fg_rows[$y], $bg_rows[$y] );
216 }
217  
218 method take_changed_spans () {
219 my @spans;
220 if ($everything_changed) {
221 @spans = map { [ 0, $columns ] } 1 .. $rows;
222 }
223 else {
224 foreach my $y ( 0 .. $#written_spans ) {
225 my $span = $self->_changed_span_of($y) // next;
226 $spans[$y] = $span;
227 }
228 }
229 $self->_remember_taken( $everything_changed ? map { [ 0, $columns ] } 1 .. $rows : @written_spans );
230 @written_spans = ();
231 $everything_changed = 0;
232 return \@spans;
233 }
234  
235 # The written columns of a row that differ from what was taken, as one
236 # [from, to) span, or undef. A write never splits a wide glyph without
237 # changing its first cell, so a span never starts in an unchanged one.
238 method _changed_span_of ($y) {
239 my $written = $written_spans[$y] // return undef;
240 my ( $glyphs, $fgs, $bgs ) = ( $glyph_rows[$y], $fg_rows[$y], $bg_rows[$y] );
241 my ( $taken_glyphs, $taken_fgs, $taken_bgs ) = ( $taken_glyph_rows[$y] // [], $taken_fg_rows[$y] // [], $taken_bg_rows[$y] // [] );
242 my ( $first, $last );
243 foreach my $x ( $written->[0] .. $written->[1] - 1 ) {
244 next if _same_glyph( $glyphs->[$x], $taken_glyphs->[$x] ) && _same_attr( $fgs->[$x], $taken_fgs->[$x] ) && _same_attr( $bgs->[$x], $taken_bgs->[$x] );
245 $first //= $x;
246 $last = $x;
247 }
248 return defined $first ? [ $first, $last + 1 ] : undef;
249 }
250  
251 method _remember_taken (@spans) {
252 foreach my $y ( 0 .. $#spans ) {
253 my $span = $spans[$y] // next;
254 my @columns = $span->[0] .. $span->[1] - 1;
255 @{ $taken_glyph_rows[$y] //= [] }[@columns] = @{ $glyph_rows[$y] }[@columns];
256 @{ $taken_fg_rows[$y] //= [] }[@columns] = @{ $fg_rows[$y] }[@columns];
257 @{ $taken_bg_rows[$y] //= [] }[@columns] = @{ $bg_rows[$y] }[@columns];
258 }
259 return;
260 }
261  
262 method content_insets () {
263 my $padding = $self->to_config->{layout}{padding} // {};
264 return map { $padding->{$_} // 0 } qw(left top right bottom);
265 }
266  
267 # The cells between the outer box and the content box on each side, as
268 # the last frame laid the canvas out (its content_box against its
269 # bounding_box), for the renderer and subclasses; the empty list when
270 # that frame did not lay it out.
271 method _laid_out_insets () {
272 my $ui = $self->ui // return;
273 my $outer = $ui->bounding_box($self) // return;
274 my $content = $ui->content_box($self);
275 return (
276 $content->{x} - $outer->{x},
277 $content->{y} - $outer->{y},
278 $outer->{x} + $outer->{width} - $content->{x} - $content->{width},
279 $outer->{y} + $outer->{height} - $content->{y} - $content->{height},
280 );
281 }
282  
283 method fit_to ( $new_columns, $new_rows ) {
284 die "Term::Fabulous::Widget::Canvas: fit_to needs a non-negative integer size, got " . describe($new_columns) . " x " . describe($new_rows)
285 unless grep( { defined && /\A[0-9]+\z/ } $new_columns, $new_rows ) == 2;
286 return if $new_columns == $columns && $new_rows == $rows;
287  
288 foreach my $rows_of_cells ( \@glyph_rows, \@fg_rows, \@bg_rows ) {
289 $#$rows_of_cells = $new_rows - 1;
290 foreach my $cells (@$rows_of_cells) {
291 $cells //= [];
292 $#$cells = $new_columns - 1 if @$cells > $new_columns;
293 }
294 }
295 _unset_cut_glyph( $_, $new_columns ) foreach @glyph_rows;
296  
297 ( $columns, $rows ) = ( $new_columns + 0, $new_rows + 0 );
298 @written_spans = ();
299 @$_ = () foreach \@taken_glyph_rows, \@taken_fg_rows, \@taken_bg_rows;
300 $everything_changed = 1;
301 $self->mark_changed;
302 $self->fire_event( Term::Fabulous::Event::CanvasResize->new( columns => $columns, rows => $rows ) );
303 return;
304 }
305  
306 # A wide glyph that no longer fits at the right edge leaves its cells unset.
307 sub _unset_cut_glyph ( $glyphs, $columns ) {
308 my $head = $columns - 1;
309 $head-- while $head > 0 && _is_tail( $glyphs->[$head] );
310 return unless ref $glyphs->[$head] && $head + $glyphs->[$head][1] > $columns;
311 $glyphs->[$_] = undef foreach $head .. $columns - 1;
312 return;
313 }
314  
315 # Writes one glyph (undef unsets the cell). Writes that do not fit into
316 # the buffer are dropped. A wide glyph partly overwritten here leaves
317 # spaces in its other cells.
318 method _store ( $x, $y, $glyph, $fg, $bg ) {
319 my $width = defined $glyph ? $glyph->[1] : 1;
320 return if $y < 0 || $y >= $rows || $x < 0 || $x + $width > $columns;
321  
322 my ( $glyphs, $fgs, $bgs ) = ( $glyph_rows[$y], $fg_rows[$y], $bg_rows[$y] );
323 my ( $from, $to ) = ( $x, $x + $width );
324 my $first = $glyphs->[$x];
325 if ( defined $first && !ref $first ) {
326 $from-- while _is_tail( $glyphs->[$from] );
327 $glyphs->[$_] = $SPACE foreach $from .. $x - 1;
328 }
329 while ( $to < $columns && _is_tail( $glyphs->[$to] ) ) {
330 $glyphs->[ $to++ ] = $SPACE;
331 }
332  
333 ( $glyphs->[$x], $fgs->[$x], $bgs->[$x] ) = ( $glyph, $fg, $bg );
334 if ( $width > 1 ) {
335 my @covered = ( $x + 1 .. $x + $width - 1 );
336 @{$glyphs}[@covered] = (TAIL) x @covered;
337 @{$fgs}[@covered] = ($fg) x @covered;
338 @{$bgs}[@covered] = ($bg) x @covered;
339 }
340  
341 return if $everything_changed;
342 my $span = $written_spans[$y];
343 if ( !defined $span ) {
344 $written_spans[$y] = [ $from, $to ];
345 return;
346 }
347 $span->[0] = $from if $from < $span->[0];
348 $span->[1] = $to if $to > $span->[1];
349 return;
350 }
351 }
352  
353 1;
354  
355 __END__
« blib/lib/Term/Fabulous/Widget/Box.pm ↑ Summary blib/lib/Term/Fabulous/Widget/Text.pm »