OSDN Git Service

Merge WebKit at r78450: Initial merge by git.
[android-x86/external-webkit.git] / Source / WebCore / platform / graphics / chromium / FontPlatformDataLinux.cpp
1 /*
2  * Copyright (c) 2006, 2007, 2008, 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 "FontPlatformData.h"
33
34 #include "HarfbuzzSkia.h"
35 #include "NotImplemented.h"
36 #include "PlatformBridge.h"
37 #include "PlatformString.h"
38
39 #include "SkAdvancedTypefaceMetrics.h"
40 #include "SkPaint.h"
41 #include "SkTypeface.h"
42
43 #include <wtf/text/StringImpl.h> 
44
45 namespace WebCore {
46
47 static SkPaint::Hinting skiaHinting = SkPaint::kNormal_Hinting;
48 static bool isSkiaAntiAlias = true;
49 static bool isSkiaSubpixelGlyphs = false;
50
51 void FontPlatformData::setHinting(SkPaint::Hinting hinting)
52 {
53     skiaHinting = hinting;
54 }
55
56 void FontPlatformData::setAntiAlias(bool isAntiAlias)
57 {
58     isSkiaAntiAlias = isAntiAlias;
59 }
60
61 void FontPlatformData::setSubpixelGlyphs(bool isSubpixelGlyphs)
62 {
63     isSkiaSubpixelGlyphs = isSubpixelGlyphs;
64 }
65
66 FontPlatformData::RefCountedHarfbuzzFace::~RefCountedHarfbuzzFace()
67 {
68     HB_FreeFace(m_harfbuzzFace);
69 }
70
71 FontPlatformData::FontPlatformData(const FontPlatformData& src)
72     : m_typeface(src.m_typeface)
73     , m_family(src.m_family)
74     , m_textSize(src.m_textSize)
75     , m_emSizeInFontUnits(src.m_emSizeInFontUnits)
76     , m_fakeBold(src.m_fakeBold)
77     , m_fakeItalic(src.m_fakeItalic)
78     , m_orientation(src.m_orientation)
79     , m_style(src.m_style)
80     , m_harfbuzzFace(src.m_harfbuzzFace)
81 {
82     SkSafeRef(m_typeface);
83 }
84
85 FontPlatformData::FontPlatformData(SkTypeface* tf, const char* family, float textSize, bool fakeBold, bool fakeItalic, FontOrientation orientation)
86     : m_typeface(tf)
87     , m_family(family)
88     , m_textSize(textSize)
89     , m_emSizeInFontUnits(0)
90     , m_fakeBold(fakeBold)
91     , m_fakeItalic(fakeItalic)
92     , m_orientation(orientation)
93 {
94     SkSafeRef(m_typeface);
95     querySystemForRenderStyle();
96 }
97
98 FontPlatformData::FontPlatformData(const FontPlatformData& src, float textSize)
99     : m_typeface(src.m_typeface)
100     , m_family(src.m_family)
101     , m_textSize(textSize)
102     , m_emSizeInFontUnits(src.m_emSizeInFontUnits)
103     , m_fakeBold(src.m_fakeBold)
104     , m_fakeItalic(src.m_fakeItalic)
105     , m_harfbuzzFace(src.m_harfbuzzFace)
106 {
107     SkSafeRef(m_typeface);
108     querySystemForRenderStyle();
109 }
110
111 FontPlatformData::~FontPlatformData()
112 {
113     SkSafeUnref(m_typeface);
114 }
115
116 int FontPlatformData::emSizeInFontUnits() const
117 {
118     if (m_emSizeInFontUnits)
119         return m_emSizeInFontUnits;
120
121     SkAdvancedTypefaceMetrics* metrics = m_typeface->getAdvancedTypefaceMetrics(false);
122     m_emSizeInFontUnits = metrics->fEmSize;
123     metrics->unref();
124     return m_emSizeInFontUnits;
125 }
126
127 FontPlatformData& FontPlatformData::operator=(const FontPlatformData& src)
128 {
129     SkRefCnt_SafeAssign(m_typeface, src.m_typeface);
130
131     m_family = src.m_family;
132     m_textSize = src.m_textSize;
133     m_fakeBold = src.m_fakeBold;
134     m_fakeItalic = src.m_fakeItalic;
135     m_harfbuzzFace = src.m_harfbuzzFace;
136     m_orientation = src.m_orientation;
137     m_style = src.m_style;
138     m_emSizeInFontUnits = src.m_emSizeInFontUnits;
139
140     return *this;
141 }
142
143 #ifndef NDEBUG
144 String FontPlatformData::description() const
145 {
146     return String();
147 }
148 #endif
149
150 void FontPlatformData::setupPaint(SkPaint* paint) const
151 {
152     const float ts = m_textSize >= 0 ? m_textSize : 12;
153
154     paint->setAntiAlias(m_style.useAntiAlias == FontRenderStyle::NoPreference ? isSkiaAntiAlias : m_style.useAntiAlias);
155     switch (m_style.useHinting) {
156     case FontRenderStyle::NoPreference:
157         paint->setHinting(skiaHinting);
158         break;
159     case 0:
160         paint->setHinting(SkPaint::kNo_Hinting);
161         break;
162     default:
163         paint->setHinting(static_cast<SkPaint::Hinting>(m_style.hintStyle));
164         break;
165     }
166
167     paint->setEmbeddedBitmapText(m_style.useBitmaps);
168     paint->setTextSize(SkFloatToScalar(ts));
169     paint->setTypeface(m_typeface);
170     paint->setFakeBoldText(m_fakeBold);
171     paint->setTextSkewX(m_fakeItalic ? -SK_Scalar1 / 4 : 0);
172     paint->setAutohinted(m_style.useAutoHint);
173
174     if (m_style.useAntiAlias == 1 || (m_style.useAntiAlias == FontRenderStyle::NoPreference && isSkiaAntiAlias))
175         paint->setLCDRenderText(m_style.useSubpixel == FontRenderStyle::NoPreference ? isSkiaSubpixelGlyphs : m_style.useSubpixel);
176 }
177
178 SkFontID FontPlatformData::uniqueID() const
179 {
180     return m_typeface->uniqueID();
181 }
182
183 bool FontPlatformData::operator==(const FontPlatformData& a) const
184 {
185     // If either of the typeface pointers are invalid (either NULL or the
186     // special deleted value) then we test for pointer equality. Otherwise, we
187     // call SkTypeface::Equal on the valid pointers.
188     bool typefacesEqual;
189     if (m_typeface == hashTableDeletedFontValue()
190         || a.m_typeface == hashTableDeletedFontValue()
191         || !m_typeface
192         || !a.m_typeface)
193         typefacesEqual = m_typeface == a.m_typeface;
194     else
195         typefacesEqual = SkTypeface::Equal(m_typeface, a.m_typeface);
196
197     return typefacesEqual 
198         && m_textSize == a.m_textSize
199         && m_fakeBold == a.m_fakeBold
200         && m_fakeItalic == a.m_fakeItalic
201         && m_orientation == a.m_orientation
202         && m_style == a.m_style;
203 }
204
205 unsigned FontPlatformData::hash() const
206 {
207     unsigned h = SkTypeface::UniqueID(m_typeface);
208     h ^= 0x01010101 * ((static_cast<int>(m_orientation) << 2) | (static_cast<int>(m_fakeBold) << 1) | static_cast<int>(m_fakeItalic));
209
210     // This memcpy is to avoid a reinterpret_cast that breaks strict-aliasing
211     // rules. Memcpy is generally optimized enough so that performance doesn't
212     // matter here.
213     uint32_t textSizeBytes;
214     memcpy(&textSizeBytes, &m_textSize, sizeof(uint32_t));
215     h ^= textSizeBytes;
216
217     return h;
218 }
219
220 bool FontPlatformData::isFixedPitch() const
221 {
222     notImplemented();
223     return false;
224 }
225
226 HB_FaceRec_* FontPlatformData::harfbuzzFace() const
227 {
228     if (!m_harfbuzzFace)
229         m_harfbuzzFace = RefCountedHarfbuzzFace::create(HB_NewFace(const_cast<FontPlatformData*>(this), harfbuzzSkiaGetTable));
230
231     return m_harfbuzzFace->face();
232 }
233
234 void FontPlatformData::querySystemForRenderStyle()
235 {
236     if (!m_family.length()) {
237         // We don't have a family for this. Probably because it's a webfont. We
238         // set all the values to 'no preference' and take the defaults passed
239         // in from XSETTINGS.
240         m_style.useBitmaps = FontRenderStyle::NoPreference;
241         m_style.useAutoHint = FontRenderStyle::NoPreference;
242         m_style.useHinting = FontRenderStyle::NoPreference;
243         m_style.useAntiAlias = FontRenderStyle::NoPreference;
244         m_style.useSubpixel = FontRenderStyle::NoPreference;
245         return;
246     }
247
248     PlatformBridge::getRenderStyleForStrike(m_family.data(), (((int)m_textSize) << 2) | (m_typeface->style() & 3), &m_style);
249 }
250
251 }  // namespace WebCore