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