2022-02-11 16:14:58 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
2024-08-09 14:00:10 +02:00
|
|
|
#include <LibGfx/Path.h>
|
2022-09-25 18:04:39 -06:00
|
|
|
#include <LibWeb/Bindings/Intrinsics.h>
|
2024-04-27 12:09:58 +12:00
|
|
|
#include <LibWeb/Bindings/SVGCircleElementPrototype.h>
|
2024-03-03 20:47:10 +00:00
|
|
|
#include <LibWeb/CSS/Parser/Parser.h>
|
|
|
|
#include <LibWeb/Layout/Node.h>
|
2022-02-11 16:14:58 +00:00
|
|
|
#include <LibWeb/SVG/AttributeNames.h>
|
|
|
|
#include <LibWeb/SVG/AttributeParser.h>
|
2022-08-28 13:42:07 +02:00
|
|
|
#include <LibWeb/SVG/SVGCircleElement.h>
|
2024-03-03 20:47:10 +00:00
|
|
|
#include <LibWeb/SVG/SVGViewport.h>
|
2022-02-11 16:14:58 +00:00
|
|
|
|
|
|
|
namespace Web::SVG {
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
GC_DEFINE_ALLOCATOR(SVGCircleElement);
|
2023-11-19 19:47:52 +01:00
|
|
|
|
2022-02-18 21:00:52 +01:00
|
|
|
SVGCircleElement::SVGCircleElement(DOM::Document& document, DOM::QualifiedName qualified_name)
|
2022-02-11 16:14:58 +00:00
|
|
|
: SVGGeometryElement(document, qualified_name)
|
|
|
|
{
|
2023-01-10 06:28:20 -05:00
|
|
|
}
|
|
|
|
|
2023-08-07 08:41:28 +02:00
|
|
|
void SVGCircleElement::initialize(JS::Realm& realm)
|
2023-01-10 06:28:20 -05:00
|
|
|
{
|
2024-03-16 13:13:08 +01:00
|
|
|
WEB_SET_PROTOTYPE_FOR_INTERFACE(SVGCircleElement);
|
2025-04-20 16:22:57 +02:00
|
|
|
Base::initialize(realm);
|
2022-02-11 16:14:58 +00:00
|
|
|
}
|
|
|
|
|
2024-12-23 17:51:10 +01:00
|
|
|
bool SVGCircleElement::is_presentational_hint(FlyString const& name) const
|
|
|
|
{
|
|
|
|
if (Base::is_presentational_hint(name))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return first_is_one_of(name,
|
|
|
|
SVG::AttributeNames::cx,
|
|
|
|
SVG::AttributeNames::cy,
|
|
|
|
SVG::AttributeNames::r);
|
|
|
|
}
|
|
|
|
|
LibWeb: Split StyleComputer work into two phases with separate outputs
Before this change, StyleComputer would essentially take a DOM element,
find all the CSS rules that apply to it, and resolve the computed value
for each CSS property for that element.
This worked great, but it meant we had to do all the work of selector
matching and cascading every time.
To enable new optimizations, this change introduces a break in the
middle of this process where we've produced a "CascadedProperties".
This object contains the result of the cascade, before we've begun
turning cascaded values into computed values.
The cascaded properties are now stored with each element, which will
later allow us to do partial updates without re-running the full
StyleComputer machine. This will be particularly valuable for
re-implementing CSS inheritance, which is extremely heavy today.
Note that CSS animations and CSS transitions operate entirely on the
computed values, even though the cascade order would have you believe
they happen earlier. I'm not confident we have the right architecture
for this, but that's a separate issue.
2024-12-12 10:06:29 +01:00
|
|
|
void SVGCircleElement::apply_presentational_hints(GC::Ref<CSS::CascadedProperties> cascaded_properties) const
|
2022-02-11 16:14:58 +00:00
|
|
|
{
|
LibWeb: Split StyleComputer work into two phases with separate outputs
Before this change, StyleComputer would essentially take a DOM element,
find all the CSS rules that apply to it, and resolve the computed value
for each CSS property for that element.
This worked great, but it meant we had to do all the work of selector
matching and cascading every time.
To enable new optimizations, this change introduces a break in the
middle of this process where we've produced a "CascadedProperties".
This object contains the result of the cascade, before we've begun
turning cascaded values into computed values.
The cascaded properties are now stored with each element, which will
later allow us to do partial updates without re-running the full
StyleComputer machine. This will be particularly valuable for
re-implementing CSS inheritance, which is extremely heavy today.
Note that CSS animations and CSS transitions operate entirely on the
computed values, even though the cascade order would have you believe
they happen earlier. I'm not confident we have the right architecture
for this, but that's a separate issue.
2024-12-12 10:06:29 +01:00
|
|
|
Base::apply_presentational_hints(cascaded_properties);
|
2025-02-05 12:08:27 +00:00
|
|
|
auto parsing_context = CSS::Parser::ParsingParams { document(), CSS::Parser::ParsingMode::SVGPresentationAttribute };
|
2024-03-03 20:47:10 +00:00
|
|
|
|
|
|
|
auto cx_attribute = attribute(SVG::AttributeNames::cx);
|
|
|
|
if (auto cx_value = parse_css_value(parsing_context, cx_attribute.value_or(String {}), CSS::PropertyID::Cx))
|
LibWeb: Split StyleComputer work into two phases with separate outputs
Before this change, StyleComputer would essentially take a DOM element,
find all the CSS rules that apply to it, and resolve the computed value
for each CSS property for that element.
This worked great, but it meant we had to do all the work of selector
matching and cascading every time.
To enable new optimizations, this change introduces a break in the
middle of this process where we've produced a "CascadedProperties".
This object contains the result of the cascade, before we've begun
turning cascaded values into computed values.
The cascaded properties are now stored with each element, which will
later allow us to do partial updates without re-running the full
StyleComputer machine. This will be particularly valuable for
re-implementing CSS inheritance, which is extremely heavy today.
Note that CSS animations and CSS transitions operate entirely on the
computed values, even though the cascade order would have you believe
they happen earlier. I'm not confident we have the right architecture
for this, but that's a separate issue.
2024-12-12 10:06:29 +01:00
|
|
|
cascaded_properties->set_property_from_presentational_hint(CSS::PropertyID::Cx, cx_value.release_nonnull());
|
2024-03-03 20:47:10 +00:00
|
|
|
|
|
|
|
auto cy_attribute = attribute(SVG::AttributeNames::cy);
|
|
|
|
if (auto cy_value = parse_css_value(parsing_context, cy_attribute.value_or(String {}), CSS::PropertyID::Cy))
|
LibWeb: Split StyleComputer work into two phases with separate outputs
Before this change, StyleComputer would essentially take a DOM element,
find all the CSS rules that apply to it, and resolve the computed value
for each CSS property for that element.
This worked great, but it meant we had to do all the work of selector
matching and cascading every time.
To enable new optimizations, this change introduces a break in the
middle of this process where we've produced a "CascadedProperties".
This object contains the result of the cascade, before we've begun
turning cascaded values into computed values.
The cascaded properties are now stored with each element, which will
later allow us to do partial updates without re-running the full
StyleComputer machine. This will be particularly valuable for
re-implementing CSS inheritance, which is extremely heavy today.
Note that CSS animations and CSS transitions operate entirely on the
computed values, even though the cascade order would have you believe
they happen earlier. I'm not confident we have the right architecture
for this, but that's a separate issue.
2024-12-12 10:06:29 +01:00
|
|
|
cascaded_properties->set_property_from_presentational_hint(CSS::PropertyID::Cy, cy_value.release_nonnull());
|
2024-03-03 20:47:10 +00:00
|
|
|
|
|
|
|
auto r_attribute = attribute(SVG::AttributeNames::r);
|
|
|
|
if (auto r_value = parse_css_value(parsing_context, r_attribute.value_or(String {}), CSS::PropertyID::R))
|
LibWeb: Split StyleComputer work into two phases with separate outputs
Before this change, StyleComputer would essentially take a DOM element,
find all the CSS rules that apply to it, and resolve the computed value
for each CSS property for that element.
This worked great, but it meant we had to do all the work of selector
matching and cascading every time.
To enable new optimizations, this change introduces a break in the
middle of this process where we've produced a "CascadedProperties".
This object contains the result of the cascade, before we've begun
turning cascaded values into computed values.
The cascaded properties are now stored with each element, which will
later allow us to do partial updates without re-running the full
StyleComputer machine. This will be particularly valuable for
re-implementing CSS inheritance, which is extremely heavy today.
Note that CSS animations and CSS transitions operate entirely on the
computed values, even though the cascade order would have you believe
they happen earlier. I'm not confident we have the right architecture
for this, but that's a separate issue.
2024-12-12 10:06:29 +01:00
|
|
|
cascaded_properties->set_property_from_presentational_hint(CSS::PropertyID::R, r_value.release_nonnull());
|
2022-02-11 16:14:58 +00:00
|
|
|
}
|
|
|
|
|
2024-08-09 14:00:10 +02:00
|
|
|
Gfx::Path SVGCircleElement::get_path(CSSPixelSize viewport_size)
|
2022-02-11 16:14:58 +00:00
|
|
|
{
|
2024-05-02 22:32:44 +12:00
|
|
|
auto node = layout_node();
|
2025-03-16 20:56:04 +13:00
|
|
|
if (!node) {
|
|
|
|
dbgln("FIXME: Null layout node in SVGCircleElement::get_path");
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2024-03-03 20:47:10 +00:00
|
|
|
auto cx = float(node->computed_values().cx().to_px(*node, viewport_size.width()));
|
|
|
|
auto cy = float(node->computed_values().cy().to_px(*node, viewport_size.height()));
|
|
|
|
// Percentages refer to the normalized diagonal of the current SVG viewport
|
|
|
|
// (see Units: https://svgwg.org/svg2-draft/coords.html#Units)
|
|
|
|
auto r = float(node->computed_values().r().to_px(*node, normalized_diagonal_length(viewport_size)));
|
2022-02-11 16:14:58 +00:00
|
|
|
|
|
|
|
// A zero radius disables rendering.
|
2024-03-03 20:15:06 +00:00
|
|
|
if (r == 0)
|
|
|
|
return {};
|
2022-02-11 16:14:58 +00:00
|
|
|
|
2024-08-09 14:00:10 +02:00
|
|
|
Gfx::Path path;
|
2022-02-11 16:14:58 +00:00
|
|
|
bool large_arc = false;
|
|
|
|
bool sweep = true;
|
|
|
|
|
|
|
|
// 1. A move-to command to the point cx+r,cy;
|
|
|
|
path.move_to({ cx + r, cy });
|
|
|
|
|
|
|
|
// 2. arc to cx,cy+r;
|
|
|
|
path.arc_to({ cx, cy + r }, r, large_arc, sweep);
|
|
|
|
|
|
|
|
// 3. arc to cx-r,cy;
|
|
|
|
path.arc_to({ cx - r, cy }, r, large_arc, sweep);
|
|
|
|
|
|
|
|
// 4. arc to cx,cy-r;
|
|
|
|
path.arc_to({ cx, cy - r }, r, large_arc, sweep);
|
|
|
|
|
|
|
|
// 5. arc with a segment-completing close path operation.
|
|
|
|
path.arc_to({ cx + r, cy }, r, large_arc, sweep);
|
|
|
|
|
2024-03-03 20:15:06 +00:00
|
|
|
return path;
|
2022-02-11 16:14:58 +00:00
|
|
|
}
|
|
|
|
|
2022-03-22 16:37:16 +00:00
|
|
|
// https://www.w3.org/TR/SVG11/shapes.html#CircleElementCXAttribute
|
2024-11-15 04:01:23 +13:00
|
|
|
GC::Ref<SVGAnimatedLength> SVGCircleElement::cx() const
|
2022-03-22 16:37:16 +00:00
|
|
|
{
|
2024-04-01 00:40:26 +01:00
|
|
|
return svg_animated_length_for_property(CSS::PropertyID::Cx);
|
2022-03-22 16:37:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// https://www.w3.org/TR/SVG11/shapes.html#CircleElementCYAttribute
|
2024-11-15 04:01:23 +13:00
|
|
|
GC::Ref<SVGAnimatedLength> SVGCircleElement::cy() const
|
2022-03-22 16:37:16 +00:00
|
|
|
{
|
2024-04-01 00:40:26 +01:00
|
|
|
return svg_animated_length_for_property(CSS::PropertyID::Cy);
|
2022-03-22 16:37:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// https://www.w3.org/TR/SVG11/shapes.html#CircleElementRAttribute
|
2024-11-15 04:01:23 +13:00
|
|
|
GC::Ref<SVGAnimatedLength> SVGCircleElement::r() const
|
2022-03-22 16:37:16 +00:00
|
|
|
{
|
2024-04-01 00:40:26 +01:00
|
|
|
return svg_animated_length_for_property(CSS::PropertyID::R);
|
2022-03-22 16:37:16 +00:00
|
|
|
}
|
|
|
|
|
2022-02-11 16:14:58 +00:00
|
|
|
}
|