File Coverage

blib/lib/Khonsu/Image.pm
Criterion Covered Total %
statement 32 35 91.4
branch 6 8 75.0
condition 7 14 50.0
subroutine 5 5 100.0
pod 0 2 0.0
total 50 64 78.1


line stmt bran cond sub pod time code
1             package Khonsu::Image;
2              
3 5     5   40 use parent 'Khonsu::Ra';
  5         8  
  5         29  
4              
5             sub attributes {
6 4     4 0 11 my $a = shift;
7             return (
8             image => {$a->RW},
9             mime => {$a->RW, $a->STR},
10             valid_mime => {$a->RW, $a->HR, default => sub {{
11 4     4   47 jpeg => 'image_jpeg',
12             tiff => 'image_tiff',
13             pnm => 'image_pnm',
14             png => 'image_png',
15             gif => 'image_gif'
16             }}},
17 4         101 $a->POINTS
18             );
19             }
20              
21             sub add {
22 8     8 0 47 my ($self, $file, %attributes) = @_;
23 8 100       37 if (!$attributes{x}) {
24 2         13 $attributes{x} = $file->page->x;
25 2         13 $attributes{y} = $file->page->y;
26 2   33     10 $attributes{h} = $attributes{h} || $file->page->remaining_height();
27 2   33     10 $attributes{w} = $attributes{w} || $file->page->width();
28 2 50       13 if ($attributes{y} + $attributes{h} > $file->page->h) {
29 0         0 $file->page->next();
30 0         0 $attributes{x} = $file->page->x;
31 0         0 $attributes{y} = $file->page->y;
32             }
33             }
34 8         54 $self->set_attributes(%attributes);
35 8         44 my $type = $self->_identify_type();
36 8         64 my $image = $file->pdf->$type($self->image);
37 8         206282541 my %points = $self->get_points();
38 8         78 my $photo = $file->page->current->gfx;
39 8 100 66     3133 if ($attributes{align} && $attributes{align} eq 'center') {
40 2         16 my $single = ($file->page->w - ($file->page->padding * (2 + ($file->page->columns - 1)))) / $file->page->columns;
41 2         21 $points{x} = (($file->page->padding + $single) * ($file->page->column - 1)) + $file->page->padding + (($single - $points{w}) / 2);
42             }
43             $photo->image(
44             $image,
45             $points{x}, $file->page->h - ($points{y} + $points{h}), $points{w}, $points{h}
46 8         64 );
47 8         4969 $file->page->y($points{y} + $points{h});
48 8         80 return $file;
49             }
50              
51             sub _identify_type {
52 8     8   26 my ($self) = @_;
53 8 50 66     75 if (!$self->mime && !ref $self->image) {
54 4         12 my $reg = sprintf '\.(%s)$', join ("|", keys %{$self->valid_mime});
  4         24  
55 4         21 $self->image =~ m/$reg/;
56 4         20 my $m = $1;
57 4   50     32 $self->mime($m || 'png');
58             }
59 8         46 return $self->valid_mime->{$self->mime};
60             }
61              
62             1;