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/logging.h"
9 #if defined(TOOLKIT_GTK)
12 #include <fontconfig/fontconfig.h>
13 #include "base/command_line.h"
14 #include "ui/gfx/switches.h"
21 // Initializes |params| with the system's default settings. |renderer| is true
22 // when setting WebKit renderer defaults.
23 void LoadDefaults(FontRenderParams
* params
, bool renderer
) {
24 #if defined(TOOLKIT_GTK)
25 params
->antialiasing
= true;
26 params
->subpixel_positioning
= false;
27 // TODO(wangxianzhu): autohinter is now true to keep original behavior
28 // of WebKit, but it might not be the best value.
29 params
->autohinter
= true;
30 params
->use_bitmaps
= true;
31 params
->hinting
= FontRenderParams::HINTING_SLIGHT
;
32 params
->subpixel_rendering
= FontRenderParams::SUBPIXEL_RENDERING_NONE
;
34 GtkSettings
* gtk_settings
= gtk_settings_get_default();
36 gint gtk_antialias
= 0;
38 gchar
* gtk_hint_style
= NULL
;
39 gchar
* gtk_rgba
= NULL
;
40 g_object_get(gtk_settings
,
41 "gtk-xft-antialias", >k_antialias
,
42 "gtk-xft-hinting", >k_hinting
,
43 "gtk-xft-hintstyle", >k_hint_style
,
44 "gtk-xft-rgba", >k_rgba
,
47 // g_object_get() doesn't tell us whether the properties were present or not,
48 // but if they aren't (because gnome-settings-daemon isn't running), we'll get
49 // NULL values for the strings.
50 if (gtk_hint_style
&& gtk_rgba
) {
51 params
->antialiasing
= gtk_antialias
;
53 if (gtk_hinting
== 0 || strcmp(gtk_hint_style
, "hintnone") == 0)
54 params
->hinting
= FontRenderParams::HINTING_NONE
;
55 else if (strcmp(gtk_hint_style
, "hintslight") == 0)
56 params
->hinting
= FontRenderParams::HINTING_SLIGHT
;
57 else if (strcmp(gtk_hint_style
, "hintmedium") == 0)
58 params
->hinting
= FontRenderParams::HINTING_MEDIUM
;
59 else if (strcmp(gtk_hint_style
, "hintfull") == 0)
60 params
->hinting
= FontRenderParams::HINTING_FULL
;
62 if (strcmp(gtk_rgba
, "none") == 0)
63 params
->subpixel_rendering
= FontRenderParams::SUBPIXEL_RENDERING_NONE
;
64 else if (strcmp(gtk_rgba
, "rgb") == 0)
65 params
->subpixel_rendering
= FontRenderParams::SUBPIXEL_RENDERING_RGB
;
66 else if (strcmp(gtk_rgba
, "bgr") == 0)
67 params
->subpixel_rendering
= FontRenderParams::SUBPIXEL_RENDERING_BGR
;
68 else if (strcmp(gtk_rgba
, "vrgb") == 0)
69 params
->subpixel_rendering
= FontRenderParams::SUBPIXEL_RENDERING_VRGB
;
70 else if (strcmp(gtk_rgba
, "vbgr") == 0)
71 params
->subpixel_rendering
= FontRenderParams::SUBPIXEL_RENDERING_VBGR
;
74 g_free(gtk_hint_style
);
77 // For non-GTK builds (read: Aura), just use reasonable hardcoded values.
78 params
->antialiasing
= true;
79 params
->autohinter
= true;
80 params
->use_bitmaps
= true;
82 // Fetch default subpixel rendering settings from FontConfig.
83 FcPattern
* pattern
= FcPatternCreate();
85 FcPattern
* match
= FcFontMatch(0, pattern
, &result
);
87 int fc_rgba
= FC_RGBA_RGB
;
88 FcPatternGetInteger(match
, FC_RGBA
, 0, &fc_rgba
);
89 FcPatternDestroy(pattern
);
90 FcPatternDestroy(match
);
94 params
->subpixel_rendering
= FontRenderParams::SUBPIXEL_RENDERING_RGB
;
97 params
->subpixel_rendering
= FontRenderParams::SUBPIXEL_RENDERING_BGR
;
100 params
->subpixel_rendering
= FontRenderParams::SUBPIXEL_RENDERING_VRGB
;
103 params
->subpixel_rendering
= FontRenderParams::SUBPIXEL_RENDERING_VBGR
;
106 params
->subpixel_rendering
= FontRenderParams::SUBPIXEL_RENDERING_NONE
;
109 params
->subpixel_positioning
=
110 CommandLine::ForCurrentProcess()->HasSwitch(
112 switches::kEnableWebkitTextSubpixelPositioning
:
113 switches::kEnableBrowserTextSubpixelPositioning
);
115 // To enable subpixel positioning, we need to disable hinting.
116 if (params
->subpixel_positioning
)
117 params
->hinting
= FontRenderParams::HINTING_NONE
;
119 params
->hinting
= FontRenderParams::HINTING_SLIGHT
;
125 const FontRenderParams
& GetDefaultFontRenderParams() {
126 static bool loaded_defaults
= false;
127 static FontRenderParams default_params
;
128 if (!loaded_defaults
)
129 LoadDefaults(&default_params
, /* renderer */ false);
130 loaded_defaults
= true;
131 return default_params
;
134 const FontRenderParams
& GetDefaultWebKitFontRenderParams() {
135 static bool loaded_defaults
= false;
136 static FontRenderParams default_params
;
137 if (!loaded_defaults
)
138 LoadDefaults(&default_params
, /* renderer */ true);
139 loaded_defaults
= true;
140 return default_params
;