SVGFormattingContext.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Format.h>
  7. #include <LibWeb/Layout/SVGFormattingContext.h>
  8. #include <LibWeb/Layout/SVGGeometryBox.h>
  9. #include <LibWeb/Layout/SVGSVGBox.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. // FIXME: This formatting context is basically a total hack.
  21. // It works by computing the united bounding box of all <path>'s
  22. // within an <svg>, and using that as the size of this box.
  23. Gfx::FloatRect total_bounding_box;
  24. box.for_each_in_subtree_of_type<SVGBox>([&](auto& descendant) {
  25. if (is<SVGGeometryBox>(descendant)) {
  26. auto& geometry_box = static_cast<SVGGeometryBox&>(descendant);
  27. auto& path = geometry_box.dom_node().get_path();
  28. geometry_box.set_content_size(path.bounding_box().size());
  29. total_bounding_box = total_bounding_box.united(path.bounding_box());
  30. }
  31. return IterationDecision::Continue;
  32. });
  33. box.set_content_size(total_bounding_box.size());
  34. }
  35. }