summaryrefslogtreecommitdiffstats
path: root/src/3rdparty/webkit/WebCore/platform/graphics/qt/FontCacheQt.cpp
blob: d8aabc6b264ec3cacf80f3796da0d55b8db32724 (plain)
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
/*
    Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies)
    Copyright (C) 2008 Holger Hans Peter Freyther
    Copyright (C) 2006, 2008 Apple Inc. All rights reserved.
    Copyright (C) 2007 Nicholas Shanks <webkit@nickshanks.com>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.

    This class provides all functionality needed for loading images, style sheets and html
    pages from the web. It has a memory cache for these objects.
*/
#include "config.h"
#include "FontCache.h"

#include "FontDescription.h"
#include "FontPlatformData.h"
#include "Font.h"
#include "PlatformString.h"
#include "StringHash.h"
#include <utility>
#include <wtf/ListHashSet.h>
#include <wtf/StdLibExtras.h>

using namespace WTF;

namespace WebCore {

FontCache* fontCache()
{
    DEFINE_STATIC_LOCAL(FontCache, globalFontCache, ());
    return &globalFontCache;
}

FontCache::FontCache()
{
}

void FontCache::getTraitsInFamily(const AtomicString&, Vector<unsigned>&)
{
}

// This type must be consistent with FontPlatformData's ctor - the one which
// gets FontDescription as it's parameter.
class FontPlatformDataCacheKey {
public:
    FontPlatformDataCacheKey(const FontDescription& description)
        : m_familyName()
        , m_size(description.computedPixelSize())
        , m_bold(false)
        , m_italic(description.italic())
        , m_smallCaps(description.smallCaps())
        , m_hash(0)
    {
        // FIXME: Map all FontWeight values to QFont weights in FontPlatformData's ctor and follow it here
        if (FontPlatformData::toQFontWeight(description.weight()) > QFont::Normal)
            m_bold = true;

        const FontFamily* family = &description.family();
        while (family) {
            m_familyName.append(family->family());
            family = family->next();
            if (family)
                m_familyName.append(',');
        }

        computeHash();
    }

    FontPlatformDataCacheKey(const FontPlatformData& fontData)
        : m_familyName(static_cast<String>(fontData.family()))
        , m_size(fontData.pixelSize())
        , m_bold(fontData.bold())
        , m_italic(fontData.italic())
        , m_smallCaps(fontData.smallCaps())
        , m_hash(0)
    {
        computeHash();
    }

    FontPlatformDataCacheKey(HashTableDeletedValueType) : m_size(hashTableDeletedSize()) { }
    bool isHashTableDeletedValue() const { return m_size == hashTableDeletedSize(); }

    enum HashTableEmptyValueType { HashTableEmptyValue };

    FontPlatformDataCacheKey(HashTableEmptyValueType)
        : m_familyName()
        , m_size(0)
        , m_bold(false)
        , m_italic(false)
        , m_smallCaps(false)
        , m_hash(0)
    {
    }

    bool operator==(const FontPlatformDataCacheKey& other) const
    {
        if (m_hash != other.m_hash)
            return false;

        return equalIgnoringCase(m_familyName, other.m_familyName) && m_size == other.m_size &&
            m_bold == other.m_bold && m_italic == other.m_italic && m_smallCaps == other.m_smallCaps;
    }

    unsigned hash() const
    {
        return m_hash;
    }

    void computeHash()
    {
        unsigned hashCodes[] = {
            CaseFoldingHash::hash(m_familyName),
            m_size | static_cast<unsigned>(m_bold << sizeof(unsigned) * 8 - 1)
                | static_cast<unsigned>(m_italic) << sizeof(unsigned) *8 - 2
                | static_cast<unsigned>(m_smallCaps) << sizeof(unsigned) * 8 - 3
        };
        m_hash = StringImpl::computeHash(reinterpret_cast<UChar*>(hashCodes), sizeof(hashCodes) / sizeof(UChar));
    }

private:
    String m_familyName;
    int m_size;
    bool m_bold;
    bool m_italic;
    bool m_smallCaps;
    unsigned m_hash;

    static unsigned hashTableDeletedSize() { return 0xFFFFFFFFU; }
};

struct FontPlatformDataCacheKeyHash {
    static unsigned hash(const FontPlatformDataCacheKey& key)
    {
        return key.hash();
    }

    static bool equal(const FontPlatformDataCacheKey& a, const FontPlatformDataCacheKey& b)
    {
        return a == b;
    }

    static const bool safeToCompareToEmptyOrDeleted = true;
};

struct FontPlatformDataCacheKeyTraits : WTF::GenericHashTraits<FontPlatformDataCacheKey> {
    static const bool needsDestruction = true;
    static const FontPlatformDataCacheKey& emptyValue()
    {
        DEFINE_STATIC_LOCAL(FontPlatformDataCacheKey, key, (FontPlatformDataCacheKey::HashTableEmptyValue));
        return key;
    }
    static void constructDeletedValue(FontPlatformDataCacheKey& slot)
    {
        new (&slot) FontPlatformDataCacheKey(HashTableDeletedValue);
    }
    static bool isDeletedValue(const FontPlatformDataCacheKey& value)
    {
        return value.isHashTableDeletedValue();
    }
};

typedef HashMap<FontPlatformDataCacheKey, FontPlatformData*, FontPlatformDataCacheKeyHash, FontPlatformDataCacheKeyTraits> FontPlatformDataCache;

// using Q_GLOBAL_STATIC leads to crash. TODO investigate the way to fix this.
static FontPlatformDataCache* gFontPlatformDataCache = 0;

FontPlatformData* FontCache::getCachedFontPlatformData(const FontDescription& description, const AtomicString& family, bool)
{
    if (!gFontPlatformDataCache)
        gFontPlatformDataCache = new FontPlatformDataCache;

    FontDescription descriptionWithResolvedFamily(description);
    FontFamily resolvedFamily;
    resolvedFamily.setFamily(family);
    descriptionWithResolvedFamily.setFamily(resolvedFamily);

    FontPlatformDataCacheKey key(descriptionWithResolvedFamily);
    FontPlatformData* platformData = gFontPlatformDataCache->get(key);
    if (!platformData) {
        platformData = new FontPlatformData(descriptionWithResolvedFamily);
        gFontPlatformDataCache->add(key, platformData);
    }
    return platformData;
}

typedef HashMap<FontPlatformDataCacheKey, std::pair<SimpleFontData*, unsigned>, FontPlatformDataCacheKeyHash, FontPlatformDataCacheKeyTraits> FontDataCache;

static FontDataCache* gFontDataCache = 0;

static const int cMaxInactiveFontData = 40;
static const int cTargetInactiveFontData = 32;

static ListHashSet<const SimpleFontData*>* gInactiveFontDataSet = 0;

SimpleFontData* FontCache::getCachedFontData(const FontPlatformData* fontPlatformData)
{
    if (!gFontDataCache) {
        gFontDataCache = new FontDataCache;
        gInactiveFontDataSet = new ListHashSet<const SimpleFontData*>;
    }

    FontPlatformDataCacheKey key(*fontPlatformData);
    FontDataCache::iterator it = gFontDataCache->find(key);
    if (it == gFontDataCache->end()) {
        SimpleFontData* fontData = new SimpleFontData(*fontPlatformData);
        gFontDataCache->add(key, std::pair<SimpleFontData*, unsigned>(fontData, 1));
        return fontData;
    }
    if (!it->second.second++) {
        ASSERT(gInactiveFontDataSet->contains(it->second.first));
        gInactiveFontDataSet->remove(it->second.first);
    }
    return it->second.first;
}

FontPlatformData* FontCache::getLastResortFallbackFont(const FontDescription&)
{
    return 0;
}

void FontCache::releaseFontData(const WebCore::SimpleFontData* fontData)
{
    ASSERT(gFontDataCache);
    ASSERT(!fontData->isCustomFont());

    FontPlatformDataCacheKey key(fontData->platformData());
    FontDataCache::iterator it = gFontDataCache->find(key);
    ASSERT(it != gFontDataCache->end());
    if (!--it->second.second) {
        gInactiveFontDataSet->add(it->second.first);
        if (gInactiveFontDataSet->size() > cMaxInactiveFontData)
            purgeInactiveFontData(gInactiveFontDataSet->size() - cTargetInactiveFontData);
    }
}

void FontCache::purgeInactiveFontData(int count)
{
    static bool isPurging;  // Guard against reentry when e.g. a deleted FontData releases its small caps FontData.
    if (isPurging)
        return;

    isPurging = true;

    ListHashSet<const SimpleFontData*>::iterator it = gInactiveFontDataSet->begin();
    ListHashSet<const SimpleFontData*>::iterator end = gInactiveFontDataSet->end();
    for (int i = 0; i < count && it != end; ++i, ++it) {
        FontPlatformDataCacheKey key = (*it)->platformData();
        pair<SimpleFontData*, unsigned> fontDataPair = gFontDataCache->take(key);
        ASSERT(fontDataPair.first != 0);
        ASSERT(!fontDataPair.second);
        delete fontDataPair.first;

        FontPlatformData* platformData = gFontPlatformDataCache->take(key);
        if (platformData)
            delete platformData;
    }

    if (it == end) {
        // Removed everything
        gInactiveFontDataSet->clear();
    } else {
        for (int i = 0; i < count; ++i)
            gInactiveFontDataSet->remove(gInactiveFontDataSet->begin());
    }

    isPurging = false;
}

void FontCache::addClient(FontSelector*)
{
}

void FontCache::removeClient(FontSelector*)
{
}

void FontCache::invalidate()
{
    if (!gFontPlatformDataCache || !gFontDataCache)
        return;

    purgeInactiveFontData();
}

} // namespace WebCore