Box.cpp 16 KB

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