update Changes
[imager.git] / lib / Imager / Font / Truetype.pm
CommitLineData
96190b6e
TC
1package Imager::Font::Truetype;
2use strict;
f17b46d8 3use vars qw(@ISA $VERSION);
96190b6e
TC
4@ISA = qw(Imager::Font);
5
e3b4d6c3 6$VERSION = "1.012";
f17b46d8 7
3799c4d1
TC
8*_first = \&Imager::Font::_first;
9
96190b6e
TC
10sub new {
11 my $class = shift;
61e5a61a 12 my %hsh=(color=>Imager::Color->new(255,0,0,255),
96190b6e
TC
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});
d93d5c10
TC
29 unless ($id) { # the low-level code may miss some error handling
30 $Imager::ERRSTR = Imager::_error_as_msg();
96190b6e
TC
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
43sub _draw {
44 my $self = shift;
45 my %input = @_;
4f68b48f 46
96190b6e
TC
47 if ( exists $input{channel} ) {
48 Imager::i_tt_cp($self->{id},$input{image}{IMG},
9d540150 49 $input{'x'}, $input{'y'}, $input{channel}, $input{size},
e3b4d6c3 50 $input{string}, $input{aa},
9ab6338b 51 $input{utf8}, $input{align});
96190b6e
TC
52 } else {
53 Imager::i_tt_text($self->{id}, $input{image}{IMG},
9d540150 54 $input{'x'}, $input{'y'}, $input{color},
96190b6e 55 $input{size}, $input{string},
e3b4d6c3 56 $input{aa}, $input{utf8},
9ab6338b 57 $input{align});
96190b6e
TC
58 }
59}
60
61sub _bounding_box {
62 my $self = shift;
63 my %input = @_;
7e3298ec
TC
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;
96190b6e
TC
72}
73
4f68b48f
TC
74sub utf8 { 1 }
75
eeaa33fd
TC
76# check if the font has the characters in the given string
77sub has_chars {
78 my ($self, %hsh) = @_;
79
7e3298ec 80 unless (defined $hsh{string}) {
eeaa33fd
TC
81 $Imager::ERRSTR = "No string supplied to \$font->has_chars()";
82 return;
83 }
7e3298ec
TC
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 }
eeaa33fd
TC
103}
104
3799c4d1
TC
105sub face_name {
106 my ($self) = @_;
107
108 Imager::i_tt_face_name($self->{id});
109}
110
7e3298ec
TC
111sub can_glyph_names {
112 1;
113}
114
3799c4d1
TC
115sub glyph_names {
116 my ($self, %input) = @_;
117
118 my $string = $input{string};
119 defined $string
a4168bea 120 or return Imager->_set_error("no string parameter passed to glyph_names");
3799c4d1
TC
121 my $utf8 = _first($input{utf8} || 0);
122
7e3298ec
TC
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;
3799c4d1
TC
130}
131
96190b6e
TC
1321;
133
134__END__
135
136=head1 NAME
137
138 Imager::Font::Truetype - low-level functions for Truetype fonts
139
140=head1 DESCRIPTION
141
5715f7c3
TC
142Imager::Font creates a Imager::Font::Truetype object when asked to
143create a font object based on a F<.ttf> file.
96190b6e
TC
144
145See Imager::Font to see how to use this type.
146
147This class provides low-level functions that require the caller to
148perform data validation.
149
150=head1 AUTHOR
151
152Addi, Tony
153
154=cut