| 1 | package Image::TextMode::Reader::PCBoard; | ||
| 2 | |||
| 3 | use Moo; | ||
| 4 | use Types::Standard qw( Int Object HashRef ); | ||
| 5 | use charnames ':full'; | ||
| 6 | |||
| 7 | extends 'Image::TextMode::Reader'; | ||
| 8 | |||
| 9 | # State definitions | ||
| 10 | my $S_TXT = 0; | ||
| 11 | my $S_OP = 1; | ||
| 12 | my $S_END = 2; | ||
| 13 | |||
| 14 | has 'linewrap' => ( is => 'rw', isa => Int, default => 80 ); | ||
| 15 | |||
| 16 | has 'tabstop' => ( is => 'rw', isa => Int, default => 8 ); | ||
| 17 | |||
| 18 | has 'image' => ( is => 'rw', isa => Object ); | ||
| 19 | |||
| 20 | has 'attr' => ( is => 'rw', isa => Int, default => 7 ); | ||
| 21 | |||
| 22 | has 'x' => ( is => 'rw', isa => Int, default => 0 ); | ||
| 23 | |||
| 24 | has 'y' => ( is => 'rw', isa => Int, default => 0 ); | ||
| 25 | |||
| 26 | has 'state' => ( is => 'rw', isa => Int, default => $S_TXT ); | ||
| 27 | |||
| 28 | has 'codes' => ( | ||
| 29 | is => 'rw', | ||
| 30 | isa => HashRef, | ||
| 31 | default => sub { { POFF => '', WAIT => '' } } | ||
| 32 | ); | ||
| 33 | |||
| 34 | sub _read { | ||
| 35 | my ( $self, $image, $fh, $options ) = @_; | ||
| 36 | |||
| 37 | if ( $options->{ width } ) { | ||
| 38 | $self->linewrap( $options->{ width } ); | ||
| 39 | } | ||
| 40 | |||
| 41 | $image->render_options->{ blink_mode } = 0; | ||
| 42 | $self->image( $image ); | ||
| 43 | |||
| 44 | # slurp in file so we can do code replacement | ||
| 45 | seek( $fh, 0, 0 ); | ||
| 46 | my $pcb = do { local $/ = undef; <$fh> }; | ||
| 47 | |||
| 48 | my $code_re = join( q(|), keys %{ $self->codes } ); | ||
| 49 | $pcb =~ s{\@($code_re)\@}{$self->codes->{ $1 }}gse; | ||
| 50 | |||
| 51 | $self->state( $S_TXT ); | ||
| 52 | |||
| 53 | my @str = split( //s, $pcb ); | ||
| 54 | while ( defined( my $ch = shift @str ) ) { | ||
| 55 | my $state = $self->state; | ||
| 56 | |||
| 57 | last if tell( $fh ) > $options->{ filesize }; | ||
| 58 | |||
| 59 | if ( $state == $S_TXT ) { | ||
| 60 | if ( $ch eq "\N{SUBSTITUTE}" ) { | ||
| 61 | $self->state( $S_END ); | ||
| 62 | } | ||
| 63 | elsif ( $ch eq "\N{COMMERCIAL AT}" ) { | ||
| 64 | $self->state( $S_OP ); | ||
| 65 | } | ||
| 66 | elsif ( $ch eq "\n" ) { | ||
| 67 | $self->new_line; | ||
| 68 | } | ||
| 69 | elsif ( $ch eq "\r" ) { | ||
| 70 | |||
| 71 | # do nothing | ||
| 72 | } | ||
| 73 | elsif ( $ch eq "\t" ) { | ||
| 74 | $self->tab; | ||
| 75 | } | ||
| 76 | else { | ||
| 77 | $self->store( $ch ); | ||
| 78 | } | ||
| 79 | } | ||
| 80 | elsif ( $state == $S_OP ) { | ||
| 81 | if ( $ch eq 'X' ) { | ||
| 82 | $self->set_attributes( hex shift @str, hex shift @str ); | ||
| 83 | } | ||
| 84 | elsif ( join( '', $ch, @str[ 0 .. 2 ] ) eq 'CLS@' ) { | ||
| 85 | shift @str for 1 .. 3; | ||
| 86 | $self->clear_screen; | ||
| 87 | } | ||
| 88 | elsif ( join( '', $ch, @str[ 0 .. 2 ] ) eq 'POS:' ) { | ||
| 89 | shift @str for 1 .. 3; | ||
| 90 | |||
| 91 | my $x = shift @str; | ||
| 92 | $x .= shift @str if $str[ 0 ] ne q(@); | ||
| 93 | $x--; | ||
| 94 | |||
| 95 | shift @str; | ||
| 96 | |||
| 97 | $self->x( $x ); | ||
| 98 | } | ||
| 99 | else { # not a valid OP | ||
| 100 | $self->store( q(@) ); | ||
| 101 | $self->store( $ch ); | ||
| 102 | } | ||
| 103 | $self->state( $S_TXT ); | ||
| 104 | } | ||
| 105 | elsif ( $state == $S_END ) { | ||
| 106 | last; | ||
| 107 | } | ||
| 108 | else { | ||
| 109 | $self->state( $S_TXT ); | ||
| 110 | } | ||
| 111 | } | ||
| 112 | |||
| 113 | return $image; | ||
| 114 | } | ||
| 115 | |||
| 116 | sub set_attributes { | ||
| 117 | my ( $self, $bg, $fg ) = @_; | ||
| 118 | |||
| 119 | $self->attr( ( $bg << 4 ) + $fg ); | ||
| 120 | } | ||
| 121 | |||
| 122 | sub clear_screen { | ||
| 123 | my $self = shift; | ||
| 124 | $self->image->clear_screen; | ||
| 125 | } | ||
| 126 | |||
| 127 | sub new_line { | ||
| 128 | my $self = shift; | ||
| 129 | |||
| 130 | $self->y( $self->y + 1 ); | ||
| 131 | $self->x( 0 ); | ||
| 132 | } | ||
| 133 | |||
| 134 | sub tab { | ||
| 135 | my $self = shift; | ||
| 136 | my $count = ( $self->x + 1 ) % $self->tabstop; | ||
| 137 | if ( $count ) { | ||
| 138 | $count = $self->tabstop - $count; | ||
| 139 | for ( 1 .. $count ) { | ||
| 140 | $self->store( ' ' ); | ||
| 141 | } | ||
| 142 | } | ||
| 143 | } | ||
| 144 | |||
| 145 | sub store { | ||
| 146 | my $self = shift; | ||
| 147 | my $char = shift; | ||
| 148 | my $x = shift; | ||
| 149 | my $y = shift; | ||
| 150 | my $attr = shift || $self->attr; | ||
| 151 | |||
| 152 | if ( defined $x and defined $y ) { | ||
| 153 | $self->image->putpixel( { char => $char, attr => $attr }, $x, $y ); | ||
| 154 | } | ||
| 155 | else { | ||
| 156 | $self->image->putpixel( { char => $char, attr => $attr }, | ||
| 157 | $self->x, $self->y ); | ||
| 158 | $self->x( $self->x + 1 ); | ||
| 159 | } | ||
| 160 | |||
| 161 | if ( $self->x >= $self->linewrap ) { | ||
| 162 | $self->new_line; | ||
| 163 | } | ||
| 164 | } | ||
| 165 | |||
| 166 | =head1 NAME | ||
| 167 | |||
| 168 | Image::TextMode::Reader::PCBoard - Reads PCBoard files | ||
| 169 | |||
| 170 | =head1 DESCRIPTION | ||
| 171 | |||
| 172 | Provides reading capabilities for the PCBoard format. | ||
| 173 | |||
| 174 | =head1 ACCESSORS | ||
| 175 | |||
| 176 | =over 4 | ||
| 177 | |||
| 178 | =item * tabstop - every Nth character will be a tab stop location (default: 8) | ||
| 179 | |||
| 180 | =item * x - current x (default: 0) | ||
| 181 | |||
| 182 | =item * y - current y (default: 0) | ||
| 183 | |||
| 184 | =item * attr - current attribute info (default: 7, gray on black) | ||
| 185 | |||
| 186 | =item * state - state of the parser (default: C<$S_TXT>) | ||
| 187 | |||
| 188 | =item * image - the image we're parsing into | ||
| 189 | |||
| 190 | =item * linewrap - max width before we wrap to the next line (default: 80) | ||
| 191 | |||
| 192 | =item * codes - hashref of key-value pairs to substitute into the image | ||
| 193 | |||
| 194 | =back | ||
| 195 | |||
| 196 | =head1 METHODS | ||
| 197 | |||
| 198 | =head2 set_attributes( $bg, $fg ) | ||
| 199 | |||
| 200 | Sets the default attribute information (fg and bg). | ||
| 201 | |||
| 202 | =head2 clear_screen( ) | ||
| 203 | |||
| 204 | Clears all data on the canvas. | ||
| 205 | |||
| 206 | =head2 new_line( ) | ||
| 207 | |||
| 208 | Simulates a C<\n> character. | ||
| 209 | |||
| 210 | =head2 tab( ) | ||
| 211 | |||
| 212 | Simulates a C<\t> character. | ||
| 213 | |||
| 214 | =head2 store( $char, $x, $y [, $attr] ) | ||
| 215 | |||
| 216 | Stores C<$char> at position C<$x, $y> with either the supplied attribute | ||
| 217 | or the current attribute setting. | ||
| 218 | |||
| 219 | =head1 AUTHOR | ||
| 220 | |||
| 221 | Brian Cassidy E<lt>bricas@cpan.orgE<gt> | ||
| 222 | |||
| 223 | =head1 COPYRIGHT AND LICENSE | ||
| 224 | |||
| 225 | Copyright 2008-2015 by Brian Cassidy | ||
| 226 | |||
| 227 | This library is free software; you can redistribute it and/or modify | ||
| 228 | it under the same terms as Perl itself. | ||
| 229 | |||
| 230 | =cut | ||
| 231 | |||
| 232 | 1; |