Writer/TableRowDecl.pm

filter: Statementstmt 82.6% 124 / 150 Branchbran 57.3% 47 / 82 Conditioncond 51.7% 29 / 56 MC/DCmcdc 23.3% 7 / 30 Subroutinesub 76.9% 10 / 13 Podpod 0.0% 0 / 8 totaltotal 64.0% 217 / 339 CC 68 SCAR 29.3
CC
68
Cov
69%
new
40.3
new_auto_for_rows
30.0
clone
20.0
CRAP 18.8
« Writer.pm ↑ Summary
1  
2 6 require 5;
3 package RTF::Writer::TableRowDecl;
4 6 ▶use strict; # Time-stamp: "2003-09-23 21:26:40 ADT"
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 6 ▶BEGIN {
Subroutine BEGIN: called CC=3 SCAR=13.1
8 6 ▶ if(defined &DEBUG) { } # nil
Branch: 2 branches
BranchTrueFalseTotal
if (defined &RTF::Writer::TableRowDecl::DEBUG) { } 0 6 6
elsif (defined &RTF::Writer::DEBUG) { } 6 0 6
9 6 elsif(defined &RTF::Writer::DEBUG) { *DEBUG = \&RTF::Writer::DEBUG }
10 0 else { *DEBUG = sub(){0} }
11 }
12 #--------------------------------------------------------------------------
13  
14 6 ▶use vars qw($DEFAULT_BORDER_WIDTH %Directions %Align_Directions);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 6 ▶$DEFAULT_BORDER_WIDTH ||= 15;
Condition50%1/2
$RTF::Writer::TableRowDecl::DEFAULT_BORDER_WIDTH ||= 15
l!l
06
MC/DC0%0/1
$RTF::Writer::TableRowDecl::DEFAULT_BORDER_WIDTH ||= 15
$RTF::Writer::TableRowDecl::DEFAULT_BORDER_WIDTH
Truth table50%1/2
A
A= $RTF::Writer::TableRowDecl::DEFAULT_BORDER_WIDTH
Aresult
11
00
16  
17 6 ▶unless(keys %Directions) {
Branch: 1 branch
BranchTrueFalseTotal
unless (keys %RTF::Writer::TableRowDecl::Directions) 6 0 6
18 6 @Directions{qw(N S E W)} = (0 .. 3);
19 6 @Directions{qw(n s e w)} = (0 .. 3);
20 6 @Directions{qw(T B R L)} = (0 .. 3);
21 6 @Directions{qw(t b r l)} = (0 .. 3);
22 }
23 # N S E W
24 6 my(@tabledirs) = qw( t b r l );
25  
26 6 ▶unless(keys %Align_Directions) { for my $d (\%Align_Directions) {
Branch: 1 branch
BranchTrueFalseTotal
unless (keys %RTF::Writer::TableRowDecl::Align_Directions) 6 0 6
27 # First char is vertical, second is horiz
28  
29 6 @$d{qw(NW N NE)} = qw(tl tc tr);
30 6 @$d{qw(W C E)} = qw(cl cc cr);
31 6 @$d{qw(SW S SE)} = qw(bl bc br);
32  
33 6 @$d{qw(WN EN)} = qw(tl tr);
34 6 @$d{qw(WS ES)} = qw(bl br);
35  
36 6 @$d{qw(TL T TR)} = qw(tl tc tr);
37 6 @$d{qw(L C R)} = qw(cl cc cr);
38 6 @$d{qw(BL B BR)} = qw(bl bc br);
39  
40 6 @$d{qw(LT RT)} = qw(tl tr);
41 6 @$d{qw(LB RB)} = qw(bl br);
42  
43 6 @$d{map lc($_), keys %$d}
44 = values %$d;
45 }}
46  
47 #--------------------------------------------------------------------------
48 # INSIDES:
49 # 0: the right-ends table
50 # 1: the left-margin setting
51 # 2: the inbetween setting
52 # 3: a list of border settings
53 # 4: a list of valign settings
54 # 5: a list of halign settings
55 # 6: the cached decl string
56  
57 5 ▶sub new {
Subroutine new: called CC=14 SCAR=37.0
Pod undocumented
58 5 my($it, %h) = @_;
59 5 my $new;
60  
61 my(@reaches);
62 5 ▶ if(ref $it) { # clone
Branch: 1 branch
BranchTrueFalseTotal
if (ref $it) { } 0 5 5
63 0 $new = $it->clone();
64 } else {
65 $new = bless [
66 \@reaches,
67 int( $h{'left_start'}||0 ) || 0,
68 5 ▶ int( $h{'inbetween' }||0 ) || 120, # 6 points, 1/12th-inch, about 2mm
Condition50%4/8
int($h{'left_start'} || 0) || 0
l!l
05
$h{'left_start'} || 0
l!l
05
int($h{'inbetween'} || 0) || 120
l!l
05
$h{'inbetween'} || 0
l!l
05
MC/DC0%0/1
int($h{'left_start'} || 0) || 0
$h{'left_start'}
MC/DC0%0/1
int($h{'inbetween'} || 0) || 120
$h{'inbetween'}
Truth table50%1/2
A
A= $h{'left_start'}
Aresult
11
00
Truth table50%1/2
A
A= $h{'inbetween'}
Aresult
11
00
69 ];
70 }
71
72 5 my $x; # scratch
73 5 ▶ if($x = $h{'widths'}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($x = $h{'widths'}) { } 0 5 5
elsif ($x = $h{'reaches'}) { } 0 5 5
74 0 ▶ Carp::croak("'widths' value has to be an arrayref")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $x eq "ARRAY" 0 0 0
75 unless ref($x) eq 'ARRAY';
76 0 my $start = $new->[1];
77 0 foreach my $w (map int($_), @$x) {
78 0 ▶ push @reaches, ($start += ($w < 1 ) ? 1 : $w);
Branch: 1 branch
BranchTrueFalseTotal
$w < 1 ? : 0 0 0
79 }
80 } elsif($x = $h{'reaches'}) {
81 Carp::croak("'reaches' value has to be an arrayref")
82 0 ▶ unless ref($h{'reaches'}) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $h{"reaches"} eq "ARRAY" 0 0 0
83 0 @reaches = sort {$a <=> $b} map int($_), @$x;
84 }
85  
86 $new->make_border_decl(
87 5 ▶ defined($h{'borders'}) ? $h{'borders'} : $h{'border'}
Branch: 1 branch
BranchTrueFalseTotal
defined $h{'borders'} ? : 2 3 5
88 );
89 $new->make_alignment_decl(
90 5 ▶ defined($h{'align'}) ? $h{'align'} : $h{'alignment'}
Branch: 1 branch
BranchTrueFalseTotal
defined $h{'align'} ? : 4 1 5
91 );
92 5 return $new;
93 }
94  
95 #--------------------------------------------------------------------------
96  
97 0 ▶sub clone {
Subroutine clone: not called CC=4 SCAR=30.0
Pod undocumented
98 # sufficient to our task, I think
99 bless [ map {;
100 0 ▶ (!defined $_) ? undef
Branch: 3 branches
BranchTrueFalseTotal
!defined($_) ? : 0 0 0
ref $_ eq 'ARRAY' ? : 0 0 0
ref $_ eq 'HASH' ? : 0 0 0
101 : (ref($_) eq 'ARRAY') ? [@$_]
102 : (ref($_) eq 'HASH' ) ? {%$_}
103 : $_
104 0 } @{$_[0]}
105 ],
106 ref $_[0];
107 }
108  
109 #--------------------------------------------------------------------------
110  
111 5 ▶sub make_border_decl {
Subroutine make_border_decl: called CC=8 SCAR=21.7
Pod undocumented
112 5 my($it, @params) = @_;
113 5 my @borders;
114  
115 5 $it->[3] = \@borders;
116  
117 5 ▶ unless( @params and grep defined($_), @params ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@params and grep defined($_), @params) 3 2 5
Condition66%2/3
@params and grep defined($_), @params
!ll&&!rl&&r
032
MC/DC50%1/2
@params and grep defined($_), @params
@params grep defined($_), @params
Truth table66%2/3
A and B
A= @paramsB= grep defined($_), @params
ABresult
0X0
100
111
118 3 @params = ('1');
119 }
120  
121 0 ▶ @params = @{$params[0]} if @params == 1 and ref $params[0];
Branch: 1 branch
BranchTrueFalseTotal
if @params == 1 and ref $params[0] 0 5 5
Condition33%1/3
@params == 1 and ref $params[0]
!ll&&!rl&&r
050
MC/DC0%0/2
@params == 1 and ref $params[0]
@params == 1 ref $params[0]
Truth table33%1/3
A and B
A= @params == 1B= ref $params[0]
ABresult
0X0
100
111
122 # I.e., if they passed border => [...]
123 5 ▶ @params = "all-$DEFAULT_BORDER_WIDTH-s"
Branch: 1 branch
BranchTrueFalseTotal
if @params == 1 and $params[0] eq 1 3 2 5
Condition66%2/3
@params == 1 and $params[0] eq 1
!ll&&!rl&&r
023
MC/DC50%1/2
@params == 1 and $params[0] eq 1
@params == 1 $params[0] eq 1
Truth table66%2/3
A and B
A= @params == 1B= $params[0] eq 1
ABresult
0X0
100
111
124 if @params == 1 and $params[0] eq '1';
125 # if they passed just border => 1
126
127 5 foreach my $spec (@params) {
128 5 push @borders, $it->_borderspec2bordercode($spec);
129 }
130
131 5 return;
132 }
133 #--------------------------------------------------------------------------
134  
135 5 ▶sub make_alignment_decl {
Subroutine make_alignment_decl: called CC=10 SCAR=23.7
Pod undocumented
136 5 my($it,@alignments) = @_;
137 5 my(@valign, @halign);
138 5 $it->[4] = \@valign;
139 5 $it->[5] = \@halign;
140  
141 5 ▶ unless(@alignments and grep defined($_), @alignments) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@alignments and grep defined($_), @alignments) 1 4 5
Condition66%2/3
@alignments and grep defined($_), @alignments
!ll&&!rl&&r
014
MC/DC50%1/2
@alignments and grep defined($_), @alignments
@alignments grep defined($_), @alignments
Truth table66%2/3
A and B
A= @alignmentsB= grep defined($_), @alignments
ABresult
0X0
100
111
142 # most common case: nothing
143 1 push @valign, '';
144 1 push @halign, '';
145 1 return;
146 }
147
148 4 ▶ if( @alignments != 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@alignments != 1) { } 0 4 4
elsif (ref $alignments[0]) { } 4 0 4
149 # Pass thru (altho normally impossible)
150 } elsif( ref $alignments[0] ) {
151 4 @alignments = @{$alignments[0]}
152 # I.e., they passed align => [...]
153 } else {
154 0 @alignments = grep length($_), split m/(?:\s*,\s*)|\s+/, $alignments[0];
155 # I.e., they passed in align => 'sw c c t' or 'sw, c, t' or whatever.
156 }
157
158 4 my($x, $v, $h);
159 4 foreach my $spec (@alignments) {
160 16 ▶ unless(defined $spec and length $spec) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $spec and length $spec) 1 15 16
Condition66%2/3
defined $spec and length $spec
!ll&&!rl&&r
0115
MC/DC50%1/2
defined $spec and length $spec
defined $spec length $spec
Truth table66%2/3
A and B
A= defined $specB= length $spec
ABresult
0X0
100
111
161 1 push @valign, '';
162 1 push @halign, '';
163 1 DEBUG and printf " - => valign - halign -\n";
164 1 next;
165 }
166 15 $x = $Align_Directions{$spec};
167 15 ▶ unless($x) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($x) 0 15 15
168 0 require Carp;
169 0 Carp::croak "Unintelligible alignment spec \"$spec\"";
170 }
171 15 ▶ die "WHAAAAA? [$x]" unless 2 == length $x; # sanity
Branch: 1 branch
BranchTrueFalseTotal
unless 2 == length $x 0 15 15
172 15 my($v,$h) = split '', $x;
173 15 push @valign, "\\clvertal$v";
174 15 push @halign, "\\q$h";
175 15 DEBUG and printf "% 2s => valign %s halign %s\n",
176 $spec, $valign[-1], $halign[-1];
177 }
178
179 4 return;
180 }
181  
182 #--------------------------------------------------------------------------
183 5 ▶sub _borderspec2bordercode {
Subroutine _borderspec2bordercode: called CC=14 SCAR=27.8
184  
185 5 my($it, $spec) = @_;
186  
187 5 ▶ $spec = 'all' unless defined $spec and length $spec;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $spec and length $spec 0 5 5
Condition33%1/3
defined $spec and length $spec
!ll&&!rl&&r
005
MC/DC0%0/2
defined $spec and length $spec
defined $spec length $spec
Truth table33%1/3
A and B
A= defined $specB= length $spec
ABresult
0X0
100
111
188 5 ▶ return '' if lc($spec) eq 'none';
Branch: 1 branch
BranchTrueFalseTotal
if lc $spec eq "none" 1 4 5
189
190 4 ▶ $spec = "all-$spec-s" if $spec =~ m/^\d+$/s;
Branch: 1 branch
BranchTrueFalseTotal
if $spec =~ /^\d+$/s 0 4 4
191  
192 4 my @widths = (undef, undef, undef, undef);
193 4 my @styles = (undef, undef, undef, undef);
194  
195 4 my($dir, $width, $style);
196 4 my @specs = split m/(?:,|\s+)/, $spec;
197
198 4 foreach my $it (@specs) {
199 5 ▶ next unless $it;
Branch: 1 branch
BranchTrueFalseTotal
unless $it 0 5 5
200  
201 5 ▶ unless( ($dir, $width, $style) = $it =~
Branch: 1 branch
BranchTrueFalseTotal
unless (() = $it =~ / ^\s* (all|[nsewNSEWtbrlTBRL]) (?:-(\d+))? (?:-([a-z]+))? \s* $ /sx) 0 5 5
202 m/
203 ^\s*
204 (all|[nsewNSEWtbrlTBRL])
205 (?:-(\d+))?
206 (?:-([a-z]+))?
207 \s*
208 $
209 /xs
210 ) {
211 0 require Carp;
212 0 Carp::croak "Unintelligible cell-border spec \"$spec\"";
213 }
214
215 5 ▶ $width = $DEFAULT_BORDER_WIDTH unless defined $width and length $width;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $width and length $width 0 5 5
Condition33%1/3
defined $width and length $width
!ll&&!rl&&r
005
MC/DC0%0/2
defined $width and length $width
defined $width length $width
Truth table33%1/3
A and B
A= defined $widthB= length $width
ABresult
0X0
100
111
216  
217 #print " $it => [$dir] [$width] [$style]\n";
218  
219 5 ▶ $style ||= 's';
Condition100%2/2
$style ||= "s"
l!l
41
MC/DC100%1/1
$style ||= "s"
$style
Truth table100%2/2
A
A= $style
Aresult
11
00
220
221 5 ▶ if($dir eq 'all') {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir eq 'all') { } 3 2 5
222 3 @widths = ($width) x 4;
223 3 @styles = ($style) x 4;
224 } else {
225 2 $dir = $Directions{$dir};
226 2 $widths[$dir] = $width;
227 2 $styles[$dir] = $style;
228 }
229 }
230  
231 4 my @out;
232 4 foreach my $i (0 .. 3) {
233 16 ▶ next unless $styles[$i];
Branch: 1 branch
BranchTrueFalseTotal
unless $styles[$i] 2 14 16
234 14 push @out, sprintf '\clbrdr%s\brdrw%s\brdr%s',
235 $tabledirs[$i],
236 $widths[$i],
237 $styles[$i],
238 ;
239 }
240 4 return join "\n", @out;
241 }
242  
243 #--------------------------------------------------------------------------
244  
245 0 ▶sub new_auto_for_rows {
Subroutine new_auto_for_rows: not called CC=5 SCAR=34.0
Pod undocumented
246 0 my $class = shift;
247 0 my $max_cols = 1;
248 0 foreach my $r (@_) {
249 0 ▶ next unless defined $r and ref $r eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $r and ref $r eq "ARRAY" 0 0 0
Condition0%0/3
defined $r and ref $r eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $r and ref $r eq "ARRAY"
defined $r ref $r eq "ARRAY"
Truth table0%0/3
A and B
A= defined $rB= ref $r eq "ARRAY"
ABresult
0X0
100
111
250 0 ▶ $max_cols = @$r if @$r > $max_cols;
Branch: 1 branch
BranchTrueFalseTotal
if @$r > $max_cols 0 0 0
251 }
252 return
253 0 $class->new( 'width' => [ ((6.5 * 1440) / $max_cols) x scalar(@_) ] );
254 }
255  
256 #--------------------------------------------------------------------------
257  
258 0 ▶sub row_count { return scalar @{ $_[0][0] } }
Subroutine row_count: not called CC=1 SCAR=6.9
Pod undocumented
259 # How many rows we were declared to handle
260  
261 #--------------------------------------------------------------------------
262  
263 5 ▶sub decl_code {
Subroutine decl_code: called CC=16 SCAR=28.8
Pod undocumented
264 5 my $it = shift;
265 5 ▶ return $it->[6] if defined $it->[6];
Branch: 1 branch
BranchTrueFalseTotal
if defined $it->[6] 1 4 5
266
267 4 my $reaches = $it->[0];
268 4 ▶ my $cell_count = int($_[0] || 0) || scalar @$reaches;
Condition50%2/4
int($_[0] || 0) || scalar @$reaches
l!l
40
$_[0] || 0
l!l
40
MC/DC0%0/1
int($_[0] || 0) || scalar @$reaches
$_[0]
Truth table50%1/2
A
A= $_[0]
Aresult
11
00
269
270 4 ▶ if($cell_count > @$reaches) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cell_count > @$reaches) 4 0 4
271 # Uncommon case -- we need to ad-hoc pad this decl.
272 4 $reaches = [@$reaches]; # so we won't mutate the original
273 4 ▶ while(@$reaches < $cell_count) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$reaches < $cell_count) 16 4 20
274 16 ▶ if(@$reaches == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (@$reaches == 0) { } 4 12 16
elsif (@$reaches == 1) { } 4 8 12
275 4 push @$reaches, $it->[1] + 1440;
276 # sane and noticeable default width, I think: 1 inch, 2.54cm
277 } elsif(@$reaches == 1) {
278 4 push @$reaches, 2 * $reaches->[0] - $it->[1];
279 # The left-margin setting
280 } else {
281 8 push @$reaches, 2 * $reaches->[-1] - $reaches->[-2];
282 # i.e., last + (last - one_before)
283 # DEBUG and printf "Improvised the width %d based on %d,%d\n",
284 # $reaches->[-1], $reaches->[-3], $reaches->[-2];
285 }
286 }
287 }
288 4 ▶ my @borders = @{ $it->[3] || [] };
Condition50%1/2
$it->[3] || []
l!l
40
MC/DC0%0/1
$it->[3] || []
$it->[3]
Truth table50%1/2
A
A= $it->[3]
Aresult
11
00
289 4 ▶ push @borders, ($borders[-1]) x ($cell_count - @borders)
Branch: 1 branch
BranchTrueFalseTotal
if @borders > 0 and @borders < $cell_count 4 0 4
Condition33%1/3
@borders > 0 and @borders < $cell_count
!ll&&!rl&&r
004
MC/DC0%0/2
@borders > 0 and @borders < $cell_count
@borders > 0 @borders < $cell_count
Truth table33%1/3
A and B
A= @borders > 0B= @borders < $cell_count
ABresult
0X0
100
111
290 if @borders > 0 and @borders < $cell_count;
291
292 4 ▶ my @valign = @{ $it->[4] || [] };
Condition50%1/2
$it->[4] || []
l!l
40
MC/DC0%0/1
$it->[4] || []
$it->[4]
Truth table50%1/2
A
A= $it->[4]
Aresult
11
00
293 4 ▶ push @valign , ($valign[-1] ) x ($cell_count - @valign )
Branch: 1 branch
BranchTrueFalseTotal
if @valign > 0 and @valign < $cell_count 1 3 4
Condition66%2/3
@valign > 0 and @valign < $cell_count
!ll&&!rl&&r
031
MC/DC50%1/2
@valign > 0 and @valign < $cell_count
@valign > 0 @valign < $cell_count
Truth table66%2/3
A and B
A= @valign > 0B= @valign < $cell_count
ABresult
0X0
100
111
294 if @valign > 0 and @valign < $cell_count;
295 # Or should I have it default to a lack of any alignment code?
296
297 4 ▶ $it->[6] = \join '',
Condition75%3/4
shift @borders || ''
l!l
160
shift @valign || ''
l!l
124
MC/DC0%0/1
shift @borders || ''
shift @borders
MC/DC100%1/1
shift @valign || ''
shift @valign
Truth table50%1/2
A
A= shift @borders
Aresult
11
00
Truth table100%2/2
A
A= shift @valign
Aresult
11
00
298 # Cache it for next time (and there usually are many next-times)
299 sprintf("\\trowd\\trleft%d\\trgaph%d\n", $it->[1], int($it->[2] / 2) ),
300 map(
301 sprintf("%s%s\\cellx%d\n",
302 (shift(@borders) ||''),
303 (shift(@valign ) ||''),
304 $_,
305 ),
306 @$reaches
307 ),
308 ;
309 4 DEBUG and print "Init code:\n", ${ $it->[6] }, "\n\n";
310 4 return $it->[6];
311 }
312  
313 #--------------------------------------------------------------------------
314 5 ▶sub cell_content_init {
Subroutine cell_content_init: called CC=2 SCAR=7.2
Pod undocumented
315 5 ▶ return @{ $_[0][5] || [] };
Condition50%1/2
$_[0][5] || []
l!l
50
MC/DC0%0/1
$_[0][5] || []
$_[0][5]
Truth table50%1/2
A
A= $_[0][5]
Aresult
11
00
316 }
317  
318 #--------------------------------------------------------------------------
319 6 1;
320  
321 __END__
« Writer.pm ↑ Summary