From 6b1c25b4adfcd0ce330c10a65d3eed22182f59f8 Mon Sep 17 00:00:00 2001 From: "derat@chromium.org" Date: Wed, 5 Aug 2009 23:48:40 +0000 Subject: Linux: make gfx::Canvas honor GTK font settings. BUG=18038 TESTED=restarted chrome a bunch while mucking around with gnome-appearance-settings Review URL: http://codereview.chromium.org/164017 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@22555 0039d316-1c4b-4281-b951-d872f2087c98 --- app/gfx/canvas_linux.cc | 76 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) (limited to 'app') diff --git a/app/gfx/canvas_linux.cc b/app/gfx/canvas_linux.cc index 091685e..155221c 100644 --- a/app/gfx/canvas_linux.cc +++ b/app/gfx/canvas_linux.cc @@ -5,6 +5,7 @@ #include "app/gfx/canvas.h" #include +#include #include #include @@ -15,6 +16,10 @@ namespace { +// Font settings that we initialize once and then use when drawing text in +// DrawStringInt(). +static cairo_font_options_t* cairo_font_options = NULL; + // Returns a new pango font, free with pango_font_description_free(). PangoFontDescription* PangoFontFromGfxFont(const gfx::Font& gfx_font) { gfx::Font font = gfx_font; // Copy so we can call non-const methods. @@ -41,6 +46,70 @@ PangoFontDescription* PangoFontFromGfxFont(const gfx::Font& gfx_font) { return pfd; } +// Update |cairo_font_options| based on GtkSettings, allocating it if needed. +static void UpdateCairoFontOptions() { + if (!cairo_font_options) + cairo_font_options = cairo_font_options_create(); + + GtkSettings* gtk_settings = gtk_settings_get_default(); + gint antialias = 0; + gint hinting = 0; + gchar* hint_style = NULL; + gchar* rgba_style = NULL; + g_object_get(gtk_settings, + "gtk-xft-antialias", &antialias, + "gtk-xft-hinting", &hinting, + "gtk-xft-hintstyle", &hint_style, + "gtk-xft-rgba", &rgba_style, + NULL); + + // g_object_get() doesn't tell us whether the properties were present or not, + // but if they aren't (because gnome-settings-daemon isn't running), we'll get + // NULL values for the strings. + if (hint_style && rgba_style) { + if (!antialias) { + cairo_font_options_set_antialias(cairo_font_options, + CAIRO_ANTIALIAS_NONE); + } else if (strcmp(rgba_style, "none") == 0) { + cairo_font_options_set_antialias(cairo_font_options, + CAIRO_ANTIALIAS_GRAY); + } else { + cairo_font_options_set_antialias(cairo_font_options, + CAIRO_ANTIALIAS_SUBPIXEL); + cairo_subpixel_order_t cairo_subpixel_order = + CAIRO_SUBPIXEL_ORDER_DEFAULT; + if (strcmp(rgba_style, "rgb") == 0) { + cairo_subpixel_order = CAIRO_SUBPIXEL_ORDER_RGB; + } else if (strcmp(rgba_style, "bgr") == 0) { + cairo_subpixel_order = CAIRO_SUBPIXEL_ORDER_BGR; + } else if (strcmp(rgba_style, "vrgb") == 0) { + cairo_subpixel_order = CAIRO_SUBPIXEL_ORDER_VRGB; + } else if (strcmp(rgba_style, "vbgr") == 0) { + cairo_subpixel_order = CAIRO_SUBPIXEL_ORDER_VBGR; + } + cairo_font_options_set_subpixel_order(cairo_font_options, + cairo_subpixel_order); + } + + cairo_hint_style_t cairo_hint_style = CAIRO_HINT_STYLE_DEFAULT; + if (hinting == 0 || strcmp(hint_style, "hintnone") == 0) { + cairo_hint_style = CAIRO_HINT_STYLE_NONE; + } else if (strcmp(hint_style, "hintslight") == 0) { + cairo_hint_style = CAIRO_HINT_STYLE_SLIGHT; + } else if (strcmp(hint_style, "hintmedium") == 0) { + cairo_hint_style = CAIRO_HINT_STYLE_MEDIUM; + } else if (strcmp(hint_style, "hintfull") == 0) { + cairo_hint_style = CAIRO_HINT_STYLE_FULL; + } + cairo_font_options_set_hint_style(cairo_font_options, cairo_hint_style); + } + + if (hint_style) + g_free(hint_style); + if (rgba_style) + g_free(rgba_style); +} + } // namespace namespace gfx { @@ -102,6 +171,13 @@ void Canvas::DrawStringInt(const std::wstring& text, cairo_t* cr = beginPlatformPaint(); PangoLayout* layout = pango_cairo_create_layout(cr); + if (!cairo_font_options) + UpdateCairoFontOptions(); + // This needs to be done early on; it has no effect when called just before + // pango_cairo_show_layout(). + pango_cairo_context_set_font_options( + pango_layout_get_context(layout), cairo_font_options); + // Callers of DrawStringInt handle RTL layout themselves, so tell pango to not // scope out RTL characters. pango_layout_set_auto_dir(layout, FALSE); -- cgit v1.1