keyboard/altgr: mkcharinfo from updated include structure
[sheet.git] / tools / mkcharinfo
1 #!/usr/bin/env perl
2 use 5.014;
3 use warnings;
4 use utf8;
5 no if $] >= 5.018, warnings => 'experimental::smartmatch';
6 use lib '.';
7
8 use open OUT => ':encoding(utf-8)', ':std';
9 use Data::Dump 'pp';
10
11 our $VERSION = '1.03';
12
13 my %info = (
14         # prepare presentational string for some control(lish) entries
15         "\xAD"     => {string => '-'},
16         "\x{200E}" => {string => '→'},
17         "\x{200F}" => {string => '←'},
18         "\x{200B}" => {string => '␣'}, # nbsp: ~ in TeX
19         "\x{200C}" => {string => '|'}, # ISO-9995-7-081 lookalike (alt: ∣ ⊺ ⟙)
20         "\x{200D}" => {string => '⁀'}, # join (alt: ∤ |ͯ ⨝)
21 );
22 $info{chr $_} //= {} for 32 .. 126;
23
24 eval {
25         my $tables = do './unicode-table.inc.pl' or die $@ || $!;
26         for (values %$tables) {
27                 for (values %$_) {
28                         for (@$_) {
29                                 length $_ == 1 or next;  # ignore meta values
30                                 s/\\//;  # unescape
31                                 $info{$_} //= {};
32                         }
33                 }
34         }
35         1;
36 } or warn "Failed reading unicode tables: $@";
37
38 eval {
39         my $kbd = do './keyboard/altgr/macos-abc.eng.inc.pl' or die $@ || $!;
40         $info{$_} //= {} for map {s/◌//g; m/\A./g} values %{ $kbd->{key} };
41         1;
42 } or warn "Failed reading additional keyboard map: $@";
43
44 eval {
45         require HTML::Entities;
46         our %char2entity;
47         HTML::Entities->import('%char2entity');
48         while (my ($char, $entity) = each %char2entity) {
49                 $entity =~ /[a-zA-Z]/ or next;  # only actual aliases
50                 $info{$char}->{html} = substr($entity, 1, -1);
51         }
52         1;
53 } or warn "Failed importing html entities: $@";
54
55 my %diinc = (
56         './data/digraphs-rfc.inc.pl' => 'u-di',
57         './data/digraphs-shiar.inc.pl' => 'u-prop',
58         './data/digraphs-vim.inc.pl' => 'u-vim',
59 );
60 for (sort keys %diinc) {
61         -e $_ or next;
62         my $di = do $_ or die "Error reading digraphs file $_: ", $@ || $!;
63         for my $mnem (sort keys %{$di}) {
64                 my $cp = $di->{$mnem};
65                 length $mnem == 2 or next;  # limit to digraphs
66                 my $class = $diinc{$_};
67                 $info{$cp}->{di} //= $mnem;
68                 $info{$cp}->{class}->{$class}++;
69         }
70 }
71
72 eval {
73         # read introducing unicode versions for known characters
74         my $agemap = do './data/unicode-age.inc.pl' or die $@ || $!;
75         for my $chr (keys %info) {
76                 my $version = $agemap->{ord $chr} or next;
77                 $info{$chr}->{class}->{'u-v'.$version}++
78         }
79         1;
80 } or warn "Failed including unicode version data: $@";
81
82 for my $chr (keys %info) {
83         my $cp = ord $chr;
84         # attempt to get unicode character information
85         my $info = eval {
86                 require Unicode::UCD;
87                 Unicode::UCD::charinfo($cp)
88                         || { block => '?', category => 'Xn', name => '', script => '' }
89         } or next;
90
91         $info->{$_} = $info{$chr}->{$_} for keys %{ $info{$chr} };
92
93         # ignore vim flag in addition to rfc support, replace otherwise
94         $info->{class}->{'u-di'} or $info->{class}->{'u-prop'}++
95                 if delete $info->{class}->{'u-vim'};
96
97         # categorise by unicode types and writing script
98         $info->{class}->{$_}++ for $info->{category};
99         $info->{class}->{$_}++ for $info->{script} || ();
100
101         # add custom categories for certain blocks
102         $info->{class}->{Xa}++ if $info->{block} eq 'Basic Latin';
103         $info->{class}->{Xl}++ if $info->{block} eq 'Latin-1 Supplement';
104
105         {
106                 if ($info->{string}) {
107                         # keep predefined presentational string
108                 }
109                 elsif ($info->{combining}) {
110                         # overlay combining accents
111                         $info->{string} = chr(9676) . $chr;
112                 }
113                 elsif (($cp & ~0b1001_1111) == 0 or $cp == 127) {
114                         # control characters (first 32 chars from 0 and 128)
115                         # rename to something more descriptive
116                         $info->{name} = $info->{unicode10}
117                                 ? '<'.$info->{unicode10}.'>'  # the old name was much more useful
118                                 : sprintf('<control U+%04X>', $cp);  # at least identify by value
119                         # show descriptive symbols instead of control chars themselves
120                         $info->{string} = $cp < 32   ? chr($cp + 0x2400) :
121                                           $cp == 127 ? chr(0x2421) :
122                                                        chr(0xFFFD);
123                 }
124         }
125
126         $info{$chr} = $info;
127 }
128
129 # output perl code of hash
130 say "# automatically generated by $0";
131 say 'use utf8;';
132 say '+{';
133 for my $cp (sort keys %info) {
134         $info{$cp}->{classstr} = join(' ', sort keys %{ $info{$cp}->{class} });
135         # convert info hashes into arrays of strings to output in display order
136         my $row = [ map { $info{$cp}->{$_} } qw/classstr name di html string/ ];
137         # strip off trailing missing values (especially string may be unknown)
138         defined $row->[-1] ? last : pop @$row for 1 .. @$row;
139         # final line (assume safe within single quotes)
140         say sprintf '"\x{%X}" => [%s],',
141                 ord $cp, join(',', map { escapeq($_) } @$row);
142 }
143 say '}';
144
145 sub escapeq {
146         local $_ = shift;
147         return 'undef' if not defined;
148         s/(['\\])/\\$1/g;
149         return "'$_'";
150 }
151
152 __END__
153
154 =head1 NAME
155
156 mkcharinfo - Gather Unicode character details in Perl array
157
158 =head1 SYNOPSIS
159
160     mkcharinfo > unicode-char.inc.pl
161
162 Test by printing the description of U+0041 (latin A):
163
164     perl -e'$u = do "unicode-char.inc.pl"; print $u->{A}->[1]'
165
166 =head1 AUTHOR
167
168 Mischa POSLAWSKY <perl@shiar.org>
169
170 =head1 LICENSE
171
172 Licensed under the GNU Affero General Public License version 3.
173