5 no if $] >= 5.018, warnings => 'experimental::smartmatch';
7 use open OUT => ':encoding(utf-8)', ':std';
10 our $VERSION = '1.02';
13 # prepare presentational string for some control(lish) entries
14 "\xAD" => {string => '-'},
15 "\x{200E}" => {string => '→'},
16 "\x{200F}" => {string => '←'},
17 "\x{200B}" => {string => '␣'}, # nbsp: ~ in TeX
18 "\x{200C}" => {string => '|'}, # ISO-9995-7-081 lookalike (alt: ∣ ⊺ ⟙)
19 "\x{200D}" => {string => '⁀'}, # join (alt: ∤ |ͯ ⨝)
21 $info{chr $_} //= {} for 32 .. 126;
24 my $tables = do './unicode-table.inc.pl' or die $@ || $!;
25 for (values %$tables) {
28 length $_ == 1 or next; # ignore meta values
35 } or warn "Failed reading unicode tables: $@";
38 do './keyboard/altgr/macos-abc.eng.inc.pl' or die $@ || $!;
40 $info{$_} //= {} for values %rows;
42 } or warn "Failed reading additional keyboard map: $@";
45 require HTML::Entities;
47 HTML::Entities->import('%char2entity');
48 while (my ($char, $entity) = each %char2entity) {
49 $entity =~ /[a-zA-Z]/ or next; # only actual aliases
50 $info{$char}->{html} = substr($entity, 1, -1);
53 } or warn "Failed importing html entities: $@";
56 './data/digraphs-rfc.inc.pl' => 'u-di',
57 './data/digraphs-shiar.inc.pl' => 'u-prop',
58 './data/digraphs-vim.inc.pl' => 'u-vim',
60 for (sort keys %diinc) {
62 my $di = do $_ or die "Error reading digraphs file $_: ", $@ || $!;
63 for my $mnem (sort keys %{$di}) {
64 my $cp = $di->{$mnem};
65 length $mnem == 2 or next; # limit to digraphs
66 my $class = $diinc{$_};
67 $info{$cp}->{di} //= $mnem;
68 $info{$cp}->{class}->{$class}++;
73 # read introducing unicode versions for known characters
74 my $agemap = do './data/unicode-age.inc.pl' or die $@ || $!;
75 for my $chr (keys %info) {
76 my $version = $agemap->{ord $chr} or next;
77 $info{$chr}->{class}->{'u-v'.$version}++
80 } or warn "Failed including unicode version data: $@";
82 for my $chr (keys %info) {
84 # attempt to get unicode character information
87 Unicode::UCD::charinfo($cp)
88 || { block => '?', category => 'Xn', name => '', script => '' }
91 $info->{$_} = $info{$chr}->{$_} for keys %{ $info{$chr} };
93 # ignore vim flag in addition to rfc support, replace otherwise
94 $info->{class}->{'u-di'} or $info->{class}->{'u-prop'}++
95 if delete $info->{class}->{'u-vim'};
97 # categorise by unicode types and writing script
98 $info->{class}->{$_}++ for $info->{category};
99 $info->{class}->{$_}++ for $info->{script} || ();
101 # add custom categories for certain blocks
102 $info->{class}->{Xa}++ if $info->{block} eq 'Basic Latin';
103 $info->{class}->{Xl}++ if $info->{block} eq 'Latin-1 Supplement';
106 if ($info->{string}) {
107 # keep predefined presentational string
109 elsif ($info->{combining}) {
110 # overlay combining accents
111 $info->{string} = chr(9676) . $chr;
113 elsif (($cp & ~0b1001_1111) == 0 or $cp == 127) {
114 # control characters (first 32 chars from 0 and 128)
115 # rename to something more descriptive
116 $info->{name} = $info->{unicode10}
117 ? '<'.$info->{unicode10}.'>' # the old name was much more useful
118 : sprintf('<control U+%04X>', $cp); # at least identify by value
119 # show descriptive symbols instead of control chars themselves
120 $info->{string} = $cp < 32 ? chr($cp + 0x2400) :
121 $cp == 127 ? chr(0x2421) :
129 # output perl code of hash
130 say "# automatically generated by $0";
133 for my $cp (sort keys %info) {
134 $info{$cp}->{classstr} = join(' ', sort keys %{ $info{$cp}->{class} });
135 # convert info hashes into arrays of strings to output in display order
136 my $row = [ map { $info{$cp}->{$_} } qw/classstr name di html string/ ];
137 # strip off trailing missing values (especially string may be unknown)
138 defined $row->[-1] ? last : pop @$row for 1 .. @$row;
139 # final line (assume safe within single quotes)
140 say sprintf '"\x{%X}" => [%s],',
141 ord $cp, join(',', map { escapeq($_) } @$row);
147 return 'undef' if not defined;
156 mkcharinfo - Gather Unicode character details in Perl array
160 mkcharinfo > unicode-char.inc.pl
162 Test by printing the description of U+0041 (latin A):
164 perl -e'$u = do "unicode-char.inc.pl"; print $u->{A}->[1]'
168 Mischa POSLAWSKY <perl@shiar.org>
172 Licensed under the GNU Affero General Public License version 3.