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