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/display.h"
10 #include "ui/gfx/switches.h"
12 #include <fontconfig/fontconfig.h>
14 #if defined(OS_LINUX) && defined(USE_AURA) && !defined(OS_CHROMEOS)
15 #include "ui/gfx/linux_font_delegate.h"
22 bool SubpixelPositioningRequested(bool renderer
) {
23 const CommandLine
* cl
= CommandLine::ForCurrentProcess();
25 // Text rendered by Blink in high-DPI mode is poorly-hinted unless subpixel
26 // positioning is used (as opposed to each glyph being individually snapped
27 // to the pixel grid).
28 return cl
->HasSwitch(switches::kEnableWebkitTextSubpixelPositioning
) ||
29 (Display::HasForceDeviceScaleFactor() &&
30 Display::GetForcedDeviceScaleFactor() != 1.0);
32 return cl
->HasSwitch(switches::kEnableBrowserTextSubpixelPositioning
);
35 // Initializes |params| with the system's default settings. |renderer| is true
36 // when setting WebKit renderer defaults.
37 void LoadDefaults(FontRenderParams
* params
, bool renderer
) {
38 // For non-GTK builds (read: Aura), just use reasonable hardcoded values.
39 params
->antialiasing
= true;
40 params
->autohinter
= true;
41 params
->use_bitmaps
= true;
42 params
->hinting
= FontRenderParams::HINTING_SLIGHT
;
44 // Fetch default subpixel rendering settings from FontConfig.
45 FcPattern
* pattern
= FcPatternCreate();
46 FcConfigSubstitute(NULL
, pattern
, FcMatchPattern
);
47 FcDefaultSubstitute(pattern
);
49 FcPattern
* match
= FcFontMatch(0, pattern
, &result
);
51 int fc_rgba
= FC_RGBA_RGB
;
52 FcPatternGetInteger(match
, FC_RGBA
, 0, &fc_rgba
);
53 FcPatternDestroy(pattern
);
54 FcPatternDestroy(match
);
58 params
->subpixel_rendering
= FontRenderParams::SUBPIXEL_RENDERING_RGB
;
61 params
->subpixel_rendering
= FontRenderParams::SUBPIXEL_RENDERING_BGR
;
64 params
->subpixel_rendering
= FontRenderParams::SUBPIXEL_RENDERING_VRGB
;
67 params
->subpixel_rendering
= FontRenderParams::SUBPIXEL_RENDERING_VBGR
;
70 params
->subpixel_rendering
= FontRenderParams::SUBPIXEL_RENDERING_NONE
;
73 #if defined(OS_LINUX) && defined(USE_AURA) && !defined(OS_CHROMEOS)
74 const LinuxFontDelegate
* delegate
= LinuxFontDelegate::instance();
76 params
->antialiasing
= delegate
->UseAntialiasing();
77 params
->hinting
= delegate
->GetHintingStyle();
78 params
->subpixel_rendering
= delegate
->GetSubpixelRenderingStyle();
82 params
->subpixel_positioning
= SubpixelPositioningRequested(renderer
);
84 // To enable subpixel positioning, we need to disable hinting.
85 if (params
->subpixel_positioning
)
86 params
->hinting
= FontRenderParams::HINTING_NONE
;
91 const FontRenderParams
& GetDefaultFontRenderParams() {
92 static bool loaded_defaults
= false;
93 static FontRenderParams default_params
;
95 LoadDefaults(&default_params
, /* renderer */ false);
96 loaded_defaults
= true;
97 return default_params
;
100 const FontRenderParams
& GetDefaultWebKitFontRenderParams() {
101 static bool loaded_defaults
= false;
102 static FontRenderParams default_params
;
103 if (!loaded_defaults
)
104 LoadDefaults(&default_params
, /* renderer */ true);
105 loaded_defaults
= true;
106 return default_params
;
109 bool GetDefaultWebkitSubpixelPositioning() {
110 return SubpixelPositioningRequested(true);