Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / platform / fonts / linux / 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 "public/platform/Platform.h"
33
34 #include "SkTypeface.h"
35 #include "platform/LayoutTestSupport.h"
36 #include "platform/RuntimeEnabledFeatures.h"
37 #include "platform/fonts/FontPlatformData.h"
38 #include "platform/graphics/GraphicsContext.h"
39 #include "public/platform/linux/WebFontRenderStyle.h"
40 #include "public/platform/linux/WebSandboxSupport.h"
41
42 namespace blink {
43
44 static SkPaint::Hinting skiaHinting = SkPaint::kNormal_Hinting;
45 static bool useSkiaAutoHint = true;
46 static bool useSkiaBitmaps = true;
47 static bool useSkiaAntiAlias = true;
48 static bool useSkiaSubpixelRendering = false;
49
50 void FontPlatformData::setHinting(SkPaint::Hinting hinting)
51 {
52     skiaHinting = hinting;
53 }
54
55 void FontPlatformData::setAutoHint(bool useAutoHint)
56 {
57     useSkiaAutoHint = useAutoHint;
58 }
59
60 void FontPlatformData::setUseBitmaps(bool useBitmaps)
61 {
62     useSkiaBitmaps = useBitmaps;
63 }
64
65 void FontPlatformData::setAntiAlias(bool useAntiAlias)
66 {
67     useSkiaAntiAlias = useAntiAlias;
68 }
69
70 void FontPlatformData::setSubpixelRendering(bool useSubpixelRendering)
71 {
72     useSkiaSubpixelRendering = useSubpixelRendering;
73 }
74
75 void FontPlatformData::setupPaint(SkPaint* paint, GraphicsContext* context, const Font*) const
76 {
77     paint->setAntiAlias(m_style.useAntiAlias);
78     paint->setHinting(static_cast<SkPaint::Hinting>(m_style.hintStyle));
79     paint->setEmbeddedBitmapText(m_style.useBitmaps);
80     paint->setAutohinted(m_style.useAutoHint);
81     if (m_style.useAntiAlias)
82         paint->setLCDRenderText(m_style.useSubpixelRendering);
83
84     // Do not enable subpixel text on low-dpi if full hinting is requested.
85     bool useSubpixelText = RuntimeEnabledFeatures::subpixelFontScalingEnabled()
86         && (paint->getHinting() != SkPaint::kFull_Hinting
87             || (context && context->deviceScaleFactor() > 1.0f));
88
89     // TestRunner specifically toggles the subpixel positioning flag.
90     if (useSubpixelText && !LayoutTestSupport::isRunningLayoutTest())
91         paint->setSubpixelText(true);
92     else
93         paint->setSubpixelText(m_style.useSubpixelPositioning);
94
95     const float ts = m_textSize >= 0 ? m_textSize : 12;
96     paint->setTextSize(SkFloatToScalar(ts));
97     paint->setTypeface(m_typeface.get());
98     paint->setFakeBoldText(m_syntheticBold);
99     paint->setTextSkewX(m_syntheticItalic ? -SK_Scalar1 / 4 : 0);
100 }
101
102 void FontPlatformData::querySystemForRenderStyle(bool useSkiaSubpixelPositioning)
103 {
104     WebFontRenderStyle style;
105 #if OS(ANDROID)
106     style.setDefaults();
107 #else
108     // If the font name is missing (i.e. probably a web font) or the sandbox is disabled, use the system defaults.
109     if (!m_family.length() || !Platform::current()->sandboxSupport()) {
110         style.setDefaults();
111     } else {
112         const int sizeAndStyle = (((int)m_textSize) << 2) | (m_typeface->style() & 3);
113         Platform::current()->sandboxSupport()->getRenderStyleForStrike(m_family.data(), sizeAndStyle, &style);
114     }
115 #endif
116     style.toFontRenderStyle(&m_style);
117
118     // Fix FontRenderStyle::NoPreference to actual styles.
119     if (m_style.useAntiAlias == FontRenderStyle::NoPreference)
120         m_style.useAntiAlias = useSkiaAntiAlias;
121
122     if (!m_style.useHinting)
123         m_style.hintStyle = SkPaint::kNo_Hinting;
124     else if (m_style.useHinting == FontRenderStyle::NoPreference)
125         m_style.hintStyle = skiaHinting;
126
127     if (m_style.useBitmaps == FontRenderStyle::NoPreference)
128         m_style.useBitmaps = useSkiaBitmaps;
129     if (m_style.useAutoHint == FontRenderStyle::NoPreference)
130         m_style.useAutoHint = useSkiaAutoHint;
131     if (m_style.useAntiAlias == FontRenderStyle::NoPreference)
132         m_style.useAntiAlias = useSkiaAntiAlias;
133     if (m_style.useSubpixelRendering == FontRenderStyle::NoPreference)
134         m_style.useSubpixelRendering = useSkiaSubpixelRendering;
135
136     // TestRunner specifically toggles the subpixel positioning flag.
137     if (m_style.useSubpixelPositioning == FontRenderStyle::NoPreference
138         || LayoutTestSupport::isRunningLayoutTest())
139         m_style.useSubpixelPositioning = useSkiaSubpixelPositioning;
140 }
141
142 bool FontPlatformData::defaultUseSubpixelPositioning()
143 {
144     return FontDescription::subpixelPositioning();
145 }
146
147 } // namespace blink