keyboard/altgr: classify lookalike symbols as transliterated
[sheet.git] / Shiar_Sheet / KeyboardChars.pm
1 package Shiar_Sheet::KeyboardChars;
2
3 use 5.020;
4 use warnings;
5 use experimental 'signatures';
6 use parent 'Exporter';
7 use Unicode::Normalize qw( NFKD );
8 use Text::Unidecode qw( unidecode );
9 use Shiar_Sheet::FormatChar;
10
11 our $VERSION = '1.01';
12 our @EXPORT = qw( kbchars kbmodes );
13
14 my $uc = Shiar_Sheet::FormatChar->new;
15
16 sub kbchars ($rows) {
17         return kbmodes({'' => $rows});
18 }
19
20 sub kbmodes ($modes) {
21         my %g; # present group classes
22         my %info = (
23                 rows => [1, 0],
24         );
25         for my $lead (keys %{$modes}) {
26                 if ($lead ne '') {
27                         $info{def}->{''}->{$lead} = "g1 mode$lead";
28                         $g{g1} = 1;
29                         $info{mode}->{$lead} //= "mode $lead";
30                         $info{def}->{$lead}{$lead} = 'g1 mode'; # back
31                 }
32                 while (my ($c, $v) = each %{ $modes->{$lead} }) {
33                         my ($glyph, $title) = $uc->glyph_html($v);
34                         $info{key}{$lead.$c} = join "\n", $glyph, $title;
35
36                         my $class = 'g'.(
37                                   !defined $v || $c eq $v ? 1 # identical
38                                 : $v =~ /\A\p{Mn}+\z/ ? 9 # combining accent
39                                 : NFKD($v) =~ /\Q$c/ ? 2 # decomposed equivalent
40                                 : unidecode($v) =~ /\Q$c\E+/i ? 4 # transliterated
41                                 : $v =~ /\A[\p{Sk}\p{Lm}]+\z/ ? 8 # modifier symbol
42                                 : $v =~ /\A[\pM\pP]+\z/ ? 7 # mark
43                                 : $v =~ /^\p{Latin}/ ? 5 # latin script
44                                 : 6
45                         );
46                         $g{$class} = 1;
47                         $info{def}{$lead}{$c} //= $class;
48                 }
49         }
50         $info{flag} = {%{{
51                 g1 => ['mode' => "switch to an alternate set of keys"],
52                 g2 => ['accented', "decomposes to the original letter with a combining accent"],
53                 g4 => ['similar', "transliterates (mostly) into the unmodified letter"],
54                 g5 => ['latin', "a different (accented) latin letter"],
55                 g6 => ['symbol', "other character not directly deducible from key"],
56                 g7 => ['punctuation', "(punctuation) mark"],
57                 g8 => ['mark', "modifier letter or mark (spacing diacritic)"],
58                 g9 => ['combining', "diacritical mark to be combined with a following character"],
59         }}{keys %g}};
60         return \%info;
61 }
62
63 1;