lib/ChordPro/Assets.pm

filter: Statementstmt 21.0% 41 / 195 Branchbran 2.8% 3 / 104 Conditioncond 4.2% 4 / 94 MC/DCmcdc 0.0% 0 / 53 Subroutinesub 62.5% 10 / 16 Podpod 0.0% 0 / 4 totaltotal 12.4% 58 / 466 CC 97 SCAR 80.9
CC
97
Cov
12%
prepare_asset
5112.0
svg_fonthandler
132.0
svg_texthandler
72.0
CRAP 3260.3
« lib/ChordPro/Delegate/ABC.pm ↑ Summary lib/ChordPro/lib/SVGPDF/Text.pm »
1 #! perl
2  
3 13 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶no warnings 'experimental::signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 package main;
8  
9 13 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 13 our $config;
11 13 our $options;
12  
13 package ChordPro::Assets;
14  
15 13 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 13 our @EXPORT;
17  
18 13 ▶use ChordPro::Files;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 13 ▶use ChordPro::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 13 ▶use ChordPro::Output::SVG::Images;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 13 ▶use IO::String;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 83 ▶sub prepare_assets( $s, $pr = undef ) {
Subroutine prepare_assets: called CC=10 SCAR=27.5
Pod undocumented
24  
25 83 ▶ my %sa = %{$s->{assets}//{}} ; # song assets
Condition50%1/2
$s->{"assets"} // {}
l!l
083
MC/DC0%0/1
$s->{"assets"} // {}
$s->{"assets"}
Truth table50%1/2
A
A= $s->{"assets"}
Aresult
11
00
26  
27 warn("Assets: Preparing ", plural(scalar(keys %sa), " image"), "\n")
28 83 ▶ if $config->{debug}->{images} || $config->{debug}->{assets};
Branch: 1 branch
BranchTrueFalseTotal
if $main::config->{"debug"}{"images"} or $main::config->{"debug"}{"assets"} 0 83 83
Condition33%1/3
$main::config->{"debug"}{"images"} or $main::config->{"debug"}{"assets"}
l!l&&r!l&&!r
0083
MC/DC0%0/2
$main::config->{"debug"}{"images"} or $main::config->{"debug"}{"assets"}
$main::config->{"debug"}{"images"} $main::config->{"debug"}{"assets"}
Truth table33%1/3
A or B
A= $main::config->{"debug"}{"images"}B= $main::config->{"debug"}{"assets"}
ABresult
1X1
011
000
29  
30 83 for my $id ( sort keys %sa ) {
31 0 prepare_asset( $id, $s, $pr );
32 }
33  
34 warn("Assets: Preparing ", plural(scalar(keys %sa), " image"), ", done\n")
35 83 ▶ if $config->{debug}->{images} || $config->{debug}->{assets};
Branch: 1 branch
BranchTrueFalseTotal
if $main::config->{"debug"}{"images"} or $main::config->{"debug"}{"assets"} 0 83 83
Condition33%1/3
$main::config->{"debug"}{"images"} or $main::config->{"debug"}{"assets"}
l!l&&r!l&&!r
0083
MC/DC0%0/2
$main::config->{"debug"}{"images"} or $main::config->{"debug"}{"assets"}
$main::config->{"debug"}{"images"} $main::config->{"debug"}{"assets"}
Truth table33%1/3
A or B
A= $main::config->{"debug"}{"images"}B= $main::config->{"debug"}{"assets"}
ABresult
1X1
011
000
36 83 ▶ my $assets = $s->{assets} || {};
Condition50%1/2
$s->{'assets'} || {}
l!l
083
MC/DC0%0/1
$s->{'assets'} || {}
$s->{'assets'}
Truth table50%1/2
A
A= $s->{'assets'}
Aresult
11
00
37 ::dump( $assets, as => "Assets, Pass 2" )
38 83 ▶ if $config->{debug}->{assets} & 0x02;
Branch: 1 branch
BranchTrueFalseTotal
if $main::config->{"debug"}{"assets"} & 2 0 83 83
39 83 return $assets;
40  
41 }
42  
43 13 push( @EXPORT, 'prepare_assets' );
44  
45 0 ▶sub prepare_asset( $id, $s, $pr ) {
Subroutine prepare_asset: not called CC=71 SCAR=85.4
Pod undocumented
46  
47 0 my $ps;
48 0 ▶ $ps = $s->{_ps} = $pr->{ps} if $pr;
Branch: 1 branch
BranchTrueFalseTotal
if $pr 0 0 0
49  
50 # All elements generate zero or one display items, except for SVG images
51 # than can result in a series of display items.
52 # So we first scan the list for SVG and delegate items and turn these
53 # into simple display items.
54  
55 # warn("_MR = ", $ps->{_marginright}, ", _RM = ", $ps->{_rightmargin},
56 # ", __RM = ", $ps->{__rightmargin}, "\n");
57 # my $pw = $ps->{__rightmargin} - $ps->{_marginleft};
58 0 ▶ my $pw = $ps ? $ps->{_marginright} - $ps->{_marginleft} : 700;
Branch: 1 branch
BranchTrueFalseTotal
$ps ? : 0 0 0
59 my $cw = $ps ? ( $pw - ( $ps->{columns} - 1 ) * $ps->{columnspace} ) /$ps->{columns}
60 0 ▶ - $ps->{_indent} : 700;
Branch: 1 branch
BranchTrueFalseTotal
$ps ? : 0 0 0
61  
62 0 for my $elt ( $s->{assets}->{$id} ) {
63 # Already prepared, e.g. multi-pass songbook.
64 0 ▶ next if UNIVERSAL::can($elt->{data}, "width");
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::can($elt->{"data"}, "width") 0 0 0
65  
66 0 ▶ $elt->{subtype} //= "image" if $elt->{uri};
Branch: 1 branch
BranchTrueFalseTotal
if $elt->{"uri"} 0 0 0
Condition0%0/2
$elt->{"subtype"} //= "image"
l!l
00
MC/DC0%0/1
$elt->{"subtype"} //= "image"
$elt->{"subtype"}
Truth table0%0/2
A
A= $elt->{"subtype"}
Aresult
11
00
67  
68 0 ▶ if ( $elt->{type} eq "image" && $elt->{subtype} eq "delegate" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elt->{"type"} eq "image" and $elt->{"subtype"} eq "delegate") 0 0 0
Condition0%0/3
$elt->{"type"} eq "image" and $elt->{"subtype"} eq "delegate"
!ll&&!rl&&r
000
MC/DC0%0/2
$elt->{"type"} eq "image" and $elt->{"subtype"} eq "delegate"
$elt->{"type"} eq "image" $elt->{"subtype"} eq "delegate"
Truth table0%0/3
A and B
A= $elt->{"type"} eq "image"B= $elt->{"subtype"} eq "delegate"
ABresult
0X0
100
111
69 0 my $delegate = $elt->{delegate};
70 warn("Assets: Preparing delegate $delegate, handler ",
71 $elt->{handler},
72 0 ▶ ( map { " $_=" . $elt->{opts}->{$_} } keys(%{$elt->{opts}//{}})),
Condition0%0/2
$elt->{"opts"} // {}
l!l
00
MC/DC0%0/1
$elt->{"opts"} // {}
$elt->{"opts"}
Truth table0%0/2
A
A= $elt->{"opts"}
Aresult
11
00
73 0 ▶ "\n") if $config->{debug}->{images};
Branch: 1 branch
BranchTrueFalseTotal
if $main::config->{"debug"}{"images"} 0 0 0
74  
75 0 my $pkg = 'ChordPro::Delegate::' . $delegate;
76 0 ▶ eval "require $pkg" || die($@);
Branch: 1 branch
BranchTrueFalseTotal
eval "require $pkg" or die($@) 0 0 0
77 0 ▶ my $hd = $pkg->can($elt->{handler}) //
Condition0%0/3
$pkg->can($elt->{"handler"}) // die(qq[Assets: Missing delegate handler ${pkg}::$elt->{"handler"}\n])
l!l&&r!l&&!r
000
MC/DC0%0/2
$pkg->can($elt->{"handler"}) // die(qq[Assets: Missing delegate handler ${pkg}::$elt->{"handler"}\n])
$pkg->can($elt->{"handler"}) die(qq[Assets: Missing delegate handler ${pkg}::$elt->{"handler"}\n])
Truth table0%0/3
A // B
A= $pkg->can($elt->{"handler"})B= die(qq[Assets: Missing delegate handler ${pkg}::$elt->{"handler"}\n])
ABresult
1X1
011
000
78 die("Assets: Missing delegate handler ${pkg}::$elt->{handler}\n");
79 0 ▶ unless ( $elt->{data} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($elt->{"data"}) 0 0 0
80 0 $elt->{data} = fs_load( $elt->{uri}, { fail => 'hard' } );
81 }
82  
83 # Determine actual width.
84 0 ▶ my $w = defined($elt->{opts}->{spread}) ? $pw : $cw;
Branch: 1 branch
BranchTrueFalseTotal
defined $elt->{'opts'}{'spread'} ? : 0 0 0
85 $w = $elt->{opts}->{width}
86 0 ▶ if $elt->{opts}->{width} && $elt->{opts}->{width} < $w;
Branch: 1 branch
BranchTrueFalseTotal
if $elt->{"opts"}{"width"} and $elt->{"opts"}{"width"} < $w 0 0 0
Condition0%0/3
$elt->{"opts"}{"width"} and $elt->{"opts"}{"width"} < $w
!ll&&!rl&&r
000
MC/DC0%0/2
$elt->{"opts"}{"width"} and $elt->{"opts"}{"width"} < $w
$elt->{"opts"}{"width"} $elt->{"opts"}{"width"} < $w
Truth table0%0/3
A and B
A= $elt->{"opts"}{"width"}B= $elt->{"opts"}{"width"} < $w
ABresult
0X0
100
111
87  
88 0 my $res = $hd->( $s, elt => $elt, pagewidth => $w );
89 0 ▶ if ( $res ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res) { } 0 0 0
90 0 ▶ $res->{opts} = { %{ $res->{opts} // {} },
Condition0%0/2
$res->{"opts"} // {}
l!l
00
MC/DC0%0/1
$res->{"opts"} // {}
$res->{"opts"}
Truth table0%0/2
A
A= $res->{"opts"}
Aresult
11
00
91 0 ▶ %{ $elt->{opts} // {} } };
Condition0%0/2
$elt->{"opts"} // {}
l!l
00
MC/DC0%0/1
$elt->{"opts"} // {}
$elt->{"opts"}
Truth table0%0/2
A
A= $elt->{"opts"}
Aresult
11
00
92 warn( "Assets: Preparing delegate $delegate, handler ",
93 $elt->{handler}, " => ",
94 $res->{type}, "/", $res->{subtype},
95 0 ▶ ( map { " $_=" . $res->{opts}->{$_} } keys(%{$res->{opts}//{}})),
Condition0%0/2
$res->{"opts"} // {}
l!l
00
MC/DC0%0/1
$res->{"opts"} // {}
$res->{"opts"}
Truth table0%0/2
A
A= $res->{"opts"}
Aresult
11
00
96 " w=$w",
97 "\n" )
98 0 ▶ if $config->{debug}->{images};
Branch: 1 branch
BranchTrueFalseTotal
if $main::config->{"debug"}{"images"} 0 0 0
99 0 $s->{assets}->{$id} = $res;
100 }
101 else {
102 # Substitute alert image.
103 $s->{assets}->{$id} = $res =
104 { type => "image",
105 line => $elt->{line},
106 subtype => "svg",
107 data => [ SVG->alert(60) ],
108 0 ▶ opts => { %{$elt->{opts}//{}} } };
Condition0%0/2
$elt->{"opts"} // {}
l!l
00
MC/DC0%0/1
$elt->{"opts"} // {}
$elt->{"opts"}
Truth table0%0/2
A
A= $elt->{"opts"}
Aresult
11
00
109 }
110  
111 # If the delegate produced an image, continue processing.
112 0 ▶ if ( $res && $res->{type} eq "image" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res and $res->{'type'} eq 'image') { } 0 0 0
Condition0%0/3
$res and $res->{"type"} eq "image"
!ll&&!rl&&r
000
MC/DC0%0/2
$res and $res->{"type"} eq "image"
$res $res->{"type"} eq "image"
Truth table0%0/3
A and B
A= $resB= $res->{"type"} eq "image"
ABresult
0X0
100
111
113 0 $elt = $res;
114 }
115 else {
116 # Proceed to next asset.
117 0 next;
118 }
119 }
120  
121 0 ▶ next unless $ps;
Branch: 1 branch
BranchTrueFalseTotal
unless $ps 0 0 0
122  
123 0 ▶ if ( $elt->{type} eq "image" && $elt->{subtype} eq "svg" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elt->{"type"} eq "image" and $elt->{"subtype"} eq "svg") 0 0 0
Condition0%0/3
$elt->{"type"} eq "image" and $elt->{"subtype"} eq "svg"
!ll&&!rl&&r
000
MC/DC0%0/2
$elt->{"type"} eq "image" and $elt->{"subtype"} eq "svg"
$elt->{"type"} eq "image" $elt->{"subtype"} eq "svg"
Truth table0%0/3
A and B
A= $elt->{"type"} eq "image"B= $elt->{"subtype"} eq "svg"
ABresult
0X0
100
111
124 0 ▶ warn("Assets: Preparing SVG image\n") if $config->{debug}->{images};
Branch: 1 branch
BranchTrueFalseTotal
if $main::config->{"debug"}{"images"} 0 0 0
125 0 require SVGPDF;
126 0 SVGPDF->VERSION(0.080);
127  
128 # One or more?
129 my $combine = ( !($elt->{opts}->{split}//1)
130 || $elt->{opts}->{id}
131 0 ▶ || defined($elt->{opts}->{spread}) )
Branch: 1 branch
BranchTrueFalseTotal
!($elt->{'opts'}{'split'} // 1) || $elt->{'opts'}{'id'} || defined $elt->{'opts'}{'spread'} ? : 0 0 0
Condition0%0/8
not $elt->{"opts"}{"split"} // 1 or $elt->{"opts"}{"id"} or defined $elt->{"opts"}{"spread"}
l!l&&r!l&&!r
000
not $elt->{"opts"}{"split"} // 1 or $elt->{"opts"}{"id"}
l!l&&r!l&&!r
000
$elt->{"opts"}{"split"} // 1
l!l
00
MC/DC0%0/3
not $elt->{"opts"}{"split"} // 1 or $elt->{"opts"}{"id"} or defined $elt->{"opts"}{"spread"}
$elt->{"opts"}{"split"} $elt->{"opts"}{"id"} defined $elt->{"opts"}{"spread"}
Truth table0%0/4
not(A) or B or C
A= $elt->{"opts"}{"split"}B= $elt->{"opts"}{"id"}C= defined $elt->{"opts"}{"spread"}
ABCresult
0XX1
11X1
1011
1000
132 ? "stacked" : "none";
133 0 ▶ my $sep = $elt->{opts}->{staffsep} || 0;
Condition0%0/2
$elt->{'opts'}{'staffsep'} || 0
l!l
00
MC/DC0%0/1
$elt->{'opts'}{'staffsep'} || 0
$elt->{'opts'}{'staffsep'}
Truth table0%0/2
A
A= $elt->{'opts'}{'staffsep'}
Aresult
11
00
134  
135 # Note we need special font and text handlers.
136 my $p = SVGPDF->new
137 ( pdf => $ps->{pr}->{pdf},
138 0 ▶ fc => sub { svg_fonthandler( $ps, @_ ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
139 0 ▶ tc => sub { svg_texthandler( $ps, @_ ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
140 atts => { debug => $config->{debug}->{svg} > 1,
141 0 ▶ verbose => $config->{debug}->{svg} // 0,
Condition0%0/2
$main::config->{"debug"}{"svg"} // 0
l!l
00
MC/DC0%0/1
$main::config->{"debug"}{"svg"} // 0
$main::config->{"debug"}{"svg"}
Truth table0%0/2
A
A= $main::config->{"debug"}{"svg"}
Aresult
11
00
142 } );
143 0 my $data = $elt->{data};
144 my $o = $p->process( $data ? \join( "\n", @$data ) : $elt->{uri},
145 0 ▶ combine => $combine,
Branch: 1 branch
BranchTrueFalseTotal
$data ? : 0 0 0
146 sep => $sep,
147 );
148 warn( "Assets: Preparing SVG image => ",
149 plural(0+@$o, " element"), ", combine=$combine\n")
150 0 ▶ if $config->{debug}->{images};
Branch: 1 branch
BranchTrueFalseTotal
if $main::config->{"debug"}{"images"} 0 0 0
151 0 ▶ if ( ! @$o ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$o) 0 0 0
152 0 warn("Error in SVG embedding (no SVG objects found)\n");
153 0 next;
154 }
155  
156 my $res =
157 $s->{assets}->{$id} = {
158 type => "image",
159 subtype => "xform",
160 width => $o->[0]->{width},
161 height => $o->[0]->{height},
162 vwidth => $o->[0]->{vwidth},
163 vheight => $o->[0]->{vheight},
164 data => $o->[0]->{xo},
165 0 ▶ opts => { %{ $o->[0]->{opts} // {} },
Condition0%0/2
$o->[0]{"opts"} // {}
l!l
00
MC/DC0%0/1
$o->[0]{"opts"} // {}
$o->[0]{"opts"}
Truth table0%0/2
A
A= $o->[0]{"opts"}
Aresult
11
00
166 0 ▶ %{ $s->{assets}->{$id}->{opts} // {} },
Condition0%0/2
$s->{"assets"}{$id}{"opts"} // {}
l!l
00
MC/DC0%0/1
$s->{"assets"}{$id}{"opts"} // {}
$s->{"assets"}{$id}{"opts"}
Truth table0%0/2
A
A= $s->{"assets"}{$id}{"opts"}
Aresult
11
00
167 },
168 sep => $sep,
169 };
170 0 ▶ if ( @$o > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$o > 1) 0 0 0
171 0 $res->{multi} = $o;
172 }
173 warn("Created asset $id (xform, ",
174 $o->[0]->{vwidth}, "x", $o->[0]->{vheight}, ")",
175 " scale=", $res->{opts}->{scale} || 1,
176 " align=", $res->{opts}->{align}//"default",
177 " sep=", $sep,
178 " base=", $res->{opts}->{base}//"",
179 "\n")
180 0 ▶ if $config->{debug}->{images};
Branch: 1 branch
BranchTrueFalseTotal
if $main::config->{"debug"}{"images"} 0 0 0
Condition0%0/6
$res->{'opts'}{'scale'} || 1
l!l
00
$res->{"opts"}{"align"} // "default"
l!l
00
$res->{"opts"}{"base"} // ""
l!l
00
MC/DC0%0/1
$res->{'opts'}{'scale'} || 1
$res->{'opts'}{'scale'}
MC/DC0%0/1
$res->{"opts"}{"align"} // "default"
$res->{"opts"}{"align"}
MC/DC0%0/1
$res->{"opts"}{"base"} // ""
$res->{"opts"}{"base"}
Truth table0%0/2
A
A= $res->{'opts'}{'scale'}
Aresult
11
00
Truth table0%0/2
A
A= $res->{"opts"}{"align"}
Aresult
11
00
Truth table0%0/2
A
A= $res->{"opts"}{"base"}
Aresult
11
00
181 0 next;
182 }
183  
184 0 ▶ if ( $elt->{type} eq "image" && $elt->{subtype} eq "xform" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elt->{"type"} eq "image" and $elt->{"subtype"} eq "xform") 0 0 0
Condition0%0/3
$elt->{"type"} eq "image" and $elt->{"subtype"} eq "xform"
!ll&&!rl&&r
000
MC/DC0%0/2
$elt->{"type"} eq "image" and $elt->{"subtype"} eq "xform"
$elt->{"type"} eq "image" $elt->{"subtype"} eq "xform"
Truth table0%0/3
A and B
A= $elt->{"type"} eq "image"B= $elt->{"subtype"} eq "xform"
ABresult
0X0
100
111
185 # Ready to go.
186 0 next;
187 }
188  
189 0 ▶ if ( $elt->{type} eq "image" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elt->{"type"} eq "image") 0 0 0
190 0 ▶ warn("Assets: Preparing $elt->{subtype} image\n") if $config->{debug}->{images};
Branch: 1 branch
BranchTrueFalseTotal
if $main::config->{"debug"}{"images"} 0 0 0
191 0 ▶ if ( ($elt->{uri}//"") =~ /^chord:(.+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($elt->{'uri'} // '') =~ /^chord:(.+)/u) { } 0 0 0
Condition0%0/2
$elt->{"uri"} // ""
l!l
00
MC/DC0%0/1
$elt->{"uri"} // ""
$elt->{"uri"}
Truth table0%0/2
A
A= $elt->{"uri"}
Aresult
11
00
192 0 my $chord = $1;
193 # Look it up.
194 0 ▶ my $info = $s->{chordsinfo}->{$chord}
Condition0%0/3
$s->{"chordsinfo"}{$chord} // ChordPro::Chords::known_chord($chord)
l!l&&r!l&&!r
000
MC/DC0%0/2
$s->{"chordsinfo"}{$chord} // ChordPro::Chords::known_chord($chord)
$s->{"chordsinfo"}{$chord} ChordPro::Chords::known_chord($chord)
Truth table0%0/3
A // B
A= $s->{"chordsinfo"}{$chord}B= ChordPro::Chords::known_chord($chord)
ABresult
1X1
011
000
195 // ChordPro::Chords::known_chord($chord);
196 # If it is defined locally, merge.
197 0 ▶ for my $def ( @{ $s->{define} // [] } ) {
Condition0%0/2
$s->{"define"} // []
l!l
00
MC/DC0%0/1
$s->{"define"} // []
$s->{"define"}
Truth table0%0/2
A
A= $s->{"define"}
Aresult
11
00
198 0 ▶ next unless $def->{name} eq $chord;
Branch: 1 branch
BranchTrueFalseTotal
unless $def->{"name"} eq $chord 0 0 0
199 0 $info->{$_} = $def->{$_} for keys(%$def);
200 }
201 0 my $xo;
202 0 ▶ unless ( $info ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info) { } 0 0 0
203 0 warn("Unknown chord in asset: $1\n");
204 0 $xo = TextLayoutImageElement::alert(20);
205 }
206 else {
207 0 ▶ my $type = $elt->{opts}->{type} || $config->{instrument}->{type};
Condition0%0/3
$elt->{'opts'}{'type'} || $main::config->{'instrument'}{'type'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$elt->{'opts'}{'type'} || $main::config->{'instrument'}{'type'}
$elt->{'opts'}{'type'} $main::config->{'instrument'}{'type'}
Truth table0%0/3
A || B
A= $elt->{'opts'}{'type'}B= $main::config->{'instrument'}{'type'}
ABresult
1X1
011
000
208 0 my $p = ChordPro::Output::PDF::diagrammer($type);
209 0 $xo = $p->diagram_xo($info);
210 }
211 my $res =
212 $s->{assets}->{$id} = {
213 type => "image",
214 subtype => "xform",
215 width => $xo->width,
216 height => $xo->height,
217 data => $xo,
218 maybe opts => $s->{assets}->{$id}->{opts},
219 0 };
220 warn("Created asset $id ($elt->{subtype}, ",
221 $res->{width}, "x", $res->{height}, ")",
222 0 ▶ map { " $_=" . $res->{opts}->{$_} } keys( %{$res->{opts}//{}} ),
Condition0%0/2
$res->{"opts"} // {}
l!l
00
MC/DC0%0/1
$res->{"opts"} // {}
$res->{"opts"}
Truth table0%0/2
A
A= $res->{"opts"}
Aresult
11
00
223 "\n")
224 0 ▶ if $config->{debug}->{images};
Branch: 1 branch
BranchTrueFalseTotal
if $main::config->{"debug"}{"images"} 0 0 0
225 }
226 else {
227 0 ▶ if ( $elt->{uri} && !$elt->{data} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elt->{"uri"} and not $elt->{"data"}) 0 0 0
Condition0%0/3
$elt->{"uri"} and not $elt->{"data"}
!ll&&!rl&&r
000
MC/DC0%0/2
$elt->{"uri"} and not $elt->{"data"}
$elt->{"uri"} not $elt->{"data"}
Truth table0%0/3
A and not(B)
A= $elt->{"uri"}B= not $elt->{"data"}
ABresult
0X0
100
111
228 0 $elt->{data} = fs_blob( $elt->{uri}, { fail => 'hard' } );
229 }
230 0 ▶ my $data = $elt->{data} ? IO::String->new($elt->{data}) : $elt->{uri};
Branch: 1 branch
BranchTrueFalseTotal
$elt->{'data'} ? : 0 0 0
231 0 my $img = $pr->{pdf}->image($data);
232 0 my $subtype = lc(ref($img) =~ s/^.*://r);
233 0 ▶ $subtype = "jpg" if $subtype eq "jpeg";
Branch: 1 branch
BranchTrueFalseTotal
if $subtype eq "jpeg" 0 0 0
234 my $res =
235 $s->{assets}->{$id} = {
236 type => "image",
237 subtype => $subtype,
238 width => $img->width,
239 height => $img->height,
240 data => $img,
241 maybe opts => $s->{assets}->{$id}->{opts},
242 0 };
243 warn("Created asset $id ($elt->{subtype}, ",
244 $res->{width}, "x", $res->{height}, ")",
245 0 ( map { " $_=" . $res->{opts}->{$_} }
246 0 ▶ keys( %{$res->{opts}//{}} ) ),
Condition0%0/2
$res->{"opts"} // {}
l!l
00
MC/DC0%0/1
$res->{"opts"} // {}
$res->{"opts"}
Truth table0%0/2
A
A= $res->{"opts"}
Aresult
11
00
247 "\n")
248 0 ▶ if $config->{debug}->{images};
Branch: 1 branch
BranchTrueFalseTotal
if $main::config->{"debug"}{"images"} 0 0 0
249 }
250 }
251  
252 0 next;
253  
254 0 ▶ if ( $elt->{type} eq "image" && $elt->{opts}->{spread} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elt->{"type"} eq "image" and $elt->{"opts"}{"spread"}) 0 0 0
Condition0%0/3
$elt->{"type"} eq "image" and $elt->{"opts"}{"spread"}
!ll&&!rl&&r
000
MC/DC0%0/2
$elt->{"type"} eq "image" and $elt->{"opts"}{"spread"}
$elt->{"type"} eq "image" $elt->{"opts"}{"spread"}
Truth table0%0/3
A and B
A= $elt->{"type"} eq "image"B= $elt->{"opts"}{"spread"}
ABresult
0X0
100
111
255 0 ▶ if ( $s->{spreadimage} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s->{'spreadimage'}) { } 0 0 0
256 0 warn("Ignoring superfluous spread image\n");
257 }
258 else {
259 0 $s->{spreadimage} = $elt;
260 warn("Assets: Preparing images, got spread image\n")
261 0 ▶ if $config->{debug}->{images};
Branch: 1 branch
BranchTrueFalseTotal
if $main::config->{"debug"}{"images"} 0 0 0
262 0 next; # do not copy back
263 }
264 }
265  
266 }
267 }
268  
269 13 push( @EXPORT, 'prepare_asset' );
270  
271 # Font handler for SVG embedding.
272 0 ▶sub svg_fonthandler {
Subroutine svg_fonthandler: not called CC=11 SCAR=48.8
Pod undocumented
273 0 my ( $ps, $svg, %args ) = @_;
274 0 my ( $pdf, $style ) = @args{qw(pdf style)};
275  
276 0 my $family = lc( $style->{'font-family'} );
277 0 ▶ my $stl = lc( $style->{'font-style'} // "normal" );
Condition0%0/2
$style->{"font-style"} // "normal"
l!l
00
MC/DC0%0/1
$style->{"font-style"} // "normal"
$style->{"font-style"}
Truth table0%0/2
A
A= $style->{"font-style"}
Aresult
11
00
278 0 ▶ my $weight = lc( $style->{'font-weight'} // "normal" );
Condition0%0/2
$style->{"font-weight"} // "normal"
l!l
00
MC/DC0%0/1
$style->{"font-weight"} // "normal"
$style->{"font-weight"}
Truth table0%0/2
A
A= $style->{"font-weight"}
Aresult
11
00
279 0 ▶ my $size = $style->{'font-size'} || 12;
Condition0%0/2
$style->{'font-size'} || 12
l!l
00
MC/DC0%0/1
$style->{'font-size'} || 12
$style->{'font-size'}
Truth table0%0/2
A
A= $style->{'font-size'}
Aresult
11
00
280  
281 # Font cache.
282 0 state $fc = {};
283 0 my $key = join( "|", $family, $stl, $weight );
284  
285 # Clear cache when the PDF changes.
286 0 state $cf = "";
287 0 ▶ if ( $cf ne $ps->{pr}->{pdf} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cf ne $ps->{"pr"}{"pdf"}) 0 0 0
288 0 $cf = $ps->{pr}->{pdf};
289 0 $fc = {};
290 }
291  
292 # As a special case we handle fonts with 'names' like
293 # pdf.font.foo and map these to the corresponding font
294 # in the pdf.fonts structure.
295 0 ▶ if ( $family =~ /^pdf\.fonts\.(.*)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($family =~ /^pdf\.fonts\.(.*)/u) 0 0 0
296 0 my $try = $ps->{fonts}->{$1};
297 0 ▶ if ( $try ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($try) 0 0 0
298 warn("SVG: Font $family found in config: ",
299 $try->{_ff}, "\n")
300 0 ▶ if $config->{debug}->{svg};
Branch: 1 branch
BranchTrueFalseTotal
if $main::config->{"debug"}{"svg"} 0 0 0
301 # The font may change during the run, so we do not
302 # cache it.
303 0 return $try->{fd}->{font};
304 }
305 }
306  
307 0 ▶ local *Text::Layout::FontConfig::_fallback = sub { 0 };
Subroutine __ANON__: not called CC=1 SCAR=6.9
308  
309 0 ▶ my $font = $fc->{$key} //= do {
Condition0%0/3
$fc->{$key} //= do { my $t; my $try = eval { do { $t = "Text::Layout::FontConfig"->find_font($family, $stl, $weight); $t->get_font($ps->{"pr"}{"layout"}->copy) } }; if ($try) { warn("SVG: Font $key found in font config: ", $t->{"loader_data"}, "\n") if $main::config->{"debug"}{"svg"}; $try; } else { return; } }
l!l&&r!l&&!r
000
MC/DC0%0/2
$fc->{$key} //= do { my $t; my $try = eval { do { $t = "Text::Layout::FontConfig"->find_font($family, $stl, $weight); $t->get_font($ps->{"pr"}{"layout"}->copy) } }; if ($try) { warn("SVG: Font $key found in font config: ", $t->{"loader_data"}, "\n") if $main::config->{"debug"}{"svg"}; $try; } else { return; } }
$fc->{$key} do { my $t; my $try = eval { do { $t = "Text::Layout::FontConfig"->find_font($family, $stl, $weight); $t->get_font($ps->{"pr"}{"layout"}->copy) } }; if ($try) { warn("SVG: Font $key found in font config: ", $t->{"loader_data"}, "\n") if $main::config->{"debug"}{"svg"}; $try; } else { return; } }
Truth table0%0/3
A //= B
A= $fc->{$key}B= do { my $t; my $try = eval { do { $t = "Text::Layout::FontConfig"->find_font($family, $stl, $weight); $t->get_font($ps->{"pr"}{"layout"}->copy) } }; if ($try) { warn("SVG: Font $key found in font config: ", $t->{"loader_data"}, "\n") if $main::config->{"debug"}{"svg"}; $try; } else { return; } }
ABresult
1X1
011
000
310  
311 0 my $t;
312 my $try =
313 0 eval {
314 0 $t = Text::Layout::FontConfig->find_font( $family, $stl, $weight );
315 0 $t->get_font($ps->{pr}->{layout}->copy);
316 };
317 0 ▶ if ( $try ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($try) { } 0 0 0
318 warn("SVG: Font $key found in font config: ",
319 $t->{loader_data},
320 "\n")
321 0 ▶ if $config->{debug}->{svg};
Branch: 1 branch
BranchTrueFalseTotal
if $main::config->{"debug"}{"svg"} 0 0 0
322 0 $try;
323 }
324 else {
325 0 return;
326 }
327 };
328  
329 0 return $font;
330 }
331  
332 # Text handler for SVG embedding.
333 0 ▶sub svg_texthandler {
Subroutine svg_texthandler: not called CC=8 SCAR=42.8
Pod undocumented
334 0 my ( $ps, $svg, %args ) = @_;
335 0 my $xo = delete($args{xo});
336 0 my $pdf = delete($args{pdf});
337 0 my $style = delete($args{style});
338 0 my $text = delete($args{text});
339 0 my %opts = %args;
340  
341 0 my @t = split( /([♯♭])/, $text );
342 0 ▶ if ( @t == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@t == 1) 0 0 0
343 # Nothing special.
344 0 $svg->set_font( $xo, $style );
345 0 return $xo->text( $text, %opts );
346 }
347  
348 0 my ( $font, $sz ) = $svg->root->fontmanager->find_font($style);
349 0 my $has_sharp = $font->glyphByUni(ord("♯")) ne ".notdef";
350 0 my $has_flat = $font->glyphByUni(ord("♭")) ne ".notdef";
351 # For convenience we assume that either both are available, or missing.
352  
353 0 ▶ if ( $has_sharp && $has_flat ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_sharp and $has_flat) 0 0 0
Condition0%0/3
$has_sharp and $has_flat
!ll&&!rl&&r
000
MC/DC0%0/2
$has_sharp and $has_flat
$has_sharp $has_flat
Truth table0%0/3
A and B
A= $has_sharpB= $has_flat
ABresult
0X0
100
111
354 # Nothing special.
355 0 $xo->font( $font, $sz );
356 0 return $xo->text( $text, %opts );
357 }
358  
359 # Replace the sharp and flat glyphs by glyps from the chordfingers font.
360 0 my $d = 0;
361 0 my $this = 0;
362 0 ▶ while ( @t ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@t) 0 0 0
363 0 my $text = shift(@t);
364 0 my $fs = shift(@t);
365 0 ▶ $xo->font( $font, $sz ) unless $this eq $font;
Branch: 1 branch
BranchTrueFalseTotal
unless $this eq $font 0 0 0
366 0 $d += $xo->text($text);
367 0 $this = $font;
368 0 ▶ next unless $fs;
Branch: 1 branch
BranchTrueFalseTotal
unless $fs 0 0 0
369 0 $xo->font( $ps->{fonts}->{chordprosymbols}->{fd}->{font}, $sz );
370 0 $this = 0;
371 0 ▶ $d += $xo->text( $fs eq '♭' ? '!' : '#' );
Branch: 1 branch
BranchTrueFalseTotal
$fs eq "\x{266d}" ? : 0 0 0
372 }
373 0 return $d;
374 }
375  
376 13 1;
« lib/ChordPro/Delegate/ABC.pm ↑ Summary lib/ChordPro/lib/SVGPDF/Text.pm »