File Coverage

lib/PDF/Boxer/Role/SizePosition.pm
Criterion Covered Total %
statement 34 107 31.7
branch 3 58 5.1
condition 1 96 1.0
subroutine 13 37 35.1
pod 1 14 7.1
total 52 312 16.6


line stmt bran cond sub pod time code
1             package PDF::Boxer::Role::SizePosition;
2             {
3             $PDF::Boxer::Role::SizePosition::VERSION = '0.004';
4             }
5 4     4   515025 use Moose::Role;
  4         18670  
  4         27  
6             # ABSTRACT: size and position stuff
7              
8 4     4   23196 use Carp qw(carp croak confess cluck);
  4         8  
  4         10659  
9              
10             requires qw!margin border padding children!;
11              
12             has 'max_width' => ( isa => 'Int', is => 'rw' );
13             has 'max_height' => ( isa => 'Int', is => 'rw' );
14              
15             has 'width' => ( isa => 'Maybe[Int]', is => 'rw', lazy_build => 1, );
16             has 'height' => ( isa => 'Maybe[Int]', is => 'rw', lazy_build => 1, );
17              
18             has 'margin_left' => ( isa => 'Maybe[Int]', is => 'rw', lazy_build => 1 );
19             has 'margin_top' => ( isa => 'Maybe[Int]', is => 'rw', lazy_build => 1 );
20              
21             has 'margin_right' => ( isa => 'Int', is => 'rw', lazy_build => 1 );
22             has 'margin_bottom' => ( isa => 'Int', is => 'rw', lazy_build => 1 );
23              
24             has 'border_left' => ( isa => 'Int', is => 'ro', lazy_build => 1 );
25             has 'border_top' => ( isa => 'Int', is => 'ro', lazy_build => 1 );
26              
27             has 'padding_left' => ( isa => 'Int', is => 'ro', lazy_build => 1 );
28             has 'padding_top' => ( isa => 'Int', is => 'ro', lazy_build => 1 );
29              
30             has 'content_left' => ( isa => 'Int', is => 'ro', lazy_build => 1 );
31             has 'content_top' => ( isa => 'Int', is => 'ro', lazy_build => 1 );
32              
33             has 'content_right' => ( isa => 'Int', is => 'rw', lazy_build => 1 );
34             has 'content_bottom' => ( isa => 'Int', is => 'rw', lazy_build => 1 );
35              
36             has 'margin_width' => ( isa => 'Int', is => 'rw', lazy_build => 1 );
37             has 'margin_height' => ( isa => 'Int', is => 'rw', lazy_build => 1 );
38              
39             has 'border_width' => ( isa => 'Int', is => 'rw', lazy_build => 1 );
40             has 'border_height' => ( isa => 'Int', is => 'rw', lazy_build => 1 );
41              
42             has 'padding_width' => ( isa => 'Int', is => 'rw', lazy_build => 1 );
43             has 'padding_height' => ( isa => 'Int', is => 'rw', lazy_build => 1 );
44              
45              
46             has 'grow' => ( isa => 'Bool', is => 'ro', default => 0 );
47              
48             has 'attribute_rels' => ( isa => 'HashRef', is => 'ro', lazy_build => 1 );
49              
50             sub _build_attribute_rels{
51             return {
52 1     1   52 max_width => [qw! margin_right content_right margin_width border_width padding_width !],
53             max_height => [qw! margin_bottom content_bottom margin_height border_height padding_height!],
54             width => [qw! margin_right content_right margin_width border_width padding_width !],
55             height => [qw! margin_bottom content_bottom margin_height border_height padding_height!],
56             margin_left => [qw! margin_right border_left padding_left content_left !],
57             margin_top => [qw! margin_bottom border_top padding_top content_top content_bottom !],
58             margin_right => [qw! margin_left border_left padding_left content_left content_right !],
59             margin_bottom => [qw! margin_top border_top padding_top content_top content_bottom !],
60             margin_width => [qw! width margin_right content_right border_width padding_width !],
61             margin_height => [qw! height margin_bottom content_bottom border_height padding_height !],
62             }
63             }
64              
65              
66             sub adjust{
67 7     7 1 17 my ($self, $spec, $sender) = @_;
68              
69 7         25 foreach my $attr (keys %$spec){
70 7         274 $self->$attr($spec->{$attr});
71 7         12 foreach ( @{$self->attribute_rels->{$attr}} ){
  7         248  
72 34 50       96 next if $spec->{$_}; # don't clear anything which is in the spec
73 34         54 my $clear = 'clear_'.$_;
74 34         1509 $self->$clear();
75             }
76             }
77              
78             }
79              
80             sub move{
81 0     0 0 0 my ($self, $x, $y) = @_;
82             return if
83 0 0 0     0 ($self->margin_left && $self->margin_left == $x)
      0        
      0        
84             && ($self->margin_top && $self->margin_top == $y);
85 0 0 0     0 warn $self->name." move $x, $y from ".join('-',(caller)[0,2])."\n" if $self->debug && $self->name;
86 0         0 $self->adjust({ margin_left => $x, margin_top => $y });
87             }
88              
89             sub set_width{
90 0     0 0 0 my ($self, $arg) = @_;
91 0 0 0     0 return if $self->width && $self->width == $arg;
92 0 0 0     0 warn $self->name." set width $arg from ".join('-',(caller)[0,2])."\n" if $self->debug && $self->name;
93 0         0 $self->adjust({ width => $arg });
94             }
95              
96             sub set_margin_width{
97 0     0 0 0 my ($self, $arg) = @_;
98 0 0 0     0 return if $self->margin_width && $self->margin_width == $arg;
99 0 0 0     0 warn $self->name." set margin_width $arg from ".join('-',(caller)[0,2])."\n" if $self->debug && $self->name;
100 0         0 $self->adjust({ margin_width => $arg });
101             }
102              
103             sub set_height{
104 0     0 0 0 my ($self, $arg) = @_;
105 0 0 0     0 return if $self->height && $self->height == $arg;
106 0 0 0     0 warn $self->name." set height $arg from ".join('-',(caller)[0,2])."\n" if $self->debug && $self->name;
107 0         0 $self->adjust({ height => $arg });
108             }
109              
110             sub set_margin_height{
111 0     0 0 0 my ($self, $arg) = @_;
112 0 0 0     0 return if $self->margin_height && $self->margin_height == $arg;
113 0 0 0     0 warn $self->name." set margin_height $arg from ".join('-',(caller)[0,2])."\n" if $self->debug && $self->name;
114 0         0 $self->adjust({ margin_height => $arg });
115             }
116              
117             sub set_size{
118 0     0 0 0 my ($self, $x, $y) = @_;
119             return if
120 0 0 0     0 ($self->width && $self->width == $x)
      0        
      0        
121             && ($self->height && $self->height == $y);
122 0 0 0     0 warn $self->name." size $x, $y from ".join('-',(caller)[0,2])."\n" if $self->debug && $self->name;
123 0         0 $self->adjust({ width => $x, height => $y });
124             }
125              
126             sub set_margin_size{
127 0     0 0 0 my ($self, $x, $y) = @_;
128 0 0 0     0 return if $self->margin_width == $x && $self->margin_height == $y;
129 0 0 0     0 warn $self->name." size $x, $y from ".join('-',(caller)[0,2])."\n" if $self->debug && $self->name;
130 0         0 $self->adjust({ margin_width => $x, margin_height => $y });
131             }
132              
133 0     0 0 0 sub child_height_set{};
134 0     0 0 0 sub child_width_set{};
135              
136             sub position_set{
137 0     0 0 0 my ($self) = @_;
138 0 0 0     0 return 1 if ($self->has_margin_left || $self->has_margin_right)
      0        
      0        
139             && ($self->has_margin_top || $self->has_margin_bottom);
140             }
141              
142             sub size_set{
143 0     0 0 0 my ($self) = @_;
144 0 0 0     0 return 1 if ($self->has_margin_width || $self->has_width)
      0        
      0        
145             && ($self->has_margin_height || $self->has_height);
146             }
147              
148             sub _build_margin_left{
149 1     1   2 my ($self) = @_;
150 1 50 33     43 if ($self->has_content_left){
    50 0        
    0          
151 0         0 return $self->content_left - $self->padding->[3] - $self->border->[3] - $self->margin->[3];
152             } elsif ($self->has_margin_right && $self->has_margin_width){
153 1         31 return $self->margin_right - $self->margin_width;
154             } elsif ($self->has_content_right && $self->has_content_width){
155 0         0 return $self->content_right - $self->content_width;
156             }
157 0         0 return;
158             }
159              
160             sub _build_margin_right{
161 4     4   9 my ($self) = @_;
162 4         136 return $self->margin_left + $self->margin_width;
163             }
164              
165             sub _build_margin_top{
166 0     0   0 my ($self) = @_;
167 0 0 0     0 if ($self->has_content_top){
    0 0        
    0          
168 0         0 return $self->content_top + $self->padding->[0] + $self->border->[0] + $self->margin->[0];
169             } elsif ($self->has_margin_bottom && $self->has_margin_height){
170 0         0 return $self->margin_bottom - $self->margin_height;
171             } elsif ($self->has_content_bottom && $self->has_content_height){
172 0         0 return $self->content_bottom - $self->content_height;
173             }
174 0         0 return;
175             }
176              
177             sub _build_margin_bottom{
178 4     4   7 my ($self) = @_;
179 4         140 return $self->margin_top - $self->margin_height;
180             }
181              
182             sub _build_border_left{
183 0     0   0 my ($self) = @_;
184 0         0 return $self->margin_left + $self->margin->[3];
185             }
186              
187             sub _build_border_top{
188 0     0   0 my ($self) = @_;
189 0         0 return $self->margin_top - $self->margin->[0];
190             }
191              
192             sub _build_padding_left{
193 0     0   0 my ($self) = @_;
194 0         0 return $self->border_left + $self->border->[3];
195             }
196              
197             sub _build_padding_top{
198 0     0   0 my ($self) = @_;
199 0         0 return $self->border_top - $self->border->[0];
200             }
201              
202             sub _build_content_left{
203 0     0   0 my ($self) = @_;
204 0         0 return $self->padding_left + $self->padding->[3];
205             }
206              
207             sub _build_content_top{
208 0     0   0 my ($self) = @_;
209 0         0 return $self->padding_top - $self->padding->[0];
210             }
211              
212             sub _build_content_right{
213 0     0   0 my ($self) = @_;
214 0         0 return $self->content_left + $self->width;
215             }
216              
217             sub _build_content_bottom{
218 0     0   0 my ($self) = @_;
219 0         0 return $self->content_top - $self->height;
220             }
221              
222             sub _build_width{
223 0     0   0 my ($self) = @_;
224 0 0 0     0 if ($self->has_margin_width){
    0 0        
    0          
225 0         0 return $self->margin_width - (
226             $self->padding->[3] + $self->padding->[1]
227             + $self->border->[3] + $self->border->[1]
228             + $self->margin->[3] + $self->margin->[1]
229             );
230             } elsif ($self->has_margin_left && $self->has_margin_right){
231 0         0 return $self->margin_right - $self->margin_left - (
232             $self->padding->[3] + $self->padding->[1]
233             + $self->border->[3] + $self->border->[1]
234             + $self->margin->[3] + $self->margin->[1]
235             );
236             } elsif ($self->has_content_left && $self->has_content_right){
237 0         0 return $self->content_right - $self->content_left;
238             }
239 0         0 return;
240             }
241              
242             sub _build_height{
243 0     0   0 my ($self) = @_;
244 0 0 0     0 if ($self->has_margin_height){
    0 0        
    0          
245 0         0 return $self->margin_height - (
246             $self->padding->[0] + $self->padding->[2]
247             + $self->border->[0] + $self->border->[2]
248             + $self->margin->[0] + $self->margin->[2]
249             );
250             } elsif ($self->has_margin_left && $self->has_margin_right){
251 0         0 return $self->margin_right - $self->margin_left - (
252             $self->padding->[0] + $self->padding->[2]
253             + $self->border->[0] + $self->border->[2]
254             + $self->margin->[0] + $self->margin->[2]
255             );
256             } elsif ($self->has_content_left && $self->has_content_right){
257 0         0 return $self->content_right - $self->content_left;
258             }
259 0         0 return;
260             }
261              
262 0     0 0 0 sub content_width{ shift->width(@_) }
263 0     0 0 0 sub content_height{ shift->height(@_) }
264              
265             sub _build_padding_width{
266 3     3   5 my ($self) = @_;
267 3         96 return $self->width + $self->padding->[1] + $self->padding->[3];
268             }
269              
270             sub _build_padding_height{
271 3     3   5 my ($self) = @_;
272 3         97 return $self->height + $self->padding->[0] + $self->padding->[2];
273             }
274              
275             sub _build_border_width{
276 3     3   5 my ($self) = @_;
277 3         105 return $self->padding_width + $self->border->[1] + $self->border->[3];
278             }
279              
280             sub _build_border_height{
281 3     3   6 my ($self) = @_;
282 3         106 return $self->padding_height + $self->border->[0] + $self->border->[2];
283             }
284              
285             sub _build_margin_width{
286 3     3   8 my ($self) = @_;
287 3         103 return $self->border_width + $self->margin->[1] + $self->margin->[3];
288             }
289              
290             sub _build_margin_height{
291 3     3   8 my ($self) = @_;
292 3         111 return $self->border_height + $self->margin->[0] + $self->margin->[2];
293             }
294              
295              
296             1;
297              
298              
299              
300             __END__
301             =pod
302              
303             =head1 NAME
304              
305             PDF::Boxer::Role::SizePosition - size and position stuff
306              
307             =head1 VERSION
308              
309             version 0.004
310              
311             =item adjust
312              
313             takes values for any of the predefined size and location attributes.
314             Decides what to do about it..
315              
316             =head1 AUTHOR
317              
318             Jason Galea <lecstor@cpan.org>
319              
320             =head1 COPYRIGHT AND LICENSE
321              
322             This software is copyright (c) 2012 by Jason Galea.
323              
324             This is free software; you can redistribute it and/or modify it under
325             the same terms as the Perl 5 programming language system itself.
326              
327             =cut
328