Hi All,
I found a bug in my TrueTypeFont wrapper class, which relates to
wrapping text. I fixed the bug and it works adequately, but I was
wondering if anyone else on here has written a word wrapping function
and what algorithm they used.

My function looks like this...
function DrawText( aText : WideString; aWidth, aHeight : Integer ) :
PSDL_Surface; overload;

As you can see, I pass a string to the function and a width and height,
in pixels. The function goes off and and wraps the text so that it fits
within the width and height and returns a new SDL surface of the
specified size, with all the text drawn on it.

The algorithm I decided to use, basically starts from the end of the
string and works backwards until it find a space. It then sees if the
string from the beginning up to that space position fits into the
Width/height provided. If it does I store that into an array and proceed
to parse the rest of the text. In code it looks like this...
[pascal]
strChopped := aText; // Where aText is the original string
i := Length( strChopped );
while ( i <> 0 ) do
begin
if ( string( strChopped[ i ] ) <> ' ' ) and ( Integer( string( strChopped[ i ] ) ) <> 13 ) then
dec( i )
else
begin
dec( i );
if TTF_SizeUTF8( FFont, PChar( string( Copy( strChopped, 0, i ) ) ), textw, texth ) = 0 then
begin
if ( textw < aWidth )
and ( texth < aHeight ) then
begin
SetLength( strlist, Length( strlist ) + 1 );
strlist[ Length( strlist ) - 1 ] := Copy( strChopped, 0, i );
strChopped := Copy( strChopped, i + 2, Length( strChopped ) - ( i - 1 ) );
i := Length( strChopped );
if TTF_SizeUTF8( FFont, PChar( string( strChopped ) ), textw, texth ) = 0 then
begin
if ( textw < aWidth )
and ( texth < aHeight ) then
begin
SetLength( strlist, Length( strlist ) + 1 );
strlist[ Length( strlist ) - 1 ] := Copy( strChopped, 0, i );
strChopped := Copy( strChopped, i + 2, Length( strChopped ) - ( i - 1 ) );
i := Length( strChopped );
end;
end;
end;
end;
end;
end;[/pascal]

The TTF_Size* functions just return the supplied text's width and height.