summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/platform/geometry/double_size.h
blob: 917c81c4c52b083d9f1c1f5bd60d129cf5be373a (plain)
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
// Copyright 2014 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.

#ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_GEOMETRY_DOUBLE_SIZE_H_
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_GEOMETRY_DOUBLE_SIZE_H_

#include <iosfwd>
#include "third_party/blink/renderer/platform/geometry/float_size.h"
#include "third_party/blink/renderer/platform/geometry/int_size.h"
#include "third_party/blink/renderer/platform/wtf/allocator.h"
#include "third_party/blink/renderer/platform/wtf/forward.h"

namespace blink {

class LayoutSize;

class PLATFORM_EXPORT DoubleSize {
  DISALLOW_NEW();

 public:
  DoubleSize() : width_(0), height_(0) {}
  DoubleSize(double width, double height) : width_(width), height_(height) {}
  explicit DoubleSize(const IntSize& p)
      : width_(p.Width()), height_(p.Height()) {}
  DoubleSize(const FloatSize& s) : width_(s.Width()), height_(s.Height()) {}
  explicit DoubleSize(const LayoutSize&);

  double Width() const { return width_; }
  double Height() const { return height_; }

  void SetWidth(double width) { width_ = width; }
  void SetHeight(double height) { height_ = height; }

  bool IsEmpty() const { return width_ <= 0 || height_ <= 0; }

  bool IsZero() const;

  void Expand(float width, float height) {
    width_ += width;
    height_ += height;
  }

  void Scale(float width_scale, float height_scale) {
    width_ = width_ * width_scale;
    height_ = height_ * height_scale;
  }

  void Scale(float scale) { this->Scale(scale, scale); }

  String ToString() const;

 private:
  double width_, height_;
};

inline DoubleSize& operator+=(DoubleSize& a, const DoubleSize& b) {
  a.SetWidth(a.Width() + b.Width());
  a.SetHeight(a.Height() + b.Height());
  return a;
}

inline DoubleSize& operator-=(DoubleSize& a, const DoubleSize& b) {
  a.SetWidth(a.Width() - b.Width());
  a.SetHeight(a.Height() - b.Height());
  return a;
}

inline DoubleSize operator+(const DoubleSize& a, const DoubleSize& b) {
  return DoubleSize(a.Width() + b.Width(), a.Height() + b.Height());
}

inline DoubleSize operator-(const DoubleSize& a, const DoubleSize& b) {
  return DoubleSize(a.Width() - b.Width(), a.Height() - b.Height());
}

inline bool operator==(const DoubleSize& a, const DoubleSize& b) {
  return a.Width() == b.Width() && a.Height() == b.Height();
}

inline bool operator!=(const DoubleSize& a, const DoubleSize& b) {
  return a.Width() != b.Width() || a.Height() != b.Height();
}

inline IntSize FlooredIntSize(const DoubleSize& p) {
  return IntSize(clampTo<int>(floor(p.Width())),
                 clampTo<int>(floor(p.Height())));
}

inline IntSize RoundedIntSize(const DoubleSize& p) {
  return IntSize(clampTo<int>(round(p.Width())),
                 clampTo<int>(round(p.Height())));
}

inline IntSize ExpandedIntSize(const DoubleSize& p) {
  return IntSize(clampTo<int>(ceil(p.Width())), clampTo<int>(ceil(p.Height())));
}

inline FloatSize ToFloatSize(const DoubleSize& p) {
  return FloatSize(p.Width(), p.Height());
}

PLATFORM_EXPORT std::ostream& operator<<(std::ostream&, const DoubleSize&);

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_PLATFORM_GEOMETRY_DOUBLE_SIZE_H_