3 my $mode = exists $get{compare};
4 my @tablist = split m{/+}, $Request || 'default';
7 title => 'charset cheat sheet',
10 "Reference sheet with all glyphs in common character encoding tables,",
11 "and an overview of Unicode ranges and UTF-8 bytes.",
14 charset codepage unicode ascii utf8 latin glyph character encoding
15 reference common overview table
17 stylesheet => [qw'light'],
19 charset-encoding.inc.pl
20 charset-unicode.inc.pl charset-ucplanes.inc.pl charset-utf8.inc.pl
24 use List::Util qw( first pairmap pairfirst pairs );
27 <h1>Character encodings</h1>
31 if ($tablist[0] eq 'default') {
32 say "Overview of Unicode allocation and common latin code pages.";
33 say "Compare alternate charsets:";
36 say "Charset comparison:";
40 my ($title, $href, $selected) = @_;
42 $selected ? '<strong>%s</strong>' : '<a href="%2$s">%s</a>',
43 EscapeHTML($title), $href
49 join " ·\n", pairmap {
50 optionlink($b || ucfirst $a, '/charset'.($a && "/$a?compare"), $a eq $Request);
59 $tablist[0] eq 'default' ? () : ('' => 'common'),
64 norteur => 'North European',
76 use Shiar_Sheet::FormatChar;
77 my $glyphs = Shiar_Sheet::FormatChar->new;
80 my $charsets = do 'charset-encoding.inc.pl'
81 or Alert('Encoding metadata could not be read', $@ || $!);
84 # generate character table(s)
85 my $input = shift or return;
86 my $params = $input =~ s/[+](.*)\z// ? $1 : undef;
87 my $charset = $charsets->{lc $input} || {};
89 if (ref $charset ne 'HASH') {
90 $params and Alert("Parameters ignored for $input",
91 "Cannot apply <q>$params</q> to multiple charsets.",
93 tabinput($_) for ref $charset ? @{$charset} : $charset;
97 state $visible = {'' => 1}; # all present tables
98 my %row = (offset => 0, cols => 16);
100 if (not defined $params) {
101 my @parents = @{ $charset->{inherit} || [] };
103 if (my ($parent, $part) = pairfirst { defined $visible->{$a} } @parents) {
104 $row{parent} = $parent;
106 $params = 80 unless $visible->{$parent}
107 or ($input eq 'MacCroatian' and defined $visible->{MacRomanian});
109 elsif (defined $visible->{ascii}) {
110 $row{parent} = $parents[0];
111 $params = $parents[1] // 80;
112 $params = 80 if hex $params >= 0x80; # ascii offset at most
115 $row{parent} = $parents[0];
116 $params = $parents[1] if hex $parents[1] == 0; # apply ascii end
118 $visible->{$_} //= 0 for $row{parent} || ();
121 for my $param (split /[+]+/, $params // '') {
122 if ($param eq 'realsize') {
125 elsif ($param =~ m{ \A cols = (\d+) \z }x) {
128 elsif ($param =~ m{ \A (?<start> \p{AHex}+) (?: [-] (?<end> \p{AHex}+) )? \z }x) {
129 if (defined $row{endpoint}) {
130 # extend earlier range
131 my $skip = int(($row{endpoint} || $row{startpoint}) / $row{cols});
132 for ($skip + 1 .. (hex($+{start}) / $row{cols}) - 1) {
133 $row{skip}->{ $_ * $row{cols} - $row{startpoint} }++;
137 $row{startpoint} = hex $+{start};
139 $row{endpoint} = hex($+{end} || 0);
142 Alert("Unknown option <q>$param</q> for charset $input");
146 if ($charset->{setup}) {
147 eval { $charset->{setup}->(\%row) }
148 or Alert("Incomplete setup of $input", $@);
152 elsif ($row{set} = Encode::resolve_alias($input)) {
153 $row{offset} = delete $row{startpoint};
154 $row{endpoint} ||= 0xFF;
155 if ($row{set} eq 'MacHebrew' or $row{set} eq 'MacThai') {
156 # array of possibly multiple characters per code point
158 map { Encode::decode($row{set}, pack 'C*', $_) } $row{offset} .. $row{endpoint}
162 # ~16x faster than decoding in loop;
163 # substr strings is twice as fast as splitting to an array
164 $row{table} = Encode::decode($row{set}, pack 'C*', $row{offset} .. $row{endpoint});
167 if ($row{set} eq 'cp437') {
168 if ($row{offset} <= 0xED and $row{endpoint} >= 0xED) {
170 substr($row{table}, 0xED - $row{offset}, 1) = 'ϕ';
172 if ($row{offset} < 0x20) {
173 # replace control characters by visible variants
174 my $sub = substr ' ☺☻♥♦♣♠•◘○◙♂♀♪♫☼►◄↕‼¶§▬↨↑↓→←∟↔▲▼', $row{offset};
175 substr($row{table}, 0, length $sub) = $sub;
178 elsif ($row{set} eq 'symbol') {
179 if ($row{offset} <= 0x60 and $row{endpoint} >= 0x60) {
180 # replace radical extender by closest unicode equivalent
181 substr($row{table}, 0x60 - $row{offset}, 1) = '│';
183 if ($row{offset} <= 0xBD and $row{endpoint} >= 0xFF) {
184 substr($row{table}, 0xBD - $row{offset}, 2) = '⏐⎯'; # arrow extenders
185 substr($row{table}, 0xD2 - $row{offset}, 3) = '®©™'; # serif variants
186 substr($row{table}, 0xE0 - $row{offset}, 1) = '◊'; # replace lookalike, should match AdobeSymbol
187 substr($row{table}, 0xE2 - $row{offset}, 3) = '®©™'; # sans-serif variants
188 substr($row{table}, 0xE6 - $row{offset}, 10) = '⎛⎜⎝⎡⎢⎣⎧⎨⎩⎪';
189 substr($row{table}, 0xF0 - $row{offset}, 1) = '€';
190 substr($row{table}, 0xF4 - $row{offset}, 11) = '⎮⌡⎞⎟⎠⎤⎥⎦⎫⎬⎭';
194 $row{endpoint} -= $row{offset};
196 $visible->{ascii} = # assume common base
197 $visible->{ $row{set} } = 1;
200 Alert("Encoding <q>$input</q> unknown");
203 push @request, \%row;
205 tabinput($_) for @tablist;
207 my $NOCHAR = chr 0xFFFD;
210 my ($info, $offset) = @_;
211 my $table = $info->{cell} or return;
212 my $def = $table->{$offset} or return;
213 my ($len, $class, $name, $title) = @{$def};
215 my $cols = $info->{cols};
216 my $colsize = $table->{colsize} || 1;
219 $name //= $len <= 2 ? 'res' : 'reserved';
221 if (my $part = ($offset/$colsize - $info->{startpoint}) % $cols) {
223 my $rest = $cols - $part; # remaining
224 $rest = $len if $len < $rest; #TODO: optimise
226 # continued on new row
227 my @next = ($len * $colsize, "$class joinu");
228 my $separate = $cols - $len > $rest; # columns not on next row
231 push @next, $name, $title;
233 $name = $separate && '…';
236 # minority on next row
237 push @next, $separate && '"', $title || $name;
239 $table->{$offset + $colsize*$rest} //= \@next;
244 elsif (my $rows = int($len / $cols)) {
246 my $rowsize = $colsize * $cols;
247 if ($len -= $rows * $cols) {
248 # partial row remains
249 $table->{$offset + $rowsize * $rows} //= [$len*$colsize, "$class joinu", '', $title];
253 unless ($info->{realsize}) {
254 # coalesce multiple rows
256 $info->{skip}->{$offset += $rowsize}++;
260 $info->{skip}->{$offset += $rowsize} = 0;
264 $attr .= sprintf ' rowspan=%d', $rows;
268 $attr .= sprintf ' colspan=%d', $len unless $len == 1;
269 $attr .= $1 if $class and $class =~ s/( \w+="[^"]*")//;
270 $attr .= sprintf ' class="%s"', $class if $class;
271 $attr .= sprintf ' title="%s"', EscapeHTML($title) if $title;
272 return "<td$attr>$name\n";
275 for my $row (@request) {
276 my $cols = $row->{cols};
277 my $colsize = $row->{cell} && $row->{cell}->{colsize} || 1;
278 my $coldigits = ceil(log($colsize * $cols) / log(16)); # uniform length of hexadecimal header
279 my $rowdiv = 16 ** $coldigits; # row divide for column digits
280 $rowdiv = 1 if $rowdiv != $cols * $colsize; # divide only if all columns are matched
281 my $offset = $row->{startpoint} * $colsize || 0;
283 printf '<div class="section"><table class="glyphs%s">', !$row->{cell} && ' charmap';
284 my $title = $row->{set};
285 $title .= " <aside>(over $_)</aside>"
286 for $row->{parent} || ();
287 printf '<caption>%s</caption>', $title;
288 print '<col>' x ($cols + 1);
289 for my $section (qw{thead}) {
290 print "<$section><tr><th>", $rowdiv == 1 ? '+' : '↱';
291 printf '<th>%0*X', $coldigits, $_ * $colsize for 0 .. $cols - 1;
296 while ($offset <= $row->{endpoint} * $colsize) {
297 if ($row->{skip}->{$offset}) {
298 $offset += $cols * $colsize;
303 if (defined $row->{skip}->{$offset}) {
307 if (my $rowmod = $offset % $rowdiv) {
308 # offset in column units
309 printf '<small>+%X</small>', $rowmod;
313 printf '%X', ($offset + $row->{offset}) / $rowdiv;
320 print range_cell($row, $offset);
324 my $cp = $offset + $row->{offset};
325 my $glyph = ref $row->{table} eq 'ARRAY' ? $row->{table}->[$offset] :
326 substr $row->{table}, $offset, 1;
327 my ($cell, $name, $class) = $glyph eq $NOCHAR ? () :
328 $glyphs->glyph_html($glyph);
333 $cp == ord $glyph ? 'l4' :
334 $row->{parent} && $glyph eq
335 Encode::decode($row->{parent}, pack 'C', $cp) ? 'l3' :
337 $visible->{$glyph} ? 'l2' :
340 $visible->{$glyph}++;
343 say sprintf $class ? '<td title="%s" class="X %s">%s' : '<td title="%s">',
344 $name, $class, $cell;
350 say '</table></div>';
357 <table class="glyphs"><tr><: if ($mode) { :>
358 <td class="X l4">unicode
359 <td class="X l3">inherited
360 <td class="X l2">existing
361 <td class="X l1">original
362 <td class="">unassigned
364 <td class="X Cc">control
365 <td class="X Zs"><span>whitespace</span>
366 <td class="X Mn">diacritic<table class="glyphs"><tr>
367 <td class="X Sk">letter
369 <td class="X Po">punctuation<table class="glyphs"><tr>
370 <td class="X Pf">quote
372 <td class="X So">symbol<table class="glyphs"><tr>
373 <td class="X Sm">math
374 <td class="X Sc">currency
376 <td class="X No">numeric
377 <td class="X Greek">greek<table class="glyphs"><tr>
378 <td class="X Latin">latin
379 <td class="X Cyrillic">cyrillic
381 <td class="X Aramaic">aramaic<table class="glyphs"><tr>
382 <td class="X Brahmic">brahmic
383 <td class="X Arabic">arabic
385 <td class="X Syllabic">syllabic<table class="glyphs"><tr>
386 <td class="X African">african
387 <td class="X Hiragana">japanese
388 <td class="X Han">cjk
389 <td class="X Bopomofo">chinese
391 <td class="X Alpha">alphabetic
394 <table class="glyphs"><tr>
395 <td class="X">unicode 7.0
396 <td class="X Xr">proposed
397 <td class="X Xd">deprecated
398 <td class="">unassigned
399 <td class="X Xi">invalid