3 my $mode = exists $get{compare};
4 my @tablist = split m{/+}, $Request || 'default';
7 title => 'charset cheat sheet',
9 canonical => "/charset/$Request" . ($mode && '?compare'),
11 "Reference sheet with all glyphs in common character encoding tables,",
12 "and an overview of Unicode ranges and UTF-8 bytes.",
15 charset codepage unicode ascii utf8 latin glyph character encoding
16 reference common overview table
18 (stylesheet => [qw'light']) x !$mode,
20 charset.inc.pl charset-encoding.inc.pl
21 charset-unicode.inc.pl charset-ucplanes.inc.pl charset-utf8.inc.pl
25 use List::Util qw( first pairmap pairfirst pairs );
28 <h1>Character encodings</h1>
32 if ($tablist[0] eq 'unicode') {
33 say "Detailed allocation of Unicode blocks.";
34 print "See ", showlink("charsets", '/charset'), " to compare specific encodings";
38 if ($tablist[0] eq 'default') {
39 say "Overview of Unicode ", showlink("allocation", '/charset/unicode');
40 say "and common latin code pages.";
41 say "Compare alternate charsets:";
44 say "Charset comparison:";
49 join " ·\n", pairmap {
50 showlink($b || ucfirst $a, '/charset'.($a && "/$a?compare"), $a eq $Request);
60 $tablist[0] eq 'default' ? () : ('' => 'common'),
65 norteur => 'North European',
79 use Shiar_Sheet::FormatChar;
80 my $glyphs = Shiar_Sheet::FormatChar->new;
83 my $charsets = Data('charset');
86 # generate character table(s)
87 my $input = shift or return;
88 my $params = $input =~ s/[+](.*)\z// ? $1 : undef;
89 my $charset = $charsets->{lc $input} || {};
91 if (ref $charset ne 'HASH') {
92 $params and Alert("Parameters ignored for $input",
93 "Cannot apply <q>$params</q> to multiple charsets.",
95 tabinput($_) for ref $charset ? @{$charset} : $charset;
99 state $visible = {'' => 1}; # all present tables
100 my %row = (offset => 0, cols => 16);
101 $row{$_} = $charset->{$_} for qw( note table ); # copy metadata
103 if (not defined $params) {
104 my @parents = @{ $charset->{inherit} || [] };
106 if (my ($parent, $part) = pairfirst { defined $visible->{$a} } @parents) {
107 $row{parent} = $parent;
109 $params = 80 unless $visible->{$parent}
110 or ($input eq 'MacCroatian' and defined $visible->{MacRomanian});
112 elsif (defined $visible->{ascii}) {
113 $row{parent} = $parents[0];
114 $params = $parents[1] // 80;
115 $params = 80 if hex $params >= 0x80; # ascii offset at most
118 $row{parent} = $parents[0];
119 $params = $parents[1] if hex $parents[1] == 0; # apply ascii end
121 $visible->{$_} //= 0 for $row{parent} || ();
124 for my $param (split /[+]+/, $params // '') {
125 if ($param eq 'realsize') {
128 elsif ($param =~ m{ \A cols = (\d+) \z }x) {
131 elsif ($param =~ m{ \A (?<start> \p{AHex}+) (?: [-] (?<end> \p{AHex}+) )? \z }x) {
132 if (defined $row{endpoint}) {
133 # extend earlier range
134 my $skip = int(($row{endpoint} || $row{startpoint}) / $row{cols});
135 for ($skip + 1 .. (hex($+{start}) / $row{cols}) - 1) {
136 $row{skip}->{ $_ * $row{cols} }++;
140 $row{startpoint} = hex $+{start};
142 $row{endpoint} = hex($+{end} || 0);
145 Alert("Unknown option <q>$param</q> for charset $input");
149 if ($charset->{setup}) {
150 eval { $charset->{setup}->(\%row) }
151 or Alert("Incomplete setup of $input", $@);
153 $row{endpoint} ||= 0xFF;
155 if (defined $row{table} or defined $row{cell}) {
156 $row{set} //= $input;
158 elsif ($row{set} = Encode::resolve_alias($charset->{set} // $input)) {
159 $row{offset} = delete $row{startpoint};
160 if ($charset->{varchar}) {
161 # array of possibly multiple characters per code point
163 map { Encode::decode($row{set}, pack 'C*', $_) } $row{offset} .. $row{endpoint}
167 # ~16x faster than decoding in loop;
168 # substr strings is twice as fast as splitting to an array
169 $row{table} = Encode::decode($row{set}, pack 'C*', $row{offset} .. $row{endpoint});
172 $row{endpoint} -= $row{offset};
173 $visible->{ascii}++; # assume common base
174 $row{set} = $input if $charset->{set}; # base override
177 Alert("Encoding <q>$input</q> unknown");
181 if (my $replace = $charset->{replace}) {
182 while (my ($offset, $sub) = each %{$replace}) {
183 $offset -= $row{offset};
185 if (ref $row{table} eq 'ARRAY') {
186 $row{table}->[$offset] = $sub
187 if $offset >= 0 and $offset <= $row{endpoint};
191 my $length = length $sub;
194 $offset > -$length or next; # at least one character after start
195 # trim leftmost part to start at offset
196 substr($sub, 0, -$offset) = '';
201 if ((my $excess = $row{endpoint} - $offset - $length + 1) < 0) {
202 $excess > -$length or next;
203 # trim rightmost part to prevent overflow
204 substr($sub, $excess) = '';
208 substr($row{table}, $offset, $length) = $sub;
212 push @request, \%row;
213 $visible->{ $row{set} } = 1 if $row{table};
215 tabinput($_) for @tablist;
217 my $NOCHAR = chr 0xFFFD;
220 my ($info, $offset) = @_;
221 my $table = $info->{cell} or return;
222 my $def = $table->{$offset} or return;
223 my ($len, $class, $name, $title) = @{$def};
225 my $cols = $info->{cols};
226 my $colsize = $table->{colsize} || 1;
229 $name //= $len <= 2 ? 'res' : 'reserved';
231 if (my $part = ($offset/$colsize - $info->{startpoint}) % $cols) {
233 my $rest = $cols - $part; # remaining
234 $rest = $len if $len < $rest; #TODO: optimise
236 # continued on new row
237 my @next = ($len * $colsize, "$class joinu");
238 my $separate = $cols - $len > $rest; # columns not on next row
241 push @next, $name, $title;
243 $name = $separate && '…';
246 # minority on next row
247 push @next, $separate && '"', $title || $name;
249 $table->{$offset + $colsize*$rest} //= \@next;
254 elsif (my $rows = int($len / $cols)) {
256 my $rowsize = $colsize * $cols;
257 if ($len -= $rows * $cols) {
258 # partial row remains
259 $table->{$offset + $rowsize * $rows} //= [$len*$colsize, "$class joinu", '', $title];
263 unless ($info->{realsize}) {
264 # coalesce multiple rows
266 $info->{skip}->{$offset += $rowsize}++;
270 $info->{skip}->{$offset += $rowsize} = 0;
274 $attr .= sprintf ' rowspan=%d', $rows;
278 $attr .= sprintf ' colspan=%d', $len unless $len == 1;
279 $attr .= $1 if $class and $class =~ s/( \w+="[^"]*")//;
280 $attr .= sprintf ' class="%s"', $class if $class;
281 $attr .= sprintf ' title="%s"', EscapeHTML($title) if $title;
282 return "<td$attr>$name\n";
285 for my $row (@request) {
286 my $cols = $row->{cols};
287 my $colsize = $row->{cell} && $row->{cell}->{colsize} || 1;
288 my $coldigits = ceil(log($colsize * $cols) / log(16)); # uniform length of hexadecimal header
289 my $rowdiv = 16 ** $coldigits; # row divide for column digits
290 $rowdiv = 1 if $rowdiv != $cols * $colsize; # divide only if all columns are matched
291 my $offset = $row->{startpoint} * $colsize || 0;
293 printf '<div class="section"><table class="glyphs%s">', !$row->{cell} && ' charmap';
294 my $title = $row->{set};
295 $title .= " <aside>(over $_)</aside>" for $row->{parent} || ();
296 $title .= " <aside>($_)</aside>" for $row->{note} || ();
297 printf '<caption>%s</caption>', $title;
298 print '<col>' x ($cols + 1);
299 for my $section (qw{thead}) {
300 print "<$section><tr><th>", $rowdiv == 1 ? '+' : '↱';
301 printf '<th>%0*X', $coldigits, $_ * $colsize for 0 .. $cols - 1;
306 while ($offset <= $row->{endpoint} * $colsize) {
307 if ($row->{skip}->{$offset + $row->{offset}}) {
308 $offset += $cols * $colsize;
313 if (defined $row->{skip}->{$offset + $row->{offset}}) {
317 if (my $rowmod = $offset % $rowdiv) {
318 # offset in column units
319 printf '<small>+%X</small>', $rowmod;
323 printf '%X', ($offset + $row->{offset}) / $rowdiv;
330 print range_cell($row, $offset);
334 my $cp = $offset + $row->{offset};
335 my $glyph = ref $row->{table} eq 'ARRAY' ? $row->{table}->[$offset] :
336 substr $row->{table}, $offset, 1;
337 my ($cell, $name, $class) = !defined $glyph || $glyph eq $NOCHAR ? () :
338 $glyphs->glyph_html($glyph);
343 $cp == ord $glyph ? 'l4' :
344 $row->{parent} && $glyph eq
345 Encode::decode($row->{parent}, pack 'C', $cp) ? 'l3' :
346 !defined $cell ? undef :
347 $visible->{$glyph} ? 'l2' :
350 $visible->{$glyph}++;
353 printf '<td title="%s"', $name;
354 say $class ? sprintf(' class="X %s">%s', $class, $cell) : '>';
360 say '</table></div>';
367 <table class="glyphs"><tr><: if ($mode) { :>
368 <td class="X l4">unicode
369 <td class="X l3">inherited
370 <td class="X l2">existing
371 <td class="X l1">original
372 <td class="">unassigned
374 <td class="X Cc">control
375 <td class="X Zs"><span>whitespace</span>
376 <td class="X Mn">diacritic<table class="glyphs"><tr>
377 <td class="X Sk">letter
379 <td class="X Po">punctuation<table class="glyphs"><tr>
380 <td class="X Pf">quote
382 <td class="X So">symbol<table class="glyphs"><tr>
383 <td class="X Sm">math
384 <td class="X Sc">currency
386 <td class="X No">numeric
387 <td class="X Greek">greek<table class="glyphs"><tr>
388 <td class="X Latin">latin
389 <td class="X Cyrillic">cyrillic
391 <td class="X Aramaic">aramaic<table class="glyphs"><tr>
392 <td class="X Brahmic">brahmic
393 <td class="X Arabic">arabic
395 <td class="X Syllabic">syllabic<table class="glyphs"><tr>
396 <td class="X African">african
397 <td class="X Hiragana">japanese
398 <td class="X Han">cjk
399 <td class="X Bopomofo">chinese
401 <td class="X Alpha">alphabetic
404 <table class="glyphs"><tr>
405 <td class="X">unicode 10.0
406 <td class="X Xr">proposed
407 <td class="X Xd">deprecated
408 <td class="">unassigned
409 <td class="X Xi">invalid