common: bump version to 1.11
[sheet.git] / charset.plp
1 <(common.inc.plp)><:
2
3 my $mode = exists $get{compare};
4 my @tablist = split m{/+}, $Request || 'default';
5
6 Html({
7         title => 'charset cheat sheet',
8         version => '1.2',
9         description => [
10                 "Reference sheet with all glyphs in common character encoding tables,",
11                 "and an overview of Unicode ranges and UTF-8 bytes.",
12         ],
13         keywords => [qw'
14                 charset codepage unicode ascii utf8 latin glyph character encoding
15                 reference common overview table
16         '],
17         stylesheet => [qw'light'],
18         data => [qw(
19                 charset-encoding.inc.pl
20                 charset-unicode.inc.pl charset-ucplanes.inc.pl charset-utf8.inc.pl
21         )],
22 });
23
24 use List::Util qw( first pairmap pairfirst pairs );
25
26 :>
27 <h1>Character encodings</h1>
28
29 <p>
30 <:
31 if ($tablist[0] eq 'unicode') {
32         say "Detailed allocation of Unicode blocks.";
33         print "See ", showlink("charsets", '/charset'), " to compare specific encodings";
34 }
35 else {
36
37 if ($tablist[0] eq 'default') {
38         say "Overview of Unicode ", showlink("allocation", '/charset/unicode');
39         say "and common latin code pages.";
40         say "Compare alternate charsets:";
41 }
42 else {
43         say "Charset comparison:";
44 }
45
46 print join " •\n", (
47         map {
48                 join " ·\n", pairmap {
49                         showlink($b || ucfirst $a, '/charset'.($a && "/$a?compare"), $a eq $Request);
50                 } @{$_}
51         }
52         [
53                 iso      => 'ISO',
54                 win      => 'Windows',
55                 dos      => 'DOS',
56                 mac      => 'Apple',
57                 ebcdic   => 'EBCDIC',
58                 $tablist[0] eq 'default' ? () : ('' => 'common'),
59         ],
60         [
61                 westeur  => 'West',
62                 centeur  => 'Central',
63                 norteur  => 'North European',
64                 turkish  => 0,
65                 greek    => 0,
66                 cyrillic => 0,
67                 hebrew   => 0,
68         ],
69 );
70
71 }
72 :>.
73 </p>
74
75 <:
76 use POSIX qw( ceil );
77 use Shiar_Sheet::FormatChar;
78 my $glyphs = Shiar_Sheet::FormatChar->new;
79 my @request;
80
81 my $charsets = do 'charset-encoding.inc.pl'
82         or Alert('Encoding metadata could not be read', $@ || $!);
83
84 sub tabinput {
85         # generate character table(s)
86         my $input = shift or return;
87         my $params = $input =~ s/[+](.*)\z// ? $1 : undef;
88         my $charset = $charsets->{lc $input} || {};
89
90         if (ref $charset ne 'HASH') {
91                 $params and Alert("Parameters ignored for $input",
92                         "Cannot apply <q>$params</q> to multiple charsets.",
93                 );
94                 tabinput($_) for ref $charset ? @{$charset} : $charset;
95                 return;
96         }
97
98         state $visible = {'' => 1};  # all present tables
99         my %row = (offset => 0, cols => 16);
100
101         if (not defined $params) {
102                 my @parents = @{ $charset->{inherit} || [] };
103
104                 if (my ($parent, $part) = pairfirst { defined $visible->{$a} } @parents) {
105                         $row{parent} = $parent;
106                         $params = $part;
107                         $params = 80 unless $visible->{$parent}
108                                 or ($input eq 'MacCroatian' and defined $visible->{MacRomanian});
109                 }
110                 elsif (defined $visible->{ascii}) {
111                         $row{parent} = $parents[0];
112                         $params = $parents[1] // 80;
113                         $params = 80 if hex $params >= 0x80;  # ascii offset at most
114                 }
115                 elsif (@parents) {
116                         $row{parent} = $parents[0];
117                         $params = $parents[1] if hex $parents[1] == 0;  # apply ascii end
118                 }
119                 $visible->{$_} //= 0 for $row{parent} || ();
120         }
121
122         for my $param (split /[+]+/, $params // '') {
123                 if ($param eq 'realsize') {
124                         $row{realsize}++;
125                 }
126                 elsif ($param =~ m{ \A cols = (\d+) \z }x) {
127                         $row{cols} = $1;
128                 }
129                 elsif ($param =~ m{ \A (?<start> \p{AHex}+) (?: [-] (?<end> \p{AHex}+) )? \z }x) {
130                         if (defined $row{endpoint}) {
131                                 # extend earlier range
132                                 my $skip = int(($row{endpoint} || $row{startpoint}) / $row{cols});
133                                 for ($skip + 1 .. (hex($+{start}) / $row{cols}) - 1) {
134                                         $row{skip}->{ $_ * $row{cols} - $row{startpoint} }++;
135                                 }
136                         }
137                         else {
138                                 $row{startpoint} = hex $+{start};
139                         }
140                         $row{endpoint} = hex($+{end} || 0);
141                 }
142                 else {
143                         Alert("Unknown option <q>$param</q> for charset $input");
144                 }
145         }
146
147         if ($charset->{setup}) {
148                 eval { $charset->{setup}->(\%row) }
149                         or Alert("Incomplete setup of $input", $@);
150         }
151         $row{endpoint} ||= 0xFF;
152
153         if (defined $row{table} or defined $row{cell}) {
154                 $row{set} //= $input;
155         }
156         elsif ($row{set} = Encode::resolve_alias($input)) {
157                 $row{offset} = delete $row{startpoint};
158                 if ($charset->{varchar}) {
159                         # array of possibly multiple characters per code point
160                         $row{table} = [
161                                 map { Encode::decode($row{set}, pack 'C*', $_) } $row{offset} .. $row{endpoint}
162                         ];
163                 }
164                 else {
165                         # ~16x faster than decoding in loop;
166                         # substr strings is twice as fast as splitting to an array
167                         $row{table} = Encode::decode($row{set}, pack 'C*', $row{offset} .. $row{endpoint});
168                 }
169
170                 $row{endpoint} -= $row{offset};
171                 $visible->{ascii}++;  # assume common base
172         }
173         else {
174                 Alert("Encoding <q>$input</q> unknown");
175                 return;
176         }
177
178         if (my $replace = $charset->{replace}) {
179                 while (my ($offset, $sub) = each %{$replace}) {
180                         $offset -= $row{offset};
181
182                         if (ref $row{table} eq 'ARRAY') {
183                                 $row{table}->[$offset] = $sub
184                                         if $offset >= 0 and $offset <= $row{endpoint};
185                                 next;
186                         }
187
188                         my $length = length $sub;
189
190                         if ($offset < 0) {
191                                 $offset > -$length or next; # at least one character after start
192                                 # trim leftmost part to start at offset
193                                 substr($sub, 0, -$offset) = '';
194                                 $length += $offset;
195                                 $offset = 0;
196                         }
197
198                         if ((my $excess = $row{endpoint} - $offset - $length + 1) < 0) {
199                                 $excess > -$length or next;
200                                 # trim rightmost part to prevent overflow
201                                 substr($sub, $excess) = '';
202                                 $length += $excess;
203                         }
204
205                         substr($row{table}, $offset, $length) = $sub;
206                 }
207         }
208
209         push @request, \%row;
210         $visible->{ $row{set} } = 1 if $row{table};
211 }
212 tabinput($_) for @tablist;
213
214 my $NOCHAR = chr 0xFFFD;
215
216 sub range_cell {
217         my ($info, $offset) = @_;
218         my $table = $info->{cell} or return;
219         my $def = $table->{$offset} or return;
220         my ($len, $class, $name, $title) = @{$def};
221
222         my $cols = $info->{cols};
223         my $colsize = $table->{colsize} || 1;
224         my $attr = '';
225         $len /= $colsize;
226         $name //= $len <= 2 ? 'res' : 'reserved';
227
228         if (my $part = ($offset/$colsize - $info->{startpoint}) % $cols) {
229                 # continued row
230                 my $rest = $cols - $part;  # remaining
231                 $rest = $len if $len < $rest; #TODO: optimise
232                 if ($len -= $rest) {
233                         # continued on new row
234                         my @next = ($len * $colsize, "$class joinu");
235                         my $separate = $cols - $len > $rest;  # columns not on next row
236                         if ($len > $rest) {
237                                 # minority remains
238                                 push @next, $name, $title;
239                                 $title ||= $name;
240                                 $name = $separate && '…';
241                         }
242                         else {
243                                 # minority on next row
244                                 push @next, $separate && '"', $title || $name;
245                         }
246                         $table->{$offset + $colsize*$rest} //= \@next;
247                         $class .= ' joind';
248                 }
249                 $len = $rest;
250         }
251         elsif (my $rows = int($len / $cols)) {
252                 # multiple full rows
253                 my $rowsize = $colsize * $cols;
254                 if ($len -= $rows * $cols) {
255                         # partial row remains
256                         $table->{$offset + $rowsize * $rows} //= [$len*$colsize, "$class joinu", '', $title];
257                         $class .= ' joind';
258                 }
259
260                 unless ($info->{realsize}) {
261                         # coalesce multiple rows
262                         while ($rows > 3) {
263                                 $info->{skip}->{$offset += $rowsize}++;
264                                 $rows--;
265                         }
266                         if ($rows > 2) {
267                                 $info->{skip}->{$offset += $rowsize} = 0;
268                         }
269                 }
270
271                 $attr .= sprintf ' rowspan=%d', $rows;
272                 $len = $cols;
273         }
274
275         $attr .= sprintf ' colspan=%d', $len unless $len == 1;
276         $attr .= $1 if $class and $class =~ s/( \w+="[^"]*")//;
277         $attr .= sprintf ' class="%s"', $class if $class;
278         $attr .= sprintf ' title="%s"', EscapeHTML($title) if $title;
279         return "<td$attr>$name\n";
280 }
281
282 for my $row (@request) {
283         my $cols = $row->{cols};
284         my $colsize = $row->{cell} && $row->{cell}->{colsize} || 1;
285         my $coldigits = ceil(log($colsize * $cols) / log(16));  # uniform length of hexadecimal header
286         my $rowdiv = 16 ** $coldigits;  # row divide for column digits
287         $rowdiv = 1 if $rowdiv != $cols * $colsize;  # divide only if all columns are matched
288         my $offset = $row->{startpoint} * $colsize || 0;
289
290         printf '<div class="section"><table class="glyphs%s">', !$row->{cell} && ' charmap';
291         my $title = $row->{set};
292         $title .= " <aside>(over $_)</aside>"
293                 for $row->{parent} || ();
294         printf '<caption>%s</caption>', $title;
295         print '<col>' x ($cols + 1);
296         for my $section (qw{thead}) {
297                 print "<$section><tr><th>", $rowdiv == 1 ? '+' : '↱';
298                 printf '<th>%0*X', $coldigits, $_ * $colsize for 0 .. $cols - 1;
299                 print "\n";
300         }
301
302         print '<tbody>';
303         while ($offset <= $row->{endpoint} * $colsize) {
304                 if ($row->{skip}->{$offset}) {
305                         $offset += $cols * $colsize;
306                         next;
307                 }
308
309                 print '<tr><th>';
310                 if (defined $row->{skip}->{$offset}) {
311                         print '⋮';
312                 }
313                 else {
314                         if (my $rowmod = $offset % $rowdiv) {
315                                 # offset in column units
316                                 printf '<small>+%X</small>', $rowmod;
317                         }
318                         else {
319                                 # divided row offset
320                                 printf '%X', ($offset + $row->{offset}) / $rowdiv;
321                         }
322                 }
323                 say '';
324
325                 for (1 .. $cols) {
326                         if ($row->{cell}) {
327                                 print range_cell($row, $offset);
328                                 next;
329                         }
330
331                         my $cp = $offset + $row->{offset};
332                         my $glyph = ref $row->{table} eq 'ARRAY' ? $row->{table}->[$offset] :
333                                 substr $row->{table}, $offset, 1;
334                         my ($cell, $name, $class) = !defined $glyph || $glyph eq $NOCHAR ? () :
335                                 $glyphs->glyph_html($glyph);
336
337                         if ($mode) {
338                                 state $visible = {};
339                                 $class = (
340                                         $cp == ord $glyph ? 'l4' :
341                                         $row->{parent} && $glyph eq
342                                                 Encode::decode($row->{parent}, pack 'C', $cp) ? 'l3' :
343                                         !$class ? undef :
344                                         $visible->{$glyph} ? 'l2' :
345                                         'l1'
346                                 );
347                                 $visible->{$glyph}++;
348                         }
349
350                         say sprintf $class ? '<td title="%s" class="X %s">%s' : '<td title="%s">',
351                                 $name, $class, $cell;
352                 }
353                 continue {
354                         $offset += $colsize;
355                 }
356         }
357         say '</table></div>';
358 }
359
360 :>
361 <hr>
362
363 <div class="legend">
364         <table class="glyphs"><tr><: if ($mode) { :>
365         <td class="X l4">unicode
366         <td class="X l3">inherited
367         <td class="X l2">existing
368         <td class="X l1">original
369         <td class="">unassigned
370 <: } else { :>
371         <td class="X Cc">control
372         <td class="X Zs"><span>whitespace</span>
373         <td class="X Mn">diacritic<table class="glyphs"><tr>
374                 <td class="X Sk">letter
375                 </table>
376         <td class="X Po">punctuation<table class="glyphs"><tr>
377                 <td class="X Pf">quote
378                 </table>
379         <td class="X So">symbol<table class="glyphs"><tr>
380                 <td class="X Sm">math
381                 <td class="X Sc">currency
382                 </table>
383         <td class="X No">numeric
384         <td class="X Greek">greek<table class="glyphs"><tr>
385                 <td class="X Latin">latin
386                 <td class="X Cyrillic">cyrillic
387                 </table>
388         <td class="X Aramaic">aramaic<table class="glyphs"><tr>
389                 <td class="X Brahmic">brahmic
390                 <td class="X Arabic">arabic
391                 </table>
392         <td class="X Syllabic">syllabic<table class="glyphs"><tr>
393                 <td class="X African">african
394                 <td class="X Hiragana">japanese
395                 <td class="X Han">cjk
396                 <td class="X Bopomofo">chinese
397                 </table>
398         <td class="X Alpha">alphabetic
399         </table>
400
401         <table class="glyphs"><tr>
402         <td class="X">unicode 10.0
403         <td class="X Xr">proposed
404         <td class="X Xd">deprecated
405         <td class="">unassigned
406         <td class="X Xi">invalid
407 <: } :> </table>
408 </div>
409