GDTextUtil

 view release on metacpan or  search on metacpan

Text.pm  view on Meta::CPAN


This module provides a font-independent way of dealing with text in
GD, for use with the GD::Text::* modules and GD::Graph.

=head1 NOTES

As with all Modules for Perl: Please stick to using the interface. If
you try to fiddle too much with knowledge of the internals of this
module, you could get burned. I may change them at any time.

You can only use TrueType fonts with version of GD > 1.20, and then
only if compiled with support for this. If you attempt to do it
anyway, you will get errors.

If you want to refer to builtin GD fonts by their short name
(C<gdTinyFont>, C<gdGiantFont>), you will need to C<use> the GD module
as well as one the GD::Text modules, because it is GD that exports
those names into your name space. If you don't like that, use the
longer alternatives (C<GD::Font->Giant>) instead.

=head1 METHODS

=cut

use strict;

use GD;
use Carp;
use Cwd;

use vars qw($FONT_PATH @FONT_PATH $OS);
BEGIN
{
    $FONT_PATH = $ENV{FONT_PATH}     || 
                 $ENV{TTF_FONT_PATH} ||
                 $ENV{TT_FONT_PATH}  || '';
    unless ($OS = $^O)
    {
        require Config;
        $OS = $Config::Config{'os_name'};
    }
}

my $ERROR;

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

Create a new object. See the C<set()> method for attributes.

=cut

sub new
{
    my $proto = shift;
    my $class = ref($proto) || $proto;
    my $self = { 
            type   => 'builtin',
            font   => gdSmallFont,
            ptsize => 10,
        };
    bless $self => $class;
    $self->set(@_) or return;
    return $self
}

=head2 GD::Text::error() or $gd_text->error();

Return the last error that occured in the class. This may be
imperfect.

=cut

# XXX This sucks! fix it
sub error { $ERROR };

sub _set_error { $ERROR = shift };

=head2 $gd_text->set_font( font, size )

Set the font to use for this string. The arguments are either a GD
builtin font (like gdSmallFont or GD::Font->Small) or the name of a
TrueType font file and the size of the font to use. See also
L<"font_path">. 

If you are not using an absolute path to the font file, you can leave of
the .ttf file extension, but you have to append it for absolute paths:

  $gd_text->set_font('arial', 12);
  # but
  $gd_text->set_font('/usr/fonts/arial.ttf', 12);

The first argument can be a reference to an array of fonts. The first
font from the array that can be found will be used. This allows you to
do something like

  $gd_text->font_path( '/usr/share/fonts:/usr/fonts');
  $gd_text->set_font(
    ['verdana', 'arial', gdMediumBoldFont], 14);

if you'd prefer verdana to be used, would be satisfied with arial, but
if none of that is available just want to make sure you can fall
back on something that will be available. 

Returns true on success, false on error.

=cut

sub set_font
{
    my $self = shift;
    my $fonts = shift;
    my $size = shift;

    # Make sure we have a reference to an array
    $fonts = [$fonts] unless ref($fonts) eq 'ARRAY';

    foreach my $font (@{$fonts})
    {
        my $rc = ref($font) && $font->isa('GD::Font') ?
            $self->_set_builtin_font($font) :
            $self->_set_TTF_font($font, $size || $self->{ptsize}) ;

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

( run in 0.490 second using v1.00-cache-2.02-grep-82fe00e-cpan-1925d2aa809 )