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>
21 bool SubpixelPositioningRequested(bool renderer) {
22 return CommandLine::ForCurrentProcess()->HasSwitch(
24 switches::kEnableWebkitTextSubpixelPositioning :
25 switches::kEnableBrowserTextSubpixelPositioning);
28 // Initializes |params| with the system's default settings. |renderer| is true
29 // when setting WebKit renderer defaults.
30 void LoadDefaults(FontRenderParams* params, bool renderer) {
31 #if defined(TOOLKIT_GTK)
32 params->antialiasing = true;
33 // TODO(wangxianzhu): autohinter is now true to keep original behavior
34 // of WebKit, but it might not be the best value.
35 params->autohinter = true;
36 params->use_bitmaps = true;
37 params->hinting = FontRenderParams::HINTING_SLIGHT;
38 params->subpixel_rendering = FontRenderParams::SUBPIXEL_RENDERING_NONE;
40 GtkSettings* gtk_settings = gtk_settings_get_default();
42 gint gtk_antialias = 0;
44 gchar* gtk_hint_style = NULL;
45 gchar* gtk_rgba = NULL;
46 g_object_get(gtk_settings,
47 "gtk-xft-antialias", >k_antialias,
48 "gtk-xft-hinting", >k_hinting,
49 "gtk-xft-hintstyle", >k_hint_style,
50 "gtk-xft-rgba", >k_rgba,
53 // g_object_get() doesn't tell us whether the properties were present or not,
54 // but if they aren't (because gnome-settings-daemon isn't running), we'll get
55 // NULL values for the strings.
56 if (gtk_hint_style && gtk_rgba) {
57 params->antialiasing = gtk_antialias;
59 if (gtk_hinting == 0 || strcmp(gtk_hint_style, "hintnone") == 0)
60 params->hinting = FontRenderParams::HINTING_NONE;
61 else if (strcmp(gtk_hint_style, "hintslight") == 0)
62 params->hinting = FontRenderParams::HINTING_SLIGHT;
63 else if (strcmp(gtk_hint_style, "hintmedium") == 0)
64 params->hinting = FontRenderParams::HINTING_MEDIUM;
65 else if (strcmp(gtk_hint_style, "hintfull") == 0)
66 params->hinting = FontRenderParams::HINTING_FULL;
68 if (strcmp(gtk_rgba, "none") == 0)
69 params->subpixel_rendering = FontRenderParams::SUBPIXEL_RENDERING_NONE;
70 else if (strcmp(gtk_rgba, "rgb") == 0)
71 params->subpixel_rendering = FontRenderParams::SUBPIXEL_RENDERING_RGB;
72 else if (strcmp(gtk_rgba, "bgr") == 0)
73 params->subpixel_rendering = FontRenderParams::SUBPIXEL_RENDERING_BGR;
74 else if (strcmp(gtk_rgba, "vrgb") == 0)
75 params->subpixel_rendering = FontRenderParams::SUBPIXEL_RENDERING_VRGB;
76 else if (strcmp(gtk_rgba, "vbgr") == 0)
77 params->subpixel_rendering = FontRenderParams::SUBPIXEL_RENDERING_VBGR;
80 g_free(gtk_hint_style);
83 // For non-GTK builds (read: Aura), just use reasonable hardcoded values.
84 params->antialiasing = true;
85 params->autohinter = true;
86 params->use_bitmaps = true;
87 params->hinting = FontRenderParams::HINTING_SLIGHT;
89 // Fetch default subpixel rendering settings from FontConfig.
90 FcPattern* pattern = FcPatternCreate();
91 FcConfigSubstitute(NULL, pattern, FcMatchPattern);
92 FcDefaultSubstitute(pattern);
94 FcPattern* match = FcFontMatch(0, pattern, &result);
96 int fc_rgba = FC_RGBA_RGB;
97 FcPatternGetInteger(match, FC_RGBA, 0, &fc_rgba);
98 FcPatternDestroy(pattern);
99 FcPatternDestroy(match);
103 params->subpixel_rendering = FontRenderParams::SUBPIXEL_RENDERING_RGB;
106 params->subpixel_rendering = FontRenderParams::SUBPIXEL_RENDERING_BGR;
109 params->subpixel_rendering = FontRenderParams::SUBPIXEL_RENDERING_VRGB;
112 params->subpixel_rendering = FontRenderParams::SUBPIXEL_RENDERING_VBGR;
115 params->subpixel_rendering = FontRenderParams::SUBPIXEL_RENDERING_NONE;
119 params->subpixel_positioning = SubpixelPositioningRequested(renderer);
121 // To enable subpixel positioning, we need to disable hinting.
122 if (params->subpixel_positioning)
123 params->hinting = FontRenderParams::HINTING_NONE;
128 const FontRenderParams& GetDefaultFontRenderParams() {
129 static bool loaded_defaults = false;
130 static FontRenderParams default_params;
131 if (!loaded_defaults)
132 LoadDefaults(&default_params, /* renderer */ false);
133 loaded_defaults = true;
134 return default_params;
137 const FontRenderParams& GetDefaultWebKitFontRenderParams() {
138 static bool loaded_defaults = false;
139 static FontRenderParams default_params;
140 if (!loaded_defaults)
141 LoadDefaults(&default_params, /* renderer */ true);
142 loaded_defaults = true;
143 return default_params;
146 bool GetDefaultWebkitSubpixelPositioning() {
147 return SubpixelPositioningRequested(true);