unicode: declare characters in unicode tables
[sheet.git] / tools / convert-unicode.pl
1 #!/usr/bin/env perl
2 use 5.010;
3 use strict;
4 use warnings;
5 use utf8;
6
7 use open OUT => ':utf8', ':std';
8 use Data::Dump 'pp';
9
10 our $VERSION = '1.00';
11
12 my %info = (
13         "\xAD"     => {string => '-'},
14         "\x{200E}" => {string => '→'},
15         "\x{200F}" => {string => '←'},
16 );
17 $info{chr $_} //= {} for 32 .. 126;
18
19 eval {
20         my $tables = do 'unicode-table.inc.pl' or die $@ || $!;
21         for (values %$tables) {
22                 for (values %$_) {
23                         for (@$_) {
24                                 length $_ == 1 or next;  # ignore meta values
25                                 s/\\//;  # unescape
26                                 $info{$_} //= {};
27                         }
28                 }
29         }
30         1;
31 } or warn "Failed reading unicode tables: $@";
32
33 my %diinc = (
34         'digraphs.inc.pl' => 'u-di',
35 );
36 for (keys %diinc) {
37         -e $_ or next;
38         my $di = do $_ or die "Error reading digraphs file $_: ", $@ || $!;
39         while (my ($mnem, $cp) = each %$di) {
40                 length $mnem == 2 or next;  # limit to digraphs
41                 $cp = chr $cp->[0] if ref $cp;  # old style array
42                 $info{$cp}->{di} //= $mnem;
43                 $info{$cp}->{class}->{$_}++ for $diinc{$_};
44         }
45 }
46
47 for my $chr (keys %info) {
48         my $cp = ord $chr;
49         # attempt to get unicode character information
50         my $info = eval {
51                 require Unicode::UCD;
52                 Unicode::UCD::charinfo($cp)
53                         || { block => '?', category => 'Xn', name => '', script => '' }
54         } or next;
55
56         $info->{$_} = $info{$chr}->{$_} for qw(di class string);
57
58         # categorise by unicode types and writing script
59         $info->{class}->{$_}++ for $info->{category};
60         $info->{class}->{"u-$_"}++ for $info->{script} || ();
61
62         # add custom categories for certain blocks
63         $info->{class}->{Xa}++ if $info->{block} eq 'Basic Latin';
64         $info->{class}->{Xl}++ if $info->{block} eq 'Latin-1 Supplement';
65
66         given ($cp) {
67                 when ($info->{string}) {
68                         # keep predefined presentational string
69                 }
70                 when ($info->{combining}) {
71                         # overlay combining accents
72                         $info->{string} = chr(9676) . $chr;
73                 }
74                 when (($cp & ~0b1001_1111) == 0 or $cp == 127) {
75                         # control characters (first 32 chars from 0 and 128)
76                         # rename to something more descriptive
77                         $info->{name} = $info->{unicode10}
78                                 ? '<'.$info->{unicode10}.'>'  # the old name was much more useful
79                                 : sprintf('<control U+%04X>', $cp);  # at least identify by value
80                         # show descriptive symbols instead of control chars themselves
81                         $info->{string} = $cp < 32 ? chr($cp + 0x2400) : chr(0xFFFD);
82                 }
83         }
84
85         $info{$chr} = $info;
86 }
87
88 # output perl code of hash
89 say '+{';
90 for my $cp (sort keys %info) {
91         $info{$cp}->{classstr} = join(' ', sort keys %{ $info{$cp}->{class} });
92         # convert info hashes into arrays of strings to output in display order
93         my $row = [ map { $info{$cp}->{$_} } qw/classstr name di string/ ];
94         # strip off trailing missing values (especially string may be unknown)
95         defined $row->[-1] ? last : pop @$row for 1 .. @$row;
96         # final line (assume safe within single quotes)
97         say sprintf '"\x{%X}" => [%s],',
98                 ord $cp, join(',', map { escapeq($_) } @$row);
99 }
100 say '}';
101
102 sub escapeq {
103         my $_ = shift;
104         return 'undef' if not defined;
105         s/(['\\])/\\$1/g;
106         return "'$_'";
107 }
108
109 __END__
110
111 =head1 NAME
112
113 convert-unicode.pl - Gather Unicode character details in Perl array
114
115 =head1 SYNOPSIS
116
117     convert-unicode > unicode-char.inc.pl
118
119 Test by printing the description of U+0041 (latin A):
120
121     perl -e'$u = do "unicode-char.inc.pl"; print $u->{A}->[1]'
122
123 =head1 AUTHOR
124
125 Mischa POSLAWSKY <perl@shiar.org>
126
127 =head1 LICENSE
128
129 Licensed under the GNU Affero General Public License version 3.
130