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 a Fontconfig FC_HINT_STYLE value to a FontRenderParams::Hinting |
msw
2014/07/12 02:26:41
optional nit: omit "a" and "value" x2 for a one-li
Daniel Erat
2014/07/12 02:44:38
Done.
| |
28 // when setting WebKit renderer defaults. | 28 // value. |
29 void LoadDefaults(FontRenderParams* params, bool renderer) { | 29 FontRenderParams::Hinting FontconfigHintStyleToFontRenderParamsHinting( |
msw
2014/07/12 02:26:41
optional nit: consider a shorter name?
Daniel Erat
2014/07/12 02:44:38
i just changed these to "ConvertFontconfig____"; t
msw
2014/07/12 17:57:11
Nice.
| |
30 // For non-GTK builds (read: Aura), just use reasonable hardcoded values. | 30 int hint_style) { |
31 params->antialiasing = true; | 31 switch (hint_style) { |
msw
2014/07/12 02:26:41
This needs a return statement after the switch.
Daniel Erat
2014/07/12 02:44:38
do we use compilers that don't understand the defa
msw
2014/07/12 17:57:11
I don't make the clang rules, I just fix the clang
Daniel Erat
2014/07/14 14:59:15
cool (seems to build locally with clang)
| |
32 params->autohinter = true; | 32 case FC_HINT_SLIGHT: return FontRenderParams::HINTING_SLIGHT; |
33 params->use_bitmaps = true; | 33 case FC_HINT_MEDIUM: return FontRenderParams::HINTING_MEDIUM; |
34 params->hinting = FontRenderParams::HINTING_SLIGHT; | 34 case FC_HINT_FULL: return FontRenderParams::HINTING_FULL; |
35 default: return FontRenderParams::HINTING_NONE; | |
36 } | |
37 } | |
35 | 38 |
36 // Fetch default subpixel rendering settings from FontConfig. | 39 // Converts a Fontconfig FC_RGBA value to a FontRenderParams::SubpixelRendering |
msw
2014/07/12 02:26:41
optional nit: omit "a" and "value" x2 for a one-li
Daniel Erat
2014/07/12 02:44:38
Done.
| |
40 // value. | |
41 FontRenderParams::SubpixelRendering | |
42 FontconfigRgbaToFontRenderParamsSubpixelRendering(int rgba) { | |
msw
2014/07/12 02:26:41
optional nit: consider a shorter name?
Daniel Erat
2014/07/12 02:44:38
Done.
| |
43 switch (rgba) { | |
msw
2014/07/12 02:26:41
This needs a return statement after the switch.
Daniel Erat
2014/07/12 02:44:38
same here
| |
44 case FC_RGBA_RGB: return FontRenderParams::SUBPIXEL_RENDERING_RGB; | |
45 case FC_RGBA_BGR: return FontRenderParams::SUBPIXEL_RENDERING_BGR; | |
46 case FC_RGBA_VRGB: return FontRenderParams::SUBPIXEL_RENDERING_VRGB; | |
47 case FC_RGBA_VBGR: return FontRenderParams::SUBPIXEL_RENDERING_VBGR; | |
48 default: return FontRenderParams::SUBPIXEL_RENDERING_NONE; | |
49 } | |
50 } | |
51 | |
52 // Queries Fontconfig for rendering settings and updates |params_out| and | |
53 // |family_out| (if non-NULL). Returns false on failure. See | |
54 // GetCustomFontRenderParams() for descriptions of arguments. | |
55 bool QueryFontconfig(const std::vector<std::string>* family_list, | |
56 const int* pixel_size, | |
57 const int* point_size | |
msw
2014/07/12 02:26:41
nit: missing comma
Daniel Erat
2014/07/12 02:44:38
ugh, broke this in post-compile reindenting
| |
58 FontRenderParams* params_out, | |
59 std::string* family_out) { | |
37 FcPattern* pattern = FcPatternCreate(); | 60 FcPattern* pattern = FcPatternCreate(); |
61 CHECK(pattern); | |
62 | |
63 if (family_list) { | |
64 for (std::vector<std::string>::const_iterator it = family_list->begin(); | |
65 it != family_list->end(); ++it) { | |
66 FcPatternAddString( | |
67 pattern, FC_FAMILY, reinterpret_cast<const FcChar8*>(it->c_str())); | |
68 } | |
69 } | |
70 if (pixel_size) | |
71 FcPatternAddDouble(pattern, FC_PIXEL_SIZE, *pixel_size); | |
72 if (point_size) | |
73 FcPatternAddInteger(pattern, FC_SIZE, *point_size); | |
74 | |
38 FcConfigSubstitute(NULL, pattern, FcMatchPattern); | 75 FcConfigSubstitute(NULL, pattern, FcMatchPattern); |
39 FcDefaultSubstitute(pattern); | 76 FcDefaultSubstitute(pattern); |
40 FcResult result; | 77 FcResult result; |
41 FcPattern* match = FcFontMatch(0, pattern, &result); | 78 FcPattern* match = FcFontMatch(NULL, pattern, &result); |
42 DCHECK(match); | 79 if (!match) |
43 int fc_rgba = FC_RGBA_RGB; | 80 return false; |
44 FcPatternGetInteger(match, FC_RGBA, 0, &fc_rgba); | |
45 FcPatternDestroy(pattern); | |
46 FcPatternDestroy(match); | |
47 | 81 |
48 switch (fc_rgba) { | 82 if (family_out) { |
49 case FC_RGBA_RGB: | 83 FcChar8* family = NULL; |
50 params->subpixel_rendering = FontRenderParams::SUBPIXEL_RENDERING_RGB; | 84 if (FcPatternGetString(match, FC_FAMILY, 0, &family) == FcResultMatch) |
51 break; | 85 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 } | 86 } |
64 | 87 |
65 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) | 88 if (params_out) { |
66 // TODO(derat): Get the autohinter setting from FontConfig. Until then, | 89 FcBool fc_antialias = 0; |
67 // maintain backward compatibility with what we were doing previously. | 90 if (FcPatternGetBool(match, FC_ANTIALIAS, 0, &fc_antialias) == |
68 params->autohinter = renderer; | 91 FcResultMatch) |
69 const LinuxFontDelegate* delegate = LinuxFontDelegate::instance(); | 92 params_out->antialiasing = fc_antialias; |
70 if (delegate) { | 93 |
71 params->antialiasing = delegate->UseAntialiasing(); | 94 FcBool fc_autohint = 0; |
72 params->hinting = delegate->GetHintingStyle(); | 95 if (FcPatternGetBool(match, FC_AUTOHINT, 0, &fc_autohint) == FcResultMatch) |
73 params->subpixel_rendering = delegate->GetSubpixelRenderingStyle(); | 96 params_out->autohinter = fc_autohint; |
97 | |
98 FcBool fc_hinting = 0; | |
99 int fc_hint_style = FC_HINT_NONE; | |
100 if (FcPatternGetBool(match, FC_HINTING, 0, &fc_hinting) == FcResultMatch && | |
101 fc_hinting && | |
102 FcPatternGetInteger(match, FC_HINT_STYLE, 0, &fc_hint_style) == | |
103 FcResultMatch) { | |
104 params_out->hinting = FontconfigHintStyleToFontRenderParamsHinting( | |
105 fc_hint_style); | |
106 } else { | |
107 params_out->hinting = FontRenderParams::HINTING_NONE; | |
108 } | |
109 | |
110 int fc_rgba = FC_RGBA_UNKNOWN; | |
111 if (FcPatternGetInteger(match, FC_RGBA, 0, &fc_rgba) == FcResultMatch) { | |
112 params_out->subpixel_rendering = | |
113 FontconfigRgbaToFontRenderParamsSubpixelRendering(fc_rgba); | |
114 } else { | |
115 params_out->subpixel_rendering = | |
116 FontRenderParams::SUBPIXEL_RENDERING_NONE; | |
117 } | |
74 } | 118 } |
75 #endif | |
76 | 119 |
77 params->subpixel_positioning = SubpixelPositioningRequested(renderer); | 120 return true; |
121 } | |
78 | 122 |
79 // To enable subpixel positioning, we need to disable hinting. | 123 // Initializes |params| with the system's default settings. |
80 if (params->subpixel_positioning) | 124 void LoadDefaults(FontRenderParams* params, bool for_web_contents) { |
81 params->hinting = FontRenderParams::HINTING_NONE; | 125 *params = GetCustomFontRenderParams(for_web_contents, NULL, NULL, NULL, NULL); |
82 } | 126 } |
83 | 127 |
84 } // namespace | 128 } // namespace |
85 | 129 |
86 const FontRenderParams& GetDefaultFontRenderParams() { | 130 const FontRenderParams& GetDefaultFontRenderParams() { |
87 static bool loaded_defaults = false; | 131 static bool loaded_defaults = false; |
88 static FontRenderParams default_params; | 132 static FontRenderParams default_params; |
89 if (!loaded_defaults) | 133 if (!loaded_defaults) |
90 LoadDefaults(&default_params, /* renderer */ false); | 134 LoadDefaults(&default_params, /* for_web_contents */ false); |
91 loaded_defaults = true; | 135 loaded_defaults = true; |
92 return default_params; | 136 return default_params; |
93 } | 137 } |
94 | 138 |
95 const FontRenderParams& GetDefaultWebKitFontRenderParams() { | 139 const FontRenderParams& GetDefaultWebKitFontRenderParams() { |
96 static bool loaded_defaults = false; | 140 static bool loaded_defaults = false; |
97 static FontRenderParams default_params; | 141 static FontRenderParams default_params; |
98 if (!loaded_defaults) | 142 if (!loaded_defaults) |
99 LoadDefaults(&default_params, /* renderer */ true); | 143 LoadDefaults(&default_params, /* for_web_contents */ true); |
100 loaded_defaults = true; | 144 loaded_defaults = true; |
101 return default_params; | 145 return default_params; |
102 } | 146 } |
103 | 147 |
104 bool GetDefaultWebkitSubpixelPositioning() { | 148 FontRenderParams GetCustomFontRenderParams( |
105 return SubpixelPositioningRequested(true); | 149 bool for_web_contents, |
150 const std::vector<std::string>* family_list, | |
151 const int* pixel_size, | |
152 const int* point_size, | |
153 std::string* family_out) { | |
154 FontRenderParams params; | |
155 if (family_out) | |
156 family_out->clear(); | |
157 | |
158 #if defined(OS_CHROMEOS) | |
159 // Use reasonable defaults. | |
160 params.antialiasing = true; | |
161 params.autohinter = true; | |
162 params.use_bitmaps = true; | |
163 params.hinting = FontRenderParams::HINTING_SLIGHT; | |
164 | |
165 // Query Fontconfig to get the family name and subpixel rendering setting. | |
166 FontRenderParams fc_params; | |
167 QueryFontconfig(family_list, pixel_size, point_size, &fc_params, family_out); | |
168 params.subpixel_rendering = fc_params.subpixel_rendering; | |
169 #else | |
170 // Start with the delegate's settings, but let Fontconfig have the final say. | |
171 // TODO(derat): Figure out if we need to query the delegate at all. Does | |
172 // GtkSettings always get overridden by Fontconfig in GTK apps? | |
173 const LinuxFontDelegate* delegate = LinuxFontDelegate::instance(); | |
174 if (delegate) { | |
175 params.antialiasing = delegate->UseAntialiasing(); | |
176 params.hinting = delegate->GetHintingStyle(); | |
177 params.subpixel_rendering = delegate->GetSubpixelRenderingStyle(); | |
178 } | |
179 QueryFontconfig(family_list, pixel_size, point_size, ¶ms, family_out); | |
180 #endif | |
181 | |
182 params.subpixel_positioning = SubpixelPositioningRequested(for_web_contents); | |
183 | |
184 // To enable subpixel positioning, we need to disable hinting. | |
185 if (params.subpixel_positioning) | |
186 params.hinting = FontRenderParams::HINTING_NONE; | |
187 | |
188 // Use the first family from the list if Fontconfig didn't suggest a family. | |
189 if (family_out && family_out->empty() && | |
190 family_list && !family_list->empty()) | |
191 *family_out = (*family_list)[0]; | |
192 | |
193 return params; | |
106 } | 194 } |
107 | 195 |
108 } // namespace gfx | 196 } // namespace gfx |
OLD | NEW |