File: int_size.h

package info (click to toggle)
chromium-browser 70.0.3538.110-1~deb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,619,476 kB
  • sloc: cpp: 13,024,755; ansic: 1,349,823; python: 916,672; xml: 314,489; java: 280,047; asm: 276,936; perl: 75,771; objc: 66,634; sh: 45,860; cs: 28,354; php: 11,064; makefile: 10,911; yacc: 9,109; tcl: 8,403; ruby: 4,065; lex: 1,779; pascal: 1,411; lisp: 1,055; awk: 41; jsp: 39; sed: 17; sql: 3
file content (167 lines) | stat: -rw-r--r-- 5,653 bytes parent folder | download
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
/*
 * Copyright (C) 2003, 2004, 2005, 2006 Apple Computer, Inc.  All rights
 * reserved.
 * Copyright (C) 2013 Google Inc. All rights reserved.
 *
 * 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_INT_SIZE_H_
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_GEOMETRY_INT_SIZE_H_

#include "build/build_config.h"
#include "third_party/blink/public/platform/web_common.h"
#include "third_party/blink/renderer/platform/platform_export.h"
#include "third_party/blink/renderer/platform/wtf/allocator.h"
#include "third_party/blink/renderer/platform/wtf/forward.h"

#if defined(OS_MACOSX)
typedef struct CGSize CGSize;

#ifdef __OBJC__
#import <Foundation/Foundation.h>
#endif
#endif

namespace gfx {
class Size;
class Vector2d;
}

namespace blink {

class PLATFORM_EXPORT IntSize {
  DISALLOW_NEW_EXCEPT_PLACEMENT_NEW();

 public:
  constexpr IntSize() : width_(0), height_(0) {}
  constexpr IntSize(int width, int height) : width_(width), height_(height) {}
  explicit IntSize(const gfx::Size&);

  constexpr int Width() const { return width_; }
  constexpr int Height() const { return height_; }

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

  constexpr bool IsEmpty() const { return width_ <= 0 || height_ <= 0; }
  constexpr bool IsZero() const { return !width_ && !height_; }

  float AspectRatio() const {
    return static_cast<float>(width_) / static_cast<float>(height_);
  }

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

  void Scale(float width_scale, float height_scale) {
    width_ = static_cast<int>(static_cast<float>(width_) * width_scale);
    height_ = static_cast<int>(static_cast<float>(height_) * height_scale);
  }

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

  IntSize ExpandedTo(const IntSize& other) const {
    return IntSize(width_ > other.width_ ? width_ : other.width_,
                   height_ > other.height_ ? height_ : other.height_);
  }

  IntSize ShrunkTo(const IntSize& other) const {
    return IntSize(width_ < other.width_ ? width_ : other.width_,
                   height_ < other.height_ ? height_ : other.height_);
  }

  void ClampNegativeToZero() { *this = ExpandedTo(IntSize()); }

  void ClampToMinimumSize(const IntSize& minimum_size) {
    if (width_ < minimum_size.Width())
      width_ = minimum_size.Width();
    if (height_ < minimum_size.Height())
      height_ = minimum_size.Height();
  }

  // Return area in a uint64_t to avoid overflow.
  uint64_t Area() const { return static_cast<uint64_t>(Width()) * Height(); }

  int DiagonalLengthSquared() const {
    return width_ * width_ + height_ * height_;
  }

  IntSize TransposedSize() const { return IntSize(height_, width_); }

#if defined(OS_MACOSX)
  explicit IntSize(const CGSize&);
  explicit operator CGSize() const;
#endif

  // Use this only for logical sizes, which can not be negative. Things that are
  // offsets instead, and can be negative, should use a gfx::Vector2d.
  explicit operator gfx::Size() const;
  // IntSize is used as an offset, which can be negative, but gfx::Size can not.
  // The Vector2d type is used for offsets instead.
  explicit operator gfx::Vector2d() const;

  String ToString() const;

 private:
  int width_, height_;
};

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

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

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

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

inline IntSize operator-(const IntSize& size) {
  return IntSize(-size.Width(), -size.Height());
}

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

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

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

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_PLATFORM_GEOMETRY_INT_SIZE_H_