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_linux.h"
7 #include "base/command_line.h"
8 #include "base/logging.h"
9 #include "ui/gfx/switches.h"
11 #if defined(TOOLKIT_GTK)
14 #include <fontconfig/fontconfig.h>
17 #if defined(OS_LINUX) && defined(USE_AURA) && !defined(OS_CHROMEOS)
18 #include "ui/gfx/linux_font_delegate.h"
25 bool SubpixelPositioningRequested(bool renderer
) {
26 return CommandLine::ForCurrentProcess()->HasSwitch(
28 switches::kEnableWebkitTextSubpixelPositioning
:
29 switches::kEnableBrowserTextSubpixelPositioning
);
32 // Initializes |params| with the system's default settings. |renderer| is true
33 // when setting WebKit renderer defaults.
34 void LoadDefaults(FontRenderParams
* params
, bool renderer
) {
35 #if defined(TOOLKIT_GTK)
36 params
->antialiasing
= true;
37 // TODO(wangxianzhu): autohinter is now true to keep original behavior
38 // of WebKit, but it might not be the best value.
39 params
->autohinter
= true;
40 params
->use_bitmaps
= true;
41 params
->hinting
= FontRenderParams::HINTING_SLIGHT
;
42 params
->subpixel_rendering
= FontRenderParams::SUBPIXEL_RENDERING_NONE
;
44 GtkSettings
* gtk_settings
= gtk_settings_get_default();
46 gint gtk_antialias
= 0;
48 gchar
* gtk_hint_style
= NULL
;
49 gchar
* gtk_rgba
= NULL
;
50 g_object_get(gtk_settings
,
51 "gtk-xft-antialias", >k_antialias
,
52 "gtk-xft-hinting", >k_hinting
,
53 "gtk-xft-hintstyle", >k_hint_style
,
54 "gtk-xft-rgba", >k_rgba
,
57 // g_object_get() doesn't tell us whether the properties were present or not,
58 // but if they aren't (because gnome-settings-daemon isn't running), we'll get
59 // NULL values for the strings.
60 if (gtk_hint_style
&& gtk_rgba
) {
61 params
->antialiasing
= gtk_antialias
;
63 if (gtk_hinting
== 0 || strcmp(gtk_hint_style
, "hintnone") == 0)
64 params
->hinting
= FontRenderParams::HINTING_NONE
;
65 else if (strcmp(gtk_hint_style
, "hintslight") == 0)
66 params
->hinting
= FontRenderParams::HINTING_SLIGHT
;
67 else if (strcmp(gtk_hint_style
, "hintmedium") == 0)
68 params
->hinting
= FontRenderParams::HINTING_MEDIUM
;
69 else if (strcmp(gtk_hint_style
, "hintfull") == 0)
70 params
->hinting
= FontRenderParams::HINTING_FULL
;
72 if (strcmp(gtk_rgba
, "none") == 0)
73 params
->subpixel_rendering
= FontRenderParams::SUBPIXEL_RENDERING_NONE
;
74 else if (strcmp(gtk_rgba
, "rgb") == 0)
75 params
->subpixel_rendering
= FontRenderParams::SUBPIXEL_RENDERING_RGB
;
76 else if (strcmp(gtk_rgba
, "bgr") == 0)
77 params
->subpixel_rendering
= FontRenderParams::SUBPIXEL_RENDERING_BGR
;
78 else if (strcmp(gtk_rgba
, "vrgb") == 0)
79 params
->subpixel_rendering
= FontRenderParams::SUBPIXEL_RENDERING_VRGB
;
80 else if (strcmp(gtk_rgba
, "vbgr") == 0)
81 params
->subpixel_rendering
= FontRenderParams::SUBPIXEL_RENDERING_VBGR
;
84 g_free(gtk_hint_style
);
87 // For non-GTK builds (read: Aura), just use reasonable hardcoded values.
88 params
->antialiasing
= true;
89 params
->autohinter
= true;
90 params
->use_bitmaps
= true;
91 params
->hinting
= FontRenderParams::HINTING_SLIGHT
;
93 // Fetch default subpixel rendering settings from FontConfig.
94 FcPattern
* pattern
= FcPatternCreate();
95 FcConfigSubstitute(NULL
, pattern
, FcMatchPattern
);
96 FcDefaultSubstitute(pattern
);
98 FcPattern
* match
= FcFontMatch(0, pattern
, &result
);
100 int fc_rgba
= FC_RGBA_RGB
;
101 FcPatternGetInteger(match
, FC_RGBA
, 0, &fc_rgba
);
102 FcPatternDestroy(pattern
);
103 FcPatternDestroy(match
);
107 params
->subpixel_rendering
= FontRenderParams::SUBPIXEL_RENDERING_RGB
;
110 params
->subpixel_rendering
= FontRenderParams::SUBPIXEL_RENDERING_BGR
;
113 params
->subpixel_rendering
= FontRenderParams::SUBPIXEL_RENDERING_VRGB
;
116 params
->subpixel_rendering
= FontRenderParams::SUBPIXEL_RENDERING_VBGR
;
119 params
->subpixel_rendering
= FontRenderParams::SUBPIXEL_RENDERING_NONE
;
122 #if defined(OS_LINUX) && defined(USE_AURA) && !defined(OS_CHROMEOS)
123 const LinuxFontDelegate
* delegate
= LinuxFontDelegate::instance();
125 params
->antialiasing
= delegate
->UseAntialiasing();
126 params
->hinting
= delegate
->GetHintingStyle();
127 params
->subpixel_rendering
= delegate
->GetSubpixelRenderingStyle();
132 params
->subpixel_positioning
= SubpixelPositioningRequested(renderer
);
134 // To enable subpixel positioning, we need to disable hinting.
135 if (params
->subpixel_positioning
)
136 params
->hinting
= FontRenderParams::HINTING_NONE
;
141 const FontRenderParams
& GetDefaultFontRenderParams() {
142 static bool loaded_defaults
= false;
143 static FontRenderParams default_params
;
144 if (!loaded_defaults
)
145 LoadDefaults(&default_params
, /* renderer */ false);
146 loaded_defaults
= true;
147 return default_params
;
150 const FontRenderParams
& GetDefaultWebKitFontRenderParams() {
151 static bool loaded_defaults
= false;
152 static FontRenderParams default_params
;
153 if (!loaded_defaults
)
154 LoadDefaults(&default_params
, /* renderer */ true);
155 loaded_defaults
= true;
156 return default_params
;
159 bool GetDefaultWebkitSubpixelPositioning() {
160 return SubpixelPositioningRequested(true);