blib/lib/PDF/Make/Builder/Layout/Row.pm

filter: Statementstmt 99.0% 106 / 107 Branchbran 85.7% 36 / 42 Conditioncond 65.3% 17 / 26 MC/DCmcdc 33.3% 5 / 15 Subroutinesub 100% 11 / 11 Podpod 0.0% 0 / 2 totaltotal 86.2% 175 / 203 CC 39 SCAR 24.5
CC
39
Cov
91%
__ANON__
18.9
cell
10.3
render
10.1
CRAP 11.6
« blib/lib/PDF/Make/Builder/Layout/Cell.pm ↑ Summary blib/lib/PDF/Make/Builder/Form/Field/Button.pm »
1 package PDF::Make::Builder::Layout::Row;
2 53 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 53 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 53 ▶use Object::Proto;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 53 ▶use PDF::Make::Builder::Layout::Cell;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 53 ▶use Layout::Flex;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 53 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 53 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
10 53 Object::Proto::define('PDF::Make::Builder::Layout::Row',
11 'layout:required',
12 'height:Num',
13 'margin:Num:default(5)',
14 'gap:Num:default(0)',
15 'cells:ArrayRef:default([])',
16 );
17 53 Object::Proto::import_accessors('PDF::Make::Builder::Layout::Row');
18 }
19  
20 # A cell keeps a reference to its row and the row keeps its cells, which is a
21 # cycle: reference counting never collects either, so every laid-out row was
22 # leaked for the life of the process. A document with a table used to cost
23 # around 90KB that was never given back, which is invisible in a script and
24 # fatal in a server rendering documents all day.
25 #
26 # The back-reference is weakened rather than removed: cells genuinely need to
27 # reach their row. The slot is found by identity rather than by index so that
28 # reordering the property list in the define() above cannot silently turn this
29 # back into a leak, and t/55-layout-cycle.t checks it still bites.
30 8019 ▶sub _weaken_row_ref {
Subroutine _weaken_row_ref: called CC=4 SCAR=14.1
31 8019 my ($cell, $row) = @_;
32 8019 for my $i (0 .. $#$cell) {
33 16038 ▶ next unless ref $cell->[$i];
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cell->[$i] 8019 8019 16038
34 8019 ▶ next unless $cell->[$i] == $row;
Branch: 1 branch
BranchTrueFalseTotal
unless $cell->[$i] == $row 0 8019 8019
35 8019 Scalar::Util::weaken($cell->[$i]);
36 8019 return 1;
37 }
38 0 return 0;
39 }
40  
41 8019 ▶sub cell {
Subroutine cell: called CC=10 SCAR=23.3
Pod undocumented
42 8019 my ($self, %args) = @_;
43 8019 my $cells = cells $self;
44 my %cell_args = (
45 row => $self,
46 weight => $args{weight} // 1,
47 align => $args{align} // 'left',
48 valign => $args{valign} // 'top',
49 pad => $args{pad} // 5,
50 text_border_width => $args{text_border_width} // 0.5,
51 8019 ▶ wrap_slack => $args{wrap_slack} // 0,
Condition75%9/12
$args{"weight"} // 1
l!l
80172
$args{"align"} // "left"
l!l
188001
$args{"valign"} // "top"
l!l
08019
$args{"pad"} // 5
l!l
257994
$args{"text_border_width"} // "0.5"
l!l
08019
$args{"wrap_slack"} // 0
l!l
08019
MC/DC100%1/1
$args{"weight"} // 1
$args{"weight"}
MC/DC100%1/1
$args{"align"} // "left"
$args{"align"}
MC/DC0%0/1
$args{"valign"} // "top"
$args{"valign"}
MC/DC100%1/1
$args{"pad"} // 5
$args{"pad"}
MC/DC0%0/1
$args{"text_border_width"} // "0.5"
$args{"text_border_width"}
MC/DC0%0/1
$args{"wrap_slack"} // 0
$args{"wrap_slack"}
Truth table100%2/2
A
A= $args{"weight"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"align"}
Aresult
11
00
Truth table50%1/2
A
A= $args{"valign"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"pad"}
Aresult
11
00
Truth table50%1/2
A
A= $args{"text_border_width"}
Aresult
11
00
Truth table50%1/2
A
A= $args{"wrap_slack"}
Aresult
11
00
52 );
53 8019 ▶ $cell_args{bg} = $args{bg} if defined $args{bg};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"bg"} 11 8008 8019
54 8019 ▶ $cell_args{border} = $args{border} if defined $args{border};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"border"} 9 8010 8019
55 8019 ▶ $cell_args{text_border} = $args{text_border} if defined $args{text_border};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"text_border"} 0 8019 8019
56 8019 my $cell = PDF::Make::Builder::Layout::Cell->new(%cell_args);
57 8019 _weaken_row_ref($cell, $self);
58 8019 push @$cells, $cell;
59 8019 return $cell;
60 }
61  
62 4011 ▶sub render {
Subroutine render: called CC=10 SCAR=23.1
Pod undocumented
63 4011 my ($self, $builder, $page) = @_;
64 4011 my @cells = @{cells $self};
65 4011 ▶ return unless @cells;
Branch: 1 branch
BranchTrueFalseTotal
unless @cells 0 4011 4011
66  
67 4011 my $canvas = $page->canvas;
68 4011 my $font = $builder->font;
69 4011 my $cx = $page->content_x;
70 4011 my $total_w = $page->width;
71 4011 my $cursor = $page->cursor_y;
72 4011 my $margin = margin $self;
73 4011 my $gap = gap $self;
74  
75 16028 ▶ my $measure = sub {
Subroutine __ANON__: called CC=18 SCAR=29.4
76 16028 my ($item, $avail_w) = @_;
77 16028 my $cell = $cells[$item->{_idx}];
78 16028 my $h = 0;
79  
80 16028 for my $ci (@{$cell->content}) {
81 16018 ▶ if ($ci->{type} eq 'text') {
Branch: 2 branches
BranchTrueFalseTotal
if ($ci->{'type'} eq 'text') { } 16016 2 16018
elsif ($ci->{'type'} eq 'image') { } 2 0 2
82 16016 ▶ if ($ci->{runs}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ci->{"runs"}) 4 16012 16016
83 4 ▶ my $inner = defined $avail_w
Branch: 1 branch
BranchTrueFalseTotal
defined $avail_w ? : 2 2 4
84 ? $avail_w - 2 * $cell->pad : undef;
85 4 ▶ $h += $cell->runs_height($font, $ci,
Branch: 1 branch
BranchTrueFalseTotal
defined $inner && $inner > 1 ? : 2 2 4
Condition66%2/3
defined $inner and $inner > 1
!ll&&!rl&&r
202
MC/DC50%1/2
defined $inner and $inner > 1
defined $inner $inner > 1
Truth table66%2/3
A and B
A= defined $innerB= $inner > 1
ABresult
0X0
100
111
86 defined $inner && $inner > 1 ? $inner : 1);
87 4 next;
88 }
89 16012 my $item_font = $cell->_resolve_item_font($font, $ci);
90 16012 ▶ my $sz = $ci->{size} // $item_font->size;
Condition66%2/3
$ci->{"size"} // $item_font->size
l!l&&r!l&&!r
22159900
MC/DC0%0/2
$ci->{"size"} // $item_font->size
$ci->{"size"} $item_font->size
Truth table66%2/3
A // B
A= $ci->{"size"}B= $item_font->size
ABresult
1X1
011
000
91 # spacing rides on the slot, exactly as the renderer takes it
92 16012 ▶ my $lh = ($ci->{line_height} // $sz)
Condition33%1/3
$ci->{"line_height"} // $sz
l!l&&r!l&&!r
0160120
MC/DC0%0/2
$ci->{"line_height"} // $sz
$ci->{"line_height"} $sz
Truth table33%1/3
A // B
A= $ci->{"line_height"}B= $sz
ABresult
1X1
011
000
93 + PDF::Make::Builder::Layout::Cell::item_spacing($ci);
94  
95 16012 ▶ if (defined $avail_w) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $avail_w) { } 8006 8006 16012
96 8006 my $inner_w = $avail_w - 2 * $cell->pad;
97 8006 ▶ $inner_w = 1 if $inner_w < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $inner_w < 1 0 8006 8006
98 8006 my $slack = $cell->wrap_slack;
99 8006 my @words = split /\s+/, $ci->{text};
100 8006 my $line = '';
101 8006 ▶ my $lines = @words ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
@words ? : 8006 0 8006
102 8006 for my $word (@words) {
103 12200 ▶ my $candidate = $line eq '' ? $word : ($line . ' ' . $word);
Branch: 1 branch
BranchTrueFalseTotal
$line eq '' ? : 8006 4194 12200
104 12200 my $test = $item_font->measure_text($candidate);
105 12200 ▶ if ($test > $inner_w + $slack && $line ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($test > $inner_w + $slack and $line ne '') { } 12 12188 12200
Condition66%2/3
$test > $inner_w + $slack and $line ne ""
!ll&&!rl&&r
12188012
MC/DC50%1/2
$test > $inner_w + $slack and $line ne ""
$test > $inner_w + $slack $line ne ""
Truth table66%2/3
A and B
A= $test > $inner_w + $slackB= $line ne ""
ABresult
0X0
100
111
106 12 $lines++;
107 12 $line = $word;
108 } else {
109 12188 $line = $candidate;
110 }
111 }
112 8006 $h += $lines * $lh;
113 } else {
114 8006 $h += $lh;
115 }
116 } elsif ($ci->{type} eq 'image') {
117 2 ▶ $h += $ci->{h} // 50;
Condition50%1/2
$ci->{"h"} // 50
l!l
20
MC/DC0%0/1
$ci->{"h"} // 50
$ci->{"h"}
Truth table50%1/2
A
A= $ci->{"h"}
Aresult
11
00
118 }
119 }
120  
121 16028 return (0, $h);
122 4011 };
123  
124 4011 my @flex_items;
125 4011 for my $i (0 .. $#cells) {
126 8014 push @flex_items, {
127 grow => $cells[$i]->weight,
128 basis => 0,
129 text => '1',
130 wrap_text => 1,
131 _idx => $i,
132 };
133 }
134  
135 4011 my $cross = 10000;
136 4011 my @rects = Layout::Flex->compute(
137 main_size => $total_w,
138 cross_size => $cross,
139 align => 'start',
140 gap => $gap,
141 measure => $measure,
142 items => \@flex_items,
143 );
144  
145 4011 my $row_h = height $self;
146 4011 ▶ unless ($row_h) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($row_h) 4003 8 4011
147 4003 $row_h = 0;
148 4003 for my $i (0 .. $#rects) {
149 7999 my $h = $rects[$i][3];
150 # A cell with no content measures zero, and Layout::Flex hands a
151 # zero-height item the whole cross size back. Taken at face value
152 # that made one empty <td> a ten-thousand point row: the cursor
153 # ran off the page, clamped to the top, and every row after it
154 # drew in the same place. An empty cell contributes nothing to the
155 # row height, which is what it looks like on the page.
156 7999 ▶ $h = 0 if $h >= $cross;
Branch: 1 branch
BranchTrueFalseTotal
if $h >= $cross 8 7991 7999
157 7999 $h += 2 * $cells[$i]->pad;
158 7999 ▶ $row_h = $h if $h > $row_h;
Branch: 1 branch
BranchTrueFalseTotal
if $h > $row_h 4003 3996 7999
159 }
160 }
161  
162 4011 my $cell_y = $cursor - $row_h;
163  
164 4011 for my $i (0 .. $#cells) {
165 8014 my $cell = $cells[$i];
166 8014 my $cell_x = $cx + $rects[$i][0];
167 8014 my $cell_w = $rects[$i][2];
168  
169 8014 ▶ if (defined $cell->bg) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cell->bg) 11 8003 8014
170 11 my ($r, $g, $b) = $font->hex_to_rgb($cell->bg);
171 11 $canvas->q->rg($r, $g, $b)->re($cell_x, $cell_y, $cell_w, $row_h)->f->Q;
172 }
173  
174 8014 ▶ if (defined $cell->border) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cell->border) 9 8005 8014
175 9 my ($r, $g, $b) = $font->hex_to_rgb($cell->border);
176 9 $canvas->q->w(0.5)->RG($r, $g, $b)
177 ->re($cell_x + 0.25, $cell_y + 0.25, $cell_w - 0.5, $row_h - 0.5)->S->Q;
178 }
179  
180 8014 $cell->render_content($canvas, $font, $page,
181 $cell_x + $cell->pad, $cell_y, $cell_w - 2 * $cell->pad, $row_h);
182 }
183  
184 4011 $page->advance_y($row_h + $margin);
185 }
186  
187 53 1;
« blib/lib/PDF/Make/Builder/Layout/Cell.pm ↑ Summary blib/lib/PDF/Make/Builder/Form/Field/Button.pm »