Home | History | Annotate | Download | only in gfx
      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.
      4 
      5 #include "ui/gfx/font_render_params_linux.h"
      6 
      7 #include "base/command_line.h"
      8 #include "base/logging.h"
      9 #include "ui/gfx/switches.h"
     10 
     11 #if defined(TOOLKIT_GTK)
     12 #include <gtk/gtk.h>
     13 #else
     14 #include <fontconfig/fontconfig.h>
     15 #endif
     16 
     17 namespace gfx {
     18 
     19 namespace {
     20 
     21 bool SubpixelPositioningRequested(bool renderer) {
     22   return CommandLine::ForCurrentProcess()->HasSwitch(
     23       renderer ?
     24       switches::kEnableWebkitTextSubpixelPositioning :
     25       switches::kEnableBrowserTextSubpixelPositioning);
     26 }
     27 
     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;
     39 
     40   GtkSettings* gtk_settings = gtk_settings_get_default();
     41   CHECK(gtk_settings);
     42   gint gtk_antialias = 0;
     43   gint gtk_hinting = 0;
     44   gchar* gtk_hint_style = NULL;
     45   gchar* gtk_rgba = NULL;
     46   g_object_get(gtk_settings,
     47                "gtk-xft-antialias", &gtk_antialias,
     48                "gtk-xft-hinting", &gtk_hinting,
     49                "gtk-xft-hintstyle", &gtk_hint_style,
     50                "gtk-xft-rgba", &gtk_rgba,
     51                NULL);
     52 
     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;
     58 
     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;
     67 
     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;
     78   }
     79 
     80   g_free(gtk_hint_style);
     81   g_free(gtk_rgba);
     82 #else
     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;
     88 
     89   // Fetch default subpixel rendering settings from FontConfig.
     90   FcPattern* pattern = FcPatternCreate();
     91   FcConfigSubstitute(NULL, pattern, FcMatchPattern);
     92   FcDefaultSubstitute(pattern);
     93   FcResult result;
     94   FcPattern* match = FcFontMatch(0, pattern, &result);
     95   DCHECK(match);
     96   int fc_rgba = FC_RGBA_RGB;
     97   FcPatternGetInteger(match, FC_RGBA, 0, &fc_rgba);
     98   FcPatternDestroy(pattern);
     99   FcPatternDestroy(match);
    100 
    101   switch (fc_rgba) {
    102     case FC_RGBA_RGB:
    103       params->subpixel_rendering = FontRenderParams::SUBPIXEL_RENDERING_RGB;
    104       break;
    105     case FC_RGBA_BGR:
    106       params->subpixel_rendering = FontRenderParams::SUBPIXEL_RENDERING_BGR;
    107       break;
    108     case FC_RGBA_VRGB:
    109       params->subpixel_rendering = FontRenderParams::SUBPIXEL_RENDERING_VRGB;
    110       break;
    111     case FC_RGBA_VBGR:
    112       params->subpixel_rendering = FontRenderParams::SUBPIXEL_RENDERING_VBGR;
    113       break;
    114     default:
    115       params->subpixel_rendering = FontRenderParams::SUBPIXEL_RENDERING_NONE;
    116   }
    117 #endif
    118 
    119   params->subpixel_positioning = SubpixelPositioningRequested(renderer);
    120 
    121   // To enable subpixel positioning, we need to disable hinting.
    122   if (params->subpixel_positioning)
    123     params->hinting = FontRenderParams::HINTING_NONE;
    124 }
    125 
    126 }  // namespace
    127 
    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;
    135 }
    136 
    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;
    144 }
    145 
    146 bool GetDefaultWebkitSubpixelPositioning() {
    147   return SubpixelPositioningRequested(true);
    148 }
    149 
    150 }  // namespace gfx
    151