blib/lib/ChordPro/Output/PDF/KeyboardDiagram.pm untested

filter: Statementstmt 0.0% 0 / 163 Branchbran 0.0% 0 / 50 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 0.0% 0 / 3 Podpod n/a totaltotal 0.0% 0 / 247 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 #! perl
2  
3 use v5.26;
4 use Object::Pad;
5 use utf8;
6  
7 my $dcache; # cache core grids
8 my $pdf = ""; # for cache flush
9  
10 class ChordPro::Output::PDF::KeyboardDiagram;
11  
12 field $pr :param;
13  
14 field $config;
15 field $ps;
16  
17 field $kw;
18 field $kh;
19 field $lw;
20 field $fg;
21 field $keys;
22 field $base;
23 field $base_k;
24 field $show;
25 field $pressed;
26  
27 ADJUST {
28 $config = $::config;
29 $ps = $pr->{ps};
30 my $ctl = $ps->{kbdiagrams};
31 $kw = $ctl->{width} || 6;
32 $kh = $ctl->{height} || 6;
33 $lw = ($ctl->{linewidth} || 0.10) * $kw;
34 $keys = $ctl->{keys};
35 $base = $ctl->{base};
36 $show = $ctl->{show};
37 $pressed = $ctl->{pressed};
38 $dcache = {} if $pr->{pdf} ne $pdf;
39 $pdf = $pr->{pdf};
40  
41 unless ( $keys =~ /^(?:7|10|14|17|21)$/ ) {
42 die("pdf.kbdiagrams.keys is $keys, must be one of 7, 10, 14, 17, or 21\n");
43 }
44  
45 unless ( $base =~ /^(?:C|F)$/i ) {
46 die("pdf.kbdiagrams.base is \"$base\", must be \"C\" or \"F\"\n");
47 }
48 if ( uc($base) eq 'C' ) {
49 $base = $base_k = 0;
50 }
51 else { # must be 'F'
52 $base_k = 3;
53 $base = 5;
54 }
55  
56 unless ( $show =~ /^(?:top|bottom|right|below)$/i ) {
57 die("pdf.kbdiagrams.show is \"$show\", must be one of ".
58 "\"top\", \"bottom\", \"right\", or \"below\"\n");
59 }
60 }
61  
62 use constant DIAG_DEBUG => 0;
63  
64 # The vertical space the diagram requires.
65 method vsp0 ( $elt, $dummy = 0 ) {
66 $ps->{fonts}->{diagram}->{size} * 1.2 + $kh + $lw;
67 }
68  
69 # The advance height.
70 method vsp1 ( $elt, $dummy = 0 ) {
71 $ps->{kbdiagrams}->{vspace} * $kh;
72 }
73  
74 # The vertical space the diagram requires, including advance height.
75 method vsp ( $elt, $dummy = 0 ) {
76 $self->vsp0($elt) + $self->vsp1($elt);
77 }
78  
79 # The horizontal space the diagram requires.
80 method hsp0 ( $elt, $dummy = 0 ) {
81 $lw + $keys * $kw;
82 }
83  
84 # The advance width.
85 method hsp1 ( $elt, $dummy = 0 ) {
86 $ps->{kbdiagrams}->{hspace} * $kw;
87 }
88  
89 # The horizontal space the diagram requires, including advance width.
90 method hsp ( $elt, $dummy = 0 ) {
91 $self->hsp0($elt) + $self->hsp1($ps);
92 }
93  
94 my %keytypes =
95 ( 0 => [0,"L"], # Left
96 1 => [0,"B"], # Black
97 2 => [1,"M"], # Middle
98 3 => [1,"B"],
99 4 => [2,"R"], # Right
100 5 => [3,"L"],
101 6 => [3,"B"],
102 7 => [4,"M"],
103 8 => [4,"B"],
104 9 => [5,"M"],
105 10 => [5,"B"],
106 11 => [6,"R"] );
107  
108  
109 # The actual draw method.
110 method draw ( $info, $x, $y, $dummy = 0 ) {
111 return unless $info;
112 my $w = $lw + $kw * $keys;
113 $fg = $info->{diagram} // $ps->{theme}->{foreground};
114  
115 # Get (or infer) keys.
116 my @keys = @{ChordPro::Chords::get_keys($info)};
117 unless ( @keys ) {
118 warn("PDF: No diagram for chord \"", $info->name, "\"\n");
119 }
120  
121 my $font = $ps->{fonts}->{diagram};
122  
123 my $xo = $self->diagram_xo($info);
124 my @bb = $xo->bbox;
125 warn("BB [ @bb ] $x $y\n") if DIAG_DEBUG;
126 $pr->{pdfgfx}->object( $xo, $x,
127 $y - ($font->{size} * 1.2 + $lw) );
128  
129 # Draw font name.
130 $pr->setfont($font);
131 my $name = $info->chord_display;
132 $name .= "?" unless @keys;
133 $name = "<span color='$fg'>$name</span>"
134 if $info->{diagram};
135 $pr->text( $name, $x + ($w - $pr->strwidth($name))/2, $y - $pr->font_bl($font) );
136 }
137  
138 # Returns the complete diagram as an xo. This includes the core grid
139 # and pressed keys.
140 # Bounding box origin is top left of the grid.
141 # Note that the chord name is not part of the diagram.
142  
143 method diagram_xo ($info) {
144 return unless $info;
145  
146 my $col = $pressed // "red";
147 $fg = $info->{diagram} // $fg // $ps->{theme}->{foreground};
148 my $w = $lw + $kw * $keys;
149 my $v = $kh;
150  
151 # Get (or infer) keys.
152 my @keys = @{ChordPro::Chords::get_keys($info)};
153 unless ( @keys ) {
154 warn("PDF: No diagram for chord \"", $info->name, "\"\n");
155 }
156  
157 my $xo = $pdf->xo_form;
158  
159 # Draw the core grid.
160 $xo->line_width($lw);
161 my $xg = $self->grid_xo;
162 $xo->bbox( $xg->bbox );
163 $xo->object( $xg, 0, 0, 1 );
164  
165 my $kk = ( $keys % 7 == 0 )
166 ? 12 * int( $keys / 7 )
167 : $keys == 10 ? 17 : 29;
168  
169 # Vertical offsets in the key image.
170 my $t = 0;
171 my $m = $t - $kh / 2;
172 my $b = $t - $kh;
173  
174 # Horizontal offsets in the key image.
175 my $l = 0;
176 my $ml = $l + 1 * $kw / 3;
177 my $mr = $l + 2 * $kw / 3;
178 my $r = $l + $kw; # 3 * $kw / 3;
179 my $xr = $l + 4 * $kw / 3;
180  
181 # Don't use theme colour, use black & white.
182 $xo->stroke_color($fg);
183 $xo->fill_color($col);
184  
185 # Shift down if would start in 2nd octave.
186 my $kd = -int(($keys[0] + $info->{root_ord}) / 12) * 12;
187 # Adjust for diagram start.
188 $kd+=12 if ($keys[0] + $info->{root_ord}) < $base;
189  
190 for my $key ( @keys ) {
191 $key += $kd + $info->{root_ord};
192 $key += 12 if $key < 0;
193 $key -= 12 while $key >= $kk;
194 # Get octave and reduce.
195 my $o = int( $key / 12 ); # octave
196 $key %= 12;
197  
198 # Get the key type.
199 my ($pos,$type) = @{$keytypes{$key}};
200  
201 # Adjust for diagram start.
202 $pos -= $base_k;
203  
204 # Reduce to single octave and scale.
205 $pos += 7, $o-- while $pos < 0;
206 $pos %= 7;
207 $pos += 7 * $o if $o >= 1;
208  
209 # Actual displacement.
210 my $pkw = $pos * $kw;
211  
212 # Draw the keys.
213 if ( $type eq "L" ) {
214 $xo->move( $pkw + $l, $b );
215 $xo->polyline( $pkw + $l, $t,
216 $pkw + $mr, $t,
217 $pkw + $mr, $m,
218 $pkw + $r, $m,
219 $pkw + $r, $b )->close->fillstroke;
220 }
221 elsif ( $type eq "R" ) {
222 $xo->move( $pkw + $l, $b );
223 $xo->polyline( $pkw + $l, $m,
224 $pkw + $ml, $m,
225 $pkw + $ml, $t,
226 $pkw + $r, $t,
227 $pkw + $r, $b )->close->fillstroke;
228 }
229 elsif ( $type eq "M" ) {
230 $xo->move( $pkw + $l, $b );
231 $xo->polyline( $pkw + $l, $m,
232 $pkw + $ml, $m,
233 $pkw + $ml, $t,
234 $pkw + $mr, $t,
235 $pkw + $mr, $m,
236 $pkw + $r, $m,
237 $pkw + $r, $b )->close->fillstroke;
238 }
239 else {
240 $xo->rectangle( $pkw + $mr, $m,
241 $pkw + $xr, $t )->fillstroke;
242 }
243 }
244 # $xo->fillstroke;
245 $xo;
246 }
247  
248 use constant DIAG_GRID_XO => 0;
249  
250 method grid_xo {
251  
252 return $dcache->{$kw,$kh,$fg,$lw,$keys} //= do {
253  
254 my $w = $kw * $keys; # total width, excl linewidth
255 my $h = $kh; # total height, excl linewidth
256  
257 my $xo = $pdf->xo_form;
258  
259 # Bounding box must take linewidth into account.
260 # Origin is top left, so y runs negative.
261 my $xp = DIAG_GRID_XO ? 2 : 0;
262 my @bb = ( -$lw/2 - $xp, $lw/2 + $xp,
263 $w + $lw/2 + $xp, -($h + $lw/2) - $xp );
264 $xo->bbox(@bb);
265 $xo->line_width($lw);
266  
267 if ( DIAG_GRID_XO ) {
268 # Draw the grid.
269 $xo->fill_color('yellow');
270 $xo->rectangle(@bb)->fill;
271 }
272  
273 $xo->stroke_color($fg);
274 $xo->fill_color($fg);
275  
276 $xo->rectangle( 0, 0, $w, -$h );
277 for ( 1 .. $keys-1 ) {
278 $xo->move( $_*$kw, 0 );
279 $xo->vline(-$h);
280 }
281 $xo->stroke;
282 for my $i ( 1, 2, 4, 5, 6, 8, 9, 11,
283 12, 13, 15, 16, 18, 19, 20, 22, 23 ) {
284 next if $i < $base_k;
285 last if $i > $keys + $base_k;
286 my $x = ($i-$base_k-1)*$kw+2*$kw/3;
287 $xo->rectangle( $x, -$kh/2, $x + 2*$kw/3, 0 )->fillstroke;
288 }
289  
290 if ( DIAG_GRID_XO ) {
291 # Show origin.
292 $xo->fill_color('red');
293 my $lw = $lw/2;
294 $xo->rectangle( -$lw, -$lw, $lw, $lw )->fill;
295 }
296  
297 $xo;
298 };
299 }
300  
301 1;