Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(141)

Side by Side Diff: ui/gfx/font_render_params_linux.cc

Issue 382273002: ui/gfx: Allow for font-specific rendering settings. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: diff against https://codereview.chromium.org/387743002/ Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 renderer) {
22 return CommandLine::ForCurrentProcess()->HasSwitch( 22 return CommandLine::ForCurrentProcess()->HasSwitch(
23 renderer ? switches::kEnableWebkitTextSubpixelPositioning 23 renderer ? switches::kEnableWebkitTextSubpixelPositioning
24 : switches::kEnableBrowserTextSubpixelPositioning); 24 : switches::kEnableBrowserTextSubpixelPositioning);
25 } 25 }
26 26
27 // Queries FontConfig for rendering settings and updates |params_out| and
28 // |family_out| (if non-NULL). Returns false on failure. See
29 // GetCustomFontRenderParams() for descriptions of arguments.
30 bool QueryFontConfig(
Daniel Erat 2014/07/11 17:15:29 this essentially just combines the code from LoadD
msw 2014/07/12 00:27:32 Acknowledged.
31 const std::vector<std::string>* family_list,
32 const int* pixel_size,
33 const int* point_size,
34 FontRenderParams* params_out,
35 std::string* family_out) {
36 FcPattern* pattern = FcPatternCreate();
37 CHECK(pattern);
38
39 if (family_list) {
40 for (std::vector<std::string>::const_iterator it = family_list->begin();
41 it != family_list->end(); ++it) {
42 FcValue value;
43 value.type = FcTypeString;
44 value.u.s = reinterpret_cast<const FcChar8*>(it->c_str());
45 FcPatternAdd(pattern, FC_FAMILY, value, FcTrue /* append */);
msw 2014/07/12 00:27:33 nit: FcPatternAddString might be simpler.
Daniel Erat 2014/07/12 01:55:32 nice! i'd missed these
46 }
47 }
48 if (pixel_size) {
49 FcValue value;
50 value.type = FcTypeDouble;
51 value.u.d = *pixel_size;
52 FcPatternAdd(pattern, FC_PIXEL_SIZE, value, FcTrue /* append */);
msw 2014/07/12 00:27:33 nit: FcPatternAddDouble might be simpler
Daniel Erat 2014/07/12 01:55:33 Done.
53 }
54 if (point_size) {
55 FcValue value;
56 value.type = FcTypeInteger;
57 value.u.i = *point_size;
58 FcPatternAdd(pattern, FC_SIZE, value, FcTrue /* append */);
msw 2014/07/12 00:27:32 nit: FcPatternAddInteger might be simpler.
Daniel Erat 2014/07/12 01:55:33 Done.
59 }
60
61 FcConfigSubstitute(NULL, pattern, FcMatchPattern);
62 FcDefaultSubstitute(pattern);
63 FcResult result;
64 FcPattern* match = FcFontMatch(0, pattern, &result);
msw 2014/07/12 00:27:32 nit: s/0/NULL/
Daniel Erat 2014/07/12 01:55:32 Done.
65 if (!match)
msw 2014/07/12 00:27:33 Should we DCHECK this or is it okay to not actuall
Daniel Erat 2014/07/12 01:55:32 the documentation is silent, of course, but the im
msw 2014/07/12 02:26:40 Acknowledged.
66 return false;
67
68 if (family_out) {
69 FcChar8* family = NULL;
70 if (FcPatternGetString(match, FC_FAMILY, 0, &family) == FcResultMatch)
71 family_out->assign(reinterpret_cast<const char*>(family));
72 }
73
74 if (params_out) {
75 FcBool fc_antialias = 0;
76 if (FcPatternGetBool(match, FC_ANTIALIAS, 0, &fc_antialias) ==
77 FcResultMatch)
78 params_out->antialiasing = fc_antialias;
79
80 FcBool fc_autohint = 0;
81 if (FcPatternGetBool(match, FC_AUTOHINT, 0, &fc_autohint) == FcResultMatch)
82 params_out->autohinter = fc_autohint;
83
84 FcBool fc_hinting = 0;
85 int fc_hint_style = FC_HINT_NONE;
86 if (FcPatternGetBool(match, FC_HINTING, 0, &fc_hinting) == FcResultMatch &&
87 FcPatternGetInteger(match, FC_HINT_STYLE, 0, &fc_hint_style) ==
msw 2014/07/12 00:27:32 Does it make sense to offer a default hinting sett
Daniel Erat 2014/07/12 01:55:33 (to make it possible for users to choose self-cont
msw 2014/07/12 02:26:40 Acknowledged.
88 FcResultMatch) {
89 params_out->hinting = FontRenderParams::HINTING_NONE;
90 if (fc_hinting) {
91 switch (fc_hint_style) {
msw 2014/07/12 00:27:33 optional nit: add a conversion helper.
Daniel Erat 2014/07/12 01:55:33 Done.
92 case FC_HINT_SLIGHT:
93 params_out->hinting = FontRenderParams::HINTING_SLIGHT;
94 break;
95 case FC_HINT_MEDIUM:
96 params_out->hinting = FontRenderParams::HINTING_MEDIUM;
97 break;
98 case FC_HINT_FULL:
99 params_out->hinting = FontRenderParams::HINTING_FULL;
100 break;
101 default:
102 // Leave hinting disabled otherwise.
103 break;
104 }
105 }
106 }
107
108 int fc_rgba = FC_RGBA_UNKNOWN;
109 if (FcPatternGetInteger(match, FC_RGBA, 0, &fc_rgba) == FcResultMatch) {
110 switch (fc_rgba) {
msw 2014/07/12 00:27:32 optional nit: add a conversion helper.
Daniel Erat 2014/07/12 01:55:32 Done.
111 case FC_RGBA_RGB:
112 params_out->subpixel_rendering =
113 FontRenderParams::SUBPIXEL_RENDERING_RGB;
114 break;
115 case FC_RGBA_BGR:
116 params_out->subpixel_rendering =
117 FontRenderParams::SUBPIXEL_RENDERING_BGR;
118 break;
119 case FC_RGBA_VRGB:
120 params_out->subpixel_rendering =
121 FontRenderParams::SUBPIXEL_RENDERING_VRGB;
122 break;
123 case FC_RGBA_VBGR:
124 params_out->subpixel_rendering =
125 FontRenderParams::SUBPIXEL_RENDERING_VBGR;
126 break;
127 default:
128 params_out->subpixel_rendering =
129 FontRenderParams::SUBPIXEL_RENDERING_NONE;
130 }
131 }
132 }
133
134 return true;
135 }
136
27 // Initializes |params| with the system's default settings. |renderer| is true 137 // Initializes |params| with the system's default settings. |renderer| is true
28 // when setting WebKit renderer defaults. 138 // when setting WebKit renderer defaults.
29 void LoadDefaults(FontRenderParams* params, bool renderer) { 139 void LoadDefaults(FontRenderParams* params, bool renderer) {
30 // For non-GTK builds (read: Aura), just use reasonable hardcoded values. 140 *params = GetCustomFontRenderParams(renderer, NULL, NULL, NULL, NULL);
31 params->antialiasing = true;
32 params->autohinter = true;
33 params->use_bitmaps = true;
34 params->hinting = FontRenderParams::HINTING_SLIGHT;
35
36 // Fetch default subpixel rendering settings from FontConfig.
37 FcPattern* pattern = FcPatternCreate();
38 FcConfigSubstitute(NULL, pattern, FcMatchPattern);
39 FcDefaultSubstitute(pattern);
40 FcResult result;
41 FcPattern* match = FcFontMatch(0, pattern, &result);
42 DCHECK(match);
43 int fc_rgba = FC_RGBA_RGB;
44 FcPatternGetInteger(match, FC_RGBA, 0, &fc_rgba);
45 FcPatternDestroy(pattern);
46 FcPatternDestroy(match);
47
48 switch (fc_rgba) {
49 case FC_RGBA_RGB:
50 params->subpixel_rendering = FontRenderParams::SUBPIXEL_RENDERING_RGB;
51 break;
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 }
64
65 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
66 // TODO(derat): Get the autohinter setting from FontConfig. Until then,
67 // maintain backward compatibility with what we were doing previously.
68 params->autohinter = renderer;
69 const LinuxFontDelegate* delegate = LinuxFontDelegate::instance();
70 if (delegate) {
71 params->antialiasing = delegate->UseAntialiasing();
72 params->hinting = delegate->GetHintingStyle();
73 params->subpixel_rendering = delegate->GetSubpixelRenderingStyle();
74 }
75 #endif
76
77 params->subpixel_positioning = SubpixelPositioningRequested(renderer);
78
79 // To enable subpixel positioning, we need to disable hinting.
80 if (params->subpixel_positioning)
81 params->hinting = FontRenderParams::HINTING_NONE;
82 } 141 }
83 142
84 } // namespace 143 } // namespace
85 144
86 const FontRenderParams& GetDefaultFontRenderParams() { 145 const FontRenderParams& GetDefaultFontRenderParams() {
87 static bool loaded_defaults = false; 146 static bool loaded_defaults = false;
88 static FontRenderParams default_params; 147 static FontRenderParams default_params;
89 if (!loaded_defaults) 148 if (!loaded_defaults)
90 LoadDefaults(&default_params, /* renderer */ false); 149 LoadDefaults(&default_params, /* renderer */ false);
91 loaded_defaults = true; 150 loaded_defaults = true;
92 return default_params; 151 return default_params;
93 } 152 }
94 153
95 const FontRenderParams& GetDefaultWebKitFontRenderParams() { 154 const FontRenderParams& GetDefaultWebKitFontRenderParams() {
96 static bool loaded_defaults = false; 155 static bool loaded_defaults = false;
97 static FontRenderParams default_params; 156 static FontRenderParams default_params;
98 if (!loaded_defaults) 157 if (!loaded_defaults)
99 LoadDefaults(&default_params, /* renderer */ true); 158 LoadDefaults(&default_params, /* renderer */ true);
100 loaded_defaults = true; 159 loaded_defaults = true;
101 return default_params; 160 return default_params;
102 } 161 }
103 162
163 FontRenderParams GetCustomFontRenderParams(
164 bool renderer,
165 const std::vector<std::string>* family_list,
166 const int* pixel_size,
167 const int* point_size,
168 std::string* family_out) {
169 FontRenderParams params;
170 if (family_out)
171 family_out->clear();
172
173 #if defined(OS_CHROMEOS)
174 // Use reasonable defaults.
175 params.antialiasing = true;
msw 2014/07/12 00:27:32 These reasonable defaults previously applied to De
Daniel Erat 2014/07/12 01:55:32 no, i don't think so. i just meant "reasonable for
msw 2014/07/12 02:26:40 Ah, ok. Should we remove these explicit assignment
Daniel Erat 2014/07/12 02:44:38 i'd rather continue to be explicit for chrome os,
176 params.autohinter = true;
177 params.use_bitmaps = true;
178 params.hinting = FontRenderParams::HINTING_SLIGHT;
179
180 // Query Fontconfig to get the family name and subpixel rendering setting.
msw 2014/07/12 00:27:33 Why don't we respect the other FontConfig settings
Daniel Erat 2014/07/12 01:55:32 i'd actually like for us to get away from using fo
msw 2014/07/12 02:26:40 Can you add a comment here? The reasons for limite
Daniel Erat 2014/07/12 02:44:38 done. also added a TODO, since i'm still not wholl
181 FontRenderParams fc_params;
182 QueryFontConfig(family_list, pixel_size, point_size, &fc_params, family_out);
183 params.subpixel_rendering = fc_params.subpixel_rendering;
184 #else
185 // Start with the delegate's settings, but let Fontconfig have the final say.
186 const LinuxFontDelegate* delegate = LinuxFontDelegate::instance();
msw 2014/07/12 00:27:33 Can we remove LinuxFontDelegate (or most if it) no
Daniel Erat 2014/07/12 01:55:32 i'll add a TODO about this since i honestly wasn't
msw 2014/07/12 02:26:40 Acknowledged.
187 if (delegate) {
188 params.antialiasing = delegate->UseAntialiasing();
189 params.hinting = delegate->GetHintingStyle();
190 params.subpixel_rendering = delegate->GetSubpixelRenderingStyle();
191 }
192 QueryFontConfig(family_list, pixel_size, point_size, &params, family_out);
193 #endif
194
195 params.subpixel_positioning = SubpixelPositioningRequested(renderer);
196
197 // To enable subpixel positioning, we need to disable hinting.
198 if (params.subpixel_positioning)
199 params.hinting = FontRenderParams::HINTING_NONE;
200
201 // Use the first family from the list if FontConfig didn't suggest a family.
202 if (family_out && family_out->empty() &&
203 family_list && !family_list->empty())
204 *family_out = (*family_list)[0];
Daniel Erat 2014/07/11 17:15:29 i didn't bother adding fallback code like this for
msw 2014/07/12 00:27:33 Acknowledged.
205
206 return params;
207 }
208
104 bool GetDefaultWebkitSubpixelPositioning() { 209 bool GetDefaultWebkitSubpixelPositioning() {
105 return SubpixelPositioningRequested(true); 210 return SubpixelPositioningRequested(true);
106 } 211 }
107 212
108 } // namespace gfx 213 } // namespace gfx
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698