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

LibWeb: Generate CanvasRenderingContext2D bindings from IDL :^)

We're still missing optional argument support, so this implementation
doesn't support fill(), only fill(fill_rule).

Still it's really nice to get rid of so much hand-written wrapper code.
This commit is contained in:
Andreas Kling 2020-06-22 18:39:22 +02:00
parent f361d25ec8
commit 9ce25bbf1d
8 changed files with 84 additions and 505 deletions

View file

@ -26,6 +26,7 @@
#include <AK/OwnPtr.h>
#include <LibGfx/Painter.h>
#include <LibWeb/Bindings/CanvasRenderingContext2DWrapper.h>
#include <LibWeb/DOM/CanvasRenderingContext2D.h>
#include <LibWeb/DOM/HTMLCanvasElement.h>
#include <LibWeb/DOM/HTMLImageElement.h>
@ -190,9 +191,20 @@ void CanvasRenderingContext2D::fill(Gfx::Painter::WindingRule winding)
painter->fill_path(path, m_fill_style, winding);
}
RefPtr<ImageData> CanvasRenderingContext2D::create_image_data(JS::GlobalObject& global_object, int width, int height) const
void CanvasRenderingContext2D::fill(const String& fill_rule)
{
return ImageData::create_with_size(global_object, width, height);
if (fill_rule == "evenodd")
return fill(Gfx::Painter::WindingRule::EvenOdd);
return fill(Gfx::Painter::WindingRule::Nonzero);
}
RefPtr<ImageData> CanvasRenderingContext2D::create_image_data(int width, int height) const
{
if (!wrapper()) {
dbg() << "Hmm! Attempted to create ImageData for wrapper-less CRC2D.";
return nullptr;
}
return ImageData::create_with_size(wrapper()->global_object(), width, height);
}
void CanvasRenderingContext2D::put_image_data(const ImageData& image_data, float x, float y)

View file

@ -72,12 +72,15 @@ public:
void line_to(float x, float y);
void quadratic_curve_to(float cx, float cy, float x, float y);
void stroke();
void fill(Gfx::Painter::WindingRule);
RefPtr<ImageData> create_image_data(JS::GlobalObject&, int width, int height) const;
// FIXME: We should only have one fill(), really. Fix the wrapper generator!
void fill(Gfx::Painter::WindingRule);
void fill(const String& fill_rule);
RefPtr<ImageData> create_image_data(int width, int height) const;
void put_image_data(const ImageData&, float x, float y);
HTMLCanvasElement* element() { return m_element; }
HTMLCanvasElement* canvas() { return m_element; }
private:
explicit CanvasRenderingContext2D(HTMLCanvasElement&);

View file

@ -0,0 +1,28 @@
interface CanvasRenderingContext2D {
void fillRect(double x, double y, double w, double h);
void strokeRect(double x, double y, double w, double h);
void scale(double x, double y);
void translate(double x, double y);
void beginPath();
void closePath();
void fill(DOMString fillRule);
void stroke();
void moveTo(double x, double y);
void lineTo(double x, double y);
void quadraticCurveTo(double cpx, double cpy, double x, double y);
void drawImage(HTMLImageElement image, double dx, double dy);
attribute DOMString fillStyle;
attribute DOMString strokeStyle;
attribute double lineWidth;
ImageData createImageData(double sw, double sh);
void putImageData(ImageData imagedata, double dx, double dy);
readonly attribute HTMLCanvasElement canvas;
}