blib/lib/PostScript/Simple/Table.pm

filter: Statementstmt 5.7% 16 / 280 Branchbran 0.0% 0 / 126 Conditioncond 0.0% 0 / 77 MC/DCmcdc 0.0% 0 / 42 Subroutinesub 45.4% 5 / 11 Podpod 50.0% 3 / 6 totaltotal 4.4% 24 / 542 CC 98 SCAR 76.1
CC
98
Cov
3%
table
3306.0
text_block
930.0
col_widths
72.0
CRAP 2009.3
↑ Summary
1 package PostScript::Simple::Table;
2  
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use PostScript::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use PostScript::Metrics;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 our $VERSION = '1.02';
10  
11 sub new
12 0 ▶{
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
13 0 my ($proto) = @_;
14
15 0 ▶ my $class = ref($proto) || $proto;
Condition0%0/3
ref $proto || $proto
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
16 0 my $self = {};
17 0 bless ($self, $class);
18 0 return $self;
19 }
20  
21 ############################################################
22 #
23 # text_block - utility method to build multi-paragraph blocks of text
24 #
25 # ($width_of_last_line, $ypos_of_last_line, $left_over_text) = text_block(
26 # $post_script_handler,
27 # $text_to_place,
28 # -x => $left_edge_of_block,
29 # -y => $baseline_of_first_line,
30 # -w => $width_of_block,
31 # -h => $height_of_block,
32 # [-lead => $font_size * 1.2 | $distance_between_lines,]
33 # [-parspace => 0 | $extra_distance_between_paragraphs,]
34 # [-align => "left|right|center|justify|fulljustify",]
35 # [-hang => $optional_hanging_indent,]
36 #);
37 #
38 ############################################################
39  
40 0 ▶sub text_block {
Subroutine text_block: not called CC=30 SCAR=68.4
Pod documented
41 0 my $self = shift;
42 0 my $ps = shift;
43 0 my $text = shift;
44 0 my %arg = @_;
45
46 0 my($align,$width,$ypos,$xpos,$line_width,$wordspace, $endw) = (undef,undef,undef,undef,undef,undef,undef,undef);
47 0 my @line = undef;
48 0 my %width = undef;
49 # Get the text in paragraphs
50 0 my @paragraphs = split(/\n/, $text);
51  
52 0 ▶ $arg{'-lead'} ||= 14;
Condition0%0/2
$arg{"-lead"} ||= 14
l!l
00
MC/DC0%0/1
$arg{"-lead"} ||= 14
$arg{"-lead"}
Truth table0%0/2
A
A= $arg{"-lead"}
Aresult
11
00
53
54 # calculate width of all words
55 0 my $space_width = $self->getStringWidth("\x20");
56 0 my @words = split(/\s+/, $text);
57  
58 0 foreach (@words) {
59 0 ▶ next if exists $width{$_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $width{$_} 0 0 0
60 0 $width{$_} = $self->getStringWidth($_);
61 }
62  
63 0 $ypos = $arg{'-y'};
64 0 my @paragraph = split(/ /, shift(@paragraphs));
65 0 my $first_line = 1;
66 0 my $first_paragraph = 1;
67  
68 # while we can add another line
69 0 ▶ while ( $ypos >= $arg{'-y'} - $arg{'-h'} + $arg{'-lead'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ypos >= $arg{"-y"} - $arg{"-h"} + $arg{"-lead"}) 0 0 0
70  
71 0 ▶ unless (@paragraph) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@paragraph) 0 0 0
72 0 ▶ last unless scalar @paragraphs;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @paragraphs 0 0 0
73 0 @paragraph = split(/ /, shift(@paragraphs));
74  
75 0 ▶ $ypos -= $arg{'-parspace'} if $arg{'-parspace'};
Branch: 1 branch
BranchTrueFalseTotal
if $arg{"-parspace"} 0 0 0
76 0 ▶ last unless $ypos >= $arg{'-y'} - $arg{'-h'};
Branch: 1 branch
BranchTrueFalseTotal
unless $ypos >= $arg{"-y"} - $arg{"-h"} 0 0 0
77 0 $first_line = 1;
78 0 $first_paragraph = 0;
79 }
80  
81 0 $xpos = $arg{'-x'};
82  
83 # while there's room on the line, add another word
84 0 @line = ();
85  
86 0 $line_width =0;
87 0 ▶ if ($first_line && exists $arg{'-hang'}) {
Branch: 4 branches
BranchTrueFalseTotal
if ($first_line and exists $arg{'-hang'}) { } 0 0 0
elsif ($first_line and exists $arg{'-flindent'}) { } 0 0 0
elsif ($first_paragraph and exists $arg{'-fpindent'}) { } 0 0 0
elsif (exists $arg{'-indent'}) { } 0 0 0
Condition0%0/3
$first_line and exists $arg{"-hang"}
!ll&&!rl&&r
000
MC/DC0%0/2
$first_line and exists $arg{"-hang"}
$first_line exists $arg{"-hang"}
Truth table0%0/3
A and B
A= $first_lineB= exists $arg{"-hang"}
ABresult
0X0
100
111
88 0 my $hang_width = $self->getStringWidth($arg{'-hang'});
89
90 0 $ps->text($xpos, $ypos, $arg{'-hang'});
91
92 0 $xpos += $hang_width;
93 0 $line_width += $hang_width;
94 0 ▶ $arg{'-indent'} += $hang_width if $first_paragraph;
Branch: 1 branch
BranchTrueFalseTotal
if $first_paragraph 0 0 0
95 ▶ } elsif ($first_line && exists $arg{'-flindent'}) {
Condition0%0/3
$first_line and exists $arg{"-flindent"}
!ll&&!rl&&r
000
MC/DC0%0/2
$first_line and exists $arg{"-flindent"}
$first_line exists $arg{"-flindent"}
Truth table0%0/3
A and B
A= $first_lineB= exists $arg{"-flindent"}
ABresult
0X0
100
111
96 0 $xpos += $arg{'-flindent'};
97 0 $line_width += $arg{'-flindent'};
98 ▶ } elsif ($first_paragraph && exists $arg{'-fpindent'}) {
Condition0%0/3
$first_paragraph and exists $arg{"-fpindent"}
!ll&&!rl&&r
000
MC/DC0%0/2
$first_paragraph and exists $arg{"-fpindent"}
$first_paragraph exists $arg{"-fpindent"}
Truth table0%0/3
A and B
A= $first_paragraphB= exists $arg{"-fpindent"}
ABresult
0X0
100
111
99 0 $xpos += $arg{'-fpindent'};
100 0 $line_width += $arg{'-fpindent'};
101 } elsif (exists $arg{'-indent'}) {
102 0 $xpos += $arg{'-indent'};
103 0 $line_width += $arg{'-indent'};
104 }
105
106 0 ▶ while ( @paragraph and $self->getStringWidth(join("\x20", @line)."\x20".$paragraph[0])+$line_width < $arg{'-w'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@paragraph and $self->getStringWidth(join(" ", @line) . ' ' . $paragraph[0]) + $line_width < $arg{"-w"}) 0 0 0
Condition0%0/3
@paragraph and $self->getStringWidth(join(" ", @line) . ' ' . $paragraph[0]) + $line_width < $arg{"-w"}
!ll&&!rl&&r
000
MC/DC0%0/2
@paragraph and $self->getStringWidth(join(" ", @line) . ' ' . $paragraph[0]) + $line_width < $arg{"-w"}
@paragraph $self->getStringWidth(join(" ", @line) . ' ' . $paragraph[0]) + $line_width < $arg{"-w"}
Truth table0%0/3
A and B
A= @paragraphB= $self->getStringWidth(join(" ", @line) . ' ' . $paragraph[0]) + $line_width < $arg{"-w"}
ABresult
0X0
100
111
107 0 push(@line, shift(@paragraph));
108 }
109 0 $line_width += $self->getStringWidth(join('', @line));
110
111 # calculate the space width
112 0 ▶ if ($arg{'-align'} eq 'fulljustify' or ($arg{'-align'} eq 'justify' and @paragraph)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg{'-align'} eq 'fulljustify' or $arg{'-align'} eq 'justify' and @paragraph) { } 0 0 0
Condition0%0/6
$arg{"-align"} eq "fulljustify" or $arg{"-align"} eq "justify" and @paragraph
l!l&&r!l&&!r
000
$arg{"-align"} eq "justify" and @paragraph
!ll&&!rl&&r
000
MC/DC0%0/3
$arg{"-align"} eq "fulljustify" or $arg{"-align"} eq "justify" and @paragraph
$arg{"-align"} eq "fulljustify" $arg{"-align"} eq "justify" @paragraph
Truth table0%0/4
A or B and C
A= $arg{"-align"} eq "fulljustify"B= $arg{"-align"} eq "justify"C= @paragraph
ABCresult
1XX1
0111
00X0
0100
113 0 ▶ if (scalar(@line) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @line == 1) 0 0 0
114 0 @line = split(//,$line[0]);
115 }
116 0 $wordspace = ($arg{'-w'} - $line_width) / (scalar(@line) - 1);
117 0 $align='justify';
118 } else {
119 0 ▶ $align=($arg{'-align'} eq 'justify') ? 'left' : $arg{'-align'};
Branch: 1 branch
BranchTrueFalseTotal
$arg{'-align'} eq 'justify' ? : 0 0 0
120 0 $wordspace = $space_width;
121 }
122 0 $line_width += $wordspace * (scalar(@line) - 1);
123  
124 0 ▶ if ($align eq 'justify') {
Branch: 1 branch
BranchTrueFalseTotal
if ($align eq 'justify') { } 0 0 0
125 0 foreach my $word (@line) {
126 0 $ps->text($xpos, $ypos, $word);
127 0 ▶ $xpos += ($width{$word} + $wordspace) if (@line);
Branch: 1 branch
BranchTrueFalseTotal
if @line 0 0 0
128 }
129 0 $endw = $arg{'-w'};
130 } else {
131 # calculate the left hand position of the line
132 0 ▶ if ($align eq 'right') {
Branch: 2 branches
BranchTrueFalseTotal
if ($align eq 'right') { } 0 0 0
elsif ($align eq 'center') { } 0 0 0
133 0 $xpos += $arg{'-w'} - $line_width;
134 } elsif ($align eq 'center') {
135 0 $xpos += ($arg{'-w'}/2) - ($line_width / 2);
136 }
137  
138 # render the line
139 0 $endw = $ps->text($xpos, $ypos, join("\x20", @line));
140 }
141 0 $ypos -= $arg{'-lead'};
142 0 $first_line = 0;
143 }
144 0 ▶ unshift(@paragraphs, join(' ',@paragraph)) if scalar(@paragraph);
Branch: 1 branch
BranchTrueFalseTotal
if scalar @paragraph 0 0 0
145 0 return ($endw, $ypos, join("\n", @paragraphs))
146 }
147  
148  
149 ############################################################
150 #
151 # table - utility method to build multi-row, multicolumn tables
152 #
153 # ($page,$pg_cnt,$cur_y) = table(
154 # $pdf_object,
155 # $page_object_to_start_on,
156 # $table_data, # an arrayref of arrayrefs
157 # -x => $left_edge_of_table,
158 # -start_y => $baseline_of_first_line_on_first_page,
159 # -next_y => $baseline_of_first_line_on_succeeding_pages,
160 # -start_h => $baseline_of_first_line_on_first_page,
161 # -next_h => $baseline_of_first_line_on_succeeding_pages,
162 # [-w => $table_width,] # technically optional, but almost always a good idea to use
163 # [-row_height => $min_row_height,] # minimum height of row
164 # [-padding => $cellpadding,] # default 0,
165 # [-padding_left => $leftpadding,] # overides -padding
166 # [-padding_right => $rightpadding,] # overides -padding
167 # [-padding_top => $toppadding,] # overides -padding
168 # [-padding_bottom => $bottompadding,] # overides -padding
169 # [-border => $border width,] # default 1, use 0 for no border
170 # [-border_color => $border_color,] # default black
171 # [-font => $pdf->corefont,] # default $pdf->corefont('Times',-encode => 'latin1')
172 # [-font_size => $font_sizwe,] # default 12
173 # [-font_color => font_color,] # font color
174 # [-font_color_odd => font_color_odd,] # font color for odd rows
175 # [-font_color_even => font_color_odd,] # font color for odd rows
176 # [-background_color => 'gray',] # cell background color
177 # [-background_color_odd => $background_color_odd,] # cell background color for odd rows
178 # [-background_color_even => $background_color_even,] # cell background color for even rows
179 # [-column_props => [
180 # {width => $col_a_width, # width of column
181 # justify => 'left'|'right', # text justify in cell
182 # font => $pdf->corefont, # font for this column
183 # font_size => $col_a_font_size, # font size for this column
184 # font_color => $col_a_font_color, # font color for this column
185 # background_color => $col_a_background_color # background color for this column
186 # },
187 # ...
188 # ]
189 # ]
190 # # column_props is an arrayref of hashrefs, where each hashref sets properties for a column in the table.
191 # # -All keys in the hashref are optional, with one caveat in the case of 'width'. See below.
192 # # -If used, there should be one hashref for each column, even if it is an empty hashref
193 # # -Column_props take precendence over general or odd/even row properties
194 # # -If using the 'width' property, it is required for all columns and the total of all column widths should
195 # # be equal to the -w parameter (overall table width). In other words, if you are going to set individual column widths,
196 # # set them accurately with respect to overall table width, otherwise behavior will be unpredictable.
197 # # This is a current limitation, not a feature :-)
198 #);
199 #
200 ############################################################
201 0 ▶sub table {
Subroutine table: not called CC=57 SCAR=81.0
Pod documented
202 0 my $self = shift;
203 0 my $ps = shift;
204 0 my $data = shift;
205 0 my %arg = @_;
206
207 # set default properties
208 0 ▶ my $fnt_name = $arg{'-font'} || 'Helvetica';
Condition0%0/2
$arg{'-font'} || 'Helvetica'
l!l
00
MC/DC0%0/1
$arg{'-font'} || 'Helvetica'
$arg{'-font'}
Truth table0%0/2
A
A= $arg{'-font'}
Aresult
11
00
209 0 ▶ my $fnt_size = $arg{'-font-size'} || 12;
Condition0%0/2
$arg{'-font-size'} || 12
l!l
00
MC/DC0%0/1
$arg{'-font-size'} || 12
$arg{'-font-size'}
Truth table0%0/2
A
A= $arg{'-font-size'}
Aresult
11
00
210 0 $ps->setfont($fnt_name,$fnt_size);
211
212 0 ▶ my $lead = $arg{'-lead'} || $fnt_size;
Condition0%0/3
$arg{'-lead'} || $fnt_size
l!l&&r!l&&!r
000
MC/DC0%0/2
$arg{'-lead'} || $fnt_size
$arg{'-lead'} $fnt_size
Truth table0%0/3
A || B
A= $arg{'-lead'}B= $fnt_size
ABresult
1X1
011
000
213 0 ▶ my $pad_left = $arg{'-padding_left'} || $arg{'-padding'} || 0;
Condition0%0/5
$arg{'-padding_left'} || $arg{'-padding'} || 0
l!l
00
$arg{"-padding_left"} or $arg{"-padding"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$arg{'-padding_left'} || $arg{'-padding'} || 0
$arg{"-padding_left"} $arg{"-padding"}
Truth table0%0/3
A or B
A= $arg{"-padding_left"}B= $arg{"-padding"}
ABresult
1X1
011
000
214 0 ▶ my $pad_right = $arg{'-padding_right'} || $arg{'-padding'} || 0;
Condition0%0/5
$arg{'-padding_right'} || $arg{'-padding'} || 0
l!l
00
$arg{"-padding_right"} or $arg{"-padding"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$arg{'-padding_right'} || $arg{'-padding'} || 0
$arg{"-padding_right"} $arg{"-padding"}
Truth table0%0/3
A or B
A= $arg{"-padding_right"}B= $arg{"-padding"}
ABresult
1X1
011
000
215 0 ▶ my $pad_top = $arg{'-padding_top'} || $arg{'-padding'} || 0;
Condition0%0/5
$arg{'-padding_top'} || $arg{'-padding'} || 0
l!l
00
$arg{"-padding_top"} or $arg{"-padding"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$arg{'-padding_top'} || $arg{'-padding'} || 0
$arg{"-padding_top"} $arg{"-padding"}
Truth table0%0/3
A or B
A= $arg{"-padding_top"}B= $arg{"-padding"}
ABresult
1X1
011
000
216 0 ▶ my $pad_bot = $arg{'-padding_bottom'} || $arg{'-padding'} || 0;
Condition0%0/5
$arg{'-padding_bottom'} || $arg{'-padding'} || 0
l!l
00
$arg{"-padding_bottom"} or $arg{"-padding"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$arg{'-padding_bottom'} || $arg{'-padding'} || 0
$arg{"-padding_bottom"} $arg{"-padding"}
Truth table0%0/3
A or B
A= $arg{"-padding_bottom"}B= $arg{"-padding"}
ABresult
1X1
011
000
217 0 my $pad_w = $pad_left+$pad_right;
218 0 my $pad_h = $pad_top+$pad_bot;
219 0 ▶ my $line_w = defined $arg{'-border'}? $arg{'-border'}:1;
Branch: 1 branch
BranchTrueFalseTotal
defined $arg{'-border'} ? : 0 0 0
220 0 ▶ my $min_row_h = defined ($arg{'-row_height'}) && ($arg{'-row_height'} > ($fnt_size + $pad_top + $pad_bot))? $arg{'-row_height'}:$fnt_size + $pad_top + $pad_bot;
Branch: 1 branch
BranchTrueFalseTotal
defined $arg{'-row_height'} && $arg{'-row_height'} > $fnt_size + $pad_top + $pad_bot ? : 0 0 0
Condition0%0/3
defined $arg{"-row_height"} and $arg{"-row_height"} > $fnt_size + $pad_top + $pad_bot
!ll&&!rl&&r
000
MC/DC0%0/2
defined $arg{"-row_height"} and $arg{"-row_height"} > $fnt_size + $pad_top + $pad_bot
defined $arg{"-row_height"} $arg{"-row_height"} > $fnt_size + $pad_top + $pad_bot
Truth table0%0/3
A and B
A= defined $arg{"-row_height"}B= $arg{"-row_height"} > $fnt_size + $pad_top + $pad_bot
ABresult
0X0
100
111
221 0 my $row_h = $min_row_h;
222 0 my $pg_cnt = 1;
223 0 my $cur_y = $arg{'-start_y'};
224  
225 # Sort out colors
226
227 0 my @border_color = (0,0,0);
228 0 ▶ if ($self->getColor($arg{'-border_color'})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->getColor($arg{"-border_color"})) 0 0 0
229 0 @border_color = $self->getColor($arg{'-border_color'});
230 }
231  
232 0 my @background_color_even = undef;
233 0 ▶ if ($self->getColor($arg{'-background_color_even'})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->getColor($arg{'-background_color_even'})) { } 0 0 0
234 0 @background_color_even = $self->getColor($arg{'-background_color_even'});
235 } else {
236 0 @background_color_even = $self->getColor($arg{'-background_color'});
237 }
238  
239 0 my @background_color_odd = undef;
240 0 ▶ if ($self->getColor($arg{'-background_color_odd'})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->getColor($arg{'-background_color_odd'})) { } 0 0 0
241 0 @background_color_odd = $self->getColor($arg{'-background_color_odd'});
242 } else {
243 0 @background_color_odd = $self->getColor($arg{'-background_color'});
244 }
245  
246 0 my @font_color_even = (0,0,0);
247 0 ▶ if ($self->getColor($arg{'-font_color_even'})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->getColor($arg{'-font_color_even'})) { } 0 0 0
248 0 @font_color_even = $self->getColor($arg{'-font_color_even'});
249 } else {
250 0 @font_color_even = $self->getColor($arg{'-font_color'});
251 }
252  
253 0 my @font_color_odd = (0,0,0);
254 0 ▶ if ($self->getColor($arg{'-font_color_odd'})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->getColor($arg{'-font_color_odd'})) { } 0 0 0
255 0 @font_color_odd = $self->getColor($arg{'-font_color_odd'});
256 } else {
257 0 @font_color_odd = $self->getColor($arg{'-font_color'});
258 }
259  
260 # Build the table
261 0 ▶ if(ref $data) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data) 0 0 0
262  
263 # determine column widths based on content
264 0 ▶ my $col_props = $arg{'-column_props'} || []; # a arrayref whose values are a hashref holding the minimum and maximum width of that column
Condition0%0/2
$arg{'-column_props'} || []
l!l
00
MC/DC0%0/1
$arg{'-column_props'} || []
$arg{'-column_props'}
Truth table0%0/2
A
A= $arg{'-column_props'}
Aresult
11
00
265 0 my $row_props = []; # an array ref of arrayrefs whose values are the actual widths of the column/row intersection
266 0 my ($total_max_w,$total_min_w) = (0,0); # scalars that hold sum of the maximum and minimum widths of all columns
267 0 my ($max_col_w,$min_col_w) = (0,0);
268 0 my $word_w = {};
269 0 my ($row,$col_name,$col_fnt_size,$space_w);
270 0 my $rcnt = 0;
271 0 foreach $row (@$data) {
272 0 my $foo = []; #holds the widths of each column
273 0 ▶ for(my $j =0;$j < scalar(@$row);$j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < scalar @$row) 0 0 0
274  
275 # look for font information for this column
276 0 ▶ $col_fnt_size = $col_props->[$j]->{'font_size'}? $col_props->[$j]->{'font_size'}:$fnt_size;
Branch: 1 branch
BranchTrueFalseTotal
$col_props->[$j]{'font_size'} ? : 0 0 0
277 0 ▶ if($col_props->[$j]->{'font'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($col_props->[$j]{'font'}) { } 0 0 0
278 0 $ps->setfont($col_props->[$j]->{'font'},$col_fnt_size);
279 } else {
280 0 $ps->setfont($fnt_name,$col_fnt_size);
281 }
282 0 $space_w = $self->getStringWidth("\x20");
283
284 0 $foo->[$j] = 0;
285 0 $max_col_w = 0;
286 0 $min_col_w = 0;
287 0 my @words = split(/\s+/, $row->[$j]);
288 0 foreach (@words) {
289 0 ▶ if(!exists $word_w->{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $word_w->{$_}) 0 0 0
290 0 $word_w->{$_} = $self->getStringWidth($_) + $space_w;
291 };
292 0 $foo->[$j] += $word_w->{$_};
293 0 ▶ $min_col_w = $word_w->{$_} if $word_w->{$_} > $min_col_w;
Branch: 1 branch
BranchTrueFalseTotal
if $word_w->{$_} > $min_col_w 0 0 0
294 0 $max_col_w += $word_w->{$_};
295 }
296 0 $min_col_w += $pad_w;
297 0 $max_col_w += $pad_w;
298 0 $foo->[$j] += $pad_w;
299  
300 # keep a running total of the overall min and max widths
301 0 ▶ $col_props->[$j]->{min_w} = $col_props->[$j]->{min_w} || 0;
Condition0%0/2
$col_props->[$j]{'min_w'} || 0
l!l
00
MC/DC0%0/1
$col_props->[$j]{'min_w'} || 0
$col_props->[$j]{'min_w'}
Truth table0%0/2
A
A= $col_props->[$j]{'min_w'}
Aresult
11
00
302 0 ▶ $col_props->[$j]->{max_w} = $col_props->[$j]->{max_w} || 0;
Condition0%0/2
$col_props->[$j]{'max_w'} || 0
l!l
00
MC/DC0%0/1
$col_props->[$j]{'max_w'} || 0
$col_props->[$j]{'max_w'}
Truth table0%0/2
A
A= $col_props->[$j]{'max_w'}
Aresult
11
00
303 0 ▶ if($min_col_w > $col_props->[$j]->{min_w}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($min_col_w > $col_props->[$j]{"min_w"}) 0 0 0
304 0 $total_min_w -= $col_props->[$j]->{min_w};
305 0 $total_min_w += $min_col_w;
306 0 $col_props->[$j]->{min_w} = $min_col_w ;
307 }
308 0 ▶ if($max_col_w > $col_props->[$j]->{max_w}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max_col_w > $col_props->[$j]{"max_w"}) 0 0 0
309 0 $total_max_w -= $col_props->[$j]->{max_w};
310 0 $total_max_w += $max_col_w;
311 0 $col_props->[$j]->{max_w} = $max_col_w ;
312 }
313 }
314 0 $row_props->[$rcnt] = $foo;
315 0 $rcnt++;
316 }
317  
318 # calc real column widths width
319 0 my ($col_widths,$width) = $self->col_widths($col_props, $total_max_w, $total_min_w, $arg{'-w'});
320 0 ▶ $width = $arg{'-w'} if $arg{'-w'};
Branch: 1 branch
BranchTrueFalseTotal
if $arg{"-w"} 0 0 0
321  
322 0 my $comp_cnt = 1;
323 0 my (@background_color, @font_color);
324 0 my ($bot_marg, $table_top_y, $text_start, $record, $record_widths);
325 0 $rcnt=0;
326  
327 # Each iteration adds a new page as neccessary
328 0 ▶ while(scalar(@{$data})) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$data;}) 0 0 0
329 0 ▶ if($pg_cnt == 1){
Branch: 1 branch
BranchTrueFalseTotal
if ($pg_cnt == 1) { } 0 0 0
330 0 $table_top_y = $arg{'-start_y'};
331 0 $bot_marg = $table_top_y - $arg{'-start_h'};
332 } else {
333 0 $ps->newpage;
334 0 $table_top_y = $arg{'-next_y'};
335 0 $bot_marg = $table_top_y - $arg{'-next_h'};
336 }
337  
338 0 $ps->setfont($fnt_name, $fnt_size);
339 0 $ps->setcolour(@border_color);
340 0 $ps->setlinewidth($line_w);
341  
342 #draw the top line
343 0 $cur_y = $table_top_y;
344 0 $ps->line($arg{'-x'}, $cur_y, $arg{'-x'} + $width, $cur_y);
345
346 0 my $safety2 = 20;
347  
348 # Each iteration adds a row to the current page until the page is full or there are no more rows to add
349 0 ▶ while(scalar(@{$data}) and $cur_y-$row_h > $bot_marg) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$data;} and $cur_y - $row_h > $bot_marg) 0 0 0
Condition0%0/3
scalar @{$data;} and $cur_y - $row_h > $bot_marg
!ll&&!rl&&r
000
MC/DC0%0/2
scalar @{$data;} and $cur_y - $row_h > $bot_marg
scalar @{$data;} $cur_y - $row_h > $bot_marg
Truth table0%0/3
A and B
A= scalar @{$data;}B= $cur_y - $row_h > $bot_marg
ABresult
0X0
100
111
350 #remove the next item from $data
351 0 $record = shift @{$data};
352 0 $record_widths = shift @$row_props;
353 0 ▶ next unless $record;
Branch: 1 branch
BranchTrueFalseTotal
unless $record 0 0 0
354  
355 # choose colors for this row
356 0 ▶ @background_color = $rcnt%2?@background_color_even:@background_color_odd;
Branch: 1 branch
BranchTrueFalseTotal
$rcnt % 2 ? : 0 0 0
357 0 ▶ @font_color = $rcnt%2?@font_color_even:@font_color_odd;
Branch: 1 branch
BranchTrueFalseTotal
$rcnt % 2 ? : 0 0 0
358  
359 0 my $cur_x = $arg{'-x'};
360  
361 # draw cell bgcolor
362 # this has to be separately from the text loop because we do not know the finel height of the cell until all text has been drawn
363 0 ▶ if(@background_color) {
Branch: 1 branch
BranchTrueFalseTotal
if (@background_color) 0 0 0
364 0 $cur_x = $arg{'-x'};
365 0 ▶ for(my $j =0;$j < scalar(@$record);$j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < scalar @$record) 0 0 0
366 0 ▶ if($col_props->[$j]->{'background_color'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($col_props->[$j]{'background_color'}) { } 0 0 0
367 0 $ps->setcolour($col_props->[$j]->{'background_color'});
368 } else {
369 0 $ps->setcolour(@background_color);
370 }
371 0 $ps->box({filled => 1}, $cur_x, $cur_y, $cur_x + $col_widths->[$j], $cur_y - $row_h);
372 0 $cur_x += $col_widths->[$j];
373 }
374 }
375  
376 # draw text
377 0 $text_start = $cur_y-$fnt_size-$pad_top;
378 0 $cur_x = $arg{'-x'};
379 0 my $leftovers = undef;
380 0 my $do_leftovers = 0;
381 0 ▶ for(my $j =0;$j < scalar(@$record);$j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < scalar @$record) 0 0 0
382 0 ▶ next unless $col_props->[$j]->{max_w};
Branch: 1 branch
BranchTrueFalseTotal
unless $col_props->[$j]{"max_w"} 0 0 0
383 0 $leftovers->[$j] = undef;
384  
385 # look for column properties that overide row properties
386 0 ▶ if($col_props->[$j]->{'font_color'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($col_props->[$j]{'font_color'}) { } 0 0 0
387 0 $ps->setcolour($col_props->[$j]->{'font_color'});
388 } else {
389 0 $ps->setcolour(@font_color);
390 }
391 0 ▶ $col_fnt_size = $col_props->[$j]->{'font_size'}? $col_props->[$j]->{'font_size'}:$fnt_size;
Branch: 1 branch
BranchTrueFalseTotal
$col_props->[$j]{'font_size'} ? : 0 0 0
392 0 ▶ if($col_props->[$j]->{'font'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($col_props->[$j]{'font'}) { } 0 0 0
393 0 $ps->setfont($col_props->[$j]->{'font'},$col_fnt_size);
394 } else {
395 0 $ps->setfont($fnt_name,$col_fnt_size);
396 }
397 0 ▶ $col_props->[$j]->{justify} = $col_props->[$j]->{justify} || 'left';
Condition0%0/2
$col_props->[$j]{'justify'} || 'left'
l!l
00
MC/DC0%0/1
$col_props->[$j]{'justify'} || 'left'
$col_props->[$j]{'justify'}
Truth table0%0/2
A
A= $col_props->[$j]{'justify'}
Aresult
11
00
398 # if the contents is wider than the specified width, we need to add the text as a text block
399 0 ▶ if($record_widths->[$j] and ($record_widths->[$j] > $col_widths->[$j])) {
Branch: 1 branch
BranchTrueFalseTotal
if ($record_widths->[$j] and $record_widths->[$j] > $col_widths->[$j]) { } 0 0 0
Condition0%0/3
$record_widths->[$j] and $record_widths->[$j] > $col_widths->[$j]
!ll&&!rl&&r
000
MC/DC0%0/2
$record_widths->[$j] and $record_widths->[$j] > $col_widths->[$j]
$record_widths->[$j] $record_widths->[$j] > $col_widths->[$j]
Truth table0%0/3
A and B
A= $record_widths->[$j]B= $record_widths->[$j] > $col_widths->[$j]
ABresult
0X0
100
111
400 my($width_of_last_line, $ypos_of_last_line, $left_over_text) = $self->text_block(
401 $ps,
402 $record->[$j],
403 -x => $cur_x+$pad_left,
404 -y => $text_start,
405 -w => $col_widths->[$j] - $pad_w,
406 -h => $cur_y - $bot_marg - $pad_top - $pad_bot,
407 -align => $col_props->[$j]->{justify},
408 0 -lead => $lead
409 );
410
411 #$lead is added here because $self->text_block returns the incorrect yposition - it is off by $lead
412 0 my $this_row_h = $cur_y - ($ypos_of_last_line +$lead-$pad_bot);
413 0 ▶ $row_h = $this_row_h if $this_row_h > $row_h;
Branch: 1 branch
BranchTrueFalseTotal
if $this_row_h > $row_h 0 0 0
414 0 ▶ if($left_over_text) {
Branch: 1 branch
BranchTrueFalseTotal
if ($left_over_text) 0 0 0
415 0 $leftovers->[$j] = $left_over_text;
416 0 $do_leftovers =1;
417 }
418 } else {
419 # Otherwise just use the $ps->text() method
420 0 my $space = $pad_left;
421 0 ▶ if($col_props->[$j]->{justify} eq 'right') {
Branch: 1 branch
BranchTrueFalseTotal
if ($col_props->[$j]{"justify"} eq "right") 0 0 0
422 0 $space = $col_widths->[$j] - ($self->getStringWidth($record->[$j]) + $pad_right);
423 }
424 0 $ps->text($cur_x + $space, $text_start, $record->[$j]);
425 }
426  
427 0 $cur_x += $col_widths->[$j];
428 }
429 0 ▶ if($do_leftovers) {
Branch: 1 branch
BranchTrueFalseTotal
if ($do_leftovers) 0 0 0
430 0 unshift @$data, $leftovers;
431 0 unshift @$row_props, $record_widths;
432 0 $rcnt--;
433 }
434
435 # draw horizontal lines
436 0 $cur_y -= $row_h;
437 0 $row_h = $min_row_h;
438 0 $ps->setcolour(@border_color);
439 0 $ps->line($arg{'-x'}, $cur_y, $arg{'-x'} + $width, $cur_y);
440 0 $rcnt++;
441  
442 }
443  
444 # draw vertical lines
445 0 $ps->setcolour(@border_color);
446 0 $ps->line($arg{'-x'}, $table_top_y, $arg{'-x'}, $cur_y);
447 0 my $cur_x = $arg{'-x'};
448 0 ▶ for(my $j =0;$j < scalar(@$record);$j++){
Branch: 1 branch
BranchTrueFalseTotal
if ($j < scalar @$record) 0 0 0
449 0 $cur_x += $col_widths->[$j];
450 0 $ps->line($cur_x, $table_top_y, $cur_x, $cur_y);
451 }
452 0 $pg_cnt++;
453 }
454 }
455  
456 #return ($page,--$pg_cnt,$cur_y);
457 0 return $cur_y;
458 }
459  
460
461 # calculate the column widths
462 0 ▶sub col_widths {
Subroutine col_widths: not called CC=8 SCAR=42.8
Pod undocumented
463 0 my $self = shift;
464 0 my $col_props = shift;
465 0 my $max_width = shift;
466 0 my $min_width = shift;
467 0 my $avail_width = shift;
468
469 0 my$calc_widths;
470 my $colname;
471 0 my $total = 0;
472 0 ▶ for(my $j =0;$j < scalar(@$col_props);$j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < scalar @$col_props) 0 0 0
473 #foreach $colname (keys %$col_props){
474
475 0 ▶ if( $col_props->[$j]->{width}) {
Branch: 4 branches
BranchTrueFalseTotal
if ($col_props->[$j]{'width'}) { } 0 0 0
elsif (not $avail_width && $col_props->[$j]{'max_w'}) { } 0 0 0
elsif ($avail_width > $max_width and $max_width > 0) { } 0 0 0
elsif ($min_width > $avail_width) { } 0 0 0
476 # if the width is specified, use that
477 0 $calc_widths->[$j] = $col_props->[$j]->{width};
478 ▶ } elsif( !$avail_width || !$col_props->[$j]->{max_w}) {
Condition0%0/3
$avail_width && $col_props->[$j]{'max_w'}
!ll&&!rl&&r
000
MC/DC0%0/2
$avail_width && $col_props->[$j]{'max_w'}
$avail_width $col_props->[$j]{'max_w'}
Truth table0%0/3
A && B
A= $avail_widthB= $col_props->[$j]{'max_w'}
ABresult
0X0
100
111
479 # if no avail_width is specified
480 # or there is no max_w for the column specified, use the max width
481 0 $calc_widths->[$j] = $col_props->[$j]->{max_w};
482 ▶ } elsif($avail_width > $max_width and $max_width > 0) {
Condition0%0/3
$avail_width > $max_width and $max_width > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$avail_width > $max_width and $max_width > 0
$avail_width > $max_width $max_width > 0
Truth table0%0/3
A and B
A= $avail_width > $max_widthB= $max_width > 0
ABresult
0X0
100
111
483 # if the available space is more than the max, grow each column proportionally
484 0 $calc_widths->[$j] = $col_props->[$j]->{max_w} * ($avail_width/$max_width);
485 } elsif($min_width > $avail_width) {
486 # if the min width is greater than the available width, return the min width
487 0 $calc_widths->[$j] = $col_props->[$j]->{min_w};
488 } else {
489 # else use the autolayout algorithm from RFC 1942
490 0 $calc_widths->[$j] = $col_props->[$j]->{min_w}+(($col_props->[$j]->{max_w} - $col_props->[$j]->{min_w}) * ($avail_width -$min_width))/ ($max_width -$min_width);
491 }
492 0 $total += $calc_widths->[$j];
493 }
494 0 return ($calc_widths,$total);
495 }
496  
497  
498 0 ▶sub getStringWidth {
Subroutine getStringWidth: not called CC=3 SCAR=24.8
Pod undocumented
499 0 my $self = shift;
500 0 my $text = shift;
501  
502 0 ▶ my $font = $self->{current_font} || 'Helvetica';
Condition0%0/2
$self->{'current_font'} || 'Helvetica'
l!l
00
MC/DC0%0/1
$self->{'current_font'} || 'Helvetica'
$self->{'current_font'}
Truth table0%0/2
A
A= $self->{'current_font'}
Aresult
11
00
503 0 ▶ my $font_size = $self->{current_font_size} || 12;
Condition0%0/2
$self->{'current_font_size'} || 12
l!l
00
MC/DC0%0/1
$self->{'current_font_size'} || 12
$self->{'current_font_size'}
Truth table0%0/2
A
A= $self->{'current_font_size'}
Aresult
11
00
504  
505 # check to make sure that this font is supported by Metrics.pm !
506 # Helvetica-Italic is not supported by PostScript::Metrics, therefore
507 # we cannot underline this font
508 0 return PostScript::Metrics::stringwidth($text,$font,$font_size);
509 }
510  
511  
512 0 ▶sub getColor {
Subroutine getColor: not called CC=3 SCAR=24.8
Pod undocumented
513 0 my $self = shift;
514 0 my $color_string = shift;
515
516 0 my @color = ();
517
518 0 ▶ if (!defined($color_string)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $color_string) 0 0 0
519 # return undef if not defined so a default can be used
520 0 return undef;
521 }
522
523 0 ▶ if ($color_string =~ /^\#[0-9a-fA-F]{6}/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($color_string =~ /^\#[0-9a-fA-F]{6}/) { } 0 0 0
524 # Given hex string, convert to base 10 array
525 0 $color_string =~ /^\#(..)(..)(..)/;
526 0 $color[0] = eval "0x$1";
527 0 $color[1] = eval "0x$2";
528 0 $color[2] = eval "0x$3";
529
530 } else {
531 0 warn "found color code";
532 # Given color code, store in array[0]
533 0 @color = ($color_string);
534
535 }
536
537 0 return @color;
538 }
539  
540  
541 1 1;
542 __END__
↑ Summary