LayoutBox.cpp 13 KB

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