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.


Draw word-wrapped text.

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>


Imager(3), Imager::Font(3)