termcol: move palette data into separate include
[sheet.git] / charset.plp
1 <(common.inc.plp)><:
2
3 Html({
4         title => 'charset cheat sheet',
5         version => '1.0',
6         description => [
7                 "Reference sheet with all glyphs in common character encoding tables,",
8                 "and an overview of Unicode ranges and UTF-8 bytes.",
9         ],
10         keywords => [qw'
11                 charset codepage unicode ascii utf8 latin glyph character encoding
12                 reference common overview table
13         '],
14         stylesheet => [qw'light'],
15         data => [qw'charset-unicode.inc.pl charset-utf8.inc.pl'],
16 });
17
18 :>
19 <h1>Character encoding</h1>
20
21 <:
22 use Shiar_Sheet::FormatChar;
23 my $glyphs = Shiar_Sheet::FormatChar->new;
24 my @nibble = (0..9, 'A'..'F');
25 my $nibsize = 1;
26
27 use Encode qw(decode resolve_alias);
28 # generate character table(s)
29 # (~16x faster than decoding in loop;
30 #  substr strings is twice as fast as splitting to an array)
31 my %ALIAS = (
32 #       default => [qw(unicode utf-8 iso-8859-1 cp437 -cp1252- --iso-8859-15- -koi8-f)],
33         default => [qw(unicode- utf-8 iso-8859-1 -cp1252- --iso-8859-15- cp437 -cp850)],
34         0 => [qw(cp437 cp863)],
35         1 => [qw(iso-8859-1 cp1252 MacRoman cp850)],
36         2 => [qw(iso-8859-2 cp1250 cp852 MacCentralEurRoman MacCroatian MacRumanian)],
37         5 => [qw(koi8-f iso-8859-5 cp1251 MacCyrillic cp855 cp866)],
38         7 => [qw(iso-8859-7 cp1253 MacGreek cp737 cp869)],
39         8 => [qw(iso-8859-8 cp1255 MacHebrew cp862)],
40 );
41 my @request = map {
42         if (my $input = $_) {
43                 my %row = (offset => 0);
44                 my $endpoint = 255;
45                 if ($input =~ s/^--//) {
46                         $row{offset} = $endpoint > 160 ? 160 : 48;
47                 }
48                 elsif ($input =~ s/^-//) {
49                         $row{offset} = $endpoint > 128 ? 128 : 32;
50                 }
51                 if ($input =~ s/-$//) {
52                         $endpoint = $row{offset} ? $row{offset} < 160 ? 159 : 191 : 127;
53                 }
54
55                 if ($input =~ /^U([0-9a-f]+)(?:-([0-9a-f]+))?/) {
56                         my $start = hex($1) << ($2 ? 4 : 8);
57                         my $end = $2 ? hex($2) << 4 : $start + 240;
58                         $row{table} = join '', map { chr } $start .. $end+15;
59                         utf8::upgrade($row{table});  # prevent latin1 output
60                         $row{set} = sprintf 'Unicode block U+%02Xxx', $start >> 8;
61                 }
62                 elsif ($input eq 'U') {
63                         $row{table} = ' ' x 1024;
64                         $row{set} = 'Unicode planes';
65                         $row{cell} = do 'charset-ucplanes.inc.pl';
66                         @nibble = (map { $_.0, $_.8 } 0 .. 7);
67                         $nibsize = 8;
68                 }
69                 elsif ($row{set} = resolve_alias($input)) {
70                         if ($row{set} eq 'Internal') {
71                                 $row{table} = ' ' x ($endpoint < 255 ? 640 : 8192);
72                                 $row{set} = 'Unicode BMP';
73                                 $row{cell} = do 'charset-unicode.inc.pl';
74                         }
75                         elsif ($row{set} eq 'utf-8-strict') {
76                                 $row{table} = undef;
77                                 $row{set} = 'UTF-8';
78                                 $row{cell} = do 'charset-utf8.inc.pl';
79                         }
80                         else {
81                                 $row{table} = decode($row{set}, pack 'C*', $row{offset} .. $endpoint);
82                         }
83                 }
84                 else {
85                         print "<p>Encoding $input unknown</p>\n";
86                 }
87                 \%row;
88         }
89         else {
90                 ();
91         }
92 } map { defined $ALIAS{$_} ? @{ $ALIAS{$_} } : $_ }
93         $ENV{PATH_INFO} =~ /\w/ ? split(m{[/+\s]}, $ENV{PATH_INFO}) : 'default';
94 my $NOCHAR = chr 0xFFFD;
95
96 for my $cp437 (grep {$request[$_]->{set} eq 'cp437'} 0 .. $#request) {
97         substr($request[$cp437]->{table}, 237, 1) = pack 'U*', 0x3D5; # phi sign
98         substr($request[$cp437]->{table}, 0, 32) = pack 'U*', map {hex} qw(
99                 2007 263A 263B 2665 2666 2663 2660 2022 25D8 25CB 25D9 2642 2640 266A 266B 263C
100                 25BA 25C4 2195 203C 00B6 00A7 25AC 21A8 2191 2193 2192 2190 221F 2194 25B2 25BC
101         );
102 }
103
104 for my $row (@request) {
105         printf '<div class="section"><table class="glyphs%s">', !$row->{cell} && ' charmap';
106         printf '<caption>%s</caption>', $row->{set};
107         print '<col>' x 17;
108         for my $section (qw{thead}) {
109                 print "<$section><tr><th>↱";
110                 print '<th>', $_ for @nibble;
111                 print "\n";
112         }
113         print '<tbody>';
114         for my $msb (0 .. (length($row->{table}) || 256) - 1 >> 4) {
115                 printf '<tr><th>%X', $msb + ($row->{offset} >> 4);
116                 for my $lsb (0 .. $#nibble) {
117                         my $val = ( ($msb<<4) + $lsb ) * $nibsize;
118                         if ($row->{cell}) {
119                                 print $row->{cell}->($val);
120                                 next;
121                         }
122
123                         my $glyph = substr $row->{table}, $val, 1;
124                         if ($glyph eq $NOCHAR) {
125                                 print '<td>';
126                                 next;
127                         }
128
129                         print "\n".$glyphs->glyph_cell($glyph);
130                 }
131                 print "\n";
132         }
133         print "</table></div>\n";
134 }
135
136 :>
137 <hr>
138
139 <div class="legend">
140         <table class="glyphs"><tr>
141         <td class="X Cc">control
142         <td class="X Zs"><span>whitespace</span>
143         <td class="X Mn">diacritic<table class="glyphs"><tr>
144                 <td class="X Sk">letter
145                 </table>
146         <td class="X Po">punctuation<table class="glyphs"><tr>
147                 <td class="X Pf">quote
148                 </table>
149         <td class="X So">symbol<table class="glyphs"><tr>
150                 <td class="X Sm">math
151                 <td class="X Sc">currency
152                 </table>
153         <td class="X No">numeric
154         <td class="X Greek">greek<table class="glyphs"><tr>
155                 <td class="X Latin">latin
156                 <td class="X Cyrillic">cyrillic
157                 </table>
158         <td class="X Aramaic">aramaic<table class="glyphs"><tr>
159                 <td class="X Brahmic">brahmic
160                 <td class="X Arabic">arabic
161                 </table>
162         <td class="X Syllabic">syllabic<table class="glyphs"><tr>
163                 <td class="X African">african
164                 <td class="X Hiragana">japanese
165                 <td class="X Han">cjk
166                 <td class="X Bopomofo">chinese
167                 </table>
168         <td class="X Alpha">alphabetic
169         </table>
170
171         <table class="glyphs"><tr>
172         <td class="X">unicode 7.0
173         <td class="X Xr">proposed
174         <td class="X Xd">deprecated
175         <td class="">unassigned
176         <td class="X Xi">invalid
177         </table>
178 </div>
179