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