blib/lib/Term/Fabulous/Chart/Surface.pm untested

filter: Statementstmt 0.0% 0 / 149 Branchbran 0.0% 0 / 50 Conditioncond 0.0% 0 / 90 MC/DCmcdc 0.0% 0 / 49 Subroutinesub 0.0% 0 / 9 Podpod n/a totaltotal 0.0% 0 / 347 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::Chart::Surface;
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 class Term::Fabulous::Chart::Surface :strict(params) {
13 use Carp qw(croak);
14 use Term::Fabulous::Render::Attr qw(cell_color_attr);
15 use Term::Fabulous::Unicode qw(grapheme_clusters cluster_columns fit_columns sanitize_text string_columns);
16  
17 # Stored in the cell right of a wide glyph.
18 use constant TAIL => '';
19  
20 # The neighbors a line may be hit from, nearest first.
21 my @NEIGHBORS = ( [ -1, 0 ], [ 1, 0 ], [ 0, -1 ], [ 0, 1 ], [ -1, -1 ], [ 1, -1 ], [ -1, 1 ], [ 1, 1 ] );
22  
23 field $columns :param :reader;
24 field $rows :param :reader;
25 field $background :param :reader = undef; # what an untouched cell shows: 0xRRGGBB or undef
26  
27 # By cell index y * columns + x.
28 field @_glyph; # a grapheme cluster, TAIL, or undef for a space
29 field @_fg;
30 field @_bg;
31 field @_flags; # termbox2 style bits for the foreground (TB_BOLD, ...)
32 field @_stroke_owner;
33 field @_fill_owner;
34 field %_dominant; # by cell index: the color most of a two-color cell shows
35  
36 ADJUST {
37 foreach my $size ( [ columns => $columns ], [ rows => $rows ] ) {
38 croak "Term::Fabulous::Chart::Surface: $size->[0] must be a non-negative integer, got " . ( $size->[1] // 'undef' )
39 unless defined $size->[1] && $size->[1] =~ /\A[0-9]+\z/;
40 }
41 @_bg = ($background) x ( $columns * $rows );
42 }
43  
44 method _index ( $x, $y ) {
45 return undef if $x < 0 || $y < 0 || $x >= $columns || $y >= $rows;
46 return $y * $columns + $x;
47 }
48  
49 method contains ( $x, $y ) {
50 return defined $self->_index( $x, $y ) ? 1 : 0;
51 }
52  
53 method glyph_at ( $x, $y ) {
54 my $index = $self->_index( $x, $y ) // return undef;
55 return $_glyph[$index];
56 }
57  
58 method fg_at ( $x, $y ) {
59 my $index = $self->_index( $x, $y ) // return undef;
60 return $_fg[$index];
61 }
62  
63 method bg_at ( $x, $y ) {
64 my $index = $self->_index( $x, $y ) // return $background;
65 return $_bg[$index];
66 }
67  
68 method flags_at ( $x, $y ) {
69 my $index = $self->_index( $x, $y ) // return 0;
70 return $_flags[$index] // 0;
71 }
72  
73 # A wide glyph cut by a write shows a space in its other cell.
74 method _release ($index) {
75 my $x = $index % $columns;
76 if ( defined $_glyph[$index] && $_glyph[$index] eq TAIL && $x > 0 ) {
77 $_glyph[ $index - 1 ] = ' ';
78 }
79 elsif ( defined $_glyph[$index] && cluster_columns( $_glyph[$index] ) > 1 && $x + 1 < $columns ) {
80 $_glyph[ $index + 1 ] = ' ';
81 }
82 return;
83 }
84  
85 # Sets the background of a rectangle and removes its glyphs.
86 method fill ( $x, $y, $width, $height, $color ) {
87 foreach my $row ( $y .. $y + $height - 1 ) {
88 foreach my $column ( $x .. $x + $width - 1 ) {
89 my $index = $self->_index( $column, $row ) // next;
90 $self->_release($index);
91 delete $_dominant{$index};
92 ( $_glyph[$index], $_fg[$index], $_flags[$index], $_bg[$index] ) = ( undef, undef, 0, $color );
93 }
94 }
95 return $self;
96 }
97  
98 # One glyph one column wide; an undef $bg keeps the cell's background.
99 method put ( $x, $y, $glyph, $fg, $bg = undef, $flags = 0 ) {
100 my $index = $self->_index( $x, $y ) // return $self;
101 $self->_release($index);
102 delete $_dominant{$index};
103 ( $_glyph[$index], $_fg[$index], $_flags[$index] ) = ( $glyph, $fg, $flags );
104 $_bg[$index] = $bg if defined $bg;
105 return $self;
106 }
107  
108 # Writes text from ($x, $y) to the right, at most $options{max}
109 # columns (cut with an ellipsis); returns the columns written.
110 method text ( $x, $y, $text, $fg, %options ) {
111 my ( $bg, $flags, $limit ) = ( $options{bg}, $options{flags} // 0, $options{max} );
112 my $shown = sanitize_text($text);
113 my @clusters = grapheme_clusters( defined $limit ? fit_columns( $shown, $limit ) : $shown );
114  
115 my $column = $x;
116 foreach my $cluster (@clusters) {
117 my $wide = cluster_columns($cluster);
118 last if $column + $wide > $columns;
119 $self->put( $column, $y, $cluster, $fg, $bg, $flags );
120 if ( $wide > 1 ) {
121 my $index = $self->_index( $column + 1, $y );
122 ( $_glyph[$index], $_fg[$index] ) = ( TAIL, $fg ) if defined $index;
123 $_bg[$index] = $bg if defined $index && defined $bg;
124 }
125 $column += $wide;
126 }
127 return $column - $x;
128 }
129  
130 # The columns text takes, as text() counts them.
131 method text_columns :common ($text) {
132 return string_columns( sanitize_text($text) );
133 }
134  
135 # Draws a raster's cells over the surface with its marker, its cell
136 # (0, 0) at ($left, $top); each drawn cell is owned, in $layer
137 # ('stroke' or 'fill'), by the owner most of its subpixels have.
138 #
139 # A block marker splits a cell in two colors; the color most of the
140 # cell shows is remembered, and a Braille pattern drawn over the cell
141 # later (a line along the top of an area) takes it as its background.
142 method composite ( $raster, $layer, $left = 0, $top = 0 ) {
143 my $owners = $layer eq 'stroke' ? \@_stroke_owner : $layer eq 'fill' ? \@_fill_owner : croak "Term::Fabulous::Chart::Surface: unknown layer '$layer'";
144 my $marker = $raster->marker;
145 my $dots = $marker->name eq 'braille';
146 $raster->each_cell(
147 sub ( $x, $y, $colors, $cell_owners, $drawn ) {
148 my $index = $self->_index( $left + $x, $top + $y ) // return;
149 my $under = $dots && exists $_dominant{$index} ? $_dominant{$index} : $_bg[$index];
150 my ( $glyph, $fg, $bg, $dominant ) = $marker->cell( $colors, $under, $drawn ) or return;
151 $self->put( $left + $x, $top + $y, $glyph, $fg, $bg );
152 if ($dots) {
153 $_bg[$index] = $under;
154 delete $_dominant{$index};
155 }
156 else {
157 $_dominant{$index} = $dominant;
158 }
159 my $owner = _most_frequent($cell_owners);
160 $owners->[$index] = $owner if defined $owner;
161 return;
162 }
163 );
164 return $self;
165 }
166  
167 sub _most_frequent ($values) {
168 my ( %count, $best, $most );
169 foreach my $value ( grep { defined } @$values ) {
170 my $seen = ++$count{$value};
171 ( $best, $most ) = ( $value, $seen ) if !defined $most || $seen > $most;
172 }
173 return $best;
174 }
175  
176 method set_owner ( $x, $y, $layer, $owner ) {
177 my $index = $self->_index( $x, $y ) // return $self;
178 ( $layer eq 'stroke' ? \@_stroke_owner : \@_fill_owner )->[$index] = $owner;
179 return $self;
180 }
181  
182 method owner ( $x, $y, $layer ) {
183 my $index = $self->_index( $x, $y ) // return undef;
184 return ( $layer eq 'stroke' ? \@_stroke_owner : \@_fill_owner )->[$index];
185 }
186  
187 # What the pointer at a cell points at: a line or point drawn there, a
188 # fill (bar, area, slice, legend entry) drawn there, or a line in a
189 # neighboring cell, so thin lines are easy to hit.
190 method owner_near ( $x, $y ) {
191 my $index = $self->_index( $x, $y ) // return undef;
192 return $_stroke_owner[$index] // $_fill_owner[$index] // do {
193 my $found;
194 foreach my $offset (@NEIGHBORS) {
195 my $neighbor = $self->_index( $x + $offset->[0], $y + $offset->[1] ) // next;
196 $found = $_stroke_owner[$neighbor] // next;
197 last;
198 }
199 $found;
200 };
201 }
202  
203 # Writes every cell into a canvas of the same size; cells showing the
204 # surface background stay unset, so the canvas's own background shows.
205 method paint ($canvas) {
206 $canvas->clear;
207 foreach my $y ( 0 .. $rows - 1 ) {
208 foreach my $x ( 0 .. $columns - 1 ) {
209 my $index = $y * $columns + $x;
210 my ( $glyph, $bg ) = ( $_glyph[$index], $_bg[$index] );
211 next if defined $glyph && $glyph eq TAIL;
212 my $own_bg = defined $bg && ( !defined $background || $bg != $background ) ? $bg : undef;
213 next if !defined $own_bg && ( !defined $glyph || $glyph eq ' ' );
214 my $fg = defined $_fg[$index] ? cell_color_attr( fg => $_fg[$index] ) | ( $_flags[$index] // 0 ) : undef;
215 $canvas->put_attrs( $x, $y, $glyph // ' ', $fg, cell_color_attr( bg => $own_bg ) );
216 }
217 }
218 return;
219 }
220  
221 # The cells as text, one string per row (for tests and debugging).
222 method lines () {
223 return map {
224 my $y = $_;
225 join '', map { my $glyph = $_glyph[ $y * $columns + $_ ]; !defined $glyph ? ' ' : $glyph eq TAIL ? '' : $glyph } 0 .. $columns - 1;
226 } 0 .. $rows - 1;
227 }
228 }
229  
230 1;
231  
232 __END__