1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
|
/*--------------------------------------------------------------------*//*:Ignore this sentence.
Copyright (C) 1999, 2001 SIL International. All rights reserved.
Distributable under the terms of either the Common Public License or the
GNU Lesser General Public License, as specified in the LICENSING.txt file.
File: FontCache.cpp
Responsibility: Sharon Correll
Last reviewed: Not yet.
Description:
A cache of all the font-face objects.
----------------------------------------------------------------------------------------------*/
//:>********************************************************************************************
//:> Include files
//:>********************************************************************************************
#include "Main.h"
#ifdef _MSC_VER
#pragma hdrstop
#endif
// any other headers (not precompiled)
#undef THIS_FILE
DEFINE_THIS_FILE
//:End Ignore
//:>********************************************************************************************
//:> Forward declarations
//:>********************************************************************************************
//:>********************************************************************************************
//:> Local Constants and static variables
//:>********************************************************************************************
//:>********************************************************************************************
//:> Methods
//:>********************************************************************************************
namespace gr
{
/*----------------------------------------------------------------------------------------------
Store the given font-face in the cache. Overwrite whatever was there before.
Return NULL if there is nothing appropriate there.
----------------------------------------------------------------------------------------------*/
void FontCache::GetFontFace(std::wstring strFaceName, bool fBold, bool fItalic,
FontFace ** ppfface)
{
int ifci = FindCacheItem(strFaceName);
if (ifci < 0) // no fonts of that family present
{
*ppfface = NULL;
return;
}
CacheItem * pfci = m_prgfci + ifci;
if (fBold)
{
if (fItalic)
*ppfface = pfci->pffaceBI;
else
*ppfface = pfci->pffaceBold;
}
else
{
if (fItalic)
*ppfface = pfci->pffaceItalic;
else
*ppfface = pfci->pffaceRegular;
}
}
/*----------------------------------------------------------------------------------------------
Store the given font-face in the cache. Overwrite whatever was there before.
----------------------------------------------------------------------------------------------*/
void FontCache::CacheFontFace(std::wstring strFaceName, bool fBold, bool fItalic,
FontFace * pfface)
{
if (m_prgfci == NULL)
Initialize();
int ifciIns = FindCacheItem(strFaceName);
int ifci = ifciIns;
if (ifciIns < 0)
{
ifci = (ifciIns + 1) * -1;
InsertCacheItem(ifci);
std::copy(strFaceName.c_str(), strFaceName.c_str() +
(strFaceName.size() + 1), m_prgfci[ifci].szFaceName);
}
CacheItem * pfci = m_prgfci + ifci;
bool fPrevNull;
if (fBold)
{
if (fItalic)
{
fPrevNull = (pfci->pffaceBI == NULL);
pfci->pffaceBI = pfface;
}
else
{
fPrevNull = (pfci->pffaceBold == NULL);
pfci->pffaceBold = pfface;
}
}
else
{
if (fItalic)
{
fPrevNull = (pfci->pffaceItalic == NULL);
pfci->pffaceItalic = pfface;
}
else
{
fPrevNull = (pfci->pffaceRegular == NULL);
pfci->pffaceRegular = pfface;
}
}
if (fPrevNull && (pfface != NULL))
m_cfface++;
}
/*----------------------------------------------------------------------------------------------
Remove the given font-face from the cache. Return false if it couldn't be found.
----------------------------------------------------------------------------------------------*/
bool FontCache::RemoveFontFace(std::wstring strFaceName, bool fBold, bool fItalic,
bool fZapCache)
{
int ifci = FindCacheItem(strFaceName);
if (ifci < 0)
{
return false;
}
bool fPrevVal;
CacheItem * pfci = m_prgfci + ifci;
if (fBold)
{
if (fItalic)
{
fPrevVal = (pfci->pffaceBI != NULL);
pfci->pffaceBI = NULL;
}
else
{
fPrevVal = (pfci->pffaceBold != NULL);
pfci->pffaceBold = NULL;
}
}
else
{
if (fItalic)
{
fPrevVal = (pfci->pffaceItalic != NULL);
pfci->pffaceItalic = NULL;
}
else
{
fPrevVal = (pfci->pffaceRegular != NULL);
pfci->pffaceRegular = NULL;
}
}
if (fPrevVal)
m_cfface--;
Assert(m_cfface >= 0);
if (m_cfface <= 0 && m_flush == kflushAuto && fZapCache)
{
// All items are NULL; delete this cache. Probably this is because
// the program is exiting.
FontFace::ZapFontCache();
}
return fPrevVal;
}
/*----------------------------------------------------------------------------------------------
Search for the font-family in the cache and return its index.
If not present, return a negative number indicating where it was expected.
----------------------------------------------------------------------------------------------*/
int FontCache::FindCacheItem(std::wstring strFaceName)
{
if (m_cfci == 0)
return -1;
Assert(m_prgfci);
// Use a binary-chop search.
int ifciLow = 0;
int ifciHigh = m_cfci;
while (true)
{
int ifciMid = (ifciHigh + ifciLow) >> 1; // divide by 2
CacheItem * pfci = m_prgfci + ifciMid;
int tst = wcscmp(strFaceName.c_str(), (const wchar_t *)pfci->szFaceName);
if (tst == 0)
return ifciMid; // found it
if (ifciLow + 1 == ifciHigh)
{
// not there; return where expected: 0 -> -1, 1 -> -2
Assert(ifciMid == ifciLow);
if (tst < 0)
return (ifciLow * -1) - 1;
else
return (ifciHigh * -1) - 1;
}
// Keep looking.
if (tst < 0)
ifciHigh = ifciMid;
else
ifciLow = ifciMid;
}
}
/*----------------------------------------------------------------------------------------------
Insert space in the cache at location ifci.
----------------------------------------------------------------------------------------------*/
void FontCache::InsertCacheItem(int ifci)
{
if (m_cfci == m_cfciMax)
{
// Cache is full; double the space.
CacheItem * m_prgfciOld = m_prgfci;
m_prgfci = new CacheItem[m_cfciMax * 2];
std::copy(m_prgfciOld, m_prgfciOld + m_cfciMax, m_prgfci);
delete[] m_prgfciOld;
m_cfciMax *= 2;
}
// This copy involves overlapping ranges, so we need copy_backward not copy
// to satisfy the preconditions
std::copy_backward(m_prgfci + ifci, m_prgfci + m_cfci, m_prgfci + m_cfci + 1);
m_cfci++;
// Initialize inserted item.
CacheItem * pfci = m_prgfci + ifci;
pfci->pffaceRegular = NULL;
pfci->pffaceBold = NULL;
pfci->pffaceItalic = NULL;
pfci->pffaceBI = NULL;
}
/*----------------------------------------------------------------------------------------------
Check that the cache is indeed empty.
----------------------------------------------------------------------------------------------*/
void FontCache::AssertEmpty()
{
#ifdef _DEBUG
for (int ifci = 0; ifci < m_cfci; ifci++)
{
CacheItem * pfci = m_prgfci + ifci;
Assert(pfci->pffaceRegular == NULL);
Assert(pfci->pffaceBold == NULL);
Assert(pfci->pffaceItalic == NULL);
Assert(pfci->pffaceBI == NULL);
}
#endif // _DEBUG
}
/*----------------------------------------------------------------------------------------------
Set the flush mode on the cache to indicate whether or not it should be deleted when
it becomes empty.
----------------------------------------------------------------------------------------------*/
void FontCache::SetFlushMode(int flush)
{
m_flush = flush;
if (m_flush == kflushAuto)
{
// Delete any font faces that have no remaining corresponding fonts.
// Work backwards so as we remove items the loop still works.
for (int ifci = m_cfci; --ifci >= 0; )
{
CacheItem * pfci = m_prgfci + ifci;
if (pfci->pffaceRegular && pfci->pffaceRegular->NoFonts())
RemoveFontFace(pfci->szFaceName, false, false, false);
if (pfci->pffaceBold && pfci->pffaceBold->NoFonts())
RemoveFontFace(pfci->szFaceName, true, false, false);
if (pfci->pffaceItalic && pfci->pffaceItalic->NoFonts())
RemoveFontFace(pfci->szFaceName, false, true, false);
if (pfci->pffaceBI && pfci->pffaceBI->NoFonts())
RemoveFontFace(pfci->szFaceName, true, true, false);
}
if (m_cfface <= 0)
FontFace::ZapFontCache();
}
}
} // namespace gr
|