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