| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package PDF::Boxer::Content::Text; |
|
2
|
|
|
|
|
|
|
{ |
|
3
|
|
|
|
|
|
|
$PDF::Boxer::Content::Text::VERSION = '0.004'; |
|
4
|
|
|
|
|
|
|
} |
|
5
|
3
|
|
|
3
|
|
19
|
use Moose; |
|
|
3
|
|
|
|
|
6
|
|
|
|
3
|
|
|
|
|
33
|
|
|
6
|
|
|
|
|
|
|
# ABSTRACT: a box that displays text |
|
7
|
|
|
|
|
|
|
|
|
8
|
3
|
|
|
3
|
|
22167
|
use namespace::autoclean; |
|
|
3
|
|
|
|
|
12
|
|
|
|
3
|
|
|
|
|
38
|
|
|
9
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
extends 'PDF::Boxer::Content::Box'; |
|
11
|
|
|
|
|
|
|
with 'PDF::Boxer::Role::Text'; |
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub get_default_size{ |
|
14
|
0
|
|
|
0
|
1
|
|
my ($self) = @_; |
|
15
|
0
|
|
|
|
|
|
my $space = $self->boxer->max_width; |
|
16
|
0
|
|
|
|
|
|
my ($width, $height) = $self->find_smallest_block($space); |
|
17
|
0
|
0
|
0
|
|
|
|
warn sprintf "Text %s default size: %s x %s\n", $self->name, $width, $height if $self->debug && $self->name; |
|
18
|
0
|
|
|
|
|
|
return (int($width+1), int($height+1)); |
|
19
|
|
|
|
|
|
|
} |
|
20
|
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
around 'update' => sub{ |
|
22
|
|
|
|
|
|
|
my ($orig, $self) = @_; |
|
23
|
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
my ($width, $height) = $self->find_smallest_block($self->width); |
|
25
|
|
|
|
|
|
|
warn sprintf "Text %s smallest_block %s x %s\n", ($self->name, $width, $height) if $self->debug && $self->name; |
|
26
|
|
|
|
|
|
|
$self->set_height($height) if $self->height < $height; |
|
27
|
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
warn sprintf "Text %s child_adjusted_height? %s < %s\n", $self->name, $self->margin_bottom, $self->parent->content_bottom if $self->debug && $self->name; |
|
29
|
|
|
|
|
|
|
if ($self->margin_bottom < $self->parent->content_bottom){ |
|
30
|
|
|
|
|
|
|
warn "Text child_adjusted_height\n" if $self->debug; |
|
31
|
|
|
|
|
|
|
$self->parent->child_adjusted_height($self); |
|
32
|
|
|
|
|
|
|
} |
|
33
|
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
}; |
|
35
|
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
around 'render' => sub{ |
|
37
|
|
|
|
|
|
|
my ($orig, $self) = @_; |
|
38
|
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
my $text = $self->prepare_text; |
|
40
|
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
my $wrapped_lines = $self->wrapped_lines([@{$self->value}], $self->width); |
|
42
|
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
my $longest_line_length = $self->longest_line($wrapped_lines); |
|
44
|
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
my $font = $self->get_font; |
|
46
|
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
my $x = $self->content_left; |
|
48
|
|
|
|
|
|
|
my $y = $self->baseline_top($font, $self->size); |
|
49
|
|
|
|
|
|
|
my $align_method = 'text'; |
|
50
|
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
foreach($self->align || ()){ |
|
52
|
|
|
|
|
|
|
/^rig/ && do { $x = $self->content_right; $align_method = 'text_right' }; |
|
53
|
|
|
|
|
|
|
/^cen/ && do { $x += ($self->width/2); $align_method = 'text_center' }; |
|
54
|
|
|
|
|
|
|
} |
|
55
|
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
$text->translate($x,$y); |
|
57
|
|
|
|
|
|
|
foreach(@$wrapped_lines){ |
|
58
|
|
|
|
|
|
|
$text->$align_method( $_ ); |
|
59
|
|
|
|
|
|
|
$text->cr; |
|
60
|
|
|
|
|
|
|
} |
|
61
|
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
$self->$orig(); |
|
63
|
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
}; |
|
65
|
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub find_smallest_block{ |
|
70
|
0
|
|
|
0
|
0
|
|
my ($self, $space) = @_; |
|
71
|
0
|
0
|
|
|
|
|
return if $space > 1000; |
|
72
|
0
|
|
|
|
|
|
my $wrapped_lines = $self->wrapped_lines([@{$self->value}], $space); |
|
|
0
|
|
|
|
|
|
|
|
73
|
0
|
|
|
|
|
|
my $width = $self->longest_line($wrapped_lines); |
|
74
|
0
|
|
|
|
|
|
my $height = $self->lead * scalar @$wrapped_lines; |
|
75
|
0
|
0
|
0
|
|
|
|
warn sprintf "Text %s find_smallest_block: %s x %s\n", $self->name, $width, $height if $self->debug && $self->name; |
|
76
|
0
|
|
|
|
|
|
return ($width, $height); # if 1 - $width/($height || 1) < .2; |
|
77
|
|
|
|
|
|
|
} |
|
78
|
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
sub longest_line{ |
|
80
|
0
|
|
|
0
|
0
|
|
my ($self, $lines) = @_; |
|
81
|
0
|
|
|
|
|
|
my $text = $self->prepare_text; |
|
82
|
0
|
|
|
|
|
|
my $longest_line_length = 0; |
|
83
|
0
|
|
|
|
|
|
foreach my $line (@$lines){ |
|
84
|
0
|
|
|
|
|
|
my $len = $text->advancewidth($line); |
|
85
|
0
|
0
|
|
|
|
|
$longest_line_length = $len if $len > $longest_line_length; |
|
86
|
|
|
|
|
|
|
} |
|
87
|
0
|
|
|
|
|
|
return $longest_line_length; |
|
88
|
|
|
|
|
|
|
} |
|
89
|
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub wrapped_lines{ |
|
91
|
0
|
|
|
0
|
0
|
|
my ($self, $lines, $space) = @_; |
|
92
|
0
|
|
|
|
|
|
my @wrapped_lines; |
|
93
|
0
|
|
|
|
|
|
my $text = $self->prepare_text; |
|
94
|
0
|
|
|
|
|
|
foreach my $line (@$lines){ |
|
95
|
0
|
|
|
|
|
|
my $len = $text->advancewidth($line); |
|
96
|
0
|
0
|
|
|
|
|
if ($len > $space){ |
|
97
|
0
|
|
|
|
|
|
my $wrapped_lines = $self->split_line($line, $space); |
|
98
|
0
|
|
|
|
|
|
push(@wrapped_lines, @$wrapped_lines); |
|
99
|
|
|
|
|
|
|
} else { |
|
100
|
0
|
|
|
|
|
|
push(@wrapped_lines, $line); |
|
101
|
|
|
|
|
|
|
} |
|
102
|
|
|
|
|
|
|
} |
|
103
|
0
|
|
|
|
|
|
return (\@wrapped_lines); |
|
104
|
|
|
|
|
|
|
} |
|
105
|
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
sub split_line{ |
|
107
|
0
|
|
|
0
|
0
|
|
my ($self, $line, $width) = @_; |
|
108
|
0
|
0
|
|
|
|
|
my @words = ref $line ? @$line : split(/\s+/, $line); |
|
109
|
0
|
|
|
|
|
|
my @wrapped_lines; |
|
110
|
0
|
|
|
|
|
|
my $text = $self->prepare_text; |
|
111
|
0
|
|
|
|
|
|
while (@words){ |
|
112
|
0
|
|
|
|
|
|
my $new_line = shift @words; |
|
113
|
0
|
|
0
|
|
|
|
while (@words && $text->advancewidth($new_line.' '.$words[0]) < $width){ |
|
114
|
0
|
0
|
|
|
|
|
last unless @words; |
|
115
|
0
|
|
|
|
|
|
$new_line .= ' ' . shift @words; |
|
116
|
|
|
|
|
|
|
} |
|
117
|
0
|
|
|
|
|
|
push(@wrapped_lines, $new_line); |
|
118
|
|
|
|
|
|
|
} |
|
119
|
0
|
|
|
|
|
|
return \@wrapped_lines; |
|
120
|
|
|
|
|
|
|
} |
|
121
|
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable; |
|
124
|
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
1; |
|
126
|
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
__END__ |
|
129
|
|
|
|
|
|
|
=pod |
|
130
|
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=head1 NAME |
|
132
|
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
PDF::Boxer::Content::Text - a box that displays text |
|
134
|
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=head1 VERSION |
|
136
|
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
version 0.004 |
|
138
|
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
=head1 AUTHOR |
|
140
|
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
Jason Galea <lecstor@cpan.org> |
|
142
|
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
|
144
|
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
This software is copyright (c) 2012 by Jason Galea. |
|
146
|
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
|
148
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
|
149
|
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=cut |
|
151
|
|
|
|
|
|
|
|