lib/PDF/Boxer/Content/Box.pm

filter: Statementstmt 17.3% 25 / 144 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 23 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 13.3% 2 / 15 Podpod 33.3% 3 / 9 totaltotal 11.7% 30 / 256 CC 42 SCAR 45.1
CC
42
Cov
11%
render
240.0
BUILDARGS
72.0
get_default_size
56.0
CRAP 91.0
« lib/PDF/Boxer/Content/Grid.pm ↑ Summary lib/PDF/Boxer/Content/Image.pm »
1 package PDF::Boxer::Content::Box;
2 {
3 3 $PDF::Boxer::Content::Box::VERSION = '0.004';
4 }
5 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 # ABSTRACT: a box
7  
8 3 ▶use Scalar::Util qw/weaken/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 has 'debug' => ( isa => 'Bool', is => 'ro', default => 0 );
11 0 ▶has 'margin' => ( isa => 'ArrayRef', is => 'ro', default => sub{ [0,0,0,0] } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
12 0 ▶has 'border' => ( isa => 'ArrayRef', is => 'ro', default => sub{ [0,0,0,0] } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
13 0 ▶has 'padding' => ( isa => 'ArrayRef', is => 'ro', default => sub{ [0,0,0,0] } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
14 0 ▶has 'children' => ( isa => 'ArrayRef', is => 'rw', default => sub{ [] } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
15  
16 3 with 'PDF::Boxer::Role::SizePosition'; #, 'PDF::Boxer::Role::BoxDev';
17  
18 3 has 'boxer' => ( isa => 'PDF::Boxer', is => 'ro' );
19 3 has 'parent' => ( isa => 'Object', is => 'ro' );
20 3 has 'name' => ( isa => 'Str', is => 'ro' );
21 3 has 'type' => ( isa => 'Str', is => 'ro', default => 'Box' );
22 3 has 'background' => ( isa => 'Str', is => 'ro' );
23 3 has 'border_color' => ( isa => 'Str', is => 'ro' );
24 3 has 'font' => ( isa => 'Str', is => 'ro', default => 'Helvetica' );
25 3 has 'align' => ( isa => 'Str', is => 'ro', default => '' );
26 3 has 'valign' => ( isa => 'Str', is => 'ro', default => '' );
27  
28  
29 0 ▶sub BUILDARGS{
Subroutine BUILDARGS: not called CC=8 SCAR=42.8
Pod documented
30 0 my ($class, $args) = @_;
31  
32 0 foreach my $attr (qw! margin border padding !){
33 0 ▶ next unless exists $args->{$attr};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args->{$attr} 0 0 0
34 0 my $arg = $args->{$attr};
35 0 ▶ if (ref($arg)){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg) { } 0 0 0
36 0 ▶ unless (ref($arg) eq 'ARRAY'){
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $arg eq "ARRAY") 0 0 0
37 0 die "Arg to $attr must be string or array reference";
38 }
39 } else {
40 0 $arg = [split(/\s+/, $arg)];
41 }
42 0 my $val = [$arg->[0]];
43 0 ▶ $val->[1] = defined $arg->[1] ? $arg->[1] : $val->[0];
Branch: 1 branch
BranchTrueFalseTotal
defined $arg->[1] ? : 0 0 0
44 0 ▶ $val->[2] = defined $arg->[2] ? $arg->[2] : $val->[0];
Branch: 1 branch
BranchTrueFalseTotal
defined $arg->[2] ? : 0 0 0
45 0 ▶ $val->[3] = defined $arg->[3] ? $arg->[3] : $val->[1];
Branch: 1 branch
BranchTrueFalseTotal
defined $arg->[3] ? : 0 0 0
46  
47 0 $args->{$attr} = $val;
48 }
49  
50 0 return $args;
51 }
52  
53 0 ▶sub BUILD{
Subroutine BUILD: not called CC=5 SCAR=34.0
Pod undocumented
54 0 my ($self) = @_;
55 0 ▶ unless($self->parent){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->parent) 0 0 0
56 0 $self->adjust({
57 margin_top => $self->boxer->max_height,
58 margin_left => 0,
59 margin_width => $self->boxer->max_width,
60 margin_height => $self->boxer->max_height,
61 },'self');
62 }
63  
64 0 foreach my $child (@{$self->children}){
65 0 $child->{boxer} = $self->boxer;
66 0 $child->{debug} = $self->debug;
67 0 ▶ $child->{font} ||= $self->font;
Condition0%0/3
$child->{"font"} ||= $self->font
l!l&&r!l&&!r
000
MC/DC0%0/2
$child->{"font"} ||= $self->font
$child->{"font"} $self->font
Truth table0%0/3
A ||= B
A= $child->{"font"}B= $self->font
ABresult
1X1
011
000
68 0 ▶ $child->{align} ||= $self->align;
Condition0%0/3
$child->{"align"} ||= $self->align
l!l&&r!l&&!r
000
MC/DC0%0/2
$child->{"align"} ||= $self->align
$child->{"align"} $self->align
Truth table0%0/3
A ||= B
A= $child->{"align"}B= $self->align
ABresult
1X1
011
000
69 0 my $weak_me = $self;
70 0 weaken($weak_me);
71 0 $child->{parent} = $weak_me;
72 0 my $class = 'PDF::Boxer::Content::'.$child->{type};
73 0 $child = $class->new($child);
74 0 $self->boxer->register_box($child);
75 }
76  
77 }
78  
79 0 ▶sub propagate{
Subroutine propagate: not called CC=4 SCAR=30.0
Pod undocumented
80 0 my ($self, $method, $args) = @_;
81 0 ▶ return unless $method;
Branch: 1 branch
BranchTrueFalseTotal
unless $method 0 0 0
82 0 my @kids = @{$self->children};
83 0 ▶ if (@kids){
Branch: 1 branch
BranchTrueFalseTotal
if (@kids) 0 0 0
84 0 foreach my $kid (@kids){
85 0 $kid->$method($args);
86 }
87 }
88 0 return @kids;
89 }
90  
91 # initialize objects with default sizes
92 # - text gets width of widest line and height of all lines (wrapped at page width)
93 # - images get their scaled size
94 # - rows get the height of their tallest child and the width of all of them
95 # - columns get the width of their widest child and the height of all of them
96 # - grids (same as columns)
97 # - box gets the width of all it's kids (wrapped at page width) and the height of the line of kids
98  
99 # if text or box are too wide they need to be resized and they're contents re-wrapped.
100 # this may result in their height increasing which needs to be communicated to their parent.
101 # the parent can then adjust itself accordingly.
102  
103  
104 0 ▶sub initialize{
Subroutine initialize: not called CC=3 SCAR=24.8
Pod documented
105 0 my ($self) = @_;
106  
107 0 my @kids = $self->propagate('initialize');
108  
109 0 ▶ $self->update unless $self->parent;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->parent 0 0 0
110  
111 # the main box should stay wide open.
112 0 ▶ return unless $self->parent;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->parent 0 0 0
113  
114 0 my ($width, $height) = $self->get_default_size;
115  
116 0 $self->set_width($width);
117 0 $self->set_height($height);
118  
119 0 return 1;
120 }
121  
122  
123 # we get our size from the children
124 0 ▶sub get_default_size{
Subroutine get_default_size: not called CC=7 SCAR=40.3
Pod documented
125 0 my ($self) = @_;
126 0 my ($width, $height) = (0,0);
127 0 my $kids = $self->children;
128 0 ▶ if (@$kids){
Branch: 1 branch
BranchTrueFalseTotal
if (@$kids) 0 0 0
129 0 my ($widest, $highest, $x, $y) = (0, 0, 0);
130 0 foreach(@$kids){
131 0 ▶ $highest = $_->margin_height if $_->margin_height > $highest;
Branch: 1 branch
BranchTrueFalseTotal
if $_->margin_height > $highest 0 0 0
132 0 ▶ if ($width + $_->margin_width > $self->boxer->max_width){
Branch: 1 branch
BranchTrueFalseTotal
if ($width + $_->margin_width > $self->boxer->max_width) { } 0 0 0
133 0 $height += $highest;
134 0 $highest = 0;
135 0 ▶ $widest = $width if $width > $widest;
Branch: 1 branch
BranchTrueFalseTotal
if $width > $widest 0 0 0
136 } else {
137 0 $width += $_->margin_width;
138 }
139 0 ▶ $width = $width ? (sort($_->margin_width,$width))[1] : $_->margin_width;
Branch: 1 branch
BranchTrueFalseTotal
$width ? : 0 0 0
140 }
141 0 $height += $highest;
142 }
143 0 return ($width, $height);
144 }
145  
146 0 ▶sub update{
Subroutine update: not called CC=1 SCAR=6.9
Pod undocumented
147 0 my ($self) = @_;
148 0 $self->update_children;
149 0 return 1;
150 }
151  
152 0 ▶sub child_adjusted_height{}
Subroutine child_adjusted_height: not called CC=1 SCAR=0.0
Pod undocumented
153  
154 0 ▶sub update_children{
Subroutine update_children: not called CC=6 SCAR=37.4
Pod undocumented
155 0 my ($self) = @_;
156 0 ▶ if ($self->position_set){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->position_set) 0 0 0
157 0 my $kids = $self->children;
158 0 ▶ if (@$kids){
Branch: 1 branch
BranchTrueFalseTotal
if (@$kids) 0 0 0
159 0 my ($highest, $x, $y) = (0, $self->content_left, $self->content_top);
160 0 foreach my $kid (@$kids){
161 0 ▶ $highest = $kid->margin_height if $kid->margin_height > $highest;
Branch: 1 branch
BranchTrueFalseTotal
if $kid->margin_height > $highest 0 0 0
162 0 ▶ if ($x + $kid->margin_width > $self->width){
Branch: 1 branch
BranchTrueFalseTotal
if ($x + $kid->margin_width > $self->width) { } 0 0 0
163 0 $kid->move($x,$y);
164 0 $y -= $highest;
165 0 $highest = 0;
166 0 $x = $self->content_left;
167 } else {
168 0 $kid->move($x,$y);
169 0 $x += $kid->margin_width;
170 }
171 }
172 }
173 }
174 }
175  
176 0 ▶sub render{
Subroutine render: not called CC=15 SCAR=54.8
Pod undocumented
177 0 my ($self) = @_;
178  
179 0 my $gfx = $self->boxer->doc->gfx;
180  
181 0 ▶ if ($self->background){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->background) 0 0 0
182 0 $gfx->fillcolor($self->background);
183 0 $gfx->rect($self->border_left, $self->border_top, $self->border_width, -$self->border_height);
184 0 $gfx->fill;
185 }
186  
187 0 ▶ if ($self->border){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->border) 0 0 0
188  
189 0 my $left = $self->border_left;
190 0 my $top = $self->border_top;
191 0 my $right = $left + $self->border_width;
192 0 my $bottom = $top - $self->border_height;
193  
194 0 ▶ $gfx->strokecolor($self->border_color || 'black');
Condition0%0/2
$self->border_color || 'black'
l!l
00
MC/DC0%0/1
$self->border_color || 'black'
$self->border_color
Truth table0%0/2
A
A= $self->border_color
Aresult
11
00
195  
196 0 ▶ if ($self->border->[0]){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->border->[0]) 0 0 0
197 0 $gfx->linewidth($self->border->[0]);
198 0 $gfx->move($left, $top);
199 0 $gfx->line($right, $top);
200 0 $gfx->stroke;
201 }
202  
203 0 ▶ if ($self->border->[1]){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->border->[1]) 0 0 0
204 0 $gfx->linewidth($self->border->[1]);
205 0 $gfx->move($right, $top);
206 0 $gfx->line($right, $bottom);
207 0 $gfx->stroke;
208 }
209  
210 0 ▶ if ($self->border->[2]){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->border->[2]) 0 0 0
211 0 $gfx->linewidth($self->border->[2]);
212 0 $gfx->move($right, $bottom);
213 0 $gfx->line($left, $bottom);
214 0 $gfx->stroke;
215 }
216  
217 0 ▶ if ($self->border->[3]){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->border->[3]) 0 0 0
218 0 $gfx->linewidth($self->border->[3]);
219 0 $gfx->move($left, $bottom);
220 0 $gfx->line($left, $top);
221 0 $gfx->stroke;
222 }
223 }
224  
225 0 ▶ if ($self->debug && $self->name &&
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug and $self->name and $self->name eq "Head" || $self->name eq "Header" || $self->name eq "Details" || $self->name eq "ContentGrid") 0 0 0
Condition0%0/15
$self->debug and $self->name and $self->name eq "Head" || $self->name eq "Header" || $self->name eq "Details" || $self->name eq "ContentGrid"
!ll&&!rl&&r
000
$self->debug and $self->name
!ll&&!rl&&r
000
$self->name eq "Head" or $self->name eq "Header" or $self->name eq "Details" or $self->name eq "ContentGrid"
l!l&&r!l&&!r
000
$self->name eq "Head" or $self->name eq "Header" or $self->name eq "Details"
l!l&&r!l&&!r
000
$self->name eq "Head" or $self->name eq "Header"
l!l&&r!l&&!r
000
MC/DC0%0/6
$self->debug and $self->name and $self->name eq "Head" || $self->name eq "Header" || $self->name eq "Details" || $self->name eq "ContentGrid"
$self->debug $self->name $self->name eq "Head" $self->name eq "Header" $self->name eq "Details" $self->name eq "ContentGrid"
Truth table0%0/7
A and B and C or D or E or F
A= $self->debugB= $self->nameC= $self->name eq "Head"D= $self->name eq "Header"E= $self->name eq "Details"F= $self->name eq "ContentGrid"
ABCDEFresult
0XXXXX0
10XXXX0
1100000
111XXX1
1101XX1
11001X1
1100011
226 ( $self->name eq 'Head'
227 || $self->name eq 'Header'
228 || $self->name eq 'Details'
229 || $self->name eq 'ContentGrid' )){
230 0 warn "Name: ".$self->name."\n";
231 0 warn sprintf "Top: %s\tRight: %s\tBottom: %s\tLeft: %s\n",
232 $self->margin_top, $self->margin_right, $self->margin_bottom, $self->margin_left;
233 }
234  
235 0 foreach(@{$self->children}){
236 0 $_->render;
237 }
238  
239 }
240  
241  
242 3 __PACKAGE__->meta->make_immutable;
243  
244 3 1;
245  
246 __END__
« lib/PDF/Boxer/Content/Grid.pm ↑ Summary lib/PDF/Boxer/Content/Image.pm »