SVGFormattingContext.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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(Box& box, FormattingContext* parent)
  12. : FormattingContext(Type::SVG, box, parent)
  13. {
  14. }
  15. SVGFormattingContext::~SVGFormattingContext()
  16. {
  17. }
  18. void SVGFormattingContext::run(Box& box, LayoutMode)
  19. {
  20. box.for_each_in_subtree_of_type<SVGBox>([&](auto& descendant) {
  21. if (is<SVGGeometryBox>(descendant)) {
  22. auto& geometry_box = static_cast<SVGGeometryBox&>(descendant);
  23. auto& path = 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. geometry_box.set_offset(bounding_box.top_left());
  29. geometry_box.set_content_size(bounding_box.size());
  30. }
  31. return IterationDecision::Continue;
  32. });
  33. }
  34. }