PlayTab/Output/PostScript.pm

filter: Statementstmt 73.9% 179 / 242 Branchbran 68.7% 55 / 80 Conditioncond 66.6% 4 / 6 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 71.8% 23 / 32 Podpod 0.0% 0 / 30 totaltotal 66.6% 262 / 393 CC 47 SCAR 21.6
CC
47
Cov
73%
ps
21.1
grids
12.0
print_grid
12.0
CRAP 8.6
« PlayTab/Chord.pm ↑ Summary PlayTab/Note.pm »
1 #! perl
2  
3 # Author : Johan Vromans
4 # Created On : Thu Mar 27 16:46:54 2014
5 # Last Modified By: Johan Vromans
6 # Last Modified On: Tue Dec 5 13:17:54 2017
7 # Update Count : 239
8 # Status : Unknown, Use with caution!
9  
10 package App::Music::PlayTab::Output::PostScript;
11  
12 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 7 our $VERSION = "0.001";
16  
17 # Object management.
18  
19 2 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
20 2 bless { init => 0 }, shift;
21 }
22  
23 # Initial default values.
24 7 my $std_width = 30;
25 7 my $std_height = -15;
26 7 my $std_margin = 40;
27  
28 # Position control.
29 7 my $xd = 0; # step (in bar lines)
30 7 my $yd = 0; # vertical space between lines
31 7 my $md = 0; # additional left margin
32 7 my $x = 0; # actual x pos
33 7 my $y = 0; # actual y pos
34  
35 7 my $barno;
36 7 my $std_gridscale = 8;
37 7 my $title;
38 my $fh; # singleton
39  
40 # New page, and init the backend if needed.
41 4 ▶sub setup { # API
Subroutine setup: called CC=2 SCAR=6.9
Pod undocumented
42 4 my ( $self, $args ) = @_;
43 4 $fh = $self->{fh};
44 4 ▶ unless ( $self->{init}++ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"init"}++) 2 2 4
45 2 ps_preamble( $args->{preamble} );
46 2 $xd = $std_width;
47 2 $yd = $std_height;
48 2 $md = $std_margin;
49 }
50 }
51  
52 4 ▶sub setuppage { # API
Subroutine setuppage: called CC=1 SCAR=0.0
Pod undocumented
53 4 my ( $self, $title, $stitles ) = @_;
54 4 ps_page( 1, $title, $stitles );
55 4 undef $barno;
56 }
57  
58 2 ▶sub finish { # API
Subroutine finish: called CC=2 SCAR=7.0
Pod undocumented
59 2 my $self = shift;
60 2 ▶ return unless $self->{init};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"init"} 0 2 2
61 2 ps_trailer();
62 2 $self->{init} = 0;
63 }
64  
65 # New print line.
66 30 ▶sub setupline { # API
Subroutine setupline: called CC=2 SCAR=6.9
Pod undocumented
67 30 my ( $self, $line ) = @_;
68 30 $xd = $line->{width};
69 30 $yd = $line->{height};
70 30 ▶ $md = $line->{margin} || 0;
Condition100%2/2
$line->{'margin'} || 0
l!l
219
MC/DC100%1/1
$line->{'margin'} || 0
$line->{'margin'}
Truth table100%2/2
A
A= $line->{'margin'}
Aresult
11
00
71 30 $barno = $line->{barno};
72 }
73  
74 118 ▶sub chord { # API
Subroutine chord: called CC=4 SCAR=14.1
Pod undocumented
75 118 my ( $self, $chord, $dup ) = @_;
76 118 ▶ if ( ref($chord) =~ /::/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref($chord) =~ /::/) { } 111 7 118
elsif (ref $chord eq 'ARRAY') { } 0 7 7
77 111 ps_chord($chord);
78 111 $self->{_prev_chord} = $chord;
79 }
80 elsif ( ref($chord) eq 'ARRAY' ) {
81 0 my $fun = "print_" . shift(@$chord);
82 0 $self->$fun( @$chord );
83 }
84 else {
85 7 my $fun = "print_$chord";
86 7 $self->$fun;
87 }
88 118 ▶ while ( $dup-- > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dup-- > 1) 260 118 378
89 260 $self->print_space;
90 }
91 }
92  
93 0 ▶sub print_again {
Subroutine print_again: not called CC=1 SCAR=6.9
Pod undocumented
94 0 my ( $self ) = @_;
95 0 $self->chord( $self->{_prev_chord} );
96 }
97  
98 141 ▶sub bar { # API
Subroutine bar: called CC=4 SCAR=20.3
Pod undocumented
99 141 my ( $self, $first ) = @_;
100 141 ps_move();
101 141 ▶ if ( defined($barno) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $barno) { } 0 141 141
102 0 ▶ if ( $first ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first) { } 0 0 0
103 0 ▶ print { $self->{fh} } ( $barno > 0 ? ("($barno) barn\n") : ("bar\n") );
Branch: 1 branch
BranchTrueFalseTotal
$barno > 0 ? : 0 0 0
104 }
105 else {
106 0 print { $self->{fh} } ("bar\n");
107 0 $barno++;
108 }
109 }
110 else {
111 141 print { $self->{fh} } ("bar\n");
112 }
113 141 ps_skip(4);
114 }
115  
116 59 ▶sub newline { # API
Subroutine newline: called CC=1 SCAR=0.0
Pod undocumented
117 59 my ( $self, $count ) = @_;
118 59 ps_advance($count);
119 }
120  
121 260 ▶sub print_space {
Subroutine print_space: called CC=1 SCAR=0.0
Pod undocumented
122 260 ps_step();
123 }
124  
125 7 ▶sub print_rest {
Subroutine print_rest: called CC=1 SCAR=0.0
Pod undocumented
126 7 my $self = shift;
127 7 ps_move();
128 7 print { $self->{fh} } ("rest\n");
129 7 ps_step();
130 }
131  
132 0 ▶sub print_same {
Subroutine print_same: not called CC=1 SCAR=6.9
Pod undocumented
133 0 my ( $self, $wh, $xs ) = @_;
134 0 my $save_x = $x;
135 0 $x += ($xs * $xd) / 2;
136 0 ps_move();
137 0 print { $self->{fh} } ("same$wh\n"); # TODO: change to "same"
138 0 $x = $save_x;
139 0 ps_skip($xs * $xd);
140 }
141  
142 0 ▶sub print_ta {
Subroutine print_ta: not called CC=1 SCAR=6.9
Pod undocumented
143 0 my $self = shift;
144 0 ps_move();
145 0 print { $self->{fh} } ("ta\n");
146 0 ps_step();
147 }
148  
149 0 ▶sub postfix { # API
Subroutine postfix: not called CC=1 SCAR=6.9
Pod undocumented
150 0 my ( $self, $text ) = @_;
151 0 ps_skip(4);
152 0 $self->text( $text, $md );
153 }
154  
155 3 ▶sub text { # API
Subroutine text: called CC=3 SCAR=11.1
Pod undocumented
156 3 my ( $self, $text, $xxmd, $font ) = @_;
157 3 ▶ $font ||= 'SF';
Condition50%1/2
$font ||= "SF"
l!l
03
MC/DC0%0/1
$font ||= "SF"
$font
Truth table50%1/2
A
A= $font
Aresult
11
00
158 3 my $xm = $md;
159 3 ▶ $md = $xxmd || 0;
Condition50%1/2
$xxmd || 0
l!l
03
MC/DC0%0/1
$xxmd || 0
$xxmd
Truth table50%1/2
A
A= $xxmd
Aresult
11
00
160 3 ps_move();
161 3 print { $self->{fh} } ( $font, ' (', $text, ') show', "\n");
162 3 $md = $xm;
163 }
164  
165 0 ▶sub print_hmore {
Subroutine print_hmore: not called CC=1 SCAR=6.9
Pod undocumented
166 0 ps_skip(4);
167 }
168  
169 0 ▶sub print_less {
Subroutine print_less: not called CC=1 SCAR=6.9
Pod undocumented
170 0 ps_skip(-4);
171 }
172  
173 0 ▶sub grids { # API
Subroutine grids: not called CC=3 SCAR=24.8
Pod undocumented
174 0 my ( $self, $grids ) = @_;
175  
176 0 my $n = int( ( 570 - $md - 60 ) / 80 );
177  
178 0 my $i = 0;
179 0 foreach my $ch ( @$grids ) {
180 0 $self->print_grid($ch);
181 0 ▶ if ( ++$i >= $n ) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$i >= $n) { } 0 0 0
182 0 $self->newline(4);
183 0 $i = 0;
184 }
185 else {
186 0 ps_gridstep();
187 }
188 }
189 0 $self->newline(3);
190 }
191  
192 7 my @Rom = qw(I II III IV V VI VII VIII IX X XI XII);
193  
194 0 ▶sub print_grid {
Subroutine print_grid: not called CC=3 SCAR=24.8
Pod undocumented
195 0 my ( $self, $grid ) = @_;
196  
197 0 my @c = @$grid;
198 0 my $chord = shift(@c);
199 0 ▶ my $ps = ref($chord) ? $chord->ps : "($chord) show";
Branch: 1 branch
BranchTrueFalseTotal
ref $chord ? : 0 0 0
200 0 print { $self->{fh} } ('1000 1000 moveto', "\n",
201 $ps, "\n",
202 'currentpoint pop 1000 sub 2 div', "\n");
203 0 ps_move();
204 0 print { $self->{fh} } (2.5*$std_gridscale, ' exch sub 8 add 0 rmoveto ',
205 $ps, "\n");
206 0 ps_move();
207  
208 0 my $c = shift(@c);
209 0 ▶ if ( $c ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c) { } 0 0 0
210 0 $c = "($Rom[$c-1])"
211 }
212 else {
213 0 $c = '()';
214 }
215  
216 0 print { $self->{fh} } ('8 ', -5-(4*$std_gridscale), " rmoveto @c $c dots\n");
217 }
218  
219 ################ PostScript routines ################
220  
221 7 my $ps_pages = 0;
222 7 my $ps_page = 1; # first logical page
223 7 my $page_left; # left margin of page
224 my $page_right; # right margin of page
225 7 my $page_top; # top margin of page
226 7 my $page_bottom; # bottom margin of page;
227  
228 4 ▶sub ps_page {
Subroutine ps_page: called CC=6 SCAR=18.3
Pod undocumented
229 4 my ( $first, $ttle, $stitles ) = @_;
230 4 ▶ print $fh ('end showpage', "\n") if $ps_pages;
Branch: 1 branch
BranchTrueFalseTotal
if $ps_pages 2 2 4
231 4 print $fh ('%%Page: ', ++$ps_pages. ' ', $ps_pages, "\n",
232 'tabdict begin', "\n");
233 4 $x = $y = 0;
234 4 ▶ $ps_page = $first ? 1 : $ps_page+1;
Branch: 1 branch
BranchTrueFalseTotal
$first ? : 4 0 4
235  
236 4 print $fh ( "$page_left $page_top m TF ($ttle) show\n" );
237 4 ▶ $title = $ttle if $first;
Branch: 1 branch
BranchTrueFalseTotal
if $first 4 0 4
238  
239 4 ▶ if ( $ps_page > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ps_page > 1) 0 4 4
240 0 my $pp = "Page $ps_page";
241 0 print $fh ( "$page_right $page_top m SF ($pp) rshow\n" );
242 }
243 4 ps_advance();
244  
245 4 my $save_md = $md;
246 4 $md = 0;
247 4 foreach ( @$stitles ) {
248 2 ps_move();
249 2 print $fh ( "SF ($_) show\n" );
250 2 ps_advance();
251 }
252 4 $md = $save_md;
253 4 ps_advance(2);
254 }
255  
256 264 ▶sub ps_move {
Subroutine ps_move: called CC=2 SCAR=8.1
Pod undocumented
257  
258 264 ▶ if ( $page_top+$y < $page_bottom ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($page_top + $y < $page_bottom) 0 264 264
259 0 ps_page( 0, $title, [] );
260 }
261  
262 264 print $fh ($page_left+$x+$md, ' ' , $page_top+$y, ' m ');
263 }
264  
265 378 ▶sub ps_step { ps_skip($xd) }
Subroutine ps_step: called CC=1 SCAR=0.0
Pod undocumented
266  
267 0 ▶sub ps_gridstep { ps_skip(80) } # #### TODO: what width?
Subroutine ps_gridstep: not called CC=1 SCAR=6.9
Pod undocumented
268  
269 69 ▶sub ps_advance {
Subroutine ps_advance: called CC=2 SCAR=6.9
Pod undocumented
270 69 $x = 0;
271 69 $y += $yd;
272 69 ▶ $y += ($_[0]-1)*$yd if defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0] 7 62 69
273 }
274  
275 519 ▶sub ps_skip {
Subroutine ps_skip: called CC=1 SCAR=0.0
Pod undocumented
276 519 $x += $_[0];
277 }
278  
279 111 ▶sub ps_chord {
Subroutine ps_chord: called CC=1 SCAR=0.0
Pod undocumented
280 111 my ( $chord ) = @_;
281 111 ps_move();
282 111 print $fh ($chord->ps, "\n");
283 111 ps_step();
284 }
285  
286 2 ▶sub ps_preamble {
Subroutine ps_preamble: called CC=3 SCAR=12.0
Pod undocumented
287 2 my ( $preamble ) = @_;
288 2 my $data;
289 2 ▶ if ( defined $preamble ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $preamble) { } 0 2 2
290 0 ▶ open(DATA, $preamble) or die("$preamble: $!\n");
Branch: 1 branch
BranchTrueFalseTotal
open DATA, $preamble or die "${preamble}: $!\n" 0 0 0
291 0 local($/);
292 0 $data = <DATA>;
293 0 close(DATA);
294 }
295 else {
296 2 require App::Music::PlayTab::Output::PostScript::Preamble;
297 2 $data = App::Music::PlayTab::Output::PostScript::Preamble->preamble( $_[0] );
298 }
299 2 $data =~ s/\$std_gridscale/$std_gridscale/g;
300 2 print $fh ($data);
301  
302 # A4 format is 595 pt x 842 pt.
303 2 $page_left = 50;
304 2 $page_right = 550;
305 2 $page_top = 800;
306 2 $page_bottom = 50;
307  
308 2 $x = $y = 0;
309 2 $ps_pages = 0;
310 }
311  
312 2 ▶sub ps_trailer {
Subroutine ps_trailer: called CC=1 SCAR=0.0
Pod undocumented
313 2 print $fh ( <<EOD );
314 end showpage
315 %%Trailer
316 %%Pages: $ps_pages
317 %%EOF
318 EOD
319 }
320  
321 # PostScript support routines for App::Music::PlayTab::Note.
322  
323 package App::Music::PlayTab::Note;
324  
325 261 ▶sub ps {
Subroutine ps: called CC=3 SCAR=11.0
Pod undocumented
326 261 my $self = shift;
327 261 my $res = $self->name;
328 261 ▶ if ( $res =~ /(.)b/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($res =~ /(.)b/) { } 37 224 261
elsif ($res =~ /(.)#/) { } 25 199 224
329 37 $res = '('.$1.') root flat';
330 }
331 elsif ( $res =~ /(.)#/ ) {
332 25 $res = '('.$1.') root sharp';
333 }
334 else {
335 199 $res = '('.$res.') root';
336 }
337 261 $res;
338 }
339  
340 # PostScript support routines for App::Music::PlayTab::Chord.
341  
342 package App::Music::PlayTab::Chord;
343  
344 249 ▶sub ps {
Subroutine ps: called CC=21 SCAR=30.5
Pod undocumented
345 249 my ($self) = @_;
346 249 my $res = $self->{key}->ps;
347  
348 249 my @v = @{$self->{vec}};
349 249 my $v = "@v ";
350 249 shift (@v);
351  
352 249 ▶ if ( $v =~ s/^0 (2 )?4 (6|7|8) / / ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($v =~ s/^0 (2 )?4 (6|7|8) / /) { } 167 82 249
elsif ($v =~ s/^0 3 6 9 / /) { } 4 78 82
elsif ($v =~ s/^0 (2 )?3 (6|7|8) / /) { } 62 16 78
353 167 ▶ $res .= $2 == 8 ? ' plus' : '';
Branch: 1 branch
BranchTrueFalseTotal
$2 == 8 ? : 3 164 167
354 167 ▶ $v = ' 6' . $v if $2 == 6;
Branch: 1 branch
BranchTrueFalseTotal
if $2 == 6 0 167 167
355 167 ▶ $v = ' 2' . $v if defined $1;
Branch: 1 branch
BranchTrueFalseTotal
if defined $1 0 167 167
356 }
357 elsif ( $v =~ s/^0 3 6 9 / / ) {
358 4 $res .= ' dim';
359 }
360 elsif ( $v =~ s/^0 (2 )?3 (6|7|8) / / ) {
361 62 ▶ if ( $2 == 6 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($2 == 6) { } 8 54 62
362 8 ▶ $res .= ( $v =~ s/^ 10 // ) ? ' hdim' : ' dim';
Branch: 1 branch
BranchTrueFalseTotal
$v =~ s/^ 10 // ? : 7 1 8
363 }
364 else {
365 54 $res .= ' minus';
366 }
367 62 ▶ $v = ' 8' . $v if $2 == 8;
Branch: 1 branch
BranchTrueFalseTotal
if $2 == 8 0 62 62
368 62 ▶ $v = ' 2' . $v if defined $1;
Branch: 1 branch
BranchTrueFalseTotal
if defined $1 2 60 62
369 }
370 249 $v =~ s/^0 5 7 / 5 7 /;
371 249 $v =~ s/ 10 14 18 (21) / $1 /; # 13
372 249 $v =~ s/ 10 14 18 (20|22) / 10 $1 /; # 7#13 7b13
373 249 $v =~ s/ 10 14 (17) / $1 /; # 11
374 249 $v =~ s/ 10 14 (18) / 10 $1 /; # 7#11
375 249 $v =~ s/ 10 (14) / $1 /; # 9
376 249 $v =~ s/ 10 (15) / 10 $1 /; # 7#9
377 249 $v =~ s/ 11 14 18 (21|22) / $1 11 /; # 13#5
378 249 $v =~ s/ 11 14 (17|18) / $1 11 /; # 11#5
379 249 $v =~ s/ 11 (14|15) / $1 11 /; # 9#5
380 249 ▶ if ( $v =~ s/ 10 / / ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($v =~ s/ 10 / /) { } 31 218 249
elsif ($v =~ s/^( \d| 10|) 11 / $1/) { } 11 207 218
381 31 $res .= ' (7) addn';
382 }
383 elsif ( $v =~ s/^( \d| 10|) 11 / $1/ ) {
384 11 ▶ $res .= ' -2 0 rmoveto' if $res =~ / flat$/;
Branch: 1 branch
BranchTrueFalseTotal
if $res =~ / flat$/ 0 11 11
385 11 $res .= ' delta';
386 }
387 249 ▶ if ( $v =~ s/ 5 7 / / ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($v =~ s/ 5 7 / /) { } 6 243 249
elsif ($v =~ s/^0 7 / /) { } 6 237 243
elsif ($v =~ s/^0 4 / /) { } 4 233 237
388 6 $res .= ' (4) susp';
389 }
390 elsif ( $v =~ s/^0 7 / / ) {
391 6 $res .= ' (2) susp';
392 }
393 elsif ( $v =~ s/^0 4 / / ) {
394 4 $res .= ' (no5) addn';
395 }
396 249 my $res1 = $res; # for debug
397  
398 249 chop ($v);
399 249 $v =~ s/^ //;
400 249 @v = split(' ', $v);
401 249 foreach ( @v ) {
402 31 $res .= ' ';
403 31 $res .= ( '(1) addn', '(2) addf', '(2) addn', '(3) addf', '(3) addn',
404 '(4) addn', '(5) addf', '(5) addn', '(5) adds', '(6) addn',
405 '(7) addn', '(7) adds', '(8) addn', '(9) addf', '(9) addn',
406 '(9) adds','(11) addf','(11) addn','(11) adds',
407 '(12) addn','(13) addf','(13) addn' )[$_];
408 }
409  
410 249 ▶ if ( $self->{high} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"high"}) 2 247 249
411 2 my $t = join(" bslash ", map { $_->ps } @{$self->{high}});
412 2 $t =~ s/root/hroot/g;
413 2 $res = join(" bslash ", $res, $t);
414 }
415  
416 249 ▶ if ( $self->{bass} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"bass"}) 2 247 249
417 2 my $t = join(" slash ", map { $_->ps } @{$self->{bass}});
418 2 $t =~ s/root/hroot/g;
419 2 $res = join(" slash ", $res, $t);
420 }
421  
422 warn("=> Chord ", $self->{_unparsed}, ": ", $self->{key}->key,
423 0 " (", $self->{key}->name, ") [ @{$self->{vec}} ] ->",
424 " $res1 [ $v ] -> $res\n")
425 249 ▶ if $self->{_debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_debug"} 0 249 249
426  
427 249 return $res;
428 }
429  
430 7 1;
431  
432 __END__
« PlayTab/Chord.pm ↑ Summary PlayTab/Note.pm »