tizen beta release
[profile/ivi/webkit-efl.git] / Source / WebCore / platform / graphics / qt / FontCacheQt.cpp
1 /*
2     Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
3     Copyright (C) 2008 Holger Hans Peter Freyther
4     Copyright (C) 2006, 2008 Apple Inc. All rights reserved.
5     Copyright (C) 2007 Nicholas Shanks <webkit@nickshanks.com>
6
7     This library is free software; you can redistribute it and/or
8     modify it under the terms of the GNU Library General Public
9     License as published by the Free Software Foundation; either
10     version 2 of the License, or (at your option) any later version.
11
12     This library is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15     Library General Public License for more details.
16
17     You should have received a copy of the GNU Library General Public License
18     along with this library; see the file COPYING.LIB.  If not, write to
19     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20     Boston, MA 02110-1301, USA.
21
22     This class provides all functionality needed for loading images, style sheets and html
23     pages from the web. It has a memory cache for these objects.
24 */
25 #include "config.h"
26 #include "FontCache.h"
27
28 #include "FontDescription.h"
29 #include "FontPlatformData.h"
30 #include "Font.h"
31 #include "PlatformString.h"
32 #include <utility>
33 #include <wtf/ListHashSet.h>
34 #include <wtf/StdLibExtras.h>
35 #include <wtf/text/StringHash.h>
36
37 #include <QFont>
38 #include <QFontDatabase>
39 #if HAVE(QRAWFONT)
40 #include <QTextLayout>
41 #endif
42
43 using namespace WTF;
44
45 namespace WebCore {
46
47 void FontCache::platformInit()
48 {
49 }
50
51 #if HAVE(QRAWFONT)
52 static QRawFont rawFontForCharacters(const QString& string, const QFont& requestedFont)
53 {
54     QFont font(requestedFont);
55     font.setStyleStrategy(QFont::NoFontMerging);
56
57     QTextLayout layout(string, font);
58     layout.beginLayout();
59     layout.createLine();
60     layout.endLayout();
61
62     QList<QGlyphRun> glyphList = layout.glyphRuns();
63
64     ASSERT(glyphList.size() == 1);
65
66     const QGlyphRun& glyphs(glyphList.at(0));
67     QVector<quint32> glyphIndexes = glyphs.glyphIndexes();
68
69     if (glyphIndexes.isEmpty())
70         return QRawFont();
71
72     return glyphs.rawFont();
73 }
74 #endif
75
76 const SimpleFontData* FontCache::getFontDataForCharacters(const Font& font, const UChar* characters, int length)
77 {
78 #if HAVE(QRAWFONT)
79     QString qstring = QString::fromRawData(reinterpret_cast<const QChar*>(characters), length);
80     QRawFont computedFont = rawFontForCharacters(qstring, font.font());
81     if (!computedFont.isValid())
82         return 0;
83     FontPlatformData alternateFont(computedFont);
84     return getCachedFontData(&alternateFont, DoNotRetain);
85 #else
86     Q_UNUSED(font);
87     Q_UNUSED(characters);
88     Q_UNUSED(length);
89     return 0;
90 #endif
91 }
92
93 SimpleFontData* FontCache::getSimilarFontPlatformData(const Font& font)
94 {
95     return 0;
96 }
97
98 SimpleFontData* FontCache::getLastResortFallbackFont(const FontDescription& fontDescription, ShouldRetain shouldRetain)
99 {
100     const AtomicString fallbackFamily = QFont(fontDescription.family().family()).lastResortFamily();
101     return getCachedFontData(new FontPlatformData(fontDescription, fallbackFamily), shouldRetain);
102 }
103
104 void FontCache::getTraitsInFamily(const AtomicString&, Vector<unsigned>&)
105 {
106 }
107
108 FontPlatformData* FontCache::createFontPlatformData(const FontDescription& fontDescription, const AtomicString& familyName)
109 {
110 #if QT_VERSION >= QT_VERSION_CHECK(4, 8, 0)
111     QFontDatabase db;
112     if (!db.hasFamily(familyName))
113         return 0;
114 #endif
115     return new FontPlatformData(fontDescription, familyName);
116 }
117
118 } // namespace WebCore