-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasciiart2svg.pl
309 lines (281 loc) · 8.5 KB
/
asciiart2svg.pl
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
use strict;
use warnings;
use Getopt::Long;
use Data::Dumper;
=head1
amiga ascii 2 svg
by venam
https://venam.nixers.net
https://github.com/venam
=cut
=head2
perl $0 -i <ascii> -f <fontname> -c <colors> -o <output>
-h print help
ascii: file or -
fn: name or sauce
colors: default or Xresources or custom file
=cut
=head3
A bit inspired by:
https://github.com/haliphax/ascii.js
https://ivanceras.github.io/elm-examples/elm-bot-lines/
=cut
=head3
Welcome to a horribly put together script.
TODO:
* Add ability to change font configurations (use from SAUCE line?)
* Add ability to change the colorscheme
* Polish script
=cut
my $input_file = "-";
my $data = "file.dat";
my $font_name = 'Topaz a600a1200a400';
my $colors = 'default';
my $output_file = 'stdout';
my $help = 0;
my %colorscheme = (
'default_background' => '#1c1c1a',
'black' => '#342f28',
'red' => '#a36f48',
'green' => '#9b9630',
'yellow' => '#b68740',
'cyan' => '#8e8e72',
'magenta' => '#b68c7c',
'blue' => '#7c7b6f',
'white' => '#dac78b',
'l_black' => '#606747',
'l_red' => '#c8623e',
'l_green' => '#a0bc30',
'l_yellow' => '#be8136',
'l_cyan' => '#7e9f86',
'l_magenta' => '#a06f6a',
'l_blue' => '#595D64',
'l_white' => '#eaddc5'
);
my %COLORS_INDEXES = (
background => 'default_background',
color0 => 'black',
color1 => 'red',
color2 => 'green',
color3 => 'yellow',
color4 => 'cyan',
color5 => 'magenta',
color6 => 'blue',
color7 => 'white',
color8 => 'l_black',
color9 => 'l_red',
color10 => 'l_green',
color11 => 'l_yellow',
color12 => 'l_cyan',
color13 => 'l_magenta',
color14 => 'l_blue',
color15 => 'l_white'
);
GetOptions (
"input=s" => \$input_file,
"font=s" => \$font_name,
"colors=s" => \$colors,
"output=s" => \$output_file,
"help" => \$help
) or die("Error in command line arguments\n");
if ($help) {
print "perl $0 -i <ascii> -f <fontname> -c <colors> -o <output>
ascii: file or -
fn: name or sauce
colors: default or Xresources or custom file (formated like Xresources)
-h print this help
";
exit;
}
sub extract_hex {
my ($source) = @_;
my $fh;
if ($source eq '-s') {
$fh = \*stdin;
}
else {
open($fh, "<", $source) or die "$!";
}
while (<$fh>) {
for my $i (keys %COLORS_INDEXES) {
if (/^[^\!]*$i\s*:/) {
chomp;
$_ =~ /#([\da-f]{6})/i;
$colorscheme{$COLORS_INDEXES{$i}} = '#'.lc($1);
}
}
}
}
if (lc($colors) eq 'xresources') {
my $resources = "$ENV{HOME}/.Xresources";
extract_hex($resources);
} elsif (lc($colors) ne 'default') {
# in case we get something that isn't default => load from file
extract_hex($colors);
}
my $fh;
if ($input_file eq '-') {
$fh = \*STDIN;
}
else {
open ($fh, $input_file) or die $!;
}
my $pixel_size = 12;
my @colors_index = ('black','red','green','yellow','cyan','magenta','blue','white');
my $pixel_hor_size = $pixel_size/2.3+1;
my $b_hor_size = $pixel_size/2.1+1.5;
my $pixel_ver_size = $pixel_size+1.2;
my $b_ver_size = $pixel_size+1.59;
my $start_x = $pixel_size/2.0;
my $start_y = $pixel_size+2;
my ($x,$y) = ($start_x,$start_y);
my $output = "";
my $back_output = "";
my $max_width = 0;
my $max_height = 0;
#default to black colorscheme
my $fg_color = $colorscheme{'white'};
my $bg_color = 'none';
# Possible states are:
# escape -> found the start of escape \x1b
# enter_escape -> \[
# in_escape -> anything in between
# end_escape -> character that ends the escape, anything that isn't ;
# or digit usually C or m for forwarding cursor and color respectively
my $state = "normal";
# stores everything that has been captured so far in an escape
my $state_value = "";
my $bold = 0;
for (<$fh>) {
last if (/\x1aSAUCE/);
$max_height++;
my $char = $_;
$char =~ s/\r//g;
$char =~ s/\x1b\[\d+D//g;
$char =~ s/\x1b\[\d+D//g;
$char =~ s/\x1b\[A\n\x1b\[\d+C//g;
$char =~ s/\x1a(?:.|\n)+$//g;
$char =~ s/\x19\x00\x00.*$//g;
my @line = split //, $char;
my $width = 0;
for (@line) {
my $c = $_;
$c =~ s/&/&/g;
$c =~ s/</</g;
$c =~ s/>/>/g;
# change state
if ($state eq 'enter_escape') {
# it's the end of the parsing
if ($c eq 'm' or $c eq 'C') {
# reset the state to normal
if ($c eq 'C') {
for (1..$state_value) {
$width++;
if ($bg_color ne 'none') {
$back_output .= "<rect x='$x' fill='$bg_color' y='$y' width='$b_hor_size' height='$b_ver_size'/>";
}
$x += $pixel_hor_size;
}
}
else {
my @cols = split /;/, $state_value;
my $fg_col = 'nil';
my $bg_col = 'nil';
for my $code (@cols) {
if ($code == 0) {
$fg_col = 7;
$bg_col = 0;
#$bold = 0;
}
elsif ($code == 1) {
$bold = 1;
}
elsif ($code == 22) {
$bold = 0;
}
elsif ($code >= 30 and $code <= 37) {
$fg_col = $code - 30;
}
elsif ($code == 39) {
$fg_col = 7;
}
elsif ( $code >= 40 and $code <= 47) {
$bg_col = $code - 40;
}
elsif ( $code == 49) {
$bg_col = 0;
}
}
unless ($fg_col eq 'nil') {
my $chosen_col = $colors_index[$fg_col];
$chosen_col = 'l_'.$chosen_col if ($bold == 1);
$fg_color = $colorscheme{$chosen_col};
}
unless ($bg_col eq 'nil') {
if ($bg_col == 0) {
$bg_color = 'none';
}
else {
my $chosen_col = $colors_index[$bg_col];
$chosen_col = 'l_'.$chosen_col if ($bold == 1);
$bg_color = $colorscheme{$chosen_col};
}
}
}
$state = 'normal';
$state_value = '';
next;
}
else {
$state_value .= $c;
next;
}
}
if ($state eq 'escape') {
if (my $a = ($c =~ /(\[)/)) {
$state = "enter_escape";
next;
}
}
# we encounter an escape character
if (my $a = ($c =~ /(\x1b)/)) {
$state = "escape";
next;
}
$width++;
if ($bg_color ne 'none') {
$back_output .= "<rect x='$x' fill='$bg_color' y='$y' width='$b_hor_size' height='$b_ver_size'/>";
}
$output .= "<text fill='$fg_color' x='$x' y='$y' style='font-family:$font_name;'>
";
$output .= "$c";
$output .= "</text>";
$x += $pixel_hor_size;
}
$max_width = $width if ($width > $max_width);
$output .= "\n";
$x = $start_x;
$y += $pixel_ver_size;
}
$max_height++;
$max_width++;
$max_height *= $pixel_ver_size;
$max_width *= $pixel_hor_size;
$output = "<?xml version='1.0' encoding='utf-8' standalone='no'?>
<!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.1//EN' 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'>
<svg
version='1.1'
width='$max_width'
height='$max_height'
style='background-color: ".$colorscheme{"default_background"}.";'
xmlns='http://www.w3.org/2000/svg'>
<g style='font-size: ${pixel_size}px'>".$back_output ."\n".$output;
$output .= "</g>\n"."</svg>";
if ($output_file eq 'stdout') {
print $output;
} else {
open(OUT, '>', $output_file) or die("couldn't write output svg");
print OUT $output;
close(OUT);
}
1;