Implement CanvasRenderingContext2D.getTransform()

This commit is contained in:
pylbrecht 2020-02-11 22:36:50 +01:00
parent 6b2079e5b3
commit 588c09b580
16 changed files with 42 additions and 22 deletions

View file

@ -919,6 +919,10 @@ impl<'a> CanvasData<'a> {
self.state.stroke_opts.set_miter_limit(limit);
}
pub fn get_transform(&self) -> Transform2D<f32> {
self.drawtarget.get_transform()
}
pub fn set_transform(&mut self, transform: &Transform2D<f32>) {
// If there is an in-progress path, store the existing transformation required
// to move between device and user space.

View file

@ -210,6 +210,10 @@ impl<'a> CanvasPaintThread<'a> {
Canvas2dMsg::SetLineCap(cap) => self.canvas(canvas_id).set_line_cap(cap),
Canvas2dMsg::SetLineJoin(join) => self.canvas(canvas_id).set_line_join(join),
Canvas2dMsg::SetMiterLimit(limit) => self.canvas(canvas_id).set_miter_limit(limit),
Canvas2dMsg::GetTransform(sender) => {
let transform = self.canvas(canvas_id).get_transform();
sender.send(transform).unwrap();
},
Canvas2dMsg::SetTransform(ref matrix) => self.canvas(canvas_id).set_transform(matrix),
Canvas2dMsg::SetGlobalAlpha(alpha) => self.canvas(canvas_id).set_global_alpha(alpha),
Canvas2dMsg::SetGlobalComposition(op) => {

View file

@ -48,6 +48,7 @@ pub enum Canvas2dMsg {
FillText(String, f64, f64, Option<f64>, FillOrStrokeStyle),
FillRect(Rect<f32>, FillOrStrokeStyle),
GetImageData(Rect<u64>, Size2D<u64>, IpcBytesSender),
GetTransform(IpcSender<Transform2D<f32>>),
IsPointInPath(f64, f64, FillRule, IpcSender<bool>),
LineTo(Point2D<f32>),
MoveTo(Point2D<f32>),

View file

@ -16,6 +16,7 @@ use crate::dom::bindings::root::{Dom, DomRoot};
use crate::dom::bindings::str::DOMString;
use crate::dom::canvasgradient::{CanvasGradient, CanvasGradientStyle, ToFillOrStrokeStyle};
use crate::dom::canvaspattern::CanvasPattern;
use crate::dom::dommatrix::DOMMatrix;
use crate::dom::element::cors_setting_for_element;
use crate::dom::element::Element;
use crate::dom::globalscope::GlobalScope;
@ -1415,6 +1416,15 @@ impl CanvasState {
self.update_transform()
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-gettransform
pub fn get_transform(&self, global: &GlobalScope) -> DomRoot<DOMMatrix> {
let (sender, receiver) = ipc::channel::<Transform2D<f32>>().unwrap();
self.send_canvas_2d_msg(Canvas2dMsg::GetTransform(sender));
let transform = receiver.recv().unwrap();
DOMMatrix::new(global, true, transform.cast::<f64>().to_3d())
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-settransform
pub fn set_transform(&self, a: f64, b: f64, c: f64, d: f64, e: f64, f: f64) {
if !(a.is_finite() &&

View file

@ -18,6 +18,7 @@ use crate::dom::bindings::root::{Dom, DomRoot, LayoutDom};
use crate::dom::bindings::str::DOMString;
use crate::dom::canvasgradient::CanvasGradient;
use crate::dom::canvaspattern::CanvasPattern;
use crate::dom::dommatrix::DOMMatrix;
use crate::dom::globalscope::GlobalScope;
use crate::dom::htmlcanvaselement::HTMLCanvasElement;
use crate::dom::imagedata::ImageData;
@ -224,6 +225,11 @@ impl CanvasRenderingContext2DMethods for CanvasRenderingContext2D {
self.canvas_state.borrow().transform(a, b, c, d, e, f)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-gettransform
fn GetTransform(&self) -> DomRoot<DOMMatrix> {
self.canvas_state.borrow().get_transform(&self.global())
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-settransform
fn SetTransform(&self, a: f64, b: f64, c: f64, d: f64, e: f64, f: f64) {
self.canvas_state.borrow().set_transform(a, b, c, d, e, f)

View file

@ -19,6 +19,7 @@ use crate::dom::bindings::root::{Dom, DomRoot};
use crate::dom::bindings::str::DOMString;
use crate::dom::canvasgradient::CanvasGradient;
use crate::dom::canvaspattern::CanvasPattern;
use crate::dom::dommatrix::DOMMatrix;
use crate::dom::globalscope::GlobalScope;
use crate::dom::htmlcanvaselement::HTMLCanvasElement;
use crate::dom::imagedata::ImageData;
@ -470,6 +471,11 @@ impl OffscreenCanvasRenderingContext2DMethods for OffscreenCanvasRenderingContex
self.canvas_state.borrow().transform(a, b, c, d, e, f)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-gettransform
fn GetTransform(&self) -> DomRoot<DOMMatrix> {
self.canvas_state.borrow().get_transform(&self.global())
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-settransform
fn SetTransform(&self, a: f64, b: f64, c: f64, d: f64, e: f64, f: f64) {
self.canvas_state.borrow().set_transform(a, b, c, d, e, f)

View file

@ -20,6 +20,7 @@ use crate::dom::bindings::str::DOMString;
use crate::dom::canvasgradient::CanvasGradient;
use crate::dom::canvaspattern::CanvasPattern;
use crate::dom::canvasrenderingcontext2d::CanvasRenderingContext2D;
use crate::dom::dommatrix::DOMMatrix;
use crate::dom::paintworkletglobalscope::PaintWorkletGlobalScope;
use crate::euclidext::Size2DExt;
use canvas_traits::canvas::CanvasImageData;
@ -119,6 +120,11 @@ impl PaintRenderingContext2DMethods for PaintRenderingContext2D {
self.context.Transform(a, b, c, d, e, f)
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-gettransform
fn GetTransform(&self) -> DomRoot<DOMMatrix> {
self.context.GetTransform()
}
// https://html.spec.whatwg.org/multipage/#dom-context-2d-settransform
fn SetTransform(&self, a: f64, b: f64, c: f64, d: f64, e: f64, f: f64) {
self.context.SetTransform(a, b, c, d, e, f);

View file

@ -59,7 +59,7 @@ interface mixin CanvasTransform {
unrestricted double e,
unrestricted double f);
// [NewObject] DOMMatrix getTransform();
[NewObject] DOMMatrix getTransform();
void setTransform(unrestricted double a,
unrestricted double b,
unrestricted double c,

View file

@ -10,7 +10,7 @@
* related or neighboring rights to this work.
*/
[Exposed=(Window,Worker),
[Exposed=(Window,Worker,PaintWorklet),
LegacyWindowAlias=WebKitCSSMatrix]
interface DOMMatrix : DOMMatrixReadOnly {
[Throws] constructor(optional (DOMString or sequence<unrestricted double>) init);

View file

@ -10,7 +10,7 @@
* related or neighboring rights to this work.
*/
[Exposed=(Window,Worker)]
[Exposed=(Window,Worker,PaintWorklet)]
interface DOMMatrixReadOnly {
[Throws] constructor(optional (DOMString or sequence<unrestricted double>) init);

View file

@ -10,7 +10,7 @@
*/
// http://dev.w3.org/fxtf/geometry/Overview.html#dompoint
[Exposed=(Window,Worker)]
[Exposed=(Window,Worker,PaintWorklet)]
interface DOMPoint : DOMPointReadOnly {
[Throws] constructor(optional unrestricted double x = 0, optional unrestricted double y = 0,
optional unrestricted double z = 0, optional unrestricted double w = 1);

View file

@ -10,7 +10,7 @@
*/
// http://dev.w3.org/fxtf/geometry/Overview.html#dompointreadonly
[Exposed=(Window,Worker)]
[Exposed=(Window,Worker,PaintWorklet)]
interface DOMPointReadOnly {
[Throws] constructor(optional unrestricted double x = 0, optional unrestricted double y = 0,
optional unrestricted double z = 0, optional unrestricted double w = 1);

View file

@ -1,4 +0,0 @@
[2d.transformation.getTransform.html]
[This test ensures that getTransform works correctly.]
expected: FAIL

View file

@ -17,9 +17,6 @@
[addPath({f: 0, m42: 5e-324}) (invalid)]
expected: FAIL
[setTransform (Sanity check without dictionary)]
expected: FAIL
[addPath (Sanity check without second parameter)]
expected: FAIL

View file

@ -59,9 +59,6 @@
[ApplicationCache interface: window.applicationCache must inherit property "ondownloading" with the proper type]
expected: FAIL
[CanvasRenderingContext2D interface: document.createElement("canvas").getContext("2d") must inherit property "getTransform()" with the proper type]
expected: FAIL
[OffscreenCanvasRenderingContext2D interface: attribute imageSmoothingEnabled]
expected: FAIL
@ -1178,9 +1175,6 @@
[External interface object length]
expected: FAIL
[CanvasRenderingContext2D interface: operation getTransform()]
expected: FAIL
[OffscreenCanvasRenderingContext2D interface: operation arc(unrestricted double, unrestricted double, unrestricted double, unrestricted double, unrestricted double, boolean)]
expected: FAIL

View file

@ -1,4 +0,0 @@
[2d.transformation.getTransform.html]
[This test ensures that getTransform works correctly.]
expected: FAIL