charset: replace table lengths by specific endpoints
[sheet.git] / charset.plp
index 9d7375d8c7fa5e05d11af279a47adc3057a9493e..c3477f124d9d9201d36e3593345602f292e9f590 100644 (file)
@@ -31,7 +31,7 @@ sub tabinput {
        my $input = shift or return;
 
        state $ALIAS = {
-               default    => [qw( unicode+0-192 utf-8 iso-8859-1 iso-8859-15 cp1252 cp437 cp850 )],
+               default    => [qw( unicode+0-639 utf-8 iso-8859-1 iso-8859-15 cp1252 cp437 cp850 )],
                us         => [qw( cp437 cp863 AdobeStandardEncoding gsm0338 )],
                ebcdic     => [qw( cp37 cp500 cp875 cp1026 cp1047 posix-bc )],
                iso        => [map {"iso-8859-$_"} 1 .. 11, 13 .. 16],
@@ -99,14 +99,15 @@ sub tabinput {
                        'hp-roman8'   => ['iso-8859-1' => '160'],
 
                        'cp1252'      => ['iso-8859-1' => '128-159'],
-                       'cp1250'      => ['iso-8859-2' => '128-191'],
-                       'cp1254'      => ['iso-8859-9' => '128-159'],
-                       'cp874'       => ['iso-8859-11' => '128-159'], # windows-874 actually cp1162
-                       'cp1257'      => ['iso-8859-13' => '128'],
+                       'cp1250'      => ['iso-8859-2' => '128-191', 'cp1252' => '128'],
+                       'cp1254'      => ['iso-8859-9' => '128-159', 'cp1252' => '128'],
+                       'cp874'       => ['iso-8859-11' => '128-159', 'cp1252' => '128'], # windows-874 actually cp1162
+                       'cp1257'      => ['iso-8859-13' => '128', 'cp1252' => '128'],
                        'cp1251'      => ['cp1252' => '128'],
                        'cp1253'      => ['cp1252' => '128'],
                        'cp1255'      => ['iso-8859-8' => '128-223', 'cp1252' => '128'],
                        'cp1256'      => ['cp1252' => '128'],
+                       'cp1258'      => ['cp1252' => '128'],
 
                        'cp850'       => ['cp437' => '144'],
                        'cp860'       => ['cp437' => '128-175'],
@@ -137,21 +138,21 @@ sub tabinput {
 
                my @parents = @{ $INHERIT->{$input} || [] };
 
-               if (my ($parent, $part) = pairfirst { exists $visible->{$a} } @parents) {
+               if (my ($parent, $part) = pairfirst { defined $visible->{$a} } @parents) {
                        $row{parent} = $parent;
                        $params = $part;
-                       $_ eq $part or $params = 128 for $visible->{$parent} // ();
+                       $params = 128 unless $visible->{$parent}
+                               or ($input eq 'MacCroatian' and defined $visible->{MacRomanian});
                }
-               elsif (exists $visible->{ascii}) {
+               elsif (defined $visible->{ascii}) {
                        $row{parent} = $parents[0];
                        $params = $parents[1] || 128;
-                       exists $visible->{$_} or $visible->{$_} = $params for $row{parent};
                        $params = 128 if $params >= 128;  # ascii offset at most
                }
                elsif (@parents) {
                        $row{parent} = $parents[0];
                }
-                       exists $visible->{$_} or $visible->{$_} = $params // 0 for $row{parent};
+               $visible->{$_} //= 0 for $row{parent};
        }
 
        if (defined $params and $params =~ m/^ (\d+) (-\d+)? /x) {
@@ -164,28 +165,28 @@ 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{endpoint} = $end + 14 - $start;
                $row{set} = sprintf 'Unicode block U+%02Xxx', $start >> 8;
        }
        elsif ($input eq 'U') {
-               $row{len} = 1024;
                $row{set} = 'Unicode planes';
                $row{cell} = do 'charset-ucplanes.inc.pl'
                        or Alert('Table data could not be read', $@ || $!);
                $row{cols} *= 2;
+               $row{endpoint} = 1023 * $row{cell}->{colsize};
        }
        elsif ($row{set} = Encode::resolve_alias($input)) {
                if ($row{set} eq 'Internal') {
-                       $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', $@ || $!);
+                       $row{endpoint} = ($endpoint || 8191) * $row{cell}->{colsize};
                }
                elsif ($row{set} eq 'utf-8-strict') {
-                       $row{len} = 256;
                        $row{set} = 'UTF-8';
                        $row{cell} = do 'charset-utf8.inc.pl'
                                or Alert('Table data could not be read', $@ || $!);
+                       $row{endpoint} = 255;
                }
                else {
                        if ($row{set} eq 'MacHebrew') {
@@ -199,7 +200,7 @@ sub tabinput {
                                # 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};
+                       $row{endpoint} = $endpoint - $row{offset};
 
                        if ($row{set} eq 'cp437' and !$row{offset}) {
                                substr($row{table}, 237, 1) = pack 'U*', 0x3D5; # phi sign
@@ -212,7 +213,7 @@ sub tabinput {
                        }
 
                        $visible->{ascii} =  # assume common base
-                       $visible->{ $row{set} } = undef;
+                       $visible->{ $row{set} } = 1;
                }
        }
        else {
@@ -284,7 +285,6 @@ 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 + $row->{len} * $colsize;
 
        printf '<div class="section"><table class="glyphs%s">', !$row->{cell} && ' charmap';
        my $title = $row->{set};
@@ -298,7 +298,7 @@ for my $row (@request) {
                print "\n";
        }
        print '<tbody>';
-       while ($offset < $endpoint - 1) {
+       while ($offset < $row->{endpoint}) {
                print '<tr><th>';
                {
                        if (my $rowmod = $offset % $rowdiv) {
@@ -323,6 +323,20 @@ for my $row (@request) {
                                next;
                        }
 
+                       if (exists $get{compare}) {
+                               state $visible = {};
+                               my $cp = $offset + $row->{offset};
+                               printf '<td class="%s" title="%3$s">%2$s',
+                                       $cp == ord $glyph ? 'l4' :
+                                       $row->{parent} && $glyph eq
+                                               Encode::decode($row->{parent}, pack 'C', $cp) ? 'l3' :
+                                       $visible->{$glyph} ? 'l2' :
+                                       'l1',
+                                       $glyphs->glyph_html($glyph);
+                               $visible->{$glyph}++;
+                               next;
+                       }
+
                        print "\n".$glyphs->glyph_cell($glyph);
                }
                continue {
@@ -337,7 +351,13 @@ for my $row (@request) {
 <hr>
 
 <div class="legend">
-       <table class="glyphs"><tr>
+       <table class="glyphs"><tr><: if (exists $get{compare}) { :>
+       <td class="X l4">unicode
+       <td class="X l3">inherited
+       <td class="X l2">existing
+       <td class="X l1">original
+       <td class="">unassigned
+<: } else { :>
        <td class="X Cc">control
        <td class="X Zs"><span>whitespace</span>
        <td class="X Mn">diacritic<table class="glyphs"><tr>
@@ -374,6 +394,6 @@ for my $row (@request) {
        <td class="X Xd">deprecated
        <td class="">unassigned
        <td class="X Xi">invalid
-       </table>
+<: } :>        </table>
 </div>