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   35 use parent 'Khonsu::Ra';
  5         8  
  5         27  
4              
5             sub attributes {
6 4     4 0 10 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   52 jpeg => 'image_jpeg',
12             tiff => 'image_tiff',
13             pnm => 'image_pnm',
14             png => 'image_png',
15             gif => 'image_gif'
16             }}},
17 4         125 $a->POINTS
18             );
19             }
20              
21             sub add {
22 8     8 0 49 my ($self, $file, %attributes) = @_;
23 8 100       38 if (!$attributes{x}) {
24 2         10 $attributes{x} = $file->page->x;
25 2         16 $attributes{y} = $file->page->y;
26 2   33     21 $attributes{h} = $attributes{h} || $file->page->remaining_height();
27 2   33     11 $attributes{w} = $attributes{w} || $file->page->width();
28 2 50       14 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         69 $self->set_attributes(%attributes);
35 8         72 my $type = $self->_identify_type();
36 8         59 my $image = $file->pdf->$type($self->image);
37 8         206446752 my %points = $self->get_points();
38 8         78 my $photo = $file->page->current->gfx;
39 8 100 66     2986 if ($attributes{align} && $attributes{align} eq 'center') {
40 2         15 my $single = ($file->page->w - ($file->page->padding * (2 + ($file->page->columns - 1)))) / $file->page->columns;
41 2         18 $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         4839 $file->page->y($points{y} + $points{h});
48 8         68 return $file;
49             }
50              
51             sub _identify_type {
52 8     8   27 my ($self) = @_;
53 8 50 66     54 if (!$self->mime && !ref $self->image) {
54 4         27 my $reg = sprintf '\.(%s)$', join ("|", keys %{$self->valid_mime});
  4         25  
55 4         26 $self->image =~ m/$reg/;
56 4         21 my $m = $1;
57 4   50     36 $self->mime($m || 'png');
58             }
59 8         46 return $self->valid_mime->{$self->mime};
60             }
61              
62             1;