OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/gfx/font_render_params.h" | 5 #include "ui/gfx/font_render_params.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "ui/gfx/switches.h" | 9 #include "ui/gfx/switches.h" |
10 | 10 |
11 #include <fontconfig/fontconfig.h> | 11 #include <fontconfig/fontconfig.h> |
12 | 12 |
13 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) | 13 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) |
14 #include "ui/gfx/linux_font_delegate.h" | 14 #include "ui/gfx/linux_font_delegate.h" |
15 #endif | 15 #endif |
16 | 16 |
17 namespace gfx { | 17 namespace gfx { |
18 | 18 |
19 namespace { | 19 namespace { |
20 | 20 |
21 bool SubpixelPositioningRequested(bool renderer) { | 21 bool SubpixelPositioningRequested(bool for_web_contents) { |
22 return CommandLine::ForCurrentProcess()->HasSwitch( | 22 return CommandLine::ForCurrentProcess()->HasSwitch( |
23 renderer ? switches::kEnableWebkitTextSubpixelPositioning | 23 for_web_contents ? switches::kEnableWebkitTextSubpixelPositioning |
24 : switches::kEnableBrowserTextSubpixelPositioning); | 24 : switches::kEnableBrowserTextSubpixelPositioning); |
25 } | 25 } |
26 | 26 |
27 // Initializes |params| with the system's default settings. |renderer| is true | 27 // Converts Fontconfig FC_HINT_STYLE to FontRenderParams::Hinting. |
28 // when setting WebKit renderer defaults. | 28 FontRenderParams::Hinting ConvertFontconfigHintStyle(int hint_style) { |
29 void LoadDefaults(FontRenderParams* params, bool renderer) { | 29 switch (hint_style) { |
30 // For non-GTK builds (read: Aura), just use reasonable hardcoded values. | 30 case FC_HINT_SLIGHT: return FontRenderParams::HINTING_SLIGHT; |
31 params->antialiasing = true; | 31 case FC_HINT_MEDIUM: return FontRenderParams::HINTING_MEDIUM; |
32 params->autohinter = true; | 32 case FC_HINT_FULL: return FontRenderParams::HINTING_FULL; |
33 params->use_bitmaps = true; | 33 default: return FontRenderParams::HINTING_NONE; |
34 params->hinting = FontRenderParams::HINTING_SLIGHT; | 34 } |
35 } | |
35 | 36 |
36 // Fetch default subpixel rendering settings from FontConfig. | 37 // Converts Fontconfig FC_RGBA to FontRenderParams::SubpixelRendering. |
38 FontRenderParams::SubpixelRendering ConvertFontconfigRgba(int rgba) { | |
39 switch (rgba) { | |
40 case FC_RGBA_RGB: return FontRenderParams::SUBPIXEL_RENDERING_RGB; | |
41 case FC_RGBA_BGR: return FontRenderParams::SUBPIXEL_RENDERING_BGR; | |
42 case FC_RGBA_VRGB: return FontRenderParams::SUBPIXEL_RENDERING_VRGB; | |
43 case FC_RGBA_VBGR: return FontRenderParams::SUBPIXEL_RENDERING_VBGR; | |
44 default: return FontRenderParams::SUBPIXEL_RENDERING_NONE; | |
45 } | |
46 } | |
47 | |
48 // Queries Fontconfig for rendering settings and updates |params_out| and | |
49 // |family_out| (if non-NULL). Returns false on failure. See | |
50 // GetCustomFontRenderParams() for descriptions of arguments. | |
51 bool QueryFontconfig(const std::vector<std::string>* family_list, | |
52 const int* pixel_size, | |
53 const int* point_size, | |
54 FontRenderParams* params_out, | |
55 std::string* family_out) { | |
37 FcPattern* pattern = FcPatternCreate(); | 56 FcPattern* pattern = FcPatternCreate(); |
57 CHECK(pattern); | |
58 | |
59 if (family_list) { | |
60 for (std::vector<std::string>::const_iterator it = family_list->begin(); | |
61 it != family_list->end(); ++it) { | |
62 FcPatternAddString( | |
63 pattern, FC_FAMILY, reinterpret_cast<const FcChar8*>(it->c_str())); | |
64 } | |
65 } | |
66 if (pixel_size) | |
67 FcPatternAddDouble(pattern, FC_PIXEL_SIZE, *pixel_size); | |
68 if (point_size) | |
69 FcPatternAddInteger(pattern, FC_SIZE, *point_size); | |
70 | |
38 FcConfigSubstitute(NULL, pattern, FcMatchPattern); | 71 FcConfigSubstitute(NULL, pattern, FcMatchPattern); |
39 FcDefaultSubstitute(pattern); | 72 FcDefaultSubstitute(pattern); |
40 FcResult result; | 73 FcResult result; |
41 FcPattern* match = FcFontMatch(0, pattern, &result); | 74 FcPattern* match = FcFontMatch(NULL, pattern, &result); |
42 DCHECK(match); | 75 if (!match) |
43 int fc_rgba = FC_RGBA_RGB; | 76 return false; |
44 FcPatternGetInteger(match, FC_RGBA, 0, &fc_rgba); | |
45 FcPatternDestroy(pattern); | |
46 FcPatternDestroy(match); | |
47 | 77 |
48 switch (fc_rgba) { | 78 if (family_out) { |
49 case FC_RGBA_RGB: | 79 FcChar8* family = NULL; |
50 params->subpixel_rendering = FontRenderParams::SUBPIXEL_RENDERING_RGB; | 80 if (FcPatternGetString(match, FC_FAMILY, 0, &family) == FcResultMatch) |
51 break; | 81 family_out->assign(reinterpret_cast<const char*>(family)); |
52 case FC_RGBA_BGR: | |
53 params->subpixel_rendering = FontRenderParams::SUBPIXEL_RENDERING_BGR; | |
54 break; | |
55 case FC_RGBA_VRGB: | |
56 params->subpixel_rendering = FontRenderParams::SUBPIXEL_RENDERING_VRGB; | |
57 break; | |
58 case FC_RGBA_VBGR: | |
59 params->subpixel_rendering = FontRenderParams::SUBPIXEL_RENDERING_VBGR; | |
60 break; | |
61 default: | |
62 params->subpixel_rendering = FontRenderParams::SUBPIXEL_RENDERING_NONE; | |
63 } | 82 } |
64 | 83 |
65 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) | 84 if (params_out) { |
66 // TODO(derat): Get the autohinter setting from FontConfig. Until then, | 85 FcBool fc_antialias = 0; |
67 // maintain backward compatibility with what we were doing previously. | 86 if (FcPatternGetBool(match, FC_ANTIALIAS, 0, &fc_antialias) == |
68 params->autohinter = renderer; | 87 FcResultMatch) |
69 const LinuxFontDelegate* delegate = LinuxFontDelegate::instance(); | 88 params_out->antialiasing = fc_antialias; |
70 if (delegate) { | 89 |
71 params->antialiasing = delegate->UseAntialiasing(); | 90 FcBool fc_autohint = 0; |
72 params->hinting = delegate->GetHintingStyle(); | 91 if (FcPatternGetBool(match, FC_AUTOHINT, 0, &fc_autohint) == FcResultMatch) |
73 params->subpixel_rendering = delegate->GetSubpixelRenderingStyle(); | 92 params_out->autohinter = fc_autohint; |
93 | |
94 FcBool fc_hinting = 0; | |
95 int fc_hint_style = FC_HINT_NONE; | |
96 if (FcPatternGetBool(match, FC_HINTING, 0, &fc_hinting) == FcResultMatch && | |
97 fc_hinting && | |
98 FcPatternGetInteger(match, FC_HINT_STYLE, 0, &fc_hint_style) == | |
99 FcResultMatch) { | |
100 params_out->hinting = ConvertFontconfigHintStyle(fc_hint_style); | |
msw
2014/07/12 17:57:11
optional nit: You could probably assume that FcPat
Daniel Erat
2014/07/14 14:59:16
thanks! i've confirmed that this is how fontconfig
| |
101 } else { | |
102 params_out->hinting = FontRenderParams::HINTING_NONE; | |
103 } | |
104 | |
105 int fc_rgba = FC_RGBA_UNKNOWN; | |
106 if (FcPatternGetInteger(match, FC_RGBA, 0, &fc_rgba) == FcResultMatch) { | |
107 params_out->subpixel_rendering = ConvertFontconfigRgba(fc_rgba); | |
108 } else { | |
109 params_out->subpixel_rendering = | |
110 FontRenderParams::SUBPIXEL_RENDERING_NONE; | |
111 } | |
74 } | 112 } |
75 #endif | |
76 | 113 |
77 params->subpixel_positioning = SubpixelPositioningRequested(renderer); | 114 return true; |
115 } | |
78 | 116 |
79 // To enable subpixel positioning, we need to disable hinting. | 117 // Initializes |params| with the system's default settings. |
80 if (params->subpixel_positioning) | 118 void LoadDefaults(FontRenderParams* params, bool for_web_contents) { |
81 params->hinting = FontRenderParams::HINTING_NONE; | 119 *params = GetCustomFontRenderParams(for_web_contents, NULL, NULL, NULL, NULL); |
82 } | 120 } |
83 | 121 |
84 } // namespace | 122 } // namespace |
85 | 123 |
86 const FontRenderParams& GetDefaultFontRenderParams() { | 124 const FontRenderParams& GetDefaultFontRenderParams() { |
87 static bool loaded_defaults = false; | 125 static bool loaded_defaults = false; |
88 static FontRenderParams default_params; | 126 static FontRenderParams default_params; |
89 if (!loaded_defaults) | 127 if (!loaded_defaults) |
90 LoadDefaults(&default_params, /* renderer */ false); | 128 LoadDefaults(&default_params, /* for_web_contents */ false); |
91 loaded_defaults = true; | 129 loaded_defaults = true; |
92 return default_params; | 130 return default_params; |
93 } | 131 } |
94 | 132 |
95 const FontRenderParams& GetDefaultWebKitFontRenderParams() { | 133 const FontRenderParams& GetDefaultWebKitFontRenderParams() { |
96 static bool loaded_defaults = false; | 134 static bool loaded_defaults = false; |
97 static FontRenderParams default_params; | 135 static FontRenderParams default_params; |
98 if (!loaded_defaults) | 136 if (!loaded_defaults) |
99 LoadDefaults(&default_params, /* renderer */ true); | 137 LoadDefaults(&default_params, /* for_web_contents */ true); |
100 loaded_defaults = true; | 138 loaded_defaults = true; |
101 return default_params; | 139 return default_params; |
102 } | 140 } |
103 | 141 |
104 bool GetDefaultWebkitSubpixelPositioning() { | 142 FontRenderParams GetCustomFontRenderParams( |
105 return SubpixelPositioningRequested(true); | 143 bool for_web_contents, |
144 const std::vector<std::string>* family_list, | |
145 const int* pixel_size, | |
146 const int* point_size, | |
147 std::string* family_out) { | |
148 FontRenderParams params; | |
149 if (family_out) | |
150 family_out->clear(); | |
151 | |
152 #if defined(OS_CHROMEOS) | |
153 // Use reasonable defaults. | |
154 params.antialiasing = true; | |
155 params.autohinter = true; | |
156 params.use_bitmaps = true; | |
157 params.hinting = FontRenderParams::HINTING_SLIGHT; | |
158 | |
159 // Query Fontconfig to get the family name and subpixel rendering setting. | |
160 // In general, we try to limit Chrome OS's dependency on Fontconfig, but it's | |
161 // used to configure fonts for different scripts and to disable subpixel | |
162 // rendering on systems that use external displays. | |
163 // TODO(derat): Decide if we should just use Fontconfig wholeheartedly on | |
164 // Chrome OS; Blink is using it, after all. | |
165 FontRenderParams fc_params; | |
166 QueryFontconfig(family_list, pixel_size, point_size, &fc_params, family_out); | |
167 params.subpixel_rendering = fc_params.subpixel_rendering; | |
168 #else | |
169 // Start with the delegate's settings, but let Fontconfig have the final say. | |
170 // TODO(derat): Figure out if we need to query the delegate at all. Does | |
171 // GtkSettings always get overridden by Fontconfig in GTK apps? | |
172 const LinuxFontDelegate* delegate = LinuxFontDelegate::instance(); | |
173 if (delegate) { | |
174 params.antialiasing = delegate->UseAntialiasing(); | |
175 params.hinting = delegate->GetHintingStyle(); | |
176 params.subpixel_rendering = delegate->GetSubpixelRenderingStyle(); | |
177 } | |
178 QueryFontconfig(family_list, pixel_size, point_size, ¶ms, family_out); | |
179 #endif | |
180 | |
181 params.subpixel_positioning = SubpixelPositioningRequested(for_web_contents); | |
182 | |
183 // To enable subpixel positioning, we need to disable hinting. | |
184 if (params.subpixel_positioning) | |
185 params.hinting = FontRenderParams::HINTING_NONE; | |
186 | |
187 // Use the first family from the list if Fontconfig didn't suggest a family. | |
188 if (family_out && family_out->empty() && | |
189 family_list && !family_list->empty()) | |
190 *family_out = (*family_list)[0]; | |
191 | |
192 return params; | |
106 } | 193 } |
107 | 194 |
108 } // namespace gfx | 195 } // namespace gfx |
OLD | NEW |