GD::Text - Text utilities for use with GD
use GD; use GD::Text;
my $gd_text = GD::Text->new() or die GD::Text::error(); $gd_text->set_font('funny.ttf', 12) or die $gd_text->error; $gd_text->set_font(gdTinyFont); $gd_text->set_font(GD::Font::Tiny); ... $gd_text->set_text($string); my ($w, $h) = $gd_text->get('width', 'height');
if ($gd_text->is_ttf) { ... }
Or alternatively
my $gd_text = GD::Text->new( text => 'Some text', font => 'funny.ttf', ptsize => 14, );
You can only use TrueType fonts with version of GD > 1.20, and then only if compiled with support for this. If you attempt to do it anyway, you will get errors.
If you want to refer to builtin GD fonts by their short name ("gdTinyFont", "gdGiantFont"), you will need to "use" the GD module as well as one the GD::Text modules, because it is GD that exports those names into your name space. If you don't like that, use the longer alternatives ("GD::Font-"Giant>) instead.
If you are not using an absolute path to the font file, you can leave of the .ttf file extension, but you have to append it for absolute paths:
$gd_text->set_font('arial', 12); # but $gd_text->set_font('/usr/fonts/arial.ttf', 12);
The first argument can be a reference to an array of fonts. The first font from the array that can be found will be used. This allows you to do something like
$gd_text->font_path( '/usr/share/fonts:/usr/fonts'); $gd_text->set_font( ['verdana', 'arial', gdMediumBoldFont], 14);
if you'd prefer verdana to be used, would be satisfied with arial, but if none of that is available just want to make sure you can fall back on something that will be available.
Returns true on success, false on error.
Returns true on success, false on any error, even if it was partially successful. When an error is returned, no guarantees are given about the correctness of the attributes.
The attributes that can be retrieved are all the ones that can be set, and:
Note that some of these parameters (char_up, char_down and space) are generic font properties, and not necessarily a property of the text that is set.
The use of this method is vaguely deprecated.
This depends on whether your version of GD is newer than 1.19 and has TTF support compiled into it.
GD::Text->font_path('/usr/ttfonts'); # Unix GD::Text->font_path('c:/fonts'); # MS-OS
Any font name that is not an absolute path will first be looked for in the current directory, and then in /usr/ttfonts (c:\fonts).
GD::Text->font_path('/usr/ttfonts:.:lib/fonts'); # Unix GD::Text->font_path('c:/fonts;.;f:/fonts'); # MS-OS
Any font name that is not an absolute path will first be looked for in /usr/ttfonts (c:\fonts), then in the current directory. and then in lib/fonts (f:\fonts), relative to the current directory.
GD::Text->font_path(undef);
Font files are only looked for in the current directory.
FONT_PATH is initialised at module load time from the environment variables FONT_PATH or, if that's not present, TTF_FONT_PATH, or TT_FONT_PATH.
Returns the value the font path is set to. If called without arguments "font_path" returns the current font path.
Note: This currently only works for unices, and (hopefully) for Microsoft based OS's. If anyone feels the urge to have a look at the code, and send me patches for their OS, I'd be most grateful)
The font height gets estimated by building a string with all printable characters (with an ordinal value between 0 and 255) that pass the POSIX::isprint() test (and not the isspace() test). If your system doesn't have POSIX, I make an approximation that may be false. Under Perl 5.8.0 the [[:print:]] character class is used, since the POSIX is*() functions don't seem to work correctly.
The whole font path thing works well on Unix, but probably not very well on other OS's. This is only a problem if you try to use a font path. If you don't use a font path, there should never be a problem. I will try to expand this in the future, but only if there's a demand for it. Suggestions welcome.
Закладки на сайте Проследить за страницей |
Created 1996-2024 by Maxim Chirkov Добавить, Поддержать, Вебмастеру |