- changed README note on libgif bug to refer to t105gif.t instead
of t10formats.t
+0.39 pre1
+ - split Imager::Font into a base, *::Type1 and *::Truetype
+
=================================================================
For latest versions check the Imager-devel pages:
return;
}
- my $aa=1;
- my $font=$input{'font'};
- my $align=$font->{'align'} unless exists $input{'align'};
- my $color=$input{'color'} || $font->{'color'};
- my $size=$input{'size'} || $font->{'size'};
-
- if (!defined($size)) { $self->{ERRSTR}='No size parameter and no default in font'; return undef; }
-
- $aa=$font->{'aa'} if exists $font->{'aa'};
- $aa=$input{'aa'} if exists $input{'aa'};
-
-
-
-# unless($font->can('text')) {
-# $self->{ERRSTR}="font is unable to do what we need";
-# return;
-# }
-
-# use Data::Dumper;
-# warn Dumper($font);
-
-# print "Channel=".$input{'channel'}."\n";
-
- if ( $font->{'type'} eq 't1' ) {
- if ( exists $input{'channel'} ) {
- Imager::Font::t1_set_aa_level($aa);
- i_t1_cp($self->{IMG},$input{'x'},$input{'y'},
- $input{'channel'},$font->{'id'},$size,
- $input{'string'},length($input{'string'}),1);
- } else {
- Imager::Font::t1_set_aa_level($aa);
- i_t1_text($self->{IMG},$input{'x'},$input{'y'},
- $color,$font->{'id'},$size,
- $input{'string'},length($input{'string'}),1);
- }
- }
-
- if ( $font->{'type'} eq 'tt' ) {
- if ( exists $input{'channel'} ) {
- i_tt_cp($font->{'id'},$self->{IMG},$input{'x'},$input{'y'},$input{'channel'},
- $size,$input{'string'},length($input{'string'}),$aa);
- } else {
- i_tt_text($font->{'id'},$self->{IMG},$input{'x'},$input{'y'},$color,$size,
- $input{'string'},length($input{'string'}),$aa);
- }
- }
+ $input{font}->draw(image=>$self, %input);
return $self;
}
use Imager::Color;
use strict;
-use vars qw(%T1_Paths %TT_Paths %T1_Cache %TT_Cache $TT_CSize $TT_CSize $T1AA);
+use File::Spec;
# This class is a container
# and works for both truetype and t1 fonts.
-# $T1AA is in there because for some reason (probably cache related) antialiasing
-# is a system wide setting in t1 lib.
-
-sub t1_set_aa_level {
- if (!defined $T1AA or $_[0] != $T1AA) {
- Imager::i_t1_set_aa($_[0]);
- $T1AA=$_[0];
- }
-}
-
# search method
# 1. start by checking if file is the parameter
# 1a. if so qualify path and compare to the cache.
# here we should have the font type or be dead already.
if ($type eq 't1') {
- $id=Imager::i_t1_new($file);
+ require 'Imager/Font/Type1.pm';
+ return Imager::Font::Type1->new(%hsh);
}
if ($type eq 'tt') {
- $id=Imager::i_tt_new($file);
+ require 'Imager/Font/Truetype.pm';
+ return Imager::Font::Truetype->new(%hsh);
}
-
- $self->{'aa'}=$hsh{'aa'}||'0';
- $self->{'file'}=$file;
- $self->{'id'}=$id;
- $self->{'type'}=$type;
- $self->{'size'}=$hsh{'size'};
- $self->{'color'}=$hsh{'color'};
- return $self;
+ # it would be nice to have some generic mechanism to select the
+ # class
+
+ return undef;
}
+# returns first defined parameter
+sub _first {
+ for (@_) {
+ return $_ if defined $_;
+ }
+ return undef;
+}
-
-
-
+sub draw {
+ my $self = shift;
+ my %input = (x => 0, 'y' => 0, @_);
+ unless ($input{image}) {
+ $Imager::ERRSTR = 'No image supplied to $font->draw()';
+ return;
+ }
+ $input{string} = _first($input{string}, $input{text});
+ unless (defined $input{string}) {
+ $Imager::ERRSTR = "Missing require parameter 'string'";
+ return;
+ }
+ $input{aa} = _first($input{aa}, $input{antialias}, $self->{aa}, 1);
+ # the original draw code worked this out but didn't use it
+ $input{align} = _first($input{align}, $self->{align});
+ $input{color} = _first($input{color}, $self->{color});
+ $input{size} = _first($input{size}, $self->{size});
+ unless (defined $input{size}) {
+ $input{image}{ERRSTR} = "No font size provided";
+ return undef;
+ }
+ $input{align} = _first($input{align}, 1);
+ $self->_draw(%input);
+}
sub bounding_box {
my $self=shift;
my %input=@_;
- my @box;
-
- if (!exists $input{'string'}) { $Imager::ERRSTR='string parameter missing'; return; }
- if ($self->{type} eq 't1') {
- @box=Imager::i_t1_bbox($self->{id}, defined($input{size}) ? $input{size} :$self->{size},
- $input{string}, length($input{string}));
- }
- if ($self->{type} eq 'tt') {
- @box=Imager::i_tt_bbox($self->{id}, defined($input{size}) ? $input{size} :$self->{size},
- $input{string}, length($input{string}));
+ if (!exists $input{'string'}) {
+ $Imager::ERRSTR='string parameter missing';
+ return;
}
+ $input{size} ||= $self->{size};
+
+ my @box = $self->_bounding_box(%input);
if(exists $input{'x'} and exists $input{'y'}) {
my($gdescent, $gascent)=@box[1,3];
1;
-
-
__END__
=head1 NAME
And a great deal of help from others - see the README for a complete
list.
+=head1 BUGS
+
+You need to modify this class to add new font types.
+
=head1 SEE ALSO
Imager(3)
--- /dev/null
+package Imager::Font::Truetype;
+use strict;
+use vars qw(@ISA);
+@ISA = qw(Imager::Font);
+
+sub new {
+ my $class = shift;
+ my %hsh=(color=>Imager::Color->new(255,0,0,0),
+ size=>15,
+ @_);
+
+ unless ($hsh{file}) {
+ $Imager::ERRSTR = "No font file specified";
+ return;
+ }
+ unless (-e $hsh{file}) {
+ $Imager::ERRSTR = "Font file $hsh{file} not found";
+ return;
+ }
+ unless ($Imager::formats{tt}) {
+ $Imager::ERRSTR = "Type 1 fonts not supported in this build";
+ return;
+ }
+ my $id = Imager::i_tt_new($hsh{file});
+ unless ($id >= 0) { # the low-level code may miss some error handling
+ $Imager::ERRSTR = "Could not load font ($id)";
+ return;
+ }
+ return bless {
+ id => $id,
+ aa => $hsh{aa} || 0,
+ file => $hsh{file},
+ type => 'tt',
+ size => $hsh{size},
+ color => $hsh{color},
+ }, $class;
+}
+
+sub _draw {
+ my $self = shift;
+ my %input = @_;
+ if ( exists $input{channel} ) {
+ Imager::i_tt_cp($self->{id},$input{image}{IMG},
+ $input{x}, $input{'y'}, $input{channel}, $input{size},
+ $input{string}, length($input{string}),$input{aa});
+ } else {
+ Imager::i_tt_text($self->{id}, $input{image}{IMG},
+ $input{x}, $input{'y'}, $input{color},
+ $input{size}, $input{string},
+ length($input{string}), $input{aa});
+ }
+}
+
+sub _bounding_box {
+ my $self = shift;
+ my %input = @_;
+ return Imager::i_tt_bbox($self->{id}, $input{size},
+ $input{string}, length($input{string}));
+}
+
+1;
+
+__END__
+
+=head1 NAME
+
+ Imager::Font::Truetype - low-level functions for Truetype fonts
+
+=head1 DESCRIPTION
+
+Imager::Font creates a Imager::Font::Truetype object when asked to create
+a font object based on a .ttf file.
+
+See Imager::Font to see how to use this type.
+
+This class provides low-level functions that require the caller to
+perform data validation.
+
+=head1 AUTHOR
+
+Addi, Tony
+
+=cut
--- /dev/null
+package Imager::Font::Type1;
+use strict;
+use Imager::Color;
+use vars qw(@ISA);
+@ISA = qw(Imager::Font);
+use File::Spec;
+
+my $t1aa;
+
+# $T1AA is in there because for some reason (probably cache related) antialiasing
+# is a system wide setting in t1 lib.
+
+sub t1_set_aa_level {
+ if (!defined $t1aa or $_[0] != $t1aa) {
+ Imager::i_t1_set_aa($_[0]);
+ $t1aa=$_[0];
+ }
+}
+
+sub new {
+ my $class = shift;
+ my %hsh=(color=>Imager::Color->new(255,0,0,0),
+ size=>15,
+ @_);
+
+ unless ($hsh{file}) {
+ $Imager::ERRSTR = "No font file specified";
+ return;
+ }
+ unless (-e $hsh{file}) {
+ $Imager::ERRSTR = "Font file $hsh{file} not found";
+ return;
+ }
+ unless ($Imager::formats{t1}) {
+ $Imager::ERRSTR = "Type 1 fonts not supported in this build";
+ return;
+ }
+ unless ($hsh{file} =~ m!^/! || $hsh{file} =~ m!^\./!) {
+ $hsh{file} = File::Spec->catfile(File::Spec->curdir, $hsh{file});
+ }
+ my $id = Imager::i_t1_new($hsh{file});
+ unless ($id >= 0) { # the low-level code may miss some error handling
+ $Imager::ERRSTR = "Could not load font ($id)";
+ return;
+ }
+ return bless {
+ id => $id,
+ aa => $hsh{aa} || 0,
+ file => $hsh{file},
+ type => 't1',
+ size => $hsh{size},
+ color => $hsh{color},
+ }, $class;
+}
+
+sub _draw {
+ my $self = shift;
+ my %input = @_;
+ t1_set_aa_level($input{aa});
+ if (exists $input{channel}) {
+ Imager::i_t1_cp($input{image}{IMG}, $input{x}, $input{'y'},
+ $input{channel}, $self->{id}, $input{size},
+ $input{string}, length($input{string}), $input{align});
+ } else {
+ Imager::i_t1_text($input{image}{IMG}, $input{x}, $input{'y'},
+ $input{color}, $self->{id}, $input{size},
+ $input{string}, length($input{string}),
+ $input{align});
+ }
+
+ return $self;
+}
+
+sub _bounding_box {
+ my $self = shift;
+ my %input = @_;
+ return Imager::i_t1_bbox($self->{id}, $input{size}, $input{string},
+ length($input{string}));
+}
+
+1;
+
+__END__
+
+=head1 NAME
+
+ Imager::Font::Type1 - low-level functions for Type1 fonts
+
+=head1 DESCRIPTION
+
+Imager::Font creates a Imager::Font::Type1 object when asked to create
+a font object based on a .pfb file.
+
+See Imager::Font to see how to use this type.
+
+This class provides low-level functions that require the caller to
+perform data validation
+
+=head1 AUTHOR
+
+Addi, Tony
+
+=cut