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