charset: cp437 graphical chars table
[sheet.git] / charset.plp
1 <:
2 use utf8;
3 use strict;
4 use warnings;
5 use open IO => ':utf8';
6
7 our $VERSION = '1.0';
8
9 $header{content_type} = 'text/html; charset=utf-8';
10
11 :><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
12  "http://www.w3.org/TR/html4/loose.dtd">
13 <html>
14
15 <head>
16 <title>charset cheat sheet</title>
17 <meta http-equiv="content-type" content="utf-8">
18 <link rel="stylesheet" type="text/css" media="all" href="/base.css">
19 </head>
20
21 <body>
22 <h1>Character encoding</h1>
23
24 <:
25 my $diinfo = do 'digraphs.inc.pl';
26 my %di = map { $diinfo->{$_}->[0] => $_ } grep { ref $diinfo->{$_} }
27         keys %$diinfo;
28
29 use Encode qw(decode);
30 # generate character table(s)
31 # (~16x faster than decoding in loop;
32 #  substr strings is twice as fast as splitting to an array)
33 my @tables = map { decode($_, pack 'C*', 0..255) } 'iso-8859-1', 'cp437';
34 my $NOCHAR = chr 0xFFFD;
35
36 for my $cp437 (grep {$request[$_] eq 'cp437'} 0 .. $#request) {
37         substr($tables[$cp437], 237, 1) = pack 'U*', 0x3D5; # phi sign
38         substr($tables[$cp437], 0, 32) = pack 'U*', map {hex} qw(
39                 2007 263A 263B 2665 2666 2663 2660 2022 25D8 25CB 25D9 2642 2640 266A 266B 263C
40                 25BA 25C4 2195 203C 00B6 00A7 25AC 21A8 2191 2193 2192 2190 221F 2194 25B2 25BC
41         );
42 }
43
44 sub quote {
45         local $_ = shift;
46         s/"/&quot;/g;
47         s/</&lt;/g;
48         s/>/&gt;/g;
49         return $_;
50 }
51
52 my @nibble = (0..9, 'A'..'F');
53 for my $table (@tables) {
54         print '<table class="glyphs"><col>';
55         for my $section (qw{thead tfoot}) {
56                 print "<$section><tr><th>↳";
57                 print '<th>', $_ for @nibble;
58                 print "<th>&nbsp;\n";
59         }
60         print '<tbody>';
61         for my $msb (0 .. $#nibble) {
62                 print '<tr><th>', $nibble[$msb];
63                 for my $lsb (0 .. $#nibble) {
64                         my $glyph = substr $table, ($msb<<4) + $lsb, 1;
65                         if ($glyph eq $NOCHAR) {
66                                 print '<td>';
67                                 next;
68                         }
69                         my $info = [ord $glyph];
70                         if (defined (my $mnem = $di{ord $glyph})) {
71                                 $info = $diinfo->{$mnem};
72                         }
73                         my ($codepoint, $name, $prop, $script, $string) = @$info;
74
75                         $glyph = quote($string || $glyph);
76                         my $desc = sprintf 'U+%04X%s', $codepoint, $name && " ($name)";
77                         my @class = ('X', grep {$_} $prop, $script);
78
79                         $glyph = "<span>$glyph</span>" if $prop eq 'Zs';
80
81                         printf "\n".'<td class="%s" title="%s">%s',
82                                 join(' ', @class), quote($desc), $glyph;
83                 }
84                 print "\n<th>", $nibble[$msb], "\n";
85         }
86         print "</table>\n";
87 }
88