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