Box.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. #include <LibGfx/Painter.h>
  27. #include <LibWeb/DOM/Document.h>
  28. #include <LibWeb/HTML/HTMLBodyElement.h>
  29. #include <LibWeb/Layout/BlockBox.h>
  30. #include <LibWeb/Layout/Box.h>
  31. #include <LibWeb/Page/Frame.h>
  32. #include <LibWeb/Painting/BorderPainting.h>
  33. namespace Web::Layout {
  34. void Box::paint(PaintContext& context, PaintPhase phase)
  35. {
  36. if (!is_visible())
  37. return;
  38. Gfx::PainterStateSaver saver(context.painter());
  39. if (is_fixed_position())
  40. context.painter().translate(context.scroll_offset());
  41. auto padded_rect = this->padded_rect();
  42. if (phase == PaintPhase::Background && !is_body()) {
  43. auto background_rect = enclosing_int_rect(padded_rect);
  44. context.painter().fill_rect(background_rect, computed_values().background_color());
  45. if (background_image() && background_image()->bitmap()) {
  46. paint_background_image(context, *background_image()->bitmap(), computed_values().background_repeat_x(), computed_values().background_repeat_y(), move(background_rect));
  47. }
  48. }
  49. if (phase == PaintPhase::Border) {
  50. auto bordered_rect = this->bordered_rect();
  51. Painting::paint_border(context, Painting::BorderEdge::Left, bordered_rect, computed_values());
  52. Painting::paint_border(context, Painting::BorderEdge::Right, bordered_rect, computed_values());
  53. Painting::paint_border(context, Painting::BorderEdge::Top, bordered_rect, computed_values());
  54. Painting::paint_border(context, Painting::BorderEdge::Bottom, bordered_rect, computed_values());
  55. }
  56. Layout::NodeWithStyleAndBoxModelMetrics::paint(context, phase);
  57. if (phase == PaintPhase::Overlay && dom_node() && document().inspected_node() == dom_node()) {
  58. auto content_rect = absolute_rect();
  59. auto margin_box = box_model().margin_box();
  60. Gfx::FloatRect margin_rect;
  61. margin_rect.set_x(absolute_x() - margin_box.left);
  62. margin_rect.set_width(width() + margin_box.left + margin_box.right);
  63. margin_rect.set_y(absolute_y() - margin_box.top);
  64. margin_rect.set_height(height() + margin_box.top + margin_box.bottom);
  65. context.painter().draw_rect(enclosing_int_rect(margin_rect), Color::Yellow);
  66. context.painter().draw_rect(enclosing_int_rect(padded_rect), Color::Cyan);
  67. context.painter().draw_rect(enclosing_int_rect(content_rect), Color::Magenta);
  68. }
  69. if (phase == PaintPhase::FocusOutline && dom_node() && dom_node()->is_element() && downcast<DOM::Element>(*dom_node()).is_focused()) {
  70. context.painter().draw_rect(enclosing_int_rect(absolute_rect()), context.palette().focus_outline());
  71. }
  72. }
  73. void Box::paint_background_image(
  74. PaintContext& context,
  75. const Gfx::Bitmap& background_image,
  76. CSS::Repeat background_repeat_x,
  77. CSS::Repeat background_repeat_y,
  78. Gfx::IntRect background_rect)
  79. {
  80. switch (background_repeat_x) {
  81. case CSS::Repeat::Round:
  82. case CSS::Repeat::Space:
  83. // FIXME: Support 'round' and 'space'. Fall through to 'repeat' since that most closely resembles these.
  84. case CSS::Repeat::Repeat:
  85. // The background rect is already sized to align with 'repeat'.
  86. break;
  87. case CSS::Repeat::NoRepeat:
  88. background_rect.set_width(background_image.width());
  89. break;
  90. }
  91. switch (background_repeat_y) {
  92. case CSS::Repeat::Round:
  93. case CSS::Repeat::Space:
  94. // FIXME: Support 'round' and 'space'. Fall through to 'repeat' since that most closely resembles these.
  95. case CSS::Repeat::Repeat:
  96. // The background rect is already sized to align with 'repeat'.
  97. break;
  98. case CSS::Repeat::NoRepeat:
  99. background_rect.set_height(background_image.height());
  100. break;
  101. }
  102. context.painter().blit_tiled(background_rect, background_image, background_image.rect());
  103. }
  104. HitTestResult Box::hit_test(const Gfx::IntPoint& position, HitTestType type) const
  105. {
  106. // FIXME: It would be nice if we could confidently skip over hit testing
  107. // parts of the layout tree, but currently we can't just check
  108. // m_rect.contains() since inline text rects can't be trusted..
  109. HitTestResult result { absolute_rect().contains(position.x(), position.y()) ? this : nullptr };
  110. for_each_child_in_paint_order([&](auto& child) {
  111. auto child_result = child.hit_test(position, type);
  112. if (child_result.layout_node)
  113. result = child_result;
  114. });
  115. return result;
  116. }
  117. void Box::set_needs_display()
  118. {
  119. if (!is_inline()) {
  120. frame().set_needs_display(enclosing_int_rect(absolute_rect()));
  121. return;
  122. }
  123. Node::set_needs_display();
  124. }
  125. bool Box::is_body() const
  126. {
  127. return dom_node() && dom_node() == document().body();
  128. }
  129. void Box::set_offset(const Gfx::FloatPoint& offset)
  130. {
  131. if (m_offset == offset)
  132. return;
  133. m_offset = offset;
  134. did_set_rect();
  135. }
  136. void Box::set_size(const Gfx::FloatSize& size)
  137. {
  138. if (m_size == size)
  139. return;
  140. m_size = size;
  141. did_set_rect();
  142. }
  143. Gfx::FloatPoint Box::effective_offset() const
  144. {
  145. if (m_containing_line_box_fragment)
  146. return m_containing_line_box_fragment->offset();
  147. return m_offset;
  148. }
  149. const Gfx::FloatRect Box::absolute_rect() const
  150. {
  151. Gfx::FloatRect rect { effective_offset(), size() };
  152. for (auto* block = containing_block(); block; block = block->containing_block()) {
  153. rect.move_by(block->effective_offset());
  154. }
  155. return rect;
  156. }
  157. void Box::set_containing_line_box_fragment(LineBoxFragment& fragment)
  158. {
  159. m_containing_line_box_fragment = fragment.make_weak_ptr();
  160. }
  161. StackingContext* Box::enclosing_stacking_context()
  162. {
  163. for (auto* ancestor = parent(); ancestor; ancestor = ancestor->parent()) {
  164. if (!is<Box>(ancestor))
  165. continue;
  166. auto& ancestor_box = downcast<Box>(*ancestor);
  167. if (!ancestor_box.establishes_stacking_context())
  168. continue;
  169. VERIFY(ancestor_box.stacking_context());
  170. return ancestor_box.stacking_context();
  171. }
  172. // We should always reach the Layout::InitialContainingBlockBox stacking context.
  173. VERIFY_NOT_REACHED();
  174. }
  175. bool Box::establishes_stacking_context() const
  176. {
  177. if (!has_style())
  178. return false;
  179. if (dom_node() == document().root())
  180. return true;
  181. auto position = computed_values().position();
  182. auto z_index = computed_values().z_index();
  183. if (position == CSS::Position::Absolute || position == CSS::Position::Relative) {
  184. if (z_index.has_value())
  185. return true;
  186. }
  187. if (position == CSS::Position::Fixed || position == CSS::Position::Sticky)
  188. return true;
  189. return false;
  190. }
  191. LineBox& Box::ensure_last_line_box()
  192. {
  193. if (m_line_boxes.is_empty())
  194. return add_line_box();
  195. return m_line_boxes.last();
  196. }
  197. LineBox& Box::add_line_box()
  198. {
  199. m_line_boxes.append(LineBox());
  200. return m_line_boxes.last();
  201. }
  202. float Box::width_of_logical_containing_block() const
  203. {
  204. auto* containing_block = this->containing_block();
  205. VERIFY(containing_block);
  206. return containing_block->width();
  207. }
  208. }