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.004';
4             }
5 3     3   17 use Moose;
  3         6  
  3         29  
6             # ABSTRACT: a box that displays an image
7              
8 3     3   20289 use namespace::autoclean;
  3         9  
  3         34  
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             foreach($self->valign || ()){
80             /^top/ && do { $y = $self->content_top - $self->image_height };
81             /^cen/ && do {
82             my $bc = $self->content_top - ($self->content_height / 2);
83             my $ic = $self->image_height / 2;
84             $y = $bc - $ic;
85             };
86             }
87              
88             foreach($self->align || ()){
89             /^rig/ && do { $x = $self->content_right - $self->image_width };
90             /^cen/ && do {
91             my $bc = $self->content_left + ($self->content_width / 2);
92             my $ic = $self->image_width / 2;
93             $x = $bc - $ic;
94             };
95             }
96              
97             $gfx->image($img, $x, $y, @args);
98              
99             $self->$orig();
100              
101             };
102              
103             __PACKAGE__->meta->make_immutable;
104              
105             1;
106              
107             __END__
108             =pod
109              
110             =head1 NAME
111              
112             PDF::Boxer::Content::Image - a box that displays an image
113              
114             =head1 VERSION
115              
116             version 0.004
117              
118             =head1 AUTHOR
119              
120             Jason Galea <lecstor@cpan.org>
121              
122             =head1 COPYRIGHT AND LICENSE
123              
124             This software is copyright (c) 2012 by Jason Galea.
125              
126             This is free software; you can redistribute it and/or modify it under
127             the same terms as the Perl 5 programming language system itself.
128              
129             =cut
130