Box.cpp 15 KB

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