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:";
41 join " ·\n", pairmap {
42 showlink($b || ucfirst $a, '/charset'.($a && "/$a?compare"), $a eq $Request);
51 $tablist[0] eq 'default' ? () : ('' => 'common'),
56 norteur => 'North European',
68 use Shiar_Sheet::FormatChar;
69 my $glyphs = Shiar_Sheet::FormatChar->new;
72 my $charsets = do 'charset-encoding.inc.pl'
73 or Alert('Encoding metadata could not be read', $@ || $!);
76 # generate character table(s)
77 my $input = shift or return;
78 my $params = $input =~ s/[+](.*)\z// ? $1 : undef;
79 my $charset = $charsets->{lc $input} || {};
81 if (ref $charset ne 'HASH') {
82 $params and Alert("Parameters ignored for $input",
83 "Cannot apply <q>$params</q> to multiple charsets.",
85 tabinput($_) for ref $charset ? @{$charset} : $charset;
89 state $visible = {'' => 1}; # all present tables
90 my %row = (offset => 0, cols => 16);
92 if (not defined $params) {
93 my @parents = @{ $charset->{inherit} || [] };
95 if (my ($parent, $part) = pairfirst { defined $visible->{$a} } @parents) {
96 $row{parent} = $parent;
98 $params = 80 unless $visible->{$parent}
99 or ($input eq 'MacCroatian' and defined $visible->{MacRomanian});
101 elsif (defined $visible->{ascii}) {
102 $row{parent} = $parents[0];
103 $params = $parents[1] // 80;
104 $params = 80 if hex $params >= 0x80; # ascii offset at most
107 $row{parent} = $parents[0];
108 $params = $parents[1] if hex $parents[1] == 0; # apply ascii end
110 $visible->{$_} //= 0 for $row{parent} || ();
113 for my $param (split /[+]+/, $params // '') {
114 if ($param eq 'realsize') {
117 elsif ($param =~ m{ \A cols = (\d+) \z }x) {
120 elsif ($param =~ m{ \A (?<start> \p{AHex}+) (?: [-] (?<end> \p{AHex}+) )? \z }x) {
121 if (defined $row{endpoint}) {
122 # extend earlier range
123 my $skip = int(($row{endpoint} || $row{startpoint}) / $row{cols});
124 for ($skip + 1 .. (hex($+{start}) / $row{cols}) - 1) {
125 $row{skip}->{ $_ * $row{cols} - $row{startpoint} }++;
129 $row{startpoint} = hex $+{start};
131 $row{endpoint} = hex($+{end} || 0);
134 Alert("Unknown option <q>$param</q> for charset $input");
138 if ($charset->{setup}) {
139 eval { $charset->{setup}->(\%row) }
140 or Alert("Incomplete setup of $input", $@);
142 $row{endpoint} ||= 0xFF;
144 if (defined $row{table} or defined $row{cell}) {
145 $row{set} //= $input;
147 elsif ($row{set} = Encode::resolve_alias($input)) {
148 $row{offset} = delete $row{startpoint};
149 if ($charset->{varchar}) {
150 # array of possibly multiple characters per code point
152 map { Encode::decode($row{set}, pack 'C*', $_) } $row{offset} .. $row{endpoint}
156 # ~16x faster than decoding in loop;
157 # substr strings is twice as fast as splitting to an array
158 $row{table} = Encode::decode($row{set}, pack 'C*', $row{offset} .. $row{endpoint});
161 $row{endpoint} -= $row{offset};
162 $visible->{ascii}++; # assume common base
165 Alert("Encoding <q>$input</q> unknown");
169 if (my $replace = $charset->{replace}) {
170 while (my ($offset, $sub) = each %{$replace}) {
171 $offset -= $row{offset};
173 if (ref $row{table} eq 'ARRAY') {
174 $row{table}->[$offset] = $sub
175 if $offset >= 0 and $offset <= $row{endpoint};
179 my $length = length $sub;
182 $offset > -$length or next; # at least one character after start
183 # trim leftmost part to start at offset
184 substr($sub, 0, -$offset) = '';
189 if ((my $excess = $row{endpoint} - $offset - $length + 1) < 0) {
190 $excess > -$length or next;
191 # trim rightmost part to prevent overflow
192 substr($sub, $excess) = '';
196 substr($row{table}, $offset, $length) = $sub;
200 push @request, \%row;
201 $visible->{ $row{set} } = 1 if $row{table};
203 tabinput($_) for @tablist;
205 my $NOCHAR = chr 0xFFFD;
208 my ($info, $offset) = @_;
209 my $table = $info->{cell} or return;
210 my $def = $table->{$offset} or return;
211 my ($len, $class, $name, $title) = @{$def};
213 my $cols = $info->{cols};
214 my $colsize = $table->{colsize} || 1;
217 $name //= $len <= 2 ? 'res' : 'reserved';
219 if (my $part = ($offset/$colsize - $info->{startpoint}) % $cols) {
221 my $rest = $cols - $part; # remaining
222 $rest = $len if $len < $rest; #TODO: optimise
224 # continued on new row
225 my @next = ($len * $colsize, "$class joinu");
226 my $separate = $cols - $len > $rest; # columns not on next row
229 push @next, $name, $title;
231 $name = $separate && '…';
234 # minority on next row
235 push @next, $separate && '"', $title || $name;
237 $table->{$offset + $colsize*$rest} //= \@next;
242 elsif (my $rows = int($len / $cols)) {
244 my $rowsize = $colsize * $cols;
245 if ($len -= $rows * $cols) {
246 # partial row remains
247 $table->{$offset + $rowsize * $rows} //= [$len*$colsize, "$class joinu", '', $title];
251 unless ($info->{realsize}) {
252 # coalesce multiple rows
254 $info->{skip}->{$offset += $rowsize}++;
258 $info->{skip}->{$offset += $rowsize} = 0;
262 $attr .= sprintf ' rowspan=%d', $rows;
266 $attr .= sprintf ' colspan=%d', $len unless $len == 1;
267 $attr .= $1 if $class and $class =~ s/( \w+="[^"]*")//;
268 $attr .= sprintf ' class="%s"', $class if $class;
269 $attr .= sprintf ' title="%s"', EscapeHTML($title) if $title;
270 return "<td$attr>$name\n";
273 for my $row (@request) {
274 my $cols = $row->{cols};
275 my $colsize = $row->{cell} && $row->{cell}->{colsize} || 1;
276 my $coldigits = ceil(log($colsize * $cols) / log(16)); # uniform length of hexadecimal header
277 my $rowdiv = 16 ** $coldigits; # row divide for column digits
278 $rowdiv = 1 if $rowdiv != $cols * $colsize; # divide only if all columns are matched
279 my $offset = $row->{startpoint} * $colsize || 0;
281 printf '<div class="section"><table class="glyphs%s">', !$row->{cell} && ' charmap';
282 my $title = $row->{set};
283 $title .= " <aside>(over $_)</aside>"
284 for $row->{parent} || ();
285 printf '<caption>%s</caption>', $title;
286 print '<col>' x ($cols + 1);
287 for my $section (qw{thead}) {
288 print "<$section><tr><th>", $rowdiv == 1 ? '+' : '↱';
289 printf '<th>%0*X', $coldigits, $_ * $colsize for 0 .. $cols - 1;
294 while ($offset <= $row->{endpoint} * $colsize) {
295 if ($row->{skip}->{$offset}) {
296 $offset += $cols * $colsize;
301 if (defined $row->{skip}->{$offset}) {
305 if (my $rowmod = $offset % $rowdiv) {
306 # offset in column units
307 printf '<small>+%X</small>', $rowmod;
311 printf '%X', ($offset + $row->{offset}) / $rowdiv;
318 print range_cell($row, $offset);
322 my $cp = $offset + $row->{offset};
323 my $glyph = ref $row->{table} eq 'ARRAY' ? $row->{table}->[$offset] :
324 substr $row->{table}, $offset, 1;
325 my ($cell, $name, $class) = !defined $glyph || $glyph eq $NOCHAR ? () :
326 $glyphs->glyph_html($glyph);
331 $cp == ord $glyph ? 'l4' :
332 $row->{parent} && $glyph eq
333 Encode::decode($row->{parent}, pack 'C', $cp) ? 'l3' :
335 $visible->{$glyph} ? 'l2' :
338 $visible->{$glyph}++;
341 say sprintf $class ? '<td title="%s" class="X %s">%s' : '<td title="%s">',
342 $name, $class, $cell;
348 say '</table></div>';
355 <table class="glyphs"><tr><: if ($mode) { :>
356 <td class="X l4">unicode
357 <td class="X l3">inherited
358 <td class="X l2">existing
359 <td class="X l1">original
360 <td class="">unassigned
362 <td class="X Cc">control
363 <td class="X Zs"><span>whitespace</span>
364 <td class="X Mn">diacritic<table class="glyphs"><tr>
365 <td class="X Sk">letter
367 <td class="X Po">punctuation<table class="glyphs"><tr>
368 <td class="X Pf">quote
370 <td class="X So">symbol<table class="glyphs"><tr>
371 <td class="X Sm">math
372 <td class="X Sc">currency
374 <td class="X No">numeric
375 <td class="X Greek">greek<table class="glyphs"><tr>
376 <td class="X Latin">latin
377 <td class="X Cyrillic">cyrillic
379 <td class="X Aramaic">aramaic<table class="glyphs"><tr>
380 <td class="X Brahmic">brahmic
381 <td class="X Arabic">arabic
383 <td class="X Syllabic">syllabic<table class="glyphs"><tr>
384 <td class="X African">african
385 <td class="X Hiragana">japanese
386 <td class="X Han">cjk
387 <td class="X Bopomofo">chinese
389 <td class="X Alpha">alphabetic
392 <table class="glyphs"><tr>
393 <td class="X">unicode 7.0
394 <td class="X Xr">proposed
395 <td class="X Xd">deprecated
396 <td class="">unassigned
397 <td class="X Xi">invalid