GDTextUtil

 view release on metacpan or  search on metacpan

Text/Align.pm  view on Meta::CPAN

  use GD::Text::Align;

  my $gd = GD::Image->new(800,600);
  # allocate colours, do other things.

  my $align = GD::Text::Align->new($gd
    valign => 'top',
    halign => 'right',
  );
  $align->set_font('arial', 12);
  $align->set_text('some string');
  @bb = $align->bounding_box(200, 400, PI/3);
  # you can do things based on the bounding box here
  $align->draw(200, 400, PI/3);

=head1 DESCRIPTION

GD::Text::Align provides an object that draws a string aligned
to a coordinate at an angle.

For builtin fonts only two angles are valid: 0 and PI/2. All other
angles will be converted to one of these two.

=head1 METHODS

This class inherits everything from GD::Text. I will only discuss the
methods and attributes here that are not discussed there, or that have a
different interface or behaviour. Methods directly inherited include
C<set_text> and C<set_font>.

=cut

use strict;

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

@GD::Text::Align::ISA = qw(GD::Text);

=head2 GD::Text::Align->new($gd_object, attrib => value, ...)

Create a new object. The first argument to new has to be a valid
GD::Image object. The other arguments will be passed on to the set
method.

=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 = $class->SUPER::new() or return;
    $self->{gd} = $gd;
    $self->_init();
    $self->set(@_);
    bless $self => $class;
}

my %defaults = (
    halign  => 'left',
    valign  => 'base',
);

sub _init
{
    my $self = shift;
    while (my ($k, $v) = each(%defaults))
    {
        $self->{$k} = $v;
    }
    $self->{colour} = $self->{gd}->colorsTotal - 1,
}

=head2 $align->set(attrib => value, ...)

Set an attribute. Valid attributes are the ones discussed in
L<GD::Text> and:

=over 4

=item valign, halign

Vertical and horizontal alignment of the string. See also set_valign and
set_halign.

=item colour, color

Synonyms. The colour to use to draw the string. This should be the index
of the colour in the GD::Image object's palette. The default value is
the last colour in the GD object's palette at the time of the creation
of C<$align>.

=back

=cut

sub set
{
    my $self = shift;
    $@ = "Incorrect attribute list", return if @_%2;
    my %args = @_;
    my @super;

    foreach (keys %args)
    {
        /^valign/ and do {
            $self->set_valign($args{$_}); 
            next;
        };
        /^halign/ and do {
            $self->set_halign($args{$_}); 
            next;
        };
        /^colou?r$/ and do {
            $self->{colour} = $args{$_};
            next;

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

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