<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE html
  PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en" xml:lang="en">
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta name="copyright" content="(C) Copyright 2010" />
  <meta name="DC.rights.owner" content="(C) Copyright 2010" />
  <meta name="DC.Type" content="concept" />
  <meta name="DC.Title" content="Selecting and Using a Font" />
  <meta name="DC.Relation" scheme="URI" content="GUID-98B85D7F-7BD9-5056-A99F-0BC99D921B87.html" />
  <meta name="DC.Relation" scheme="URI" content="GUID-72986B3C-047C-5411-8F15-BC9C65C3289C.html" />
  <meta name="DC.Relation" scheme="URI" content="index.html" />
  <meta name="DC.Relation" scheme="URI" content="GUID-32E29020-1956-461A-B79A-1492E06049E7.html" />
  <meta name="DC.Relation" scheme="URI" content="GUID-1DCA2F4D-ABE6-52A0-AC4E-5AAC1AB5909D.html" />
  <meta name="DC.Relation" scheme="URI" content="GUID-762A665F-43D0-53ED-B698-0CBD3AC46391.html" />
  <meta name="DC.Relation" scheme="URI" content="GUID-98B85D7F-7BD9-5056-A99F-0BC99D921B87.html" />
  <meta name="DC.Relation" scheme="URI" content="GUID-C1B080D9-9C6C-520B-B73E-4EB344B1FC5E.html" />
  <meta name="DC.Relation" scheme="URI" content="GUID-90644B52-69D7-595C-95E3-D6F7A30C060D.html" />
  <meta name="DC.Relation" scheme="URI" content="GUID-71DADA82-3ABC-52D2-8360-33FAEB2E5DE9.html" />
  <meta name="DC.Relation" scheme="URI" content="GUID-416A3756-B5D5-5BCD-830E-2371C5F6B502.html" />
  <meta name="DC.Format" content="XHTML" />
  <meta name="DC.Identifier" content="GUID-78FB26E2-AA60-5531-B2FE-4FA6C88F2D47" />
  <meta name="DC.Language" content="en" />
  <link rel="stylesheet" type="text/css" href="commonltr.css" />
  <title>
   Selecting and Using a Font
  </title>
  <link type="text/css" rel="stylesheet" href="css/common.css" media="screen"/>
<link type="text/css" rel="stylesheet" href="css/sdl.css" media="screen"/>

<!--[if IE]>
<link href="css/iefix.css" rel="stylesheet" type="text/css" media="screen" />
<![endif]-->
  <link rel="stylesheet" type="text/css" href="nokiacxxref.css" />
 </head>
 <body id="GUID-78FB26E2-AA60-5531-B2FE-4FA6C88F2D47">
  <a name="GUID-78FB26E2-AA60-5531-B2FE-4FA6C88F2D47">
   <!-- -->
  </a>
  <?php include_once (CURRENT_SKIN_PATH.'/sdl_header.html'); ?>
  <div id="sdl_container">
   <div id="leftMenu">
    <div id="expandcontractdiv">
     <a id="collapseTree" href="javascript:tree.collapseAll()">
      Collapse all
     </a>
     <a id="index" href="index.html">
      Symbian^3 Application Developer Library
     </a>
    </div>
    <iframe style="border:none" height="800" width="300" src="index-toc.html"></iframe>
<div id="treeDiv1">
     &#160;
    </div>
    <script type="text/javascript">
     var currentIconMode = 0; window.name="id2470542 id2400118 id2400126 id2400140 ";
	YAHOO.util.Event.onDOMReady(buildTree, this,true);
    </script>
   </div>
   <div id="sdl_content">
<div class="breadcrumb">
     <a href="index.html" title="Symbian^3 Application Developer Library">
      Symbian^3 Application Developer Library
     </a>
     &gt;
     <a href="GUID-32E29020-1956-461A-B79A-1492E06049E7.html" title="The Symbian Guide describes the architecture and functionality of the platform, and provides guides on using its APIs.">
      Symbian Guide
     </a>
     &gt;
     <a href="GUID-1DCA2F4D-ABE6-52A0-AC4E-5AAC1AB5909D.html">
      Text and Localization Guide
     </a>
     &gt;
     <a href="GUID-762A665F-43D0-53ED-B698-0CBD3AC46391.html" title="The Font and Text Services Collection contains the Font Store and related plug-ins and the Text Rendering component. Application developers can select fonts from the Font Store. Device creators can create and add fonts.">
      Font and Text Services Collection
     </a>
     &gt;
     <a href="GUID-98B85D7F-7BD9-5056-A99F-0BC99D921B87.html" title="You can use the Font and Text Services to select a font or typeface for your text.">
      Font and Text Services Tutorials
     </a>
     &gt;
    </div>
    <h1 class="topictitle1">
     Selecting and Using a Font
    </h1>
    <div>
     <p>
      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.
     </p>
     <p>
      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.
     </p>
     <a name="GUID-78FB26E2-AA60-5531-B2FE-4FA6C88F2D47__GUID-C4671145-FEDE-5A82-8085-7E5802545DE9">
      <!-- -->
     </a>
     <ol id="GUID-78FB26E2-AA60-5531-B2FE-4FA6C88F2D47__GUID-C4671145-FEDE-5A82-8085-7E5802545DE9">
      <li id="GUID-78FB26E2-AA60-5531-B2FE-4FA6C88F2D47__GUID-66508D29-FC01-5B64-A043-759503EC04FF">
       <a name="GUID-78FB26E2-AA60-5531-B2FE-4FA6C88F2D47__GUID-66508D29-FC01-5B64-A043-759503EC04FF">
        <!-- -->
       </a>
       <p>
        Set up a font specification (
        <samp class="codeph">
         TFontSpec
        </samp>
        ).
       </p>
      </li>
      <li id="GUID-78FB26E2-AA60-5531-B2FE-4FA6C88F2D47__GUID-E6F70C38-003C-5AFF-B73D-67244058FFCF">
       <a name="GUID-78FB26E2-AA60-5531-B2FE-4FA6C88F2D47__GUID-E6F70C38-003C-5AFF-B73D-67244058FFCF">
        <!-- -->
       </a>
       <p>
        Create a
        <samp class="codeph">
         CFont
        </samp>
        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.
       </p>
      </li>
      <li id="GUID-78FB26E2-AA60-5531-B2FE-4FA6C88F2D47__GUID-93C743E0-2689-5AA3-8F24-937C38D7A7BB">
       <a name="GUID-78FB26E2-AA60-5531-B2FE-4FA6C88F2D47__GUID-93C743E0-2689-5AA3-8F24-937C38D7A7BB">
        <!-- -->
       </a>
       <p>
        Use the font spec and the font pointer to obtain a font from the Font Store. This must be done through the screen device.
       </p>
      </li>
      <li id="GUID-78FB26E2-AA60-5531-B2FE-4FA6C88F2D47__GUID-9DB15BD2-8014-59AC-94CA-30161E3DB553">
       <a name="GUID-78FB26E2-AA60-5531-B2FE-4FA6C88F2D47__GUID-9DB15BD2-8014-59AC-94CA-30161E3DB553">
        <!-- -->
       </a>
       <p>
        Set the graphic context's current font to the
        <samp class="codeph">
         CFont
        </samp>
        obtained from the Font Store.
       </p>
      </li>
      <li id="GUID-78FB26E2-AA60-5531-B2FE-4FA6C88F2D47__GUID-2F9586D4-5C1C-5ADC-A816-4AE7C29CA44A">
       <a name="GUID-78FB26E2-AA60-5531-B2FE-4FA6C88F2D47__GUID-2F9586D4-5C1C-5ADC-A816-4AE7C29CA44A">
        <!-- -->
       </a>
       <p>
        Use the font.
       </p>
      </li>
      <li id="GUID-78FB26E2-AA60-5531-B2FE-4FA6C88F2D47__GUID-DAB05CE0-1710-5B79-A921-41BFDEF8B1FD">
       <a name="GUID-78FB26E2-AA60-5531-B2FE-4FA6C88F2D47__GUID-DAB05CE0-1710-5B79-A921-41BFDEF8B1FD">
        <!-- -->
       </a>
       <p>
        Tidy up by discarding the font from the graphics context and releasing it from the graphics device.
       </p>
      </li>
     </ol>
     <pre class="codeblock" id="GUID-78FB26E2-AA60-5531-B2FE-4FA6C88F2D47__GUID-D37CB9EF-CED1-59EB-95AE-D50E020D2FF8">
      <a name="GUID-78FB26E2-AA60-5531-B2FE-4FA6C88F2D47__GUID-D37CB9EF-CED1-59EB-95AE-D50E020D2FF8">
       <!-- -->
      </a>
      // 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-&gt;ScreenDevice() ;
    
// Get the font that most closely matches the font spec.
screenDevice-&gt;GetNearestFontToMaxHeightInTwips( myFont , myFontSpec ) ;
    
// Pass the font to the current graphics context
CWindowGc&amp; gc = SystemGc() ;
gc.UseFont( myFont ) ;
    
// Use the gc to draw text.  Use the most appropriate CGraphicsContext::DrawText() function.
TPoint textPos( 0, myFont-&gt;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-&gt;ReleaseFont( myFont ) ;
     </pre>
     <p>
      There are a number of
      <samp class="codeph">
       DrawText()
      </samp>
      function in
      <a href="GUID-DAD09DCF-3123-38B4-99E9-91FB24B92138.html">
       <span class="apiname">
        CGraphicsContext
       </span>
      </a>
      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.
     </p>
     <p>
      You can query the metrics of the font using
      <a href="GUID-2A12FE3B-47F2-3016-8161-A971CA506491.html">
       <span class="apiname">
        CFont
       </span>
      </a>
      to ensure that your text is correctly located. You can also establish the size of individual characters and text strings.
     </p>
     <p>
      <strong>
       Note:
      </strong>
      <a href="GUID-2A12FE3B-47F2-3016-8161-A971CA506491.html">
       <span class="apiname">
        CFont
       </span>
      </a>
      is an abstract base class. You can use it to query a font supplied by the font system but you cannot instantiate it.
     </p>
     <pre class="codeblock" id="GUID-78FB26E2-AA60-5531-B2FE-4FA6C88F2D47__GUID-996A661A-581B-50A4-B844-91DE4514F1EA">
      <a name="GUID-78FB26E2-AA60-5531-B2FE-4FA6C88F2D47__GUID-996A661A-581B-50A4-B844-91DE4514F1EA">
       <!-- -->
      </a>
      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&amp; aText) const;
    TInt BaselineOffsetInPixels() const;
    TInt TextCount(const TDesC&amp; aText,TInt aWidthInPixels) const;
    TInt TextCount(const TDesC&amp; aText,TInt aWidthInPixels,
                   TInt&amp; aExcessWidthInPixels) const;
    
    TInt MaxCharWidthInPixels() const;
    TInt MaxNormalCharWidthInPixels() const;
    TFontSpec FontSpecInTwips() const;
    TCharacterDataAvailability GetCharacterData(TUint aCode, 
                   TOpenFontCharMetrics&amp; aMetrics,
                   const TUint8*&amp; aBitmap,
                   TSize&amp; aBitmapSize) const;
    
    TBool GetCharacterPosition(TPositionParam&amp; aParam) const;
    TInt WidthZeroInPixels() const;
    TInt MeasureText(const TDesC&amp; 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&amp; aParam, RShapeInfo&amp; aShapeInfo) const;
    TInt TextWidthInPixels(const TDesC&amp; aText,const TMeasureTextInput* aParam) const;
    ....
     </pre>
    </div>
    <div>
     <ul class="ullinks">
      <li class="ulchildlink">
       <strong>
        <a href="GUID-72986B3C-047C-5411-8F15-BC9C65C3289C.html">
         Using Typefaces
        </a>
       </strong>
       <br />
      </li>
     </ul>
     <div class="familylinks">
      <div class="parentlink">
       <strong>
        Parent topic:
       </strong>
       <a href="GUID-98B85D7F-7BD9-5056-A99F-0BC99D921B87.html" title="You can use the Font and Text Services to select a font or typeface for your text.">
        Font and Text Services Tutorials
       </a>
      </div>
     </div>
     <div class="relinfo relconcepts">
      <strong>
       Related concepts
      </strong>
      <br />
      <div>
       <a href="GUID-C1B080D9-9C6C-520B-B73E-4EB344B1FC5E.html" title="The Graphics Device Interface (GDI) collection is an important collection within the Graphics subsystem. It provides a suite of abstract base classes, interfaces and data structures. The suite represents and interacts with physical graphics hardware such as display screens, off-screen memory and printers.">
        GDI Collection Overview
       </a>
      </div>
      <div>
       <a href="GUID-90644B52-69D7-595C-95E3-D6F7A30C060D.html" title="A font is a set of characters of matching size (height) and appearance. In order to be displayed each character must ultimately be drawn as a series of pixels (a bitmap). Symbian can store fonts in bitmap or vector form. A vector font (for example, an OpenType font) must be converted to bitmaps (or rasterized) before it can be drawn. Symbian caches and shares bitmaps for performance and memory efficiency.">
        Font and Text Services Collection
                Overview
       </a>
      </div>
      <div>
       <a href="GUID-71DADA82-3ABC-52D2-8360-33FAEB2E5DE9.html" title="The Font and Bitmap Server (FBServ) provides a memory efficient way of using bitmaps and fonts. It stores a single instance of each bitmap in either a shared heap or in eXecute In Place (XIP) ROM and provides clients with read and (when appropriate) write access.">
        Font and Bitmap Server Component Overview
       </a>
      </div>
      <div>
       <a href="GUID-416A3756-B5D5-5BCD-830E-2371C5F6B502.html" title="The Font Store contains all of the fonts and typefaces on a phone. It is encapsulated by the Font and Bitmap server which provides a client-side class that applications use to find which fonts are available and to find fonts to match their requirements.">
        Font Store Component Overview
       </a>
      </div>
     </div>
    </div>
    <p class="copyright">Copyright &#169;2010 Nokia Corporation and/or its subsidiary(-ies).<br /> All rights
reserved. Unless otherwise stated, these materials are provided under the terms of the <a href=" http://www.eclipse.org/legal/epl-v10.html"> Eclipse Public License
v1.0</a>.</p> 
   </div>
  </div>
  <?php include_once (CURRENT_SKIN_PATH.'/sdl_footer.html'); ?>
 </body>
</html>