| 1 | package Term::Fabulous::Render::Text; | ||
| 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 | our $VERSION = '0.05'; | ||
| 9 | |||
| 10 | 1 | ▶ | use Object::Pad 0.825; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | role Term::Fabulous::Render::Text { | ||
| 13 | 1 | ▶ | use List::Util qw(min); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | 1 | ▶ | use Term::Fabulous::Termbox qw(TB_DEFAULT); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 15 | 1 | ▶ | use Term::Fabulous::Render::Attr qw(color_attr clay_color); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 16 | 1 | ▶ | use Term::Fabulous::Render::Geometry qw(cell_rect); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 17 | 0 | ▶ | use Term::Fabulous::Text::Style qw(apply_style); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 18 | use Term::Fabulous::Unicode qw(grapheme_clusters cluster_columns); | ||
| 19 | |||
| 20 | use constant CLUSTER_CACHE_LIMIT => 4096; | ||
| 21 | |||
| 22 | # Text contents rarely change between frames: memoize the sanitized | ||
| 23 | # clusters and their widths per string. | ||
| 24 | my %clusters_by_text; | ||
| 25 | |||
| 26 | sub _clusters_with_columns ($text) { | ||
| 27 | my $clusters = $clusters_by_text{$text}; | ||
| 28 | return $clusters if defined $clusters; | ||
| 29 | |||
| 30 | %clusters_by_text = () if keys(%clusters_by_text) >= CLUSTER_CACHE_LIMIT; | ||
| 31 | return $clusters_by_text{$text} = [ map { [ $_, cluster_columns($_) ] } grapheme_clusters($text) ]; | ||
| 32 | } | ||
| 33 | |||
| 34 | # The cells the command being painted may touch (Term::Fabulous::Render). | ||
| 35 | method clip_rect; | ||
| 36 | |||
| 37 | # The cell target the frame is painted into (Term::Fabulous::Render). | ||
| 38 | method cell_target; | ||
| 39 | |||
| 40 | # Draws one line of text from the top-left cell of its bounding box. | ||
| 41 | # Clusters are sanitized (no control characters reach the terminal) and | ||
| 42 | # advance by the same widths the measure callback reported. Drawing stops | ||
| 43 | # before a cluster that would cross the box's right edge or the clip | ||
| 44 | # rect; clusters left of the clip rect are skipped but still advance. | ||
| 45 | # A widget with line_styles (a Text) paints the line in runs: each | ||
| 46 | # cluster takes the look of the run its first character lies in. A run | ||
| 47 | # is [ $characters, $set, $clear, $fg_attr, $bg_attr ] (see | ||
| 48 | # Term::Fabulous::Widget::Text::_runs_of_text): a style whose | ||
| 49 | # colors are termbox attributes, applied onto the widget's own look. | ||
| 50 | method render_text ( $command, $widget, $buffer ) { | ||
| 51 | my ( $x, $y, $x1 ) = cell_rect( $command->{boundingBox} ); | ||
| 52 | my ( $clip_x0, $clip_y0, $clip_x1, $clip_y1 ) = @{ $self->clip_rect }; | ||
| 53 | return if $y < $clip_y0 || $y >= $clip_y1; | ||
| 54 | |||
| 55 | my $right_limit = min( $x1, $clip_x1 ); | ||
| 56 | my $data = $command->{renderData}; | ||
| 57 | my $color_attr = color_attr( clay_color( $data->{textColor} ) ); | ||
| 58 | my $style_bits = defined $widget && $widget->can('style_attrs') ? $widget->style_attrs : 0; | ||
| 59 | my $runs = defined $widget && $widget->can('line_styles') ? $widget->line_styles( $data->{stringOffset} // 0, length $data->{stringContents} ) : []; | ||
| 60 | my $row = $buffer->[$y] //= []; | ||
| 61 | my $target = $self->cell_target; | ||
| 62 | |||
| 63 | my $fg_attr = $color_attr | $style_bits; | ||
| 64 | my $run_bg_attr; | ||
| 65 | my ( $run_index, $left_in_run ) = ( -1, 0 ); | ||
| 66 | foreach my $cluster_with_columns ( @{ _clusters_with_columns( $data->{stringContents} ) } ) { | ||
| 67 | my ( $cluster, $columns ) = @$cluster_with_columns; | ||
| 68 | last if $x + $columns > $right_limit; | ||
| 69 | |||
| 70 | while ( $left_in_run <= 0 && $run_index < $#$runs ) { | ||
| 71 | my ( $characters, $set, $clear, $run_fg_attr, $bg_attr ) = @{ $runs->[ ++$run_index ] }; | ||
| 72 | my $look = apply_style( { attrs => $style_bits, color => $color_attr, background => undef }, { set => $set, clear => $clear, color => $run_fg_attr, background => $bg_attr } ); | ||
| 73 | $left_in_run = $characters; | ||
| 74 | $fg_attr = $look->{color} | $look->{attrs}; | ||
| 75 | $run_bg_attr = $look->{background}; | ||
| 76 | } | ||
| 77 | $left_in_run -= length $cluster; | ||
| 78 | |||
| 79 | if ( $x >= $clip_x0 ) { | ||
| 80 | my $bg_attr = $run_bg_attr // $row->[$x] // TB_DEFAULT; | ||
| 81 | my ( $base, @extenders ) = split //, $cluster; | ||
| 82 | $target->set_cell( $x, $y, $base, $fg_attr, $bg_attr ); | ||
| 83 | $target->extend_cell( $x, $y, $_ ) foreach @extenders; | ||
| 84 | $row->[$_] = $bg_attr foreach $x .. $x + $columns - 1; | ||
| 85 | } | ||
| 86 | $x += $columns; | ||
| 87 | } | ||
| 88 | return; | ||
| 89 | } | ||
| 90 | } | ||
| 91 | |||
| 92 | 1; | ||
| 93 | |||
| 94 | __END__ | ||