]> git.imager.perl.org - imager.git/blob - lib/Imager/Font/Truetype.pm
68e1520879f5b6d6c3b927518cbe6274a81e03f7
[imager.git] / lib / Imager / Font / Truetype.pm
1 package Imager::Font::Truetype;
2 use strict;
3 use vars qw(@ISA $VERSION);
4 @ISA = qw(Imager::Font);
5
6 $VERSION = "1.012";
7
8 *_first = \&Imager::Font::_first;
9
10 sub new {
11   my $class = shift;
12   my %hsh=(color=>Imager::Color->new(255,0,0,255),
13            size=>15,
14            @_);
15
16   unless ($hsh{file}) {
17     $Imager::ERRSTR = "No font file specified";
18     return;
19   }
20   unless (-e $hsh{file}) {
21     $Imager::ERRSTR = "Font file $hsh{file} not found";
22     return;
23   }
24   unless ($Imager::formats{tt}) {
25     $Imager::ERRSTR = "Type 1 fonts not supported in this build";
26     return;
27   }
28   my $id = Imager::i_tt_new($hsh{file});
29   unless ($id) { # the low-level code may miss some error handling
30     $Imager::ERRSTR = Imager::_error_as_msg();
31     return;
32   }
33   return bless {
34                 id    => $id,
35                 aa    => $hsh{aa} || 0,
36                 file  => $hsh{file},
37                 type  => 'tt',
38                 size  => $hsh{size},
39                 color => $hsh{color},
40                }, $class;
41 }
42
43 sub _draw {
44   my $self = shift;
45   my %input = @_;
46
47   if ( exists $input{channel} ) {
48     Imager::i_tt_cp($self->{id},$input{image}{IMG},
49                     $input{'x'}, $input{'y'}, $input{channel}, $input{size},
50                     $input{string}, $input{aa},
51                     $input{utf8}, $input{align}); 
52   } else {
53     Imager::i_tt_text($self->{id}, $input{image}{IMG}, 
54                       $input{'x'}, $input{'y'}, $input{color},
55                       $input{size}, $input{string}, 
56                       $input{aa}, $input{utf8},
57                       $input{align}); 
58   }
59 }
60
61 sub _bounding_box {
62   my $self = shift;
63   my %input = @_;
64   my @result =
65     Imager::i_tt_bbox($self->{id}, $input{size}, $input{string}, $input{utf8});
66   unless (@result) {
67     Imager->_set_error(Imager->_error_as_msg);
68     return;
69   }
70
71   return @result;
72 }
73
74 sub utf8 { 1 }
75
76 # check if the font has the characters in the given string
77 sub has_chars {
78   my ($self, %hsh) = @_;
79
80   unless (defined $hsh{string}) {
81     $Imager::ERRSTR = "No string supplied to \$font->has_chars()";
82     return;
83   }
84   if (wantarray) {
85     my @result = Imager::i_tt_has_chars($self->{id}, $hsh{string}, 
86                                         _first($hsh{'utf8'}, $self->{utf8}, 0));
87     unless (@result) {
88       Imager->_set_error(Imager->_error_as_msg);
89       return;
90     }
91     return @result;
92   }
93   else {
94     my $result = Imager::i_tt_has_chars($self->{id}, $hsh{string}, 
95                                         _first($hsh{'utf8'}, $self->{utf8}, 0));
96     unless (defined $result) {
97       Imager->_set_error(Imager->_error_as_msg);
98       return;
99     }
100
101     return $result;
102   }
103 }
104
105 sub face_name {
106   my ($self) = @_;
107
108   Imager::i_tt_face_name($self->{id});
109 }
110
111 sub can_glyph_names {
112   1;
113 }
114
115 sub glyph_names {
116   my ($self, %input) = @_;
117
118   my $string = $input{string};
119   defined $string
120     or return Imager->_set_error("no string parameter passed to glyph_names");
121   my $utf8 = _first($input{utf8} || 0);
122
123   my @names = Imager::i_tt_glyph_name($self->{id}, $string, $utf8);
124   unless (@names) {
125     Imager->_set_error(Imager->_error_as_msg);
126     return;
127   }
128
129   return @names;
130 }
131
132 1;
133
134 __END__
135
136 =head1 NAME
137
138   Imager::Font::Truetype - low-level functions for Truetype fonts
139
140 =head1 DESCRIPTION
141
142 Imager::Font creates a Imager::Font::Truetype object when asked to
143 create a font object based on a F<.ttf> file.
144
145 See Imager::Font to see how to use this type.
146
147 This class provides low-level functions that require the caller to
148 perform data validation.
149
150 =head1 AUTHOR
151
152 Addi, Tony
153
154 =cut