blib/lib/PDF/Make/Markup/Build.pm

filter: Statementstmt 96.3% 265 / 275 Branchbran 73.1% 136 / 186 Conditioncond 60.0% 45 / 75 MC/DCmcdc 34.0% 15 / 44 Subroutinesub 100% 23 / 23 Podpod 100% 1 / 1 totaltotal 80.3% 485 / 604 CC 135 SCAR 26.8
CC
135
Cov
83%
_block
33.3
_add_cell_item
19.8
build
14.7
CRAP 14.6
« blib/lib/PDF/Make/Builder.pm ↑ Summary blib/lib/PDF/Make/Builder/TOC.pm »
1 package PDF::Make::Markup::Build;
2 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use PDF::Make::Builder;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use PDF::Make::Markup::Style;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 6 our $VERSION = '0.15';
9  
10 6 my $S = 'PDF::Make::Markup::Style';
11  
12 # What each element may contain.
13 #
14 # block - other blocks: rows, tables, paragraphs
15 # inline - text and the inline styling tags
16 # rows - <tr> only
17 # cells - <cell>, <th>, <td> only
18 # none - nothing
19 #
20 # Checked rather than assumed: <text><row/></text> is a mistake worth an
21 # exception with a line number, and the alternative is a builder call that
22 # fails somewhere far away with no idea which template line caused it.
23 6 my %CONTENT = (
24 doc => 'block', page => 'block', box => 'block',
25 header => 'block', footer => 'block',
26 cell => 'inline', th => 'inline', td => 'inline',
27 h1 => 'inline', h2 => 'inline', h3 => 'inline',
28 h4 => 'inline', h5 => 'inline', h6 => 'inline',
29 p => 'inline', text => 'inline',
30 b => 'inline', i => 'inline', span => 'inline',
31 row => 'cells', table => 'rows', tr => 'cells',
32 hr => 'none', pagebreak => 'none', img => 'none',
33 style => 'none', bookmark => 'none',
34 );
35  
36 6 my %IS_HEADING = map { $_ => 1 } qw(h1 h2 h3 h4 h5 h6);
37 6 my %IS_CELL = map { $_ => 1 } qw(cell th td);
38  
39 # What a tag IS, which is not what %CONTENT says a tag may HOLD. <h1> holds
40 # inline content and is itself a block; only these three are inline-level.
41 # Reading %CONTENT as the former is what made every block inside a <box>
42 # flow onto one line.
43 6 my %IS_INLINE = map { $_ => 1 } qw(b i span);
44  
45 7 ▶sub _err {
Subroutine _err: called CC=3 SCAR=12.5
46 7 my ($node, $fmt, @args) = @_;
47 my $where = $node && defined $node->{line}
48 7 ▶ ? sprintf(' at line %d, column %d', $node->{line}, $node->{col}) : '';
Branch: 1 branch
BranchTrueFalseTotal
$node && defined $node->{'line'} ? : 7 0 7
Condition33%1/3
$node and defined $node->{"line"}
!ll&&!rl&&r
007
MC/DC0%0/2
$node and defined $node->{"line"}
$node defined $node->{"line"}
Truth table33%1/3
A and B
A= $nodeB= defined $node->{"line"}
ABresult
0X0
100
111
49 7 Carp::croak(sprintf($fmt, @args) . $where);
50 }
51  
52 260 ▶sub _kids { return @{ $_[0]{children} || [] } }
Subroutine _kids: called CC=2 SCAR=7.2
Condition50%1/2
$_[0]{'children'} || []
l!l
2600
MC/DC0%0/1
$_[0]{'children'} || []
$_[0]{'children'}
Truth table50%1/2
A
A= $_[0]{'children'}
Aresult
11
00
53 101 ▶sub _elems { return grep { $_->{kind} eq 'elem' } _kids($_[0]) }
Subroutine _elems: called CC=1 SCAR=0.0
54  
55 # ---------------------------------------------------------------------------
56 # Inline runs
57 # ---------------------------------------------------------------------------
58  
59 # Flatten a text-bearing element's children into styled runs. Nested inline
60 # tags contribute their own style; a block tag in here is an error.
61 67 ▶sub _runs {
Subroutine _runs: called CC=8 SCAR=20.8
62 67 my ($class, $node, $style, $out) = @_;
63 67 ▶ $out ||= [];
Condition100%2/2
$out ||= []
l!l
958
MC/DC100%1/1
$out ||= []
$out
Truth table100%2/2
A
A= $out
Aresult
11
00
64  
65 67 for my $c (_kids($node)) {
66 79 ▶ if ($c->{kind} eq 'text') {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->{"kind"} eq "text") 70 9 79
67 70 push @$out, { %{ $S->font_args($style) }, text => $c->{text} };
68 70 next;
69 }
70 9 my $tag = $c->{tag};
71 _err($c, "<%s> cannot appear inside <%s>, which holds text", $tag,
72 $node->{tag})
73 9 ▶ unless ($CONTENT{$tag} || '') eq 'inline';
Branch: 1 branch
BranchTrueFalseTotal
unless ($CONTENT{$tag} || "") eq "inline" 1 8 9
Condition50%1/2
$CONTENT{$tag} || ''
l!l
90
MC/DC0%0/1
$CONTENT{$tag} || ''
$CONTENT{$tag}
Truth table50%1/2
A
A= $CONTENT{$tag}
Aresult
11
00
74  
75 8 my $own = $S->attrs($c);
76 8 ▶ $own->{bold} = 1 if $tag eq 'b';
Branch: 1 branch
BranchTrueFalseTotal
if $tag eq "b" 4 4 8
77 8 ▶ $own->{italic} = 1 if $tag eq 'i';
Branch: 1 branch
BranchTrueFalseTotal
if $tag eq "i" 2 6 8
78 8 $class->_runs($c, $S->inherit($style, $own), $out);
79 }
80 66 return $out;
81 }
82  
83 # Text arguments for a block: a plain string when nothing inside it is
84 # styled, runs when something is. The plain case matters - it keeps the
85 # single-font path in Builder::Text, whose output existing documents depend
86 # on, and skips the run machinery entirely.
87 51 ▶sub _text_args {
Subroutine _text_args: called CC=7 SCAR=19.5
88 51 my ($class, $node, $style) = @_;
89 51 my $runs = $class->_runs($node, $style);
90 50 ▶ return (text => '') unless @$runs;
Branch: 1 branch
BranchTrueFalseTotal
unless @$runs 0 50 50
91  
92 50 my $styled = 0;
93 50 for my $r (@$runs) {
94 55 ▶ $styled = 1, last if $r->{bold} || $r->{italic};
Branch: 1 branch
BranchTrueFalseTotal
if $r->{"bold"} or $r->{"italic"} 4 51 55
Condition100%3/3
$r->{"bold"} or $r->{"italic"}
l!l&&r!l&&!r
3151
MC/DC100%2/2
$r->{"bold"} or $r->{"italic"}
$r->{"bold"} $r->{"italic"}
Truth table100%3/3
A or B
A= $r->{"bold"}B= $r->{"italic"}
ABresult
1X1
011
000
95 }
96 50 ▶ if (!$styled && @$runs == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $styled and @$runs == 1) 45 5 50
Condition100%3/3
not $styled and @$runs == 1
!ll&&!rl&&r
4145
MC/DC100%2/2
not $styled and @$runs == 1
not $styled @$runs == 1
Truth table100%3/3
not(A) and B
A= not $styledB= @$runs == 1
ABresult
0X0
100
111
97 45 return (text => $runs->[0]{text}, font => $S->font_args($style));
98 }
99 5 return (runs => $runs, font => $S->font_args($style));
100 }
101  
102 # Block-level arguments shared by the text blocks.
103 50 ▶sub _block_args {
Subroutine _block_args: called CC=7 SCAR=21.0
104 50 my ($class, $style) = @_;
105 50 my %a;
106 50 ▶ $a{align} = $style->{align} if defined $style->{align};
Branch: 1 branch
BranchTrueFalseTotal
if defined $style->{"align"} 0 50 50
107 50 ▶ $a{indent} = $style->{indent} if defined $style->{indent};
Branch: 1 branch
BranchTrueFalseTotal
if defined $style->{"indent"} 0 50 50
108 50 ▶ $a{spacing} = $style->{spacing} if defined $style->{spacing};
Branch: 1 branch
BranchTrueFalseTotal
if defined $style->{"spacing"} 0 50 50
109 50 ▶ $a{margin} = $style->{margin} if defined $style->{margin};
Branch: 1 branch
BranchTrueFalseTotal
if defined $style->{"margin"} 0 50 50
110 50 ▶ $a{padding} = $style->{pad} if defined $style->{pad};
Branch: 1 branch
BranchTrueFalseTotal
if defined $style->{"pad"} 0 50 50
111 $a{preformatted} = $style->{preformatted}
112 50 ▶ if defined $style->{preformatted};
Branch: 1 branch
BranchTrueFalseTotal
if defined $style->{"preformatted"} 0 50 50
113 50 return %a;
114 }
115  
116 # ---------------------------------------------------------------------------
117 # Blocks
118 # ---------------------------------------------------------------------------
119  
120 84 ▶sub _block {
Subroutine _block: called CC=29 SCAR=35.0
121 84 my ($class, $pdf, $node, $style) = @_;
122 84 my $tag = $node->{tag};
123  
124 # <style> carries tag names as attributes, so it is not validated the way
125 # every other element is; _configure has already read it.
126 84 ▶ return if $tag eq 'style';
Branch: 1 branch
BranchTrueFalseTotal
if $tag eq "style" 3 81 84
127  
128 81 my $own = $S->attrs($node);
129 80 my $st = $S->inherit($style, $own);
130  
131 80 ▶ if ($IS_HEADING{$tag}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($IS_HEADING{$tag}) 8 72 80
132 8 my $m = "add_$tag";
133 8 $pdf->$m($class->_text_args($node, $st), $class->_block_args($st));
134 8 return;
135 }
136  
137 72 ▶ if ($tag eq 'p' || $tag eq 'text') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag eq "p" or $tag eq "text") 43 29 72
Condition100%3/3
$tag eq "p" or $tag eq "text"
l!l&&r!l&&!r
232029
MC/DC100%2/2
$tag eq "p" or $tag eq "text"
$tag eq "p" $tag eq "text"
Truth table100%3/3
A or B
A= $tag eq "p"B= $tag eq "text"
ABresult
1X1
011
000
138 43 $pdf->add_text($class->_text_args($node, $st),
139 $class->_block_args($st));
140 42 return;
141 }
142  
143 29 ▶ if ($tag eq 'pagebreak') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag eq "pagebreak") 3 26 29
144 3 $pdf->add_page($class->_page_args($pdf, {}));
145 3 return;
146 }
147  
148 26 ▶ if ($tag eq 'page') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag eq "page") 2 24 26
149 2 $pdf->add_page($class->_page_args($pdf, $own));
150 2 $class->_children($pdf, $node, $st);
151 2 return;
152 }
153  
154 24 ▶ if ($tag eq 'hr') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag eq "hr") 2 22 24
155 2 my $page = $pdf->page;
156 2 my $y = $page->cursor_y;
157 2 my $x = $page->content_x;
158 2 ▶ my $w = defined $st->{width} ? $st->{width} : $page->width;
Branch: 1 branch
BranchTrueFalseTotal
defined $st->{'width'} ? : 0 2 2
159 $pdf->add_line(
160 x => $x, y => $y, ex => $x + $w, ey => $y,
161 2 ▶ fill_colour => (defined $st->{colour} ? $st->{colour} : '#000000'),
Branch: 1 branch
BranchTrueFalseTotal
defined $st->{'colour'} ? : 0 2 2
162 );
163 2 ▶ my $gap = defined $st->{spacing} ? $st->{spacing} : 6;
Branch: 1 branch
BranchTrueFalseTotal
defined $st->{'spacing'} ? : 0 2 2
164 2 $page->y($y - $gap);
165 2 return;
166 }
167  
168 22 ▶ if ($tag eq 'img') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag eq "img") 1 21 22
169 1 ▶ _err($node, '<img> needs a src') unless defined $own->{src};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $own->{"src"} 1 0 1
170 0 my %a = (image => $own->{src});
171 0 ▶ $a{w} = $own->{width} if defined $own->{width};
Branch: 1 branch
BranchTrueFalseTotal
if defined $own->{"width"} 0 0 0
172 0 ▶ $a{h} = $own->{height} if defined $own->{height};
Branch: 1 branch
BranchTrueFalseTotal
if defined $own->{"height"} 0 0 0
173 0 ▶ $a{align} = $own->{align} if defined $own->{align};
Branch: 1 branch
BranchTrueFalseTotal
if defined $own->{"align"} 0 0 0
174 0 $pdf->add_image(%a);
175 0 return;
176 }
177  
178 21 ▶ if ($tag eq 'bookmark') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag eq "bookmark") 2 19 21
179 2 ▶ _err($node, '<bookmark> needs a title') unless defined $own->{title};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $own->{"title"} 1 1 2
180 1 $pdf->add_outline($own->{title}, page => $pdf->page_count - 1);
181 1 return;
182 }
183  
184 # A box is one cell in one row: the layout engine already draws a padded,
185 # bordered, background-filled rectangle that grows with its content, and
186 # a second implementation of that would be a second set of bugs.
187 19 ▶ if ($tag eq 'box') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag eq "box") 2 17 19
188 2 my $lay = $pdf->layout;
189 2 my $row = $lay->row;
190 2 my %cell = (weight => 1);
191 2 ▶ $cell{pad} = $st->{pad} if defined $st->{pad};
Branch: 1 branch
BranchTrueFalseTotal
if defined $st->{"pad"} 2 0 2
192 2 ▶ $cell{bg} = $st->{bg} if defined $st->{bg};
Branch: 1 branch
BranchTrueFalseTotal
if defined $st->{"bg"} 1 1 2
193 2 ▶ $cell{border} = $st->{border} if defined $st->{border};
Branch: 1 branch
BranchTrueFalseTotal
if defined $st->{"border"} 1 1 2
194 2 my $cell = $row->cell(%cell);
195 2 $class->_cell_content($cell, $node, $st, $pdf);
196 2 $lay->render;
197 2 return;
198 }
199  
200 17 ▶ if ($tag eq 'row') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag eq "row") 6 11 17
201 6 $class->_row($pdf, $node, $st, [ _elems($node) ]);
202 4 return;
203 }
204  
205 11 ▶ if ($tag eq 'table') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag eq "table") 7 4 11
206 7 $class->_table($pdf, $node, $st);
207 6 return;
208 }
209  
210 4 ▶ if ($tag eq 'header' || $tag eq 'footer') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag eq "header" or $tag eq "footer") 4 0 4
Condition66%2/3
$tag eq "header" or $tag eq "footer"
l!l&&r!l&&!r
220
MC/DC0%0/2
$tag eq "header" or $tag eq "footer"
$tag eq "header" $tag eq "footer"
Truth table66%2/3
A or B
A= $tag eq "header"B= $tag eq "footer"
ABresult
1X1
011
000
211 4 ▶ my $m = $tag eq 'header' ? 'add_page_header' : 'add_page_footer';
Branch: 1 branch
BranchTrueFalseTotal
$tag eq 'header' ? : 2 2 4
212 4 my $runs = $class->_runs($node, $st);
213 4 my $text = join '', map { $_->{text} } @$runs;
214 4 ▶ return unless length $text;
Branch: 1 branch
BranchTrueFalseTotal
unless length $text 0 4 4
215  
216 # Headers and footers draw through a callback with a context object -
217 # they are not text blocks, and passing text => to add_page_header
218 # does nothing at all, silently. The alignment and font come from the
219 # element's own style so <footer align="center"> means what it says.
220 4 my $font = $S->font_args($st);
221 4 ▶ my $align = defined $st->{align} ? $st->{align} : 'left';
Branch: 1 branch
BranchTrueFalseTotal
defined $st->{'align'} ? : 0 4 4
222 4 ▶ $pdf->$m(cb => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
223 4 my (undef, undef, %a) = @_;
224 4 $a{ctx}->text(text => $text, align => $align, font => $font);
225 4 });
226 4 return;
227 }
228  
229 0 _err($node, "<%s> cannot appear here", $tag);
230 }
231  
232 70 ▶sub _page_args {
Subroutine _page_args: called CC=11 SCAR=24.9
233 70 my ($class, $pdf, $own) = @_;
234 70 my %a;
235 70 ▶ $a{page_size} = $own->{'page-size'} if defined $own->{'page-size'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $own->{"page-size"} 5 65 70
236 70 ▶ $a{padding} = $own->{margin} if defined $own->{margin};
Branch: 1 branch
BranchTrueFalseTotal
if defined $own->{"margin"} 5 65 70
237 70 ▶ $a{columns} = $own->{columns} if defined $own->{columns};
Branch: 1 branch
BranchTrueFalseTotal
if defined $own->{"columns"} 0 70 70
238 70 ▶ $a{background} = $own->{background} if defined $own->{background};
Branch: 1 branch
BranchTrueFalseTotal
if defined $own->{"background"} 0 70 70
239 70 my $cur = $pdf->page;
240 70 ▶ if ($cur) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cur) 5 65 70
241 # A new page keeps the shape of the one it follows unless told
242 # otherwise; a pagebreak that silently resized the paper would be a
243 # surprise nobody wants halfway through a statement run.
244 5 ▶ $a{page_size} = $cur->page_size unless defined $a{page_size};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $a{"page_size"} 4 1 5
245 5 ▶ $a{padding} = $cur->padding unless defined $a{padding};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $a{"padding"} 5 0 5
246 5 ▶ $a{columns} = $cur->columns unless defined $a{columns};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $a{"columns"} 5 0 5
247 $a{background} = $cur->background
248 5 ▶ unless defined $a{background} || !defined $cur->background;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $a{"background"} or not defined $cur->background 5 0 5
Condition33%1/3
defined $a{"background"} or not defined $cur->background
l!l&&r!l&&!r
005
MC/DC0%0/2
defined $a{"background"} or not defined $cur->background
defined $a{"background"} not defined $cur->background
Truth table33%1/3
A or not(B)
A= defined $a{"background"}B= not defined $cur->background
ABresult
1X1
011
000
249 }
250 70 return %a;
251 }
252  
253 # ---------------------------------------------------------------------------
254 # Rows, cells and tables
255 # ---------------------------------------------------------------------------
256  
257 # A cell's contents. Text and inline styling become one run item; a block
258 # child (a heading or a paragraph, as in a <box>) becomes an item of its own,
259 # which is how a box stacks blocks without a second layout engine.
260 25 ▶sub _cell_content {
Subroutine _cell_content: called CC=12 SCAR=25.0
261 25 my ($class, $cell, $node, $style, $pdf) = @_;
262 25 ▶ my $cfg = $pdf ? ($pdf->configure || {}) : {};
Branch: 1 branch
BranchTrueFalseTotal
$pdf ? : 2 23 25
Condition50%1/2
$pdf->configure or {}
l!l
20
MC/DC0%0/1
$pdf->configure or {}
$pdf->configure
Truth table50%1/2
A
A= $pdf->configure
Aresult
11
00
263  
264 25 my @pending; # inline nodes accumulating into one item
265 27 ▶ my $flush = sub {
Subroutine __ANON__: called CC=7 SCAR=19.6
266 27 ▶ return unless @pending;
Branch: 1 branch
BranchTrueFalseTotal
unless @pending 4 23 27
267 23 my $runs = [];
268 23 for my $c (@pending) {
269 25 ▶ if ($c->{kind} eq 'text') {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->{'kind'} eq 'text') { } 24 1 25
270 24 push @$runs, { %{ $S->font_args($style) }, text => $c->{text} };
271 } else {
272 1 my $own = $S->attrs($c);
273 1 ▶ $own->{bold} = 1 if $c->{tag} eq 'b';
Branch: 1 branch
BranchTrueFalseTotal
if $c->{"tag"} eq "b" 1 0 1
274 1 ▶ $own->{italic} = 1 if $c->{tag} eq 'i';
Branch: 1 branch
BranchTrueFalseTotal
if $c->{"tag"} eq "i" 0 1 1
275 1 $class->_runs($c, $S->inherit($style, $own), $runs);
276 }
277 }
278 23 @pending = ();
279 23 ▶ return unless @$runs;
Branch: 1 branch
BranchTrueFalseTotal
unless @$runs 0 23 23
280 23 $class->_add_cell_item($cell, $runs, $style);
281 25 };
282  
283 25 for my $c (_kids($node)) {
284 29 ▶ if ($c->{kind} eq 'text' || $IS_INLINE{ $c->{tag} || '' }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->{"kind"} eq "text" or $IS_INLINE{$c->{"tag"} or ""}) 25 4 29
Condition80%4/5
$c->{"kind"} eq "text" or $IS_INLINE{$c->{"tag"} or ""}
l!l&&r!l&&!r
2414
$c->{'tag'} || ''
l!l
50
MC/DC100%2/2
$c->{"kind"} eq "text" or $IS_INLINE{$c->{"tag"} or ""}
$c->{"kind"} eq "text" $IS_INLINE{$c->{"tag"} or ""}
MC/DC0%0/1
$c->{'tag'} || ''
$c->{'tag'}
Truth table100%3/3
A or B
A= $c->{"kind"} eq "text"B= $IS_INLINE{$c->{"tag"} or ""}
ABresult
1X1
011
000
Truth table50%1/2
A
A= $c->{'tag'}
Aresult
11
00
285 25 push @pending, $c;
286 25 next;
287 }
288  
289 _err($c, "<%s> cannot appear inside <%s>", $c->{tag}, $node->{tag})
290 unless $IS_HEADING{ $c->{tag} }
291 4 ▶ || $c->{tag} eq 'p' || $c->{tag} eq 'text';
Branch: 1 branch
BranchTrueFalseTotal
unless $IS_HEADING{$c->{"tag"}} or $c->{"tag"} eq "p" or $c->{"tag"} eq "text" 1 3 4
Condition83%5/6
$IS_HEADING{$c->{"tag"}} or $c->{"tag"} eq "p" or $c->{"tag"} eq "text"
l!l&&r!l&&!r
121
$IS_HEADING{$c->{"tag"}} or $c->{"tag"} eq "p"
l!l&&r!l&&!r
103
MC/DC66%2/3
$IS_HEADING{$c->{"tag"}} or $c->{"tag"} eq "p" or $c->{"tag"} eq "text"
$IS_HEADING{$c->{"tag"}} $c->{"tag"} eq "p" $c->{"tag"} eq "text"
Truth table75%3/4
A or B or C
A= $IS_HEADING{$c->{"tag"}}B= $c->{"tag"} eq "p"C= $c->{"tag"} eq "text"
ABCresult
1XX1
01X1
0011
0000
292  
293 3 $flush->();
294 3 my $own = $S->attrs($c);
295 3 my $st = $S->inherit($style, $own);
296 3 my $runs = $class->_runs($c, $st);
297 # <style h1="size:30"> reaches add_h1 through the builder's configure,
298 # which a cell item never goes near - so fold the tag's declared font
299 # in here, under anything the element said for itself.
300 3 ▶ my $tagf = $cfg->{ $c->{tag} } ? $cfg->{ $c->{tag} }{font} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$$cfg{$c->{'tag'}} ? : 2 1 3
301 3 ▶ $class->_add_cell_item($cell, $runs, $st, $tagf, $own) if @$runs;
Branch: 1 branch
BranchTrueFalseTotal
if @$runs 3 0 3
302 }
303 24 $flush->();
304 24 return;
305 }
306  
307 # One item on the cell: runs when anything in it is styled differently from
308 # the block, a plain string otherwise. The plain form keeps the cell's
309 # original wrapping, which existing documents depend on.
310 26 ▶sub _add_cell_item {
Subroutine _add_cell_item: called CC=16 SCAR=29.8
311 26 my ($class, $cell, $runs, $style, $tag_font, $own) = @_;
312 # inherited < the tag's <style> declaration < what the element itself said
313 26 my %f = %{ $S->font_args($style) };
314 26 ▶ if ($tag_font && %$tag_font) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag_font and %$tag_font) 0 26 26
Condition33%1/3
$tag_font and %$tag_font
!ll&&!rl&&r
2600
MC/DC0%0/2
$tag_font and %$tag_font
$tag_font %$tag_font
Truth table33%1/3
A and B
A= $tag_fontB= %$tag_font
ABresult
0X0
100
111
315 0 %f = (%f, %$tag_font);
316 0 ▶ %f = (%f, %{ $S->font_args($S->inherit({}, $own)) }) if $own;
Branch: 1 branch
BranchTrueFalseTotal
if $own 0 0 0
317 }
318 # A cell has one alignment; its items each have their own, which is what
319 # a <box> of centred blocks needs. spacing is per item for the same
320 # reason: it is what separates the blocks stacked in one box.
321 26 ▶ $f{align} = $style->{align} if defined $style->{align};
Branch: 1 branch
BranchTrueFalseTotal
if defined $style->{"align"} 9 17 26
322 26 ▶ $f{spacing} = $style->{spacing} if defined $style->{spacing};
Branch: 1 branch
BranchTrueFalseTotal
if defined $style->{"spacing"} 0 26 26
323  
324 26 my $styled = 0;
325 26 for my $r (@$runs) {
326 28 my $same = 1;
327 28 for my $k (qw(bold italic colour size family line_height)) {
328 158 ▶ next unless defined $r->{$k} || defined $f{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $r->{$k} or defined $f{$k} 150 8 158
Condition66%2/3
defined $r->{$k} or defined $f{$k}
l!l&&r!l&&!r
80150
MC/DC50%1/2
defined $r->{$k} or defined $f{$k}
defined $r->{$k} defined $f{$k}
Truth table66%2/3
A or B
A= defined $r->{$k}B= defined $f{$k}
ABresult
1X1
011
000
329 $same = 0, last
330 if (defined $r->{$k} ? $r->{$k} : '') ne
331 8 ▶ (defined $f{$k} ? $f{$k} : '');
Branch: 3 branches
BranchTrueFalseTotal
if (defined $r->{$k} ? $r->{$k} : "") ne (defined $f{$k} ? $f{$k} : "") 2 6 8
defined $r->{$k} ? : 8 0 8
defined $f{$k} ? : 6 2 8
332 }
333 28 ▶ $styled = 1, last unless $same;
Branch: 1 branch
BranchTrueFalseTotal
unless $same 2 26 28
334 }
335  
336 26 ▶ if (!$styled && @$runs == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $styled and @$runs == 1) { } 24 2 26
Condition66%2/3
not $styled and @$runs == 1
!ll&&!rl&&r
2024
MC/DC50%1/2
not $styled and @$runs == 1
not $styled @$runs == 1
Truth table66%2/3
not(A) and B
A= not $styledB= @$runs == 1
ABresult
0X0
100
111
337 24 $cell->text($runs->[0]{text}, %f);
338 } else {
339 2 $cell->runs($runs, %f);
340 }
341 26 return;
342 }
343  
344 15 ▶sub _cells {
Subroutine _cells: called CC=13 SCAR=25.8
345 15 my ($class, $row, $node, $style, $cells, $colw) = @_;
346 15 my $i = -1;
347 15 for my $c (@$cells) {
348 24 $i++;
349 _err($c, "<%s> cannot appear inside <%s>, which holds cells",
350 $c->{tag}, $node->{tag})
351 24 ▶ unless $IS_CELL{ $c->{tag} };
Branch: 1 branch
BranchTrueFalseTotal
unless $IS_CELL{$c->{"tag"}} 1 23 24
352  
353 23 my $own = $S->attrs($c);
354 23 my $st = $S->inherit($style, $own);
355 23 ▶ $st->{bold} = 1 if $c->{tag} eq 'th' && !exists $own->{bold};
Branch: 1 branch
BranchTrueFalseTotal
if $c->{"tag"} eq "th" and not exists $own->{"bold"} 6 17 23
Condition66%2/3
$c->{"tag"} eq "th" and not exists $own->{"bold"}
!ll&&!rl&&r
1706
MC/DC50%1/2
$c->{"tag"} eq "th" and not exists $own->{"bold"}
$c->{"tag"} eq "th" not exists $own->{"bold"}
Truth table66%2/3
A and not(B)
A= $c->{"tag"} eq "th"B= not exists $own->{"bold"}
ABresult
0X0
100
111
356  
357 # A table's columns are the table's, not each row's. Without this a
358 # header that declares weight="3" and body rows that declare nothing
359 # produce two different column layouts stacked on top of each other,
360 # which is what "the table is not aligned" looks like.
361 my $weight = defined $own->{weight} ? $own->{weight}
362 23 ▶ : ($colw && defined $colw->[$i]) ? $colw->[$i]
Branch: 2 branches
BranchTrueFalseTotal
defined $own->{'weight'} ? : 4 19 23
$colw && defined $colw->[$i] ? : 15 4 19
Condition66%2/3
$colw and defined $colw->[$i]
!ll&&!rl&&r
4015
MC/DC50%1/2
$colw and defined $colw->[$i]
$colw defined $colw->[$i]
Truth table66%2/3
A and B
A= $colwB= defined $colw->[$i]
ABresult
0X0
100
111
363 : 1;
364 23 my %a = (weight => $weight);
365 23 ▶ $a{pad} = $own->{pad} if defined $own->{pad};
Branch: 1 branch
BranchTrueFalseTotal
if defined $own->{"pad"} 2 21 23
366 23 ▶ $a{bg} = $own->{bg} if defined $own->{bg};
Branch: 1 branch
BranchTrueFalseTotal
if defined $own->{"bg"} 1 22 23
367 23 ▶ $a{border} = $own->{border} if defined $own->{border};
Branch: 1 branch
BranchTrueFalseTotal
if defined $own->{"border"} 0 23 23
368 23 ▶ $a{align} = $st->{align} if defined $st->{align};
Branch: 1 branch
BranchTrueFalseTotal
if defined $st->{"align"} 9 14 23
369 23 ▶ $a{valign} = $own->{valign} if defined $own->{valign};
Branch: 1 branch
BranchTrueFalseTotal
if defined $own->{"valign"} 0 23 23
370  
371 23 my $cell = $row->cell(%a);
372 23 $class->_cell_content($cell, $c, $st);
373 }
374 }
375  
376 6 ▶sub _row {
Subroutine _row: called CC=5 SCAR=17.0
377 6 my ($class, $pdf, $node, $style, $cells, $lay) = @_;
378 6 my $own = $S->attrs($node);
379 6 ▶ my $mine = $lay || $pdf->layout;
Condition33%1/3
$lay || $pdf->layout
l!l&&r!l&&!r
060
MC/DC0%0/2
$lay || $pdf->layout
$lay $pdf->layout
Truth table33%1/3
A || B
A= $layB= $pdf->layout
ABresult
1X1
011
000
380 6 my %a;
381 6 ▶ $a{gap} = $own->{gap} if defined $own->{gap};
Branch: 1 branch
BranchTrueFalseTotal
if defined $own->{"gap"} 0 6 6
382 6 ▶ $a{height} = $own->{height} if defined $own->{height};
Branch: 1 branch
BranchTrueFalseTotal
if defined $own->{"height"} 0 6 6
383 6 my $row = $mine->row(%a);
384 6 $class->_cells($row, $node, $style, $cells);
385 4 ▶ $mine->render unless $lay;
Branch: 1 branch
BranchTrueFalseTotal
unless $lay 4 0 4
386 4 return $mine;
387 }
388  
389 7 ▶sub _table {
Subroutine _table: called CC=9 SCAR=22.1
390 7 my ($class, $pdf, $node, $style) = @_;
391 7 my $own = $S->attrs($node);
392 7 my $st = $S->inherit($style, $own);
393  
394 # Column widths come from the first row - usually the header, which is
395 # where anyone writing a table declares them - and every later row uses
396 # them unless one of its own cells says otherwise.
397 7 my @colw;
398 {
399 7 my ($first) = _elems($node);
400 7 ▶ if ($first) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first) 7 0 7
401 7 for my $c (_elems($first)) {
402 9 ▶ my $a = eval { $S->attrs($c) } || {};
Condition50%1/2
eval { do { $S->attrs($c) } } || {}
l!l
90
MC/DC0%0/1
eval { do { $S->attrs($c) } } || {}
eval { do { $S->attrs($c) } }
Truth table50%1/2
A
A= eval { do { $S->attrs($c) } }
Aresult
11
00
403 9 ▶ push @colw, defined $a->{weight} ? $a->{weight} : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $a->{'weight'} ? : 2 7 9
404 }
405 }
406 }
407  
408 7 for my $tr (_elems($node)) {
409 _err($tr, "<%s> cannot appear inside <table>, which holds rows",
410 $tr->{tag})
411 10 ▶ unless $tr->{tag} eq 'tr';
Branch: 1 branch
BranchTrueFalseTotal
unless $tr->{"tag"} eq "tr" 1 9 10
412  
413 9 my $tr_own = $S->attrs($tr);
414 9 my $lay = $pdf->layout;
415 9 my %a;
416 9 ▶ $a{gap} = $own->{gap} if defined $own->{gap};
Branch: 1 branch
BranchTrueFalseTotal
if defined $own->{"gap"} 0 9 9
417 9 ▶ $a{height} = $tr_own->{height} if defined $tr_own->{height};
Branch: 1 branch
BranchTrueFalseTotal
if defined $tr_own->{"height"} 0 9 9
418 9 my $row = $lay->row(%a);
419 9 $class->_cells($row, $tr, $S->inherit($st, $tr_own), [ _elems($tr) ], \@colw);
420 9 $lay->render;
421 }
422 }
423  
424 # ---------------------------------------------------------------------------
425 # Walking
426 # ---------------------------------------------------------------------------
427  
428 67 ▶sub _children {
Subroutine _children: called CC=4 SCAR=13.9
429 67 my ($class, $pdf, $node, $style) = @_;
430 67 for my $c (_kids($node)) {
431 85 ▶ if ($c->{kind} eq 'text') {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->{"kind"} eq "text") 1 84 85
432 # Only whitespace survives the parser inside a container, and it
433 # is indentation. Anything else is text somebody expected to see.
434 1 ▶ next unless $c->{text} =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless $c->{"text"} =~ /\S/ 0 1 1
435 _err($c, "text directly inside <%s>: put it in a <text> block",
436 1 $node->{tag});
437 }
438 84 $class->_block($pdf, $c, $style);
439 }
440 }
441  
442 # <style> children become the builder's configure map, so that h1 and text
443 # defaults go through the same path a hand-written builder script uses.
444 65 ▶sub _configure {
Subroutine _configure: called CC=8 SCAR=21.2
445 65 my ($class, $root) = @_;
446 65 my %cfg;
447 65 for my $s (grep { $_->{tag} eq 'style' } _elems($root)) {
448 3 for my $tag (sort keys %{ $s->{attrs} }) {
449 4 my $decl = $S->declarations($s->{attrs}{$tag}, $s, $tag);
450 4 my $font = $S->font_args($decl);
451 4 ▶ $cfg{$tag} ||= {};
Condition50%1/2
$cfg{$tag} ||= {}
l!l
04
MC/DC0%0/1
$cfg{$tag} ||= {}
$cfg{$tag}
Truth table50%1/2
A
A= $cfg{$tag}
Aresult
11
00
452 4 ▶ $cfg{$tag}{font} = { %{ $cfg{$tag}{font} || {} }, %$font };
Condition50%1/2
$cfg{$tag}{'font'} || {}
l!l
04
MC/DC0%0/1
$cfg{$tag}{'font'} || {}
$cfg{$tag}{'font'}
Truth table50%1/2
A
A= $cfg{$tag}{'font'}
Aresult
11
00
453 4 for my $k (qw(align indent spacing margin)) {
454 16 ▶ $cfg{$tag}{$k} = $decl->{$k} if defined $decl->{$k};
Branch: 1 branch
BranchTrueFalseTotal
if defined $decl->{$k} 0 16 16
455 }
456 4 ▶ $cfg{$tag}{padding} = $decl->{pad} if defined $decl->{pad};
Branch: 1 branch
BranchTrueFalseTotal
if defined $decl->{"pad"} 0 4 4
457 }
458 }
459 65 return \%cfg;
460 }
461  
462 =head2 build
463  
464 my $pdf = PDF::Make::Markup::Build->build($root, file_name => 'out');
465  
466 Walks a parsed tree onto a L<PDF::Make::Builder> and returns it, unsaved, so
467 the caller decides where the bytes go.
468  
469 =cut
470  
471 65 ▶sub build {
Subroutine build: called CC=9 SCAR=26.9
Pod documented
472 65 my ($class, $root, %opt) = @_;
473 _err($root, 'the root element must be <doc>')
474 65 ▶ unless $root && ($root->{tag} || '') eq 'doc';
Branch: 1 branch
BranchTrueFalseTotal
unless $root and ($root->{"tag"} || "") eq "doc" 0 65 65
Condition40%2/5
$root and ($root->{"tag"} || "") eq "doc"
!ll&&!rl&&r
0065
$root->{'tag'} || ''
l!l
650
MC/DC0%0/2
$root and ($root->{"tag"} || "") eq "doc"
$root $root->{'tag'}
Truth table33%1/3
A and B
A= $rootB= $root->{'tag'}
ABresult
0X0
100
111
475  
476 Carp::croak('PDF::Make::Markup::Build->build needs a file_name')
477 65 ▶ unless defined $opt{file_name} && length $opt{file_name};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opt{"file_name"} and length $opt{"file_name"} 0 65 65
Condition33%1/3
defined $opt{"file_name"} and length $opt{"file_name"}
!ll&&!rl&&r
0065
MC/DC0%0/2
defined $opt{"file_name"} and length $opt{"file_name"}
defined $opt{"file_name"} length $opt{"file_name"}
Truth table33%1/3
A and B
A= defined $opt{"file_name"}B= length $opt{"file_name"}
ABresult
0X0
100
111
478  
479 65 my $own = $S->attrs($root);
480 my $pdf = PDF::Make::Builder->new(
481 file_name => $opt{file_name},
482 65 configure => $class->_configure($root),
483 );
484  
485 # <doc tagged="1">: accessibility is a property of the document, so
486 # it is declared where the document starts. Builder's add_* methods
487 # already push P/H1-H6/Figure structure elements; this turns the
488 # tree on so they land in a StructTreeRoot. Deeper semantics
489 # (Table/TR/TD) are future work and the docs say so plainly.
490 $pdf->enable_tagging
491 65 ▶ if defined $own->{tagged} && $own->{tagged} && $own->{tagged} ne '0';
Branch: 1 branch
BranchTrueFalseTotal
if defined $own->{"tagged"} and $own->{"tagged"} and $own->{"tagged"} ne 0 0 65 65
Condition33%2/6
defined $own->{"tagged"} and $own->{"tagged"} and $own->{"tagged"} ne 0
!ll&&!rl&&r
6500
defined $own->{"tagged"} and $own->{"tagged"}
!ll&&!rl&&r
6500
MC/DC0%0/3
defined $own->{"tagged"} and $own->{"tagged"} and $own->{"tagged"} ne 0
defined $own->{"tagged"} $own->{"tagged"} $own->{"tagged"} ne 0
Truth table25%1/4
A and B and C
A= defined $own->{"tagged"}B= $own->{"tagged"}C= $own->{"tagged"} ne 0
ABCresult
0XX0
10X0
1100
1111
492  
493 65 $pdf->add_page($class->_page_args($pdf, $own));
494 65 $class->_children($pdf, $root, $S->inherit(undef, $own));
495 57 return $pdf;
496 }
497  
498 6 1;
499  
500 __END__
« blib/lib/PDF/Make/Builder.pm ↑ Summary blib/lib/PDF/Make/Builder/TOC.pm »