From ebf3829f1cfc7f19f53bae46a42fcf130952250c Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Mon, 21 Mar 2022 14:20:48 -0400 Subject: [PATCH] LibWeb: Begin implementing the SVGLength type There are a few unimplemented features for this type: 1. The value setter should throw a DOMException if it is invoked on an SVGLength that was declared readonly in another IDL file. 2. SVG::AttributeParser does not parse unit types when it parses lengths so all SVGLength will have an "unknown" unit for now. 3. Due to (2), methods which convert between units are unimplemented. --- Userland/Libraries/LibWeb/CMakeLists.txt | 2 ++ Userland/Libraries/LibWeb/Forward.h | 2 ++ Userland/Libraries/LibWeb/SVG/SVGLength.cpp | 30 ++++++++++++++++ Userland/Libraries/LibWeb/SVG/SVGLength.h | 40 +++++++++++++++++++++ Userland/Libraries/LibWeb/SVG/SVGLength.idl | 24 +++++++++++++ 5 files changed, 98 insertions(+) create mode 100644 Userland/Libraries/LibWeb/SVG/SVGLength.cpp create mode 100644 Userland/Libraries/LibWeb/SVG/SVGLength.h create mode 100644 Userland/Libraries/LibWeb/SVG/SVGLength.idl diff --git a/Userland/Libraries/LibWeb/CMakeLists.txt b/Userland/Libraries/LibWeb/CMakeLists.txt index e5895b71ac..aabfab54be 100644 --- a/Userland/Libraries/LibWeb/CMakeLists.txt +++ b/Userland/Libraries/LibWeb/CMakeLists.txt @@ -317,6 +317,7 @@ set(SOURCES SVG/SVGPathElement.cpp SVG/SVGCircleElement.cpp SVG/SVGEllipseElement.cpp + SVG/SVGLength.cpp SVG/SVGLineElement.cpp SVG/SVGPolygonElement.cpp SVG/SVGPolylineElement.cpp @@ -575,6 +576,7 @@ libweb_js_wrapper(SVG/SVGGeometryElement) libweb_js_wrapper(SVG/SVGGraphicsElement) libweb_js_wrapper(SVG/SVGCircleElement) libweb_js_wrapper(SVG/SVGEllipseElement) +libweb_js_wrapper(SVG/SVGLength) libweb_js_wrapper(SVG/SVGLineElement) libweb_js_wrapper(SVG/SVGPathElement) libweb_js_wrapper(SVG/SVGPolygonElement) diff --git a/Userland/Libraries/LibWeb/Forward.h b/Userland/Libraries/LibWeb/Forward.h index 42de3509cc..0749d21b6c 100644 --- a/Userland/Libraries/LibWeb/Forward.h +++ b/Userland/Libraries/LibWeb/Forward.h @@ -291,6 +291,7 @@ class SVGElement; class SVGEllipseElement; class SVGGeometryElement; class SVGGraphicsElement; +class SVGLength; class SVGLineElement; class SVGPathElement; class SVGPolygonElement; @@ -514,6 +515,7 @@ class SVGElementWrapper; class SVGEllipseElementWrapper; class SVGGeometryElementWrapper; class SVGGraphicsElementWrapper; +class SVGLengthWrapper; class SVGLineElementWrapper; class SVGPathElementWrapper; class SVGPolygonElementWrapper; diff --git a/Userland/Libraries/LibWeb/SVG/SVGLength.cpp b/Userland/Libraries/LibWeb/SVG/SVGLength.cpp new file mode 100644 index 0000000000..11c3865479 --- /dev/null +++ b/Userland/Libraries/LibWeb/SVG/SVGLength.cpp @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2022, Tim Flynn + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include + +namespace Web::SVG { + +NonnullRefPtr SVGLength::create(u8 unit_type, float value) +{ + return adopt_ref(*new SVGLength(unit_type, value)); +} + +SVGLength::SVGLength(u8 unit_type, float value) + : m_unit_type(unit_type) + , m_value(value) +{ +} + +// https://www.w3.org/TR/SVG11/types.html#__svg__SVGLength__value +DOM::ExceptionOr SVGLength::set_value(float value) +{ + // FIXME: Raise an exception if this is read-only. + m_value = value; + return {}; +} + +} diff --git a/Userland/Libraries/LibWeb/SVG/SVGLength.h b/Userland/Libraries/LibWeb/SVG/SVGLength.h new file mode 100644 index 0000000000..9e838775eb --- /dev/null +++ b/Userland/Libraries/LibWeb/SVG/SVGLength.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2022, Tim Flynn + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include +#include +#include +#include +#include + +namespace Web::SVG { + +// https://www.w3.org/TR/SVG11/types.html#InterfaceSVGLength +class SVGLength + : public RefCounted + , public Bindings::Wrappable + , public Weakable { +public: + using WrapperType = Bindings::SVGLengthWrapper; + + static NonnullRefPtr create(u8 unit_type, float value); + virtual ~SVGLength() = default; + + u8 unit_type() const { return m_unit_type; } + + float value() const { return m_value; } + DOM::ExceptionOr set_value(float value); + +private: + SVGLength(u8 unit_type, float value); + + u8 m_unit_type { 0 }; + float m_value { 0 }; +}; + +} diff --git a/Userland/Libraries/LibWeb/SVG/SVGLength.idl b/Userland/Libraries/LibWeb/SVG/SVGLength.idl new file mode 100644 index 0000000000..08e4e29e85 --- /dev/null +++ b/Userland/Libraries/LibWeb/SVG/SVGLength.idl @@ -0,0 +1,24 @@ +interface SVGLength { + const unsigned short SVG_LENGTHTYPE_UNKNOWN = 0; + const unsigned short SVG_LENGTHTYPE_NUMBER = 1; + const unsigned short SVG_LENGTHTYPE_PERCENTAGE = 2; + const unsigned short SVG_LENGTHTYPE_EMS = 3; + const unsigned short SVG_LENGTHTYPE_EXS = 4; + const unsigned short SVG_LENGTHTYPE_PX = 5; + const unsigned short SVG_LENGTHTYPE_CM = 6; + const unsigned short SVG_LENGTHTYPE_MM = 7; + const unsigned short SVG_LENGTHTYPE_IN = 8; + const unsigned short SVG_LENGTHTYPE_PT = 9; + const unsigned short SVG_LENGTHTYPE_PC = 10; + + readonly attribute unsigned short unitType; + + // FIXME: Support setraises(). + attribute float value; // setraises(DOMException); + + // attribute float valueInSpecifiedUnits setraises(DOMException); + // attribute DOMString valueAsString setraises(DOMException); + + // void newValueSpecifiedUnits(in unsigned short unitType, in float valueInSpecifiedUnits) raises(DOMException); + // void convertToSpecifiedUnits(in unsigned short unitType) raises(DOMException); +};