| 1 | package Mojo::PDF::Primitive::Table; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | our $VERSION = '1.005004'; # VERSION | |||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use List::AllUtils qw/sum uniq/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Types::Standard qw/ | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | HashRef ArrayRef Tuple InstanceOf StrictNum Str CodeRef Optional | ||||||||||||||||||||||||||||
| 8 | 2 | /; | |||||||||||||||||||||||||||
| 9 | 2 | ▶ | use Types::Common::Numeric qw/PositiveInt PositiveOrZeroNum PositiveNum/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use Moo 2.000002; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use Tie::RangeHash 1.05; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 2 | $Carp::Internal{ (__PACKAGE__) }++; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | ##### Required | ||||||||||||||||||||||||||||
| 17 | 2 | has at => ( | |||||||||||||||||||||||||||
| 18 | is => 'ro', | ||||||||||||||||||||||||||||
| 19 | required => 1, | ||||||||||||||||||||||||||||
| 20 | isa => Tuple[ StrictNum, StrictNum ], | ||||||||||||||||||||||||||||
| 21 | ); | ||||||||||||||||||||||||||||
| 22 | 2 | has data => ( is => 'ro', required => 1, isa => ArrayRef, ); | |||||||||||||||||||||||||||
| 23 | 2 | has pdf => ( | |||||||||||||||||||||||||||
| 24 | is => 'ro', | ||||||||||||||||||||||||||||
| 25 | required => 1, | ||||||||||||||||||||||||||||
| 26 | isa => InstanceOf['Mojo::PDF'], | ||||||||||||||||||||||||||||
| 27 | ); | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | ##### Defaults/Optional | ||||||||||||||||||||||||||||
| 30 | 2 | has header => ( | |||||||||||||||||||||||||||
| 31 | is => 'ro', | ||||||||||||||||||||||||||||
| 32 | isa => Str, | ||||||||||||||||||||||||||||
| 33 | ); | ||||||||||||||||||||||||||||
| 34 | has border => ( | ||||||||||||||||||||||||||||
| 35 | is => 'ro', | ||||||||||||||||||||||||||||
| 36 | 0 | ▶ | default => sub { [.5, '#ccc'] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 37 | 2 | isa => Tuple[PositiveOrZeroNum, Str], | |||||||||||||||||||||||||||
| 38 | ); | ||||||||||||||||||||||||||||
| 39 | has max_height => ( | ||||||||||||||||||||||||||||
| 40 | is => 'ro', | ||||||||||||||||||||||||||||
| 41 | default => '+Inf', | ||||||||||||||||||||||||||||
| 42 | 0 | ▶ | coerce => sub { ref $_[0] ? $_[0]->[0] : $_[0] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | 2 | isa => PositiveOrZeroNum, | |||||||||||||||||||||||||||
| 44 | ); | ||||||||||||||||||||||||||||
| 45 | 2 | has min_width => ( is => 'ro', default => 0, isa =>PositiveOrZeroNum); | |||||||||||||||||||||||||||
| 46 | has padding => ( | ||||||||||||||||||||||||||||
| 47 | is => 'ro', | ||||||||||||||||||||||||||||
| 48 | 0 | ▶ | default => sub { [3, 6] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 49 | isa => Tuple[PositiveOrZeroNum,Optional[PositiveOrZeroNum],Optional[PositiveOrZeroNum],Optional[PositiveOrZeroNum]], | ||||||||||||||||||||||||||||
| 50 | 0 | ▶ | coerce => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 51 | 0 | my $v = shift; | |||||||||||||||||||||||||||
| 52 | 0 | ▶ | return @$v == 4 ? [@$v] | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 53 | : @$v == 3 ? [ $v->[0], $v->[1], $v->[2], $v->[1] ] | ||||||||||||||||||||||||||||
| 54 | : @$v == 2 ? [ $v->[0], $v->[1], $v->[0], $v->[1] ] | ||||||||||||||||||||||||||||
| 55 | : [ (@$v) x 4 ]; | ||||||||||||||||||||||||||||
| 56 | }, | ||||||||||||||||||||||||||||
| 57 | 2 | ); | |||||||||||||||||||||||||||
| 58 | 2 | has row_height => ( is => 'ro', default => 12, isa => PositiveNum, ); | |||||||||||||||||||||||||||
| 59 | has str_width_mult => ( | ||||||||||||||||||||||||||||
| 60 | is => 'ro', | ||||||||||||||||||||||||||||
| 61 | default => 1, | ||||||||||||||||||||||||||||
| 62 | isa => HashRef, | ||||||||||||||||||||||||||||
| 63 | 0 | ▶ | coerce => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 64 | 0 | my ( $v ) = @_; | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 0 | tie my %widths, 'Tie::RangeHash'; | |||||||||||||||||||||||||||
| 67 | 0 | ▶ | unless ( ref $v ) { $widths{'0,'} = $v; return \%widths; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 0 | my $prev = -1; | |||||||||||||||||||||||||||
| 70 | 0 | for ( uniq map sprintf('%.f', $_), sort { $a <=> $b } keys %$v ) { | |||||||||||||||||||||||||||
| 71 | 0 | $widths{"$prev.1,$_"} = $v->{$_}; | |||||||||||||||||||||||||||
| 72 | 0 | $prev = $_; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 0 | return \%widths; | |||||||||||||||||||||||||||
| 76 | }, | ||||||||||||||||||||||||||||
| 77 | 2 | ); | |||||||||||||||||||||||||||
| 78 | ##### Internal | ||||||||||||||||||||||||||||
| 79 | 2 | has _extra_row_h => ( is => 'rw', default => 0, ); | |||||||||||||||||||||||||||
| 80 | 2 | has _row_lines => ( is => 'rw', default => 0, ); | |||||||||||||||||||||||||||
| 81 | 0 | ▶ | has _border_color => ( is => 'lazy', builder => sub { shift->border->[1] }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 82 | 0 | ▶ | has _border_width => ( is => 'lazy', builder => sub { shift->border->[0] }); | ||||||||||||||||||||||||||
|
Subroutine
_build__border_width: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 83 | 2 | has _col_widths => ( is => 'lazy', ); | |||||||||||||||||||||||||||
| 84 | 2 | has _cols => ( is => 'lazy', ); | |||||||||||||||||||||||||||
| 85 | 0 | ▶ | has _header_row => ( is => 'lazy', builder => sub { shift->data->[0] }); | ||||||||||||||||||||||||||
|
Subroutine
_build__header_row: not called CC=1 SCAR=2.6
Subroutine
__ANON__: not called CC=1 SCAR=2.6
| |||||||||||||||||||||||||||||
| 86 | 0 | ▶ | has _rows => ( is => 'lazy', builder => sub {scalar @{shift->data}}); | ||||||||||||||||||||||||||
|
Subroutine
_build__rows: not called CC=1 SCAR=2.6
| |||||||||||||||||||||||||||||
| 87 | 0 | ▶ | has _x => ( is => 'lazy', builder => sub { shift->at->[0] }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 88 | 0 | ▶ | has _y => ( is => 'lazy', builder => sub { shift->at->[1] }); | ||||||||||||||||||||||||||
|
Subroutine
_build__y: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 0 | ▶ | sub _build__cols { | ||||||||||||||||||||||||||
|
Subroutine
_build__cols: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 91 | 0 | my $data = shift->data; | |||||||||||||||||||||||||||
| 92 | 0 | my $col_num = 0; | |||||||||||||||||||||||||||
| 93 | 0 | ▶ | @$_ > $col_num and $col_num = @$_ for @$data; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 0 | return $col_num; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 0 | ▶ | sub _build__col_widths { | ||||||||||||||||||||||||||
|
Subroutine
_build__col_widths: not called CC=12 SCAR=50.5
| |||||||||||||||||||||||||||||
| 98 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 99 | 0 | my $data = $self->data; | |||||||||||||||||||||||||||
| 100 | 0 | my $col_num = $self->_cols; | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 0 | my @col_widths = (0) x $col_num; | |||||||||||||||||||||||||||
| 103 | 0 | my $w_mult = $self->str_width_mult; | |||||||||||||||||||||||||||
| 104 | 0 | for my $row ( @$data ) { | |||||||||||||||||||||||||||
| 105 | 0 | for ( 0 .. $col_num - 1 ) { | |||||||||||||||||||||||||||
| 106 | 0 | ▶ | next unless my $l = length $row->[$_]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 0 | my $w = 0; | |||||||||||||||||||||||||||
| 108 | 0 | for ( split /\n/, $row->[$_] ) { | |||||||||||||||||||||||||||
| 109 | 0 | ▶ | my $mult = $w_mult->{ +length } // 1; | ||||||||||||||||||||||||||
|
Condition0%0/2
$$w_mult{length $_} // 1
MC/DC0%0/1
$$w_mult{length $_} // 1
$$w_mult{length $_}
Truth table0%0/2
A
A= $$w_mult{length $_}
| |||||||||||||||||||||||||||||
| 110 | 0 | my $new_w = $mult * $self->pdf->_str_width( $_ ); | |||||||||||||||||||||||||||
| 111 | 0 | ▶ | $w = $new_w if $new_w > $w; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | 0 | ▶ | $col_widths[$_] = $w if $w > $col_widths[$_]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 0 | my ( undef, $pad_xr, undef, $pad_xl ) = @{ $self->padding }; | |||||||||||||||||||||||||||
| 118 | 0 | $_ += $pad_xr + $pad_xl for @col_widths; # cell padding | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | # Stretch largest column to fill table to its min_width | ||||||||||||||||||||||||||||
| 121 | 0 | ▶ | if ( $self->min_width > sum @col_widths ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | 0 | my $idx = 0; | |||||||||||||||||||||||||||
| 123 | 0 | for ( 0 .. $#col_widths ) { | |||||||||||||||||||||||||||
| 124 | 0 | ▶ | $idx = $_ if $col_widths[$_] > $col_widths[0] | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | 0 | $col_widths[$idx] += $self->min_width - sum @col_widths; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 0 | return \@col_widths; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | #### | ||||||||||||||||||||||||||||
| 133 | #### METHODS | ||||||||||||||||||||||||||||
| 134 | #### | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 0 | ▶ | sub draw { | ||||||||||||||||||||||||||
|
Subroutine
draw: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 137 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 138 | 0 | my $data = $self->data; | |||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 0 | $self->pdf->_stroke( $self->_border_width ); | |||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 0 | for my $row ( 1 .. $self->_rows ) { | |||||||||||||||||||||||||||
| 143 | 0 | ▶ | $self->_draw_row( $row, $data->[$row-1] ) | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 144 | or return ( | ||||||||||||||||||||||||||||
| 145 | $self->header ? $self->_header_row : (), | ||||||||||||||||||||||||||||
| 146 | @$data[$row-1 .. $self->_rows-1], | ||||||||||||||||||||||||||||
| 147 | ); | ||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 0 | return; | |||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 0 | ▶ | sub _draw_row { | ||||||||||||||||||||||||||
|
Subroutine
_draw_row: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 154 | 0 | my ( $self, $r_num, $cells ) = @_; | |||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 0 | my $row_lines = 1; | |||||||||||||||||||||||||||
| 157 | 0 | for ( @$cells ) { | |||||||||||||||||||||||||||
| 158 | 0 | my $cell_lines = 1 + tr/\n//; | |||||||||||||||||||||||||||
| 159 | 0 | ▶ | $row_lines = $cell_lines if $row_lines < $cell_lines; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | 0 | $self->_row_lines( $row_lines ); | |||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 0 | for my $cell ( 1 .. @$cells ) { | |||||||||||||||||||||||||||
| 164 | 0 | ▶ | $self->_draw_cell( $r_num, $cell, $cells->[$cell-1] ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 165 | or return; | ||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | $self->_extra_row_h( | ||||||||||||||||||||||||||||
| 169 | 0 | $self->_extra_row_h + $self->row_height * ($row_lines-1) | |||||||||||||||||||||||||||
| 170 | ); | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 0 | return 1; | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 0 | ▶ | sub _draw_cell { | ||||||||||||||||||||||||||
|
Subroutine
_draw_cell: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||
| 176 | 0 | my ( $self, $r_num, $c_num, $text ) = @_; | |||||||||||||||||||||||||||
| 177 | 0 | my $pdf = $self->pdf; | |||||||||||||||||||||||||||
| 178 | 0 | ▶ | return 1 unless length $text; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 0 | my ( $pad_yt, undef, $pad_yb, $pad_xl ) = @{ $self->padding }; | |||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 0 | my $x1 = $self->_x; | |||||||||||||||||||||||||||
| 183 | 0 | $x1 += $self->_col_widths->[$_] for 0 .. $c_num - 2; | |||||||||||||||||||||||||||
| 184 | 0 | my $y1 = $self->_y + ($self->row_height + $pad_yt + $pad_yb)*($r_num-1) | |||||||||||||||||||||||||||
| 185 | + $self->_extra_row_h; | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 0 | my $x2 = $x1 + $self->_col_widths->[$c_num-1]; | |||||||||||||||||||||||||||
| 188 | 0 | my $y2 = $y1 + $pad_yt + $pad_yb | |||||||||||||||||||||||||||
| 189 | + ( $self->row_height * $self->_row_lines ); | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | 0 | ▶ | return if $y2 > $self->max_height; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 0 | my $saved_color = $pdf->_cur_color; | |||||||||||||||||||||||||||
| 194 | 0 | $pdf->color( $self->_border_color ); | |||||||||||||||||||||||||||
| 195 | 0 | $pdf->_line( $x1, $y1, $x2, $y1 ); | |||||||||||||||||||||||||||
| 196 | 0 | $pdf->_line( $x2, $y1, $x2, $y2 ); | |||||||||||||||||||||||||||
| 197 | 0 | $pdf->_line( $x2, $y2, $x1, $y2 ); | |||||||||||||||||||||||||||
| 198 | 0 | $pdf->_line( $x1, $y2, $x1, $y1 ); | |||||||||||||||||||||||||||
| 199 | 0 | $pdf->color( @$saved_color ); | |||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | # Render table header | ||||||||||||||||||||||||||||
| 202 | 0 | ▶ | if ( $r_num == 1 and $self->header ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$r_num == 1 and $self->header
MC/DC0%0/2
$r_num == 1 and $self->header
$r_num == 1
$self->header
Truth table0%0/3
A and B
A= $r_num == 1B= $self->header
| |||||||||||||||||||||||||||||
| 203 | 0 | my $saved_font = $pdf->_cur_font; | |||||||||||||||||||||||||||
| 204 | 0 | $pdf->font( $self->header ); | |||||||||||||||||||||||||||
| 205 | 0 | $pdf->text( | |||||||||||||||||||||||||||
| 206 | $text, | ||||||||||||||||||||||||||||
| 207 | $x1 + ( .5*$self->_col_widths->[$c_num-1] ), | ||||||||||||||||||||||||||||
| 208 | $y1 + $self->row_height + $pad_yt - 2, | ||||||||||||||||||||||||||||
| 209 | 'center', | ||||||||||||||||||||||||||||
| 210 | ); | ||||||||||||||||||||||||||||
| 211 | 0 | $pdf->font( $saved_font ); | |||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | 0 | return 1; | |||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | $pdf->text( | ||||||||||||||||||||||||||||
| 217 | 0 | $text, | |||||||||||||||||||||||||||
| 218 | $x1 + $pad_xl, | ||||||||||||||||||||||||||||
| 219 | $y1 + $self->row_height + $pad_yt - 2 | ||||||||||||||||||||||||||||
| 220 | ); | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | 0 | return 1; | |||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | 2 | 1; | |||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | __END__ | ||||||||||||||||||||||||||||