fixed some minor test code hiccups
[imager.git] / lib / Imager / Font / Type1.pm
CommitLineData
96190b6e
TC
1package Imager::Font::Type1;
2use strict;
3use Imager::Color;
4use vars qw(@ISA);
5@ISA = qw(Imager::Font);
96190b6e
TC
6
7my $t1aa;
8
9# $T1AA is in there because for some reason (probably cache related) antialiasing
10# is a system wide setting in t1 lib.
11
12sub t1_set_aa_level {
13 if (!defined $t1aa or $_[0] != $t1aa) {
14 Imager::i_t1_set_aa($_[0]);
15 $t1aa=$_[0];
16 }
17}
18
19sub new {
20 my $class = shift;
21 my %hsh=(color=>Imager::Color->new(255,0,0,0),
22 size=>15,
23 @_);
24
25 unless ($hsh{file}) {
26 $Imager::ERRSTR = "No font file specified";
27 return;
28 }
29 unless (-e $hsh{file}) {
30 $Imager::ERRSTR = "Font file $hsh{file} not found";
31 return;
32 }
33 unless ($Imager::formats{t1}) {
34 $Imager::ERRSTR = "Type 1 fonts not supported in this build";
35 return;
36 }
37 unless ($hsh{file} =~ m!^/! || $hsh{file} =~ m!^\./!) {
9d540150 38 $hsh{file} = './' . $hsh{file};
96190b6e 39 }
6b012d62
TC
40
41 if($hsh{afm}) {
42 unless (-e $hsh{afm}) {
43 $Imager::ERRSTR = "Afm file $hsh{afm} not found";
44 return;
45 }
46 unless ($hsh{afm} =~ m!^/! || $hsh{afm} =~ m!^\./!) {
47 $hsh{file} = './' . $hsh{file};
48 }
49 } else {
50 $hsh{afm} = 0;
51 }
52
53 my $id = Imager::i_t1_new($hsh{file},$hsh{afm});
96190b6e
TC
54 unless ($id >= 0) { # the low-level code may miss some error handling
55 $Imager::ERRSTR = "Could not load font ($id)";
56 return;
57 }
58 return bless {
59 id => $id,
60 aa => $hsh{aa} || 0,
61 file => $hsh{file},
62 type => 't1',
63 size => $hsh{size},
64 color => $hsh{color},
65 }, $class;
66}
67
68sub _draw {
69 my $self = shift;
70 my %input = @_;
71 t1_set_aa_level($input{aa});
1bd75e4c
TC
72 my $flags = '';
73 $flags .= 'u' if $input{underline};
74 $flags .= 's' if $input{strikethrough};
75 $flags .= 'o' if $input{overline};
96190b6e 76 if (exists $input{channel}) {
9d540150 77 Imager::i_t1_cp($input{image}{IMG}, $input{'x'}, $input{'y'},
96190b6e 78 $input{channel}, $self->{id}, $input{size},
1bd75e4c
TC
79 $input{string}, length($input{string}), $input{align},
80 $input{utf8}, $flags);
96190b6e 81 } else {
9d540150 82 Imager::i_t1_text($input{image}{IMG}, $input{'x'}, $input{'y'},
96190b6e
TC
83 $input{color}, $self->{id}, $input{size},
84 $input{string}, length($input{string}),
1bd75e4c 85 $input{align}, $input{utf8}, $flags);
96190b6e
TC
86 }
87
88 return $self;
89}
90
91sub _bounding_box {
92 my $self = shift;
93 my %input = @_;
1bd75e4c
TC
94 my $flags = '';
95 $flags .= 'u' if $input{underline};
96 $flags .= 's' if $input{strikethrough};
97 $flags .= 'o' if $input{overline};
96190b6e 98 return Imager::i_t1_bbox($self->{id}, $input{size}, $input{string},
1bd75e4c 99 length($input{string}), $input{utf8}, $flags);
96190b6e
TC
100}
101
1021;
103
104__END__
105
106=head1 NAME
107
108 Imager::Font::Type1 - low-level functions for Type1 fonts
109
110=head1 DESCRIPTION
111
112Imager::Font creates a Imager::Font::Type1 object when asked to create
113a font object based on a .pfb file.
114
115See Imager::Font to see how to use this type.
116
117This class provides low-level functions that require the caller to
118perform data validation
119
21aa4e10
TC
120By default Imager no longer creates the F<t1lib.log> log file. You
121can re-enable that by calling Imager::init() with the C<t1log> option:
122
123 Imager::init(t1log=>1);
124
1bd75e4c
TC
125Currently specific to Imager::Font::Type1, you can use the following
126flags when drawing text or calculating a bounding box:
127
128=over
129
130=item underline
131
132Draw the text with an underline.
133
134=item overline
135
136Draw the text with an overline.
137
138=item strikethrough
139
140Draw the text with a strikethrough.
141
142=back
143
144Obviously, if you're calculating the bounding box the size of the line
145is included in the box, and the line isn't drawn :)
146
96190b6e
TC
147=head1 AUTHOR
148
149Addi, Tony
150
151=cut