1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 07:17:35 +00:00

LibWeb: Extract CanvasDrawPath class from CRC2D

Again, this is an entirely virtual class since the methods involve
direct access to the Painter. Though, maybe I could just expose the
Painter...
This commit is contained in:
Sam Atkins 2022-08-12 16:52:17 +01:00 committed by Andreas Kling
parent c0494988ed
commit 62b561e2e1
5 changed files with 70 additions and 21 deletions

View file

@ -648,6 +648,7 @@ class XMLHttpRequestPrototype;
class XMLHttpRequestWrapper;
class XMLSerializerWrapper;
enum class CanPlayTypeResult;
enum class CanvasFillRule;
enum class EndingType;
enum class DOMParserSupportedType;
enum class ResizeObserverBoxOptions;

View file

@ -0,0 +1,33 @@
/*
* Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/String.h>
#include <LibWeb/HTML/Path2D.h>
namespace Web::HTML {
// https://html.spec.whatwg.org/multipage/canvas.html#canvasdrawpath
class CanvasDrawPath {
public:
virtual ~CanvasDrawPath() = default;
virtual void begin_path() = 0;
virtual void fill(String const& fill_rule) = 0;
virtual void fill(Path2D& path, String const& fill_rule) = 0;
virtual void stroke() = 0;
virtual void stroke(Path2D const& path) = 0;
virtual void clip() = 0;
protected:
CanvasDrawPath() = default;
};
}

View file

@ -0,0 +1,25 @@
#import <HTML/Path2D.idl>
// https://html.spec.whatwg.org/multipage/canvas.html#canvasfillrule
enum CanvasFillRule { "nonzero", "evenodd" };
// https://html.spec.whatwg.org/multipage/canvas.html#canvasdrawpath
interface mixin CanvasDrawPath {
undefined beginPath();
// FIXME: `DOMString` should be `CanvasFillRule`
undefined fill(optional DOMString fillRule = "nonzero");
// FIXME: `DOMString` should be `CanvasFillRule`
undefined fill(Path2D path, optional DOMString fillRule = "nonzero");
undefined stroke();
undefined stroke(Path2D path);
// FIXME: Replace this with these two definitions:
// undefined clip(optional CanvasFillRule fillRule = "nonzero");
// undefined clip(Path2D path, optional CanvasFillRule fillRule = "nonzero");
undefined clip();
// FIXME: boolean isPointInPath(unrestricted double x, unrestricted double y, optional CanvasFillRule fillRule = "nonzero");
// FIXME: boolean isPointInPath(Path2D path, unrestricted double x, unrestricted double y, optional CanvasFillRule fillRule = "nonzero");
// FIXME: boolean isPointInStroke(unrestricted double x, unrestricted double y);
// FIXME: boolean isPointInStroke(Path2D path, unrestricted double x, unrestricted double y);
};

View file

@ -16,6 +16,7 @@
#include <LibGfx/Path.h>
#include <LibWeb/Bindings/Wrappable.h>
#include <LibWeb/DOM/ExceptionOr.h>
#include <LibWeb/HTML/Canvas/CanvasDrawPath.h>
#include <LibWeb/HTML/Canvas/CanvasFillStrokeStyles.h>
#include <LibWeb/HTML/Canvas/CanvasPath.h>
#include <LibWeb/HTML/Canvas/CanvasRect.h>
@ -38,7 +39,8 @@ class CanvasRenderingContext2D
, public CanvasState
, public CanvasTransform<CanvasRenderingContext2D>
, public CanvasFillStrokeStyles<CanvasRenderingContext2D>
, public CanvasRect {
, public CanvasRect
, public CanvasDrawPath {
AK_MAKE_NONCOPYABLE(CanvasRenderingContext2D);
AK_MAKE_NONMOVABLE(CanvasRenderingContext2D);
@ -60,15 +62,15 @@ public:
void set_line_width(float line_width) { drawing_state().line_width = line_width; }
float line_width() const { return drawing_state().line_width; }
void begin_path();
void stroke();
void stroke(Path2D const& path);
virtual void begin_path() override;
virtual void stroke() override;
virtual void stroke(Path2D const& path) override;
void fill_text(String const&, float x, float y, Optional<double> max_width);
void stroke_text(String const&, float x, float y, Optional<double> max_width);
void fill(String const& fill_rule);
void fill(Path2D& path, String const& fill_rule);
virtual void fill(String const& fill_rule) override;
virtual void fill(Path2D& path, String const& fill_rule) override;
RefPtr<ImageData> create_image_data(int width, int height) const;
DOM::ExceptionOr<RefPtr<ImageData>> get_image_data(int x, int y, int width, int height) const;
@ -80,7 +82,7 @@ public:
RefPtr<TextMetrics> measure_text(String const& text);
void clip();
virtual void clip() override;
private:
explicit CanvasRenderingContext2D(HTMLCanvasElement&);

View file

@ -2,25 +2,17 @@
#import <HTML/HTMLImageElement.idl>
#import <HTML/ImageData.idl>
#import <HTML/TextMetrics.idl>
#import <HTML/Canvas/CanvasDrawPath.idl>
#import <HTML/Canvas/CanvasFillStrokeStyles.idl>
#import <HTML/Canvas/CanvasPath.idl>
#import <HTML/Canvas/CanvasRect.idl>
#import <HTML/Canvas/CanvasState.idl>
#import <HTML/Canvas/CanvasTransform.idl>
#import <HTML/Path2D.idl>
// https://html.spec.whatwg.org/multipage/canvas.html#canvasrenderingcontext2d
[Exposed=Window]
interface CanvasRenderingContext2D {
undefined beginPath();
// FIXME: `DOMString` should be `CanvasFillRule`
undefined fill(optional DOMString fillRule = "nonzero");
// FIXME: `DOMString` should be `CanvasFillRule`
undefined fill(Path2D path, optional DOMString fillRule = "nonzero");
undefined stroke();
undefined stroke(Path2D path);
undefined fillText(DOMString text, double x, double y, optional double maxWidth);
undefined strokeText(DOMString text, double x, double y, optional double maxWidth);
@ -38,15 +30,11 @@ interface CanvasRenderingContext2D {
TextMetrics measureText(DOMString text);
// undefined clip(optional CanvasFillRule fillRule = "nonzero");
// undefined clip(Path2D path, optional CanvasFillRule fillRule = "nonzero");
// FIXME: Replace this with the two definitions above.
undefined clip();
};
CanvasRenderingContext2D includes CanvasState;
CanvasRenderingContext2D includes CanvasTransform;
CanvasRenderingContext2D includes CanvasFillStrokeStyles;
CanvasRenderingContext2D includes CanvasRect;
CanvasRenderingContext2D includes CanvasDrawPath;
CanvasRenderingContext2D includes CanvasPath;