SVGFormattingContext.cpp 3.9 KB

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