File: dom_quad.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 (129 lines) | stat: -rw-r--r-- 4,439 bytes parent folder | download | duplicates (4)
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
// Copyright 2016 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/geometry/dom_quad.h"

#include "third_party/blink/renderer/bindings/core/v8/v8_dom_point_init.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_dom_quad_init.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_dom_rect_init.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_object_builder.h"
#include "third_party/blink/renderer/core/geometry/dom_point.h"
#include "third_party/blink/renderer/core/geometry/dom_rect.h"
#include "third_party/blink/renderer/core/geometry/geometry_util.h"

namespace blink {
namespace {

class DOMQuadPoint final : public DOMPoint {
 public:
  static DOMQuadPoint* Create(double x,
                              double y,
                              double z,
                              double w,
                              DOMQuad* quad) {
    return MakeGarbageCollected<DOMQuadPoint>(x, y, z, w, quad);
  }

  static DOMQuadPoint* FromPoint(const DOMPointInit* other, DOMQuad* quad) {
    return MakeGarbageCollected<DOMQuadPoint>(other->x(), other->y(),
                                              other->z(), other->w(), quad);
  }

  DOMQuadPoint(double x, double y, double z, double w, DOMQuad* quad)
      : DOMPoint(x, y, z, w), quad_(quad) {}

  void setX(double x) override {
    DOMPoint::setX(x);
    if (quad_)
      quad_->set_needs_bounds_calculation(true);
  }

  void setY(double y) override {
    DOMPoint::setY(y);
    if (quad_)
      quad_->set_needs_bounds_calculation(true);
  }

  void Trace(Visitor* visitor) const override {
    visitor->Trace(quad_);
    DOMPoint::Trace(visitor);
  }

 private:
  WeakMember<DOMQuad> quad_;
};

double NanSafeMin4(double a, double b, double c, double d) {
  using geometry_util::NanSafeMin;
  return NanSafeMin(NanSafeMin(a, b), NanSafeMin(c, d));
}

double NanSafeMax4(double a, double b, double c, double d) {
  using geometry_util::NanSafeMax;
  return NanSafeMax(NanSafeMax(a, b), NanSafeMax(c, d));
}

}  // namespace

DOMQuad* DOMQuad::Create(const DOMPointInit* p1,
                         const DOMPointInit* p2,
                         const DOMPointInit* p3,
                         const DOMPointInit* p4) {
  return MakeGarbageCollected<DOMQuad>(p1, p2, p3, p4);
}

DOMQuad* DOMQuad::fromRect(const DOMRectInit* other) {
  return MakeGarbageCollected<DOMQuad>(other->x(), other->y(), other->width(),
                                       other->height());
}

DOMQuad* DOMQuad::fromQuad(const DOMQuadInit* other) {
  return MakeGarbageCollected<DOMQuad>(
      other->hasP1() ? other->p1() : DOMPointInit::Create(),
      other->hasP2() ? other->p2() : DOMPointInit::Create(),
      other->hasP3() ? other->p3() : DOMPointInit::Create(),
      other->hasP4() ? other->p4() : DOMPointInit::Create());
}

DOMRect* DOMQuad::getBounds() {
  if (needs_bounds_calculation_)
    CalculateBounds();
  return DOMRect::Create(x_, y_, width_, height_);
}

void DOMQuad::CalculateBounds() {
  x_ = NanSafeMin4(p1()->x(), p2()->x(), p3()->x(), p4()->x());
  y_ = NanSafeMin4(p1()->y(), p2()->y(), p3()->y(), p4()->y());
  width_ = NanSafeMax4(p1()->x(), p2()->x(), p3()->x(), p4()->x()) - x_;
  height_ = NanSafeMax4(p1()->y(), p2()->y(), p3()->y(), p4()->y()) - y_;
  needs_bounds_calculation_ = false;
}

DOMQuad::DOMQuad(const DOMPointInit* p1,
                 const DOMPointInit* p2,
                 const DOMPointInit* p3,
                 const DOMPointInit* p4)
    : p1_(DOMQuadPoint::FromPoint(p1, this)),
      p2_(DOMQuadPoint::FromPoint(p2, this)),
      p3_(DOMQuadPoint::FromPoint(p3, this)),
      p4_(DOMQuadPoint::FromPoint(p4, this)),
      needs_bounds_calculation_(true) {}

DOMQuad::DOMQuad(double x, double y, double width, double height)
    : p1_(DOMQuadPoint::Create(x, y, 0, 1, this)),
      p2_(DOMQuadPoint::Create(x + width, y, 0, 1, this)),
      p3_(DOMQuadPoint::Create(x + width, y + height, 0, 1, this)),
      p4_(DOMQuadPoint::Create(x, y + height, 0, 1, this)),
      needs_bounds_calculation_(true) {}

ScriptObject DOMQuad::toJSONForBinding(ScriptState* script_state) const {
  V8ObjectBuilder result(script_state);
  result.Add("p1", p1());
  result.Add("p2", p2());
  result.Add("p3", p3());
  result.Add("p4", p4());
  return result.ToScriptObject();
}

}  // namespace blink