« blib/lib/Term/Fabulous/Role/CanParseLayout.pm
↑ Summary
blib/lib/Term/Fabulous/Widget/ScrollBox.pm »
| 1 | package Term::Fabulous::Unicode; | ||||||||||
| 2 | |||||||||||
| 3 | 4 | ▶ | use v5.32; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 4 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 4 | ▶ | use feature 'signatures'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 4 | ▶ | no warnings 'experimental::signatures'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 4 | our $VERSION = '0.02'; | |||||||||
| 9 | |||||||||||
| 10 | 4 | ▶ | use Exporter 'import'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 4 | our @EXPORT_OK = qw(sanitize_text grapheme_clusters cluster_columns string_columns terminal_is_utf8); | |||||||||
| 12 | |||||||||||
| 13 | 4 | ▶ | use I18N::Langinfo qw(langinfo CODESET); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 4 | ▶ | use Term::Fabulous::Termbox qw(tb_cluster_width); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 4 | ▶ | use Unicode::GCString; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | |||||||||||
| 17 | 4 | ▶ | use constant CACHE_LIMIT => 4096; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | |||||||||||
| 19 | # The same texts are segmented and measured on every frame (layout | ||||||||||
| 20 | # callbacks, text and canvas drawing), so each result is remembered per | ||||||||||
| 21 | # input string. A cache is emptied once it reaches CACHE_LIMIT entries. | ||||||||||
| 22 | 4 | my ( %clusters_by_text, %columns_by_cluster, %columns_by_text ); | |||||||||
| 23 | |||||||||||
| 24 | 15 | ▶ | sub sanitize_text ($text) { | ||||||||
|
Subroutine
sanitize_text: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 25 | 15 | ▶ | die "Term::Fabulous::Unicode: sanitize_text needs well-formed UTF-8" unless utf8::valid($text); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 26 | 13 | return $text =~ tr/\t\x00-\x08\x0A-\x1F\x7F-\x9F/ \x{FFFD}/r; | |||||||||
| 27 | } | ||||||||||
| 28 | |||||||||||
| 29 | 14 | ▶ | sub grapheme_clusters ($text) { | ||||||||
|
Subroutine
grapheme_clusters: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 30 | 14 | my $clusters = $clusters_by_text{$text}; | |||||||||
| 31 | 14 | ▶ | return @$clusters if defined $clusters; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 32 | |||||||||||
| 33 | 10 | ▶ | %clusters_by_text = () if keys(%clusters_by_text) >= CACHE_LIMIT; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 34 | 10 | $clusters = $clusters_by_text{$text} = [ map { $_->as_string } @{ Unicode::GCString->new( sanitize_text($text) )->as_arrayref } ]; | |||||||||
| 35 | 9 | return @$clusters; | |||||||||
| 36 | } | ||||||||||
| 37 | |||||||||||
| 38 | 50 | ▶ | sub cluster_columns ($cluster) { | ||||||||
|
Subroutine
cluster_columns: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 39 | 50 | my $columns = $columns_by_cluster{$cluster}; | |||||||||
| 40 | 50 | ▶ | return $columns if defined $columns; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 41 | |||||||||||
| 42 | 32 | ▶ | die "Term::Fabulous::Unicode: cluster_columns needs a non-empty cluster" unless length $cluster; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 43 | 31 | ▶ | %columns_by_cluster = () if keys(%columns_by_cluster) >= CACHE_LIMIT; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 44 | 31 | return $columns_by_cluster{$cluster} = _terminal_columns($cluster); | |||||||||
| 45 | } | ||||||||||
| 46 | |||||||||||
| 47 | 12 | ▶ | sub string_columns ($text) { | ||||||||
|
Subroutine
string_columns: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 48 | 12 | my $columns = $columns_by_text{$text}; | |||||||||
| 49 | 12 | ▶ | return $columns if defined $columns; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 50 | |||||||||||
| 51 | 8 | ▶ | %columns_by_text = () if keys(%columns_by_text) >= CACHE_LIMIT; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | 8 | $columns = 0; | |||||||||
| 53 | 8 | $columns += cluster_columns($_) foreach grapheme_clusters($text); | |||||||||
| 54 | 8 | return $columns_by_text{$text} = $columns; | |||||||||
| 55 | } | ||||||||||
| 56 | |||||||||||
| 57 | 0 | ▶ | sub terminal_is_utf8 () { | ||||||||
|
Subroutine
terminal_is_utf8: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 58 | 0 | ▶ | return langinfo(CODESET) =~ /\Autf-?8\z/i ? 1 : 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | } | ||||||||||
| 60 | |||||||||||
| 61 | # Mirrors termbox2's tb_present(): a cluster advances by tb_cluster_width() | ||||||||||
| 62 | # (its widest codepoint, forced to 1 by a text presentation selector and | ||||||||||
| 63 | # to 2 by an emoji presentation selector, a zero-width joiner or a pair of | ||||||||||
| 64 | # regional indicators), and any result below 1 advances by one column. | ||||||||||
| 65 | 31 | ▶ | sub _terminal_columns ($cluster) { | ||||||||
|
Subroutine
_terminal_columns: called CC=2 SCAR=6.9
| |||||||||||
| 66 | 31 | my $width = tb_cluster_width($cluster); | |||||||||
| 67 | 31 | ▶ | return $width < 1 ? 1 : $width; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | } | ||||||||||
| 69 | |||||||||||
| 70 | 4 | 1; | |||||||||
| 71 | |||||||||||
| 72 | __END__ | ||||||||||