Box.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
  3. * Copyright (c) 2021-2022, Sam Atkins <atkinssj@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibWeb/DOM/Document.h>
  8. #include <LibWeb/HTML/HTMLHtmlElement.h>
  9. #include <LibWeb/Layout/BlockContainer.h>
  10. #include <LibWeb/Layout/Box.h>
  11. #include <LibWeb/Layout/FormattingContext.h>
  12. #include <LibWeb/Painting/PaintableBox.h>
  13. namespace Web::Layout {
  14. Box::Box(DOM::Document& document, DOM::Node* node, CSS::StyleProperties style)
  15. : NodeWithStyleAndBoxModelMetrics(document, node, move(style))
  16. {
  17. }
  18. Box::Box(DOM::Document& document, DOM::Node* node, NonnullOwnPtr<CSS::ComputedValues> computed_values)
  19. : NodeWithStyleAndBoxModelMetrics(document, node, move(computed_values))
  20. {
  21. }
  22. Box::~Box()
  23. {
  24. }
  25. void Box::visit_edges(Cell::Visitor& visitor)
  26. {
  27. Base::visit_edges(visitor);
  28. visitor.visit(m_contained_abspos_children);
  29. }
  30. JS::GCPtr<Painting::Paintable> Box::create_paintable() const
  31. {
  32. return Painting::PaintableBox::create(*this);
  33. }
  34. Painting::PaintableBox* Box::paintable_box()
  35. {
  36. return static_cast<Painting::PaintableBox*>(Node::first_paintable());
  37. }
  38. Painting::PaintableBox const* Box::paintable_box() const
  39. {
  40. return static_cast<Painting::PaintableBox const*>(Node::first_paintable());
  41. }
  42. Optional<CSSPixelFraction> Box::preferred_aspect_ratio() const
  43. {
  44. auto computed_aspect_ratio = computed_values().aspect_ratio();
  45. if (computed_aspect_ratio.use_natural_aspect_ratio_if_available && natural_aspect_ratio().has_value())
  46. return natural_aspect_ratio();
  47. if (!computed_aspect_ratio.preferred_ratio.has_value())
  48. return {};
  49. auto ratio = computed_aspect_ratio.preferred_ratio.release_value();
  50. if (ratio.is_degenerate())
  51. return {};
  52. return CSSPixelFraction(ratio.numerator(), ratio.denominator());
  53. }
  54. }