LayoutBox.cpp 12 KB

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