Selecting and Using a Font

To select a font you must create a specification that defines the characteristics of the font that you require. The fonts available are specific to the current graphics device (normally the screen device). The graphics device uses the Font and Bitmap server to access the Font Store. The Font Store finds the best match from the fonts installed.

The current font is part of the current graphics context. Once the font system has selected a font to match your specification you must update the current context to use the new font.

  1. Set up a font specification (TFontSpec).

  2. Create a CFont pointer. The font itself will be allocated by FBServ on the shared heap. This pointer will not be used to allocate or free any memory.

  3. Use the font spec and the font pointer to obtain a font from the Font Store. This must be done through the screen device.

  4. Set the graphic context's current font to the CFont obtained from the Font Store.

  5. Use the font.

  6. Tidy up by discarding the font from the graphics context and releasing it from the graphics device.

// Create a font specification
_LIT( KMyFontName,"Swiss") ;
const TInt KMyFontHeightInTwips = 240 ; // 12 point
TFontSpec myFontSpec( KMyFontName, KMyFontHeightInTwips ) ; 
    
// Create a font pointer (the font itself is on the FBServ shared heap)
CFont* myFont ;
    
// Fonts are graphics device specific.  In this case the graphics device is the screen
CGraphicsDevice* screenDevice = iCoeEnv->ScreenDevice() ;
    
// Get the font that most closely matches the font spec.
screenDevice->GetNearestFontToMaxHeightInTwips( myFont , myFontSpec ) ;
    
// Pass the font to the current graphics context
CWindowGc& gc = SystemGc() ;
gc.UseFont( myFont ) ;
    
// Use the gc to draw text.  Use the most appropriate CGraphicsContext::DrawText() function.
TPoint textPos( 0, myFont->AscentInPixels() ) ; // left hand end of baseline.
_LIT( KMyText, "Some text to write" ) ;
    
gc.DrawText( KMyText, textPos ) ; // Uses current pen etc.
    
// Tidy up.  Discard and release the font
gc.DiscardFont() ;
screenDevice->ReleaseFont( myFont ) ;

There are a number of DrawText() function in CGraphicsContext that you can use for positioning text at a specified position or within a rectangle. The pen and brush are those in the current context.

You can query the metrics of the font using CFont to ensure that your text is correctly located. You can also establish the size of individual characters and text strings.

Note: CFont is an abstract base class. You can use it to query a font supplied by the font system but you cannot instantiate it.

class CFont  // CFont is an abstract class.
    {
    
public:
    TUid TypeUid() const;
    TInt HeightInPixels() const;
    TInt AscentInPixels() const;
    TInt DescentInPixels() const;
    TInt CharWidthInPixels(TChar aChar) const;
    TInt TextWidthInPixels(const TDesC& aText) const;
    TInt BaselineOffsetInPixels() const;
    TInt TextCount(const TDesC& aText,TInt aWidthInPixels) const;
    TInt TextCount(const TDesC& aText,TInt aWidthInPixels,
                   TInt& aExcessWidthInPixels) const;
    
    TInt MaxCharWidthInPixels() const;
    TInt MaxNormalCharWidthInPixels() const;
    TFontSpec FontSpecInTwips() const;
    TCharacterDataAvailability GetCharacterData(TUint aCode, 
                   TOpenFontCharMetrics& aMetrics,
                   const TUint8*& aBitmap,
                   TSize& aBitmapSize) const;
    
    TBool GetCharacterPosition(TPositionParam& aParam) const;
    TInt WidthZeroInPixels() const;
    TInt MeasureText(const TDesC& aText, 
                   const TMeasureTextInput* aInput = NULL, 
                   TMeasureTextOutput* aOutput = NULL) const;
    
    static TBool CharactersJoin(TInt aLeftCharacter, TInt aRightCharacter);
    TInt ExtendedFunction(TUid aFunctionId, TAny* aParam = NULL) const;
    TBool GetCharacterPosition2(TPositionParam& aParam, RShapeInfo& aShapeInfo) const;
    TInt TextWidthInPixels(const TDesC& aText,const TMeasureTextInput* aParam) const;
    ....

Related concepts