lib/ChordPro/Output/Common.pm

filter: Statementstmt 76.2% 173 / 227 Branchbran 46.8% 45 / 96 Conditioncond 56.5% 26 / 46 MC/DCmcdc 23.0% 6 / 26 Subroutinesub 79.1% 19 / 24 Podpod 0.0% 0 / 9 totaltotal 62.8% 269 / 428 CC 79 SCAR 42.4
CC
79
Cov
66%
mimedata
295.0
prep_outlines
37.8
arabic
20.0
CRAP 69.7
« lib/ChordPro/lib/SVGPDF/Colour.pm ↑ Summary lib/ChordPro/Utils.pm »
1 #! perl
2  
3 package main;
4  
5 92 our $config;
6 92 our $options;
7  
8 package ChordPro::Output::Common;
9  
10 92 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 92 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 92 ▶use feature qw( state );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 92 ▶use ChordPro::Chords;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 92 ▶use ChordPro::Utils qw( demarkup is_true plural );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 92 ▶use String::Interpolate::Named;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 92 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 92 ▶use POSIX qw(setlocale LC_TIME strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 92 ▶use Ref::Util qw( is_arrayref );
Subroutine BEGIN: called CC=1 SCAR=0.0
19 92 ▶use File::LoadLines ();
Subroutine BEGIN: called CC=1 SCAR=0.0
20 92 ▶use Encode qw( encode_utf8 );
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 92 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
23 92 our @EXPORT;
24 92 our @EXPORT_OK;
25  
26 3053 ▶sub fmt_subst {
Subroutine fmt_subst: called CC=12 SCAR=25.1
Pod undocumented
27 3053 my ( $s, $t ) = @_;
28 3053 my $res = "";
29 3053 ▶ my $m = { %{$s->{meta} || {} } };
Condition50%1/2
$s->{'meta'} || {}
l!l
30530
MC/DC0%0/1
$s->{'meta'} || {}
$s->{'meta'}
Truth table50%1/2
A
A= $s->{'meta'}
Aresult
11
00
30  
31 3053 ▶ $m->{tuning} //= [ join(" ", ChordPro::Chords::get_tuning) ];
Condition50%1/2
$m->{"tuning"} //= [join(" ", ChordPro::Chords::get_tuning())]
l!l
03053
MC/DC0%0/1
$m->{"tuning"} //= [join(" ", ChordPro::Chords::get_tuning())]
$m->{"tuning"}
Truth table50%1/2
A
A= $m->{"tuning"}
Aresult
11
00
32 # If config->{instrument} is missing, or null, the program abends with
33 # Modification of a read-only value attempted.
34 3053 ▶ if ( $config->{instrument} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main::config->{"instrument"}) 3053 0 3053
35 3053 $m->{instrument} = [ $config->{instrument}->{type} ];
36 3053 $m->{"instrument.type"} = [ $config->{instrument}->{type} ];
37 3053 $m->{"instrument.description"} = [ $config->{instrument}->{description} ];
38 }
39 # Same here.
40 3053 ▶ if ( $config->{user} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main::config->{"user"}) 3053 0 3053
41 3053 $m->{user} = [ $config->{user}->{name} ];
42 3053 $m->{"user.name"} = [ $config->{user}->{name} ];
43 3053 $m->{"user.fullname"} = [ $config->{user}->{fullname} ];
44 }
45 3053 setlocale( LC_TIME, "" );
46 $m->{today} //= strftime( $config->{dates}->{today}->{format},
47 3053 ▶ localtime(time) );
Condition33%1/3
$m->{"today"} //= strftime($main::config->{"dates"}{"today"}{"format"}, localtime time)
l!l&&r!l&&!r
030530
MC/DC0%0/2
$m->{"today"} //= strftime($main::config->{"dates"}{"today"}{"format"}, localtime time)
$m->{"today"} strftime($main::config->{"dates"}{"today"}{"format"}, localtime time)
Truth table33%1/3
A //= B
A= $m->{"today"}B= strftime($main::config->{"dates"}{"today"}{"format"}, localtime time)
ABresult
1X1
011
000
48 3053 $m->{chordpro} = "ChordPro";
49 3053 $m->{"chordpro.version"} = $ChordPro::VERSION;
50 3053 for ( keys %{ $config->{settings} } ) {
51 76325 my $v = $config->{settings}->{$_};
52 76325 ▶ $v = '' if $v =~ /^(0|false|off)$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $v =~ /^(0|false|off)$/i 39966 36359 76325
53 76325 ▶ $v = 1 if $v=~ /^(true|on)$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $v =~ /^(true|on)$/i 17714 58611 76325
54 76325 $m->{"settings.$_"} = $v;
55 }
56  
57 # Legacy.
58 3053 ▶ $m->{key_actual} = $m->{key_sound} // [];
Condition100%2/2
$m->{"key_sound"} // []
l!l
10581995
MC/DC100%1/1
$m->{"key_sound"} // []
$m->{"key_sound"}
Truth table100%2/2
A
A= $m->{"key_sound"}
Aresult
11
00
59 # Modern.
60 3053 ▶ $m->{'key.print'} = $m->{key_print} // [];
Condition100%2/2
$m->{"key_print"} // []
l!l
10581995
MC/DC100%1/1
$m->{"key_print"} // []
$m->{"key_print"}
Truth table100%2/2
A
A= $m->{"key_print"}
Aresult
11
00
61 3053 ▶ $m->{'key.sound'} = $m->{key_sound} // [];
Condition100%2/2
$m->{"key_sound"} // []
l!l
10581995
MC/DC100%1/1
$m->{"key_sound"} // []
$m->{"key_sound"}
Truth table100%2/2
A
A= $m->{"key_sound"}
Aresult
11
00
62  
63 interpolate( { %$s, args => $m,
64 separator => $config->{metadata}->{separator} },
65 3053 $t );
66 }
67 92 push( @EXPORT, 'fmt_subst' );
68  
69 # Roman - functions for converting between Roman and Arabic numerals
70 #
71 # Stolen from Roman Version 1.24 by OZAWA Sakuro <ozawa at aisoft.co.jp>
72 # 1995-1997 and Alexandr Ciornii, C<< <alexchorny at gmail.com> >> 2007
73 #
74 # Copyright (c) 1995 OZAWA Sakuro. All rights reserved. This program
75 # is free software; you can redistribute it and/or modify it under the
76 # same terms as Perl itself.
77  
78 92 our %roman2arabic = qw(I 1 V 5 X 10 L 50 C 100 D 500 M 1000);
79 92 my %roman_digit = qw(1 IV 10 XL 100 CD 1000 MMMMMM);
80 92 my @figure = reverse sort keys %roman_digit;
81 #my %roman_digit;
82 92 $roman_digit{$_} = [split(//, $roman_digit{$_}, 2)] foreach @figure;
83  
84 0 ▶sub isroman($) {
Subroutine isroman: not called CC=2 SCAR=17.9
Pod undocumented
85 0 my $arg = shift;
86 0 ▶ $arg ne '' and
Branch: 1 branch
BranchTrueFalseTotal
$arg ne "" and $arg =~ /^(?: M{0,3}) (?: D?C{0,3} | C[DM]) (?: L?X{0,3} | X[LC]) (?: V?I{0,3} | I[VX])$/xi 0 0 0
87 $arg =~ /^(?: M{0,3})
88 (?: D?C{0,3} | C[DM])
89 (?: L?X{0,3} | X[LC])
90 (?: V?I{0,3} | I[VX])$/ix;
91 }
92 92 push( @EXPORT_OK, 'isroman' );
93  
94 0 ▶sub arabic($) {
Subroutine arabic: not called CC=4 SCAR=30.0
Pod undocumented
95 0 my $arg = shift;
96 0 ▶ isroman $arg or return undef;
Branch: 1 branch
BranchTrueFalseTotal
&isroman($arg) or return undef 0 0 0
97 0 my($last_digit) = 1000;
98 0 my($arabic);
99 0 foreach (split(//, uc $arg)) {
100 0 my($digit) = $roman2arabic{$_};
101 0 ▶ $arabic -= 2 * $last_digit if $last_digit < $digit;
Branch: 1 branch
BranchTrueFalseTotal
if $last_digit < $digit 0 0 0
102 0 $arabic += ($last_digit = $digit);
103 }
104 0 $arabic;
105 }
106 92 push( @EXPORT_OK, 'arabic' );
107  
108 36 ▶sub Roman($) {
Subroutine Roman: called CC=11 SCAR=29.0
Pod undocumented
109 36 my $arg = shift;
110 36 ▶ 0 < $arg and $arg < 4000 or return undef;
Branch: 1 branch
BranchTrueFalseTotal
0 < $arg and $arg < 4000 or return undef 36 0 36
Condition33%1/3
0 < $arg and $arg < 4000
!ll&&!rl&&r
0036
MC/DC0%0/2
0 < $arg and $arg < 4000
0 < $arg $arg < 4000
Truth table33%1/3
A and B
A= 0 < $argB= $arg < 4000
ABresult
0X0
100
111
111 36 my($x, $roman);
112 36 foreach (@figure) {
113 144 my($digit, $i, $v) = (int($arg / $_), @{$roman_digit{$_}});
114 144 ▶ if (1 <= $digit and $digit <= 3) {
Branch: 5 branches
BranchTrueFalseTotal
if (1 <= $digit and $digit <= 3) { } 36 108 144
elsif ($digit == 4) { } 0 108 108
elsif ($digit == 5) { } 0 108 108
elsif (6 <= $digit and $digit <= 8) { } 0 108 108
elsif ($digit == 9) { } 0 108 108
Condition66%2/3
1 <= $digit and $digit <= 3
!ll&&!rl&&r
108036
MC/DC50%1/2
1 <= $digit and $digit <= 3
1 <= $digit $digit <= 3
Truth table66%2/3
A and B
A= 1 <= $digitB= $digit <= 3
ABresult
0X0
100
111
115 36 $roman .= $i x $digit;
116 } elsif ($digit == 4) {
117 0 $roman .= "$i$v";
118 } elsif ($digit == 5) {
119 0 $roman .= $v;
120 ▶ } elsif (6 <= $digit and $digit <= 8) {
Condition33%1/3
6 <= $digit and $digit <= 8
!ll&&!rl&&r
10800
MC/DC0%0/2
6 <= $digit and $digit <= 8
6 <= $digit $digit <= 8
Truth table33%1/3
A and B
A= 6 <= $digitB= $digit <= 8
ABresult
0X0
100
111
121 0 $roman .= $v . $i x ($digit - 5);
122 } elsif ($digit == 9) {
123 0 $roman .= "$i$x";
124 }
125 144 $arg -= $digit * $_;
126 144 $x = $i;
127 }
128 36 $roman;
129 }
130 92 push( @EXPORT_OK, 'Roman' );
131  
132 36 ▶sub roman($) {
Subroutine roman: called CC=1 SCAR=0.0
Pod undocumented
133 36 lc( Roman(shift) );
134 }
135 92 push( @EXPORT_OK, 'roman' );
136  
137 # Prepare outlines.
138 # This mainly untangles alternative names when being sorted on.
139 # Returns a book array where each element consists of the sort items,
140 # and the song.
141  
142 #sub PODBG() { $config->{debug}->{x1} }
143 sub PODBG() { 0 }
144  
145 # Suppress toc entry.
146 179 ▶sub _suppresstoc {
Subroutine _suppresstoc: called CC=2 SCAR=7.1
147 179 my ( $meta ) = @_;
148 179 ▶ return !is_true($meta->{_TOC}->[0]) if exists($meta->{_TOC});
Branch: 1 branch
BranchTrueFalseTotal
if exists $meta->{"_TOC"} 0 179 179
149 # return unless exists($meta->{sorttitle});
150 # my $st = $meta->{sorttitle};
151 # defined($st) && ( $st->[0] eq "" || $st->[0] eq '""' );
152 179 return;
153 }
154  
155 49 ▶sub prep_outlines {
Subroutine prep_outlines: called CC=35 SCAR=36.3
Pod undocumented
156 49 my ( $book, $ctl ) = @_;
157 49 ▶ return [] unless $book && @$book; # unlikely
Branch: 1 branch
BranchTrueFalseTotal
unless $book and @$book 0 49 49
Condition33%1/3
$book and @$book
!ll&&!rl&&r
0049
MC/DC0%0/2
$book and @$book
$book @$book
Truth table33%1/3
A and B
A= $bookB= @$book
ABresult
0X0
100
111
158 49 ▶ return [] if $ctl->{omit};
Branch: 1 branch
BranchTrueFalseTotal
if $ctl->{"omit"} 0 49 49
159  
160 49 warn("FLD: ", join(" ", @{$ctl->{fields}}), "\n") if PODBG;
161  
162 49 ▶ my @fields = map { /^[-+]*(.*)/ ? $1 : $_ } @{$ctl->{fields}};
Branch: 1 branch
BranchTrueFalseTotal
/^[-+]*(.*)/ ? : 87 0 87
163 49 ▶ if ( @fields == 1 && $fields[0] eq "songindex" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fields == 1 and $fields[0] eq "songindex") 9 40 49
Condition100%3/3
@fields == 1 and $fields[0] eq "songindex"
!ll&&!rl&&r
3829
MC/DC100%2/2
@fields == 1 and $fields[0] eq "songindex"
@fields == 1 $fields[0] eq "songindex"
Truth table100%3/3
A and B
A= @fields == 1B= $fields[0] eq "songindex"
ABresult
0X0
100
111
164 # Return in book order.
165 27 return [ map { [ $_->{meta}->{songindex}, $_ ] }
166 9 grep { !_suppresstoc($_->{meta}) } @$book ];
167 }
168 40 ▶ return $book unless @fields; # ?
Branch: 1 branch
BranchTrueFalseTotal
unless @fields 0 40 40
169  
170 40 my @book;
171 40 foreach my $song ( @$book ) {
172 152 my $meta = { %{$song->{meta}} };
173 152 ▶ next if _suppresstoc($meta);
Branch: 1 branch
BranchTrueFalseTotal
if _suppresstoc($meta) 0 152 152
174  
175 152 my @split;
176  
177 152 foreach my $item ( @fields ) {
178 298 ( my $coreitem = $item ) =~ s/^sort//;
179 298 ▶ push( @split, [ $coreitem, [""] ] ), next unless $meta->{$coreitem};
Branch: 1 branch
BranchTrueFalseTotal
unless $meta->{$coreitem} 57 241 298
180  
181 252 my @s = map { [ $_ ] }
182 241 @{ is_arrayref( $meta->{$coreitem} )
183 ? $meta->{$coreitem}
184 241 ▶ : [ $meta->{$coreitem} ]
Branch: 1 branch
BranchTrueFalseTotal
is_arrayref($meta->{$coreitem}) ? : 241 0 241
185 };
186  
187 241 ▶ if ( $meta->{"sort$coreitem"} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$meta{"sort$coreitem"}) 53 188 241
188 53 ▶ if ( $coreitem eq $item ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($coreitem eq $item) { } 6 47 53
189 6 ▶ for ( my $i = 0; $i < @{$meta->{"sort$coreitem"}}; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$$meta{"sort$coreitem"};}) 8 6 14
190 8 ▶ next unless defined $s[$i]->[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s[$i][0] 0 8 8
191 8 $s[$i]->[1] = $meta->{"sort$coreitem"}->[$i];
192 }
193 }
194 else {
195 47 ▶ for ( my $i = 0; $i < @{$meta->{$item}}; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$meta->{$item};}) 48 47 95
196 48 ▶ next unless defined $s[$i]->[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s[$i][0] 0 48 48
197 48 $s[$i]->[1] = $meta->{$item}->[$i];
198 }
199 }
200 }
201 241 push( @split, [ $coreitem, @s ] );
202 }
203  
204 # Merge with (unique) copies of the song.
205 152 ▶ if ( @split == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@split == 0) { } 0 152 152
206 0 push( @book, $song );
207 }
208 # elsif ( @split == 1 ) {
209 # my $f1 = shift(@{$split[0]});
210 # my $addsort1 = $f1 =~ /^(title|artist)$/;
211 # for my $s1 ( @{$split[0]} ) {
212 # push( @book,
213 # { %$song,
214 # meta =>
215 # { %$meta,
216 # $f1 => [ $s1->[0] ],
217 # $addsort1
218 # ? ( "sort$f1" => [ $s1->[1] // $s1->[0] ] )
219 # : (),
220 # }
221 # }
222 # );
223 # }
224 # }
225 # elsif ( @split == 200 ) {
226 # my $f1 = shift(@{$split[0]}) // "";
227 # my $f2 = shift(@{$split[1]}) // "";
228 # my $addsort1 = $f1 =~ /^(title|artist)$/;
229 # my $addsort2 = $f2 =~ /^(title|artist)$/;
230 # for my $s1 ( @{$split[0]} ) {
231 # for my $s2 ( @{$split[1]} ) {
232 # push( @book,
233 # { %$song,
234 # meta =>
235 # { %$meta,
236 # $f1 => [ $s1->[0] ],
237 # $addsort1
238 # ? ( "sort$f1" => [ $s1->[1] // $s1->[0] ] )
239 # : (),
240 # $f2 => [ $s2->[0] ],
241 # $addsort2
242 # ? ( "sort$f2" => [ $s2->[1] // $s2->[0] ] )
243 # : (),
244 # }
245 # }
246 # );
247 # }
248 # }
249 # }
250 else {
251 152 my @mm;
252 152 for my $split ( @split ) {
253 298 ▶ my $f = shift(@$split) // "";
Condition50%1/2
shift @$split // ""
l!l
2980
MC/DC0%0/1
shift @$split // ""
shift @$split
Truth table50%1/2
A
A= shift @$split
Aresult
11
00
254 298 warn("F: $f\n") if PODBG;
255 298 my $addsort = $f =~ /^(title|artist)$/;
256 298 my @x;
257 298 for my $s ( @{$split} ) {
258 309 warn("V: $s->[0]\n") if PODBG;
259 309 my %x = ( $f => [ $s->[0] ] );
260 309 ▶ $x{"sort$f"} = [ $s->[1] // $s->[0] ] if $addsort;
Branch: 1 branch
BranchTrueFalseTotal
if $addsort 301 8 309
Condition66%2/3
$s->[1] // $s->[0]
l!l&&r!l&&!r
562450
MC/DC0%0/2
$s->[1] // $s->[0]
$s->[1] $s->[0]
Truth table66%2/3
A // B
A= $s->[1]B= $s->[0]
ABresult
1X1
011
000
261 309 ▶ if ( @mm ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@mm) { } 152 157 309
262 152 push( @x, { %x, %$_ } ) for @mm;
263 }
264 else {
265 157 push( @x, \%x );
266 }
267 309 warn("X: ", scalar(@x), " items\n") if PODBG;
268 }
269 298 @mm = @x;
270 }
271 152 push( @book, { %$song, meta => { %$meta, %$_ } } ) for @mm;
272 }
273 }
274  
275 # Sort.
276 40 my $i = -1;
277 92 ▶ use Unicode::Collate;
Subroutine BEGIN: called CC=1 SCAR=0.0
278 40 my $collator = Unicode::Collate->new;
279 my $srt =
280 "sub { " .
281 join( " or ",
282 78 map { $i++;
283 78 my ( $rev, $f ) = /^([-+]*)(.*)/;
284 78 my $num = $rev =~ s/\+//g;
285 78 ▶ my ( $a, $b ) = $rev =~ /-/ ? qw( b a ) : qw( a b );
Branch: 1 branch
BranchTrueFalseTotal
$rev =~ /-/ ? : 1 77 78
286 78 my $l = "\$$a"."->[$i]";
287 78 my $r = "\$$b"."->[$i]";
288 78 warn("F: $f, N: $num, R: $rev\n") if PODBG;
289 78 ▶ $num ? "$l <=> $r" : "\$collator->cmp( $l, $r )"
Branch: 1 branch
BranchTrueFalseTotal
$num ? : 0 78 78
290 }
291 40 @{$ctl->{fields}} ) .
292 " }";
293 40 warn("SRT; $srt\n") if PODBG;
294 40 ▶ $srt = eval $srt or die($@);
Branch: 1 branch
BranchTrueFalseTotal
$srt = eval $srt or die($@) 40 0 40
295 @book =
296 sort $srt
297 40 map { my $t = $_;
298 166 [ ( map { demarkup(lc( ( index($_,"sort") && is_arrayref($t->{meta}->{"sort$_"})
299 ? $t->{meta}->{"sort$_"}->[0]
300 : undef ) //
301 324 ▶ $t->{meta}->{$_}->[0] //
Branch: 1 branch
BranchTrueFalseTotal
index($_, 'sort') && is_arrayref($t->{'meta'}{"sort$_"}) ? : 185 139 324
Condition75%6/8
((index($_, "sort") && is_arrayref($t->{"meta"}{"sort$_"}) ? $t->{"meta"}{"sort$_"}[0] : undef) // $t->{"meta"}{$_}[0]) // ""
l!l
3240
(index($_, "sort") && is_arrayref($t->{"meta"}{"sort$_"}) ? $t->{"meta"}{"sort$_"}[0] : undef) // $t->{"meta"}{$_}[0]
l!l&&r!l&&!r
1851390
index $_, "sort" and is_arrayref($t->{"meta"}{"sort$_"})
!ll&&!rl&&r
1318185
MC/DC0%0/3
((index($_, "sort") && is_arrayref($t->{"meta"}{"sort$_"}) ? $t->{"meta"}{"sort$_"}[0] : undef) // $t->{"meta"}{$_}[0]) // ""
index $_, "sort" is_arrayref($t->{"meta"}{"sort$_"}) $t->{"meta"}{$_}[0]
Truth table60%3/5
A and B // C
A= index $_, "sort"B= is_arrayref($t->{"meta"}{"sort$_"})C= $t->{"meta"}{$_}[0]
ABCresult
11X1
0X11
1011
0X00
1000
302 "")) }
303 @fields ),
304 $_ ] }
305 @book;
306  
307 40 return \@book;
308 }
309 92 push( @EXPORT_OK, 'prep_outlines' );
310  
311 0 ▶sub encode_html {
Subroutine encode_html: not called CC=2 SCAR=17.9
Pod undocumented
312 0 my $text = shift;
313 0 ▶ return "" unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 0 0
314 0 $text =~ s/&/&amp;/g;
315 0 $text =~ s/</&lt;/g;
316 0 $text =~ s/>/&gt;/g;
317 0 $text =~ s/"/&quot;/g;
318 0 $text =~ s/'/&#39;/g;
319 0 $text;
320 }
321  
322 92 push( @EXPORT_OK, 'encode_html' );
323  
324 # Make a data: URI (string) from an external source.
325 # This is mostly intended for images.
326 # Since ABC generated SVG can contain multiple images, this function
327 # can be called in list context to get all the images.
328 # In scalar context, it returns the first (or only) image.
329  
330 0 ▶sub encode_percent {
Subroutine encode_percent: not called CC=1 SCAR=6.9
Pod undocumented
331 0 my ( $str ) = @_;
332 0 require URI::Escape;
333 0 return URI::Escape::uri_escape_utf8($str);
334 }
335  
336 0 ▶sub mimedata {
Subroutine mimedata: not called CC=18 SCAR=56.9
Pod undocumented
337 0 my ( $src, $mimetype ) = @_;
338  
339 warn( "mimedata: \"$src\"",
340 $mimetype ? " $mimetype" : "",
341 0 ▶ "\n" ) if $config->{debug}->{images};
Branch: 2 branches
BranchTrueFalseTotal
if $main::config->{"debug"}{"images"} 0 0 0
$mimetype ? : 0 0 0
342 0 my $data = File::LoadLines::loadblob($src);
343 0 ▶ if ( $mimetype ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mimetype) { } 0 0 0
344 0 $mimetype = lc $mimetype;
345 }
346 else {
347 0 ▶ $mimetype = ChordPro::Utils::_detect_image_format($data)
Condition0%0/2
ChordPro::Utils::_detect_image_format($data) || Carp::croak(qq[Unrecognized imge data in "$src"])
l!l
00
MC/DC0%0/1
ChordPro::Utils::_detect_image_format($data) || Carp::croak(qq[Unrecognized imge data in "$src"])
ChordPro::Utils::_detect_image_format($data)
Truth table0%0/2
A
A= ChordPro::Utils::_detect_image_format($data)
Aresult
11
00
348 || Carp::croak("Unrecognized imge data in \"$src\"");
349 }
350  
351 0 ▶ unless ( $mimetype =~ m;^(image|text)/.*; ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($mimetype =~ m[^(image|text)/.*]) 0 0 0
352 0 state $mimetypes =
353 { png => 'image/png',
354 jpg => 'image/jpeg',
355 jpeg => 'image/jpeg',
356 gif => 'image/gif',
357 svg => 'image/svg+xml',
358 css => 'text/css',
359 };
360 Carp::croak( "Unhandled MIME type \"$mimetype\"" )
361 0 ▶ unless $mimetypes->{$mimetype};
Branch: 1 branch
BranchTrueFalseTotal
unless $mimetypes->{$mimetype} 0 0 0
362 0 $mimetype = $mimetypes->{$mimetype};
363 }
364  
365 0 my @img; # there can be more than one
366 0 ▶ if ( $mimetype =~/svg/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mimetype =~ /svg/) { } 0 0 0
367 # There may be several images. Split them.
368 0 @img = split( /\<\/svg\>[\n\r]*\<svg/, $data );
369 0 $img[$_] .= "</svg>" for 0..$#img-1;
370 0 $img[$_] = "<svg" . $img[$_] for 1..$#img;
371 }
372 else {
373 0 @img = ( $data );
374 }
375  
376 92 ▶ use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
377 # Emit as individual images.
378 0 for my $img ( @img ) {
379 0 ▶ if ( $mimetype =~ m;(text/|/svg); ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mimetype =~ m[(text/|/svg)]) { } 0 0 0
380 0 $img = "data:$mimetype,". encode_percent($img);
381 warn("mimedata: $mimetype, ", length($img), " bytes\n")
382 0 ▶ if $config->{debug}->{images};
Branch: 1 branch
BranchTrueFalseTotal
if $main::config->{"debug"}{"images"} 0 0 0
383 }
384 else {
385 0 ▶ $img = encode_utf8($img) if $mimetype =~ /svg/;
Branch: 1 branch
BranchTrueFalseTotal
if $mimetype =~ /svg/ 0 0 0
386 0 $img = "data:$mimetype;base64,". encode_base64( $img, '' );
387 warn("mimedata: $mimetype, ", length($img), " bytes\n")
388 0 ▶ if $config->{debug}->{images};
Branch: 1 branch
BranchTrueFalseTotal
if $main::config->{"debug"}{"images"} 0 0 0
389 }
390 0 ▶ if ( @img > 1 && !wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@img > 1 and not wantarray) 0 0 0
Condition0%0/3
@img > 1 and not wantarray
!ll&&!rl&&r
000
MC/DC0%0/2
@img > 1 and not wantarray
@img > 1 not wantarray
Truth table0%0/3
A and not(B)
A= @img > 1B= not wantarray
ABresult
0X0
100
111
391 0 Carp::carp( "Warning: Ignoring ",
392 plural( @img-1, " excess SVG image" ));
393 }
394 0 ▶ return $img[0] unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
395 }
396 0 return @img;
397 }
398  
399 92 push( @EXPORT_OK, "mimedata" );
400  
401 92 1;
« lib/ChordPro/lib/SVGPDF/Colour.pm ↑ Summary lib/ChordPro/Utils.pm »