diff options
author | danakj@chromium.org <danakj@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-11-10 00:03:19 +0000 |
---|---|---|
committer | danakj@chromium.org <danakj@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-11-10 00:03:19 +0000 |
commit | 2e648601b082774989dec39da9983a5e27e9a797 (patch) | |
tree | fc961f320f1d39cd9a8d0213382b4221ede73c6b /ui/gfx | |
parent | c36000e00d75b51321a18aff9260ee7f66f2b54e (diff) | |
download | chromium_src-2e648601b082774989dec39da9983a5e27e9a797.zip chromium_src-2e648601b082774989dec39da9983a5e27e9a797.tar.gz chromium_src-2e648601b082774989dec39da9983a5e27e9a797.tar.bz2 |
ui: Prefer +/- operators to apply offsets.
Adds +/- operators to gfx::Rect that are applied to its origin Point.
Removes gfx::Point method a.OffsetFrom(b) in favor of (b - a).
Begin use +/- instead of Offset() in ui/gfx/ and cc/
New tests:
ui_unittests:RectTest.Offset
R=pkasting,enne
BUG=158416
Review URL: https://codereview.chromium.org/11293194
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@167012 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui/gfx')
-rw-r--r-- | ui/gfx/blit.cc | 19 | ||||
-rw-r--r-- | ui/gfx/blit.h | 4 | ||||
-rw-r--r-- | ui/gfx/canvas_skia.cc | 8 | ||||
-rw-r--r-- | ui/gfx/pango_util.cc | 2 | ||||
-rw-r--r-- | ui/gfx/point.h | 2 | ||||
-rw-r--r-- | ui/gfx/point_base.h | 10 | ||||
-rw-r--r-- | ui/gfx/point_f.h | 2 | ||||
-rw-r--r-- | ui/gfx/point_unittest.cc | 1 | ||||
-rw-r--r-- | ui/gfx/rect.cc | 12 | ||||
-rw-r--r-- | ui/gfx/rect.h | 7 | ||||
-rw-r--r-- | ui/gfx/rect_base.h | 2 | ||||
-rw-r--r-- | ui/gfx/rect_base_impl.h | 26 | ||||
-rw-r--r-- | ui/gfx/rect_f.cc | 12 | ||||
-rw-r--r-- | ui/gfx/rect_f.h | 7 | ||||
-rw-r--r-- | ui/gfx/rect_unittest.cc | 54 | ||||
-rw-r--r-- | ui/gfx/render_text.cc | 2 |
16 files changed, 125 insertions, 45 deletions
diff --git a/ui/gfx/blit.cc b/ui/gfx/blit.cc index c6c8d672..9bb7e4a 100644 --- a/ui/gfx/blit.cc +++ b/ui/gfx/blit.cc @@ -129,7 +129,7 @@ void BlitCanvasToCanvas(SkCanvas *dst_canvas, void ScrollCanvas(SkCanvas* canvas, const gfx::Rect& in_clip, - const gfx::Vector2d& amount) { + const gfx::Vector2d& offset) { DCHECK(!HasClipOrTransform(*canvas)); // Don't support special stuff. #if defined(OS_WIN) // If we have a PlatformCanvas, we should use ScrollDC. Otherwise, fall @@ -140,7 +140,7 @@ void ScrollCanvas(SkCanvas* canvas, RECT damaged_rect; RECT r = in_clip.ToRECT(); - ScrollDC(hdc, amount.x(), amount.y(), NULL, &r, NULL, &damaged_rect); + ScrollDC(hdc, offset.x(), offset.y(), NULL, &r, NULL, &damaged_rect); return; } #endif // defined(OS_WIN) @@ -157,32 +157,29 @@ void ScrollCanvas(SkCanvas* canvas, in_clip, gfx::Rect(0, 0, bitmap.width(), bitmap.height())); // Compute the set of pixels we'll actually end up painting. - gfx::Rect dest_rect = clip; - dest_rect.Offset(amount); - dest_rect.Intersect(clip); - if (dest_rect.size() == gfx::Size()) + gfx::Rect dest_rect = gfx::IntersectRects(clip + offset, clip); + if (dest_rect.size().IsEmpty()) return; // Nothing to do. // Compute the source pixels that will map to the dest_rect - gfx::Rect src_rect = dest_rect; - src_rect.Offset(-amount.x(), -amount.y()); + gfx::Rect src_rect = dest_rect - offset; size_t row_bytes = dest_rect.width() * 4; - if (amount.y() > 0) { + if (offset.y() > 0) { // Data is moving down, copy from the bottom up. for (int y = dest_rect.height() - 1; y >= 0; y--) { memcpy(bitmap.getAddr32(dest_rect.x(), dest_rect.y() + y), bitmap.getAddr32(src_rect.x(), src_rect.y() + y), row_bytes); } - } else if (amount.y() < 0) { + } else if (offset.y() < 0) { // Data is moving up, copy from the top down. for (int y = 0; y < dest_rect.height(); y++) { memcpy(bitmap.getAddr32(dest_rect.x(), dest_rect.y() + y), bitmap.getAddr32(src_rect.x(), src_rect.y() + y), row_bytes); } - } else if (amount.x() != 0) { + } else if (offset.x() != 0) { // Horizontal-only scroll. We can do it in either top-to-bottom or bottom- // to-top, but have to be careful about the order for copying each row. // Fortunately, memmove already handles this for us. diff --git a/ui/gfx/blit.h b/ui/gfx/blit.h index 01fa4f3..616fda3 100644 --- a/ui/gfx/blit.h +++ b/ui/gfx/blit.h @@ -40,12 +40,12 @@ UI_EXPORT void BlitCanvasToCanvas(SkCanvas *dst_canvas, SkCanvas *src_canvas, const Point& src_origin); -// Scrolls the given subset of the given canvas by the given amount. +// Scrolls the given subset of the given canvas by the given offset. // The canvas should not have a clip or a transform applied, since platforms // may implement those operations differently. UI_EXPORT void ScrollCanvas(SkCanvas* canvas, const Rect& clip, - const Vector2d& amount); + const Vector2d& offset); } // namespace gfx diff --git a/ui/gfx/canvas_skia.cc b/ui/gfx/canvas_skia.cc index baa4db9..ece6e98 100644 --- a/ui/gfx/canvas_skia.cc +++ b/ui/gfx/canvas_skia.cc @@ -288,7 +288,7 @@ void Canvas::DrawStringWithShadows(const string16& text, if (i == 0) { // TODO(msw|asvitkine): Support multi-line text with varied heights. const int aggregate_height = strings.size() * line_height; - rect.Offset(0, (text_bounds.height() - aggregate_height) / 2); + rect += gfx::Vector2d(0, (text_bounds.height() - aggregate_height) / 2); } #endif @@ -297,7 +297,7 @@ void Canvas::DrawStringWithShadows(const string16& text, ApplyUnderlineStyle(range, render_text.get()); render_text->SetDisplayRect(rect); render_text->Draw(this); - rect.Offset(0, line_height); + rect += gfx::Vector2d(0, line_height); } } else { ui::Range range = StripAcceleratorChars(flags, &adjusted_text); @@ -327,7 +327,7 @@ void Canvas::DrawStringWithShadows(const string16& text, const int line_height = render_text->GetStringSize().height(); // Center the text vertically. - rect.Offset(0, (text_bounds.height() - line_height) / 2); + rect += gfx::Vector2d(0, (text_bounds.height() - line_height) / 2); rect.set_height(line_height); render_text->SetDisplayRect(rect); @@ -446,7 +446,7 @@ void Canvas::DrawFadeTruncatingString( const int line_height = render_text->GetStringSize().height(); // Center the text vertically. - rect.Offset(0, (display_rect.height() - line_height) / 2); + rect += gfx::Vector2d(0, (display_rect.height() - line_height) / 2); rect.set_height(line_height); render_text->SetDisplayRect(rect); diff --git a/ui/gfx/pango_util.cc b/ui/gfx/pango_util.cc index b3e102e..cd874c3 100644 --- a/ui/gfx/pango_util.cc +++ b/ui/gfx/pango_util.cc @@ -168,7 +168,7 @@ void DrawTextOntoCairoSurface(cairo_t* cr, pango_layout_get_pixel_size(layout, &width, &height); Rect text_rect(bounds.x(), bounds.y(), width, height); // Vertically center |text_rect| in |bounds|. - text_rect.Offset(0, (bounds.height() - text_rect.height()) / 2); + text_rect += gfx::Vector2d(0, (bounds.height() - text_rect.height()) / 2); DrawPangoLayout(cr, layout, font, bounds, text_rect, text_color, text_direction, flags); diff --git a/ui/gfx/point.h b/ui/gfx/point.h index f81dd90..4404582 100644 --- a/ui/gfx/point.h +++ b/ui/gfx/point.h @@ -74,7 +74,7 @@ inline Point operator-(const Point& lhs, const Vector2d& rhs) { } inline Vector2d operator-(const Point& lhs, const Point& rhs) { - return lhs.OffsetFrom(rhs); + return Vector2d(lhs.x() - rhs.x(), lhs.y() - rhs.y()); } inline Point PointAtOffsetFromOrigin(const Vector2d& offset_from_origin) { diff --git a/ui/gfx/point_base.h b/ui/gfx/point_base.h index ae3342c..2578520 100644 --- a/ui/gfx/point_base.h +++ b/ui/gfx/point_base.h @@ -34,11 +34,13 @@ class UI_EXPORT PointBase { } void operator+=(const VectorClass& vector) { - Offset(vector.x(), vector.y()); + x_ += vector.x(); + y_ += vector.y(); } void operator-=(const VectorClass& vector) { - Offset(-vector.x(), -vector.y()); + x_ -= vector.x(); + y_ -= vector.y(); } bool IsOrigin() const { @@ -49,10 +51,6 @@ class UI_EXPORT PointBase { return VectorClass(x_, y_); } - VectorClass OffsetFrom(const Class& other) const { - return VectorClass(x_ - other.x_, y_ - other.y_); - } - // A point is less than another point if its y-value is closer // to the origin. If the y-values are the same, then point with // the x-value closer to the origin is considered less than the diff --git a/ui/gfx/point_f.h b/ui/gfx/point_f.h index 9b2e934..7a828be 100644 --- a/ui/gfx/point_f.h +++ b/ui/gfx/point_f.h @@ -53,7 +53,7 @@ inline PointF operator-(const PointF& lhs, const Vector2dF& rhs) { } inline Vector2dF operator-(const PointF& lhs, const PointF& rhs) { - return lhs.OffsetFrom(rhs); + return Vector2dF(lhs.x() - rhs.x(), lhs.y() - rhs.y()); } inline PointF PointAtOffsetFromOrigin(const Vector2dF& offset_from_origin) { diff --git a/ui/gfx/point_unittest.cc b/ui/gfx/point_unittest.cc index e76bdb5..82bf78c 100644 --- a/ui/gfx/point_unittest.cc +++ b/ui/gfx/point_unittest.cc @@ -74,7 +74,6 @@ TEST(PointTest, VectorArithmetic) { TEST(PointTest, OffsetFromPoint) { Point a(1, 5); Point b(-20, 8); - EXPECT_EQ(Vector2d(-20 - 1, 8 - 5).ToString(), b.OffsetFrom(a).ToString()); EXPECT_EQ(Vector2d(-20 - 1, 8 - 5).ToString(), (b - a).ToString()); } diff --git a/ui/gfx/rect.cc b/ui/gfx/rect.cc index d52332a..0fe3813 100644 --- a/ui/gfx/rect.cc +++ b/ui/gfx/rect.cc @@ -90,6 +90,18 @@ std::string Rect::ToString() const { size().ToString().c_str()); } +Rect operator+(const Rect& lhs, const Vector2d& rhs) { + Rect result(lhs); + result += rhs; + return result; +} + +Rect operator-(const Rect& lhs, const Vector2d& rhs) { + Rect result(lhs); + result -= rhs; + return result; +} + Rect IntersectRects(const Rect& a, const Rect& b) { Rect result = a; result.Intersect(b); diff --git a/ui/gfx/rect.h b/ui/gfx/rect.h index 5479d2e..076f9fc 100644 --- a/ui/gfx/rect.h +++ b/ui/gfx/rect.h @@ -77,6 +77,13 @@ inline bool operator!=(const Rect& lhs, const Rect& rhs) { return !(lhs == rhs); } +UI_EXPORT Rect operator+(const Rect& lhs, const Vector2d& rhs); +UI_EXPORT Rect operator-(const Rect& lhs, const Vector2d& rhs); + +inline Rect operator+(const Vector2d& lhs, const Rect& rhs) { + return rhs + lhs; +} + UI_EXPORT Rect IntersectRects(const Rect& a, const Rect& b); UI_EXPORT Rect UnionRects(const Rect& a, const Rect& b); UI_EXPORT Rect SubtractRects(const Rect& a, const Rect& b); diff --git a/ui/gfx/rect_base.h b/ui/gfx/rect_base.h index 0a3298c..3ad12c8 100644 --- a/ui/gfx/rect_base.h +++ b/ui/gfx/rect_base.h @@ -69,6 +69,8 @@ class UI_EXPORT RectBase { void Offset(const VectorClass& distance) { Offset(distance.x(), distance.y()); } + void operator+=(const VectorClass& offset); + void operator-=(const VectorClass& offset); InsetsClass InsetsFrom(const Class& inner) const { return InsetsClass(inner.y() - y(), diff --git a/ui/gfx/rect_base_impl.h b/ui/gfx/rect_base_impl.h index 5ca9afa..b4cdef0 100644 --- a/ui/gfx/rect_base_impl.h +++ b/ui/gfx/rect_base_impl.h @@ -102,7 +102,7 @@ template<typename Class, typename Type> void RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>:: Inset(Type left, Type top, Type right, Type bottom) { - Offset(left, top); + origin_ += VectorClass(left, top); set_width(std::max(width() - left - right, static_cast<Type>(0))); set_height(std::max(height() - top - bottom, static_cast<Type>(0))); } @@ -115,7 +115,29 @@ template<typename Class, typename Type> void RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>:: Offset(Type horizontal, Type vertical) { - origin_.Offset(horizontal, vertical); + origin_ += VectorClass(horizontal, vertical); +} + +template<typename Class, + typename PointClass, + typename SizeClass, + typename InsetsClass, + typename VectorClass, + typename Type> +void RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>:: + operator+=(const VectorClass& offset) { + origin_ += offset; +} + +template<typename Class, + typename PointClass, + typename SizeClass, + typename InsetsClass, + typename VectorClass, + typename Type> +void RectBase<Class, PointClass, SizeClass, InsetsClass, VectorClass, Type>:: + operator-=(const VectorClass& offset) { + origin_ -= offset; } template<typename Class, diff --git a/ui/gfx/rect_f.cc b/ui/gfx/rect_f.cc index fa4e99d..4413fb8 100644 --- a/ui/gfx/rect_f.cc +++ b/ui/gfx/rect_f.cc @@ -52,6 +52,18 @@ std::string RectF::ToString() const { size().ToString().c_str()); } +RectF operator+(const RectF& lhs, const Vector2dF& rhs) { + RectF result(lhs); + result += rhs; + return result; +} + +RectF operator-(const RectF& lhs, const Vector2dF& rhs) { + RectF result(lhs); + result -= rhs; + return result; +} + RectF IntersectRects(const RectF& a, const RectF& b) { RectF result = a; result.Intersect(b); diff --git a/ui/gfx/rect_f.h b/ui/gfx/rect_f.h index a86ac9e..c931019 100644 --- a/ui/gfx/rect_f.h +++ b/ui/gfx/rect_f.h @@ -58,6 +58,13 @@ inline bool operator!=(const RectF& lhs, const RectF& rhs) { return !(lhs == rhs); } +UI_EXPORT RectF operator+(const RectF& lhs, const Vector2dF& rhs); +UI_EXPORT RectF operator-(const RectF& lhs, const Vector2dF& rhs); + +inline RectF operator+(const Vector2dF& lhs, const RectF& rhs) { + return rhs + lhs; +} + UI_EXPORT RectF IntersectRects(const RectF& a, const RectF& b); UI_EXPORT RectF UnionRects(const RectF& a, const RectF& b); UI_EXPORT RectF SubtractRects(const RectF& a, const RectF& b); diff --git a/ui/gfx/rect_unittest.cc b/ui/gfx/rect_unittest.cc index 621e5d9..f8222c6 100644 --- a/ui/gfx/rect_unittest.cc +++ b/ui/gfx/rect_unittest.cc @@ -671,33 +671,57 @@ TEST(RectTest, BoundingRect) { } TEST(RectTest, IsExpressibleAsRect) { - EXPECT_TRUE(gfx::RectF().IsExpressibleAsRect()); + EXPECT_TRUE(RectF().IsExpressibleAsRect()); float min = std::numeric_limits<int>::min(); float max = std::numeric_limits<int>::max(); float infinity = std::numeric_limits<float>::infinity(); - EXPECT_TRUE(gfx::RectF( + EXPECT_TRUE(RectF( min + 200, min + 200, max - 200, max - 200).IsExpressibleAsRect()); - EXPECT_FALSE(gfx::RectF( + EXPECT_FALSE(RectF( min - 200, min + 200, max + 200, max + 200).IsExpressibleAsRect()); - EXPECT_FALSE(gfx::RectF( + EXPECT_FALSE(RectF( min + 200 , min - 200, max + 200, max + 200).IsExpressibleAsRect()); - EXPECT_FALSE(gfx::RectF( + EXPECT_FALSE(RectF( min + 200, min + 200, max + 200, max - 200).IsExpressibleAsRect()); - EXPECT_FALSE(gfx::RectF( + EXPECT_FALSE(RectF( min + 200, min + 200, max - 200, max + 200).IsExpressibleAsRect()); - EXPECT_TRUE(gfx::RectF(0, 0, max - 200, max - 200).IsExpressibleAsRect()); - EXPECT_FALSE(gfx::RectF(200, 0, max + 200, max - 200).IsExpressibleAsRect()); - EXPECT_FALSE(gfx::RectF(0, 200, max - 200, max + 200).IsExpressibleAsRect()); - EXPECT_FALSE(gfx::RectF(0, 0, max + 200, max - 200).IsExpressibleAsRect()); - EXPECT_FALSE(gfx::RectF(0, 0, max - 200, max + 200).IsExpressibleAsRect()); + EXPECT_TRUE(RectF(0, 0, max - 200, max - 200).IsExpressibleAsRect()); + EXPECT_FALSE(RectF(200, 0, max + 200, max - 200).IsExpressibleAsRect()); + EXPECT_FALSE(RectF(0, 200, max - 200, max + 200).IsExpressibleAsRect()); + EXPECT_FALSE(RectF(0, 0, max + 200, max - 200).IsExpressibleAsRect()); + EXPECT_FALSE(RectF(0, 0, max - 200, max + 200).IsExpressibleAsRect()); - EXPECT_FALSE(gfx::RectF(infinity, 0, 1, 1).IsExpressibleAsRect()); - EXPECT_FALSE(gfx::RectF(0, infinity, 1, 1).IsExpressibleAsRect()); - EXPECT_FALSE(gfx::RectF(0, 0, infinity, 1).IsExpressibleAsRect()); - EXPECT_FALSE(gfx::RectF(0, 0, 1, infinity).IsExpressibleAsRect()); + EXPECT_FALSE(RectF(infinity, 0, 1, 1).IsExpressibleAsRect()); + EXPECT_FALSE(RectF(0, infinity, 1, 1).IsExpressibleAsRect()); + EXPECT_FALSE(RectF(0, 0, infinity, 1).IsExpressibleAsRect()); + EXPECT_FALSE(RectF(0, 0, 1, infinity).IsExpressibleAsRect()); +} + +TEST(RectTest, Offset) { + Rect i(1, 2, 3, 4); + + EXPECT_EQ(Rect(2, 1, 3, 4).ToString(), (i + Vector2d(1, -1)).ToString()); + EXPECT_EQ(Rect(2, 1, 3, 4).ToString(), (Vector2d(1, -1) + i).ToString()); + i += Vector2d(1, -1); + EXPECT_EQ(Rect(2, 1, 3, 4).ToString(), i.ToString()); + EXPECT_EQ(Rect(1, 2, 3, 4).ToString(), (i - Vector2d(1, -1)).ToString()); + i -= Vector2d(1, -1); + EXPECT_EQ(Rect(1, 2, 3, 4).ToString(), i.ToString()); + + RectF f(1.1f, 2.2f, 3.3f, 4.4f); + EXPECT_EQ(RectF(2.2f, 1.1f, 3.3f, 4.4f).ToString(), + (f + Vector2dF(1.1f, -1.1f)).ToString()); + EXPECT_EQ(RectF(2.2f, 1.1f, 3.3f, 4.4f).ToString(), + (Vector2dF(1.1f, -1.1f) + f).ToString()); + f += Vector2dF(1.1f, -1.1f); + EXPECT_EQ(RectF(2.2f, 1.1f, 3.3f, 4.4f).ToString(), f.ToString()); + EXPECT_EQ(RectF(1.1f, 2.2f, 3.3f, 4.4f).ToString(), + (f - Vector2dF(1.1f, -1.1f)).ToString()); + f -= Vector2dF(1.1f, -1.1f); + EXPECT_EQ(RectF(1.1f, 2.2f, 3.3f, 4.4f).ToString(), f.ToString()); } } // namespace gfx diff --git a/ui/gfx/render_text.cc b/ui/gfx/render_text.cc index 55f0e0c..2cedd9d 100644 --- a/ui/gfx/render_text.cc +++ b/ui/gfx/render_text.cc @@ -1002,7 +1002,7 @@ void RenderText::UpdateCachedBoundsAndOffset() { gfx::Vector2d delta_offset(delta_x, 0); display_offset_ += delta_offset; - cursor_bounds_.Offset(delta_offset); + cursor_bounds_ += delta_offset; } void RenderText::DrawSelection(Canvas* canvas) { |