font: ?q offset by hex value or block name
[sheet.git] / font.plp
1 <(common.inc.plp)><:
2 use 5.014;
3
4 Html({
5         title => 'character support sheet',
6         version => 'v1.1',
7         keywords => [qw(
8                 unicode font glyph char character support overview cover coverage
9                 script block symbol sign mark reference table
10         )],
11         stylesheet => [qw( light dark mono circus red )],
12         data => [qw( unicode-cover.inc.pl )],
13 });
14
15 if (my $font = $ENV{PATH_INFO} =~ s{^/}{}r) {
16         my ($fontmeta, @cover) = do "ttfsupport/$font.inc.pl";
17         $fontmeta or die "Unknown font $font\n";
18
19         require Unicode::UCD;
20
21         my $size = 0x200;
22         my $offset = eval {
23                 local $_ = $get{q} || 0;
24                 return $_ if /\A\d+\z/;  # numeric
25                 return hex $1 if /\A (?: 0?x | u\W* ) ([0-9a-f]+) \z/ix;  # hexadecimal
26                 return $_->[0]->[0] for Unicode::UCD::charblock(ucfirst) || ();  # block
27                 die "Unknown offset query '$_'\n";
28         };
29         die $@ if $@;
30
31         say "<h1>Font coverage</h1>";
32         say "<h2>$_</h2>" for EscapeHTML($fontmeta->{name});
33         printf("<p>Version <strong%s>%s</strong> released %s contains %d glyphs.</p>\n",
34                 !!$_->[2] && qq( title="revision $_->[2]"),
35                 $_->[1], $_->[0],
36                 scalar @cover,
37         ) for [
38                 grep { $_ }
39                 ($fontmeta->{date} || '?') =~ s/T.*//r,
40                 EscapeHTML($fontmeta->{version}),
41                 $fontmeta->{revision},
42         ];
43         printf "<p>%s</p>\n", join('<br>', map { $_ ? EscapeHTML($_) : () }
44                 $fontmeta->{copyright}, $fontmeta->{license},
45         );
46
47         require Shiar_Sheet::FormatChar;
48         my $glyphs = Shiar_Sheet::FormatChar->new;
49
50         my %cover = map { ($_ => 1) } @cover;  # lookup map
51
52         say <<"EOT";
53
54 <style>
55         .glyphs tbody th { text-align: right }
56         .glyphs tbody td { font-family: "$fontmeta->{name}" }
57 </style>
58 EOT
59         say '<table class="glyphs big">';
60         say "<caption>$_</caption>" for join(' ', grep {$_}
61                 $offset > $size && sprintf('<a rel="start" href="?q=%d">◄</a>', 0),
62                 $offset > 0 && sprintf(
63                         '<a rel="prev" href="?q=%d" title="U+%1$04X">◅</a>',
64                         $offset - $size,
65                 ),
66                 sprintf('U+%04X', $offset), #TODO: block name
67                 $offset + $size < 0x11_0000 && sprintf(
68                         '<a rel="next" href="?q=%d" title="U+%1$04X">▻</a>',
69                         $offset + $size,
70                 ),
71         );
72         for my $cp ($offset .. $offset+$size-1) {
73                 my $info = $glyphs->glyph_info($cp);
74                 my ($class, $name, $mnem, $html, $string) = @{$info};
75                 my $np = $class =~ /\bC\S\b/;  # noprint if control or invalid
76                 say sprintf '<tr><th>%X', $cp if $cp % 32 == 0;
77                 say sprintf '<td class="%s" title="U+%04X%s">%s',
78                         !$class ? ('l0', $cp, '', '') :
79                         $cover{$cp} ? $np ? 'l2' : 'l5' : $np ? 'Xi' : 'l1',
80                         $cp, !!$name && ": $name",
81                         ($cover{$cp} || !$np) && EscapeHTML(chr $cp);
82         }
83         say '</table>';
84
85         exit;
86 }
87
88 :>
89 <h1>Font coverage</h1>
90
91 <p>
92 Character support of Unicode
93 <a href="/charset">blocks</a> and <a href="/unicode">presets</a>.
94 </p>
95
96 <div>
97
98 <:
99
100 my $cover = do 'unicode-cover.inc.pl' or die $@ || $!;
101
102 my @ossel = @{ $cover->{osdefault} };
103 my @fontlist = map { @{ $cover->{os}->{$_} } } @ossel;
104
105 my @rows = (
106         'block/Latin-1 Supplement',
107         'block/Latin Extended-A',
108         'block/Latin Extended Additional',
109         'block/Latin Extended-B',
110         'script/Latin',
111         'script/Greek',
112         'script/Cyrillic',
113         'script/Arabic',
114         'script/Hebrew',
115         'script/Devanagari',
116         'script/Thai',
117         'script/Hangul',
118         'table/japanese',
119         'script/Han',
120         'table/ipa',
121         'table/punctuation',
122         'block/Dingbats',
123         'table/symbols',
124         'category/Sc', # currency
125         'table/math',
126         'category/Sm', # mathematical
127         'table/arrows/single',
128         'table/lines/single',
129         'table/block',
130         'table/lines',
131         'table/html',
132 );
133
134 if (my $group = $get{q}) {
135         my $grouprows = $cover->{$group}
136                 or die "Unknown character category $_\n";
137         @rows = map { "$group/$_" } sort keys %{$grouprows};
138 }
139
140 # output character list
141
142 print '<table class=mapped>';
143 print '<col><col>';
144 print "<colgroup span=$_>"
145         for map { scalar @{ $cover->{os}->{$_} } } @ossel;
146
147 print '<thead><tr>';
148 print '<th colspan=2>';
149 printf '<td colspan=%d>%s fonts', scalar @{ $cover->{os}->{$_} }, $_
150         for @ossel;
151
152 print '<tr>';
153 print '<th colspan=2>';
154 printf('<td title="%s"><a href="%s">%s</a>', map { EscapeHTML($_) }
155         join("\n", $_->{name}, $_->{description}),
156         "/font/$_->{file}",
157         $_->{abbr},
158 ) for @{ $cover->{fonts} }[@fontlist];
159 say '</thead>';
160
161 for (@rows) {
162         my ($group, $name) = split m{/}, $_, 2;
163         my $row = $cover->{$group}->{$name};
164
165         print '<tr>';
166         $name = sprintf '<a href="%s">%s</a>', EncodeURI("/chars/$group/$name"), EscapeHTML($name)
167                 if $row->{count} and $row->{count} < 1280;
168         print '<th>', $name;
169         print '<td class=right>', $row->{count};
170         for my $count (@{ $row->{support} }[@fontlist]) {
171                 if (not defined $count) {
172                         print '<td class="l0">?';
173                         next;
174                 }
175                 if (not $count) {
176                         print '<td class="l1">✘';
177                         next;
178                 }
179                 if ($count == $row->{count}) {
180                         print '<td class="l5">✔';
181                         next;
182                 }
183
184                 my $rel = $count / $row->{count};
185                 my $class = $rel < .5 ? 2 : $rel < .9 ? 3 : 4;
186                 printf '<td class="%s">%d%%', "l$class", $rel*100;
187         }
188         say '</tr>';
189 }
190
191 say "</table>\n";
192
193 :></div>
194