SVGGraphicsElement.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Copyright (c) 2020, Matthew Olsson <mattco@serenityos.org>
  3. * Copyright (c) 2021-2022, Sam Atkins <atkinssj@serenityos.org>
  4. * Copyright (c) 2023, MacDue <macdue@dueutil.tech>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <LibWeb/Bindings/Intrinsics.h>
  9. #include <LibWeb/CSS/Parser/Parser.h>
  10. #include <LibWeb/DOM/Document.h>
  11. #include <LibWeb/Layout/Node.h>
  12. #include <LibWeb/SVG/AttributeParser.h>
  13. #include <LibWeb/SVG/SVGGradientElement.h>
  14. #include <LibWeb/SVG/SVGGraphicsElement.h>
  15. #include <LibWeb/SVG/SVGSVGElement.h>
  16. namespace Web::SVG {
  17. SVGGraphicsElement::SVGGraphicsElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  18. : SVGElement(document, move(qualified_name))
  19. {
  20. }
  21. JS::ThrowCompletionOr<void> SVGGraphicsElement::initialize(JS::Realm& realm)
  22. {
  23. MUST_OR_THROW_OOM(Base::initialize(realm));
  24. set_prototype(&Bindings::ensure_web_prototype<Bindings::SVGGraphicsElementPrototype>(realm, "SVGGraphicsElement"));
  25. return {};
  26. }
  27. void SVGGraphicsElement::parse_attribute(DeprecatedFlyString const& name, DeprecatedString const& value)
  28. {
  29. SVGElement::parse_attribute(name, value);
  30. if (name == "transform"sv) {
  31. auto transform_list = AttributeParser::parse_transform(value);
  32. if (transform_list.has_value())
  33. m_transform = transform_from_transform_list(*transform_list);
  34. }
  35. }
  36. Optional<Gfx::PaintStyle const&> SVGGraphicsElement::svg_paint_computed_value_to_gfx_paint_style(SVGPaintContext const& paint_context, Optional<CSS::SVGPaint> const& paint_value) const
  37. {
  38. // FIXME: This entire function is an ad-hoc hack:
  39. if (!paint_value.has_value() || !paint_value->is_url())
  40. return {};
  41. auto& url = paint_value->as_url();
  42. auto gradient = document().get_element_by_id(url.fragment());
  43. if (!gradient)
  44. return {};
  45. if (is<SVG::SVGGradientElement>(*gradient))
  46. return static_cast<SVG::SVGGradientElement const&>(*gradient).to_gfx_paint_style(paint_context);
  47. return {};
  48. }
  49. Optional<Gfx::PaintStyle const&> SVGGraphicsElement::fill_paint_style(SVGPaintContext const& paint_context) const
  50. {
  51. if (!layout_node())
  52. return {};
  53. return svg_paint_computed_value_to_gfx_paint_style(paint_context, layout_node()->computed_values().fill());
  54. }
  55. Optional<Gfx::PaintStyle const&> SVGGraphicsElement::stroke_paint_style(SVGPaintContext const& paint_context) const
  56. {
  57. if (!layout_node())
  58. return {};
  59. return svg_paint_computed_value_to_gfx_paint_style(paint_context, layout_node()->computed_values().stroke());
  60. }
  61. Gfx::AffineTransform transform_from_transform_list(ReadonlySpan<Transform> transform_list)
  62. {
  63. Gfx::AffineTransform affine_transform;
  64. auto to_radians = [](float degrees) {
  65. return degrees * (AK::Pi<float> / 180.0f);
  66. };
  67. for (auto& transform : transform_list) {
  68. transform.operation.visit(
  69. [&](Transform::Translate const& translate) {
  70. affine_transform.multiply(Gfx::AffineTransform {}.translate({ translate.x, translate.y }));
  71. },
  72. [&](Transform::Scale const& scale) {
  73. affine_transform.multiply(Gfx::AffineTransform {}.scale({ scale.x, scale.y }));
  74. },
  75. [&](Transform::Rotate const& rotate) {
  76. Gfx::AffineTransform translate_transform;
  77. affine_transform.multiply(
  78. Gfx::AffineTransform {}
  79. .translate({ rotate.x, rotate.y })
  80. .rotate_radians(to_radians(rotate.a))
  81. .translate({ -rotate.x, -rotate.y }));
  82. },
  83. [&](Transform::SkewX const& skew_x) {
  84. affine_transform.multiply(Gfx::AffineTransform {}.skew_radians(to_radians(skew_x.a), 0));
  85. },
  86. [&](Transform::SkewY const& skew_y) {
  87. affine_transform.multiply(Gfx::AffineTransform {}.skew_radians(0, to_radians(skew_y.a)));
  88. },
  89. [&](Transform::Matrix const& matrix) {
  90. affine_transform.multiply(Gfx::AffineTransform {
  91. matrix.a, matrix.b, matrix.c, matrix.d, matrix.e, matrix.f });
  92. });
  93. }
  94. return affine_transform;
  95. }
  96. Gfx::AffineTransform SVGGraphicsElement::get_transform() const
  97. {
  98. // FIXME: It would be nice to do this using the SVGContext, however, then layout/hit testing knows nothing about the transform.
  99. Gfx::AffineTransform transform = m_transform;
  100. for (auto* svg_ancestor = shadow_including_first_ancestor_of_type<SVGGraphicsElement>(); svg_ancestor; svg_ancestor = svg_ancestor->shadow_including_first_ancestor_of_type<SVGGraphicsElement>()) {
  101. transform = Gfx::AffineTransform { svg_ancestor->m_transform }.multiply(transform);
  102. }
  103. return transform;
  104. }
  105. void SVGGraphicsElement::apply_presentational_hints(CSS::StyleProperties& style) const
  106. {
  107. CSS::Parser::ParsingContext parsing_context { document() };
  108. for_each_attribute([&](auto& name, auto& value) {
  109. if (name.equals_ignoring_ascii_case("fill"sv)) {
  110. // FIXME: The `fill` attribute and CSS `fill` property are not the same! But our support is limited enough that they are equivalent for now.
  111. if (auto fill_value = parse_css_value(parsing_context, value, CSS::PropertyID::Fill).release_value_but_fixme_should_propagate_errors())
  112. style.set_property(CSS::PropertyID::Fill, fill_value.release_nonnull());
  113. } else if (name.equals_ignoring_ascii_case("stroke"sv)) {
  114. // FIXME: The `stroke` attribute and CSS `stroke` property are not the same! But our support is limited enough that they are equivalent for now.
  115. if (auto stroke_value = parse_css_value(parsing_context, value, CSS::PropertyID::Stroke).release_value_but_fixme_should_propagate_errors())
  116. style.set_property(CSS::PropertyID::Stroke, stroke_value.release_nonnull());
  117. } else if (name.equals_ignoring_ascii_case("stroke-width"sv)) {
  118. if (auto stroke_width_value = parse_css_value(parsing_context, value, CSS::PropertyID::StrokeWidth).release_value_but_fixme_should_propagate_errors())
  119. style.set_property(CSS::PropertyID::StrokeWidth, stroke_width_value.release_nonnull());
  120. } else if (name.equals_ignoring_ascii_case("fill-opacity"sv)) {
  121. if (auto fill_opacity_value = parse_css_value(parsing_context, value, CSS::PropertyID::FillOpacity).release_value_but_fixme_should_propagate_errors())
  122. style.set_property(CSS::PropertyID::FillOpacity, fill_opacity_value.release_nonnull());
  123. } else if (name.equals_ignoring_ascii_case("stroke-opacity"sv)) {
  124. if (auto stroke_opacity_value = parse_css_value(parsing_context, value, CSS::PropertyID::FillOpacity).release_value_but_fixme_should_propagate_errors())
  125. style.set_property(CSS::PropertyID::StrokeOpacity, stroke_opacity_value.release_nonnull());
  126. }
  127. });
  128. }
  129. Optional<Gfx::Color> SVGGraphicsElement::fill_color() const
  130. {
  131. if (!layout_node())
  132. return {};
  133. // FIXME: In the working-draft spec, `fill` is intended to be a shorthand, with `fill-color`
  134. // being what we actually want to use. But that's not final or widely supported yet.
  135. return layout_node()->computed_values().fill().map([&](auto& paint) -> Gfx::Color {
  136. if (!paint.is_color())
  137. return Color::Black;
  138. return paint.as_color();
  139. });
  140. }
  141. Optional<Gfx::Color> SVGGraphicsElement::stroke_color() const
  142. {
  143. if (!layout_node())
  144. return {};
  145. // FIXME: In the working-draft spec, `stroke` is intended to be a shorthand, with `stroke-color`
  146. // being what we actually want to use. But that's not final or widely supported yet.
  147. return layout_node()->computed_values().stroke().map([](auto& paint) -> Gfx::Color {
  148. if (!paint.is_color())
  149. return Color::Black;
  150. return paint.as_color();
  151. });
  152. }
  153. Optional<float> SVGGraphicsElement::fill_opacity() const
  154. {
  155. if (!layout_node())
  156. return {};
  157. return layout_node()->computed_values().fill_opacity();
  158. }
  159. Optional<float> SVGGraphicsElement::stroke_opacity() const
  160. {
  161. if (!layout_node())
  162. return {};
  163. return layout_node()->computed_values().stroke_opacity();
  164. }
  165. Optional<float> SVGGraphicsElement::stroke_width() const
  166. {
  167. if (!layout_node())
  168. return {};
  169. // FIXME: Converting to pixels isn't really correct - values should be in "user units"
  170. // https://svgwg.org/svg2-draft/coords.html#TermUserUnits
  171. if (auto width = layout_node()->computed_values().stroke_width(); width.has_value()) {
  172. // Resolved relative to the "Scaled viewport size": https://www.w3.org/TR/2017/WD-fill-stroke-3-20170413/#scaled-viewport-size
  173. // FIXME: This isn't right, but it's something.
  174. CSSPixels viewport_width = 0;
  175. CSSPixels viewport_height = 0;
  176. if (auto* svg_svg_element = shadow_including_first_ancestor_of_type<SVGSVGElement>()) {
  177. if (auto* svg_svg_layout_node = svg_svg_element->layout_node()) {
  178. viewport_width = svg_svg_layout_node->computed_values().width().to_px(*svg_svg_layout_node, 0);
  179. viewport_height = svg_svg_layout_node->computed_values().height().to_px(*svg_svg_layout_node, 0);
  180. }
  181. }
  182. auto scaled_viewport_size = (viewport_width + viewport_height) * 0.5;
  183. return width->to_px(*layout_node(), scaled_viewport_size).value();
  184. }
  185. return {};
  186. }
  187. }