LayoutNode.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/NonnullRefPtr.h>
  28. #include <AK/TypeCasts.h>
  29. #include <AK/Vector.h>
  30. #include <LibGfx/Rect.h>
  31. #include <LibWeb/CSS/StyleProperties.h>
  32. #include <LibWeb/Forward.h>
  33. #include <LibWeb/Layout/BoxModelMetrics.h>
  34. #include <LibWeb/Layout/LayoutPosition.h>
  35. #include <LibWeb/Layout/LayoutStyle.h>
  36. #include <LibWeb/Painting/PaintContext.h>
  37. #include <LibWeb/TreeNode.h>
  38. namespace Web {
  39. struct HitTestResult {
  40. RefPtr<LayoutNode> layout_node;
  41. int index_in_node { 0 };
  42. };
  43. enum class HitTestType {
  44. Exact, // Exact matches only
  45. TextCursor, // Clicking past the right/bottom edge of text will still hit the text
  46. };
  47. class LayoutNode : public TreeNode<LayoutNode> {
  48. public:
  49. virtual ~LayoutNode();
  50. virtual HitTestResult hit_test(const Gfx::IntPoint&, HitTestType) const;
  51. bool is_anonymous() const { return !m_node; }
  52. const DOM::Node* node() const { return m_node; }
  53. DOM::Node* node() { return m_node; }
  54. DOM::Document& document() { return m_document; }
  55. const DOM::Document& document() const { return m_document; }
  56. const Frame& frame() const;
  57. Frame& frame();
  58. const LayoutDocument& root() const;
  59. LayoutDocument& root();
  60. virtual const char* class_name() const = 0;
  61. virtual bool is_root() const { return false; }
  62. virtual bool is_text() const { return false; }
  63. virtual bool is_block() const { return false; }
  64. virtual bool is_replaced() const { return false; }
  65. virtual bool is_widget() const { return false; }
  66. virtual bool is_frame() const { return false; }
  67. virtual bool is_image() const { return false; }
  68. virtual bool is_canvas() const { return false; }
  69. virtual bool is_box() const { return false; }
  70. virtual bool is_table() const { return false; }
  71. virtual bool is_table_row() const { return false; }
  72. virtual bool is_table_cell() const { return false; }
  73. virtual bool is_table_row_group() const { return false; }
  74. virtual bool is_break() const { return false; }
  75. bool has_style() const { return m_has_style; }
  76. bool is_inline() const { return m_inline; }
  77. void set_inline(bool b) { m_inline = b; }
  78. bool is_inline_block() const { return is_inline() && is_block(); }
  79. enum class LayoutMode {
  80. Default,
  81. AllPossibleLineBreaks,
  82. OnlyRequiredLineBreaks,
  83. };
  84. virtual void layout(LayoutMode);
  85. enum class PaintPhase {
  86. Background,
  87. Border,
  88. Foreground,
  89. Overlay,
  90. };
  91. virtual void paint(PaintContext&, PaintPhase);
  92. bool is_floating() const;
  93. bool is_absolutely_positioned() const;
  94. bool is_fixed_position() const;
  95. const LayoutBlock* containing_block() const;
  96. bool can_contain_boxes_with_position_absolute() const;
  97. virtual LayoutNode& inline_wrapper() { return *this; }
  98. const CSS::StyleProperties& specified_style() const;
  99. const ImmutableLayoutStyle& style() const;
  100. LayoutNodeWithStyle* parent();
  101. const LayoutNodeWithStyle* parent() const;
  102. void inserted_into(LayoutNode&) { }
  103. void removed_from(LayoutNode&) { }
  104. void children_changed() { }
  105. virtual void split_into_lines(LayoutBlock& container, LayoutMode);
  106. bool is_visible() const { return m_visible; }
  107. void set_visible(bool visible) { m_visible = visible; }
  108. virtual void set_needs_display();
  109. bool children_are_inline() const { return m_children_are_inline; }
  110. void set_children_are_inline(bool value) { m_children_are_inline = value; }
  111. Gfx::FloatPoint box_type_agnostic_position() const;
  112. float font_size() const;
  113. protected:
  114. LayoutNode(DOM::Document&, DOM::Node*);
  115. private:
  116. friend class LayoutNodeWithStyle;
  117. DOM::Document& m_document;
  118. DOM::Node* m_node { nullptr };
  119. bool m_inline { false };
  120. bool m_has_style { false };
  121. bool m_visible { true };
  122. bool m_children_are_inline { false };
  123. };
  124. class LayoutNodeWithStyle : public LayoutNode {
  125. public:
  126. virtual ~LayoutNodeWithStyle() override { }
  127. const CSS::StyleProperties& specified_style() const { return m_specified_style; }
  128. void set_specified_style(const CSS::StyleProperties& style) { m_specified_style = style; }
  129. const ImmutableLayoutStyle& style() const { return static_cast<const ImmutableLayoutStyle&>(m_style); }
  130. void apply_style(const CSS::StyleProperties&);
  131. protected:
  132. LayoutNodeWithStyle(DOM::Document&, DOM::Node*, NonnullRefPtr<CSS::StyleProperties>);
  133. private:
  134. LayoutStyle m_style;
  135. NonnullRefPtr<CSS::StyleProperties> m_specified_style;
  136. CSS::Position m_position;
  137. CSS::TextAlign m_text_align;
  138. };
  139. class LayoutNodeWithStyleAndBoxModelMetrics : public LayoutNodeWithStyle {
  140. public:
  141. BoxModelMetrics& box_model() { return m_box_model; }
  142. const BoxModelMetrics& box_model() const { return m_box_model; }
  143. protected:
  144. LayoutNodeWithStyleAndBoxModelMetrics(DOM::Document& document, DOM::Node* node, NonnullRefPtr<CSS::StyleProperties> style)
  145. : LayoutNodeWithStyle(document, node, move(style))
  146. {
  147. }
  148. private:
  149. BoxModelMetrics m_box_model;
  150. };
  151. inline const CSS::StyleProperties& LayoutNode::specified_style() const
  152. {
  153. if (m_has_style)
  154. return static_cast<const LayoutNodeWithStyle*>(this)->specified_style();
  155. return parent()->specified_style();
  156. }
  157. inline const ImmutableLayoutStyle& LayoutNode::style() const
  158. {
  159. if (m_has_style)
  160. return static_cast<const LayoutNodeWithStyle*>(this)->style();
  161. return parent()->style();
  162. }
  163. inline const LayoutNodeWithStyle* LayoutNode::parent() const
  164. {
  165. return static_cast<const LayoutNodeWithStyle*>(TreeNode<LayoutNode>::parent());
  166. }
  167. inline LayoutNodeWithStyle* LayoutNode::parent()
  168. {
  169. return static_cast<LayoutNodeWithStyle*>(TreeNode<LayoutNode>::parent());
  170. }
  171. }
  172. AK_BEGIN_TYPE_TRAITS(Web::LayoutNodeWithStyle)
  173. static bool is_type(const Web::LayoutNode& node) { return node.has_style(); }
  174. AK_END_TYPE_TRAITS()