SVGGraphicsElement.cpp 7.8 KB

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