[futurebasic] truncating text

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : September 2001 : Group Archive : Group : All Groups

From: jonathan <jonnnathan@...>
Date: Thu, 20 Sep 2001 23:32:02 +0200
i recently needed to truncate a text, and found it annoying that the text
always seemed to have just one letter after a space.
example: "Times New R..."

the [self-contained] routine below attempts to lop off words, starting from
the back, and only reverts to 'ugly' truncation when the lopping off [called
intelligent] in the code, would make a text smaller than your desired
minimum value.

for completeness sake, i also included middle truncation.

of course, alain will reduce this to just one line of code, but that's why i
posted it :-)

:-j


'------------- start code ---------------------------------------------
/*   truncates a passed string according to 'myType' and returns it.
     myTargetStr: the passed pascal string
     myType: _zTrue -> truncate at end
             _false -> truncate in middle
     myLen: length in pixels that string must fit in
     myMin: if intelligent truncation makes it smaller than this
            then use standard truncation.
*/
local mode
local fn doTruncStr$( myTargetStr as str255,myType as int, [+]
            myLen as int,myMin as int)
dim myCntr     as int
dim myString   as str255
'
long if( myType)
long if( fn stringwidth( myTargetStr) > myLen)
// try intelligent truncation first
myString = myTargetStr
for myCntr = myTargetStr[0] to 1 step -1
long if( myString[ myCntr] = 32)
myString[0] = myCntr -1
if( fn stringwidth( myString) < myLen) then exit for
end if
next

long if( FN stringwidth( myString) > myMin)
myTargetStr = myString
xelse
myLen = fn TruncString( myLen,myTargetStr,0)
end if
end if

xelse
myLen = fn TruncString( myLen,myTargetStr,&h4000)
end if

end fn = myTargetStr
'------------ end code ------------------------------------