2022-03-21 18:20:48 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2022, Tim Flynn <trflynn89@serenityos.org>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
2022-09-26 00:04:39 +00:00
|
|
|
#include <LibWeb/Bindings/Intrinsics.h>
|
2024-04-27 00:09:58 +00:00
|
|
|
#include <LibWeb/Bindings/SVGLengthPrototype.h>
|
2024-03-03 20:33:40 +00:00
|
|
|
#include <LibWeb/CSS/PercentageOr.h>
|
2022-03-21 18:20:48 +00:00
|
|
|
#include <LibWeb/SVG/SVGLength.h>
|
|
|
|
|
|
|
|
namespace Web::SVG {
|
|
|
|
|
2024-11-14 15:01:23 +00:00
|
|
|
GC_DEFINE_ALLOCATOR(SVGLength);
|
2023-11-19 18:47:52 +00:00
|
|
|
|
2024-11-14 15:01:23 +00:00
|
|
|
GC::Ref<SVGLength> SVGLength::create(JS::Realm& realm, u8 unit_type, float value)
|
2022-03-21 18:20:48 +00:00
|
|
|
{
|
2024-11-13 16:50:17 +00:00
|
|
|
return realm.create<SVGLength>(realm, unit_type, value);
|
2022-03-21 18:20:48 +00:00
|
|
|
}
|
|
|
|
|
2024-11-14 15:01:23 +00:00
|
|
|
GC::Ref<SVGLength> SVGLength::from_length_percentage(JS::Realm& realm, CSS::LengthPercentage const& length_percentage)
|
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())
|
|
|
|
return SVGLength::create(realm, SVG_LENGTHTYPE_PERCENTAGE, length_percentage.percentage().value());
|
|
|
|
if (length_percentage.is_length())
|
|
|
|
return SVGLength::create(
|
|
|
|
realm, [&] {
|
|
|
|
switch (length_percentage.length().type()) {
|
|
|
|
case CSS::Length::Type::Em:
|
|
|
|
return SVG_LENGTHTYPE_EMS;
|
|
|
|
case CSS::Length::Type::Ex:
|
|
|
|
return SVG_LENGTHTYPE_EXS;
|
|
|
|
case CSS::Length::Type::Px:
|
|
|
|
return SVG_LENGTHTYPE_PX;
|
|
|
|
case CSS::Length::Type::Cm:
|
|
|
|
return SVG_LENGTHTYPE_CM;
|
|
|
|
case CSS::Length::Type::Mm:
|
|
|
|
return SVG_LENGTHTYPE_MM;
|
|
|
|
case CSS::Length::Type::In:
|
|
|
|
return SVG_LENGTHTYPE_IN;
|
|
|
|
case CSS::Length::Type::Pt:
|
|
|
|
return SVG_LENGTHTYPE_PT;
|
|
|
|
case CSS::Length::Type::Pc:
|
|
|
|
return SVG_LENGTHTYPE_PC;
|
|
|
|
default:
|
|
|
|
return SVG_LENGTHTYPE_UNKNOWN;
|
|
|
|
}
|
|
|
|
}(),
|
|
|
|
length_percentage.length().raw_value());
|
|
|
|
return SVGLength::create(realm, SVG_LENGTHTYPE_UNKNOWN, 0);
|
|
|
|
}
|
|
|
|
|
2022-09-26 00:04:39 +00:00
|
|
|
SVGLength::SVGLength(JS::Realm& realm, u8 unit_type, float value)
|
|
|
|
: PlatformObject(realm)
|
2022-09-02 12:04:59 +00:00
|
|
|
, m_unit_type(unit_type)
|
2022-03-21 18:20:48 +00:00
|
|
|
, m_value(value)
|
|
|
|
{
|
2023-01-10 11:28:20 +00:00
|
|
|
}
|
|
|
|
|
2023-08-07 06:41:28 +00:00
|
|
|
void SVGLength::initialize(JS::Realm& realm)
|
2023-01-10 11:28:20 +00:00
|
|
|
{
|
2023-08-07 06:41:28 +00:00
|
|
|
Base::initialize(realm);
|
2024-03-16 12:13:08 +00:00
|
|
|
WEB_SET_PROTOTYPE_FOR_INTERFACE(SVGLength);
|
2022-03-21 18:20:48 +00:00
|
|
|
}
|
|
|
|
|
2022-09-02 12:04:59 +00:00
|
|
|
SVGLength::~SVGLength() = default;
|
|
|
|
|
2022-03-21 18:20:48 +00:00
|
|
|
// https://www.w3.org/TR/SVG11/types.html#__svg__SVGLength__value
|
2022-09-25 16:03:42 +00:00
|
|
|
WebIDL::ExceptionOr<void> SVGLength::set_value(float value)
|
2022-03-21 18:20:48 +00:00
|
|
|
{
|
|
|
|
// FIXME: Raise an exception if this <length> is read-only.
|
|
|
|
m_value = value;
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|