fixed some minor test code hiccups
[imager.git] / lib / Imager / Font / Truetype.pm
CommitLineData
96190b6e
TC
1package Imager::Font::Truetype;
2use strict;
3use vars qw(@ISA);
4@ISA = qw(Imager::Font);
5
6sub new {
7 my $class = shift;
8 my %hsh=(color=>Imager::Color->new(255,0,0,0),
9 size=>15,
10 @_);
11
12 unless ($hsh{file}) {
13 $Imager::ERRSTR = "No font file specified";
14 return;
15 }
16 unless (-e $hsh{file}) {
17 $Imager::ERRSTR = "Font file $hsh{file} not found";
18 return;
19 }
20 unless ($Imager::formats{tt}) {
21 $Imager::ERRSTR = "Type 1 fonts not supported in this build";
22 return;
23 }
24 my $id = Imager::i_tt_new($hsh{file});
25 unless ($id >= 0) { # the low-level code may miss some error handling
26 $Imager::ERRSTR = "Could not load font ($id)";
27 return;
28 }
29 return bless {
30 id => $id,
31 aa => $hsh{aa} || 0,
32 file => $hsh{file},
33 type => 'tt',
34 size => $hsh{size},
35 color => $hsh{color},
36 }, $class;
37}
38
39sub _draw {
40 my $self = shift;
41 my %input = @_;
4f68b48f
TC
42
43 # note that the string length parameter is ignored and calculated in
44 # XS with SvPV(), since we want the number of bytes rather than the
45 # number of characters, which is what we'd get in perl for a UTF8
46 # encoded string in 5.6 and later
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},
4f68b48f
TC
51 $input{string}, length($input{string}),$input{aa},
52 $input{utf8});
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},
4f68b48f 57 length($input{string}), $input{aa}, $input{utf8});
96190b6e
TC
58 }
59}
60
61sub _bounding_box {
62 my $self = shift;
63 my %input = @_;
64 return Imager::i_tt_bbox($self->{id}, $input{size},
4f68b48f
TC
65 $input{string}, length($input{string}),
66 $input{utf8});
96190b6e
TC
67}
68
4f68b48f
TC
69sub utf8 { 1 }
70
eeaa33fd
TC
71# check if the font has the characters in the given string
72sub has_chars {
73 my ($self, %hsh) = @_;
74
75 unless (defined $hsh{string} && length $hsh{string}) {
76 $Imager::ERRSTR = "No string supplied to \$font->has_chars()";
77 return;
78 }
79 return Imager::i_tt_has_chars($self->{id}, $hsh{string}, $hsh{'utf8'} || 0);
80}
81
96190b6e
TC
821;
83
84__END__
85
86=head1 NAME
87
88 Imager::Font::Truetype - low-level functions for Truetype fonts
89
90=head1 DESCRIPTION
91
92Imager::Font creates a Imager::Font::Truetype object when asked to create
93a font object based on a .ttf file.
94
95See Imager::Font to see how to use this type.
96
97This class provides low-level functions that require the caller to
98perform data validation.
99
100=head1 AUTHOR
101
102Addi, Tony
103
104=cut