SVGFormattingContext.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
  4. * Copyright (c) 2022, Tobias Christiansen <tobyase@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <AK/Format.h>
  9. #include <LibWeb/Layout/BlockFormattingContext.h>
  10. #include <LibWeb/Layout/SVGFormattingContext.h>
  11. #include <LibWeb/Layout/SVGGeometryBox.h>
  12. #include <LibWeb/SVG/SVGForeignObjectElement.h>
  13. #include <LibWeb/SVG/SVGSVGElement.h>
  14. namespace Web::Layout {
  15. SVGFormattingContext::SVGFormattingContext(LayoutState& state, Box const& box, FormattingContext* parent)
  16. : FormattingContext(Type::SVG, state, box, parent)
  17. {
  18. }
  19. SVGFormattingContext::~SVGFormattingContext() = default;
  20. float SVGFormattingContext::automatic_content_height() const
  21. {
  22. return 0;
  23. }
  24. void SVGFormattingContext::run(Box const& box, LayoutMode, [[maybe_unused]] AvailableSpace const& available_space)
  25. {
  26. // FIXME: This entire thing is an ad-hoc hack.
  27. auto& svg_svg_element = verify_cast<SVG::SVGSVGElement>(*box.dom_node());
  28. auto root_offset = m_state.get(box).offset;
  29. box.for_each_child_of_type<BlockContainer>([&](BlockContainer const& child_box) {
  30. if (is<SVG::SVGForeignObjectElement>(child_box.dom_node())) {
  31. Layout::BlockFormattingContext bfc(m_state, child_box, this);
  32. bfc.run(child_box, LayoutMode::Normal, available_space);
  33. auto& child_state = m_state.get_mutable(child_box);
  34. child_state.set_content_offset(child_state.offset.translated(root_offset));
  35. }
  36. return IterationDecision::Continue;
  37. });
  38. box.for_each_in_subtree_of_type<SVGBox>([&](SVGBox const& descendant) {
  39. if (is<SVGGeometryBox>(descendant)) {
  40. auto const& geometry_box = static_cast<SVGGeometryBox const&>(descendant);
  41. auto& geometry_box_state = m_state.get_mutable(geometry_box);
  42. auto& dom_node = const_cast<SVGGeometryBox&>(geometry_box).dom_node();
  43. auto& svg_svg_state = m_state.get(static_cast<Box const&>(*svg_svg_element.layout_node()));
  44. if (svg_svg_state.has_definite_width() && svg_svg_state.has_definite_height()) {
  45. geometry_box_state.set_content_offset({ 0, 0 });
  46. geometry_box_state.set_content_width(svg_svg_state.content_width());
  47. geometry_box_state.set_content_height(svg_svg_state.content_height());
  48. return IterationDecision::Continue;
  49. }
  50. // FIXME: Allow for one of {width, height} to not be specified}
  51. if (svg_svg_element.has_attribute(HTML::AttributeNames::width)) {
  52. }
  53. if (svg_svg_element.has_attribute(HTML::AttributeNames::height)) {
  54. }
  55. auto& path = dom_node.get_path();
  56. auto path_bounding_box = path.bounding_box();
  57. // Stroke increases the path's size by stroke_width/2 per side.
  58. auto stroke_width = geometry_box.dom_node().stroke_width().value_or(0);
  59. path_bounding_box.inflate(stroke_width, stroke_width);
  60. auto& maybe_view_box = svg_svg_element.view_box();
  61. if (maybe_view_box.has_value()) {
  62. auto view_box = maybe_view_box.value();
  63. Gfx::FloatPoint viewbox_offset = { view_box.min_x, view_box.min_y };
  64. geometry_box_state.set_content_offset(path_bounding_box.top_left() + viewbox_offset);
  65. geometry_box_state.set_content_width(view_box.width);
  66. geometry_box_state.set_content_height(view_box.height);
  67. return IterationDecision::Continue;
  68. }
  69. geometry_box_state.set_content_offset(path_bounding_box.top_left());
  70. geometry_box_state.set_content_width(path_bounding_box.width());
  71. geometry_box_state.set_content_height(path_bounding_box.height());
  72. }
  73. return IterationDecision::Continue;
  74. });
  75. }
  76. }