Box.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibGfx/Painter.h>
  7. #include <LibWeb/DOM/Document.h>
  8. #include <LibWeb/HTML/HTMLBodyElement.h>
  9. #include <LibWeb/HTML/HTMLHtmlElement.h>
  10. #include <LibWeb/Layout/BlockBox.h>
  11. #include <LibWeb/Layout/Box.h>
  12. #include <LibWeb/Page/BrowsingContext.h>
  13. #include <LibWeb/Painting/BorderPainting.h>
  14. namespace Web::Layout {
  15. void Box::paint(PaintContext& context, PaintPhase phase)
  16. {
  17. if (!is_visible())
  18. return;
  19. Gfx::PainterStateSaver saver(context.painter());
  20. if (is_fixed_position())
  21. context.painter().translate(context.scroll_offset());
  22. auto padded_rect = this->padded_rect();
  23. if (phase == PaintPhase::Background) {
  24. paint_background(context);
  25. }
  26. if (phase == PaintPhase::Border) {
  27. paint_border(context);
  28. }
  29. if (phase == PaintPhase::Overlay && dom_node() && document().inspected_node() == dom_node()) {
  30. auto content_rect = absolute_rect();
  31. auto margin_box = box_model().margin_box();
  32. Gfx::FloatRect margin_rect;
  33. margin_rect.set_x(absolute_x() - margin_box.left);
  34. margin_rect.set_width(width() + margin_box.left + margin_box.right);
  35. margin_rect.set_y(absolute_y() - margin_box.top);
  36. margin_rect.set_height(height() + margin_box.top + margin_box.bottom);
  37. context.painter().draw_rect(enclosing_int_rect(margin_rect), Color::Yellow);
  38. context.painter().draw_rect(enclosing_int_rect(padded_rect), Color::Cyan);
  39. context.painter().draw_rect(enclosing_int_rect(content_rect), Color::Magenta);
  40. }
  41. if (phase == PaintPhase::FocusOutline && dom_node() && dom_node()->is_element() && verify_cast<DOM::Element>(*dom_node()).is_focused()) {
  42. context.painter().draw_rect(enclosing_int_rect(absolute_rect()), context.palette().focus_outline());
  43. }
  44. }
  45. void Box::paint_border(PaintContext& context)
  46. {
  47. auto bordered_rect = this->bordered_rect();
  48. auto border_rect = enclosing_int_rect(bordered_rect);
  49. auto border_radius_data = normalized_border_radius_data();
  50. auto top_left_radius = border_radius_data.top_left;
  51. auto top_right_radius = border_radius_data.top_right;
  52. auto bottom_right_radius = border_radius_data.bottom_right;
  53. auto bottom_left_radius = border_radius_data.bottom_left;
  54. // FIXME: Support elliptical border radii.
  55. Gfx::FloatRect top_border_rect = {
  56. border_rect.x() + top_left_radius,
  57. border_rect.y(),
  58. border_rect.width() - top_left_radius - top_right_radius,
  59. border_rect.height()
  60. };
  61. Gfx::FloatRect right_border_rect = {
  62. border_rect.x(),
  63. border_rect.y() + top_right_radius,
  64. border_rect.width(),
  65. border_rect.height() - top_right_radius - bottom_right_radius
  66. };
  67. Gfx::FloatRect bottom_border_rect = {
  68. border_rect.x() + bottom_left_radius,
  69. border_rect.y(),
  70. border_rect.width() - bottom_left_radius - bottom_right_radius,
  71. border_rect.height()
  72. };
  73. Gfx::FloatRect left_border_rect = {
  74. border_rect.x(),
  75. border_rect.y() + top_left_radius,
  76. border_rect.width(),
  77. border_rect.height() - top_left_radius - bottom_left_radius
  78. };
  79. Painting::paint_border(context, Painting::BorderEdge::Top, top_border_rect, computed_values());
  80. Painting::paint_border(context, Painting::BorderEdge::Right, right_border_rect, computed_values());
  81. Painting::paint_border(context, Painting::BorderEdge::Bottom, bottom_border_rect, computed_values());
  82. Painting::paint_border(context, Painting::BorderEdge::Left, left_border_rect, computed_values());
  83. // Draws a quarter cirle clockwise
  84. auto draw_quarter_circle = [&](Gfx::IntPoint& from, Gfx::IntPoint& to, Gfx::Color color, int thickness) {
  85. Gfx::IntPoint center = { 0, 0 };
  86. Gfx::IntPoint offset = { 0, 0 };
  87. Gfx::IntPoint circle_position = { 0, 0 };
  88. auto radius = abs(from.x() - to.x());
  89. if (from.x() < to.x() && from.y() > to.y()) {
  90. // top-left
  91. center.set_x(radius);
  92. center.set_y(radius);
  93. offset.set_y(1);
  94. } else if (from.x() < to.x() && from.y() < to.y()) {
  95. // top-right
  96. circle_position.set_x(from.x());
  97. center.set_y(radius);
  98. offset.set_x(-1);
  99. offset.set_y(1);
  100. } else if (from.x() > to.x() && from.y() < to.y()) {
  101. // bottom-right
  102. circle_position.set_x(to.x());
  103. circle_position.set_y(from.y());
  104. offset.set_x(-1);
  105. } else if (from.x() > to.x() && from.y() > to.y()) {
  106. // bottom-left
  107. circle_position.set_y(to.y());
  108. center.set_x(radius);
  109. } else {
  110. // How did you get here?
  111. VERIFY_NOT_REACHED();
  112. }
  113. Gfx::IntRect circle_rect = {
  114. border_rect.x() + circle_position.x(),
  115. border_rect.y() + circle_position.y(),
  116. radius,
  117. radius
  118. };
  119. context.painter().draw_circle_arc_intersecting(
  120. circle_rect,
  121. center + offset,
  122. radius,
  123. color,
  124. thickness);
  125. };
  126. // FIXME: Which color to use?
  127. if (top_left_radius) {
  128. Gfx::IntPoint arc_start = { 0, (int)top_left_radius };
  129. Gfx::IntPoint arc_end = { (int)top_left_radius, 0 };
  130. draw_quarter_circle(arc_start, arc_end, computed_values().border_top().color, computed_values().border_top().width);
  131. }
  132. if (top_right_radius) {
  133. Gfx::IntPoint arc_start = { (int)top_left_radius + (int)top_border_rect.width(), 0 };
  134. Gfx::IntPoint arc_end = { (int)bordered_rect.width(), (int)top_right_radius };
  135. draw_quarter_circle(arc_start, arc_end, computed_values().border_top().color, computed_values().border_top().width);
  136. }
  137. if (bottom_right_radius) {
  138. Gfx::IntPoint arc_start = { (int)bordered_rect.width(), (int)top_right_radius + (int)right_border_rect.height() };
  139. Gfx::IntPoint arc_end = { (int)bottom_border_rect.width() + (int)bottom_left_radius, (int)bordered_rect.height() };
  140. draw_quarter_circle(arc_start, arc_end, computed_values().border_bottom().color, computed_values().border_bottom().width);
  141. }
  142. if (bottom_left_radius) {
  143. Gfx::IntPoint arc_start = { (int)bottom_left_radius, (int)bordered_rect.height() };
  144. Gfx::IntPoint arc_end = { 0, (int)bordered_rect.height() - (int)bottom_left_radius };
  145. draw_quarter_circle(arc_start, arc_end, computed_values().border_bottom().color, computed_values().border_bottom().width);
  146. }
  147. }
  148. void Box::paint_background(PaintContext& context)
  149. {
  150. auto padded_rect = this->padded_rect();
  151. // If the body's background properties were propagated to the root element, do no re-paint the body's background.
  152. if (is_body() && document().html_element()->should_use_body_background_properties())
  153. return;
  154. Gfx::IntRect background_rect;
  155. Color background_color = computed_values().background_color();
  156. const Gfx::Bitmap* background_image = this->background_image() ? this->background_image()->bitmap() : nullptr;
  157. CSS::Repeat background_repeat_x = computed_values().background_repeat_x();
  158. CSS::Repeat background_repeat_y = computed_values().background_repeat_y();
  159. if (is_root_element()) {
  160. // CSS 2.1 Appendix E.2: If the element is a root element, paint the background over the entire canvas.
  161. background_rect = context.viewport_rect();
  162. // Section 2.11.2: If the computed value of background-image on the root element is none and its background-color is transparent,
  163. // user agents must instead propagate the computed values of the background properties from that element’s first HTML BODY child element.
  164. if (document().html_element()->should_use_body_background_properties()) {
  165. background_color = document().background_color(context.palette());
  166. background_image = document().background_image();
  167. background_repeat_x = document().background_repeat_x();
  168. background_repeat_y = document().background_repeat_y();
  169. }
  170. } else {
  171. background_rect = enclosing_int_rect(padded_rect);
  172. }
  173. // HACK: If the Box has a border, use the bordered_rect to paint the background.
  174. // This way if we have a border-radius there will be no gap between the filling and actual border.
  175. if (computed_values().border_top().width || computed_values().border_right().width || computed_values().border_bottom().width || computed_values().border_left().width)
  176. background_rect = enclosing_int_rect(bordered_rect());
  177. // FIXME: some values should be relative to the height() if specified, but which? For now, all relative values are relative to the width.
  178. auto border_radius_data = normalized_border_radius_data();
  179. auto top_left_radius = border_radius_data.top_left;
  180. auto top_right_radius = border_radius_data.top_right;
  181. auto bottom_right_radius = border_radius_data.bottom_right;
  182. auto bottom_left_radius = border_radius_data.bottom_left;
  183. context.painter().fill_rect_with_rounded_corners(background_rect, move(background_color), top_left_radius, top_right_radius, bottom_right_radius, bottom_left_radius);
  184. if (background_image)
  185. paint_background_image(context, *background_image, background_repeat_x, background_repeat_y, move(background_rect));
  186. }
  187. void Box::paint_background_image(
  188. PaintContext& context,
  189. const Gfx::Bitmap& background_image,
  190. CSS::Repeat background_repeat_x,
  191. CSS::Repeat background_repeat_y,
  192. Gfx::IntRect background_rect)
  193. {
  194. switch (background_repeat_x) {
  195. case CSS::Repeat::Round:
  196. case CSS::Repeat::Space:
  197. // FIXME: Support 'round' and 'space'. Fall through to 'repeat' since that most closely resembles these.
  198. case CSS::Repeat::Repeat:
  199. // The background rect is already sized to align with 'repeat'.
  200. break;
  201. case CSS::Repeat::NoRepeat:
  202. background_rect.set_width(background_image.width());
  203. break;
  204. }
  205. switch (background_repeat_y) {
  206. case CSS::Repeat::Round:
  207. case CSS::Repeat::Space:
  208. // FIXME: Support 'round' and 'space'. Fall through to 'repeat' since that most closely resembles these.
  209. case CSS::Repeat::Repeat:
  210. // The background rect is already sized to align with 'repeat'.
  211. break;
  212. case CSS::Repeat::NoRepeat:
  213. background_rect.set_height(background_image.height());
  214. break;
  215. }
  216. context.painter().blit_tiled(background_rect, background_image, background_image.rect());
  217. }
  218. Box::BorderRadiusData Box::normalized_border_radius_data()
  219. {
  220. // FIXME: some values should be relative to the height() if specified, but which? For now, all relative values are relative to the width.
  221. auto bottom_left_radius = computed_values().border_bottom_left_radius().resolved_or_zero(*this, width()).to_px(*this);
  222. auto bottom_right_radius = computed_values().border_bottom_right_radius().resolved_or_zero(*this, width()).to_px(*this);
  223. auto top_left_radius = computed_values().border_top_left_radius().resolved_or_zero(*this, width()).to_px(*this);
  224. auto top_right_radius = computed_values().border_top_right_radius().resolved_or_zero(*this, width()).to_px(*this);
  225. // Scale overlapping curves according to https://www.w3.org/TR/css-backgrounds-3/#corner-overlap
  226. auto f = 1.0f;
  227. f = min(f, bordered_rect().width() / (float)(top_left_radius + top_right_radius));
  228. f = min(f, bordered_rect().height() / (float)(top_right_radius + bottom_right_radius));
  229. f = min(f, bordered_rect().width() / (float)(bottom_left_radius + bottom_right_radius));
  230. f = min(f, bordered_rect().height() / (float)(top_left_radius + bottom_left_radius));
  231. top_left_radius = (int)(top_left_radius * f);
  232. top_right_radius = (int)(top_right_radius * f);
  233. bottom_right_radius = (int)(bottom_right_radius * f);
  234. bottom_left_radius = (int)(bottom_left_radius * f);
  235. return { (int)top_left_radius, (int)top_right_radius, (int)bottom_right_radius, (int)bottom_left_radius };
  236. }
  237. HitTestResult Box::hit_test(const Gfx::IntPoint& position, HitTestType type) const
  238. {
  239. // FIXME: It would be nice if we could confidently skip over hit testing
  240. // parts of the layout tree, but currently we can't just check
  241. // m_rect.contains() since inline text rects can't be trusted..
  242. HitTestResult result { absolute_rect().contains(position.x(), position.y()) ? this : nullptr };
  243. for_each_child_in_paint_order([&](auto& child) {
  244. auto child_result = child.hit_test(position, type);
  245. if (child_result.layout_node)
  246. result = child_result;
  247. });
  248. return result;
  249. }
  250. void Box::set_needs_display()
  251. {
  252. if (!is_inline()) {
  253. browsing_context().set_needs_display(enclosing_int_rect(absolute_rect()));
  254. return;
  255. }
  256. Node::set_needs_display();
  257. }
  258. bool Box::is_body() const
  259. {
  260. return dom_node() && dom_node() == document().body();
  261. }
  262. void Box::set_offset(const Gfx::FloatPoint& offset)
  263. {
  264. if (m_offset == offset)
  265. return;
  266. m_offset = offset;
  267. did_set_rect();
  268. }
  269. void Box::set_size(const Gfx::FloatSize& size)
  270. {
  271. if (m_size == size)
  272. return;
  273. m_size = size;
  274. did_set_rect();
  275. }
  276. Gfx::FloatPoint Box::effective_offset() const
  277. {
  278. if (m_containing_line_box_fragment)
  279. return m_containing_line_box_fragment->offset();
  280. return m_offset;
  281. }
  282. const Gfx::FloatRect Box::absolute_rect() const
  283. {
  284. Gfx::FloatRect rect { effective_offset(), size() };
  285. for (auto* block = containing_block(); block; block = block->containing_block()) {
  286. rect.translate_by(block->effective_offset());
  287. }
  288. return rect;
  289. }
  290. void Box::set_containing_line_box_fragment(LineBoxFragment& fragment)
  291. {
  292. m_containing_line_box_fragment = fragment.make_weak_ptr();
  293. }
  294. StackingContext* Box::enclosing_stacking_context()
  295. {
  296. for (auto* ancestor = parent(); ancestor; ancestor = ancestor->parent()) {
  297. if (!is<Box>(ancestor))
  298. continue;
  299. auto& ancestor_box = verify_cast<Box>(*ancestor);
  300. if (!ancestor_box.establishes_stacking_context())
  301. continue;
  302. VERIFY(ancestor_box.stacking_context());
  303. return ancestor_box.stacking_context();
  304. }
  305. // We should always reach the Layout::InitialContainingBlockBox stacking context.
  306. VERIFY_NOT_REACHED();
  307. }
  308. LineBox& Box::ensure_last_line_box()
  309. {
  310. if (m_line_boxes.is_empty())
  311. return add_line_box();
  312. return m_line_boxes.last();
  313. }
  314. LineBox& Box::add_line_box()
  315. {
  316. m_line_boxes.append(LineBox());
  317. return m_line_boxes.last();
  318. }
  319. float Box::width_of_logical_containing_block() const
  320. {
  321. auto* containing_block = this->containing_block();
  322. VERIFY(containing_block);
  323. return containing_block->width();
  324. }
  325. }