]> git.imager.perl.org - imager.git/blob - lib/Imager/Font/Type1.pm
minor error handling in bmp.c
[imager.git] / lib / Imager / Font / Type1.pm
1 package Imager::Font::Type1;
2 use strict;
3 use Imager::Color;
4 use vars qw(@ISA);
5 @ISA = qw(Imager::Font);
6 use File::Spec;
7
8 my $t1aa;
9
10 # $T1AA is in there because for some reason (probably cache related) antialiasing
11 # is a system wide setting in t1 lib.
12
13 sub t1_set_aa_level {
14   if (!defined $t1aa or $_[0] != $t1aa) {
15     Imager::i_t1_set_aa($_[0]);
16     $t1aa=$_[0];
17   }
18 }
19
20 sub new {
21   my $class = shift;
22   my %hsh=(color=>Imager::Color->new(255,0,0,0),
23            size=>15,
24            @_);
25
26   unless ($hsh{file}) {
27     $Imager::ERRSTR = "No font file specified";
28     return;
29   }
30   unless (-e $hsh{file}) {
31     $Imager::ERRSTR = "Font file $hsh{file} not found";
32     return;
33   }
34   unless ($Imager::formats{t1}) {
35     $Imager::ERRSTR = "Type 1 fonts not supported in this build";
36     return;
37   }
38   unless ($hsh{file} =~ m!^/! || $hsh{file} =~ m!^\./!) {
39     $hsh{file} = File::Spec->catfile(File::Spec->curdir, $hsh{file});
40   }
41   my $id = Imager::i_t1_new($hsh{file});
42   unless ($id >= 0) { # the low-level code may miss some error handling
43     $Imager::ERRSTR = "Could not load font ($id)";
44     return;
45   }
46   return bless {
47                 id    => $id,
48                 aa    => $hsh{aa} || 0,
49                 file  => $hsh{file},
50                 type  => 't1',
51                 size  => $hsh{size},
52                 color => $hsh{color},
53                }, $class;
54 }
55
56 sub _draw {
57   my $self = shift;
58   my %input = @_;
59   t1_set_aa_level($input{aa});
60   if (exists $input{channel}) {
61     Imager::i_t1_cp($input{image}{IMG}, $input{x}, $input{'y'},
62                     $input{channel}, $self->{id}, $input{size},
63                     $input{string}, length($input{string}), $input{align});
64   } else {
65     Imager::i_t1_text($input{image}{IMG}, $input{x}, $input{'y'}, 
66                       $input{color}, $self->{id}, $input{size}, 
67                       $input{string}, length($input{string}), 
68                       $input{align});
69   }
70
71   return $self;
72 }
73
74 sub _bounding_box {
75   my $self = shift;
76   my %input = @_;
77   return Imager::i_t1_bbox($self->{id}, $input{size}, $input{string},
78                            length($input{string}));
79 }
80
81 1;
82
83 __END__
84
85 =head1 NAME
86
87   Imager::Font::Type1 - low-level functions for Type1 fonts
88
89 =head1 DESCRIPTION
90
91 Imager::Font creates a Imager::Font::Type1 object when asked to create
92 a font object based on a .pfb file.
93
94 See Imager::Font to see how to use this type.
95
96 This class provides low-level functions that require the caller to
97 perform data validation
98
99 =head1 AUTHOR
100
101 Addi, Tony
102
103 =cut