SVGSVGBox.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright (c) 2020, Matthew Olsson <mattco@serenityos.org>
  3. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibWeb/HTML/Parser/HTMLParser.h>
  8. #include <LibWeb/Layout/ReplacedBox.h>
  9. #include <LibWeb/Layout/SVGGeometryBox.h>
  10. #include <LibWeb/Painting/SVGSVGPaintable.h>
  11. namespace Web::Layout {
  12. SVGSVGBox::SVGSVGBox(DOM::Document& document, SVG::SVGSVGElement& element, NonnullRefPtr<CSS::StyleProperties> properties)
  13. : ReplacedBox(document, element, move(properties))
  14. {
  15. }
  16. RefPtr<Painting::Paintable> SVGSVGBox::create_paintable() const
  17. {
  18. return Painting::SVGSVGPaintable::create(*this);
  19. }
  20. void SVGSVGBox::prepare_for_replaced_layout()
  21. {
  22. if (dom_node().has_attribute(HTML::AttributeNames::width) && dom_node().has_attribute(HTML::AttributeNames::height)) {
  23. Optional<float> w;
  24. Optional<float> h;
  25. if (auto width = HTML::parse_dimension_value(dom_node().attribute(HTML::AttributeNames::width))) {
  26. if (width->has_length())
  27. w = width->to_length().to_px(*this);
  28. }
  29. if (auto height = HTML::parse_dimension_value(dom_node().attribute(HTML::AttributeNames::height))) {
  30. if (height->has_length())
  31. h = height->to_length().to_px(*this);
  32. }
  33. if (w.has_value() && h.has_value()) {
  34. set_intrinsic_width(*w);
  35. set_intrinsic_height(*h);
  36. set_intrinsic_aspect_ratio(*w / *h);
  37. return;
  38. }
  39. }
  40. Optional<Gfx::FloatRect> united_rect;
  41. auto add_to_united_rect = [&](Gfx::FloatRect const& rect) {
  42. if (united_rect.has_value())
  43. united_rect = united_rect->united(rect);
  44. else
  45. united_rect = rect;
  46. };
  47. for_each_in_subtree_of_type<SVGGeometryBox>([&](SVGGeometryBox const& geometry_box) {
  48. auto& dom_node = const_cast<SVG::SVGGeometryElement&>(geometry_box.dom_node());
  49. if (dom_node.has_attribute(HTML::AttributeNames::width) && dom_node.has_attribute(HTML::AttributeNames::height)) {
  50. Gfx::FloatRect rect;
  51. // FIXME: Allow for relative lengths here
  52. rect.set_width(computed_values().width().resolved(*this, CSS::Length::make_px(0)).to_px(*this));
  53. rect.set_height(computed_values().height().resolved(*this, CSS::Length::make_px(0)).to_px(*this));
  54. add_to_united_rect(rect);
  55. return IterationDecision::Continue;
  56. }
  57. auto& path = dom_node.get_path();
  58. auto path_bounding_box = path.bounding_box();
  59. // Stroke increases the path's size by stroke_width/2 per side.
  60. auto stroke_width = geometry_box.dom_node().stroke_width().value_or(0);
  61. path_bounding_box.inflate(stroke_width, stroke_width);
  62. auto& maybe_view_box = this->dom_node().view_box();
  63. if (maybe_view_box.has_value()) {
  64. auto view_box = maybe_view_box.value();
  65. Gfx::FloatRect rect(view_box.min_x, view_box.min_y, view_box.width, view_box.height);
  66. add_to_united_rect(rect);
  67. return IterationDecision::Continue;
  68. }
  69. add_to_united_rect(path_bounding_box);
  70. return IterationDecision::Continue;
  71. });
  72. if (united_rect.has_value()) {
  73. set_intrinsic_width(united_rect->width());
  74. set_intrinsic_height(united_rect->height());
  75. set_intrinsic_aspect_ratio(united_rect->width() / united_rect->height());
  76. }
  77. }
  78. }