File Coverage

blib/lib/Image/TextMode/Format.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             package Image::TextMode::Format;
2              
3 1     1   1231 use Moose;
  0            
  0            
4              
5             use Module::Runtime ();
6             use Image::TextMode::Font::8x16;
7             use Image::TextMode::Palette::VGA;
8             use Image::TextMode::SAUCE;
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 => 'ro',
42             isa => 'Image::TextMode::Reader',
43             lazy_build => 1,
44             );
45              
46             has 'writer' => (
47             is => 'ro',
48             isa => 'Image::TextMode::Writer',
49             lazy_build => 1,
50             );
51              
52             sub _build_reader {
53             my ( $self ) = @_;
54             return $self->_xs_or_not( 'Reader' );
55             }
56              
57             sub _build_writer {
58             my ( $self ) = @_;
59             return $self->_xs_or_not( 'Writer' );
60             }
61              
62             sub _xs_or_not {
63             my ( $class, $type ) = @_;
64             ( my $name = ( ref $class || $class ) ) =~ s{\bFormat\b}{$type}s;
65              
66             unless ( $ENV{ IMAGE_TEXTMODE_NOXS } ) {
67             my $xs = $name . '::XS';
68             my $result = eval { Module::Runtime::require_module( $xs ); };
69             if ( $result && !$@ ) { return $xs->new; }
70             }
71              
72             Module::Runtime::require_module( $name );
73             return $name->new;
74             }
75              
76             has 'font' => (
77             is => 'rw',
78             isa => 'Object',
79             default => sub { Image::TextMode::Font::8x16->new }
80             );
81              
82             has 'palette' => (
83             is => 'rw',
84             isa => 'Object',
85             default => sub { Image::TextMode::Palette::VGA->new }
86             );
87              
88             has 'sauce' => (
89             is => 'rw',
90             isa => 'Object',
91             default => sub { Image::TextMode::SAUCE->new },
92             handles => [ qw( author title group has_sauce ) ]
93             );
94              
95             has 'render_options' =>
96             ( is => 'rw', isa => 'HashRef', default => sub { {} } );
97              
98             =head1 METHODS
99              
100             =head2 new( %args )
101              
102             Creates a new instance.
103              
104             =head2 read( $file, \%options )
105              
106             Proxies to the reader's C<read()> method.
107              
108             =cut
109              
110             sub read { ## no critic (Subroutines::ProhibitBuiltinHomonyms)
111             my ( $self, @rest ) = @_;
112             $self->reader->read( $self, @rest );
113             }
114              
115             =head2 write( $file, \%options )
116              
117             Proxies to the writer's C<write()> method.
118              
119             =cut
120              
121             sub write { ## no critic (Subroutines::ProhibitBuiltinHomonyms)
122             my ( $self, @rest ) = @_;
123             $self->writer->write( $self, @rest );
124             }
125              
126             =head1 PROXIED METHODS
127              
128             The following methods are proxies to C<sauce>.
129              
130             =over 4
131              
132             =item * author
133              
134             =item * title
135              
136             =item * group
137              
138             =item * has_sauce
139              
140             =back
141              
142             =cut
143              
144             no Moose;
145              
146             __PACKAGE__->meta->make_immutable;
147              
148             =head1 AUTHOR
149              
150             Brian Cassidy E<lt>bricas@cpan.orgE<gt>
151              
152             =head1 COPYRIGHT AND LICENSE
153              
154             Copyright 2008-2013 by Brian Cassidy
155              
156             This library is free software; you can redistribute it and/or modify
157             it under the same terms as Perl itself.
158              
159             =cut
160              
161             1;