LayoutBox.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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.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();
  42. int int_width = max((int)width, 1);
  43. Color color;
  44. auto border_color = 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 = 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. }
  98. auto draw_line = [&](auto& p1, auto& p2) {
  99. context.painter().draw_line({ (int)p1.x(), (int)p1.y() }, { (int)p2.x(), (int)p2.y() }, color, 1, line_style);
  100. };
  101. auto width_for = [&](CSS::PropertyID property_id) -> float {
  102. auto width = style().property(property_id);
  103. if (!width.has_value())
  104. return 0;
  105. return width.value()->to_length().to_px();
  106. };
  107. float p1_step = 0;
  108. float p2_step = 0;
  109. switch (edge) {
  110. case Edge::Top:
  111. p1_step = width_for(CSS::PropertyID::BorderLeftWidth) / (float)int_width;
  112. p2_step = width_for(CSS::PropertyID::BorderRightWidth) / (float)int_width;
  113. for (int i = 0; i < int_width; ++i) {
  114. draw_line(p1, p2);
  115. p1.move_by(p1_step, 1);
  116. p2.move_by(-p2_step, 1);
  117. }
  118. break;
  119. case Edge::Right:
  120. p1_step = width_for(CSS::PropertyID::BorderTopWidth) / (float)int_width;
  121. p2_step = width_for(CSS::PropertyID::BorderBottomWidth) / (float)int_width;
  122. for (int i = int_width - 1; i >= 0; --i) {
  123. draw_line(p1, p2);
  124. p1.move_by(-1, p1_step);
  125. p2.move_by(-1, -p2_step);
  126. }
  127. break;
  128. case Edge::Bottom:
  129. p1_step = width_for(CSS::PropertyID::BorderLeftWidth) / (float)int_width;
  130. p2_step = width_for(CSS::PropertyID::BorderRightWidth) / (float)int_width;
  131. for (int i = int_width - 1; i >= 0; --i) {
  132. draw_line(p1, p2);
  133. p1.move_by(p1_step, -1);
  134. p2.move_by(-p2_step, -1);
  135. }
  136. break;
  137. case Edge::Left:
  138. p1_step = width_for(CSS::PropertyID::BorderTopWidth) / (float)int_width;
  139. p2_step = width_for(CSS::PropertyID::BorderBottomWidth) / (float)int_width;
  140. for (int i = 0; i < int_width; ++i) {
  141. draw_line(p1, p2);
  142. p1.move_by(1, p1_step);
  143. p2.move_by(1, -p2_step);
  144. }
  145. break;
  146. }
  147. }
  148. void LayoutBox::render(RenderingContext& context)
  149. {
  150. if (!is_visible())
  151. return;
  152. #ifdef DRAW_BOXES_AROUND_LAYOUT_NODES
  153. context.painter().draw_rect(m_rect, Color::Blue);
  154. #endif
  155. #ifdef DRAW_BOXES_AROUND_HOVERED_NODES
  156. if (!is_anonymous() && node() == document().hovered_node())
  157. context.painter().draw_rect(m_rect, Color::Red);
  158. #endif
  159. if (node() && document().inspected_node() == node())
  160. context.painter().draw_rect(enclosing_int_rect(m_rect), Color::Magenta);
  161. Gfx::FloatRect padded_rect;
  162. padded_rect.set_x(x() - box_model().padding().left.to_px());
  163. padded_rect.set_width(width() + box_model().padding().left.to_px() + box_model().padding().right.to_px());
  164. padded_rect.set_y(y() - box_model().padding().top.to_px());
  165. padded_rect.set_height(height() + box_model().padding().top.to_px() + box_model().padding().bottom.to_px());
  166. if (!is_body()) {
  167. auto bgcolor = style().property(CSS::PropertyID::BackgroundColor);
  168. if (bgcolor.has_value() && bgcolor.value()->is_color()) {
  169. context.painter().fill_rect(enclosing_int_rect(padded_rect), bgcolor.value()->to_color(document()));
  170. }
  171. auto bgimage = style().property(CSS::PropertyID::BackgroundImage);
  172. if (bgimage.has_value() && bgimage.value()->is_image()) {
  173. auto& image_value = static_cast<const ImageStyleValue&>(*bgimage.value());
  174. if (image_value.bitmap()) {
  175. context.painter().draw_tiled_bitmap(enclosing_int_rect(padded_rect), *image_value.bitmap());
  176. }
  177. }
  178. }
  179. Gfx::FloatRect bordered_rect;
  180. bordered_rect.set_x(padded_rect.x() - box_model().border().left.to_px());
  181. bordered_rect.set_width(padded_rect.width() + box_model().border().left.to_px() + box_model().border().right.to_px());
  182. bordered_rect.set_y(padded_rect.y() - box_model().border().top.to_px());
  183. bordered_rect.set_height(padded_rect.height() + box_model().border().top.to_px() + box_model().border().bottom.to_px());
  184. paint_border(context, Edge::Left, bordered_rect, CSS::PropertyID::BorderLeftStyle, CSS::PropertyID::BorderLeftColor, CSS::PropertyID::BorderLeftWidth);
  185. paint_border(context, Edge::Right, bordered_rect, CSS::PropertyID::BorderRightStyle, CSS::PropertyID::BorderRightColor, CSS::PropertyID::BorderRightWidth);
  186. paint_border(context, Edge::Top, bordered_rect, CSS::PropertyID::BorderTopStyle, CSS::PropertyID::BorderTopColor, CSS::PropertyID::BorderTopWidth);
  187. paint_border(context, Edge::Bottom, bordered_rect, CSS::PropertyID::BorderBottomStyle, CSS::PropertyID::BorderBottomColor, CSS::PropertyID::BorderBottomWidth);
  188. LayoutNodeWithStyleAndBoxModelMetrics::render(context);
  189. }
  190. HitTestResult LayoutBox::hit_test(const Gfx::Point& position) const
  191. {
  192. // FIXME: It would be nice if we could confidently skip over hit testing
  193. // parts of the layout tree, but currently we can't just check
  194. // m_rect.contains() since inline text rects can't be trusted..
  195. HitTestResult result { m_rect.contains(position.x(), position.y()) ? this : nullptr };
  196. for_each_child([&](auto& child) {
  197. auto child_result = child.hit_test(position);
  198. if (child_result.layout_node)
  199. result = child_result;
  200. });
  201. return result;
  202. }
  203. void LayoutBox::set_needs_display()
  204. {
  205. auto* frame = document().frame();
  206. ASSERT(frame);
  207. if (!is_inline()) {
  208. const_cast<Frame*>(frame)->set_needs_display(enclosing_int_rect(rect()));
  209. return;
  210. }
  211. LayoutNode::set_needs_display();
  212. }
  213. bool LayoutBox::is_body() const
  214. {
  215. return node() && node() == document().body();
  216. }
  217. }