2022-03-21 14:20:48 -04:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2022, Tim Flynn <trflynn89@serenityos.org>
|
2025-08-26 16:51:46 +02:00
|
|
|
* Copyright (c) 2025, Jelle Raaijmakers <jelle@ladybird.org>
|
2022-03-21 14:20:48 -04:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
2022-09-25 18:04:39 -06:00
|
|
|
#include <LibWeb/Bindings/Intrinsics.h>
|
2024-04-27 12:09:58 +12:00
|
|
|
#include <LibWeb/Bindings/SVGLengthPrototype.h>
|
2024-03-03 20:33:40 +00:00
|
|
|
#include <LibWeb/CSS/PercentageOr.h>
|
2022-03-21 14:20:48 -04:00
|
|
|
#include <LibWeb/SVG/SVGLength.h>
|
|
|
|
|
|
|
|
namespace Web::SVG {
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
GC_DEFINE_ALLOCATOR(SVGLength);
|
2023-11-19 19:47:52 +01:00
|
|
|
|
2025-08-26 16:51:46 +02:00
|
|
|
GC::Ref<SVGLength> SVGLength::create(JS::Realm& realm, u8 unit_type, float value, ReadOnly read_only)
|
2022-03-21 14:20:48 -04:00
|
|
|
{
|
2025-08-26 16:51:46 +02:00
|
|
|
return realm.create<SVGLength>(realm, unit_type, value, read_only);
|
2022-03-21 14:20:48 -04:00
|
|
|
}
|
|
|
|
|
2025-08-26 16:51:46 +02:00
|
|
|
GC::Ref<SVGLength> SVGLength::from_length_percentage(JS::Realm& realm, CSS::LengthPercentage const& length_percentage,
|
|
|
|
ReadOnly read_only)
|
2024-03-03 20:33:40 +00:00
|
|
|
{
|
|
|
|
// FIXME: We can't tell if a CSS::LengthPercentage was a unitless length.
|
|
|
|
(void)SVG_LENGTHTYPE_NUMBER;
|
|
|
|
if (length_percentage.is_percentage())
|
2025-08-26 16:51:46 +02:00
|
|
|
return create(realm, SVG_LENGTHTYPE_PERCENTAGE, length_percentage.percentage().value(), read_only);
|
2024-03-03 20:33:40 +00:00
|
|
|
if (length_percentage.is_length())
|
2025-08-26 16:51:46 +02:00
|
|
|
return create(
|
2024-03-03 20:33:40 +00:00
|
|
|
realm, [&] {
|
2025-09-02 13:48:49 +01:00
|
|
|
switch (length_percentage.length().unit()) {
|
|
|
|
case CSS::LengthUnit::Em:
|
2024-03-03 20:33:40 +00:00
|
|
|
return SVG_LENGTHTYPE_EMS;
|
2025-09-02 13:48:49 +01:00
|
|
|
case CSS::LengthUnit::Ex:
|
2024-03-03 20:33:40 +00:00
|
|
|
return SVG_LENGTHTYPE_EXS;
|
2025-09-02 13:48:49 +01:00
|
|
|
case CSS::LengthUnit::Px:
|
2024-03-03 20:33:40 +00:00
|
|
|
return SVG_LENGTHTYPE_PX;
|
2025-09-02 13:48:49 +01:00
|
|
|
case CSS::LengthUnit::Cm:
|
2024-03-03 20:33:40 +00:00
|
|
|
return SVG_LENGTHTYPE_CM;
|
2025-09-02 13:48:49 +01:00
|
|
|
case CSS::LengthUnit::Mm:
|
2024-03-03 20:33:40 +00:00
|
|
|
return SVG_LENGTHTYPE_MM;
|
2025-09-02 13:48:49 +01:00
|
|
|
case CSS::LengthUnit::In:
|
2024-03-03 20:33:40 +00:00
|
|
|
return SVG_LENGTHTYPE_IN;
|
2025-09-02 13:48:49 +01:00
|
|
|
case CSS::LengthUnit::Pt:
|
2024-03-03 20:33:40 +00:00
|
|
|
return SVG_LENGTHTYPE_PT;
|
2025-09-02 13:48:49 +01:00
|
|
|
case CSS::LengthUnit::Pc:
|
2024-03-03 20:33:40 +00:00
|
|
|
return SVG_LENGTHTYPE_PC;
|
|
|
|
default:
|
|
|
|
return SVG_LENGTHTYPE_UNKNOWN;
|
|
|
|
}
|
|
|
|
}(),
|
2025-08-26 16:51:46 +02:00
|
|
|
length_percentage.length().raw_value(), read_only);
|
|
|
|
return create(realm, SVG_LENGTHTYPE_UNKNOWN, 0, read_only);
|
2024-03-03 20:33:40 +00:00
|
|
|
}
|
|
|
|
|
2025-08-26 16:51:46 +02:00
|
|
|
SVGLength::SVGLength(JS::Realm& realm, u8 unit_type, float value, ReadOnly read_only)
|
2022-09-25 18:04:39 -06:00
|
|
|
: PlatformObject(realm)
|
2022-03-21 14:20:48 -04:00
|
|
|
, m_value(value)
|
2025-08-26 16:51:46 +02:00
|
|
|
, m_unit_type(unit_type)
|
|
|
|
, m_read_only(read_only)
|
2022-03-21 14:20:48 -04:00
|
|
|
{
|
2023-01-10 06:28:20 -05:00
|
|
|
}
|
|
|
|
|
2023-08-07 08:41:28 +02:00
|
|
|
void SVGLength::initialize(JS::Realm& realm)
|
2023-01-10 06:28:20 -05:00
|
|
|
{
|
2024-03-16 13:13:08 +01:00
|
|
|
WEB_SET_PROTOTYPE_FOR_INTERFACE(SVGLength);
|
2025-04-20 16:22:57 +02:00
|
|
|
Base::initialize(realm);
|
2022-03-21 14:20:48 -04:00
|
|
|
}
|
|
|
|
|
2022-09-02 14:04:59 +02:00
|
|
|
SVGLength::~SVGLength() = default;
|
|
|
|
|
2025-08-26 16:51:46 +02:00
|
|
|
// https://svgwg.org/svg2-draft/types.html#__svg__SVGLength__value
|
2022-09-25 17:03:42 +01:00
|
|
|
WebIDL::ExceptionOr<void> SVGLength::set_value(float value)
|
2022-03-21 14:20:48 -04:00
|
|
|
{
|
2025-08-26 16:51:46 +02:00
|
|
|
// 1. If the SVGLength object is read only, then throw a NoModificationAllowedError.
|
|
|
|
if (m_read_only == ReadOnly::Yes)
|
|
|
|
return WebIDL::NoModificationAllowedError::create(realm(), "Cannot modify value of read-only SVGLength"_utf16);
|
|
|
|
|
|
|
|
// 2. Let value be the value being assigned to value.
|
|
|
|
// 3. Set the SVGLength's value to a <number> whose value is value.
|
2022-03-21 14:20:48 -04:00
|
|
|
m_value = value;
|
2025-08-26 16:51:46 +02:00
|
|
|
m_unit_type = SVG_LENGTHTYPE_NUMBER;
|
|
|
|
|
|
|
|
// FIXME: 4. If the SVGLength reflects the base value of a reflected attribute, reflects a presentation attribute, or
|
|
|
|
// reflects an element of the base value of a reflected attribute, then reserialize the reflected attribute.
|
|
|
|
|
2022-03-21 14:20:48 -04:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|