5 use vars qw(%T1_Paths %TT_Paths %T1_Cache %TT_Cache $TT_CSize $TT_CSize $T1AA);
7 # This class is a container
8 # and works for both truetype and t1 fonts.
11 # $T1AA is in there because for some reason (probably cache related) antialiasing
12 # is a system wide setting in t1 lib.
15 if (!defined $T1AA or $_[0] != $T1AA) {
16 Imager::i_t1_set_aa($_[0]);
22 # 1. start by checking if file is the parameter
23 # 1a. if so qualify path and compare to the cache.
24 # 2a. if in cache - take it's id from there and increment count.
31 my %hsh=(color=>Imager::Color->new(255,0,0,0),
39 if ( $file !~ m/^\// ) {
42 $Imager::ERRSTR="Font $file not found";
48 if (!defined($type) or $type !~ m/^(t1|tt)/) {
49 $type='tt' if $file =~ m/\.ttf$/i;
50 $type='t1' if $file =~ m/\.pfb$/i;
52 if (!defined($type)) {
53 $Imager::ERRSTR="Font type not found";
57 $Imager::ERRSTR="No font file specified";
61 if (!$Imager::formats{$type}) {
62 $Imager::ERRSTR="`$type' not enabled";
66 # here we should have the font type or be dead already.
69 $id=Imager::i_t1_new($file);
73 $id=Imager::i_tt_new($file);
76 $self->{'aa'}=$hsh{'aa'}||'0';
77 $self->{'file'}=$file;
79 $self->{'type'}=$type;
80 $self->{'size'}=$hsh{'size'};
81 $self->{'color'}=$hsh{'color'};
95 if (!exists $input{'string'}) { $Imager::ERRSTR='string parameter missing'; return; }
97 if ($self->{type} eq 't1') {
98 @box=Imager::i_t1_bbox($self->{id}, defined($input{size}) ? $input{size} :$self->{size},
99 $input{string}, length($input{string}));
101 if ($self->{type} eq 'tt') {
102 @box=Imager::i_tt_bbox($self->{id}, defined($input{size}) ? $input{size} :$self->{size},
103 $input{string}, length($input{string}));
106 if(exists $input{'x'} and exists $input{'y'}) {
107 my($gdescent, $gascent)=@box[1,3];
108 $box[1]=$input{'y'}-$gascent; # top = base - ascent (Y is down)
109 $box[3]=$input{'y'}-$gdescent; # bottom = base - descent (Y is down, descent is negative)
110 $box[0]+=$input{'x'};
111 $box[2]+=$input{'x'};
112 } elsif (exists $input{'canon'}) {
113 $box[3]-=$box[1]; # make it cannoical (ie (0,0) - (width, height))
127 Imager::Font - Font handling for Imager.
131 $t1font = Imager::Font->new(file => 'pathtofont.pfb');
132 $ttfont = Imager::Font->new(file => 'pathtofont.ttf');
134 $blue = Imager::Color->new("#0000FF");
135 $font = Imager::Font->new(file => 'pathtofont.ttf',
144 $ascent) = $font->bounding_box(string=>"Foo");
146 $logo = $font->logo(text => "Slartibartfast Enterprises",
150 # logo is proposed - doesn't exist yet
153 $img->string(font => $font,
161 # Documentation in Imager.pm
165 This module handles creating Font objects used by imager. The module
166 also handles querying fonts for sizes and such. If both T1lib and
167 freetype were avaliable at the time of compilation then Imager should
168 be able to work with both truetype fonts and t1 postscript fonts. To
169 check if Imager is t1 or truetype capable you can use something like
173 print "Has truetype" if $Imager::formats{tt};
174 print "Has t1 postscript" if $Imager::formats{t1};
181 This creates a font object to pass to functions that take a font argument.
183 $font = Imager::Font->new(file => 'denmark.ttf',
188 This creates a font which is the truetype font denmark.ttf. It's
189 default color is $blue, default size is 30 pixels and it's rendered
190 antialised by default. Imager can see which type of font a file is by
191 looking at the suffix of the filename for the font. A suffix of 'ttf'
192 is taken to mean a truetype font while a suffix of 'pfb' is taken to
193 mean a t1 postscript font. If Imager cannot tell which type a font is
194 you can tell it explicitly by using the C<type> parameter:
196 $t1font = Imager::Font->new(file => 'fruitcase', type => 't1');
197 $ttfont = Imager::Font->new(file => 'arglebarf', type => 'tt');
199 If any of the C<color>, C<size> or C<aa> parameters are omitted when
200 calling C<Imager::Font->new()> the they take the following values:
203 color => Imager::Color->new(255, 0, 0, 0); # this default should be changed
208 Returns the bounding box for the specified string. Example:
215 $ascent) = $font->bounding_box(string => "A Fool");
217 The C<$neg_width> is the relative start of a the string. In some
218 cases this can be a negative number, in that case the first letter
219 stretches to the left of the starting position that is specified in
220 the string method of the Imager class. <$global_descent> is the how
221 far down the lowest letter of the entire font reaches below the
222 baseline (this is often j). C<$pos_width> is how wide the string from
223 from the starting position is. The total width of the string is
224 C<$pos_width-$neg_width>. C<$descent> and C<$ascent> are the as
225 <$global_descent> and <$global_ascent> except that they are only for
226 the characters that appear in the string. Obviously we can stuff all
227 the results into an array just as well:
229 @metrics = $font->bounding_box(string => "testing 123");
231 Note that extra values may be added, so $metrics[-1] isn't supported.
232 It's possible to translate the output by a passing coordinate to the
235 @metrics = $font->bounding_box(string => "testing 123", x=>45, y=>34);
237 This gives the bounding box as if the string had been put down at C<(x,y)>
238 By giving bounding_box 'canon' as a true value it's possible to measure
239 the space needed for the string:
241 @metrics = $font->bounding_box(string=>"testing",size=>15,canon=>1);
243 This returns tha same values in $metrics[0] and $metrics[1],
246 $bbox[2] - horizontal space taken by glyphs
247 $bbox[3] - vertical space taken by glyphs
253 This is a method of the Imager class but because it's described in
254 here since it belongs to the font routines. Example:
257 $img=read(file=>"test.jpg");
258 $img->string(font=>$t1font,
264 $img->write(file=>"testout.jpg");
266 This would put a 40 pixel high text in the top left corner of an
267 image. If you measure the actuall pixels it varies since the fonts
268 usually do not use their full height. It seems that the color and
269 size can be specified twice. When a font is created only the actual
270 font specified matters. It his however convenient to store default
271 values in a font, such as color and size. If parameters are passed to
272 the string function they are used instead of the defaults stored in
275 If string() is called with the C<channel> parameter then the color
276 isn't used and the font is drawn in only one channel of the image.
277 This can be quite handy to create overlays. See the examples for tips
280 Sometimes it is necessary to know how much space a string takes before
281 rendering it. The bounding_box() method described earlier can be used
287 This method doesn't exist yet but is under consideration. It would mostly
288 be helpful for generating small tests and such. Its proposed interface is:
290 $img = $font->logo(string=>"Plan XYZ", color=>$blue, border=>7);
292 This would be nice for writing (admittedly multiline) one liners like:
294 Imager::Font->new(file=>"arial.ttf", color=>$blue, aa=>1)
295 ->string(text=>"Plan XYZ", border=>5)
296 ->write(file=>"xyz.png");
304 Arnar M. Hrafnkelsson, addi@umich.edu
305 And a great deal of help from others - see the README for a complete
311 http://www.eecs.umich.edu/~addi/perl/Imager/