LayoutNode.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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/Vector.h>
  29. #include <LibGfx/FloatRect.h>
  30. #include <LibGfx/Rect.h>
  31. #include <LibWeb/CSS/StyleProperties.h>
  32. #include <LibWeb/Layout/BoxModelMetrics.h>
  33. #include <LibWeb/Layout/LayoutPosition.h>
  34. #include <LibWeb/RenderingContext.h>
  35. #include <LibWeb/TreeNode.h>
  36. namespace Web {
  37. class Document;
  38. class Element;
  39. class LayoutBlock;
  40. class LayoutDocument;
  41. class LayoutNode;
  42. class LayoutNodeWithStyle;
  43. class LineBoxFragment;
  44. class Node;
  45. struct HitTestResult {
  46. RefPtr<LayoutNode> layout_node;
  47. int index_in_node { 0 };
  48. };
  49. class LayoutNode : public TreeNode<LayoutNode> {
  50. public:
  51. virtual ~LayoutNode();
  52. virtual HitTestResult hit_test(const Gfx::Point&) const;
  53. bool is_anonymous() const { return !m_node; }
  54. const Node* node() const { return m_node; }
  55. Document& document();
  56. const Document& document() const;
  57. const LayoutDocument& root() const;
  58. LayoutDocument& root();
  59. template<typename Callback>
  60. inline void for_each_child(Callback callback) const
  61. {
  62. for (auto* node = first_child(); node; node = node->next_sibling())
  63. callback(*node);
  64. }
  65. template<typename Callback>
  66. inline void for_each_child(Callback callback)
  67. {
  68. for (auto* node = first_child(); node; node = node->next_sibling())
  69. callback(*node);
  70. }
  71. virtual const char* class_name() const { return "LayoutNode"; }
  72. virtual bool is_text() const { return false; }
  73. virtual bool is_block() const { return false; }
  74. virtual bool is_replaced() const { return false; }
  75. virtual bool is_widget() const { return false; }
  76. virtual bool is_image() const { return false; }
  77. virtual bool is_canvas() const { return false; }
  78. virtual bool is_box() const { return false; }
  79. virtual bool is_table() const { return false; }
  80. virtual bool is_table_row() const { return false; }
  81. virtual bool is_table_cell() const { return false; }
  82. bool has_style() const { return m_has_style; }
  83. bool is_inline() const { return m_inline; }
  84. void set_inline(bool b) { m_inline = b; }
  85. bool is_inline_block() const { return is_inline() && is_block(); }
  86. enum class LayoutMode {
  87. Default,
  88. AllPossibleLineBreaks,
  89. OnlyRequiredLineBreaks,
  90. };
  91. virtual void layout(LayoutMode);
  92. virtual void render(RenderingContext&);
  93. const LayoutBlock* containing_block() const;
  94. virtual LayoutNode& inline_wrapper() { return *this; }
  95. const StyleProperties& style() const;
  96. LayoutNodeWithStyle* parent();
  97. const LayoutNodeWithStyle* parent() const;
  98. void inserted_into(LayoutNode&) { }
  99. void removed_from(LayoutNode&) { }
  100. void children_changed() { }
  101. virtual void split_into_lines(LayoutBlock& container, LayoutMode);
  102. bool is_visible() const { return m_visible; }
  103. void set_visible(bool visible) { m_visible = visible; }
  104. virtual void set_needs_display();
  105. bool children_are_inline() const { return m_children_are_inline; }
  106. void set_children_are_inline(bool value) { m_children_are_inline = value; }
  107. template<typename U>
  108. const U* next_sibling_of_type() const;
  109. template<typename U>
  110. U* next_sibling_of_type();
  111. template<typename T>
  112. const T* first_child_of_type() const;
  113. template<typename T>
  114. T* first_child_of_type();
  115. template<typename T>
  116. const T* first_ancestor_of_type() const;
  117. template<typename T>
  118. T* first_ancestor_of_type();
  119. Gfx::FloatPoint box_type_agnostic_position() const;
  120. protected:
  121. explicit LayoutNode(const Node*);
  122. private:
  123. friend class LayoutNodeWithStyle;
  124. const Node* m_node { nullptr };
  125. bool m_inline { false };
  126. bool m_has_style { false };
  127. bool m_visible { true };
  128. bool m_children_are_inline { false };
  129. };
  130. class LayoutNodeWithStyle : public LayoutNode {
  131. public:
  132. virtual ~LayoutNodeWithStyle() override { }
  133. const StyleProperties& style() const { return m_style; }
  134. void set_style(const StyleProperties& style) { m_style = style; }
  135. protected:
  136. explicit LayoutNodeWithStyle(const Node* node, NonnullRefPtr<StyleProperties> style)
  137. : LayoutNode(node)
  138. , m_style(move(style))
  139. {
  140. m_has_style = true;
  141. }
  142. private:
  143. NonnullRefPtr<StyleProperties> m_style;
  144. };
  145. class LayoutNodeWithStyleAndBoxModelMetrics : public LayoutNodeWithStyle {
  146. public:
  147. BoxModelMetrics& box_model() { return m_box_model; }
  148. const BoxModelMetrics& box_model() const { return m_box_model; }
  149. protected:
  150. LayoutNodeWithStyleAndBoxModelMetrics(const Node* node, NonnullRefPtr<StyleProperties> style)
  151. : LayoutNodeWithStyle(node, move(style))
  152. {
  153. }
  154. private:
  155. BoxModelMetrics m_box_model;
  156. };
  157. inline const StyleProperties& 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. template<typename T>
  172. inline bool is(const LayoutNode&)
  173. {
  174. return false;
  175. }
  176. template<typename T>
  177. inline bool is(const LayoutNode* node)
  178. {
  179. return !node || is<T>(*node);
  180. }
  181. template<>
  182. inline bool is<LayoutNode>(const LayoutNode&)
  183. {
  184. return true;
  185. }
  186. template<>
  187. inline bool is<LayoutNodeWithStyle>(const LayoutNode& node)
  188. {
  189. return node.has_style();
  190. }
  191. template<typename T>
  192. inline const T& to(const LayoutNode& node)
  193. {
  194. ASSERT(is<T>(node));
  195. return static_cast<const T&>(node);
  196. }
  197. template<typename T>
  198. inline T* to(LayoutNode* node)
  199. {
  200. ASSERT(is<T>(node));
  201. return static_cast<T*>(node);
  202. }
  203. template<typename T>
  204. inline const T* to(const LayoutNode* node)
  205. {
  206. ASSERT(is<T>(node));
  207. return static_cast<const T*>(node);
  208. }
  209. template<typename T>
  210. inline T& to(LayoutNode& node)
  211. {
  212. ASSERT(is<T>(node));
  213. return static_cast<T&>(node);
  214. }
  215. template<typename T>
  216. inline const T* LayoutNode::next_sibling_of_type() const
  217. {
  218. for (auto* sibling = next_sibling(); sibling; sibling = sibling->next_sibling()) {
  219. if (is<T>(*sibling))
  220. return &to<T>(*sibling);
  221. }
  222. return nullptr;
  223. }
  224. template<typename T>
  225. inline T* LayoutNode::next_sibling_of_type()
  226. {
  227. for (auto* sibling = next_sibling(); sibling; sibling = sibling->next_sibling()) {
  228. if (is<T>(*sibling))
  229. return &to<T>(*sibling);
  230. }
  231. return nullptr;
  232. }
  233. template<typename T>
  234. inline const T* LayoutNode::first_child_of_type() const
  235. {
  236. for (auto* child = first_child(); child; child = child->next_sibling()) {
  237. if (is<T>(*child))
  238. return &to<T>(*child);
  239. }
  240. return nullptr;
  241. }
  242. template<typename T>
  243. inline T* LayoutNode::first_child_of_type()
  244. {
  245. for (auto* child = first_child(); child; child = child->next_sibling()) {
  246. if (is<T>(*child))
  247. return &to<T>(*child);
  248. }
  249. return nullptr;
  250. }
  251. template<typename T>
  252. inline const T* LayoutNode::first_ancestor_of_type() const
  253. {
  254. for (auto* ancestor = parent(); ancestor; ancestor = ancestor->parent()) {
  255. if (is<T>(*ancestor))
  256. return &to<T>(*ancestor);
  257. }
  258. return nullptr;
  259. }
  260. template<typename T>
  261. inline T* LayoutNode::first_ancestor_of_type()
  262. {
  263. for (auto* ancestor = parent(); ancestor; ancestor = ancestor->parent()) {
  264. if (is<T>(*ancestor))
  265. return &to<T>(*ancestor);
  266. }
  267. return nullptr;
  268. }
  269. }