File: style_ray.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (138 lines) | stat: -rw-r--r-- 5,033 bytes parent folder | download | duplicates (3)
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
// Copyright 2017 The Chromium Authors
// 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/style/style_ray.h"

#include "third_party/blink/renderer/core/style/basic_shapes.h"
#include "third_party/blink/renderer/platform/geometry/path.h"
#include "ui/gfx/geometry/point_f.h"
#include "ui/gfx/geometry/rect_f.h"
#include "ui/gfx/geometry/size_f.h"

namespace blink {

StyleRay::StyleRay(float angle,
                   RaySize size,
                   bool contain,
                   const BasicShapeCenterCoordinate& center_x,
                   const BasicShapeCenterCoordinate& center_y,
                   bool has_explicit_center)
    : angle_(angle),
      size_(size),
      contain_(contain),
      center_x_(center_x),
      center_y_(center_y),
      has_explicit_center_(has_explicit_center) {}

bool StyleRay::IsEqualAssumingSameType(const BasicShape& o) const {
  const StyleRay& other = To<StyleRay>(o);
  return angle_ == other.angle_ && size_ == other.size_ &&
         contain_ == other.contain_ && center_x_ == other.center_x_ &&
         center_y_ == other.center_y_ &&
         has_explicit_center_ == other.has_explicit_center_;
}

Path StyleRay::GetPath(const gfx::RectF&, float, float) const {
  // ComputedStyle::ApplyMotionPathTransform cannot call GetPath
  // for rays as they may have infinite length.
  NOTREACHED();
}

namespace {

float CalculatePerpendicularDistanceToReferenceBoxSide(
    const gfx::PointF& point,
    const gfx::SizeF& reference_box_size,
    float (*comp)(std::initializer_list<float>)) {
  return comp(
      {std::abs(point.x()), std::abs(point.x() - reference_box_size.width()),
       std::abs(point.y()), std::abs(point.y() - reference_box_size.height())});
}

float CalculateDistance(const gfx::PointF& a, const gfx::PointF& b) {
  return (a - b).Length();
}

float CalculateDistanceToReferenceBoxCorner(
    const gfx::PointF& point,
    const gfx::SizeF& box_size,
    float (*comp)(std::initializer_list<float>)) {
  return comp({CalculateDistance(point, {0, 0}),
               CalculateDistance(point, {box_size.width(), 0}),
               CalculateDistance(point, {box_size.width(), box_size.height()}),
               CalculateDistance(point, {0, box_size.height()})});
}

float CalculateDistanceToReferenceBoxSide(
    const gfx::PointF& point,
    const float angle,
    const gfx::SizeF& reference_box_size) {
  if (!gfx::RectF(reference_box_size).InclusiveContains(point)) {
    return 0;
  }
  const float theta = Deg2rad(angle);
  float cos_t = std::cos(theta);
  float sin_t = std::sin(theta);
  // We are looking for % point, let's swap signs and lines
  // so that we end up in situation like this:
  //         (0, 0) #--------------%--# (box.width, 0)
  //                |        |    /   |
  //                |        v   /    |
  //                |        |  /     |
  //                |        |t/      |
  //                |        |/       |
  //                 (point) *---h----* (box.width, point.y)
  //                |        |        |
  //                |        |        |
  // (0, box.height)#-----------------# (box.width, box.height)

  // cos_t and sin_t swapped due to the 0 angle is pointing up.
  const float vertical =
      cos_t >= 0 ? point.y() : reference_box_size.height() - point.y();
  const float horizontal =
      sin_t >= 0 ? reference_box_size.width() - point.x() : point.x();
  cos_t = std::abs(cos_t);
  sin_t = std::abs(sin_t);
  // Check what side we hit.
  if (vertical * sin_t > horizontal * cos_t) {
    return horizontal / sin_t;
  }
  return vertical / cos_t;
}

}  // namespace

float StyleRay::CalculateRayPathLength(
    const gfx::PointF& starting_point,
    const gfx::SizeF& reference_box_size) const {
  switch (Size()) {
    case StyleRay::RaySize::kClosestSide:
      return CalculatePerpendicularDistanceToReferenceBoxSide(
          starting_point, reference_box_size, std::min);
    case StyleRay::RaySize::kFarthestSide:
      return CalculatePerpendicularDistanceToReferenceBoxSide(
          starting_point, reference_box_size, std::max);
    case StyleRay::RaySize::kClosestCorner:
      return CalculateDistanceToReferenceBoxCorner(
          starting_point, reference_box_size, std::min);
    case StyleRay::RaySize::kFarthestCorner:
      return CalculateDistanceToReferenceBoxCorner(
          starting_point, reference_box_size, std::max);
    case StyleRay::RaySize::kSides:
      return CalculateDistanceToReferenceBoxSide(starting_point, Angle(),
                                                 reference_box_size);
  }
}

PointAndTangent StyleRay::PointAndNormalAtLength(
    const gfx::PointF& starting_point,
    float length) const {
  const float angle = Angle() - 90;
  const float rad = Deg2rad(angle);
  const float x = starting_point.x() + length * std::cos(rad);
  const float y = starting_point.y() + length * std::sin(rad);
  return {{x, y}, angle};
}

}  // namespace blink