LayoutBox.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #include <LibGUI/GPainter.h>
  2. #include <LibHTML/DOM/Document.h>
  3. #include <LibHTML/DOM/HTMLBodyElement.h>
  4. #include <LibHTML/Frame.h>
  5. #include <LibHTML/Layout/LayoutBlock.h>
  6. #include <LibHTML/Layout/LayoutBox.h>
  7. //#define DRAW_BOXES_AROUND_LAYOUT_NODES
  8. //#define DRAW_BOXES_AROUND_HOVERED_NODES
  9. void LayoutBox::paint_border(RenderingContext& context, Edge edge, const FloatRect& rect, CSS::PropertyID style_property_id, CSS::PropertyID color_property_id, CSS::PropertyID width_property_id)
  10. {
  11. auto border_width = style().property(width_property_id);
  12. if (!border_width.has_value())
  13. return;
  14. auto border_style = style().property(style_property_id);
  15. float width = border_width.value()->to_length().to_px();
  16. int int_width = max((int)width, 1);
  17. Color color;
  18. auto border_color = style().property(color_property_id);
  19. if (border_color.has_value()) {
  20. color = border_color.value()->to_color(document());
  21. } else {
  22. // FIXME: This is basically CSS "currentColor" which should be handled elsewhere
  23. // in a much more reusable way.
  24. auto current_color = style().property(CSS::PropertyID::Color);
  25. if (current_color.has_value())
  26. color = current_color.value()->to_color(document());
  27. else
  28. color = Color::Black;
  29. }
  30. auto first_point_for_edge = [](Edge edge, const FloatRect& rect) {
  31. switch (edge) {
  32. case Edge::Top:
  33. return rect.top_left();
  34. case Edge::Right:
  35. return rect.top_right();
  36. case Edge::Bottom:
  37. return rect.bottom_left();
  38. case Edge::Left:
  39. default:
  40. return rect.top_left();
  41. }
  42. };
  43. auto second_point_for_edge = [](Edge edge, const FloatRect& rect) {
  44. switch (edge) {
  45. case Edge::Top:
  46. return rect.top_right();
  47. case Edge::Right:
  48. return rect.bottom_right();
  49. case Edge::Bottom:
  50. return rect.bottom_right();
  51. case Edge::Left:
  52. default:
  53. return rect.bottom_left();
  54. }
  55. };
  56. auto p1 = first_point_for_edge(edge, rect);
  57. auto p2 = second_point_for_edge(edge, rect);
  58. if (border_style.has_value() && border_style.value()->to_string() == "inset") {
  59. auto top_left_color = Color::from_rgb(0x5a5a5a);
  60. auto bottom_right_color = Color::from_rgb(0x888888);
  61. color = (edge == Edge::Left || edge == Edge::Top) ? top_left_color : bottom_right_color;
  62. } else if (border_style.has_value() && border_style.value()->to_string() == "outset") {
  63. auto top_left_color = Color::from_rgb(0x888888);
  64. auto bottom_right_color = Color::from_rgb(0x5a5a5a);
  65. color = (edge == Edge::Left || edge == Edge::Top) ? top_left_color : bottom_right_color;
  66. }
  67. bool dotted = border_style.has_value() && border_style.value()->to_string() == "dotted";
  68. auto draw_line = [&](auto& p1, auto& p2) {
  69. context.painter().draw_line({ (int)p1.x(), (int)p1.y() }, { (int)p2.x(), (int)p2.y() }, color, 1, dotted);
  70. };
  71. auto width_for = [&](CSS::PropertyID property_id) -> float {
  72. auto width = style().property(property_id);
  73. if (!width.has_value())
  74. return 0;
  75. return width.value()->to_length().to_px();
  76. };
  77. float p1_step = 0;
  78. float p2_step = 0;
  79. switch (edge) {
  80. case Edge::Top:
  81. p1_step = width_for(CSS::PropertyID::BorderLeftWidth) / (float)int_width;
  82. p2_step = width_for(CSS::PropertyID::BorderRightWidth) / (float)int_width;
  83. for (int i = 0; i < int_width; ++i) {
  84. draw_line(p1, p2);
  85. p1.move_by(p1_step, 1);
  86. p2.move_by(-p2_step, 1);
  87. }
  88. break;
  89. case Edge::Right:
  90. p1_step = width_for(CSS::PropertyID::BorderTopWidth) / (float)int_width;
  91. p2_step = width_for(CSS::PropertyID::BorderBottomWidth) / (float)int_width;
  92. for (int i = int_width - 1; i >= 0; --i) {
  93. draw_line(p1, p2);
  94. p1.move_by(-1, p1_step);
  95. p2.move_by(-1, -p2_step);
  96. }
  97. break;
  98. case Edge::Bottom:
  99. p1_step = width_for(CSS::PropertyID::BorderLeftWidth) / (float)int_width;
  100. p2_step = width_for(CSS::PropertyID::BorderRightWidth) / (float)int_width;
  101. for (int i = int_width - 1; i >= 0; --i) {
  102. draw_line(p1, p2);
  103. p1.move_by(p1_step, -1);
  104. p2.move_by(-p2_step, -1);
  105. }
  106. break;
  107. case Edge::Left:
  108. p1_step = width_for(CSS::PropertyID::BorderTopWidth) / (float)int_width;
  109. p2_step = width_for(CSS::PropertyID::BorderBottomWidth) / (float)int_width;
  110. for (int i = 0; i < int_width; ++i) {
  111. draw_line(p1, p2);
  112. p1.move_by(1, p1_step);
  113. p2.move_by(1, -p2_step);
  114. }
  115. break;
  116. }
  117. }
  118. void LayoutBox::render(RenderingContext& context)
  119. {
  120. if (!is_visible())
  121. return;
  122. #ifdef DRAW_BOXES_AROUND_LAYOUT_NODES
  123. context.painter().draw_rect(m_rect, Color::Blue);
  124. #endif
  125. #ifdef DRAW_BOXES_AROUND_HOVERED_NODES
  126. if (!is_anonymous() && node() == document().hovered_node())
  127. context.painter().draw_rect(m_rect, Color::Red);
  128. #endif
  129. if (node() && document().inspected_node() == node())
  130. context.painter().draw_rect(enclosing_int_rect(m_rect), Color::Magenta);
  131. FloatRect padded_rect;
  132. padded_rect.set_x(x() - box_model().padding().left.to_px());
  133. padded_rect.set_width(width() + box_model().padding().left.to_px() + box_model().padding().right.to_px());
  134. padded_rect.set_y(y() - box_model().padding().top.to_px());
  135. padded_rect.set_height(height() + box_model().padding().top.to_px() + box_model().padding().bottom.to_px());
  136. if (!is_body()) {
  137. auto bgcolor = style().property(CSS::PropertyID::BackgroundColor);
  138. if (bgcolor.has_value() && bgcolor.value()->is_color()) {
  139. context.painter().fill_rect(enclosing_int_rect(padded_rect), bgcolor.value()->to_color(document()));
  140. }
  141. auto bgimage = style().property(CSS::PropertyID::BackgroundImage);
  142. if (bgimage.has_value() && bgimage.value()->is_image()) {
  143. auto& image_value = static_cast<const ImageStyleValue&>(*bgimage.value());
  144. if (image_value.bitmap()) {
  145. context.painter().draw_tiled_bitmap(enclosing_int_rect(padded_rect), *image_value.bitmap());
  146. }
  147. }
  148. }
  149. FloatRect bordered_rect;
  150. bordered_rect.set_x(padded_rect.x() - box_model().border().left.to_px());
  151. bordered_rect.set_width(padded_rect.width() + box_model().border().left.to_px() + box_model().border().right.to_px());
  152. bordered_rect.set_y(padded_rect.y() - box_model().border().top.to_px());
  153. bordered_rect.set_height(padded_rect.height() + box_model().border().top.to_px() + box_model().border().bottom.to_px());
  154. paint_border(context, Edge::Left, bordered_rect, CSS::PropertyID::BorderLeftStyle, CSS::PropertyID::BorderLeftColor, CSS::PropertyID::BorderLeftWidth);
  155. paint_border(context, Edge::Right, bordered_rect, CSS::PropertyID::BorderRightStyle, CSS::PropertyID::BorderRightColor, CSS::PropertyID::BorderRightWidth);
  156. paint_border(context, Edge::Top, bordered_rect, CSS::PropertyID::BorderTopStyle, CSS::PropertyID::BorderTopColor, CSS::PropertyID::BorderTopWidth);
  157. paint_border(context, Edge::Bottom, bordered_rect, CSS::PropertyID::BorderBottomStyle, CSS::PropertyID::BorderBottomColor, CSS::PropertyID::BorderBottomWidth);
  158. LayoutNodeWithStyleAndBoxModelMetrics::render(context);
  159. }
  160. HitTestResult LayoutBox::hit_test(const Point& position) const
  161. {
  162. // FIXME: It would be nice if we could confidently skip over hit testing
  163. // parts of the layout tree, but currently we can't just check
  164. // m_rect.contains() since inline text rects can't be trusted..
  165. HitTestResult result { m_rect.contains(FloatPoint(position.x(), position.y())) ? this : nullptr };
  166. for_each_child([&](auto& child) {
  167. auto child_result = child.hit_test(position);
  168. if (child_result.layout_node)
  169. result = child_result;
  170. });
  171. return result;
  172. }
  173. void LayoutBox::set_needs_display()
  174. {
  175. auto* frame = document().frame();
  176. ASSERT(frame);
  177. if (!is_inline()) {
  178. const_cast<Frame*>(frame)->set_needs_display(enclosing_int_rect(rect()));
  179. return;
  180. }
  181. LayoutNode::set_needs_display();
  182. }
  183. bool LayoutBox::is_body() const
  184. {
  185. return node() && node() == document().body();
  186. }