2020-07-22 15:17:39 -07:00
|
|
|
/*
|
2021-04-22 16:53:07 -07:00
|
|
|
* Copyright (c) 2020, Matthew Olsson <mattco@serenityos.org>
|
2021-09-16 12:28:14 +01:00
|
|
|
* Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
|
2020-07-22 15:17:39 -07:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-07-22 15:17:39 -07:00
|
|
|
*/
|
|
|
|
|
2020-07-23 09:44:42 -07:00
|
|
|
#include <LibWeb/SVG/SVGGraphicsElement.h>
|
2020-07-22 15:17:39 -07:00
|
|
|
|
2020-07-23 09:44:42 -07:00
|
|
|
namespace Web::SVG {
|
2020-07-22 15:17:39 -07:00
|
|
|
|
2021-02-07 11:20:15 +01:00
|
|
|
SVGGraphicsElement::SVGGraphicsElement(DOM::Document& document, QualifiedName qualified_name)
|
|
|
|
: SVGElement(document, move(qualified_name))
|
2020-07-22 15:17:39 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-09-16 12:28:14 +01:00
|
|
|
void SVGGraphicsElement::parse_attribute(FlyString const& name, String const& value)
|
2020-07-22 15:17:39 -07:00
|
|
|
{
|
2020-07-23 09:44:42 -07:00
|
|
|
SVGElement::parse_attribute(name, value);
|
2020-07-22 15:17:39 -07:00
|
|
|
|
2020-07-23 09:44:42 -07:00
|
|
|
if (name == "fill") {
|
|
|
|
m_fill_color = Gfx::Color::from_string(value).value_or(Color::Transparent);
|
|
|
|
} else if (name == "stroke") {
|
|
|
|
m_stroke_color = Gfx::Color::from_string(value).value_or(Color::Transparent);
|
|
|
|
} else if (name == "stroke-width") {
|
|
|
|
auto result = value.to_int();
|
|
|
|
if (result.has_value())
|
|
|
|
m_stroke_width = result.value();
|
|
|
|
}
|
2020-07-22 15:17:39 -07:00
|
|
|
}
|
|
|
|
|
2021-09-16 12:28:14 +01:00
|
|
|
Optional<Gfx::Color> SVGGraphicsElement::fill_color() const
|
|
|
|
{
|
|
|
|
if (m_fill_color.has_value())
|
|
|
|
return m_fill_color;
|
|
|
|
if (!layout_node())
|
|
|
|
return {};
|
|
|
|
// FIXME: In the working-draft spec, `fill` is intended to be a shorthand, with `fill-color`
|
|
|
|
// being what we actually want to use. But that's not final or widely supported yet.
|
|
|
|
return layout_node()->computed_values().fill();
|
|
|
|
}
|
|
|
|
|
|
|
|
Optional<Gfx::Color> SVGGraphicsElement::stroke_color() const
|
|
|
|
{
|
|
|
|
if (m_stroke_color.has_value())
|
|
|
|
return m_stroke_color;
|
|
|
|
if (!layout_node())
|
|
|
|
return {};
|
|
|
|
// FIXME: In the working-draft spec, `stroke` is intended to be a shorthand, with `stroke-color`
|
|
|
|
// being what we actually want to use. But that's not final or widely supported yet.
|
|
|
|
return layout_node()->computed_values().stroke();
|
|
|
|
}
|
|
|
|
|
|
|
|
Optional<float> SVGGraphicsElement::stroke_width() const
|
|
|
|
{
|
|
|
|
if (m_stroke_width.has_value())
|
|
|
|
return m_stroke_width;
|
|
|
|
if (!layout_node())
|
|
|
|
return {};
|
|
|
|
// FIXME: Converting to pixels isn't really correct - values should be in "user units"
|
|
|
|
// https://svgwg.org/svg2-draft/coords.html#TermUserUnits
|
|
|
|
if (auto width = layout_node()->computed_values().stroke_width(); width.has_value())
|
|
|
|
return width->to_px(*layout_node());
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2020-07-22 15:17:39 -07:00
|
|
|
}
|