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