Flutter Engine
The Flutter Engine
size_f.h
Go to the documentation of this file.
1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef UI_GFX_GEOMETRY_SIZE_F_H_
6#define UI_GFX_GEOMETRY_SIZE_F_H_
7
8#include <iosfwd>
9#include <string>
10
12#include "gfx/gfx_export.h"
13#include "size.h"
14
15namespace gfx {
16
17// A floating version of gfx::Size.
19 public:
20 constexpr SizeF() : width_(0.f), height_(0.f) {}
21 constexpr SizeF(float width, float height)
22 : width_(clamp(width)), height_(clamp(height)) {}
23
24 constexpr explicit SizeF(const Size& size)
25 : SizeF(static_cast<float>(size.width()),
26 static_cast<float>(size.height())) {}
27
28 constexpr float width() const { return width_; }
29 constexpr float height() const { return height_; }
30
31 void set_width(float width) { width_ = clamp(width); }
32 void set_height(float height) { height_ = clamp(height); }
33
34 float GetArea() const;
35
36 void SetSize(float width, float height) {
37 set_width(width);
38 set_height(height);
39 }
40
41 void Enlarge(float grow_width, float grow_height);
42
43 void SetToMin(const SizeF& other);
44 void SetToMax(const SizeF& other);
45
46 bool IsEmpty() const { return !width() || !height(); }
47
48 void Scale(float scale) { Scale(scale, scale); }
49
50 void Scale(float x_scale, float y_scale) {
51 SetSize(width() * x_scale, height() * y_scale);
52 }
53
54 std::string ToString() const;
55
56 private:
57 static constexpr float kTrivial = 8.f * std::numeric_limits<float>::epsilon();
58
59 static constexpr float clamp(float f) { return f > kTrivial ? f : 0.f; }
60
61 float width_;
62 float height_;
63};
64
65inline bool operator==(const SizeF& lhs, const SizeF& rhs) {
66 return lhs.width() == rhs.width() && lhs.height() == rhs.height();
67}
68
69inline bool operator!=(const SizeF& lhs, const SizeF& rhs) {
70 return !(lhs == rhs);
71}
72
73GFX_EXPORT SizeF ScaleSize(const SizeF& p, float x_scale, float y_scale);
74
75inline SizeF ScaleSize(const SizeF& p, float scale) {
76 return ScaleSize(p, scale, scale);
77}
78
79// This is declared here for use in gtest-based unit tests but is defined in
80// the //ui/gfx:test_support target. Depend on that to use this in your unit
81// test. This should not be used in production code - call ToString() instead.
82void PrintTo(const SizeF& size, ::std::ostream* os);
83
84} // namespace gfx
85
86#endif // UI_GFX_GEOMETRY_SIZE_F_H_
static unsigned clamp(SkFixed fx, int max)
constexpr SizeF(const Size &size)
Definition: size_f.h:24
void set_width(float width)
Definition: size_f.h:31
bool IsEmpty() const
Definition: size_f.h:46
constexpr float height() const
Definition: size_f.h:29
constexpr SizeF(float width, float height)
Definition: size_f.h:21
void Scale(float scale)
Definition: size_f.h:48
void set_height(float height)
Definition: size_f.h:32
void SetSize(float width, float height)
Definition: size_f.h:36
constexpr SizeF()
Definition: size_f.h:20
constexpr float width() const
Definition: size_f.h:28
void Scale(float x_scale, float y_scale)
Definition: size_f.h:50
Definition: size.h:26
#define GFX_EXPORT
Definition: gfx_export.h:26
Definition: insets.cc:10
void PrintTo(const Point &point, ::std::ostream *os)
Definition: gfx_util.cc:74
SizeF ScaleSize(const SizeF &s, float x_scale, float y_scale)
Definition: size_f.cc:33
bool operator==(const Point &lhs, const Point &rhs)
Definition: point.h:96
constexpr size_t size(const T(&array)[N]) noexcept
bool operator!=(const Point &lhs, const Point &rhs)
Definition: point.h:100
static std::string ToString(CompilerBackend::Type type)
Definition: reflector.cc:559
int32_t height
int32_t width
const Scalar scale