File Coverage

blib/lib/PDF/Make/Builder/Layout/Row.pm
Criterion Covered Total %
statement 56 56 100.0
branch 14 16 87.5
condition 9 12 75.0
subroutine 7 7 100.0
pod 0 2 0.0
total 86 93 92.4


line stmt bran cond sub pod time code
1             package PDF::Make::Builder::Layout::Row;
2 42     42   192 use strict;
  42         66  
  42         1103  
3 42     42   130 use warnings;
  42         68  
  42         1646  
4 42     42   195 use Object::Proto;
  42         77  
  42         1972  
5 42     42   15649 use PDF::Make::Builder::Layout::Cell;
  42         88  
  42         1965  
6              
7             BEGIN {
8 42     42   1457 Object::Proto::define('PDF::Make::Builder::Layout::Row',
9             'layout:required',
10             'height:Num',
11             'margin:Num:default(5)',
12             'gap:Num:default(0)',
13             'cells:ArrayRef:default([])',
14             );
15 42         21222 Object::Proto::import_accessors('PDF::Make::Builder::Layout::Row');
16             }
17              
18             sub cell {
19 24     24 0 741 my ($self, %args) = @_;
20 24         31 my $cells = cells $self;
21             my %cell_args = (
22             row => $self,
23             weight => $args{weight} // 1,
24             align => $args{align} // 'left',
25             valign => $args{valign} // 'top',
26             pad => $args{pad} // 5,
27             text_border_width => $args{text_border_width} // 0.5,
28 24   100     236 wrap_slack => $args{wrap_slack} // 0,
      100        
      50        
      100        
      50        
      50        
29             );
30 24 100       42 $cell_args{bg} = $args{bg} if defined $args{bg};
31 24 100       41 $cell_args{border} = $args{border} if defined $args{border};
32 24 50       30 $cell_args{text_border} = $args{text_border} if defined $args{text_border};
33 24         160 push @$cells, PDF::Make::Builder::Layout::Cell->new(%cell_args);
34 24         86 return $cells->[-1];
35             }
36              
37             sub render {
38 13     13 0 24 my ($self, $builder, $page) = @_;
39 13         15 my @cells = @{cells $self};
  13         23  
40 13 50       32 return unless @cells;
41              
42 13         31 my $canvas = $page->canvas;
43 13         28 my $font = $builder->font;
44 13         29 my $cx = $page->content_x;
45 13         28 my $total_w = $page->width;
46 13         25 my $cursor = $page->cursor_y;
47 13         14 my $margin = margin $self;
48              
49 13         16 my $total_weight = 0;
50 13         36 $total_weight += $_->weight for @cells;
51 13         17 my $gap = gap $self;
52 13         25 my $available = $total_w - ($#cells * $gap);
53              
54 13         16 my $row_h = height $self;
55 13 100       31 unless ($row_h) {
56 5         7 $row_h = 0;
57 5         8 for my $cell (@cells) {
58 9         39 my $ch = $cell->measure_height($font, $available * $cell->weight / $total_weight);
59 9 100       21 $row_h = $ch if $ch > $row_h;
60             }
61 5         7 $row_h += 10;
62             }
63              
64 13         18 my $cell_y = $cursor - $row_h;
65 13         16 my $cell_x = $cx;
66              
67 13         23 for my $cell (@cells) {
68 24         46 my $cell_w = $available * $cell->weight / $total_weight;
69              
70 24 100       49 if (defined $cell->bg) {
71 9         28 my ($r, $g, $b) = $font->hex_to_rgb($cell->bg);
72 9         172 $canvas->q->rg($r, $g, $b)->re($cell_x, $cell_y, $cell_w, $row_h)->f->Q;
73             }
74              
75 24 100       65 if (defined $cell->border) {
76 8         28 my ($r, $g, $b) = $font->hex_to_rgb($cell->border);
77 8         127 $canvas->q->w(0.5)->RG($r, $g, $b)
78             ->re($cell_x + 0.25, $cell_y + 0.25, $cell_w - 0.5, $row_h - 0.5)->S->Q;
79             }
80              
81 24         145 $cell->render_content($canvas, $font, $page,
82             $cell_x + $cell->pad, $cell_y, $cell_w - 2 * $cell->pad, $row_h);
83              
84 24         55 $cell_x += $cell_w + $gap;
85             }
86              
87 13         36 $page->advance_y($row_h + $margin);
88             }
89              
90             1;