<h1>Character encoding</h1>
<:
+use POSIX qw( ceil );
use Shiar_Sheet::FormatChar;
my $glyphs = Shiar_Sheet::FormatChar->new;
-my $cols = 16; # columns
-my $colsize = 1; # increment per column
# generate character table(s)
# (~16x faster than decoding in loop;
);
my @request = map {
if (my $input = $_) {
- my %row = (offset => 0);
+ my %row = (offset => 0, cols => 16);
my $endpoint = 255;
if ($input =~ s/^--//) {
$row{offset} = $endpoint > 160 ? 160 : 48;
$row{set} = 'Unicode planes';
$row{cell} = do 'charset-ucplanes.inc.pl'
or Alert('Table data could not be read', $@ || $!);
- $colsize = 8;
+ $row{cols} *= 2;
}
elsif ($row{set} = Encode::resolve_alias($input)) {
if ($row{set} eq 'Internal') {
}
sub range_cell {
- my ($table, $offset) = @_;
+ my ($info, $offset) = @_;
+ my $table = $info->{cell} or return;
my $def = $table->{$offset} or return;
my ($len, $class, $name, $title) = @{$def};
+ my $cols = $info->{cols};
+ my $colsize = $table->{colsize} || 1;
my $attr = '';
$len /= $colsize;
$name //= $len <= 2 ? 'res' : 'reserved';
}
for my $row (@request) {
+ my $cols = $row->{cols};
+ my $colsize = $row->{cell} && $row->{cell}->{colsize} || 1;
+ my $coldigits = ceil(log($colsize * $cols) / log(16)); # uniform length of hexadecimal header
+ my $rowdiv = 16 ** $coldigits; # row divide for column digits
+ $rowdiv = 1 if $rowdiv != $cols * $colsize; # divide only if all columns are matched
+ my $offset = 0;
+ my $endpoint = $offset + (length($row->{table}) || 256) * $colsize;
+
printf '<div class="section"><table class="glyphs%s">', !$row->{cell} && ' charmap';
my $title = $row->{set};
$title .= " <aside>($_)</aside>" for $row->{setnote} // ();
printf '<caption>%s</caption>', $title;
print '<col>' x ($cols + 1);
- my $coldigits = $colsize * $cols <= 16 ? 1 : 2; # uniform length of hexadecimal header
for my $section (qw{thead}) {
- print "<$section><tr><th>↱";
+ print "<$section><tr><th>", $rowdiv == 1 ? '+' : '↱';
printf '<th>%0*X', $coldigits, $_ * $colsize for 0 .. $cols - 1;
print "\n";
}
print '<tbody>';
- for my $msb (0 .. ((length($row->{table}) || 256) - 1) / $cols) {
- printf '<tr><th>%X', ($msb + int($row->{offset} / $cols)) * $cols * $colsize;
- for my $lsb (0 .. $cols - 1) {
- my $val = ( ($msb * $cols) + $lsb ) * $colsize;
+ while ($offset < $endpoint - 1) {
+ print '<tr><th>';
+ {
+ if (my $rowmod = $offset % $rowdiv) {
+ # offset in column units
+ printf '<small>+%X</small>', $rowmod;
+ }
+ else {
+ # divided row offset
+ printf '%X', ($offset + $row->{offset}) / $rowdiv;
+ }
+ }
+ for (1 .. $cols) {
if ($row->{cell}) {
- print range_cell($row->{cell}, $val);
+ print range_cell($row, $offset);
next;
}
- my $glyph = substr $row->{table}, $val, 1;
+ my $glyph = substr $row->{table}, $offset, 1;
if ($glyph eq $NOCHAR) {
print '<td>';
next;
print "\n".$glyphs->glyph_cell($glyph);
}
+ continue {
+ $offset += $colsize;
+ }
print "\n";
}
say '</table></div>';