font: mkfontinfo script to create unicode-cover.inc.pl
[sheet.git] / chars.plp
1 <(common.inc.plp)><:
2
3 Html({
4         title => 'character support sheet',
5         version => 'v1.0',
6         keywords => [qw'
7                 unicode glyph char character reference common ipa symbol sign mark table digraph
8         '],
9         stylesheet => [qw'light dark mono circus red'],
10         data => [qw'unicode-table.inc.pl unicode-char.inc.pl'],
11 });
12
13 :>
14 <h1>Character support</h1>
15
16 <p>
17 Selected characters from Unicode <a href="/unicode">preset</a>
18 or <a href="/charset">range</a>.
19 </p>
20
21 <div>
22
23 <:
24 use 5.010;
25 use Shiar_Sheet::FormatChar;
26 my $glyphs = Shiar_Sheet::FormatChar->new;
27
28 my %oslist = (
29         win95   => [qw( arial ariuni verdana times )],  # microsoft
30         mac10   => [qw( )],  # apple
31         android => [qw( droidsans )],  # google
32         oss     => [qw( dvsans c2k unifont )],
33 );
34 my @ossel = qw( win95 oss android );
35
36 my $tables = do 'unicode-table.inc.pl' or die $@ || $!;
37 my (%font, @fontlist);
38 for my $os (@ossel) {
39         my $osfonts = $oslist{$os};
40         for my $fontid (@{$osfonts}) {
41                 push @fontlist, $fontid;
42                 my ($fontmeta, @fontrange) = do "ttfsupport/$fontid.inc.pl";
43                 $fontmeta or next;
44                 $font{$fontid} = {
45                         -id   => $fontmeta->{id} || $fontid,
46                         -name => $fontmeta->{name},
47                         map { (chr $_ => 1) } @fontrange
48                 };
49         }
50 }
51
52 # parse input
53
54 my @chars;
55 my @querydesc;
56
57 my $query = $ENV{PATH_INFO} || $get{q} || 'ipa';
58 for ($query) {
59         s{^/}{};
60         when (qr{^[a-z]+(?:/|\z)}) {
61                 for (split / /) {
62                         push @querydesc, "preset group $_";
63                         my ($tablegroup, $tablename) = split m{/}, $_, 2;
64                         my @tables = $tablename ? $tables->{$tablegroup}->{$tablename}
65                                    : sort values %{ $tables->{$tablegroup} };
66                         for (@tables) {
67                                 my $includerows;  # ignore rows before body row
68                                 for (@{$_}) {
69                                         $includerows ||= m/^[.]/ or next;
70                                         next if /^[.-]/;
71                                         next if $_ eq '>' or $_ eq '=';
72                                         push @chars, $_;
73                                 }
74                         }
75                 }
76                 when ('ipa') {
77                         @chars = grep { !m/[a-zA-Z]/ } @chars;
78                 }
79         }
80         when (qr{[\d,;\s+-]+}) {
81                 push @querydesc, "character codepoints $_";
82                 for (map { split /[^\d-]/ } $_) {
83                         my ($charnum, $range) = split /-/, $_;
84                         push @chars, chr $_ for $charnum .. ($range // $charnum);
85                 }
86         }
87         when (qr{[A-Z]}) {
88                 push @querydesc, "unicode match $_";
89                 eval {
90                         my $match = qr/\A\p{$_}\z/;
91                         push @chars, grep { m/$match/ } map { chr $_ }
92                                 0..0xD7FF, 0xE000..0xFDCF, 0xFDF0..0xFFFD;
93                 } or die "invalid unicode match: $_\n";
94         }
95         default {
96                 die "unknown parameter: $_\n";
97         }
98 }
99
100 @chars <= 1500 or die sprintf(
101         'too many matches (%d) for %s'."\n",
102         scalar @chars, join(', ', @querydesc),
103 );
104
105 # output character list
106
107 print '<table class=mapped>';
108 say '<caption>'.EscapeHTML(join ', ', @querydesc).'</caption>';
109 print '<col>' x 3;
110 print "<colgroup span=$_>" for 2, map { scalar @{$oslist{$_}} } @ossel;
111
112 print '<thead><tr>';
113 print '<td colspan=3>character';
114 print '<td colspan=2>input';
115 printf '<td colspan=%d>%s fonts', scalar @{ $oslist{$_} }, $_
116         for @ossel;
117
118 print '<tr>';
119 print '<td colspan=2>unicode';
120 print '<td>name';
121 print '<td><a href="/digraphs" title="digraph">di</a><td>html';
122 printf '<td title="%s">%s', $font{$_}->{-name}, $font{$_}->{-id} // $_
123         for @fontlist;
124 say '</thead>';
125
126 for my $chr (@chars) {
127         my $codepoint = ord $chr;
128         my $ascii = $codepoint <= 127;
129
130         print "<tr><th>$chr\n";
131         my $info = $glyphs->glyph_info($codepoint);
132         my ($class, $name, $mnem, $html, $string) = @$info;
133         print "<td>$_" for sprintf('%X', $codepoint), EscapeHTML($name || '?');
134         printf '<td class="%s">%s', @$_ for (
135                 [$ascii ? 'l0' : defined $mnem ? $class =~ /\bu-di\b/ ? 'l4' : 'l3' : 'l1', $mnem // ''],
136                 [$ascii ? 'l0' : defined $html ? 'l4' : 'l1', $html // ''],
137                 (map {
138                         !$font{$_}->{-id} ? [l0 => '?'] :
139                         $font{$_}->{$chr} ? [l4 => '✔'] : [l1 => '✘']
140                 } @fontlist),
141         );
142 }
143
144 say "</table>\n";
145
146 :></div>
147