~ubuntu-branches/ubuntu/raring/qtwebkit-source/raring-proposed

« back to all changes in this revision

Viewing changes to Source/WebCore/platform/graphics/skia/FontCacheSkia.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-02-18 14:24:18 UTC
  • Revision ID: package-import@ubuntu.com-20130218142418-eon0jmjg3nj438uy
Tags: upstream-2.3
ImportĀ upstreamĀ versionĀ 2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2006, 2007, 2008, 2009 Google Inc. All rights reserved.
 
3
 * 
 
4
 * Redistribution and use in source and binary forms, with or without
 
5
 * modification, are permitted provided that the following conditions are
 
6
 * met:
 
7
 * 
 
8
 *     * Redistributions of source code must retain the above copyright
 
9
 * notice, this list of conditions and the following disclaimer.
 
10
 *     * Redistributions in binary form must reproduce the above
 
11
 * copyright notice, this list of conditions and the following disclaimer
 
12
 * in the documentation and/or other materials provided with the
 
13
 * distribution.
 
14
 *     * Neither the name of Google Inc. nor the names of its
 
15
 * contributors may be used to endorse or promote products derived from
 
16
 * this software without specific prior written permission.
 
17
 * 
 
18
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 
19
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 
20
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 
21
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 
22
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 
23
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 
24
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 
25
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 
26
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
27
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 
28
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
29
 */
 
30
 
 
31
#include "config.h"
 
32
#include "FontCache.h"
 
33
#include "Font.h"
 
34
#include "FontDescription.h"
 
35
#include "FontFamily.h"
 
36
#include "FontPlatformData.h"
 
37
#include "Logging.h"
 
38
#include "NotImplemented.h"
 
39
#include "SimpleFontData.h"
 
40
#include "SkPaint.h"
 
41
#include "SkTypeface.h"
 
42
#include "SkUtils.h"
 
43
#include <unicode/locid.h>
 
44
#include <wtf/Assertions.h>
 
45
#include <wtf/text/AtomicString.h>
 
46
#include <wtf/text/CString.h>
 
47
 
 
48
namespace WebCore {
 
49
 
 
50
void FontCache::platformInit()
 
51
{
 
52
}
 
53
 
 
54
PassRefPtr<SimpleFontData> FontCache::getFontDataForCharacters(const Font& font, const UChar* characters, int length)
 
55
{
 
56
    icu::Locale locale = icu::Locale::getDefault();
 
57
    FontCache::SimpleFontFamily family;
 
58
    FontCache::getFontFamilyForCharacters(characters, length, locale.getLanguage(), &family);
 
59
    if (family.name.isEmpty())
 
60
        return 0;
 
61
 
 
62
    AtomicString atomicFamily(family.name);
 
63
    // Changes weight and/or italic of given FontDescription depends on
 
64
    // the result of fontconfig so that keeping the correct font mapping
 
65
    // of the given characters. See http://crbug.com/32109 for details.
 
66
    bool shouldSetFakeBold = false;
 
67
    bool shouldSetFakeItalic = false;
 
68
    FontDescription description(font.fontDescription());
 
69
    if (family.isBold && description.weight() < FontWeightBold)
 
70
        description.setWeight(FontWeightBold);
 
71
    if (!family.isBold && description.weight() >= FontWeightBold) {
 
72
        shouldSetFakeBold = true;
 
73
        description.setWeight(FontWeightNormal);
 
74
    }
 
75
    if (family.isItalic && description.italic() == FontItalicOff)
 
76
        description.setItalic(FontItalicOn);
 
77
    if (!family.isItalic && description.italic() == FontItalicOn) {
 
78
        shouldSetFakeItalic = true;
 
79
        description.setItalic(FontItalicOff);
 
80
    }
 
81
 
 
82
    FontPlatformData* substitutePlatformData = getCachedFontPlatformData(description, atomicFamily, DoNotRetain);
 
83
    if (!substitutePlatformData)
 
84
        return 0;
 
85
    FontPlatformData platformData = FontPlatformData(*substitutePlatformData);
 
86
    platformData.setFakeBold(shouldSetFakeBold);
 
87
    platformData.setFakeItalic(shouldSetFakeItalic);
 
88
    return getCachedFontData(&platformData, DoNotRetain);
 
89
}
 
90
 
 
91
PassRefPtr<SimpleFontData> FontCache::getSimilarFontPlatformData(const Font& font)
 
92
{
 
93
    return 0;
 
94
}
 
95
 
 
96
PassRefPtr<SimpleFontData> FontCache::getLastResortFallbackFont(const FontDescription& description, ShouldRetain shouldRetain)
 
97
{
 
98
    DEFINE_STATIC_LOCAL(const AtomicString, sansStr, ("Sans", AtomicString::ConstructFromLiteral));
 
99
    DEFINE_STATIC_LOCAL(const AtomicString, serifStr, ("Serif", AtomicString::ConstructFromLiteral));
 
100
    DEFINE_STATIC_LOCAL(const AtomicString, monospaceStr, ("Monospace", AtomicString::ConstructFromLiteral));
 
101
 
 
102
    FontPlatformData* fontPlatformData = 0;
 
103
    switch (description.genericFamily()) {
 
104
    case FontDescription::SerifFamily:
 
105
        fontPlatformData = getCachedFontPlatformData(description, serifStr);
 
106
        break;
 
107
    case FontDescription::MonospaceFamily:
 
108
        fontPlatformData = getCachedFontPlatformData(description, monospaceStr);
 
109
        break;
 
110
    case FontDescription::SansSerifFamily:
 
111
    default:
 
112
        fontPlatformData = getCachedFontPlatformData(description, sansStr);
 
113
        break;
 
114
    }
 
115
 
 
116
    if (!fontPlatformData) {
 
117
        // we should at least have Arial; this is the SkFontHost_fontconfig last resort fallback
 
118
        DEFINE_STATIC_LOCAL(const AtomicString, arialStr, ("Arial", AtomicString::ConstructFromLiteral));
 
119
        fontPlatformData = getCachedFontPlatformData(description, arialStr);
 
120
    }
 
121
 
 
122
    ASSERT(fontPlatformData);
 
123
    return getCachedFontData(fontPlatformData, shouldRetain);
 
124
}
 
125
 
 
126
void FontCache::getTraitsInFamily(const AtomicString& familyName,
 
127
                                  Vector<unsigned>& traitsMasks)
 
128
{
 
129
    notImplemented();
 
130
}
 
131
 
 
132
FontPlatformData* FontCache::createFontPlatformData(const FontDescription& fontDescription,
 
133
                                                    const AtomicString& family)
 
134
{
 
135
    const char* name = 0;
 
136
    CString s;
 
137
 
 
138
    // If we're creating a fallback font (e.g. "-webkit-monospace"), convert the name into
 
139
    // the fallback name (like "monospace") that fontconfig understands.
 
140
    if (!family.length() || family.startsWith("-webkit-")) {
 
141
        static const struct {
 
142
            FontDescription::GenericFamilyType mType;
 
143
            const char* mName;
 
144
        } fontDescriptions[] = {
 
145
            { FontDescription::SerifFamily, "serif" },
 
146
            { FontDescription::SansSerifFamily, "sans-serif" },
 
147
            { FontDescription::MonospaceFamily, "monospace" },
 
148
            { FontDescription::CursiveFamily, "cursive" },
 
149
            { FontDescription::FantasyFamily, "fantasy" }
 
150
        };
 
151
 
 
152
        FontDescription::GenericFamilyType type = fontDescription.genericFamily();
 
153
        for (unsigned i = 0; i < SK_ARRAY_COUNT(fontDescriptions); i++) {
 
154
            if (type == fontDescriptions[i].mType) {
 
155
                name = fontDescriptions[i].mName;
 
156
                break;
 
157
            }
 
158
        }
 
159
        if (!name)
 
160
            name = "";
 
161
    } else {
 
162
        // convert the name to utf8
 
163
        s = family.string().utf8();
 
164
        name = s.data();
 
165
    }
 
166
 
 
167
    int style = SkTypeface::kNormal;
 
168
    if (fontDescription.weight() >= FontWeightBold)
 
169
        style |= SkTypeface::kBold;
 
170
    if (fontDescription.italic())
 
171
        style |= SkTypeface::kItalic;
 
172
 
 
173
    SkTypeface* tf = SkTypeface::CreateFromName(name, static_cast<SkTypeface::Style>(style));
 
174
    if (!tf)
 
175
        return 0;
 
176
 
 
177
    FontPlatformData* result =
 
178
        new FontPlatformData(tf,
 
179
                             name,
 
180
                             fontDescription.computedSize(),
 
181
                             (style & SkTypeface::kBold) && !tf->isBold(),
 
182
                             (style & SkTypeface::kItalic) && !tf->isItalic(),
 
183
                             fontDescription.orientation(),
 
184
                             fontDescription.textOrientation());
 
185
    tf->unref();
 
186
    return result;
 
187
}
 
188
 
 
189
} // namespace WebCore