File Coverage

blib/lib/Image/TextMode/Reader/ATASCII.pm
Criterion Covered Total %
statement 33 64 51.5
branch 24 54 44.4
condition 1 3 33.3
subroutine 5 8 62.5
pod 4 4 100.0
total 67 133 50.3


line stmt bran cond sub pod time code
1             package Image::TextMode::Reader::ATASCII;
2              
3 1     1   451 use Moo;
  1         3  
  1         6  
4 1     1   255 use Types::Standard qw( Int Object );
  1         1  
  1         7  
5 1     1   1105 use charnames ':full';
  1         23514  
  1         5  
6              
7             extends 'Image::TextMode::Reader';
8              
9             # State definitions
10             my $S_TXT = 0;
11             my $S_ESC = 1;
12             my $S_END = 2;
13              
14             has 'linewrap' => ( is => 'rw', isa => Int, default => 41 );
15              
16             has 'tabstop' => ( is => 'rw', isa => Int, default => 8 );
17              
18             has 'image' => ( is => 'rw', isa => Object );
19              
20             has 'x' => ( is => 'rw', isa => Int, default => 0 );
21              
22             has 'y' => ( is => 'rw', isa => Int, default => 0 );
23              
24             has 'state' => ( is => 'rw', isa => Int, default => $S_TXT );
25              
26             sub _read {
27 1     1   2 my ( $self, $image, $fh, $options ) = @_;
28              
29 1 50       3 if ( $options->{ width } ) {
30 0         0 $self->linewrap( $options->{ width } );
31             }
32              
33 1         7 $image->render_options->{ blink_mode } = 0;
34 1         476 $self->image( $image );
35              
36 1         629 $self->state( $S_TXT );
37              
38 1         547 my $ch;
39 1         39675 while ( read( $fh, $ch, 1 ) ) {
40 8         813 my $state = $self->state;
41              
42 8 50       52 last if tell( $fh ) > $options->{ filesize };
43              
44 8 100       15 if ( $state == $S_TXT ) {
    50          
    0          
45 6 100       61 if ( ord $ch == 27 ) {
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
46 2         30 $self->state( $S_ESC );
47             }
48             elsif ( ord $ch == 28 ) {
49 0         0 $self->y( $self->y - 1 );
50 0 0       0 $self->y( 0 ) if $self->y < 0;
51             }
52             elsif ( ord $ch == 29 ) {
53 0         0 $self->y( $self->y + 1 );
54             }
55             elsif ( ord $ch == 30 ) {
56 0         0 $self->x( $self->x - 1 );
57 0 0       0 $self->x( 0 ) if $self->x < 0;
58             }
59             elsif ( ord $ch == 31 ) {
60 0         0 $self->x( $self->x + 1 );
61 0 0       0 $self->x( $self->linewrap - 1 ) if $self->x == $self->linewrap;
62             }
63             elsif ( ord $ch == 125 ) {
64 0         0 $self->clear_screen;
65             }
66             elsif ( ord $ch == 126 ) {
67 0         0 $self->x( $self->x - 1 );
68 0         0 $self->store( ' ' );
69 0         0 $self->x( $self->x - 1 );
70             }
71             elsif ( ord $ch == 127 ) {
72 0         0 $self->tab;
73             }
74             elsif ( ord $ch == 155 ) {
75 0         0 $self->new_line;
76             }
77             elsif ( ord $ch == 156 ) {
78             # delete line
79             }
80             elsif ( ord $ch == 157 ) {
81             # insert line
82             }
83             elsif ( ord $ch == 158 ) {
84             # clear tab stop
85             }
86             elsif ( ord $ch == 159 ) {
87             # set tab stop
88             }
89             elsif ( ord $ch == 253 ) {
90             # buzzer
91             }
92             elsif ( ord $ch == 254 ) {
93             # delete char
94             }
95             elsif ( ord $ch == 255 ) {
96             # insert char
97             }
98             else {
99 4         10 $self->store( $ch );
100             }
101             }
102             elsif ( $state == $S_ESC ) {
103 2         4 $self->store( $ch );
104 2         67 $self->state( $S_TXT );
105             }
106             elsif ( $state == $S_END ) {
107 0         0 last;
108             }
109             else {
110 0         0 $self->state( $S_TXT );
111             }
112             }
113              
114 1         65 return $image;
115             }
116              
117             sub clear_screen {
118 0     0 1 0 my $self = shift;
119 0         0 $self->image->clear_screen;
120 0         0 $self->x( 0 );
121 0         0 $self->y( 0 );
122             }
123              
124             sub new_line {
125 0     0 1 0 my $self = shift;
126              
127 0         0 $self->y( $self->y + 1 );
128 0         0 $self->x( 0 );
129             }
130              
131             sub tab {
132 0     0 1 0 my $self = shift;
133 0         0 my $count = ( $self->x + 1 ) % $self->tabstop;
134 0 0       0 if ( $count ) {
135 0         0 $count = $self->tabstop - $count;
136 0         0 for ( 1 .. $count ) {
137 0         0 $self->store( ' ' );
138             }
139             }
140             }
141              
142             sub store {
143 6     6 1 9 my $self = shift;
144 6         6 my $char = shift;
145 6         8 my $x = shift;
146 6         4 my $y = shift;
147              
148 6 50 33     18 if ( defined $x and defined $y ) {
149 0         0 $self->image->putpixel( { char => $char, bg => 0, fg => 1 }, $x, $y );
150             }
151             else {
152 6         97 $self->image->putpixel( { char => $char, bg => 0, fg => 1 },
153             $self->x, $self->y );
154 6         298 $self->x( $self->x + 1 );
155             }
156              
157 6 50       183 if ( $self->x >= $self->linewrap ) {
158 0           $self->new_line;
159             }
160             }
161              
162             =head1 NAME
163              
164             Image::TextMode::Reader::ATASCII - Reads ATASCII files
165              
166             =head1 DESCRIPTION
167              
168             Provides reading capabilities for the ATASCII format.
169              
170             =head1 ACCESSORS
171              
172             =over 4
173              
174             =item * tabstop - every Nth character will be a tab stop location (default: 8)
175              
176             =item * x - current x (default: 0)
177              
178             =item * y - current y (default: 0)
179              
180             =item * state - state of the parser (default: C<$S_TXT>)
181              
182             =item * image - the image we're parsing into
183              
184             =item * linewrap - max width before we wrap to the next line (default: 80)
185              
186             =back
187              
188             =head1 METHODS
189              
190             =head2 clear_screen( )
191              
192             Clears all data on the canvas.
193              
194             =head2 new_line( )
195              
196             Simulates a C<\n> character.
197              
198             =head2 tab( )
199              
200             Simulates a C<\t> character.
201              
202             =head2 store( $char, $x, $y )
203              
204             Stores C<$char> at position C<$x, $y>.
205              
206             =head1 AUTHOR
207              
208             Brian Cassidy Ebricas@cpan.orgE
209              
210             =head1 COPYRIGHT AND LICENSE
211              
212             Copyright 2008-2014 by Brian Cassidy
213              
214             This library is free software; you can redistribute it and/or modify
215             it under the same terms as Perl itself.
216              
217             =cut
218              
219             1;