GDTextUtil

 view release on metacpan or  search on metacpan

Text/Wrap.pm  view on Meta::CPAN

      line_space  => 4,
      color       => $black,
      text        => $text,
  );
  $wrapbox->set_font(gdMediumBoldFont);
  $wrapbox->set_font('arial', 12);
  $wrapbox->set(align => 'left', width => 120);
  $wrapbox->draw(10,140);

  $gd->rectangle($wrap_box->get_bounds(10,140), $color);

=head1 DESCRIPTION

GD::Text::Wrap provides an object that draws a formatted paragraph of
text in a box on a GD::Image canvas, using either a builtin GD font
or a TrueType font.

=head1 METHODS

This class doesn't inherit from GD::Text directly, but delegates most of
its work to it (in fact to a GD::Text::Align object. That means that
most of the GD::Text::Align methods are available for this class,
specifically C<set_font> and C<font_path>. Other methods and methods
with a different interface are described here:

=cut

use strict;

# XXX add version number to GD
use GD;
use GD::Text::Align;
use Carp;

my %attribs = (
    width       => undef,
    height      => undef,
    line_space  => 2,
    para_space  => 0,
    align       => 'justified',
    text        => undef,
    preserve_nl => 0,
);

=head2 GD::Text::Wrap->new( $gd_object, attribute => value, ... )

Create a new object. The first argument to new has to be a valid
GD::Image object. The other arguments will be passed to the set() method
for initialisation of the attributes.

=cut

sub new
{
    my $proto = shift;
    my $class = ref($proto) || $proto;
    my $gd    = shift;
    ref($gd) and $gd->isa('GD::Image') 
        or croak "Not a GD::Image object";
    my $self  = { gd => $gd };
    bless $self => $class;
    $self->_init();
    $self->set(@_);
    return $self
}

sub _init
{
    my $self = shift;

    $self->{render} = GD::Text::Align->new($self->{gd}, text => 'Foo');
    croak "Cannot allocate GD::Text::Align object" unless $self->{render};

    # XXX 5.004_04 doesn't like foreach as a modifier
    #$self->set($_, $attribs{$_}) foreach (keys %attribs);
    foreach (keys %attribs) { $self->set($_, $attribs{$_}) };
    # XXX SET DEFAULTS

    $self->set(
        colour => $self->{gd}->colorsTotal - 1,
        width  => ($self->{gd}->getBounds())[0] - 1,
    );
}

=head2 $wrapbox->set( attribute => value, ... )

set the value for an attribute. Valid attributes are:

=over 4

=item width

The width of the box to draw the text in. If unspecified, they will
default to the width of the GD::Image object.

=item line_space

The number of pixels between lines. Defaults to 2.

=item para_space, paragraph_space

The number of extra pixels between paragraphs, above line_space.
Defaults to 0.

=item color, colour

Synonyms. The colour to use when drawing the font. Will be initialised
to the last colour in the GD object's palette.

=item align, alignment

Synonyms. One of 'justified' (the default), 'left', 'right' or 'center'.

=item text

The text to draw. This is the only attribute that you absolutely have to
set.

=item preserve_nl

If set to a true value, newlines in the text will cause a line break.

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 0.765 second using v1.00-cache-2.02-grep-82fe00e-cpan-72ae3ad1e6da )