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