SVGGraphicsElement.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. // FIXME: Hack to ensure unitless SVG properties (such as font-size) are parsed.
  109. FIXME::TemporarilyEnableQuirksMode enable_quirks(document());
  110. CSS::Parser::ParsingContext parsing_context { document() };
  111. for_each_attribute([&](auto& name, auto& value) {
  112. if (name.equals_ignoring_ascii_case("fill"sv)) {
  113. // FIXME: The `fill` attribute and CSS `fill` property are not the same! But our support is limited enough that they are equivalent for now.
  114. if (auto fill_value = parse_css_value(parsing_context, value, CSS::PropertyID::Fill).release_value_but_fixme_should_propagate_errors())
  115. style.set_property(CSS::PropertyID::Fill, fill_value.release_nonnull());
  116. } else if (name.equals_ignoring_ascii_case("stroke"sv)) {
  117. // FIXME: The `stroke` attribute and CSS `stroke` property are not the same! But our support is limited enough that they are equivalent for now.
  118. if (auto stroke_value = parse_css_value(parsing_context, value, CSS::PropertyID::Stroke).release_value_but_fixme_should_propagate_errors())
  119. style.set_property(CSS::PropertyID::Stroke, stroke_value.release_nonnull());
  120. } else if (name.equals_ignoring_ascii_case("stroke-width"sv)) {
  121. if (auto stroke_width_value = parse_css_value(parsing_context, value, CSS::PropertyID::StrokeWidth).release_value_but_fixme_should_propagate_errors())
  122. style.set_property(CSS::PropertyID::StrokeWidth, stroke_width_value.release_nonnull());
  123. } else if (name.equals_ignoring_ascii_case("fill-rule"sv)) {
  124. if (auto fill_rule_value = parse_css_value(parsing_context, value, CSS::PropertyID::FillRule).release_value_but_fixme_should_propagate_errors())
  125. style.set_property(CSS::PropertyID::FillRule, fill_rule_value.release_nonnull());
  126. } else if (name.equals_ignoring_ascii_case("fill-opacity"sv)) {
  127. if (auto fill_opacity_value = parse_css_value(parsing_context, value, CSS::PropertyID::FillOpacity).release_value_but_fixme_should_propagate_errors())
  128. style.set_property(CSS::PropertyID::FillOpacity, fill_opacity_value.release_nonnull());
  129. } else if (name.equals_ignoring_ascii_case("stroke-opacity"sv)) {
  130. if (auto stroke_opacity_value = parse_css_value(parsing_context, value, CSS::PropertyID::FillOpacity).release_value_but_fixme_should_propagate_errors())
  131. style.set_property(CSS::PropertyID::StrokeOpacity, stroke_opacity_value.release_nonnull());
  132. } else if (name.equals_ignoring_ascii_case(SVG::AttributeNames::opacity)) {
  133. if (auto stroke_opacity_value = parse_css_value(parsing_context, value, CSS::PropertyID::Opacity).release_value_but_fixme_should_propagate_errors())
  134. style.set_property(CSS::PropertyID::Opacity, stroke_opacity_value.release_nonnull());
  135. } else if (name.equals_ignoring_ascii_case("text-anchor"sv)) {
  136. if (auto text_anchor_value = parse_css_value(parsing_context, value, CSS::PropertyID::TextAnchor).release_value_but_fixme_should_propagate_errors())
  137. style.set_property(CSS::PropertyID::TextAnchor, text_anchor_value.release_nonnull());
  138. } else if (name.equals_ignoring_ascii_case("font-size"sv)) {
  139. if (auto font_size_value = parse_css_value(parsing_context, value, CSS::PropertyID::FontSize).release_value_but_fixme_should_propagate_errors())
  140. style.set_property(CSS::PropertyID::FontSize, font_size_value.release_nonnull());
  141. }
  142. });
  143. }
  144. Optional<FillRule> SVGGraphicsElement::fill_rule() const
  145. {
  146. if (!layout_node())
  147. return {};
  148. switch (layout_node()->computed_values().fill_rule()) {
  149. case CSS::FillRule::Nonzero:
  150. return FillRule::Nonzero;
  151. case CSS::FillRule::Evenodd:
  152. return FillRule::Evenodd;
  153. default:
  154. VERIFY_NOT_REACHED();
  155. }
  156. }
  157. Optional<Gfx::Color> SVGGraphicsElement::fill_color() const
  158. {
  159. if (!layout_node())
  160. return {};
  161. // FIXME: In the working-draft spec, `fill` is intended to be a shorthand, with `fill-color`
  162. // being what we actually want to use. But that's not final or widely supported yet.
  163. return layout_node()->computed_values().fill().map([&](auto& paint) -> Gfx::Color {
  164. if (!paint.is_color())
  165. return Color::Black;
  166. return paint.as_color();
  167. });
  168. }
  169. Optional<Gfx::Color> SVGGraphicsElement::stroke_color() const
  170. {
  171. if (!layout_node())
  172. return {};
  173. // FIXME: In the working-draft spec, `stroke` is intended to be a shorthand, with `stroke-color`
  174. // being what we actually want to use. But that's not final or widely supported yet.
  175. return layout_node()->computed_values().stroke().map([](auto& paint) -> Gfx::Color {
  176. if (!paint.is_color())
  177. return Color::Black;
  178. return paint.as_color();
  179. });
  180. }
  181. Optional<float> SVGGraphicsElement::fill_opacity() const
  182. {
  183. if (!layout_node())
  184. return {};
  185. return layout_node()->computed_values().fill_opacity();
  186. }
  187. Optional<float> SVGGraphicsElement::stroke_opacity() const
  188. {
  189. if (!layout_node())
  190. return {};
  191. return layout_node()->computed_values().stroke_opacity();
  192. }
  193. Optional<float> SVGGraphicsElement::stroke_width() const
  194. {
  195. if (!layout_node())
  196. return {};
  197. // FIXME: Converting to pixels isn't really correct - values should be in "user units"
  198. // https://svgwg.org/svg2-draft/coords.html#TermUserUnits
  199. auto width = layout_node()->computed_values().stroke_width();
  200. // Resolved relative to the "Scaled viewport size": https://www.w3.org/TR/2017/WD-fill-stroke-3-20170413/#scaled-viewport-size
  201. // FIXME: This isn't right, but it's something.
  202. CSSPixels viewport_width = 0;
  203. CSSPixels viewport_height = 0;
  204. if (auto* svg_svg_element = shadow_including_first_ancestor_of_type<SVGSVGElement>()) {
  205. if (auto* svg_svg_layout_node = svg_svg_element->layout_node()) {
  206. viewport_width = svg_svg_layout_node->computed_values().width().to_px(*svg_svg_layout_node, 0);
  207. viewport_height = svg_svg_layout_node->computed_values().height().to_px(*svg_svg_layout_node, 0);
  208. }
  209. }
  210. auto scaled_viewport_size = (viewport_width + viewport_height) * 0.5;
  211. return width.to_px(*layout_node(), scaled_viewport_size).to_double();
  212. }
  213. }