| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
=head1 NAME |
|
2
|
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
Term::Graille - Graphical Display in the terminal using UTF8 Braille characters |
|
4
|
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
6
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
my $canvas = Term::Graille->new( |
|
8
|
|
|
|
|
|
|
width => 72, # pixel width |
|
9
|
|
|
|
|
|
|
height => 64, # pixel height |
|
10
|
|
|
|
|
|
|
top=>3, # row position in terminal (optional,defaults to 1) |
|
11
|
|
|
|
|
|
|
left=>10, # column position (optional,defaults to 1) |
|
12
|
|
|
|
|
|
|
borderStyle => "double", # |
|
13
|
|
|
|
|
|
|
); |
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
16
|
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
Inspired by Drawille by asciimoo, which has many variants (including |
|
18
|
|
|
|
|
|
|
a perl variant Term::Drawille by RHOELZ), this is a clone with a few |
|
19
|
|
|
|
|
|
|
extras. The goal is to achieve performance and features. with built-in |
|
20
|
|
|
|
|
|
|
turtle-like graphics, line and curve drawing (Algorithm::Line::Bresenham), |
|
21
|
|
|
|
|
|
|
scrolling, border setting, and more in development. |
|
22
|
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
=begin html |
|
24
|
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
=end html |
|
28
|
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
=head1 FUNCTIONS |
|
31
|
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
=cut |
|
33
|
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
package Term::Graille; |
|
35
|
|
|
|
|
|
|
|
|
36
|
1
|
|
|
1
|
|
73564
|
use strict;use warnings; |
|
|
1
|
|
|
1
|
|
3
|
|
|
|
1
|
|
|
|
|
29
|
|
|
|
1
|
|
|
|
|
5
|
|
|
|
1
|
|
|
|
|
11
|
|
|
|
1
|
|
|
|
|
38
|
|
|
37
|
|
|
|
|
|
|
our $VERSION="0.10"; |
|
38
|
1
|
|
|
1
|
|
601
|
use utf8; |
|
|
1
|
|
|
|
|
14
|
|
|
|
1
|
|
|
|
|
5
|
|
|
39
|
1
|
|
|
1
|
|
568
|
use open ":std", ":encoding(UTF-8)"; |
|
|
1
|
|
|
|
|
1220
|
|
|
|
1
|
|
|
|
|
7
|
|
|
40
|
1
|
|
|
1
|
|
12536
|
use base 'Exporter'; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
168
|
|
|
41
|
|
|
|
|
|
|
our @EXPORT_OK = qw/colour paint printAt clearScreen border blockBlit block2braille pixelAt cursorAt wrapText/; |
|
42
|
1
|
|
|
1
|
|
6
|
use Algorithm::Line::Bresenham 0.15; |
|
|
1
|
|
|
|
|
20
|
|
|
|
1
|
|
|
|
|
39
|
|
|
43
|
1
|
|
|
1
|
|
551
|
use Time::HiRes "sleep"; |
|
|
1
|
|
|
|
|
1408
|
|
|
|
1
|
|
|
|
|
5
|
|
|
44
|
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
BEGIN { |
|
46
|
1
|
50
|
|
1
|
|
838
|
if ($^O eq "MSWin32") { |
|
47
|
0
|
|
|
|
|
0
|
system("chcp 65001 >nul"); |
|
48
|
0
|
|
|
|
|
0
|
system("echo 'Please set console font to one that can handle utf8 fonts'") |
|
49
|
|
|
|
|
|
|
} |
|
50
|
|
|
|
|
|
|
} |
|
51
|
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
=head3 Cnew(%params)> |
|
53
|
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
Creates a new canavas; params are |
|
55
|
|
|
|
|
|
|
C The pixel width, required C the pixel height, required, |
|
56
|
|
|
|
|
|
|
C terminal row (optional,default 1) C terminal column ( |
|
57
|
|
|
|
|
|
|
optional,default 1) C border type (optional,one of 'simple', |
|
58
|
|
|
|
|
|
|
'double', 'thin', 'thick' or 'shadow') C border colour ( |
|
59
|
|
|
|
|
|
|
optional), C Title text for top border,(optional) C |
|
60
|
|
|
|
|
|
|
Title colour (optional) |
|
61
|
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=cut |
|
63
|
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub new{ |
|
66
|
0
|
|
|
0
|
1
|
0
|
my ( $class, %params ) = @_; # params are width and height in pixels |
|
67
|
0
|
|
|
|
|
0
|
my $self={width=>$params{width},height=>$params{height}}; |
|
68
|
0
|
|
|
|
|
0
|
for my $key (qw/borderStyle borderColour title titleColour top left cartesian/){ |
|
69
|
0
|
0
|
|
|
|
0
|
$self->{ $key}=$params{$key} if exists $params{$key} |
|
70
|
|
|
|
|
|
|
} |
|
71
|
0
|
|
0
|
|
|
0
|
$self->{top}//=1; |
|
72
|
0
|
|
0
|
|
|
0
|
$self->{left}//=1; |
|
73
|
0
|
|
0
|
|
|
0
|
$self->{cartesian}//=1; |
|
74
|
0
|
|
|
|
|
0
|
$self->{setPix}=[['⡀','⠄','⠂','⠁'],['⢀','⠠','⠐','⠈']]; |
|
75
|
0
|
|
|
|
|
0
|
$self->{unsetPix}=[['⢿','⣻','⣽','⣾'],['⡿','⣟','⣯','⣷']]; |
|
76
|
|
|
|
|
|
|
$self->{logoVars}={x=>$self->{width}/2, # integrated Turtle Graphics |
|
77
|
0
|
|
|
|
|
0
|
y=>$self->{height}/2, # initial variables x, y and direction |
|
78
|
|
|
|
|
|
|
d=>0, |
|
79
|
|
|
|
|
|
|
p=>1, |
|
80
|
|
|
|
|
|
|
GV=>{}, |
|
81
|
|
|
|
|
|
|
PC=>0, |
|
82
|
|
|
|
|
|
|
Stk=>[],}; |
|
83
|
0
|
|
|
|
|
0
|
bless $self,$class; |
|
84
|
0
|
|
|
|
|
0
|
$self->clear; # initiallises canvas to blank |
|
85
|
0
|
|
|
|
|
0
|
return $self; |
|
86
|
|
|
|
|
|
|
} |
|
87
|
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
=head3 C<$canvas-Edraw()> , C<$canvas-Edraw($row, $column)> |
|
90
|
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
Draws the canvas to the terminal window. Optional row and column |
|
92
|
|
|
|
|
|
|
parameters may be passed to position the displayed canvas. If |
|
93
|
|
|
|
|
|
|
borderStyle is specified, the border is drawn, If title is specified |
|
94
|
|
|
|
|
|
|
this is added to the top border |
|
95
|
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=cut |
|
97
|
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
sub draw{ |
|
99
|
0
|
|
|
0
|
1
|
0
|
my ($self,$top,$left)=@_; # the location on the screen can be overridden by passing row coloum position |
|
100
|
0
|
|
0
|
|
|
0
|
$top//=$self->{top};$left//=$self->{left}; |
|
|
0
|
|
0
|
|
|
0
|
|
|
101
|
0
|
|
|
|
|
0
|
border($top-1,$left-1,$top+@{$self->{grid}}-1,$left+@{$self->{grid}->[0]}, |
|
|
0
|
|
|
|
|
0
|
|
|
102
|
|
|
|
|
|
|
$self->{borderStyle},$self->{borderColour}, |
|
103
|
|
|
|
|
|
|
$self->{title},$self->{titleColour}) |
|
104
|
0
|
0
|
0
|
|
|
0
|
if ((defined $self->{borderStyle})&&(ref $self->{grid} eq "ARRAY")); |
|
105
|
0
|
|
|
|
|
0
|
printAt($top,$left, [reverse @{$self->{grid}}]); |
|
|
0
|
|
|
|
|
0
|
|
|
106
|
0
|
|
|
|
|
0
|
print colour("reset"); |
|
107
|
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
} |
|
109
|
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=head3 C<$canvas-Eas_string()> |
|
111
|
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
Returns the string containing the canvas of utf8 braille symbols, rows |
|
113
|
|
|
|
|
|
|
being separated by newline. |
|
114
|
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=cut |
|
116
|
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
sub as_string{ |
|
118
|
0
|
|
|
0
|
1
|
0
|
my $self=shift; |
|
119
|
0
|
|
|
|
|
0
|
my $str=""; |
|
120
|
0
|
|
|
|
|
0
|
$str.=join("",@$_)."\n" foreach (reverse @{$self->{grid}}); |
|
|
0
|
|
|
|
|
0
|
|
|
121
|
0
|
|
|
|
|
0
|
return $str; |
|
122
|
|
|
|
|
|
|
} |
|
123
|
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=head3 C<$canvas-Eset($x,$y,$pixelValue)> |
|
125
|
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
Sets a particular pixel on (default if C<$pixelValue> not sent) or off. |
|
127
|
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=cut |
|
129
|
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
# this function creates a callback for plotting pixels directly |
|
131
|
|
|
|
|
|
|
sub BresenhamPlot{ |
|
132
|
0
|
|
|
0
|
0
|
0
|
my($x,$y,$args)=@_; |
|
133
|
0
|
|
|
|
|
0
|
my ($canvas,$value)=(@$args); |
|
134
|
0
|
|
|
|
|
0
|
set($canvas,$x,$y,$value); |
|
135
|
|
|
|
|
|
|
} |
|
136
|
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
sub set{ |
|
138
|
1
|
|
|
1
|
|
8
|
use integer; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
8
|
|
|
139
|
0
|
0
|
|
0
|
1
|
0
|
push @_, 1 if @_ == 3; |
|
140
|
0
|
|
|
|
|
0
|
my ($self,$x,$y,$value)=@_; |
|
141
|
|
|
|
|
|
|
#exit if out of bounds |
|
142
|
0
|
0
|
0
|
|
|
0
|
return unless(($x<$self->{width})&&($x>=0)&&($y<$self->{height})&&($y>=0)); |
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
143
|
|
|
|
|
|
|
#convert coordinates to character / pixel offset position |
|
144
|
0
|
|
|
|
|
0
|
my ($chrX,$chrY,$xOffset,$yOffset)=$self->charOffset($x,$y); |
|
145
|
|
|
|
|
|
|
|
|
146
|
0
|
|
|
|
|
0
|
my $bChr=chop($self->{grid}->[$chrY]->[$chrX]); |
|
147
|
0
|
0
|
|
|
|
0
|
if ($value=~/^[a-z]/){$self->{grid}->[$chrY]->[$chrX]=colour($value);} |
|
|
0
|
0
|
|
|
|
0
|
|
|
148
|
0
|
|
|
|
|
0
|
elsif ($value=~/^\033\[/){$self->{grid}->[$chrY]->[$chrX]=$value;} |
|
149
|
|
|
|
|
|
|
# ensure character is a braille character to start with |
|
150
|
0
|
0
|
|
|
|
0
|
$bChr='⠀' if (ord($bChr)&0x2800 !=0x2800); |
|
151
|
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
$self->{grid}->[$chrY]->[$chrX].=$value? # if $value is false, unset, or else set pixel |
|
153
|
|
|
|
|
|
|
(chr( ord($self->{setPix} -> [$xOffset]->[$yOffset]) | ord($bChr) ) ): |
|
154
|
0
|
0
|
|
|
|
0
|
(chr( ord($self->{unsetPix}-> [$xOffset]->[$yOffset]) & ord($bChr))); |
|
155
|
|
|
|
|
|
|
} |
|
156
|
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=head3 C<$canvas-Eunset($x,$y)> |
|
158
|
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
Sets the pixel value at C<$x,$y> to blank |
|
160
|
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
=cut |
|
162
|
|
|
|
|
|
|
sub unset{ |
|
163
|
0
|
|
|
0
|
1
|
0
|
my ($self,$x,$y)=@_; |
|
164
|
0
|
|
|
|
|
0
|
$self->set($x,$y,0); |
|
165
|
|
|
|
|
|
|
} |
|
166
|
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
sub charOffset{ # gets the character grid position and offset within that character |
|
168
|
|
|
|
|
|
|
# give the pixel position |
|
169
|
1
|
|
|
1
|
|
368
|
use integer; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
13
|
|
|
170
|
0
|
|
|
0
|
0
|
0
|
my ($self,$x,$y)=@_; |
|
171
|
0
|
0
|
0
|
|
|
0
|
return -1 unless(($x<$self->{width})&&($x>=0)&&($y<$self->{height})&&($y>=0)); |
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
172
|
0
|
|
|
|
|
0
|
my $chrX=$x/2;my $xOffset=$x- $chrX*2; |
|
|
0
|
|
|
|
|
0
|
|
|
173
|
0
|
|
|
|
|
0
|
my $chrY=$y/4;my $yOffset=$y- $chrY*4; |
|
|
0
|
|
|
|
|
0
|
|
|
174
|
0
|
|
|
|
|
0
|
return ($chrX,$chrY,$xOffset,$yOffset); |
|
175
|
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
} |
|
177
|
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
=head3 C<$canvas-Epixel($x,$y)> |
|
179
|
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
Gets the pixel value at C<$x,$y> |
|
181
|
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=cut |
|
183
|
|
|
|
|
|
|
sub pixel{ #get pixel value at coordinates |
|
184
|
0
|
|
|
0
|
1
|
0
|
my ($self,$x,$y,$value)=@_; |
|
185
|
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
#exit if out of bounds |
|
187
|
0
|
0
|
0
|
|
|
0
|
return unless(($x<($self->{width}-1))&&($x>=0)&&($y<($self->{height}-1))&&($x>=0)); |
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
188
|
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
#convert coordinates to character / pixel offset position |
|
190
|
0
|
|
|
|
|
0
|
my $chrX=$x/2;my $xOffset=$x- $chrX*2; |
|
|
0
|
|
|
|
|
0
|
|
|
191
|
0
|
|
|
|
|
0
|
my $chrY=$y/4;my $yOffset=$y- $chrY*4; |
|
|
0
|
|
|
|
|
0
|
|
|
192
|
0
|
|
|
|
|
0
|
my $orOp=ord($self->{setPix}-> [$xOffset]->[$yOffset]) & ord($self->{grid}->[$chrY]->[$chrX]); |
|
193
|
0
|
0
|
|
|
|
0
|
return $orOp == ord('⠀')?0:1; |
|
194
|
|
|
|
|
|
|
} |
|
195
|
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
=head3 C<$canvas-Eclear()> |
|
197
|
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
Re-initialises the canvas with blank braille characters |
|
199
|
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
=cut |
|
201
|
|
|
|
|
|
|
sub clear{ |
|
202
|
0
|
|
|
0
|
1
|
0
|
my ($self,$x1,$y1,$x2,$y2)=@_; |
|
203
|
0
|
0
|
|
|
|
0
|
if(@_<2){ |
|
204
|
0
|
0
|
|
|
|
0
|
$self->{grid}=[map {[( '⠀')x ($self->{width}/2+($self->{width}%2?1:0))]}(0..($self->{height}/4+($self->{height}%4?1:0)))] |
|
|
0
|
0
|
|
|
|
0
|
|
|
205
|
|
|
|
|
|
|
} |
|
206
|
|
|
|
|
|
|
else{ |
|
207
|
0
|
0
|
|
|
|
0
|
my @xr=$x1>$x2?($x2..$x1):($x1..$x2); |
|
208
|
0
|
0
|
|
|
|
0
|
my @yr=$y1>$y2?($y2..$y1):($y1..$y2); |
|
209
|
0
|
|
|
|
|
0
|
foreach my $y(@yr){ |
|
210
|
0
|
|
|
|
|
0
|
foreach my $x(@xr){ |
|
211
|
0
|
|
|
|
|
0
|
$self->{grid}->[$y]->[$x]='⠀'; |
|
212
|
|
|
|
|
|
|
} |
|
213
|
|
|
|
|
|
|
} |
|
214
|
|
|
|
|
|
|
} |
|
215
|
|
|
|
|
|
|
} |
|
216
|
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
# Pixel plotting primitives for shapes using Bresenham (Algorithm::Line::Bresenham) |
|
220
|
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
=head3 C<$canvas-Eline($x1,$y1,$x2,$y2,$value)> |
|
222
|
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
Uses Algorithm::Line::Bresenham to draw a line from C<$x1,$y1> to C<$x2,$y2>. |
|
224
|
|
|
|
|
|
|
The optional value C<$value> sets or unsets the pixels. If C<$value> is a |
|
225
|
|
|
|
|
|
|
valid colour (see below) the line will be drawn with that colour. |
|
226
|
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
=cut |
|
228
|
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
sub line{ |
|
230
|
0
|
0
|
|
0
|
1
|
0
|
push @_, 1 if @_ == 5; # final optional parameter is set to one if not given |
|
231
|
0
|
|
|
|
|
0
|
my ($self,$x1,$y1,$x2,$y2,$value)=@_; |
|
232
|
0
|
|
|
|
|
0
|
my $setRef=\&Term::Graille::BresenhamPlot; |
|
233
|
0
|
|
|
|
|
0
|
my @points=Algorithm::Line::Bresenham::line($x1,$y1,$x2,$y2,$setRef,[$self,$value]); |
|
234
|
|
|
|
|
|
|
} |
|
235
|
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
=head3 C<$canvas-Ecircle($x1,$y1,$radius,$value)> |
|
238
|
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
Uses Algorithm::Line::Bresenham to draw a circle centered at C<$x1,$y1> |
|
240
|
|
|
|
|
|
|
with radius C<$radius> to C<$x2,$y2>. |
|
241
|
|
|
|
|
|
|
The optional value C<$value> sets or unsets the pixels. If C<$value> is a |
|
242
|
|
|
|
|
|
|
valid colour (see below) the line will be drawn with that colour. |
|
243
|
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
=cut |
|
245
|
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
sub circle{ |
|
247
|
0
|
0
|
|
0
|
1
|
0
|
push @_, 1 if @_ == 4; |
|
248
|
0
|
|
|
|
|
0
|
my ($self,$x1,$y1,$radius,$value)=@_; |
|
249
|
0
|
|
|
|
|
0
|
my @points=Algorithm::Line::Bresenham::circle($x1,$y1,$radius); |
|
250
|
0
|
|
|
|
|
0
|
$self->set(@$_,$value) foreach (@points); |
|
251
|
|
|
|
|
|
|
} |
|
252
|
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
=head3 C<$canvas-Eellipse_rect($x1,$y1,$x2,$y2,$value)> |
|
255
|
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
Uses Algorithm::Line::Bresenham to draw a rectangular ellipse, (an |
|
257
|
|
|
|
|
|
|
ellipse bounded by a rectangle defined by C<$x1,$y1,$x2,$y2>). |
|
258
|
|
|
|
|
|
|
The optional value C<$value> sets or unsets the pixels. If C<$value> is a |
|
259
|
|
|
|
|
|
|
valid colour (see below) the line will be drawn with that colour. |
|
260
|
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
=cut |
|
262
|
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
sub ellipse_rect{ |
|
264
|
0
|
0
|
|
0
|
1
|
0
|
push @_, 1 if @_ == 5; |
|
265
|
0
|
|
|
|
|
0
|
my ($self,$x1,$y1,$x2,$y2,$value)=@_; |
|
266
|
0
|
|
|
|
|
0
|
my @points=Algorithm::Line::Bresenham::ellipse_rect($x1,$y1,$x2,$y2); |
|
267
|
0
|
|
|
|
|
0
|
$self->set(@$_,$value) foreach (@points); |
|
268
|
|
|
|
|
|
|
} |
|
269
|
|
|
|
|
|
|
|
|
270
|
|
|
|
|
|
|
=head3 C<$canvas-Equad_bezier($x1,$y1,$x2,$y2,$x3,$y3,$value)> |
|
271
|
|
|
|
|
|
|
|
|
272
|
|
|
|
|
|
|
Uses Algorithm::Line::Bresenham to draw a quadratic bezier, defined by |
|
273
|
|
|
|
|
|
|
end points C<$x1,$y1,$x3,$y3>) and control point C<$x2,$y2>. |
|
274
|
|
|
|
|
|
|
The optional value C<$value> sets or unsets the pixels. If C<$value> is a |
|
275
|
|
|
|
|
|
|
valid colour (see below) the line will be drawn with that colour. |
|
276
|
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
=cut |
|
278
|
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
|
|
280
|
|
|
|
|
|
|
sub quad_bezier{ |
|
281
|
0
|
0
|
|
0
|
1
|
0
|
push @_, 1 if @_ == 7; |
|
282
|
0
|
|
|
|
|
0
|
my ($self,$x1,$y1,$x2,$y2,$x3,$y3,$value)=@_; |
|
283
|
0
|
|
|
|
|
0
|
my @points=Algorithm::Line::Bresenham::quad_bezier($x1,$y1,$x2,$y2,$x3,$y3); |
|
284
|
0
|
|
|
|
|
0
|
$self->set(@$_,$value) foreach (@points); |
|
285
|
|
|
|
|
|
|
} |
|
286
|
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
|
|
288
|
|
|
|
|
|
|
=head3 C<$canvas-Ethick_line($x1,$y1,$x2,$y2,$thickness,$value)> |
|
289
|
|
|
|
|
|
|
|
|
290
|
|
|
|
|
|
|
Uses Algorithm::Line::Bresenham to draw a thick line, defined by |
|
291
|
|
|
|
|
|
|
end points C<$x1,$y1,$x2,$y2>) and thickness C<$thickness>. |
|
292
|
|
|
|
|
|
|
The optional value C<$value> sets or unsets the pixels. If C<$value> is a |
|
293
|
|
|
|
|
|
|
valid colour (see below) the line will be drawn with that colour. |
|
294
|
|
|
|
|
|
|
|
|
295
|
|
|
|
|
|
|
=cut |
|
296
|
|
|
|
|
|
|
|
|
297
|
|
|
|
|
|
|
|
|
298
|
|
|
|
|
|
|
sub thick_line{ |
|
299
|
0
|
0
|
|
0
|
1
|
0
|
push @_, 1 if @_ == 6; |
|
300
|
0
|
|
|
|
|
0
|
my ($self,$x0,$y0,$x1,$y1,$thickness,$value)=@_; |
|
301
|
0
|
|
|
|
|
0
|
my @points=Algorithm::Line::Bresenham::thick_line($x0,$y0,$x1,$y1,$thickness); |
|
302
|
0
|
|
|
|
|
0
|
$self->set(@$_,$value) foreach (@points); |
|
303
|
|
|
|
|
|
|
} |
|
304
|
|
|
|
|
|
|
|
|
305
|
|
|
|
|
|
|
|
|
306
|
|
|
|
|
|
|
=head3 C<$canvas-Evarthick_line($x0,$y0,$x1,$y1, |
|
307
|
|
|
|
|
|
|
$left,$argL, |
|
308
|
|
|
|
|
|
|
$right,$argR, $value)=@_; |
|
309
|
|
|
|
|
|
|
|
|
310
|
|
|
|
|
|
|
Uses Algorithm::Line::Bresenham to draw a variable thickness, defined by |
|
311
|
|
|
|
|
|
|
end points C<$x0,$y0,$x1,$y1>) and thickness defined by two user defined |
|
312
|
|
|
|
|
|
|
functions, each function taking as arguments CL|RE, $pos, $len> |
|
313
|
|
|
|
|
|
|
and returning thickness of the left and right sides |
|
314
|
|
|
|
|
|
|
of the line. The optional value C<$value> sets or unsets the pixels. |
|
315
|
|
|
|
|
|
|
If C<$value> is a valid colour (see below) the line will be drawn with that colour. |
|
316
|
|
|
|
|
|
|
|
|
317
|
|
|
|
|
|
|
=cut |
|
318
|
|
|
|
|
|
|
|
|
319
|
|
|
|
|
|
|
|
|
320
|
|
|
|
|
|
|
sub varthick_line{ |
|
321
|
0
|
0
|
|
0
|
1
|
0
|
push @_, 1 if @_ == 9; |
|
322
|
0
|
|
|
|
|
0
|
my ($self,$x0,$y0,$x1,$y1,$left,$argL,$right,$argR,$value)=@_; |
|
323
|
0
|
|
|
|
|
0
|
my @points=Algorithm::Line::Bresenham::varthick_line($x0,$y0,$x1,$y1,$left,$argL,$right,$argR); |
|
324
|
0
|
|
|
|
|
0
|
$self->set(@$_,$value) foreach (@points); |
|
325
|
|
|
|
|
|
|
|
|
326
|
|
|
|
|
|
|
} |
|
327
|
|
|
|
|
|
|
|
|
328
|
|
|
|
|
|
|
|
|
329
|
|
|
|
|
|
|
|
|
330
|
|
|
|
|
|
|
=head3 C<$canvas-Epolyline($x1,$y1,....,$xn,$yn,$value)> |
|
331
|
|
|
|
|
|
|
|
|
332
|
|
|
|
|
|
|
Uses Algorithm::Line::Bresenham to draw a poly line, form a |
|
333
|
|
|
|
|
|
|
sequences of points. |
|
334
|
|
|
|
|
|
|
The optional value C<$value> sets or unsets the pixels. If C<$value> is a |
|
335
|
|
|
|
|
|
|
valid colour (see below) the line will be drawn with that colour. |
|
336
|
|
|
|
|
|
|
|
|
337
|
|
|
|
|
|
|
=cut |
|
338
|
|
|
|
|
|
|
|
|
339
|
|
|
|
|
|
|
|
|
340
|
|
|
|
|
|
|
sub polyline{ |
|
341
|
0
|
|
|
0
|
1
|
0
|
my $self=shift; |
|
342
|
0
|
|
|
|
|
0
|
my @vertices=@_; |
|
343
|
0
|
0
|
|
|
|
0
|
my $value= pop @vertices if (scalar @vertices & 1); |
|
344
|
0
|
|
0
|
|
|
0
|
$value//=1; |
|
345
|
0
|
|
|
|
|
0
|
my @points=Algorithm::Line::Bresenham::polyline(@vertices); |
|
346
|
0
|
|
|
|
|
0
|
$self->set(@$_,$value) foreach (@points); |
|
347
|
|
|
|
|
|
|
} |
|
348
|
|
|
|
|
|
|
|
|
349
|
|
|
|
|
|
|
sub degToRad{ |
|
350
|
0
|
0
|
|
0
|
0
|
0
|
return $_[0]?3.14159267*$_[0]/180:0; ; |
|
351
|
|
|
|
|
|
|
} |
|
352
|
|
|
|
|
|
|
|
|
353
|
|
|
|
|
|
|
=head2 Character Level Functions; |
|
354
|
|
|
|
|
|
|
|
|
355
|
|
|
|
|
|
|
=head3 C<$canvas-Escroll($direction,$wrap)> |
|
356
|
|
|
|
|
|
|
|
|
357
|
|
|
|
|
|
|
Scrolls in C<$direction>. $direction may be |
|
358
|
|
|
|
|
|
|
"l", "left", "r","right", "u","up","d", "down". |
|
359
|
|
|
|
|
|
|
Beacuse of the use of Braille characters, up/down scrolling is 4 pixels |
|
360
|
|
|
|
|
|
|
at a time, whereas left right scrolling is 2 pixels at a time. If |
|
361
|
|
|
|
|
|
|
C<$wrap> is a true value, the screen wraps around. |
|
362
|
|
|
|
|
|
|
|
|
363
|
|
|
|
|
|
|
=cut |
|
364
|
|
|
|
|
|
|
|
|
365
|
|
|
|
|
|
|
sub scroll{ |
|
366
|
0
|
|
|
0
|
1
|
0
|
my ($self,$direction,$wrap,$numberOfChar)=@_; |
|
367
|
0
|
|
0
|
|
|
0
|
$numberOfChar//=1;$wrap//=0; |
|
|
0
|
|
0
|
|
|
0
|
|
|
368
|
0
|
|
|
|
|
0
|
for($direction){ |
|
369
|
0
|
0
|
|
|
|
0
|
/^r/i && do{ |
|
370
|
0
|
|
|
|
|
0
|
foreach my $row (0..$#{$self->{grid}}){ |
|
|
0
|
|
|
|
|
0
|
|
|
371
|
0
|
|
|
|
|
0
|
my @r=@{$self->{grid}->[$row]}; |
|
|
0
|
|
|
|
|
0
|
|
|
372
|
0
|
|
|
|
|
0
|
my @end=splice @r, -$numberOfChar, $numberOfChar; |
|
373
|
0
|
0
|
|
|
|
0
|
@end=('⠀')x$numberOfChar unless $wrap; |
|
374
|
|
|
|
|
|
|
# unshift(@r,$wrap?@end:(('⠀')x$numberOfChar)); |
|
375
|
0
|
|
|
|
|
0
|
$self->{grid}->[$row]=[@end,@r]; |
|
376
|
|
|
|
|
|
|
} |
|
377
|
0
|
|
|
|
|
0
|
last; |
|
378
|
|
|
|
|
|
|
}; |
|
379
|
|
|
|
|
|
|
|
|
380
|
0
|
0
|
|
|
|
0
|
/^l/i && do{ |
|
381
|
0
|
|
|
|
|
0
|
foreach my $row (0..$#{$self->{grid}}){ |
|
|
0
|
|
|
|
|
0
|
|
|
382
|
0
|
|
|
|
|
0
|
my @r=@{$self->{grid}->[$row]}; |
|
|
0
|
|
|
|
|
0
|
|
|
383
|
0
|
|
|
|
|
0
|
my @end=splice @r, 0, $numberOfChar; |
|
384
|
0
|
0
|
|
|
|
0
|
@end=('⠀')x$numberOfChar unless $wrap; |
|
385
|
|
|
|
|
|
|
# push(@r,$wrap?@end:(('⠀')x$numberOfChar)); |
|
386
|
0
|
|
|
|
|
0
|
$self->{grid}->[$row]=[@r,@end]; |
|
387
|
|
|
|
|
|
|
} |
|
388
|
0
|
|
|
|
|
0
|
last; |
|
389
|
|
|
|
|
|
|
}; |
|
390
|
|
|
|
|
|
|
|
|
391
|
0
|
0
|
|
|
|
0
|
/^d/i && do{ |
|
392
|
0
|
|
|
|
|
0
|
my @r=@{$self->{grid}}; |
|
|
0
|
|
|
|
|
0
|
|
|
393
|
0
|
|
|
|
|
0
|
my @end=splice @r, 0, $numberOfChar; |
|
394
|
0
|
0
|
|
|
|
0
|
@end=([('⠀')x ($self->{width}/2+($self->{width}%2?1:0))])x$numberOfChar unless $wrap; |
|
|
|
0
|
|
|
|
|
|
|
395
|
|
|
|
|
|
|
# push(@r,$wrap?@end:(([('⠀')x ($self->{width}/2+($self->{width}%2?1:0))])x$numberOfChar)); |
|
396
|
0
|
|
|
|
|
0
|
$self->{grid}=[@r,@end]; |
|
397
|
0
|
|
|
|
|
0
|
last; |
|
398
|
|
|
|
|
|
|
}; |
|
399
|
|
|
|
|
|
|
|
|
400
|
0
|
0
|
|
|
|
0
|
/^u/i && do{ |
|
401
|
0
|
|
|
|
|
0
|
my @r=@{$self->{grid}}; |
|
|
0
|
|
|
|
|
0
|
|
|
402
|
0
|
|
|
|
|
0
|
my @end=splice @r, -$numberOfChar, $numberOfChar; |
|
403
|
0
|
0
|
|
|
|
0
|
@end=([('⠀')x ($self->{width}/2+($self->{width}%2?1:0))])x$numberOfChar unless $wrap; |
|
|
|
0
|
|
|
|
|
|
|
404
|
|
|
|
|
|
|
# unshift(@r,$wrap?@end:(([('⠀')x ($self->{width}/2+($self->{width}%2?1:0))])x$numberOfChar)); |
|
405
|
0
|
|
|
|
|
0
|
$self->{grid}=[@end,@r]; |
|
406
|
0
|
|
|
|
|
0
|
last; |
|
407
|
|
|
|
|
|
|
}; |
|
408
|
|
|
|
|
|
|
} |
|
409
|
|
|
|
|
|
|
} |
|
410
|
|
|
|
|
|
|
|
|
411
|
|
|
|
|
|
|
|
|
412
|
|
|
|
|
|
|
=head3 C<$canvas-EblockBlit($block,$gridX, $gridY)> |
|
413
|
|
|
|
|
|
|
|
|
414
|
|
|
|
|
|
|
Allows blitting a 2d arrays to a grid location in the canvas. Useful for |
|
415
|
|
|
|
|
|
|
printing using a Graille font. |
|
416
|
|
|
|
|
|
|
|
|
417
|
|
|
|
|
|
|
=cut |
|
418
|
|
|
|
|
|
|
|
|
419
|
|
|
|
|
|
|
sub blockBlit{ # needs protection |
|
420
|
0
|
|
|
0
|
1
|
0
|
my ($self, $blk, $gridX, $gridY)=@_; |
|
421
|
0
|
|
|
|
|
0
|
for my $x(0..$#{$blk->[0]}){ |
|
|
0
|
|
|
|
|
0
|
|
|
422
|
0
|
|
|
|
|
0
|
for my $y(0..$#$blk){ |
|
423
|
0
|
|
|
|
|
0
|
$self->{grid}->[$gridY+$y]->[$gridX+$x]=$blk->[$#$blk-$y]->[$x] |
|
424
|
|
|
|
|
|
|
} |
|
425
|
|
|
|
|
|
|
} |
|
426
|
|
|
|
|
|
|
} |
|
427
|
|
|
|
|
|
|
|
|
428
|
|
|
|
|
|
|
|
|
429
|
|
|
|
|
|
|
=head3 C<$canvas-EexportCanvas($filename)> , C<$canvas-EimportCanvas($filename,[$toBuffer])> |
|
430
|
|
|
|
|
|
|
|
|
431
|
|
|
|
|
|
|
This allows the loading and unloading of a canvas from a file. There is |
|
432
|
|
|
|
|
|
|
no checking of the dimension of the canvas being imported at the moment. |
|
433
|
|
|
|
|
|
|
Import can be direct to the canvas, the function return the loaded data |
|
434
|
|
|
|
|
|
|
as an ArrayRef, the optional c<$toBuffer> parameter, if true prevents |
|
435
|
|
|
|
|
|
|
loading the data onto the canvas. |
|
436
|
|
|
|
|
|
|
|
|
437
|
|
|
|
|
|
|
=cut |
|
438
|
|
|
|
|
|
|
|
|
439
|
|
|
|
|
|
|
|
|
440
|
|
|
|
|
|
|
sub exportCanvas{ |
|
441
|
0
|
|
|
0
|
1
|
0
|
my ($self,$file)=@_; |
|
442
|
0
|
0
|
|
|
|
0
|
open (my $fh, ">$file") or die "can not open $file for writing $!"; |
|
443
|
0
|
|
|
|
|
0
|
binmode($fh, ":utf8"); |
|
444
|
0
|
|
|
|
|
0
|
print $fh $self->as_string(); |
|
445
|
0
|
|
|
|
|
0
|
close $fh; |
|
446
|
|
|
|
|
|
|
|
|
447
|
|
|
|
|
|
|
} |
|
448
|
|
|
|
|
|
|
|
|
449
|
|
|
|
|
|
|
sub importCanvas{ |
|
450
|
0
|
|
|
0
|
1
|
0
|
my ($self,$file,$toBuffer)=@_; |
|
451
|
0
|
0
|
|
|
|
0
|
open (my $fh,'<', $file) or die "can not open $file for reading $!"; |
|
452
|
0
|
|
|
|
|
0
|
my @grd; |
|
453
|
0
|
|
|
|
|
0
|
while (<$fh>){ |
|
454
|
0
|
0
|
|
|
|
0
|
last if (@grd > ($self->{height}/4)); # stop if too big for canvas |
|
455
|
|
|
|
|
|
|
# extend if too narrow for canvas, trucate if too wide |
|
456
|
0
|
|
|
|
|
0
|
unshift @grd, [split(//,substr($_.('⠀')x($self->{width}/2),0,$self->{width}/2))]; |
|
457
|
|
|
|
|
|
|
} |
|
458
|
0
|
|
|
|
|
0
|
close $fh; |
|
459
|
0
|
0
|
|
|
|
0
|
$self->{grid}=[@grd] unless $toBuffer; |
|
460
|
0
|
|
|
|
|
0
|
return [@grd]; |
|
461
|
|
|
|
|
|
|
} |
|
462
|
|
|
|
|
|
|
|
|
463
|
|
|
|
|
|
|
=head3 C<$canvas-EtextAt($x,$y,$text,$fmt)> |
|
464
|
|
|
|
|
|
|
|
|
465
|
|
|
|
|
|
|
Printing text on the C<$canvas>. This is different fromthe exported |
|
466
|
|
|
|
|
|
|
C function. the characters are printed on the C<$canvas> |
|
467
|
|
|
|
|
|
|
and may be scrolled with the canvas and will overwrite or be over written |
|
468
|
|
|
|
|
|
|
othe $canvas drawing actions. The optional C<$fmt> allows the setting of colour; |
|
469
|
|
|
|
|
|
|
|
|
470
|
|
|
|
|
|
|
=cut |
|
471
|
|
|
|
|
|
|
|
|
472
|
|
|
|
|
|
|
|
|
473
|
|
|
|
|
|
|
sub textAt{ |
|
474
|
0
|
|
|
0
|
1
|
0
|
my ($self,$x,$y,$text,$fmt)=@_; |
|
475
|
0
|
0
|
|
|
|
0
|
return unless defined $text; |
|
476
|
0
|
|
|
|
|
0
|
my ($chrX,$chrY,$xOffset,$yOffset)=charOffset($self,$x,$y); |
|
477
|
0
|
0
|
|
|
|
0
|
if ($chrX!=-1){ |
|
478
|
0
|
|
|
|
|
0
|
my @chrs=split(//,$text); |
|
479
|
0
|
|
|
|
|
0
|
my $lastChar=$chrX+(length $text)-1; |
|
480
|
0
|
0
|
|
|
|
0
|
$lastChar = $self->{width}/2 if ($lastChar>$self->{width}/2); |
|
481
|
0
|
0
|
|
|
|
0
|
$chrs[0]=colour($fmt).$chrs[0] if $fmt; |
|
482
|
0
|
|
|
|
|
0
|
for my $tc ($chrX..$lastChar){ |
|
483
|
0
|
|
|
|
|
0
|
$self->{grid}->[$chrY]->[$tc]=shift @chrs; |
|
484
|
|
|
|
|
|
|
} |
|
485
|
|
|
|
|
|
|
|
|
486
|
|
|
|
|
|
|
} |
|
487
|
|
|
|
|
|
|
} |
|
488
|
|
|
|
|
|
|
|
|
489
|
|
|
|
|
|
|
sub resetUnpainted{ |
|
490
|
0
|
|
|
0
|
0
|
0
|
my ($self,$chX,$chY)=@_; |
|
491
|
0
|
0
|
0
|
|
|
0
|
return if (($chX>$self->{width}/2)||($chX<0)||($chY>$self->{height}/4)||($chY<0)); |
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
492
|
0
|
0
|
|
|
|
0
|
$self->{grid}->[$chY]->[$chX]=colour("reset").$self->{grid}->[$chY]->[$chX] if (length $self->{grid}->[$chY]->[$chX] ==1); |
|
493
|
|
|
|
|
|
|
} |
|
494
|
|
|
|
|
|
|
|
|
495
|
|
|
|
|
|
|
sub stripColour{ |
|
496
|
0
|
|
|
0
|
0
|
0
|
my $ch=shift; |
|
497
|
0
|
|
|
|
|
0
|
return chop $ch; |
|
498
|
|
|
|
|
|
|
} |
|
499
|
|
|
|
|
|
|
|
|
500
|
|
|
|
|
|
|
sub axis{ |
|
501
|
0
|
|
|
0
|
0
|
0
|
my ($self,$xOrigin,$yOrigin,$xPos,$xNeg,$yPos,$yNeg,$xTics,$yTics)=@_; |
|
502
|
0
|
|
|
|
|
0
|
for my $y ($yOrigin-$yNeg..$yOrigin+$yPos) { |
|
503
|
0
|
|
|
|
|
0
|
$self->textAt($xOrigin,$y,'│'); |
|
504
|
|
|
|
|
|
|
} |
|
505
|
0
|
|
|
|
|
0
|
$self->textAt($xOrigin-$xNeg,$yOrigin,'─' x (($xNeg+$xPos)/2)); |
|
506
|
0
|
|
|
|
|
0
|
$self->textAt($xOrigin,$yOrigin,'┼' ); |
|
507
|
|
|
|
|
|
|
|
|
508
|
|
|
|
|
|
|
} |
|
509
|
|
|
|
|
|
|
|
|
510
|
|
|
|
|
|
|
|
|
511
|
|
|
|
|
|
|
sub axis2{ |
|
512
|
0
|
|
|
0
|
0
|
0
|
my ($self,$xOrigin,$yOrigin,$xPos,$xNeg,$yPos,$yNeg,$xTics,$yTics)=@_; |
|
513
|
0
|
|
|
|
|
0
|
$self->line($xOrigin-$xNeg,$yOrigin,$xOrigin+$xPos,$yOrigin); |
|
514
|
0
|
|
|
|
|
0
|
$self->line($xOrigin,$yOrigin-$yNeg,$xOrigin,$yOrigin+$yPos); |
|
515
|
|
|
|
|
|
|
} |
|
516
|
|
|
|
|
|
|
|
|
517
|
|
|
|
|
|
|
|
|
518
|
|
|
|
|
|
|
=head2 Enhancements |
|
519
|
|
|
|
|
|
|
|
|
520
|
|
|
|
|
|
|
=head3 C<$canvas-Elogo($script)> |
|
521
|
|
|
|
|
|
|
|
|
522
|
|
|
|
|
|
|
Interface to Graille's built in Turtle interpreter. |
|
523
|
|
|
|
|
|
|
A string is taken and split by senicolons or newlines into instructions. |
|
524
|
|
|
|
|
|
|
The instructions are trimmed, and split by the first space character into |
|
525
|
|
|
|
|
|
|
command and parameters. Very simple in other words. No syntax checking |
|
526
|
|
|
|
|
|
|
is done |
|
527
|
|
|
|
|
|
|
|
|
528
|
|
|
|
|
|
|
C<"fd distance"> pen moves forward a certain distance. |
|
529
|
|
|
|
|
|
|
C<"lt angle">, C<"rt angle"> turns left or right. |
|
530
|
|
|
|
|
|
|
C<"bk distance"> pen moves back a certain distance. |
|
531
|
|
|
|
|
|
|
C<"pu">, C<"pd"> Pen is up or down, up means no drawing takes place, |
|
532
|
|
|
|
|
|
|
and down means the turtle draws as it moves. |
|
533
|
|
|
|
|
|
|
C<"dir"> set the direction at a specific angle in dgrees, |
|
534
|
|
|
|
|
|
|
with 0 being directly left. |
|
535
|
|
|
|
|
|
|
C<"mv">moves pen to specific coordinates without drawing. |
|
536
|
|
|
|
|
|
|
C<"ce"> centers the turtle in the middle of a canvas |
|
537
|
|
|
|
|
|
|
C<"sp"> allows animated drawing by specifiying the the number |
|
538
|
|
|
|
|
|
|
of centiseconds between instructions |
|
539
|
|
|
|
|
|
|
|
|
540
|
|
|
|
|
|
|
=cut |
|
541
|
|
|
|
|
|
|
|
|
542
|
|
|
|
|
|
|
sub logo{ |
|
543
|
0
|
|
|
0
|
1
|
0
|
my ($self,$script)=@_; |
|
544
|
0
|
|
|
|
|
0
|
my @commands= map{s/^\s+|\s+$//g; $_}split(/[\n;]+/,$script); |
|
|
0
|
|
|
|
|
0
|
|
|
|
0
|
|
|
|
|
0
|
|
|
545
|
0
|
|
|
|
|
0
|
foreach my $instr (@commands){ |
|
546
|
0
|
0
|
|
|
|
0
|
next unless $instr; |
|
547
|
0
|
|
|
|
|
0
|
$instr=~s/#\.*$//; |
|
548
|
0
|
0
|
|
|
|
0
|
next if ($instr=~/#/); |
|
549
|
0
|
|
|
|
|
0
|
my ($c,$p)=split(/[\s]+/,$instr,2); |
|
550
|
0
|
0
|
|
|
|
0
|
my @pars=split(/,/,$p) if $p; |
|
551
|
0
|
|
|
|
|
0
|
for ($c){ |
|
552
|
0
|
0
|
|
|
|
0
|
/^(fd|forward)/ && do{ |
|
553
|
0
|
0
|
0
|
|
|
0
|
last unless ($pars[0] && (0+$pars[0])); |
|
554
|
0
|
|
|
|
|
0
|
my $x=$self->{logoVars}->{x}+$pars[0]*cos(degToRad($self->{logoVars}->{d})); |
|
555
|
0
|
|
|
|
|
0
|
my $y=$self->{logoVars}->{y}+$pars[0]*sin(degToRad($self->{logoVars}->{d})); |
|
556
|
0
|
0
|
|
|
|
0
|
if ($self->{logoVars}->{p}){ |
|
557
|
0
|
|
0
|
|
|
0
|
$self->line($self->{logoVars}->{x},$self->{logoVars}->{y},$x,$y,($self->{logoVars}->{c}//1)) |
|
558
|
|
|
|
|
|
|
} |
|
559
|
0
|
|
|
|
|
0
|
$self->{logoVars}->{y}=$y; |
|
560
|
0
|
|
|
|
|
0
|
$self->{logoVars}->{x}=$x; |
|
561
|
0
|
|
|
|
|
0
|
last; |
|
562
|
|
|
|
|
|
|
}; |
|
563
|
0
|
0
|
|
|
|
0
|
/^(lt|left)/ && do{ |
|
564
|
0
|
0
|
0
|
|
|
0
|
last unless ($pars[0] && (0+$pars[0])); |
|
565
|
0
|
|
|
|
|
0
|
$self->{logoVars}->{d}+=$pars[0]; |
|
566
|
0
|
|
|
|
|
0
|
$self->{logoVars}->{d}-=360 while($self->{logoVars}->{d}>360); |
|
567
|
0
|
|
|
|
|
0
|
last; |
|
568
|
|
|
|
|
|
|
}; |
|
569
|
0
|
0
|
|
|
|
0
|
/^(rt|right)/ && do{ |
|
570
|
0
|
0
|
0
|
|
|
0
|
last unless ($pars[0] && ($pars[0]=~/^\d+$/)); |
|
571
|
0
|
|
|
|
|
0
|
$self->{logoVars}->{d}-=$pars[0]; |
|
572
|
0
|
|
|
|
|
0
|
$self->{logoVars}->{d}+=360 while($self->{logoVars}->{d}<360); |
|
573
|
0
|
|
|
|
|
0
|
last; |
|
574
|
|
|
|
|
|
|
}; |
|
575
|
0
|
0
|
|
|
|
0
|
/^(bk|back)/ && do{ |
|
576
|
0
|
|
|
|
|
0
|
$pars[0]=-$pars[0]; |
|
577
|
0
|
|
|
|
|
0
|
$_="fd"; |
|
578
|
0
|
|
|
|
|
0
|
redo; |
|
579
|
|
|
|
|
|
|
}; |
|
580
|
0
|
0
|
|
|
|
0
|
/^pu/ && do{ |
|
581
|
0
|
|
|
|
|
0
|
$self->{logoVars}->{p}=0; |
|
582
|
0
|
|
|
|
|
0
|
last; |
|
583
|
|
|
|
|
|
|
}; |
|
584
|
0
|
0
|
|
|
|
0
|
/^pd/ && do{ |
|
585
|
0
|
|
|
|
|
0
|
$self->{logoVars}->{p}=1; |
|
586
|
0
|
|
|
|
|
0
|
last; |
|
587
|
|
|
|
|
|
|
}; |
|
588
|
0
|
0
|
|
|
|
0
|
/^pc/ && do{ |
|
589
|
0
|
|
|
|
|
0
|
$self->{logoVars}->{c}=colour($pars[0]); |
|
590
|
0
|
|
|
|
|
0
|
last; |
|
591
|
|
|
|
|
|
|
}; |
|
592
|
0
|
0
|
|
|
|
0
|
/^dir/ && do{ |
|
593
|
0
|
|
|
|
|
0
|
$self->{logoVars}->{d}=$pars[0]; |
|
594
|
0
|
|
|
|
|
0
|
last; |
|
595
|
|
|
|
|
|
|
}; |
|
596
|
0
|
0
|
|
|
|
0
|
/^mv/ && do{ |
|
597
|
0
|
|
|
|
|
0
|
$self->{logoVars}->{x}=$pars[0]; |
|
598
|
0
|
|
|
|
|
0
|
$self->{logoVars}->{y}=$pars[1 ]; |
|
599
|
0
|
|
|
|
|
0
|
last; |
|
600
|
|
|
|
|
|
|
}; |
|
601
|
0
|
0
|
|
|
|
0
|
/^ce/ && do{ |
|
602
|
0
|
|
|
|
|
0
|
$self->{logoVars}->{x}=$self->{width}/2; |
|
603
|
0
|
|
|
|
|
0
|
$self->{logoVars}->{y}=$self->{height}/2; |
|
604
|
0
|
|
|
|
|
0
|
last; |
|
605
|
|
|
|
|
|
|
}; |
|
606
|
0
|
0
|
|
|
|
0
|
/^cs/ && do{ |
|
607
|
0
|
|
|
|
|
0
|
$self->clear(); |
|
608
|
0
|
|
|
|
|
0
|
last; |
|
609
|
|
|
|
|
|
|
}; |
|
610
|
0
|
0
|
|
|
|
0
|
/^sp/ && do{ |
|
611
|
0
|
|
|
|
|
0
|
$self->{logoVars}->{sp}=$pars[0]; |
|
612
|
|
|
|
|
|
|
last |
|
613
|
0
|
|
|
|
|
0
|
} |
|
614
|
|
|
|
|
|
|
|
|
615
|
|
|
|
|
|
|
|
|
616
|
|
|
|
|
|
|
} |
|
617
|
0
|
0
|
|
|
|
0
|
if (defined $self->{logoVars}->{sp}){ |
|
618
|
0
|
|
|
|
|
0
|
sleep $self->{logoVars}->{sp}/100; |
|
619
|
0
|
|
|
|
|
0
|
$self->draw(); |
|
620
|
|
|
|
|
|
|
} |
|
621
|
|
|
|
|
|
|
} |
|
622
|
|
|
|
|
|
|
} |
|
623
|
|
|
|
|
|
|
|
|
624
|
|
|
|
|
|
|
|
|
625
|
|
|
|
|
|
|
=head3 Exported Routines |
|
626
|
|
|
|
|
|
|
|
|
627
|
|
|
|
|
|
|
Graille exports some functions for additional console graphical manipulation |
|
628
|
|
|
|
|
|
|
This includes drawing of borders, printing characters at specific locations |
|
629
|
|
|
|
|
|
|
in the terminal window, and colouring the characters, clearing the screen, etc. |
|
630
|
|
|
|
|
|
|
|
|
631
|
|
|
|
|
|
|
printAt($row,$column,@textRows); |
|
632
|
|
|
|
|
|
|
|
|
633
|
|
|
|
|
|
|
Prints text sent as a scalar, a list of scalars or a reference to list |
|
634
|
|
|
|
|
|
|
of scalars, at a specific location on the screen. Lists are printed |
|
635
|
|
|
|
|
|
|
from the same column but increasing row positions. |
|
636
|
|
|
|
|
|
|
|
|
637
|
|
|
|
|
|
|
border($top,$left,$bottom,$right,$style,$colour); |
|
638
|
|
|
|
|
|
|
|
|
639
|
|
|
|
|
|
|
Draws a border box. |
|
640
|
|
|
|
|
|
|
|
|
641
|
|
|
|
|
|
|
paint($txt,$fmt) |
|
642
|
|
|
|
|
|
|
|
|
643
|
|
|
|
|
|
|
Paints text the colour and background specified, $text may be a string, or ref |
|
644
|
|
|
|
|
|
|
to a list of strings. This is combined with C abouve |
|
645
|
|
|
|
|
|
|
|
|
646
|
|
|
|
|
|
|
clearScreen() |
|
647
|
|
|
|
|
|
|
|
|
648
|
|
|
|
|
|
|
Guess what? clears the enire screen. This is different from C<$canvas-Eclear()> |
|
649
|
|
|
|
|
|
|
which clears the Graille canvas. |
|
650
|
|
|
|
|
|
|
|
|
651
|
|
|
|
|
|
|
|
|
652
|
|
|
|
|
|
|
block2braille($block) |
|
653
|
|
|
|
|
|
|
|
|
654
|
|
|
|
|
|
|
Given a block of binary data (a 2D Array ref of 8-bit data), return a |
|
655
|
|
|
|
|
|
|
corresponding 2d Array ref of braille blocks. This is handy to convert, |
|
656
|
|
|
|
|
|
|
say, binary font data tinto Braille blocks for blittting into the canvas; |
|
657
|
|
|
|
|
|
|
|
|
658
|
|
|
|
|
|
|
pixelAt($block,$px,$py) |
|
659
|
|
|
|
|
|
|
|
|
660
|
|
|
|
|
|
|
Given a binary block of data e.g. a font or a sprite offered as a 2D Array ref |
|
661
|
|
|
|
|
|
|
find the pixel value at a certain coordinate in that block. |
|
662
|
|
|
|
|
|
|
|
|
663
|
|
|
|
|
|
|
|
|
664
|
|
|
|
|
|
|
=cut |
|
665
|
|
|
|
|
|
|
|
|
666
|
|
|
|
|
|
|
|
|
667
|
|
|
|
|
|
|
our %borders=( |
|
668
|
|
|
|
|
|
|
simple=>{tl=>"+", t=>"-", tr=>"+", l=>"|", r=>"|", bl=>"+", b=>"-", br=>"+",ts=>"|",te=>"|",}, |
|
669
|
|
|
|
|
|
|
double=>{tl=>"╔", t=>"═", tr=>"╗", l=>"║", r=>"║", bl=>"╚", b=>"═", br=>"╝",ts=>"╣",te=>"╠",}, |
|
670
|
|
|
|
|
|
|
shadow=>{tl=>"┌", t=>"─", tr=>"╖", l=>"│", r=>"║", bl=>"╘", b=>"═", br=>"╝",ts=>"┨",te=>"┠",}, |
|
671
|
|
|
|
|
|
|
thin =>{tl=>"┌", t=>"─", tr=>"┐", l=>"│", r=>"│", bl=>"└", b=>"─", br=>"┘",ts=>"┤",te=>"├",}, |
|
672
|
|
|
|
|
|
|
thick =>{tl=>"┏", t=>"━", tr=>"┓", l=>"┃", r=>"┃", bl=>"┗", b=>"━", br=>"┛",ts=>"┫",te=>"┣",}, |
|
673
|
|
|
|
|
|
|
); |
|
674
|
|
|
|
|
|
|
|
|
675
|
|
|
|
|
|
|
our %colours=(black =>30,red =>31,green =>32,yellow =>33,blue =>34,magenta =>35,cyan =>36,white =>37, |
|
676
|
|
|
|
|
|
|
on_black=>40,on_red=>41,on_green=>42,on_yellow=>43,on_blue=>44,on_magenta=>4,on_cyan=>46,on_white=>47, |
|
677
|
|
|
|
|
|
|
reset=>0, bold=>1, italic=>3, underline=>4, blink=>5, strikethrough=>9, invert=>7,); |
|
678
|
|
|
|
|
|
|
|
|
679
|
|
|
|
|
|
|
sub printAt{ |
|
680
|
0
|
|
|
0
|
0
|
0
|
my ($row,$column,@textRows)=@_; |
|
681
|
0
|
0
|
|
|
|
0
|
@textRows = @{$textRows[0]} if ref $textRows[0]; |
|
|
0
|
|
|
|
|
0
|
|
|
682
|
0
|
|
|
|
|
0
|
my $blit="\033[?25l"; |
|
683
|
0
|
0
|
|
|
|
0
|
$blit.= defined $_?("\033[".$row++.";".$column."H".(ref $_?join("",@$_):$_)):"" foreach (@textRows) ; |
|
|
|
0
|
|
|
|
|
|
|
684
|
0
|
|
|
|
|
0
|
print $blit; |
|
685
|
0
|
|
|
|
|
0
|
print "\n"; # seems to flush the STDOUT buffer...if not then set $| to 1 |
|
686
|
|
|
|
|
|
|
}; |
|
687
|
|
|
|
|
|
|
|
|
688
|
|
|
|
|
|
|
sub cursorAt{ |
|
689
|
0
|
|
|
0
|
0
|
0
|
my ($r,$c)=@_; |
|
690
|
0
|
|
|
|
|
0
|
return "\033[?25l\033[".$r.";".$c."H"; |
|
691
|
|
|
|
|
|
|
} |
|
692
|
|
|
|
|
|
|
|
|
693
|
|
|
|
|
|
|
sub border{ |
|
694
|
0
|
|
|
0
|
0
|
0
|
my ($top,$left,$bottom,$right,$style,$colour,$title,$titleColour)=@_; |
|
695
|
0
|
|
0
|
|
|
0
|
$style//="simple"; |
|
696
|
0
|
0
|
|
|
|
0
|
return unless exists $borders{$style}; |
|
697
|
0
|
|
|
|
|
0
|
my @box=(colour($colour).$borders{$style}{tl}.($borders{$style}{t}x($right-$left)).$borders{$style}{tr}); |
|
698
|
0
|
0
|
|
|
|
0
|
if ($title){ |
|
699
|
0
|
|
|
|
|
0
|
my $titleSize=4+length $title; |
|
700
|
0
|
|
0
|
|
|
0
|
$title=$borders{$style}{ts}.colour($titleColour||"reset")." ".$title.colour($colour)." ".$borders{$style}{te}; |
|
701
|
0
|
|
|
|
|
0
|
substr ($box[0],6,$titleSize)=$title; |
|
702
|
|
|
|
|
|
|
}; |
|
703
|
0
|
|
|
|
|
0
|
push @box,($borders{$style}{l}.(" "x($right-$left)).$borders{$style}{r})x($bottom-$top);; |
|
704
|
0
|
|
|
|
|
0
|
push @box,($borders{$style}{bl}.($borders{$style}{b}x($right-$left)).$borders{$style}{br}.colour("reset")); |
|
705
|
0
|
|
|
|
|
0
|
printAt($top,$left,\@box); |
|
706
|
|
|
|
|
|
|
|
|
707
|
|
|
|
|
|
|
} |
|
708
|
|
|
|
|
|
|
|
|
709
|
|
|
|
|
|
|
sub paint{ |
|
710
|
0
|
|
|
0
|
0
|
0
|
my ($txt,$fmt)=@_; |
|
711
|
0
|
0
|
|
|
|
0
|
return $txt unless $fmt; |
|
712
|
0
|
0
|
|
|
|
0
|
return colour($fmt).$txt.colour("reset") unless ref $txt; |
|
713
|
0
|
|
|
|
|
0
|
return [map {colour($fmt).$_.colour("reset");} @$txt] |
|
|
0
|
|
|
|
|
0
|
|
|
714
|
|
|
|
|
|
|
} |
|
715
|
|
|
|
|
|
|
|
|
716
|
|
|
|
|
|
|
sub clearScreen{ |
|
717
|
0
|
0
|
|
0
|
0
|
0
|
system($^O eq 'MSWin32'?'cls':'clear'); |
|
718
|
|
|
|
|
|
|
} |
|
719
|
|
|
|
|
|
|
|
|
720
|
|
|
|
|
|
|
sub colour{ |
|
721
|
10
|
|
|
10
|
0
|
19
|
my ($fmts)=@_; |
|
722
|
10
|
50
|
|
|
|
19
|
return "" unless $fmts; |
|
723
|
10
|
|
|
|
|
22
|
my @formats=map {lc $_} split / +/,$fmts; |
|
|
10
|
|
|
|
|
24
|
|
|
724
|
10
|
50
|
|
|
|
18
|
return join "",map {defined $colours{$_}?"\033[$colours{$_}m":""} @formats; |
|
|
10
|
|
|
|
|
105
|
|
|
725
|
|
|
|
|
|
|
} |
|
726
|
|
|
|
|
|
|
|
|
727
|
|
|
|
|
|
|
sub wrapText{ |
|
728
|
0
|
|
|
0
|
0
|
|
my ($str,$width)=@_; |
|
729
|
0
|
|
|
|
|
|
my @lines=(); |
|
730
|
0
|
|
|
|
|
|
my $line=""; |
|
731
|
0
|
|
|
|
|
|
$str=~s/ +/ /gm; |
|
732
|
0
|
|
|
|
|
|
$str=~s/\n/ \n /gm; |
|
733
|
0
|
|
|
|
|
|
foreach my $word(split / /,$str){ |
|
734
|
0
|
0
|
|
|
|
|
if ($word eq "\n"){ |
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
735
|
0
|
|
|
|
|
|
push @lines,$line; |
|
736
|
0
|
|
|
|
|
|
$line=""; |
|
737
|
|
|
|
|
|
|
} |
|
738
|
|
|
|
|
|
|
elsif (1+length $line.$word > $width){ |
|
739
|
0
|
|
|
|
|
|
push @lines,$line; |
|
740
|
0
|
|
|
|
|
|
$line=$word; |
|
741
|
|
|
|
|
|
|
} |
|
742
|
|
|
|
|
|
|
elsif ($line eq "") { |
|
743
|
0
|
|
|
|
|
|
$line=$word |
|
744
|
|
|
|
|
|
|
} |
|
745
|
|
|
|
|
|
|
else{ |
|
746
|
0
|
|
|
|
|
|
$line=$line." ".$word |
|
747
|
|
|
|
|
|
|
} |
|
748
|
|
|
|
|
|
|
} |
|
749
|
0
|
|
|
|
|
|
push @lines,$line; |
|
750
|
0
|
|
|
|
|
|
return \@lines; |
|
751
|
|
|
|
|
|
|
} |
|
752
|
|
|
|
|
|
|
|
|
753
|
|
|
|
|
|
|
|
|
754
|
|
|
|
|
|
|
|
|
755
|
|
|
|
|
|
|
# given an 8 bit block of data, produce a braille block |
|
756
|
|
|
|
|
|
|
sub block2braille{ |
|
757
|
1
|
|
|
1
|
|
4852
|
use integer; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
5
|
|
|
758
|
0
|
|
|
0
|
0
|
|
my ($block)=@_; |
|
759
|
0
|
|
|
|
|
|
my $pixelHeight=@$block; |
|
760
|
0
|
|
|
|
|
|
my $pixelWidth=@{$block->[0]}*8; |
|
|
0
|
|
|
|
|
|
|
|
761
|
0
|
0
|
|
|
|
|
my $brCharWidth=$pixelWidth/2 +($pixelWidth & 1?1:0); |
|
762
|
0
|
0
|
|
|
|
|
my $brCharHeight=$pixelHeight/4 + ($pixelHeight & 1?1:0); |
|
763
|
0
|
|
|
|
|
|
my $brBlk=[]; |
|
764
|
|
|
|
|
|
|
|
|
765
|
0
|
|
|
|
|
|
foreach my $chX(0..($brCharWidth-1)){ |
|
766
|
0
|
|
|
|
|
|
foreach my $chY(0..($brCharHeight-1)){ |
|
767
|
0
|
|
|
|
|
|
my $b=ord('⠀'); |
|
768
|
0
|
0
|
|
|
|
|
$b|=ord('⠁') if (pixelAt($block,$chX*2,$chY*4)); |
|
769
|
0
|
0
|
|
|
|
|
$b|=ord('⠂') if (pixelAt($block,$chX*2,$chY*4+1)); |
|
770
|
0
|
0
|
|
|
|
|
$b|=ord('⠄') if (pixelAt($block,$chX*2,$chY*4+2)); |
|
771
|
0
|
0
|
|
|
|
|
$b|=ord('⡀') if (pixelAt($block,$chX*2,$chY*4+3)); |
|
772
|
0
|
0
|
|
|
|
|
$b|=ord('⠈') if (pixelAt($block,$chX*2+1,$chY*4)); |
|
773
|
0
|
0
|
|
|
|
|
$b|=ord('⠐') if (pixelAt($block,$chX*2+1,$chY*4+1)); |
|
774
|
0
|
0
|
|
|
|
|
$b|=ord('⠠') if (pixelAt($block,$chX*2+1,$chY*4+2)); |
|
775
|
0
|
0
|
|
|
|
|
$b|=ord('⢀') if (pixelAt($block,$chX*2+1,$chY*4+3)); |
|
776
|
0
|
|
|
|
|
|
$brBlk->[$chY]->[$chX]=chr($b); |
|
777
|
|
|
|
|
|
|
} |
|
778
|
|
|
|
|
|
|
} |
|
779
|
0
|
|
|
|
|
|
return $brBlk; |
|
780
|
|
|
|
|
|
|
} |
|
781
|
|
|
|
|
|
|
|
|
782
|
|
|
|
|
|
|
# given a block of binary data identify pixel value at a |
|
783
|
|
|
|
|
|
|
# particular position |
|
784
|
|
|
|
|
|
|
sub pixelAt{ |
|
785
|
1
|
|
|
1
|
|
370
|
use integer; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
4
|
|
|
786
|
0
|
|
|
0
|
0
|
|
my ($blk,$px,$py)=@_; |
|
787
|
0
|
|
|
|
|
|
return (($blk->[$py]->[$px/8]) & 2**(7-($px%8))); |
|
788
|
|
|
|
|
|
|
} |
|
789
|
|
|
|
|
|
|
|
|
790
|
|
|
|
|
|
|
|
|
791
|
|
|
|
|
|
|
sub DESTROY{ |
|
792
|
0
|
0
|
|
0
|
|
|
if ($^O eq "MSWin32") { |
|
793
|
0
|
|
|
|
|
|
system("chcp 850"); |
|
794
|
|
|
|
|
|
|
} |
|
795
|
|
|
|
|
|
|
} |
|
796
|
|
|
|
|
|
|
|
|
797
|
|
|
|
|
|
|
1; |
|
798
|
|
|
|
|
|
|
__END__ |