SVGGraphicsElement.cpp 10 KB

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