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