mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 02:37:36 +00:00
LibWeb: Add SVG <polygon>
element and test case :^)
This commit is contained in:
parent
116a1f485c
commit
2fad940b0b
10 changed files with 114 additions and 1 deletions
54
Userland/Libraries/LibWeb/SVG/SVGPolygonElement.cpp
Normal file
54
Userland/Libraries/LibWeb/SVG/SVGPolygonElement.cpp
Normal file
|
@ -0,0 +1,54 @@
|
|||
/*
|
||||
* Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include "SVGPolygonElement.h"
|
||||
#include <LibWeb/SVG/AttributeNames.h>
|
||||
#include <LibWeb/SVG/AttributeParser.h>
|
||||
|
||||
namespace Web::SVG {
|
||||
|
||||
SVGPolygonElement::SVGPolygonElement(DOM::Document& document, QualifiedName qualified_name)
|
||||
: SVGGeometryElement(document, qualified_name)
|
||||
{
|
||||
}
|
||||
|
||||
void SVGPolygonElement::parse_attribute(FlyString const& name, String const& value)
|
||||
{
|
||||
SVGGeometryElement::parse_attribute(name, value);
|
||||
|
||||
if (name == SVG::AttributeNames::points) {
|
||||
m_points = AttributeParser::parse_points(value);
|
||||
m_path.clear();
|
||||
}
|
||||
}
|
||||
|
||||
Gfx::Path& SVGPolygonElement::get_path()
|
||||
{
|
||||
if (m_path.has_value())
|
||||
return m_path.value();
|
||||
|
||||
Gfx::Path path;
|
||||
|
||||
if (m_points.is_empty()) {
|
||||
m_path = move(path);
|
||||
return m_path.value();
|
||||
}
|
||||
|
||||
// 1. perform an absolute moveto operation to the first coordinate pair in the list of points
|
||||
path.move_to(m_points.first());
|
||||
|
||||
// 2. for each subsequent coordinate pair, perform an absolute lineto operation to that coordinate pair.
|
||||
for (size_t point_index = 1; point_index < m_points.size(); ++point_index)
|
||||
path.line_to(m_points[point_index]);
|
||||
|
||||
// 3. perform a closepath command
|
||||
path.close();
|
||||
|
||||
m_path = move(path);
|
||||
return m_path.value();
|
||||
}
|
||||
|
||||
}
|
30
Userland/Libraries/LibWeb/SVG/SVGPolygonElement.h
Normal file
30
Userland/Libraries/LibWeb/SVG/SVGPolygonElement.h
Normal file
|
@ -0,0 +1,30 @@
|
|||
/*
|
||||
* Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <LibWeb/SVG/SVGGeometryElement.h>
|
||||
|
||||
namespace Web::SVG {
|
||||
|
||||
class SVGPolygonElement final : public SVGGeometryElement {
|
||||
public:
|
||||
using WrapperType = Bindings::SVGPolygonElementWrapper;
|
||||
|
||||
SVGPolygonElement(DOM::Document&, QualifiedName);
|
||||
virtual ~SVGPolygonElement() override = default;
|
||||
|
||||
virtual void parse_attribute(FlyString const& name, String const& value) override;
|
||||
|
||||
virtual Gfx::Path& get_path() override;
|
||||
|
||||
private:
|
||||
Optional<Gfx::Path> m_path;
|
||||
|
||||
Vector<Gfx::FloatPoint> m_points;
|
||||
};
|
||||
|
||||
}
|
5
Userland/Libraries/LibWeb/SVG/SVGPolygonElement.idl
Normal file
5
Userland/Libraries/LibWeb/SVG/SVGPolygonElement.idl
Normal file
|
@ -0,0 +1,5 @@
|
|||
[Exposed=Window]
|
||||
interface SVGPolygonElement : SVGGeometryElement {
|
||||
};
|
||||
|
||||
// SVGPolygonElement includes SVGAnimatedPoints;
|
|
@ -16,6 +16,7 @@ namespace Web::SVG::TagNames {
|
|||
__ENUMERATE_SVG_TAG(g) \
|
||||
__ENUMERATE_SVG_TAG(line) \
|
||||
__ENUMERATE_SVG_TAG(path) \
|
||||
__ENUMERATE_SVG_TAG(polygon) \
|
||||
__ENUMERATE_SVG_TAG(polyline) \
|
||||
__ENUMERATE_SVG_TAG(rect) \
|
||||
__ENUMERATE_SVG_TAG(svg)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue