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

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: fix fontconfig leak 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
« no previous file with comments | « ui/gfx/font_render_params_android.cc ('k') | ui/gfx/pango_util.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 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);
43 int fc_rgba = FC_RGBA_RGB;
44 FcPatternGetInteger(match, FC_RGBA, 0, &fc_rgba);
45 FcPatternDestroy(pattern); 75 FcPatternDestroy(pattern);
46 FcPatternDestroy(match); 76 if (!match)
77 return false;
47 78
48 switch (fc_rgba) { 79 if (family_out) {
49 case FC_RGBA_RGB: 80 FcChar8* family = NULL;
50 params->subpixel_rendering = FontRenderParams::SUBPIXEL_RENDERING_RGB; 81 FcPatternGetString(match, FC_FAMILY, 0, &family);
51 break; 82 if (family)
52 case FC_RGBA_BGR: 83 family_out->assign(reinterpret_cast<const char*>(family));
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 } 84 }
64 85
65 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) 86 if (params_out) {
66 // TODO(derat): Get the autohinter setting from FontConfig. Until then, 87 FcBool fc_antialias = 0;
67 // maintain backward compatibility with what we were doing previously. 88 FcPatternGetBool(match, FC_ANTIALIAS, 0, &fc_antialias);
68 params->autohinter = renderer; 89 params_out->antialiasing = fc_antialias;
69 const LinuxFontDelegate* delegate = LinuxFontDelegate::instance(); 90
70 if (delegate) { 91 FcBool fc_autohint = 0;
71 params->antialiasing = delegate->UseAntialiasing(); 92 FcPatternGetBool(match, FC_AUTOHINT, 0, &fc_autohint);
72 params->hinting = delegate->GetHintingStyle(); 93 params_out->autohinter = fc_autohint;
73 params->subpixel_rendering = delegate->GetSubpixelRenderingStyle(); 94
95 FcBool fc_hinting = 0;
96 int fc_hint_style = FC_HINT_NONE;
97 FcPatternGetBool(match, FC_HINTING, 0, &fc_hinting);
98 if (fc_hinting)
99 FcPatternGetInteger(match, FC_HINT_STYLE, 0, &fc_hint_style);
100 params_out->hinting = ConvertFontconfigHintStyle(fc_hint_style);
101
102 int fc_rgba = FC_RGBA_NONE;
103 FcPatternGetInteger(match, FC_RGBA, 0, &fc_rgba);
104 params_out->subpixel_rendering = ConvertFontconfigRgba(fc_rgba);
74 } 105 }
75 #endif
76 106
77 params->subpixel_positioning = SubpixelPositioningRequested(renderer); 107 FcPatternDestroy(match);
108 return true;
109 }
78 110
79 // To enable subpixel positioning, we need to disable hinting. 111 // Initializes |params| with the system's default settings.
80 if (params->subpixel_positioning) 112 void LoadDefaults(FontRenderParams* params, bool for_web_contents) {
81 params->hinting = FontRenderParams::HINTING_NONE; 113 *params = GetCustomFontRenderParams(for_web_contents, NULL, NULL, NULL, NULL);
82 } 114 }
83 115
84 } // namespace 116 } // namespace
85 117
86 const FontRenderParams& GetDefaultFontRenderParams() { 118 const FontRenderParams& GetDefaultFontRenderParams() {
87 static bool loaded_defaults = false; 119 static bool loaded_defaults = false;
88 static FontRenderParams default_params; 120 static FontRenderParams default_params;
89 if (!loaded_defaults) 121 if (!loaded_defaults)
90 LoadDefaults(&default_params, /* renderer */ false); 122 LoadDefaults(&default_params, /* for_web_contents */ false);
91 loaded_defaults = true; 123 loaded_defaults = true;
92 return default_params; 124 return default_params;
93 } 125 }
94 126
95 const FontRenderParams& GetDefaultWebKitFontRenderParams() { 127 const FontRenderParams& GetDefaultWebKitFontRenderParams() {
96 static bool loaded_defaults = false; 128 static bool loaded_defaults = false;
97 static FontRenderParams default_params; 129 static FontRenderParams default_params;
98 if (!loaded_defaults) 130 if (!loaded_defaults)
99 LoadDefaults(&default_params, /* renderer */ true); 131 LoadDefaults(&default_params, /* for_web_contents */ true);
100 loaded_defaults = true; 132 loaded_defaults = true;
101 return default_params; 133 return default_params;
102 } 134 }
103 135
104 bool GetDefaultWebkitSubpixelPositioning() { 136 FontRenderParams GetCustomFontRenderParams(
105 return SubpixelPositioningRequested(true); 137 bool for_web_contents,
138 const std::vector<std::string>* family_list,
139 const int* pixel_size,
140 const int* point_size,
141 std::string* family_out) {
142 FontRenderParams params;
143 if (family_out)
144 family_out->clear();
145
146 #if defined(OS_CHROMEOS)
147 // Use reasonable defaults.
148 params.antialiasing = true;
149 params.autohinter = true;
150 params.use_bitmaps = true;
151 params.hinting = FontRenderParams::HINTING_SLIGHT;
152
153 // Query Fontconfig to get the family name and subpixel rendering setting.
154 // In general, we try to limit Chrome OS's dependency on Fontconfig, but it's
155 // used to configure fonts for different scripts and to disable subpixel
156 // rendering on systems that use external displays.
157 // TODO(derat): Decide if we should just use Fontconfig wholeheartedly on
158 // Chrome OS; Blink is using it, after all.
159 FontRenderParams fc_params;
160 QueryFontconfig(family_list, pixel_size, point_size, &fc_params, family_out);
161 params.subpixel_rendering = fc_params.subpixel_rendering;
162 #else
163 // Start with the delegate's settings, but let Fontconfig have the final say.
164 // TODO(derat): Figure out if we need to query the delegate at all. Does
165 // GtkSettings always get overridden by Fontconfig in GTK apps?
166 const LinuxFontDelegate* delegate = LinuxFontDelegate::instance();
167 if (delegate) {
168 params.antialiasing = delegate->UseAntialiasing();
169 params.hinting = delegate->GetHintingStyle();
170 params.subpixel_rendering = delegate->GetSubpixelRenderingStyle();
171 }
172 QueryFontconfig(family_list, pixel_size, point_size, &params, family_out);
173 #endif
174
175 params.subpixel_positioning = SubpixelPositioningRequested(for_web_contents);
176
177 // To enable subpixel positioning, we need to disable hinting.
178 if (params.subpixel_positioning)
179 params.hinting = FontRenderParams::HINTING_NONE;
180
181 // Use the first family from the list if Fontconfig didn't suggest a family.
182 if (family_out && family_out->empty() &&
183 family_list && !family_list->empty())
184 *family_out = (*family_list)[0];
185
186 return params;
106 } 187 }
107 188
108 } // namespace gfx 189 } // namespace gfx
OLDNEW
« no previous file with comments | « ui/gfx/font_render_params_android.cc ('k') | ui/gfx/pango_util.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698