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