From 193ab3757b72c7c3e6e0f32011f1ac1310fbcd25 Mon Sep 17 00:00:00 2001 From: Totto16 Date: Wed, 18 Jun 2025 02:51:52 +0200 Subject: [PATCH] LibWeb: Factor out canvas rendering options algorihtms Factor out canvas parsing algorihtm for CanvasRenderingContext2DSettings from JS::Value. This was only used in one place but needs to be usable from other places too in the future. --- Libraries/LibWeb/CMakeLists.txt | 1 + .../LibWeb/HTML/Canvas/CanvasSettings.cpp | 56 +++++++++++++++++++ Libraries/LibWeb/HTML/Canvas/CanvasSettings.h | 12 +++- .../LibWeb/HTML/CanvasRenderingContext2D.cpp | 48 +--------------- .../LibWeb/HTML/CanvasRenderingContext2D.h | 2 - 5 files changed, 68 insertions(+), 51 deletions(-) create mode 100644 Libraries/LibWeb/HTML/Canvas/CanvasSettings.cpp diff --git a/Libraries/LibWeb/CMakeLists.txt b/Libraries/LibWeb/CMakeLists.txt index bd49313ea0a..a1a291ca4e4 100644 --- a/Libraries/LibWeb/CMakeLists.txt +++ b/Libraries/LibWeb/CMakeLists.txt @@ -342,6 +342,7 @@ set(SOURCES HTML/BrowsingContextGroup.cpp HTML/Canvas/CanvasDrawImage.cpp HTML/Canvas/CanvasPath.cpp + HTML/Canvas/CanvasSettings.cpp HTML/Canvas/CanvasState.cpp HTML/Canvas/SerializeBitmap.cpp HTML/CanvasGradient.cpp diff --git a/Libraries/LibWeb/HTML/Canvas/CanvasSettings.cpp b/Libraries/LibWeb/HTML/Canvas/CanvasSettings.cpp new file mode 100644 index 00000000000..bd65ce0b011 --- /dev/null +++ b/Libraries/LibWeb/HTML/Canvas/CanvasSettings.cpp @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025, Ladybird contributors + * Copyright (c) 2025, Jelle Raaijmakers + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include +#include + +// https://html.spec.whatwg.org/multipage/canvas.html#canvasrenderingcontext2dsettings +JS::ThrowCompletionOr Web::HTML::CanvasRenderingContext2DSettings::from_js_value(JS::VM& vm, JS::Value value) +{ + if (!value.is_nullish() && !value.is_object()) + return vm.throw_completion(JS::ErrorType::NotAnObjectOfType, "CanvasRenderingContext2DSettings"); + + CanvasRenderingContext2DSettings settings; + if (value.is_nullish()) + return settings; + + auto& value_object = value.as_object(); + + JS::Value alpha = TRY(value_object.get("alpha"_fly_string)); + settings.alpha = alpha.is_undefined() ? true : alpha.to_boolean(); + + JS::Value desynchronized = TRY(value_object.get("desynchronized"_fly_string)); + settings.desynchronized = desynchronized.is_undefined() ? false : desynchronized.to_boolean(); + + JS::Value color_space = TRY(value_object.get("colorSpace"_fly_string)); + if (!color_space.is_undefined()) { + auto color_space_string = TRY(color_space.to_string(vm)); + if (color_space_string == "srgb"sv) + settings.color_space = Bindings::PredefinedColorSpace::Srgb; + else if (color_space_string == "display-p3"sv) + settings.color_space = Bindings::PredefinedColorSpace::DisplayP3; + else + return vm.throw_completion(JS::ErrorType::InvalidEnumerationValue, color_space_string, "colorSpace"); + } + + JS::Value color_type = TRY(value_object.get("colorType"_fly_string)); + if (!color_type.is_undefined()) { + auto color_type_string = TRY(color_type.to_string(vm)); + if (color_type_string == "unorm8"sv) + settings.color_type = Bindings::CanvasColorType::Unorm8; + else if (color_type_string == "float16"sv) + settings.color_type = Bindings::CanvasColorType::Float16; + else + return vm.throw_completion(JS::ErrorType::InvalidEnumerationValue, color_type_string, "colorType"); + } + + JS::Value will_read_frequently = TRY(value_object.get("willReadFrequently"_fly_string)); + settings.will_read_frequently = will_read_frequently.is_undefined() ? false : will_read_frequently.to_boolean(); + + return settings; +} diff --git a/Libraries/LibWeb/HTML/Canvas/CanvasSettings.h b/Libraries/LibWeb/HTML/Canvas/CanvasSettings.h index 9563a607175..64f8b468ec3 100644 --- a/Libraries/LibWeb/HTML/Canvas/CanvasSettings.h +++ b/Libraries/LibWeb/HTML/Canvas/CanvasSettings.h @@ -6,14 +6,22 @@ #pragma once +#include + +// FIXME: this needs to be shared inside the idl, this is not that easily fixable, so until that we are just using the definition of ImageDataPrototype and CanvasRenderingContext2DPrototype, and assert that it's the same in all duplicate declarations +#include +#include + namespace Web::HTML { struct CanvasRenderingContext2DSettings { bool alpha { true }; bool desynchronized { false }; - Bindings::PredefinedColorSpace color_space { Bindings::PredefinedColorSpace::Srgb }; - Bindings::CanvasColorType color_type { Bindings::CanvasColorType::Unorm8 }; + Web::Bindings::PredefinedColorSpace color_space { Web::Bindings::PredefinedColorSpace::Srgb }; + Web::Bindings::CanvasColorType color_type { Web::Bindings::CanvasColorType::Unorm8 }; bool will_read_frequently { false }; + + [[nodiscard]] static JS::ThrowCompletionOr from_js_value(JS::VM&, JS::Value); }; // https://html.spec.whatwg.org/multipage/canvas.html#canvassettings diff --git a/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp b/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp index 26e5e7f2a1b..8116ad8d3b0 100644 --- a/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp +++ b/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp @@ -37,7 +37,7 @@ GC_DEFINE_ALLOCATOR(CanvasRenderingContext2D); JS::ThrowCompletionOr> CanvasRenderingContext2D::create(JS::Realm& realm, HTMLCanvasElement& element, JS::Value options) { - auto context_attributes = TRY(context_attributes_from_options(realm.vm(), options)); + auto context_attributes = TRY(CanvasRenderingContext2DSettings::from_js_value(realm.vm(), options)); return realm.create(realm, element, context_attributes); } @@ -64,52 +64,6 @@ void CanvasRenderingContext2D::visit_edges(Cell::Visitor& visitor) visitor.visit(m_element); } -// https://html.spec.whatwg.org/multipage/canvas.html#canvasrenderingcontext2dsettings -JS::ThrowCompletionOr CanvasRenderingContext2D::context_attributes_from_options(JS::VM& vm, JS::Value value) -{ - if (!value.is_nullish() && !value.is_object()) - return vm.throw_completion(JS::ErrorType::NotAnObjectOfType, "CanvasRenderingContext2DSettings"); - - CanvasRenderingContext2DSettings settings; - if (value.is_nullish()) - return settings; - - auto& value_object = value.as_object(); - - JS::Value alpha = TRY(value_object.get("alpha"_fly_string)); - settings.alpha = alpha.is_undefined() ? true : alpha.to_boolean(); - - JS::Value desynchronized = TRY(value_object.get("desynchronized"_fly_string)); - settings.desynchronized = desynchronized.is_undefined() ? false : desynchronized.to_boolean(); - - JS::Value color_space = TRY(value_object.get("colorSpace"_fly_string)); - if (!color_space.is_undefined()) { - auto color_space_string = TRY(color_space.to_string(vm)); - if (color_space_string == "srgb"sv) - settings.color_space = Bindings::PredefinedColorSpace::Srgb; - else if (color_space_string == "display-p3"sv) - settings.color_space = Bindings::PredefinedColorSpace::DisplayP3; - else - return vm.throw_completion(JS::ErrorType::InvalidEnumerationValue, color_space_string, "colorSpace"); - } - - JS::Value color_type = TRY(value_object.get("colorType"_fly_string)); - if (!color_type.is_undefined()) { - auto color_type_string = TRY(color_type.to_string(vm)); - if (color_type_string == "unorm8"sv) - settings.color_type = Bindings::CanvasColorType::Unorm8; - else if (color_type_string == "float16"sv) - settings.color_type = Bindings::CanvasColorType::Float16; - else - return vm.throw_completion(JS::ErrorType::InvalidEnumerationValue, color_type_string, "colorType"); - } - - JS::Value will_read_frequently = TRY(value_object.get("willReadFrequently"_fly_string)); - settings.will_read_frequently = will_read_frequently.is_undefined() ? false : will_read_frequently.to_boolean(); - - return settings; -} - HTMLCanvasElement& CanvasRenderingContext2D::canvas_element() { return *m_element; diff --git a/Libraries/LibWeb/HTML/CanvasRenderingContext2D.h b/Libraries/LibWeb/HTML/CanvasRenderingContext2D.h index fc6f1ff3a5a..a101d92afb4 100644 --- a/Libraries/LibWeb/HTML/CanvasRenderingContext2D.h +++ b/Libraries/LibWeb/HTML/CanvasRenderingContext2D.h @@ -134,8 +134,6 @@ private: virtual void initialize(JS::Realm&) override; virtual void visit_edges(Cell::Visitor&) override; - static JS::ThrowCompletionOr context_attributes_from_options(JS::VM&, JS::Value); - virtual Gfx::Painter* painter_for_canvas_state() override { return painter(); } virtual Gfx::Path& path_for_canvas_state() override { return path(); }