4 title => 'charset cheat sheet',
7 "Reference sheet with all glyphs in common character encoding tables,",
8 "and an overview of Unicode ranges and UTF-8 bytes.",
11 charset codepage unicode ascii utf8 latin glyph character encoding
12 reference common overview table
14 stylesheet => [qw'light'],
15 data => [qw'charset-unicode.inc.pl charset-ucplanes.inc.pl charset-utf8.inc.pl'],
18 my @tablist = split m{/+}, $Request || 'default';
20 use List::Util qw( first pairmap pairfirst pairs );
23 <h1>Character encodings</h1>
27 if ($tablist[0] eq 'default') {
28 say "Overview of Unicode allocation and common latin code pages.";
29 say "Compare alternate charsets:";
32 say "Charset comparison:";
36 my ($title, $href, $selected) = @_;
38 $selected ? '<strong>%s</strong>' : '<a href="%2$s">%s</a>',
39 EscapeHTML($title), $href
45 join " ·\n", pairmap {
46 optionlink($b || ucfirst $a, '/charset'.($a && "/$a?compare"), $a eq $Request);
55 $tablist[0] eq 'default' ? () : ('' => 'common'),
60 norteur => 'North European',
72 use Shiar_Sheet::FormatChar;
73 my $glyphs = Shiar_Sheet::FormatChar->new;
77 # generate character table(s)
78 my $input = shift or return;
81 default => [qw( u+0-27F utf-8+realsize iso-8859-1 iso-8859-15 cp1252 cp437 cp850 )],
82 unicode => [qw( uu+cols=32+realsize u+0-FFF u+1000-18AF u+18B0-1FFF )],
83 us => [qw( cp437 cp863 gsm0338 AdobeStandardEncoding )],
84 ebcdic => [qw( cp37 cp500 cp1047 posix-bc cp1026 cp875 )],
85 iso => [map {"iso-8859-$_"} 1 .. 11, 13 .. 16],
86 dos => [qw( cp437 cp865 cp861 cp860 cp863 cp850 cp857 cp852 cp775
87 cp737 cp869 cp866 cp855 cp862 cp864 )],
88 aix => [qw( cp1006 )],
89 win => [qw( cp1252 cp1250 cp1254 cp1257 cp1258 cp1253 cp1251 cp1255 cp1256 cp874 )],
90 mac => [qw( MacRoman MacRomanian MacRumanian MacCroatian MacCentralEurRoman MacTurkish MacIcelandic MacSami
91 MacGreek MacCyrillic MacHebrew MacArabic MacFarsi MacThai )],
94 westeur => [qw( iso-8859-1 iso-8859-15 cp1252 iso-8859-14 cp850 hp-roman8 nextstep MacRoman )],
95 centeur => [qw( iso-8859-2 iso-8859-16 cp1250 cp852 MacRomanian MacCroatian MacCentralEurRoman )], # MacRumanian only for DB
96 turkish => [qw( iso-8859-9 iso-8859-3 cp1254 cp857 MacTurkish )],
97 norteur => [qw( baltic nordic )],
98 baltic => [qw( iso-8859-4 iso-8859-13 cp1257 cp775 )],
99 nordic => [qw( iso-8859-10 cp865 cp861 MacIcelandic MacSami )],
100 cyrillic => [qw( koi8-r koi8-u koi8-f iso-8859-5 cp1251 MacCyrillic cp866 cp855
101 +400 +2DE0 +A640-A69F +500-52F )], # MacUkrainian is broken
102 arabic => [qw( iso-8859-6 cp1256 MacArabic cp864 cp1006 MacFarsi
103 +600 +8A0-8BF+8E0 +750-77F )],
104 greek => [qw( iso-8859-7 cp1253 MacGreek cp737 cp869 +370 +1F00 )],
105 hebrew => [qw( iso-8859-8 cp1255 MacHebrew cp862 +590 )],
106 thai => [qw( iso-8859-11 cp874 MacThai )],
107 vietnamese => [qw( viscii cp1258 MacVietnamese )],
108 symbols => [qw( symbol dingbats MacDingbats wingdings wingdings2 wingdings3 webdings )],
123 if (my $follow = $ALIAS->{$input}) {
124 tabinput($_) for ref $follow ? @{$follow} : $follow;
128 state $visible = {'' => 1}; # all present tables
129 my %row = (offset => 0, cols => 16);
130 my $params = $input =~ s/[+](.*)\z// ? $1 : undef;
132 if (not defined $params) {
134 'ascii' => ['' => '00-7F'],
135 'cp437' => ['cp850' => 0, 'ascii' => '00-1F+80'], # ascii range overridden later
136 'gsm0338' => ['ascii' => '00-7F'],
137 'dingbats' => ['' => '20-7F+A0'],
138 'MacDingbats' => ['dingbats' => '80-9F'],
139 'AdobeZdingbat'=> ['MacDingbats' => '80-9F'], # should be identical but maps to private use
140 'symbol' => ['' => '20-7F+A0'],
141 'AdobeSymbol' => ['symbol' => '20-7F+A0', '' => '20-7F+A0'], # minor differences, irrelevant except for different '€'
142 'wingdings' => ['' => '20'],
143 'wingdings2' => ['' => '20'],
144 'wingdings3' => ['' => '20'],
145 'webdings' => ['' => '20'],
147 'iso-8859-2' => ['iso-8859-1' => 'A0'],
148 'iso-8859-3' => ['iso-8859-1' => 'A0'], #TODO: also apply to iso-8859-9
149 'iso-8859-4' => ['iso-8859-2' => 'A0'],
150 'iso-8859-5' => ['iso-8859-1' => 'A0'],
151 'iso-8859-6' => ['cp1256' => '80', 'iso-8859-1' => 'A0'],
152 'iso-8859-7' => ['iso-8859-1' => 'A0'],
153 'iso-8859-8' => ['iso-8859-1' => 'A0'],
154 'iso-8859-9' => ['iso-8859-1' => 'D0-DF+F0'],
155 'iso-8859-10' => ['iso-8859-4' => 'A0'],
156 'iso-8859-11' => ['iso-8859-1' => 'A0'],
157 'iso-8859-13' => ['iso-8859-4' => 'A0'],
158 'iso-8859-14' => ['iso-8859-1' => 'A0'],
159 'iso-8859-15' => ['iso-8859-1' => 'A0-BF'],
160 'iso-8859-16' => ['iso-8859-2' => 'A0'],
161 'hp-roman8' => ['iso-8859-1' => 'A0'],
163 'cp1252' => ['iso-8859-1' => '80-9F'],
164 'cp1250' => ['iso-8859-2' => '80-BF', 'cp1252' => '80'],
165 'cp1254' => ['iso-8859-9' => '80-9F', 'cp1252' => '80-9F+D0'],
166 'cp874' => ['iso-8859-11' => '80-9F', 'cp1252' => '80'], # windows-874 actually cp1162
167 'cp1257' => ['iso-8859-13' => '80-9F+FF', 'cp1252' => '80'],
168 'cp1251' => ['cp1252' => '80'],
169 'cp1253' => ['cp1252' => '80'],
170 'cp1255' => ['iso-8859-8' => '80-DF', 'cp1252' => '80'],
171 'cp1256' => ['cp1252' => '80'],
172 'cp1258' => ['cp1252' => '80-9F+C0'],
174 'cp850' => ['cp437' => '90'],
175 'cp860' => ['cp437' => '80-AF'],
176 'cp861' => ['cp865' => '80-AF'],
177 'cp863' => ['cp437' => '80-AF'],
178 'cp865' => ['cp437' => '90-AF'],
179 'cp852' => ['cp850' => '80', 'cp437' => '80'],
180 'cp857' => ['cp850' => '80-AF+D0-EF', 'cp437' => '80'],
181 'cp775' => ['cp850' => '80'], # partial cp437
182 'cp866' => ['cp437' => '80-AF+E0'],
183 'cp855' => ['cp437' => '80'],
184 'cp1006' => ['iso-8859-6' => 'A0', 'cp437' => '80'],
185 'cp737' => ['cp437' => '80-AF+E0'],
186 'cp869' => ['cp437' => '80'],
187 'cp862' => ['cp437' => '80-9F'],
188 'cp864' => ['MacArabic' => '80', 'iso-8859-6' => '80', 'cp437' => '80'], #TODO: compare form variants
190 'koi8-u' => ['koi8-r' => '90-BF'],
191 'koi8-f' => ['koi8-u' => '90-BF'],
193 'MacRomanian' => ['MacRoman' => 'A0-BF+D0-DF'],
194 'MacRumanian' => ['MacRomanian' => 'A0-BF+D0-DF', 'MacRoman' => 'A0-BF+D0-DF'],
195 'MacCroatian' => ['MacRoman' => 'A0'],
196 'MacCentralEurRoman' => ['MacRoman' => '80'],
197 'MacIcelandic'=> ['MacRoman' => 'A0-AF+D0-EF'],
198 'MacTurkish' => ['MacRoman' => 'D0-DF'], # F5 is unassigned
199 'MacSami' => ['MacIcelandic' => '90-BF+D0-DF+F0', 'MacRoman' => '90'],
200 'MacGreek' => ['MacRoman' => '80'],
201 'MacCyrillic' => ['MacRoman' => '80'],
202 'MacHebrew' => ['iso-8859-8' => '80', 'MacRoman' => '80-8F+A0'], # partial ascii
203 'MacArabic' => ['iso-8859-6' => '80', 'cp864' => '80', 'MacRoman' => '80'], #TODO: multiple parents
204 'MacFarsi' => ['MacArabic' => 'B0-BF', 'MacRoman' => '80'],
206 'cp37' => ['posix-bc' => '00'],
207 'posix-bc' => ['cp1047' => '40'],
208 'cp500' => ['cp37' => '40-5F+B0-BF'],
209 'cp1047' => ['cp37' => '10-2F+50-5F+A0-BF'],
210 'cp1026' => ['cp37' => '40'],
211 'cp875' => ['cp37' => '30'],
215 my @parents = @{ $INHERIT->{$input} || [] };
217 if (my ($parent, $part) = pairfirst { defined $visible->{$a} } @parents) {
218 $row{parent} = $parent;
220 $params = 80 unless $visible->{$parent}
221 or ($input eq 'MacCroatian' and defined $visible->{MacRomanian});
223 elsif (defined $visible->{ascii}) {
224 $row{parent} = $parents[0];
225 $params = $parents[1] // 80;
226 $params = 80 if hex $params >= 0x80; # ascii offset at most
229 $row{parent} = $parents[0];
230 $params = $parents[1] if hex $parents[1] == 0; # apply ascii end
232 $visible->{$_} //= 0 for $row{parent} || ();
235 for my $param (split /[+]+/, $params // '') {
236 if ($param eq 'realsize') {
239 elsif ($param =~ m{ \A cols = (\d+) \z }x) {
242 elsif ($param =~ m{ \A (?<start> \p{AHex}+) (?: [-] (?<end> \p{AHex}+) )? \z }x) {
243 if (defined $row{endpoint}) {
244 # extend earlier range
245 my $skip = int(($row{endpoint} || $row{startpoint}) / $row{cols});
246 for ($skip + 1 .. (hex($+{start}) / $row{cols}) - 1) {
247 $row{skip}->{ $_ * $row{cols} - $row{startpoint} }++;
251 $row{startpoint} = hex $+{start};
253 $row{endpoint} = hex($+{end} || 0);
256 Alert("Unknown option <q>$param</q> for charset $input");
260 if ($input =~ m{ \A (?:wing|web)dings \d* \z }ix) {
261 eval "require Encode::\u$input";
265 $row{offset} = delete $row{startpoint};
266 $row{set} = 'Unicode characters';
267 my $block = $row{offset} >> 8;
268 $row{endpoint} ||= ($block + 1 << 8) - 1;
269 $block == ($row{endpoint} >> 8) or undef $block;
271 $row{table} = join '', map { chr } $row{offset} .. $row{endpoint};
272 utf8::upgrade($row{table}); # prevent latin1 output
274 $row{endpoint} -= $row{offset};
276 if (defined $block) {
277 $row{set} = sprintf 'Unicode block U+%02Xxx', $block;
278 $row{offset} %= 0x100;
281 elsif (lc $input eq 'uu') {
282 $row{cell} = do 'charset-ucplanes.inc.pl'
283 or Alert('Table data could not be read', $@ || $!);
284 $row{endpoint} ||= 0x3FF;
285 $row{set} = 'Unicode planes';
287 elsif (lc $input eq 'u') {
288 $row{cell} = do 'charset-unicode.inc.pl'
289 or Alert('Table data could not be read', $@ || $!);
291 $row{endpoint} ||= 0x1FFF;
292 $row{set} = 'Unicode ' . (
293 $row{startpoint} < 0x1000 && $row{endpoint} < 0x1000 ? 'BMP' :
294 $row{startpoint} >= 0x1000 && $row{endpoint} < 0x2000 ? 'SMP' :
298 elsif ($input =~ m/^utf-*8$/i) {
300 $row{cell} = do 'charset-utf8.inc.pl'
301 or Alert('Table data could not be read', $@ || $!);
302 $row{endpoint} = 0xFF;
304 elsif ($row{set} = Encode::resolve_alias($input)) {
305 $row{offset} = delete $row{startpoint};
306 $row{endpoint} ||= 0xFF;
307 if ($row{set} eq 'MacHebrew' or $row{set} eq 'MacThai') {
308 # array of possibly multiple characters per code point
310 map { Encode::decode($row{set}, pack 'C*', $_) } $row{offset} .. $row{endpoint}
314 # ~16x faster than decoding in loop;
315 # substr strings is twice as fast as splitting to an array
316 $row{table} = Encode::decode($row{set}, pack 'C*', $row{offset} .. $row{endpoint});
319 if ($row{set} eq 'cp437') {
320 if ($row{offset} <= 0xED and $row{endpoint} >= 0xED) {
322 substr($row{table}, 0xED - $row{offset}, 1) = 'ϕ';
324 if ($row{offset} < 0x20) {
325 # replace control characters by visible variants
326 my $sub = substr ' ☺☻♥♦♣♠•◘○◙♂♀♪♫☼►◄↕‼¶§▬↨↑↓→←∟↔▲▼', $row{offset};
327 substr($row{table}, 0, length $sub) = $sub;
330 elsif ($row{set} eq 'symbol') {
331 if ($row{offset} <= 0x60 and $row{endpoint} >= 0x60) {
332 # replace radical extender by closest unicode equivalent
333 substr($row{table}, 0x60 - $row{offset}, 1) = '│';
335 if ($row{offset} <= 0xBD and $row{endpoint} >= 0xFF) {
336 substr($row{table}, 0xBD - $row{offset}, 2) = '⏐⎯'; # arrow extenders
337 substr($row{table}, 0xD2 - $row{offset}, 3) = '®©™'; # serif variants
338 substr($row{table}, 0xE0 - $row{offset}, 1) = '◊'; # replace lookalike, should match AdobeSymbol
339 substr($row{table}, 0xE2 - $row{offset}, 3) = '®©™'; # sans-serif variants
340 substr($row{table}, 0xE6 - $row{offset}, 10) = '⎛⎜⎝⎡⎢⎣⎧⎨⎩⎪';
341 substr($row{table}, 0xF0 - $row{offset}, 1) = '€';
342 substr($row{table}, 0xF4 - $row{offset}, 11) = '⎮⌡⎞⎟⎠⎤⎥⎦⎫⎬⎭';
346 $row{endpoint} -= $row{offset};
348 $visible->{ascii} = # assume common base
349 $visible->{ $row{set} } = 1;
352 Alert("Encoding <q>$input</q> unknown");
355 push @request, \%row;
357 tabinput($_) for @tablist;
359 my $NOCHAR = chr 0xFFFD;
362 my ($info, $offset) = @_;
363 my $table = $info->{cell} or return;
364 my $def = $table->{$offset} or return;
365 my ($len, $class, $name, $title) = @{$def};
367 my $cols = $info->{cols};
368 my $colsize = $table->{colsize} || 1;
371 $name //= $len <= 2 ? 'res' : 'reserved';
373 if (my $part = ($offset/$colsize - $info->{startpoint}) % $cols) {
375 my $rest = $cols - $part; # remaining
376 $rest = $len if $len < $rest; #TODO: optimise
378 # continued on new row
379 my @next = ($len * $colsize, "$class joinu");
380 my $separate = $cols - $len > $rest; # columns not on next row
383 push @next, $name, $title;
385 $name = $separate && '…';
388 # minority on next row
389 push @next, $separate && '"', $title || $name;
391 $table->{$offset + $colsize*$rest} //= \@next;
396 elsif (my $rows = int($len / $cols)) {
398 my $rowsize = $colsize * $cols;
399 if ($len -= $rows * $cols) {
400 # partial row remains
401 $table->{$offset + $rowsize * $rows} //= [$len*$colsize, "$class joinu", '', $title];
405 unless ($info->{realsize}) {
406 # coalesce multiple rows
408 $info->{skip}->{$offset += $rowsize}++;
412 $info->{skip}->{$offset += $rowsize} = 0;
416 $attr .= sprintf ' rowspan=%d', $rows;
420 $attr .= sprintf ' colspan=%d', $len unless $len == 1;
421 $attr .= $1 if $class and $class =~ s/( \w+="[^"]*")//;
422 $attr .= sprintf ' class="%s"', $class if $class;
423 $attr .= sprintf ' title="%s"', EscapeHTML($title) if $title;
424 return "<td$attr>$name\n";
427 for my $row (@request) {
428 my $cols = $row->{cols};
429 my $colsize = $row->{cell} && $row->{cell}->{colsize} || 1;
430 my $coldigits = ceil(log($colsize * $cols) / log(16)); # uniform length of hexadecimal header
431 my $rowdiv = 16 ** $coldigits; # row divide for column digits
432 $rowdiv = 1 if $rowdiv != $cols * $colsize; # divide only if all columns are matched
433 my $offset = $row->{startpoint} * $colsize || 0;
435 printf '<div class="section"><table class="glyphs%s">', !$row->{cell} && ' charmap';
436 my $title = $row->{set};
437 $title .= " <aside>(over $_)</aside>"
438 for $row->{parent} || ();
439 printf '<caption>%s</caption>', $title;
440 print '<col>' x ($cols + 1);
441 for my $section (qw{thead}) {
442 print "<$section><tr><th>", $rowdiv == 1 ? '+' : '↱';
443 printf '<th>%0*X', $coldigits, $_ * $colsize for 0 .. $cols - 1;
448 while ($offset <= $row->{endpoint} * $colsize) {
449 if ($row->{skip}->{$offset}) {
450 $offset += $cols * $colsize;
455 if (defined $row->{skip}->{$offset}) {
459 if (my $rowmod = $offset % $rowdiv) {
460 # offset in column units
461 printf '<small>+%X</small>', $rowmod;
465 printf '%X', ($offset + $row->{offset}) / $rowdiv;
472 print range_cell($row, $offset);
476 my $cp = $offset + $row->{offset};
477 my $glyph = ref $row->{table} eq 'ARRAY' ? $row->{table}->[$offset] :
478 substr $row->{table}, $offset, 1;
479 my ($cell, $name, $class) = $glyph eq $NOCHAR ? () :
480 $glyphs->glyph_html($glyph);
482 if (exists $get{compare}) {
485 $cp == ord $glyph ? 'l4' :
486 $row->{parent} && $glyph eq
487 Encode::decode($row->{parent}, pack 'C', $cp) ? 'l3' :
489 $visible->{$glyph} ? 'l2' :
492 $visible->{$glyph}++;
495 say sprintf $class ? '<td title="%s" class="X %s">%s' : '<td title="%s">',
496 $name, $class, $cell;
502 say '</table></div>';
509 <table class="glyphs"><tr><: if (exists $get{compare}) { :>
510 <td class="X l4">unicode
511 <td class="X l3">inherited
512 <td class="X l2">existing
513 <td class="X l1">original
514 <td class="">unassigned
516 <td class="X Cc">control
517 <td class="X Zs"><span>whitespace</span>
518 <td class="X Mn">diacritic<table class="glyphs"><tr>
519 <td class="X Sk">letter
521 <td class="X Po">punctuation<table class="glyphs"><tr>
522 <td class="X Pf">quote
524 <td class="X So">symbol<table class="glyphs"><tr>
525 <td class="X Sm">math
526 <td class="X Sc">currency
528 <td class="X No">numeric
529 <td class="X Greek">greek<table class="glyphs"><tr>
530 <td class="X Latin">latin
531 <td class="X Cyrillic">cyrillic
533 <td class="X Aramaic">aramaic<table class="glyphs"><tr>
534 <td class="X Brahmic">brahmic
535 <td class="X Arabic">arabic
537 <td class="X Syllabic">syllabic<table class="glyphs"><tr>
538 <td class="X African">african
539 <td class="X Hiragana">japanese
540 <td class="X Han">cjk
541 <td class="X Bopomofo">chinese
543 <td class="X Alpha">alphabetic
546 <table class="glyphs"><tr>
547 <td class="X">unicode 7.0
548 <td class="X Xr">proposed
549 <td class="X Xd">deprecated
550 <td class="">unassigned
551 <td class="X Xi">invalid