SVGGraphicsElement.cpp 11 KB

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