LibWeb: Implement support for drawing with CanvasPattern

We already had the API, but drawing to the canvas was not affected by
any created CanvasPattern. This moves CanvasPatternPaintStyle to LibGfx
so we don't have to reach into LibWeb, and implements the plumbing to
let Skia use images as a fill pattern.
This commit is contained in:
Jelle Raaijmakers 2025-10-22 20:00:17 +02:00 committed by Tim Ledbetter
parent 9753b8e62c
commit 62ae4e878f
Notes: github-actions[bot] 2025-10-23 12:21:23 +00:00
20 changed files with 237 additions and 167 deletions

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2023, MacDue <macdue@dueutil.tech>
* Copyright (c) 2025, Jelle Raaijmakers <jelle@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -13,6 +14,7 @@
#include <LibGfx/Color.h>
#include <LibGfx/Forward.h>
#include <LibGfx/Gradients.h>
#include <LibGfx/ImmutableBitmap.h>
#include <LibGfx/Rect.h>
namespace Gfx {
@ -78,6 +80,34 @@ private:
Optional<float> m_repeat_length;
};
class CanvasPatternPaintStyle : public PaintStyle {
public:
enum class Repetition : u8 {
Repeat,
RepeatX,
RepeatY,
NoRepeat
};
static ErrorOr<NonnullRefPtr<CanvasPatternPaintStyle>> create(RefPtr<ImmutableBitmap> image, Repetition repetition)
{
return adopt_nonnull_ref_or_enomem(new (nothrow) CanvasPatternPaintStyle(image, repetition));
}
RefPtr<ImmutableBitmap> image() const { return m_image; }
Repetition repetition() const { return m_repetition; }
private:
CanvasPatternPaintStyle(RefPtr<ImmutableBitmap> image, Repetition repetition)
: m_image(image)
, m_repetition(repetition)
{
}
RefPtr<ImmutableBitmap> m_image;
Repetition m_repetition { Repetition::Repeat };
};
// The following paint styles implement the gradients required for the HTML canvas.
// These gradients are (unlike CSS ones) not relative to the painted shape, and do not
// support premultiplied alpha.