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