use the standard font tests for FT2 too
[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
5715f7c3 6$VERSION = "1.011";
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
TC
46
47 # note that the string length parameter is ignored and calculated in
48 # XS with SvPV(), since we want the number of bytes rather than the
49 # number of characters, which is what we'd get in perl for a UTF8
50 # encoded string in 5.6 and later
51
96190b6e
TC
52 if ( exists $input{channel} ) {
53 Imager::i_tt_cp($self->{id},$input{image}{IMG},
9d540150 54 $input{'x'}, $input{'y'}, $input{channel}, $input{size},
4f68b48f 55 $input{string}, length($input{string}),$input{aa},
9ab6338b 56 $input{utf8}, $input{align});
96190b6e
TC
57 } else {
58 Imager::i_tt_text($self->{id}, $input{image}{IMG},
9d540150 59 $input{'x'}, $input{'y'}, $input{color},
96190b6e 60 $input{size}, $input{string},
9ab6338b
TC
61 length($input{string}), $input{aa}, $input{utf8},
62 $input{align});
96190b6e
TC
63 }
64}
65
66sub _bounding_box {
67 my $self = shift;
68 my %input = @_;
69 return Imager::i_tt_bbox($self->{id}, $input{size},
4f68b48f
TC
70 $input{string}, length($input{string}),
71 $input{utf8});
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
80 unless (defined $hsh{string} && length $hsh{string}) {
81 $Imager::ERRSTR = "No string supplied to \$font->has_chars()";
82 return;
83 }
e6ab3b7b
TC
84 return Imager::i_tt_has_chars($self->{id}, $hsh{string},
85 _first($hsh{'utf8'}, $self->{utf8}, 0));
eeaa33fd
TC
86}
87
3799c4d1
TC
88sub face_name {
89 my ($self) = @_;
90
91 Imager::i_tt_face_name($self->{id});
92}
93
94sub glyph_names {
95 my ($self, %input) = @_;
96
97 my $string = $input{string};
98 defined $string
a4168bea 99 or return Imager->_set_error("no string parameter passed to glyph_names");
3799c4d1
TC
100 my $utf8 = _first($input{utf8} || 0);
101
102 Imager::i_tt_glyph_name($self->{id}, $string, $utf8);
103}
104
96190b6e
TC
1051;
106
107__END__
108
109=head1 NAME
110
111 Imager::Font::Truetype - low-level functions for Truetype fonts
112
113=head1 DESCRIPTION
114
5715f7c3
TC
115Imager::Font creates a Imager::Font::Truetype object when asked to
116create a font object based on a F<.ttf> file.
96190b6e
TC
117
118See Imager::Font to see how to use this type.
119
120This class provides low-level functions that require the caller to
121perform data validation.
122
123=head1 AUTHOR
124
125Addi, Tony
126
127=cut