SVGGraphicsElement.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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::fill_paint_style(SVGPaintContext const& paint_context) const
  37. {
  38. // FIXME: This entire function is an ad-hoc hack:
  39. if (!layout_node())
  40. return {};
  41. auto& fill = layout_node()->computed_values().fill();
  42. if (!fill.has_value() || !fill->is_url())
  43. return {};
  44. auto& url = fill->as_url();
  45. auto gradient = document().get_element_by_id(url.fragment());
  46. if (!gradient)
  47. return {};
  48. if (is<SVG::SVGGradientElement>(*gradient))
  49. return static_cast<SVG::SVGGradientElement const&>(*gradient).to_gfx_paint_style(paint_context);
  50. return {};
  51. }
  52. Gfx::AffineTransform transform_from_transform_list(ReadonlySpan<Transform> transform_list)
  53. {
  54. Gfx::AffineTransform affine_transform;
  55. auto to_radians = [](float degrees) {
  56. return degrees * (AK::Pi<float> / 180.0f);
  57. };
  58. for (auto& transform : transform_list) {
  59. transform.operation.visit(
  60. [&](Transform::Translate const& translate) {
  61. affine_transform.multiply(Gfx::AffineTransform {}.translate({ translate.x, translate.y }));
  62. },
  63. [&](Transform::Scale const& scale) {
  64. affine_transform.multiply(Gfx::AffineTransform {}.scale({ scale.x, scale.y }));
  65. },
  66. [&](Transform::Rotate const& rotate) {
  67. Gfx::AffineTransform translate_transform;
  68. affine_transform.multiply(
  69. Gfx::AffineTransform {}
  70. .translate({ rotate.x, rotate.y })
  71. .rotate_radians(to_radians(rotate.a))
  72. .translate({ -rotate.x, -rotate.y }));
  73. },
  74. [&](Transform::SkewX const& skew_x) {
  75. affine_transform.multiply(Gfx::AffineTransform {}.skew_radians(to_radians(skew_x.a), 0));
  76. },
  77. [&](Transform::SkewY const& skew_y) {
  78. affine_transform.multiply(Gfx::AffineTransform {}.skew_radians(0, to_radians(skew_y.a)));
  79. },
  80. [&](Transform::Matrix const& matrix) {
  81. affine_transform.multiply(Gfx::AffineTransform {
  82. matrix.a, matrix.b, matrix.c, matrix.d, matrix.e, matrix.f });
  83. });
  84. }
  85. return affine_transform;
  86. }
  87. Gfx::AffineTransform SVGGraphicsElement::get_transform() const
  88. {
  89. // FIXME: It would be nice to do this using the SVGContext, however, then layout/hit testing knows nothing about the transform.
  90. Gfx::AffineTransform transform = m_transform;
  91. for (auto* svg_ancestor = first_ancestor_of_type<SVGGraphicsElement>(); svg_ancestor; svg_ancestor = svg_ancestor->first_ancestor_of_type<SVGGraphicsElement>()) {
  92. transform = Gfx::AffineTransform { svg_ancestor->m_transform }.multiply(transform);
  93. }
  94. return transform;
  95. }
  96. void SVGGraphicsElement::apply_presentational_hints(CSS::StyleProperties& style) const
  97. {
  98. CSS::Parser::ParsingContext parsing_context { document() };
  99. for_each_attribute([&](auto& name, auto& value) {
  100. if (name.equals_ignoring_ascii_case("fill"sv)) {
  101. // FIXME: The `fill` attribute and CSS `fill` property are not the same! But our support is limited enough that they are equivalent for now.
  102. if (auto fill_value = parse_css_value(parsing_context, value, CSS::PropertyID::Fill).release_value_but_fixme_should_propagate_errors())
  103. style.set_property(CSS::PropertyID::Fill, fill_value.release_nonnull());
  104. } else if (name.equals_ignoring_ascii_case("stroke"sv)) {
  105. // FIXME: The `stroke` attribute and CSS `stroke` property are not the same! But our support is limited enough that they are equivalent for now.
  106. if (auto stroke_value = parse_css_value(parsing_context, value, CSS::PropertyID::Stroke).release_value_but_fixme_should_propagate_errors())
  107. style.set_property(CSS::PropertyID::Stroke, stroke_value.release_nonnull());
  108. } else if (name.equals_ignoring_ascii_case("stroke-width"sv)) {
  109. if (auto stroke_width_value = parse_css_value(parsing_context, value, CSS::PropertyID::StrokeWidth).release_value_but_fixme_should_propagate_errors())
  110. style.set_property(CSS::PropertyID::StrokeWidth, stroke_width_value.release_nonnull());
  111. } else if (name.equals_ignoring_ascii_case("fill-opacity"sv)) {
  112. if (auto fill_opacity_value = parse_css_value(parsing_context, value, CSS::PropertyID::FillOpacity).release_value_but_fixme_should_propagate_errors())
  113. style.set_property(CSS::PropertyID::FillOpacity, fill_opacity_value.release_nonnull());
  114. } else if (name.equals_ignoring_ascii_case("stroke-opacity"sv)) {
  115. if (auto stroke_opacity_value = parse_css_value(parsing_context, value, CSS::PropertyID::FillOpacity).release_value_but_fixme_should_propagate_errors())
  116. style.set_property(CSS::PropertyID::StrokeOpacity, stroke_opacity_value.release_nonnull());
  117. }
  118. });
  119. }
  120. Optional<Gfx::Color> SVGGraphicsElement::fill_color() const
  121. {
  122. if (!layout_node())
  123. return {};
  124. // FIXME: In the working-draft spec, `fill` is intended to be a shorthand, with `fill-color`
  125. // being what we actually want to use. But that's not final or widely supported yet.
  126. return layout_node()->computed_values().fill().map([&](auto& paint) -> Gfx::Color {
  127. if (!paint.is_color())
  128. return Color::Black;
  129. return paint.as_color();
  130. });
  131. }
  132. Optional<Gfx::Color> SVGGraphicsElement::stroke_color() const
  133. {
  134. if (!layout_node())
  135. return {};
  136. // FIXME: In the working-draft spec, `stroke` is intended to be a shorthand, with `stroke-color`
  137. // being what we actually want to use. But that's not final or widely supported yet.
  138. return layout_node()->computed_values().stroke().map([](auto& paint) -> Gfx::Color {
  139. if (!paint.is_color())
  140. return Color::Black;
  141. return paint.as_color();
  142. });
  143. }
  144. Optional<float> SVGGraphicsElement::fill_opacity() const
  145. {
  146. if (!layout_node())
  147. return {};
  148. return layout_node()->computed_values().fill_opacity();
  149. }
  150. Optional<float> SVGGraphicsElement::stroke_opacity() const
  151. {
  152. if (!layout_node())
  153. return {};
  154. return layout_node()->computed_values().stroke_opacity();
  155. }
  156. Optional<float> SVGGraphicsElement::stroke_width() const
  157. {
  158. if (!layout_node())
  159. return {};
  160. // FIXME: Converting to pixels isn't really correct - values should be in "user units"
  161. // https://svgwg.org/svg2-draft/coords.html#TermUserUnits
  162. if (auto width = layout_node()->computed_values().stroke_width(); width.has_value()) {
  163. // Resolved relative to the "Scaled viewport size": https://www.w3.org/TR/2017/WD-fill-stroke-3-20170413/#scaled-viewport-size
  164. // FIXME: This isn't right, but it's something.
  165. CSSPixels viewport_width = 0;
  166. CSSPixels viewport_height = 0;
  167. if (auto* svg_svg_element = first_ancestor_of_type<SVGSVGElement>()) {
  168. if (auto* svg_svg_layout_node = svg_svg_element->layout_node()) {
  169. viewport_width = svg_svg_layout_node->computed_values().width().to_px(*svg_svg_layout_node, 0);
  170. viewport_height = svg_svg_layout_node->computed_values().height().to_px(*svg_svg_layout_node, 0);
  171. }
  172. }
  173. auto scaled_viewport_size = (viewport_width + viewport_height) * 0.5f;
  174. return width->to_px(*layout_node(), scaled_viewport_size).value();
  175. }
  176. return {};
  177. }
  178. }