Mailbox support for texture layers.
[chromium-blink-merge.git] / ui / gfx / font_render_params_linux.cc
blob3e3e92a1343a38a85f6c8570ae4cba628727462f
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)
10 #include <gtk/gtk.h>
11 #else
12 #include <fontconfig/fontconfig.h>
13 #include "base/command_line.h"
14 #include "ui/gfx/switches.h"
15 #endif
17 namespace gfx {
19 namespace {
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();
35 CHECK(gtk_settings);
36 gint gtk_antialias = 0;
37 gint gtk_hinting = 0;
38 gchar* gtk_hint_style = NULL;
39 gchar* gtk_rgba = NULL;
40 g_object_get(gtk_settings,
41 "gtk-xft-antialias", &gtk_antialias,
42 "gtk-xft-hinting", &gtk_hinting,
43 "gtk-xft-hintstyle", &gtk_hint_style,
44 "gtk-xft-rgba", &gtk_rgba,
45 NULL);
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);
75 g_free(gtk_rgba);
76 #else
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();
84 FcResult result;
85 FcPattern* match = FcFontMatch(0, pattern, &result);
86 DCHECK(match);
87 int fc_rgba = FC_RGBA_RGB;
88 FcPatternGetInteger(match, FC_RGBA, 0, &fc_rgba);
89 FcPatternDestroy(pattern);
90 FcPatternDestroy(match);
92 switch (fc_rgba) {
93 case FC_RGBA_RGB:
94 params->subpixel_rendering = FontRenderParams::SUBPIXEL_RENDERING_RGB;
95 break;
96 case FC_RGBA_BGR:
97 params->subpixel_rendering = FontRenderParams::SUBPIXEL_RENDERING_BGR;
98 break;
99 case FC_RGBA_VRGB:
100 params->subpixel_rendering = FontRenderParams::SUBPIXEL_RENDERING_VRGB;
101 break;
102 case FC_RGBA_VBGR:
103 params->subpixel_rendering = FontRenderParams::SUBPIXEL_RENDERING_VBGR;
104 break;
105 default:
106 params->subpixel_rendering = FontRenderParams::SUBPIXEL_RENDERING_NONE;
109 params->subpixel_positioning =
110 CommandLine::ForCurrentProcess()->HasSwitch(
111 renderer ?
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;
118 else
119 params->hinting = FontRenderParams::HINTING_SLIGHT;
120 #endif
123 } // namespace
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;
143 } // namespace gfx