selection_bound.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright 2014 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. #include <algorithm>
  5. #include "base/strings/stringprintf.h"
  6. #include "ui/gfx/geometry/point_conversions.h"
  7. #include "ui/gfx/geometry/rect.h"
  8. #include "ui/gfx/geometry/rect_f.h"
  9. #include "ui/gfx/selection_bound.h"
  10. namespace gfx {
  11. SelectionBound::SelectionBound() : type_(EMPTY), visible_(false) {}
  12. SelectionBound::SelectionBound(const SelectionBound& other) = default;
  13. SelectionBound::~SelectionBound() {}
  14. void SelectionBound::SetEdgeStart(const gfx::PointF& value) {
  15. edge_start_ = value;
  16. edge_start_rounded_ = gfx::ToRoundedPoint(value);
  17. }
  18. void SelectionBound::SetVisibleEdgeStart(const gfx::PointF& value) {
  19. visible_edge_start_ = value;
  20. }
  21. void SelectionBound::SetEdgeEnd(const gfx::PointF& value) {
  22. edge_end_ = value;
  23. edge_end_rounded_ = gfx::ToRoundedPoint(value);
  24. }
  25. void SelectionBound::SetVisibleEdgeEnd(const gfx::PointF& value) {
  26. visible_edge_end_ = value;
  27. }
  28. void SelectionBound::SetEdge(const gfx::PointF& start, const gfx::PointF& end) {
  29. SetEdgeStart(start);
  30. SetEdgeEnd(end);
  31. }
  32. void SelectionBound::SetVisibleEdge(const gfx::PointF& start,
  33. const gfx::PointF& end) {
  34. SetVisibleEdgeStart(start);
  35. SetVisibleEdgeEnd(end);
  36. }
  37. int SelectionBound::GetHeight() const {
  38. return edge_end_rounded_.y() - edge_start_rounded_.y();
  39. }
  40. std::string SelectionBound::ToString() const {
  41. return base::StringPrintf(
  42. "SelectionBound(%s, %s, %s, %s, %d)", edge_start_.ToString().c_str(),
  43. edge_end_.ToString().c_str(), edge_start_rounded_.ToString().c_str(),
  44. edge_end_rounded_.ToString().c_str(), visible_);
  45. }
  46. bool operator==(const SelectionBound& lhs, const SelectionBound& rhs) {
  47. return lhs.type() == rhs.type() && lhs.visible() == rhs.visible() &&
  48. lhs.edge_start() == rhs.edge_start() &&
  49. lhs.edge_end() == rhs.edge_end() &&
  50. lhs.visible_edge_start() == rhs.visible_edge_start() &&
  51. lhs.visible_edge_end() == rhs.visible_edge_end();
  52. }
  53. bool operator!=(const SelectionBound& lhs, const SelectionBound& rhs) {
  54. return !(lhs == rhs);
  55. }
  56. gfx::Rect RectBetweenSelectionBounds(const SelectionBound& b1,
  57. const SelectionBound& b2) {
  58. gfx::Point top_left(b1.edge_start_rounded());
  59. top_left.SetToMin(b1.edge_end_rounded());
  60. top_left.SetToMin(b2.edge_start_rounded());
  61. top_left.SetToMin(b2.edge_end_rounded());
  62. gfx::Point bottom_right(b1.edge_start_rounded());
  63. bottom_right.SetToMax(b1.edge_end_rounded());
  64. bottom_right.SetToMax(b2.edge_start_rounded());
  65. bottom_right.SetToMax(b2.edge_end_rounded());
  66. gfx::Vector2d diff = bottom_right - top_left;
  67. return gfx::Rect(top_left, gfx::Size(diff.x(), diff.y()));
  68. }
  69. gfx::RectF RectFBetweenSelectionBounds(const SelectionBound& b1,
  70. const SelectionBound& b2) {
  71. gfx::PointF top_left(b1.edge_start());
  72. top_left.SetToMin(b1.edge_end());
  73. top_left.SetToMin(b2.edge_start());
  74. top_left.SetToMin(b2.edge_end());
  75. gfx::PointF bottom_right(b1.edge_start());
  76. bottom_right.SetToMax(b1.edge_end());
  77. bottom_right.SetToMax(b2.edge_start());
  78. bottom_right.SetToMax(b2.edge_end());
  79. gfx::Vector2dF diff = bottom_right - top_left;
  80. return gfx::RectF(top_left, gfx::SizeF(diff.x(), diff.y()));
  81. }
  82. gfx::RectF RectFBetweenVisibleSelectionBounds(const SelectionBound& b1,
  83. const SelectionBound& b2) {
  84. // The selection bound is determined by the |start| and |end| points. For the
  85. // writing-mode is vertical-*, the bounds are horizontal, the |end| might
  86. // be on the left side of the |start|.
  87. gfx::PointF top_left(b1.visible_edge_start());
  88. top_left.SetToMin(b1.visible_edge_end());
  89. top_left.SetToMin(b2.visible_edge_start());
  90. top_left.SetToMin(b2.visible_edge_end());
  91. gfx::PointF bottom_right(b1.visible_edge_start());
  92. bottom_right.SetToMax(b1.visible_edge_end());
  93. bottom_right.SetToMax(b2.visible_edge_start());
  94. bottom_right.SetToMax(b2.visible_edge_end());
  95. gfx::Vector2dF diff = bottom_right - top_left;
  96. return gfx::RectF(top_left, gfx::SizeF(diff.x(), diff.y()));
  97. }
  98. } // namespace gfx