SVGGraphicsElement.cpp 11 KB

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