X-Git-Url: http://git.shiar.nl/sheet.git/blobdiff_plain/3b395d58206174e283f62ac5ca3a382c9d375a02..4e051d54eec34ed62a4fa8982c2b34493a61e016:/charset.plp diff --git a/charset.plp b/charset.plp index fcd647d..73b3740 100644 --- a/charset.plp +++ b/charset.plp @@ -19,10 +19,10 @@ Html({

Character encoding

<: +use POSIX qw( ceil ); use Shiar_Sheet::FormatChar; my $glyphs = Shiar_Sheet::FormatChar->new; -my @nibble = (0..9, 'A'..'F'); -my $nibsize = 1; +my $cols = 16; # columns # generate character table(s) # (~16x faster than decoding in loop; @@ -66,29 +66,28 @@ my @request = map { $row{table} = ' ' x 1024; $row{set} = 'Unicode planes'; $row{cell} = do 'charset-ucplanes.inc.pl' - or printf "

Table data could not be read: %s.

\n", $@ || $!; - @nibble = (map { $_.0, $_.8 } 0 .. 7); - $nibsize = 8; + or Alert('Table data could not be read', $@ || $!); + $cols *= 2; } elsif ($row{set} = Encode::resolve_alias($input)) { if ($row{set} eq 'Internal') { $row{table} = ' ' x ($endpoint < 255 ? 640 : 8192); $row{set} = 'Unicode BMP'; $row{cell} = do 'charset-unicode.inc.pl' - or printf "

Table data could not be read: %s.

\n", $@ || $!; + or Alert('Table data could not be read', $@ || $!); } elsif ($row{set} eq 'utf-8-strict') { $row{table} = undef; $row{set} = 'UTF-8'; $row{cell} = do 'charset-utf8.inc.pl' - or printf "

Table data could not be read: %s.

\n", $@ || $!; + or Alert('Table data could not be read', $@ || $!); } else { $row{table} = Encode::decode($row{set}, pack 'C*', $row{offset} .. $endpoint); } } else { - say "

Encoding $input unknown

"; + Alert("Encoding $input unknown"); } $row{set} ? \%row : (); } @@ -112,31 +111,42 @@ sub range_cell { my $def = $table->{$offset} or return; my ($len, $class, $name, $title) = @{$def}; + my $colsize = $table->{colsize} || 1; my $attr = ''; - $len /= $nibsize; + $len /= $colsize; $name //= $len <= 2 ? 'res' : 'reserved'; - if (my $part = $offset/$nibsize % 16) { + if (my $part = $offset/$colsize % $cols) { # continued row - my $cols = 16 - $part; # remaining - $cols = $len if $len < $cols; #TODO: optimise - if ($len -= $cols) { + my $rest = $cols - $part; # remaining + $rest = $len if $len < $rest; #TODO: optimise + if ($len -= $rest) { # continued on new row - $table->{$offset + $nibsize*$cols} //= [$len*$nibsize, "$class joinu", $name, $title]; - $name = ''; + my @next = ($len * $colsize, "$class joinu"); + if ($len > $rest) { + # minority remains + push @next, $name, $title; + $title ||= $name; + $name = ''; + } + else { + # minority on next row + push @next, '"', $title || $name; + } + $table->{$offset + $colsize*$rest} //= \@next; $class .= ' joind'; } - $len = $cols; + $len = $rest; } - elsif (my $rows = $len >> 4) { + elsif (my $rows = int($len / $cols)) { # multiple full rows - if ($len -= $rows << 4) { + if ($len -= $rows * $cols) { # partial row remains - $table->{$offset + $nibsize*$rows * 16} //= [$len*$nibsize, "$class joinu", '', $title]; + $table->{$offset + $colsize*$rows * $cols} //= [$len*$colsize, "$class joinu", '', $title]; $class .= ' joind'; } $attr .= sprintf ' rowspan=%d', $rows; - $len = 16; + $len = $cols; } $attr .= sprintf ' colspan=%d', $len unless $len == 1; @@ -147,26 +157,37 @@ sub range_cell { } for my $row (@request) { + 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; # divider of row headers + printf '
', !$row->{cell} && ' charmap'; my $title = $row->{set}; $title .= " " for $row->{setnote} // (); printf '', $title; - print '' x 17; + print '' x ($cols + 1); for my $section (qw{thead}) { print "<$section>'; - for my $msb (0 .. (length($row->{table}) || 256) - 1 >> 4) { - printf '
%s
↱"; - print '', $_ for @nibble; + printf '%0*X', $coldigits, $_ * $colsize for 0 .. $cols - 1; print "\n"; } print '
%X', ($msb + ($row->{offset} >> 4)) * $nibsize; - for my $lsb (0 .. $#nibble) { - my $val = ( ($msb<<4) + $lsb ) * $nibsize; + for my $msb (0 .. ((length($row->{table}) || 256) - 1) / $cols) { + print '
'; + { + my $rowlabel = ($msb + int($row->{offset} / $cols)) * $cols * $colsize; + if (my $rowmod = $rowlabel % $rowdiv) { + # offset in column units + printf '+%X', $rowmod; + } + else { + # divided row offset + printf '%X', $rowlabel / $rowdiv; + } + } + for my $lsb (0 .. $cols - 1) { + my $val = ( ($msb * $cols) + $lsb ) * $colsize; if ($row->{cell}) { - if (ref $row->{cell} eq 'CODE') { - print $row->{cell}->($val); - next; - } print range_cell($row->{cell}, $val); next; }