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