SVGFormattingContext.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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/SVGFormattingContext.h>
  10. #include <LibWeb/Layout/SVGGeometryBox.h>
  11. #include <LibWeb/SVG/SVGSVGElement.h>
  12. namespace Web::Layout {
  13. SVGFormattingContext::SVGFormattingContext(LayoutState& state, Box const& box, FormattingContext* parent)
  14. : FormattingContext(Type::SVG, state, box, parent)
  15. {
  16. }
  17. SVGFormattingContext::~SVGFormattingContext() = default;
  18. float SVGFormattingContext::automatic_content_height() const
  19. {
  20. return 0;
  21. }
  22. void SVGFormattingContext::run(Box const& box, LayoutMode, [[maybe_unused]] AvailableSpace const& available_space)
  23. {
  24. auto& svg_svg_element = verify_cast<SVG::SVGSVGElement>(*box.dom_node());
  25. box.for_each_in_subtree_of_type<SVGBox>([&](SVGBox const& descendant) {
  26. if (is<SVGGeometryBox>(descendant)) {
  27. auto const& geometry_box = static_cast<SVGGeometryBox const&>(descendant);
  28. auto& geometry_box_state = m_state.get_mutable(geometry_box);
  29. auto& dom_node = const_cast<SVGGeometryBox&>(geometry_box).dom_node();
  30. if (svg_svg_element.has_attribute(HTML::AttributeNames::width) && svg_svg_element.has_attribute(HTML::AttributeNames::height)) {
  31. geometry_box_state.set_content_offset({ 0, 0 });
  32. auto& layout_node = *svg_svg_element.layout_node();
  33. // FIXME: Allow for relative lengths here
  34. geometry_box_state.set_content_width(layout_node.computed_values().width().resolved(layout_node, { 0, CSS::Length::Type::Px }).to_px(layout_node));
  35. geometry_box_state.set_content_height(layout_node.computed_values().height().resolved(layout_node, { 0, CSS::Length::Type::Px }).to_px(layout_node));
  36. return IterationDecision::Continue;
  37. }
  38. // FIXME: Allow for one of {width, height} to not be specified}
  39. if (svg_svg_element.has_attribute(HTML::AttributeNames::width)) {
  40. }
  41. if (svg_svg_element.has_attribute(HTML::AttributeNames::height)) {
  42. }
  43. auto& path = dom_node.get_path();
  44. auto path_bounding_box = path.bounding_box();
  45. // Stroke increases the path's size by stroke_width/2 per side.
  46. auto stroke_width = geometry_box.dom_node().stroke_width().value_or(0);
  47. path_bounding_box.inflate(stroke_width, stroke_width);
  48. auto& maybe_view_box = svg_svg_element.view_box();
  49. if (maybe_view_box.has_value()) {
  50. auto view_box = maybe_view_box.value();
  51. Gfx::FloatPoint viewbox_offset = { view_box.min_x, view_box.min_y };
  52. geometry_box_state.set_content_offset(path_bounding_box.top_left() + viewbox_offset);
  53. geometry_box_state.set_content_width(view_box.width);
  54. geometry_box_state.set_content_height(view_box.height);
  55. return IterationDecision::Continue;
  56. }
  57. geometry_box_state.set_content_offset(path_bounding_box.top_left());
  58. geometry_box_state.set_content_width(path_bounding_box.width());
  59. geometry_box_state.set_content_height(path_bounding_box.height());
  60. }
  61. return IterationDecision::Continue;
  62. });
  63. }
  64. }