diff options
author | davemoore@chromium.org <davemoore@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-06-01 04:51:19 +0000 |
---|---|---|
committer | davemoore@chromium.org <davemoore@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-06-01 04:51:19 +0000 |
commit | 6c5fb2cb139b944ed048bf91139772e42c2c6a2a (patch) | |
tree | 1f84e7af46cd07d54d0d75fe6f56af0ae992ce30 /ui | |
parent | fdcf2dbf32cacc1f5649752bd7e347fdd758b5dd (diff) | |
download | chromium_src-6c5fb2cb139b944ed048bf91139772e42c2c6a2a.zip chromium_src-6c5fb2cb139b944ed048bf91139772e42c2c6a2a.tar.gz chromium_src-6c5fb2cb139b944ed048bf91139772e42c2c6a2a.tar.bz2 |
Ellide by fading
BUG=chromium-os:15552
TEST=Examine tabs when text overflows
Review URL: http://codereview.chromium.org/7105001
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@87431 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui')
-rw-r--r-- | ui/gfx/canvas_skia_linux.cc | 96 |
1 files changed, 79 insertions, 17 deletions
diff --git a/ui/gfx/canvas_skia_linux.cc b/ui/gfx/canvas_skia_linux.cc index 1d5d298..761d317 100644 --- a/ui/gfx/canvas_skia_linux.cc +++ b/ui/gfx/canvas_skia_linux.cc @@ -9,6 +9,7 @@ #include <pango/pango.h> #include <pango/pangocairo.h> +#include "base/i18n/rtl.h" #include "base/logging.h" #include "base/utf_string_conversions.h" #include "ui/gfx/font.h" @@ -20,6 +21,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 kFadeFinalAlpha = 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 +116,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); } // Set the resolution to match that used by Gtk. If we don't set the @@ -253,13 +265,63 @@ 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(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, kFadeFinalAlpha); + + // 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, |