index: release v1.18 with only altgr index linked
[sheet.git] / font.plp
1 <(common.inc.plp)><:
2
3 my $font = $Request;
4
5 Html({
6         title => 'font coverage '.($font ? "for $font" : 'sheet'),
7         version => '1.4',
8         keywords => [qw(
9                 unicode font glyph char character support overview cover coverage
10                 script block symbol sign mark reference table
11         )],
12         stylesheet => [qw( light dark mono circus red )],
13         data => [qw( data/unicode-cover.inc.pl )],
14 });
15
16 if ($font) {
17         my $fontmeta = eval { Data("data/font/$font") }
18                 or Abort("Unknown font $font", '404 font not found', ref $@ && $@->[1]);
19
20         my $map = eval {
21                 $get{map} or return;
22
23                 my $groupinfo = Data('data/unicode-cover');
24
25                 my ($cat, $name) = split m{/}, $get{map}, 2 or die "invalid map\n";
26                 if (!$name) {
27                         ($cat, $name) = ('table', $cat);
28                 }
29
30                 my $row = $groupinfo->{$cat}->{$name}
31                         or die "unknown character group $cat/$name\n";
32                 my $query = $row->{query};
33
34                 my @map;
35                 for (map { split /[^\d-]/ } $query) {
36                         my @range = split /-/, $_, 2;
37                         m/^[0-9]+$/ or die "Invalid code point $_ in query $query\n" for @range;
38                         push @map, $_ for $range[0] .. ($range[1] // $range[0]);
39                 }
40                 return \@map;
41         };
42         Abort($@, '404 invalid query') if $@;
43
44         require Unicode::UCD;
45
46         my $pagerows = 0x200;
47         my $pagecols = 32;
48         my $offset = eval {
49                 local $_ = $get{q} || 0;
50                 return $_ if /\A\d+\z/;  # numeric
51                 return hex $1 if /\A (?: 0?x | u\W* ) ([0-9a-f]+) \z/ix;  # hexadecimal
52                 return $_->[0]->[0] for Unicode::UCD::charblock(ucfirst) || ();  # block
53                 die "Unknown offset query '$_'\n";
54         };
55         Abort($@, '400 invalid offset') if $@;
56
57         say "<h1>Font coverage</h1>";
58         say "<h2>$_</h2>" for EscapeHTML($fontmeta->{name});
59         printf("<p>Version <strong%s>%s</strong> released %s contains %d glyphs.",
60                 !!$_->[2] && qq( title="revision $_->[2]"),
61                 $_->[1], $_->[0],
62                 scalar @{ $fontmeta->{cover} },
63         ) for [
64                 grep { $_ }
65                 ($fontmeta->{date} || '?') =~ s/T.*//r,
66                 EscapeHTML($fontmeta->{version}),
67                 $fontmeta->{revision},
68         ];
69         for ($fontmeta->{os}) {
70                 say '<br>';
71                 print ucfirst join(' ',
72                         "distributed",
73                         (map { "by $_" } $fontmeta->{oscorp} || "various sources"),
74                         (map { "with <em>$_</em>" } $_ || ()),
75                         ('and published as freeware "Core Web font"') x ($_ eq 'Windows 2000'),
76                         (map { "under a $_ license" }
77                                 map { $fontmeta->{license} ? qq(<a href="$fontmeta->{license}" rel=nofollow>$_</a>) : $_ }
78                                 $_ && $_ ne 'Android' ? 'proprietary' : 'free',
79                         ),
80                 );
81                 print '.';
82         }
83         say '</p>';
84         say "<p>$_</p>" for EscapeHTML($fontmeta->{copyright}) || ();
85
86         require Shiar_Sheet::FormatChar;
87         my $glyphs = Shiar_Sheet::FormatChar->new;
88
89         my %cover = map { ($_ => 1) } @{ $fontmeta->{cover} };  # lookup map
90
91         say <<"EOT";
92
93 <style>
94         .glyphs tbody th[!colspan] { text-align: right }
95         .glyphs tbody td { font-family: "$fontmeta->{name}" }
96         .glyphs tbody td:nth-child(18) { border-left-width: 2px }
97         \@font-face {
98                 font-family: "$fontmeta->{name}";
99                 src: url(/data/font/$font.ttf);
100         }
101 </style>
102 EOT
103         say '<table class="glyphs big">';
104
105         my $offsetlink = '?' . join('&amp;',
106                 (map { $_ . '=' . EncodeURI($get{$_}) } grep { defined $get{$_} } qw{ map }),
107                 'q',
108         );
109         say "<caption>$_</caption>" for join(' ', grep {$_}
110                 $offset > $pagerows && sprintf('<a rel="start" href="%s=%d">◄</a>', $offsetlink, 0),
111                 $offset > 0 && sprintf(
112                         '<a rel="prev" href="%s=%d" title="U+%2$04X">◅</a>',
113                         $offsetlink, $offset - $pagerows,
114                 ),
115                 sprintf('U+%04X', $map ? $map->[$offset] : $offset),
116                 Unicode::UCD::charblock($map ? $map->[$offset] : $offset),
117                 $offset + $pagerows < ($map ? @{$map} : 0x11_0000) && sprintf(
118                         '<a rel="next" href="%s=%d" title="U+%2$04X">▻</a>',
119                         $offsetlink, $offset + $pagerows,
120                 ),
121         );
122
123         for my $cp ($offset .. $offset+$pagerows-1) {
124                 $cp = $map->[$cp] or next if $map;
125
126                 state $colpos;
127                 my $block = Unicode::UCD::charblock($cp);
128                 if ($block ne (state $sameblock = $block) and $block ne 'No_Block') {
129                         print '<tbody>';
130                         printf '<tr><th colspan=%d>%s', $pagecols+1, $block
131                                 unless $block eq 'No_Block';
132                         say '';
133                         $sameblock = $block;
134                         $colpos = 0;
135                 }
136
137                 if ($map) {
138                         # compare previous code point and indicate gaps
139                         state $lastcp = 0;
140                         if ($cp != ++$lastcp) {
141                                 if (!$colpos or $colpos++ % $pagecols > $pagecols - 3) {
142                                         # nearly last column, start new row
143                                         $colpos = 0;
144                                 }
145                                 else {
146                                         # mark repositioning in existing row
147                                         printf '<th>%X', $cp;
148                                 }
149                                 $lastcp = $cp;
150                         }
151                 }
152
153                 say sprintf '<tr><th>%X', $cp if $colpos++ % $pagecols == 0;
154
155                 my $info = $glyphs->glyph_info($cp);
156                 my ($class, $name, $mnem, $entity, $string) = @{$info};
157                 my $np = $class =~ /\bC\S\b/;  # noprint if control or invalid
158                 # display literal character, with placeholder circle if non-spacing/enclosing
159                 $string ||= ($class =~ /\bM[ne]\b/ && chr 9676) . chr($cp);
160                 my $html = $np ? !!$cover{$cp} && sprintf("&#%d;", $cp) :
161                         EscapeHTML($string);
162                 say sprintf '<td class="%s" title="U+%04X%s">%s',
163                         !$class ? ('l0', $cp, '', '') : (
164                         $cover{$cp} ? $np ? 'l2' : 'l5' : $np ? 'Xi' : 'l1',
165                         $cp, !!$name && ": $name",
166                         $html
167                         );
168         }
169         say '</table>';
170
171         exit;
172 }
173
174 :>
175 <h1>Font coverage</h1>
176
177 <p>
178 Character support of Unicode
179 <a href="/charset">blocks</a> and <a href="/unicode">presets</a>.
180 </p>
181
182 <div>
183
184 <:
185
186 my $cover = Data('data/unicode-cover');
187
188 my @ossel = @{ $cover->{osdefault} };
189 my @fontlist = map { @{ $cover->{os}->{$_} } } @ossel;
190
191 my @rows = (
192         'version/11',
193         'version/63',
194         'block/Latin-1 Supplement',
195         'block/Latin Extended-A',
196         'block/Latin Extended Additional',
197         'block/Latin Extended-B',
198         'script/Latin',
199         'script/Greek',
200         'script/Cyrillic',
201         'script/Arabic',
202         'script/Hebrew',
203         'script/Devanagari',
204         'script/Thai',
205         'script/Hangul',
206         'table/japanese',
207         'script/Han',
208         'table/ipa',
209         'table/punctuation',
210         'block/Dingbats',
211         'table/symbols',
212         'category/Sc', # currency
213         'table/math',
214         'category/Sm', # mathematical
215         'table/arrows/single',
216         'table/lines/single',
217         'table/block',
218         'table/lines',
219         'table/html',
220 );
221
222 if (my $group = $get{q}) {
223         my $grouprows = $cover->{$group}
224                 or Abort("Unknown character category $_", 404);
225         @rows = map { "$group/$_" } sort keys %{$grouprows};
226 }
227
228 # output character list
229
230 print '<table class="mapped cover">';
231 print '<col><col>';
232 print "<colgroup span=$_>"
233         for map { scalar @{ $cover->{os}->{$_} } } @ossel;
234
235 print '<thead><tr>';
236 print '<th colspan=2>';
237 for my $os (@ossel) {
238         my $osfonts = $cover->{os}->{$os};
239         my $osfont = $cover->{fonts}->[ $osfonts->[0] ]; # first font
240         printf '<td colspan=%d>%s', scalar @{$osfonts}, $osfont->{os} || ''
241 }
242
243 print '<tr>';
244 print '<th colspan=2>';
245 printf('<td title="%s"><a href="%s">%s</a>', map { EscapeHTML($_) }
246         join("\n", $_->{name}, $_->{description}),
247         "/font/$_->{file}",
248         $_->{abbr},
249 ) for @{ $cover->{fonts} }[@fontlist];
250 say '</thead>';
251
252 for (@rows) {
253         my ($group, $name) = split m{/}, $_, 2;
254         my $row = $cover->{$group}->{$name};
255
256         print '<tr>';
257         $name = sprintf 'Unicode v%.1f', $name / 10 if $group eq 'version';
258         $name = sprintf '<a href="%s">%s</a>', EncodeURI("/chars/$group/$name"), EscapeHTML($name)
259                 if $row->{count} and $row->{count} < 1280;
260         print '<th>', $name;
261         print '<td class=right>', $row->{count};
262         for my $count (@{ $row->{support} }[@fontlist]) {
263                 if (not defined $count) {
264                         print '<td class="l0">?';
265                         next;
266                 }
267                 if (not $count) {
268                         print '<td class="l1">✘';
269                         next;
270                 }
271                 if ($count == $row->{count}) {
272                         print '<td class="l5">✔';
273                         next;
274                 }
275
276                 my $rel = $count / $row->{count};
277                 my $class = $rel < .5 ? 2 : $rel < .9 ? 3 : 4;
278                 printf '<td class="%s">%d', "l$class", $rel*10;
279         }
280         say '</tr>';
281 }
282
283 say "</table>\n";
284
285 :></div>
286