Graille.pm

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