Chromium Code Reviews| Index: ui/gfx/canvas_skia_linux.cc |
| diff --git a/ui/gfx/canvas_skia_linux.cc b/ui/gfx/canvas_skia_linux.cc |
| index 1d5d29856a7d091c2073a3727988cce74f3c58df..dd8aaf725edfde51437bca30fd4c7f6097120a68 100644 |
| --- a/ui/gfx/canvas_skia_linux.cc |
| +++ b/ui/gfx/canvas_skia_linux.cc |
| @@ -9,7 +9,9 @@ |
| #include <pango/pango.h> |
| #include <pango/pangocairo.h> |
| +#include "base/i18n/rtl.h" |
| #include "base/logging.h" |
| +#include "base/stringprintf.h" |
|
oshima
2011/05/31 23:56:10
you dont need this anymore? (guessing you added fo
|
| #include "base/utf_string_conversions.h" |
| #include "ui/gfx/font.h" |
| #include "ui/gfx/gtk_util.h" |
| @@ -20,6 +22,13 @@ namespace { |
| const gunichar kAcceleratorChar = '&'; |
| +// Multiply by the text height to determine how much text should be faded |
| +// when elliding. |
| +const double kFadeWidthFactor = 1.5; |
| + |
| +// End state of the elliding fade. |
| +const double kKFadeFinalAlpha = 0.15; |
| + |
| // Font settings that we initialize once and then use when drawing text in |
| // DrawStringInt(). |
| static cairo_font_options_t* cairo_font_options = NULL; |
| @@ -108,22 +117,26 @@ static void SetupPangoLayout(PangoLayout* layout, |
| if (width > 0) |
| pango_layout_set_width(layout, width * PANGO_SCALE); |
| - if (flags & gfx::Canvas::NO_ELLIPSIS) { |
| - pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_NONE); |
| - } else { |
| - pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END); |
| - } |
| - |
| if (flags & gfx::Canvas::TEXT_ALIGN_CENTER) { |
| pango_layout_set_alignment(layout, PANGO_ALIGN_CENTER); |
| } else if (flags & gfx::Canvas::TEXT_ALIGN_RIGHT) { |
| pango_layout_set_alignment(layout, PANGO_ALIGN_RIGHT); |
| } |
| - if (flags & gfx::Canvas::MULTI_LINE) { |
| - pango_layout_set_wrap(layout, |
| - (flags & gfx::Canvas::CHARACTER_BREAK) ? |
| - PANGO_WRAP_WORD_CHAR : PANGO_WRAP_WORD); |
| + if (flags & gfx::Canvas::NO_ELLIPSIS) { |
| + pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_NONE); |
| + if (flags & gfx::Canvas::MULTI_LINE) { |
| + pango_layout_set_wrap(layout, |
| + (flags & gfx::Canvas::CHARACTER_BREAK) ? |
| + PANGO_WRAP_WORD_CHAR : PANGO_WRAP_WORD); |
| + } |
| + } else if (base::i18n::IsRTL()){ |
| + pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END); |
| + } else { |
| + // Fading the text will be handled in the draw operation. |
| + // that the text is only on one line. |
| + pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_NONE); |
| + pango_layout_set_width(layout, -1); |
|
Dmitry Polukhin
2011/06/08 12:50:35
Setting width to -1 breaks TEXT_ALIGN_CENTER. See
Dmitry Polukhin
2011/06/08 13:09:48
It looks like we can't disable wrap without settin
|
| } |
| // Set the resolution to match that used by Gtk. If we don't set the |
| @@ -253,13 +266,64 @@ DrawStringContext::~DrawStringContext() { |
| } |
| void DrawStringContext::Draw(const SkColor& text_color) { |
| - cairo_set_source_rgba(cr_, |
| - SkColorGetR(text_color) / 255.0, |
| - SkColorGetG(text_color) / 255.0, |
| - SkColorGetB(text_color) / 255.0, |
| - SkColorGetA(text_color) / 255.0); |
| - cairo_move_to(cr_, text_x_, text_y_); |
| - pango_cairo_show_layout(cr_, layout_); |
| + double r = SkColorGetR(text_color) / 255.0, |
| + g = SkColorGetG(text_color) / 255.0, |
| + b = SkColorGetB(text_color) / 255.0, |
| + a = SkColorGetA(text_color) / 255.0; |
| + |
| + if (base::i18n::IsRTL() || |
| + (flags_ & gfx::Canvas::NO_ELLIPSIS) || |
| + text_width_ <= bounds_.width()) { |
| + cairo_set_source_rgba(cr_, r, g, b, a); |
| + cairo_move_to(cr_, text_x_, text_y_); |
| + pango_cairo_show_layout(cr_, layout_); |
| + } else { |
| + // Fade to gray to ellide. |
| + int fade_width = static_cast<double>(text_height_) * kFadeWidthFactor; |
| + if (fade_width > (bounds_.width() / 2)) { |
| + // Don't fade more than half the text. |
| + fade_width = bounds_.width() / 2; |
| + } |
| + int fade_x = bounds_.x() + bounds_.width() - fade_width; |
| + |
| + // Draw left part of text, clipped at fade_width. |
| + cairo_save(cr_); |
| + cairo_rectangle( |
|
oshima
2011/05/31 23:56:10
indent at "(" ?
|
| + cr_, |
| + bounds_.x(), |
| + bounds_.y(), |
| + bounds_.width() - fade_width, |
| + bounds_.height()); |
| + cairo_clip(cr_); |
| + cairo_set_source_rgba(cr_, r, g, b, a); |
| + cairo_move_to(cr_, text_x_, text_y_); |
| + pango_cairo_show_layout(cr_, layout_); |
| + cairo_restore(cr_); |
| + |
| + // Clip to the left of fade_width. |
| + cairo_save(cr_); |
| + cairo_rectangle(cr_, fade_x, bounds_.y(), fade_width, bounds_.height()); |
| + cairo_clip(cr_); |
| + |
| + // Create clip for text. |
| + cairo_move_to(cr_, text_x_, text_y_); |
| + pango_cairo_layout_path(cr_, layout_); |
| + cairo_clip(cr_); |
| + |
| + // Create alpha fade pattern. |
| + cairo_pattern_t* pattern = cairo_pattern_create_linear( |
| + fade_x, bounds_.y(), bounds_.x() + bounds_.width(), bounds_.y()); |
| + cairo_pattern_add_color_stop_rgba(pattern, 0, r, g, b, a); |
| + cairo_pattern_add_color_stop_rgba(pattern, 1, r, g, b, kKFadeFinalAlpha); |
| + |
| + // Draw pattern, clipped by text. |
| + cairo_set_source(cr_, pattern); |
| + cairo_fill(cr_); |
| + cairo_paint(cr_); |
| + cairo_pattern_destroy(pattern); |
| + |
| + cairo_restore(cr_); |
| + } |
| } |
| void DrawStringContext::DrawWithHalo(const SkColor& text_color, |