charset: array table for multibyte MacHebrew
[sheet.git] / charset.plp
index f80a25012543a130f66470c87d695ea578eaa789..e2dac1a67a7307655934a44e5d5a76c50c6a532c 100644 (file)
@@ -137,10 +137,11 @@ sub tabinput {
                my $end = $2 ? hex($2) << 4 : $start + 240;
                $row{table} = join '', map { chr } $start .. $end+15;
                utf8::upgrade($row{table});  # prevent latin1 output
+               $row{len} = length $row{table};
                $row{set} = sprintf 'Unicode block U+%02Xxx', $start >> 8;
        }
        elsif ($input eq 'U') {
-               $row{table} = ' ' x 1024;
+               $row{len} = 1024;
                $row{set} = 'Unicode planes';
                $row{cell} = do 'charset-ucplanes.inc.pl'
                        or Alert('Table data could not be read', $@ || $!);
@@ -148,21 +149,30 @@ sub tabinput {
        }
        elsif ($row{set} = Encode::resolve_alias($input)) {
                if ($row{set} eq 'Internal') {
-                       $row{table} = ' ' x ($endpoint < 255 ? 640 : 8192);
+                       $row{len} = $endpoint < 255 ? 640 : 8192;
                        $row{set} = 'Unicode BMP';
                        $row{cell} = do 'charset-unicode.inc.pl'
                                or Alert('Table data could not be read', $@ || $!);
                }
                elsif ($row{set} eq 'utf-8-strict') {
-                       $row{table} = undef;
+                       $row{len} = 256;
                        $row{set} = 'UTF-8';
                        $row{cell} = do 'charset-utf8.inc.pl'
                                or Alert('Table data could not be read', $@ || $!);
                }
                else {
-                       $row{table} = Encode::decode($row{set}, pack 'C*', $row{offset} .. $endpoint);
-                               # (~16x faster than decoding in loop;
-                               #  substr strings is twice as fast as splitting to an array)
+                       if ($row{set} eq 'MacHebrew') {
+                               # array of possibly multiple characters per code point
+                               $row{table} = [
+                                       map { Encode::decode($row{set}, pack 'C*', $_) } $row{offset} .. $endpoint
+                               ];
+                       }
+                       else {
+                               # ~16x faster than decoding in loop;
+                               # substr strings is twice as fast as splitting to an array
+                               $row{table} = Encode::decode($row{set}, pack 'C*', $row{offset} .. $endpoint);
+                       }
+                       $row{len} = $endpoint - $row{offset};
 
                        if ($row{set} eq 'cp437' and !$row{offset}) {
                                substr($row{table}, 237, 1) = pack 'U*', 0x3D5; # phi sign
@@ -247,7 +257,7 @@ for my $row (@request) {
        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;
+       my $endpoint = $offset + $row->{len} * $colsize;
 
        printf '<div class="section"><table class="glyphs%s">', !$row->{cell} && ' charmap';
        my $title = $row->{set};
@@ -279,7 +289,8 @@ for my $row (@request) {
                                next;
                        }
 
-                       my $glyph = substr $row->{table}, $offset, 1;
+                       my $glyph = ref $row->{table} eq 'ARRAY' ? $row->{table}->[$offset] :
+                               substr $row->{table}, $offset, 1;
                        if ($glyph eq $NOCHAR) {
                                print '<td>';
                                next;