keyboard/altgr: proper function to convert char maps
[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         my %g; # present group classes
12         return {
13         rows => [1, 0],
14         def => {
15                 '' => {
16                         map {
17                                 my $v = $rows->{$_};
18                                 my $class = 'g'.(
19                                           !defined $v || $_ eq $v ? 1 # identical
20                                         : $v =~ /\A\p{Mn}+\z/ ? 9 # combining accent
21                                         : $v =~ /\A[\p{Sk}\p{Lm}]+\z/ ? 8 # modifier symbol
22                                         : $v =~ /\A[\pM\pP]+\z/ ? 7 # mark
23                                         : NFKD($v) =~ /\Q$_/ ? 2 # decomposed equivalent
24                                         : unidecode($v) =~ /\Q$_\E+/i ? 4 # transliterated
25                                         : $v =~ /^\p{Latin}/ ? 5 # latin script
26                                         : 6
27                                 );
28                                 $g{$class} = 1;
29                                 $_ => $class
30                         } keys %{$rows}
31                 },
32         },
33         key => {
34                 map {
35                         my ($glyph, $title) = $uc->glyph_html($rows->{$_});
36                         $_ => join "\n", $glyph, $title
37                 } keys %{$rows}
38         },
39         flag => {%{{
40                 g2 => ['accented', "decomposes to the original letter with a combining accent"],
41                 g4 => ['similar', "transliterates (mostly) into the unmodified letter"],
42                 g5 => ['latin', "a different (accented) latin letter"],
43                 g6 => ['symbol', "other character not directly deducible from key"],
44                 g7 => ['punctuation', "(punctuation) mark"],
45                 g8 => ['mark', "modifier letter or mark (spacing diacritic)"],
46                 g9 => ['combining', "diacritical mark to be combined with a following character"],
47         }}{keys %g}},
48         };
49 }
50
51 1;