File Coverage

blib/lib/PDF/TableX/Cell.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             package PDF::TableX::Cell;
2              
3 1     1   2076 use Moose;
  0            
  0            
4             use MooseX::Types;
5             use PDF::API2;
6              
7             with 'PDF::TableX::Drawable';
8             with 'PDF::TableX::Stylable';
9              
10             has width => (is => 'rw', isa => 'Num');
11             has min_width => (is => 'rw', isa => 'Num', lazy => 1, builder => '_get_min_width');
12             has reg_width => (is => 'rw', isa => 'Num', lazy => 1, builder => '_get_reg_width');
13             has height => (is => 'rw', isa => 'Num', lazy => 1, builder => '_get_height' );
14             has content => (is => 'rw', isa => 'Str', default=> '', trigger => sub { $_[0]->{_overflow} = $_[0]->content; });
15              
16             has _overflow => (is => 'ro', isa => 'Str', init_arg => undef, default => '');
17             has _pdf_api_content => (is => 'ro', isa => class_type('PDF::API2::Page'), lazy => 1, builder => '_get_content');
18             has _parent => (is => 'ro', isa => 'Object');
19             has _row_idx => (is => 'ro', isa => 'Int', default => 0);
20             has _col_idx => (is => 'ro', isa => 'Int', default => 0);
21              
22             around 'content' => sub {
23             my $orig = shift;
24             my $self = shift;
25             return $self->$orig() unless @_;
26             $self->$orig(@_);
27             return $self;
28             };
29              
30             sub _get_content { return PDF::API2->new()->page; }
31              
32             sub _get_min_width {
33             my ($self) = @_;
34             my $txt = $self->_pdf_api_content->text();
35             $txt->font( PDF::API2->new()->corefont( $self->font ), $self->font_size );
36             my $min_width = 0;
37             for (split(/\s/, $self->content)) {
38             my $word_width = $txt->advancewidth($_);
39             $min_width = ($word_width > $min_width) ? $word_width : $min_width;
40             }
41             return $min_width + $self->padding->[1] + $self->padding->[3];
42             }
43              
44             sub _get_reg_width {
45             my ($self) = @_;
46             my $txt = $self->_pdf_api_content->text();
47             $txt->font( PDF::API2->new()->corefont( $self->font ), $self->font_size );
48             my $reg_width = 0;
49             for (split("\n", $self->content)) {
50             my $line_width = $txt->advancewidth($_);
51             $reg_width = ($line_width > $reg_width) ? $line_width : $reg_width;
52             }
53             return $reg_width + $self->padding->[1] + $self->padding->[3];
54             }
55              
56             sub _get_height {
57             my ($self) = @_;
58             my $height = $self->padding->[0] + $self->padding->[2];
59             $height += scalar(split("\n", $self->content)) * $self->font_size;
60             return $height;
61             }
62              
63             sub draw_content {
64             my ($self, $x, $y, $gfx, $txt) = @_;
65             $y -= $self->padding->[0] + $self->font_size;
66             $x += $self->padding->[3]
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             my $width = 0;
71             $txt->save;
72             $txt->font( PDF::API2->new()->corefont( $self->font ), $self->font_size );
73             $txt->lead( $self->font_size );
74             $txt->fillcolor( $self->font_color );
75             $txt->translate($x, $y);
76             my $overflow = '';
77             for my $p ( split ( "\n", $self->{_overflow} ) ) {
78             if ($overflow) {
79             $overflow .= "\n" . $p;
80             } else {
81             $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             $self->{_overflow} = $overflow;
85             $self->height( $y - [ $txt->textpos() ]->[1] + $self->padding->[0] + $self->padding->[2]);
86             $txt->restore;
87             return ($self->get_text_width, $self->height, length($overflow));
88             }
89              
90             sub reset_content {
91             my ($self) = @_;
92             $self->{_overflow} = $self->content;
93             return $self;
94             }
95              
96             sub get_text_width {
97             my ($self) = @_;
98             return $self->width - $self->padding->[1] - $self->padding->[3];
99             }
100              
101             sub draw_borders {
102             my ($self, $x, $y, $gfx, $txt) = @_;
103             if ($self->border_width->[0]) {$self->_draw_top_border($x, $y, $gfx)}
104             if ($self->border_width->[1]) {$self->_draw_right_border($x, $y, $gfx)}
105             if ($self->border_width->[2]) {$self->_draw_bottom_border($x, $y, $gfx)}
106             if ($self->border_width->[3]) {$self->_draw_left_border($x, $y, $gfx)}
107             }
108              
109             sub _draw_top_border {
110             my ($self, $x, $y, $gfx) = @_;
111             $y = ($self->{_row_idx} == 0) ? $y-$self->border_width->[0]/2 : $y;
112             $gfx->move($x, $y);
113             $gfx->linewidth($self->border_width->[0]);
114             $gfx->strokecolor($self->border_color->[0]);
115             $gfx->hline($x+$self->width);
116             $gfx->stroke();
117             }
118              
119             sub _draw_right_border {
120             my ($self, $x, $y, $gfx) = @_;
121             $x = ($self->{_parent}->is_last_in_row($self->{_col_idx})) ? $x-$self->border_width->[1]/2 : $x;
122             $gfx->move($x+$self->width, $y);
123             $gfx->linewidth($self->border_width->[1]);
124             $gfx->strokecolor($self->border_color->[1]);
125             $gfx->vline($y-$self->height);
126             $gfx->stroke();
127             }
128              
129             sub _draw_bottom_border {
130             my ($self, $x, $y, $gfx) = @_;
131             $y = ($self->{_parent}->is_last_in_col($self->{_row_idx})) ? $y+$self->border_width->[2]/2 : $y;
132             $gfx->move($x, $y-$self->height);
133             $gfx->linewidth($self->border_width->[2]);
134             $gfx->strokecolor($self->border_color->[2]);
135             $gfx->hline($x+$self->width);
136             $gfx->stroke();
137             }
138              
139             sub _draw_left_border {
140             my ($self, $x, $y, $gfx) = @_;
141             $x = ($self->{_col_idx} == 0) ? $x+$self->border_width->[3]/2 : $x;
142             $gfx->move($x, $y);
143             $gfx->linewidth($self->border_width->[3]);
144             $gfx->strokecolor($self->border_color->[3]);
145             $gfx->vline($y-$self->height);
146             $gfx->stroke();
147             }
148              
149             sub draw_background {
150             my ($self, $x, $y, $gfx, $txt) = @_;
151             if ( $self->background_color ) {
152             $gfx->linewidth(0);
153             $gfx->fillcolor($self->background_color);
154             $gfx->rect($x, $y-$self->height, $self->width, $self->height);
155             $gfx->fill();
156             }
157             }
158              
159             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 FUNCTIONS
174              
175             =head1 AUTHOR
176              
177             Grzegorz Papkala, C<< <grzegorzpapkala at gmail.com> >>
178              
179             =head1 BUGS
180              
181             Please report any bugs or feature requests at: L<https://github.com/grzegorzpapkala/PDF-TableX/issues>
182              
183             =head1 SUPPORT
184              
185             PDF::TableX is hosted on GitHub L<https://github.com/grzegorzpapkala/PDF-TableX>
186              
187             =head1 ACKNOWLEDGEMENTS
188              
189             =head1 COPYRIGHT & LICENSE
190              
191             Copyright 2013 Grzegorz Papkala, all rights reserved.
192              
193             This program is free software; you can redistribute it and/or modify it
194             under the same terms as Perl itself.
195              
196             =cut