blib/lib/ChordPro/Delegate/Lilypond.pm untested

filter: Statementstmt 0.0% 0 / 130 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 234 CC 27 SCAR 57.3
CC
27
Cov
0%
ly2svg
552.0
options
30.0
DEBUG
2.0
CRAP 306.5
1 #!/usr/bin/perl
2  
3 package main;
4  
5 our $config;
6 our $options;
7  
8 package ChordPro::Delegate::Lilypond;
9  
10 use strict;
11 use warnings;
12 use feature qw( signatures );
13 no warnings "experimental::signatures";
14 use utf8;
15 use File::Temp ();
16 use File::LoadLines;
17 use feature 'state';
18  
19 use ChordPro::Files;
20 use ChordPro::Paths;
21 use ChordPro::Utils;
22 use Text::ParseWords qw(shellwords);
23  
24 sub DEBUG() { $config->{debug}->{ly} }
25  
26 sub ly2svg( $self, %args ) {
27  
28 =for later
29  
30 my $elt = $args{elt};
31 my $ctl = { %{ $::config->{delegates}->{$elt->{context}} } };
32  
33 $ctl->{program} ||= "lilypond";
34 $ctl->{input} = "argfile";
35 $ctl->{result} = "\%{tmpbase}.cropped.svg";
36 $ctl->{args} = [ "-dno-point-and-click", "--svg", "--silent",
37 "--output", "\%{tmpbase}"
38 ];
39 $ctl->{align} = "left";
40 unshift( @{ $ctl->{preamble} },
41 "#(ly:set-option 'crop #t)" );
42  
43 use ChordPro::Delegate::Program;
44 ChordPro::Delegate::Program::_cmd2image( $self, $ctl, %args );
45 }
46  
47 sub _ly2svg( $self, %args ) {
48  
49 =cut
50  
51 my ( $elt, $pw ) = @args{qw(elt pagewidth)};
52  
53 state $imgcnt = 0;
54 state $td = File::Temp::tempdir( CLEANUP => !$config->{debug}->{ly} );
55  
56 $imgcnt++;
57 my $src = fn_catfile( $td, "tmp${imgcnt}.ly" );
58 my $svg = fn_catfile( $td, "tmp${imgcnt}.svg" );
59  
60 my $fd;
61 unless ( $fd = fs_open( $src, '>:utf8' ) ) {
62 warn("Error in Lilypond embedding: $src: $!\n");
63 return;
64 }
65  
66 my $need_version = 1;
67 my @pre;
68 for ( keys(%{$elt->{opts}}) ) {
69  
70 if ( $_ eq "version" ) {
71 push( @pre, "\\version \"", $elt->{opts}->{$_}, "\"" );
72 warn ( "\\version \"", $elt->{opts}->{$_}, "\"\n" ) if DEBUG;
73 $need_version = 0;
74 }
75 else {
76 push( @pre, '%%'.$_." ".$elt->{opts}->{$_} );
77 warn('%%'.$_." ".$elt->{opts}->{$_}."\n") if DEBUG;
78 }
79 }
80  
81 for ( @{ $config->{delegates}->{ly}->{preamble} } ) {
82 push( @pre, $_ );
83 warn( "$_\n") if DEBUG;
84 $need_version = 0 if /^\\version\s+/;
85 }
86  
87 if ( $need_version ) {
88 my $v = "2.21.0";
89 unshift( @pre, "\\version \"$v\"" );
90 warn("ly: no \\version seen, assuming \"$v\"\n");
91 }
92 print $fd "$_\n" for @pre,
93 "#(ly:set-option 'crop #t)",
94 "\\header { tagline = ##f }";
95  
96 @pre = ();
97 my @data = @{$elt->{data}};
98 while ( @data ) {
99 $_ = shift(@data);
100 unshift( @data, $_ ), last if /^[%\\]/; # LP data
101 push( @pre, $_ );
102 }
103 if ( @pre && !@data ) { # no LP found
104 @data = @pre;
105 @pre = ();
106 }
107  
108 my $kv = { %$elt };
109 $kv = parse_kvm( @pre ) if @pre;
110 $kv->{scale} ||= 1;
111 $kv->{align} //= ($kv->{center}//0) ? "center" : "left";
112  
113 # Copy. We assume the user knows how to write LilyPond.
114 for ( @data ) {
115 print $fd $_, "\n";
116 warn($_, "\n") if DEBUG;
117 }
118  
119 unless ( close($fd) ) {
120 warn("Error in Lilypond embedding: $src: $!\n");
121 return;
122 }
123  
124 if ( $kv->{width} ) {
125 $pw = $kv->{width};
126 }
127  
128 my $program = $config->{delegates}->{ly}->{program} || "lilypond";
129 state $lilypond = CP->findexe( $program, silent => 1 );
130 unless ( $lilypond ) {
131 warn("Error in Lilypond embedding: missing 'lilypond' tool.\n");
132 return;
133 }
134  
135 my @cmd = ( $lilypond, qw( -dno-point-and-click --svg ) );
136 push( @cmd, "--silent" ) unless DEBUG;
137 ( my $im1 = $svg ) =~ s/\.\w+$//;
138 push( @cmd, "-o", $im1, $src );
139 warn( "+ @cmd\n" ) if DEBUG;
140 my $ret = sys( @cmd );
141  
142 if ( $ret ) {
143 warn( sprintf( "Error in Lilypond embedding (ret = 0x%x)\n", $ret ) );
144 return;
145 }
146 if ( !fs_test( s => "$im1.cropped.svg" ) ) {
147 warn("Error in Lilypond embedding (no output?)\n");
148 return;
149 }
150  
151 warn("SVG: ", fs_test( s => $svg ), " bytes\n") if $config->{debug}->{ly};
152 my $scale;
153 my $design_scale;
154 if ( $kv->{scale} != 1 ) {
155 if ( $kv->{id} ) {
156 $design_scale = $kv->{scale};
157 }
158 else {
159 $scale = $kv->{scale};
160 }
161 }
162 return
163 { type => "image",
164 line => $elt->{line},
165 subtype => "svg",
166 uri => "$im1.cropped.svg",
167 opts => { maybe id => $kv->{id},
168 maybe align => $kv->{align},
169 maybe spread => $kv->{spread},
170 maybe scale => $scale,
171 maybe design_scale => $design_scale,
172 } };
173 }
174  
175 sub ly2image( $s, $pw, $elt ) {
176 croak("Lilypond: Please adjust your delegate config to use handler \"ly2svg\" instead of \"ly2image\"");
177 }
178  
179 # Pre-scan.
180 sub options( $data ) {
181  
182 my @pre;
183 my @data = @$data;
184 while ( @data ) {
185 last if $data[0] =~ /^[%\\]/; # LP data
186 push( @pre, shift(@data) );
187 }
188 @pre = () if @pre && !@$data; # no LP found
189 my $kv = {};
190 $kv = parse_kvm( @pre ) if @pre;
191 $kv->{align} //= ($kv->{center}//0) ? "center" : "left";
192 $kv;
193 }
194  
195 1;