font: fix code warning about sprintf arguments
[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.2',
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, @cover) = do "data/font/$font.inc.pl";
18         $fontmeta or Abort("Unknown font $font", '404 font not found');
19
20         my $map = eval {
21                 $get{map} or return;
22
23                 my $groupinfo = do 'data/unicode-cover.inc.pl' or die $@ || $!;
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 @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) } @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                 my $html = ($class =~ /\bM[ne]\b/ && chr 9676) . EscapeHTML(chr $cp);
160                 say sprintf '<td class="%s" title="U+%04X%s">%s',
161                         !$class ? ('l0', $cp, '', '') : (
162                         $cover{$cp} ? $np ? 'l2' : 'l5' : $np ? 'Xi' : 'l1',
163                         $cp, !!$name && ": $name",
164                         ($cover{$cp} || !$np) && $html
165                         );
166         }
167         say '</table>';
168
169         exit;
170 }
171
172 :>
173 <h1>Font coverage</h1>
174
175 <p>
176 Character support of Unicode
177 <a href="/charset">blocks</a> and <a href="/unicode">presets</a>.
178 </p>
179
180 <div>
181
182 <:
183
184 my $cover = do 'data/unicode-cover.inc.pl' or die $@ || $!;
185
186 my @ossel = @{ $cover->{osdefault} };
187 my @fontlist = map { @{ $cover->{os}->{$_} } } @ossel;
188
189 my @rows = (
190         'version/11',
191         'version/63',
192         'block/Latin-1 Supplement',
193         'block/Latin Extended-A',
194         'block/Latin Extended Additional',
195         'block/Latin Extended-B',
196         'script/Latin',
197         'script/Greek',
198         'script/Cyrillic',
199         'script/Arabic',
200         'script/Hebrew',
201         'script/Devanagari',
202         'script/Thai',
203         'script/Hangul',
204         'table/japanese',
205         'script/Han',
206         'table/ipa',
207         'table/punctuation',
208         'block/Dingbats',
209         'table/symbols',
210         'category/Sc', # currency
211         'table/math',
212         'category/Sm', # mathematical
213         'table/arrows/single',
214         'table/lines/single',
215         'table/block',
216         'table/lines',
217         'table/html',
218 );
219
220 if (my $group = $get{q}) {
221         my $grouprows = $cover->{$group}
222                 or Abort("Unknown character category $_", 404);
223         @rows = map { "$group/$_" } sort keys %{$grouprows};
224 }
225
226 # output character list
227
228 print '<table class="mapped cover">';
229 print '<col><col>';
230 print "<colgroup span=$_>"
231         for map { scalar @{ $cover->{os}->{$_} } } @ossel;
232
233 print '<thead><tr>';
234 print '<th colspan=2>';
235 for my $os (@ossel) {
236         my $osfonts = $cover->{os}->{$os};
237         my $osfont = $cover->{fonts}->[ $osfonts->[0] ]; # first font
238         printf '<td colspan=%d>%s', scalar @{$osfonts}, $osfont->{os} || ''
239 }
240
241 print '<tr>';
242 print '<th colspan=2>';
243 printf('<td title="%s"><a href="%s">%s</a>', map { EscapeHTML($_) }
244         join("\n", $_->{name}, $_->{description}),
245         "/font/$_->{file}",
246         $_->{abbr},
247 ) for @{ $cover->{fonts} }[@fontlist];
248 say '</thead>';
249
250 for (@rows) {
251         my ($group, $name) = split m{/}, $_, 2;
252         my $row = $cover->{$group}->{$name};
253
254         print '<tr>';
255         $name = sprintf 'Unicode v%.1f', $name / 10 if $group eq 'version';
256         $name = sprintf '<a href="%s">%s</a>', EncodeURI("/chars/$group/$name"), EscapeHTML($name)
257                 if $row->{count} and $row->{count} < 1280;
258         print '<th>', $name;
259         print '<td class=right>', $row->{count};
260         for my $count (@{ $row->{support} }[@fontlist]) {
261                 if (not defined $count) {
262                         print '<td class="l0">?';
263                         next;
264                 }
265                 if (not $count) {
266                         print '<td class="l1">✘';
267                         next;
268                 }
269                 if ($count == $row->{count}) {
270                         print '<td class="l5">✔';
271                         next;
272                 }
273
274                 my $rel = $count / $row->{count};
275                 my $class = $rel < .5 ? 2 : $rel < .9 ? 3 : 4;
276                 printf '<td class="%s">%d', "l$class", $rel*10;
277         }
278         say '</tr>';
279 }
280
281 say "</table>\n";
282
283 :></div>
284