File Coverage

blib/lib/Khonsu/Text.pm
Criterion Covered Total %
statement 68 68 100.0
branch 29 32 90.6
condition 14 17 82.3
subroutine 6 6 100.0
pod 0 2 0.0
total 117 125 93.6


line stmt bran cond sub pod time code
1             package Khonsu::Text;
2              
3 5     5   31 use parent 'Khonsu::Ra';
  5         9  
  5         32  
4              
5             sub attributes {
6 398     398 0 586 my $a = shift;
7             return (
8             text => {$a->RW, $a->STR},
9             overflow => {$a->RW, $a->BOOL},
10 398     398   912 indent => {$a->RW, $a->NUM, default => sub { 0 }},
11             pad => {$a->RW, $a->STR},
12             pad_end => {$a->RW, $a->STR},
13 398     398   990 align => {$a->RW, $a->STR, default => sub { 'left' }},
14             end_w => {$a->RW, $a->NUM},
15 398     398   829 margin => {$a->RW, $a->NUM, default => sub { 5 }},
16 398         17831 $a->FONT,
17             $a->POINTS
18             );
19             }
20              
21             sub add {
22 1084     1084 0 5832 my ($self, $file, %attributes) = @_;
23            
24 1084 100       4854 my $font = $self->font->load($file, %{ delete $attributes{font} || {} });
  1084         7276  
25              
26 1084 100 100     5373 if (!$attributes{x} && !$attributes{align}) {
27 34         184 $attributes{x} = $file->page->x;
28 34   33     262 $attributes{y} = $attributes{y} || $file->page->y;
29 34         182 $attributes{h} = $file->page->remaining_height();
30 34         171 $attributes{w} = $file->page->width();
31             }
32            
33 1084         5055 $self->set_attributes(%attributes);
34              
35 1084         5330 my $size = $self->font->size;
36            
37 1084         5558 my $text = $file->page->current->text;
38            
39 1084         345074 $text->font($font, $size);
40            
41 1084         210822 $text->fillcolor($self->font->colour);
42              
43 1084         160858 my (%pos) = (
44             l => $self->font->line_height,
45             $self->get_points()
46             );
47              
48 1084         5513 my $ypos = $file->page->h - ($pos{y} + $pos{l});
49            
50 1084         2579 my $max_height = $ypos - $pos{h};
51            
52 1084         1707 my $xpos = $pos{x};
53              
54 1084         4923 my $indent = $self->indent;
55            
56 1084         4811 my @paragraphs = split /\n/, $self->text;
57 1084         3935 my $space = $text->advancewidth(" ");
58              
59 1084         88017 my $next_line = shift @paragraphs;
60 1084         15054 my @words = split(/ /, $next_line);
61 1084 100 66     20186 push @words, $1 if $next_line !~ m/^\s+$/ && $next_line =~ m/(\s+)$/;
62 1084 50       2670 push @words, $next_line unless scalar @words;
63              
64 1084 100       2203 if ($indent) {
65 289         927 unshift @words, map { " " } 0 .. $indent;
  2011         3819  
66             }
67            
68 1084   100     4681 while ($ypos - $pos{l} >= $max_height && @words) {
69 2419         4559 my $word = shift @words;
70 2419         4869 $word =~ s/\t/ /g;
71 2419         6090 my ($width, @line) = ($text->advancewidth($word), ());
72 2419   100     271990 while ($width <= $pos{w} && defined $word) {
73 39345         3489347 push @line, $word;
74 39345         60666 $word = shift @words;
75 39345 100       67893 if (defined $word) {
76 38280 100       86891 $width += $space unless $word =~ m/^(\s+)$/;
77 38280         54389 $word =~ s/\t/ /g;
78 38280         75787 $width += $text->advancewidth($word);
79             }
80             }
81 2419         150836 $self->end_w($xpos + $width);
82 2419 100       7975 if ($word) {
    100          
83 1354 50       3747 unshift @words, $word if (scalar @words);
84             } elsif ($self->pad) {
85 348         1414 my $pad_width = $text->advancewidth($self->pad);
86 348 100       25010 my $pad_end_width = $self->pad_end ? $text->advancewidth($self->pad_end) : 0;
87 348         28836 my $left = int(($pos{w} - ($width + $pad_end_width)) / $pad_width);
88 348 50       1829 push @line, $self->pad x $left if $left;
89 348 100       1444 push @line, $self->pad_end if $self->pad_end;
90 348         803 $width = $pos{w};
91             }
92              
93 2419         9377 my $align = $self->align;
94 2419 100       7212 if ($align eq 'center') {
    100          
95 172         520 $xpos = $xpos + ((($pos{w} - $width) / 2) - ($width / 2));
96             } elsif ($align eq 'right') {
97 86         250 $xpos = $xpos + ($pos{w} - $width);
98 86         333 $self->end_w($xpos + $pos{w});
99             }
100              
101 2419         7543 $text->translate($xpos, $ypos);
102 2419         1139532 $text->text(join(" ", @line));
103 2419         1534294 $ypos -= $pos{l};
104             }
105              
106 1084         6677 $file->page->y($file->page->h - (($ypos + $pos{l}) - $self->margin));
107              
108 1084 100 100     5719 if (!$self->overflow && scalar @words) {
109 17         96 $file->page->next($file);
110 17         2676 return $self->add($file, text => join " ", @words);
111             }
112              
113 1067         8670 return $file;
114             }
115              
116             1;