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); }
CFont* CMainMenuListContainer::FindLargestPossibleFontL(const TDesC& aTextToFit, TInt aMaxWidthInPixels, enum TAknLogicalFontId aPreferredLogicalFontId) const { TInt fontMinimizerFactorInPixels = 2; // Get the screen device so that we can calc the twips // per pixel TPixelsTwipsAndRotation twips; CWsScreenDevice* screenDev = CEikonEnv::Static()->ScreenDevice(); screenDev->GetScreenModeSizeAndRotation(screenDev->CurrentScreenMode(), twips); // Calc the twips per pixel TReal twipsPerPixel = twips.iTwipsSize.iHeight / TReal(twips.iPixelSize.iHeight); // Get the preferred logical font from the font store const CFont* preferredFont = AknLayoutUtils:: FontFromId(aPreferredLogicalFontId); TFontSpec fontSpec = preferredFont->FontSpecInTwips(); // Get the font that matches the fontspec the best CFont* fontToUse; screenDev-> GetNearestFontToDesignHeightInTwips(fontToUse, fontSpec); TInt fontMinimizerFactorInTwips = TInt(fontMinimizerFactorInPixels * twipsPerPixel); while (aMaxWidthInPixels < fontToUse->TextWidthInPixels(aTextToFit)) { // The text didnt fit within the given space, make the font // a bit smaller and try again screenDev->ReleaseFont(fontToUse); fontSpec.iHeight -= fontMinimizerFactorInTwips; screenDev-> GetNearestFontToDesignHeightInTwips(fontToUse, fontSpec); } // Return the font, the caller has to release the font // from the CWsScreenDevice return fontToUse; }