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/harfbuzz/FontPlatformDataHarfBuzz.h"
38#include "public/platform/linux/WebFontInfo.h"
39#include "public/platform/linux/WebFontRenderStyle.h"
40#include "public/platform/linux/WebSandboxSupport.h"
41
42namespace WebCore {
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*) 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    // TestRunner specifically toggles the subpixel positioning flag.
85    if (RuntimeEnabledFeatures::subpixelFontScalingEnabled()
86        && paint->getHinting() != SkPaint::kFull_Hinting
87        && !isRunningLayoutTest())
88        paint->setSubpixelText(true);
89    else
90        paint->setSubpixelText(m_style.useSubpixelPositioning);
91
92    const float ts = m_textSize >= 0 ? m_textSize : 12;
93    paint->setTextSize(SkFloatToScalar(ts));
94    paint->setTypeface(m_typeface.get());
95    paint->setFakeBoldText(m_syntheticBold);
96    paint->setTextSkewX(m_syntheticItalic ? -SK_Scalar1 / 4 : 0);
97}
98
99
100static inline void getRenderStyleForStrike(FontRenderStyle& fontRenderStyle, const char* font, int sizeAndStyle)
101{
102    blink::WebFontRenderStyle style;
103
104#if OS(ANDROID)
105    style.setDefaults();
106#else
107    if (!font || !*font)
108        style.setDefaults(); // It's probably a webfont. Take the system defaults.
109    else if (blink::Platform::current()->sandboxSupport())
110        blink::Platform::current()->sandboxSupport()->getRenderStyleForStrike(font, sizeAndStyle, &style);
111    else
112        blink::WebFontInfo::renderStyleForStrike(font, sizeAndStyle, &style);
113#endif
114
115    style.toFontRenderStyle(&fontRenderStyle);
116}
117
118void FontPlatformData::querySystemForRenderStyle(bool useSkiaSubpixelPositioning)
119{
120    getRenderStyleForStrike(m_style, m_family.data(), (((int)m_textSize) << 2) | (m_typeface->style() & 3));
121
122    // Fix FontRenderStyle::NoPreference to actual styles.
123    if (m_style.useAntiAlias == FontRenderStyle::NoPreference)
124        m_style.useAntiAlias = useSkiaAntiAlias;
125
126    if (!m_style.useHinting)
127        m_style.hintStyle = SkPaint::kNo_Hinting;
128    else if (m_style.useHinting == FontRenderStyle::NoPreference)
129        m_style.hintStyle = skiaHinting;
130
131    if (m_style.useBitmaps == FontRenderStyle::NoPreference)
132        m_style.useBitmaps = useSkiaBitmaps;
133    if (m_style.useAutoHint == FontRenderStyle::NoPreference)
134        m_style.useAutoHint = useSkiaAutoHint;
135    if (m_style.useAntiAlias == FontRenderStyle::NoPreference)
136        m_style.useAntiAlias = useSkiaAntiAlias;
137    if (m_style.useSubpixelRendering == FontRenderStyle::NoPreference)
138        m_style.useSubpixelRendering = useSkiaSubpixelRendering;
139
140    // TestRunner specifically toggles the subpixel positioning flag.
141    if (m_style.useSubpixelPositioning == FontRenderStyle::NoPreference
142        || isRunningLayoutTest())
143        m_style.useSubpixelPositioning = useSkiaSubpixelPositioning;
144}
145
146bool FontPlatformData::defaultUseSubpixelPositioning()
147{
148    return FontDescription::subpixelPositioning();
149}
150
151} // namespace WebCore
152