use strict;
use vars qw($VERSION);
-$VERSION = "1.034";
+$VERSION = "1.038";
# the aim here is that we can:
# - add file based types in one place: here
module=>'Imager/Font/Truetype.pm',
files=>'.*\.ttf$',
description => 'FreeType 1.x',
+ checktype => 1,
},
t1=>{
- class=>'Imager::Font::Type1',
- module=>'Imager/Font/Type1.pm',
+ class=>'Imager::Font::T1',
+ module=>'Imager/Font/T1.pm',
files=>'.*\.pfb$',
description => 'T1Lib',
},
ft2=>{
- class=>'Imager::Font::FreeType2',
- module=>'Imager/Font/FreeType2.pm',
+ class=>'Imager::Font::FT2',
+ module=>'Imager/Font/FT2.pm',
files=>'.*\.(pfa|pfb|otf|ttf|fon|fnt|dfont|pcf(\.gz)?)$',
description => 'FreeType 2.x',
},
files=>'.*\.ifs$',
},
w32=>{
- class=>'Imager::Font::Win32',
- module=>'Imager/Font/Win32.pm',
+ class=>'Imager::Font::W32',
+ module=>'Imager/Font/W32.pm',
description => 'Win32 GDI Fonts',
},
);
# this currently should only contain file based types, don't add w32
my @priority = qw(t1 tt ft2 ifs);
-# when Imager::Font is loaded, Imager.xs has not been bootstrapped yet
-# this function is called from Imager.pm to finish initialization
-sub __init {
- @priority = grep Imager::i_has_format($_), @priority;
- for my $driver_name (grep Imager::i_has_format($_), keys %drivers) {
- $drivers{$driver_name}{enabled} = 1;
- }
-}
-
-# search method
-# 1. start by checking if file is the parameter
-# 1a. if so qualify path and compare to the cache.
-# 2a. if in cache - take it's id from there and increment count.
-#
-
sub new {
my $class = shift;
my $self = {};
my ($file, $type, $id);
- my %hsh=(color => Imager::Color->new(255,0,0,0),
+ my %hsh=(color => Imager::Color->new(255,0,0,255),
size => 15,
@_);
$file = $hsh{'file'};
$type = $hsh{'type'};
- if (!defined($type) or !$drivers{$type} or !$drivers{$type}{enabled}) {
+ if (defined $type) {
+ unless ($drivers{$type}) {
+ Imager->_set_error("Unknown font type $type");
+ return;
+ }
+
+ unless ($Imager::formats{$type}) {
+ Imager->_set_error("The $type {$drivers{$type}) font driver is not installed");
+ return;
+ }
+ }
+ else {
for my $drv (@priority) {
undef $type;
my $re = $drivers{$drv}{files} or next;
if ($file =~ /$re/i) {
- $type = $drv;
- last;
+ if (eval { require $drivers{$drv}{module}; 1 } and !( $drivers{$drv}{checktype} && !$Imager::formats{$drv} )) {
+ $type = $drv;
+ last;
+ }
}
}
}
return;
}
- if (!$Imager::formats{$type}) {
+ if ($drivers{$type}{checktype} && !$Imager::formats{$type}) {
$Imager::ERRSTR = "`$type' not enabled";
return;
}
$input{sizew} = _first($input{sizew}, $self->{sizew}, 0);
$input{utf8} = _first($input{utf8}, $self->{utf8}, 0);
- my @box = $self->_bounding_box(%input);
+ my @box = $self->_bounding_box(%input)
+ or return;
if (wantarray) {
if(@box && exists $input{'x'} and exists $input{'y'}) {
$box[0]+=$input{'x'};
$box[2]+=$input{'x'};
} elsif (@box && $input{'canon'}) {
- $box[3]-=$box[1]; # make it cannoical (ie (0,0) - (width, height))
+ $box[3]-=$box[1]; # make it canonical (ie (0,0) - (width, height))
$box[2]-=$box[0];
}
return @box;
my @old = @priority;
if (@_) {
- @priority = grep Imager::i_has_format($_), @_;
+ @priority = @_;
}
return @old;
}
+sub register {
+ my ($self, %opts) = @_;
+
+ my $type = delete $opts{type};
+ my $class = delete $opts{class};
+ my $files = delete $opts{files};
+ my $description = delete $opts{description} || $class;
+
+ defined $type
+ or return Imager->_set_error("No type parameter supplied to Imager::Font->regster");
+
+ defined $class
+ or return Imager->_set_error("No class parameter supplied to Imager::Font->register");
+
+ if ($files) {
+ eval { qr/$files/ }
+ or return Imager->_set_error("files isn't a valid regexp");
+ }
+
+ if ($drivers{$type} && $drivers{$type}{class} ne $class) {
+ Imager->_set_error("Font type $type already registered as $drivers{$type}{class}");
+ return;
+ }
+
+ (my $module = $class . ".pm") =~ s(::)(/)g;
+
+ my $driver =
+ {
+ class => $class,
+ module => $module,
+ description => $description,
+ };
+ $files and $driver->{files} = $files;
+
+ $drivers{$type} = $driver;
+
+ 1;
+}
+
1;
__END__
=back
+Returns the new font object on success. Returns C<undef> on failure
+and sets an error message readable with C<< Imager->errstr >>.
-
-=item bounding_box
+=item bounding_box()
Returns the bounding box for the specified string. Example:
C<utf8> - For drivers that support it, treat the string as UTF-8 encoded.
For versions of perl that support Unicode (5.6 and later), this will
be enabled automatically if the 'string' parameter is already a UTF-8
-string. See L<UTF-8> for more information. Default: the C<utf8> value
+string. See L</UTF-8> for more information. Default: the C<utf8> value
passed to Imager::Font->new(...) or 0.
=item *
=back
+On success returns either the list of bounds, or a bounding box object
+in scalar context. Returns an empty list or C<undef> on failure and
+sets an error message readable with C<< Imager->errstr >>.
+
+The transformation matrix set by L</transform()> has no effect on the
+result of this method - the bounds of the untransformed text is
+returned.
+
=item string()
The $img->string(...) method is now documented in
-L<Imager::Draw/string>
+L<Imager::Draw/string()>
=item align(string=>$text,size=>$size,x=>...,y=>...,valign => ...,halign=>...)
=back
-Returns a list specifying the bounds of the drawn text.
+Returns a list specifying the bounds of the drawn text on success.
+Returns an empty list on failure, if an C<image> parameter was
+supplied the error message can be read with C<< $image->errstr >>,
+otherwise it's available as C<< Imager->errstr >>.
=item dpi()
=back
-Returns a list containing the previous C<xdpi>, C<ydpi> values.
+Returns a list containing the previous C<xdpi>, C<ydpi> values on
+success. Returns an empty list on failure, with an error message
+returned in C<< Imager->errstr >>.
-=item transform(matrix=>$matrix)
+=item transform()
+
+ $font->transform(matrix=>$matrix);
Applies a transformation to the font, where matrix is an array ref of
numbers representing a 2 x 3 matrix:
increases as you move up, not image co-ordinates where y decreases as
you move up.
+C<transform()> has no effect on the results of L</bounding_box()>.
+
+Returns true on success. Returns false on failure with the cause
+readable from C<< Imager->errstr >>.
+
=item has_chars(string=>$text)
Checks if the characters in $text are defined by the font.
Not all fonts support this method (use $font->can("has_chars") to
check.)
+On error, returns an empty list or undef in scalar context, and sets
+an error message readable with C<< Imager->errstr >>.
+
=over
=item *
C<utf8> - For drivers that support it, treat the string as UTF-8
encoded. For versions of perl that support Unicode (5.6 and later),
this will be enabled automatically if the 'string' parameter is
-already a UTF-8 string. See L<UTF-8> for more information. Default:
+already a UTF-8 string. See L</UTF-8> for more information. Default:
the C<utf8> value passed to Imager::Font->new(...) or 0.
=back
=item face_name()
Returns the internal name of the face. Not all font types support
-this method yet.
+this method yet, so you should check with C<< $font->can("face_name")
+>> before calling C<face_name>.
=item glyph_names(string=>$string [, utf8=>$utf8 ][, reliable_only=>0 ] );
Both FreeType 1.x and 2.x allow support for glyph names to not be
included.
+If the supplied C<string> is marked as UTF-8 or the C<utf8> parameter
+is true and the supplied string does not contain valid UTF-8, returns
+an empty string and set an error message readable from C<<
+Imager->errstr >>,
+
+=item can_glyph_names()
+
+As a class method, returns true if the underlying library supports
+returning glyph names.
+
+As an object method, returns true if the supplied font supports
+returning glyph names.
+
=item draw
This is used by Imager's string() method to implement drawing text.
-See L<Imager::Draw/string>.
+See L<Imager::Draw/string()>.
=back
$x = pack("C*", 0xE2, 0x80, 0x90); # character code 0x2010 HYPHEN
-You need to be be careful with versions of perl that have UTF-8
+You need to be careful with versions of perl that have UTF-8
support, since your string may end up doubly UTF-8 encoded.
For example:
# at this point $x is has the UTF-8 flag set, but has 5 characters,
# none, of which is the constructed UTF-8 character
-The test script t/t38ft2font.t has a small example of this after the
+The test script t/t38ft2font.t has a small example of this after the
comment:
# an attempt using emulation of UTF-8
@old = Imager::Font->priorities(@drivers);
-=back
-
If you supply driver names that are not currently supported, they will
be ignored.
my @old = Imager::Font->priorities(qw(tt ft2 t1));
+=item register
+
+Registers an extra font driver. Accepts the following parameters:
+
+=over
+
+=item *
+
+type - a brief identifier for the font driver. You can supply this
+value to C<< Imager::Font->new() >> to create fonts of this type.
+Required.
+
+=item *
+
+class - the font class name. Imager will attempted to load this
+module by name. Required.
+
+=item *
+
+files - a regular expression to match against file names. If supplied
+this must be a valid perl regular expression. If not supplied you can
+only create fonts of this type by supplying the C<type> parameter to
+C<< Imager::Font->new() >>
+
+=item *
+
+description - a brief description of the font driver. Defaults to the
+value supplied in C<class>.
+
+=back
+
+=back
+
=head1 AUTHOR
Arnar M. Hrafnkelsson, addi@umich.edu
=head1 BUGS
-You need to modify this class to add new font types.
-
The $pos_width member returned by the bounding_box() method has
historically returned different values from different drivers. The
FreeType 1.x and 2.x, and the Win32 drivers return the max of the