]> git.imager.perl.org - imager.git/blobdiff - lib/Imager/Font.pm
add an internal i_img_samef() and adapt is_imaged() to use it
[imager.git] / lib / Imager / Font.pm
index 53b150fd24c406fcb3eb1f847cf4411d52d5acd8..a7e13bd220ba40be8f6168e6fb95575c22c9703a 100644 (file)
@@ -4,7 +4,7 @@ use Imager::Color;
 use strict;
 use vars qw($VERSION);
 
-$VERSION = "1.034";
+$VERSION = "1.035";
 
 # the aim here is that we can:
 #  - add file based types in one place: here
@@ -18,16 +18,17 @@ my %drivers =
         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',
         },
@@ -46,21 +47,6 @@ my %drivers =
 # 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 = {};
@@ -75,13 +61,26 @@ sub new {
     $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;
+         }
         }
       }
     }
@@ -110,7 +109,7 @@ sub new {
     return;
   }
 
-  if (!$Imager::formats{$type}) {
+  if ($drivers{$type}{checktype} && !$Imager::formats{$type}) {
     $Imager::ERRSTR = "`$type' not enabled";
     return;
   }
@@ -316,11 +315,50 @@ sub priorities {
   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__
@@ -506,9 +544,7 @@ the FreeType 2.x driver by setting C<type> to C<'ft2'>:
 
 =back
 
-
-
-=item bounding_box
+=item bounding_box()
 
 Returns the bounding box for the specified string.  Example:
 
@@ -614,7 +650,7 @@ parameter.
 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 *
@@ -633,7 +669,7 @@ and height of the text instead.
 =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=>...)
 
@@ -741,7 +777,9 @@ C<dpi> - set both horizontal and vertical resolution to this value.
 
 Returns a list containing the previous C<xdpi>, C<ydpi> values.
 
-=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:
@@ -788,7 +826,7 @@ at least one character.
 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
@@ -821,7 +859,7 @@ included.
 =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
 
@@ -956,8 +994,6 @@ You can set new priorities and save the old priorities with:
 
   @old = Imager::Font->priorities(@drivers);
 
-=back
-
 If you supply driver names that are not currently supported, they will
 be ignored.
 
@@ -968,6 +1004,39 @@ do font transformations, you may want to give that a higher priority:
 
   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
@@ -976,8 +1045,6 @@ list.
 
 =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