1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "third_party/blink/public/platform/web_font_render_style.h"
6 
7 #include "build/build_config.h"
8 #include "third_party/blink/renderer/platform/fonts/font_cache.h"
9 #include "third_party/blink/renderer/platform/fonts/font_description.h"
10 #include "third_party/blink/renderer/platform/web_test_support.h"
11 #include "third_party/skia/include/core/SkFont.h"
12 
13 namespace blink {
14 
15 namespace {
16 
17 SkFontHinting g_skia_hinting = SkFontHinting::kNormal;
18 bool g_use_skia_auto_hint = true;
19 bool g_use_skia_bitmaps = true;
20 bool g_use_skia_anti_alias = true;
21 bool g_use_skia_subpixel_rendering = false;
22 
23 }  // namespace
24 
25 // static
SetSkiaFontManager(sk_sp<SkFontMgr> font_mgr)26 void WebFontRenderStyle::SetSkiaFontManager(sk_sp<SkFontMgr> font_mgr) {
27   FontCache::SetFontManager(std::move(font_mgr));
28 }
29 
30 // static
SetHinting(SkFontHinting hinting)31 void WebFontRenderStyle::SetHinting(SkFontHinting hinting) {
32   g_skia_hinting = hinting;
33 }
34 
35 // static
SetAutoHint(bool use_auto_hint)36 void WebFontRenderStyle::SetAutoHint(bool use_auto_hint) {
37   g_use_skia_auto_hint = use_auto_hint;
38 }
39 
40 // static
SetUseBitmaps(bool use_bitmaps)41 void WebFontRenderStyle::SetUseBitmaps(bool use_bitmaps) {
42   g_use_skia_bitmaps = use_bitmaps;
43 }
44 
45 // static
SetAntiAlias(bool use_anti_alias)46 void WebFontRenderStyle::SetAntiAlias(bool use_anti_alias) {
47   g_use_skia_anti_alias = use_anti_alias;
48 }
49 
50 // static
SetSubpixelRendering(bool use_subpixel_rendering)51 void WebFontRenderStyle::SetSubpixelRendering(bool use_subpixel_rendering) {
52   g_use_skia_subpixel_rendering = use_subpixel_rendering;
53 }
54 
55 // static
SetSubpixelPositioning(bool use_subpixel_positioning)56 void WebFontRenderStyle::SetSubpixelPositioning(bool use_subpixel_positioning) {
57   FontDescription::SetSubpixelPositioning(use_subpixel_positioning);
58 }
59 
60 // static
SetSystemFontFamily(const WebString & name)61 void WebFontRenderStyle::SetSystemFontFamily(const WebString& name) {
62   FontCache::SetSystemFontFamily(name);
63 }
64 
65 // static
GetDefault()66 WebFontRenderStyle WebFontRenderStyle::GetDefault() {
67   WebFontRenderStyle result;
68   result.use_anti_alias = g_use_skia_anti_alias;
69   result.hint_style = static_cast<char>(g_skia_hinting);
70   result.use_bitmaps = g_use_skia_bitmaps;
71   result.use_auto_hint = g_use_skia_auto_hint;
72   result.use_anti_alias = g_use_skia_anti_alias;
73   result.use_subpixel_rendering = g_use_skia_subpixel_rendering;
74   result.use_subpixel_positioning = FontDescription::SubpixelPositioning();
75   return result;
76 }
77 
OverrideWith(const WebFontRenderStyle & other)78 void WebFontRenderStyle::OverrideWith(const WebFontRenderStyle& other) {
79   if (other.use_anti_alias != WebFontRenderStyle::kNoPreference)
80     use_anti_alias = other.use_anti_alias;
81 
82   if (other.use_hinting != WebFontRenderStyle::kNoPreference) {
83     use_hinting = other.use_hinting;
84     hint_style = other.hint_style;
85   }
86 
87   if (other.use_bitmaps != WebFontRenderStyle::kNoPreference)
88     use_bitmaps = other.use_bitmaps;
89   if (other.use_auto_hint != WebFontRenderStyle::kNoPreference)
90     use_auto_hint = other.use_auto_hint;
91   if (other.use_anti_alias != WebFontRenderStyle::kNoPreference)
92     use_anti_alias = other.use_anti_alias;
93   if (other.use_subpixel_rendering != WebFontRenderStyle::kNoPreference)
94     use_subpixel_rendering = other.use_subpixel_rendering;
95   if (other.use_subpixel_positioning != WebFontRenderStyle::kNoPreference)
96     use_subpixel_positioning = other.use_subpixel_positioning;
97 }
98 
ApplyToSkFont(SkFont * font,float device_scale_factor) const99 void WebFontRenderStyle::ApplyToSkFont(SkFont* font,
100                                        float device_scale_factor) const {
101   auto sk_hint_style = static_cast<SkFontHinting>(hint_style);
102   font->setHinting(sk_hint_style);
103   font->setEmbeddedBitmaps(use_bitmaps);
104   font->setForceAutoHinting(use_auto_hint);
105   if (use_anti_alias && use_subpixel_rendering) {
106     font->setEdging(SkFont::Edging::kSubpixelAntiAlias);
107   } else if (use_anti_alias) {
108     font->setEdging(SkFont::Edging::kAntiAlias);
109   } else {
110     font->setEdging(SkFont::Edging::kAlias);
111   }
112 
113   // Force-enable subpixel positioning, except when normal or full hinting is
114   // requested on low-dpi screen or when running web tests.
115   bool force_subpixel_positioning =
116       !WebTestSupport::IsRunningWebTest() &&
117       (sk_hint_style < SkFontHinting::kNormal || device_scale_factor > 1.0f);
118 
119   font->setSubpixel(force_subpixel_positioning || use_subpixel_positioning);
120 
121   font->setLinearMetrics(use_subpixel_positioning == 1);
122 }
123 
124 }  // namespace blink
125