/src/serenity/Userland/Libraries/LibWeb/SVG/SVGLength.cpp
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2022, Tim Flynn <trflynn89@serenityos.org> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #include <LibWeb/Bindings/Intrinsics.h> |
8 | | #include <LibWeb/Bindings/SVGLengthPrototype.h> |
9 | | #include <LibWeb/CSS/PercentageOr.h> |
10 | | #include <LibWeb/SVG/SVGLength.h> |
11 | | |
12 | | namespace Web::SVG { |
13 | | |
14 | | JS_DEFINE_ALLOCATOR(SVGLength); |
15 | | |
16 | | JS::NonnullGCPtr<SVGLength> SVGLength::create(JS::Realm& realm, u8 unit_type, float value) |
17 | 0 | { |
18 | 0 | return realm.heap().allocate<SVGLength>(realm, realm, unit_type, value); |
19 | 0 | } |
20 | | |
21 | | JS::NonnullGCPtr<SVGLength> SVGLength::from_length_percentage(JS::Realm& realm, CSS::LengthPercentage const& length_percentage) |
22 | 0 | { |
23 | | // FIXME: We can't tell if a CSS::LengthPercentage was a unitless length. |
24 | 0 | (void)SVG_LENGTHTYPE_NUMBER; |
25 | 0 | if (length_percentage.is_percentage()) |
26 | 0 | return SVGLength::create(realm, SVG_LENGTHTYPE_PERCENTAGE, length_percentage.percentage().value()); |
27 | 0 | if (length_percentage.is_length()) |
28 | 0 | return SVGLength::create( |
29 | 0 | realm, [&] { |
30 | 0 | switch (length_percentage.length().type()) { |
31 | 0 | case CSS::Length::Type::Em: |
32 | 0 | return SVG_LENGTHTYPE_EMS; |
33 | 0 | case CSS::Length::Type::Ex: |
34 | 0 | return SVG_LENGTHTYPE_EXS; |
35 | 0 | case CSS::Length::Type::Px: |
36 | 0 | return SVG_LENGTHTYPE_PX; |
37 | 0 | case CSS::Length::Type::Cm: |
38 | 0 | return SVG_LENGTHTYPE_CM; |
39 | 0 | case CSS::Length::Type::Mm: |
40 | 0 | return SVG_LENGTHTYPE_MM; |
41 | 0 | case CSS::Length::Type::In: |
42 | 0 | return SVG_LENGTHTYPE_IN; |
43 | 0 | case CSS::Length::Type::Pt: |
44 | 0 | return SVG_LENGTHTYPE_PT; |
45 | 0 | case CSS::Length::Type::Pc: |
46 | 0 | return SVG_LENGTHTYPE_PC; |
47 | 0 | default: |
48 | 0 | return SVG_LENGTHTYPE_UNKNOWN; |
49 | 0 | } |
50 | 0 | }(), |
51 | 0 | length_percentage.length().raw_value()); |
52 | 0 | return SVGLength::create(realm, SVG_LENGTHTYPE_UNKNOWN, 0); |
53 | 0 | } |
54 | | |
55 | | SVGLength::SVGLength(JS::Realm& realm, u8 unit_type, float value) |
56 | 0 | : PlatformObject(realm) |
57 | 0 | , m_unit_type(unit_type) |
58 | 0 | , m_value(value) |
59 | 0 | { |
60 | 0 | } |
61 | | |
62 | | void SVGLength::initialize(JS::Realm& realm) |
63 | 0 | { |
64 | 0 | Base::initialize(realm); |
65 | 0 | WEB_SET_PROTOTYPE_FOR_INTERFACE(SVGLength); |
66 | 0 | } |
67 | | |
68 | 0 | SVGLength::~SVGLength() = default; |
69 | | |
70 | | // https://www.w3.org/TR/SVG11/types.html#__svg__SVGLength__value |
71 | | WebIDL::ExceptionOr<void> SVGLength::set_value(float value) |
72 | 0 | { |
73 | | // FIXME: Raise an exception if this <length> is read-only. |
74 | 0 | m_value = value; |
75 | 0 | return {}; |
76 | 0 | } |
77 | | |
78 | | } |