SVGFormattingContext.cpp 4.4 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. * Copyright (c) 2023, MacDue <macdue@dueutil.tech>
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  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. CSSPixels SVGFormattingContext::automatic_content_width() const
  21. {
  22. return 0;
  23. }
  24. CSSPixels SVGFormattingContext::automatic_content_height() const
  25. {
  26. return 0;
  27. }
  28. void SVGFormattingContext::run(Box const& box, LayoutMode, [[maybe_unused]] AvailableSpace const& available_space)
  29. {
  30. // FIXME: This entire thing is an ad-hoc hack.
  31. auto& svg_svg_element = verify_cast<SVG::SVGSVGElement>(*box.dom_node());
  32. auto svg_box_state = m_state.get(box);
  33. auto root_offset = svg_box_state.offset;
  34. box.for_each_child_of_type<BlockContainer>([&](BlockContainer const& child_box) {
  35. if (is<SVG::SVGForeignObjectElement>(child_box.dom_node())) {
  36. Layout::BlockFormattingContext bfc(m_state, child_box, this);
  37. bfc.run(child_box, LayoutMode::Normal, available_space);
  38. auto& child_state = m_state.get_mutable(child_box);
  39. child_state.set_content_offset(child_state.offset.translated(root_offset));
  40. }
  41. return IterationDecision::Continue;
  42. });
  43. box.for_each_in_subtree_of_type<SVGBox>([&](SVGBox const& descendant) {
  44. if (is<SVGGeometryBox>(descendant)) {
  45. auto const& geometry_box = static_cast<SVGGeometryBox const&>(descendant);
  46. auto& geometry_box_state = m_state.get_mutable(geometry_box);
  47. auto& dom_node = const_cast<SVGGeometryBox&>(geometry_box).dom_node();
  48. auto& path = dom_node.get_path();
  49. auto transform = dom_node.get_transform();
  50. auto& maybe_view_box = svg_svg_element.view_box();
  51. float viewbox_scale = 1.0f;
  52. CSSPixelPoint offset {};
  53. if (maybe_view_box.has_value()) {
  54. auto view_box = maybe_view_box.value();
  55. // FIXME: This should allow just one of width or height to be specified.
  56. // E.g. We should be able to layout <svg width="100%"> where height is unspecified/auto.
  57. if (!svg_box_state.has_definite_width() || !svg_box_state.has_definite_height()) {
  58. dbgln("FIXME: Attempting to layout indefinitely sized SVG with a viewbox -- this likely won't work!");
  59. }
  60. auto scale_width = svg_box_state.has_definite_width() ? svg_box_state.content_width().value() / view_box.width : 1;
  61. auto scale_height = svg_box_state.has_definite_height() ? svg_box_state.content_height().value() / view_box.height : 1;
  62. viewbox_scale = min(scale_width, scale_height);
  63. // Center the viewbox within the SVG element:
  64. if (svg_box_state.has_definite_width())
  65. offset.translate_by((svg_box_state.content_width() - (view_box.width * viewbox_scale)) / 2, 0);
  66. if (svg_box_state.has_definite_height())
  67. offset.translate_by(0, (svg_box_state.content_height() - (view_box.height * viewbox_scale)) / 2);
  68. transform = Gfx::AffineTransform {}.scale(viewbox_scale, viewbox_scale).translate({ -view_box.min_x, -view_box.min_y }).multiply(transform);
  69. }
  70. // Stroke increases the path's size by stroke_width/2 per side.
  71. auto path_bounding_box = transform.map(path.bounding_box()).to_type<CSSPixels>();
  72. CSSPixels stroke_width = geometry_box.dom_node().visible_stroke_width() * viewbox_scale;
  73. path_bounding_box.inflate(stroke_width, stroke_width);
  74. geometry_box_state.set_content_offset(path_bounding_box.top_left() + offset);
  75. geometry_box_state.set_content_width(path_bounding_box.width());
  76. geometry_box_state.set_content_height(path_bounding_box.height());
  77. }
  78. return IterationDecision::Continue;
  79. });
  80. }
  81. }