SVGFormattingContext.cpp 4.0 KB

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