mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-07-29 20:29:18 +00:00
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.
This commit is contained in:
parent
49500ac386
commit
193ab3757b
Notes:
github-actions[bot]
2025-06-30 15:47:50 +00:00
Author: https://github.com/Totto16
Commit: 193ab3757b
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/3788
Reviewed-by: https://github.com/ADKaster ✅
Reviewed-by: https://github.com/tcl3
5 changed files with 68 additions and 51 deletions
56
Libraries/LibWeb/HTML/Canvas/CanvasSettings.cpp
Normal file
56
Libraries/LibWeb/HTML/Canvas/CanvasSettings.cpp
Normal file
|
@ -0,0 +1,56 @@
|
|||
/*
|
||||
* Copyright (c) 2025, Ladybird contributors
|
||||
* Copyright (c) 2025, Jelle Raaijmakers <jelle@ladybird.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/FlyString.h>
|
||||
#include <LibJS/Runtime/VM.h>
|
||||
#include <LibWeb/HTML/Canvas/CanvasSettings.h>
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/canvas.html#canvasrenderingcontext2dsettings
|
||||
JS::ThrowCompletionOr<Web::HTML::CanvasRenderingContext2DSettings> Web::HTML::CanvasRenderingContext2DSettings::from_js_value(JS::VM& vm, JS::Value value)
|
||||
{
|
||||
if (!value.is_nullish() && !value.is_object())
|
||||
return vm.throw_completion<JS::TypeError>(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::TypeError>(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::TypeError>(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;
|
||||
}
|
|
@ -6,14 +6,22 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <LibJS/Forward.h>
|
||||
|
||||
// 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 <LibWeb/Bindings/CanvasRenderingContext2DPrototype.h>
|
||||
#include <LibWeb/Bindings/ImageDataPrototype.h>
|
||||
|
||||
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<CanvasRenderingContext2DSettings> from_js_value(JS::VM&, JS::Value);
|
||||
};
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/canvas.html#canvassettings
|
||||
|
|
|
@ -37,7 +37,7 @@ GC_DEFINE_ALLOCATOR(CanvasRenderingContext2D);
|
|||
|
||||
JS::ThrowCompletionOr<GC::Ref<CanvasRenderingContext2D>> 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<CanvasRenderingContext2D>(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<CanvasRenderingContext2DSettings> CanvasRenderingContext2D::context_attributes_from_options(JS::VM& vm, JS::Value value)
|
||||
{
|
||||
if (!value.is_nullish() && !value.is_object())
|
||||
return vm.throw_completion<JS::TypeError>(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::TypeError>(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::TypeError>(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;
|
||||
|
|
|
@ -134,8 +134,6 @@ private:
|
|||
virtual void initialize(JS::Realm&) override;
|
||||
virtual void visit_edges(Cell::Visitor&) override;
|
||||
|
||||
static JS::ThrowCompletionOr<CanvasRenderingContext2DSettings> 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(); }
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue