diff options
author | danakj@chromium.org <danakj@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-11-10 05:24:19 +0000 |
---|---|---|
committer | danakj@chromium.org <danakj@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-11-10 05:24:19 +0000 |
commit | 685836bbb4799a9333821b431a7535b29391bd2e (patch) | |
tree | e594b61457e5a487267c6be0a039353f849548f3 /ui/gfx | |
parent | fc290d614c7fab02dcdaf88b992a0204be23aa90 (diff) | |
download | chromium_src-685836bbb4799a9333821b431a7535b29391bd2e.zip chromium_src-685836bbb4799a9333821b431a7535b29391bd2e.tar.gz chromium_src-685836bbb4799a9333821b431a7535b29391bd2e.tar.bz2 |
ui: Remove gfx::Size::ClampToNonNegative, prevent negative sizes always.
This was added with the intention of using Size as a vector, replacing use of
IntSize. Since we have Vector2d now, negative sizes should not exist, so clamp
them in set_width/set_height and the constructor.
R=sky
BUG=160158
Review URL: https://codereview.chromium.org/11365160
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@167062 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui/gfx')
-rw-r--r-- | ui/gfx/rect_f.h | 1 | ||||
-rw-r--r-- | ui/gfx/rect_unittest.cc | 5 | ||||
-rw-r--r-- | ui/gfx/size.cc | 13 | ||||
-rw-r--r-- | ui/gfx/size_base.h | 17 | ||||
-rw-r--r-- | ui/gfx/size_base_impl.h | 37 | ||||
-rw-r--r-- | ui/gfx/size_f.cc | 7 | ||||
-rw-r--r-- | ui/gfx/size_unittest.cc | 18 |
7 files changed, 50 insertions, 48 deletions
diff --git a/ui/gfx/rect_f.h b/ui/gfx/rect_f.h index 7595091..965e5af 100644 --- a/ui/gfx/rect_f.h +++ b/ui/gfx/rect_f.h @@ -37,7 +37,6 @@ class UI_EXPORT RectF set_origin(ScalePoint(origin(), x_scale, y_scale)); SizeF new_size = size().Scale(x_scale, y_scale); - new_size.ClampToNonNegative(); set_size(new_size); } diff --git a/ui/gfx/rect_unittest.cc b/ui/gfx/rect_unittest.cc index f8222c6..dce1e38 100644 --- a/ui/gfx/rect_unittest.cc +++ b/ui/gfx/rect_unittest.cc @@ -433,10 +433,7 @@ TEST(RectTest, ScaleRect) { std::numeric_limits<float>::max(), std::numeric_limits<float>::max(), std::numeric_limits<float>::max(), - std::numeric_limits<float>::max() }, - { 3, 3, 3, 3, - -1.0f, - -3.0f, -3.0f, 0.0f, 0.0f } + std::numeric_limits<float>::max() } }; for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { diff --git a/ui/gfx/size.cc b/ui/gfx/size.cc index 6d28ded..26bb973 100644 --- a/ui/gfx/size.cc +++ b/ui/gfx/size.cc @@ -8,9 +8,8 @@ #include <windows.h> #endif -#include "base/logging.h" #include "base/stringprintf.h" -#include "ui/gfx/size_base.h" +#include "ui/gfx/size_base_impl.h" namespace gfx { @@ -18,15 +17,13 @@ template class SizeBase<Size, int>; Size::Size() : SizeBase<Size, int>(0, 0) {} -Size::Size(int width, int height) : SizeBase<Size, int>(0, 0) { - set_width(width); - set_height(height); +Size::Size(int width, int height) + : SizeBase<Size, int>(width, height) { } #if defined(OS_MACOSX) -Size::Size(const CGSize& s) : SizeBase<Size, int>(0, 0) { - set_width(s.width); - set_height(s.height); +Size::Size(const CGSize& s) + : SizeBase<Size, int>(s.width, s.height) { } Size& Size::operator=(const CGSize& s) { diff --git a/ui/gfx/size_base.h b/ui/gfx/size_base.h index 0dbfabd..33051a3 100644 --- a/ui/gfx/size_base.h +++ b/ui/gfx/size_base.h @@ -32,8 +32,8 @@ class UI_EXPORT SizeBase { set_height(height_ + height); } - void set_width(Type width) { width_ = width; } - void set_height(Type height) { height_ = height; } + void set_width(Type width); + void set_height(Type height); void ClampToMax(const Class& max) { width_ = width_ <= max.width_ ? width_ : max.width_; @@ -46,20 +46,11 @@ class UI_EXPORT SizeBase { } bool IsEmpty() const { - return (width_ <= 0) || (height_ <= 0); - } - - void ClampToNonNegative() { - if (width_ < 0) - width_ = 0; - if (height_ < 0) - height_ = 0; + return (width_ == 0) || (height_ == 0); } protected: - SizeBase(Type width, Type height) - : width_(width), - height_(height) {} + SizeBase(Type width, Type height); // Destructor is intentionally made non virtual and protected. // Do not make this public. diff --git a/ui/gfx/size_base_impl.h b/ui/gfx/size_base_impl.h new file mode 100644 index 0000000..9e87468 --- /dev/null +++ b/ui/gfx/size_base_impl.h @@ -0,0 +1,37 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "ui/gfx/size_base.h" + +#include "base/logging.h" + +// This file provides the implementation for SizeBase template and +// used to instantiate the base class for Size and SizeF classes. +#if !defined(UI_IMPLEMENTATION) +#error "This file is intended for UI implementation only" +#endif + +namespace gfx { + +template<typename Class, typename Type> +void SizeBase<Class, Type>::set_width(Type width) { + DCHECK(!(width < 0)); + width_ = width < 0 ? 0 : width; +} + +template<typename Class, typename Type> +void SizeBase<Class, Type>::set_height(Type height) { + DCHECK(!(height < 0)); + height_ = height < 0 ? 0 : height; +} + +template<typename Class, typename Type> +SizeBase<Class, Type>::SizeBase(Type width, Type height) + : width_(width < 0 ? 0 : width), + height_(height < 0 ? 0 : height) { + DCHECK(!(width < 0)); + DCHECK(!(height < 0)); +} + +} // namespace gfx diff --git a/ui/gfx/size_f.cc b/ui/gfx/size_f.cc index 96c1867..0aad4ad 100644 --- a/ui/gfx/size_f.cc +++ b/ui/gfx/size_f.cc @@ -4,8 +4,8 @@ #include "ui/gfx/size_f.h" -#include "base/logging.h" #include "base/stringprintf.h" +#include "ui/gfx/size_base_impl.h" namespace gfx { @@ -13,9 +13,8 @@ template class SizeBase<SizeF, float>; SizeF::SizeF() : SizeBase<SizeF, float>(0, 0) {} -SizeF::SizeF(float width, float height) : SizeBase<SizeF, float>(0, 0) { - set_width(width); - set_height(height); +SizeF::SizeF(float width, float height) + : SizeBase<SizeF, float>(width, height) { } SizeF::~SizeF() {} diff --git a/ui/gfx/size_unittest.cc b/ui/gfx/size_unittest.cc index a6fda9d..e68bdd6 100644 --- a/ui/gfx/size_unittest.cc +++ b/ui/gfx/size_unittest.cc @@ -43,12 +43,6 @@ TEST(SizeTest, ToFlooredSize) { EXPECT_EQ(Size(10, 10), ToFlooredSize(SizeF(10.4999f, 10.4999f))); EXPECT_EQ(Size(10, 10), ToFlooredSize(SizeF(10.5f, 10.5f))); EXPECT_EQ(Size(10, 10), ToFlooredSize(SizeF(10.9999f, 10.9999f))); - - EXPECT_EQ(Size(-10, -10), ToFlooredSize(SizeF(-10, -10))); - EXPECT_EQ(Size(-11, -11), ToFlooredSize(SizeF(-10.0001f, -10.0001f))); - EXPECT_EQ(Size(-11, -11), ToFlooredSize(SizeF(-10.4999f, -10.4999f))); - EXPECT_EQ(Size(-11, -11), ToFlooredSize(SizeF(-10.5f, -10.5f))); - EXPECT_EQ(Size(-11, -11), ToFlooredSize(SizeF(-10.9999f, -10.9999f))); } TEST(SizeTest, ToCeiledSize) { @@ -63,12 +57,6 @@ TEST(SizeTest, ToCeiledSize) { EXPECT_EQ(Size(11, 11), ToCeiledSize(SizeF(10.4999f, 10.4999f))); EXPECT_EQ(Size(11, 11), ToCeiledSize(SizeF(10.5f, 10.5f))); EXPECT_EQ(Size(11, 11), ToCeiledSize(SizeF(10.9999f, 10.9999f))); - - EXPECT_EQ(Size(-10, -10), ToCeiledSize(SizeF(-10, -10))); - EXPECT_EQ(Size(-10, -10), ToCeiledSize(SizeF(-10.0001f, -10.0001f))); - EXPECT_EQ(Size(-10, -10), ToCeiledSize(SizeF(-10.4999f, -10.4999f))); - EXPECT_EQ(Size(-10, -10), ToCeiledSize(SizeF(-10.5f, -10.5f))); - EXPECT_EQ(Size(-10, -10), ToCeiledSize(SizeF(-10.9999f, -10.9999f))); } TEST(SizeTest, ToRoundedSize) { @@ -83,12 +71,6 @@ TEST(SizeTest, ToRoundedSize) { EXPECT_EQ(Size(10, 10), ToRoundedSize(SizeF(10.4999f, 10.4999f))); EXPECT_EQ(Size(11, 11), ToRoundedSize(SizeF(10.5f, 10.5f))); EXPECT_EQ(Size(11, 11), ToRoundedSize(SizeF(10.9999f, 10.9999f))); - - EXPECT_EQ(Size(-10, -10), ToRoundedSize(SizeF(-10, -10))); - EXPECT_EQ(Size(-10, -10), ToRoundedSize(SizeF(-10.0001f, -10.0001f))); - EXPECT_EQ(Size(-10, -10), ToRoundedSize(SizeF(-10.4999f, -10.4999f))); - EXPECT_EQ(Size(-11, -11), ToRoundedSize(SizeF(-10.5f, -10.5f))); - EXPECT_EQ(Size(-11, -11), ToRoundedSize(SizeF(-10.9999f, -10.9999f))); } TEST(SizeTest, ClampSize) { |