Imager...::Wrap(3pm)
hey bestie!! π you just opened the Imager...::Wrap(3pm) page from the User Contributed Perl Documentation section, and we are about to make Imager::Font::Wrap make sense. sheβs giving simple wrapped text output energy, and honestly? iconic. β¨
πΈ NAME πΈ
Imager::Font::Wrap β simple wrapped text output. thatβs the name, thatβs the assignment, and she ate. π
β¨ SYNOPSIS β¨
okay so first the vibe check: hereβs the whole setup. keep these tokens exactly as they are, bestie β this is code, not a caption. π
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,
.... );
π DESCRIPTION π
okay so this is a simple text wrapper with options to control the layout of text within the line. no cap. π«Ά
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. thatβs the little trick, iykyk. π
π wrap_text() π
Draw word-wrapped text. thatβs her job. π
x,yβ The top-left corner of the rectangle the text is formatted into. Defaults to(0, 0).widthβ The width of the formatted text in pixels. Defaults to the horizontal gap between the top-left corner and the right edge of the image. If no image is supplied then this is required.heightβ The maximum height of the formatted text in pixels. Not required.saveposβ The amount of text consumed (as a count of characters) will be stored into the scalar this refers to.
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");
}
imageβ The image to render the text to. Can be supplied asundefor not provided to simply calculate the bounding box.fontβ The font used to render the text. Required.sizeβ The size to render the font in. Defaults to the size stored in the font object. Required if it isnβt stored in the font object.stringβ The text to render. This can contain non-white-space, blanks (ASCII0x20), and newlines. Newlines must match/(?:\x0A\x0D?|\x0D\x0A?)/. White-space other than blanks and newlines are completely ignored.justifyβ The way text is formatted within each line. Possible values include:leftβ left aligned against the left edge of the text box.rightβ right aligned against the right edge of the text box.centerβ centered horizontally in the text box.fillβ all but the final line of the paragraph has spaces expanded so that the line fills from the left to the right edge of the text box.
linegapβ Gap between lines of text in pixels. This is in addition to the size from$font->font_height. Can be positive or negative. Default0.
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);
π«Ά BUGS π«Ά
okay so bug time, bestie β not the fun kind of tea, but we still gotta spill it. π
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. periodt.
π AUTHOR π
Tony Cook β <tony@develop-help.com> π«Ά
π SEE ALSO π
Imager(3), Imager::Font(3) β go read those too, bestie. π
mwah π this page is from perl v5.42.3, dated 2026-08-19, and itβs the Imager...::Wrap(3pm) manual. byeee bestie, go wrap some text and slay ππ
π¦ where this page comes from
- ubuntu stonking: libimager-perl 1.035+dfsg-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms