aboutsummaryrefslogtreecommitdiff
path: root/a8utf8
blob: 0cca2acf8a46cf8fe5266f3026f2ab1f71dd3531 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/perl -w

# convert A8 text to UTF-8. Control graphics characters are replaced with
# nearest Unicode equivalents (mostly from the box-drawing range, or from
# the basic-latin range with -i option).

# Careful editing this script: you need an editor that groks UTF-8, or at
# least one that won't mangle the UTF-8 sequences embedded in the tables
# below.

($SELF = $0) =~ s,.*/,,;

binmode(STDOUT, ":utf8");
binmode(STDIN, ":bytes");

use utf8;

%atascii_table = (
	0 => "♥",
	1 => "┣",
	2 => "┃",
	3 => "┛",
	4 => "┫",
	5 => "┓",
	6 => "╱",
	7 => "╲",
	8 => "◢",
	9 => "▗",
	10 => "◣",
	11 => "▝",
	12 => "▘",
	13 => "▔",
	14 => "▁",
	15 => "▖",
	16 => "♣",
	17 => "┏",
	18 => "━",
	19 => "╋",
	20 => "●",
	21 => "▄",
	22 => "▎",
	23 => "┳",
	24 => "┻",
	25 => "▌",
	26 => "┗",
	27 => "␛",
	28 => "↑",
	29 => "↓",
	30 => "←",
	31 => "→",
	96 => "◆",
	123 => "♠",
	125 => "↰",
	126 => "◀",
	127 => "▶",
	136 => "◤",
	137 => "▛",
	138 => "◥",
	139 => "▙",
	140 => "▟",
	141 => "▆",
	142 => "🮅",
	143 => "▜",
	148 => "◙",
	149 => "▀",
	150 => "🮊",
	153 => "▐",
	155 => "\n",
	156 => "⍐",
	157 => "⍗",
	158 => "⍇",
	159 => "⍈",
	160 => "█",
);

%xl_intl_table = (
	0 => "á",
	1 => "ù",
	2 => "Ñ",
	3 => "É",
	4 => "ç",
	5 => "ô",
	6 => "ò",
	7 => "ì",
	8 => "£",
	9 => "ï",
	10 => "ü",
	11 => "ä",
	12 => "Ö",
	13 => "ú",
	14 => "ó",
	15 => "ö",
	16 => "Ü",
	17 => "â",
	18 => "û",
	19 => "î",
	20 => "é",
	21 => "è",
	22 => "ñ",
	23 => "ê",
	24 => "ȧ",
	25 => "à",
	26 => "Ȧ",
	27 => "␛",
	28 => "↑",
	29 => "↓",
	30 => "←",
	31 => "→",
	96 => "¡",
	123 => "Ä",
	126 => "◀",
	127 => "▶",
	155 => "\n",
);

undef $/;

$table = \%atascii_table;
$print_table = 0;
while(@ARGV && $ARGV[0] =~ /^-/) {
	for($ARGV[0]) {
		/^-i$/ && do { $table = \%xl_intl_table; next; };
		/^-t$/ && do { $print_table = 1; next; };
		/^-r$/ && do { $reverse = 1; next; };
		/^--?h/ && do { usage(0) };
		warn "$SELF: unknown option: $_\n";
		usage(1);
	}
	shift @ARGV;
}

if($reverse) {
	binmode(STDOUT, ":bytes");
	for(keys %$table) {
		$revtable{ord($table->{$_})} = chr($_);
	}
	$table = \%revtable;
}

if($print_table) {
	for(sort { $a <=> $b } keys %$table) {
		my $chr = translate(chr $_);
		$chr = '\n' if $chr eq "\n";
		printf '"%d","$%02x","%s"' . "\n", $_, $_, $chr;
	}
	exit 0;
}

$_ = <>;
s/(.)/translate($1)/seg;
print;

sub translate {
	my $o = ord(shift);
	my $ret;

	$ret = $table->{$o};
	return $ret if defined($ret);

	$ret = $table->{$o & 0x7f};
	return $ret if defined($ret);

	return chr($o & 0x7f);
}

sub usage {
	print <<EOF;
Usage: $SELF [--help] | [-t [-r] [-i]] | [-r] [-i] infile [infile ...]
See man page for details.
EOF
	exit $_[0];
}