File Coverage

blib/lib/PDF/Make/Builder/Page/Header.pm
Criterion Covered Total %
statement 38 40 95.0
branch 5 8 62.5
condition 1 2 50.0
subroutine 6 6 100.0
pod 1 1 100.0
total 51 57 89.4


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