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