OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "core/dom/DOMQuad.h" | 5 #include "core/dom/DOMQuad.h" |
6 | 6 |
7 #include "bindings/core/v8/V8ObjectBuilder.h" | 7 #include "bindings/core/v8/V8ObjectBuilder.h" |
8 #include "core/dom/DOMPoint.h" | 8 #include "core/dom/DOMPoint.h" |
9 #include "core/dom/DOMQuadInit.h" | 9 #include "core/dom/DOMQuadInit.h" |
| 10 #include "core/dom/DOMRect.h" |
10 #include "core/dom/DOMRectInit.h" | 11 #include "core/dom/DOMRectInit.h" |
11 | 12 |
12 namespace blink { | 13 namespace blink { |
13 | 14 |
14 DOMQuad* DOMQuad::create(const DOMPointInit& p1, | 15 DOMQuad* DOMQuad::create(const DOMPointInit& p1, |
15 const DOMPointInit& p2, | 16 const DOMPointInit& p2, |
16 const DOMPointInit& p3, | 17 const DOMPointInit& p3, |
17 const DOMPointInit& p4) { | 18 const DOMPointInit& p4) { |
18 return new DOMQuad(p1, p2, p3, p4); | 19 return new DOMQuad(p1, p2, p3, p4); |
19 } | 20 } |
20 | 21 |
21 DOMQuad* DOMQuad::fromRect(const DOMRectInit& other) { | 22 DOMQuad* DOMQuad::fromRect(const DOMRectInit& other) { |
22 return new DOMQuad(other.x(), other.y(), other.width(), other.height()); | 23 return new DOMQuad(other.x(), other.y(), other.width(), other.height()); |
23 } | 24 } |
24 | 25 |
25 DOMQuad* DOMQuad::fromQuad(const DOMQuadInit& other) { | 26 DOMQuad* DOMQuad::fromQuad(const DOMQuadInit& other) { |
26 return new DOMQuad(other.hasP1() ? other.p1() : DOMPointInit(), | 27 return new DOMQuad(other.hasP1() ? other.p1() : DOMPointInit(), |
27 other.hasP2() ? other.p2() : DOMPointInit(), | 28 other.hasP2() ? other.p2() : DOMPointInit(), |
28 other.hasP3() ? other.p3() : DOMPointInit(), | 29 other.hasP3() ? other.p3() : DOMPointInit(), |
29 other.hasP3() ? other.p4() : DOMPointInit()); | 30 other.hasP3() ? other.p4() : DOMPointInit()); |
30 } | 31 } |
31 | 32 |
| 33 DOMRect* DOMQuad::getBounds() { |
| 34 return DOMRect::create(m_left, m_top, m_right - m_left, m_bottom - m_top); |
| 35 } |
| 36 |
| 37 void DOMQuad::calculateBounds() { |
| 38 m_left = std::min(p1()->x(), p2()->x()); |
| 39 m_left = std::min(m_left, p3()->x()); |
| 40 m_left = std::min(m_left, p4()->x()); |
| 41 m_top = std::min(p1()->y(), p2()->y()); |
| 42 m_top = std::min(m_top, p3()->y()); |
| 43 m_top = std::min(m_top, p4()->y()); |
| 44 m_right = std::max(p1()->x(), p2()->x()); |
| 45 m_right = std::max(m_right, p3()->x()); |
| 46 m_right = std::max(m_right, p4()->x()); |
| 47 m_bottom = std::max(p1()->y(), p2()->y()); |
| 48 m_bottom = std::max(m_bottom, p3()->y()); |
| 49 m_bottom = std::max(m_bottom, p4()->y()); |
| 50 } |
| 51 |
32 DOMQuad::DOMQuad(const DOMPointInit& p1, | 52 DOMQuad::DOMQuad(const DOMPointInit& p1, |
33 const DOMPointInit& p2, | 53 const DOMPointInit& p2, |
34 const DOMPointInit& p3, | 54 const DOMPointInit& p3, |
35 const DOMPointInit& p4) | 55 const DOMPointInit& p4) |
36 : m_p1(DOMPoint::fromPoint(p1)), | 56 : m_p1(DOMPoint::fromPoint(p1)), |
37 m_p2(DOMPoint::fromPoint(p2)), | 57 m_p2(DOMPoint::fromPoint(p2)), |
38 m_p3(DOMPoint::fromPoint(p3)), | 58 m_p3(DOMPoint::fromPoint(p3)), |
39 m_p4(DOMPoint::fromPoint(p4)) {} | 59 m_p4(DOMPoint::fromPoint(p4)) { |
| 60 calculateBounds(); |
| 61 } |
40 | 62 |
41 DOMQuad::DOMQuad(double x, double y, double width, double height) | 63 DOMQuad::DOMQuad(double x, double y, double width, double height) |
42 : m_p1(DOMPoint::create(x, y, 0, 1)), | 64 : m_p1(DOMPoint::create(x, y, 0, 1)), |
43 m_p2(DOMPoint::create(x + width, y, 0, 1)), | 65 m_p2(DOMPoint::create(x + width, y, 0, 1)), |
44 m_p3(DOMPoint::create(x + width, y + height, 0, 1)), | 66 m_p3(DOMPoint::create(x + width, y + height, 0, 1)), |
45 m_p4(DOMPoint::create(x, y + height, 0, 1)) {} | 67 m_p4(DOMPoint::create(x, y + height, 0, 1)) { |
| 68 calculateBounds(); |
| 69 } |
46 | 70 |
47 ScriptValue DOMQuad::toJSONForBinding(ScriptState* scriptState) const { | 71 ScriptValue DOMQuad::toJSONForBinding(ScriptState* scriptState) const { |
48 V8ObjectBuilder result(scriptState); | 72 V8ObjectBuilder result(scriptState); |
49 result.add("p1", p1()); | 73 result.add("p1", p1()); |
50 result.add("p2", p2()); | 74 result.add("p2", p2()); |
51 result.add("p3", p3()); | 75 result.add("p3", p3()); |
52 result.add("p4", p4()); | 76 result.add("p4", p4()); |
53 return result.scriptValue(); | 77 return result.scriptValue(); |
54 } | 78 } |
55 | 79 |
56 } // namespace blink | 80 } // namespace blink |
OLD | NEW |