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 <windows.h>
33 
34 #include "third_party/blink/renderer/platform/fonts/font_cache.h"
35 #include "third_party/blink/renderer/platform/fonts/font_platform_data.h"
36 #include "third_party/blink/renderer/platform/web_test_support.h"
37 #include "third_party/skia/include/core/SkFont.h"
38 #include "third_party/skia/include/core/SkTypeface.h"
39 
40 namespace blink {
41 
SetupSkFont(SkFont * font,float,const Font *) const42 void FontPlatformData::SetupSkFont(SkFont* font, float, const Font*) const {
43   font->setSize(SkFloatToScalar(text_size_));
44   font->setTypeface(typeface_);
45   font->setEmbolden(synthetic_bold_);
46   font->setSkewX(synthetic_italic_ ? -SK_Scalar1 / 4 : 0);
47 
48   uint32_t font_flags = FontFlags();
49   if (font_flags & kSubpixelsAntiAlias) {
50     font->setEdging(SkFont::Edging::kSubpixelAntiAlias);
51   } else if (font_flags & kAntiAlias) {
52     font->setEdging(SkFont::Edging::kAntiAlias);
53   } else {
54     font->setEdging(SkFont::Edging::kAlias);
55   }
56 
57   // Only use sub-pixel positioning if anti aliasing is enabled. Otherwise,
58   // without font smoothing, subpixel text positioning leads to uneven spacing
59   // since subpixel test placement coordinates would be passed to Skia, which
60   // only has non-antialiased glyphs to draw, so they necessarily get clamped at
61   // pixel positions, which leads to uneven spacing, either too close or too far
62   // away from adjacent glyphs. We avoid this by linking the two flags.
63   if (font_flags & kAntiAlias)
64     font->setSubpixel(true);
65 
66   if (WebTestSupport::IsRunningWebTest() &&
67       !WebTestSupport::IsTextSubpixelPositioningAllowedForTest())
68     font->setSubpixel(false);
69 
70   font->setEmbeddedBitmaps(!avoid_embedded_bitmaps_);
71 }
72 
ComputeFontFlags(String font_family_name)73 static int ComputeFontFlags(String font_family_name) {
74   if (WebTestSupport::IsRunningWebTest())
75     return WebTestSupport::IsFontAntialiasingEnabledForTest()
76                ? FontPlatformData::kAntiAlias
77                : 0;
78 
79   int font_flags = 0;
80   if (FontCache::GetFontCache()->AntialiasedTextEnabled()) {
81     int lcd_flag = FontCache::GetFontCache()->LcdTextEnabled()
82                        ? FontPlatformData::kSubpixelsAntiAlias
83                        : 0;
84     font_flags = FontPlatformData::kAntiAlias | lcd_flag;
85   }
86 
87   return font_flags;
88 }
89 
QuerySystemForRenderStyle()90 void FontPlatformData::QuerySystemForRenderStyle() {
91   font_flags_ = ComputeFontFlags(FontFamilyName());
92 }
93 
94 }  // namespace blink
95