keyboard/altgr/ipa: preformatted (double) combining diacritics
[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         # attempt to get unicode character information
88         my $info = eval {
89                 require Unicode::UCD;
90                 Unicode::UCD::charinfo($cp)
91                         || { block => '?', category => 'Xn', name => '', script => '' }
92         } or next;
93
94         $info->{$_} = $info{$chr}->{$_} for keys %{ $info{$chr} };
95
96         # ignore vim flag in addition to rfc support, replace otherwise
97         $info->{class}->{'u-di'} or $info->{class}->{'u-prop'}++
98                 if delete $info->{class}->{'u-vim'};
99
100         # categorise by unicode types and writing script
101         $info->{class}->{$_}++ for $info->{category};
102         $info->{class}->{$_}++ for $info->{script} || ();
103
104         # add custom categories for certain blocks
105         $info->{class}->{Xa}++ if $info->{block} eq 'Basic Latin';
106         $info->{class}->{Xl}++ if $info->{block} eq 'Latin-1 Supplement';
107
108         {
109                 if ($info->{string}) {
110                         # keep predefined presentational string
111                 }
112                 elsif ($info->{combining}) {
113                         # overlay combining accents
114                         $info->{string} = chr(9676) . $chr;
115                 }
116                 elsif (($cp & ~0b1001_1111) == 0 or $cp == 127) {
117                         # control characters (first 32 chars from 0 and 128)
118                         # rename to something more descriptive
119                         $info->{name} = $info->{unicode10}
120                                 ? '<'.$info->{unicode10}.'>'  # the old name was much more useful
121                                 : sprintf('<control U+%04X>', $cp);  # at least identify by value
122                         # show descriptive symbols instead of control chars themselves
123                         $info->{string} = $cp < 32   ? chr($cp + 0x2400) :
124                                           $cp == 127 ? chr(0x2421) :
125                                                        chr(0xFFFD);
126                 }
127         }
128
129         $info{$chr} = $info;
130 }
131
132 # output perl code of hash
133 say "# automatically generated by $0";
134 say 'use utf8;';
135 say '+{';
136 for my $cp (sort keys %info) {
137         $info{$cp}->{classstr} = join(' ', sort keys %{ $info{$cp}->{class} });
138         # convert info hashes into arrays of strings to output in display order
139         my $row = [ map { $info{$cp}->{$_} } qw/classstr name di html string/ ];
140         # strip off trailing missing values (especially string may be unknown)
141         defined $row->[-1] ? last : pop @$row for 1 .. @$row;
142         # final line (assume safe within single quotes)
143         say sprintf '"\x{%X}" => [%s],',
144                 ord $cp, join(',', map { escapeq($_) } @$row);
145 }
146 say '}';
147
148 sub escapeq {
149         local $_ = shift;
150         return 'undef' if not defined;
151         s/(['\\])/\\$1/g;
152         return "'$_'";
153 }
154
155 __END__
156
157 =head1 NAME
158
159 mkcharinfo - Gather Unicode character details in Perl array
160
161 =head1 SYNOPSIS
162
163     mkcharinfo > unicode-char.inc.pl
164
165 Test by printing the description of U+0041 (latin A):
166
167     perl -e'$u = do "unicode-char.inc.pl"; print $u->{A}->[1]'
168
169 =head1 AUTHOR
170
171 Mischa POSLAWSKY <perl@shiar.org>
172
173 =head1 LICENSE
174
175 Licensed under the GNU Affero General Public License version 3.
176