keyboard/altgr: integrate css overiddes in common stylesheet
[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         Ʊ U  ǝ e  Ǝ E  ʌ v  χ X  ɥ h  ʘ O  ɰ mw ɯ mw Ɯ MW ə @ae Ə @AE
20         ɸ PF ʎ yl ɔ co Ɔ CO ɛ 3E ƣ q  Ƣ Q  ∀ A  ∃ E  ∪ u  ∩ n   ≠ !=
21         ≈ =~ ∅ /0 ∘ o  ⋅ .  ∫ s  ≝ =d ″ "  ≤ <  ≥ >  √ rV ∛ 3V  ∜ 4V
22 );
23
24 sub unidecode {
25         return $unaccent{$_[0]} // Text::Unidecode::unidecode($_[0]);
26 }
27
28 sub kbchars ($rows) {
29         return kbmodes({'' => $rows});
30 }
31
32 sub kbmodes ($modes) {
33         my %g; # present group classes
34         my %info = (
35                 tableclass => 'keys big',
36                 rows => [1, 0],
37         );
38         for my $lead (keys %{$modes}) {
39                 if ($lead ne '') {
40                         $info{def}->{''}->{$lead} = "g1 mode$lead";
41                         $g{g1} = 1;
42                         $info{mode}->{$lead} //= "mode $lead";
43                         $info{def}->{$lead}{$lead} = 'g1 mode'; # back
44                 }
45                 while (my ($c, $v) = each %{ $modes->{$lead} }) {
46                         my ($glyph, $title) = $uc->glyph_html($v);
47                         $info{key}{$lead.$c} = join "\n", $glyph, $title;
48
49                         my $class = 'g'.(
50                                   !defined $v || $c eq $v ? 1 # identical
51                                 : $v =~ /\A\p{Mn}+\z/ ? 9 # combining accent
52                                 : NFKD($v) =~ /\Q$c/ ? 2 # decomposed equivalent
53                                 : unidecode($v) =~ /\Q$c\E+/i ? 4 # transliterated
54                                 : $v =~ /\A[\p{Sk}\p{Lm}]+\z/ ? 8 # modifier symbol
55                                 : $v =~ /\A[\pM\pP]+\z/ ? 7 # mark
56                                 : $v =~ /^\p{Latin}/ ? 5 # latin script
57                                 : 6
58                         );
59                         $g{$class} = 1;
60                         $info{def}{$lead}{$c} //= $class;
61                 }
62         }
63         $info{flag} = {%{{
64                 g1 => ['mode' => "switch to an alternate set of keys"],
65                 g2 => ['accented', "decomposes to the original letter with a combining accent"],
66                 g4 => ['similar', "transliterates (mostly) into the unmodified letter"],
67                 g5 => ['latin', "a different (accented) latin letter"],
68                 g6 => ['symbol', "other character not directly deducible from key"],
69                 g7 => ['punctuation', "(punctuation) mark"],
70                 g8 => ['mark', "modifier letter or mark (spacing diacritic)"],
71                 g9 => ['combining', "diacritical mark to be combined with a following character"],
72         }}{keys %g}};
73         return \%info;
74 }
75
76 1;