+sub range_cell {
+ my ($table, $offset) = @_;
+ my $def = $table->{$offset} or return;
+ my ($len, $class, $name, $title) = @{$def};
+
+ my $attr = '';
+ $len /= $nibsize;
+ $name //= $len <= 2 ? 'res' : 'reserved';
+
+ if (my $part = $offset/$nibsize % 16) {
+ # continued row
+ my $cols = 16 - $part; # remaining
+ $cols = $len if $len < $cols; #TODO: optimise
+ if ($len -= $cols) {
+ # continued on new row
+ $table->{$offset + $nibsize*$cols} //= [$len*$nibsize, "$class joinu", $name, $title];
+ $name = '';
+ $class .= ' joind';
+ }
+ $len = $cols;
+ }
+ elsif (my $rows = $len >> 4) {
+ # multiple full rows
+ if ($len -= $rows << 4) {
+ # partial row remains
+ $table->{$offset + $nibsize*$rows * 16} //= [$len*$nibsize, "$class joinu", '', $title];
+ $class .= ' joind';
+ }
+ $attr .= sprintf ' rowspan=%d', $rows;
+ $len = 16;
+ }
+
+ $attr .= sprintf ' colspan=%d', $len unless $len == 1;
+ $attr .= $1 if $class and $class =~ s/( \w+="[^"]*")//;
+ $attr .= sprintf ' class="%s"', $class if $class;
+ $attr .= sprintf ' title="%s"', EscapeHTML($title) if $title;
+ return "<td$attr>$name";
+}
+