Box.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/OwnPtr.h>
  8. #include <LibGfx/Rect.h>
  9. #include <LibWeb/Layout/Node.h>
  10. #include <LibWeb/Painting/BorderPainting.h>
  11. #include <LibWeb/Painting/StackingContext.h>
  12. namespace Web::Layout {
  13. class Box : public NodeWithStyleAndBoxModelMetrics {
  14. public:
  15. struct OverflowData {
  16. Gfx::FloatRect scrollable_overflow_rect;
  17. Gfx::FloatPoint scroll_offset;
  18. };
  19. const Gfx::FloatRect absolute_rect() const;
  20. Gfx::FloatPoint effective_offset() const;
  21. void set_offset(const Gfx::FloatPoint& offset);
  22. void set_offset(float x, float y) { set_offset({ x, y }); }
  23. Gfx::FloatSize const& content_size() const { return m_content_size; }
  24. void set_content_size(Gfx::FloatSize const&);
  25. void set_content_size(float width, float height) { set_content_size({ width, height }); }
  26. void set_content_width(float width) { set_content_size(width, content_height()); }
  27. void set_content_height(float height) { set_content_size(content_width(), height); }
  28. float content_width() const { return m_content_size.width(); }
  29. float content_height() const { return m_content_size.height(); }
  30. Gfx::FloatRect absolute_padding_box_rect() const
  31. {
  32. auto absolute_rect = this->absolute_rect();
  33. Gfx::FloatRect rect;
  34. rect.set_x(absolute_rect.x() - box_model().padding.left);
  35. rect.set_width(content_width() + box_model().padding.left + box_model().padding.right);
  36. rect.set_y(absolute_rect.y() - box_model().padding.top);
  37. rect.set_height(content_height() + box_model().padding.top + box_model().padding.bottom);
  38. return rect;
  39. }
  40. Gfx::FloatRect absolute_border_box_rect() const
  41. {
  42. auto padded_rect = this->absolute_padding_box_rect();
  43. Gfx::FloatRect rect;
  44. rect.set_x(padded_rect.x() - box_model().border.left);
  45. rect.set_width(padded_rect.width() + box_model().border.left + box_model().border.right);
  46. rect.set_y(padded_rect.y() - box_model().border.top);
  47. rect.set_height(padded_rect.height() + box_model().border.top + box_model().border.bottom);
  48. return rect;
  49. }
  50. float border_box_width() const
  51. {
  52. auto border_box = box_model().border_box();
  53. return content_width() + border_box.left + border_box.right;
  54. }
  55. float border_box_height() const
  56. {
  57. auto border_box = box_model().border_box();
  58. return content_height() + border_box.top + border_box.bottom;
  59. }
  60. float absolute_x() const { return absolute_rect().x(); }
  61. float absolute_y() const { return absolute_rect().y(); }
  62. Gfx::FloatPoint absolute_position() const { return absolute_rect().location(); }
  63. bool is_out_of_flow(FormattingContext const&) const;
  64. virtual HitTestResult hit_test(const Gfx::IntPoint&, HitTestType) const override;
  65. virtual void set_needs_display() override;
  66. bool is_body() const;
  67. void set_containing_line_box_fragment(LineBoxFragment&);
  68. StackingContext* stacking_context() { return m_stacking_context; }
  69. const StackingContext* stacking_context() const { return m_stacking_context; }
  70. void set_stacking_context(NonnullOwnPtr<StackingContext> context) { m_stacking_context = move(context); }
  71. StackingContext* enclosing_stacking_context();
  72. virtual void paint(PaintContext&, PaintPhase) override;
  73. virtual void paint_border(PaintContext& context);
  74. virtual void paint_box_shadow(PaintContext& context);
  75. virtual void paint_background(PaintContext& context);
  76. Painting::BorderRadiusData normalized_border_radius_data();
  77. virtual Optional<float> intrinsic_width() const { return {}; }
  78. virtual Optional<float> intrinsic_height() const { return {}; }
  79. virtual Optional<float> intrinsic_aspect_ratio() const { return {}; }
  80. bool has_intrinsic_width() const { return intrinsic_width().has_value(); }
  81. bool has_intrinsic_height() const { return intrinsic_height().has_value(); }
  82. bool has_intrinsic_aspect_ratio() const { return intrinsic_aspect_ratio().has_value(); }
  83. bool has_overflow() const { return m_overflow_data; }
  84. Optional<Gfx::FloatRect> scrollable_overflow_rect() const
  85. {
  86. if (!m_overflow_data)
  87. return {};
  88. return m_overflow_data->scrollable_overflow_rect;
  89. }
  90. void set_overflow_data(OwnPtr<OverflowData> data) { m_overflow_data = move(data); }
  91. virtual void before_children_paint(PaintContext&, PaintPhase) override;
  92. virtual void after_children_paint(PaintContext&, PaintPhase) override;
  93. protected:
  94. Box(DOM::Document& document, DOM::Node* node, NonnullRefPtr<CSS::StyleProperties> style)
  95. : NodeWithStyleAndBoxModelMetrics(document, node, move(style))
  96. {
  97. }
  98. Box(DOM::Document& document, DOM::Node* node, CSS::ComputedValues computed_values)
  99. : NodeWithStyleAndBoxModelMetrics(document, node, move(computed_values))
  100. {
  101. }
  102. virtual void did_set_rect() { }
  103. private:
  104. virtual bool is_box() const final { return true; }
  105. Gfx::FloatPoint m_offset;
  106. Gfx::FloatSize m_content_size;
  107. // Some boxes hang off of line box fragments. (inline-block, inline-table, replaced, etc)
  108. WeakPtr<LineBoxFragment> m_containing_line_box_fragment;
  109. OwnPtr<StackingContext> m_stacking_context;
  110. OwnPtr<OverflowData> m_overflow_data;
  111. };
  112. template<>
  113. inline bool Node::fast_is<Box>() const { return is_box(); }
  114. }