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,22 +1,21 @@
/*
* Copyright (c) 2023, MacDue <macdue@dueutil.tech>
* Copyright (c) 2024, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
* Copyright (c) 2025, Jelle Raaijmakers <jelle@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibGfx/Bitmap.h>
#include <LibWeb/Bindings/CanvasPatternPrototype.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/HTML/CanvasPattern.h>
#include <LibWeb/HTML/CanvasRenderingContext2D.h>
#include <LibWeb/SVG/SVGImageElement.h>
namespace Web::HTML {
GC_DEFINE_ALLOCATOR(CanvasPattern);
CanvasPattern::CanvasPattern(JS::Realm& realm, CanvasPatternPaintStyle& pattern)
CanvasPattern::CanvasPattern(JS::Realm& realm, Gfx::CanvasPatternPaintStyle& pattern)
: PlatformObject(realm)
, m_pattern(pattern)
{
@ -27,15 +26,15 @@ CanvasPattern::~CanvasPattern() = default;
// https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-createpattern
WebIDL::ExceptionOr<GC::Ptr<CanvasPattern>> CanvasPattern::create(JS::Realm& realm, CanvasImageSource const& image, StringView repetition)
{
auto parse_repetition = [&](auto repetition) -> Optional<CanvasPatternPaintStyle::Repetition> {
if (repetition == "repeat"sv)
return CanvasPatternPaintStyle::Repetition::Repeat;
if (repetition == "repeat-x"sv)
return CanvasPatternPaintStyle::Repetition::RepeatX;
if (repetition == "repeat-y"sv)
return CanvasPatternPaintStyle::Repetition::RepeatY;
if (repetition == "no-repeat"sv)
return CanvasPatternPaintStyle::Repetition::NoRepeat;
auto parse_repetition = [&](auto value) -> Optional<Gfx::CanvasPatternPaintStyle::Repetition> {
if (value == "repeat"sv)
return Gfx::CanvasPatternPaintStyle::Repetition::Repeat;
if (value == "repeat-x"sv)
return Gfx::CanvasPatternPaintStyle::Repetition::RepeatX;
if (value == "repeat-y"sv)
return Gfx::CanvasPatternPaintStyle::Repetition::RepeatY;
if (value == "no-repeat"sv)
return Gfx::CanvasPatternPaintStyle::Repetition::NoRepeat;
return {};
};
@ -60,12 +59,14 @@ WebIDL::ExceptionOr<GC::Ptr<CanvasPattern>> CanvasPattern::create(JS::Realm& rea
return WebIDL::SyntaxError::create(realm, "Repetition value is not valid"_utf16);
// 6. Let pattern be a new CanvasPattern object with the image image and the repetition behavior given by repetition.
auto pattern = TRY_OR_THROW_OOM(realm.vm(), CanvasPatternPaintStyle::create(image, *repetition_value));
auto immutable_bitmap = canvas_image_source_bitmap(image);
auto paint_style = TRY_OR_THROW_OOM(realm.vm(), Gfx::CanvasPatternPaintStyle::create(immutable_bitmap, *repetition_value));
auto pattern = realm.create<CanvasPattern>(realm, *paint_style);
// FIXME: 7. If image is not origin-clean, then mark pattern as not origin-clean.
// 8. Return pattern.
return realm.create<CanvasPattern>(realm, *pattern);
return pattern;
}
void CanvasPattern::initialize(JS::Realm& realm)