LayoutBox.cpp 9.7 KB

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