6 no if $] >= 5.018, warnings => 'experimental::smartmatch';
8 use open OUT => ':utf8', ':std';
11 our $VERSION = '1.00';
14 "\xAD" => {string => '-'},
15 "\x{200E}" => {string => '→'},
16 "\x{200F}" => {string => '←'},
18 $info{chr $_} //= {} for 32 .. 126;
21 my $tables = do 'unicode-table.inc.pl' or die $@ || $!;
22 for (values %$tables) {
25 length $_ == 1 or next; # ignore meta values
32 } or warn "Failed reading unicode tables: $@";
35 require HTML::Entities;
37 HTML::Entities->import('%char2entity');
38 while (my ($char, $entity) = each %char2entity) {
39 $entity =~ /[a-zA-Z]/ or next; # only actual aliases
40 $info{$char}->{html} = substr($entity, 1, -1);
43 } or warn "Failed importing html entities: $@";
46 'digraphs.inc.pl' => 'u-di',
50 my $di = do $_ or die "Error reading digraphs file $_: ", $@ || $!;
51 while (my ($mnem, $cp) = each %$di) {
52 length $mnem == 2 or next; # limit to digraphs
53 my $class = $diinc{$_};
56 $class = 'u-prop' if $cp->[2] and $cp->[2] =~ m/\bXz\b/;
59 $info{$cp}->{di} //= $mnem;
60 $info{$cp}->{class}->{$class}++;
65 # read introducing unicode versions for known characters
66 my $agemap = do 'unicode-age.inc.pl' or die $@ || $!;
67 for my $chr (keys %info) {
68 my $version = $agemap->{ord $chr} or next;
69 $info{$chr}->{class}->{'u-v'.$version}++
72 } or warn "Failed including unicode version data $@";
74 for my $chr (keys %info) {
76 # attempt to get unicode character information
79 Unicode::UCD::charinfo($cp)
80 || { block => '?', category => 'Xn', name => '', script => '' }
83 $info->{$_} = $info{$chr}->{$_} for keys %{ $info{$chr} };
85 # categorise by unicode types and writing script
86 $info->{class}->{$_}++ for $info->{category};
87 $info->{class}->{$_}++ for $info->{script} || ();
89 # add custom categories for certain blocks
90 $info->{class}->{Xa}++ if $info->{block} eq 'Basic Latin';
91 $info->{class}->{Xl}++ if $info->{block} eq 'Latin-1 Supplement';
94 when ($info->{string}) {
95 # keep predefined presentational string
97 when ($info->{combining}) {
98 # overlay combining accents
99 $info->{string} = chr(9676) . $chr;
101 when (($cp & ~0b1001_1111) == 0 or $cp == 127) {
102 # control characters (first 32 chars from 0 and 128)
103 # rename to something more descriptive
104 $info->{name} = $info->{unicode10}
105 ? '<'.$info->{unicode10}.'>' # the old name was much more useful
106 : sprintf('<control U+%04X>', $cp); # at least identify by value
107 # show descriptive symbols instead of control chars themselves
108 $info->{string} = $cp < 32 ? chr($cp + 0x2400) : chr(0xFFFD);
115 # output perl code of hash
118 for my $cp (sort keys %info) {
119 $info{$cp}->{classstr} = join(' ', sort keys %{ $info{$cp}->{class} });
120 # convert info hashes into arrays of strings to output in display order
121 my $row = [ map { $info{$cp}->{$_} } qw/classstr name di html string/ ];
122 # strip off trailing missing values (especially string may be unknown)
123 defined $row->[-1] ? last : pop @$row for 1 .. @$row;
124 # final line (assume safe within single quotes)
125 say sprintf '"\x{%X}" => [%s],',
126 ord $cp, join(',', map { escapeq($_) } @$row);
132 return 'undef' if not defined;
141 mkcharinfo - Gather Unicode character details in Perl array
145 mkcharinfo > unicode-char.inc.pl
147 Test by printing the description of U+0041 (latin A):
149 perl -e'$u = do "unicode-char.inc.pl"; print $u->{A}->[1]'
153 Mischa POSLAWSKY <perl@shiar.org>
157 Licensed under the GNU Affero General Public License version 3.