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: $@";
41 for my $layout ('macos-abc', 'windows') {
43 my $kbd = do "./keyboard/altgr/$layout.eng.inc.pl" or die $@ || $!;
44 $info{$_} //= {} for map {s/◌//g; m/\A./g} values %{ $kbd->{key} };
46 } or warn "Failed reading additional keyboard map $layout: $@";
50 require HTML::Entities;
52 HTML::Entities->import('%char2entity');
53 while (my ($char, $entity) = each %char2entity) {
54 $entity =~ /[a-zA-Z]/ or next; # only actual aliases
55 $info{$char}->{html} = substr($entity, 1, -1);
58 } or warn "Failed importing html entities: $@";
61 './data/digraphs-rfc.inc.pl' => 'u-di',
62 './data/digraphs-shiar.inc.pl' => 'u-prop',
63 './data/digraphs-vim.inc.pl' => 'u-vim',
65 for (sort keys %diinc) {
67 my $di = do $_ or die "Error reading digraphs file $_: ", $@ || $!;
68 for my $mnem (sort keys %{$di}) {
69 my $cp = $di->{$mnem};
70 length $mnem == 2 or next; # limit to digraphs
71 my $class = $diinc{$_};
72 $info{$cp}->{di} //= $mnem;
73 $info{$cp}->{class}->{$class}++;
78 # read introducing unicode versions for known characters
79 my $agemap = do './data/unicode-age.inc.pl' or die $@ || $!;
80 for my $chr (keys %info) {
81 my $version = $agemap->{ord $chr} or next;
82 $info{$chr}->{class}->{'u-v'.$version}++
85 } or warn "Failed including unicode version data: $@";
87 for my $chr (keys %info) {
89 #my $info = glyph_mkinfo($cp) or next;
90 # attempt to get unicode character information
93 Unicode::UCD::charinfo($cp)
94 || { block => '?', category => 'Xn', name => '', script => '' }
97 $info->{$_} = $info{$chr}->{$_} for keys %{ $info{$chr} };
99 # ignore vim flag in addition to rfc support, replace otherwise
100 $info->{class}->{'u-di'} or $info->{class}->{'u-prop'}++
101 if delete $info->{class}->{'u-vim'};
103 # categorise by unicode types and writing script
104 $info->{class}->{$_}++ for $info->{category};
105 $info->{class}->{$_}++ for $info->{script} || ();
107 # add custom categories for certain blocks
108 $info->{class}->{Xa}++ if $info->{block} eq 'Basic Latin';
109 $info->{class}->{Xl}++ if $info->{block} eq 'Latin-1 Supplement';
112 if ($info->{string}) {
113 # keep predefined presentational string
115 elsif ($info->{combining}) {
116 # overlay combining accents
117 $info->{string} = chr(9676) . $chr;
119 elsif (($cp & ~0b1001_1111) == 0 or $cp == 127) {
120 # control characters (first 32 chars from 0 and 128)
121 # rename to something more descriptive
122 $info->{name} = $info->{unicode10}
123 ? '<'.$info->{unicode10}.'>' # the old name was much more useful
124 : sprintf('<control U+%04X>', $cp); # at least identify by value
125 # show descriptive symbols instead of control chars themselves
126 $info->{string} = $cp < 32 ? chr($cp + 0x2400) :
127 $cp == 127 ? chr(0x2421) :
135 # output perl code of hash
136 say "# automatically generated by $0";
139 for my $cp (sort keys %info) {
140 $info{$cp}->{classstr} = join(' ', sort keys %{ $info{$cp}->{class} });
141 # convert info hashes into arrays of strings to output in display order
142 my $row = [ map { $info{$cp}->{$_} } qw/classstr name di html string/ ];
143 # strip off trailing missing values (especially string may be unknown)
144 defined $row->[-1] ? last : pop @$row for 1 .. @$row;
145 # final line (assume safe within single quotes)
146 say sprintf '"\x{%X}" => [%s],',
147 ord $cp, join(',', map { escapeq($_) } @$row);
153 return 'undef' if not defined;
162 mkcharinfo - Gather Unicode character details in Perl array
166 mkcharinfo > unicode-char.inc.pl
168 Test by printing the description of U+0041 (latin A):
170 perl -e'$u = do "unicode-char.inc.pl"; print $u->{A}->[1]'
174 Mischa POSLAWSKY <perl@shiar.org>
178 Licensed under the GNU Affero General Public License version 3.