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