File Coverage

lib/PDF/Boxer/Content/Image.pm
Criterion Covered Total %
statement 6 29 20.6
branch 0 8 0.0
condition n/a
subroutine 2 9 22.2
pod 1 1 100.0
total 9 47 19.1


line stmt bran cond sub pod time code
1             package PDF::Boxer::Content::Image;
2             {
3             $PDF::Boxer::Content::Image::VERSION = '0.001'; # TRIAL
4             }
5 3     3   22 use Moose;
  3         6  
  3         23  
6             # ABSTRACT: a box that displays an image
7              
8 3     3   18523 use namespace::autoclean;
  3         6  
  3         30  
9              
10             extends 'PDF::Boxer::Content::Box';
11              
12             has 'src' => ( isa => 'Str', is => 'ro' );
13             has 'scale' => ( isa => 'Num', is => 'ro' );
14             has 'format' => ( isa => 'Str', is => 'ro', lazy_build => 1 );
15              
16             has 'image' => ( is => 'rw', lazy_build => 1 );
17             has 'image_width' => ( isa => 'Int', is => 'rw', lazy_build => 1 );
18             has 'image_height' => ( isa => 'Int', is => 'rw', lazy_build => 1 );
19              
20             has 'align' => ( isa => 'Str', is => 'ro' );
21             has 'valign' => ( isa => 'Str', is => 'ro' );
22              
23 0     0     sub _build_pressure_width{ 1 }
24 0     0     sub _build_pressure_height{ 0 }
25              
26             sub _build_format{
27 0     0     my ($self) = @_;
28 0 0         return unless $self->src;
29 0           my ($ext) = $self->src =~ /\.([^\.]+)$/;
30 0           return $ext;
31             }
32              
33             sub _build_image{
34 0     0     my ($self) = @_;
35 0 0         die $self->src.": $!" unless -f $self->src;
36 0           my $pdf = $self->boxer->doc->pdf;
37 0           my $method = 'image_'.$self->format;
38 0           return $self->boxer->doc->pdf->$method($self->src);
39             }
40              
41             sub _build_image_width{
42 0     0     my ($self) = @_;
43 0           my $width = $self->image->width;
44 0 0         if (my $sc = $self->scale){
45 0           $width = $width * $sc / 100;
46             }
47 0           return $width;
48             }
49              
50             sub _build_image_height{
51 0     0     my ($self) = @_;
52 0           my $height = $self->image->height;
53 0 0         if (my $sc = $self->scale){
54 0           $height = $height * $sc / 100;
55             }
56 0           return $height;
57             }
58              
59              
60              
61              
62             sub get_default_size{
63 0     0 1   my ($self) = @_;
64 0           return ($self->image_width, $self->image_height);
65             }
66              
67             around 'render' => sub{
68             my ($orig, $self) = @_;
69              
70             my $img = $self->image;
71              
72             my $gfx = $self->boxer->doc->gfx;
73              
74             my $x = $self->content_left;
75             my $y = $self->content_top-$self->height;
76              
77             my @args = $self->scale ? ($self->scale/100) : ($self->image->width, $self->image->height);
78              
79             if (my $al = $self->valign){
80             if ($al eq 'top'){
81             $y = $self->content_top - $self->image_height;
82             } elsif ($al eq 'center'){
83             my $bc = $self->content_top - ($self->content_height / 2);
84             my $ic = $self->image_height / 2;
85             $y = $bc - $ic;
86             }
87             }
88              
89             if (my $al = $self->align){
90             if ($al eq 'right'){
91             $x = $self->content_right - $self->image_width;
92             } elsif ($al eq 'center'){
93             my $bc = $self->content_left + ($self->content_width / 2);
94             my $ic = $self->image_width / 2;
95             $x = $bc - $ic;
96             }
97             }
98              
99             $gfx->image($img, $x, $y, @args);
100              
101             $self->$orig();
102              
103             };
104              
105             __PACKAGE__->meta->make_immutable;
106              
107             1;
108              
109             __END__
110             =pod
111              
112             =head1 NAME
113              
114             PDF::Boxer::Content::Image - a box that displays an image
115              
116             =head1 VERSION
117              
118             version 0.001
119              
120             =head1 AUTHOR
121              
122             Jason Galea <lecstor@cpan.org>
123              
124             =head1 COPYRIGHT AND LICENSE
125              
126             This software is copyright (c) 2011 by Jason Galea.
127              
128             This is free software; you can redistribute it and/or modify it under
129             the same terms as the Perl 5 programming language system itself.
130              
131             =cut
132