| 1 | package Video::PlaybackMachine::FillProducer::TextFrame::Column; | ||
| 2 | |||
| 3 | our $VERSION = '0.09'; # VERSION | ||
| 4 | |||
| 5 | use Moo; | ||
| 6 | |||
| 7 | has 'image' => ( 'is' => 'ro' ); | ||
| 8 | |||
| 9 | has 'text' => ( 'is' => 'ro' ); | ||
| 10 | |||
| 11 | # TODO: Make 'wrap_width' a parameter to 'new' | ||
| 12 | |||
| 13 | sub draw { | ||
| 14 | my $self = shift; | ||
| 15 | my ($x, $y, $width) = @_; | ||
| 16 | |||
| 17 | my $y_curr = $y; | ||
| 18 | foreach my $line ( $self->get_lines($width) ) { | ||
| 19 | chomp($line); | ||
| 20 | my ($width, $height) = $self->{'image'}->get_text_size($line); | ||
| 21 | $self->{'image'}->draw_text($x, $y_curr, $line); | ||
| 22 | $y_curr += $height; | ||
| 23 | } | ||
| 24 | } | ||
| 25 | |||
| 26 | sub get_dimensions { | ||
| 27 | my $self = shift; | ||
| 28 | my ($wrap_width) = @_; | ||
| 29 | my ($width, $height) = $self->_wrap_lines($wrap_width); | ||
| 30 | return $width, $height; | ||
| 31 | } | ||
| 32 | |||
| 33 | |||
| 34 | sub get_lines { | ||
| 35 | my $self = shift; | ||
| 36 | my ($wrap_width) = @_; | ||
| 37 | my (undef, undef, @lines) = $self->_wrap_lines($wrap_width); | ||
| 38 | return @lines; | ||
| 39 | } | ||
| 40 | |||
| 41 | |||
| 42 | sub _wrap_lines { | ||
| 43 | my $self = shift; | ||
| 44 | my ($wrap_width) = @_; | ||
| 45 | |||
| 46 | |||
| 47 | my @lines = (); | ||
| 48 | my $total_height = 0; | ||
| 49 | my $max_width = 0; | ||
| 50 | |||
| 51 | foreach my $text ( split(/\n/, $self->text() ) ) { | ||
| 52 | |||
| 53 | my @atoms = split(/(\s+)/, $text); | ||
| 54 | |||
| 55 | my $curr_line = shift @atoms; | ||
| 56 | defined $curr_line or $curr_line = ''; | ||
| 57 | my ($line_width, $line_height) = $self->{'image'}->get_text_size($curr_line); | ||
| 58 | $total_height += $line_height; | ||
| 59 | |||
| 60 | |||
| 61 | foreach my $atom (@atoms) { | ||
| 62 | my ($width, $height) = $self->{'image'}->get_text_size($atom); | ||
| 63 | |||
| 64 | # If the current atom makes the line wrap, do it | ||
| 65 | if ( ( $line_width + $width ) > $wrap_width ) { | ||
| 66 | push(@lines, $curr_line); | ||
| 67 | $curr_line = $atom; | ||
| 68 | $max_width = $line_width if $line_width > $max_width; | ||
| 69 | $line_width = $width; | ||
| 70 | $total_height += $height; | ||
| 71 | } | ||
| 72 | # Otherwise, append atom to current line | ||
| 73 | else { | ||
| 74 | $curr_line .= $atom; | ||
| 75 | $line_width += $width; | ||
| 76 | } | ||
| 77 | } | ||
| 78 | push(@lines, $curr_line); | ||
| 79 | $max_width = $line_width if $line_width > $max_width; | ||
| 80 | } | ||
| 81 | |||
| 82 | return $max_width, $total_height, @lines; | ||
| 83 | |||
| 84 | } | ||
| 85 | |||
| 86 | no Moo; | ||
| 87 | |||
| 88 | 1; |