font: html encode control characters
[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                 my $html = $np ? !!$cover{$cp} && sprintf("&#%d;", $cp) :
160                         ($class =~ /\bM[ne]\b/ && chr 9676) . EscapeHTML(chr $cp);
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