Image::ANSI::Parser - Reads in ANSI files


Image-ANSI documentation Contained in the Image-ANSI distribution.

Index


Code Index:

NAME

Top

Image::ANSI::Parser - Reads in ANSI files

SYNOPSIS

Top

	my $parser = Image::ANSI::Parser->new;
	my $ansi   = $parser->parse( file => 'file.ans' );

METHODS

Top

new( [%options] )

Creates a new parser object and reads in a file, handle or string.

clear( )

Clears the internal ANSI object.

parse( %options )

Reads in a file, handle or string

	my $parser = Image::ANSI::Parser->new;

	# filename
	$ansi = $parser->parse( file => 'file.ans' );

	# file handle
	$ansi = $parser->parse( handle => $handle );

	# string
	$ansi = $parser->parse( string => $string );

ansi( [$ansi] )

Gets / sets the internal ANSI object.

x( [$x] )

stores the current 'x' location.

y( [$y] )

stores the current 'y' location.

set_position( [$x, $y] )

sets the x() and y() positions.

set_attributes( @attributes )

sets the attributes of the pixel (fg, bg, blinking)

move_up( [$number] )

moves y() up by $number (default 1).

move_down( [$number] )

moves y() down by $number (default 1).

move_right( [$number] )

moves x() right by $number (default 1).

move_left( [$number] )

moves x() left by $number (default 1).

save_position( )

saves the current x() and y() positions.

restore_position( )

restored the saved x() and y() positions.

clear_line( )

clears the pixels on the current line.

clear_screen( )

clears all pixels.

new_line( )

simulates a newline char.

tab( )

simulates a tab char (8 spaces).

store( $char, $x, $y [, $attr] )

stores $char at position $x, $y using attributes $attr (or the current attr setting if none are supplied).

putpixel( @args )

same as the pixel() method

getpixel( @args )

same as the pixel() method

pixel( $x, $y [, $char, $attr] )

get/sets the pixel at $x, $y.

AUTHOR

Top

* Brian Cassidy <bricas@cpan.org>

COPYRIGHT AND LICENSE

Top


Image-ANSI documentation Contained in the Image-ANSI distribution.
package Image::ANSI::Parser;

use base qw( Class::Accessor );

use warnings;
use strict;

use Image::ANSI;
use Image::ANSI::Pixel;
use Image::ANSI::Utils;

# State definitions
use constant S_TXT      => 0;
use constant S_CHK_B    => 1;
use constant S_WAIT_LTR => 2;
use constant S_END      => 3;

use constant TABSTOP    => 8;

our $VERSION  = '0.10';
my @accessors = qw( save_x save_y attr state );

__PACKAGE__->mk_accessors( @accessors );

sub new {
	my $class = shift;
	my $self  = {};

	bless $self, $class;

	$self->clear;

	return $self->parse( @_ ) if @_;

	return $self;
}

sub clear {
	my $self = shift;

	$self->$_( 0 ) for qw( x y save_x save_y attr );
	$self->ansi( Image::ANSI->new );

	# default to white on black;
	$self->set_attributes( 37 );
}

sub parse {
	my $self    = shift;
	my %options = @_;
	my $file    = $self->create_io_object( \%options, '<' );

	my( $argbuf, $ch );

	$self->clear;

	$self->ansi->sauce->read( handle => $file );

	$self->state( S_TXT );

	seek( $file, 0, 0 );
	while( $file->read( $ch, 1 ) ) {
		my $state = $self->state;
		if( $state == S_TXT ) {
			if( $ch eq "\x1a" ) {
				$self->state( S_END );
			}
			elsif( $ch eq "\x1b" ) {
				$self->state( S_CHK_B );
			}
			elsif( $ch eq "\n" ) {
				$self->new_line;
			}
			elsif( $ch eq "\r" ) {
				# do nothing
			}
			elsif( $ch eq "\t" ) {
				$self->tab;
			}
			else {
				$self->store( $ch );
			}
		}
		elsif( $state == S_CHK_B ) {
                	if( $ch ne '[' ) {
				$self->store( chr( 27 ) );
				$self->store( $ch );
				$self->state( S_TXT );
			}
			else {
				$self->state( S_WAIT_LTR );
			}
		}
		elsif( $state == S_WAIT_LTR ) {
			if( $ch =~ /[a-zA-Z]/ ) {
				my @args = split( /;/, $argbuf );

				if( $ch eq 'm' ) {
					$self->set_attributes( @args );
				}
				elsif( $ch eq 'H' or $ch eq 'f' ) {
					$self->set_position( @args );
				}
				elsif( $ch eq 'A' ) {
					$self->move_up( @args );
				}
				elsif( $ch eq 'B' ) {
					$self->move_down( @args );
				}
				elsif( $ch eq 'C' ) {
					$self->move_right( @args );
				}
				elsif( $ch eq 'D' ) {
					$self->move_left( @args );
				}
				elsif( $ch eq 's' ) {
					$self->save_position( @args );
				}
				elsif( $ch eq 'u' ) {
					$self->restore_position( @args );
				}
				elsif( $ch eq 'J' ) {
					$self->clear_screen( @args );
				}
				elsif( $ch eq 'K' ) {
					$self->clear_line( @args );
				}

				$argbuf = '';
				$self->state( S_TXT );
			}
			else {
				$argbuf .= $ch;
			}
		}
		elsif( $state == S_END ) {
			last;
		}
		else {
			$self->state( S_TXT );
		}
	}

	return $self->ansi;
}


sub ansi {
	my $self    = shift;
	my( $ansi ) = @_;

	if( @_ ) {
		$self->{ _ANSI } = $ansi;
	}

	return $self->{ _ANSI };
}

sub x {
	my $self = shift;
	my $data = shift;

	$self->{ x } = $data if defined $data;

	if( $self->{ x } < 0 ) {
		$self->{ x } = 0;
	}

	return $self->{ x };
}

sub y {
	my $self = shift;
	my $data = shift;

	$self->{ y } = $data if defined $data;

	if( $self->{ y } < 0 ) {
		$self->{ y } = 0;
	}

	return $self->{ y };
}

# Handlers


sub set_position {
	my $self = shift;
	my $y    = shift || 1;
	my $x    = shift || 1;

	$self->x( $x - 1 );
	$self->y( $y - 1 );
}

sub set_attributes {
	my $self = shift;
	my @args = @_;

	foreach( @args ) {
		if( $_ == 0 ) {
			$self->attr( 7 );
		}
		elsif( $_ == 1 ) {
			$self->attr( $self->attr | 8 );
		}
		elsif( $_ == 5 ) {
			$self->attr( $self->attr | 128 );
		}
		elsif( $_  >= 30 and $_ <= 37 ) {
			$self->attr( $self->attr & 248 );
			$self->attr( $self->attr | ( $_ - 30 ) );
		}
		elsif( $_  >= 40 and $_ <= 47 ) {
			$self->attr( $self->attr & 143 );
			$self->attr( $self->attr | ( ( $_ - 40 ) << 4 ) );
		}
	}
}

sub move_up {
	my $self = shift;
	my $y    = shift || 1;

	$self->y( $self->y - $y );
}


sub move_down {
	my $self = shift;
	my $y    = shift || 1;

	$self->y( $self->y + $y );
}

sub move_right {
	my $self = shift;
	my $x    = shift || 1;

	$self->x( $self->x + $x );
}

sub move_left {
	my $self = shift;
	my $x    = shift || 1;

	$self->x( $self->x - $x );
}

sub save_position {
	my $self = shift;

	$self->save_x( $self->x );
	$self->save_y( $self->y );
}

sub restore_position {
	my $self = shift;

	$self->x( $self->save_x );
	$self->y( $self->save_y );
}

sub clear_line {
	my $self = shift;

	$self->ansi->clear_line( $self->y );
}

sub clear_screen {
	my $self = shift;

	$self->ansi->clear;
}

sub new_line {
	my $self = shift;

	$self->y( $self->y + 1 );
	$self->x( 0 );
}


sub tab {
	my $self = shift;
	my $count = ( $self->x + 1 ) % TABSTOP;
	if( $count ) {
		$count = TABSTOP - $count;
		for( 1..$count ) {
			$self->store( ' ' );
		}
	}
}

sub store {
	my $self = shift;
	my $char = shift;
	my $x    = shift;
	my $y    = shift;
	my $attr = shift || $self->attr;

	if( defined $x and defined $y ) {
		$self->putpixel( $x, $y, $char, $attr );
	}
	else {
		$self->putpixel( $self->x, $self->y, $char, $attr );
		$self->x( $self->x + 1 );
	}
}

*putpixel = \&pixel;

*getpixel = \&pixel;

sub pixel {
	my $self = shift;
	my $x    = shift;
	my $y    = shift;
	my $char = shift;
	my $attr = shift;

	if( defined $char ) {
		my $pixel = Image::ANSI::Pixel->new( char => $char, attr => $attr );
		$self->ansi->putpixel( $x, $y, $pixel );
	}

	return $self->ansi->getpixel( $x, $y );
}

1;