SVGGraphicsElement.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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/Layout/Node.h>
  11. #include <LibWeb/SVG/AttributeParser.h>
  12. #include <LibWeb/SVG/SVGGraphicsElement.h>
  13. #include <LibWeb/SVG/SVGSVGElement.h>
  14. namespace Web::SVG {
  15. SVGGraphicsElement::SVGGraphicsElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  16. : SVGElement(document, move(qualified_name))
  17. {
  18. }
  19. JS::ThrowCompletionOr<void> SVGGraphicsElement::initialize(JS::Realm& realm)
  20. {
  21. MUST_OR_THROW_OOM(Base::initialize(realm));
  22. set_prototype(&Bindings::ensure_web_prototype<Bindings::SVGGraphicsElementPrototype>(realm, "SVGGraphicsElement"));
  23. return {};
  24. }
  25. void SVGGraphicsElement::parse_attribute(DeprecatedFlyString const& name, DeprecatedString const& value)
  26. {
  27. SVGElement::parse_attribute(name, value);
  28. if (name == "fill-opacity"sv) {
  29. m_fill_opacity = AttributeParser::parse_length(value);
  30. } else 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. Gfx::AffineTransform transform_from_transform_list(ReadonlySpan<Transform> transform_list)
  37. {
  38. Gfx::AffineTransform affine_transform;
  39. auto to_radians = [](float degrees) {
  40. return degrees * (AK::Pi<float> / 180.0f);
  41. };
  42. for (auto& transform : transform_list) {
  43. transform.operation.visit(
  44. [&](Transform::Translate const& translate) {
  45. affine_transform.multiply(Gfx::AffineTransform {}.translate({ translate.x, translate.y }));
  46. },
  47. [&](Transform::Scale const& scale) {
  48. affine_transform.multiply(Gfx::AffineTransform {}.scale({ scale.x, scale.y }));
  49. },
  50. [&](Transform::Rotate const& rotate) {
  51. Gfx::AffineTransform translate_transform;
  52. affine_transform.multiply(
  53. Gfx::AffineTransform {}
  54. .translate({ rotate.x, rotate.y })
  55. .rotate_radians(to_radians(rotate.a))
  56. .translate({ -rotate.x, -rotate.y }));
  57. },
  58. [&](Transform::SkewX const& skew_x) {
  59. affine_transform.multiply(Gfx::AffineTransform {}.skew_radians(to_radians(skew_x.a), 0));
  60. },
  61. [&](Transform::SkewY const& skew_y) {
  62. affine_transform.multiply(Gfx::AffineTransform {}.skew_radians(0, to_radians(skew_y.a)));
  63. },
  64. [&](Transform::Matrix const& matrix) {
  65. affine_transform.multiply(Gfx::AffineTransform {
  66. matrix.a, matrix.b, matrix.c, matrix.d, matrix.e, matrix.f });
  67. });
  68. }
  69. return affine_transform;
  70. }
  71. Gfx::AffineTransform SVGGraphicsElement::get_transform() const
  72. {
  73. // FIXME: It would be nice to do this using the SVGContext, however, then layout/hit testing knows nothing about the transform.
  74. Gfx::AffineTransform transform = m_transform;
  75. for (auto* svg_ancestor = first_ancestor_of_type<SVGGraphicsElement>(); svg_ancestor; svg_ancestor = svg_ancestor->first_ancestor_of_type<SVGGraphicsElement>()) {
  76. transform = Gfx::AffineTransform { svg_ancestor->m_transform }.multiply(transform);
  77. }
  78. return transform;
  79. }
  80. void SVGGraphicsElement::apply_presentational_hints(CSS::StyleProperties& style) const
  81. {
  82. CSS::Parser::ParsingContext parsing_context { document() };
  83. for_each_attribute([&](auto& name, auto& value) {
  84. if (name.equals_ignoring_ascii_case("fill"sv)) {
  85. // FIXME: The `fill` attribute and CSS `fill` property are not the same! But our support is limited enough that they are equivalent for now.
  86. if (auto fill_value = parse_css_value(parsing_context, value, CSS::PropertyID::Fill))
  87. style.set_property(CSS::PropertyID::Fill, fill_value.release_nonnull());
  88. } else if (name.equals_ignoring_ascii_case("stroke"sv)) {
  89. // FIXME: The `stroke` attribute and CSS `stroke` property are not the same! But our support is limited enough that they are equivalent for now.
  90. if (auto stroke_value = parse_css_value(parsing_context, value, CSS::PropertyID::Stroke))
  91. style.set_property(CSS::PropertyID::Stroke, stroke_value.release_nonnull());
  92. } else if (name.equals_ignoring_ascii_case("stroke-width"sv)) {
  93. if (auto stroke_width_value = parse_css_value(parsing_context, value, CSS::PropertyID::StrokeWidth))
  94. style.set_property(CSS::PropertyID::StrokeWidth, stroke_width_value.release_nonnull());
  95. }
  96. });
  97. }
  98. Optional<Gfx::Color> SVGGraphicsElement::fill_color() const
  99. {
  100. if (!layout_node())
  101. return {};
  102. // FIXME: In the working-draft spec, `fill` is intended to be a shorthand, with `fill-color`
  103. // being what we actually want to use. But that's not final or widely supported yet.
  104. return layout_node()->computed_values().fill().map([&](Gfx::Color color) {
  105. return color.with_alpha(m_fill_opacity.value_or(1) * 255);
  106. });
  107. }
  108. Optional<Gfx::Color> SVGGraphicsElement::stroke_color() const
  109. {
  110. if (!layout_node())
  111. return {};
  112. // FIXME: In the working-draft spec, `stroke` is intended to be a shorthand, with `stroke-color`
  113. // being what we actually want to use. But that's not final or widely supported yet.
  114. return layout_node()->computed_values().stroke();
  115. }
  116. Optional<float> SVGGraphicsElement::stroke_width() const
  117. {
  118. if (!layout_node())
  119. return {};
  120. // FIXME: Converting to pixels isn't really correct - values should be in "user units"
  121. // https://svgwg.org/svg2-draft/coords.html#TermUserUnits
  122. if (auto width = layout_node()->computed_values().stroke_width(); width.has_value()) {
  123. // Resolved relative to the "Scaled viewport size": https://www.w3.org/TR/2017/WD-fill-stroke-3-20170413/#scaled-viewport-size
  124. // FIXME: This isn't right, but it's something.
  125. CSSPixels viewport_width = 0;
  126. CSSPixels viewport_height = 0;
  127. if (auto* svg_svg_element = first_ancestor_of_type<SVGSVGElement>()) {
  128. if (auto* svg_svg_layout_node = svg_svg_element->layout_node()) {
  129. viewport_width = svg_svg_layout_node->computed_values().width().resolved(*svg_svg_layout_node, CSS::Length::make_px(0)).to_px(*svg_svg_layout_node);
  130. viewport_height = svg_svg_layout_node->computed_values().height().resolved(*svg_svg_layout_node, CSS::Length::make_px(0)).to_px(*svg_svg_layout_node);
  131. }
  132. }
  133. auto scaled_viewport_size = CSS::Length::make_px((viewport_width + viewport_height) * 0.5f);
  134. return width->resolved(*layout_node(), scaled_viewport_size).to_px(*layout_node()).value();
  135. }
  136. return {};
  137. }
  138. }