lib/ChordPro/Output/PDF.pm

filter: Statementstmt 69.4% 444 / 639 Branchbran 50.8% 177 / 348 Conditioncond 30.5% 44 / 144 MC/DCmcdc 9.6% 8 / 83 Subroutinesub 80.4% 33 / 41 Podpod 0.0% 0 / 7 totaltotal 55.9% 706 / 1262 CC 260 SCAR 62.6
CC
260
Cov
59%
generate_songbook
931.1
parse
702.0
getimage
342.0
CRAP 521.4
« lib/ChordPro/Output/HTML.pm ↑ Summary lib/ChordPro/Output/MMA.pm »
1 #! perl
2  
3 package main;
4  
5 12 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 our $config;
7 12 our $options;
8  
9 12 our $ps;
10 12 our $pr;
11 12 our $dw;
12  
13 package ChordPro::Output::PDF;
14  
15 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 12 ▶use File::Temp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18 12 ▶use Ref::Util qw(is_hashref is_arrayref is_coderef);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 12 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 12 ▶use ChordPro::Output::Common qw( prep_outlines fmt_subst );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 12 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 12 ▶use ChordPro::Output::PDF::Song;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 12 ▶use ChordPro::Output::PDF::Writer;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 12 ▶use ChordPro::Files;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 12 ▶use ChordPro::Paths;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 12 ▶use ChordPro::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 # Set by Configurator.
30 12 our $pdfapi;
31  
32 12 ▶use Text::Layout;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 12 ▶use List::Util qw(any);
Subroutine BEGIN: called CC=1 SCAR=0.0
34 12 ▶use Unicode::Collate;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 12 my $verbose = 0;
37  
38 # For regression testing, run perl with PERL_HASH_SEED set to zero.
39 # This eliminates the arbitrary order of font definitions and triggers
40 # us to pinpoint some other data that would otherwise be varying.
41 12 ▶my $regtest = defined($ENV{PERL_HASH_SEED}) && $ENV{PERL_HASH_SEED} == 0;
Condition33%1/3
defined $ENV{'PERL_HASH_SEED'} && $ENV{'PERL_HASH_SEED'} == 0
!ll&&!rl&&r
1200
MC/DC0%0/2
defined $ENV{'PERL_HASH_SEED'} && $ENV{'PERL_HASH_SEED'} == 0
defined $ENV{'PERL_HASH_SEED'} $ENV{'PERL_HASH_SEED'} == 0
Truth table33%1/3
A && B
A= defined $ENV{'PERL_HASH_SEED'}B= $ENV{'PERL_HASH_SEED'} == 0
ABresult
0X0
100
111
42  
43 # Convenience.
44 12 *generate_song = \&ChordPro::Output::PDF::Song::generate_song;
45  
46 13 ▶sub generate_songbook {
Subroutine generate_songbook: called CC=140 SCAR=68.4
Pod undocumented
47 13 my ( $self, $sb ) = @_;
48  
49 return [] unless $sb->{songs}->[0]->{body}
50 13 ▶ || $sb->{songs}->[0]->{source}->{embedding};
Branch: 1 branch
BranchTrueFalseTotal
unless $sb->{"songs"}[0]{"body"} or $sb->{"songs"}[0]{"source"}{"embedding"} 0 13 13
Condition33%1/3
$sb->{"songs"}[0]{"body"} or $sb->{"songs"}[0]{"source"}{"embedding"}
l!l&&r!l&&!r
1300
MC/DC0%0/2
$sb->{"songs"}[0]{"body"} or $sb->{"songs"}[0]{"source"}{"embedding"}
$sb->{"songs"}[0]{"body"} $sb->{"songs"}[0]{"source"}{"embedding"}
Truth table33%1/3
A or B
A= $sb->{"songs"}[0]{"body"}B= $sb->{"songs"}[0]{"source"}{"embedding"}
ABresult
1X1
011
000
51 13 ▶ $verbose ||= $options->{verbose};
Condition33%1/3
$verbose ||= $main::options->{"verbose"}
l!l&&r!l&&!r
0013
MC/DC0%0/2
$verbose ||= $main::options->{"verbose"}
$verbose $main::options->{"verbose"}
Truth table33%1/3
A ||= B
A= $verboseB= $main::options->{"verbose"}
ABresult
1X1
011
000
52  
53  
54 13 $config->unlock;
55 13 $ps = $config->{pdf};
56 # use DDP; p $ps->{songbook}, as => "in PDF";
57 13 my $pagectrl = $self->pagectrl;
58 13 $config->lock;
59  
60 13 my $extra_matter = 0;
61 13 ▶ if ( $options->{toc} // (@{$sb->{songs}} > 1) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main::options->{"toc"} // @{$sb->{"songs"};} > 1) 9 4 13
Condition100%2/2
$main::options->{"toc"} // @{$sb->{"songs"};} > 1
l!l
49
MC/DC100%1/1
$main::options->{"toc"} // @{$sb->{"songs"};} > 1
$main::options->{"toc"}
Truth table100%2/2
A
A= $main::options->{"toc"}
Aresult
11
00
62 9 for ( @{ $::config->{contents} } ) {
63 # Treat ToCs as one.
64 9 ▶ $extra_matter++, last unless $_->{omit};
Branch: 1 branch
BranchTrueFalseTotal
unless $_->{"omit"} 9 0 9
65 }
66 9 ▶ $extra_matter++ if $options->{title};
Branch: 1 branch
BranchTrueFalseTotal
if $main::options->{"title"} 0 9 9
67 }
68 13 ▶ $extra_matter++ if $pagectrl->{cover} && !$options->{title};
Branch: 1 branch
BranchTrueFalseTotal
if $pagectrl->{"cover"} and not $main::options->{"title"} 1 12 13
Condition66%2/3
$pagectrl->{"cover"} and not $main::options->{"title"}
!ll&&!rl&&r
1201
MC/DC50%1/2
$pagectrl->{"cover"} and not $main::options->{"title"}
$pagectrl->{"cover"} not $main::options->{"title"}
Truth table66%2/3
A and not(B)
A= $pagectrl->{"cover"}B= not $main::options->{"title"}
ABresult
0X0
100
111
69 13 ▶ $extra_matter++ if $pagectrl->{front_matter};
Branch: 1 branch
BranchTrueFalseTotal
if $pagectrl->{"front_matter"} 4 9 13
70 13 ▶ $extra_matter++ if $pagectrl->{back_matter};
Branch: 1 branch
BranchTrueFalseTotal
if $pagectrl->{"back_matter"} 4 9 13
71 13 ▶ $extra_matter++ if $options->{csv};
Branch: 1 branch
BranchTrueFalseTotal
if $main::options->{"csv"} 13 0 13
72  
73 # $prefill indicates that in 2page mode, a filler page is needed to
74 # get the songs properly aligned.
75 13 my $prefill = 0;
76 13 ▶ if ( $pagectrl->{align_songs_spread} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pagectrl->{"align_songs_spread"}) 3 10 13
77 3 $prefill = 1;
78 }
79 13 ▶ if ( $pagectrl->{sort_songs} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pagectrl->{"sort_songs"}) 3 10 13
80 3 sort_songbook( $sb, $pagectrl );
81 }
82 13 ▶ if ( $pagectrl->{compact_songs} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pagectrl->{"compact_songs"}) 1 12 13
83 1 $prefill = compact_songbook( $sb, $pagectrl );
84 1 ▶ return unless defined $prefill; # cancelled
Branch: 1 branch
BranchTrueFalseTotal
unless defined $prefill 0 1 1
85 }
86  
87 progress( phase => "PDF",
88 index => 0,
89 13 total => scalar(@{$sb->{songs}}) );
90  
91 13 $pr = (__PACKAGE__."::Writer")->new( $ps, $pdfapi );
92 warn("Generating PDF ", $options->{output} || "__new__.pdf", "...\n")
93 13 ▶ if $options->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $main::options->{"verbose"} 0 13 13
Condition0%0/2
$main::options->{'output'} || '__new__.pdf'
l!l
00
MC/DC0%0/1
$main::options->{'output'} || '__new__.pdf'
$main::options->{'output'}
Truth table0%0/2
A
A= $main::options->{'output'}
Aresult
11
00
94  
95 13 my $name = ::runtimeinfo("short");
96 13 ▶ $name =~ s/version.*/regression testing/ if $regtest;
Branch: 1 branch
BranchTrueFalseTotal
if $regtest 0 13 13
97 13 ▶ my %info = ( Title => $options->{title} || $sb->{songs}->[0]->{meta}->{title}->[0],
Condition33%1/3
$main::options->{'title'} || $sb->{'songs'}[0]{'meta'}{'title'}[0]
l!l&&r!l&&!r
0130
MC/DC0%0/2
$main::options->{'title'} || $sb->{'songs'}[0]{'meta'}{'title'}[0]
$main::options->{'title'} $sb->{'songs'}[0]{'meta'}{'title'}[0]
Truth table33%1/3
A || B
A= $main::options->{'title'}B= $sb->{'songs'}[0]{'meta'}{'title'}[0]
ABresult
1X1
011
000
98 Creator => $name );
99 13 ▶ while ( my ( $k, $v ) = each %{ $ps->{info} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$main::ps{"info"};}) 52 13 65
100 52 ▶ next unless defined($v) && $v ne "";
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v and $v ne "" 39 13 52
Condition66%2/3
defined $v and $v ne ""
!ll&&!rl&&r
03913
MC/DC50%1/2
defined $v and $v ne ""
defined $v $v ne ""
Truth table66%2/3
A and B
A= defined $vB= $v ne ""
ABresult
0X0
100
111
101 13 ▶ next if $k eq "title" && $options->{title} && $k eq '%{title}';
Branch: 1 branch
BranchTrueFalseTotal
if $k eq "title" and $main::options->{"title"} and $k eq "%{title}" 0 13 13
Condition33%2/6
$k eq "title" and $main::options->{"title"} and $k eq "%{title}"
!ll&&!rl&&r
1300
$k eq "title" and $main::options->{"title"}
!ll&&!rl&&r
0130
MC/DC0%0/3
$k eq "title" and $main::options->{"title"} and $k eq "%{title}"
$k eq "title" $main::options->{"title"} $k eq "%{title}"
Truth table25%1/4
A and B and C
A= $k eq "title"B= $main::options->{"title"}C= $k eq "%{title}"
ABCresult
0XX0
10X0
1100
1111
102 13 $info{ucfirst($k)} = fmt_subst( $sb->{songs}->[0], $v );
103 }
104  
105 13 $info{PageCtrl} = pagectrl_msg($pagectrl);
106 13 $pr->info(%info);
107  
108 # The resultant songbook consists of 5 parts:
109 # 1, The cover. PDF doc or cho template.
110 # 2. The front matter. PDF doc or cho template.
111 # 3. The table of contents. May be templated.
112 # 4. The songs.
113 # 5. The back matter. PDF doc.
114 # All parts except the songs are optional.
115 13 my ( %start_of, %pages_of );
116 13 for ( qw( cover front toc songbook back ) ) {
117 65 $start_of{$_} = 1;
118 65 $pages_of{$_} = 0;
119 }
120  
121 # The songbook...
122 13 my @book;
123  
124 # Page number in the PDF (for now, later we'll prepend tocs etc.).
125 # Note that PDF page numbers start at 1.
126 13 my $page = 1;
127 # Logical page number offset.
128 13 ▶ my $page_offset = ( $options->{'start-page-number'} || 1 ) - 1;
Condition100%2/2
$main::options->{'start-page-number'} || 1
l!l
112
MC/DC100%1/1
$main::options->{'start-page-number'} || 1
$main::options->{'start-page-number'}
Truth table100%2/2
A
A= $main::options->{'start-page-number'}
Aresult
11
00
129 13 ▶ $page_offset++ if $prefill && is_even($page_offset);
Branch: 1 branch
BranchTrueFalseTotal
if $prefill and is_even($page_offset) 2 11 13
Condition66%2/3
$prefill and is_even($page_offset)
!ll&&!rl&&r
1102
MC/DC50%1/2
$prefill and is_even($page_offset)
$prefill is_even($page_offset)
Truth table66%2/3
A and B
A= $prefillB= is_even($page_offset)
ABresult
0X0
100
111
130  
131 # if ( $pagectrl->{dual_pages} && is_odd($page_offset) ) {
132 # warn("Warning: Specifying an even start page when ".
133 # "pdf.odd-even-pages is in effect may yield surprising results.\n");
134 # }
135  
136 # If there is back matter, and it has even pages, force
137 # alignment of the final song as well.
138 13 my $back_matter;
139 my $force_align;
140 13 ▶ if ( $pagectrl->{back_matter} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pagectrl->{"back_matter"}) 4 9 13
141 4 $back_matter = $pdfapi->open( expand_tilde($pagectrl->{back_matter}) );
142 4 ▶ die("Missing back matter: ", $pagectrl->{back_matter}, "\n")
Branch: 1 branch
BranchTrueFalseTotal
unless $back_matter 0 4 4
143 unless $back_matter;
144 $force_align =
145 !( is_even($page_offset) xor is_even($back_matter->pages))
146 4 ▶ if $pagectrl->{align_songs_extend};
Branch: 1 branch
BranchTrueFalseTotal
if $pagectrl->{"align_songs_extend"} 0 4 4
Condition0%0/4
is_even($page_offset) ^^ is_even($back_matter->pages)
l&&rl&&!r!l&&r!l&&!r
0000
MC/DC0%0/2
is_even($page_offset) ^^ is_even($back_matter->pages)
is_even($page_offset) is_even($back_matter->pages)
Truth table0%0/4
A ^^ B
A= is_even($page_offset)B= is_even($back_matter->pages)
ABresult
110
101
011
000
147 }
148  
149 13 for my $songindex ( 1 .. @{$sb->{songs}} ) {
150 55 my $song = $sb->{songs}->[$songindex-1];
151 55 local $pagectrl->{align_songs_spread} = $pagectrl->{align_songs_spread};
152 55 ▶ $pagectrl->{align_songs_spread} = 1 if is_odd($page_offset);
Branch: 1 branch
BranchTrueFalseTotal
if is_odd($page_offset) 10 45 55
153  
154 # Align.
155 55 ▶ if ( $song->{meta}->{pages} ) { # 2nd pass
Branch: 1 branch
BranchTrueFalseTotal
if ($song->{'meta'}{'pages'}) { } 7 48 55
156 7 ▶ if ( ( ($page+$page_offset) % 2)
Branch: 1 branch
BranchTrueFalseTotal
if (($page + $page_offset) % 2 and $song->{"meta"}{"pages"} and $song->{"meta"}{"pages"} == 2) 0 7 7
Condition66%4/6
($page + $page_offset) % 2 and $song->{"meta"}{"pages"} and $song->{"meta"}{"pages"} == 2
!ll&&!rl&&r
610
($page + $page_offset) % 2 and $song->{"meta"}{"pages"}
!ll&&!rl&&r
601
MC/DC0%0/3
($page + $page_offset) % 2 and $song->{"meta"}{"pages"} and $song->{"meta"}{"pages"} == 2
($page + $page_offset) % 2 $song->{"meta"}{"pages"} $song->{"meta"}{"pages"} == 2
Truth table50%2/4
A and B and C
A= ($page + $page_offset) % 2B= $song->{"meta"}{"pages"}C= $song->{"meta"}{"pages"} == 2
ABCresult
0XX0
10X0
1100
1111
157 && $song->{meta}->{pages}
158 && $song->{meta}->{pages} == 2 ) {
159 0 $pr->newpage($page+1);
160 0 $page++;
161 }
162  
163 }
164 else {
165 48 $page += $pr->page_align( $pagectrl, "song$songindex", $page );
166 }
167  
168 55 $song->{meta}->{tocpage} = $page; # physical
169 55 push( @book, [ $song->{meta}->{title}->[0], $song ] );
170  
171 # Copy persistent assets into each of the songs.
172 0 ▶ if ( $sb->{assets} && %{$sb->{assets}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sb->{"assets"} and %{$sb->{"assets"};}) 0 55 55
Condition33%1/3
$sb->{"assets"} and %{$sb->{"assets"};}
!ll&&!rl&&r
5500
MC/DC0%0/2
$sb->{"assets"} and %{$sb->{"assets"};}
$sb->{"assets"} %{$sb->{"assets"};}
Truth table33%1/3
A and B
A= $sb->{"assets"}B= %{$sb->{"assets"};}
ABresult
0X0
100
111
173 0 ▶ $song->{assets} //= {};
Condition0%0/2
$song->{"assets"} //= {}
l!l
00
MC/DC0%0/1
$song->{"assets"} //= {}
$song->{"assets"}
Truth table0%0/2
A
A= $song->{"assets"}
Aresult
11
00
174 0 ▶ while ( my ($k,$v) = each %{$sb->{assets}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$sb{"assets"};}) 0 0 0
175 0 $song->{assets}->{$k} = $v;
176 }
177 }
178  
179 55 ▶ return unless progress( msg => $song->{meta}->{title}->[0] );
Branch: 1 branch
BranchTrueFalseTotal
unless progress("msg", $song->{"meta"}{"title"}[0]) 0 55 55
180  
181 55 ▶ $song->{meta}->{"chordpro.songsource"} //= $song->{source}->{file};
Condition33%1/3
$song->{"meta"}{"chordpro.songsource"} //= $song->{"source"}{"file"}
l!l&&r!l&&!r
0550
MC/DC0%0/2
$song->{"meta"}{"chordpro.songsource"} //= $song->{"source"}{"file"}
$song->{"meta"}{"chordpro.songsource"} $song->{"source"}{"file"}
Truth table33%1/3
A //= B
A= $song->{"meta"}{"chordpro.songsource"}B= $song->{"source"}{"file"}
ABresult
1X1
011
000
182 55 $pr->{bookmark} = "song_$songindex";
183 my $pages =
184 generate_song( $song,
185 { pr => $pr,
186 page_idx => $page,
187 page_num => $page+$page_offset,
188 songindex => $songindex,
189 55 numsongs => scalar(@{$sb->{songs}}),
190 forcealign => $force_align,
191 pagectrl => $pagectrl,
192 } );
193  
194 # Easy access to toc page.
195 55 $song->{meta}->{page} = $page+$page_offset;
196 55 ▶ if ( $song->{meta}->{bookmark} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($song->{'meta'}{'bookmark'}) { } 55 0 55
197 $pr->named_dest( $song->{meta}->{bookmark},
198 55 ▶ $pr->{pdf}->openpage($page)) if $pages;
Branch: 1 branch
BranchTrueFalseTotal
if $pages 55 0 55
199 }
200 else {
201 # Embedded PDF -> no toc.
202 0 $song->{meta}->{_TOC} = [ "no" ];
203 }
204 55 $page += $song->{meta}->{pages} = $pages;
205 }
206 13 $pages_of{songbook} = $page - 1;
207 13 $start_of{back} = $page;
208  
209 $::config->{contents} //=
210 [ { $::config->{toc}->{order} eq "alpha"
211 ? ( fields => [ "title" ] )
212 : ( fields => [ "songindex" ] ),
213 label => $::config->{toc}->{title},
214 13 ▶ line => $::config->{toc}->{line} } ];
Branch: 1 branch
BranchTrueFalseTotal
$main::config->{'toc'}{'order'} eq 'alpha' ? : 0 0 0
Condition50%1/2
$main::config->{"contents"} //= [{$main::config->{"toc"}{"order"} eq "alpha" ? ("fields", ["title"]) : ("fields", ["songindex"]), "label", $main::config->{"toc"}{"title"}, "line", $main::config->{"toc"}{"line"}}]
l!l
130
MC/DC0%0/1
$main::config->{"contents"} //= [{$main::config->{"toc"}{"order"} eq "alpha" ? ("fields", ["title"]) : ("fields", ["songindex"]), "label", $main::config->{"toc"}{"title"}, "line", $main::config->{"toc"}{"line"}}]
$main::config->{"contents"}
Truth table50%1/2
A
A= $main::config->{"contents"}
Aresult
11
00
215  
216 13 my @tocs = @{ $::config->{contents} };
217  
218 13 ▶ if ( $extra_matter ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($extra_matter) 13 0 13
219 13 progress( phase => "PDF(extra)",
220 index => 0,
221 total => $extra_matter );
222 }
223  
224 13 my $tocix;
225 my $frontmatter_songbook;
226 13 ▶ while ( @tocs ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@tocs) 39 13 52
227 39 my $ctl = shift(@tocs);
228 39 ▶ next unless $options->{toc} // @book > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $main::options->{"toc"} // @book > 1 12 27 39
Condition100%2/2
$main::options->{"toc"} // @book > 1
l!l
1227
MC/DC100%1/1
$main::options->{"toc"} // @book > 1
$main::options->{"toc"}
Truth table100%2/2
A
A= $main::options->{"toc"}
Aresult
11
00
229  
230 27 for ( qw( fields label line pageno ) ) {
231 108 ▶ next if exists $ctl->{$_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $ctl->{$_} 108 0 108
232 0 die("Config error: \"contents\" is missing \"$_\"\n");
233 }
234 27 ▶ next if $ctl->{omit};
Branch: 1 branch
BranchTrueFalseTotal
if $ctl->{"omit"} 9 18 27
235 18 $tocix++;
236  
237 18 my $book = prep_outlines( [ map { $_->[1] } @book ], $ctl );
238  
239 # Create a pseudo-song for the table of contents.
240 18 my $toctitle = fmt_subst( $book[0][-1], $ctl->{label} );
241 18 my $start = $start_of{songbook} - $page_offset;
242 # Templates for toc line and page.
243 18 my $tltpl = $ctl->{line};
244 18 my $pgtpl = $ctl->{pageno};
245  
246 18 my $song;
247 my $tmplfile;
248 18 ▶ if ( $ctl->{template} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ctl->{"template"}) 18 0 18
249 18 my $tpl = $ctl->{template};
250 18 ▶ if ( $tpl =~ /\.\w+/ ) { # file
Branch: 1 branch
BranchTrueFalseTotal
if ($tpl =~ /\.\w+/) { } 0 18 18
251 $tmplfile = CP->siblingres( $book[0][-1]->{source}->{file},
252 0 $tpl, class => "templates" );
253 0 ▶ warn("ToC template not found: $tpl\n") unless $tmplfile;
Branch: 1 branch
BranchTrueFalseTotal
unless $tmplfile 0 0 0
254 }
255 else {
256 18 $tmplfile = CP->findres( $tpl.".cho", class => "templates" );
257 18 ▶ if ( $verbose ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 18 18
258 0 ▶ warn("ToC template",
Branch: 1 branch
BranchTrueFalseTotal
$tmplfile ? : 0 0 0
259 $tmplfile ? " found: $tmplfile" : " not found: $tpl.cho\n")
260 }
261 }
262 }
263  
264 # Construct front matter songbook.
265 18 my $fmsb;
266 my $lines;
267 18 my $opts;
268 18 ▶ if ( $tmplfile ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tmplfile) { } 0 18 18
269 # Songbook from template file.
270 0 $opts = { fail => 'hard' };
271 0 $lines = fs_load( $tmplfile, $opts );
272 }
273 else {
274 18 $lines = [ "{title: $toctitle}" ];
275 18 $opts = { _filesource => "<builtin>" };
276 }
277 18 $fmsb = ChordPro::Songbook->new;
278 18 $fmsb->parse_file( $lines, { %$opts,
279 bookmark => "toc_$tocix",
280 generate => 'PDF' } );
281 18 for ( $fmsb->{songs}->[-1] ) {
282 $_->{title} = $_->{title}
283 ? fmt_subst( $book[0][-1], $_->{title} )
284 18 ▶ : $toctitle;
Branch: 1 branch
BranchTrueFalseTotal
$_->{'title'} ? : 18 0 18
285 18 ▶ $_->{meta}->{title} //= [ $_->{title} ];
Condition50%1/2
$_->{"meta"}{"title"} //= [$_->{"title"}]
l!l
180
MC/DC0%0/1
$_->{"meta"}{"title"} //= [$_->{"title"}]
$_->{"meta"}{"title"}
Truth table50%1/2
A
A= $_->{"meta"}{"title"}
Aresult
11
00
286 }
287  
288 18 my @songs = @{$fmsb->{songs}};
289  
290 # The first (of multiple) gets the global title/subtitle.
291 18 ▶ if ( @songs > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@songs > 1) 0 18 18
292 0 for ( $songs[0] ) {
293 $_->{meta}->{title} =
294 [ fmt_subst( $_, $options->{title} ) ]
295 0 ▶ if defined $options->{title};
Branch: 1 branch
BranchTrueFalseTotal
if defined $main::options->{"title"} 0 0 0
296 $_->{meta}->{subtitle} =
297 [ fmt_subst( $_, $options->{subtitle} ) ]
298 0 ▶ if defined $options->{subtitle};
Branch: 1 branch
BranchTrueFalseTotal
if defined $main::options->{"subtitle"} 0 0 0
299 0 $_->{title} = $_->{meta}->{title}->[0];
300 }
301 }
302  
303 # The last song gets the ToC appended.
304 18 $song = pop(@songs);
305  
306 18 my %prev;
307 18 ▶ if ( $ctl->{break} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ctl->{'break'}) { } 0 18 18
308 0 my $prevbreak = "";
309 0 ▶ $song->{body} //= [];
Condition0%0/2
$song->{"body"} //= []
l!l
00
MC/DC0%0/1
$song->{"body"} //= []
$song->{"body"}
Truth table0%0/2
A
A= $song->{"body"}
Aresult
11
00
310 0 for ( @$book ) {
311 0 my $break = fmt_subst( $_->[-1], $ctl->{break} );
312 0 my $nl = 0;
313 0 ▶ $nl++ while $break =~ s/^(\n|\\n)//;
Branch: 1 branch
BranchTrueFalseTotal
$break =~ s/^(\n|\\n)// and do { ++$nl } 0 0 0
314  
315 0 my $p = $pr->{pdf}->openpage($_->[-1]->{meta}->{tocpage});
316 0 ▶ if ( $nl && $break ne $prevbreak ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nl and $break ne $prevbreak) 0 0 0
Condition0%0/3
$nl and $break ne $prevbreak
!ll&&!rl&&r
000
MC/DC0%0/2
$nl and $break ne $prevbreak
$nl $break ne $prevbreak
Truth table0%0/3
A and B
A= $nlB= $break ne $prevbreak
ABresult
0X0
100
111
317 0 push( @{ $song->{body} },
318 { type => "empty",
319 context => "toc",
320 0 } ) for 1..$nl;
321 }
322 0 my $title = fmt_subst( $_->[-1], $tltpl );
323 0 my $pageno = fmt_subst( $_->[-1], $pgtpl );
324 0 ▶ %prev = () if $break ne $prevbreak;
Branch: 1 branch
BranchTrueFalseTotal
if $break ne $prevbreak 0 0 0
325 0 ▶ push( @{ $song->{body} },
Branch: 1 branch
BranchTrueFalseTotal
$break ne $prevbreak ? : 0 0 0
326 { type => "tocline",
327 context => "toc",
328 title => $title,
329 page => $p,
330 pageno => $pageno,
331 maybe break => ($break ne $prevbreak ? $break : undef),
332 } )
333 0 ▶ unless %prev && $prev{title} eq $title && $prev{pageno} eq $pageno;
Branch: 1 branch
BranchTrueFalseTotal
unless %prev and $prev{"title"} eq $title and $prev{"pageno"} eq $pageno 0 0 0
Condition0%0/6
%prev and $prev{"title"} eq $title and $prev{"pageno"} eq $pageno
!ll&&!rl&&r
000
%prev and $prev{"title"} eq $title
!ll&&!rl&&r
000
MC/DC0%0/3
%prev and $prev{"title"} eq $title and $prev{"pageno"} eq $pageno
%prev $prev{"title"} eq $title $prev{"pageno"} eq $pageno
Truth table0%0/4
A and B and C
A= %prevB= $prev{"title"} eq $titleC= $prev{"pageno"} eq $pageno
ABCresult
0XX0
10X0
1100
1111
334 0 $prevbreak = $break;
335 0 $prev{title} = $title;
336 0 $prev{pageno} = $pageno;
337 }
338 }
339 else {
340 18 ▶ push( @{ $song->{body} //= [] },
Condition50%1/2
$song->{"body"} //= []
l!l
018
MC/DC0%0/1
$song->{"body"} //= []
$song->{"body"}
Truth table50%1/2
A
A= $song->{"body"}
Aresult
11
00
341 18 map { my $p = $pr->{pdf}->openpage($_->[-1]->{meta}->{tocpage});
342 54 +{ type => "tocline",
343 context => "toc",
344 title => fmt_subst( $_->[-1], $tltpl ),
345 page => $p,
346 pageno => fmt_subst( $_->[-1], $pgtpl ),
347 } } @$book );
348 }
349  
350 18 ▶ $frontmatter_songbook //= ChordPro::Songbook->new;
Condition66%2/3
$frontmatter_songbook //= "ChordPro::Songbook"->new
l!l&&r!l&&!r
990
MC/DC0%0/2
$frontmatter_songbook //= "ChordPro::Songbook"->new
$frontmatter_songbook "ChordPro::Songbook"->new
Truth table66%2/3
A //= B
A= $frontmatter_songbookB= "ChordPro::Songbook"->new
ABresult
1X1
011
000
351 18 $frontmatter_songbook->add($_) for @songs;
352 18 $frontmatter_songbook->add($song);
353 }
354  
355 # Prepend the front matter songs.
356  
357 13 $force_align = $pagectrl->{align_songs_extend};
358 13 ▶ if ( $frontmatter_songbook && @{$frontmatter_songbook->{songs}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($frontmatter_songbook and @{$frontmatter_songbook->{"songs"};}) 9 4 13
Condition66%2/3
$frontmatter_songbook and @{$frontmatter_songbook->{"songs"};}
!ll&&!rl&&r
409
MC/DC50%1/2
$frontmatter_songbook and @{$frontmatter_songbook->{"songs"};}
$frontmatter_songbook @{$frontmatter_songbook->{"songs"};}
Truth table66%2/3
A and B
A= $frontmatter_songbookB= @{$frontmatter_songbook->{"songs"};}
ABresult
0X0
100
111
359 9 ▶ return unless progress( msg => "ToC" );
Branch: 1 branch
BranchTrueFalseTotal
unless progress("msg", "ToC") 0 9 9
360 9 $page = 1;
361  
362 9 my $toc = 0;
363 9 for ( @{$frontmatter_songbook->{songs}} ) {
364 # Localize song alignment settings.
365 local $pagectrl->{align_songs} =
366 18 $pagectrl->{align_tocs};
367 local $pagectrl->{align_songs_spread} =
368 $pagectrl->{align_tocs} eq "songs"
369 18 ▶ ? $pagectrl->{align_songs_spread} : 0;
Branch: 1 branch
BranchTrueFalseTotal
$pagectrl->{'align_tocs'} eq 'songs' ? : 0 18 18
370 local $pagectrl->{align_songs_extend} =
371 $pagectrl->{align_tocs} eq "songs"
372 18 ▶ ? $pagectrl->{align_songs_extend} : 0;
Branch: 1 branch
BranchTrueFalseTotal
$pagectrl->{'align_tocs'} eq 'songs' ? : 0 18 18
373  
374 18 $toc++;
375 18 $pr->{bookmark} = "toc_$toc";
376 # warn("TOC $toc $page\n");
377 # use DDP; p $pagectrl, as => "for toc";
378 18 $page += $pr->page_align( $pagectrl, "toc$toc", $page );
379 # warn("TOC $toc $page\n");
380 my $pages =
381 generate_song( $_,
382 { pr => $pr,
383 prepend => 1,
384 roman => 1,
385 page_idx => $page,
386 page_num => $page,
387 songindex => $toc,
388 18 numsongs => 0+@{$frontmatter_songbook->{songs}},
389 bookmark => $pr->{bookmark},
390 # forcealign => $force_align,
391 18 pagectrl => $pagectrl,
392 } );
393 $pr->named_dest( $_->{meta}->{bookmark},
394 18 ▶ $pr->{pdf}->openpage($page)) if $pages;
Branch: 1 branch
BranchTrueFalseTotal
if $pages 18 0 18
395 18 $page += $pages;
396 # warn("TOC $toc $page\n");
397 }
398 9 $pages_of{toc} = $page - 1;
399 9 $start_of{$_} += $page - 1 for qw( songbook back );
400 }
401  
402 13 ▶ if ( $pagectrl->{front_matter} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pagectrl->{"front_matter"}) 4 9 13
403 4 $page = 1;
404 4 my $matter = $pdfapi->open( expand_tilde($pagectrl->{front_matter}) );
405 4 ▶ die("Missing front matter: ", $pagectrl->{front_matter}, "\n") unless $matter;
Branch: 1 branch
BranchTrueFalseTotal
unless $matter 0 4 4
406 4 ▶ return unless progress( msg => "Front matter" );
Branch: 1 branch
BranchTrueFalseTotal
unless progress("msg", "Front matter") 0 4 4
407 4 for ( 1 .. $matter->pages ) {
408 4 $pr->{pdf}->import_page( $matter, $_, $_ );
409 4 $page++;
410 }
411 4 $pages_of{front} = $matter->pages;
412 4 $start_of{$_} += $page - 1 for qw( toc songbook back );
413 }
414  
415 # If we have a template, process it as a song and prepend.
416 13 my $covertpl;
417 13 ▶ if ( defined($options->{title}) && !@tocs ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $main::options->{"title"} and not @tocs) 0 13 13
Condition33%1/3
defined $main::options->{"title"} and not @tocs
!ll&&!rl&&r
1300
MC/DC0%0/2
defined $main::options->{"title"} and not @tocs
defined $main::options->{"title"} not @tocs
Truth table33%1/3
A and not(B)
A= defined $main::options->{"title"}B= not @tocs
ABresult
0X0
100
111
418 0 my $tpl = "cover";
419 0 $covertpl = CP->findres( "$tpl.cho", class => "templates" );
420 0 ▶ if ( $verbose ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 0 0
421 0 ▶ warn("Cover template",
Branch: 1 branch
BranchTrueFalseTotal
$covertpl ? : 0 0 0
422 $covertpl ? " found: $covertpl" : " not found: $tpl.cho\n")
423 }
424 }
425 13 ▶ if ( $covertpl ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($covertpl) { } 0 13 13
elsif (defined $pagectrl->{'cover'}) { } 1 12 13
426 0 my $page = 1;
427 0 my $opts = { fail => 'hard' };
428 0 my $lines = fs_load( $covertpl, $opts );
429 0 my $csb = ChordPro::Songbook->new;
430 0 $csb->parse_file( $lines, { %$opts,
431 generate => 'PDF' } );
432 0 for ( $csb->{songs}->[0] ) {
433 0 @{$_->{meta}}{ keys( %{$config->{meta}} ) } =
434 0 values ( %{$config->{meta}} );
435 $_->{meta}->{title} =
436 $options->{title} ?
437 0 ▶ [ $options->{title} ] : [ $_->{meta}->{title}->[0] ];
Branch: 1 branch
BranchTrueFalseTotal
$main::options->{'title'} ? : 0 0 0
438 $_->{meta}->{subtitle} =
439 $options->{subtitle} ?
440 0 ▶ [ $options->{subtitle} ] : $_->{meta}->{subtitle};
Branch: 1 branch
BranchTrueFalseTotal
$main::options->{'subtitle'} ? : 0 0 0
441 }
442 0 for ( @{$csb->{songs}} ) {
443 0 my $p =
444 generate_song( $_,
445 { pr => $pr,
446 prepend => 1,
447 roman => 1,
448 page_idx => $page,
449 page_num => $page,
450 songindex => 0,
451 numsongs => 1,
452 pagectrl => $pagectrl,
453 } );
454 0 $page += $p;
455 0 $start_of{$_} += $p for qw( songbook front toc back );
456 }
457 0 $pages_of{cover} = $page - 1;
458 }
459 elsif ( defined( $pagectrl->{cover} ) ) {
460 1 my $cover = $pdfapi->open( expand_tilde($pagectrl->{cover}) );
461 1 ▶ die("Missing cover: ", $pagectrl->{cover}, "\n") unless $cover;
Branch: 1 branch
BranchTrueFalseTotal
unless $cover 0 1 1
462 1 $page = 0;
463 1 ▶ return unless progress( msg => "Cover" );
Branch: 1 branch
BranchTrueFalseTotal
unless progress("msg", "Cover") 0 1 1
464 1 for ( 1 .. $cover->pages ) {
465 1 $page++;
466 1 $pr->{pdf}->import_page( $cover, $_, $page );
467 }
468 1 $pages_of{cover} = $page;
469 1 $start_of{$_} += $page for qw( songbook front toc back );
470 }
471  
472 # Back matter (if any) has already been opened.
473 13 ▶ if ( $back_matter ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($back_matter) 4 9 13
474 4 $page = $start_of{back};
475 4 ▶ return unless progress( msg => "Back matter" );
Branch: 1 branch
BranchTrueFalseTotal
unless progress("msg", "Back matter") 0 4 4
476 warn( "ASSERT: pages=", $pr->{pdf}->pages,
477 " back=", $start_of{back}, "\n" )
478 4 ▶ unless 1+$pr->{pdf}->pages == $start_of{back};
Branch: 1 branch
BranchTrueFalseTotal
unless 1 + $main::pr->{"pdf"}->pages == $start_of{"back"} 0 4 4
479 4 for ( 1 .. $back_matter->pages ) {
480 4 $pr->{pdf}->import_page( $back_matter, $_, $page );
481 4 $page++;
482 }
483 4 $pages_of{back} = $back_matter->pages;
484 }
485  
486 13 if ( 0 and $::config->{debug}->{pages} & 0x01 ) {
487 warn("-- pre alignment\n");
488 for ( qw( cover front toc songbook back ) ) {
489 warn( sprintf("%4d %-10s %s\n",
490 $start_of{$_}, $_,
491 plural( sprintf("%4d",$pages_of{$_})," page") ));
492 }
493 warn("-- final\n");
494 }
495  
496 # Alignment. Only if odd/even pages.
497 13 ▶ if ( $pagectrl->{dual_pages} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pagectrl->{"dual_pages"}) 11 2 13
498 11 my @parts = qw( front toc songbook back );
499 11 ▶ while ( @parts ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@parts) 44 11 55
500 44 my $part = shift(@parts);
501 44 ▶ next unless $pages_of{$part};
Branch: 1 branch
BranchTrueFalseTotal
unless $pages_of{$part} 18 26 44
502  
503 # Always align parts, regardless of pagealign-songs.
504 26 local $pagectrl->{align_songs} = 1;
505  
506 26 ▶ if ( @parts ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@parts) { } 22 4 26
507 22 ▶ if ( $pr->page_align( $pagectrl,
Branch: 3 branches
BranchTrueFalseTotal
if ($main::pr->page_align($pagectrl, $part, $start_of{$part}, $part eq "songbook" ? $prefill ? 1 : is_odd($page_offset) : 0)) 12 10 22
$part eq 'songbook' ? : 11 11 22
$prefill ? : 2 9 11
508 $part,
509 $start_of{$part},
510 $part eq "songbook"
511 ? $prefill
512 ? 1
513 : is_odd($page_offset)
514 : 0 ) ) {
515 12 $start_of{$_}++ for $part, @parts;
516 }
517 }
518 else {
519 $start_of{$part} +=
520 4 $pr->page_align( $pagectrl, $part, $start_of{$part},
521 is_odd($back_matter->pages) );
522 }
523 }
524 }
525  
526 13 ▶ if ( $::config->{debug}->{pages} & 0x01 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main::config->{"debug"}{"pages"} & 1) 0 13 13
527 0 for ( qw( cover front toc songbook back ) ) {
528 warn( sprintf("%4d %-10s %s\n",
529 $start_of{$_}, $_,
530 0 plural( sprintf("%4d",$pages_of{$_})," page") ));
531 }
532 }
533  
534 # Note that the page indices run from zero.
535 13 $pr->pagelabel( 0, 'arabic', 'cover-' );
536 $pr->pagelabel( $start_of{front}-1, 'arabic', 'front-' )
537 13 ▶ if $pages_of{front};
Branch: 1 branch
BranchTrueFalseTotal
if $pages_of{"front"} 4 9 13
538 $pr->pagelabel( $start_of{toc}-1, 'roman' )
539 13 ▶ if $pages_of{toc};
Branch: 1 branch
BranchTrueFalseTotal
if $pages_of{"toc"} 9 4 13
540 # Label song pages according to the user visible number.
541 $pr->pagelabel( $start_of{songbook}-1, 'arabic', '', ,
542 $options->{'start-page-number'} || 1 )
543 13 ▶ if $pages_of{songbook};
Branch: 1 branch
BranchTrueFalseTotal
if $pages_of{"songbook"} 13 0 13
Condition100%2/2
$main::options->{'start-page-number'} || 1
l!l
112
MC/DC100%1/1
$main::options->{'start-page-number'} || 1
$main::options->{'start-page-number'}
Truth table100%2/2
A
A= $main::options->{'start-page-number'}
Aresult
11
00
544 $pr->pagelabel( $start_of{back}-1, 'arabic', 'back-' )
545 13 ▶ if $pages_of{back};
Branch: 1 branch
BranchTrueFalseTotal
if $pages_of{"back"} 4 9 13
546  
547 # Add the bookmarks.
548 13 for ( qw( cover front toc back ) ) {
549 52 ▶ next unless $pages_of{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless $pages_of{$_} 34 18 52
550 18 my $p = $pr->{pdf}->openpage( $start_of{$_} );
551 18 $pr->named_dest( $_, $p );
552 }
553  
554 # Add the outlines.
555 13 $pr->make_outlines( [ map { $_->[1] } @book ], $start_of{songbook} );
556  
557 13 ▶ $pr->finish( $options->{output} || "__new__.pdf" );
Condition50%1/2
$main::options->{'output'} || '__new__.pdf'
l!l
130
MC/DC0%0/1
$main::options->{'output'} || '__new__.pdf'
$main::options->{'output'}
Truth table50%1/2
A
A= $main::options->{'output'}
Aresult
11
00
558 13 ▶ warn("Generated PDF...\n") if $options->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $main::options->{"verbose"} 0 13 13
559  
560 13 ▶ if ( $options->{csv} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main::options->{"csv"}) 13 0 13
561 13 ▶ return unless progress( msg => "CSV" );
Branch: 1 branch
BranchTrueFalseTotal
unless progress("msg", "CSV") 0 13 13
562 13 generate_csv( \@book, $page, \%pages_of, \%start_of )
563 }
564  
565 13 []
566 }
567  
568 13 ▶sub generate_csv {
Subroutine generate_csv: called CC=19 SCAR=30.8
Pod undocumented
569 13 my ( $book, $page, $pages_of, $start_of ) = @_;
570  
571 # Create an MSPro compatible CSV for this PDF.
572 13 push( @$book, [ "CSV", { meta => { tocpage => $page } } ] );
573 13 my $csv = CP->sibling( $options->{output}, ext => ".csv" );
574 13 ▶ my $fd = fs_open( $csv, '>:utf8' )
Branch: 1 branch
BranchTrueFalseTotal
my $fd = fs_open($csv, ">:utf8") or die($csv, ": $!\n") 13 0 13
575 or die( $csv, ": $!\n" );
576  
577 warn("Generating CSV $csv...\n")
578 13 ▶ if $config->{debug}->{csv} || $options->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $main::config->{"debug"}{"csv"} or $main::options->{"verbose"} 0 13 13
Condition33%1/3
$main::config->{"debug"}{"csv"} or $main::options->{"verbose"}
l!l&&r!l&&!r
0013
MC/DC0%0/2
$main::config->{"debug"}{"csv"} or $main::options->{"verbose"}
$main::config->{"debug"}{"csv"} $main::options->{"verbose"}
Truth table33%1/3
A or B
A= $main::config->{"debug"}{"csv"}B= $main::options->{"verbose"}
ABresult
1X1
011
000
579  
580 13 $ps = $config->{pdf};
581 13 my $ctl = $ps->{csv};
582 13 ▶ my $sep = $ctl->{separator} // ";";
Condition50%1/2
$ctl->{"separator"} // ";"
l!l
130
MC/DC0%0/1
$ctl->{"separator"} // ";"
$ctl->{"separator"}
Truth table50%1/2
A
A= $ctl->{"separator"}
Aresult
11
00
583 13 ▶ my $vsep = $ctl->{vseparator} // "|";
Condition50%1/2
$ctl->{"vseparator"} // "|"
l!l
130
MC/DC0%0/1
$ctl->{"vseparator"} // "|"
$ctl->{"vseparator"}
Truth table50%1/2
A
A= $ctl->{"vseparator"}
Aresult
11
00
584  
585 648 ▶ my $rfc4180 = sub {
Subroutine __ANON__: called CC=5 SCAR=19.6
586 648 my ( $v ) = @_;
587 648 ▶ $v = [$v] unless is_arrayref($v);
Branch: 1 branch
BranchTrueFalseTotal
unless is_arrayref($v) 648 0 648
588 648 ▶ return "" unless defined($v) && defined($v->[0]);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v and defined $v->[0] 0 648 648
Condition33%1/3
defined $v and defined $v->[0]
!ll&&!rl&&r
00648
MC/DC0%0/2
defined $v and defined $v->[0]
defined $v defined $v->[0]
Truth table33%1/3
A and B
A= defined $vB= defined $v->[0]
ABresult
0X0
100
111
589 648 $v = join( $sep, @$v );
590 648 ▶ return $v unless $v =~ m/[$sep"\n\r]/s;
Branch: 1 branch
BranchTrueFalseTotal
unless $v =~ /[$sep"\n\r]/s 648 0 648
591 0 $v =~ s/"/""/g;
592 0 return '"' . $v . '"';
593 13 };
594  
595 68 ▶ my $pagerange = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
596 68 my ( $pages, $page ) = @_;
597 68 ▶ if ( @_ == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) 13 55 68
598 13 $pages = $pages_of->{$_[0]};
599 13 $page = $start_of->{$_[0]};
600 }
601 68 ▶ $pages > 1
Branch: 1 branch
BranchTrueFalseTotal
$pages > 1 ? : 24 44 68
602 ? ( $page ."-". ($page+$pages-1) )
603 : $page,
604 13 };
605  
606 68 ▶ my $csvline = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
607 68 my ( $m ) = @_;
608 68 my @cols = ();
609 68 for ( @{ $ctl->{fields} } ) {
610 612 ▶ next if $_->{omit};
Branch: 1 branch
BranchTrueFalseTotal
if $_->{"omit"} 68 544 612
611 544 ▶ my $v = $_->{value} // '%{'.$_->{meta}.'}';
Condition50%1/2
$_->{"value"} // '%{' . $_->{"meta"} . '}'
l!l
0544
MC/DC0%0/1
$_->{"value"} // '%{' . $_->{"meta"} . '}'
$_->{"value"}
Truth table50%1/2
A
A= $_->{"value"}
Aresult
11
00
612 544 local( $config->{metadata}->{separator} ) = $vsep;
613 544 push( @cols, $rfc4180->( fmt_subst( { meta => $m }, $v ) ) );
614 }
615 68 print $fd ( join( $sep, @cols ), "\n" );
616 68 scalar(@cols);
617 13 };
618  
619 13 my @cols;
620 my $ncols;
621 13 for ( @{ $ctl->{fields} } ) {
622 117 ▶ next if $_->{omit};
Branch: 1 branch
BranchTrueFalseTotal
if $_->{"omit"} 13 104 117
623 104 push( @cols, $rfc4180->($_->{name}) );
624 }
625 13 $ncols = @cols;
626 #warn( "CSV: $ncols fields\n" );
627 13 print $fd ( join( $sep, @cols ), "\n" );
628  
629 # Extra meta info from command line, for non-song CSV.
630 13 ▶ my $xm = $options->{meta} // {};
Condition50%1/2
$main::options->{"meta"} // {}
l!l
013
MC/DC0%0/1
$main::options->{"meta"} // {}
$main::options->{"meta"}
Truth table50%1/2
A
A= $main::options->{"meta"}
Aresult
11
00
631 13 ▶ unless ( $ctl->{songsonly} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ctl->{"songsonly"}) 4 9 13
632 $csvline->( { %$xm,
633 title => 'Cover',
634 pagerange => $pagerange->("cover"),
635 sorttitle => 'Cover',
636 artist => 'ChordPro' } )
637 4 ▶ if $pages_of->{cover};
Branch: 1 branch
BranchTrueFalseTotal
if $pages_of->{"cover"} 1 3 4
638 $csvline->( { %$xm,
639 title => 'Front Matter',
640 pagerange => $pagerange->("front"),
641 sorttitle => 'Front Matter',
642 artist => 'ChordPro' } )
643 4 ▶ if $pages_of->{front};
Branch: 1 branch
BranchTrueFalseTotal
if $pages_of->{"front"} 4 0 4
644 $csvline->( { %$xm,
645 title => "Table of Contents",
646 pagerange => $pagerange->("toc"),
647 sorttitle => "Table of Contents",
648 artist => 'ChordPro' } )
649 4 ▶ if $pages_of->{toc};
Branch: 1 branch
BranchTrueFalseTotal
if $pages_of->{"toc"} 4 0 4
650 }
651  
652 warn( "CSV: ", scalar(@$book), " songs in book\n")
653 13 ▶ if $config->{debug}->{csv};
Branch: 1 branch
BranchTrueFalseTotal
if $main::config->{"debug"}{"csv"} 0 13 13
654 13 ▶ for ( my $p = 0; $p < @$book-1; $p++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p < @$book - 1) 55 13 68
655 55 my ( $title, $song ) = @{$book->[$p]};
656 55 my $page = $start_of->{songbook} + $song->{meta}->{tocpage} - 1;
657 55 my $pp = $song->{meta}->{pages};
658 55 my $m = { %{$song->{meta}},
659 pagerange => [ $pagerange->($pp, $page) ] };
660 55 $csvline->($m);
661 }
662  
663 13 ▶ unless ( $ctl->{songsonly} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ctl->{"songsonly"}) 4 9 13
664 $csvline->( { %$xm,
665 title => 'Back Matter',
666 pagerange => $pagerange->("back"),
667 sorttitle => 'Back Matter',
668 artist => 'ChordPro'} )
669 4 ▶ if $pages_of->{back};
Branch: 1 branch
BranchTrueFalseTotal
if $pages_of->{"back"} 4 0 4
670 }
671 13 close($fd);
672 warn("Generated CSV...\n")
673 13 ▶ if $config->{debug}->{csv} || $options->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $main::config->{"debug"}{"csv"} or $main::options->{"verbose"} 0 13 13
Condition33%1/3
$main::config->{"debug"}{"csv"} or $main::options->{"verbose"}
l!l&&r!l&&!r
0013
MC/DC0%0/2
$main::config->{"debug"}{"csv"} or $main::options->{"verbose"}
$main::config->{"debug"}{"csv"} $main::options->{"verbose"}
Truth table33%1/3
A or B
A= $main::config->{"debug"}{"csv"}B= $main::options->{"verbose"}
ABresult
1X1
011
000
674 }
675  
676 ################ ################
677  
678 0 ▶sub _dump {
Subroutine _dump: not called CC=7 SCAR=40.3
679 0 ▶ return unless $config->{debug}->{fonts};
Branch: 1 branch
BranchTrueFalseTotal
unless $main::config->{"debug"}{"fonts"} 0 0 0
680 0 my ( $ps ) = @_;
681 0 print STDERR ("== Font family map\n");
682 0 ▶ Text::Layout::FontConfig->new->_dump if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
683 0 print STDERR ("== Font associations\n");
684 0 foreach my $f ( sort keys( %{$ps->{fonts}} ) ) {
685 printf STDERR ("%-15s %s\n", $f,
686 0 eval { $ps->{fonts}->{$f}->{description} } ||
687 0 eval { $ps->{fonts}->{$f}->{file} } ||
688 0 ▶ eval { "[".$ps->{fonts}->{$f}->{name}."]" } ||
Condition0%0/8
eval { do { $ps->{'fonts'}{$f}{'description'} } } || eval { do { $ps->{'fonts'}{$f}{'file'} } } || eval { do { '[' . $ps->{'fonts'}{$f}{'name'} . ']' } } || '[]'
l!l
00
eval { do { $ps->{"fonts"}{$f}{"description"} } } or eval { do { $ps->{"fonts"}{$f}{"file"} } } or eval { do { '[' . $ps->{"fonts"}{$f}{"name"} . ']' } }
l!l&&r!l&&!r
000
eval { do { $ps->{"fonts"}{$f}{"description"} } } or eval { do { $ps->{"fonts"}{$f}{"file"} } }
l!l&&r!l&&!r
000
MC/DC0%0/3
eval { do { $ps->{'fonts'}{$f}{'description'} } } || eval { do { $ps->{'fonts'}{$f}{'file'} } } || eval { do { '[' . $ps->{'fonts'}{$f}{'name'} . ']' } } || '[]'
eval { do { $ps->{"fonts"}{$f}{"description"} } } eval { do { $ps->{"fonts"}{$f}{"file"} } } eval { do { '[' . $ps->{"fonts"}{$f}{"name"} . ']' } }
Truth table0%0/4
A or B or C
A= eval { do { $ps->{"fonts"}{$f}{"description"} } }B= eval { do { $ps->{"fonts"}{$f}{"file"} } }C= eval { do { '[' . $ps->{"fonts"}{$f}{"name"} . ']' } }
ABCresult
1XX1
01X1
0011
0000
689 "[]"
690 );
691 }
692 }
693  
694 # Derive new style page controls from old style.
695 13 ▶sub pagectrl {
Subroutine pagectrl: called CC=7 SCAR=19.5
Pod undocumented
696 13 my ( $self ) = @_;
697  
698 # If at this point we still have old style page controls,
699 # they were passed via command line and thus override.
700 # $config->migrate_songbook_pagectrl;
701  
702 13 my $sb = $config->{pdf}->{songbook};
703 my $pagectrl = { dual_pages => $sb->{'dual-pages'},
704 align_tocs => $sb->{'align-tocs'},
705 align_songs => $sb->{'align-songs'},
706 align_songs_spread => $sb->{'align-songs-spread'},
707 align_songs_extend => $sb->{'align-songs-extend'},
708 sort_songs => $sb->{'sort-songs'},
709 compact_songs => $sb->{'compact-songs'},
710 cover => $sb->{cover},
711 front_matter => $sb->{'front-matter'},
712 13 back_matter => $sb->{'back-matter'},
713 };
714  
715 13 ▶ unless ( $pagectrl->{dual_pages} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pagectrl->{"dual_pages"}) 2 11 13
716 2 $pagectrl->{align_songs} = 0;
717 2 $pagectrl->{align_tocs} = 0;
718 }
719 13 ▶ unless ( $pagectrl->{align_songs} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pagectrl->{"align_songs"}) 3 10 13
720 $pagectrl->{$_} = 0
721 3 for qw( align_songs_spread align_songs_extend compact_songs);
722 }
723 13 for ( qw( cover front_matter back_matter ) ) {
724 39 ▶ $pagectrl->{$_} = undef unless is_true($pagectrl->{$_});
Branch: 1 branch
BranchTrueFalseTotal
unless is_true($pagectrl->{$_}) 30 9 39
725 }
726 13 ▶ if ( $config->{debug}->{pagectrl} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main::config->{"debug"}{"pagectrl"}) 0 13 13
727 0 ▶ use DDP; p $pagectrl, as => "pagectrl";
Subroutine BEGIN: called CC=1 SCAR=0.2
728 }
729 13 return $pagectrl;
730 }
731  
732 13 ▶sub pagectrl_msg {
Subroutine pagectrl_msg: called CC=8 SCAR=20.8
Pod undocumented
733 13 my ( $pagectrl ) = @_;
734 13 ▶ my $msg = $pagectrl->{dual_pages} ? "dual" : "single";
Branch: 1 branch
BranchTrueFalseTotal
$pagectrl->{'dual_pages'} ? : 11 2 13
735 13 ▶ if ( $pagectrl->{align_tocs} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pagectrl->{"align_tocs"}) 11 2 13
736 11 $msg .= ", align_tocs";
737 11 ▶ $msg .= "_song" if $pagectrl->{align_tocs} eq "song";
Branch: 1 branch
BranchTrueFalseTotal
if $pagectrl->{"align_tocs"} eq "song" 0 11 11
738 }
739 13 ▶ if ( $pagectrl->{align_songs} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pagectrl->{"align_songs"}) 10 3 13
740 10 $msg .= ", align_songs";
741 10 ▶ $msg .= ", extend" if $pagectrl->{align_songs_extend};
Branch: 1 branch
BranchTrueFalseTotal
if $pagectrl->{"align_songs_extend"} 0 10 10
742 10 ▶ $msg .= ", spread" if $pagectrl->{align_songs_spread};
Branch: 1 branch
BranchTrueFalseTotal
if $pagectrl->{"align_songs_spread"} 3 7 10
743 }
744 13 ▶ $msg .= ", " . $pagectrl->{sort_songs} if $pagectrl->{sort_songs};
Branch: 1 branch
BranchTrueFalseTotal
if $pagectrl->{"sort_songs"} 3 10 13
745  
746 13 return $msg;
747 }
748  
749 3 ▶sub sort_songbook {
Subroutine sort_songbook: called CC=9 SCAR=23.0
Pod undocumented
750 3 my ( $sb, $pagectrl ) = @_;
751 3 ▶ return unless my $sorting = $pagectrl->{sort_songs};
Branch: 1 branch
BranchTrueFalseTotal
unless my $sorting = $pagectrl->{"sort_songs"} 0 3 3
752  
753 3 foreach my $song ( @{$sb->{songs}} ) {
754 21 ▶ if (!defined($song->{meta}->{sorttitle})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $song->{"meta"}{"sorttitle"}) 18 3 21
755 18 $song->{meta}->{sorttitle} = $song->{meta}->{title};
756 }
757 }
758  
759 3 my @songlist = @{$sb->{songs}};
760  
761 3 my @tbs; # to be sorted
762 3 my $desc = 0; # descending
763 3 ▶ if ( $sorting =~ /^([-+]?)title$/i ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($sorting =~ /^([-+]?)title$/i) { } 3 0 3
elsif ($sorting =~ /^([-+]?)subtitle$/i) { } 0 0 0
764 3 $desc = $1 eq "-";
765 3 @tbs = map { [ $_->{meta}->{sorttitle}->[0], $_ ] } @songlist;
766 }
767 elsif ( $sorting =~ /^([-+]?)subtitle$/i ) {
768 0 $desc = $1 eq "-";
769 0 @tbs = map { [ $_->{meta}->{subtitle}->[0], $_ ] } @songlist;
770 }
771 3 ▶ return unless @tbs;
Branch: 1 branch
BranchTrueFalseTotal
unless @tbs 0 3 3
772  
773 3 if ( 1 ) {
774 3 my $collator = Unicode::Collate->new;
775 3 ▶ my ( $aa, $bb ) = $desc ? qw( b a ) : qw( a b );
Branch: 1 branch
BranchTrueFalseTotal
$desc ? : 0 3 3
776 3 my $l = "\$$aa"."->[0]";
777 3 my $r = "\$$bb"."->[0]";
778 3 my $proc = 'sub { my $tbs = shift; ';
779 3 $proc .= '[ map { $_->[1] } sort { ';
780 3 $proc .= "\$collator->cmp( $l, $r )";
781 3 $proc .= ' } @$tbs ] }';
782 3 my $sorter = eval $proc;
783 3 ▶ die("OOPS $proc\n$@") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 3 3
784 3 $sb->{songs} = $sorter->(\@tbs);
785 }
786 else {
787 my $proc = 'sub { my $tbs = shift; use locale; ';
788 $proc .= '[ map { $_->[1] } sort { ';
789 $proc .= $desc ? '$b->[0] cmp $a->[0]' : '$a->[0] cmp $b->[0]';
790 $proc .= ' } @$tbs ] }';
791 my $sorter = eval $proc;
792 die("OOPS $proc\n$@") if $@;
793 $sb->{songs} = $sorter->(\@tbs);
794 }
795 }
796  
797 1 ▶sub compact_songbook {
Subroutine compact_songbook: called CC=21 SCAR=31.2
Pod undocumented
798 1 my ( $sb, $pagectrl ) = @_;
799 1 ▶ return 0 unless $pagectrl->{compact_songs};
Branch: 1 branch
BranchTrueFalseTotal
unless $pagectrl->{"compact_songs"} 0 1 1
800  
801 1 my $ps = $config->{pdf};
802 1 my $pri = ( __PACKAGE__."::Writer" )->new( $ps, $pdfapi );
803  
804 # Count pages to properly align multi-page songs without
805 # needing to turn page.
806 1 ▶ my $page = $options->{"start-page-number"} ||= 1;
Condition50%1/2
$main::options->{"start-page-number"} ||= 1
l!l
01
MC/DC0%0/1
$main::options->{"start-page-number"} ||= 1
$main::options->{"start-page-number"}
Truth table50%1/2
A
A= $main::options->{"start-page-number"}
Aresult
11
00
807  
808 1 foreach my $song ( @{$sb->{songs}} ) {
809 7 ▶ if (!defined($song->{meta}->{sorttitle})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $song->{"meta"}{"sorttitle"}) 0 7 7
810 0 $song->{meta}->{sorttitle} = $song->{meta}->{title};
811 }
812 }
813  
814 1 my @songlist = @{$sb->{songs}};
815 1 my $filler = 0; # filler for 2page
816  
817 # Progress indicator
818 progress( phase => "Counting",
819 index => 0,
820 1 total => scalar(@{$sb->{songs}}) );
821  
822 1 my $i = 1;
823 1 foreach my $song ( @songlist ) {
824 7 ▶ return unless progress( msg => $song->{title} );
Branch: 1 branch
BranchTrueFalseTotal
unless progress("msg", $song->{"title"}) 0 7 7
825 7 $i++;
826  
827 #### HACK ATTACK.
828 # Assets will be rendered, but then they are part of the temp
829 # PDF, not the final one.
830 # We copy the unprocessed assets and restore after the 1st pass.
831 12 ▶ use Storable qw(dclone);
Subroutine BEGIN: called CC=1 SCAR=0.0
832 7 my $assets;
833 7 ▶ $assets = dclone( $song->{assets} ) if $song->{assets};
Branch: 1 branch
BranchTrueFalseTotal
if $song->{"assets"} 0 7 7
834 ####
835  
836 $song->{meta}->{pages} =
837 7 generate_song( $song,
838 { pr => $pri,
839 startpage => 1,
840 pagectrl => $pagectrl,
841 } );
842 ####
843 7 ▶ $song->{assets} = $assets if $assets;
Branch: 1 branch
BranchTrueFalseTotal
if $assets 0 7 7
844 ####
845 }
846  
847 1 my @new;
848 1 my $used = "";
849 # First an arbitrary odd-pages song.
850 1 ▶ for ( my $i=0; $i < @songlist; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @songlist) 4 0 4
851 4 ▶ next unless is_odd( $options->{'start-page-number'}||1 );
Branch: 1 branch
BranchTrueFalseTotal
unless is_odd($main::options->{"start-page-number"} || 1) 0 4 4
Condition50%1/2
$main::options->{'start-page-number'} || 1
l!l
40
MC/DC0%0/1
$main::options->{'start-page-number'} || 1
$main::options->{'start-page-number'}
Truth table50%1/2
A
A= $main::options->{'start-page-number'}
Aresult
11
00
852 4 ▶ next unless is_odd($songlist[$i]->{meta}->{pages});
Branch: 1 branch
BranchTrueFalseTotal
unless is_odd($songlist[$i]{"meta"}{"pages"}) 3 1 4
853 1 push( @new, $songlist[$i] );
854 1 vec( $used, $i, 1 ) = 1;
855 1 last;
856 }
857 ##### TODO: If still empty, need filler.
858 1 ▶ $filler++ unless @new;
Branch: 1 branch
BranchTrueFalseTotal
unless @new 0 1 1
859  
860 # Then all even-pages songs.
861 1 ▶ for ( my $i=0; $i < @songlist; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @songlist) 7 1 8
862 7 ▶ next if vec( $used, $i, 1 );
Branch: 1 branch
BranchTrueFalseTotal
if vec $used, $i, 1 1 6 7
863 6 ▶ next unless is_even($songlist[$i]->{meta}->{pages});
Branch: 1 branch
BranchTrueFalseTotal
unless is_even($songlist[$i]{"meta"}{"pages"}) 1 5 6
864 5 push( @new, $songlist[$i] );
865 5 vec( $used, $i, 1 ) = 1;
866 }
867  
868 # Finally, all other odd-pages songs.
869 1 ▶ for ( my $i=0; $i < @songlist; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @songlist) 7 1 8
870 7 ▶ next if vec( $used, $i, 1 );
Branch: 1 branch
BranchTrueFalseTotal
if vec $used, $i, 1 6 1 7
871 1 ▶ next unless is_odd($songlist[$i]->{meta}->{pages});
Branch: 1 branch
BranchTrueFalseTotal
unless is_odd($songlist[$i]{"meta"}{"pages"}) 0 1 1
872 1 push( @new, $songlist[$i] );
873 1 vec( $used, $i, 1 ) = 1;
874 }
875  
876 1 ▶ die("compact ", scalar(@new), " <> ", scalar(@songlist), "!\n")
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @new == scalar @songlist 0 1 1
877 unless scalar(@new) == scalar(@songlist);
878  
879 1 @songlist = @new;
880  
881 1 $sb->{songs} = [@songlist];
882  
883 1 return $filler;
884 }
885  
886 0 ▶sub diagrammer {
Subroutine diagrammer: not called CC=2 SCAR=17.9
Pod undocumented
887 0 my ( $type ) = @_;
888 0 my $p;
889 0 ▶ if ( $type eq "keyboard" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'keyboard') { } 0 0 0
890 0 require ChordPro::Output::PDF::KeyboardDiagram;
891 0 $p = ChordPro::Output::PDF::KeyboardDiagram->new( pr => $pr );
892 }
893 else {
894 0 require ChordPro::Output::PDF::StringDiagram;
895 0 $p = ChordPro::Output::PDF::StringDiagram->new( pr => $pr );
896 }
897 0 return $p;
898 }
899  
900 12 ▶use Object::Pad;
Subroutine BEGIN: called CC=1 SCAR=0.0
901  
902 12 ▶class TextLayoutImageElement :isa(Text::Layout::PDFAPI2::ImageElement);
Subroutine BEGIN: called CC=1 SCAR=0.0
903  
904 12 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
905  
906 12 ▶use Text::ParseWords qw( shellwords );
Subroutine BEGIN: called CC=1 SCAR=0.0
907  
908 0 ▶method parse( $ctx, $k, $v ) {
Subroutine parse: not called CC=26 SCAR=65.5
909  
910 0 my %ctl = ( type => "img", %$ctx );
911 0 my $err;
912  
913 # Split the attributes.
914 0 foreach my $kk ( shellwords($v) ) {
915  
916 # key=value
917 0 ▶ if ( $kk =~ /^([-\w]+)=(.+)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kk =~ /^([-\w]+)=(.+)$/) { } 0 0 0
918 0 my ( $k, $v ) = ( $1, $2 );
919  
920 # Ignore case unless required.
921 0 ▶ $v = lc $v unless $k =~ /^(id|chord|src)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $k =~ /^(id|chord|src)$/ 0 0 0
922  
923 0 ▶ if ( $k =~ /^(chord|src)$/ ) {
Branch: 6 branches
BranchTrueFalseTotal
if ($k =~ /^(chord|src)$/) { } 0 0 0
elsif ($k =~ /^(id|bbox)$/) { } 0 0 0
elsif ($k eq 'align' and $v =~ /^(left|right|center)$/) { } 0 0 0
elsif ($k eq 'type' and $v =~ /^(strings?|keyboard)$/) { } 0 0 0
elsif ($k =~ /^(width|height|dx|dy|w|h)$/) { } 0 0 0
elsif ($k =~ /^(scale)$/) { } 0 0 0
924 0 ▶ if ( $v =~ /^(chord|builtin):/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v =~ /^(chord|builtin):/) 0 0 0
925 0 $k = $1;
926 0 $v = $';
927 }
928 0 $ctl{$k} = $v;
929 }
930 elsif ( $k =~ /^(id|bbox)$/ ) {
931 0 $ctl{$k} = $v;
932 }
933 ▶ elsif ( $k eq "align" && $v =~ /^(left|right|center)$/ ) {
Condition0%0/3
$k eq "align" and $v =~ /^(left|right|center)$/
!ll&&!rl&&r
000
MC/DC0%0/2
$k eq "align" and $v =~ /^(left|right|center)$/
$k eq "align" $v =~ /^(left|right|center)$/
Truth table0%0/3
A and B
A= $k eq "align"B= $v =~ /^(left|right|center)$/
ABresult
0X0
100
111
934 0 $ctl{$k} = $v;
935 }
936 ▶ elsif ( $k eq "type" && $v =~ /^(strings?|keyboard)$/ ) {
Condition0%0/3
$k eq "type" and $v =~ /^(strings?|keyboard)$/
!ll&&!rl&&r
000
MC/DC0%0/2
$k eq "type" and $v =~ /^(strings?|keyboard)$/
$k eq "type" $v =~ /^(strings?|keyboard)$/
Truth table0%0/3
A and B
A= $k eq "type"B= $v =~ /^(strings?|keyboard)$/
ABresult
0X0
100
111
937 0 $ctl{instrument} = $v;
938 }
939 elsif ( $k =~ /^(width|height|dx|dy|w|h)$/ ) {
940 0 ▶ $v = $1 if $v =~ /^(-?[\d.]+)pt$/;
Branch: 1 branch
BranchTrueFalseTotal
if $v =~ /^(-?[\d.]+)pt$/ 0 0 0
941 0 ▶ $v = $1 * $ctx->{size} if $v =~ /^(-?[\d.]+)em$/;
Branch: 1 branch
BranchTrueFalseTotal
if $v =~ /^(-?[\d.]+)em$/ 0 0 0
942 0 ▶ $v = $1 * $ctx->{size} / 2 if $v =~ /^(-?[\d.]+)ex$/;
Branch: 1 branch
BranchTrueFalseTotal
if $v =~ /^(-?[\d.]+)ex$/ 0 0 0
943 #$v = $1 * $ctx->{size} / 100 if $v =~ /^(-?[\d.]+)\%$/;
944 0 ▶ if ( $v =~ /^(-?[\d.]+)\%$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v =~ /^(-?[\d.]+)\%$/) { } 0 0 0
945 0 warn("Invalid img attribute: \"$kk\" (percentage not allowed)\n");
946 0 $err++;
947 }
948 else {
949 0 $ctl{$k} = $v;
950 }
951 }
952 elsif ( $k =~ /^(scale)$/ ) {
953 0 my @s;
954 0 for ( split( /,/, $v ) ) {
955 0 ▶ $_ = $1 / 100 if /^([\d.]+)\%$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^([\d.]+)\%$/ 0 0 0
956 0 push( @s, $_ );
957 }
958 0 ▶ push( @s, $s[0] ) unless @s > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @s > 1 0 0 0
959 0 ▶ unless ( @s == 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@s == 2) 0 0 0
960 0 warn("Invalid img attribute: \"$kk\" (too many values)\n");
961 0 $err++;
962 }
963 0 $ctl{$k} = \@s;
964 }
965 else {
966 0 warn("Invalid img attribute: \"$k\" ($kk)\n");
967 0 $err++;
968 }
969 }
970  
971 # Currently we do not have value-less attributes.
972 else {
973 0 warn("Invalid img attribute: \"$kk\"\n");
974 0 $err++;
975 }
976 }
977  
978 0 ▶ if ( $err ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($err) { } 0 0 0
elsif ($ctl{'id'}) { } 0 0 0
979 0 ▶ if ( $ctl{id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ctl{"id"}) 0 0 0
980 0 $ctl{id} = "__ERROR__";
981 }
982 }
983 elsif ( $ctl{id} ) {
984 0 my $a = ChordPro::Output::PDF::Song::assets($ctl{id});
985 0 ▶ if ( $a && $a->{opts}->{base} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a and $a->{"opts"}{"base"}) 0 0 0
Condition0%0/3
$a and $a->{"opts"}{"base"}
!ll&&!rl&&r
000
MC/DC0%0/2
$a and $a->{"opts"}{"base"}
$a $a->{"opts"}{"base"}
Truth table0%0/3
A and B
A= $aB= $a->{"opts"}{"base"}
ABresult
0X0
100
111
986 0 $ctl{base} = $a->{opts}->{base};
987 }
988 }
989  
990 0 return \%ctl;
991 12 }
992  
993 0 ▶method getimage ($fragment) {
Subroutine getimage: not called CC=18 SCAR=58.3
994 0 ▶ $fragment->{_img} //= do {
Condition0%0/3
$fragment->{"_img"} //= do { my $xo; if ($fragment->{"id"}) { my $o = ChordPro::Output::PDF::Song::assets($fragment->{"id"}); $xo = $o->{"data"} if $o; unless ($o and $xo) { warn(qq[Unknown image ID in <img>: $fragment->{"id"}\n]) unless $fragment->{"id"} eq "__ERROR__"; $xo = alert($fragment->{"size"}); } ; $fragment->{"design_scale"} = $o->{"opts"}{"scale"}; if ($o->{"width"} and $o->{"vwidth"}) { $fragment->{"design_scale"} ||= 1; $fragment->{"design_scale"} *= $o->{"vwidth"} / $o->{"width"}; } ; } elsif ($fragment->{"builtin"}) { my $i = $fragment->{"builtin"}; if ($i =~ /^alert(?:\(([\d.]+)\))?$/) { $xo = alert($1 || $fragment->{"size"}); } else { warn("Unknown builtin image in <img>: $i\n"); $xo = alert($fragment->{"size"}); } ; } elsif ($fragment->{"chord"}) { my $info = ChordPro::Chords::known_chord($fragment->{"chord"}); if ($info) { my $type = $fragment->{"instrument"} // $main::config->{"instrument"}{"type"}; my $p = ChordPro::Output::PDF::diagrammer($type); $xo = $p->diagram_xo($info); } else { warn(qq[Unknown chord in <img>: $fragment->{"chord"}\n]); $xo = alert($fragment->{"size"}); } ; } ; ($xo // $self->SUPER::getimage($fragment)) // alert($fragment->{"size"}) }
l!l&&r!l&&!r
000
MC/DC0%0/2
$fragment->{"_img"} //= do { my $xo; if ($fragment->{"id"}) { my $o = ChordPro::Output::PDF::Song::assets($fragment->{"id"}); $xo = $o->{"data"} if $o; unless ($o and $xo) { warn(qq[Unknown image ID in <img>: $fragment->{"id"}\n]) unless $fragment->{"id"} eq "__ERROR__"; $xo = alert($fragment->{"size"}); } ; $fragment->{"design_scale"} = $o->{"opts"}{"scale"}; if ($o->{"width"} and $o->{"vwidth"}) { $fragment->{"design_scale"} ||= 1; $fragment->{"design_scale"} *= $o->{"vwidth"} / $o->{"width"}; } ; } elsif ($fragment->{"builtin"}) { my $i = $fragment->{"builtin"}; if ($i =~ /^alert(?:\(([\d.]+)\))?$/) { $xo = alert($1 || $fragment->{"size"}); } else { warn("Unknown builtin image in <img>: $i\n"); $xo = alert($fragment->{"size"}); } ; } elsif ($fragment->{"chord"}) { my $info = ChordPro::Chords::known_chord($fragment->{"chord"}); if ($info) { my $type = $fragment->{"instrument"} // $main::config->{"instrument"}{"type"}; my $p = ChordPro::Output::PDF::diagrammer($type); $xo = $p->diagram_xo($info); } else { warn(qq[Unknown chord in <img>: $fragment->{"chord"}\n]); $xo = alert($fragment->{"size"}); } ; } ; ($xo // $self->SUPER::getimage($fragment)) // alert($fragment->{"size"}) }
$fragment->{"_img"} do { my $xo; if ($fragment->{"id"}) { my $o = ChordPro::Output::PDF::Song::assets($fragment->{"id"}); $xo = $o->{"data"} if $o; unless ($o and $xo) { warn(qq[Unknown image ID in <img>: $fragment->{"id"}\n]) unless $fragment->{"id"} eq "__ERROR__"; $xo = alert($fragment->{"size"}); } ; $fragment->{"design_scale"} = $o->{"opts"}{"scale"}; if ($o->{"width"} and $o->{"vwidth"}) { $fragment->{"design_scale"} ||= 1; $fragment->{"design_scale"} *= $o->{"vwidth"} / $o->{"width"}; } ; } elsif ($fragment->{"builtin"}) { my $i = $fragment->{"builtin"}; if ($i =~ /^alert(?:\(([\d.]+)\))?$/) { $xo = alert($1 || $fragment->{"size"}); } else { warn("Unknown builtin image in <img>: $i\n"); $xo = alert($fragment->{"size"}); } ; } elsif ($fragment->{"chord"}) { my $info = ChordPro::Chords::known_chord($fragment->{"chord"}); if ($info) { my $type = $fragment->{"instrument"} // $main::config->{"instrument"}{"type"}; my $p = ChordPro::Output::PDF::diagrammer($type); $xo = $p->diagram_xo($info); } else { warn(qq[Unknown chord in <img>: $fragment->{"chord"}\n]); $xo = alert($fragment->{"size"}); } ; } ; ($xo // $self->SUPER::getimage($fragment)) // alert($fragment->{"size"}) }
Truth table0%0/3
A //= B
A= $fragment->{"_img"}B= do { my $xo; if ($fragment->{"id"}) { my $o = ChordPro::Output::PDF::Song::assets($fragment->{"id"}); $xo = $o->{"data"} if $o; unless ($o and $xo) { warn(qq[Unknown image ID in <img>: $fragment->{"id"}\n]) unless $fragment->{"id"} eq "__ERROR__"; $xo = alert($fragment->{"size"}); } ; $fragment->{"design_scale"} = $o->{"opts"}{"scale"}; if ($o->{"width"} and $o->{"vwidth"}) { $fragment->{"design_scale"} ||= 1; $fragment->{"design_scale"} *= $o->{"vwidth"} / $o->{"width"}; } ; } elsif ($fragment->{"builtin"}) { my $i = $fragment->{"builtin"}; if ($i =~ /^alert(?:\(([\d.]+)\))?$/) { $xo = alert($1 || $fragment->{"size"}); } else { warn("Unknown builtin image in <img>: $i\n"); $xo = alert($fragment->{"size"}); } ; } elsif ($fragment->{"chord"}) { my $info = ChordPro::Chords::known_chord($fragment->{"chord"}); if ($info) { my $type = $fragment->{"instrument"} // $main::config->{"instrument"}{"type"}; my $p = ChordPro::Output::PDF::diagrammer($type); $xo = $p->diagram_xo($info); } else { warn(qq[Unknown chord in <img>: $fragment->{"chord"}\n]); $xo = alert($fragment->{"size"}); } ; } ; ($xo // $self->SUPER::getimage($fragment)) // alert($fragment->{"size"}) }
ABresult
1X1
011
000
995 0 my $xo;
996 0 ▶ if ( $fragment->{id} ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($fragment->{'id'}) { } 0 0 0
elsif ($fragment->{'builtin'}) { } 0 0 0
elsif ($fragment->{'chord'}) { } 0 0 0
997 0 my $o = ChordPro::Output::PDF::Song::assets($fragment->{id});
998 0 ▶ $xo = $o->{data} if $o;
Branch: 1 branch
BranchTrueFalseTotal
if $o 0 0 0
999 0 ▶ unless ( $o && $xo ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($o and $xo) 0 0 0
Condition0%0/3
$o and $xo
!ll&&!rl&&r
000
MC/DC0%0/2
$o and $xo
$o $xo
Truth table0%0/3
A and B
A= $oB= $xo
ABresult
0X0
100
111
1000 warn("Unknown image ID in <img>: $fragment->{id}\n")
1001 0 ▶ unless $fragment->{id} eq "__ERROR__";
Branch: 1 branch
BranchTrueFalseTotal
unless $fragment->{"id"} eq "__ERROR__" 0 0 0
1002 0 $xo = alert( $fragment->{size} );
1003 }
1004 0 $fragment->{design_scale} = $o->{opts}->{scale};
1005 0 ▶ if ( $o->{width} && $o->{vwidth} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($o->{"width"} and $o->{"vwidth"}) 0 0 0
Condition0%0/3
$o->{"width"} and $o->{"vwidth"}
!ll&&!rl&&r
000
MC/DC0%0/2
$o->{"width"} and $o->{"vwidth"}
$o->{"width"} $o->{"vwidth"}
Truth table0%0/3
A and B
A= $o->{"width"}B= $o->{"vwidth"}
ABresult
0X0
100
111
1006 0 ▶ $fragment->{design_scale} ||= 1;
Condition0%0/2
$fragment->{"design_scale"} ||= 1
l!l
00
MC/DC0%0/1
$fragment->{"design_scale"} ||= 1
$fragment->{"design_scale"}
Truth table0%0/2
A
A= $fragment->{"design_scale"}
Aresult
11
00
1007 0 $fragment->{design_scale} *= $o->{vwidth}/$o->{width};
1008 }
1009 }
1010 elsif ( $fragment->{builtin} ) {
1011 0 my $i = $fragment->{builtin};
1012 0 ▶ if ( $i =~ /^alert(?:\(([\d.]+)\))?$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i =~ /^alert(?:\(([\d.]+)\))?$/) { } 0 0 0
1013 0 ▶ $xo = alert( $1 || $fragment->{size} );
Condition0%0/3
$1 || $fragment->{'size'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$1 || $fragment->{'size'}
$1 $fragment->{'size'}
Truth table0%0/3
A || B
A= $1B= $fragment->{'size'}
ABresult
1X1
011
000
1014 }
1015 else {
1016 0 warn("Unknown builtin image in <img>: $i\n");
1017 0 $xo = alert( $fragment->{size} );
1018 }
1019 }
1020 elsif ( $fragment->{chord} ) {
1021 0 my $info = ChordPro::Chords::known_chord($fragment->{chord});
1022 0 ▶ unless ( $info ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info) { } 0 0 0
1023 0 warn("Unknown chord in <img>: $fragment->{chord}\n");
1024 0 $xo = alert( $fragment->{size} );
1025 }
1026 else {
1027 0 ▶ my $type = $fragment->{instrument} // $config->{instrument}->{type};
Condition0%0/3
$fragment->{"instrument"} // $main::config->{"instrument"}{"type"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$fragment->{"instrument"} // $main::config->{"instrument"}{"type"}
$fragment->{"instrument"} $main::config->{"instrument"}{"type"}
Truth table0%0/3
A // B
A= $fragment->{"instrument"}B= $main::config->{"instrument"}{"type"}
ABresult
1X1
011
000
1028 0 my $p = ChordPro::Output::PDF::diagrammer($type);
1029 0 $xo = $p->diagram_xo($info);
1030 }
1031 }
1032 0 ▶ $xo // $self->SUPER::getimage($fragment) // alert( $fragment->{size} );
Condition0%0/6
($xo // $self->SUPER::getimage($fragment)) // alert($fragment->{"size"})
l!l&&r!l&&!r
000
$xo // $self->SUPER::getimage($fragment)
l!l&&r!l&&!r
000
MC/DC0%0/3
($xo // $self->SUPER::getimage($fragment)) // alert($fragment->{"size"})
$xo $self->SUPER::getimage($fragment) alert($fragment->{"size"})
Truth table0%0/4
A // B // C
A= $xoB= $self->SUPER::getimage($fragment)C= alert($fragment->{"size"})
ABCresult
1XX1
01X1
0011
0000
1033 };
1034 12 }
1035  
1036 0 ▶sub alert ($size) {
Subroutine alert: not called CC=1 SCAR=6.9
1037 0 my $scale = $size/20;
1038 0 my $xo = $pr->{pdf}->xo_form;
1039 0 $xo->bbox( 0, -18*$scale, 20*$scale, 0 );
1040 0 $xo->matrix( $scale, 0, 0, -$scale, 0, 0 );
1041 0 $xo->line_width(2)->line_join(1);
1042 0 $xo->stroke_color("red");
1043 0 $xo->fill_color("red");
1044 0 $xo->move( 1, 17 )->polyline( 19, 17, 10, 1 )->close->stroke;
1045 0 $xo->rectangle( 9, 13, 11, 15 );
1046 0 $xo->move( 9, 12 )->polyline( 8.5, 7, 11.5, 7, 11, 12 )->close->fill;
1047 0 return $xo;
1048 }
1049  
1050 class TextLayoutSymbolElement :does(Text::Layout::ElementRole);
1051  
1052 12 ▶use ChordPro::Utils qw(parse_kv);
Subroutine BEGIN: called CC=1 SCAR=0.0
1053 12 ▶use ChordPro::Symbols;
Subroutine BEGIN: called CC=1 SCAR=0.0
1054  
1055 12 field $glyphs;
1056  
1057 BUILD {
1058 $glyphs = ChordPro::Symbols::symbols();
1059 12 };
1060  
1061 0 ▶method parse( $ctx, $k, $v ) {
Subroutine parse: not called CC=4 SCAR=30.0
1062 0 my $kv = parse_kv($v);
1063 0 my $res =
1064 { %$ctx,
1065 type => "text",
1066 font => Text::Layout::FontConfig->from_string("ChordProSymbols"),
1067 };
1068  
1069 0 ▶ while ( ( $k,$v) = each(%$kv) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$kv) 0 0 0
1070 0 ▶ $res->{$k} = $v, next
Branch: 1 branch
BranchTrueFalseTotal
if $k =~ /^(size|color|bgcolor|href)$/ 0 0 0
1071 if $k =~ /^(size|color|bgcolor|href)$/;
1072 0 ▶ $res->{text} = $glyphs->{$k}, next if defined $glyphs->{$k};
Branch: 1 branch
BranchTrueFalseTotal
if defined $glyphs->{$k} 0 0 0
1073 0 warn("Unknown attribute in <sym>: $k (ignored)\n");
1074 }
1075  
1076 0 return $res;
1077 12 }
1078  
1079 # These methods must be defined for the role, but will not be used.
1080 0 ▶method render( $hash, $gfx, $x, $y ) {}
Subroutine render: not called CC=1 SCAR=4.9
1081 0 ▶method bbox( $hash ) {}
Subroutine bbox: not called CC=1 SCAR=3.5
1082  
1083 12 1;
« lib/ChordPro/Output/HTML.pm ↑ Summary lib/ChordPro/Output/MMA.pm »