windowing/windowserver/nga/SERVER/wsfont.cpp
changeset 0 5d03bc08d59c
equal deleted inserted replaced
-1:000000000000 0:5d03bc08d59c
       
     1 // Copyright (c) 2007-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 // FONT related functions and the font cache
       
    15 // 
       
    16 //
       
    17 
       
    18 #include "wsfont.h"
       
    19 
       
    20 CWsFontCache * CWsFontCache::iSelf = NULL;
       
    21 TDblQue<CWsFbsFont> * CWsFontCache::iList = NULL;
       
    22 
       
    23 CWsFontCache::CWsFontCache()
       
    24 	{
       
    25 	}
       
    26 
       
    27 CWsFontCache::~CWsFontCache()
       
    28 	{
       
    29 	}
       
    30 
       
    31 void CWsFontCache::CreateInstanceL()
       
    32 	{
       
    33 	// The list MUST exist before creation of the cache since the cache
       
    34 	// contains an array of fonts. When the array is created the fonts add themselves
       
    35 	// to the list.
       
    36 	// The list is ordered from newest to oldest font used.
       
    37 	iList = new (ELeave) TDblQue<CWsFbsFont>(_FOFF(CWsFbsFont,iLink));
       
    38 	CleanupStack::PushL(iList);
       
    39 	iSelf = new (ELeave) CWsFontCache;
       
    40 	CleanupStack::Pop(iList);
       
    41 	}
       
    42 
       
    43 void CWsFontCache::DestroyInstance()
       
    44 	{
       
    45 	// The cache has to be destroyed first since the fonts are
       
    46 	// destroyed when the cache's array if destroyed. The fonts 
       
    47 	// remove themselves from the list
       
    48 	delete iSelf;
       
    49 	iSelf = NULL;
       
    50 	delete iList;
       
    51 	iList = NULL;
       
    52 
       
    53 	}
       
    54 
       
    55 void CWsFontCache::ReleaseFont(CWsFbsFont *&aFont)
       
    56 	{
       
    57 	if (aFont)
       
    58 		{
       
    59 		WS_ASSERT_DEBUG(aFont->iCount>0, EWsPanicFontCacheCount);
       
    60 		// decrease usage count
       
    61 		aFont->iCount--;
       
    62 		// when the font reaches zero then the font entry can be reused later
       
    63 		aFont = NULL;
       
    64 		}
       
    65 	}
       
    66 
       
    67 TBool CWsFontCache::UseFont(CWsFbsFont *&aFont, TInt aHandle)
       
    68 	{
       
    69 	ReleaseFont(aFont);
       
    70 	CWsFbsFont *font = NULL;
       
    71 
       
    72 	// The list is ordered from newest to oldest font used.
       
    73 	TDblQueIter<CWsFbsFont> iter(*iList);
       
    74 	
       
    75 	
       
    76 	// Search for the font from newest to oldest font
       
    77 	while ((font = iter++) != NULL)
       
    78 		{
       
    79 		// if the font handle already exists use existing font
       
    80 		if (font->Handle() == aHandle)
       
    81 			{
       
    82 			break;
       
    83 			}
       
    84 		}
       
    85 
       
    86 	// Font not found in cache so find an unused entry
       
    87 	if (!font)
       
    88 		{
       
    89 		// Starting search from oldest to newest font unsed
       
    90 		iter.SetToLast();
       
    91 		while((font = iter--) != NULL)
       
    92 			{
       
    93 			// if you find an unused font then use that font entry
       
    94 			if (font->iCount == 0)
       
    95 				{
       
    96 				break;
       
    97 				}
       
    98 			}
       
    99 		// if an unused font is not found then all fonts are being used and 
       
   100 		// the font can not be created
       
   101 		if (!font)
       
   102 			{
       
   103 			return(ETrue);	// No room in cache
       
   104 			}
       
   105 		// release and reuse the font entry to create a new font
       
   106 		font->Reset();
       
   107 		if (font->Duplicate(aHandle) != KErrNone)
       
   108 			{
       
   109 			return(EFalse);	// Bad handle, (indicated by aFont=NULL)
       
   110 			}
       
   111 		}
       
   112 	// put font at the font of the list as the font is the newest font
       
   113 	font->iLink.Deque();
       
   114 	iList->AddFirst(*font);
       
   115 	// increase usage count
       
   116 	font->iCount++;
       
   117 	aFont = font;
       
   118 	return(EFalse);
       
   119 	}
       
   120 
       
   121 TDblQue<CWsFbsFont>& CWsFontCache::List()
       
   122 	{
       
   123 	return *Instance()->iList;
       
   124 	}
       
   125 	
       
   126 CWsFbsFont::CWsFbsFont()
       
   127 	{
       
   128 	// add itself to the font cache
       
   129 	CWsFontCache::List().AddLast(*this);
       
   130 	}
       
   131 
       
   132 CWsFbsFont::~CWsFbsFont()
       
   133 	{
       
   134 	// remove itself from the font cache
       
   135 	iLink.Deque();
       
   136 	WS_ASSERT_DEBUG(iCount==0, EWsPanicFontCacheCount);
       
   137 	}