EXPORT_C TInt AknPhoneNumberTextUtils::CharsThatFitOnRight( const TDesC& aText, TInt aLineWidth, const CFont& aFont ) { // Current candidate text for fitting text TPtrC currentText(aText); // These two variables are converged by this algorithm until they differ by one // Set max chars to be total length + 1, for the sake of the algorithm TInt minCharsFromRightThatDoNotFit( aText.Length() + 1 ); TInt maxCharsFromRightThatFit(0); // must start at 0 // some incidentally used lengths TInt currentLength(0); // arbitrary init value TInt charsThatFitOnLeft(0); // arbitrary init value TInt newLength(0); // arbitrary init value while ( (minCharsFromRightThatDoNotFit - maxCharsFromRightThatFit) != 1 ) { // At the top of the loop, all state is held by currentText itself, and the two // maxCharsXXX variables. currentLength = currentText.Length(); charsThatFitOnLeft = aFont.TextCount( currentText, aLineWidth); if ( charsThatFitOnLeft == currentLength ) // Does this text fit ? { maxCharsFromRightThatFit = Max( maxCharsFromRightThatFit, charsThatFitOnLeft ); // If this text cannot be made any bigger, and still fit, then bail out if ( (maxCharsFromRightThatFit + 1) >= minCharsFromRightThatDoNotFit ) { // We can exit the loop now. Just fall out and the while test will handle it. break; } else // if this string did fit, and it might have been too small, try making it bigger { // we know this is still be < minCharsFromRightThatDoNotFit newLength = maxCharsFromRightThatFit + 1; } } else // currentText does not fit. Repeat loop { // we know that this many characters did not fit on the line minCharsFromRightThatDoNotFit = Min( minCharsFromRightThatDoNotFit, currentLength ); // Try using the number that fit on the left as the number we will fit on the right. newLength = charsThatFitOnLeft; } currentText.Set(aText.Right(newLength)); } return maxCharsFromRightThatFit; }
EXPORT_C void TextUtils::ClipToFit(TDes& aBuffer,const CFont& aFont,TInt aMaxWidthInPixels,TChar aAlternativeEnd) /** Clips text to fit into a maximum width. If the text is too wide to fit in the width when displayed in aFont, it is truncated and the specified character (by default, a horizontal ellipsis) is appended to it. @param aBuffer A buffer containing the text to clip. @param aFont The font. @param aMaxWidthInPixels The maximum width in pixels. @param aAlternativeEnd The Unicode character to append to the buffer if truncated. By default, this is the horizontal ellipsis. */ { TInt textWidth=aFont.TextWidthInPixels(aBuffer); if (textWidth<=aMaxWidthInPixels) return; TBuf<1> ellipse; ellipse.Append(aAlternativeEnd); TInt extraWidth=aFont.TextWidthInPixels(ellipse); TInt cutOff=aFont.TextCount(aBuffer,aMaxWidthInPixels-extraWidth); aBuffer.SetLength(cutOff); aBuffer.Append(ellipse); }