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