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

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