# Test it with soft and hard break: my $textline = 'This is a very long text and I think it breaks on end of line. But you don\'t know it already and so wie test and test and test. And now comes a very long line for hard break_because_we_dont_take_space_but_underline_for_word_seperating_and_now_we_look_what_happens_with_this_line. And? How looks it?'; my $column_width = 280; $line_spacing = 2; # without 'my' because I use this code in another content for (my $fontsize = 5; $fontsize <= 21; $fontsize += 3) { # for each font size 5 up to 20 in step 3 my @word = split / /,$textline; # split text into words my (@zeile,$word_new,$width,$inserted); my $myline = 0; while (scalar @word) { $zeile[$myline] = '' if !defined $zeile[$myline]; # prevent "use of uninitialized value...." $word_new = shift @word; # get next word $width = $page->string_width($font,$zeile[$myline] . ' ' . $word_new) * $fontsize; # width of line inc. new word $width = ($width - int($width) ? int($width) + 1 : $width); # round up if ($width > $column_width) { # new line to long with new word if ($zeile[$myline] ne '') { $myline ++; unshift @word,$word_new; # back to list for new width check in next round } else { # word to long for one line, hard break $inserted = 0; for (my $xpos = 0; $xpos < length($word_new); $xpos ++) { $width = $page->string_width($font,substr($word_new,0,$xpos + 1)) * $fontsize; # width of line inc. new word $width = ($width - int($width) ? int($width) + 1 : $width); # round up if ($width > $column_width) { # hard break $zeile[$myline] = substr($word_new,0,$xpos); # take well-fitting portion in line $myline ++; unshift @word,substr($word_new,$xpos); # put rest back to list für next round $inserted = 1; last; } } if (!$inserted) { # word alone is well-fitting for one line $zeile[$myline] = $word_new; $myline ++; } } } else { # add word to line soft break $zeile[$myline] .= ($zeile[$myline] ne '' ? ' ' : '') . $word_new; # insert splited blank } } while (scalar @zeile) { $page->stringl($font,$fontsize,295,$position_y,shift @zeile); # print out line $position_y -= ($fontsize+$line_spacing); # cursor to next line } }