Refactor HpackDecoder's public API to ease integration into SpdyFramer.
[chromium-blink-merge.git] / ui / gfx / font_render_params_linux.cc
blob5e30f4dd947acf73a05e435d2dfe8420bdfd0aeb
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)
12 #include <gtk/gtk.h>
13 #else
14 #include <fontconfig/fontconfig.h>
15 #endif
17 #if defined(OS_LINUX) && defined(USE_AURA) && !defined(OS_CHROMEOS)
18 #include "ui/gfx/linux_font_delegate.h"
19 #endif
21 namespace gfx {
23 namespace {
25 bool SubpixelPositioningRequested(bool renderer) {
26 return CommandLine::ForCurrentProcess()->HasSwitch(
27 renderer ?
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();
45 CHECK(gtk_settings);
46 gint gtk_antialias = 0;
47 gint gtk_hinting = 0;
48 gchar* gtk_hint_style = NULL;
49 gchar* gtk_rgba = NULL;
50 g_object_get(gtk_settings,
51 "gtk-xft-antialias", &gtk_antialias,
52 "gtk-xft-hinting", &gtk_hinting,
53 "gtk-xft-hintstyle", &gtk_hint_style,
54 "gtk-xft-rgba", &gtk_rgba,
55 NULL);
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);
85 g_free(gtk_rgba);
86 #else
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);
97 FcResult result;
98 FcPattern* match = FcFontMatch(0, pattern, &result);
99 DCHECK(match);
100 int fc_rgba = FC_RGBA_RGB;
101 FcPatternGetInteger(match, FC_RGBA, 0, &fc_rgba);
102 FcPatternDestroy(pattern);
103 FcPatternDestroy(match);
105 switch (fc_rgba) {
106 case FC_RGBA_RGB:
107 params->subpixel_rendering = FontRenderParams::SUBPIXEL_RENDERING_RGB;
108 break;
109 case FC_RGBA_BGR:
110 params->subpixel_rendering = FontRenderParams::SUBPIXEL_RENDERING_BGR;
111 break;
112 case FC_RGBA_VRGB:
113 params->subpixel_rendering = FontRenderParams::SUBPIXEL_RENDERING_VRGB;
114 break;
115 case FC_RGBA_VBGR:
116 params->subpixel_rendering = FontRenderParams::SUBPIXEL_RENDERING_VBGR;
117 break;
118 default:
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();
124 if (delegate) {
125 params->antialiasing = delegate->UseAntialiasing();
126 params->hinting = delegate->GetHintingStyle();
127 params->subpixel_rendering = delegate->GetSubpixelRenderingStyle();
129 #endif
130 #endif
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;
139 } // namespace
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);
163 } // namespace gfx