Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011 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/pango_util.h" | |
| 6 | |
| 7 #include <cairo/cairo.h> | |
| 8 #include <gtk/gtk.h> | |
| 9 #include <pango/pango.h> | |
| 10 #include <pango/pangocairo.h> | |
| 11 | |
| 12 #include "base/logging.h" | |
| 13 #include "base/utf_string_conversions.h" | |
| 14 #include "ui/gfx/canvas.h" | |
| 15 #include "ui/gfx/font.h" | |
| 16 | |
| 17 #if !defined(USE_WAYLAND) | |
| 18 #include "ui/gfx/gtk_util.h" | |
| 19 #endif | |
| 20 | |
| 21 #include "ui/gfx/skia_util.h" | |
| 22 | |
| 23 namespace { | |
| 24 | |
| 25 // Marker for accelerators in the text. | |
| 26 const gunichar kAcceleratorChar = '&'; | |
| 27 | |
| 28 // Return |cairo_font_options|. If needed, allocate and update it based on | |
| 29 // GtkSettings. | |
| 30 cairo_font_options_t* GetCairoFontOptions() { | |
| 31 // Font settings that we initialize once and then use when drawing text. | |
| 32 static cairo_font_options_t* cairo_font_options = NULL; | |
| 33 | |
| 34 if (cairo_font_options) | |
| 35 return cairo_font_options; | |
| 36 | |
| 37 cairo_font_options = cairo_font_options_create(); | |
| 38 | |
| 39 gint antialias = 0; | |
| 40 gint hinting = 0; | |
| 41 gchar* hint_style = NULL; | |
| 42 gchar* rgba_style = NULL; | |
| 43 | |
| 44 #if !defined(USE_WAYLAND) | |
| 45 // TODO(xji): still has gtk dependency. | |
| 46 GtkSettings* gtk_settings = gtk_settings_get_default(); | |
| 47 g_object_get(gtk_settings, | |
| 48 "gtk-xft-antialias", &antialias, | |
| 49 "gtk-xft-hinting", &hinting, | |
| 50 "gtk-xft-hintstyle", &hint_style, | |
| 51 "gtk-xft-rgba", &rgba_style, | |
| 52 NULL); | |
| 53 #endif | |
| 54 | |
| 55 // g_object_get() doesn't tell us whether the properties were present or not, | |
| 56 // but if they aren't (because gnome-settings-daemon isn't running), we'll get | |
| 57 // NULL values for the strings. | |
| 58 if (hint_style && rgba_style) { | |
| 59 if (!antialias) { | |
| 60 cairo_font_options_set_antialias(cairo_font_options, | |
| 61 CAIRO_ANTIALIAS_NONE); | |
| 62 } else if (strcmp(rgba_style, "none") == 0) { | |
| 63 cairo_font_options_set_antialias(cairo_font_options, | |
| 64 CAIRO_ANTIALIAS_GRAY); | |
| 65 } else { | |
| 66 cairo_font_options_set_antialias(cairo_font_options, | |
| 67 CAIRO_ANTIALIAS_SUBPIXEL); | |
| 68 cairo_subpixel_order_t cairo_subpixel_order = | |
| 69 CAIRO_SUBPIXEL_ORDER_DEFAULT; | |
| 70 if (strcmp(rgba_style, "rgb") == 0) { | |
| 71 cairo_subpixel_order = CAIRO_SUBPIXEL_ORDER_RGB; | |
| 72 } else if (strcmp(rgba_style, "bgr") == 0) { | |
| 73 cairo_subpixel_order = CAIRO_SUBPIXEL_ORDER_BGR; | |
| 74 } else if (strcmp(rgba_style, "vrgb") == 0) { | |
| 75 cairo_subpixel_order = CAIRO_SUBPIXEL_ORDER_VRGB; | |
| 76 } else if (strcmp(rgba_style, "vbgr") == 0) { | |
| 77 cairo_subpixel_order = CAIRO_SUBPIXEL_ORDER_VBGR; | |
| 78 } | |
| 79 cairo_font_options_set_subpixel_order(cairo_font_options, | |
| 80 cairo_subpixel_order); | |
| 81 } | |
| 82 | |
| 83 cairo_hint_style_t cairo_hint_style = CAIRO_HINT_STYLE_DEFAULT; | |
| 84 if (hinting == 0 || strcmp(hint_style, "hintnone") == 0) { | |
| 85 cairo_hint_style = CAIRO_HINT_STYLE_NONE; | |
| 86 } else if (strcmp(hint_style, "hintslight") == 0) { | |
| 87 cairo_hint_style = CAIRO_HINT_STYLE_SLIGHT; | |
| 88 } else if (strcmp(hint_style, "hintmedium") == 0) { | |
| 89 cairo_hint_style = CAIRO_HINT_STYLE_MEDIUM; | |
| 90 } else if (strcmp(hint_style, "hintfull") == 0) { | |
| 91 cairo_hint_style = CAIRO_HINT_STYLE_FULL; | |
| 92 } | |
| 93 cairo_font_options_set_hint_style(cairo_font_options, cairo_hint_style); | |
| 94 } | |
| 95 | |
| 96 if (hint_style) | |
| 97 g_free(hint_style); | |
| 98 if (rgba_style) | |
| 99 g_free(rgba_style); | |
| 100 | |
| 101 return cairo_font_options; | |
| 102 } | |
| 103 | |
| 104 } // namespace | |
| 105 | |
| 106 namespace gfx { | |
| 107 | |
| 108 // Pass a width greater than 0 to force wrapping and eliding. | |
| 109 void SetupPangoLayout(PangoLayout* layout, | |
| 110 const string16& text, | |
| 111 const Font& font, | |
| 112 int width, | |
| 113 base::i18n::TextDirection text_direction, | |
| 114 int flags) { | |
| 115 cairo_font_options_t* cairo_font_options = GetCairoFontOptions(); | |
| 116 // This needs to be done early on; it has no effect when called just before | |
| 117 // pango_cairo_show_layout(). | |
| 118 pango_cairo_context_set_font_options( | |
| 119 pango_layout_get_context(layout), cairo_font_options); | |
| 120 | |
| 121 // Callers of DrawStringInt handle RTL layout themselves, so tell pango to not | |
| 122 // scope out RTL characters. | |
| 123 pango_layout_set_auto_dir(layout, FALSE); | |
| 124 | |
| 125 if (width > 0) | |
| 126 pango_layout_set_width(layout, width * PANGO_SCALE); | |
| 127 | |
| 128 if (flags & Canvas::TEXT_ALIGN_CENTER) { | |
| 129 // We don't support center aligned w/ eliding. | |
| 130 DCHECK(gfx::Canvas::NO_ELLIPSIS); | |
| 131 pango_layout_set_alignment(layout, PANGO_ALIGN_CENTER); | |
| 132 } else if (flags & Canvas::TEXT_ALIGN_RIGHT) { | |
| 133 pango_layout_set_alignment(layout, PANGO_ALIGN_RIGHT); | |
| 134 } | |
| 135 | |
| 136 if (flags & Canvas::NO_ELLIPSIS) { | |
| 137 pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_NONE); | |
| 138 if (flags & Canvas::MULTI_LINE) { | |
| 139 pango_layout_set_wrap(layout, | |
| 140 (flags & Canvas::CHARACTER_BREAK) ? | |
| 141 PANGO_WRAP_WORD_CHAR : PANGO_WRAP_WORD); | |
| 142 } | |
| 143 } else if (text_direction == base::i18n::RIGHT_TO_LEFT) { | |
| 144 pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END); | |
| 145 } else { | |
| 146 // Fading the text will be handled in the draw operation. | |
| 147 // Ensure that the text is only on one line. | |
| 148 pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_NONE); | |
| 149 pango_layout_set_width(layout, -1); | |
| 150 } | |
| 151 | |
| 152 // Set the resolution to match that used by Gtk. If we don't set the | |
| 153 // resolution and the resolution differs from the default, Gtk and Chrome end | |
| 154 // up drawing at different sizes. | |
| 155 double resolution = GetPangoResolution(); | |
| 156 if (resolution > 0) { | |
| 157 pango_cairo_context_set_resolution(pango_layout_get_context(layout), | |
| 158 resolution); | |
| 159 } | |
| 160 | |
| 161 PangoFontDescription* desc = font.GetNativeFont(); | |
| 162 pango_layout_set_font_description(layout, desc); | |
| 163 pango_font_description_free(desc); | |
| 164 | |
| 165 // Set text and accelerator character if needed. | |
| 166 std::string utf8 = UTF16ToUTF8(text); | |
| 167 if (flags & Canvas::SHOW_PREFIX) { | |
| 168 // Escape the text string to be used as markup. | |
| 169 gchar* escaped_text = g_markup_escape_text(utf8.c_str(), utf8.size()); | |
| 170 pango_layout_set_markup_with_accel(layout, | |
| 171 escaped_text, | |
| 172 strlen(escaped_text), | |
| 173 kAcceleratorChar, NULL); | |
| 174 g_free(escaped_text); | |
| 175 } else if (flags & Canvas::HIDE_PREFIX) { | |
| 176 // Remove the ampersand character. A double ampersand is output as | |
| 177 // a single ampersand. | |
| 178 DCHECK_EQ(1, g_unichar_to_utf8(kAcceleratorChar, NULL)); | |
| 179 const std::string accelerator_removed = | |
| 180 RemoveAcceleratorChar(utf8, static_cast<char>(kAcceleratorChar)); | |
| 181 | |
| 182 pango_layout_set_text(layout, | |
| 183 accelerator_removed.data(), accelerator_removed.size()); | |
| 184 } else { | |
| 185 pango_layout_set_text(layout, utf8.data(), utf8.size()); | |
| 186 } | |
| 187 } | |
| 188 | |
| 189 } | |
|
msw
2011/08/29 20:38:05
} // namespace gfx
Also, Lint thinks this needs a
| |
| OLD | NEW |