| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Image::TextMode::Format; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 29 |  |  | 29 |  | 14599 | use Moo; | 
|  | 29 |  |  |  |  | 12123 |  | 
|  | 29 |  |  |  |  | 161 |  | 
| 4 | 29 |  |  | 29 |  | 12414 | use Types::Standard qw( Object HashRef InstanceOf ); | 
|  | 29 |  |  |  |  | 330476 |  | 
|  | 29 |  |  |  |  | 239 |  | 
| 5 | 29 |  |  | 29 |  | 18317 | use Module::Runtime (); | 
|  | 29 |  |  |  |  | 45 |  | 
|  | 29 |  |  |  |  | 444 |  | 
| 6 | 29 |  |  | 29 |  | 14989 | use Image::TextMode::Font::8x16; | 
|  | 29 |  |  |  |  | 76 |  | 
|  | 29 |  |  |  |  | 1002 |  | 
| 7 | 29 |  |  | 29 |  | 12164 | use Image::TextMode::Palette::VGA; | 
|  | 29 |  |  |  |  | 63 |  | 
|  | 29 |  |  |  |  | 946 |  | 
| 8 | 29 |  |  | 29 |  | 11948 | use Image::TextMode::SAUCE; | 
|  | 29 |  |  |  |  | 60 |  | 
|  | 29 |  |  |  |  | 13777 |  | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | =head1 NAME | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | Image::TextMode::Format - A base class for text mode file formats | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | This is a base class for all textmode formats. It provides the basic | 
| 17 |  |  |  |  |  |  | structure for reading and writing, plus provides some defaults for | 
| 18 |  |  |  |  |  |  | common attributes (e.g. font and palette). | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | =head1 ACCESSORS | 
| 21 |  |  |  |  |  |  |  | 
| 22 |  |  |  |  |  |  | =over 4 | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | =item * reader - an instance of a file reader | 
| 25 |  |  |  |  |  |  |  | 
| 26 |  |  |  |  |  |  | =item * writer - and instance of a file writer | 
| 27 |  |  |  |  |  |  |  | 
| 28 |  |  |  |  |  |  | =item * font - a font instance | 
| 29 |  |  |  |  |  |  |  | 
| 30 |  |  |  |  |  |  | =item * palette - a palette instance | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  | =item * sauce - a SAUCE metadata object | 
| 33 |  |  |  |  |  |  |  | 
| 34 |  |  |  |  |  |  | =item * render_options - default options for use when rasterizing the data | 
| 35 |  |  |  |  |  |  |  | 
| 36 |  |  |  |  |  |  | =back | 
| 37 |  |  |  |  |  |  |  | 
| 38 |  |  |  |  |  |  | =cut | 
| 39 |  |  |  |  |  |  |  | 
| 40 |  |  |  |  |  |  | has 'reader' => ( | 
| 41 |  |  |  |  |  |  | is         => 'lazy', | 
| 42 |  |  |  |  |  |  | isa        => InstanceOf['Image::TextMode::Reader'], | 
| 43 |  |  |  |  |  |  | ); | 
| 44 |  |  |  |  |  |  |  | 
| 45 |  |  |  |  |  |  | has 'writer' => ( | 
| 46 |  |  |  |  |  |  | is         => 'lazy', | 
| 47 |  |  |  |  |  |  | isa        => InstanceOf['Image::TextMode::Writer'], | 
| 48 |  |  |  |  |  |  | ); | 
| 49 |  |  |  |  |  |  |  | 
| 50 |  |  |  |  |  |  | sub _build_reader { | 
| 51 | 38 |  |  | 38 |  | 11599 | my ( $self ) = @_; | 
| 52 | 38 |  |  |  |  | 203 | return $self->_xs_or_not( 'Reader' ); | 
| 53 |  |  |  |  |  |  | } | 
| 54 |  |  |  |  |  |  |  | 
| 55 |  |  |  |  |  |  | sub _build_writer { | 
| 56 | 7 |  |  | 7 |  | 4592 | my ( $self ) = @_; | 
| 57 | 7 |  |  |  |  | 39 | return $self->_xs_or_not( 'Writer' ); | 
| 58 |  |  |  |  |  |  | } | 
| 59 |  |  |  |  |  |  |  | 
| 60 |  |  |  |  |  |  | sub _xs_or_not { | 
| 61 | 45 |  |  | 45 |  | 121 | my ( $class, $type ) = @_; | 
| 62 | 45 |  | 33 |  |  | 424 | ( my $name = ( ref $class || $class ) ) =~ s{\bFormat\b}{$type}s; | 
| 63 |  |  |  |  |  |  |  | 
| 64 | 45 | 100 |  |  |  | 264 | unless ( $ENV{ IMAGE_TEXTMODE_NOXS } ) { | 
| 65 | 20 |  |  |  |  | 56 | my $xs = $name . '::XS'; | 
| 66 | 20 |  |  |  |  | 36 | my $result = eval { Module::Runtime::require_module( $xs ); }; | 
|  | 20 |  |  |  |  | 96 |  | 
| 67 | 20 | 50 | 33 |  |  | 5331 | if ( $result && !$@ ) { return $xs->new; } | 
|  | 0 |  |  |  |  | 0 |  | 
| 68 |  |  |  |  |  |  | } | 
| 69 |  |  |  |  |  |  |  | 
| 70 | 45 |  |  |  |  | 195 | Module::Runtime::require_module( $name ); | 
| 71 | 45 |  |  |  |  | 1160 | return $name->new; | 
| 72 |  |  |  |  |  |  | } | 
| 73 |  |  |  |  |  |  |  | 
| 74 |  |  |  |  |  |  | has 'font' => ( | 
| 75 |  |  |  |  |  |  | is      => 'rw', | 
| 76 |  |  |  |  |  |  | isa     => Object, | 
| 77 |  |  |  |  |  |  | default => sub { Image::TextMode::Font::8x16->new } | 
| 78 |  |  |  |  |  |  | ); | 
| 79 |  |  |  |  |  |  |  | 
| 80 |  |  |  |  |  |  | has 'palette' => ( | 
| 81 |  |  |  |  |  |  | is      => 'rw', | 
| 82 |  |  |  |  |  |  | isa     => Object, | 
| 83 |  |  |  |  |  |  | default => sub { Image::TextMode::Palette::VGA->new } | 
| 84 |  |  |  |  |  |  | ); | 
| 85 |  |  |  |  |  |  |  | 
| 86 |  |  |  |  |  |  | has 'sauce' => ( | 
| 87 |  |  |  |  |  |  | is      => 'rw', | 
| 88 |  |  |  |  |  |  | isa     => Object, | 
| 89 |  |  |  |  |  |  | default => sub { Image::TextMode::SAUCE->new }, | 
| 90 |  |  |  |  |  |  | handles => [ qw( author title group has_sauce ) ] | 
| 91 |  |  |  |  |  |  | ); | 
| 92 |  |  |  |  |  |  |  | 
| 93 |  |  |  |  |  |  | has 'render_options' => | 
| 94 |  |  |  |  |  |  | ( is => 'rw', isa => HashRef, default => sub { {} } ); | 
| 95 |  |  |  |  |  |  |  | 
| 96 |  |  |  |  |  |  | =head1 METHODS | 
| 97 |  |  |  |  |  |  |  | 
| 98 |  |  |  |  |  |  | =head2 new( %args ) | 
| 99 |  |  |  |  |  |  |  | 
| 100 |  |  |  |  |  |  | Creates a new instance. | 
| 101 |  |  |  |  |  |  |  | 
| 102 |  |  |  |  |  |  | =head2 read( $file, \%options ) | 
| 103 |  |  |  |  |  |  |  | 
| 104 |  |  |  |  |  |  | Proxies to the reader's C method. | 
| 105 |  |  |  |  |  |  |  | 
| 106 |  |  |  |  |  |  | =cut | 
| 107 |  |  |  |  |  |  |  | 
| 108 |  |  |  |  |  |  | sub read {    ## no critic (Subroutines::ProhibitBuiltinHomonyms) | 
| 109 | 38 |  |  | 38 | 1 | 2514 | my ( $self, @rest ) = @_; | 
| 110 |  |  |  |  |  |  |  | 
| 111 |  |  |  |  |  |  | # Work-around RT#99225 until we switch to proper roles | 
| 112 | 38 | 100 |  |  |  | 312 | if( $self->isa( 'Image::TextMode::Canvas' ) ) { | 
| 113 | 34 |  |  |  |  | 493 | $self->pixeldata; | 
| 114 | 34 |  |  |  |  | 1161 | $self->width; | 
| 115 | 34 |  |  |  |  | 15526 | $self->height; | 
| 116 |  |  |  |  |  |  | } | 
| 117 |  |  |  |  |  |  |  | 
| 118 | 38 |  |  |  |  | 16619 | $self->reader->read( $self, @rest ); | 
| 119 |  |  |  |  |  |  | } | 
| 120 |  |  |  |  |  |  |  | 
| 121 |  |  |  |  |  |  | =head2 write( $file, \%options ) | 
| 122 |  |  |  |  |  |  |  | 
| 123 |  |  |  |  |  |  | Proxies to the writer's C method. | 
| 124 |  |  |  |  |  |  |  | 
| 125 |  |  |  |  |  |  | =cut | 
| 126 |  |  |  |  |  |  |  | 
| 127 |  |  |  |  |  |  | sub write {    ## no critic (Subroutines::ProhibitBuiltinHomonyms) | 
| 128 | 7 |  |  | 7 | 1 | 29276 | my ( $self, @rest ) = @_; | 
| 129 | 7 |  |  |  |  | 75 | $self->writer->write( $self, @rest ); | 
| 130 |  |  |  |  |  |  | } | 
| 131 |  |  |  |  |  |  |  | 
| 132 |  |  |  |  |  |  | =head1 PROXIED METHODS | 
| 133 |  |  |  |  |  |  |  | 
| 134 |  |  |  |  |  |  | The following methods are proxies to C. | 
| 135 |  |  |  |  |  |  |  | 
| 136 |  |  |  |  |  |  | =over 4 | 
| 137 |  |  |  |  |  |  |  | 
| 138 |  |  |  |  |  |  | =item * author | 
| 139 |  |  |  |  |  |  |  | 
| 140 |  |  |  |  |  |  | =item * title | 
| 141 |  |  |  |  |  |  |  | 
| 142 |  |  |  |  |  |  | =item * group | 
| 143 |  |  |  |  |  |  |  | 
| 144 |  |  |  |  |  |  | =item * has_sauce | 
| 145 |  |  |  |  |  |  |  | 
| 146 |  |  |  |  |  |  | =back | 
| 147 |  |  |  |  |  |  |  | 
| 148 |  |  |  |  |  |  | =cut | 
| 149 |  |  |  |  |  |  |  | 
| 150 |  |  |  |  |  |  | =head1 AUTHOR | 
| 151 |  |  |  |  |  |  |  | 
| 152 |  |  |  |  |  |  | Brian Cassidy Ebricas@cpan.orgE | 
| 153 |  |  |  |  |  |  |  | 
| 154 |  |  |  |  |  |  | =head1 COPYRIGHT AND LICENSE | 
| 155 |  |  |  |  |  |  |  | 
| 156 |  |  |  |  |  |  | Copyright 2008-2014 by Brian Cassidy | 
| 157 |  |  |  |  |  |  |  | 
| 158 |  |  |  |  |  |  | This library is free software; you can redistribute it and/or modify | 
| 159 |  |  |  |  |  |  | it under the same terms as Perl itself. | 
| 160 |  |  |  |  |  |  |  | 
| 161 |  |  |  |  |  |  | =cut | 
| 162 |  |  |  |  |  |  |  | 
| 163 |  |  |  |  |  |  | 1; |