blib/lib/PDF/Collage/Template.pm

filter: Statementstmt 77.2% 102 / 132 Branchbran 39.1% 18 / 46 Conditioncond 33.3% 9 / 27 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 78.1% 25 / 32 Podpod 100% 1 / 1 totaltotal 61.2% 155 / 253 CC 39 SCAR 25.8
CC
39
Cov
63%
__fc_list
72.0
_op_log
20.0
_render_text
12.4
CRAP 13.3
« blib/lib/PDF/Collage.pm ↑ Summary blib/lib/PDF/Collage/TemplatesCollection.pm »
1 package PDF::Collage::Template;
2 2 ▶use v5.24;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 { our $VERSION = '0.002' }
5  
6 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use English;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Template::Perlish ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Data::Resolver ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use PDF::Builder;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use experimental qw< signatures >;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶no warnings qw< experimental::signatures >;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 2 has commands => (is => ro => required => 1);
19 2 has functions => (is => 'lazy');
20 2 has logger => (is => 'lazy');
21 2 has metadata => (is => 'lazy');
22  
23 2 has _data => (is => 'lazy');
24 2 has _defaults => (is => 'lazy');
25 2 has _fonts => (is => 'lazy');
26 2 has _pdf => (is => 'lazy');
27  
28 0 ▶sub _build_functions ($self) { return {} }
Subroutine _build_functions: not called CC=1 SCAR=6.9
29 ▶sub _build_logger ($self) {
Subroutine _build_logger: not called CC=1 SCAR=6.9
30 0 eval { require Log::Any; Log::Any->get_logger }
31 }
32 0 ▶sub _build_metadata ($self) { return {} }
Subroutine _build_metadata: not called CC=1 SCAR=6.9
33 0 ▶sub _build__data ($self) { return {} }
Subroutine _build__data: not called CC=1 SCAR=6.9
34 17 ▶sub _build__defaults ($self) { return {} }
Subroutine _build__defaults: called CC=1 SCAR=0.0
35 16 ▶sub _build__fonts ($self) { return {} }
Subroutine _build__fonts: called CC=1 SCAR=0.0
36 16 ▶sub _build__pdf ($self) { return PDF::Builder->new }
Subroutine _build__pdf: called CC=1 SCAR=0.0
37  
38 ▶sub render ($self, $data) {
Subroutine render: called CC=1 SCAR=0.0
Pod documented
39 17 $self->new( # hand over to a disposable clone
40 commands => $self->commands,
41 functions => $self->functions,
42 _data => $data,
43 )->_real_render;
44 } ## end sub render
45  
46 ▶sub _real_render ($self) {
Subroutine _real_render: called CC=3 SCAR=11.1
47 17 for my $command ($self->commands->@*) {
48 66 my $op = $command->{op} =~ s{-}{_}rgmxs;
49 66 ▶ my $method = $self->can('_op_' . $op)
Branch: 1 branch
BranchTrueFalseTotal
my $method = $self->can('_op_' . $op) or croak(qq[unsupported op<$command->{"op"}>]) 0 66 66
50 or croak "unsupported op<$command->{op}>";
51 66 $self->$method($command);
52 } ## end for my $command ($self->...)
53 16 return $self->_pdf;
54 } ## end sub _real_render
55  
56 ▶sub _tpr ($self, $tmpl) {
Subroutine _tpr: called CC=1 SCAR=0.0
57 193 return Template::Perlish::render($tmpl, $self->_data,
58 {functions => $self->functions});
59 }
60  
61 ▶sub _expand ($self, $command, @keys) {
Subroutine _expand: called CC=4 SCAR=13.9
62 49 my %auto_expand = map { $_ => 1 } @keys;
63 49 my %overall = ($self->_defaults->%*, $command->%*);
64 49 my %retval;
65 49 for my $key (sort { $a cmp $b } keys %overall) {
66 307 my $nkey = $key =~ s{-}{_}rgmxs;
67 307 ▶ next if exists $retval{$nkey};
Branch: 1 branch
BranchTrueFalseTotal
if exists $retval{$nkey} 0 307 307
68 307 my $value = $overall{$key};
69 307 ▶ $retval{$nkey} = $auto_expand{$nkey} ? $self->_tpr($value) : $value;
Branch: 1 branch
BranchTrueFalseTotal
$auto_expand{$nkey} ? : 177 130 307
70 } ## end for my $key (sort { $a ...})
71 48 return \%retval;
72 } ## end sub _expand
73  
74 64 ▶sub __pageno ($input) { return $input eq 'last' ? 0 : $input }
Subroutine __pageno: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$input eq 'last' ? : 48 16 64
75  
76 ▶sub __fc_list ($key) {
Subroutine __fc_list: not called CC=8 SCAR=42.8
77 0 my @command = ('fc-list', $key, qw< file style >);
78 0 ▶ open my $fh, '-|', @command or croak "fc-list: $OS_ERROR";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "-|", @command or croak("fc-list: $PDF::Collage::Template::OS_ERROR") 0 0 0
79 my @candidates = map {
80 0 s{\s+\z}{}mxs;
81 0 ▶ my ($filename, $style) = m{\A (.*?): \s* :style=(.*)}mxs
Branch: 1 branch
BranchTrueFalseTotal
() = /\A (.*?): \s* :style=(.*)/msux or croak("fc-list: unexpected line '${_}'") 0 0 0
82 or croak "fc-list: unexpected line '$_'";
83 0 my %style = map { $_ => 1 } split m{,}mxs, $style;
84 0 {filename => $filename, style => \%style};
85 } <$fh>;
86 0 ▶ return unless @candidates;
Branch: 1 branch
BranchTrueFalseTotal
unless @candidates 0 0 0
87 0 ▶ return $candidates[0]{filename} if @candidates == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @candidates == 1 0 0 0
88  
89 # get Regular/Normal if exists
90 0 for my $candidate (@candidates) {
91 return $candidate->{filename}
92 0 ▶ if $candidate->{style}{Regular} || $candidate->{style}{Normal};
Branch: 1 branch
BranchTrueFalseTotal
if $candidate->{"style"}{"Regular"} or $candidate->{"style"}{"Normal"} 0 0 0
Condition0%0/3
$candidate->{"style"}{"Regular"} or $candidate->{"style"}{"Normal"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$candidate->{"style"}{"Regular"} or $candidate->{"style"}{"Normal"}
$candidate->{"style"}{"Regular"} $candidate->{"style"}{"Normal"}
Truth table0%0/3
A or B
A= $candidate->{"style"}{"Regular"}B= $candidate->{"style"}{"Normal"}
ABresult
1X1
011
000
93 }
94  
95 # bail out, request more data
96 0 croak "fc-list: too many outputs for '$key'";
97 }
98  
99 ▶sub _font ($s, $key) {
Subroutine _font: called CC=4 SCAR=14.9
100 16 ▶ if (! defined($s->_fonts->{$key})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $s->_fonts->{$key}) 16 0 16
101 16 ▶ $key = $key =~ m{\A fc: (.*) \z}mxs ? __fc_list($1)
Branch: 2 branches
BranchTrueFalseTotal
$key =~ /\A fc: (.*) \z/msux ? : 0 16 16
$key =~ /\A file: (.*) \z/msux ? : 0 16 16
102 : $key =~ m{\A file: (.*) \z}mxs ? $1
103 : $key;
104 16 $s->_fonts->{$key} = $s->_pdf->font($key);
105 }
106 16 return $s->_fonts->{$key};
107 }
108  
109 ▶sub _op_add_image ($self, $command) {
Subroutine _op_add_image: called CC=2 SCAR=7.0
110 16 my $opts = $self->_expand($command, qw< page path x y width height >);
111 16 ▶ my $page = $self->_pdf->open_page(__pageno($opts->{page} // 'last'));
Condition50%1/2
$opts->{"page"} // "last"
l!l
016
MC/DC0%0/1
$opts->{"page"} // "last"
$opts->{"page"}
Truth table50%1/2
A
A= $opts->{"page"}
Aresult
11
00
112 16 my $image = $self->_pdf->image($opts->{path});
113 16 $page->object($image, $opts->@{qw< x y width height >});
114 16 return;
115 } ## end sub _op_add_image
116  
117 ▶sub _op_add_page ($self, $command) {
Subroutine _op_add_page: called CC=5 SCAR=17.8
118 17 my $opts =
119 $self->_expand($command, qw< page from from_path from_page >);
120 16 ▶ my $target_n = __pageno($opts->{page} // 'last');
Condition50%1/2
$opts->{"page"} // "last"
l!l
016
MC/DC0%0/1
$opts->{"page"} // "last"
$opts->{"page"}
Truth table50%1/2
A
A= $opts->{"page"}
Aresult
11
00
121 defined(my $source_path = $opts->{from} // $opts->{from_path})
122 16 ▶ or return $self->_pdf->page($target_n);
Branch: 1 branch
BranchTrueFalseTotal
defined(my $source_path = $opts->{"from"} // $opts->{"from_path"}) or return $self->_pdf->page($target_n) 0 16 16
Condition33%1/3
$opts->{"from"} // $opts->{"from_path"}
l!l&&r!l&&!r
1600
MC/DC0%0/2
$opts->{"from"} // $opts->{"from_path"}
$opts->{"from"} $opts->{"from_path"}
Truth table33%1/3
A // B
A= $opts->{"from"}B= $opts->{"from_path"}
ABresult
1X1
011
000
123 16 my $source = PDF::Builder->open($source_path);
124 16 ▶ my $source_n = __pageno($opts->{from_page} // 'last');
Condition50%1/2
$opts->{"from_page"} // "last"
l!l
160
MC/DC0%0/1
$opts->{"from_page"} // "last"
$opts->{"from_page"}
Truth table50%1/2
A
A= $opts->{"from_page"}
Aresult
11
00
125 16 return $self->_pdf->import_page($source, $source_n, $target_n);
126 } ## end sub _op_add_page
127  
128 ▶sub _op_add_text ($self, $command) {
Subroutine _op_add_text: called CC=8 SCAR=23.9
129 16 my $opts =
130 $self->_expand($command, qw< align page font font_family font_size x y >);
131  
132 my $content =
133 16 $self->_render_text($opts->@{qw< text text_template text_var >});
134  
135 16 ▶ my $font = $self->_font($opts->{font} // $opts->{font_family});
Condition33%1/3
$opts->{"font"} // $opts->{"font_family"}
l!l&&r!l&&!r
1600
MC/DC0%0/2
$opts->{"font"} // $opts->{"font_family"}
$opts->{"font"} $opts->{"font_family"}
Truth table33%1/3
A // B
A= $opts->{"font"}B= $opts->{"font_family"}
ABresult
1X1
011
000
136 16 my $font_size = $opts->{font_size};
137  
138 16 ▶ my ($x, $y) = map { $_ // 0 } $opts->@{qw< x y >};
Condition50%1/2
$_ // 0
l!l
320
MC/DC0%0/1
$_ // 0
$_
Truth table50%1/2
A
A= $_
Aresult
11
00
139  
140 16 ▶ my $align = $opts->{align} // 'start';
Condition50%1/2
$opts->{"align"} // "start"
l!l
016
MC/DC0%0/1
$opts->{"align"} // "start"
$opts->{"align"}
Truth table50%1/2
A
A= $opts->{"align"}
Aresult
11
00
141 16 ▶ if ($align ne 'start') {
Branch: 1 branch
BranchTrueFalseTotal
if ($align ne "start") 0 16 16
142 0 my $width = $font_size * $font->width($content);
143 0 ▶ $x -= $align eq 'end' ? $width : ($width / 2);
Branch: 1 branch
BranchTrueFalseTotal
$align eq 'end' ? : 0 0 0
144 }
145  
146 16 ▶ my $page = $self->_pdf->open_page(__pageno($opts->{page} // 'last'));
Condition50%1/2
$opts->{"page"} // "last"
l!l
016
MC/DC0%0/1
$opts->{"page"} // "last"
$opts->{"page"}
Truth table50%1/2
A
A= $opts->{"page"}
Aresult
11
00
147 16 my $text = $page->text;
148 16 $text->position($x, $y);
149 16 $text->font($font, $opts->{font_size});
150 16 ▶ $text->text($content // '');
Condition50%1/2
$content // ""
l!l
160
MC/DC0%0/1
$content // ""
$content
Truth table50%1/2
A
A= $content
Aresult
11
00
151  
152 16 return $self;
153 } ## end sub _op_add_text
154  
155 ▶sub _render_text ($self, $plain, $template, $crumbs) {
Subroutine _render_text: called CC=5 SCAR=25.2
156 16 ▶ return $plain if defined $plain;
Branch: 1 branch
BranchTrueFalseTotal
if defined $plain 0 16 16
157 16 ▶ return $self->_tpr($template) if defined $template;
Branch: 1 branch
BranchTrueFalseTotal
if defined $template 16 0 16
158 0 ▶ return Template::Perlish::traverse($self->_data, $crumbs) // ''
Branch: 1 branch
BranchTrueFalseTotal
if defined $crumbs 0 0 0
Condition0%0/2
Template::Perlish::traverse($self->_data, $crumbs) // ""
l!l
00
MC/DC0%0/1
Template::Perlish::traverse($self->_data, $crumbs) // ""
Template::Perlish::traverse($self->_data, $crumbs)
Truth table0%0/2
A
A= Template::Perlish::traverse($self->_data, $crumbs)
Aresult
11
00
159 if defined $crumbs;
160 0 return;
161 } ## end sub _render_text
162  
163 ▶sub _op_set_defaults ($self, $command) {
Subroutine _op_set_defaults: called CC=4 SCAR=14.0
164 17 my $defaults = $self->_defaults;
165 17 ▶ while (my ($key, $value) = each $command->%*) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$command) 51 17 68
166 51 ▶ next if $key eq 'op';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "op" 17 34 51
167 34 ▶ if (defined $value) { $defaults->{$key} = $value }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 34 0 34
168 0 else { delete $defaults->{$key} }
169 }
170 17 return;
171 } ## end sub _op_set_defaults
172  
173 ▶sub _default_log ($self, $command) {
Subroutine _default_log: not called CC=1 SCAR=6.9
174 0 warn "[$command->{level}] $command->{message}\n";
175 0 return $self;
176 }
177  
178 ▶sub _op_log ($self, $command) {
Subroutine _op_log: not called CC=4 SCAR=30.0
179 0 ▶ my $logger = $self->logger or return $self->_default_log($command);
Branch: 1 branch
BranchTrueFalseTotal
my $logger = $self->logger or return $self->_default_log($command) 0 0 0
180 0 ▶ my $method = $logger->can(lc($command->{level}) // 'info')
Branch: 1 branch
BranchTrueFalseTotal
my $method = $logger->can(lc $command->{"level"} // "info") or return $self->_default_log($command) 0 0 0
Condition0%0/2
lc $command->{"level"} // "info"
l!l
00
MC/DC0%0/1
lc $command->{"level"} // "info"
lc $command->{"level"}
Truth table0%0/2
A
A= lc $command->{"level"}
Aresult
11
00
181 or return $self->_default_log($command);
182 0 $logger->$method($command->{message});
183 0 return $self;
184 }
185  
186 2 1;
« blib/lib/PDF/Collage.pm ↑ Summary blib/lib/PDF/Collage/TemplatesCollection.pm »