Imager::Font::Wrap(3pm) | User Contributed Perl Documentation | Imager::Font::Wrap(3pm) |
Imager::Font::Wrap - simple wrapped text output
use Imager::Font::Wrap; my $img = Imager->new(xsize=>$xsize, ysize=>$ysize); my $font = Imager::Font->new(file=>$fontfile); my $string = "..."; # text with or without newlines Imager::Font::Wrap->wrap_text( image => $img, font => $font, string => $string, x => $left, y => $top, width => $width, .... );
This is a simple text wrapper with options to control the layout of text within the line.
You can control the position, width and height of the text with the "image", "x", "y", "width" and "height" options.
You can simply calculate space usage by setting "image" to "undef", or set "savepos" to see how much text can fit within the given "height".
my $pagenum = 1; my $string = "..."; my $font = ...; my $savepos; while (length $string) { my $img = Imager->new(xsize=>$xsize, ysize=>$ysize); Imager::Font::Wrap->wrap_text(string=>$string, font=>$font, image=>$img, savepos => \$savepos) or die $img->errstr; $savepos > 0 or die "Could not fit any text on page\n"; $string = substr($string, $savepos); $img->write(file=>"page$pagenum.ppm"); }
Newlines must match /(?:\x0A\x0D?|\x0D\x0A?)/. White-space other than blanks and newlines are completely ignored.
The way text is formatted within each line. Possible values include:
Any other parameters are passed onto Imager::Font->draw().
Returns a list:
($left, $top, $right, $bottom)
which are the bounds of the space used to layout the text.
If "height" is set then this is the space used within that height.
You can use this to calculate the space required to format the text before doing it:
my ($left, $top, $right, $bottom) = Imager::Font::Wrap->wrap_text(string => $string, font => $font, width => $xsize); my $img = Imager->new(xsize=>$xsize, ysize=>$bottom); Imager::Font::Wrap->wrap_text(string => $string, font => $font, width => $xsize, image => $image);
Imager::Font can handle UTF-8 encoded text itself, but this module doesn't support that (and probably won't). This could probably be done with regex magic.
Currently ignores the "sizew" parameter, if you supply one it will be supplied to the draw() function and the text will be too short or too long for the "width".
Uses a simplistic text model, which is why there's no hyphenation, and no tabs.
Tony Cook <tony@develop-help.com>
2023-01-11 | perl v5.36.0 |