]> git.imager.perl.org - imager.git/blob - lib/Imager/Font/Truetype.pm
ebb5ce61160539a88bad97b37081358bf106047f
[imager.git] / lib / Imager / Font / Truetype.pm
1 package Imager::Font::Truetype;
2 use strict;
3 use vars qw(@ISA);
4 @ISA = qw(Imager::Font);
5
6 *_first = \&Imager::Font::_first;
7
8 sub new {
9   my $class = shift;
10   my %hsh=(color=>Imager::Color->new(255,0,0,0),
11            size=>15,
12            @_);
13
14   unless ($hsh{file}) {
15     $Imager::ERRSTR = "No font file specified";
16     return;
17   }
18   unless (-e $hsh{file}) {
19     $Imager::ERRSTR = "Font file $hsh{file} not found";
20     return;
21   }
22   unless ($Imager::formats{tt}) {
23     $Imager::ERRSTR = "Type 1 fonts not supported in this build";
24     return;
25   }
26   my $id = Imager::i_tt_new($hsh{file});
27   unless ($id >= 0) { # the low-level code may miss some error handling
28     $Imager::ERRSTR = "Could not load font ($id)";
29     return;
30   }
31   return bless {
32                 id    => $id,
33                 aa    => $hsh{aa} || 0,
34                 file  => $hsh{file},
35                 type  => 'tt',
36                 size  => $hsh{size},
37                 color => $hsh{color},
38                }, $class;
39 }
40
41 sub _draw {
42   my $self = shift;
43   my %input = @_;
44
45   # note that the string length parameter is ignored and calculated in
46   # XS with SvPV(), since we want the number of bytes rather than the
47   # number of characters, which is what we'd get in perl for a UTF8
48   # encoded string in 5.6 and later
49
50   if ( exists $input{channel} ) {
51     Imager::i_tt_cp($self->{id},$input{image}{IMG},
52                     $input{'x'}, $input{'y'}, $input{channel}, $input{size},
53                     $input{string}, length($input{string}),$input{aa},
54                     $input{utf8}); 
55   } else {
56     Imager::i_tt_text($self->{id}, $input{image}{IMG}, 
57                       $input{'x'}, $input{'y'}, $input{color},
58                       $input{size}, $input{string}, 
59                       length($input{string}), $input{aa}, $input{utf8}); 
60   }
61 }
62
63 sub _bounding_box {
64   my $self = shift;
65   my %input = @_;
66   return Imager::i_tt_bbox($self->{id}, $input{size},
67                            $input{string}, length($input{string}),
68                            $input{utf8});
69 }
70
71 sub utf8 { 1 }
72
73 # check if the font has the characters in the given string
74 sub has_chars {
75   my ($self, %hsh) = @_;
76
77   unless (defined $hsh{string} && length $hsh{string}) {
78     $Imager::ERRSTR = "No string supplied to \$font->has_chars()";
79     return;
80   }
81   return Imager::i_tt_has_chars($self->{id}, $hsh{string}, $hsh{'utf8'} || 0);
82 }
83
84 sub face_name {
85   my ($self) = @_;
86
87   Imager::i_tt_face_name($self->{id});
88 }
89
90 sub glyph_names {
91   my ($self, %input) = @_;
92
93   my $string = $input{string};
94   defined $string
95     or return Imager->_seterror("no string parameter passed to glyph_names");
96   my $utf8 = _first($input{utf8} || 0);
97
98   Imager::i_tt_glyph_name($self->{id}, $string, $utf8);
99 }
100
101 1;
102
103 __END__
104
105 =head1 NAME
106
107   Imager::Font::Truetype - low-level functions for Truetype fonts
108
109 =head1 DESCRIPTION
110
111 Imager::Font creates a Imager::Font::Truetype object when asked to create
112 a font object based on a .ttf file.
113
114 See Imager::Font to see how to use this type.
115
116 This class provides low-level functions that require the caller to
117 perform data validation.
118
119 =head1 AUTHOR
120
121 Addi, Tony
122
123 =cut