Flutter Engine
The Flutter Engine
size.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_H_
6#define UI_GFX_GEOMETRY_SIZE_H_
7
8#include <algorithm>
9#include <iosfwd>
10#include <string>
11
15#include "gfx/gfx_export.h"
16
17#if defined(OS_WIN)
18typedef struct tagSIZE SIZE;
19#elif defined(OS_APPLE)
20typedef struct CGSize CGSize;
21#endif
22
23namespace gfx {
24
25// A size has width and height values.
27 public:
28 constexpr Size() : width_(0), height_(0) {}
29 constexpr Size(int width, int height)
30 : width_(std::max(0, width)), height_(std::max(0, height)) {}
31#if defined(OS_APPLE)
32 explicit Size(const CGSize& s);
33#endif
34
35#if defined(OS_APPLE)
36 Size& operator=(const CGSize& s);
37#endif
38
39 void operator+=(const Size& size);
40
41 void operator-=(const Size& size);
42
43#if defined(OS_WIN)
44 SIZE ToSIZE() const;
45#elif defined(OS_APPLE)
46 CGSize ToCGSize() const;
47#endif
48
49 constexpr int width() const { return width_; }
50 constexpr int height() const { return height_; }
51
52 void set_width(int width) { width_ = std::max(0, width); }
53 void set_height(int height) { height_ = std::max(0, height); }
54
55 // This call will CHECK if the area of this size would overflow int.
56 int GetArea() const;
57 // Returns a checked numeric representation of the area.
58 base::CheckedNumeric<int> GetCheckedArea() const;
59
60 void SetSize(int width, int height) {
61 set_width(width);
62 set_height(height);
63 }
64
65 void Enlarge(int grow_width, int grow_height);
66
67 void SetToMin(const Size& other);
68 void SetToMax(const Size& other);
69
70 bool IsEmpty() const { return !width() || !height(); }
71
72 std::string ToString() const;
73
74 private:
75 int width_;
76 int height_;
77};
78
79inline bool operator==(const Size& lhs, const Size& rhs) {
80 return lhs.width() == rhs.width() && lhs.height() == rhs.height();
81}
82
83inline bool operator!=(const Size& lhs, const Size& rhs) {
84 return !(lhs == rhs);
85}
86
87inline Size operator+(Size lhs, const Size& rhs) {
88 lhs += rhs;
89 return lhs;
90}
91
92inline Size operator-(Size lhs, const Size& rhs) {
93 lhs -= rhs;
94 return lhs;
95}
96
97// This is declared here for use in gtest-based unit tests but is defined in
98// the //ui/gfx:test_support target. Depend on that to use this in your unit
99// test. This should not be used in production code - call ToString() instead.
100void PrintTo(const Size& size, ::std::ostream* os);
101
102// Helper methods to scale a gfx::Size to a new gfx::Size.
103GFX_EXPORT Size ScaleToCeiledSize(const Size& size, float x_scale, float y_scale);
105GFX_EXPORT Size ScaleToFlooredSize(const Size& size, float x_scale, float y_scale);
107GFX_EXPORT Size ScaleToRoundedSize(const Size& size, float x_scale, float y_scale);
109
110} // namespace gfx
111
112#endif // UI_GFX_GEOMETRY_SIZE_H_
Definition: size.h:26
void set_height(int height)
Definition: size.h:53
bool IsEmpty() const
Definition: size.h:70
constexpr Size()
Definition: size.h:28
void set_width(int width)
Definition: size.h:52
constexpr int height() const
Definition: size.h:50
constexpr Size(int width, int height)
Definition: size.h:29
void SetSize(int width, int height)
Definition: size.h:60
constexpr int width() const
Definition: size.h:49
struct MyStruct s
#define GFX_EXPORT
Definition: gfx_export.h:26
static float max(float r, float g, float b)
Definition: hsl.cpp:49
Definition: insets.cc:10
Insets operator+(Insets lhs, const Insets &rhs)
Definition: insets.h:175
void PrintTo(const Point &point, ::std::ostream *os)
Definition: gfx_util.cc:74
Insets operator-(Insets lhs, const Insets &rhs)
Definition: insets.h:180
bool operator==(const Point &lhs, const Point &rhs)
Definition: point.h:96
Size ScaleToCeiledSize(const Size &size, float x_scale, float y_scale)
Definition: size.cc:84
Size ScaleToFlooredSize(const Size &size, float x_scale, float y_scale)
Definition: size.cc:96
constexpr size_t size(const T(&array)[N]) noexcept
bool operator!=(const Point &lhs, const Point &rhs)
Definition: point.h:100
Size ScaleToRoundedSize(const Size &size, float x_scale, float y_scale)
Definition: size.cc:108
static std::string ToString(CompilerBackend::Type type)
Definition: reflector.cc:559
TSize< Scalar > Size
Definition: size.h:137
SINT Vec< N, T > & operator-=(Vec< N, T > &x, const Vec< N, T > &y)
Definition: SkVx.h:451
SINT Vec< N, T > & operator+=(Vec< N, T > &x, const Vec< N, T > &y)
Definition: SkVx.h:450
Definition: ref_ptr.h:256
#define SIZE
int32_t height
int32_t width
const Scalar scale