Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / platform / fonts / win / FontPlatformDataWin.cpp
1 /*
2  * Copyright (C) 2006, 2007 Apple Computer, Inc.
3  * Copyright (c) 2006, 2007, 2008, 2009, 2012 Google Inc. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  *     * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *     * Redistributions in binary form must reproduce the above
12  * copyright notice, this list of conditions and the following disclaimer
13  * in the documentation and/or other materials provided with the
14  * distribution.
15  *     * Neither the name of Google Inc. nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 #include "config.h"
33 #include "platform/fonts/FontPlatformData.h"
34
35 #include "SkTypeface.h"
36 #include "platform/LayoutTestSupport.h"
37 #include "platform/fonts/FontCache.h"
38 #include "platform/graphics/GraphicsContext.h"
39 #include <windows.h>
40
41 namespace blink {
42
43 // Maximum font size, in pixels, at which embedded bitmaps will be used
44 // if available.
45 const float kMaxSizeForEmbeddedBitmap = 24.0f;
46
47 void FontPlatformData::setupPaint(SkPaint* paint, GraphicsContext* context) const
48 {
49     const float ts = m_textSize >= 0 ? m_textSize : 12;
50     paint->setTextSize(SkFloatToScalar(m_textSize));
51     paint->setTypeface(typeface());
52     paint->setFakeBoldText(m_syntheticBold);
53     paint->setTextSkewX(m_syntheticItalic ? -SK_Scalar1 / 4 : 0);
54
55     uint32_t textFlags = paintTextFlags();
56     uint32_t flags = paint->getFlags();
57     static const uint32_t textFlagsMask = SkPaint::kAntiAlias_Flag |
58         SkPaint::kLCDRenderText_Flag |
59         SkPaint::kGenA8FromLCD_Flag;
60     flags &= ~textFlagsMask;
61
62     if (ts <= kMaxSizeForEmbeddedBitmap)
63         flags |= SkPaint::kEmbeddedBitmapText_Flag;
64
65     if (ts >= m_minSizeForAntiAlias) {
66
67         if (m_useSubpixelPositioning
68             // Disable subpixel text for certain older fonts at smaller sizes as
69             // they tend to get quite blurry at non-integer sizes and positions.
70             // For high-DPI this workaround isn't required.
71             && (ts >= m_minSizeForSubpixel
72                 || FontCache::fontCache()->deviceScaleFactor() >= 1.5)
73
74             // Subpixel text positioning looks pretty bad without font
75             // smoothing. Disable it unless some type of font smoothing is used.
76             // As most tests run without font smoothing we enable it for tests
77             // to ensure we get good test coverage matching the more common
78             // smoothing enabled behavior.
79             && ((textFlags & SkPaint::kAntiAlias_Flag)
80                 || LayoutTestSupport::isRunningLayoutTest()))
81             flags |= SkPaint::kSubpixelText_Flag;
82
83         // Only set painting flags when we're actually painting.
84         if (context && !context->couldUseLCDRenderedText()) {
85             textFlags &= ~SkPaint::kLCDRenderText_Flag;
86             // If we *just* clear our request for LCD, then GDI seems to
87             // sometimes give us AA text, and sometimes give us BW text. Since the
88             // original intent was LCD, we want to force AA (rather than BW), so we
89             // add a special bit to tell Skia to do its best to avoid the BW: by
90             // drawing LCD offscreen and downsampling that to AA.
91             textFlags |= SkPaint::kGenA8FromLCD_Flag;
92         }
93         SkASSERT(!(textFlags & ~textFlagsMask));
94         flags |= textFlags;
95     }
96
97     paint->setFlags(flags);
98 }
99
100 // Lookup the current system settings for font smoothing.
101 // We cache these values for performance, but if the browser has a way to be
102 // notified when these change, we could re-query them at that time.
103 static uint32_t getSystemTextFlags()
104 {
105     static bool gInited;
106     static uint32_t gFlags;
107     if (!gInited) {
108         BOOL enabled;
109         gFlags = 0;
110         if (SystemParametersInfo(SPI_GETFONTSMOOTHING, 0, &enabled, 0)) {
111             if (enabled) {
112                 gFlags |= SkPaint::kAntiAlias_Flag;
113
114                 UINT smoothType;
115                 if (SystemParametersInfo(SPI_GETFONTSMOOTHINGTYPE, 0, &smoothType, 0)) {
116                     if (FE_FONTSMOOTHINGCLEARTYPE == smoothType)
117                         gFlags |= SkPaint::kLCDRenderText_Flag;
118                 }
119             }
120         } else {
121             // SystemParametersInfo will fail only under full sandbox lockdown on Win8+.
122             // So, we default to settings we know are supported and look good.
123             // FIXME(eae): We should be querying the DirectWrite settings directly
124             // so we can respect the settings for users who turn off smoothing.
125             gFlags = SkPaint::kAntiAlias_Flag | SkPaint::kLCDRenderText_Flag;
126         }
127         gInited = true;
128     }
129     return gFlags;
130 }
131
132 static bool isWebFont(const String& familyName)
133 {
134     // Web-fonts have artifical names constructed to always be:
135     // 1. 24 characters, followed by a '\0'
136     // 2. the last two characters are '=='
137     return familyName.length() == 24
138         && '=' == familyName[22] && '=' == familyName[23];
139 }
140
141 static int computePaintTextFlags(String fontFamilyName)
142 {
143     if (LayoutTestSupport::isRunningLayoutTest())
144         return LayoutTestSupport::isFontAntialiasingEnabledForTest() ? SkPaint::kAntiAlias_Flag : 0;
145
146     int textFlags = getSystemTextFlags();
147
148     // Many web-fonts are so poorly hinted that they are terrible to read when drawn in BW.
149     // In these cases, we have decided to FORCE these fonts to be drawn with at least grayscale AA,
150     // even when the System (getSystemTextFlags) tells us to draw only in BW.
151     if (isWebFont(fontFamilyName))
152         textFlags |= SkPaint::kAntiAlias_Flag;
153
154     return textFlags;
155 }
156
157
158 void FontPlatformData::querySystemForRenderStyle(bool)
159 {
160     m_paintTextFlags = computePaintTextFlags(fontFamilyName());
161 }
162
163 bool FontPlatformData::defaultUseSubpixelPositioning()
164 {
165     return FontCache::fontCache()->useSubpixelPositioning();
166 }
167
168 } // namespace blink