| 1 | package Imager::Font::Truetype; | ||
| 2 | use 5.006; | ||
| 3 | use strict; | ||
| 4 | |||
| 5 | our @ISA = qw(Imager::Font); | ||
| 6 | |||
| 7 | our $VERSION = "1.013"; | ||
| 8 | |||
| 9 | *_first = \&Imager::Font::_first; | ||
| 10 | |||
| 11 | sub new { | ||
| 12 | my $class = shift; | ||
| 13 | my %hsh=(color=>Imager::Color->new(255,0,0,255), | ||
| 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}); | ||
| 30 | unless ($id) { # the low-level code may miss some error handling | ||
| 31 | $Imager::ERRSTR = Imager::_error_as_msg(); | ||
| 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 | |||
| 44 | sub _draw { | ||
| 45 | my $self = shift; | ||
| 46 | my %input = @_; | ||
| 47 | |||
| 48 | if ( exists $input{channel} ) { | ||
| 49 | Imager::i_tt_cp($self->{id},$input{image}{IMG}, | ||
| 50 | $input{'x'}, $input{'y'}, $input{channel}, $input{size}, | ||
| 51 | $input{string}, $input{aa}, | ||
| 52 | $input{utf8}, $input{align}); | ||
| 53 | } else { | ||
| 54 | Imager::i_tt_text($self->{id}, $input{image}{IMG}, | ||
| 55 | $input{'x'}, $input{'y'}, $input{color}, | ||
| 56 | $input{size}, $input{string}, | ||
| 57 | $input{aa}, $input{utf8}, | ||
| 58 | $input{align}); | ||
| 59 | } | ||
| 60 | } | ||
| 61 | |||
| 62 | sub _bounding_box { | ||
| 63 | my $self = shift; | ||
| 64 | my %input = @_; | ||
| 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; | ||
| 73 | } | ||
| 74 | |||
| 75 | sub utf8 { 1 } | ||
| 76 | |||
| 77 | # check if the font has the characters in the given string | ||
| 78 | sub has_chars { | ||
| 79 | my ($self, %hsh) = @_; | ||
| 80 | |||
| 81 | unless (defined $hsh{string}) { | ||
| 82 | $Imager::ERRSTR = "No string supplied to \$font->has_chars()"; | ||
| 83 | return; | ||
| 84 | } | ||
| 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 | } | ||
| 104 | } | ||
| 105 | |||
| 106 | sub face_name { | ||
| 107 | my ($self) = @_; | ||
| 108 | |||
| 109 | Imager::i_tt_face_name($self->{id}); | ||
| 110 | } | ||
| 111 | |||
| 112 | sub can_glyph_names { | ||
| 113 | 1; | ||
| 114 | } | ||
| 115 | |||
| 116 | sub glyph_names { | ||
| 117 | my ($self, %input) = @_; | ||
| 118 | |||
| 119 | my $string = $input{string}; | ||
| 120 | defined $string | ||
| 121 | or return Imager->_set_error("no string parameter passed to glyph_names"); | ||
| 122 | my $utf8 = _first($input{utf8} || 0); | ||
| 123 | |||
| 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; | ||
| 131 | } | ||
| 132 | |||
| 133 | 1; | ||
| 134 | |||
| 135 | __END__ |