summaryrefslogtreecommitdiff
path: root/chromium/ui/gfx/selection_bound.cc
blob: 4e341072cd12f6e36cf78e8350d98f9d73f384c6 (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// 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.

#include <algorithm>

#include "base/macros.h"
#include "base/strings/stringprintf.h"
#include "ui/gfx/geometry/point_conversions.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/rect_f.h"
#include "ui/gfx/selection_bound.h"

namespace gfx {

SelectionBound::SelectionBound() : type_(EMPTY), visible_(false) {}

SelectionBound::SelectionBound(const SelectionBound& other) = default;

SelectionBound::~SelectionBound() {}

void SelectionBound::SetEdgeStart(const gfx::PointF& value) {
  edge_start_ = value;
  edge_start_rounded_ = gfx::ToRoundedPoint(value);
}

void SelectionBound::SetVisibleEdgeStart(const gfx::PointF& value) {
  visible_edge_start_ = value;
}

void SelectionBound::SetEdgeEnd(const gfx::PointF& value) {
  edge_end_ = value;
  edge_end_rounded_ = gfx::ToRoundedPoint(value);
}

void SelectionBound::SetVisibleEdgeEnd(const gfx::PointF& value) {
  visible_edge_end_ = value;
}

void SelectionBound::SetEdge(const gfx::PointF& start, const gfx::PointF& end) {
  SetEdgeStart(start);
  SetEdgeEnd(end);
}

void SelectionBound::SetVisibleEdge(const gfx::PointF& start,
                                    const gfx::PointF& end) {
  SetVisibleEdgeStart(start);
  SetVisibleEdgeEnd(end);
}

int SelectionBound::GetHeight() const {
  return edge_end_rounded_.y() - edge_start_rounded_.y();
}

std::string SelectionBound::ToString() const {
  return base::StringPrintf(
      "SelectionBound(%s, %s, %s, %s, %d)", edge_start_.ToString().c_str(),
      edge_end_.ToString().c_str(), edge_start_rounded_.ToString().c_str(),
      edge_end_rounded_.ToString().c_str(), visible_);
}

bool operator==(const SelectionBound& lhs, const SelectionBound& rhs) {
  return lhs.type() == rhs.type() && lhs.visible() == rhs.visible() &&
         lhs.edge_start() == rhs.edge_start() &&
         lhs.edge_end() == rhs.edge_end() &&
         lhs.visible_edge_start() == rhs.visible_edge_start() &&
         lhs.visible_edge_end() == rhs.visible_edge_end();
}

bool operator!=(const SelectionBound& lhs, const SelectionBound& rhs) {
  return !(lhs == rhs);
}

gfx::Rect RectBetweenSelectionBounds(const SelectionBound& b1,
                                     const SelectionBound& b2) {
  gfx::Point top_left(b1.edge_start_rounded());
  top_left.SetToMin(b1.edge_end_rounded());
  top_left.SetToMin(b2.edge_start_rounded());
  top_left.SetToMin(b2.edge_end_rounded());

  gfx::Point bottom_right(b1.edge_start_rounded());
  bottom_right.SetToMax(b1.edge_end_rounded());
  bottom_right.SetToMax(b2.edge_start_rounded());
  bottom_right.SetToMax(b2.edge_end_rounded());

  gfx::Vector2d diff = bottom_right - top_left;
  return gfx::Rect(top_left, gfx::Size(diff.x(), diff.y()));
}

gfx::RectF RectFBetweenSelectionBounds(const SelectionBound& b1,
                                       const SelectionBound& b2) {
  gfx::PointF top_left(b1.edge_start());
  top_left.SetToMin(b1.edge_end());
  top_left.SetToMin(b2.edge_start());
  top_left.SetToMin(b2.edge_end());

  gfx::PointF bottom_right(b1.edge_start());
  bottom_right.SetToMax(b1.edge_end());
  bottom_right.SetToMax(b2.edge_start());
  bottom_right.SetToMax(b2.edge_end());

  gfx::Vector2dF diff = bottom_right - top_left;
  return gfx::RectF(top_left, gfx::SizeF(diff.x(), diff.y()));
}

gfx::RectF RectFBetweenVisibleSelectionBounds(const SelectionBound& b1,
                                              const SelectionBound& b2) {
  // The selection bound is determined by the |start| and |end| points. For the
  // writing-mode is vertical-*, the bounds are horizontal, the |end| might
  // be on the left side of the |start|.
  gfx::PointF top_left(b1.visible_edge_start());
  top_left.SetToMin(b1.visible_edge_end());
  top_left.SetToMin(b2.visible_edge_start());
  top_left.SetToMin(b2.visible_edge_end());

  gfx::PointF bottom_right(b1.visible_edge_start());
  bottom_right.SetToMax(b1.visible_edge_end());
  bottom_right.SetToMax(b2.visible_edge_start());
  bottom_right.SetToMax(b2.visible_edge_end());

  gfx::Vector2dF diff = bottom_right - top_left;
  return gfx::RectF(top_left, gfx::SizeF(diff.x(), diff.y()));
}

}  // namespace gfx