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
3 // found in the LICENSE file.
5 #include "ui/gfx/font_render_params.h"
7 #include "base/command_line.h"
8 #include "base/logging.h"
9 #include "ui/gfx/switches.h"
11 #include <fontconfig/fontconfig.h>
13 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
14 #include "ui/gfx/linux_font_delegate.h"
21 bool SubpixelPositioningRequested(bool for_web_contents
) {
22 return CommandLine::ForCurrentProcess()->HasSwitch(
23 for_web_contents
? switches::kEnableWebkitTextSubpixelPositioning
24 : switches::kEnableBrowserTextSubpixelPositioning
);
27 // Converts Fontconfig FC_HINT_STYLE to FontRenderParams::Hinting.
28 FontRenderParams::Hinting
ConvertFontconfigHintStyle(int hint_style
) {
30 case FC_HINT_SLIGHT
: return FontRenderParams::HINTING_SLIGHT
;
31 case FC_HINT_MEDIUM
: return FontRenderParams::HINTING_MEDIUM
;
32 case FC_HINT_FULL
: return FontRenderParams::HINTING_FULL
;
33 default: return FontRenderParams::HINTING_NONE
;
37 // Converts Fontconfig FC_RGBA to FontRenderParams::SubpixelRendering.
38 FontRenderParams::SubpixelRendering
ConvertFontconfigRgba(int 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
;
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
) {
56 FcPattern
* pattern
= FcPatternCreate();
60 for (std::vector
<std::string
>::const_iterator it
= family_list
->begin();
61 it
!= family_list
->end(); ++it
) {
63 pattern
, FC_FAMILY
, reinterpret_cast<const FcChar8
*>(it
->c_str()));
67 FcPatternAddDouble(pattern
, FC_PIXEL_SIZE
, *pixel_size
);
69 FcPatternAddInteger(pattern
, FC_SIZE
, *point_size
);
71 FcConfigSubstitute(NULL
, pattern
, FcMatchPattern
);
72 FcDefaultSubstitute(pattern
);
74 FcPattern
* match
= FcFontMatch(NULL
, pattern
, &result
);
75 FcPatternDestroy(pattern
);
80 FcChar8
* family
= NULL
;
81 FcPatternGetString(match
, FC_FAMILY
, 0, &family
);
83 family_out
->assign(reinterpret_cast<const char*>(family
));
87 FcBool fc_antialias
= 0;
88 FcPatternGetBool(match
, FC_ANTIALIAS
, 0, &fc_antialias
);
89 params_out
->antialiasing
= fc_antialias
;
91 FcBool fc_autohint
= 0;
92 FcPatternGetBool(match
, FC_AUTOHINT
, 0, &fc_autohint
);
93 params_out
->autohinter
= fc_autohint
;
95 FcBool fc_hinting
= 0;
96 int fc_hint_style
= FC_HINT_NONE
;
97 FcPatternGetBool(match
, FC_HINTING
, 0, &fc_hinting
);
99 FcPatternGetInteger(match
, FC_HINT_STYLE
, 0, &fc_hint_style
);
100 params_out
->hinting
= ConvertFontconfigHintStyle(fc_hint_style
);
102 int fc_rgba
= FC_RGBA_NONE
;
103 FcPatternGetInteger(match
, FC_RGBA
, 0, &fc_rgba
);
104 params_out
->subpixel_rendering
= ConvertFontconfigRgba(fc_rgba
);
107 FcPatternDestroy(match
);
111 // Initializes |params| with the system's default settings.
112 void LoadDefaults(FontRenderParams
* params
, bool for_web_contents
) {
113 *params
= GetCustomFontRenderParams(for_web_contents
, NULL
, NULL
, NULL
, NULL
);
118 const FontRenderParams
& GetDefaultFontRenderParams() {
119 static bool loaded_defaults
= false;
120 static FontRenderParams default_params
;
121 if (!loaded_defaults
)
122 LoadDefaults(&default_params
, /* for_web_contents */ false);
123 loaded_defaults
= true;
124 return default_params
;
127 const FontRenderParams
& GetDefaultWebKitFontRenderParams() {
128 static bool loaded_defaults
= false;
129 static FontRenderParams default_params
;
130 if (!loaded_defaults
)
131 LoadDefaults(&default_params
, /* for_web_contents */ true);
132 loaded_defaults
= true;
133 return default_params
;
136 FontRenderParams
GetCustomFontRenderParams(
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
;
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
;
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
;
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();
168 params
.antialiasing
= delegate
->UseAntialiasing();
169 params
.hinting
= delegate
->GetHintingStyle();
170 params
.subpixel_rendering
= delegate
->GetSubpixelRenderingStyle();
172 QueryFontconfig(family_list
, pixel_size
, point_size
, ¶ms
, family_out
);
175 params
.subpixel_positioning
= SubpixelPositioningRequested(for_web_contents
);
177 // To enable subpixel positioning, we need to disable hinting.
178 if (params
.subpixel_positioning
)
179 params
.hinting
= FontRenderParams::HINTING_NONE
;
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];