lib/ChordPro/Delegate/ABC.pm

filter: Statementstmt 25.9% 76 / 293 Branchbran 4.8% 8 / 166 Conditioncond 10.4% 9 / 86 MC/DCmcdc 0.0% 0 / 45 Subroutinesub 56.2% 18 / 32 Podpod 0.0% 0 / 8 totaltotal 17.6% 111 / 630 CC 121 SCAR 81.0
CC
121
Cov
17%
abc2svg
6320.0
options
90.0
info
89.5
CRAP 3305.3
« lib/ChordPro/Output/PDF/Song.pm ↑ Summary lib/ChordPro/Assets.pm »
1 #!/usr/bin/perl
2  
3 11 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 package main;
7  
8 11 our $config;
9 11 our $options;
10  
11 package ChordPro::Delegate::ABC;
12  
13 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 11 ▶use feature qw( signatures );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 11 ▶no warnings "experimental::signatures";
Subroutine BEGIN: called CC=1 SCAR=0.0
17 11 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 11 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 11 ▶use File::Temp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
20 11 ▶use File::LoadLines;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 11 ▶use feature 'state';
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 11 ▶use ChordPro::Files;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 11 ▶use ChordPro::Paths;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 11 ▶use ChordPro::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 11 ▶use Text::ParseWords qw(shellwords);
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 11 ▶use constant { QUICKJS => "QuickJS",
Subroutine BEGIN: called CC=1 SCAR=0.0
29 11 QUICKJSXS => "QuickJS_XS" };
30  
31 0 ▶sub DEBUG() { $config->{debug}->{abc} }
Subroutine DEBUG: not called CC=1 SCAR=6.9
Pod undocumented
32  
33 # ABC processing using abc2svg and custom SVG processor.
34 # See info() below how the method is determined.
35  
36 # Song and PDF module uses 'can' to get at this.
37 0 ▶sub can( $class, $method ) {
Subroutine can: not called CC=2 SCAR=17.9
Pod undocumented
38 0 ▶ if ( $method eq "options" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq "options") 0 0 0
39 0 return \&options;
40 }
41 # abc2svg handlers are sorted out by info().
42 0 return \&abc2svg;
43 }
44  
45 11 my $backend;
46  
47 # Fetch a value for key $k from hash $h, with possible specialisation
48 # for the current backend.
49 # NOTE: This is a copy from Song.pm. Redo later.
50 0 ▶sub beo {
Subroutine beo: not called CC=6 SCAR=37.4
Pod undocumented
51 0 my ( $h, $k ) = @_;
52 0 ▶ if ( exists( $h->{$backend} )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $h->{$backend} and exists $h->{$backend}{$k} and defined $h->{$backend}{$k}) 0 0 0
Condition0%0/6
exists $h->{$backend} and exists $h->{$backend}{$k} and defined $h->{$backend}{$k}
!ll&&!rl&&r
000
exists $h->{$backend} and exists $h->{$backend}{$k}
!ll&&!rl&&r
000
MC/DC0%0/3
exists $h->{$backend} and exists $h->{$backend}{$k} and defined $h->{$backend}{$k}
exists $h->{$backend} exists $h->{$backend}{$k} defined $h->{$backend}{$k}
Truth table0%0/4
A and B and C
A= exists $h->{$backend}B= exists $h->{$backend}{$k}C= defined $h->{$backend}{$k}
ABCresult
0XX0
10X0
1100
1111
53 && exists( $h->{$backend}->{$k} )
54 && defined( $h->{$backend}->{$k} ) ) {
55 0 return $h->{$backend}->{$k};
56 }
57 0 ▶ return '' unless exists($h->{$k}) && defined($h->{$k});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $h->{$k} and defined $h->{$k} 0 0 0
Condition0%0/3
exists $h->{$k} and defined $h->{$k}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $h->{$k} and defined $h->{$k}
exists $h->{$k} defined $h->{$k}
Truth table0%0/3
A and B
A= exists $h->{$k}B= defined $h->{$k}
ABresult
0X0
100
111
58 0 return $h->{$k};
59 }
60  
61 # Default entry point.
62  
63 0 ▶sub abc2svg( $song, %args ) {
Subroutine abc2svg: not called CC=79 SCAR=87.5
Pod undocumented
64  
65 0 my $abc2svg = info();
66 0 $backend = lc $song->{generate};
67 0 my $cfg = { %{$config->{delegates}->{abc} } };
68  
69 0 ▶ if ( DEBUG() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (DEBUG()) 0 0 0
70 0 ::dump($abc2svg);
71 }
72  
73 0 state $cfg_checked;
74 0 ▶ unless ( $cfg_checked++ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cfg_checked++) 0 0 0
75 0 ▶ if ( ( beo( $cfg, 'config' ) || "default" ) ne "default" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ((beo($cfg, "config") || "default") ne "default") 0 0 0
Condition0%0/2
beo($cfg, 'config') || 'default'
l!l
00
MC/DC0%0/1
beo($cfg, 'config') || 'default'
beo($cfg, 'config')
Truth table0%0/2
A
A= beo($cfg, 'config')
Aresult
11
00
76 0 warn("ABC: delegates.abc.config is no longer used.\n");
77 warn("ABC: Config \"default.abc\" will be loaded instead.\n")
78 0 ▶ if !$abc2svg->{external} && fs_test( s => "default.abc" );
Branch: 1 branch
BranchTrueFalseTotal
if not $abc2svg->{"external"} and fs_test("s", "default.abc") 0 0 0
Condition0%0/3
not $abc2svg->{"external"} and fs_test("s", "default.abc")
!ll&&!rl&&r
000
MC/DC0%0/2
not $abc2svg->{"external"} and fs_test("s", "default.abc")
not $abc2svg->{"external"} fs_test("s", "default.abc")
Truth table0%0/3
not(A) and B
A= not $abc2svg->{"external"}B= fs_test("s", "default.abc")
ABresult
0X0
100
111
79 }
80 }
81  
82 0 my ( $elt, $pw ) = @args{qw(elt pagewidth)};
83  
84 0 ▶ return { type => "ignore" } unless @{ $elt->{data} };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$elt->{"data"};} 0 0 0
85 # Bail out if we don't have a suitable program.
86 0 ▶ unless ( $abc2svg->{method} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($abc2svg->{"method"}) 0 0 0
87 0 warn("Error in ABC embedding. Please install the JavaScript::QuickJS module.\n");
88 0 return;
89 }
90  
91 0 state $td = File::Temp::tempdir( CLEANUP => !$config->{debug}->{abc} );
92  
93 # External tools usually process a default.abc.
94 warn("ABC: Using config \"default.abc\".\n")
95 0 ▶ if index( $abc2svg->{method}, QUICKJSXS ) < 0 && fs_test( s => "default.abc" );
Branch: 1 branch
BranchTrueFalseTotal
if index($abc2svg->{"method"}, "QuickJS_XS") == -1 and fs_test("s", "default.abc") 0 0 0
Condition0%0/3
index($abc2svg->{"method"}, "QuickJS_XS") == -1 and fs_test("s", "default.abc")
!ll&&!rl&&r
000
MC/DC0%0/2
index($abc2svg->{"method"}, "QuickJS_XS") == -1 and fs_test("s", "default.abc")
index($abc2svg->{"method"}, "QuickJS_XS") == -1 fs_test("s", "default.abc")
Truth table0%0/3
A and B
A= index($abc2svg->{"method"}, "QuickJS_XS") == -1B= fs_test("s", "default.abc")
ABresult
0X0
100
111
96  
97 0 my $prep = make_preprocessor( beo( $cfg, 'preprocess' ) );
98  
99 # Prepare names for temporary files.
100 0 state $imgcnt = 0;
101 0 $imgcnt++;
102 0 my $src = fn_catfile( $td, "tmp${imgcnt}.abc" );
103 0 my $svg = fn_catfile( $td, "tmp${imgcnt}.svg" );
104 0 my $out = fn_catfile( $td, "tmp${imgcnt}.out" );
105 0 my $err = fn_catfile( $td, "tmp${imgcnt}.err" );
106  
107 # Get rid of as much space as possible.
108 # Jean-François Moine:
109 # If you have both "%%stretchstaff 1" and "%%trimsvg 1", and
110 # "%%stretchlast 0", only the final line is shorter.
111 # Otherwise, you can have "%%trimsvg 1" as the last line of the tune.
112 my @preamble =
113 ( '%%topspace 0',
114 '%%titlespace 0',
115 '%%musicspace 0',
116 '%%composerspace 0',
117 '%%infospace 0',
118 '%%textspace 0',
119 '%%leftmargin 0cm',
120 '%%rightmargin 0cm',
121 '%%stretchstaff 1',
122 '%%stretchlast 0',
123 '%%trimsvg 1',
124 '%%staffsep 0',
125 $backend =~ /html/ ? ( '%%fullsvg 1' ) : (),
126 0 ▶ @{ beo( $cfg, 'preamble' ) || [] } );
Branch: 1 branch
BranchTrueFalseTotal
$backend =~ /html/u ? : 0 0 0
Condition0%0/2
beo($cfg, 'preamble') || []
l!l
00
MC/DC0%0/1
beo($cfg, 'preamble') || []
beo($cfg, 'preamble')
Truth table0%0/2
A
A= beo($cfg, 'preamble')
Aresult
11
00
127  
128 0 ▶ if ( defined $elt->{opts}->{transpose} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $elt->{"opts"}{"transpose"}) 0 0 0
129 0 my $tr = $elt->{opts}->{transpose};
130 0 my $x;
131 0 ▶ if ( $tr->forced ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tr->forced) { } 0 0 0
132 0 ▶ $x = $tr->xp . ( $config->{keys}->{flats} ? "b" : "#" );
Branch: 1 branch
BranchTrueFalseTotal
$main::config->{'keys'}{'flats'} ? : 0 0 0
133 }
134 else {
135 0 $x = $tr->xp % @{ $config->{notes}->{sharp} };
136 }
137 0 ▶ unshift( @preamble, '%%transpose'." $x" ) if $x;
Branch: 1 branch
BranchTrueFalseTotal
if $x 0 0 0
138 }
139  
140 # Add mandatory field.
141 0 my @pre;
142 0 my @data = @{$elt->{data}};
143 0 ▶ while ( @data ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@data) 0 0 0
144 0 $_ = shift(@data);
145 0 ▶ unshift( @data, $_ ), last if /^X:/;
Branch: 1 branch
BranchTrueFalseTotal
if /^X:/u 0 0 0
146 0 push( @pre, $_ );
147 }
148 0 ▶ if ( @pre && !@data ) { # no X: found
Branch: 1 branch
BranchTrueFalseTotal
if (@pre and not @data) 0 0 0
Condition0%0/3
@pre and not @data
!ll&&!rl&&r
000
MC/DC0%0/2
@pre and not @data
@pre not @data
Truth table0%0/3
A and not(B)
A= @preB= not @data
ABresult
0X0
100
111
149 0 ▶ warn("X:1 (added)\n") if DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if DEBUG() 0 0 0
150 0 @data = ( "X:1", @pre );
151 0 @pre = ();
152 }
153 0 my $kv = {};
154 0 ▶ $kv = parse_kvm( @pre ) if @pre;
Branch: 1 branch
BranchTrueFalseTotal
if @pre 0 0 0
155 0 $kv = { %$kv, %{$elt->{opts}} };
156 0 ▶ $kv->{split} //= 1; # less overhead. really.
Condition0%0/2
$kv->{"split"} //= 1
l!l
00
MC/DC0%0/1
$kv->{"split"} //= 1
$kv->{"split"}
Truth table0%0/2
A
A= $kv->{"split"}
Aresult
11
00
157 0 ▶ $kv->{scale} ||= 1; # with id: design scale
Condition0%0/2
$kv->{"scale"} ||= 1
l!l
00
MC/DC0%0/1
$kv->{"scale"} ||= 1
$kv->{"scale"}
Truth table0%0/2
A
A= $kv->{"scale"}
Aresult
11
00
158 0 ▶ $kv->{align} //= ($kv->{center}//0) ? "center" : "left";
Branch: 1 branch
BranchTrueFalseTotal
$kv->{'center'} // 0 ? : 0 0 0
Condition0%0/4
$kv->{"align"} //= $kv->{"center"} // 0 ? "center" : "left"
l!l
00
$kv->{"center"} // 0
l!l
00
MC/DC0%0/1
$kv->{"align"} //= $kv->{"center"} // 0 ? "center" : "left"
$kv->{"align"}
Truth table0%0/2
A
A= $kv->{"align"}
Aresult
11
00
159 0 ▶ if ( $kv->{width} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kv->{"width"}) 0 0 0
160 0 $pw = $kv->{width};
161 }
162  
163 unshift( @preamble,
164 0 ▶ grep { /^%%/ } @pre,
Branch: 1 branch
BranchTrueFalseTotal
$pw ? : 0 0 0
165 $pw ? sprintf("%%%%pagewidth %dpx", $pw) : (),
166 );
167  
168 # Create the temp file for the ABC source.
169 0 my $fd;
170 0 ▶ unless ( $fd = fs_open( $src, '>:utf8' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fd = fs_open($src, ">:utf8")) 0 0 0
171 0 warn("Error in ABC embedding: $src: $!\n");
172 0 return;
173 }
174  
175 # Copy. We assume the user knows how to write ABC.
176 0 for ( @preamble ) {
177 0 print $fd $_, "\n";
178 0 ▶ warn($_, "\n") if DEBUG > 1;
Branch: 1 branch
BranchTrueFalseTotal
if DEBUG() > 1 0 0 0
179 }
180 0 for ( @data ) {
181 0 ▶ $prep->{abc}->($_) if $prep->{abc};
Branch: 1 branch
BranchTrueFalseTotal
if $prep->{"abc"} 0 0 0
182 0 print $fd $_, "\n";
183 0 ▶ warn($_, "\n") if DEBUG > 1;
Branch: 1 branch
BranchTrueFalseTotal
if DEBUG() > 1 0 0 0
184 }
185 0 ▶ for ( @{ beo( $cfg, 'postamble' ) || [] } ) {
Condition0%0/2
beo($cfg, 'postamble') || []
l!l
00
MC/DC0%0/1
beo($cfg, 'postamble') || []
beo($cfg, 'postamble')
Truth table0%0/2
A
A= beo($cfg, 'postamble')
Aresult
11
00
186 0 print $fd $_, "\n";
187 0 ▶ warn($_, "\n") if DEBUG > 1;
Branch: 1 branch
BranchTrueFalseTotal
if DEBUG() > 1 0 0 0
188 }
189  
190 0 ▶ unless ( close($fd) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (close $fd) 0 0 0
191 0 warn("Error in ABC embedding: $src: $!\n");
192 0 return;
193 }
194  
195 0 my @lines;
196 my $ret;
197  
198 0 ▶ if ( $abc2svg->{method} eq QUICKJSXS ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($abc2svg->{'method'} eq 'QuickJS_XS') { } 0 0 0
elsif ($abc2svg->{'method'} eq 'QuickJS') { } 0 0 0
elsif (not is_wx() || is_msw()) { } 0 0 0
199  
200 # QuickJS with embedded interpreter.
201  
202 0 my $js = JavaScript::QuickJS->new;
203 0 my $base = $abc2svg->{abclib} . "/abc2svg";
204 0 $js->set_module_base($base);
205  
206 my $qjsdata =
207 {
208 0 ▶ print => sub { push( @lines, split(/\n/, $_) ) for @_ },
Subroutine __ANON__: not called CC=2 SCAR=17.9
209 0 ▶ printErr => sub { print STDERR @_ },
Subroutine __ANON__: not called CC=1 SCAR=6.9
210 0 ▶ quit => sub { exit 66 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
211 0 ▶ readFile => sub { slurp($_[0]) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
212 0 ▶ get_mtime => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
213 0 my @stat = stat($_[0]);
214 0 ▶ return @stat ? 1000*$stat[9] : undef;
Branch: 1 branch
BranchTrueFalseTotal
@stat ? : 0 0 0
215 },
216 0 ▶ loadjs => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
217 0 my ( $fn, $relay, $onerror ) = @_;
218 0 ▶ if ( fs_test( sr => "$base/$fn" ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (fs_test('sr', "$base/$fn")) { } 0 0 0
elsif ($onerror) { } 0 0 0
219 0 $js->eval(slurp("$base/$_[0]"));
220 0 ▶ $relay->() if $relay;
Branch: 1 branch
BranchTrueFalseTotal
if $relay 0 0 0
221 }
222 elsif ( $onerror ) {
223 0 $onerror->();
224 }
225 else {
226 0 warn( qq{loadjs("$fn"): $!\n} );
227 }
228 },
229 0 };
230  
231 $js->set_globals
232 ( args => [ $src ],
233 0 ▶ load => sub { $js->eval(slurp("$base/$_[0]")) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
234 0 abc2svg => $qjsdata,
235 abc => {}, # for backends
236 );
237  
238 0 ▶ warn( "+ QuickJS_XS[", CP->display($base), "] $src\n") if DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if DEBUG() 0 0 0
239 0 my $hooks = "$base/../hooks.js";
240 0 ▶ undef $hooks unless fs_test( s => $hooks );
Branch: 1 branch
BranchTrueFalseTotal
unless fs_test("s", $hooks) 0 0 0
241  
242 0 eval {
243 0 $js->eval( slurp("$base/abc2svg-1.js") );
244 0 ▶ $js->eval( slurp($hooks) ) if $hooks;
Branch: 1 branch
BranchTrueFalseTotal
if $hooks 0 0 0
245 0 ▶ if ( -r "$base/../cmd.js" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-r "$base/../cmd.js") { } 0 0 0
246 0 ▶ warn(" QuickJS_XS using ", CP->display("$base/../cmd.js"),
Branch: 2 branches
BranchTrueFalseTotal
if DEBUG() 0 0 0
$hooks ? : 0 0 0
247 $hooks ? "+hooks" : "", "\n" )
248 if DEBUG;
249 0 $js->eval( slurp("$base/../cmd.js") );
250 }
251 else {
252 0 ▶ warn(" QuickJS_XS using ", CP->display("$base/cmdline.js"),
Branch: 2 branches
BranchTrueFalseTotal
if DEBUG() 0 0 0
$hooks ? : 0 0 0
253 $hooks ? "+hooks" : "", "\n" )
254 if DEBUG;
255 0 $js->eval( slurp("$base/cmdline.js") );
256 }
257 0 $js->eval( slurp("$base/tohtml.js") );
258 0 $js->eval( qq{abc_cmd("ChordPro", args, "QuickJS_XS")} );
259 };
260 0 ▶ warn($@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
261 0 undef $js;
262  
263 0 ▶ if ( DEBUG ) {
Branch: 1 branch
BranchTrueFalseTotal
if (DEBUG()) 0 0 0
264 0 my $fd = fs_open( $out, '>:utf8' );
265 0 print $fd join("\n", @lines), "\n";
266 0 close($fd);
267 }
268 }
269  
270 elsif ( $abc2svg->{method} eq QUICKJS ) {
271  
272 # QuickJS with external interpreter.
273  
274 0 my @cmd = @{ $abc2svg->{command} };
275  
276 0 push( @cmd, $out, $src );
277 0 ▶ if ( DEBUG ) {
Branch: 1 branch
BranchTrueFalseTotal
if (DEBUG()) 0 0 0
278 0 warn( "+ @cmd\n" );
279 0 $ENV{CHORDPRO_ABC_DEBUG} = 1;
280 }
281  
282 # Run the command.
283 0 $ret = eval { sys( @cmd ) };
284  
285 # Load data.
286 0 @lines = loadlines($out)
287 }
288  
289 # Not packaged. Check for Wx on Windows since we cannot redirect STD***.
290 ▶ elsif ( !is_wx() && !is_msw() ) {
Condition0%0/3
is_wx() || is_msw()
l!l&&r!l&&!r
000
MC/DC0%0/2
is_wx() || is_msw()
is_wx() is_msw()
Truth table0%0/3
A || B
A= is_wx()B= is_msw()
ABresult
1X1
011
000
291  
292 0 my @cmd = @{ $abc2svg->{command} };
293  
294 0 push( @cmd, $src );
295 0 ▶ warn( "+ @cmd\n" ) if DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if DEBUG() 0 0 0
296  
297 # Setup redirection for STDOUT/ERR.
298 0 my ( $oldout, $olderr );
299 0 ▶ open( $oldout, ">&STDOUT" )
Branch: 1 branch
BranchTrueFalseTotal
open $oldout, ">&STDOUT" or die("Can't dup STDOUT: $!") 0 0 0
300 or die "Can't dup STDOUT: $!";
301 0 ▶ open( $olderr, ">&", \*STDERR )
Branch: 1 branch
BranchTrueFalseTotal
open $olderr, ">&", \*STDERR or die("Can't dup STDERR: $!") 0 0 0
302 or die "Can't dup STDERR: $!";
303  
304 0 ▶ open(STDOUT, '>:utf8', $out)
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">:utf8", $out or die("Can't redirect STDOUT: $!") 0 0 0
305 or die "Can't redirect STDOUT: $!";
306 0 ▶ open(STDERR, ">:utf8", $err)
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">:utf8", $err or die("Can't dup STDERR: $!") 0 0 0
307 or die "Can't dup STDERR: $!";
308  
309 0 select STDERR; $| = 1; # make unbuffered
310 0 select STDOUT; $| = 1; # make unbuffered
311  
312 # Run the command.
313 0 $ret = eval { sys(@cmd) };
314  
315 # Reconnect STDOUT/ERR.
316 0 ▶ open(STDOUT, ">&", $oldout)
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">&", $oldout or die("Can't dup OLDOUT: $!") 0 0 0
317 or die "Can't dup OLDOUT: $!";
318 0 ▶ open(STDERR, ">&", $olderr)
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">&", $olderr or die("Can't dup OLDERR: $!") 0 0 0
319 or die "Can't dup OLDERR: $!";
320 0 select STDERR; $| = 1; # make unbuffered
321  
322 # Load data.
323 0 @lines = loadlines($out);
324 }
325  
326 else {
327 0 my @cmd = @{ $abc2svg->{command} };
328 0 push( @cmd, $src );
329 0 if ( 0 ) {
330 # This seemed a good idea but unfortunately Wx has problems
331 # returning the UTF8 data correctly. Non-ASCII characters are
332 # crippled.
333 warn( "+ @cmd\n" ) if DEBUG;
334 ( $ret, $out, $err ) = Wx::ExecuteStdoutStderr( "@cmd", 32 );
335 warn("ABC: $_") for @$err;
336 @lines = @$out;
337 }
338 else {
339 # This will cause a console window flash, but at least we get
340 # the data right.
341 0 ▶ warn( "+ @cmd > $out\n" ) if DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if DEBUG() 0 0 0
342 0 system( "@cmd > $out" );
343 0 @lines = loadlines($out);
344 }
345 }
346  
347 0 ▶ if ( $ret ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ret) 0 0 0
348 0 warn( sprintf( "Error in ABC embedding (ret = 0x%x)\n", $ret ) );
349 0 return;
350 }
351 0 ▶ if ( ! @lines ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@lines) 0 0 0
352 0 warn("Error in ABC embedding (no output?)\n");
353 0 return;
354 }
355 0 ▶ warn("SVG: ", scalar(@lines), " lines (raw)\n") if DEBUG > 1;
Branch: 1 branch
BranchTrueFalseTotal
if DEBUG() > 1 0 0 0
356  
357 # Postprocess the SVG data.
358 0 my $staffbase;
359 0 my $copy = 0;
360 0 @data = ();
361 0 my $lines = 1;
362 0 ▶ while ( @lines ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@lines) 0 0 0
363 0 $_ = shift(@lines);
364 0 ▶ if ( /\<!-- staffbase:(.*) --\>/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\<!-- staffbase:(.*) --\>/u) 0 0 0
365 0 $staffbase = $1;
366 0 ▶ warn("ABC: staffbase = $staffbase\n") if DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if DEBUG() 0 0 0
367 0 next;
368 }
369 0 ▶ if ( /^<svg/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^<svg/u) 0 0 0
370 0 $copy++;
371 }
372 0 ▶ push( @data, $_ ), $lines++ if $copy;
Branch: 1 branch
BranchTrueFalseTotal
if $copy 0 0 0
373 0 ▶ if ( /^<\/svg/ && @lines && $lines[0] =~ /^<\/div/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (m[^</svg]u and @lines and $lines[0] =~ m[^</div]u) 0 0 0
Condition0%0/6
m[^</svg]u and @lines and $lines[0] =~ m[^</div]u
!ll&&!rl&&r
000
m[^</svg]u and @lines
!ll&&!rl&&r
000
MC/DC0%0/3
m[^</svg]u and @lines and $lines[0] =~ m[^</div]u
m[^</svg]u @lines $lines[0] =~ m[^</div]u
Truth table0%0/4
A and B and C
A= m[^</svg]uB= @linesC= $lines[0] =~ m[^</div]u
ABCresult
0XX0
10X0
1100
1111
374 0 last;
375 }
376 }
377  
378 0 ▶ if ( DEBUG ) {
Branch: 1 branch
BranchTrueFalseTotal
if (DEBUG()) 0 0 0
379 0 open( $fd, '>:utf8', $svg );
380 0 print( $fd $_, "\n" ) for @data;
381 0 close($fd);
382 0 ▶ warn("SVG: ", 1+$lines, " lines (", fs_test( s => $svg ), " bytes)\n") if DEBUG > 1;
Branch: 1 branch
BranchTrueFalseTotal
if DEBUG() > 1 0 0 0
383 }
384  
385 0 my $scale;
386 my $design_scale;
387 0 ▶ if ( $kv->{scale} != 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kv->{"scale"} != 1) 0 0 0
388 0 ▶ if ( $kv->{id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kv->{'id'}) { } 0 0 0
389 0 $design_scale = $kv->{scale};
390 }
391 else {
392 0 $scale = $kv->{scale};
393 }
394 }
395 return
396 { type => "image",
397 line => $elt->{line},
398 subtype => "svg",
399 data => \@data,
400 opts => { maybe id => $kv->{id},
401 maybe align => $kv->{align},
402 maybe split => $kv->{split},
403 maybe spread => $kv->{spread},
404 maybe sep => $kv->{staffsep},
405 0 maybe base => $staffbase,
406 maybe scale => $scale,
407 maybe design_scale => $design_scale,
408 } };
409 }
410  
411 11 ▶sub have_xs {
Subroutine have_xs: called CC=2 SCAR=7.4
Pod undocumented
412 11 ▶ local $SIG{__WARN__} = sub {};
Subroutine __ANON__: not called CC=1 SCAR=0.0
413 11 state $ok;
414 11 ▶ $ok //= eval { require JavaScript::QuickJS };
Condition33%1/3
$ok //= eval { do { require JavaScript::QuickJS } }
l!l&&r!l&&!r
0110
MC/DC0%0/2
$ok //= eval { do { require JavaScript::QuickJS } }
$ok eval { do { require JavaScript::QuickJS } }
Truth table33%1/3
A //= B
A= $okB= eval { do { require JavaScript::QuickJS } }
ABresult
1X1
011
000
415 }
416  
417 0 ▶sub slurp {
Subroutine slurp: not called CC=3 SCAR=24.8
Pod undocumented
418 0 my ( $fn ) = @_;
419 0 my $opts = { split => 0, fail => "soft" };
420 0 my $data = loadlines( $fn, $opts );
421 0 ▶ warn("LOAD($fn): ", $opts->{error}, "\n")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data or $fn eq "default.abc" 0 0 0
Condition0%0/3
defined $data or $fn eq "default.abc"
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $data or $fn eq "default.abc"
defined $data $fn eq "default.abc"
Truth table0%0/3
A or B
A= defined $dataB= $fn eq "default.abc"
ABresult
1X1
011
000
422 unless defined $data || $fn eq "default.abc";
423 0 $data;
424 }
425  
426 # Determine the method to process the abc, and much more.
427 14 ▶sub info {
Subroutine info: called CC=21 SCAR=44.9
Pod undocumented
428 14 state $info = { handler => "" };
429 14 my $ctl = $::config->{delegates}->{abc};
430 14 ▶ my $handler = $ctl->{handler} // "abc2svg";
Condition50%1/2
$ctl->{"handler"} // "abc2svg"
l!l
140
MC/DC0%0/1
$ctl->{"handler"} // "abc2svg"
$ctl->{"handler"}
Truth table50%1/2
A
A= $ctl->{"handler"}
Aresult
11
00
431  
432 # Use cached info, but allow handler change between songs.
433 14 ▶ return $info if $handler eq $info->{handler};
Branch: 1 branch
BranchTrueFalseTotal
if $handler eq $info->{"handler"} 3 11 14
434  
435 11 my $exe;
436 11 $info->{handler} = $handler;
437  
438 11 state $checked;
439 11 ▶ unless ( $checked
Branch: 1 branch
BranchTrueFalseTotal
unless ($checked or $handler eq "abc2svg" or $handler =~ /^quickjs(?:_(?:xs|qjs))?$/u) 0 11 11
Condition33%2/6
$checked or $handler eq "abc2svg" or $handler =~ /^quickjs(?:_(?:xs|qjs))?$/u
l!l&&r!l&&!r
1100
$checked or $handler eq "abc2svg"
l!l&&r!l&&!r
0110
MC/DC0%0/3
$checked or $handler eq "abc2svg" or $handler =~ /^quickjs(?:_(?:xs|qjs))?$/u
$checked $handler eq "abc2svg" $handler =~ /^quickjs(?:_(?:xs|qjs))?$/u
Truth table25%1/4
A or B or C
A= $checkedB= $handler eq "abc2svg"C= $handler =~ /^quickjs(?:_(?:xs|qjs))?$/u
ABCresult
1XX1
01X1
0011
0000
440 || $handler eq "abc2svg"
441 || $handler =~ /^quickjs(?:_(?:xs|qjs))?$/
442 ) {
443 0 warn("ABC: Please remove handler \"$handler\" from your ABC delegates config and use \"abc2svg\" instead.\n");
444 0 $handler = "abc2svg";
445 0 $checked++;
446 }
447  
448 # Default handler "abc2svg" uses program (if set),
449 # otherwise embedded QuickJS or external QuickJS (in that order).
450 # Handler "quickjs_xs" uses embedded QuickJS only.
451 # Handler "quickjs_qjs" uses external QuickJS only.
452 # Handler "quickjs" uses internal or external QuickJS.
453  
454 11 ▶ if ( $handler eq "abc2svg" && !$ctl->{program} # QuickJS
Branch: 2 branches
BranchTrueFalseTotal
if ($handler eq 'abc2svg' and not $ctl->{'program'} or $handler eq 'quickjs' or $handler eq 'quickjs_xs' or $handler eq 'quickjs_qjs') { } 11 0 11
elsif ($handler eq 'abc2svg' and $ctl->{'program'} and $exe = CP()->findexe($ctl->{'program'}, 'silent', 1)) { } 0 0 0
Condition33%4/12
$handler eq "abc2svg" and not $ctl->{"program"} or $handler eq "quickjs" or $handler eq "quickjs_xs" or $handler eq "quickjs_qjs"
l!l&&r!l&&!r
1100
$handler eq "abc2svg" and not $ctl->{"program"} or $handler eq "quickjs" or $handler eq "quickjs_xs"
l!l&&r!l&&!r
1100
$handler eq "abc2svg" and not $ctl->{"program"} or $handler eq "quickjs"
l!l&&r!l&&!r
1100
$handler eq "abc2svg" and not $ctl->{"program"}
!ll&&!rl&&r
0011
MC/DC0%0/5
$handler eq "abc2svg" and not $ctl->{"program"} or $handler eq "quickjs" or $handler eq "quickjs_xs" or $handler eq "quickjs_qjs"
$handler eq "abc2svg" not $ctl->{"program"} $handler eq "quickjs" $handler eq "quickjs_xs" $handler eq "quickjs_qjs"
Truth table11%1/9
A and not(B) or C or D or E
A= $handler eq "abc2svg"B= not $ctl->{"program"}C= $handler eq "quickjs"D= $handler eq "quickjs_xs"E= $handler eq "quickjs_qjs"
ABCDEresult
11XXX1
0X1XX1
101XX1
0X01X1
1001X1
0X0011
100011
0X0000
100000
455 || $handler eq "quickjs" # XS or QJS
456 || $handler eq "quickjs_xs" # XS only
457 || $handler eq "quickjs_qjs" # QJS only
458 ) {
459 11 ▶ if ( $handler ne "quickjs_qjs" && have_xs() ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($handler ne 'quickjs_qjs' and have_xs()) { } 11 0 11
elsif ($handler ne 'quickjs_xs' and $exe = CP()->findexe('qjs', 'silent', 1)) { } 0 0 0
Condition33%1/3
$handler ne "quickjs_qjs" and have_xs()
!ll&&!rl&&r
0011
MC/DC0%0/2
$handler ne "quickjs_qjs" and have_xs()
$handler ne "quickjs_qjs" have_xs()
Truth table33%1/3
A and B
A= $handler ne "quickjs_qjs"B= have_xs()
ABresult
0X0
100
111
460 11 $info->{method} = QUICKJSXS;
461 }
462 ▶ elsif ( $handler ne "quickjs_xs"
Condition0%0/3
$handler ne "quickjs_xs" and $exe = CP()->findexe("qjs", "silent", 1)
!ll&&!rl&&r
000
MC/DC0%0/2
$handler ne "quickjs_xs" and $exe = CP()->findexe("qjs", "silent", 1)
$handler ne "quickjs_xs" $exe = CP()->findexe("qjs", "silent", 1)
Truth table0%0/3
A and B
A= $handler ne "quickjs_xs"B= $exe = CP()->findexe("qjs", "silent", 1)
ABresult
0X0
100
111
463 && ($exe = CP->findexe("qjs", silent => 1 )) ) {
464 0 $info->{method} = QUICKJS;
465 }
466 11 ▶ if ( $info->{method} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info->{"method"}) 11 0 11
467 11 my $dir = CP->findresdirs("abc")->[-1];
468 11 my $js = "$dir/abc2svg/abc2svg-1.js";
469 11 my @js = loadlines($js);
470 11 ▶ if ( $js[-1] =~ /abc2svg.version="(.*?)";abc2svg.vdate="(.*?)"/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($js[-1] =~ /abc2svg.version="(.*?)";abc2svg.vdate="(.*?)"/u) 11 0 11
471 11 $info->{version} = "ABC2SVG version $1 of $2";
472 11 $info->{abclib} = $dir;
473 }
474 11 $info->{info} = $info->{method} . " (" . $info->{version} . ")";
475 11 ▶ if ( $info->{method} eq QUICKJS ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info->{"method"} eq "QuickJS") 0 11 11
476 $info->{command} =
477 0 [ $exe, "--std", "${dir}/chordproabc.js",
478 "${dir}/abc2svg" ];
479 }
480 }
481 }
482  
483 ▶ elsif ( $handler eq "abc2svg" && $ctl->{program}
Condition0%0/6
$handler eq "abc2svg" and $ctl->{"program"} and $exe = CP()->findexe($ctl->{"program"}, "silent", 1)
!ll&&!rl&&r
000
$handler eq "abc2svg" and $ctl->{"program"}
!ll&&!rl&&r
000
MC/DC0%0/3
$handler eq "abc2svg" and $ctl->{"program"} and $exe = CP()->findexe($ctl->{"program"}, "silent", 1)
$handler eq "abc2svg" $ctl->{"program"} $exe = CP()->findexe($ctl->{"program"}, "silent", 1)
Truth table0%0/4
A and B and C
A= $handler eq "abc2svg"B= $ctl->{"program"}C= $exe = CP()->findexe($ctl->{"program"}, "silent", 1)
ABCresult
0XX0
10X0
1100
1111
484 && ( $exe = CP->findexe($ctl->{program}, silent => 1 ) ) ) {
485 0 $info->{handler} = $handler;
486 0 $info->{method} = CP->display($exe);
487 0 $info->{info} = $info->{method};
488 0 $info->{command} = [ $exe ];
489 }
490  
491 11 return $info;
492 }
493  
494 # Pre-scan.
495 0 ▶sub options( $data ) {
Subroutine options: not called CC=9 SCAR=45.0
Pod undocumented
496  
497 0 my @pre;
498 0 my @data = @$data;
499 0 ▶ while ( @data ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@data) 0 0 0
500 0 ▶ last if $data[0] =~ /^([A-Z]:|\%)/;
Branch: 1 branch
BranchTrueFalseTotal
if $data[0] =~ /^([A-Z]:|\%)/u 0 0 0
501 0 push( @pre, shift(@data) );
502 }
503 0 ▶ @pre = () if @pre && !@data; # no data found
Branch: 1 branch
BranchTrueFalseTotal
if @pre and not @data 0 0 0
Condition0%0/3
@pre and not @data
!ll&&!rl&&r
000
MC/DC0%0/2
@pre and not @data
@pre not @data
Truth table0%0/3
A and not(B)
A= @preB= not @data
ABresult
0X0
100
111
504 0 my $kv = {};
505 0 ▶ $kv = parse_kvm( @pre ) if @pre;
Branch: 1 branch
BranchTrueFalseTotal
if @pre 0 0 0
506 0 ▶ $kv->{align} //= ($kv->{center}//0) ? "center" : "left";
Branch: 1 branch
BranchTrueFalseTotal
$kv->{'center'} // 0 ? : 0 0 0
Condition0%0/4
$kv->{"align"} //= $kv->{"center"} // 0 ? "center" : "left"
l!l
00
$kv->{"center"} // 0
l!l
00
MC/DC0%0/1
$kv->{"align"} //= $kv->{"center"} // 0 ? "center" : "left"
$kv->{"align"}
Truth table0%0/2
A
A= $kv->{"align"}
Aresult
11
00
507 0 $kv;
508 }
509  
510 11 1;
« lib/ChordPro/Output/PDF/Song.pm ↑ Summary lib/ChordPro/Assets.pm »