mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-07-19 07:22:21 +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;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue