From f8b9aba8214b9c1f6ccd5852879d6a1cbb16a304 Mon Sep 17 00:00:00 2001 From: janekptacijarabaci Date: Sun, 15 Apr 2018 06:51:24 +0200 Subject: moebius#106: SVG - CanvasImageSource - allow using an SVGImageElement as a CanvasImageSource --- dom/canvas/CanvasRenderingContext2D.cpp | 24 ++++++++++++----- dom/canvas/CanvasRenderingContext2D.h | 4 +-- dom/webidl/CanvasRenderingContext2D.webidl | 3 +++ testing/web-platform/meta/MANIFEST.json | 12 +++++++++ .../drawimage_html_image_1.html | 30 ---------------------- .../drawimage_html_image_1_ref.html | 19 -------------- .../drawimage_svg_image_1.html | 30 ++++++++++++++++++++++ .../drawimage_svg_image_1_ref.html | 19 ++++++++++++++ 8 files changed, 84 insertions(+), 57 deletions(-) delete mode 100644 testing/web-platform/tests/2dcontext/drawing-images-to-the-canvas/drawimage_html_image_1.html delete mode 100644 testing/web-platform/tests/2dcontext/drawing-images-to-the-canvas/drawimage_html_image_1_ref.html create mode 100644 testing/web-platform/tests/2dcontext/drawing-images-to-the-canvas/drawimage_svg_image_1.html create mode 100644 testing/web-platform/tests/2dcontext/drawing-images-to-the-canvas/drawimage_svg_image_1_ref.html diff --git a/dom/canvas/CanvasRenderingContext2D.cpp b/dom/canvas/CanvasRenderingContext2D.cpp index 15df2b3377..a38c382938 100644 --- a/dom/canvas/CanvasRenderingContext2D.cpp +++ b/dom/canvas/CanvasRenderingContext2D.cpp @@ -105,6 +105,7 @@ #include "mozilla/dom/CanvasPath.h" #include "mozilla/dom/HTMLImageElement.h" #include "mozilla/dom/HTMLVideoElement.h" +#include "mozilla/dom/SVGImageElement.h" #include "mozilla/dom/SVGMatrix.h" #include "mozilla/dom/TextMetrics.h" #include "mozilla/dom/SVGMatrix.h" @@ -2477,10 +2478,10 @@ CanvasRenderingContext2D::CreatePattern(const CanvasImageSource& aSource, return nullptr; } - Element* htmlElement; + Element* element; if (aSource.IsHTMLCanvasElement()) { HTMLCanvasElement* canvas = &aSource.GetAsHTMLCanvasElement(); - htmlElement = canvas; + element = canvas; nsIntSize size = canvas->GetSize(); if (size.width == 0 || size.height == 0) { @@ -2505,7 +2506,7 @@ CanvasRenderingContext2D::CreatePattern(const CanvasImageSource& aSource, } RefPtr pat = - new CanvasPattern(this, srcSurf, repeatMode, htmlElement->NodePrincipal(), canvas->IsWriteOnly(), false); + new CanvasPattern(this, srcSurf, repeatMode, element->NodePrincipal(), canvas->IsWriteOnly(), false); return pat.forget(); } @@ -2516,11 +2517,19 @@ CanvasRenderingContext2D::CreatePattern(const CanvasImageSource& aSource, return nullptr; } - htmlElement = img; + element = img; + } else if (aSource.IsSVGImageElement()) { + SVGImageElement* img = &aSource.GetAsSVGImageElement(); + if (img->IntrinsicState().HasState(NS_EVENT_STATE_BROKEN)) { + aError.Throw(NS_ERROR_DOM_INVALID_STATE_ERR); + return nullptr; + } + + element = img; } else if (aSource.IsHTMLVideoElement()) { auto& video = aSource.GetAsHTMLVideoElement(); video.MarkAsContentSource(mozilla::dom::HTMLVideoElement::CallerAPI::CREATE_PATTERN); - htmlElement = &video; + element = &video; } else { // Special case for ImageBitmap ImageBitmap& imgBitmap = aSource.GetAsImageBitmap(); @@ -2559,7 +2568,7 @@ CanvasRenderingContext2D::CreatePattern(const CanvasImageSource& aSource, // The canvas spec says that createPattern should use the first frame // of animated images nsLayoutUtils::SurfaceFromElementResult res = - nsLayoutUtils::SurfaceFromElement(htmlElement, + nsLayoutUtils::SurfaceFromElement(element, nsLayoutUtils::SFE_WANT_FIRST_FRAME, mTarget); if (!res.GetSourceSurface()) { @@ -4949,6 +4958,9 @@ CanvasRenderingContext2D::DrawImage(const CanvasImageSource& aImage, if (aImage.IsHTMLImageElement()) { HTMLImageElement* img = &aImage.GetAsHTMLImageElement(); element = img; + } else if (aImage.IsSVGImageElement()) { + SVGImageElement* img = &aImage.GetAsSVGImageElement(); + element = img; } else { HTMLVideoElement* video = &aImage.GetAsHTMLVideoElement(); video->MarkAsContentSource(mozilla::dom::HTMLVideoElement::CallerAPI::DRAW_IMAGE); diff --git a/dom/canvas/CanvasRenderingContext2D.h b/dom/canvas/CanvasRenderingContext2D.h index c3ee3bdcbc..d5dff8f3b0 100644 --- a/dom/canvas/CanvasRenderingContext2D.h +++ b/dom/canvas/CanvasRenderingContext2D.h @@ -38,8 +38,8 @@ class SourceSurface; } // namespace gl namespace dom { -class HTMLImageElementOrHTMLCanvasElementOrHTMLVideoElementOrImageBitmap; -typedef HTMLImageElementOrHTMLCanvasElementOrHTMLVideoElementOrImageBitmap CanvasImageSource; +class HTMLImageElementOrSVGImageElementOrHTMLCanvasElementOrHTMLVideoElementOrImageBitmap; +typedef HTMLImageElementOrSVGImageElementOrHTMLCanvasElementOrHTMLVideoElementOrImageBitmap CanvasImageSource; class ImageData; class StringOrCanvasGradientOrCanvasPattern; class OwningStringOrCanvasGradientOrCanvasPattern; diff --git a/dom/webidl/CanvasRenderingContext2D.webidl b/dom/webidl/CanvasRenderingContext2D.webidl index 38a30f9e39..1c55642152 100644 --- a/dom/webidl/CanvasRenderingContext2D.webidl +++ b/dom/webidl/CanvasRenderingContext2D.webidl @@ -27,6 +27,9 @@ dictionary HitRegionOptions { }; typedef (HTMLImageElement or + SVGImageElement) HTMLOrSVGImageElement; + +typedef (HTMLOrSVGImageElement or HTMLCanvasElement or HTMLVideoElement or ImageBitmap) CanvasImageSource; diff --git a/testing/web-platform/meta/MANIFEST.json b/testing/web-platform/meta/MANIFEST.json index 65626d6b8c..ca574833b5 100644 --- a/testing/web-platform/meta/MANIFEST.json +++ b/testing/web-platform/meta/MANIFEST.json @@ -39138,6 +39138,18 @@ "url": "/2dcontext/drawing-images-to-the-canvas/drawimage_html_image_9.html" } ], + "2dcontext/drawing-images-to-the-canvas/drawimage_svg_image_1.html": [ + { + "path": "2dcontext/drawing-images-to-the-canvas/drawimage_svg_image_1.html", + "references": [ + [ + "/2dcontext/drawing-images-to-the-canvas/drawimage_svg_image_1_ref.html", + "==" + ] + ], + "url": "/2dcontext/drawing-images-to-the-canvas/drawimage_svg_image_1.html" + } + ], "2dcontext/line-styles/canvas_linestyles_linecap_001.htm": [ { "path": "2dcontext/line-styles/canvas_linestyles_linecap_001.htm", diff --git a/testing/web-platform/tests/2dcontext/drawing-images-to-the-canvas/drawimage_html_image_1.html b/testing/web-platform/tests/2dcontext/drawing-images-to-the-canvas/drawimage_html_image_1.html deleted file mode 100644 index b9de85a977..0000000000 --- a/testing/web-platform/tests/2dcontext/drawing-images-to-the-canvas/drawimage_html_image_1.html +++ /dev/null @@ -1,30 +0,0 @@ - - - - - - diff --git a/testing/web-platform/tests/2dcontext/drawing-images-to-the-canvas/drawimage_html_image_1_ref.html b/testing/web-platform/tests/2dcontext/drawing-images-to-the-canvas/drawimage_html_image_1_ref.html deleted file mode 100644 index 60545df17f..0000000000 --- a/testing/web-platform/tests/2dcontext/drawing-images-to-the-canvas/drawimage_html_image_1_ref.html +++ /dev/null @@ -1,19 +0,0 @@ - - - - - - - -
- - diff --git a/testing/web-platform/tests/2dcontext/drawing-images-to-the-canvas/drawimage_svg_image_1.html b/testing/web-platform/tests/2dcontext/drawing-images-to-the-canvas/drawimage_svg_image_1.html new file mode 100644 index 0000000000..74a00e037d --- /dev/null +++ b/testing/web-platform/tests/2dcontext/drawing-images-to-the-canvas/drawimage_svg_image_1.html @@ -0,0 +1,30 @@ + + + + + + diff --git a/testing/web-platform/tests/2dcontext/drawing-images-to-the-canvas/drawimage_svg_image_1_ref.html b/testing/web-platform/tests/2dcontext/drawing-images-to-the-canvas/drawimage_svg_image_1_ref.html new file mode 100644 index 0000000000..60545df17f --- /dev/null +++ b/testing/web-platform/tests/2dcontext/drawing-images-to-the-canvas/drawimage_svg_image_1_ref.html @@ -0,0 +1,19 @@ + + + + + + + +
+ + -- cgit v1.2.3