« lib/Video/PlaybackMachine/DirectoryScanner.pm
↑ Summary
lib/Video/PlaybackMachine/FillProducer/TextFrame/TextTable.pm »
| 1 | package Video::PlaybackMachine::FillProducer::TextFrame; | ||
| 2 | |||
| 3 | our $VERSION = '0.09'; # VERSION | ||
| 4 | |||
| 5 | #### | ||
| 6 | #### Video::PlaybackMachine::FillProducer::TextFrame | ||
| 7 | #### | ||
| 8 | #### $Revision$ | ||
| 9 | #### | ||
| 10 | |||
| 11 | use Moo; | ||
| 12 | use Carp; | ||
| 13 | |||
| 14 | use POE; | ||
| 15 | |||
| 16 | use Image::Imlib2; | ||
| 17 | use File::Temp qw(tempfile); | ||
| 18 | use POSIX qw(strftime); | ||
| 19 | |||
| 20 | with 'Video::PlaybackMachine::FillProducer::AbstractStill', | ||
| 21 | 'Video::PlaybackMachine::Logger'; | ||
| 22 | |||
| 23 | ############################# Parameters ############################# | ||
| 24 | |||
| 25 | has 'width' => ( | ||
| 26 | 'is' => 'ro', | ||
| 27 | 'default' => 800 | ||
| 28 | ); | ||
| 29 | |||
| 30 | has 'height' => ( | ||
| 31 | 'is' => 'ro', | ||
| 32 | 'default' => 600 | ||
| 33 | ); | ||
| 34 | |||
| 35 | has 'background_color' => ( | ||
| 36 | 'is' => 'ro', | ||
| 37 | 'default' => sub { return [ 0,0,100,255 ] } | ||
| 38 | ); | ||
| 39 | |||
| 40 | has 'text_color' => ( | ||
| 41 | 'is' => 'ro', | ||
| 42 | 'default' => sub { return [0,255,255,255] } | ||
| 43 | ); | ||
| 44 | |||
| 45 | has 'font_path' => ( | ||
| 46 | 'is' => 'ro', | ||
| 47 | 'default' => sub { return [ qw(/usr/share/fonts/truetype/ubuntu-font-family) ] } | ||
| 48 | ); | ||
| 49 | |||
| 50 | has 'font' => ( | ||
| 51 | 'is' => 'ro', | ||
| 52 | 'default' => 'FreeMono' | ||
| 53 | ); | ||
| 54 | |||
| 55 | has 'font_size' => ( | ||
| 56 | 'is' => 'ro', | ||
| 57 | 'default' => 40 | ||
| 58 | ); | ||
| 59 | |||
| 60 | |||
| 61 | ############################# Object Methods ############################## | ||
| 62 | |||
| 63 | |||
| 64 | ## | ||
| 65 | ## start() | ||
| 66 | ## | ||
| 67 | sub start { | ||
| 68 | my $self = shift; | ||
| 69 | |||
| 70 | my $image = $self->create_image() | ||
| 71 | or die "Couldn't create image for some reason"; | ||
| 72 | |||
| 73 | $self->add_text($image); | ||
| 74 | |||
| 75 | my ($fh, $filename) = tempfile( SUFFIX => '.png'); | ||
| 76 | $image->save($filename); | ||
| 77 | |||
| 78 | # Scurvy trick-- passing the filehandle as an unused argument so that | ||
| 79 | # it will survive as long as the event does. | ||
| 80 | $poe_kernel->post('Player', 'play_still', $filename, undef, undef, $fh); | ||
| 81 | $poe_kernel->delay('next_fill', , $self->time_layout()->preferred_time()); | ||
| 82 | |||
| 83 | } | ||
| 84 | |||
| 85 | sub get_font_string { | ||
| 86 | my $self = shift; | ||
| 87 | return $self->font() . '/' . $self->font_size(); | ||
| 88 | } | ||
| 89 | |||
| 90 | |||
| 91 | ## | ||
| 92 | ## create_image() | ||
| 93 | ## | ||
| 94 | sub create_image { | ||
| 95 | my $self = shift; | ||
| 96 | |||
| 97 | my $image = Image::Imlib2->new($self->width, $self->height); | ||
| 98 | |||
| 99 | $image->set_color(@{ $self->background_color }); | ||
| 100 | $image->fill_rectangle(0,0,$self->width() ,$self->height()); | ||
| 101 | |||
| 102 | $image->set_color(@{ $self->text_color() }); | ||
| 103 | $image->add_font_path(@{ $self->font_path() }); | ||
| 104 | $image->load_font($self->get_font_string() ); | ||
| 105 | |||
| 106 | return $image; | ||
| 107 | } | ||
| 108 | |||
| 109 | sub measure_block { | ||
| 110 | my $self = shift; | ||
| 111 | my ($image, @lines) = @_; | ||
| 112 | |||
| 113 | my $max = 0; | ||
| 114 | my $total = 0; | ||
| 115 | foreach my $line (@lines) { | ||
| 116 | my ($width, $height) = $image->get_text_size($line); | ||
| 117 | $max = $width if $width > $max; | ||
| 118 | $total += $height; | ||
| 119 | } | ||
| 120 | return ($max,$total); | ||
| 121 | } | ||
| 122 | |||
| 123 | sub max_width { | ||
| 124 | my $self = shift; | ||
| 125 | my ($image, @lines) = @_; | ||
| 126 | |||
| 127 | my ($max, undef) = $self->measure_block($image, @lines); | ||
| 128 | return $max; | ||
| 129 | |||
| 130 | } | ||
| 131 | |||
| 132 | sub total_height { | ||
| 133 | my $self = shift; | ||
| 134 | my ($image, @lines) = @_; | ||
| 135 | |||
| 136 | my (undef, $total) = $self->measure_block($image, @lines); | ||
| 137 | return $total; | ||
| 138 | } | ||
| 139 | |||
| 140 | sub write_block { | ||
| 141 | my $self = shift; | ||
| 142 | my ($image, $x, $y, @lines) = @_; | ||
| 143 | |||
| 144 | my $y_curr = $y; | ||
| 145 | my $max_width = 0; | ||
| 146 | foreach my $line (@lines) { | ||
| 147 | chomp($line); | ||
| 148 | my $y_next = $y_curr; | ||
| 149 | my ($width, $height) = $image->get_text_size($line); | ||
| 150 | $y_next += $height; | ||
| 151 | $width > $max_width and $max_width = $width; | ||
| 152 | last if ($y_next > $image->get_height()); | ||
| 153 | $image->draw_text($x, $y_curr, $line); | ||
| 154 | $y_curr = $y_next; | ||
| 155 | } | ||
| 156 | |||
| 157 | return ($x + $max_width, $y_curr); | ||
| 158 | } | ||
| 159 | |||
| 160 | sub write_centered { | ||
| 161 | my $self = shift; | ||
| 162 | my ($image, $text) = @_; | ||
| 163 | |||
| 164 | my ($words_height, @lines) = wrap_words($image, $text); | ||
| 165 | my $start_height = ( $self->height() - $words_height ) / 2; | ||
| 166 | $self->draw_centered($image, $start_height, @lines); | ||
| 167 | |||
| 168 | } | ||
| 169 | |||
| 170 | sub wrap_words { | ||
| 171 | my ($image, $in_text, $wrap_width) = @_; | ||
| 172 | |||
| 173 | defined $wrap_width | ||
| 174 | or $wrap_width = $image->get_width(); | ||
| 175 | |||
| 176 | my @lines = (); | ||
| 177 | my $total_height = 0; | ||
| 178 | |||
| 179 | foreach my $text ( split(/\n/, $in_text) ) { | ||
| 180 | |||
| 181 | my @atoms = split(/(\s+)/, $text); | ||
| 182 | |||
| 183 | my $curr_line = shift @atoms; | ||
| 184 | defined $curr_line or $curr_line = ''; | ||
| 185 | my ($line_width, $line_height) = $image->get_text_size($curr_line); | ||
| 186 | $total_height += $line_height; | ||
| 187 | |||
| 188 | foreach my $atom (@atoms) { | ||
| 189 | my ($width, $height) = $image->get_text_size($atom); | ||
| 190 | if ( ( $line_width + $width ) > $wrap_width ) { | ||
| 191 | push(@lines, $curr_line); | ||
| 192 | $curr_line = $atom; | ||
| 193 | $line_width = $width; | ||
| 194 | $total_height += $height; | ||
| 195 | } | ||
| 196 | else { | ||
| 197 | $curr_line .= $atom; | ||
| 198 | $line_width += $width; | ||
| 199 | } | ||
| 200 | } | ||
| 201 | push(@lines, $curr_line); | ||
| 202 | } | ||
| 203 | return $total_height, @lines; | ||
| 204 | |||
| 205 | } | ||
| 206 | |||
| 207 | sub draw_centered { | ||
| 208 | my $self = shift; | ||
| 209 | my ($image, $starty, @lines) = @_; | ||
| 210 | |||
| 211 | my $y = $starty; | ||
| 212 | |||
| 213 | foreach my $line (@lines) { | ||
| 214 | my @words = split(/(\s+)/, $line); | ||
| 215 | my ($width, $height) = $image->get_text_size($line, TEXT_TO_RIGHT, 0); | ||
| 216 | my $x = ($image->get_width() - $width) / 2; | ||
| 217 | $image->draw_text($x, $y, $line); | ||
| 218 | $y += $height; | ||
| 219 | } | ||
| 220 | |||
| 221 | return $y; | ||
| 222 | } | ||
| 223 | |||
| 224 | no Moo; | ||
| 225 | |||
| 226 | 1; |