File: T1.pm

package info (click to toggle)
libimager-perl 1.012%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,376 kB
  • sloc: perl: 31,562; ansic: 27,846; makefile: 53; cpp: 4
file content (313 lines) | stat: -rw-r--r-- 6,414 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package Imager::Font::T1;
use 5.006;
use strict;
use Imager::Color;
our @ISA = qw(Imager::Font);
use Scalar::Util ();

BEGIN {
  our $VERSION = "1.027";

  require XSLoader;
  XSLoader::load('Imager::Font::T1', $VERSION);
}


*_first = \&Imager::Font::_first;

my $t1aa = 2;

sub new {
  my $class = shift;
  my %hsh=(color=>Imager::Color->new(255,0,0,255),
	   size=>15,
	   @_);

  unless ($hsh{file}) {
    $Imager::ERRSTR = "No font file specified";
    return;
  }
  unless (-e $hsh{file}) {
    $Imager::ERRSTR = "Font file $hsh{file} not found";
    return;
  }
  unless ($Imager::formats{t1}) {
    $Imager::ERRSTR = "Type 1 fonts not supported in this build";
    return;
  }
  # we want to avoid T1Lib's file search mechanism
  unless ($hsh{file} =~ m!^/!
	  || $hsh{file} =~ m!^\.\/?/!
	  || $^O =~ /^(MSWin32|cygwin)$/ && $hsh{file} =~ /^[a-z]:/i) {
    $hsh{file} = './' . $hsh{file};
  }

  if($hsh{afm}) {
	  unless (-e $hsh{afm}) {
	    $Imager::ERRSTR = "Afm file $hsh{afm} not found";
	    return;
	  }
	  unless ($hsh{afm} =~ m!^/!
		  || $hsh{afm} =~ m!^\./!
		  || $^O =~ /^(MSWin32|cygwin)$/ && $hsh{file} =~ /^[a-z]:/i) {
	    $hsh{file} = './' . $hsh{file};
	  }
  } else {
	  $hsh{afm} = 0;
  }

  my $font = Imager::Font::T1xs->new($hsh{file},$hsh{afm});
  unless ($font) { # the low-level code may miss some error handling
    Imager->_set_error(Imager->_error_as_msg);
    return;
  }
  return bless {
		t1font    => $font,
		aa    => $hsh{aa} || 0,
		file  => $hsh{file},
		type  => 't1',
		size  => $hsh{size},
		color => $hsh{color},
		t1aa  => $t1aa,
	       }, $class;
}

sub _draw {
  my $self = shift;

  $self->_valid
    or return;

  my %input = @_;
  my $flags = '';
  $flags .= 'u' if $input{underline};
  $flags .= 's' if $input{strikethrough};
  $flags .= 'o' if $input{overline};
  my $aa = $input{aa} ? $self->{t1aa} : 0;
  if (exists $input{channel}) {
    $self->{t1font}->cp($input{image}{IMG}, $input{'x'}, $input{'y'},
		    $input{channel}, $input{size},
		    $input{string}, $input{align},
                    $input{utf8}, $flags, $aa)
      or return;
  } else {
    $self->{t1font}->text($input{image}{IMG}, $input{'x'}, $input{'y'}, 
		      $input{color}, $input{size}, 
		      $input{string}, $input{align}, $input{utf8}, $flags, $aa)
      or return;
  }

  return $self;
}

sub _bounding_box {
  my $self = shift;

  $self->_valid
    or return;

  my %input = @_;
  my $flags = '';
  $flags .= 'u' if $input{underline};
  $flags .= 's' if $input{strikethrough};
  $flags .= 'o' if $input{overline};
  my @bbox =  $self->{t1font}->bbox($input{size}, $input{string},
				    $input{utf8}, $flags);
  unless (@bbox) {
    Imager->_set_error(Imager->_error_as_msg);
    return;
  }

  return @bbox;
}

# check if the font has the characters in the given string
sub has_chars {
  my ($self, %hsh) = @_;

  $self->_valid
    or return;

  unless (defined $hsh{string}) {
    $Imager::ERRSTR = "No string supplied to \$font->has_chars()";
    return;
  }
  if (wantarray) {
    my @result = $self->{t1font}
      ->has_chars($hsh{string}, _first($hsh{'utf8'}, $self->{utf8}, 0));
    unless (@result) {
      Imager->_set_error(Imager->_error_as_msg);
      return;
    }

    return @result;
  }
  else {
    my $result = $self->{t1font}
      ->has_chars($hsh{string}, _first($hsh{'utf8'}, $self->{utf8}, 0));
    unless (defined $result) {
      Imager->_set_error(Imager->_error_as_msg);
      return;
    }
    return $result;
  }
}

sub utf8 {
  1;
}

sub can_glyph_names {
  1;
}

sub face_name {
  my ($self) = @_;

  $self->_valid
    or return;

  return $self->{t1font}->face_name();
}

sub glyph_names {
  my ($self, %input) = @_;

  $self->_valid
    or return;

  my $string = $input{string};
  defined $string
    or return Imager->_set_error("no string parameter passed to glyph_names");
  my $utf8 = _first($input{utf8} || 0);

  my @result = $self->{t1font}->glyph_names($string, $utf8);
  unless (@result) {
    Imager->_set_error(Imager->_error_as_msg);
    return;
  }

  return @result;
}

sub set_aa_level {
  my ($self, $new_t1aa) = @_;

  if (!defined $new_t1aa ||
      ($new_t1aa != 1 && $new_t1aa != 2)) {
    Imager->_set_error("set_aa_level: parameter must be 1 or 2");
    return;
  }

  if (ref $self) {
    $self->_valid
      or return;

    $self->{t1aa} = $new_t1aa;
  }
  else {
    $t1aa = $new_t1aa;
  }

  return 1;
}

sub _valid {
  my $self = shift;

  unless ($self->{t1font} && Scalar::Util::blessed($self->{t1font})) {
    Imager->_set_error("font object was created in another thread");
    return;
  }

  return 1;
}

1;

__END__

=head1 NAME

  Imager::Font::Type1 - low-level functions for Type1 fonts

=head1 DESCRIPTION

=for stopwords Freetype

Imager::Font::T1 is deprecated.

F<T1Lib> is unmaintained and has serious bugs when built on 64-bit
systems.  Freetype 2 has Type 1 font support and is supported by
Imager via L<Imager::Font::FT2>.

L<Imager::Font> creates a C<Imager::Font::Type1 object> when asked to create
a font object based on a C<.pfb> file.

See Imager::Font to see how to use this type.

This class provides low-level functions that require the caller to
perform data validation

By default Imager no longer creates the F<t1lib.log> log file.  You
can re-enable that by calling Imager::init() with the C<t1log> option:

  Imager::init(t1log=>1);

This must be called before creating any fonts.

Currently specific to Imager::Font::Type1, you can use the following
flags when drawing text or calculating a bounding box:

=for stopwords overline strikethrough

=over

=item *

C<underline> - Draw the text with an underline.

=item *

C<overline> - Draw the text with an overline.

=item *

C<strikethrough> - Draw the text with a strikethrough.

=back

Obviously, if you're calculating the bounding box the size of the line
is included in the box, and the line isn't drawn :)

=head2 Anti-aliasing

T1Lib supports multiple levels of anti-aliasing, by default, if you
request anti-aliased output, Imager::Font::T1 will use the maximum
level.

You can override this with the set_t1_aa() method:

=over

=item set_aa_level()

Usage:

  $font->set_aa_level(1);
  Imager::Font::T1->set_aa_level(2);

Sets the T1Lib anti-aliasing level either for the specified font, or
for new font objects.

The only parameter must be 1 or 2.

Returns true on success.

=back

=head1 AUTHOR

Addi, Tony

=cut