| 1 | #! perl | ||
| 2 | |||
| 3 | package ChordPro::Output::PDF::Grid; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | use Carp; | ||
| 8 | use feature 'state'; | ||
| 9 | use feature 'signatures'; | ||
| 10 | no warnings 'experimental::signatures'; | ||
| 11 | use ChordPro::Utils qw( is_ttrue ); | ||
| 12 | |||
| 13 | sub gridline( $elt, $x, $y, $cellwidth, $barwidth, $margin, $ps, %opts ) { | ||
| 14 | |||
| 15 | # Grid context. | ||
| 16 | |||
| 17 | my $pr = $ps->{pr}; | ||
| 18 | my $fonts = $ps->{fonts}; | ||
| 19 | |||
| 20 | # Use the chords font for the chords, and for the symbols size. | ||
| 21 | my $fchord = { %{ $fonts->{grid} || $fonts->{chord} } }; | ||
| 22 | my $schord = $fonts->{gridstrum}; | ||
| 23 | delete($fchord->{background}); | ||
| 24 | $y -= $pr->font_bl($fchord); | ||
| 25 | |||
| 26 | pr_label_maybe( $ps, $x, $y ); | ||
| 27 | |||
| 28 | $x += $barwidth; | ||
| 29 | $cellwidth += $barwidth; | ||
| 30 | |||
| 31 | $elt->{tokens} //= [ {} ]; | ||
| 32 | |||
| 33 | my $firstbar; | ||
| 34 | my $lastbar; | ||
| 35 | foreach my $i ( 0 .. $#{ $elt->{tokens} } ) { | ||
| 36 | next unless is_bar( $elt->{tokens}->[$i] ); | ||
| 37 | $lastbar = $i; | ||
| 38 | $firstbar //= $i; | ||
| 39 | } | ||
| 40 | |||
| 41 | my $prevbar = -1; | ||
| 42 | my @tokens = @{ $elt->{tokens} }; | ||
| 43 | my $t; | ||
| 44 | |||
| 45 | if ( $margin->[0] ) { | ||
| 46 | $x -= $barwidth; | ||
| 47 | my $t = $elt->{margin}; | ||
| 48 | if ( $t && $t->{chords} ) { | ||
| 49 | if ( 0 && $::config->{settings}->{'chords-as-chords'} ) { | ||
| 50 | my $x = $x; | ||
| 51 | for ( 0..$#{ $t->{chords} } ) { | ||
| 52 | $x = $pr->text( $t->{chords}->[$_]->chord_display, | ||
| 53 | $x, $y, $fonts->{chord} ) | ||
| 54 | unless $t->{chords}->[$_] eq ""; | ||
| 55 | $x = $pr->text( $t->{phrases}->[$_], | ||
| 56 | $x, $y, $fonts->{grid_margin} ) | ||
| 57 | } | ||
| 58 | } | ||
| 59 | else { | ||
| 60 | $t->{text} = ""; | ||
| 61 | for ( 0..$#{ $t->{chords} } ) { | ||
| 62 | $t->{text} .= $t->{chords}->[$_]->chord_display | ||
| 63 | unless $t->{chords}->[$_] eq ""; | ||
| 64 | $t->{text} .= $t->{phrases}->[$_]; | ||
| 65 | } | ||
| 66 | $pr->text( $t->{text}, $x, $y, $fonts->{grid_margin} ); | ||
| 67 | } | ||
| 68 | } | ||
| 69 | elsif ( $t ) { | ||
| 70 | $pr->text( $t->{text}, $x, $y, $fonts->{grid_margin} ); | ||
| 71 | } | ||
| 72 | $x += $margin->[0] * $cellwidth + $barwidth; | ||
| 73 | } | ||
| 74 | |||
| 75 | my $ctl = $pr->{ps}->{grids}->{cellbar}; | ||
| 76 | my $col = $pr->{ps}->{grids}->{symbols}->{color}; | ||
| 77 | $opts{subtype} //= $opts{type} eq "gridline" ? "cellbars" : ""; | ||
| 78 | my $needcell = ( $opts{type} eq "gridline" | ||
| 79 | || $opts{subtype} eq "cellbars" ) && $ctl->{width}; | ||
| 80 | |||
| 81 | state $prevvoltastart; | ||
| 82 | my $align; | ||
| 83 | if ( $prevvoltastart && @tokens | ||
| 84 | && $tokens[0]->{class} eq "bar" && $tokens[0]->{align} ) { | ||
| 85 | $align = $prevvoltastart; | ||
| 86 | } | ||
| 87 | $prevvoltastart = 0; | ||
| 88 | |||
| 89 | my $voltastart; | ||
| 90 | foreach my $i ( 0 .. $#tokens ) { | ||
| 91 | my $token = $tokens[$i]; | ||
| 92 | my $sz = $fchord->{size}; | ||
| 93 | |||
| 94 | if ( $token->{class} eq "bar" ) { | ||
| 95 | $x -= $barwidth; | ||
| 96 | if ( $voltastart ) { | ||
| 97 | pr_voltafinish( $voltastart, $y, $x - $voltastart, $sz, $col, $pr ); | ||
| 98 | $voltastart = 0; | ||
| 99 | } | ||
| 100 | |||
| 101 | $t = $token->{symbol}; | ||
| 102 | if ( 0 ) { | ||
| 103 | $t = "{" if $t eq "|:"; | ||
| 104 | $t = "}" if $t eq ":|"; | ||
| 105 | $t = "}{" if $t eq ":|:"; | ||
| 106 | } | ||
| 107 | else { | ||
| 108 | $t = "|:" if $t eq "{"; | ||
| 109 | $t = ":|" if $t eq "}"; | ||
| 110 | $t = ":|:" if $t eq "}{"; | ||
| 111 | } | ||
| 112 | |||
| 113 | my $lcr = -1; # left, center, right | ||
| 114 | $lcr = 0 if $i > $firstbar; | ||
| 115 | $lcr = 1 if $i == $lastbar; | ||
| 116 | |||
| 117 | unless ( $opts{subtype} eq "cellbars" ) { | ||
| 118 | $x += $barwidth; | ||
| 119 | $prevbar = $i; | ||
| 120 | $needcell = 0; | ||
| 121 | next; | ||
| 122 | } | ||
| 123 | |||
| 124 | if ( $t eq "|" || $t eq ":|" ) { | ||
| 125 | if ( $token->{volta} ) { | ||
| 126 | if ( $align ) { | ||
| 127 | $x = $align; | ||
| 128 | $lcr = 0; | ||
| 129 | } | ||
| 130 | $voltastart = | ||
| 131 | pr_rptvolta( $x, $y, $lcr, $sz, $col, $pr, $token ); | ||
| 132 | $prevvoltastart ||= $x; | ||
| 133 | } | ||
| 134 | elsif ( $t eq ":|" ) { | ||
| 135 | pr_rptend( $x, $y, $lcr, $sz, $col, $pr ); | ||
| 136 | } | ||
| 137 | else { | ||
| 138 | pr_barline( $x, $y, $lcr, $sz, $col, $pr ); | ||
| 139 | } | ||
| 140 | } | ||
| 141 | elsif ( $t eq "||" ) { | ||
| 142 | pr_dbarline( $x, $y, $lcr, $sz, $col, $pr ); | ||
| 143 | } | ||
| 144 | elsif ( $t eq "|:" ) { | ||
| 145 | pr_rptstart( $x, $y, $lcr, $sz, $col, $pr ); | ||
| 146 | } | ||
| 147 | elsif ( $t eq ":|:" ) { | ||
| 148 | pr_rptendstart( $x, $y, $lcr, $sz, $col, $pr ); | ||
| 149 | } | ||
| 150 | elsif ( $t eq "|." ) { | ||
| 151 | pr_endline( $x, $y, $lcr, $sz, $col, $pr ); | ||
| 152 | } | ||
| 153 | elsif ( $t eq " %" ) { # repeat2Bars | ||
| 154 | pr_repeat( $x+$sz/2, $y, 0, $sz, $col, $pr ); | ||
| 155 | } | ||
| 156 | elsif ( $t eq " %%" ) { # repeat2Bars | ||
| 157 | pr_repeat2( $x+$sz/2, $y, 0, $sz, $col, $pr ); | ||
| 158 | } | ||
| 159 | else { | ||
| 160 | die($t); # can't happen | ||
| 161 | } | ||
| 162 | $x += $barwidth; | ||
| 163 | $prevbar = $i; | ||
| 164 | $needcell = 0; | ||
| 165 | next; | ||
| 166 | } | ||
| 167 | |||
| 168 | if ( $token->{class} eq "repeat2" ) { | ||
| 169 | # For repeat2Bars, change the next bar line to pseudo-bar. | ||
| 170 | my $k = $prevbar + 1; | ||
| 171 | while ( $k <= $#tokens | ||
| 172 | && !is_bar($tokens[$k]) ) { | ||
| 173 | $k++; | ||
| 174 | } | ||
| 175 | $tokens[$k] = { symbol => " %%", class => "bar" }; | ||
| 176 | $x += $cellwidth; | ||
| 177 | $needcell = 0; | ||
| 178 | next; | ||
| 179 | } | ||
| 180 | |||
| 181 | pr_cellline( $x-$barwidth, $y, 0, $sz, $ctl->{width}, | ||
| 182 | $pr->_fgcolor($ctl->{color}), $pr ) | ||
| 183 | if $needcell; | ||
| 184 | $needcell = ( $opts{type} eq "gridline" | ||
| 185 | || $opts{subtype} eq "cellbars" ) && $ctl->{width}; | ||
| 186 | |||
| 187 | if ( $token->{class} eq "chord" || $token->{class} eq "chords" ) { | ||
| 188 | my $tok = $token->{chords} // [ $token->{chord} ]; | ||
| 189 | my $cellwidth = $cellwidth / @$tok; | ||
| 190 | for my $t ( @$tok ) { | ||
| 191 | $x += $cellwidth, next if $t eq ''; | ||
| 192 | my $text = $t eq '/' ? $t : $t->chord_display; | ||
| 193 | $pr->text( $text, $x, $y, | ||
| 194 | $t->info->is_gridstrum ? $schord : $fchord ); | ||
| 195 | $x += $cellwidth; | ||
| 196 | } | ||
| 197 | } | ||
| 198 | elsif ( exists $token->{chord} ) { | ||
| 199 | # I'm not sure why not testing for class = chord... | ||
| 200 | warn("Chord token without class\n") | ||
| 201 | unless $token->{class} eq "chord"; | ||
| 202 | my $t = $token->{chord}; | ||
| 203 | $t = $t->chord_display; | ||
| 204 | $pr->text( $t, $x, $y, $fchord ) | ||
| 205 | unless $token eq "."; | ||
| 206 | $x += $cellwidth; | ||
| 207 | } | ||
| 208 | elsif ( $token->{class} eq "slash" ) { | ||
| 209 | $pr->text( "/", $x, $y, $fchord ); | ||
| 210 | $x += $cellwidth; | ||
| 211 | } | ||
| 212 | elsif ( $token->{class} eq "space" ) { | ||
| 213 | $x += $cellwidth; | ||
| 214 | } | ||
| 215 | elsif ( $token->{class} eq "repeat1" ) { | ||
| 216 | $t = $token->{symbol}; | ||
| 217 | my $k = $prevbar + 1; | ||
| 218 | while ( $k <= $#tokens | ||
| 219 | && !is_bar($tokens[$k]) ) { | ||
| 220 | $k++; | ||
| 221 | } | ||
| 222 | pr_repeat( $x + ($k - $prevbar - 1)*$cellwidth/2, $y, | ||
| 223 | 0, $fchord->{size}, $col, $pr ); | ||
| 224 | $x += $cellwidth; | ||
| 225 | } | ||
| 226 | if ( $x > $ps->{papersize}->[0] ) { | ||
| 227 | # This should be signalled by the parser. | ||
| 228 | # warn("PDF: Too few cells for content\n"); | ||
| 229 | last; | ||
| 230 | } | ||
| 231 | } | ||
| 232 | |||
| 233 | if ( $margin->[1] && $elt->{comment} ) { | ||
| 234 | my $t = $elt->{comment}; | ||
| 235 | if ( $t->{chords} ) { | ||
| 236 | $t->{text} = ""; | ||
| 237 | for ( 0..$#{ $t->{chords} } ) { | ||
| 238 | $t->{text} .= $t->{chords}->[$_]->chord_display . $t->{phrases}->[$_]; | ||
| 239 | } | ||
| 240 | } | ||
| 241 | $pr->text( " " . $t->{text}, $x, $y, $fonts->{grid_margin} ); | ||
| 242 | } | ||
| 243 | } | ||
| 244 | |||
| 245 | sub is_bar( $elt ) { | ||
| 246 | exists( $elt->{class} ) && $elt->{class} eq "bar"; | ||
| 247 | } | ||
| 248 | |||
| 249 | # Location and size of vertical bars | ||
| 250 | sub yh( $y, $h, $pr ) { | ||
| 251 | my $d = $pr->{ps}->{grids}->{stretch} || 0.825; | ||
| 252 | return ( $y + 0.9 * $h, | ||
| 253 | $h * $d * $pr->{ps}->{spacing}->{grid} ); | ||
| 254 | } | ||
| 255 | |||
| 256 | sub pr_cellline( $x, $y, $lcr, $sz, $w, $col, $pr ) { | ||
| 257 | $x -= $w / 2; | ||
| 258 | $pr->vline( $x, yh($y,$sz,$pr), $w, $col ); | ||
| 259 | } | ||
| 260 | |||
| 261 | sub pr_barline( $x, $y, $lcr, $sz, $col, $pr ) { | ||
| 262 | my $w = $sz / 10; # glyph width = $w | ||
| 263 | $x -= $w / 2; | ||
| 264 | $x += $w if $lcr < 0; | ||
| 265 | $pr->vline( $x, yh($y,$sz,$pr), $w, $col ); | ||
| 266 | } | ||
| 267 | |||
| 268 | sub pr_dbarline( $x, $y, $lcr, $sz, $col, $pr ) { | ||
| 269 | my $w = $sz / 10; # glyph width = 3 * $w | ||
| 270 | $x -= 0.5 + $w * ($lcr + 1); | ||
| 271 | # $x -= $w * (( 0.5, 1.5, 2.5 )[1+$lcr]); | ||
| 272 | $pr->vline( $x, yh($y,$sz,$pr), $w, $col ); | ||
| 273 | $x += 2 * $w; | ||
| 274 | $pr->vline( $x, yh($y,$sz,$pr), $w, $col ); | ||
| 275 | } | ||
| 276 | |||
| 277 | sub pr_rptstart( $x, $y, $lcr, $sz, $col, $pr ) { | ||
| 278 | my $w = $sz / 10; # glyph width = 3 * $w | ||
| 279 | $x -= 0.5; | ||
| 280 | $x += $w if $lcr < 0; | ||
| 281 | $pr->vline( $x, yh($y,$sz,$pr), $w, $col ); | ||
| 282 | $x += 2 * $w; | ||
| 283 | $y += 0.55 * $sz; | ||
| 284 | $pr->line( $x, $y, $x, $y+$w, $w, $col ); | ||
| 285 | $y -= 0.4 * $sz; | ||
| 286 | $pr->line( $x, $y, $x, $y+$w, $w, $col ); | ||
| 287 | } | ||
| 288 | |||
| 289 | sub pr_rptvolta( $x, $y, $lcr, $sz, $symcol, $pr, $token ) { | ||
| 290 | my $w = $sz / 10; # glyph width = 3 * $w | ||
| 291 | my $col = $pr->{ps}->{grids}->{volta}->{color}; | ||
| 292 | $x += $w if $lcr < 0; | ||
| 293 | if ( $token->{symbol} eq ":|" ) { | ||
| 294 | pr_rptend( $x, $y, $lcr, $sz, $col, $pr ); | ||
| 295 | } | ||
| 296 | else { | ||
| 297 | pr_barline( $x, $y, 0, $sz, $col, $pr ); | ||
| 298 | } | ||
| 299 | my $ret = $x -= $w / 2; | ||
| 300 | my $font = $pr->{ps}->{fonts}->{grid}; | ||
| 301 | $pr->setfont($font); | ||
| 302 | $pr->text( "<span color='$col'><sup>" . $token->{volta} . "</sup></span>", | ||
| 303 | $x+$w, $y, $font ); | ||
| 304 | $ret; | ||
| 305 | } | ||
| 306 | |||
| 307 | sub pr_voltafinish( $x, $y, $width, $sz, $symcol, $pr ) { | ||
| 308 | my $w = $sz / 10; # glyph width = 3 * $w | ||
| 309 | my ( $col, $span ) = @{$pr->{ps}->{grids}->{volta}}{qw(color span)}; | ||
| 310 | $pr->hline( $x, $y+0.9*$sz+$w/4, $width*$span, $w/2, $col ); | ||
| 311 | } | ||
| 312 | |||
| 313 | sub pr_rptend( $x, $y, $lcr, $sz, $col, $pr ) { | ||
| 314 | my $w = $sz / 10; # glyph width = 3 * $w | ||
| 315 | $x -= 2.5 * $w if $lcr >= 0; | ||
| 316 | $x += $w if $lcr < 0; | ||
| 317 | $pr->vline( $x + 2*$w, yh($y,$sz,$pr), $w, $col ); | ||
| 318 | $y += 0.55 * $sz; | ||
| 319 | $pr->line( $x, $y, $x, $y+$w, $w, $col ); | ||
| 320 | $y -= 0.4 * $sz; | ||
| 321 | $pr->line( $x, $y, $x, $y+$w, $w, $col ); | ||
| 322 | } | ||
| 323 | |||
| 324 | sub pr_rptendstart( $x, $y, $lcr, $sz, $col, $pr ) { | ||
| 325 | my $w = $sz / 10; # glyph width = 5 * $w | ||
| 326 | $lcr = 0; # center | ||
| 327 | $x -= 2.5 * $w * ($lcr + 1); | ||
| 328 | $x += $w if $lcr < 0; | ||
| 329 | $pr->vline( $x + 2*$w, yh($y,$sz,$pr), $w, $col ); | ||
| 330 | $y += 0.55 * $sz; | ||
| 331 | $pr->line( $x, $y, $x , $y+$w, $w, $col ); | ||
| 332 | $pr->line( $x+4*$w, $y, $x+4*$w, $y+$w, $w, $col ); | ||
| 333 | $y -= 0.4 * $sz; | ||
| 334 | $pr->line( $x, $y, $x, $y+$w, $w, $col ); | ||
| 335 | $pr->line( $x+4*$w, $y, $x+4*$w, $y+$w, $w, $col ); | ||
| 336 | } | ||
| 337 | |||
| 338 | sub pr_repeat( $x, $y, $lcr, $sz, $col, $pr ) { | ||
| 339 | my $w = $sz / 3; # glyph width = 3 * $w | ||
| 340 | $x -= 1.5 * $w; | ||
| 341 | my $lw = $sz / 10; | ||
| 342 | $x -= $w / 2; | ||
| 343 | $pr->line( $x, $y+0.2*$sz, $x + $w, $y+0.7*$sz, $lw ); | ||
| 344 | $pr->line( $x, $y+0.6*$sz, $x + 0.07*$sz , $y+0.7*$sz, $lw ); | ||
| 345 | $x += $w; | ||
| 346 | $pr->line( $x - 0.05*$sz, $y+0.2*$sz, $x + 0.02*$sz, $y+0.3*$sz, $lw ); | ||
| 347 | } | ||
| 348 | |||
| 349 | sub pr_repeat2( $x, $y, $lcr, $sz, $col, $pr ) { | ||
| 350 | my $w = $sz / 4; # glyph width = 4 * $w | ||
| 351 | $x -= 2.7 * $w; | ||
| 352 | my $lw = $sz / 10; | ||
| 353 | $x -= $w / 2; | ||
| 354 | $pr->line( $x, $y+0.2*$sz, $x + $w, $y+0.7*$sz, $lw ); | ||
| 355 | $pr->line( $x, $y+0.6*$sz, $x + 0.07*$sz , $y+0.7*$sz, $lw ); | ||
| 356 | $x += $w; | ||
| 357 | $pr->line( $x - $w/4, $y+0.2*$sz, $x + 0.75*$w, $y+0.7*$sz, $lw ); | ||
| 358 | $x += 0.75 * $w; | ||
| 359 | $pr->line( $x - 0.05*$sz, $y+0.2*$sz, $x + 0.02*$sz, $y+0.3*$sz, $lw ); | ||
| 360 | } | ||
| 361 | |||
| 362 | sub pr_endline( $x, $y, $lcr, $sz, $col, $pr ) { | ||
| 363 | my $w = $sz / 10; # glyph width = 2 * $w | ||
| 364 | $x -= 0.5 * $w * ($lcr + 1); | ||
| 365 | $x += $w if $lcr < 0; | ||
| 366 | $pr->vline( $x, yh($y,$sz,$pr), 2*$w ); | ||
| 367 | } | ||
| 368 | |||
| 369 | ################ Hooks ################ | ||
| 370 | |||
| 371 | *pr_label_maybe = *ChordPro::Output::PDF::Song::pr_label_maybe; | ||
| 372 | |||
| 373 | 1; |