-
-
Notifications
You must be signed in to change notification settings - Fork 338
/
FontCacheEntry.cpp
414 lines (338 loc) · 10.3 KB
/
FontCacheEntry.cpp
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/*
* Copyright 2007-2009, Haiku. All rights reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* Maxim Shemanarev <mcseemagg@yahoo.com>
* Stephan Aßmus <superstippi@gmx.de>
* Andrej Spielmann, <andrej.spielmann@seh.ox.ac.uk>
*/
//----------------------------------------------------------------------------
// Anti-Grain Geometry - Version 2.4
// Copyright (C) 2002-2005 Maxim Shemanarev (http://www.antigrain.com)
//
// Permission to copy, use, modify, sell and distribute this software
// is granted provided this copyright notice appears in all copies.
// This software is provided "as is" without express or implied
// warranty, and with no claim as to its suitability for any purpose.
//
//----------------------------------------------------------------------------
// Contact: mcseem@antigrain.com
// mcseemagg@yahoo.com
// http://www.antigrain.com
//----------------------------------------------------------------------------
#include "FontCacheEntry.h"
#include <string.h>
#include <new>
#include <Autolock.h>
#include <agg_array.h>
#include <utf8_functions.h>
#include <util/OpenHashTable.h>
#include "GlobalSubpixelSettings.h"
BLocker FontCacheEntry::sUsageUpdateLock("FontCacheEntry usage lock");
class FontCacheEntry::GlyphCachePool {
// This class needs to be defined before any inline functions, as otherwise
// gcc2 will barf in debug mode.
struct GlyphHashTableDefinition {
typedef uint32 KeyType;
typedef GlyphCache ValueType;
size_t HashKey(uint32 key) const
{
return key;
}
size_t Hash(GlyphCache* value) const
{
return value->glyph_index;
}
bool Compare(uint32 key, GlyphCache* value) const
{
return value->glyph_index == key;
}
GlyphCache*& GetLink(GlyphCache* value) const
{
return value->hash_link;
}
};
public:
GlyphCachePool()
{
}
~GlyphCachePool()
{
GlyphCache* glyph = fGlyphTable.Clear(true);
while (glyph != NULL) {
GlyphCache* next = glyph->hash_link;
delete glyph;
glyph = next;
}
}
status_t Init()
{
return fGlyphTable.Init();
}
const GlyphCache* FindGlyph(uint32 glyphIndex) const
{
return fGlyphTable.Lookup(glyphIndex);
}
GlyphCache* CacheGlyph(uint32 glyphIndex,
uint32 dataSize, glyph_data_type dataType, const agg::rect_i& bounds,
float advanceX, float advanceY, float preciseAdvanceX,
float preciseAdvanceY, float insetLeft, float insetRight)
{
GlyphCache* glyph = fGlyphTable.Lookup(glyphIndex);
if (glyph != NULL)
return NULL;
glyph = new(std::nothrow) GlyphCache(glyphIndex, dataSize, dataType,
bounds, advanceX, advanceY, preciseAdvanceX, preciseAdvanceY,
insetLeft, insetRight);
if (glyph == NULL || glyph->data == NULL) {
delete glyph;
return NULL;
}
// TODO: The HashTable grows without bounds. We should cleanup
// older entries from time to time.
fGlyphTable.Insert(glyph);
return glyph;
}
private:
typedef BOpenHashTable<GlyphHashTableDefinition> GlyphTable;
GlyphTable fGlyphTable;
};
// #pragma mark -
FontCacheEntry::FontCacheEntry()
:
MultiLocker("FontCacheEntry lock"),
fGlyphCache(new(std::nothrow) GlyphCachePool()),
fEngine(),
fLastUsedTime(LONGLONG_MIN),
fUseCounter(0)
{
}
FontCacheEntry::~FontCacheEntry()
{
//printf("~FontCacheEntry()\n");
delete fGlyphCache;
}
bool
FontCacheEntry::Init(const ServerFont& font, bool forceVector)
{
if (fGlyphCache == NULL)
return false;
glyph_rendering renderingType = _RenderTypeFor(font, forceVector);
// TODO: encoding from font
FT_Encoding charMap = FT_ENCODING_NONE;
bool hinting = font.Hinting();
if (!fEngine.Init(font.Path(), 0, font.Size(), charMap,
renderingType, hinting)) {
fprintf(stderr, "FontCacheEntry::Init() - some error loading font "
"file %s\n", font.Path());
return false;
}
if (fGlyphCache->Init() != B_OK) {
fprintf(stderr, "FontCacheEntry::Init() - failed to allocate "
"GlyphCache table for font file %s\n", font.Path());
return false;
}
return true;
}
bool
FontCacheEntry::HasGlyphs(const char* utf8String, ssize_t length) const
{
uint32 glyphCode;
const char* start = utf8String;
while ((glyphCode = UTF8ToCharCode(&utf8String))) {
if (fGlyphCache->FindGlyph(glyphCode) == NULL)
return false;
if (utf8String - start + 1 > length)
break;
}
return true;
}
inline bool
render_as_space(uint32 glyphCode)
{
// whitespace: render as space
// as per Unicode PropList.txt: White_Space
return (glyphCode >= 0x0009 && glyphCode <= 0x000d)
// control characters
|| (glyphCode == 0x0085)
// another control
|| (glyphCode == 0x00a0)
// no-break space
|| (glyphCode == 0x1680)
// ogham space mark
|| (glyphCode == 0x180e)
// mongolian vowel separator
|| (glyphCode >= 0x2000 && glyphCode <= 0x200a)
// en quand, hair space
|| (glyphCode >= 0x2028 && glyphCode <= 0x2029)
// line and paragraph separators
|| (glyphCode == 0x202f)
// narrow no-break space
|| (glyphCode == 0x205f)
// medium math space
|| (glyphCode == 0x3000)
// ideographic space
;
}
inline bool
render_as_zero_width(uint32 glyphCode)
{
// ignorable chars: render as invisible
// as per Unicode DerivedCoreProperties.txt: Default_Ignorable_Code_Point
return (glyphCode == 0x00ad)
// soft hyphen
|| (glyphCode == 0x034f)
// combining grapheme joiner
|| (glyphCode >= 0x115f && glyphCode <= 0x1160)
// hangul fillers
|| (glyphCode >= 0x17b4 && glyphCode <= 0x17b5)
// ignorable khmer vowels
|| (glyphCode >= 0x180b && glyphCode <= 0x180d)
// variation selectors
|| (glyphCode >= 0x200b && glyphCode <= 0x200f)
// zero width space, cursive joiners, ltr marks
|| (glyphCode >= 0x202a && glyphCode <= 0x202e)
// left to right embed, override
|| (glyphCode >= 0x2060 && glyphCode <= 0x206f)
// word joiner, invisible math operators, reserved
|| (glyphCode == 0x3164)
// hangul filler
|| (glyphCode >= 0xfe00 && glyphCode <= 0xfe0f)
// variation selectors
|| (glyphCode == 0xfeff)
// zero width no-break space
|| (glyphCode == 0xffa0)
// halfwidth hangul filler
|| (glyphCode >= 0xfff0 && glyphCode <= 0xfff8)
// reserved
|| (glyphCode >= 0x1d173 && glyphCode <= 0x1d17a)
// musical symbols
|| (glyphCode >= 0xe0000 && glyphCode <= 0xe01ef)
// variation selectors, tag space, reserved
;
}
const GlyphCache*
FontCacheEntry::CachedGlyph(uint32 glyphCode)
{
// Only requires a read lock.
return fGlyphCache->FindGlyph(glyphCode);
}
const GlyphCache*
FontCacheEntry::CreateGlyph(uint32 glyphCode, FontCacheEntry* fallbackEntry)
{
// We cache the glyph by the requested glyphCode. The FontEngine of this
// FontCacheEntry may not contain a glyph for the given code, in which case
// we ask the fallbackEntry for the code to index translation and let it
// generate the glyph data. We will still use our own cache for storing the
// glyph. The next time it will be found (by glyphCode).
// NOTE: Both this and the fallback FontCacheEntry are expected to be
// write-locked!
const GlyphCache* glyph = fGlyphCache->FindGlyph(glyphCode);
if (glyph != NULL)
return glyph;
FontEngine* engine = &fEngine;
uint32 glyphIndex = engine->GlyphIndexForGlyphCode(glyphCode);
if (glyphIndex == 0 && fallbackEntry != NULL) {
// Our FontEngine does not contain this glyph, but we can retry with
// the fallbackEntry.
engine = &fallbackEntry->fEngine;
glyphIndex = engine->GlyphIndexForGlyphCode(glyphCode);
}
if (glyphIndex == 0) {
if (render_as_zero_width(glyphCode)) {
// cache and return a zero width glyph
return fGlyphCache->CacheGlyph(glyphCode, 0, glyph_data_invalid,
agg::rect_i(0, 0, -1, -1), 0, 0, 0, 0, 0, 0);
}
// reset to our engine
engine = &fEngine;
if (render_as_space(glyphCode)) {
// get the normal space glyph
glyphIndex = engine->GlyphIndexForGlyphCode(0x20 /* space */);
} else {
// The glyph was not found anywhere.
return NULL;
}
}
if (engine->PrepareGlyph(glyphIndex)) {
glyph = fGlyphCache->CacheGlyph(glyphCode,
engine->DataSize(), engine->DataType(), engine->Bounds(),
engine->AdvanceX(), engine->AdvanceY(),
engine->PreciseAdvanceX(), engine->PreciseAdvanceY(),
engine->InsetLeft(), engine->InsetRight());
if (glyph != NULL)
engine->WriteGlyphTo(glyph->data);
}
return glyph;
}
void
FontCacheEntry::InitAdaptors(const GlyphCache* glyph,
double x, double y, GlyphMonoAdapter& monoAdapter,
GlyphGray8Adapter& gray8Adapter, GlyphPathAdapter& pathAdapter,
double scale)
{
if (glyph == NULL)
return;
switch(glyph->data_type) {
case glyph_data_mono:
monoAdapter.init(glyph->data, glyph->data_size, x, y);
break;
case glyph_data_gray8:
gray8Adapter.init(glyph->data, glyph->data_size, x, y);
break;
case glyph_data_subpix:
gray8Adapter.init(glyph->data, glyph->data_size, x, y);
break;
case glyph_data_outline:
pathAdapter.init(glyph->data, glyph->data_size, x, y, scale);
break;
default:
break;
}
}
bool
FontCacheEntry::GetKerning(uint32 glyphCode1, uint32 glyphCode2,
double* x, double* y)
{
return fEngine.GetKerning(glyphCode1, glyphCode2, x, y);
}
/*static*/ void
FontCacheEntry::GenerateSignature(char* signature, size_t signatureSize,
const ServerFont& font, bool forceVector)
{
glyph_rendering renderingType = _RenderTypeFor(font, forceVector);
// TODO: read more of these from the font
FT_Encoding charMap = FT_ENCODING_NONE;
bool hinting = font.Hinting();
uint8 averageWeight = gSubpixelAverageWeight;
snprintf(signature, signatureSize, "%" B_PRId32 ",%u,%d,%d,%.1f,%d,%d",
font.GetFamilyAndStyle(), charMap,
font.Face(), int(renderingType), font.Size(), hinting, averageWeight);
}
void
FontCacheEntry::UpdateUsage()
{
// this is a static lock to prevent usage of too many semaphores,
// but on the other hand, it is not so nice to be using a lock
// here at all
// the hope is that the time is so short to hold this lock, that
// there is not much contention
BAutolock _(sUsageUpdateLock);
fLastUsedTime = system_time();
fUseCounter++;
}
/*static*/ glyph_rendering
FontCacheEntry::_RenderTypeFor(const ServerFont& font, bool forceVector)
{
glyph_rendering renderingType = gSubpixelAntialiasing ?
glyph_ren_subpix : glyph_ren_native_gray8;
if (forceVector || font.Rotation() != 0.0 || font.Shear() != 90.0
|| font.FalseBoldWidth() != 0.0
|| (font.Flags() & B_DISABLE_ANTIALIASING) != 0
|| font.Size() > 30
|| !font.Hinting()) {
renderingType = glyph_ren_outline;
}
return renderingType;
}