SVGFormattingContext.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/Format.h>
  8. #include <LibWeb/Layout/SVGFormattingContext.h>
  9. #include <LibWeb/Layout/SVGGeometryBox.h>
  10. namespace Web::Layout {
  11. SVGFormattingContext::SVGFormattingContext(FormattingState& state, Box const& box, FormattingContext* parent)
  12. : FormattingContext(Type::SVG, state, box, parent)
  13. {
  14. }
  15. SVGFormattingContext::~SVGFormattingContext()
  16. {
  17. }
  18. void SVGFormattingContext::run(Box const& box, LayoutMode)
  19. {
  20. box.for_each_in_subtree_of_type<SVGBox>([&](auto const& descendant) {
  21. if (is<SVGGeometryBox>(descendant)) {
  22. auto const& geometry_box = static_cast<SVGGeometryBox const&>(descendant);
  23. auto& path = const_cast<SVGGeometryBox&>(geometry_box).dom_node().get_path();
  24. auto bounding_box = path.bounding_box();
  25. // Stroke increases the path's size by stroke_width/2 per side.
  26. auto stroke_width = geometry_box.dom_node().stroke_width().value_or(0);
  27. bounding_box.inflate(stroke_width, stroke_width);
  28. auto& geometry_box_state = m_state.ensure(geometry_box);
  29. geometry_box_state.offset = bounding_box.top_left();
  30. geometry_box_state.content_width = bounding_box.width();
  31. geometry_box_state.content_height = bounding_box.height();
  32. }
  33. return IterationDecision::Continue;
  34. });
  35. }
  36. }