lafagnosticuifoundation/cone/src/COEFONTPROVIDER.cpp
changeset 0 2f259fa3e83a
equal deleted inserted replaced
-1:000000000000 0:2f259fa3e83a
       
     1 // Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // class CCoeFontProvider 
       
    15 // 
       
    16 //
       
    17 
       
    18 #include <coefontprovider.h>
       
    19 #include <coecntss.h>
       
    20 
       
    21 CCoeFontProvider::TFont::TFont(const TCoeFont& aCoeFont, TInt aZoomFactor, const CFont* aFont) : 
       
    22                 iCoeFont(aCoeFont), iZoomFactor(aZoomFactor), iFont(aFont) 
       
    23     { 
       
    24     } 
       
    25 
       
    26 TInt CCoeFontProvider::TFont::Compare(const TFont& aFirst, const TFont& aSecond) 
       
    27     {                 
       
    28     const TInt comp = TCoeFont::Compare(aFirst.iCoeFont, aSecond.iCoeFont); 
       
    29     if(comp) 
       
    30     	{
       
    31     	return comp; 
       
    32     	}
       
    33             
       
    34     return (aFirst.iZoomFactor - aSecond.iZoomFactor); 
       
    35     } 
       
    36 
       
    37 /** Standard Symbian factory method. Uses a default typeface
       
    38 
       
    39 @publishedAll
       
    40 @released
       
    41 */
       
    42 EXPORT_C CCoeFontProvider* CCoeFontProvider::NewL() 
       
    43     { 
       
    44     return NewL(CCoeControlStaticSettings::SystemTypeface()); 
       
    45     } 
       
    46 
       
    47 /** Standard Symbian factory method. Typeface to use is provided by caller.
       
    48 
       
    49 @param aTypefaceName Typeface to use.
       
    50 @publishedAll
       
    51 @released
       
    52 */
       
    53 EXPORT_C CCoeFontProvider* CCoeFontProvider::NewL(const TDesC& aTypefaceName) 
       
    54     { 
       
    55     CCoeFontProvider* self = new (ELeave) CCoeFontProvider(aTypefaceName); 
       
    56     CleanupStack::PushL(self); 
       
    57     self->ConstructL(); 
       
    58     CleanupStack::Pop(self); 
       
    59     return self; 
       
    60     } 
       
    61 
       
    62 /** Constructor
       
    63 */
       
    64 CCoeFontProvider::CCoeFontProvider(const TDesC& aTypefaceName) 
       
    65     { 
       
    66     iTypeface.iName = aTypefaceName; 
       
    67     } 
       
    68 
       
    69 /** Symbian second-phase constructor
       
    70 */
       
    71 void CCoeFontProvider::ConstructL() 
       
    72     { 
       
    73     CCoeControlStaticSettings::GetLogicalToPixelFontSizesL(iLogicalToPixelSizes);
       
    74     } 
       
    75 
       
    76 CCoeFontProvider::~CCoeFontProvider() 
       
    77     { 
       
    78     ReleaseAllFonts(); 
       
    79     iLogicalToPixelSizes.Reset(); 
       
    80     iLogicalToPixelSizes.Close();
       
    81     } 
       
    82 
       
    83 void CCoeFontProvider::ReleaseAllFonts() 
       
    84     { 
       
    85     TInt count = iFonts.Count(); 
       
    86     while(count--) 
       
    87         { 
       
    88         CCoeEnv::Static()->ScreenDevice()->ReleaseFont(const_cast<CFont*>(iFonts[count].iFont)); 
       
    89         } 
       
    90     iFonts.Reset(); 
       
    91     } 
       
    92 
       
    93 /** Change the typeface
       
    94 
       
    95 @param aTypeface The typeface to use
       
    96 @publishedAll
       
    97 @released
       
    98 */
       
    99 EXPORT_C void CCoeFontProvider::SetTypeface(TTypeface aTypeface) 
       
   100     { 
       
   101     if(iTypeface.iName.CompareF(aTypeface.iName) == 0) 
       
   102         { 
       
   103         iTypeface = aTypeface; 
       
   104         ReleaseAllFonts(); 
       
   105         
       
   106         CCoeAppUi* appUi = static_cast<CCoeAppUi*>(CCoeEnv::Static()->AppUi()); 
       
   107         if(appUi) 
       
   108         	{
       
   109         	appUi->NotifyFontChange(*this);
       
   110         	}
       
   111         } 
       
   112     } 
       
   113 
       
   114 /** Use the system typeface
       
   115 
       
   116 @publishedAll
       
   117 @released
       
   118 */
       
   119 EXPORT_C void CCoeFontProvider::UseSystemTypeface() 
       
   120     {        
       
   121     const TDesC& systemTypeFaceName = CCoeControlStaticSettings::SystemTypeface();
       
   122     const TBool oldNameNull = (!iTypeface.iName.Length()); 
       
   123     if(iTypeface.iName.CompareF(systemTypeFaceName) == 0)
       
   124         { 
       
   125         iTypeface.iName = systemTypeFaceName; 
       
   126         ReleaseAllFonts(); 
       
   127 
       
   128         if(!oldNameNull) 
       
   129             { 
       
   130             CCoeAppUi* appUi = static_cast<CCoeAppUi*>(CCoeEnv::Static()->AppUi()); 
       
   131             if(appUi) 
       
   132             	{
       
   133             	appUi->NotifyFontChange(*this); 
       
   134             	}        
       
   135             } 
       
   136         } 
       
   137     } 
       
   138 
       
   139 /** Return the current typeface
       
   140 
       
   141 @publishedAll
       
   142 @released
       
   143 */
       
   144 EXPORT_C TTypeface CCoeFontProvider::Typeface() const 
       
   145     { 
       
   146     return iTypeface; 
       
   147     } 
       
   148 
       
   149 /** Returns the closest available match to the specified logical font and zoom factor.
       
   150 Note that all CFont objects referenced through this method are owned by the font provider. 
       
   151 Thus do not explicitly release the font object after use!
       
   152 
       
   153 A default font provider is made available by the CCoeEnv singleton and returned from 
       
   154 CCoeControl::FindFontProvider() if no overriding font provider has been attached to 
       
   155 the control tree.
       
   156 
       
   157 For an example of how to use this function, see CCoeControl::ScreenFont().
       
   158 
       
   159 @see CCoeControl::ScreenFont()
       
   160 @see CCoeControl::FindFontProvider()
       
   161 @see CCoeControl::AccumulatedZoom()
       
   162 
       
   163 @param aFont The requested logical font.
       
   164 @param aZoomFactor Zoom factor used when deriving the font height.
       
   165 @return The closest matching available font.
       
   166 @publishedAll
       
   167 @released
       
   168 */
       
   169 EXPORT_C const CFont& CCoeFontProvider::Font(const TCoeFont& aFont, const TZoomFactor& aZoomFactor) const 
       
   170     { 
       
   171     const TInt index = iFonts.FindInOrder(TFont(aFont, aZoomFactor.ZoomFactor()), TLinearOrder<TFont>(TFont::Compare)); 
       
   172     if(index != KErrNotFound) 
       
   173     	{
       
   174     	return *iFonts[index].iFont; 
       
   175     	}
       
   176                 
       
   177     const TInt heightInPixels = (aFont.LogicalSize() != TCoeFont::EUndefinedSize) ? iLogicalToPixelSizes[aFont.LogicalSize()] : aFont.HeightInPixels(); 
       
   178     
       
   179     TFontSpec fontSpec; 
       
   180     fontSpec.iTypeface = iTypeface; 
       
   181     fontSpec.iHeight = aZoomFactor.GraphicsDeviceMap()->VerticalPixelsToTwips(heightInPixels);        // don't care about the zoom just yet 
       
   182     fontSpec.iFontStyle = aFont.Style(); 
       
   183 
       
   184     CFont* font = NULL; 
       
   185     TInt err = KErrNone; 
       
   186     if(aFont.IsNonZooming()) 
       
   187     	{
       
   188         err = const_cast<MGraphicsDeviceMap*>(aZoomFactor.GraphicsDeviceMap())->GetNearestFontToMaxHeightInTwips(font, fontSpec,fontSpec.iHeight);
       
   189     	}
       
   190     else
       
   191 		{
       
   192         err = const_cast<TZoomFactor&>(aZoomFactor).GetNearestFontToMaxHeightInTwips(font, fontSpec, fontSpec.iHeight);     // here, consider the zoom
       
   193 		} 
       
   194 	if (err != KErrNone)
       
   195 		{
       
   196 		//error has occured so just return normal font.
       
   197 		const CFont* normalFont = CCoeEnv::Static()->NormalFont();
       
   198 		return *normalFont;
       
   199 		}
       
   200 
       
   201     err = iFonts.InsertInOrder(TFont(aFont, aZoomFactor.ZoomFactor(), font), TLinearOrder<TFont>(TFont::Compare)); 
       
   202 	if (err != KErrNone)
       
   203 		{
       
   204 		//error has occured so just return normal font.
       
   205 		const CFont* normalFont = CCoeEnv::Static()->NormalFont();
       
   206 		return *normalFont;
       
   207 		}
       
   208 		
       
   209     return *font; 
       
   210     } 
       
   211     
       
   212 /** flushes the array & refetches the logical-to-pixel mapping. Called when global control settings are updated
       
   213 
       
   214 @internalTechnology
       
   215 */
       
   216 void CCoeFontProvider::RefetchPixelMappingL()
       
   217 	{
       
   218 	CCoeControlStaticSettings::GetLogicalToPixelFontSizesL(iLogicalToPixelSizes);
       
   219 	ReleaseAllFonts();
       
   220 	}
       
   221