Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(724)

Unified Diff: ui/gfx/canvas_skia_linux.cc

Issue 8392011: GTK: Step 1 of tab strip refresh. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: More cleanup Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: ui/gfx/canvas_skia_linux.cc
diff --git a/ui/gfx/canvas_skia_linux.cc b/ui/gfx/canvas_skia_linux.cc
index 8101dfa9ea9e0d15d640b3889799df7c61518b31..4728eee6bbdf7e2c9e0187acee5c9f1f957b2c98 100644
--- a/ui/gfx/canvas_skia_linux.cc
+++ b/ui/gfx/canvas_skia_linux.cc
@@ -26,18 +26,11 @@ using std::max;
namespace {
-// 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;
-
// Width of the border drawn around haloed text.
const double kTextHaloWidth = 1.0;
// A class to encapsulate string drawing params and operations.
-class DrawStringContext {
+class DrawStringContext : public gfx::PangoDrawString {
public:
DrawStringContext(gfx::CanvasSkia* canvas,
const string16& text,
@@ -47,29 +40,10 @@ class DrawStringContext {
int flags);
~DrawStringContext();
- void Draw(const SkColor& text_color);
void DrawWithHalo(const SkColor& text_color, const SkColor& halo_color);
private:
- // Draw an underline under the text using |cr|, which must already be
- // initialized with the correct source. |extra_edge_width| is added to the
- // outer edge of the line. Helper method for Draw() and DrawWithHalo().
- void DrawUnderline(cairo_t* cr, double extra_edge_width);
-
- const gfx::Rect& bounds_;
- int flags_;
- const gfx::Font& font_;
-
gfx::CanvasSkia* canvas_;
- cairo_t* cr_;
- PangoLayout* layout_;
-
- int text_x_;
- int text_y_;
- int text_width_;
- int text_height_;
-
- base::i18n::TextDirection text_direction_;
DISALLOW_COPY_AND_ASSIGN(DrawStringContext);
};
@@ -80,93 +54,17 @@ DrawStringContext::DrawStringContext(gfx::CanvasSkia* canvas,
const gfx::Rect& bounds,
const gfx::Rect& clip,
int flags)
- : bounds_(bounds),
- flags_(flags),
- font_(font),
- canvas_(canvas),
- cr_(NULL),
- layout_(NULL),
- text_x_(bounds.x()),
- text_y_(bounds.y()),
- text_width_(0),
- text_height_(0),
- text_direction_(base::i18n::GetFirstStrongCharacterDirection(text)) {
- DCHECK(!bounds_.IsEmpty());
-
- cr_ = skia::BeginPlatformPaint(canvas_->sk_canvas());
- layout_ = pango_cairo_create_layout(cr_);
-
- gfx::SetupPangoLayout(
- layout_, text, font, bounds_.width(), text_direction_, flags_);
-
- pango_layout_set_height(layout_, bounds_.height() * PANGO_SCALE);
-
- cairo_save(cr_);
-
- cairo_rectangle(cr_, clip.x(), clip.y(), clip.width(), clip.height());
- cairo_clip(cr_);
-
- pango_layout_get_pixel_size(layout_, &text_width_, &text_height_);
-
- if (flags_ & gfx::Canvas::TEXT_VALIGN_TOP) {
- // Cairo should draw from the top left corner already.
- } else if (flags_ & gfx::Canvas::TEXT_VALIGN_BOTTOM) {
- text_y_ += (bounds.height() - text_height_);
- } else {
- // Vertically centered.
- text_y_ += ((bounds.height() - text_height_) / 2);
- }
+ : PangoDrawString(skia::BeginPlatformPaint(canvas->sk_canvas()),
+ text,
+ font,
+ bounds,
+ clip,
+ flags),
+ canvas_(canvas) {
}
DrawStringContext::~DrawStringContext() {
- cairo_restore(cr_);
skia::EndPlatformPaint(canvas_->sk_canvas());
- g_object_unref(layout_);
- // NOTE: BeginPlatformPaint returned its surface, we shouldn't destroy it.
-}
-
-void DrawStringContext::Draw(const SkColor& text_color) {
- 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;
-
- cairo_pattern_t* pattern = NULL;
-
- cairo_save(cr_);
-
- // If we're not eliding, use a fixed color.
- // Otherwise, create a gradient pattern to use as the source.
- if (text_direction_ == base::i18n::RIGHT_TO_LEFT ||
- (flags_ & gfx::Canvas::NO_ELLIPSIS) ||
- text_width_ <= bounds_.width()) {
- cairo_set_source_rgba(cr_, r, g, b, a);
- } else {
- // Fade to semi-transparent to elide.
- 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;
-
- 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);
- cairo_set_source(cr_, pattern);
- }
-
- cairo_move_to(cr_, text_x_, text_y_);
- pango_cairo_show_layout(cr_, layout_);
-
- if (font_.GetStyle() & gfx::Font::UNDERLINED)
- DrawUnderline(cr_, 0.0);
-
- if (pattern)
- cairo_pattern_destroy(pattern);
-
- cairo_restore(cr_);
}
void DrawStringContext::DrawWithHalo(const SkColor& text_color,
@@ -220,19 +118,6 @@ void DrawStringContext::DrawWithHalo(const SkColor& text_color,
canvas_->DrawBitmapInt(text_bitmap, text_x_ - 1, text_y_ - 1);
}
-void DrawStringContext::DrawUnderline(cairo_t* cr, double extra_edge_width) {
- gfx::PlatformFontPango* platform_font =
- static_cast<gfx::PlatformFontPango*>(font_.platform_font());
- const double underline_y =
- static_cast<double>(text_y_) + text_height_ +
- platform_font->underline_position();
- cairo_set_line_width(
- cr, platform_font->underline_thickness() + 2 * extra_edge_width);
- cairo_move_to(cr, text_x_ - extra_edge_width, underline_y);
- cairo_line_to(cr, text_x_ + text_width_ + extra_edge_width, underline_y);
- cairo_stroke(cr);
-}
-
} // namespace
namespace gfx {

Powered by Google App Engine
This is Rietveld 408576698