Box.cpp 17 KB

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