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 as undef or 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 (ASCII 0x20), 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. Default 0.

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