font: support character ranges and unicode matches
[sheet.git] / font.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
56 my $query = $ENV{PATH_INFO} || $get{q} || 'ipa';
57 for ($query) {
58         s{^/}{};
59         when (qr{^[a-z]+(?:/|\z)}) {
60                 for (split / /) {
61                         my ($tablegroup, $tablename) = split m{/}, $_, 2;
62                         my @tables = $tablename ? $tables->{$tablegroup}->{$tablename}
63                                    : sort values %{ $tables->{$tablegroup} };
64                         for (@tables) {
65                                 my $includerows;  # ignore rows before body row
66                                 for (@{$_}) {
67                                         $includerows ||= m/^[.]/ or next;
68                                         next if /^[.-]/;
69                                         next if $_ eq '>' or $_ eq '=';
70                                         push @chars, $_;
71                                 }
72                         }
73                 }
74         }
75         when (qr{[\d,;\s+-]+}) {
76                 for (map { split /[^\d-]/ } $_) {
77                         my ($charnum, $range) = split /-/, $_;
78                         push @chars, chr $_ for $charnum .. ($range // $charnum);
79                 }
80         }
81         when (qr{[A-Z]}) {
82                 eval {
83                         my $match = qr/\A\p{$_}\z/;
84                         push @chars, grep { m/$match/ } map { chr $_ }
85                                 0..0xD7FF, 0xE000..0xFDCF, 0xFDF0..0xFFFD;
86                 } or die "invalid unicode match: $_\n";
87         }
88         default {
89                 die "unknown parameter: $_\n";
90         }
91 }
92
93 @chars <= 1500
94         or die sprintf 'too many matches (%d)'."\n", scalar @chars;
95
96 # output character list
97
98 print '<table class=mapped>';
99 print '<col>' x 3;
100 print "<colgroup span=$_>" for 2, map { scalar @{$oslist{$_}} } @ossel;
101
102 print '<thead><tr>';
103 print '<td colspan=3>character';
104 print '<td colspan=2>input';
105 printf '<td colspan=%d>%s fonts', scalar @{ $oslist{$_} }, $_
106         for @ossel;
107
108 print '<tr>';
109 print '<td colspan=2>unicode';
110 print '<td>name';
111 print '<td>di<td>html';
112 printf '<td title="%s">%s', $font{$_}->{-name}, $font{$_}->{-id} // $_
113         for @fontlist;
114 say '</thead>';
115
116 for my $chr (@chars) {
117         my $codepoint = ord $chr;
118         my $ascii = $codepoint <= 127;
119
120         print "<tr><th>$chr\n";
121         my $info = $glyphs->glyph_info($codepoint);
122         my ($class, $name, $mnem, $html, $string) = @$info;
123         print "<td>$_" for sprintf('%X', $codepoint), EscapeHTML($name || '?');
124         printf '<td class="%s">%s', @$_ for (
125                 [$ascii ? 'l0' : defined $mnem ? 'l4' : 'l1', $mnem // ''],
126                 [$ascii ? 'l0' : defined $html ? 'l4' : 'l1', $html // ''],
127                 (map {
128                         !$font{$_}->{-id} ? [l0 => '?'] :
129                         $font{$_}->{$chr} ? [l4 => '✔'] : [l1 => '✘']
130                 } @fontlist),
131         );
132 }
133
134 say "</table>\n";
135
136 :></div>
137