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-639 utf-8+realsize iso-8859-1 iso-8859-15 cp1252 cp437 cp850 )],
82 unicode => [qw( uu+cols=32+realsize u+0-4095 u+4096-6319 u+6320-8191 )],
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 U4 U2DE-2DF UA64-A69 U50-52 )], # MacUkrainian is broken
102 arabic => [qw( iso-8859-6 cp1256 MacArabic cp864 cp1006 MacFarsi
103 U6 U8A-8F+0-31+64 U75-77 )],
104 greek => [qw( iso-8859-7 cp1253 MacGreek cp737 cp869 U37-3F U1F )],
105 hebrew => [qw( iso-8859-8 cp1255 MacHebrew cp862 U59-5F )],
106 thai => [qw( iso-8859-11 cp874 MacThai )],
107 vietnamese => [qw( viscii cp1258 MacVietnamese )],
108 symbols => [qw( symbol dingbats MacDingbats wingdings wingdings2 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 'cp437' => ['cp850' => 0, 'ascii' => '0-31+128'], # ascii range overridden later
135 'gsm0338' => ['ascii' => '0-127'],
136 'dingbats' => ['' => '32-127+160'],
137 'MacDingbats' => ['dingbats' => '128-159'],
138 'AdobeZdingbat'=> ['MacDingbats' => '128-159'], # should be identical but maps to private use
139 'symbol' => ['' => '32-127+160'],
140 'AdobeSymbol' => ['symbol' => '32-127+160', '' => '32-127+160'], # minor differences, irrelevant except for different '€'
141 'wingdings' => ['' => '32'],
142 'wingdings2' => ['' => '32'],
143 'webdings' => ['' => '32'],
145 'iso-8859-2' => ['iso-8859-1' => '160'],
146 'iso-8859-3' => ['iso-8859-1' => '160'], #TODO: also apply to iso-8859-9
147 'iso-8859-4' => ['iso-8859-2' => '160'],
148 'iso-8859-5' => ['iso-8859-1' => '160'],
149 'iso-8859-6' => ['cp1256' => '128', 'iso-8859-1' => '160'],
150 'iso-8859-7' => ['iso-8859-1' => '160'],
151 'iso-8859-8' => ['iso-8859-1' => '160'],
152 'iso-8859-9' => ['iso-8859-1' => '208-223+240'],
153 'iso-8859-10' => ['iso-8859-4' => '160'],
154 'iso-8859-11' => ['iso-8859-1' => '160'],
155 'iso-8859-13' => ['iso-8859-4' => '160'],
156 'iso-8859-14' => ['iso-8859-1' => '160'],
157 'iso-8859-15' => ['iso-8859-1' => '160-191'],
158 'iso-8859-16' => ['iso-8859-2' => '160'],
159 'hp-roman8' => ['iso-8859-1' => '160'],
161 'cp1252' => ['iso-8859-1' => '128-159'],
162 'cp1250' => ['iso-8859-2' => '128-191', 'cp1252' => '128'],
163 'cp1254' => ['iso-8859-9' => '128-159', 'cp1252' => '128-159+208'],
164 'cp874' => ['iso-8859-11' => '128-159', 'cp1252' => '128'], # windows-874 actually cp1162
165 'cp1257' => ['iso-8859-13' => '128-159+255', 'cp1252' => '128'],
166 'cp1251' => ['cp1252' => '128'],
167 'cp1253' => ['cp1252' => '128'],
168 'cp1255' => ['iso-8859-8' => '128-223', 'cp1252' => '128'],
169 'cp1256' => ['cp1252' => '128'],
170 'cp1258' => ['cp1252' => '128-159+192'],
172 'cp850' => ['cp437' => '144'],
173 'cp860' => ['cp437' => '128-175'],
174 'cp861' => ['cp865' => '128-175'],
175 'cp863' => ['cp437' => '128-175'],
176 'cp865' => ['cp437' => '144-175'],
177 'cp852' => ['cp850' => '128', 'cp437' => '128'],
178 'cp857' => ['cp850' => '128-175+208-239', 'cp437' => '128'],
179 'cp775' => ['cp850' => '128'], # partial cp437
180 'cp866' => ['cp437' => '128-175+224'],
181 'cp855' => ['cp437' => '128'],
182 'cp1006' => ['iso-8859-6' => '160', 'cp437' => '128'],
183 'cp737' => ['cp437' => '128-175+224'],
184 'cp869' => ['cp437' => '128'],
185 'cp862' => ['cp437' => '128-159'],
186 'cp864' => ['MacArabic' => '128', 'iso-8859-6' => '128', 'cp437' => '128'], #TODO: compare form variants
188 'koi8-u' => ['koi8-r' => '144-191'],
189 'koi8-f' => ['koi8-u' => '144-191'],
191 'MacRomanian' => ['MacRoman' => '160-191+208-223'],
192 'MacRumanian' => ['MacRomanian' => '160-191+208-223', 'MacRoman' => '160-191+208-223'],
193 'MacCroatian' => ['MacRoman' => '160'],
194 'MacCentralEurRoman' => ['MacRoman' => '128'],
195 'MacIcelandic'=> ['MacRoman' => '160-175+208-239'],
196 'MacTurkish' => ['MacRoman' => '208-223'], # F5 is unassigned
197 'MacSami' => ['MacIcelandic' => '144-191+208-223+240', 'MacRoman' => '144'],
198 'MacGreek' => ['MacRoman' => '128'],
199 'MacCyrillic' => ['MacRoman' => '128'],
200 'MacHebrew' => ['iso-8859-8' => '128', 'MacRoman' => '128-143+160'], # partial ascii
201 'MacArabic' => ['iso-8859-6' => '128', 'cp864' => '128', 'MacRoman' => '128'], #TODO: multiple parents
202 'MacFarsi' => ['MacArabic' => '176-191', 'MacRoman' => '128'],
204 'cp37' => ['posix-bc' => '0'],
205 'posix-bc' => ['cp1047' => '64'],
206 'cp500' => ['cp37' => '64-95+176-191'],
207 'cp1047' => ['cp37' => '16-47+80-95+160-191'],
208 'cp1026' => ['cp37' => '64'],
209 'cp875' => ['cp37' => '48'],
213 my @parents = @{ $INHERIT->{$input} || [] };
215 if (my ($parent, $part) = pairfirst { defined $visible->{$a} } @parents) {
216 $row{parent} = $parent;
218 $params = 128 unless $visible->{$parent}
219 or ($input eq 'MacCroatian' and defined $visible->{MacRomanian});
221 elsif (defined $visible->{ascii}) {
222 $row{parent} = $parents[0];
223 $params = $parents[1] // 128;
224 $params = 128 if $params >= 128; # ascii offset at most
227 $row{parent} = $parents[0];
228 $params = $parents[1] if $parents[1] == 0; # apply ascii end
230 $visible->{$_} //= 0 for $row{parent} || ();
233 for my $param (split /[+]+/, $params // '') {
234 if ($param eq 'realsize') {
237 elsif ($param =~ m{ \A cols = (\d+) \z }x) {
240 elsif ($param =~ m{ \A (?<offset> \d+) (?: [-] (?<endpoint> \d+) )? \z }x) {
241 if (defined $row{endpoint}) {
242 # extend earlier range
243 my $skip = int(($row{endpoint} || $row{offset}) / $row{cols});
244 for ($skip + 1 .. ($+{offset} / $row{cols}) - 1) {
245 $row{skip}->{ $_ * $row{cols} - $row{offset} }++;
249 $row{offset} = $+{offset};
251 $row{endpoint} = $+{endpoint} || 0;
254 Alert("Unknown option <q>$param</q> for charset $input");
258 if ($input =~ m{ \A (?:wing|web)dings \d* \z }ix) {
259 eval "require Encode::\u$input";
262 if ($input =~ m{ \A u ([0-9a-f]+) (?:-([0-9a-f]+))? \z }ix) {
263 my $start = hex($1) << ($2 ? 4 : 8);
264 my $end = $2 ? (hex($2) << 4) + $row{cols} - 1 : $start + 255;
265 $row{table} = join '', map { chr } $start .. $end;
266 utf8::upgrade($row{table}); # prevent latin1 output
267 $row{endpoint} = $end - $start;
268 $row{set} = sprintf 'Unicode block U+%02Xxx', $start >> 8;
269 $row{offset} = $start % 256;
271 elsif (lc $input eq 'uu') {
272 $row{set} = 'Unicode planes';
273 $row{cell} = do 'charset-ucplanes.inc.pl'
274 or Alert('Table data could not be read', $@ || $!);
275 $row{endpoint} = 1023 * $row{cell}->{colsize};
277 elsif (lc $input eq 'u') {
278 $row{cell} = do 'charset-unicode.inc.pl'
279 or Alert('Table data could not be read', $@ || $!);
281 $row{endpoint} ||= 8191;
282 $row{endpoint} *= $row{cell}->{colsize};
283 $row{startpoint} = $row{cell}->{colsize} * $row{offset};
285 $row{set} = 'Unicode ' . (
286 $row{startpoint} < 0x10000 && $row{endpoint} < 0x10000 ? 'BMP' :
287 $row{startpoint} >= 0x10000 && $row{endpoint} < 0x20000 ? 'SMP' :
291 elsif ($input =~ m/^utf-*8$/i) {
293 $row{cell} = do 'charset-utf8.inc.pl'
294 or Alert('Table data could not be read', $@ || $!);
295 $row{endpoint} = 255;
297 elsif ($row{set} = Encode::resolve_alias($input)) {
298 $row{endpoint} ||= 255;
299 if ($row{set} eq 'MacHebrew' or $row{set} eq 'MacThai') {
300 # array of possibly multiple characters per code point
302 map { Encode::decode($row{set}, pack 'C*', $_) } $row{offset} .. $row{endpoint}
306 # ~16x faster than decoding in loop;
307 # substr strings is twice as fast as splitting to an array
308 $row{table} = Encode::decode($row{set}, pack 'C*', $row{offset} .. $row{endpoint});
310 $row{endpoint} -= $row{offset};
312 if ($row{set} eq 'cp437') {
313 if ($row{offset} <= 0xED and $row{endpoint} >= 0xED - $row{offset}) {
315 substr($row{table}, 0xED - $row{offset}, 1) = 'ϕ';
317 if ($row{offset} < 0x20) {
318 # replace control characters by visible variants
319 my $sub = substr ' ☺☻♥♦♣♠•◘○◙♂♀♪♫☼►◄↕‼¶§▬↨↑↓→←∟↔▲▼', $row{offset};
320 substr($row{table}, 0, length $sub) = $sub;
323 elsif ($row{set} eq 'symbol') {
324 if ($row{offset} <= 0x60 and $row{endpoint} >= 0x60 - $row{offset}) {
325 # replace radical extender by closest unicode equivalent
326 substr($row{table}, 0x60 - $row{offset}, 1) = '│';
328 if ($row{offset} <= 0xBD and $row{endpoint} >= 0xFF - $row{offset}) {
329 substr($row{table}, 0xBD - $row{offset}, 2) = '⏐⎯'; # arrow extenders
330 substr($row{table}, 0xD2 - $row{offset}, 3) = '®©™'; # serif variants
331 substr($row{table}, 0xE0 - $row{offset}, 1) = '◊'; # replace lookalike, should match AdobeSymbol
332 substr($row{table}, 0xE2 - $row{offset}, 3) = '®©™'; # sans-serif variants
333 substr($row{table}, 0xE6 - $row{offset}, 10) = '⎛⎜⎝⎡⎢⎣⎧⎨⎩⎪';
334 substr($row{table}, 0xF0 - $row{offset}, 1) = '€';
335 substr($row{table}, 0xF4 - $row{offset}, 11) = '⎮⌡⎞⎟⎠⎤⎥⎦⎫⎬⎭';
339 $visible->{ascii} = # assume common base
340 $visible->{ $row{set} } = 1;
343 Alert("Encoding <q>$input</q> unknown");
346 push @request, \%row;
348 tabinput($_) for @tablist;
350 my $NOCHAR = chr 0xFFFD;
353 my ($info, $offset) = @_;
354 my $table = $info->{cell} or return;
355 my $def = $table->{$offset} or return;
356 my ($len, $class, $name, $title) = @{$def};
358 my $cols = $info->{cols};
359 my $colsize = $table->{colsize} || 1;
362 $name //= $len <= 2 ? 'res' : 'reserved';
364 if (my $part = $offset/$colsize % $cols) {
366 my $rest = $cols - $part; # remaining
367 $rest = $len if $len < $rest; #TODO: optimise
369 # continued on new row
370 my @next = ($len * $colsize, "$class joinu");
371 my $separate = $cols - $len > $rest; # columns not on next row
374 push @next, $name, $title;
376 $name = $separate && '…';
379 # minority on next row
380 push @next, $separate && '"', $title || $name;
382 $table->{$offset + $colsize*$rest} //= \@next;
387 elsif (my $rows = int($len / $cols)) {
389 my $rowsize = $colsize * $cols;
390 if ($len -= $rows * $cols) {
391 # partial row remains
392 $table->{$offset + $rowsize * $rows} //= [$len*$colsize, "$class joinu", '', $title];
396 unless ($info->{realsize}) {
397 # coalesce multiple rows
399 $info->{skip}->{$offset += $rowsize}++;
403 $info->{skip}->{$offset += $rowsize} = 0;
407 $attr .= sprintf ' rowspan=%d', $rows;
411 $attr .= sprintf ' colspan=%d', $len unless $len == 1;
412 $attr .= $1 if $class and $class =~ s/( \w+="[^"]*")//;
413 $attr .= sprintf ' class="%s"', $class if $class;
414 $attr .= sprintf ' title="%s"', EscapeHTML($title) if $title;
415 return "<td$attr>$name\n";
418 for my $row (@request) {
419 my $cols = $row->{cols};
420 my $colsize = $row->{cell} && $row->{cell}->{colsize} || 1;
421 my $coldigits = ceil(log($colsize * $cols) / log(16)); # uniform length of hexadecimal header
422 my $rowdiv = 16 ** $coldigits; # row divide for column digits
423 $rowdiv = 1 if $rowdiv != $cols * $colsize; # divide only if all columns are matched
424 my $offset = $row->{startpoint} || 0;
426 printf '<div class="section"><table class="glyphs%s">', !$row->{cell} && ' charmap';
427 my $title = $row->{set};
428 $title .= " <aside>(over $_)</aside>"
429 for $row->{parent} || ();
430 printf '<caption>%s</caption>', $title;
431 print '<col>' x ($cols + 1);
432 for my $section (qw{thead}) {
433 print "<$section><tr><th>", $rowdiv == 1 ? '+' : '↱';
434 printf '<th>%0*X', $coldigits, $_ * $colsize for 0 .. $cols - 1;
438 while ($offset < $row->{endpoint}) {
439 if ($row->{skip}->{$offset}) {
440 $offset += $cols * $colsize;
445 if (defined $row->{skip}->{$offset}) {
449 if (my $rowmod = $offset % $rowdiv) {
450 # offset in column units
451 printf '<small>+%X</small>', $rowmod;
455 printf '%X', ($offset + $row->{offset}) / $rowdiv;
462 print range_cell($row, $offset);
466 my $cp = $offset + $row->{offset};
467 my $glyph = ref $row->{table} eq 'ARRAY' ? $row->{table}->[$offset] :
468 substr $row->{table}, $offset, 1;
469 my ($cell, $name, $class) = $glyph eq $NOCHAR ? () :
470 $glyphs->glyph_html($glyph);
472 if (exists $get{compare}) {
475 $cp == ord $glyph ? 'l4' :
476 $row->{parent} && $glyph eq
477 Encode::decode($row->{parent}, pack 'C', $cp) ? 'l3' :
479 $visible->{$glyph} ? 'l2' :
482 $visible->{$glyph}++;
485 say sprintf $class ? '<td title="%s" class="X %s">%s' : '<td title="%s">',
486 $name, $class, $cell;
492 say '</table></div>';
499 <table class="glyphs"><tr><: if (exists $get{compare}) { :>
500 <td class="X l4">unicode
501 <td class="X l3">inherited
502 <td class="X l2">existing
503 <td class="X l1">original
504 <td class="">unassigned
506 <td class="X Cc">control
507 <td class="X Zs"><span>whitespace</span>
508 <td class="X Mn">diacritic<table class="glyphs"><tr>
509 <td class="X Sk">letter
511 <td class="X Po">punctuation<table class="glyphs"><tr>
512 <td class="X Pf">quote
514 <td class="X So">symbol<table class="glyphs"><tr>
515 <td class="X Sm">math
516 <td class="X Sc">currency
518 <td class="X No">numeric
519 <td class="X Greek">greek<table class="glyphs"><tr>
520 <td class="X Latin">latin
521 <td class="X Cyrillic">cyrillic
523 <td class="X Aramaic">aramaic<table class="glyphs"><tr>
524 <td class="X Brahmic">brahmic
525 <td class="X Arabic">arabic
527 <td class="X Syllabic">syllabic<table class="glyphs"><tr>
528 <td class="X African">african
529 <td class="X Hiragana">japanese
530 <td class="X Han">cjk
531 <td class="X Bopomofo">chinese
533 <td class="X Alpha">alphabetic
536 <table class="glyphs"><tr>
537 <td class="X">unicode 7.0
538 <td class="X Xr">proposed
539 <td class="X Xd">deprecated
540 <td class="">unassigned
541 <td class="X Xi">invalid