7 use open OUT => ':utf8', ':std';
10 our $VERSION = '1.00';
13 "\xAD" => {string => '-'},
14 "\x{200E}" => {string => '→'},
15 "\x{200F}" => {string => '←'},
17 $info{chr $_} //= {} for 32 .. 126;
20 my $tables = do 'unicode-table.inc.pl' or die $@ || $!;
21 for (values %$tables) {
24 length $_ == 1 or next; # ignore meta values
31 } or warn "Failed reading unicode tables: $@";
34 'digraphs.inc.pl' => 'u-di',
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{$_};
47 for my $chr (keys %info) {
49 # attempt to get unicode character information
52 Unicode::UCD::charinfo($cp)
53 || { block => '?', category => 'Xn', name => '', script => '' }
56 $info->{$_} = $info{$chr}->{$_} for qw(di class string);
58 # categorise by unicode types and writing script
59 $info->{class}->{$_}++ for $info->{category};
60 $info->{class}->{"u-$_"}++ for $info->{script} || ();
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';
67 when ($info->{string}) {
68 # keep predefined presentational string
70 when ($info->{combining}) {
71 # overlay combining accents
72 $info->{string} = chr(9676) . $chr;
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);
88 # output perl code of hash
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);
104 return 'undef' if not defined;
113 convert-unicode.pl - Gather Unicode character details in Perl array
117 convert-unicode > unicode-char.inc.pl
119 Test by printing the description of U+0041 (latin A):
121 perl -e'$u = do "unicode-char.inc.pl"; print $u->{A}->[1]'
125 Mischa POSLAWSKY <perl@shiar.org>
129 Licensed under the GNU Affero General Public License version 3.