LayoutBox.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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 <LibGUI/Painter.h>
  27. #include <LibWeb/DOM/Document.h>
  28. #include <LibWeb/HTML/HTMLBodyElement.h>
  29. #include <LibWeb/Frame/Frame.h>
  30. #include <LibWeb/Layout/LayoutBlock.h>
  31. #include <LibWeb/Layout/LayoutBox.h>
  32. namespace Web {
  33. void LayoutBox::paint_border(PaintContext& context, Edge edge, const Gfx::FloatRect& rect, CSS::PropertyID style_property_id, const BorderData& border_data)
  34. {
  35. float width = border_data.width;
  36. if (width <= 0)
  37. return;
  38. auto color = border_data.color;
  39. auto border_style = specified_style().property(style_property_id);
  40. int int_width = max((int)width, 1);
  41. auto first_point_for_edge = [](Edge edge, const Gfx::FloatRect& rect) {
  42. switch (edge) {
  43. case Edge::Top:
  44. return rect.top_left();
  45. case Edge::Right:
  46. return rect.top_right();
  47. case Edge::Bottom:
  48. return rect.bottom_left();
  49. case Edge::Left:
  50. default:
  51. return rect.top_left();
  52. }
  53. };
  54. auto second_point_for_edge = [](Edge edge, const Gfx::FloatRect& rect) {
  55. switch (edge) {
  56. case Edge::Top:
  57. return rect.top_right();
  58. case Edge::Right:
  59. return rect.bottom_right();
  60. case Edge::Bottom:
  61. return rect.bottom_right();
  62. case Edge::Left:
  63. default:
  64. return rect.bottom_left();
  65. }
  66. };
  67. auto p1 = first_point_for_edge(edge, rect);
  68. auto p2 = second_point_for_edge(edge, rect);
  69. if (border_style.has_value() && border_style.value()->to_string() == "inset") {
  70. auto top_left_color = Color::from_rgb(0x5a5a5a);
  71. auto bottom_right_color = Color::from_rgb(0x888888);
  72. color = (edge == Edge::Left || edge == Edge::Top) ? top_left_color : bottom_right_color;
  73. } else if (border_style.has_value() && border_style.value()->to_string() == "outset") {
  74. auto top_left_color = Color::from_rgb(0x888888);
  75. auto bottom_right_color = Color::from_rgb(0x5a5a5a);
  76. color = (edge == Edge::Left || edge == Edge::Top) ? top_left_color : bottom_right_color;
  77. }
  78. auto line_style = Gfx::Painter::LineStyle::Solid;
  79. if (border_style.has_value()) {
  80. if (border_style.value()->to_string() == "dotted")
  81. line_style = Gfx::Painter::LineStyle::Dotted;
  82. if (border_style.value()->to_string() == "dashed")
  83. line_style = Gfx::Painter::LineStyle::Dashed;
  84. }
  85. if (line_style != Gfx::Painter::LineStyle::Solid) {
  86. switch (edge) {
  87. case Edge::Top:
  88. p1.move_by(int_width / 2, int_width / 2);
  89. p2.move_by(-int_width / 2, int_width / 2);
  90. break;
  91. case Edge::Right:
  92. p1.move_by(-int_width / 2, int_width / 2);
  93. p2.move_by(-int_width / 2, -int_width / 2);
  94. break;
  95. case Edge::Bottom:
  96. p1.move_by(int_width / 2, -int_width / 2);
  97. p2.move_by(-int_width / 2, -int_width / 2);
  98. break;
  99. case Edge::Left:
  100. p1.move_by(int_width / 2, int_width / 2);
  101. p2.move_by(int_width / 2, -int_width / 2);
  102. break;
  103. }
  104. context.painter().draw_line({ (int)p1.x(), (int)p1.y() }, { (int)p2.x(), (int)p2.y() }, color, int_width, line_style);
  105. return;
  106. }
  107. auto draw_line = [&](auto& p1, auto& p2) {
  108. context.painter().draw_line({ (int)p1.x(), (int)p1.y() }, { (int)p2.x(), (int)p2.y() }, color, 1, line_style);
  109. };
  110. float p1_step = 0;
  111. float p2_step = 0;
  112. switch (edge) {
  113. case Edge::Top:
  114. p1_step = style().border_left().width / (float)int_width;
  115. p2_step = style().border_right().width / (float)int_width;
  116. for (int i = 0; i < int_width; ++i) {
  117. draw_line(p1, p2);
  118. p1.move_by(p1_step, 1);
  119. p2.move_by(-p2_step, 1);
  120. }
  121. break;
  122. case Edge::Right:
  123. p1_step = style().border_top().width / (float)int_width;
  124. p2_step = style().border_bottom().width / (float)int_width;
  125. for (int i = int_width - 1; i >= 0; --i) {
  126. draw_line(p1, p2);
  127. p1.move_by(-1, p1_step);
  128. p2.move_by(-1, -p2_step);
  129. }
  130. break;
  131. case Edge::Bottom:
  132. p1_step = style().border_left().width / (float)int_width;
  133. p2_step = style().border_right().width / (float)int_width;
  134. for (int i = int_width - 1; i >= 0; --i) {
  135. draw_line(p1, p2);
  136. p1.move_by(p1_step, -1);
  137. p2.move_by(-p2_step, -1);
  138. }
  139. break;
  140. case Edge::Left:
  141. p1_step = style().border_top().width / (float)int_width;
  142. p2_step = style().border_bottom().width / (float)int_width;
  143. for (int i = 0; i < int_width; ++i) {
  144. draw_line(p1, p2);
  145. p1.move_by(1, p1_step);
  146. p2.move_by(1, -p2_step);
  147. }
  148. break;
  149. }
  150. }
  151. void LayoutBox::paint(PaintContext& context, PaintPhase phase)
  152. {
  153. if (!is_visible())
  154. return;
  155. Gfx::PainterStateSaver saver(context.painter());
  156. if (is_fixed_position())
  157. context.painter().translate(context.scroll_offset());
  158. Gfx::FloatRect padded_rect;
  159. padded_rect.set_x(absolute_x() - box_model().padding.left.to_px(*this));
  160. padded_rect.set_width(width() + box_model().padding.left.to_px(*this) + box_model().padding.right.to_px(*this));
  161. padded_rect.set_y(absolute_y() - box_model().padding.top.to_px(*this));
  162. padded_rect.set_height(height() + box_model().padding.top.to_px(*this) + box_model().padding.bottom.to_px(*this));
  163. if (phase == PaintPhase::Background && !is_body()) {
  164. // FIXME: We should paint the body here too, but that currently happens at the view layer.
  165. auto bgcolor = specified_style().property(CSS::PropertyID::BackgroundColor);
  166. if (bgcolor.has_value() && bgcolor.value()->is_color()) {
  167. context.painter().fill_rect(enclosing_int_rect(padded_rect), bgcolor.value()->to_color(document()));
  168. }
  169. auto bgimage = specified_style().property(CSS::PropertyID::BackgroundImage);
  170. if (bgimage.has_value() && bgimage.value()->is_image()) {
  171. auto& image_value = static_cast<const CSS::ImageStyleValue&>(*bgimage.value());
  172. if (image_value.bitmap()) {
  173. context.painter().draw_tiled_bitmap(enclosing_int_rect(padded_rect), *image_value.bitmap());
  174. }
  175. }
  176. }
  177. if (phase == PaintPhase::Border) {
  178. Gfx::FloatRect bordered_rect;
  179. bordered_rect.set_x(padded_rect.x() - box_model().border.left.to_px(*this));
  180. bordered_rect.set_width(padded_rect.width() + box_model().border.left.to_px(*this) + box_model().border.right.to_px(*this));
  181. bordered_rect.set_y(padded_rect.y() - box_model().border.top.to_px(*this));
  182. bordered_rect.set_height(padded_rect.height() + box_model().border.top.to_px(*this) + box_model().border.bottom.to_px(*this));
  183. paint_border(context, Edge::Left, bordered_rect, CSS::PropertyID::BorderLeftStyle, style().border_left());
  184. paint_border(context, Edge::Right, bordered_rect, CSS::PropertyID::BorderRightStyle, style().border_right());
  185. paint_border(context, Edge::Top, bordered_rect, CSS::PropertyID::BorderTopStyle, style().border_top());
  186. paint_border(context, Edge::Bottom, bordered_rect, CSS::PropertyID::BorderBottomStyle, style().border_bottom());
  187. }
  188. LayoutNodeWithStyleAndBoxModelMetrics::paint(context, phase);
  189. if (phase == PaintPhase::Overlay && node() && document().inspected_node() == node()) {
  190. auto content_rect = absolute_rect();
  191. auto margin_box = box_model().margin_box(*this);
  192. Gfx::FloatRect margin_rect;
  193. margin_rect.set_x(absolute_x() - margin_box.left);
  194. margin_rect.set_width(width() + margin_box.left + margin_box.right);
  195. margin_rect.set_y(absolute_y() - margin_box.top);
  196. margin_rect.set_height(height() + margin_box.top + margin_box.bottom);
  197. context.painter().draw_rect(enclosing_int_rect(margin_rect), Color::Yellow);
  198. context.painter().draw_rect(enclosing_int_rect(padded_rect), Color::Cyan);
  199. context.painter().draw_rect(enclosing_int_rect(content_rect), Color::Magenta);
  200. }
  201. }
  202. HitTestResult LayoutBox::hit_test(const Gfx::IntPoint& position) const
  203. {
  204. // FIXME: It would be nice if we could confidently skip over hit testing
  205. // parts of the layout tree, but currently we can't just check
  206. // m_rect.contains() since inline text rects can't be trusted..
  207. HitTestResult result { absolute_rect().contains(position.x(), position.y()) ? this : nullptr };
  208. for_each_child([&](auto& child) {
  209. if (is<LayoutBox>(child) && downcast<LayoutBox>(child).stacking_context())
  210. return;
  211. auto child_result = child.hit_test(position);
  212. if (child_result.layout_node)
  213. result = child_result;
  214. });
  215. return result;
  216. }
  217. void LayoutBox::set_needs_display()
  218. {
  219. if (!is_inline()) {
  220. frame().set_needs_display(enclosing_int_rect(absolute_rect()));
  221. return;
  222. }
  223. LayoutNode::set_needs_display();
  224. }
  225. bool LayoutBox::is_body() const
  226. {
  227. return node() && node() == document().body();
  228. }
  229. void LayoutBox::set_offset(const Gfx::FloatPoint& offset)
  230. {
  231. if (m_offset == offset)
  232. return;
  233. m_offset = offset;
  234. did_set_rect();
  235. }
  236. void LayoutBox::set_size(const Gfx::FloatSize& size)
  237. {
  238. if (m_size == size)
  239. return;
  240. m_size = size;
  241. did_set_rect();
  242. }
  243. Gfx::FloatPoint LayoutBox::effective_offset() const
  244. {
  245. if (m_containing_line_box_fragment)
  246. return m_containing_line_box_fragment->offset();
  247. return m_offset;
  248. }
  249. const Gfx::FloatRect LayoutBox::absolute_rect() const
  250. {
  251. Gfx::FloatRect rect { effective_offset(), size() };
  252. for (auto* block = containing_block(); block; block = block->containing_block()) {
  253. rect.move_by(block->effective_offset());
  254. }
  255. return rect;
  256. }
  257. void LayoutBox::set_containing_line_box_fragment(LineBoxFragment& fragment)
  258. {
  259. m_containing_line_box_fragment = fragment.make_weak_ptr();
  260. }
  261. StackingContext* LayoutBox::enclosing_stacking_context()
  262. {
  263. for (auto* ancestor = parent(); ancestor; ancestor = ancestor->parent()) {
  264. if (!ancestor->is_box())
  265. continue;
  266. auto& ancestor_box = downcast<LayoutBox>(*ancestor);
  267. if (!ancestor_box.establishes_stacking_context())
  268. continue;
  269. ASSERT(ancestor_box.stacking_context());
  270. return ancestor_box.stacking_context();
  271. }
  272. // We should always reach the LayoutDocument stacking context.
  273. ASSERT_NOT_REACHED();
  274. }
  275. bool LayoutBox::establishes_stacking_context() const
  276. {
  277. if (!has_style())
  278. return false;
  279. if (node() == document().root())
  280. return true;
  281. auto position = style().position();
  282. auto z_index = style().z_index();
  283. if (position == CSS::Position::Absolute || position == CSS::Position::Relative) {
  284. if (z_index.has_value())
  285. return true;
  286. }
  287. if (position == CSS::Position::Fixed || position == CSS::Position::Sticky)
  288. return true;
  289. return false;
  290. }
  291. }