summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/layout/ng/geometry/ng_logical_rect.cc
blob: 0c3b40cc7080b552f9f663f48d722736e51227e9 (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
// Copyright 2017 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.

#include "third_party/blink/renderer/core/layout/ng/geometry/ng_logical_rect.h"

#include <algorithm>
#include "third_party/blink/renderer/platform/geometry/layout_rect.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"

namespace blink {

namespace {

inline NGLogicalOffset Min(NGLogicalOffset a, NGLogicalOffset b) {
  return {std::min(a.inline_offset, b.inline_offset),
          std::min(a.block_offset, b.block_offset)};
}

inline NGLogicalOffset Max(NGLogicalOffset a, NGLogicalOffset b) {
  return {std::max(a.inline_offset, b.inline_offset),
          std::max(a.block_offset, b.block_offset)};
}

}  // namespace

NGLogicalRect::NGLogicalRect(const LayoutRect& source)
    : NGLogicalRect({source.X(), source.Y()},
                    {source.Width(), source.Height()}) {}

LayoutRect NGLogicalRect::ToLayoutRect() const {
  return {offset.inline_offset, offset.block_offset, size.inline_size,
          size.block_size};
}

bool NGLogicalRect::operator==(const NGLogicalRect& other) const {
  return other.offset == offset && other.size == size;
}

NGLogicalRect NGLogicalRect::operator+(const NGLogicalOffset& offset) const {
  return {this->offset + offset, size};
}

void NGLogicalRect::Unite(const NGLogicalRect& other) {
  if (other.IsEmpty())
    return;
  if (IsEmpty()) {
    *this = other;
    return;
  }

  NGLogicalOffset new_end_offset(Max(EndOffset(), other.EndOffset()));
  offset = Min(offset, other.offset);
  size = new_end_offset - offset;
}

String NGLogicalRect::ToString() const {
  return String::Format("%s,%s %sx%s",
                        offset.inline_offset.ToString().Ascii().data(),
                        offset.block_offset.ToString().Ascii().data(),
                        size.inline_size.ToString().Ascii().data(),
                        size.block_size.ToString().Ascii().data());
}

std::ostream& operator<<(std::ostream& os, const NGLogicalRect& value) {
  return os << value.ToString();
}

}  // namespace blink