use Text::Flow; use Font::TTFMetrics; my $text = 'asdfk lasdjf asd flkasd fkjasdf la ksdf asdlkja sdkf laksdf laksdj ajsd lkasdf alksdj flkajsdl falsd flkasd asd flkaj sdlkfja lsf alsdkf lksadjfl asdfkj laksdf a sdkjf laksdjf kasdf lkasdfkja sdflka sdf asd flkajds lfka sdflak sdflkajsd fkjal j adsfkj alsdf ads Some string'; $text =~ s/\n/ /g; my $resolution = 72; # 72dpi typical, 96, 100, 120 common my $pointsize = 12; # Whatever size the target text is my $font = Font::TTFMetrics->new('georgia.ttf'); my $width = 200; # Constrain to this number of pixels in width. my $flow = Text::Flow->new( check_height => sub { return 1; # Should really enforce height constraint here }, wrapper => Text::Flow::Wrap->new( check_width => sub { my $string = shift; my $font_units = $font->string_width($string); my $pixels = $font_units * $pointsize * $resolution / (72 * $font->get_units_per_em); return $pixels < $width; }, ) ); my ($reflow) = $flow->flow($text); print $reflow;