| 1 |
package Imager::Font::T1; |
| 2 |
use 5.006; |
| 3 |
use strict; |
| 4 |
use Imager::Color; |
| 5 |
our @ISA = qw(Imager::Font); |
| 6 |
use Scalar::Util (); |
| 7 |
|
| 8 |
BEGIN { |
| 9 |
our $VERSION = "1.034"; |
| 10 |
|
| 11 |
require XSLoader; |
| 12 |
XSLoader::load('Imager::Font::T1', $VERSION); |
| 13 |
} |
| 14 |
|
| 15 |
|
| 16 |
*_first = \&Imager::Font::_first; |
| 17 |
|
| 18 |
my $t1aa = 2; |
| 19 |
|
| 20 |
sub new { |
| 21 |
my $class = shift; |
| 22 |
my %hsh=(color=>Imager::Color->new(255,0,0,255), |
| 23 |
size=>15, |
| 24 |
@_); |
| 25 |
|
| 26 |
my $file = $hsh{file}; |
| 27 |
my $afm = $hsh{afm}; |
| 28 |
unless (defined $file) { |
| 29 |
$Imager::ERRSTR = "No font file specified"; |
| 30 |
return; |
| 31 |
} |
| 32 |
unless (-e $file) { |
| 33 |
$Imager::ERRSTR = "Font file $file not found"; |
| 34 |
return; |
| 35 |
} |
| 36 |
unless ($Imager::formats{t1}) { |
| 37 |
$Imager::ERRSTR = "Type 1 fonts not supported in this build"; |
| 38 |
return; |
| 39 |
} |
| 40 |
# we want to avoid T1Lib's file search mechanism |
| 41 |
unless ($file =~ m!^/! |
| 42 |
|| $file =~ m!^\.\/?/! |
| 43 |
|| $^O =~ /^(MSWin32|cygwin)$/ && $file =~ /^[a-z]:/i) { |
| 44 |
$file = './' . $file; |
| 45 |
} |
| 46 |
|
| 47 |
if(defined $afm && length $afm) { |
| 48 |
unless (-e $afm) { |
| 49 |
$Imager::ERRSTR = "Afm file $afm not found"; |
| 50 |
return; |
| 51 |
} |
| 52 |
unless ($afm =~ m!^/! |
| 53 |
|| $afm =~ m!^\./! |
| 54 |
|| $^O =~ /^(MSWin32|cygwin)$/ && $file =~ /^[a-z]:/i) { |
| 55 |
$afm = './' . $afm; |
| 56 |
} |
| 57 |
} |
| 58 |
else { |
| 59 |
$afm = undef; |
| 60 |
} |
| 61 |
|
| 62 |
my $font = Imager::Font::T1xs->new($file, $afm); |
| 63 |
unless ($font) { # the low-level code may miss some error handling |
| 64 |
Imager->_set_error(Imager->_error_as_msg); |
| 65 |
return; |
| 66 |
} |
| 67 |
return bless { |
| 68 |
t1font => $font, |
| 69 |
aa => $hsh{aa} || 0, |
| 70 |
file => $file, |
| 71 |
type => 't1', |
| 72 |
size => $hsh{size}, |
| 73 |
color => $hsh{color}, |
| 74 |
t1aa => $t1aa, |
| 75 |
}, $class; |
| 76 |
} |
| 77 |
|
| 78 |
sub _draw { |
| 79 |
my $self = shift; |
| 80 |
|
| 81 |
$self->_valid |
| 82 |
or return; |
| 83 |
|
| 84 |
my %input = @_; |
| 85 |
my $flags = ''; |
| 86 |
$flags .= 'u' if $input{underline}; |
| 87 |
$flags .= 's' if $input{strikethrough}; |
| 88 |
$flags .= 'o' if $input{overline}; |
| 89 |
my $aa = $input{aa} ? $self->{t1aa} : 0; |
| 90 |
if (exists $input{channel}) { |
| 91 |
$self->{t1font}->cp($input{image}{IMG}, $input{'x'}, $input{'y'}, |
| 92 |
$input{channel}, $input{size}, |
| 93 |
$input{string}, $input{align}, |
| 94 |
$input{utf8}, $flags, $aa) |
| 95 |
or return; |
| 96 |
} else { |
| 97 |
$self->{t1font}->text($input{image}{IMG}, $input{'x'}, $input{'y'}, |
| 98 |
$input{color}, $input{size}, |
| 99 |
$input{string}, $input{align}, $input{utf8}, $flags, $aa) |
| 100 |
or return; |
| 101 |
} |
| 102 |
|
| 103 |
return $self; |
| 104 |
} |
| 105 |
|
| 106 |
sub _bounding_box { |
| 107 |
my $self = shift; |
| 108 |
|
| 109 |
$self->_valid |
| 110 |
or return; |
| 111 |
|
| 112 |
my %input = @_; |
| 113 |
my $flags = ''; |
| 114 |
$flags .= 'u' if $input{underline}; |
| 115 |
$flags .= 's' if $input{strikethrough}; |
| 116 |
$flags .= 'o' if $input{overline}; |
| 117 |
my @bbox = $self->{t1font}->bbox($input{size}, $input{string}, |
| 118 |
$input{utf8}, $flags); |
| 119 |
unless (@bbox) { |
| 120 |
Imager->_set_error(Imager->_error_as_msg); |
| 121 |
return; |
| 122 |
} |
| 123 |
|
| 124 |
return @bbox; |
| 125 |
} |
| 126 |
|
| 127 |
# check if the font has the characters in the given string |
| 128 |
sub has_chars { |
| 129 |
my ($self, %hsh) = @_; |
| 130 |
|
| 131 |
$self->_valid |
| 132 |
or return; |
| 133 |
|
| 134 |
unless (defined $hsh{string}) { |
| 135 |
$Imager::ERRSTR = "No string supplied to \$font->has_chars()"; |
| 136 |
return; |
| 137 |
} |
| 138 |
if (wantarray) { |
| 139 |
my @result = $self->{t1font} |
| 140 |
->has_chars($hsh{string}, _first($hsh{'utf8'}, $self->{utf8}, 0)); |
| 141 |
unless (@result) { |
| 142 |
Imager->_set_error(Imager->_error_as_msg); |
| 143 |
return; |
| 144 |
} |
| 145 |
|
| 146 |
return @result; |
| 147 |
} |
| 148 |
else { |
| 149 |
my $result = $self->{t1font} |
| 150 |
->has_chars($hsh{string}, _first($hsh{'utf8'}, $self->{utf8}, 0)); |
| 151 |
unless (defined $result) { |
| 152 |
Imager->_set_error(Imager->_error_as_msg); |
| 153 |
return; |
| 154 |
} |
| 155 |
return $result; |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
sub utf8 { |
| 160 |
1; |
| 161 |
} |
| 162 |
|
| 163 |
sub can_glyph_names { |
| 164 |
1; |
| 165 |
} |
| 166 |
|
| 167 |
sub face_name { |
| 168 |
my ($self) = @_; |
| 169 |
|
| 170 |
$self->_valid |
| 171 |
or return; |
| 172 |
|
| 173 |
return $self->{t1font}->face_name(); |
| 174 |
} |
| 175 |
|
| 176 |
sub glyph_names { |
| 177 |
my ($self, %input) = @_; |
| 178 |
|
| 179 |
$self->_valid |
| 180 |
or return; |
| 181 |
|
| 182 |
my $string = $input{string}; |
| 183 |
defined $string |
| 184 |
or return Imager->_set_error("no string parameter passed to glyph_names"); |
| 185 |
my $utf8 = _first($input{utf8} || 0); |
| 186 |
|
| 187 |
my @result = $self->{t1font}->glyph_names($string, $utf8); |
| 188 |
unless (@result) { |
| 189 |
Imager->_set_error(Imager->_error_as_msg); |
| 190 |
return; |
| 191 |
} |
| 192 |
|
| 193 |
return @result; |
| 194 |
} |
| 195 |
|
| 196 |
sub set_aa_level { |
| 197 |
my ($self, $new_t1aa) = @_; |
| 198 |
|
| 199 |
if (!defined $new_t1aa || |
| 200 |
($new_t1aa != 1 && $new_t1aa != 2)) { |
| 201 |
Imager->_set_error("set_aa_level: parameter must be 1 or 2"); |
| 202 |
return; |
| 203 |
} |
| 204 |
|
| 205 |
if (ref $self) { |
| 206 |
$self->_valid |
| 207 |
or return; |
| 208 |
|
| 209 |
$self->{t1aa} = $new_t1aa; |
| 210 |
} |
| 211 |
else { |
| 212 |
$t1aa = $new_t1aa; |
| 213 |
} |
| 214 |
|
| 215 |
return 1; |
| 216 |
} |
| 217 |
|
| 218 |
sub _valid { |
| 219 |
my $self = shift; |
| 220 |
|
| 221 |
unless ($self->{t1font} && Scalar::Util::blessed($self->{t1font})) { |
| 222 |
Imager->_set_error("font object was created in another thread"); |
| 223 |
return; |
| 224 |
} |
| 225 |
|
| 226 |
return 1; |
| 227 |
} |
| 228 |
|
| 229 |
1; |
| 230 |
|
| 231 |
__END__ |
| 232 |
|
| 233 |
=head1 NAME |
| 234 |
|
| 235 |
Imager::Font::Type1 - low-level functions for Type1 fonts |
| 236 |
|
| 237 |
=head1 DESCRIPTION |
| 238 |
|
| 239 |
=for stopwords Freetype |
| 240 |
|
| 241 |
Imager::Font::T1 is deprecated. |
| 242 |
|
| 243 |
F<T1Lib> is unmaintained and has serious bugs when built on 64-bit |
| 244 |
systems. Freetype 2 has Type 1 font support and is supported by |
| 245 |
Imager via L<Imager::Font::FT2>. |
| 246 |
|
| 247 |
L<Imager::Font> creates a C<Imager::Font::Type1 object> when asked to create |
| 248 |
a font object based on a C<.pfb> file. |
| 249 |
|
| 250 |
See Imager::Font to see how to use this type. |
| 251 |
|
| 252 |
This class provides low-level functions that require the caller to |
| 253 |
perform data validation |
| 254 |
|
| 255 |
By default Imager no longer creates the F<t1lib.log> log file. You |
| 256 |
can re-enable that by calling Imager::init() with the C<t1log> option: |
| 257 |
|
| 258 |
Imager::init(t1log=>1); |
| 259 |
|
| 260 |
This must be called before creating any fonts. |
| 261 |
|
| 262 |
Currently specific to Imager::Font::Type1, you can use the following |
| 263 |
flags when drawing text or calculating a bounding box: |
| 264 |
|
| 265 |
=for stopwords overline strikethrough |
| 266 |
|
| 267 |
=over |
| 268 |
|
| 269 |
=item * |
| 270 |
|
| 271 |
C<underline> - Draw the text with an underline. |
| 272 |
|
| 273 |
=item * |
| 274 |
|
| 275 |
C<overline> - Draw the text with an overline. |
| 276 |
|
| 277 |
=item * |
| 278 |
|
| 279 |
C<strikethrough> - Draw the text with a strikethrough. |
| 280 |
|
| 281 |
=back |
| 282 |
|
| 283 |
Obviously, if you're calculating the bounding box the size of the line |
| 284 |
is included in the box, and the line isn't drawn :) |
| 285 |
|
| 286 |
=head2 Anti-aliasing |
| 287 |
|
| 288 |
T1Lib supports multiple levels of anti-aliasing, by default, if you |
| 289 |
request anti-aliased output, Imager::Font::T1 will use the maximum |
| 290 |
level. |
| 291 |
|
| 292 |
You can override this with the set_t1_aa() method: |
| 293 |
|
| 294 |
=over |
| 295 |
|
| 296 |
=item set_aa_level() |
| 297 |
|
| 298 |
Usage: |
| 299 |
|
| 300 |
$font->set_aa_level(1); |
| 301 |
Imager::Font::T1->set_aa_level(2); |
| 302 |
|
| 303 |
Sets the T1Lib anti-aliasing level either for the specified font, or |
| 304 |
for new font objects. |
| 305 |
|
| 306 |
The only parameter must be 1 or 2. |
| 307 |
|
| 308 |
Returns true on success. |
| 309 |
|
| 310 |
=back |
| 311 |
|
| 312 |
=head1 AUTHOR |
| 313 |
|
| 314 |
Addi, Tony |
| 315 |
|
| 316 |
=cut |