Skip to main content

script/dom/
dompoint.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use dom_struct::dom_struct;
6use js::context::JSContext;
7use js::rust::HandleObject;
8use rustc_hash::FxHashMap;
9use script_bindings::reflector::reflect_dom_object_with_proto_and_cx;
10use servo_base::id::{DomPointId, DomPointIndex};
11use servo_constellation_traits::DomPoint;
12
13use crate::dom::bindings::codegen::Bindings::DOMPointBinding::{DOMPointInit, DOMPointMethods};
14use crate::dom::bindings::codegen::Bindings::DOMPointReadOnlyBinding::DOMPointReadOnlyMethods;
15use crate::dom::bindings::error::Fallible;
16use crate::dom::bindings::root::DomRoot;
17use crate::dom::bindings::serializable::Serializable;
18use crate::dom::bindings::structuredclone::StructuredData;
19use crate::dom::dompointreadonly::{DOMPointReadOnly, DOMPointWriteMethods};
20use crate::dom::globalscope::GlobalScope;
21
22// http://dev.w3.org/fxtf/geometry/Overview.html#dompoint
23#[dom_struct]
24pub(crate) struct DOMPoint {
25    point: DOMPointReadOnly,
26}
27
28impl DOMPoint {
29    fn new_inherited(x: f64, y: f64, z: f64, w: f64) -> DOMPoint {
30        DOMPoint {
31            point: DOMPointReadOnly::new_inherited(x, y, z, w),
32        }
33    }
34
35    pub(crate) fn new(
36        cx: &mut JSContext,
37        global: &GlobalScope,
38        x: f64,
39        y: f64,
40        z: f64,
41        w: f64,
42    ) -> DomRoot<DOMPoint> {
43        Self::new_with_proto(cx, global, None, x, y, z, w)
44    }
45
46    fn new_with_proto(
47        cx: &mut JSContext,
48        global: &GlobalScope,
49        proto: Option<HandleObject>,
50        x: f64,
51        y: f64,
52        z: f64,
53        w: f64,
54    ) -> DomRoot<DOMPoint> {
55        reflect_dom_object_with_proto_and_cx(
56            Box::new(DOMPoint::new_inherited(x, y, z, w)),
57            global,
58            proto,
59            cx,
60        )
61    }
62
63    pub(crate) fn new_from_init(
64        cx: &mut JSContext,
65        global: &GlobalScope,
66        p: &DOMPointInit,
67    ) -> DomRoot<DOMPoint> {
68        DOMPoint::new(cx, global, p.x, p.y, p.z, p.w)
69    }
70}
71
72impl DOMPointMethods<crate::DomTypeHolder> for DOMPoint {
73    /// <https://drafts.fxtf.org/geometry/#dom-dompointreadonly-dompointreadonly>
74    fn Constructor(
75        cx: &mut JSContext,
76        global: &GlobalScope,
77        proto: Option<HandleObject>,
78        x: f64,
79        y: f64,
80        z: f64,
81        w: f64,
82    ) -> Fallible<DomRoot<DOMPoint>> {
83        Ok(DOMPoint::new_with_proto(cx, global, proto, x, y, z, w))
84    }
85
86    /// <https://drafts.fxtf.org/geometry/#dom-dompoint-frompoint>
87    fn FromPoint(cx: &mut JSContext, global: &GlobalScope, init: &DOMPointInit) -> DomRoot<Self> {
88        Self::new_from_init(cx, global, init)
89    }
90
91    /// <https://dev.w3.org/fxtf/geometry/Overview.html#dom-dompointreadonly-x>
92    fn X(&self) -> f64 {
93        self.point.X()
94    }
95
96    /// <https://dev.w3.org/fxtf/geometry/Overview.html#dom-dompointreadonly-x>
97    fn SetX(&self, value: f64) {
98        self.point.SetX(value);
99    }
100
101    /// <https://dev.w3.org/fxtf/geometry/Overview.html#dom-dompointreadonly-y>
102    fn Y(&self) -> f64 {
103        self.point.Y()
104    }
105
106    /// <https://dev.w3.org/fxtf/geometry/Overview.html#dom-dompointreadonly-y>
107    fn SetY(&self, value: f64) {
108        self.point.SetY(value);
109    }
110
111    /// <https://dev.w3.org/fxtf/geometry/Overview.html#dom-dompointreadonly-z>
112    fn Z(&self) -> f64 {
113        self.point.Z()
114    }
115
116    /// <https://dev.w3.org/fxtf/geometry/Overview.html#dom-dompointreadonly-z>
117    fn SetZ(&self, value: f64) {
118        self.point.SetZ(value);
119    }
120
121    /// <https://dev.w3.org/fxtf/geometry/Overview.html#dom-dompointreadonly-w>
122    fn W(&self) -> f64 {
123        self.point.W()
124    }
125
126    /// <https://dev.w3.org/fxtf/geometry/Overview.html#dom-dompointreadonly-w>
127    fn SetW(&self, value: f64) {
128        self.point.SetW(value);
129    }
130}
131
132impl Serializable for DOMPoint {
133    type Index = DomPointIndex;
134    type Data = DomPoint;
135
136    fn serialize(&self) -> Result<(DomPointId, Self::Data), ()> {
137        let serialized = DomPoint {
138            x: self.X(),
139            y: self.Y(),
140            z: self.Z(),
141            w: self.W(),
142        };
143        Ok((DomPointId::new(), serialized))
144    }
145
146    fn deserialize(
147        cx: &mut JSContext,
148        owner: &GlobalScope,
149        serialized: Self::Data,
150    ) -> Result<DomRoot<Self>, ()> {
151        Ok(Self::new(
152            cx,
153            owner,
154            serialized.x,
155            serialized.y,
156            serialized.z,
157            serialized.w,
158        ))
159    }
160
161    fn serialized_storage<'a>(
162        data: StructuredData<'a, '_>,
163    ) -> &'a mut Option<FxHashMap<DomPointId, Self::Data>> {
164        match data {
165            StructuredData::Reader(reader) => &mut reader.points,
166            StructuredData::Writer(writer) => &mut writer.points,
167        }
168    }
169}