PaintableBox.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. /*
  2. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibUnicode/CharacterTypes.h>
  7. #include <LibWeb/DOM/Document.h>
  8. #include <LibWeb/HTML/HTMLHtmlElement.h>
  9. #include <LibWeb/Layout/BlockContainer.h>
  10. #include <LibWeb/Layout/InitialContainingBlock.h>
  11. #include <LibWeb/Painting/BackgroundPainting.h>
  12. #include <LibWeb/Painting/PaintableBox.h>
  13. #include <LibWeb/Painting/ShadowPainting.h>
  14. #include <LibWeb/Painting/StackingContext.h>
  15. namespace Web::Painting {
  16. NonnullRefPtr<PaintableBox> PaintableBox::create(Layout::Box const& layout_box)
  17. {
  18. return adopt_ref(*new PaintableBox(layout_box));
  19. }
  20. PaintableBox::PaintableBox(Layout::Box const& layout_box)
  21. : Paintable(layout_box)
  22. {
  23. }
  24. PaintableBox::~PaintableBox()
  25. {
  26. }
  27. void PaintableBox::invalidate_stacking_context()
  28. {
  29. m_stacking_context = nullptr;
  30. }
  31. PaintableWithLines::PaintableWithLines(Layout::BlockContainer const& layout_box)
  32. : PaintableBox(layout_box)
  33. {
  34. }
  35. PaintableWithLines::~PaintableWithLines()
  36. {
  37. }
  38. void PaintableBox::set_offset(Gfx::FloatPoint const& offset)
  39. {
  40. m_offset = offset;
  41. // FIXME: This const_cast is gross.
  42. const_cast<Layout::Box&>(layout_box()).did_set_rect();
  43. }
  44. void PaintableBox::set_content_size(Gfx::FloatSize const& size)
  45. {
  46. m_content_size = size;
  47. // FIXME: This const_cast is gross.
  48. const_cast<Layout::Box&>(layout_box()).did_set_rect();
  49. }
  50. Gfx::FloatPoint PaintableBox::effective_offset() const
  51. {
  52. Gfx::FloatPoint offset;
  53. if (m_containing_line_box_fragment.has_value()) {
  54. auto const& fragment = containing_block()->paint_box()->line_boxes()[m_containing_line_box_fragment->line_box_index].fragments()[m_containing_line_box_fragment->fragment_index];
  55. offset = fragment.offset();
  56. } else {
  57. offset = m_offset;
  58. }
  59. if (layout_box().computed_values().position() == CSS::Position::Relative) {
  60. auto const& inset = layout_box().box_model().inset;
  61. offset.translate_by(inset.left, inset.top);
  62. }
  63. return offset;
  64. }
  65. Gfx::FloatRect PaintableBox::compute_absolute_rect() const
  66. {
  67. Gfx::FloatRect rect { effective_offset(), content_size() };
  68. for (auto const* block = containing_block(); block && block->paintable(); block = block->paintable()->containing_block())
  69. rect.translate_by(block->paint_box()->effective_offset());
  70. return rect;
  71. }
  72. Gfx::FloatRect PaintableBox::absolute_rect() const
  73. {
  74. if (!m_absolute_rect.has_value())
  75. m_absolute_rect = compute_absolute_rect();
  76. return *m_absolute_rect;
  77. }
  78. void PaintableBox::set_containing_line_box_fragment(Optional<Layout::LineBoxFragmentCoordinate> fragment_coordinate)
  79. {
  80. m_containing_line_box_fragment = fragment_coordinate;
  81. }
  82. Painting::StackingContext* PaintableBox::enclosing_stacking_context()
  83. {
  84. for (auto* ancestor = layout_box().parent(); ancestor; ancestor = ancestor->parent()) {
  85. if (!is<Layout::Box>(ancestor))
  86. continue;
  87. auto& ancestor_box = static_cast<Layout::Box&>(const_cast<Layout::NodeWithStyle&>(*ancestor));
  88. if (auto* ancestor_paint_box = ancestor_box.paint_box(); ancestor_paint_box && ancestor_paint_box->stacking_context())
  89. return const_cast<StackingContext*>(ancestor_paint_box->stacking_context());
  90. }
  91. // We should always reach the Layout::InitialContainingBlock stacking context.
  92. VERIFY_NOT_REACHED();
  93. }
  94. void PaintableBox::paint(PaintContext& context, PaintPhase phase) const
  95. {
  96. if (!is_visible())
  97. return;
  98. if (phase == PaintPhase::Background) {
  99. paint_background(context);
  100. paint_box_shadow(context);
  101. }
  102. if (phase == PaintPhase::Border) {
  103. paint_border(context);
  104. }
  105. if (phase == PaintPhase::Overlay && layout_box().dom_node() && layout_box().document().inspected_node() == layout_box().dom_node()) {
  106. auto content_rect = absolute_rect();
  107. auto margin_box = box_model().margin_box();
  108. Gfx::FloatRect margin_rect;
  109. margin_rect.set_x(absolute_x() - margin_box.left);
  110. margin_rect.set_width(content_width() + margin_box.left + margin_box.right);
  111. margin_rect.set_y(absolute_y() - margin_box.top);
  112. margin_rect.set_height(content_height() + margin_box.top + margin_box.bottom);
  113. auto border_rect = absolute_border_box_rect();
  114. auto padding_rect = absolute_padding_box_rect();
  115. auto paint_inspector_rect = [&](Gfx::FloatRect const& rect, Color color) {
  116. context.painter().fill_rect(enclosing_int_rect(rect), Color(color).with_alpha(100));
  117. context.painter().draw_rect(enclosing_int_rect(rect), Color(color));
  118. };
  119. paint_inspector_rect(margin_rect, Color::Yellow);
  120. paint_inspector_rect(padding_rect, Color::Cyan);
  121. paint_inspector_rect(border_rect, Color::Green);
  122. paint_inspector_rect(content_rect, Color::Magenta);
  123. auto& font = Gfx::FontDatabase::default_font();
  124. StringBuilder builder;
  125. if (layout_box().dom_node())
  126. builder.append(layout_box().dom_node()->debug_description());
  127. else
  128. builder.append(layout_box().debug_description());
  129. builder.appendff(" {}x{} @ {},{}", border_rect.width(), border_rect.height(), border_rect.x(), border_rect.y());
  130. auto size_text = builder.to_string();
  131. auto size_text_rect = border_rect;
  132. size_text_rect.set_y(border_rect.y() + border_rect.height());
  133. size_text_rect.set_top(size_text_rect.top());
  134. size_text_rect.set_width((float)font.width(size_text) + 4);
  135. size_text_rect.set_height(font.pixel_size() + 4);
  136. context.painter().fill_rect(enclosing_int_rect(size_text_rect), context.palette().color(Gfx::ColorRole::Tooltip));
  137. context.painter().draw_rect(enclosing_int_rect(size_text_rect), context.palette().threed_shadow1());
  138. context.painter().draw_text(enclosing_int_rect(size_text_rect), size_text, font, Gfx::TextAlignment::Center, context.palette().color(Gfx::ColorRole::TooltipText));
  139. }
  140. if (phase == PaintPhase::FocusOutline && layout_box().dom_node() && layout_box().dom_node()->is_element() && verify_cast<DOM::Element>(*layout_box().dom_node()).is_focused()) {
  141. // FIXME: Implement this as `outline` using :focus-visible in the default UA stylesheet to make it possible to override/disable.
  142. auto focus_outline_rect = enclosing_int_rect(absolute_border_box_rect()).inflated(4, 4);
  143. context.painter().draw_focus_rect(focus_outline_rect, context.palette().focus_outline());
  144. }
  145. }
  146. void PaintableBox::paint_border(PaintContext& context) const
  147. {
  148. auto borders_data = BordersData {
  149. .top = computed_values().border_top(),
  150. .right = computed_values().border_right(),
  151. .bottom = computed_values().border_bottom(),
  152. .left = computed_values().border_left(),
  153. };
  154. paint_all_borders(context, absolute_border_box_rect(), normalized_border_radii_data(), borders_data);
  155. }
  156. void PaintableBox::paint_background(PaintContext& context) const
  157. {
  158. // If the body's background properties were propagated to the root element, do no re-paint the body's background.
  159. if (layout_box().is_body() && document().html_element()->should_use_body_background_properties())
  160. return;
  161. Gfx::FloatRect background_rect;
  162. Color background_color = computed_values().background_color();
  163. auto* background_layers = &computed_values().background_layers();
  164. if (layout_box().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().to_type<float>();
  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_layers = document().background_layers();
  171. background_color = document().background_color(context.palette());
  172. }
  173. } else {
  174. background_rect = absolute_padding_box_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 = absolute_border_box_rect();
  180. Painting::paint_background(context, layout_box(), background_rect, background_color, background_layers, normalized_border_radii_data());
  181. }
  182. void PaintableBox::paint_box_shadow(PaintContext& context) const
  183. {
  184. auto box_shadow_data = computed_values().box_shadow();
  185. if (box_shadow_data.is_empty())
  186. return;
  187. Vector<ShadowData> resolved_box_shadow_data;
  188. resolved_box_shadow_data.ensure_capacity(box_shadow_data.size());
  189. for (auto const& layer : box_shadow_data) {
  190. resolved_box_shadow_data.empend(
  191. layer.color,
  192. static_cast<int>(layer.offset_x.to_px(layout_box())),
  193. static_cast<int>(layer.offset_y.to_px(layout_box())),
  194. static_cast<int>(layer.blur_radius.to_px(layout_box())),
  195. static_cast<int>(layer.spread_distance.to_px(layout_box())),
  196. layer.placement == CSS::ShadowPlacement::Outer ? ShadowPlacement::Outer : ShadowPlacement::Inner);
  197. }
  198. Painting::paint_box_shadow(context, enclosing_int_rect(absolute_border_box_rect()), resolved_box_shadow_data);
  199. }
  200. BorderRadiiData PaintableBox::normalized_border_radii_data() const
  201. {
  202. return Painting::normalized_border_radii_data(layout_box(), absolute_border_box_rect(),
  203. computed_values().border_top_left_radius(),
  204. computed_values().border_top_right_radius(),
  205. computed_values().border_bottom_right_radius(),
  206. computed_values().border_bottom_left_radius());
  207. }
  208. void PaintableBox::before_children_paint(PaintContext& context, PaintPhase) const
  209. {
  210. // FIXME: Support more overflow variations.
  211. if (computed_values().overflow_x() == CSS::Overflow::Hidden && computed_values().overflow_y() == CSS::Overflow::Hidden) {
  212. context.painter().save();
  213. context.painter().add_clip_rect(enclosing_int_rect(absolute_border_box_rect()));
  214. }
  215. }
  216. void PaintableBox::after_children_paint(PaintContext& context, PaintPhase) const
  217. {
  218. // FIXME: Support more overflow variations.
  219. if (computed_values().overflow_x() == CSS::Overflow::Hidden && computed_values().overflow_y() == CSS::Overflow::Hidden)
  220. context.painter().restore();
  221. }
  222. static void paint_cursor_if_needed(PaintContext& context, Layout::TextNode const& text_node, Layout::LineBoxFragment const& fragment)
  223. {
  224. auto const& browsing_context = text_node.browsing_context();
  225. if (!browsing_context.is_focused_context())
  226. return;
  227. if (!browsing_context.cursor_blink_state())
  228. return;
  229. if (browsing_context.cursor_position().node() != &text_node.dom_node())
  230. return;
  231. // NOTE: This checks if the cursor is before the start or after the end of the fragment. If it is at the end, after all text, it should still be painted.
  232. if (browsing_context.cursor_position().offset() < (unsigned)fragment.start() || browsing_context.cursor_position().offset() > (unsigned)(fragment.start() + fragment.length()))
  233. return;
  234. if (!fragment.layout_node().dom_node() || !fragment.layout_node().dom_node()->is_editable())
  235. return;
  236. auto fragment_rect = fragment.absolute_rect();
  237. float cursor_x = fragment_rect.x() + text_node.font().width(fragment.text().substring_view(0, text_node.browsing_context().cursor_position().offset() - fragment.start()));
  238. float cursor_top = fragment_rect.top();
  239. float cursor_height = fragment_rect.height();
  240. Gfx::IntRect cursor_rect(cursor_x, cursor_top, 1, cursor_height);
  241. context.painter().draw_rect(cursor_rect, text_node.computed_values().color());
  242. }
  243. static void paint_text_decoration(Gfx::Painter& painter, Layout::Node const& text_node, Layout::LineBoxFragment const& fragment)
  244. {
  245. auto& font = fragment.layout_node().font();
  246. auto fragment_box = enclosing_int_rect(fragment.absolute_rect());
  247. auto glyph_height = font.pixel_size();
  248. auto baseline = fragment_box.height() / 2 - (glyph_height + 4) / 2 + glyph_height;
  249. auto line_color = text_node.computed_values().text_decoration_color();
  250. int line_thickness = [&] {
  251. CSS::Length computed_thickness = text_node.computed_values().text_decoration_thickness().resolved(text_node, CSS::Length(1, CSS::Length::Type::Em));
  252. if (computed_thickness.is_auto())
  253. return max(glyph_height * 0.1f, 1.f);
  254. return computed_thickness.to_px(text_node);
  255. }();
  256. auto text_decoration_lines = text_node.computed_values().text_decoration_line();
  257. for (auto line : text_decoration_lines) {
  258. Gfx::IntPoint line_start_point {};
  259. Gfx::IntPoint line_end_point {};
  260. switch (line) {
  261. case CSS::TextDecorationLine::None:
  262. return;
  263. case CSS::TextDecorationLine::Underline:
  264. line_start_point = fragment_box.top_left().translated(0, baseline + 2);
  265. line_end_point = fragment_box.top_right().translated(0, baseline + 2);
  266. break;
  267. case CSS::TextDecorationLine::Overline:
  268. line_start_point = fragment_box.top_left().translated(0, baseline - glyph_height);
  269. line_end_point = fragment_box.top_right().translated(0, baseline - glyph_height);
  270. break;
  271. case CSS::TextDecorationLine::LineThrough: {
  272. auto x_height = font.x_height();
  273. line_start_point = fragment_box.top_left().translated(0, baseline - x_height / 2);
  274. line_end_point = fragment_box.top_right().translated(0, baseline - x_height / 2);
  275. break;
  276. }
  277. case CSS::TextDecorationLine::Blink:
  278. // Conforming user agents may simply not blink the text
  279. return;
  280. }
  281. switch (text_node.computed_values().text_decoration_style()) {
  282. case CSS::TextDecorationStyle::Solid:
  283. painter.draw_line(line_start_point, line_end_point, line_color, line_thickness, Gfx::Painter::LineStyle::Solid);
  284. break;
  285. case CSS::TextDecorationStyle::Double:
  286. switch (line) {
  287. case CSS::TextDecorationLine::Underline:
  288. break;
  289. case CSS::TextDecorationLine::Overline:
  290. line_start_point.translate_by(0, -line_thickness - 1);
  291. line_end_point.translate_by(0, -line_thickness - 1);
  292. break;
  293. case CSS::TextDecorationLine::LineThrough:
  294. line_start_point.translate_by(0, -line_thickness / 2);
  295. line_end_point.translate_by(0, -line_thickness / 2);
  296. break;
  297. default:
  298. VERIFY_NOT_REACHED();
  299. }
  300. painter.draw_line(line_start_point, line_end_point, line_color, line_thickness);
  301. painter.draw_line(line_start_point.translated(0, line_thickness + 1), line_end_point.translated(0, line_thickness + 1), line_color, line_thickness);
  302. break;
  303. case CSS::TextDecorationStyle::Dashed:
  304. painter.draw_line(line_start_point, line_end_point, line_color, line_thickness, Gfx::Painter::LineStyle::Dashed);
  305. break;
  306. case CSS::TextDecorationStyle::Dotted:
  307. painter.draw_line(line_start_point, line_end_point, line_color, line_thickness, Gfx::Painter::LineStyle::Dotted);
  308. break;
  309. case CSS::TextDecorationStyle::Wavy:
  310. painter.draw_triangle_wave(line_start_point, line_end_point, line_color, line_thickness + 1, line_thickness);
  311. break;
  312. }
  313. }
  314. }
  315. static void paint_text_fragment(PaintContext& context, Layout::TextNode const& text_node, Layout::LineBoxFragment const& fragment, Painting::PaintPhase phase)
  316. {
  317. auto& painter = context.painter();
  318. if (phase == Painting::PaintPhase::Foreground) {
  319. auto fragment_absolute_rect = fragment.absolute_rect();
  320. if (text_node.document().inspected_node() == &text_node.dom_node())
  321. context.painter().draw_rect(enclosing_int_rect(fragment_absolute_rect), Color::Magenta);
  322. // FIXME: text-transform should be done already in layout, since uppercase glyphs may be wider than lowercase, etc.
  323. auto text = text_node.text_for_rendering();
  324. auto text_transform = text_node.computed_values().text_transform();
  325. if (text_transform == CSS::TextTransform::Uppercase)
  326. text = Unicode::to_unicode_uppercase_full(text_node.text_for_rendering());
  327. if (text_transform == CSS::TextTransform::Lowercase)
  328. text = Unicode::to_unicode_lowercase_full(text_node.text_for_rendering());
  329. Gfx::FloatPoint baseline_start { fragment_absolute_rect.x(), fragment_absolute_rect.y() + fragment.baseline() };
  330. Utf8View view { text.substring_view(fragment.start(), fragment.length()) };
  331. painter.draw_text_run(baseline_start, view, fragment.layout_node().font(), text_node.computed_values().color());
  332. auto selection_rect = fragment.selection_rect(text_node.font());
  333. if (!selection_rect.is_empty()) {
  334. painter.fill_rect(enclosing_int_rect(selection_rect), context.palette().selection());
  335. Gfx::PainterStateSaver saver(painter);
  336. painter.add_clip_rect(enclosing_int_rect(selection_rect));
  337. painter.draw_text_run(baseline_start, view, fragment.layout_node().font(), context.palette().selection_text());
  338. }
  339. paint_text_decoration(painter, text_node, fragment);
  340. paint_cursor_if_needed(context, text_node, fragment);
  341. }
  342. }
  343. void PaintableWithLines::paint(PaintContext& context, PaintPhase phase) const
  344. {
  345. if (!is_visible())
  346. return;
  347. PaintableBox::paint(context, phase);
  348. if (m_line_boxes.is_empty())
  349. return;
  350. bool should_clip_overflow = computed_values().overflow_x() != CSS::Overflow::Visible && computed_values().overflow_y() != CSS::Overflow::Visible;
  351. if (should_clip_overflow) {
  352. context.painter().save();
  353. // FIXME: Handle overflow-x and overflow-y being different values.
  354. context.painter().add_clip_rect(enclosing_int_rect(absolute_padding_box_rect()));
  355. auto scroll_offset = static_cast<Layout::BlockContainer const&>(layout_box()).scroll_offset();
  356. context.painter().translate(-scroll_offset.to_type<int>());
  357. }
  358. // Text shadows
  359. // This is yet another loop, but done here because all shadows should appear under all text.
  360. // So, we paint the shadows before painting any text.
  361. // FIXME: Find a smarter way to do this?
  362. if (phase == PaintPhase::Foreground) {
  363. for (auto& line_box : m_line_boxes) {
  364. for (auto& fragment : line_box.fragments()) {
  365. if (is<Layout::TextNode>(fragment.layout_node())) {
  366. auto& text_shadow = fragment.layout_node().computed_values().text_shadow();
  367. if (!text_shadow.is_empty()) {
  368. Vector<ShadowData> resolved_shadow_data;
  369. resolved_shadow_data.ensure_capacity(text_shadow.size());
  370. for (auto const& layer : text_shadow) {
  371. resolved_shadow_data.empend(
  372. layer.color,
  373. static_cast<int>(layer.offset_x.to_px(layout_box())),
  374. static_cast<int>(layer.offset_y.to_px(layout_box())),
  375. static_cast<int>(layer.blur_radius.to_px(layout_box())),
  376. static_cast<int>(layer.spread_distance.to_px(layout_box())),
  377. ShadowPlacement::Outer);
  378. }
  379. context.painter().set_font(fragment.layout_node().font());
  380. Painting::paint_text_shadow(context, fragment, resolved_shadow_data);
  381. }
  382. }
  383. }
  384. }
  385. }
  386. for (auto& line_box : m_line_boxes) {
  387. for (auto& fragment : line_box.fragments()) {
  388. if (context.should_show_line_box_borders()) {
  389. auto fragment_absolute_rect = fragment.absolute_rect();
  390. context.painter().draw_rect(enclosing_int_rect(fragment_absolute_rect), Color::Green);
  391. context.painter().draw_line(
  392. fragment_absolute_rect.top_left().translated(0, fragment.baseline()).to_rounded<int>(),
  393. fragment_absolute_rect.top_right().translated(0, fragment.baseline()).to_rounded<int>(), Color::Red);
  394. }
  395. if (is<Layout::TextNode>(fragment.layout_node()))
  396. paint_text_fragment(context, static_cast<Layout::TextNode const&>(fragment.layout_node()), fragment, phase);
  397. }
  398. }
  399. if (should_clip_overflow) {
  400. context.painter().restore();
  401. }
  402. // FIXME: Merge this loop with the above somehow..
  403. if (phase == PaintPhase::FocusOutline) {
  404. for (auto& line_box : m_line_boxes) {
  405. for (auto& fragment : line_box.fragments()) {
  406. auto* node = fragment.layout_node().dom_node();
  407. if (!node)
  408. continue;
  409. auto* parent = node->parent_element();
  410. if (!parent)
  411. continue;
  412. if (parent->is_focused()) {
  413. // FIXME: Implement this as `outline` using :focus-visible in the default UA stylesheet to make it possible to override/disable.
  414. auto focus_outline_rect = enclosing_int_rect(fragment.absolute_rect()).inflated(4, 4);
  415. context.painter().draw_focus_rect(focus_outline_rect, context.palette().focus_outline());
  416. }
  417. }
  418. }
  419. }
  420. }
  421. bool PaintableWithLines::handle_mousewheel(Badge<EventHandler>, Gfx::IntPoint const&, unsigned, unsigned, int wheel_delta_x, int wheel_delta_y)
  422. {
  423. if (!layout_box().is_scrollable())
  424. return false;
  425. auto new_offset = layout_box().scroll_offset();
  426. new_offset.translate_by(wheel_delta_x, wheel_delta_y);
  427. const_cast<Layout::BlockContainer&>(layout_box()).set_scroll_offset(new_offset);
  428. return true;
  429. }
  430. Layout::BlockContainer const& PaintableWithLines::layout_box() const
  431. {
  432. return static_cast<Layout::BlockContainer const&>(PaintableBox::layout_box());
  433. }
  434. Layout::BlockContainer& PaintableWithLines::layout_box()
  435. {
  436. return static_cast<Layout::BlockContainer&>(PaintableBox::layout_box());
  437. }
  438. void PaintableBox::set_stacking_context(NonnullOwnPtr<StackingContext> stacking_context)
  439. {
  440. m_stacking_context = move(stacking_context);
  441. }
  442. Optional<HitTestResult> PaintableBox::hit_test(Gfx::FloatPoint const& position, HitTestType type) const
  443. {
  444. if (!is_visible())
  445. return {};
  446. if (layout_box().is_initial_containing_block_box()) {
  447. const_cast<Layout::InitialContainingBlock&>(static_cast<Layout::InitialContainingBlock const&>(layout_box())).build_stacking_context_tree_if_needed();
  448. return stacking_context()->hit_test(position, type);
  449. }
  450. if (absolute_border_box_rect().contains(position.x(), position.y()))
  451. return HitTestResult { *this };
  452. return {};
  453. }
  454. Optional<HitTestResult> PaintableWithLines::hit_test(Gfx::FloatPoint const& position, HitTestType type) const
  455. {
  456. if (!layout_box().children_are_inline())
  457. return PaintableBox::hit_test(position, type);
  458. Optional<HitTestResult> last_good_candidate;
  459. for (auto& line_box : m_line_boxes) {
  460. for (auto& fragment : line_box.fragments()) {
  461. if (is<Layout::Box>(fragment.layout_node()) && static_cast<Layout::Box const&>(fragment.layout_node()).paint_box()->stacking_context())
  462. continue;
  463. auto fragment_absolute_rect = fragment.absolute_rect();
  464. if (fragment_absolute_rect.contains(position)) {
  465. if (is<Layout::BlockContainer>(fragment.layout_node()) && fragment.layout_node().paintable())
  466. return fragment.layout_node().paintable()->hit_test(position, type);
  467. return HitTestResult { *fragment.layout_node().paintable(), fragment.text_index_at(position.x()) };
  468. }
  469. if (fragment_absolute_rect.top() <= position.y())
  470. last_good_candidate = HitTestResult { *fragment.layout_node().paintable(), fragment.text_index_at(position.x()) };
  471. }
  472. }
  473. if (type == HitTestType::TextCursor && last_good_candidate.has_value())
  474. return last_good_candidate;
  475. if (is_visible() && absolute_border_box_rect().contains(position.x(), position.y()))
  476. return HitTestResult { *this };
  477. return {};
  478. }
  479. }