SVGSVGBox.cpp 3.6 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/CSS/Parser/Parser.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. JS::GCPtr<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<CSSPixels> w;
  24. Optional<CSSPixels> h;
  25. auto parsing_context = CSS::Parser::ParsingContext { document() };
  26. auto width = parse_css_value(parsing_context, dom_node().attribute(Web::HTML::AttributeNames::width), CSS::PropertyID::Width);
  27. if (!width.is_null() && width->has_length())
  28. w = width->to_length().to_px(*this);
  29. auto height = parse_css_value(parsing_context, dom_node().attribute((HTML::AttributeNames::height)), CSS::PropertyID::Height);
  30. if (!height.is_null() && height->has_length())
  31. h = height->to_length().to_px(*this);
  32. if (w.has_value() && h.has_value()) {
  33. set_intrinsic_width(*w);
  34. set_intrinsic_height(*h);
  35. set_intrinsic_aspect_ratio(w->value() / h->value());
  36. return;
  37. }
  38. }
  39. Optional<CSSPixelRect> united_rect;
  40. auto add_to_united_rect = [&](CSSPixelRect const& rect) {
  41. if (united_rect.has_value())
  42. united_rect = united_rect->united(rect);
  43. else
  44. united_rect = rect;
  45. };
  46. for_each_in_subtree_of_type<SVGGeometryBox>([&](SVGGeometryBox const& geometry_box) {
  47. auto& dom_node = const_cast<SVG::SVGGeometryElement&>(geometry_box.dom_node());
  48. if (dom_node.has_attribute(HTML::AttributeNames::width) && dom_node.has_attribute(HTML::AttributeNames::height)) {
  49. CSSPixelRect rect;
  50. // FIXME: Allow for relative lengths here
  51. rect.set_width(computed_values().width().resolved(*this, CSS::Length::make_px(0)).to_px(*this));
  52. rect.set_height(computed_values().height().resolved(*this, CSS::Length::make_px(0)).to_px(*this));
  53. add_to_united_rect(rect);
  54. return IterationDecision::Continue;
  55. }
  56. auto& path = dom_node.get_path();
  57. auto path_bounding_box = path.bounding_box().to_type<CSSPixels>();
  58. // Stroke increases the path's size by stroke_width/2 per side.
  59. auto stroke_width = geometry_box.dom_node().stroke_width().value_or(0);
  60. path_bounding_box.inflate(stroke_width, stroke_width);
  61. auto& maybe_view_box = this->dom_node().view_box();
  62. if (maybe_view_box.has_value()) {
  63. auto view_box = maybe_view_box.value();
  64. CSSPixelRect rect(view_box.min_x, view_box.min_y, view_box.width, view_box.height);
  65. add_to_united_rect(rect);
  66. return IterationDecision::Continue;
  67. }
  68. add_to_united_rect(path_bounding_box);
  69. return IterationDecision::Continue;
  70. });
  71. if (united_rect.has_value()) {
  72. set_intrinsic_width(united_rect->width());
  73. set_intrinsic_height(united_rect->height());
  74. set_intrinsic_aspect_ratio(united_rect->width().value() / united_rect->height().value());
  75. }
  76. }
  77. }