File Coverage

blib/lib/PDF/Make/Builder/Page/Footer.pm
Criterion Covered Total %
statement 37 39 94.8
branch 6 8 75.0
condition 1 2 50.0
subroutine 6 6 100.0
pod 1 1 100.0
total 51 56 91.0


line stmt bran cond sub pod time code
1             package PDF::Make::Builder::Page::Footer;
2 42     42   215 use strict;
  42         63  
  42         1274  
3 42     42   146 use warnings;
  42         53  
  42         1500  
4 42     42   168 use Object::Proto;
  42         63  
  42         1676  
5 42     42   194 use PDF::Make::Builder::Page::HeaderFooterContext;
  42         56  
  42         1861  
6              
7             BEGIN {
8 42     42   1907 Object::Proto::define('PDF::Make::Builder::Page::Footer',
9             'h:Num:default(30)',
10             'padding:Num:default(20)',
11             'show_page_num:Str',
12             'page_num_text:Str:default(Page {num})',
13             'cb:CodeRef',
14             );
15 42         13553 Object::Proto::import_accessors('PDF::Make::Builder::Page::Footer');
16             }
17              
18             sub render {
19 12     12 1 36 my ($self, $builder, $page, $page_num) = @_;
20 12         37 my $canvas = $page->canvas;
21 12         23 my $pw = $page->w;
22 12         18 my $pad = padding $self;
23 12         45 my $fh = $self->h;
24              
25             # Footer region: bottom of page
26 12         18 my $footer_y = $pad + $fh;
27              
28             # Custom callback
29 12         19 my $callback = cb $self;
30 12 100       38 if ($callback) {
31 7         53 my $ctx = PDF::Make::Builder::Page::HeaderFooterContext->new(
32             builder => $builder,
33             page => $page,
34             canvas => $canvas,
35             x0 => 0,
36             y0 => $pad,
37             w => $pw,
38             h => $fh,
39             padding => $pad,
40             num => $page_num,
41             role => 'footer',
42             );
43 7         19 $callback->($self, $builder,
44             ctx => $ctx,
45             canvas => $canvas, y => $footer_y, w => $pw, h => $fh,
46             page_num => $page_num);
47             }
48              
49             # Page number
50 12         30 my $show = show_page_num $self;
51 12 100       36 if ($show) {
52 5         8 my $text = page_num_text $self;
53 5         18 $text =~ s/\{num\}/$page_num/g;
54 5         12 my $font = $builder->font;
55 5         17 my $res = $font->ensure_loaded($page->xs_page, 'normal');
56 5         8 my $font_size = 8;
57 5         10 my ($r, $g, $b) = $font->hex_to_rgb('#666');
58 5   50     12 my $tw = $font->measure_text($text) * ($font_size / ($font->size || 9));
59 5         13 my $tx;
60 5 50       23 if ($show eq 'right') {
    50          
61 0         0 $tx = $pw - $pad - $tw;
62             } elsif ($show eq 'center') {
63 5         10 $tx = ($pw - $tw) / 2;
64             } else {
65 0         0 $tx = $pad;
66             }
67 5         88 $canvas->BT
68             ->rg($r, $g, $b)
69             ->Tf($res, $font_size)
70             ->Tm(1, 0, 0, 1, $tx, $pad + 4)
71             ->Tj($text)
72             ->ET;
73             }
74             }
75              
76             1;
77              
78             __END__