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