1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
|
/*
* Copyright (C) 2003, 2006 Apple Computer, Inc. All rights reserved.
* Copyright (C) 2005 Nokia. All rights reserved.
* 2008 Eric Seidel <eric@webkit.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_GEOMETRY_FLOAT_SIZE_H_
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_GEOMETRY_FLOAT_SIZE_H_
#include <iosfwd>
#include "build/build_config.h"
#include "third_party/blink/renderer/platform/geometry/int_point.h"
#include "third_party/blink/renderer/platform/wtf/allocator.h"
#include "third_party/blink/renderer/platform/wtf/forward.h"
#include "third_party/blink/renderer/platform/wtf/math_extras.h"
#if defined(OS_MACOSX)
typedef struct CGSize CGSize;
#ifdef __OBJC__
#import <Foundation/Foundation.h>
#endif
#endif
struct SkSize;
namespace gfx {
class SizeF;
class Vector2dF;
} // namespace gfx
namespace blink {
class IntSize;
class LayoutSize;
class PLATFORM_EXPORT FloatSize {
DISALLOW_NEW();
public:
constexpr FloatSize() : width_(0), height_(0) {}
constexpr FloatSize(float width, float height)
: width_(width), height_(height) {}
explicit FloatSize(const IntSize& size)
: width_(size.Width()), height_(size.Height()) {}
FloatSize(const SkSize&);
explicit FloatSize(const LayoutSize&);
static FloatSize NarrowPrecision(double width, double height);
constexpr float Width() const { return width_; }
constexpr float Height() const { return height_; }
constexpr void SetWidth(float width) { width_ = width; }
constexpr void SetHeight(float height) { height_ = height; }
constexpr bool IsEmpty() const { return width_ <= 0 || height_ <= 0; }
constexpr bool IsZero() const {
// Not using fabs as it is not a constexpr in LLVM libc++
return -std::numeric_limits<float>::epsilon() < width_ &&
width_ < std::numeric_limits<float>::epsilon() &&
-std::numeric_limits<float>::epsilon() < height_ &&
height_ < std::numeric_limits<float>::epsilon();
}
bool IsExpressibleAsIntSize() const;
float AspectRatio() const { return width_ / height_; }
float Area() const { return width_ * height_; }
void Expand(float width, float height) {
width_ += width;
height_ += height;
}
void Scale(float s) { Scale(s, s); }
void Scale(float scale_x, float scale_y) {
width_ *= scale_x;
height_ *= scale_y;
}
void ScaleAndFloor(float scale) {
width_ = floorf(width_ * scale);
height_ = floorf(height_ * scale);
}
FloatSize ExpandedTo(const FloatSize& other) const {
return FloatSize(width_ > other.width_ ? width_ : other.width_,
height_ > other.height_ ? height_ : other.height_);
}
FloatSize ShrunkTo(const FloatSize& other) const {
return FloatSize(width_ < other.width_ ? width_ : other.width_,
height_ < other.height_ ? height_ : other.height_);
}
void ClampNegativeToZero() { *this = ExpandedTo(FloatSize()); }
float DiagonalLength() const;
float DiagonalLengthSquared() const {
return width_ * width_ + height_ * height_;
}
FloatSize TransposedSize() const { return FloatSize(height_, width_); }
FloatSize ScaledBy(float scale) const { return ScaledBy(scale, scale); }
FloatSize ScaledBy(float scale_x, float scale_y) const {
return FloatSize(width_ * scale_x, height_ * scale_y);
}
#if defined(OS_MACOSX)
explicit FloatSize(
const CGSize&); // don't do this implicitly since it's lossy
operator CGSize() const;
#endif
operator SkSize() const;
// Use this only for logical sizes, which can not be negative. Things that are
// offsets instead, and can be negative, should use a gfx::Vector2dF.
explicit operator gfx::SizeF() const;
// FloatSize is used as an offset, which can be negative, but gfx::SizeF can
// not. The Vector2dF type is used for offsets instead.
explicit operator gfx::Vector2dF() const;
String ToString() const;
private:
float width_, height_;
};
inline FloatSize& operator+=(FloatSize& a, const FloatSize& b) {
a.SetWidth(a.Width() + b.Width());
a.SetHeight(a.Height() + b.Height());
return a;
}
constexpr FloatSize& operator-=(FloatSize& a, const FloatSize& b) {
a.SetWidth(a.Width() - b.Width());
a.SetHeight(a.Height() - b.Height());
return a;
}
constexpr FloatSize operator+(const FloatSize& a, const FloatSize& b) {
return FloatSize(a.Width() + b.Width(), a.Height() + b.Height());
}
constexpr FloatSize operator-(const FloatSize& a, const FloatSize& b) {
return FloatSize(a.Width() - b.Width(), a.Height() - b.Height());
}
constexpr FloatSize operator-(const FloatSize& size) {
return FloatSize(-size.Width(), -size.Height());
}
constexpr FloatSize operator*(const FloatSize& a, const float b) {
return FloatSize(a.Width() * b, a.Height() * b);
}
constexpr FloatSize operator*(const float a, const FloatSize& b) {
return FloatSize(a * b.Width(), a * b.Height());
}
constexpr bool operator==(const FloatSize& a, const FloatSize& b) {
return a.Width() == b.Width() && a.Height() == b.Height();
}
constexpr bool operator!=(const FloatSize& a, const FloatSize& b) {
return !(a == b);
}
inline IntSize RoundedIntSize(const FloatSize& p) {
return IntSize(clampTo<int>(roundf(p.Width())),
clampTo<int>(roundf(p.Height())));
}
inline IntSize FlooredIntSize(const FloatSize& p) {
return IntSize(clampTo<int>(floorf(p.Width())),
clampTo<int>(floorf(p.Height())));
}
inline IntSize ExpandedIntSize(const FloatSize& p) {
return IntSize(clampTo<int>(ceilf(p.Width())),
clampTo<int>(ceilf(p.Height())));
}
inline IntPoint FlooredIntPoint(const FloatSize& p) {
return IntPoint(clampTo<int>(floorf(p.Width())),
clampTo<int>(floorf(p.Height())));
}
PLATFORM_EXPORT std::ostream& operator<<(std::ostream&, const FloatSize&);
PLATFORM_EXPORT WTF::TextStream& operator<<(WTF::TextStream&, const FloatSize&);
} // namespace blink
// Allows this class to be stored in a HeapVector.
WTF_ALLOW_CLEAR_UNUSED_SLOTS_WITH_MEM_FUNCTIONS(blink::FloatSize);
#endif // THIRD_PARTY_BLINK_RENDERER_PLATFORM_GEOMETRY_FLOAT_SIZE_H_
|