charset: array table for multibyte MacHebrew
[sheet.git] / charset.plp
1 <(common.inc.plp)><:
2
3 Html({
4         title => 'charset cheat sheet',
5         version => '1.0',
6         description => [
7                 "Reference sheet with all glyphs in common character encoding tables,",
8                 "and an overview of Unicode ranges and UTF-8 bytes.",
9         ],
10         keywords => [qw'
11                 charset codepage unicode ascii utf8 latin glyph character encoding
12                 reference common overview table
13         '],
14         stylesheet => [qw'light'],
15         data => [qw'charset-unicode.inc.pl charset-utf8.inc.pl'],
16 });
17
18 my @tablist = split /[^\w-]+/, $Request || 'default';
19
20 :>
21 <h1>Character encoding</h1>
22
23 <:
24 use POSIX qw( ceil );
25 use Shiar_Sheet::FormatChar;
26 my $glyphs = Shiar_Sheet::FormatChar->new;
27 my @request;
28
29 sub tabinput {
30         # generate character table(s)
31         my $input = shift or return;
32
33         state $ALIAS = {
34                 default    => [qw( unicode+0-192 utf-8 iso-8859-1 iso-8859-15 cp1252 cp437 cp850 )],
35                 us         => [qw( cp437 cp863 AdobeStandardEncoding gsm0338 )],
36                 ebcdic     => [qw( cp37 cp500 cp875 cp1026 cp1047 posix-bc )],
37                 iso        => [map {"iso-8859-$_"} 1 .. 11, 13 .. 16],
38                 dos        => [qw( cp437 cp863 cp850 cp860 cp852 cp855 cp866 cp864 cp1006 cp737 cp869 cp862 cp862 cp857 cp865 cp861 cp874 cp775 )],
39
40                 westeur    => [qw( iso-8859-1 iso-8859-15 cp1252 iso-8859-14 cp850 hp-roman8 nextstep MacRoman )],
41                 centeur    => [qw( iso-8859-2 iso-8859-16 cp1250 cp852 MacRomanian MacCroatian MacCentralEurRoman )], # MacRumanian only for DB
42                 turkish    => [qw( iso-8859-9 iso-8859-3 cp1254 cp857 MacTurkish )],
43                 baltic     => [qw( iso-8859-4 iso-8859-13 cp1257 cp775 )],
44                 nordic     => [qw( iso-8859-10 cp865 cp861 MacIcelandic MacSami )],
45                 cyrillic   => [qw( koi8-f koi8-r koi8-u iso-8859-5 cp1251 MacCyrillic cp855 cp866 )], # MacUkrainian is broken
46                 arabic     => [qw( iso-8859-6 cp1006 cp864 cp1256 MacArabic )], # MacFarsi same as MacArabic?
47                 greek      => [qw( iso-8859-7 cp1253 MacGreek cp737 cp869 )],
48                 hebrew     => [qw( iso-8859-8 cp1255 MacHebrew cp862 )],
49                 thai       => [qw( iso-8859-11 cp874 MacThai )],
50                 vietnamese => [qw( viscii cp1258 MacVietnamese )],
51                 symbol     => [qw( symbol dingbats MacDingbats AdobeZdingbat AdobeSymbol )],
52                 # iso-code shorthand
53                 1 => 'westeur',
54                 2 => 'centeur',
55                 3 => 'turkish',
56                 4 => 'baltic',
57                 5 => 'cyrillic',
58                 6 => 'arabic',
59                 7 => 'greek',
60                 8 => 'hebrew',
61                 9 => 'turkish',
62                 10 => 'nordic',
63                 11 => 'thai',
64         };
65         if (my $follow = $ALIAS->{$input}) {
66                 tabinput($_) for ref $follow ? @{$follow} : $follow;
67                 return;
68         }
69
70         state $visible = {};  # all present tables
71         my %row = (offset => 0, cols => 16);
72         my $endpoint = 255;
73         my $params = $input =~ s/[+](.*)\z// ? $1 : undef;
74
75         if (not defined $params) {
76                 use List::Util qw( first pairfirst pairs );
77
78                 state $INHERIT = {
79                         'cp437'       => ['cp850' => 0], # ascii range overridden later
80                         'gsm0338'     => ['ascii' => '0-127'],
81
82                         'iso-8859-2'  => ['iso-8859-1' => '160'],
83                         'iso-8859-3'  => ['iso-8859-1' => '160'],
84                         'iso-8859-4'  => ['iso-8859-2' => '160'],
85                         'iso-8859-5'  => ['iso-8859-1' => '160'],
86                         'iso-8859-6'  => ['iso-8859-1' => '160'],
87                         'iso-8859-7'  => ['iso-8859-1' => '160'],
88                         'iso-8859-8'  => ['iso-8859-1' => '160'],
89                         'iso-8859-9'  => ['iso-8859-1' => '208'],
90                         'iso-8859-10' => ['iso-8859-4' => '160'],
91                         'iso-8859-11' => ['iso-8859-1' => '160'],
92                         'iso-8859-13' => ['iso-8859-4' => '160'],
93                         'iso-8859-14' => ['iso-8859-1' => '160'],
94                         'iso-8859-15' => ['iso-8859-1' => '160-191'],
95                         'iso-8859-16' => ['iso-8859-2' => '160'],
96
97                         'cp1252'      => ['iso-8859-1' => '128-159'],
98                         'cp1250'      => ['iso-8859-2' => '128-191'],
99                         'cp1254'      => ['iso-8859-9' => '128-159'],
100                         'cp1257'      => ['iso-8859-13' => '128-'],
101
102                         'cp850'       => ['cp437' => '144'],
103                         'cp865'       => ['cp437' => '144-176'],
104                         'cp852'       => ['cp850' => '128', 'cp437' => '128'],
105                         'cp857'       => ['cp850' => '128-239', 'cp437' => '128'],
106                         'cp775'       => ['cp850' => '128'],  # partial cp437
107
108                         'MacRomanian' => ['MacRoman' => '160'],
109                         'MacCroatian' => ['MacRoman' => '160'],
110                         'MacCentralEurRoman' => ['MacRoman' => '128'],
111                         'MacTurkish'  => ['MacRoman' => '208-223'], # F5 is unassigned
112                 };
113
114                 my @parents = @{ $INHERIT->{$input} || [] };
115
116                 if (my ($parent, $part) = pairfirst { $visible->{$a} } @parents) {
117                         $row{parent} = $parent;
118                         $params = $part;
119                 }
120                 elsif ($visible->{ascii}) {
121                         $row{parent} = $parents[0] || 'ascii';
122                         $params = $parents[1] || 128;
123                         $params = 128 if $params >= 128;  # ascii offset at most
124                 }
125                 elsif (@parents) {
126                         $row{parent} = $parents[0];
127                 }
128         }
129
130         if (defined $params and $params =~ m/^ (\d+) (-\d+)? /x) {
131                 $row{offset} = $1;
132                 $endpoint = -$2 if $2;
133         }
134
135         if ($input =~ /^U([0-9a-f]+)(?:-([0-9a-f]+))?/) {
136                 my $start = hex($1) << ($2 ? 4 : 8);
137                 my $end = $2 ? hex($2) << 4 : $start + 240;
138                 $row{table} = join '', map { chr } $start .. $end+15;
139                 utf8::upgrade($row{table});  # prevent latin1 output
140                 $row{len} = length $row{table};
141                 $row{set} = sprintf 'Unicode block U+%02Xxx', $start >> 8;
142         }
143         elsif ($input eq 'U') {
144                 $row{len} = 1024;
145                 $row{set} = 'Unicode planes';
146                 $row{cell} = do 'charset-ucplanes.inc.pl'
147                         or Alert('Table data could not be read', $@ || $!);
148                 $row{cols} *= 2;
149         }
150         elsif ($row{set} = Encode::resolve_alias($input)) {
151                 if ($row{set} eq 'Internal') {
152                         $row{len} = $endpoint < 255 ? 640 : 8192;
153                         $row{set} = 'Unicode BMP';
154                         $row{cell} = do 'charset-unicode.inc.pl'
155                                 or Alert('Table data could not be read', $@ || $!);
156                 }
157                 elsif ($row{set} eq 'utf-8-strict') {
158                         $row{len} = 256;
159                         $row{set} = 'UTF-8';
160                         $row{cell} = do 'charset-utf8.inc.pl'
161                                 or Alert('Table data could not be read', $@ || $!);
162                 }
163                 else {
164                         if ($row{set} eq 'MacHebrew') {
165                                 # array of possibly multiple characters per code point
166                                 $row{table} = [
167                                         map { Encode::decode($row{set}, pack 'C*', $_) } $row{offset} .. $endpoint
168                                 ];
169                         }
170                         else {
171                                 # ~16x faster than decoding in loop;
172                                 # substr strings is twice as fast as splitting to an array
173                                 $row{table} = Encode::decode($row{set}, pack 'C*', $row{offset} .. $endpoint);
174                         }
175                         $row{len} = $endpoint - $row{offset};
176
177                         if ($row{set} eq 'cp437' and !$row{offset}) {
178                                 substr($row{table}, 237, 1) = pack 'U*', 0x3D5; # phi sign
179                                 substr($row{table}, 0, 32) = pack 'U*', map {hex} qw(
180                                         2007 263A 263B 2665 2666 2663 2660 2022
181                                         25D8 25CB 25D9 2642 2640 266A 266B 263C
182                                         25BA 25C4 2195 203C 00B6 00A7 25AC 21A8
183                                         2191 2193 2192 2190 221F 2194 25B2 25BC
184                                 );
185                         }
186
187                         $visible->{ascii} =  # assume common base
188                         $visible->{ $row{set} } = 1;
189                 }
190         }
191         else {
192                 Alert("Encoding <q>$input</q> unknown");
193                 return;
194         }
195         push @request, \%row;
196 }
197 tabinput($_) for @tablist;
198
199 my $NOCHAR = chr 0xFFFD;
200
201 sub range_cell {
202         my ($info, $offset) = @_;
203         my $table = $info->{cell} or return;
204         my $def = $table->{$offset} or return;
205         my ($len, $class, $name, $title) = @{$def};
206
207         my $cols = $info->{cols};
208         my $colsize = $table->{colsize} || 1;
209         my $attr = '';
210         $len /= $colsize;
211         $name //= $len <= 2 ? 'res' : 'reserved';
212
213         if (my $part = $offset/$colsize % $cols) {
214                 # continued row
215                 my $rest = $cols - $part;  # remaining
216                 $rest = $len if $len < $rest; #TODO: optimise
217                 if ($len -= $rest) {
218                         # continued on new row
219                         my @next = ($len * $colsize, "$class joinu");
220                         if ($len > $rest) {
221                                 # minority remains
222                                 push @next, $name, $title;
223                                 $title ||= $name;
224                                 $name = '';
225                         }
226                         else {
227                                 # minority on next row
228                                 push @next, '"', $title || $name;
229                         }
230                         $table->{$offset + $colsize*$rest} //= \@next;
231                         $class .= ' joind';
232                 }
233                 $len = $rest;
234         }
235         elsif (my $rows = int($len / $cols)) {
236                 # multiple full rows
237                 if ($len -= $rows * $cols) {
238                         # partial row remains
239                         $table->{$offset + $colsize*$rows * $cols} //= [$len*$colsize, "$class joinu", '', $title];
240                         $class .= ' joind';
241                 }
242                 $attr .= sprintf ' rowspan=%d', $rows;
243                 $len = $cols;
244         }
245
246         $attr .= sprintf ' colspan=%d', $len unless $len == 1;
247         $attr .= $1 if $class and $class =~ s/( \w+="[^"]*")//;
248         $attr .= sprintf ' class="%s"', $class if $class;
249         $attr .= sprintf ' title="%s"', EscapeHTML($title) if $title;
250         return "<td$attr>$name";
251 }
252
253 for my $row (@request) {
254         my $cols = $row->{cols};
255         my $colsize = $row->{cell} && $row->{cell}->{colsize} || 1;
256         my $coldigits = ceil(log($colsize * $cols) / log(16));  # uniform length of hexadecimal header
257         my $rowdiv = 16 ** $coldigits;  # row divide for column digits
258         $rowdiv = 1 if $rowdiv != $cols * $colsize;  # divide only if all columns are matched
259         my $offset = 0;
260         my $endpoint = $offset + $row->{len} * $colsize;
261
262         printf '<div class="section"><table class="glyphs%s">', !$row->{cell} && ' charmap';
263         my $title = $row->{set};
264         $title .= " <aside>(over $_)</aside>"
265                 for grep { $_ ne 'iso-8859-1' } $row->{parent} // ();
266         printf '<caption>%s</caption>', $title;
267         print '<col>' x ($cols + 1);
268         for my $section (qw{thead}) {
269                 print "<$section><tr><th>", $rowdiv == 1 ? '+' : '↱';
270                 printf '<th>%0*X', $coldigits, $_ * $colsize for 0 .. $cols - 1;
271                 print "\n";
272         }
273         print '<tbody>';
274         while ($offset < $endpoint - 1) {
275                 print '<tr><th>';
276                 {
277                         if (my $rowmod = $offset % $rowdiv) {
278                                 # offset in column units
279                                 printf '<small>+%X</small>', $rowmod;
280                         }
281                         else {
282                                 # divided row offset
283                                 printf '%X', ($offset + $row->{offset}) / $rowdiv;
284                         }
285                 }
286                 for (1 .. $cols) {
287                         if ($row->{cell}) {
288                                 print range_cell($row, $offset);
289                                 next;
290                         }
291
292                         my $glyph = ref $row->{table} eq 'ARRAY' ? $row->{table}->[$offset] :
293                                 substr $row->{table}, $offset, 1;
294                         if ($glyph eq $NOCHAR) {
295                                 print '<td>';
296                                 next;
297                         }
298
299                         print "\n".$glyphs->glyph_cell($glyph);
300                 }
301                 continue {
302                         $offset += $colsize;
303                 }
304                 print "\n";
305         }
306         say '</table></div>';
307 }
308
309 :>
310 <hr>
311
312 <div class="legend">
313         <table class="glyphs"><tr>
314         <td class="X Cc">control
315         <td class="X Zs"><span>whitespace</span>
316         <td class="X Mn">diacritic<table class="glyphs"><tr>
317                 <td class="X Sk">letter
318                 </table>
319         <td class="X Po">punctuation<table class="glyphs"><tr>
320                 <td class="X Pf">quote
321                 </table>
322         <td class="X So">symbol<table class="glyphs"><tr>
323                 <td class="X Sm">math
324                 <td class="X Sc">currency
325                 </table>
326         <td class="X No">numeric
327         <td class="X Greek">greek<table class="glyphs"><tr>
328                 <td class="X Latin">latin
329                 <td class="X Cyrillic">cyrillic
330                 </table>
331         <td class="X Aramaic">aramaic<table class="glyphs"><tr>
332                 <td class="X Brahmic">brahmic
333                 <td class="X Arabic">arabic
334                 </table>
335         <td class="X Syllabic">syllabic<table class="glyphs"><tr>
336                 <td class="X African">african
337                 <td class="X Hiragana">japanese
338                 <td class="X Han">cjk
339                 <td class="X Bopomofo">chinese
340                 </table>
341         <td class="X Alpha">alphabetic
342         </table>
343
344         <table class="glyphs"><tr>
345         <td class="X">unicode 7.0
346         <td class="X Xr">proposed
347         <td class="X Xd">deprecated
348         <td class="">unassigned
349         <td class="X Xi">invalid
350         </table>
351 </div>
352