Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / platform / fonts / harfbuzz / FontPlatformDataHarfBuzz.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 "platform/fonts/harfbuzz/FontPlatformDataHarfBuzz.h"
33
34 #include "RuntimeEnabledFeatures.h"
35 #include "SkTypeface.h"
36 #include "platform/LayoutTestSupport.h"
37 #include "platform/NotImplemented.h"
38 #include "platform/fonts/FontCache.h"
39 #include "platform/fonts/harfbuzz/HarfBuzzFace.h"
40
41 #include "public/platform/linux/WebFontInfo.h"
42 #include "public/platform/linux/WebFontRenderStyle.h"
43 #include "public/platform/linux/WebSandboxSupport.h"
44 #include "public/platform/Platform.h"
45 #include "wtf/text/WTFString.h"
46
47 namespace WebCore {
48
49 static SkPaint::Hinting skiaHinting = SkPaint::kNormal_Hinting;
50 static bool useSkiaAutoHint = true;
51 static bool useSkiaBitmaps = true;
52 static bool useSkiaAntiAlias = true;
53 static bool useSkiaSubpixelRendering = false;
54
55 void FontPlatformData::setHinting(SkPaint::Hinting hinting)
56 {
57     skiaHinting = hinting;
58 }
59
60 void FontPlatformData::setAutoHint(bool useAutoHint)
61 {
62     useSkiaAutoHint = useAutoHint;
63 }
64
65 void FontPlatformData::setUseBitmaps(bool useBitmaps)
66 {
67     useSkiaBitmaps = useBitmaps;
68 }
69
70 void FontPlatformData::setAntiAlias(bool useAntiAlias)
71 {
72     useSkiaAntiAlias = useAntiAlias;
73 }
74
75 void FontPlatformData::setSubpixelRendering(bool useSubpixelRendering)
76 {
77     useSkiaSubpixelRendering = useSubpixelRendering;
78 }
79
80 FontPlatformData::FontPlatformData(WTF::HashTableDeletedValueType)
81     : m_textSize(0)
82     , m_syntheticBold(false)
83     , m_syntheticItalic(false)
84     , m_orientation(Horizontal)
85     , m_isHashTableDeletedValue(true)
86 {
87 }
88
89 FontPlatformData::FontPlatformData()
90     : m_textSize(0)
91     , m_syntheticBold(false)
92     , m_syntheticItalic(false)
93     , m_orientation(Horizontal)
94     , m_isHashTableDeletedValue(false)
95 {
96 }
97
98 FontPlatformData::FontPlatformData(float textSize, bool syntheticBold, bool syntheticItalic)
99     : m_textSize(textSize)
100     , m_syntheticBold(syntheticBold)
101     , m_syntheticItalic(syntheticItalic)
102     , m_orientation(Horizontal)
103     , m_isHashTableDeletedValue(false)
104 {
105 }
106
107 FontPlatformData::FontPlatformData(const FontPlatformData& src)
108     : m_typeface(src.m_typeface)
109     , m_family(src.m_family)
110     , m_textSize(src.m_textSize)
111     , m_syntheticBold(src.m_syntheticBold)
112     , m_syntheticItalic(src.m_syntheticItalic)
113     , m_orientation(src.m_orientation)
114     , m_style(src.m_style)
115     , m_harfBuzzFace(nullptr)
116     , m_isHashTableDeletedValue(false)
117 {
118 }
119
120 FontPlatformData::FontPlatformData(PassRefPtr<SkTypeface> tf, const char* family, float textSize, bool syntheticBold, bool syntheticItalic, FontOrientation orientation, bool subpixelTextPosition)
121     : m_typeface(tf)
122     , m_family(family)
123     , m_textSize(textSize)
124     , m_syntheticBold(syntheticBold)
125     , m_syntheticItalic(syntheticItalic)
126     , m_orientation(orientation)
127     , m_isHashTableDeletedValue(false)
128 {
129     querySystemForRenderStyle(subpixelTextPosition);
130 }
131
132 FontPlatformData::FontPlatformData(const FontPlatformData& src, float textSize)
133     : m_typeface(src.m_typeface)
134     , m_family(src.m_family)
135     , m_textSize(textSize)
136     , m_syntheticBold(src.m_syntheticBold)
137     , m_syntheticItalic(src.m_syntheticItalic)
138     , m_orientation(src.m_orientation)
139     , m_harfBuzzFace(nullptr)
140     , m_isHashTableDeletedValue(false)
141 {
142     querySystemForRenderStyle(FontDescription::subpixelPositioning());
143 }
144
145 FontPlatformData::~FontPlatformData()
146 {
147 }
148
149 FontPlatformData& FontPlatformData::operator=(const FontPlatformData& src)
150 {
151     m_typeface = src.m_typeface;
152     m_family = src.m_family;
153     m_textSize = src.m_textSize;
154     m_syntheticBold = src.m_syntheticBold;
155     m_syntheticItalic = src.m_syntheticItalic;
156     m_harfBuzzFace = nullptr;
157     m_orientation = src.m_orientation;
158     m_style = src.m_style;
159
160     return *this;
161 }
162
163 #ifndef NDEBUG
164 String FontPlatformData::description() const
165 {
166     return String();
167 }
168 #endif
169
170 void FontPlatformData::setupPaint(SkPaint* paint, GraphicsContext*) const
171 {
172     paint->setAntiAlias(m_style.useAntiAlias);
173     paint->setHinting(static_cast<SkPaint::Hinting>(m_style.hintStyle));
174     paint->setEmbeddedBitmapText(m_style.useBitmaps);
175     paint->setAutohinted(m_style.useAutoHint);
176     if (m_style.useAntiAlias)
177         paint->setLCDRenderText(m_style.useSubpixelRendering);
178
179     // TestRunner specifically toggles the subpixel positioning flag.
180     if (RuntimeEnabledFeatures::subpixelFontScalingEnabled() && !isRunningLayoutTest())
181         paint->setSubpixelText(true);
182     else
183         paint->setSubpixelText(m_style.useSubpixelPositioning);
184
185     const float ts = m_textSize >= 0 ? m_textSize : 12;
186     paint->setTextSize(SkFloatToScalar(ts));
187     paint->setTypeface(m_typeface.get());
188     paint->setFakeBoldText(m_syntheticBold);
189     paint->setTextSkewX(m_syntheticItalic ? -SK_Scalar1 / 4 : 0);
190 }
191
192 SkFontID FontPlatformData::uniqueID() const
193 {
194     return m_typeface->uniqueID();
195 }
196
197 String FontPlatformData::fontFamilyName() const
198 {
199     // FIXME(crbug.com/326582): come up with a proper way of handling SVG.
200     if (!this->typeface())
201         return "";
202     SkTypeface::LocalizedStrings* fontFamilyIterator = this->typeface()->createFamilyNameIterator();
203     SkTypeface::LocalizedString localizedString;
204     while (fontFamilyIterator->next(&localizedString) && !localizedString.fString.size()) { }
205     fontFamilyIterator->unref();
206     return String(localizedString.fString.c_str());
207 }
208
209 bool FontPlatformData::operator==(const FontPlatformData& a) const
210 {
211     // If either of the typeface pointers are null then we test for pointer
212     // equality. Otherwise, we call SkTypeface::Equal on the valid pointers.
213     bool typefacesEqual;
214     if (!m_typeface || !a.m_typeface)
215         typefacesEqual = m_typeface == a.m_typeface;
216     else
217         typefacesEqual = SkTypeface::Equal(m_typeface.get(), a.m_typeface.get());
218
219     return typefacesEqual
220         && m_textSize == a.m_textSize
221         && m_syntheticBold == a.m_syntheticBold
222         && m_syntheticItalic == a.m_syntheticItalic
223         && m_orientation == a.m_orientation
224         && m_style == a.m_style
225         && m_isHashTableDeletedValue == a.m_isHashTableDeletedValue;
226 }
227
228 bool FontPlatformData::isFixedPitch() const
229 {
230     notImplemented();
231     return false;
232 }
233
234 HarfBuzzFace* FontPlatformData::harfBuzzFace() const
235 {
236     if (!m_harfBuzzFace)
237         m_harfBuzzFace = HarfBuzzFace::create(const_cast<FontPlatformData*>(this), uniqueID());
238
239     return m_harfBuzzFace.get();
240 }
241
242 void FontPlatformData::getRenderStyleForStrike(const char* font, int sizeAndStyle)
243 {
244     blink::WebFontRenderStyle style;
245
246 #if OS(ANDROID)
247     style.setDefaults();
248 #else
249     if (!font || !*font)
250         style.setDefaults(); // It's probably a webfont. Take the system defaults.
251     else if (blink::Platform::current()->sandboxSupport())
252         blink::Platform::current()->sandboxSupport()->getRenderStyleForStrike(font, sizeAndStyle, &style);
253     else
254         blink::WebFontInfo::renderStyleForStrike(font, sizeAndStyle, &style);
255 #endif
256
257     style.toFontRenderStyle(&m_style);
258 }
259
260 void FontPlatformData::querySystemForRenderStyle(bool useSkiaSubpixelPositioning)
261 {
262     getRenderStyleForStrike(m_family.data(), (((int)m_textSize) << 2) | (m_typeface->style() & 3));
263
264     // Fix FontRenderStyle::NoPreference to actual styles.
265     if (m_style.useAntiAlias == FontRenderStyle::NoPreference)
266          m_style.useAntiAlias = useSkiaAntiAlias;
267
268     if (!m_style.useHinting)
269         m_style.hintStyle = SkPaint::kNo_Hinting;
270     else if (m_style.useHinting == FontRenderStyle::NoPreference)
271         m_style.hintStyle = skiaHinting;
272
273     if (m_style.useBitmaps == FontRenderStyle::NoPreference)
274         m_style.useBitmaps = useSkiaBitmaps;
275     if (m_style.useAutoHint == FontRenderStyle::NoPreference)
276         m_style.useAutoHint = useSkiaAutoHint;
277     if (m_style.useAntiAlias == FontRenderStyle::NoPreference)
278         m_style.useAntiAlias = useSkiaAntiAlias;
279     if (m_style.useSubpixelRendering == FontRenderStyle::NoPreference)
280         m_style.useSubpixelRendering = useSkiaSubpixelRendering;
281
282     // TestRunner specifically toggles the subpixel positioning flag.
283     if (m_style.useSubpixelPositioning == FontRenderStyle::NoPreference
284         || isRunningLayoutTest())
285         m_style.useSubpixelPositioning = useSkiaSubpixelPositioning;
286 }
287
288 bool FontPlatformData::defaultUseSubpixelPositioning()
289 {
290     return FontDescription::subpixelPositioning();
291 }
292
293 } // namespace WebCore