lib/ChordPro/Delegate/TextBlock.pm

filter: Statementstmt 20.3% 21 / 103 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 46 MC/DCmcdc 0.0% 0 / 25 Subroutinesub 63.6% 7 / 11 Podpod 0.0% 0 / 4 totaltotal 12.1% 28 / 231 CC 43 SCAR 72.7
CC
43
Cov
11%
txt2xform
1806.0
txt2html
6.0
DEBUG
2.0
CRAP 1431.6
« lib/ChordPro/Song.pm ↑ Summary blib/lib/ChordPro/Output/PDF/Grid.pm »
1 #! perl
2  
3 2 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use feature qw( signatures );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶no warnings "experimental::signatures";
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 package ChordPro::Delegate::TextBlock;
11  
12 # Combine one or more text lines into a single xforms object.
13 #
14 # Attributes:
15 #
16 # width: Width of the resultant object.
17 # Defaults to the actual width (tight fit) of the texts.
18 # height: Height of the resultant object.
19 # Defaults to the actual height of the text, including
20 # the advance of the last line (non-tight fit).
21 # When height or padding is set, a tight fit is used.
22 # padding: Provide padding between the object and the inner text.
23 # When height or padding is set, a tight fit is used.
24 # flush: Horizontal text flush (left, center, right).
25 # vflush: Vertical text flush (top, middle, bottom).
26 # textstyle: Style (font) to be used. Must be one of "text", "chords",
27 # "comment" etc.
28 # textsize: Initial value for the text size.
29 # textspacing: Text spacing. A factor (e.g. 1.2) or "flex".
30 # textcolor: Initial color for the text.
31 # background: Background color of the object.
32 #
33 # Common attributes:
34 #
35 # id: Make asset instead of image.
36 # align: Image alignment (left, center, right)
37 # border: Draw border around the image.
38  
39 2 ▶use ChordPro::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 0 ▶sub DEBUG() { $::config->{debug}->{txtblk} }
Subroutine DEBUG: not called CC=1 SCAR=6.9
Pod undocumented
42  
43 0 ▶sub txt2xform( $self, %args ) {
Subroutine txt2xform: not called CC=42 SCAR=75.0
Pod undocumented
44 0 my $elt = $args{elt};
45  
46 0 my $ps = $self->{_ps};
47 0 my $pr = $ps->{pr};
48 0 my $opts = { %{$elt->{opts}} };
49  
50 # Text style must be one of the known styles (text, chord, comment, ...).
51 0 ▶ my $style = delete($opts->{textstyle}) // "text";
Condition0%0/2
delete $opts->{"textstyle"} // "text"
l!l
00
MC/DC0%0/1
delete $opts->{"textstyle"} // "text"
delete $opts->{"textstyle"}
Truth table0%0/2
A
A= delete $opts->{"textstyle"}
Aresult
11
00
52 0 ▶ unless ( defined($ps->{fonts}->{$style} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ps->{"fonts"}{$style}) 0 0 0
53 0 warn("TextBlock: Unknown style \"$style\", using \"text\"\n");
54 0 $style = "text";
55 }
56 0 my $font = $ps->{fonts}->{$style};
57 0 my $bgcol = $pr->_bgcolor($font->{background});
58 0 ▶ $bgcol = "" if $bgcol eq "none";
Branch: 1 branch
BranchTrueFalseTotal
if $bgcol eq "none" 0 0 0
59 0 ▶ my $vsp = delete($opts->{textspacing}) // "flex";
Condition0%0/2
delete $opts->{"textspacing"} // "flex"
l!l
00
MC/DC0%0/1
delete $opts->{"textspacing"} // "flex"
delete $opts->{"textspacing"}
Truth table0%0/2
A
A= delete $opts->{"textspacing"}
Aresult
11
00
60 my $sp = $vsp eq "flex"
61 0 ▶ ? ($font->{leading} || $ps->{spacing}->{$style} || 1) : $vsp;
Branch: 1 branch
BranchTrueFalseTotal
$vsp eq 'flex' ? : 0 0 0
Condition0%0/5
$font->{"leading"} or $ps->{"spacing"}{$style} or 1
l!l
00
$font->{"leading"} or $ps->{"spacing"}{$style}
l!l&&r!l&&!r
000
MC/DC0%0/2
$font->{"leading"} or $ps->{"spacing"}{$style} or 1
$font->{"leading"} $ps->{"spacing"}{$style}
Truth table0%0/3
A or B
A= $font->{"leading"}B= $ps->{"spacing"}{$style}
ABresult
1X1
011
000
62 0 my $size = fontsize( delete($opts->{textsize}), $font->{size} );
63 0 ▶ my $color = delete($opts->{textcolor}) // $font->{color};
Condition0%0/3
delete $opts->{"textcolor"} // $font->{"color"}
l!l&&r!l&&!r
000
MC/DC0%0/2
delete $opts->{"textcolor"} // $font->{"color"}
delete $opts->{"textcolor"} $font->{"color"}
Truth table0%0/3
A // B
A= delete $opts->{"textcolor"}B= $font->{"color"}
ABresult
1X1
011
000
64 0 ▶ my $flush = delete($opts->{flush}) // "left";
Condition0%0/2
delete $opts->{"flush"} // "left"
l!l
00
MC/DC0%0/1
delete $opts->{"flush"} // "left"
delete $opts->{"flush"}
Truth table0%0/2
A
A= delete $opts->{"flush"}
Aresult
11
00
65 0 ▶ my $vflush = delete($opts->{vflush}) // "top";
Condition0%0/2
delete $opts->{"vflush"} // "top"
l!l
00
MC/DC0%0/1
delete $opts->{"vflush"} // "top"
delete $opts->{"vflush"}
Truth table0%0/2
A
A= delete $opts->{"vflush"}
Aresult
11
00
66 0 ▶ $color = $ps->{pr}->_fgcolor($color) if $color;
Branch: 1 branch
BranchTrueFalseTotal
if $color 0 0 0
67  
68 0 my $data = $elt->{data};
69 0 ▶ if ( $color || $bgcol ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($color or $bgcol) 0 0 0
Condition0%0/3
$color or $bgcol
l!l&&r!l&&!r
000
MC/DC0%0/2
$color or $bgcol
$color $bgcol
Truth table0%0/3
A or B
A= $colorB= $bgcol
ABresult
1X1
011
000
70 0 my $span = "";
71 0 ▶ $span .= " color='$color'" if $color;
Branch: 1 branch
BranchTrueFalseTotal
if $color 0 0 0
72 0 ▶ $span .= " background='$bgcol'" if $bgcol;
Branch: 1 branch
BranchTrueFalseTotal
if $bgcol 0 0 0
73 0 $data = [ map { "<span$span>$_</span>" } @$data ];
74 }
75 0 my $padding = delete($opts->{padding});
76  
77 # New xo.
78 0 my $xo = $pr->{pdf}->xo_form;
79  
80 # Pre-pass to establish the actual width/height.
81 0 my ( $awidth, $aheight ) = ( 0, undef );
82 0 my ( $w, $h );
83 0 for ( @$data ) {
84 0 ( $w, $h ) = $pr->strwidth( $_, $font, $size );
85 0 ▶ $awidth = $w if $w > $awidth;
Branch: 1 branch
BranchTrueFalseTotal
if $w > $awidth 0 0 0
86 0 ▶ if ( defined($aheight) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $aheight) { } 0 0 0
87 0 ▶ $aheight += $vsp eq "flex" ? ($h||$size)*$sp : $size*$vsp;
Branch: 1 branch
BranchTrueFalseTotal
$vsp eq 'flex' ? : 0 0 0
Condition0%0/3
$h || $size
l!l&&r!l&&!r
000
MC/DC0%0/2
$h || $size
$h $size
Truth table0%0/3
A || B
A= $hB= $size
ABresult
1X1
011
000
88 }
89 else {
90 0 ▶ $aheight = ($h||$size);
Condition0%0/3
$h || $size
l!l&&r!l&&!r
000
MC/DC0%0/2
$h || $size
$h $size
Truth table0%0/3
A || B
A= $hB= $size
ABresult
1X1
011
000
91 }
92 }
93  
94 # Desired width (includes padding).
95 0 my ( $width, $height );
96 0 ▶ if ( $width = delete($opts->{width}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($width = delete $opts->{'width'}) { } 0 0 0
97 # Note that using dimension is not yet operational.
98 0 ▶ $width = dimension( $width, width => $size ) - 2*($padding||0);
Condition0%0/2
$padding || 0
l!l
00
MC/DC0%0/1
$padding || 0
$padding
Truth table0%0/2
A
A= $padding
Aresult
11
00
99 }
100 else {
101 0 $width = $awidth;
102 }
103  
104 # Correction for tight y-fit.
105 0 my $ycorr = 0;#($vsp eq "flex" ? $h||$size : $size) * ($sp - 1);
106  
107 # Desired height (includes padding).
108 0 ▶ if ( $height = delete($opts->{height}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($height = delete $opts->{'height'}) { } 0 0 0
109 # Note that using dimension is not yet operational.
110 0 ▶ $height = dimension( $height, width => $size ) - 2*($padding||0);
Condition0%0/2
$padding || 0
l!l
00
MC/DC0%0/1
$padding || 0
$padding
Truth table0%0/2
A
A= $padding
Aresult
11
00
111 }
112 else {
113 0 $height = $aheight - $ycorr;
114 0 ▶ $ycorr = 0 unless defined($padding);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $padding 0 0 0
115 }
116 # Width and height are now the 'inner' box (w/o padding).
117  
118 # With padding, we cancel the leading after the last line.
119 0 ▶ if ( defined $padding ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $padding) { } 0 0 0
120 0 $ycorr = 0;
121 }
122 else {
123 0 $padding = 0;
124 }
125 0 ▶ if ( $::config->{debug}->{txtblk} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main::config->{"debug"}{"txtblk"}) 0 0 0
126 0 warn("tb: w = $width, h = $height, p = $padding, sp = $sp, c = $ycorr\n");
127 }
128 # Note that the padding will be dealt with in the bbox.
129  
130 # Draw background.
131 0 $xo->bbox( -$padding, -$padding, $width+$padding, $height+$padding );
132 0 ▶ if ( my $bg = delete($opts->{background}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $bg = delete $opts->{"background"}) 0 0 0
133 0 $xo->rectangle( $xo->bbox );
134 0 $xo->fill_color($bg);
135 0 $xo->fill;
136 }
137  
138 # Put it in text mode.
139 0 $xo->textstart;
140  
141 0 my $y = $height - $ycorr;
142  
143 0 ▶ if ( $flush eq "right" || $flush eq "center"
Branch: 1 branch
BranchTrueFalseTotal
if ($flush eq 'right' or $flush eq 'center' or $vflush eq 'middle' or $vflush eq 'bottom') { } 0 0 0
Condition0%0/9
$flush eq "right" or $flush eq "center" or $vflush eq "middle" or $vflush eq "bottom"
l!l&&r!l&&!r
000
$flush eq "right" or $flush eq "center" or $vflush eq "middle"
l!l&&r!l&&!r
000
$flush eq "right" or $flush eq "center"
l!l&&r!l&&!r
000
MC/DC0%0/4
$flush eq "right" or $flush eq "center" or $vflush eq "middle" or $vflush eq "bottom"
$flush eq "right" $flush eq "center" $vflush eq "middle" $vflush eq "bottom"
Truth table0%0/5
A or B or C or D
A= $flush eq "right"B= $flush eq "center"C= $vflush eq "middle"D= $vflush eq "bottom"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
144 || $vflush eq "middle" || $vflush eq "bottom" ) {
145  
146 0 ▶ if ( $vflush eq "middle" ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($vflush eq 'middle') { } 0 0 0
elsif ($vflush eq 'bottom') { } 0 0 0
147 0 $y += ($aheight-$height)/2;
148 }
149 elsif ( $vflush eq "bottom" ) {
150 0 $y += $aheight - $height;
151 }
152  
153 0 for ( @$data ) {
154 0 ▶ my $h = $pr->strheight( $_, $font, $size ) || $size;
Condition0%0/3
$pr->strheight($_, $font, $size) || $size
l!l&&r!l&&!r
000
MC/DC0%0/2
$pr->strheight($_, $font, $size) || $size
$pr->strheight($_, $font, $size) $size
Truth table0%0/3
A || B
A= $pr->strheight($_, $font, $size)B= $size
ABresult
1X1
011
000
155 0 $pr->{tmplayout}->set_width($width);
156 0 $pr->{tmplayout}->set_alignment($flush);
157 0 $pr->{tmplayout}->show( 0, $y, $xo );
158 0 ▶ $y -= ($vsp eq "flex" ? $h : $size) * $sp;
Branch: 1 branch
BranchTrueFalseTotal
$vsp eq 'flex' ? : 0 0 0
159 }
160 }
161 else { # assume top/left
162 0 for ( @$data ) {
163 0 ▶ my $h = $pr->strheight( $_, $font, $size ) || $size;
Condition0%0/3
$pr->strheight($_, $font, $size) || $size
l!l&&r!l&&!r
000
MC/DC0%0/2
$pr->strheight($_, $font, $size) || $size
$pr->strheight($_, $font, $size) $size
Truth table0%0/3
A || B
A= $pr->strheight($_, $font, $size)B= $size
ABresult
1X1
011
000
164 0 $pr->{tmplayout}->set_alignment($flush);
165 0 $pr->{tmplayout}->show( 0, $y, $xo );
166 0 ▶ $y -= ($vsp eq "flex" ? $h : $size) * $sp;
Branch: 1 branch
BranchTrueFalseTotal
$vsp eq 'flex' ? : 0 0 0
167 }
168 }
169  
170 # Finish.
171 0 $xo->textend;
172  
173 return
174 { type => "image",
175 subtype => "xform",
176 line => $elt->{line},
177 0 data => $xo,
178 width => $width + 2*$padding,
179 height => $height + 2*$padding,
180 opts => { align => "left", %$opts },
181 };
182 }
183  
184 0 ▶sub txt2html( $self, %args ) {
Subroutine txt2html: not called CC=2 SCAR=17.9
Pod undocumented
185 0 my $elt = $args{elt};
186  
187 0 require ChordPro::Output::Common;
188 0 ▶ my @lines = map { ChordPro::Output::Common::encode_html($_) } @{ $elt->{data} // [] };
Condition0%0/2
$elt->{"data"} // []
l!l
00
MC/DC0%0/1
$elt->{"data"} // []
$elt->{"data"}
Truth table0%0/2
A
A= $elt->{"data"}
Aresult
11
00
189 0 return { type => "html",
190 data => [ join( "<br/>\n", @lines ) ]
191 };
192 }
193  
194 # Pre-scan.
195 0 ▶sub options( $data ) { {} }
Subroutine options: not called CC=1 SCAR=6.9
Pod undocumented
196  
197 2 1;
« lib/ChordPro/Song.pm ↑ Summary blib/lib/ChordPro/Output/PDF/Grid.pm »