| 1 | package Image::TextMode::Reader::AVATAR; | ||
| 2 | |||
| 3 | use Moo; | ||
| 4 | use Types::Standard qw( Int Bool Object ); | ||
| 5 | use charnames ':full'; | ||
| 6 | |||
| 7 | extends 'Image::TextMode::Reader'; | ||
| 8 | |||
| 9 | has 'tabstop' => ( is => 'rw', isa => Int, default => 8 ); | ||
| 10 | |||
| 11 | has 'x' => ( is => 'rw', isa => Int, default => 0 ); | ||
| 12 | |||
| 13 | has 'y' => ( is => 'rw', isa => Int, default => 0 ); | ||
| 14 | |||
| 15 | has 'insert' => ( is => 'rw', isa => Bool, default => 0 ); | ||
| 16 | |||
| 17 | has 'attr' => ( is => 'rw', isa => Int, default => 3 ); | ||
| 18 | |||
| 19 | has 'image' => ( is => 'rw', isa => Object ); | ||
| 20 | |||
| 21 | has 'linewrap' => ( is => 'rw', isa => Int, default => 80 ); | ||
| 22 | |||
| 23 | sub _read { | ||
| 24 | my ( $self, $image, $fh, $options ) = @_; | ||
| 25 | |||
| 26 | $self->image( $image ); | ||
| 27 | if ( $options->{ width } ) { | ||
| 28 | $self->linewrap( $options->{ width } ); | ||
| 29 | } | ||
| 30 | |||
| 31 | if ( $image->has_sauce ) { | ||
| 32 | $image->render_options->{ blink_mode } = ($image->sauce->flags_id & 1) ^ 1; | ||
| 33 | } | ||
| 34 | |||
| 35 | seek( $fh, 0, 0 ); | ||
| 36 | |||
| 37 | my $ch; | ||
| 38 | while ( read( $fh, $ch, 1 ) ) { | ||
| 39 | last if tell( $fh ) > $options->{ filesize }; | ||
| 40 | if ( $ch eq "\N{SUBSTITUTE}" ) { | ||
| 41 | last; | ||
| 42 | } | ||
| 43 | elsif ( $ch eq "\n" ) { | ||
| 44 | $self->new_line; | ||
| 45 | } | ||
| 46 | elsif ( $ch eq "\r" ) { | ||
| 47 | |||
| 48 | # do nothing | ||
| 49 | } | ||
| 50 | elsif ( $ch eq "\t" ) { | ||
| 51 | $self->tab; | ||
| 52 | } | ||
| 53 | elsif ( ord $ch == 12 ) { | ||
| 54 | $self->clear_screen; | ||
| 55 | $self->attr( 3 ); | ||
| 56 | $self->insert( 0 ); | ||
| 57 | } | ||
| 58 | elsif ( ord $ch == 25 ) { | ||
| 59 | my $i; | ||
| 60 | read( $fh, $ch, 1 ); | ||
| 61 | read( $fh, $i, 1 ); | ||
| 62 | $self->store( $ch ) for 1 .. ord $i; | ||
| 63 | } | ||
| 64 | elsif ( ord $ch == 22 ) { | ||
| 65 | read( $fh, $ch, 1 ); | ||
| 66 | my $c = ord $ch; | ||
| 67 | if ( $c == 1 ) { | ||
| 68 | my $a; | ||
| 69 | read( $fh, $a, 1 ); | ||
| 70 | $self->attr( ord $a & 0x7f ); | ||
| 71 | } | ||
| 72 | elsif ( $c == 2 ) { | ||
| 73 | $self->attr( $self->attr | 0x80 ); | ||
| 74 | } | ||
| 75 | elsif ( $c == 3 ) { | ||
| 76 | $self->move_up; | ||
| 77 | } | ||
| 78 | elsif ( $c == 4 ) { | ||
| 79 | $self->move_down; | ||
| 80 | } | ||
| 81 | elsif ( $c == 5 ) { | ||
| 82 | $self->move_left; | ||
| 83 | } | ||
| 84 | elsif ( $c == 6 ) { | ||
| 85 | $self->move_right; | ||
| 86 | } | ||
| 87 | elsif ( $c == 7 ) { | ||
| 88 | $self->clear_line; | ||
| 89 | } | ||
| 90 | elsif ( $c == 8 ) { | ||
| 91 | my ( $x, $y ); | ||
| 92 | read( $fh, $y, 1 ); | ||
| 93 | read( $fh, $x, 1 ); | ||
| 94 | $self->set_position( ord $y, ord $x ); | ||
| 95 | } | ||
| 96 | |||
| 97 | # AVT/0+ spec starts here | ||
| 98 | elsif ( $c == 9 ) { | ||
| 99 | $self->insert( 1 ); | ||
| 100 | } | ||
| 101 | elsif ( $c == 10 || $c == 11 ) { | ||
| 102 | my ( $n, $x0, $y0, $x1, $y1 ); | ||
| 103 | read( $fh, $n, 1 ); | ||
| 104 | read( $fh, $x0, 1 ); | ||
| 105 | read( $fh, $y0, 1 ); | ||
| 106 | read( $fh, $x1, 1 ); | ||
| 107 | read( $fh, $y1, 1 ); | ||
| 108 | |||
| 109 | $self->scroll( $c == 10 ? 'up' : 'down', | ||
| 110 | $n, $x0, $y0, $x1, $y1 ); | ||
| 111 | } | ||
| 112 | elsif ( $c == 12 || $c == 13 ) { | ||
| 113 | my ( $a, $char, $rows, $cols ); | ||
| 114 | read( $fh, $a, 1 ); | ||
| 115 | read( $fh, $char, 1 ) if $c == 13; | ||
| 116 | read( $fh, $rows, 1 ); | ||
| 117 | read( $fh, $cols, 1 ); | ||
| 118 | |||
| 119 | $self->attr( ord $a & 0x7f ); | ||
| 120 | $self->clear_box( ord $rows, ord $cols, $char ); | ||
| 121 | } | ||
| 122 | elsif ( $c == 14 ) { | ||
| 123 | splice( @{ $self->image->pixeldata->[ $self->y ] }, | ||
| 124 | $self->x, 1 ); | ||
| 125 | } | ||
| 126 | elsif ( $c == 25 ) { | ||
| 127 | my ( $n, $buf, $i ); | ||
| 128 | read( $fh, $n, 1 ); | ||
| 129 | read( $fh, $buf, ord $n ); | ||
| 130 | read( $fh, $i, 1 ); | ||
| 131 | my @chars = split //s, $buf; | ||
| 132 | |||
| 133 | # According to spec, this can contain AVT/0 codes and should | ||
| 134 | # probably be written back to the stream for parsing. | ||
| 135 | # We'll send it directly to the screen for now. | ||
| 136 | for ( 1 .. ord $i ) { | ||
| 137 | $self->store( $_ ) for @chars; | ||
| 138 | } | ||
| 139 | } | ||
| 140 | |||
| 141 | $self->insert( 0 ) if $c < 9; | ||
| 142 | } | ||
| 143 | else { | ||
| 144 | $self->store( $ch ); | ||
| 145 | } | ||
| 146 | } | ||
| 147 | |||
| 148 | return $image; | ||
| 149 | } | ||
| 150 | |||
| 151 | sub set_position { | ||
| 152 | my ( $self, $y, $x ) = @_; | ||
| 153 | $y = ( $y || 1 ) - 1; | ||
| 154 | $x = ( $x || 1 ) - 1; | ||
| 155 | |||
| 156 | $y = 0 if $y < 0; | ||
| 157 | $x = 0 if $x < 0; | ||
| 158 | |||
| 159 | $self->x( $x ); | ||
| 160 | $self->y( $y ); | ||
| 161 | } | ||
| 162 | |||
| 163 | sub move_up { | ||
| 164 | my $self = shift; | ||
| 165 | my $y = $self->y - ( shift || 1 ); | ||
| 166 | $y = 0 if $y < 0; | ||
| 167 | |||
| 168 | $self->y( $y ); | ||
| 169 | } | ||
| 170 | |||
| 171 | sub move_down { | ||
| 172 | my $self = shift; | ||
| 173 | my $y = shift || 1; | ||
| 174 | |||
| 175 | $self->y( $self->y + $y ); | ||
| 176 | } | ||
| 177 | |||
| 178 | sub move_right { | ||
| 179 | my $self = shift; | ||
| 180 | my $x = shift || 1; | ||
| 181 | |||
| 182 | $self->x( $self->x + $x ); | ||
| 183 | } | ||
| 184 | |||
| 185 | sub move_left { | ||
| 186 | my $self = shift; | ||
| 187 | my $x = $self->x - ( shift || 1 ); | ||
| 188 | |||
| 189 | $x = 0 if $x < 0; | ||
| 190 | |||
| 191 | $self->x( $x ); | ||
| 192 | } | ||
| 193 | |||
| 194 | sub scroll { ## no critic (Subroutines::ProhibitManyArgs) | ||
| 195 | my ( $self, $dir, $n, $x0, $y0, $x1, $y1 ) = @_; | ||
| 196 | $x0--; | ||
| 197 | $y0--; | ||
| 198 | $x1--; | ||
| 199 | $y1--; | ||
| 200 | |||
| 201 | my $pixeldata = $self->image->pixeldata; | ||
| 202 | my $cols = $x1 - $x0; | ||
| 203 | my @rows = $y0 .. $y1; | ||
| 204 | if ( $dir eq 'down' ) { | ||
| 205 | @rows = reverse @rows; | ||
| 206 | } | ||
| 207 | else { | ||
| 208 | $n = 0 - $n; | ||
| 209 | } | ||
| 210 | |||
| 211 | my $attr = $self->attr; | ||
| 212 | my @blank = ( { char => ' ', attr => $attr } ) x $cols; | ||
| 213 | |||
| 214 | for my $from ( @rows ) { | ||
| 215 | my $to = $from + $n; | ||
| 216 | next if $to < 0; | ||
| 217 | splice( @{ $pixeldata->[ $to ] }, | ||
| 218 | $x0, $cols, @{ $pixeldata->[ $from ] }[ $x0 .. $x1 ] ); | ||
| 219 | splice( @{ $pixeldata->[ $from ] }, $x0, $cols, @blank ); | ||
| 220 | } | ||
| 221 | } | ||
| 222 | |||
| 223 | sub clear_box { | ||
| 224 | my ( $self, $rows, $cols, $char ) = @_; | ||
| 225 | |||
| 226 | $char = ' ' unless defined $char; | ||
| 227 | my $sx = $self->x; | ||
| 228 | my $sy = $self->y; | ||
| 229 | |||
| 230 | for my $x ( map { $sx + $_ } 0 .. $cols - 1 ) { | ||
| 231 | for my $y ( map { $sy + $_ } 0 .. $rows - 1 ) { | ||
| 232 | $self->store( $char, $x, $y ); | ||
| 233 | } | ||
| 234 | } | ||
| 235 | |||
| 236 | $self->x( $sx ); | ||
| 237 | $self->y( $sy ); | ||
| 238 | } | ||
| 239 | |||
| 240 | sub clear_line { | ||
| 241 | my $self = shift; | ||
| 242 | |||
| 243 | $self->image->clear_line( $self->y, [ $self->x, -1 ] ); | ||
| 244 | } | ||
| 245 | |||
| 246 | sub clear_screen { | ||
| 247 | my $self = shift; | ||
| 248 | |||
| 249 | $self->image->clear_screen; | ||
| 250 | } | ||
| 251 | |||
| 252 | sub new_line { | ||
| 253 | my $self = shift; | ||
| 254 | |||
| 255 | $self->y( $self->y + 1 ); | ||
| 256 | $self->x( 0 ); | ||
| 257 | } | ||
| 258 | |||
| 259 | sub tab { | ||
| 260 | my $self = shift; | ||
| 261 | my $count = ( $self->x + 1 ) % $self->tabstop; | ||
| 262 | if ( $count ) { | ||
| 263 | $count = $self->tabstop - $count; | ||
| 264 | for ( 1 .. $count ) { | ||
| 265 | $self->store( ' ' ); | ||
| 266 | } | ||
| 267 | } | ||
| 268 | } | ||
| 269 | |||
| 270 | sub store { | ||
| 271 | my $self = shift; | ||
| 272 | my $char = shift; | ||
| 273 | my $x = shift; | ||
| 274 | my $y = shift; | ||
| 275 | my $attr = shift || $self->attr; | ||
| 276 | |||
| 277 | if ( $self->insert ) { | ||
| 278 | my $col = $self->x; | ||
| 279 | my $row = $self->image->pixeldata->[ $self->y ]; | ||
| 280 | splice( @$row, $col + 1, @$row - 1 - $col, @{ $row }[ $col .. -1 ] ); | ||
| 281 | } | ||
| 282 | |||
| 283 | if ( defined $x and defined $y ) { | ||
| 284 | $self->image->putpixel( { char => $char, attr => $attr }, $x, $y ); | ||
| 285 | } | ||
| 286 | else { | ||
| 287 | $self->image->putpixel( { char => $char, attr => $attr }, | ||
| 288 | $self->x, $self->y ); | ||
| 289 | $self->x( $self->x + 1 ); | ||
| 290 | } | ||
| 291 | |||
| 292 | if ( $self->x >= $self->linewrap ) { | ||
| 293 | $self->new_line; | ||
| 294 | } | ||
| 295 | } | ||
| 296 | |||
| 297 | =head1 NAME | ||
| 298 | |||
| 299 | Image::TextMode::Reader::AVATAR - Reads AVATAR files | ||
| 300 | |||
| 301 | =head1 DESCRIPTION | ||
| 302 | |||
| 303 | Provides reading capabilities for the AVATAR format. | ||
| 304 | |||
| 305 | =head1 COMPATIBILITY | ||
| 306 | |||
| 307 | The reader implements all of the AVT/0 specification as well as the majority | ||
| 308 | of the AVT/0+ specification. The main difference being that AVT/0+ character | ||
| 309 | expansion is not re-interpreted, thus expansions containing further AVT/0 | ||
| 310 | codes will simply be written as characters to the canvas. | ||
| 311 | |||
| 312 | =head1 ACCESSORS | ||
| 313 | |||
| 314 | =over 4 | ||
| 315 | |||
| 316 | =item * tabstop - every Nth character will be a tab stop location (default: 8) | ||
| 317 | |||
| 318 | =item * x - current x (default: 0) | ||
| 319 | |||
| 320 | =item * y - current y (default: 0) | ||
| 321 | |||
| 322 | =item * attr - current attribute info (default: 7, gray on black) | ||
| 323 | |||
| 324 | =item * image - the image we're parsing into | ||
| 325 | |||
| 326 | =item * insert - insert mode (default: off) | ||
| 327 | |||
| 328 | =item * linewrap - max width before we wrap to the next line (default: 80) | ||
| 329 | |||
| 330 | =back | ||
| 331 | |||
| 332 | =head1 METHODS | ||
| 333 | |||
| 334 | =head2 set_position( [$y, $x] ) | ||
| 335 | |||
| 336 | Moves the cursor to C<$x, $y>. | ||
| 337 | |||
| 338 | =head2 move_up( $y ) | ||
| 339 | |||
| 340 | Moves the cursor up C<$y> lines. | ||
| 341 | |||
| 342 | =head2 move_down( $y ) | ||
| 343 | |||
| 344 | Moves the cursor down C<$y> lines. | ||
| 345 | |||
| 346 | =head2 move_left( $x ) | ||
| 347 | |||
| 348 | Moves the cursor left C<$x> columns. | ||
| 349 | |||
| 350 | =head2 move_right( $x ) | ||
| 351 | |||
| 352 | Moves the cursor right C<$x> columns. | ||
| 353 | |||
| 354 | =head2 scroll( $dir, $n, $x0, $y0, $x1, $y1 ) | ||
| 355 | |||
| 356 | Scrolls box bound by (C<$x0>, C<$y0>) and (C<$x1>, C<$y1>) in direction | ||
| 357 | C<$dir> (up or down), by C<$n> lines. | ||
| 358 | |||
| 359 | =head2 clear_box( $rows, $cols [, $char] ) | ||
| 360 | |||
| 361 | Clears box bound from current cursor position for C<$rows> rows and C<$cols> | ||
| 362 | columns using C<$char> as the character. | ||
| 363 | |||
| 364 | =head2 clear_screen( ) | ||
| 365 | |||
| 366 | Clears all data on the canvas. | ||
| 367 | |||
| 368 | =head2 clear_line( ) | ||
| 369 | |||
| 370 | Clears the remainder of the current line. | ||
| 371 | |||
| 372 | =head2 new_line( ) | ||
| 373 | |||
| 374 | Simulates a C<\n> character. | ||
| 375 | |||
| 376 | =head2 tab( ) | ||
| 377 | |||
| 378 | Simulates a C<\t> character. | ||
| 379 | |||
| 380 | =head2 store( $char, $x, $y [, $attr] ) | ||
| 381 | |||
| 382 | Stores C<$char> at position C<$x, $y> with either the supplied attribute | ||
| 383 | or the current attribute setting. | ||
| 384 | |||
| 385 | =head1 AUTHOR | ||
| 386 | |||
| 387 | Brian Cassidy E<lt>bricas@cpan.orgE<gt> | ||
| 388 | |||
| 389 | =head1 COPYRIGHT AND LICENSE | ||
| 390 | |||
| 391 | Copyright 2008-2015 by Brian Cassidy | ||
| 392 | |||
| 393 | This library is free software; you can redistribute it and/or modify | ||
| 394 | it under the same terms as Perl itself. | ||
| 395 | |||
| 396 | =cut | ||
| 397 | |||
| 398 | 1; |