File Coverage

blib/lib/PDF/Make/Builder/Page.pm
Criterion Covered Total %
statement 68 68 100.0
branch 14 16 87.5
condition 2 3 66.6
subroutine 15 15 100.0
pod 8 11 72.7
total 107 113 94.6


line stmt bran cond sub pod time code
1             package PDF::Make::Builder::Page;
2 42     42   213 use strict;
  42         67  
  42         1213  
3 42     42   131 use warnings;
  42         49  
  42         1385  
4 42     42   152 use Object::Proto;
  42         62  
  42         3562  
5              
6             BEGIN {
7 42     42   4754 Object::Proto::define('PDF::Make::Builder::Page',
8             'page_size:Str:default(A4)',
9             'background:Str:default(#fff)',
10             'columns:Int:default(1)',
11             'column:Int:default(1)',
12             'column_y:Num:default(0)',
13             'is_rotated:Bool:default(0)',
14             'header:Any',
15             'footer:Any',
16             'padding:Num:default(20)',
17             'num:Int:required',
18             'x:Num:default(0)',
19             'y:Num:default(0)',
20             'w:Num:required',
21             'h:Num:required',
22             'canvas:Any:required',
23             'xs_page:Any:required',
24             'imported:Bool:default(0)',
25             'redactions:ArrayRef:default([])',
26             );
27 42         26958 Object::Proto::import_accessors('PDF::Make::Builder::Page', 'page_');
28             }
29              
30             my %PAGE_SIZES = (
31             A4 => [595, 842],
32             Letter => [612, 792],
33             Legal => [612, 1008],
34             A3 => [842, 1191],
35             A5 => [420, 595],
36             B5 => [499, 709],
37             Tabloid => [792, 1224],
38             );
39              
40             sub page_dimensions {
41 151     151 1 152427 my ($class_or_size) = @_;
42 151 50       368 my $size = ref $class_or_size ? $class_or_size->page_size : $class_or_size;
43 151   66     226 return @{$PAGE_SIZES{$size} // $PAGE_SIZES{A4}};
  151         756  
44             }
45              
46             sub top_y {
47 149     149 1 232 my ($self) = @_;
48 149         193 my $ph = page_h $self;
49 149         180 my $pad = page_padding $self;
50 149         224 my $hdr = page_header $self;
51 149         174 my $top = $ph - $pad;
52 149 100       273 $top -= $hdr->h if $hdr;
53 149         290 return $top;
54             }
55              
56             sub bottom_y {
57 1004     1004 1 1076 my ($self) = @_;
58 1004         1044 my $pad = page_padding $self;
59 1004         930 my $ftr = page_footer $self;
60 1004         903 my $bot = $pad;
61 1004 100       1209 $bot += $ftr->h if $ftr;
62 1004         1518 return $bot;
63             }
64              
65             sub remaining_height {
66 1     1 1 220 my ($self) = @_;
67 1         4 return $self->cursor_y - $self->bottom_y;
68             }
69              
70             sub width {
71 448     448 1 2348 my ($self) = @_;
72 448         538 my $pw = page_w $self;
73 448         474 my $pad = page_padding $self;
74 448         449 my $cols = page_columns $self;
75 448         631 my $usable = $pw - 2 * $pad;
76 448 100       679 if ($cols > 1) {
77 137         125 my $col_gap = 20;
78 137         260 return ($usable - ($cols - 1) * $col_gap) / $cols;
79             }
80 311         654 return $usable;
81             }
82              
83             sub content_x {
84 415     415 1 1004 my ($self) = @_;
85 415         496 my $pad = page_padding $self;
86 415         431 my $col = page_column $self;
87 415 100       668 if ((page_columns $self) > 1) {
88 69         70 my $col_gap = 20;
89 69         91 my $col_w = $self->width;
90 69         170 return $pad + ($col - 1) * ($col_w + $col_gap);
91             }
92 346         685 return $pad;
93             }
94              
95             sub cursor_y {
96 555     555 1 1033 my ($self) = @_;
97 555         601 my $cy = page_y $self;
98 555 100       1065 return $cy > 0 ? $cy : $self->top_y;
99             }
100              
101             sub advance_y {
102 89     89 1 160 my ($self, $amount) = @_;
103 89         170 my $current = $self->cursor_y;
104 89         252 page_y $self, $current - $amount;
105             }
106              
107             sub has_next_column {
108 21     21 0 1121 my ($self) = @_;
109 21         78 return (page_column $self) < (page_columns $self);
110             }
111              
112             sub next_column {
113 6     6 0 14 my ($self) = @_;
114 6         10 my $col = page_column $self;
115 6         11 my $cols = page_columns $self;
116 6 50       23 return 0 if $col >= $cols;
117              
118             # Save cursor for the column we're leaving (for balanced columns)
119 6 100       16 if ($col == 1) {
120 5         16 page_column_y $self, $self->cursor_y;
121             }
122              
123 6         16 page_column $self, $col + 1;
124             # Reset cursor to top of content area for the new column
125 6         14 page_y $self, $self->top_y;
126 6         18 return 1;
127             }
128              
129             sub reset_columns {
130 1     1 0 2 my ($self) = @_;
131 1         3 page_column $self, 1;
132 1         2 page_column_y $self, 0;
133 1         4 page_y $self, $self->top_y;
134             }
135              
136             1;
137              
138             __END__