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