« blib/lib/Term/Fabulous/Widget/Table/Filter.pm
↑ Summary
blib/lib/Term/Fabulous/Chart/Transform.pm »
| 1 | package Term::Fabulous::Chart::Raster; | ||
| 2 | |||
| 3 | 2 | ▶ | use v5.32; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 2 | ▶ | use feature 'signatures'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 2 | ▶ | no warnings 'experimental::signatures'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | our $VERSION = '0.05'; | ||
| 9 | |||
| 10 | 2 | ▶ | use Object::Pad 0.825; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | 2 | ▶ | use Term::Fabulous::Chart::Marker; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | |||
| 14 | class Term::Fabulous::Chart::Raster :strict(params) { | ||
| 15 | 2 | ▶ | use Carp qw(croak); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 16 | 2 | ▶ | use List::Util qw(max min); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 17 | 2 | ▶ | use POSIX qw(ceil floor); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 18 | 0 | ▶ | use Term::Fabulous::Chart::Palette qw(mix_rgb); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 19 | use Term::Fabulous::Render::Geometry qw(line_steps); | ||
| 20 | |||
| 21 | field $marker :param :reader; | ||
| 22 | field $columns :param :reader; # in cells | ||
| 23 | field $rows :param :reader; | ||
| 24 | |||
| 25 | field $width :reader; # in subpixels | ||
| 26 | field $height :reader; | ||
| 27 | field $_cell_columns; # subpixels per cell | ||
| 28 | field $_cell_rows; | ||
| 29 | |||
| 30 | field @_color; # by subpixel index y * width + x: packed 0xRRGGBB or undef | ||
| 31 | field @_owner; # by subpixel index: whatever the caller tags it with | ||
| 32 | field @_drawn; # by subpixel index: when it was drawn (a growing count) | ||
| 33 | field $_count = 0; | ||
| 34 | field @_touched_cells; # cell indexes with drawn subpixels, in drawing order | ||
| 35 | field %_is_touched; | ||
| 36 | |||
| 37 | # Where the dash pattern of a line continues; see start_pattern. | ||
| 38 | field $_pattern; | ||
| 39 | field $_pattern_step = 0; | ||
| 40 | |||
| 41 | ADJUST { | ||
| 42 | croak "Term::Fabulous::Chart::Raster: marker must be a Term::Fabulous::Chart::Marker" | ||
| 43 | unless ref $marker && $marker->isa('Term::Fabulous::Chart::Marker'); | ||
| 44 | foreach my $size ( [ columns => $columns ], [ rows => $rows ] ) { | ||
| 45 | croak "Term::Fabulous::Chart::Raster: $size->[0] must be a non-negative integer, got " . ( $size->[1] // 'undef' ) | ||
| 46 | unless defined $size->[1] && $size->[1] =~ /\A[0-9]+\z/; | ||
| 47 | } | ||
| 48 | ( $_cell_columns, $_cell_rows ) = ( $marker->columns, $marker->rows ); | ||
| 49 | ( $width, $height ) = ( $columns * $_cell_columns, $rows * $_cell_rows ); | ||
| 50 | } | ||
| 51 | |||
| 52 | # --------------------------------------------------------------------- | ||
| 53 | # Subpixels | ||
| 54 | # --------------------------------------------------------------------- | ||
| 55 | |||
| 56 | method _touch ( $x, $y ) { | ||
| 57 | my $cell = int( $y / $_cell_rows ) * $columns + int( $x / $_cell_columns ); | ||
| 58 | push @_touched_cells, $cell unless $_is_touched{$cell}++; | ||
| 59 | return; | ||
| 60 | } | ||
| 61 | |||
| 62 | # Colors one subpixel (whole coordinates); outside the raster nothing | ||
| 63 | # happens. | ||
| 64 | method set ( $x, $y, $color, $owner = undef ) { | ||
| 65 | return if $x < 0 || $y < 0 || $x >= $width || $y >= $height; | ||
| 66 | my $index = $y * $width + $x; | ||
| 67 | ( $_color[$index], $_owner[$index], $_drawn[$index] ) = ( $color, $owner, ++$_count ); | ||
| 68 | $self->_touch( $x, $y ); | ||
| 69 | return; | ||
| 70 | } | ||
| 71 | |||
| 72 | # Mixes $opacity of $color into a subpixel: into its color when it has | ||
| 73 | # one, else into $base. | ||
| 74 | method blend ( $x, $y, $color, $opacity, $base, $owner = undef ) { | ||
| 75 | return if $x < 0 || $y < 0 || $x >= $width || $y >= $height; | ||
| 76 | my $index = $y * $width + $x; | ||
| 77 | $_color[$index] = $opacity >= 1 ? $color : mix_rgb( $_color[$index] // $base, $color, $opacity ); | ||
| 78 | $_owner[$index] = $owner; | ||
| 79 | $_drawn[$index] = ++$_count; | ||
| 80 | $self->_touch( $x, $y ); | ||
| 81 | return; | ||
| 82 | } | ||
| 83 | |||
| 84 | method color_at ( $x, $y ) { | ||
| 85 | return undef if $x < 0 || $y < 0 || $x >= $width || $y >= $height; | ||
| 86 | return $_color[ $y * $width + $x ]; | ||
| 87 | } | ||
| 88 | |||
| 89 | method owner_at ( $x, $y ) { | ||
| 90 | return undef if $x < 0 || $y < 0 || $x >= $width || $y >= $height; | ||
| 91 | return $_owner[ $y * $width + $x ]; | ||
| 92 | } | ||
| 93 | |||
| 94 | # --------------------------------------------------------------------- | ||
| 95 | # Lines | ||
| 96 | # --------------------------------------------------------------------- | ||
| 97 | |||
| 98 | # The dash pattern of the lines drawn next: an array of run lengths in | ||
| 99 | # subpixel steps, alternately drawn and skipped ([3, 2]: three on, two | ||
| 100 | # off), or undef for solid lines. The pattern runs on from line to line, | ||
| 101 | # so a polyline drawn as segments keeps an even rhythm. | ||
| 102 | method start_pattern ($pattern) { | ||
| 103 | $_pattern = defined $pattern && @$pattern ? [@$pattern] : undef; | ||
| 104 | $_pattern_step = 0; | ||
| 105 | return $self; | ||
| 106 | } | ||
| 107 | |||
| 108 | method _pattern_allows () { | ||
| 109 | return 1 unless $_pattern; | ||
| 110 | my $period = 0; | ||
| 111 | $period += $_ foreach @$_pattern; | ||
| 112 | my $phase = $_pattern_step++ % $period; | ||
| 113 | foreach my $index ( 0 .. $#$_pattern ) { | ||
| 114 | return $index % 2 == 0 ? 1 : 0 if $phase < $_pattern->[$index]; | ||
| 115 | $phase -= $_pattern->[$index]; | ||
| 116 | } | ||
| 117 | return 1; | ||
| 118 | } | ||
| 119 | |||
| 120 | # Every subpixel the line between two points passes (the subpixels | ||
| 121 | # that contain the points included); coordinates are continuous, in | ||
| 122 | # subpixels. With $opacity below 1 the line is blended into what is | ||
| 123 | # there (or $base). | ||
| 124 | method line ( $x0, $y0, $x1, $y1, $color, $owner = undef, $opacity = 1, $base = undef ) { | ||
| 125 | my ( $first, @pixels ) = line_steps( ( map { floor($_) } $x0, $y0, $x1, $y1 ), $width, $height ); | ||
| 126 | $_pattern_step += $first if $_pattern; # the steps before the raster count for the dashes | ||
| 127 | foreach my $pixel (@pixels) { | ||
| 128 | next unless $self->_pattern_allows; | ||
| 129 | $opacity >= 1 ? $self->set( @$pixel, $color, $owner ) : $self->blend( @$pixel, $color, $opacity, $base, $owner ); | ||
| 130 | } | ||
| 131 | return $self; | ||
| 132 | } | ||
| 133 | |||
| 134 | # --------------------------------------------------------------------- | ||
| 135 | # Fills: the subpixels whose centers lie inside the shape | ||
| 136 | # --------------------------------------------------------------------- | ||
| 137 | |||
| 138 | # The whole subpixel indexes whose centers lie in [from, to). | ||
| 139 | sub _covered ( $from, $to, $limit ) { | ||
| 140 | return () if $to <= $from; | ||
| 141 | return ( max( 0, ceil( $from - 0.5 ) ), min( $limit, ceil( $to - 0.5 ) ) - 1 ); | ||
| 142 | } | ||
| 143 | |||
| 144 | method fill_rect ( $x0, $y0, $x1, $y1, $color, $owner = undef, $opacity = 1, $base = undef ) { | ||
| 145 | ( $x0, $x1 ) = ( $x1, $x0 ) if $x1 < $x0; | ||
| 146 | ( $y0, $y1 ) = ( $y1, $y0 ) if $y1 < $y0; | ||
| 147 | my ( $left, $right ) = _covered( $x0, $x1, $width ) or return $self; | ||
| 148 | my ( $top, $bottom ) = _covered( $y0, $y1, $height ) or return $self; | ||
| 149 | foreach my $y ( $top .. $bottom ) { | ||
| 150 | foreach my $x ( $left .. $right ) { | ||
| 151 | $opacity >= 1 ? $self->set( $x, $y, $color, $owner ) : $self->blend( $x, $y, $color, $opacity, $base, $owner ); | ||
| 152 | } | ||
| 153 | } | ||
| 154 | return $self; | ||
| 155 | } | ||
| 156 | |||
| 157 | # The subpixels of column $x from $top to $bottom (continuous, either | ||
| 158 | # order). | ||
| 159 | method fill_column ( $x, $top, $bottom, $color, $owner = undef, $opacity = 1, $base = undef ) { | ||
| 160 | return $self if $x < 0 || $x >= $width; | ||
| 161 | ( $top, $bottom ) = ( $bottom, $top ) if $bottom < $top; | ||
| 162 | my ( $first, $last ) = _covered( $top, $bottom, $height ) or return $self; | ||
| 163 | foreach my $y ( $first .. $last ) { | ||
| 164 | $opacity >= 1 ? $self->set( $x, $y, $color, $owner ) : $self->blend( $x, $y, $color, $opacity, $base, $owner ); | ||
| 165 | } | ||
| 166 | return $self; | ||
| 167 | } | ||
| 168 | |||
| 169 | # A polygon ([x, y] corners, continuous), filled by the even-odd rule. | ||
| 170 | method fill_polygon ( $corners, $color, $owner = undef, $opacity = 1, $base = undef ) { | ||
| 171 | return $self if @$corners < 3; | ||
| 172 | my @ys = map { $_->[1] } @$corners; | ||
| 173 | my ( $top, $bottom ) = _covered( min(@ys), max(@ys), $height ) or return $self; | ||
| 174 | foreach my $y ( $top .. $bottom ) { | ||
| 175 | my $center = $y + 0.5; | ||
| 176 | my @crossings; | ||
| 177 | foreach my $index ( 0 .. $#$corners ) { | ||
| 178 | my ( $from, $to ) = ( $corners->[$index], $corners->[ ( $index + 1 ) % @$corners ] ); | ||
| 179 | my ( $low, $high ) = $from->[1] <= $to->[1] ? ( $from, $to ) : ( $to, $from ); | ||
| 180 | next unless $center >= $low->[1] && $center < $high->[1]; | ||
| 181 | push @crossings, $low->[0] + ( $center - $low->[1] ) * ( $high->[0] - $low->[0] ) / ( $high->[1] - $low->[1] ); | ||
| 182 | } | ||
| 183 | @crossings = sort { $a <=> $b } @crossings; | ||
| 184 | while ( my ( $enter, $leave ) = splice @crossings, 0, 2 ) { | ||
| 185 | last unless defined $leave; | ||
| 186 | my ( $left, $right ) = _covered( $enter, $leave, $width ) or next; | ||
| 187 | foreach my $x ( $left .. $right ) { | ||
| 188 | $opacity >= 1 ? $self->set( $x, $y, $color, $owner ) : $self->blend( $x, $y, $color, $opacity, $base, $owner ); | ||
| 189 | } | ||
| 190 | } | ||
| 191 | } | ||
| 192 | return $self; | ||
| 193 | } | ||
| 194 | |||
| 195 | # Calls $paint->($x, $y) for every subpixel of the area [x0, x1) x | ||
| 196 | # [y0, y1) (whole numbers, clipped); it returns ( $color, $owner ) to | ||
| 197 | # draw the subpixel, or the empty list to leave it. | ||
| 198 | method paint_area ( $x0, $y0, $x1, $y1, $paint ) { | ||
| 199 | foreach my $y ( max( 0, $y0 ) .. min( $height, $y1 ) - 1 ) { | ||
| 200 | foreach my $x ( max( 0, $x0 ) .. min( $width, $x1 ) - 1 ) { | ||
| 201 | my ( $color, $owner ) = $paint->( $x, $y ) or next; | ||
| 202 | $self->set( $x, $y, $color, $owner ); | ||
| 203 | } | ||
| 204 | } | ||
| 205 | return $self; | ||
| 206 | } | ||
| 207 | |||
| 208 | # --------------------------------------------------------------------- | ||
| 209 | # Cells | ||
| 210 | # --------------------------------------------------------------------- | ||
| 211 | |||
| 212 | # Every cell with drawn subpixels, in the order they were first drawn: | ||
| 213 | # ( $cell_x, $cell_y, \@colors, \@owners, \@drawn ), the subpixels row | ||
| 214 | # by row; @drawn tells when each was drawn (later is larger). | ||
| 215 | method each_cell ($visit) { | ||
| 216 | foreach my $cell (@_touched_cells) { | ||
| 217 | my ( $cell_y, $cell_x ) = ( int( $cell / $columns ), $cell % $columns ); | ||
| 218 | my ( @colors, @owners, @drawn ); | ||
| 219 | foreach my $row ( 0 .. $_cell_rows - 1 ) { | ||
| 220 | my $start = ( $cell_y * $_cell_rows + $row ) * $width + $cell_x * $_cell_columns; | ||
| 221 | push @colors, @_color[ $start .. $start + $_cell_columns - 1 ]; | ||
| 222 | push @owners, @_owner[ $start .. $start + $_cell_columns - 1 ]; | ||
| 223 | push @drawn, @_drawn[ $start .. $start + $_cell_columns - 1 ]; | ||
| 224 | } | ||
| 225 | $visit->( $cell_x, $cell_y, \@colors, \@owners, \@drawn ); | ||
| 226 | } | ||
| 227 | return; | ||
| 228 | } | ||
| 229 | |||
| 230 | method is_empty () { | ||
| 231 | return @_touched_cells ? 0 : 1; | ||
| 232 | } | ||
| 233 | } | ||
| 234 | |||
| 235 | 1; | ||
| 236 | |||
| 237 | __END__ | ||