Skip to content

Commit

Permalink
SDLx::Text word wrapping support
Browse files Browse the repository at this point in the history
  • Loading branch information
garu committed Oct 4, 2011
1 parent f737c5b commit 0b0fa51
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions lib/SDLx/Text.pm
Expand Up @@ -59,6 +59,9 @@ sub new {
$self->underline($options{'underline'}) if exists $options{'underline'};
$self->strikethrough($options{'strikethrough'}) if exists $options{'strikethrough'};

# word wrapping
$self->{word_wrap} = $options{'word_wrap'} || 0;

$self->text( $options{'text'} ) if exists $options{'text'};

return $self;
Expand Down Expand Up @@ -228,6 +231,7 @@ sub text {
return $self->{text} if scalar @_ == 1;

if ( defined $text ) {
$text = $self->_word_wrap($text) if $self->{word_wrap};
my $font = $self->{_font};
my $surface = _get_surfaces_for($font, $text, $self->{_color} )
or Carp::croak 'TTF rendering error: ' . SDL::get_error;
Expand Down Expand Up @@ -269,6 +273,42 @@ sub _get_surfaces_for {
return \@surfaces;
}

sub _word_wrap {
my ($self, $text) = @_;

my $maxlen = $self->{word_wrap};
my $font = $self->{_font};

# code heavily based on Text::Flow::Wrap
my @paragraphs = split /\n/ => $text;
my @output;

foreach my $paragraph (@paragraphs) {
my @paragraph_output = ('');
my @words = split /\s+/ => $paragraph;

foreach my $word (@words) {
my $padded = $word . q[ ];
my $candidate = $paragraph_output[-1] . $padded;
my ($w) = @{ SDL::TTF::size_utf8($font, $candidate) };
if ($w < $maxlen) {
$paragraph_output[-1] = $candidate;
}
else {
push @paragraph_output, $padded;
}
}
chop $paragraph_output[-1] if substr( $paragraph_output[-1], -1, 1 ) eq q[ ];

push @output, \@paragraph_output;

}

return join "\n" => map {
join "\n" => @$_
} @output;
}

sub surface {
return $_[0]->{surface};
}
Expand Down

0 comments on commit 0b0fa51

Please sign in to comment.