in reply to Formatting block of text

The best suggestion I know of is to use Font::TTFMetrics (if you are using TrueType fonts or can find/convert your font to an equivalent), or Image::Magick for other font types. You would then probably have to go word by word (or break-by-break if you're using hyphenation) and calculate the length. The other option would be to use a D&C algorithm and keep splitting the string until it falls within the desired min/max width. With Font::TTFMetrics, you can calculate the width of a string something like so:

use Font::TTFMetrics; my $resolution = 72; # 72dpi typical, 96, 100, 120 common my $pointsize = 12; # Whatever size the target text is my $string = 'Some string'; my $font = Font::TTFMetrics->new('/path/to/Font.ttf'); my $font_units = $font->string_width($string); my $pixels = $font_units * $pointsize * $resolution / (72 * $font->get_units_per_em); printf "Width of `%s' is %.2f font units, or %d pixels\n", $string, $font_units, $pixels;

Hope this helps!