| 1 | package Text::VisualWidth::PP; | ||||||||||||||
| 2 | 10 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 3 | 10 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 10 | ▶ | use 5.008001; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 10 | ▶ | use parent qw(Exporter); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 10 | our $VERSION = '0.09'; | |||||||||||||
| 7 | 10 | our @EXPORT_OK = qw(vwidth vtrim); | |||||||||||||
| 8 | |||||||||||||||
| 9 | # The East_Asian_Width property of perl follows the Unicode version | ||||||||||||||
| 10 | # of the perl in use, while the table of Unicode::EastAsianWidth is | ||||||||||||||
| 11 | # Unicode 12.0 and does not know the characters added since (U+1FAAA | ||||||||||||||
| 12 | # IDENTIFICATION CARD, for example), which it gives no width. Use | ||||||||||||||
| 13 | # the property of perl when it is newer than the table: perl 5.30 is | ||||||||||||||
| 14 | # the first with Unicode 12.1. Older perls use the table. | ||||||||||||||
| 15 | 10 | ▶ | use constant BUILTIN_EAW => $^V ge v5.30; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 16 | |||||||||||||||
| 17 | 10 | ▶ | BEGIN { | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 18 | 10 | unless (BUILTIN_EAW) { | |||||||||||||
| 19 | require Unicode::EastAsianWidth; | ||||||||||||||
| 20 | Unicode::EastAsianWidth->import; | ||||||||||||||
| 21 | } | ||||||||||||||
| 22 | } | ||||||||||||||
| 23 | |||||||||||||||
| 24 | # for backward compatibility, see the POD | ||||||||||||||
| 25 | 10 | ▶ | our $EastAsian = do { no warnings 'once'; $Unicode::EastAsianWidth::EastAsian }; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 26 | |||||||||||||||
| 27 | # Each class as a user-defined property body: the built-in property, | ||||||||||||||
| 28 | # or the table of Unicode::EastAsianWidth. | ||||||||||||||
| 29 | 227 | ▶ | sub EAW { | ||||||||||||
|
Subroutine
EAW: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 30 | 227 | my $class = shift; | |||||||||||||
| 31 | 227 | if (BUILTIN_EAW) { | |||||||||||||
| 32 | 227 | "+utf8::East_Asian_Width=$class\n"; | |||||||||||||
| 33 | } else { | ||||||||||||||
| 34 | 10 | ▶ | no strict 'refs'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 35 | &{"InEastAsian$class"}(); | ||||||||||||||
| 36 | } | ||||||||||||||
| 37 | } | ||||||||||||||
| 38 | |||||||||||||||
| 39 | # The Emoji_Modifier property came to perl in 5.32 with Unicode 13.0. | ||||||||||||||
| 40 | # Spacing refers to it as utf8::Is_Emoji_Modifier: on 5.32 and later | ||||||||||||||
| 41 | # that is the built-in property, and older perls get this user-defined | ||||||||||||||
| 42 | # property of the same name, with the code points as of the Unicode | ||||||||||||||
| 43 | # versions those perls know. | ||||||||||||||
| 44 | 10 | ▶ | if ($^V lt v5.32) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 45 | 10 | ▶ | no strict 'refs'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 46 | 0 | ▶ | *{"utf8::Is_Emoji_Modifier"} = sub { "1F3FB\t1F3FF\n" }; | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 47 | }; | ||||||||||||||
| 48 | |||||||||||||||
| 49 | 79 | ▶ | sub Spacing { | ||||||||||||
|
Subroutine
Spacing: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 50 | 79 | $_[0] . <<END | |||||||||||||
| 51 | -utf8::Nonspacing_Mark | ||||||||||||||
| 52 | -utf8::Enclosing_Mark | ||||||||||||||
| 53 | -utf8::Default_Ignorable_Code_Point | ||||||||||||||
| 54 | -utf8::Is_Emoji_Modifier | ||||||||||||||
| 55 | END | ||||||||||||||
| 56 | } | ||||||||||||||
| 57 | |||||||||||||||
| 58 | 23 | ▶ | sub InVWPP1Fullwidth() { | ||||||||||||
|
Subroutine
InVWPP1Fullwidth: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 59 | 23 | Spacing | |||||||||||||
| 60 | EAW('Fullwidth') . EAW('Wide') . EAW('Ambiguous') | ||||||||||||||
| 61 | } | ||||||||||||||
| 62 | 28 | ▶ | sub InVWPP0Fullwidth() { | ||||||||||||
|
Subroutine
InVWPP0Fullwidth: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 63 | 28 | Spacing | |||||||||||||
| 64 | EAW('Fullwidth') . EAW('Wide') | ||||||||||||||
| 65 | } | ||||||||||||||
| 66 | 10 | ▶ | sub InVWPP1Halfwidth() { | ||||||||||||
|
Subroutine
InVWPP1Halfwidth: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 67 | 10 | Spacing | |||||||||||||
| 68 | EAW('Halfwidth') . EAW('Narrow') . EAW('Neutral') | ||||||||||||||
| 69 | } | ||||||||||||||
| 70 | 18 | ▶ | sub InVWPP0Halfwidth() { | ||||||||||||
|
Subroutine
InVWPP0Halfwidth: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 71 | 18 | Spacing | |||||||||||||
| 72 | EAW('Halfwidth') . EAW('Narrow') . EAW('Neutral') . EAW('Ambiguous') | ||||||||||||||
| 73 | } | ||||||||||||||
| 74 | |||||||||||||||
| 75 | 12 | ▶ | sub vwidth { width(@_) } | ||||||||||||
|
Subroutine
vwidth: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 76 | 0 | ▶ | sub vtrim { trim(@_) } | ||||||||||||
|
Subroutine
vtrim: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 77 | |||||||||||||||
| 78 | # E_Modifier not following E_Base has full-width visual representation | ||||||||||||||
| 79 | 10 | ▶ | my $EM = $^V ge v5.28 ? '(?>(?<!\p{Line_Break=E_Base})\p{Line_Break=E_Modifier})' | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 80 | : '(?!)' ; | ||||||||||||||
| 81 | 10 | $EM = qr/$EM/; # to avoid error on v5.24 | |||||||||||||
| 82 | |||||||||||||||
| 83 | # Each character counts by its class, full or half width, except one | ||||||||||||||
| 84 | # after ZERO WIDTH JOINER (U+200D): it joins the one before it in an | ||||||||||||||
| 85 | # emoji sequence and adds no width. | ||||||||||||||
| 86 | # | ||||||||||||||
| 87 | # Compiled once. With $EM interpolated next to \N{U+200D}, perl parses | ||||||||||||||
| 88 | # the pattern on every match before it finds that it has not changed, | ||||||||||||||
| 89 | # which for a short string takes several times longer than the match. | ||||||||||||||
| 90 | my @WIDTH_RE = map { | ||||||||||||||
| 91 | 10 | qr/((?:$EM|(?<!\N{U+200D})\p{InVWPP${_}Fullwidth})+) | |||||||||||||
| 92 | |((?: (?<!\N{U+200D})\p{InVWPP${_}Halfwidth})+)/x | ||||||||||||||
| 93 | } 0, 1; | ||||||||||||||
| 94 | |||||||||||||||
| 95 | 61 | ▶ | sub width { | ||||||||||||
|
Subroutine
width: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||
| 96 | 61 | my $str = shift; | |||||||||||||
| 97 | 61 | ▶ | my $re = $WIDTH_RE[$EastAsian ? 1 : 0]; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 98 | 61 | my $ret = 0; | |||||||||||||
| 99 | 61 | ▶ | while ($str =~ /$re/g) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 100 | 66 | ▶ | $ret += $1 ? length($1) * 2 : length($2) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 101 | } | ||||||||||||||
| 102 | 61 | $ret; | |||||||||||||
| 103 | } | ||||||||||||||
| 104 | |||||||||||||||
| 105 | 24 | ▶ | sub trim { | ||||||||||||
|
Subroutine
trim: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||||
| 106 | 24 | my ($str, $limit) = @_; | |||||||||||||
| 107 | |||||||||||||||
| 108 | 24 | my $cnt = 0; | |||||||||||||
| 109 | 24 | my $ret = ''; | |||||||||||||
| 110 | 24 | ▶ | my $fullwidth = $EastAsian ? qr/\p{InVWPP1Fullwidth}/ : qr/\p{InVWPP0Fullwidth}/; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 111 | 24 | ▶ | while ($str =~ /\G(\X)/g) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 112 | 100 | my $ch = $1; | |||||||||||||
| 113 | 100 | my $w = do { | |||||||||||||
| 114 | 100 | ▶ | if ($ch =~ /\A$fullwidth\z/) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 115 | 17 | 2; | |||||||||||||
| 116 | } elsif (length($ch) == 1) { | ||||||||||||||
| 117 | 56 | 1; | |||||||||||||
| 118 | } else { | ||||||||||||||
| 119 | 27 | width($ch); | |||||||||||||
| 120 | } | ||||||||||||||
| 121 | }; | ||||||||||||||
| 122 | 100 | ▶ | if ($cnt+$w <= $limit) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 123 | 80 | $ret .= $ch; | |||||||||||||
| 124 | 80 | $cnt += $w; | |||||||||||||
| 125 | } else { | ||||||||||||||
| 126 | 20 | last; | |||||||||||||
| 127 | } | ||||||||||||||
| 128 | } | ||||||||||||||
| 129 | 24 | $ret; | |||||||||||||
| 130 | } | ||||||||||||||
| 131 | |||||||||||||||
| 132 | 10 | 1; | |||||||||||||
| 133 | __END__ | ||||||||||||||