| 1 | #! perl | ||
| 2 | |||
| 3 | use v5.26; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use feature qw( signatures ); | ||
| 7 | no warnings "experimental::signatures"; | ||
| 8 | use utf8; | ||
| 9 | |||
| 10 | package ChordPro::Delegate::Program; | ||
| 11 | |||
| 12 | =for docs | ||
| 13 | |||
| 14 | ** EXPERIMENTAL ** EXPERIMENTAL ** EXPERIMENTAL ** EXPERIMENTAL ** | ||
| 15 | |||
| 16 | Experimental delegate to produce an image by running an external program. | ||
| 17 | |||
| 18 | Example config file: | ||
| 19 | |||
| 20 | delegates { | ||
| 21 | |||
| 22 | // Emulate the 'ly' environment using Program delegate. | ||
| 23 | |||
| 24 | ly { | ||
| 25 | // Fixed. | ||
| 26 | module : Program | ||
| 27 | handler : cmd2image | ||
| 28 | type : image | ||
| 29 | |||
| 30 | // The program / command to execute. | ||
| 31 | program : /usr/bin/lilypond | ||
| 32 | |||
| 33 | // Input. | ||
| 34 | // Use value "stdin" to pass data via standard input. | ||
| 35 | // Use value "argfileN" or "argfile" to pass data via temporary | ||
| 36 | // file whose name is appended to the command arguments. | ||
| 37 | // Use value "argfile0" to pass data via temporary file | ||
| 38 | // whose name is prepended to the command arguments. | ||
| 39 | // Alternatively, the name of one of the predefined temporary | ||
| 40 | // files (%{tmpfile1} and %{tmpfile2}) can be used. Do not | ||
| 41 | // forget to add this name to the command arguments. | ||
| 42 | input : argfile | ||
| 43 | |||
| 44 | // Get the resultant output from a (temporary) file. | ||
| 45 | // Use value "stdout" to collect results from standard output. | ||
| 46 | // Note that lilypond will append "cropped.svg", so we pass | ||
| 47 | // the base of the file names. | ||
| 48 | result : "%{tmpbase}.cropped.svg" | ||
| 49 | |||
| 50 | // Command argumenta. | ||
| 51 | args : [ -dno-point-and-click --svg --silent | ||
| 52 | // Output to... | ||
| 53 | --output "%{tmpbase}" | ||
| 54 | // Input from... (appended, see "argfile" above) | ||
| 55 | ] | ||
| 56 | |||
| 57 | // Preprocessing. "first" and "last" are applied to the whole | ||
| 58 | // data at once. "lines" is applied line by line, after "first" | ||
| 59 | // and before "last". | ||
| 60 | preprocess { | ||
| 61 | first : [ {...} ] | ||
| 62 | lines : [ {...} ] | ||
| 63 | last : [ {...} ] | ||
| 64 | } | ||
| 65 | |||
| 66 | // (Optional) Input lines to prepend to the user data. | ||
| 67 | preamble : [ | ||
| 68 | '\\version "2.21.0"' | ||
| 69 | "#(ly:set-option 'crop #t)", | ||
| 70 | "\\header { tagline = ##f }" | ||
| 71 | ] | ||
| 72 | |||
| 73 | // (Optional) Input lines to apppend to the user data. | ||
| 74 | postamble : [] | ||
| 75 | |||
| 76 | // Default alignment is center, but ly delegate wants left. | ||
| 77 | align : left | ||
| 78 | |||
| 79 | } | ||
| 80 | } | ||
| 81 | |||
| 82 | Example of usage: | ||
| 83 | |||
| 84 | {start_of_ly} | ||
| 85 | \relative c''{ c d e f } | ||
| 86 | {end_of_ly} | ||
| 87 | |||
| 88 | Notes: | ||
| 89 | |||
| 90 | * No attribute options in the input. | ||
| 91 | |||
| 92 | =cut | ||
| 93 | |||
| 94 | use ChordPro::Files; | ||
| 95 | use ChordPro::Utils qw(dimension maybe make_preprocessor); | ||
| 96 | use ChordPro::Output::Common qw(fmt_subst); | ||
| 97 | use IPC::Run3 qw(run3); | ||
| 98 | use Ref::Util qw( is_arrayref ); | ||
| 99 | use ChordPro::Utils qw(detect_image_format); | ||
| 100 | |||
| 101 | sub DEBUG() { $::config->{debug}->{x1} } | ||
| 102 | |||
| 103 | sub cmd2image( $song, %args ) { | ||
| 104 | my $elt = $args{elt}; | ||
| 105 | my $ctl = { %{ $::config->{delegates}->{$elt->{context}} } }; | ||
| 106 | _cmd2image( $song, $ctl, %args ); | ||
| 107 | } | ||
| 108 | |||
| 109 | sub _cmd2image( $song, $ctl, %args ) { | ||
| 110 | my $elt = $args{elt}; | ||
| 111 | my $kv = { %{$elt->{opts}} }; | ||
| 112 | my $context = $elt->{context}; | ||
| 113 | |||
| 114 | # Default alignment. | ||
| 115 | $kv->{align} //= $ctl->{align}; | ||
| 116 | |||
| 117 | if ( DEBUG > 1 ) { | ||
| 118 | use DDP; p %args, as => "args"; | ||
| 119 | use DDP; p $elt, as => "elt"; | ||
| 120 | use DDP; p $kv, as => "opts"; | ||
| 121 | use DDP; p $ctl, as => "ctl"; | ||
| 122 | } | ||
| 123 | |||
| 124 | # Predefined temporary (file) names. | ||
| 125 | state $imgcnt++; | ||
| 126 | state $td = File::Temp::tempdir( CLEANUP => !DEBUG ); | ||
| 127 | my $tmpbase = fn_catfile( $td, "tmp${imgcnt}" ); | ||
| 128 | my $tmpfile1 = fn_catfile( $td, "tmp${imgcnt}a.tmp" ); | ||
| 129 | my $tmpfile2 = fn_catfile( $td, "tmp${imgcnt}b.tmp" ); | ||
| 130 | |||
| 131 | # Prepare meta for substitutions. | ||
| 132 | my $ss = { meta => { %{$song->{meta}}, | ||
| 133 | tmpdir => $td, | ||
| 134 | tmpbase => $tmpbase, | ||
| 135 | tmpfile1 => $tmpfile1, | ||
| 136 | tmpfile2 => $tmpfile2, | ||
| 137 | %$kv } }; | ||
| 138 | my $subst = sub($t) { defined($t) ? fmt_subst( $ss, $t ) : "" }; | ||
| 139 | |||
| 140 | # Transfer command arguments. | ||
| 141 | my @cmd; | ||
| 142 | for ( @{$ctl->{args}} ) { | ||
| 143 | push( @cmd, $subst->($_) ); | ||
| 144 | } | ||
| 145 | |||
| 146 | #### Input handling #### | ||
| 147 | |||
| 148 | my $input = $subst->($ctl->{input}) || "stdin"; | ||
| 149 | my @data = @{$elt->{data}}; | ||
| 150 | |||
| 151 | # Preprocessor. | ||
| 152 | my $prep = make_preprocessor( $ctl->{preprocess} ); | ||
| 153 | |||
| 154 | my $data; | ||
| 155 | if ( $prep->{first} ) { | ||
| 156 | $data = join( "\n", @data ); | ||
| 157 | $::config->{debug}->{pp} && warn("PRE[first]: ---\n", $data, "\n---\n"); | ||
| 158 | $prep->{first}->($data); | ||
| 159 | $::config->{debug}->{pp} && warn("POST[first]: ---\n", $data, "\n---\n"); | ||
| 160 | } | ||
| 161 | |||
| 162 | if ( $prep->{lines} ) { | ||
| 163 | @data = split( /\n/, $data ) if defined $data; | ||
| 164 | undef $data; | ||
| 165 | while ( @data ) { | ||
| 166 | $_ = shift(@data); | ||
| 167 | $::config->{debug}->{pp} && warn("PRE: ", $_, "\n"); | ||
| 168 | $prep->{lines}->($_); | ||
| 169 | $::config->{debug}->{pp} && warn("POST: ", $_, "\n"); | ||
| 170 | if ( /\n/ ) { | ||
| 171 | my @a = split( /\n/, $_ ); | ||
| 172 | unshift( @data, @a ); | ||
| 173 | } | ||
| 174 | } | ||
| 175 | } | ||
| 176 | |||
| 177 | if ( $prep->{last} ) { | ||
| 178 | $data = join( "\n", @data ) unless defined $data; | ||
| 179 | $::config->{debug}->{pp} && warn("PRE[last]: ---\n", $data, "\n---\n"); | ||
| 180 | $prep->{last}->($data); | ||
| 181 | $::config->{debug}->{pp} && warn("POST[last]: ---\n", $data, "\n---\n"); | ||
| 182 | } | ||
| 183 | @data = split( /\n/, $data ) if defined $data; | ||
| 184 | |||
| 185 | # Optional pre- and postamble. | ||
| 186 | if ( is_arrayref($ctl->{preamble}) ) { | ||
| 187 | unshift( @data, @{ $ctl->{preamble} } ); | ||
| 188 | } | ||
| 189 | |||
| 190 | if ( is_arrayref($ctl->{postamble}) ) { | ||
| 191 | push( @data, @{ $ctl->{postamble} } ); | ||
| 192 | } | ||
| 193 | |||
| 194 | my $input_data; | ||
| 195 | if ( $input eq 'stdin' ) { | ||
| 196 | $input_data = join( "\n", @data ); | ||
| 197 | DEBUG && ::dump( $input_data, as => "Input from $input"); | ||
| 198 | } | ||
| 199 | else { | ||
| 200 | if ( $input =~ /^argfile[0N]?$/i ) { | ||
| 201 | my $pos = uc($1 // "N"); | ||
| 202 | $input = fn_catfile( $td, "tmp${imgcnt}.in" ); | ||
| 203 | if ( $pos eq "0" ) { | ||
| 204 | unshift( @cmd, $input ); | ||
| 205 | } | ||
| 206 | else { | ||
| 207 | push( @cmd, $input ); | ||
| 208 | } | ||
| 209 | } | ||
| 210 | |||
| 211 | # Store input data in temp file. | ||
| 212 | my $fd = fs_open( $input, '>:utf8' ); | ||
| 213 | print $fd "$_\n" for @data; | ||
| 214 | close($fd); | ||
| 215 | DEBUG && ::dump( \@data, as => "Input from $input"); | ||
| 216 | } | ||
| 217 | |||
| 218 | #### Output handling #### | ||
| 219 | |||
| 220 | my $result = $subst->($ctl->{result}) || "stdout"; | ||
| 221 | DEBUG && warn("Result to $result\n"); | ||
| 222 | |||
| 223 | #### Diagnostics handling #### | ||
| 224 | |||
| 225 | my $errors = $subst->($ctl->{errors}) || "stderr"; | ||
| 226 | |||
| 227 | #### Run time #### | ||
| 228 | |||
| 229 | unshift( @cmd, $subst->($ctl->{program}) ); | ||
| 230 | DEBUG && ::dump( \@cmd, as => "Command:" ); | ||
| 231 | |||
| 232 | my $stdout_buf = ''; | ||
| 233 | my $stderr_buf = ''; | ||
| 234 | |||
| 235 | my $status; | ||
| 236 | if ( is_wx ) { | ||
| 237 | DEBUG && warn("Using Wx::ExecuteStdoutStderr\n"); | ||
| 238 | ( $status, $stdout_buf, $stderr_buf ) = | ||
| 239 | Wx::ExecuteStdoutStderr( "@cmd" ); | ||
| 240 | # $stdout_buf and $stderr_buf will be strings. | ||
| 241 | } | ||
| 242 | else { | ||
| 243 | DEBUG && warn("Using IPC::Run3::run3\n"); | ||
| 244 | $stdout_buf = ''; | ||
| 245 | $stderr_buf = ''; | ||
| 246 | run3( \@cmd, \$input_data, \$stdout_buf, \$stderr_buf, | ||
| 247 | { binmode_stdout => ':raw', | ||
| 248 | return_if_system_error => 1 } ); | ||
| 249 | # $stdout_buf and $stderr_buf will be strings. | ||
| 250 | $status = $? >> 8; | ||
| 251 | } | ||
| 252 | |||
| 253 | if ( DEBUG ) { | ||
| 254 | ::dump( $stdout_buf, as => "Raw result (status: $status)" ); | ||
| 255 | ::dump( $stderr_buf, as => "Raw diagnostics" ); | ||
| 256 | my $files = fs_find( $td, { filter => qr/^[^.]/ } ); | ||
| 257 | warn("Temp files:\n") if @$files; | ||
| 258 | for ( @$files ) { | ||
| 259 | warn( " ", fn_catfile( $td, $_->{name} ), | ||
| 260 | ", ", $_->{size}, " bytes\n" ); | ||
| 261 | } | ||
| 262 | } | ||
| 263 | |||
| 264 | my $o = { fail => 'soft' }; | ||
| 265 | if ( $result eq 'stdout' ) { | ||
| 266 | $result = $stdout_buf; | ||
| 267 | } | ||
| 268 | else { | ||
| 269 | $result = fs_blob( $result, $o ); | ||
| 270 | } | ||
| 271 | if ( $o->{error} ) { | ||
| 272 | warn("?Error fetching results: ", $o->{error}, "\n" ); | ||
| 273 | return; | ||
| 274 | } | ||
| 275 | DEBUG && ::dump( $result, as => "Result" ); | ||
| 276 | |||
| 277 | $o = { fail => 'soft' }; | ||
| 278 | $errors = fs_load( \$stderr_buf, $o ); | ||
| 279 | if ( $o->{error} ) { | ||
| 280 | warn("?Error fetching diagnostics: ", $o->{error}, "\n" ); | ||
| 281 | return; | ||
| 282 | } | ||
| 283 | |||
| 284 | if ( $errors && @$errors ) { | ||
| 285 | warn("Diagnostics from delegate '", $elt->{context}, "':\n"); | ||
| 286 | warn("$_\n") for @$errors; | ||
| 287 | } | ||
| 288 | |||
| 289 | if ( $status ) { | ||
| 290 | warn("?Error excuting @cmd (status = $status)\n"); | ||
| 291 | return; | ||
| 292 | } | ||
| 293 | |||
| 294 | if ( !$result ) { | ||
| 295 | warn("?Error excuting @cmd (no output?)\n"); | ||
| 296 | return; | ||
| 297 | } | ||
| 298 | |||
| 299 | my $subtype; | ||
| 300 | unless ( $subtype = $ctl->{subtype} ) { | ||
| 301 | # Get info. | ||
| 302 | my $info = detect_image_format(\$result); | ||
| 303 | if ( $info->{error} ) { | ||
| 304 | warn("?Error executing @cmd: ", $info->{error}, "\n"); | ||
| 305 | return; | ||
| 306 | } | ||
| 307 | $subtype = $info->{file_ext}; | ||
| 308 | } | ||
| 309 | $result = [ $result ] if $subtype eq 'svg'; | ||
| 310 | |||
| 311 | # Finish. | ||
| 312 | my $scale; | ||
| 313 | my $design_scale; | ||
| 314 | $kv->{scale} = dimension( $kv->{scale}//1, width => 1 ); | ||
| 315 | if ( $kv->{scale} != 1 ) { | ||
| 316 | if ( $kv->{id} ) { | ||
| 317 | $design_scale = $kv->{scale}; | ||
| 318 | } | ||
| 319 | else { | ||
| 320 | $scale = $kv->{scale}; | ||
| 321 | } | ||
| 322 | } | ||
| 323 | return | ||
| 324 | { type => $ctl->{type}, | ||
| 325 | line => $elt->{line}, | ||
| 326 | subtype => $subtype, | ||
| 327 | data => $result, | ||
| 328 | opts => { maybe id => $kv->{id}, | ||
| 329 | maybe align => $kv->{align}, | ||
| 330 | maybe spread => $kv->{spread}, | ||
| 331 | maybe scale => $scale, | ||
| 332 | maybe design_scale => $design_scale, | ||
| 333 | } }; | ||
| 334 | } | ||
| 335 | |||
| 336 | # Pre-scan. | ||
| 337 | sub options( $data ) { {} } | ||
| 338 | |||
| 339 | 1; |