| 1 | package PDF::TableX::Cell; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 8 | ▶ | use Moose; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 8 | ▶ | use MooseX::Types; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 8 | ▶ | use PDF::API2; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | |||||||||||||||
| 7 | 8 | with 'PDF::TableX::Drawable'; | |||||||||||||
| 8 | 8 | with 'PDF::TableX::Stylable'; | |||||||||||||
| 9 | |||||||||||||||
| 10 | 8 | has width => (is => 'rw', isa => 'Num'); | |||||||||||||
| 11 | 8 | has min_width => (is => 'rw', isa => 'Num', lazy => 1, builder => '_get_min_width'); | |||||||||||||
| 12 | 8 | has reg_width => (is => 'rw', isa => 'Num', lazy => 1, builder => '_get_reg_width'); | |||||||||||||
| 13 | 8 | has height => (is => 'rw', isa => 'Num', lazy => 1, builder => '_get_height' ); | |||||||||||||
| 14 | 99 | ▶ | has content => (is => 'rw', isa => 'Str', default=> '', trigger => sub { $_[0]->{_overflow} = $_[0]->content; }); | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 15 | |||||||||||||||
| 16 | 8 | has _overflow => (is => 'ro', isa => 'Str', init_arg => undef, default => ''); | |||||||||||||
| 17 | 8 | has _pdf_api_content => (is => 'ro', isa => class_type('PDF::API2::Page'), lazy => 1, builder => '_get_content'); | |||||||||||||
| 18 | 8 | has _parent => (is => 'ro', isa => 'Object'); | |||||||||||||
| 19 | 8 | has _row_idx => (is => 'ro', isa => 'Int', default => 0); | |||||||||||||
| 20 | 8 | has _col_idx => (is => 'ro', isa => 'Int', default => 0); | |||||||||||||
| 21 | |||||||||||||||
| 22 | around 'content' => sub { | ||||||||||||||
| 23 | 199 | ▶ | my $orig = shift; | ||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||
| 24 | 199 | my $self = shift; | |||||||||||||
| 25 | 199 | ▶ | return $self->$orig() unless @_; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 26 | 99 | $self->$orig(@_); | |||||||||||||
| 27 | 99 | return $self; | |||||||||||||
| 28 | 8 | }; | |||||||||||||
| 29 | |||||||||||||||
| 30 | 6 | ▶ | sub _get_content { return PDF::API2->new()->page; } | ||||||||||||
|
Subroutine
_get_content: called CC=1 SCAR=0.0
| |||||||||||||||
| 31 | |||||||||||||||
| 32 | sub _get_min_width { | ||||||||||||||
| 33 | 6 | ▶ | my ($self) = @_; | ||||||||||||
|
Subroutine
_get_min_width: called CC=3 SCAR=20.3
| |||||||||||||||
| 34 | 6 | my $txt = $self->_pdf_api_content->text(); | |||||||||||||
| 35 | 0 | $txt->font( PDF::API2->new()->corefont( $self->font ), $self->font_size ); | |||||||||||||
| 36 | 0 | my $min_width = 0; | |||||||||||||
| 37 | 0 | for (split(/\s/, $self->content)) { | |||||||||||||
| 38 | 0 | my $word_width = $txt->advancewidth($_); | |||||||||||||
| 39 | 0 | ▶ | $min_width = ($word_width > $min_width) ? $word_width : $min_width; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 40 | } | ||||||||||||||
| 41 | 0 | return $min_width + $self->padding->[1] + $self->padding->[3]; | |||||||||||||
| 42 | } | ||||||||||||||
| 43 | |||||||||||||||
| 44 | sub _get_reg_width { | ||||||||||||||
| 45 | 0 | ▶ | my ($self) = @_; | ||||||||||||
|
Subroutine
_get_reg_width: not called CC=3 SCAR=24.8
| |||||||||||||||
| 46 | 0 | my $txt = $self->_pdf_api_content->text(); | |||||||||||||
| 47 | 0 | $txt->font( PDF::API2->new()->corefont( $self->font ), $self->font_size ); | |||||||||||||
| 48 | 0 | my $reg_width = 0; | |||||||||||||
| 49 | 0 | for (split("\n", $self->content)) { | |||||||||||||
| 50 | 0 | my $line_width = $txt->advancewidth($_); | |||||||||||||
| 51 | 0 | ▶ | $reg_width = ($line_width > $reg_width) ? $line_width : $reg_width; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 52 | } | ||||||||||||||
| 53 | 0 | return $reg_width + $self->padding->[1] + $self->padding->[3]; | |||||||||||||
| 54 | } | ||||||||||||||
| 55 | |||||||||||||||
| 56 | sub _get_height { | ||||||||||||||
| 57 | 0 | ▶ | my ($self) = @_; | ||||||||||||
|
Subroutine
_get_height: not called CC=1 SCAR=6.9
| |||||||||||||||
| 58 | 0 | my $height = $self->padding->[0] + $self->padding->[2]; | |||||||||||||
| 59 | 0 | $height += scalar(split("\n", $self->content)) * $self->font_size; | |||||||||||||
| 60 | 0 | return $height; | |||||||||||||
| 61 | } | ||||||||||||||
| 62 | |||||||||||||||
| 63 | sub draw_content { | ||||||||||||||
| 64 | 0 | ▶ | my ($self, $x, $y, $gfx, $txt) = @_; | ||||||||||||
|
Subroutine
draw_content: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||
| 65 | 0 | $y -= $self->padding->[0] + $self->font_size; | |||||||||||||
| 66 | 0 | ▶ | $x += $self->padding->[3] | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 67 | + ($self->text_align eq 'right' ? $self->get_text_width : 0) | ||||||||||||||
| 68 | + ($self->text_align eq 'center' ? $self->get_text_width/2 : 0) | ||||||||||||||
| 69 | ; | ||||||||||||||
| 70 | 0 | my $width = 0; | |||||||||||||
| 71 | 0 | $txt->save; | |||||||||||||
| 72 | 0 | $txt->font( PDF::API2->new()->corefont( $self->font ), $self->font_size ); | |||||||||||||
| 73 | 0 | $txt->lead( $self->font_size ); | |||||||||||||
| 74 | 0 | $txt->fillcolor( $self->font_color ); | |||||||||||||
| 75 | 0 | $txt->translate($x, $y); | |||||||||||||
| 76 | 0 | my $overflow = ''; | |||||||||||||
| 77 | 0 | for my $p ( split ( "\n", $self->{_overflow} ) ) { | |||||||||||||
| 78 | 0 | ▶ | if ($overflow) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 79 | 0 | $overflow .= "\n" . $p; | |||||||||||||
| 80 | } else { | ||||||||||||||
| 81 | 0 | $overflow = $txt->paragraph($p, $self->get_text_width, ($y-$self->margin->[2]-$self->padding->[2]+$self->font_size), -spillover => 0, -align => $self->text_align); | |||||||||||||
| 82 | } | ||||||||||||||
| 83 | } | ||||||||||||||
| 84 | 0 | $self->{_overflow} = $overflow; | |||||||||||||
| 85 | 0 | $self->height( $y - [ $txt->textpos() ]->[1] + $self->padding->[0] + $self->padding->[2]); | |||||||||||||
| 86 | 0 | $txt->restore; | |||||||||||||
| 87 | 0 | return ($self->get_text_width, $self->height, length($overflow)); | |||||||||||||
| 88 | } | ||||||||||||||
| 89 | |||||||||||||||
| 90 | sub reset_content { | ||||||||||||||
| 91 | 0 | ▶ | my ($self) = @_; | ||||||||||||
|
Subroutine
reset_content: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 92 | 0 | $self->{_overflow} = $self->content; | |||||||||||||
| 93 | 0 | return $self; | |||||||||||||
| 94 | } | ||||||||||||||
| 95 | |||||||||||||||
| 96 | sub get_text_width { | ||||||||||||||
| 97 | 0 | ▶ | my ($self) = @_; | ||||||||||||
|
Subroutine
get_text_width: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 98 | 0 | return $self->width - $self->padding->[1] - $self->padding->[3]; | |||||||||||||
| 99 | } | ||||||||||||||
| 100 | |||||||||||||||
| 101 | sub draw_borders { | ||||||||||||||
| 102 | 0 | ▶ | my ($self, $x, $y, $gfx, $txt) = @_; | ||||||||||||
|
Subroutine
draw_borders: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||
| 103 | 0 | ▶ | if ($self->border_width->[0]) {$self->_draw_top_border($x, $y, $gfx)} | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 104 | 0 | ▶ | if ($self->border_width->[1]) {$self->_draw_right_border($x, $y, $gfx)} | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 105 | 0 | ▶ | if ($self->border_width->[2]) {$self->_draw_bottom_border($x, $y, $gfx)} | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 106 | 0 | ▶ | if ($self->border_width->[3]) {$self->_draw_left_border($x, $y, $gfx)} | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 107 | } | ||||||||||||||
| 108 | |||||||||||||||
| 109 | sub _draw_top_border { | ||||||||||||||
| 110 | 0 | ▶ | my ($self, $x, $y, $gfx) = @_; | ||||||||||||
|
Subroutine
_draw_top_border: not called CC=2 SCAR=17.9
| |||||||||||||||
| 111 | 0 | ▶ | $y = ($self->{_row_idx} == 0) ? $y-$self->border_width->[0]/2 : $y; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 112 | 0 | $gfx->move($x, $y); | |||||||||||||
| 113 | 0 | $gfx->linewidth($self->border_width->[0]); | |||||||||||||
| 114 | 0 | $gfx->strokecolor($self->border_color->[0]); | |||||||||||||
| 115 | 0 | $gfx->hline($x+$self->width); | |||||||||||||
| 116 | 0 | $gfx->stroke(); | |||||||||||||
| 117 | } | ||||||||||||||
| 118 | |||||||||||||||
| 119 | sub _draw_right_border { | ||||||||||||||
| 120 | 0 | ▶ | my ($self, $x, $y, $gfx) = @_; | ||||||||||||
|
Subroutine
_draw_right_border: not called CC=2 SCAR=17.9
| |||||||||||||||
| 121 | 0 | ▶ | $x = ($self->{_parent}->is_last_in_row($self->{_col_idx})) ? $x-$self->border_width->[1]/2 : $x; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 122 | 0 | $gfx->move($x+$self->width, $y); | |||||||||||||
| 123 | 0 | $gfx->linewidth($self->border_width->[1]); | |||||||||||||
| 124 | 0 | $gfx->strokecolor($self->border_color->[1]); | |||||||||||||
| 125 | 0 | $gfx->vline($y-$self->height); | |||||||||||||
| 126 | 0 | $gfx->stroke(); | |||||||||||||
| 127 | } | ||||||||||||||
| 128 | |||||||||||||||
| 129 | sub _draw_bottom_border { | ||||||||||||||
| 130 | 0 | ▶ | my ($self, $x, $y, $gfx) = @_; | ||||||||||||
|
Subroutine
_draw_bottom_border: not called CC=2 SCAR=17.9
| |||||||||||||||
| 131 | 0 | ▶ | $y = ($self->{_parent}->is_last_in_col($self->{_row_idx})) ? $y+$self->border_width->[2]/2 : $y; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 132 | 0 | $gfx->move($x, $y-$self->height); | |||||||||||||
| 133 | 0 | $gfx->linewidth($self->border_width->[2]); | |||||||||||||
| 134 | 0 | $gfx->strokecolor($self->border_color->[2]); | |||||||||||||
| 135 | 0 | $gfx->hline($x+$self->width); | |||||||||||||
| 136 | 0 | $gfx->stroke(); | |||||||||||||
| 137 | } | ||||||||||||||
| 138 | |||||||||||||||
| 139 | sub _draw_left_border { | ||||||||||||||
| 140 | 0 | ▶ | my ($self, $x, $y, $gfx) = @_; | ||||||||||||
|
Subroutine
_draw_left_border: not called CC=2 SCAR=17.9
| |||||||||||||||
| 141 | 0 | ▶ | $x = ($self->{_col_idx} == 0) ? $x+$self->border_width->[3]/2 : $x; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 142 | 0 | $gfx->move($x, $y); | |||||||||||||
| 143 | 0 | $gfx->linewidth($self->border_width->[3]); | |||||||||||||
| 144 | 0 | $gfx->strokecolor($self->border_color->[3]); | |||||||||||||
| 145 | 0 | $gfx->vline($y-$self->height); | |||||||||||||
| 146 | 0 | $gfx->stroke(); | |||||||||||||
| 147 | } | ||||||||||||||
| 148 | |||||||||||||||
| 149 | sub draw_background { | ||||||||||||||
| 150 | 0 | ▶ | my ($self, $x, $y, $gfx, $txt) = @_; | ||||||||||||
|
Subroutine
draw_background: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||
| 151 | 0 | ▶ | if ( $self->background_color ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 152 | 0 | $gfx->linewidth(0); | |||||||||||||
| 153 | 0 | $gfx->fillcolor($self->background_color); | |||||||||||||
| 154 | 0 | $gfx->rect($x, $y-$self->height, $self->width, $self->height); | |||||||||||||
| 155 | 0 | $gfx->fill(); | |||||||||||||
| 156 | } | ||||||||||||||
| 157 | } | ||||||||||||||
| 158 | |||||||||||||||
| 159 | 8 | 1; | |||||||||||||
| 160 | |||||||||||||||
| 161 | =head1 NAME | ||||||||||||||
| 162 | |||||||||||||||
| 163 | PDF::TableX::Cell | ||||||||||||||
| 164 | |||||||||||||||
| 165 | =head1 VERSION | ||||||||||||||
| 166 | |||||||||||||||
| 167 | Version 0.01 | ||||||||||||||
| 168 | |||||||||||||||
| 169 | =cut | ||||||||||||||
| 170 | |||||||||||||||
| 171 | =head1 SYNOPSIS | ||||||||||||||
| 172 | |||||||||||||||
| 173 | =head1 METHODS | ||||||||||||||
| 174 | |||||||||||||||
| 175 | =head2 background_color | ||||||||||||||
| 176 | |||||||||||||||
| 177 | TODO | ||||||||||||||
| 178 | |||||||||||||||
| 179 | =head2 border_color | ||||||||||||||
| 180 | |||||||||||||||
| 181 | TODO | ||||||||||||||
| 182 | |||||||||||||||
| 183 | =head2 border_style | ||||||||||||||
| 184 | |||||||||||||||
| 185 | TODO | ||||||||||||||
| 186 | |||||||||||||||
| 187 | =head2 border_width | ||||||||||||||
| 188 | |||||||||||||||
| 189 | TODO | ||||||||||||||
| 190 | |||||||||||||||
| 191 | =head2 content | ||||||||||||||
| 192 | |||||||||||||||
| 193 | TODO | ||||||||||||||
| 194 | |||||||||||||||
| 195 | =head2 draw_background | ||||||||||||||
| 196 | |||||||||||||||
| 197 | TODO | ||||||||||||||
| 198 | |||||||||||||||
| 199 | =head2 draw_borders | ||||||||||||||
| 200 | |||||||||||||||
| 201 | TODO | ||||||||||||||
| 202 | |||||||||||||||
| 203 | =head2 draw_content | ||||||||||||||
| 204 | |||||||||||||||
| 205 | TODO | ||||||||||||||
| 206 | |||||||||||||||
| 207 | =head2 font | ||||||||||||||
| 208 | |||||||||||||||
| 209 | TODO | ||||||||||||||
| 210 | |||||||||||||||
| 211 | =head2 font_color | ||||||||||||||
| 212 | |||||||||||||||
| 213 | TODO | ||||||||||||||
| 214 | |||||||||||||||
| 215 | =head2 font_size | ||||||||||||||
| 216 | |||||||||||||||
| 217 | TODO | ||||||||||||||
| 218 | |||||||||||||||
| 219 | =head2 get_text_width | ||||||||||||||
| 220 | |||||||||||||||
| 221 | TODO | ||||||||||||||
| 222 | |||||||||||||||
| 223 | =head2 height | ||||||||||||||
| 224 | |||||||||||||||
| 225 | TODO | ||||||||||||||
| 226 | |||||||||||||||
| 227 | =head2 margin | ||||||||||||||
| 228 | |||||||||||||||
| 229 | TODO | ||||||||||||||
| 230 | |||||||||||||||
| 231 | =head2 meta | ||||||||||||||
| 232 | |||||||||||||||
| 233 | TODO | ||||||||||||||
| 234 | |||||||||||||||
| 235 | =head2 min_width | ||||||||||||||
| 236 | |||||||||||||||
| 237 | TODO | ||||||||||||||
| 238 | |||||||||||||||
| 239 | =head2 padding | ||||||||||||||
| 240 | |||||||||||||||
| 241 | TODO | ||||||||||||||
| 242 | |||||||||||||||
| 243 | =head2 reg_width | ||||||||||||||
| 244 | |||||||||||||||
| 245 | TODO | ||||||||||||||
| 246 | |||||||||||||||
| 247 | =head2 reset_content | ||||||||||||||
| 248 | |||||||||||||||
| 249 | TODO | ||||||||||||||
| 250 | |||||||||||||||
| 251 | =head2 text_align | ||||||||||||||
| 252 | |||||||||||||||
| 253 | TODO | ||||||||||||||
| 254 | |||||||||||||||
| 255 | =head2 width | ||||||||||||||
| 256 | |||||||||||||||
| 257 | TODO | ||||||||||||||
| 258 | |||||||||||||||
| 259 | =head1 AUTHOR | ||||||||||||||
| 260 | |||||||||||||||
| 261 | Grzegorz Papkala, C<< <grzegorzpapkala at gmail.com> >> | ||||||||||||||
| 262 | |||||||||||||||
| 263 | =head1 BUGS | ||||||||||||||
| 264 | |||||||||||||||
| 265 | Please report any bugs or feature requests at: L<https://github.com/grzegorzpapkala/PDF-TableX/issues> | ||||||||||||||
| 266 | |||||||||||||||
| 267 | =head1 SUPPORT | ||||||||||||||
| 268 | |||||||||||||||
| 269 | PDF::TableX is hosted on GitHub L<https://github.com/grzegorzpapkala/PDF-TableX> | ||||||||||||||
| 270 | |||||||||||||||
| 271 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||
| 272 | |||||||||||||||
| 273 | =head1 COPYRIGHT & LICENSE | ||||||||||||||
| 274 | |||||||||||||||
| 275 | Copyright 2013 Grzegorz Papkala, all rights reserved. | ||||||||||||||
| 276 | |||||||||||||||
| 277 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||
| 278 | under the same terms as Perl itself. | ||||||||||||||
| 279 | |||||||||||||||
| 280 | =cut | ||||||||||||||