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