LayoutBlock.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 <LibHTML/Layout/LayoutBox.h>
  28. #include <LibHTML/Layout/LineBox.h>
  29. class Element;
  30. class LayoutBlock : public LayoutBox {
  31. public:
  32. LayoutBlock(const Node*, NonnullRefPtr<StyleProperties>);
  33. virtual ~LayoutBlock() override;
  34. virtual const char* class_name() const override { return "LayoutBlock"; }
  35. virtual void layout() override;
  36. virtual void render(RenderingContext&) override;
  37. virtual LayoutNode& inline_wrapper() override;
  38. Vector<LineBox>& line_boxes() { return m_line_boxes; }
  39. const Vector<LineBox>& line_boxes() const { return m_line_boxes; }
  40. LineBox& ensure_last_line_box();
  41. LineBox& add_line_box();
  42. virtual HitTestResult hit_test(const Gfx::Point&) const override;
  43. LayoutBlock* previous_sibling() { return to<LayoutBlock>(LayoutNode::previous_sibling()); }
  44. const LayoutBlock* previous_sibling() const { return to<LayoutBlock>(LayoutNode::previous_sibling()); }
  45. LayoutBlock* next_sibling() { return to<LayoutBlock>(LayoutNode::next_sibling()); }
  46. const LayoutBlock* next_sibling() const { return to<LayoutBlock>(LayoutNode::next_sibling()); }
  47. template<typename Callback>
  48. void for_each_fragment(Callback);
  49. template<typename Callback>
  50. void for_each_fragment(Callback) const;
  51. private:
  52. virtual bool is_block() const override { return true; }
  53. NonnullRefPtr<StyleProperties> style_for_anonymous_block() const;
  54. void layout_inline_children();
  55. void layout_block_children();
  56. void compute_width();
  57. void compute_position();
  58. void compute_height();
  59. Vector<LineBox> m_line_boxes;
  60. };
  61. template<typename Callback>
  62. void LayoutBlock::for_each_fragment(Callback callback)
  63. {
  64. for (auto& line_box : line_boxes()) {
  65. for (auto& fragment : line_box.fragments()) {
  66. if (callback(fragment) == IterationDecision::Break)
  67. return;
  68. }
  69. }
  70. }
  71. template<typename Callback>
  72. void LayoutBlock::for_each_fragment(Callback callback) const
  73. {
  74. for (auto& line_box : line_boxes()) {
  75. for (auto& fragment : line_box.fragments()) {
  76. if (callback(fragment) == IterationDecision::Break)
  77. return;
  78. }
  79. }
  80. }
  81. template<>
  82. inline bool is<LayoutBlock>(const LayoutNode& node)
  83. {
  84. return node.is_block();
  85. }