blib/lib/Document/Writer.pm

filter: Statementstmt 60.3% 67 / 111 Branchbran 34.3% 11 / 32 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 83.3% 10 / 12 Podpod 100% 5 / 5 totaltotal 58.1% 93 / 160 CC 21 SCAR 30.5
CC
21
Cov
55%
draw
38.1
find
30.0
get_tree
6.0
CRAP 21.2
↑ Summary blib/lib/Document/Writer/Page.pm »
1 package Document::Writer;
2 4 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use MooseX::AttributeHelpers;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Forest;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Paper::Specs units => 'pt';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 4 ▶use Document::Writer::Page;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 4 our $AUTHORITY = 'cpan:GPHAT';
12 4 our $VERSION = '0.13';
13  
14 has 'components' => (
15 metaclass => 'Collection::Array',
16 is => 'rw',
17 isa => 'ArrayRef[Graphics::Primitive::Component]',
18 2 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
19 4 provides => {
20 'clear'=> 'clear_components',
21 'count'=> 'component_count',
22 'get' => 'get_component',
23 'push' => 'add_component',
24 'first'=> 'first_component',
25 'last' => 'last_component'
26 }
27 );
28 4 has 'last_page' => (
29 is => 'rw',
30 isa => 'Document::Writer::Page',
31 );
32  
33 1 ▶sub draw {
Subroutine draw: called CC=13 SCAR=36.4
Pod documented
34 1 my ($self, $driver) = @_;
35  
36 1 my @pages;
37 1 foreach my $c (@{ $self->components }) {
38  
39 2 ▶ next unless(defined($c));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $c 0 2 2
40  
41 2 $driver->prepare($c);
42  
43 2 ▶ if($c->isa('Document::Writer::Page')) {
Branch: 3 branches
BranchTrueFalseTotal
if ($c->isa('Document::Writer::Page')) { } 1 1 2
elsif ($c->isa('Document::Writer::TextArea')) { } 1 0 1
elsif ($c->isa('Graphics::Primitive::Container')) { } 0 0 0
44 1 $c->layout_manager->do_layout($c);
45 1 push(@pages, $c);
46 } elsif($c->isa('Document::Writer::TextArea')) {
47 1 my $page = $self->last_page;
48  
49 1 my $avail = $page->body->inside_height - $page->body->layout_manager->used->[1];
50 1 $c->width($page->body->inside_width);
51  
52 1 my $tl = $driver->get_textbox_layout($c);
53  
54 1 my $tlh = $tl->height;
55 1 my $used = 0;
56  
57 # This is around to keep control of runaway page adding. We
58 # should never end up with an empty page.
59 1 my $newpage = 0;
60 # So. We need to 'use' all of the TextLayout we got. The height
61 # is $tlh and we have $avail space available on the page.
62 1 ▶ while($used < $tlh) {
Branch: 1 branch
BranchTrueFalseTotal
if ($used < $tlh) 0 1 1
63 # We've not yet used all of the TextLayout...
64 0 ▶ if($avail <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($avail <= 0) { } 0 0 0
65  
66 # Stop runaway page adding.
67 0 ▶ if($newpage >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($newpage >= 1) 0 0 0
68 0 last;
69 }
70  
71 # But we ran out of available space. So we need to add a
72 # new page. We do this at the top so that we don't add
73 # a new page on the last iteration and then never use it!
74 0 $page = $self->add_page_break($driver);
75 0 $avail = $page->body->inside_height - $page->body->layout_manager->used->[1];
76 0 $newpage += 1;
77 } else {
78 0 $newpage = 0;
79 }
80  
81 # Ask the TL to slice off a chunk. Ask it for however much
82 # space we have available ($avail). If the TL doesn't have
83 # that much, it will give us all it has. If it has more, we'll
84 # re-loop.
85 0 my $new_ta = $tl->slice($used, $avail);
86  
87 0 ▶ if(defined($new_ta)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $new_ta) { } 0 0 0
88 0 $used += $new_ta->height;
89 # Add whatever we got to the page body.
90 0 $page->body->add_component($new_ta, 'n');
91 # Relayout the page.
92 0 $page->layout_manager->do_layout($page);
93 # Get the new avail
94 0 $avail = $page->body->inside_height - $page->body->layout_manager->used->[1];
95 } else {
96 0 $avail = 0;
97 }
98 }
99  
100 } elsif($c->isa('Graphics::Primitive::Container')) {
101  
102 0 my $page = $self->last_page;
103 0 my $avail = $page->body->inside_height - $page->body->layout_manager->used->[1];
104  
105 0 $driver->prepare($c);
106 0 ▶ if($avail < $c->minimum_height) {
Branch: 1 branch
BranchTrueFalseTotal
if ($avail < $c->minimum_height) 0 0 0
107 0 $page = $self->add_page_break($driver);
108 0 $avail = $page->body->inside_height;
109 }
110 0 $page->body->add_component($c, 'n');
111 0 $page->layout_manager->do_layout($page);
112 }
113 }
114  
115 1 foreach my $p (@pages) {
116 # Prepare all the pages...
117 1 $driver->prepare($p);
118 # Layout each page...
119  
120 1 ▶ if($p->layout_manager) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->layout_manager) 1 0 1
121 1 $p->layout_manager->do_layout($p);
122 1 $p->body->layout_manager->do_layout($p->body);
123 }
124 1 $driver->finalize($p);
125 1 $driver->reset;
126 1 $driver->draw($p);
127 }
128  
129 1 return \@pages;
130 }
131  
132 0 ▶sub find {
Subroutine find: not called CC=5 SCAR=34.0
Pod documented
133 0 my ($self, $predicate) = @_;
134  
135 0 my $newlist = Graphics::Primitive::ComponentList->new;
136 0 foreach my $c (@{ $self->components }) {
137  
138 0 ▶ return unless(defined($c));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $c 0 0 0
139  
140 0 ▶ unless($c->can('components')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($c->can("components")) 0 0 0
141 0 return $newlist;
142 }
143 0 my $list = $c->find($predicate);
144 0 ▶ if(scalar(@{ $list->components })) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$list->components;}) 0 0 0
145 0 $newlist->push_components(@{ $list->components });
146 0 $newlist->push_constraints(@{ $list->constraints });
147 }
148 }
149  
150 0 return $newlist;
151 }
152  
153 2 ▶sub get_paper_dimensions {
Subroutine get_paper_dimensions: called CC=2 SCAR=7.4
Pod documented
154 2 my ($self, $name) = @_;
155  
156 2 my $form = Paper::Specs->find(brand => 'standard', code => uc($name));
157 2 ▶ if(defined($form)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $form) { } 2 0 2
158 2 return $form->sheet_size;
159 } else {
160 0 return (undef, undef);
161 }
162 }
163  
164 0 ▶sub get_tree {
Subroutine get_tree: not called CC=2 SCAR=17.9
Pod documented
165 0 my ($self) = @_;
166  
167 0 my $tree = Forest::Tree->new(node => $self);
168  
169 0 foreach my $c (@{ $self->components }) {
170 0 $tree->add_child($c->get_tree);
171 }
172  
173 0 return $tree;
174 }
175  
176 5 ▶sub add_page_break {
Subroutine add_page_break: called CC=3 SCAR=11.0
Pod documented
177 5 my ($self, $driver, $page) = @_;
178  
179 5 my $newpage;
180 5 ▶ if(defined($page)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $page) { } 3 2 5
181 3 $newpage = $page;
182 } else {
183 2 ▶ die('Must add a first page to create implicit ones') unless defined($self->last_page);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->last_page 1 1 2
184 1 my $last = $self->last_page;
185 1 $newpage = Document::Writer::Page->new(
186 color => $last->color,
187 width => $last->width,
188 height => $last->height,
189 );
190 }
191  
192 4 $driver->prepare($newpage);
193 4 $newpage->layout_manager->do_layout($newpage);
194  
195 4 $self->add_component($newpage);
196 4 $self->last_page($newpage);
197 4 return $newpage;
198 }
199  
200 4 __PACKAGE__->meta->make_immutable;
201  
202 4 1;
203 __END__
↑ Summary blib/lib/Document/Writer/Page.pm »