LayoutBox.cpp 8.2 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. } else {
  67. // border-style: solid
  68. }
  69. auto draw_line = [&](auto& p1, auto& p2) {
  70. context.painter().draw_line({ (int)p1.x(), (int)p1.y() }, { (int)p2.x(), (int)p2.y() }, color, 1);
  71. };
  72. auto width_for = [&](CSS::PropertyID property_id) -> float {
  73. auto width = style().property(property_id);
  74. if (!width.has_value())
  75. return 0;
  76. return width.value()->to_length().to_px();
  77. };
  78. float p1_step = 0;
  79. float p2_step = 0;
  80. switch (edge) {
  81. case Edge::Top:
  82. p1_step = width_for(CSS::PropertyID::BorderLeftWidth) / (float)int_width;
  83. p2_step = width_for(CSS::PropertyID::BorderRightWidth) / (float)int_width;
  84. for (int i = 0; i < int_width; ++i) {
  85. draw_line(p1, p2);
  86. p1.move_by(p1_step, 1);
  87. p2.move_by(-p2_step, 1);
  88. }
  89. break;
  90. case Edge::Right:
  91. p1_step = width_for(CSS::PropertyID::BorderTopWidth) / (float)int_width;
  92. p2_step = width_for(CSS::PropertyID::BorderBottomWidth) / (float)int_width;
  93. for (int i = int_width - 1; i >= 0; --i) {
  94. draw_line(p1, p2);
  95. p1.move_by(-1, p1_step);
  96. p2.move_by(-1, -p2_step);
  97. }
  98. break;
  99. case Edge::Bottom:
  100. p1_step = width_for(CSS::PropertyID::BorderLeftWidth) / (float)int_width;
  101. p2_step = width_for(CSS::PropertyID::BorderRightWidth) / (float)int_width;
  102. for (int i = int_width - 1; i >= 0; --i) {
  103. draw_line(p1, p2);
  104. p1.move_by(p1_step, -1);
  105. p2.move_by(-p2_step, -1);
  106. }
  107. break;
  108. case Edge::Left:
  109. p1_step = width_for(CSS::PropertyID::BorderTopWidth) / (float)int_width;
  110. p2_step = width_for(CSS::PropertyID::BorderBottomWidth) / (float)int_width;
  111. for (int i = 0; i < int_width; ++i) {
  112. draw_line(p1, p2);
  113. p1.move_by(1, p1_step);
  114. p2.move_by(1, -p2_step);
  115. }
  116. break;
  117. }
  118. }
  119. void LayoutBox::render(RenderingContext& context)
  120. {
  121. if (!is_visible())
  122. return;
  123. #ifdef DRAW_BOXES_AROUND_LAYOUT_NODES
  124. context.painter().draw_rect(m_rect, Color::Blue);
  125. #endif
  126. #ifdef DRAW_BOXES_AROUND_HOVERED_NODES
  127. if (!is_anonymous() && node() == document().hovered_node())
  128. context.painter().draw_rect(m_rect, Color::Red);
  129. #endif
  130. if (node() && document().inspected_node() == node())
  131. context.painter().draw_rect(enclosing_int_rect(m_rect), Color::Magenta);
  132. FloatRect padded_rect;
  133. padded_rect.set_x(x() - box_model().padding().left.to_px());
  134. padded_rect.set_width(width() + box_model().padding().left.to_px() + box_model().padding().right.to_px());
  135. padded_rect.set_y(y() - box_model().padding().top.to_px());
  136. padded_rect.set_height(height() + box_model().padding().top.to_px() + box_model().padding().bottom.to_px());
  137. if (!is_body()) {
  138. auto bgcolor = style().property(CSS::PropertyID::BackgroundColor);
  139. if (bgcolor.has_value() && bgcolor.value()->is_color()) {
  140. context.painter().fill_rect(enclosing_int_rect(padded_rect), bgcolor.value()->to_color(document()));
  141. }
  142. auto bgimage = style().property(CSS::PropertyID::BackgroundImage);
  143. if (bgimage.has_value() && bgimage.value()->is_image()) {
  144. auto& image_value = static_cast<const ImageStyleValue&>(*bgimage.value());
  145. if (image_value.bitmap()) {
  146. context.painter().draw_tiled_bitmap(enclosing_int_rect(padded_rect), *image_value.bitmap());
  147. }
  148. }
  149. }
  150. FloatRect bordered_rect;
  151. bordered_rect.set_x(padded_rect.x() - box_model().border().left.to_px());
  152. bordered_rect.set_width(padded_rect.width() + box_model().border().left.to_px() + box_model().border().right.to_px());
  153. bordered_rect.set_y(padded_rect.y() - box_model().border().top.to_px());
  154. bordered_rect.set_height(padded_rect.height() + box_model().border().top.to_px() + box_model().border().bottom.to_px());
  155. paint_border(context, Edge::Left, bordered_rect, CSS::PropertyID::BorderLeftStyle, CSS::PropertyID::BorderLeftColor, CSS::PropertyID::BorderLeftWidth);
  156. paint_border(context, Edge::Right, bordered_rect, CSS::PropertyID::BorderRightStyle, CSS::PropertyID::BorderRightColor, CSS::PropertyID::BorderRightWidth);
  157. paint_border(context, Edge::Top, bordered_rect, CSS::PropertyID::BorderTopStyle, CSS::PropertyID::BorderTopColor, CSS::PropertyID::BorderTopWidth);
  158. paint_border(context, Edge::Bottom, bordered_rect, CSS::PropertyID::BorderBottomStyle, CSS::PropertyID::BorderBottomColor, CSS::PropertyID::BorderBottomWidth);
  159. LayoutNodeWithStyleAndBoxModelMetrics::render(context);
  160. }
  161. HitTestResult LayoutBox::hit_test(const Point& position) const
  162. {
  163. // FIXME: It would be nice if we could confidently skip over hit testing
  164. // parts of the layout tree, but currently we can't just check
  165. // m_rect.contains() since inline text rects can't be trusted..
  166. HitTestResult result { m_rect.contains(FloatPoint(position.x(), position.y())) ? this : nullptr };
  167. for_each_child([&](auto& child) {
  168. auto child_result = child.hit_test(position);
  169. if (child_result.layout_node)
  170. result = child_result;
  171. });
  172. return result;
  173. }
  174. void LayoutBox::set_needs_display()
  175. {
  176. auto* frame = document().frame();
  177. ASSERT(frame);
  178. if (!is_inline()) {
  179. const_cast<Frame*>(frame)->set_needs_display(enclosing_int_rect(rect()));
  180. return;
  181. }
  182. LayoutNode::set_needs_display();
  183. }
  184. bool LayoutBox::is_body() const
  185. {
  186. return node() && node() == document().body();
  187. }