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 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);
40 } or warn "Failed importing html entities: $@";
43 'digraphs.inc.pl' => 'u-di',
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{$_};
53 $class = 'u-prop' if $cp->[2] and $cp->[2] =~ m/\bXz\b/;
56 $info{$cp}->{di} //= $mnem;
57 $info{$cp}->{class}->{$class}++;
61 for my $chr (keys %info) {
63 # attempt to get unicode character information
66 Unicode::UCD::charinfo($cp)
67 || { block => '?', category => 'Xn', name => '', script => '' }
70 $info->{$_} = $info{$chr}->{$_} for qw(di html class string);
72 # categorise by unicode types and writing script
73 $info->{class}->{$_}++ for $info->{category};
74 $info->{class}->{$_}++ for $info->{script} || ();
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';
81 when ($info->{string}) {
82 # keep predefined presentational string
84 when ($info->{combining}) {
85 # overlay combining accents
86 $info->{string} = chr(9676) . $chr;
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);
102 # output perl code of hash
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);
119 return 'undef' if not defined;
128 mkcharinfo - Gather Unicode character details in Perl array
132 mkcharinfo > unicode-char.inc.pl
134 Test by printing the description of U+0041 (latin A):
136 perl -e'$u = do "unicode-char.inc.pl"; print $u->{A}->[1]'
140 Mischa POSLAWSKY <perl@shiar.org>
144 Licensed under the GNU Affero General Public License version 3.