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