File Coverage

blib/lib/Khonsu/Text.pm
Criterion Covered Total %
statement 61 61 100.0
branch 22 24 91.6
condition 11 11 100.0
subroutine 6 6 100.0
pod 0 2 0.0
total 100 104 96.1


line stmt bran cond sub pod time code
1             package Khonsu::Text;
2              
3 5     5   34 use parent 'Khonsu::Ra';
  5         16  
  5         31  
4              
5             sub attributes {
6 398     398 0 558 my $a = shift;
7             return (
8             text => {$a->RW, $a->STR},
9             overflow => {$a->RW, $a->BOOL},
10 398     398   1015 indent => {$a->RW, $a->NUM, default => sub { 0 }},
11             pad => {$a->RW, $a->STR},
12             pad_end => {$a->RW, $a->STR},
13 398     398   1126 align => {$a->RW, $a->STR, default => sub { 'left' }},
14             end_w => {$a->RW, $a->NUM},
15 398     398   954 margin => {$a->RW, $a->NUM, default => sub { 5 }},
16 398         17807 $a->FONT,
17             $a->POINTS
18             );
19             }
20              
21             sub add {
22 1084     1084 0 6078 my ($self, $file, %attributes) = @_;
23            
24 1084 100       5191 my $font = $self->font->load($file, %{ delete $attributes{font} || {} });
  1084         8106  
25              
26 1084 100 100     6272 if (!$attributes{x} && !$attributes{align}) {
27 34         157 $attributes{x} = $file->page->x;
28 34         224 $attributes{y} = $file->page->y;
29 34         161 $attributes{h} = $file->page->remaining_height();
30 34         183 $attributes{w} = $file->page->width();
31             }
32            
33 1084         5674 $self->set_attributes(%attributes);
34              
35 1084         5763 my $size = $self->font->size;
36            
37 1084         5727 my $text = $file->page->current->text;
38            
39 1084         360041 $text->font($font, $size);
40            
41 1084         219434 $text->fillcolor($self->font->colour);
42              
43 1084         166031 my (%pos) = (
44             l => $self->font->line_height,
45             $self->get_points()
46             );
47              
48 1084         5652 my $ypos = $file->page->h - ($pos{y} + $pos{l});
49            
50 1084         2792 my $max_height = $ypos - $pos{h};
51            
52 1084         1933 my $xpos = $pos{x};
53              
54 1084         5074 my $indent = $self->indent;
55            
56 1084         5109 my @paragraphs = split /\n/, $self->text;
57            
58 1084         3840 my $space = $text->advancewidth(" ");
59              
60 1084         106583 my @words = split / /, shift @paragraphs;
61              
62 1084 100       3332 if ($indent) {
63 289         899 unshift @words, map { " " } 0 .. $indent;
  2011         4039  
64             }
65              
66 1084   100     5735 while ($ypos - $pos{l} >= $max_height && @words) {
67 2419         4873 my $word = shift @words;
68 2419         5763 my ($width, @line) = ($text->advancewidth($word), ());
69 2419   100     273805 while ($width <= $pos{w} && $word) {
70 39316         3476801 $width += $space;
71 39316         63881 push @line, $word;
72 39316         57172 $word = shift @words;
73 39316         74977 $width += $text->advancewidth($word);
74             }
75 2419         157956 $self->end_w($xpos + $width);
76 2419 100       8612 if ($word) {
    100          
77 1354 50       3828 unshift @words, $word if (scalar @words);
78             } elsif ($self->pad) {
79 348         1491 my $pad_width = $text->advancewidth($self->pad);
80 348 100       26236 my $pad_end_width = $self->pad_end ? $text->advancewidth($self->pad_end) : 0;
81 348         29707 my $left = int(($pos{w} - ($width + $pad_end_width)) / $pad_width);
82 348 50       2226 push @line, $self->pad x $left if $left;
83 348 100       1381 push @line, $self->pad_end if $self->pad_end;
84 348         825 $width = $pos{w};
85             }
86              
87 2419         9386 my $align = $self->align;
88 2419 100       7419 if ($align eq 'center') {
    100          
89 172         635 $xpos = $xpos + ((($pos{w} - $width) / 2) - ($width / 2));
90             } elsif ($align eq 'right') {
91 86         287 $xpos = $xpos + ($pos{w} - $width);
92             }
93              
94 2419         7673 $text->translate($xpos, $ypos);
95 2419         1148829 $text->text(join(" ", @line));
96 2419         1538638 $ypos -= $pos{l};
97             }
98              
99 1084         6751 $file->page->y($file->page->h - (($ypos + $pos{l}) - $self->margin));
100              
101 1084 100 100     5859 if (!$self->overflow && scalar @words) {
102 17         130 $file->page->next($file);
103 17         2580 return $self->add($file, text => join " ", @words);
104             }
105              
106 1067         9190 return $file;
107             }
108              
109             1;