PaintableBox.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  1. /*
  2. * Copyright (c) 2022-2023, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/GenericShorthands.h>
  8. #include <LibUnicode/CharacterTypes.h>
  9. #include <LibWeb/DOM/Document.h>
  10. #include <LibWeb/HTML/HTMLHtmlElement.h>
  11. #include <LibWeb/Layout/BlockContainer.h>
  12. #include <LibWeb/Layout/Viewport.h>
  13. #include <LibWeb/Painting/BackgroundPainting.h>
  14. #include <LibWeb/Painting/FilterPainting.h>
  15. #include <LibWeb/Painting/PaintableBox.h>
  16. #include <LibWeb/Painting/StackingContext.h>
  17. #include <LibWeb/Painting/ViewportPaintable.h>
  18. #include <LibWeb/Platform/FontPlugin.h>
  19. namespace Web::Painting {
  20. JS::NonnullGCPtr<PaintableWithLines> PaintableWithLines::create(Layout::BlockContainer const& block_container)
  21. {
  22. return block_container.heap().allocate_without_realm<PaintableWithLines>(block_container);
  23. }
  24. JS::NonnullGCPtr<PaintableBox> PaintableBox::create(Layout::Box const& layout_box)
  25. {
  26. return layout_box.heap().allocate_without_realm<PaintableBox>(layout_box);
  27. }
  28. PaintableBox::PaintableBox(Layout::Box const& layout_box)
  29. : Paintable(layout_box)
  30. {
  31. }
  32. PaintableBox::~PaintableBox()
  33. {
  34. }
  35. void PaintableBox::invalidate_stacking_context()
  36. {
  37. m_stacking_context = nullptr;
  38. }
  39. bool PaintableBox::is_out_of_view(PaintContext& context) const
  40. {
  41. return context.would_be_fully_clipped_by_painter(context.enclosing_device_rect(absolute_paint_rect()));
  42. }
  43. PaintableWithLines::PaintableWithLines(Layout::BlockContainer const& layout_box)
  44. : PaintableBox(layout_box)
  45. {
  46. }
  47. PaintableWithLines::~PaintableWithLines()
  48. {
  49. }
  50. CSSPixelPoint PaintableBox::scroll_offset() const
  51. {
  52. auto const& node = layout_node();
  53. if (node.is_generated_for_before_pseudo_element())
  54. return node.pseudo_element_generator()->scroll_offset(DOM::Element::ScrollOffsetFor::PseudoBefore);
  55. if (node.is_generated_for_after_pseudo_element())
  56. return node.pseudo_element_generator()->scroll_offset(DOM::Element::ScrollOffsetFor::PseudoAfter);
  57. if (!(dom_node() && is<DOM::Element>(*dom_node())))
  58. return {};
  59. return static_cast<DOM::Element const*>(dom_node())->scroll_offset(DOM::Element::ScrollOffsetFor::Self);
  60. }
  61. void PaintableBox::set_scroll_offset(CSSPixelPoint offset)
  62. {
  63. // FIXME: If there is horizontal and vertical scroll ignore only part of the new offset
  64. if (offset.y() < 0 || scroll_offset() == offset)
  65. return;
  66. auto& node = layout_node();
  67. if (node.is_generated_for_before_pseudo_element()) {
  68. node.pseudo_element_generator()->set_scroll_offset(DOM::Element::ScrollOffsetFor::PseudoBefore, offset);
  69. } else if (node.is_generated_for_after_pseudo_element()) {
  70. node.pseudo_element_generator()->set_scroll_offset(DOM::Element::ScrollOffsetFor::PseudoAfter, offset);
  71. } else if (is<DOM::Element>(*dom_node())) {
  72. static_cast<DOM::Element*>(dom_node())->set_scroll_offset(DOM::Element::ScrollOffsetFor::Self, offset);
  73. } else {
  74. return;
  75. }
  76. node.set_needs_display();
  77. }
  78. void PaintableBox::scroll_by(int delta_x, int delta_y)
  79. {
  80. auto scrollable_overflow_rect = this->scrollable_overflow_rect();
  81. if (!scrollable_overflow_rect.has_value())
  82. return;
  83. auto max_x_offset = scrollable_overflow_rect->width() - content_size().width();
  84. auto max_y_offset = scrollable_overflow_rect->height() - content_size().height();
  85. auto current_offset = scroll_offset();
  86. auto new_offset_x = clamp(current_offset.x() + delta_x, 0, max_x_offset);
  87. auto new_offset_y = clamp(current_offset.y() + delta_y, 0, max_y_offset);
  88. set_scroll_offset({ new_offset_x, new_offset_y });
  89. }
  90. void PaintableBox::set_offset(CSSPixelPoint offset)
  91. {
  92. m_offset = offset;
  93. }
  94. void PaintableBox::set_content_size(CSSPixelSize size)
  95. {
  96. m_content_size = size;
  97. layout_box().did_set_content_size();
  98. }
  99. CSSPixelPoint PaintableBox::offset() const
  100. {
  101. return m_offset;
  102. }
  103. CSSPixelRect PaintableBox::compute_absolute_rect() const
  104. {
  105. CSSPixelRect rect { offset(), content_size() };
  106. for (auto const* block = containing_block(); block && block->paintable(); block = block->paintable()->containing_block())
  107. rect.translate_by(block->paintable_box()->offset());
  108. return rect;
  109. }
  110. CSSPixelRect PaintableBox::absolute_rect() const
  111. {
  112. if (!m_absolute_rect.has_value())
  113. m_absolute_rect = compute_absolute_rect();
  114. return *m_absolute_rect;
  115. }
  116. CSSPixelRect PaintableBox::compute_absolute_paint_rect() const
  117. {
  118. // FIXME: This likely incomplete:
  119. auto rect = absolute_border_box_rect();
  120. if (has_scrollable_overflow()) {
  121. auto scrollable_overflow_rect = this->scrollable_overflow_rect().value();
  122. if (computed_values().overflow_x() == CSS::Overflow::Visible)
  123. rect.unite_horizontally(scrollable_overflow_rect);
  124. if (computed_values().overflow_y() == CSS::Overflow::Visible)
  125. rect.unite_vertically(scrollable_overflow_rect);
  126. }
  127. auto resolved_box_shadow_data = resolve_box_shadow_data();
  128. for (auto const& shadow : resolved_box_shadow_data) {
  129. if (shadow.placement == ShadowPlacement::Inner)
  130. continue;
  131. auto inflate = shadow.spread_distance + shadow.blur_radius;
  132. auto shadow_rect = rect.inflated(inflate, inflate, inflate, inflate).translated(shadow.offset_x, shadow.offset_y);
  133. rect = rect.united(shadow_rect);
  134. }
  135. return rect;
  136. }
  137. CSSPixelRect PaintableBox::absolute_paint_rect() const
  138. {
  139. if (!m_absolute_paint_rect.has_value())
  140. m_absolute_paint_rect = compute_absolute_paint_rect();
  141. return *m_absolute_paint_rect;
  142. }
  143. StackingContext* PaintableBox::enclosing_stacking_context()
  144. {
  145. for (auto* ancestor = layout_box().parent(); ancestor; ancestor = ancestor->parent()) {
  146. if (!is<Layout::Box>(ancestor))
  147. continue;
  148. auto& ancestor_box = static_cast<Layout::Box&>(const_cast<Layout::NodeWithStyle&>(*ancestor));
  149. if (auto* ancestor_paintable_box = ancestor_box.paintable_box(); ancestor_paintable_box && ancestor_paintable_box->stacking_context())
  150. return const_cast<StackingContext*>(ancestor_paintable_box->stacking_context());
  151. }
  152. // We should always reach the Layout::Viewport stacking context.
  153. VERIFY_NOT_REACHED();
  154. }
  155. void PaintableBox::paint(PaintContext& context, PaintPhase phase) const
  156. {
  157. if (!is_visible())
  158. return;
  159. if (phase == PaintPhase::Background) {
  160. paint_backdrop_filter(context);
  161. paint_background(context);
  162. paint_box_shadow(context);
  163. }
  164. if (phase == PaintPhase::Border) {
  165. paint_border(context);
  166. }
  167. if (phase == PaintPhase::Outline) {
  168. auto outline_width = computed_values().outline_width().to_px(layout_node());
  169. auto borders_data = borders_data_for_outline(layout_node(), computed_values().outline_color(), computed_values().outline_style(), outline_width);
  170. if (borders_data.has_value()) {
  171. auto outline_offset = computed_values().outline_offset().to_px(layout_node());
  172. auto border_radius_data = normalized_border_radii_data(ShrinkRadiiForBorders::No);
  173. auto borders_rect = absolute_border_box_rect();
  174. auto outline_offset_x = outline_offset;
  175. auto outline_offset_y = outline_offset;
  176. // "Both the height and the width of the outside of the shape drawn by the outline should not
  177. // become smaller than twice the computed value of the outline-width property to make sure
  178. // that an outline can be rendered even with large negative values."
  179. // https://www.w3.org/TR/css-ui-4/#outline-offset
  180. // So, if the horizontal outline offset is > half the borders_rect's width then we set it to that.
  181. // (And the same for y)
  182. if ((borders_rect.width() / 2) + outline_offset_x < 0)
  183. outline_offset_x = -borders_rect.width() / 2;
  184. if ((borders_rect.height() / 2) + outline_offset_y < 0)
  185. outline_offset_y = -borders_rect.height() / 2;
  186. border_radius_data.inflate(outline_width + outline_offset_y, outline_width + outline_offset_x, outline_width + outline_offset_y, outline_width + outline_offset_x);
  187. borders_rect.inflate(outline_width + outline_offset_y, outline_width + outline_offset_x, outline_width + outline_offset_y, outline_width + outline_offset_x);
  188. paint_all_borders(context, context.rounded_device_rect(borders_rect), border_radius_data, borders_data.value());
  189. }
  190. }
  191. if (phase == PaintPhase::Overlay && layout_box().document().inspected_layout_node() == &layout_box()) {
  192. auto content_rect = absolute_rect();
  193. auto margin_box = box_model().margin_box();
  194. CSSPixelRect margin_rect;
  195. margin_rect.set_x(absolute_x() - margin_box.left);
  196. margin_rect.set_width(content_width() + margin_box.left + margin_box.right);
  197. margin_rect.set_y(absolute_y() - margin_box.top);
  198. margin_rect.set_height(content_height() + margin_box.top + margin_box.bottom);
  199. auto border_rect = absolute_border_box_rect();
  200. auto padding_rect = absolute_padding_box_rect();
  201. auto paint_inspector_rect = [&](CSSPixelRect const& rect, Color color) {
  202. auto device_rect = context.enclosing_device_rect(rect).to_type<int>();
  203. context.painter().fill_rect(device_rect, Color(color).with_alpha(100));
  204. context.painter().draw_rect(device_rect, Color(color));
  205. };
  206. paint_inspector_rect(margin_rect, Color::Yellow);
  207. paint_inspector_rect(padding_rect, Color::Cyan);
  208. paint_inspector_rect(border_rect, Color::Green);
  209. paint_inspector_rect(content_rect, Color::Magenta);
  210. auto& font = Platform::FontPlugin::the().default_font();
  211. StringBuilder builder;
  212. if (layout_box().dom_node())
  213. builder.append(layout_box().dom_node()->debug_description());
  214. else
  215. builder.append(layout_box().debug_description());
  216. builder.appendff(" {}x{} @ {},{}", border_rect.width(), border_rect.height(), border_rect.x(), border_rect.y());
  217. auto size_text = builder.to_deprecated_string();
  218. auto size_text_rect = border_rect;
  219. size_text_rect.set_y(border_rect.y() + border_rect.height());
  220. size_text_rect.set_top(size_text_rect.top());
  221. size_text_rect.set_width((float)font.width(size_text) + 4);
  222. size_text_rect.set_height(font.pixel_size() + 4);
  223. auto size_text_device_rect = context.enclosing_device_rect(size_text_rect).to_type<int>();
  224. context.painter().fill_rect(size_text_device_rect, context.palette().color(Gfx::ColorRole::Tooltip));
  225. context.painter().draw_rect(size_text_device_rect, context.palette().threed_shadow1());
  226. context.painter().draw_text(size_text_device_rect, size_text, font, Gfx::TextAlignment::Center, context.palette().color(Gfx::ColorRole::TooltipText));
  227. }
  228. }
  229. BordersData PaintableBox::remove_element_kind_from_borders_data(PaintableBox::BordersDataWithElementKind borders_data)
  230. {
  231. return {
  232. .top = borders_data.top.border_data,
  233. .right = borders_data.right.border_data,
  234. .bottom = borders_data.bottom.border_data,
  235. .left = borders_data.left.border_data,
  236. };
  237. }
  238. void PaintableBox::paint_border(PaintContext& context) const
  239. {
  240. auto borders_data = m_override_borders_data.has_value() ? remove_element_kind_from_borders_data(m_override_borders_data.value()) : BordersData {
  241. .top = box_model().border.top == 0 ? CSS::BorderData() : computed_values().border_top(),
  242. .right = box_model().border.right == 0 ? CSS::BorderData() : computed_values().border_right(),
  243. .bottom = box_model().border.bottom == 0 ? CSS::BorderData() : computed_values().border_bottom(),
  244. .left = box_model().border.left == 0 ? CSS::BorderData() : computed_values().border_left(),
  245. };
  246. paint_all_borders(context, context.rounded_device_rect(absolute_border_box_rect()), normalized_border_radii_data(), borders_data);
  247. }
  248. void PaintableBox::paint_backdrop_filter(PaintContext& context) const
  249. {
  250. auto& backdrop_filter = computed_values().backdrop_filter();
  251. if (!backdrop_filter.is_none())
  252. apply_backdrop_filter(context, layout_node(), absolute_border_box_rect(), normalized_border_radii_data(), backdrop_filter);
  253. }
  254. void PaintableBox::paint_background(PaintContext& context) const
  255. {
  256. // If the body's background properties were propagated to the root element, do no re-paint the body's background.
  257. if (layout_box().is_body() && document().html_element()->should_use_body_background_properties())
  258. return;
  259. CSSPixelRect background_rect;
  260. Color background_color = computed_values().background_color();
  261. auto* background_layers = &computed_values().background_layers();
  262. if (layout_box().is_root_element()) {
  263. // CSS 2.1 Appendix E.2: If the element is a root element, paint the background over the entire canvas.
  264. background_rect = context.css_viewport_rect();
  265. // Section 2.11.2: If the computed value of background-image on the root element is none and its background-color is transparent,
  266. // user agents must instead propagate the computed values of the background properties from that element’s first HTML BODY child element.
  267. if (document().html_element()->should_use_body_background_properties()) {
  268. background_layers = document().background_layers();
  269. background_color = document().background_color();
  270. }
  271. } else {
  272. background_rect = absolute_padding_box_rect();
  273. }
  274. // HACK: If the Box has a border, use the bordered_rect to paint the background.
  275. // This way if we have a border-radius there will be no gap between the filling and actual border.
  276. if (computed_values().border_top().width != 0 || computed_values().border_right().width != 0 || computed_values().border_bottom().width != 0 || computed_values().border_left().width != 0)
  277. background_rect = absolute_border_box_rect();
  278. Painting::paint_background(context, layout_box(), background_rect, background_color, computed_values().image_rendering(), background_layers, normalized_border_radii_data());
  279. }
  280. Vector<ShadowData> PaintableBox::resolve_box_shadow_data() const
  281. {
  282. auto box_shadow_data = computed_values().box_shadow();
  283. if (box_shadow_data.is_empty())
  284. return {};
  285. Vector<ShadowData> resolved_box_shadow_data;
  286. resolved_box_shadow_data.ensure_capacity(box_shadow_data.size());
  287. for (auto const& layer : box_shadow_data) {
  288. resolved_box_shadow_data.empend(
  289. layer.color,
  290. layer.offset_x.to_px(layout_box()),
  291. layer.offset_y.to_px(layout_box()),
  292. layer.blur_radius.to_px(layout_box()),
  293. layer.spread_distance.to_px(layout_box()),
  294. layer.placement == CSS::ShadowPlacement::Outer ? ShadowPlacement::Outer : ShadowPlacement::Inner);
  295. }
  296. return resolved_box_shadow_data;
  297. }
  298. void PaintableBox::paint_box_shadow(PaintContext& context) const
  299. {
  300. auto resolved_box_shadow_data = resolve_box_shadow_data();
  301. if (resolved_box_shadow_data.is_empty())
  302. return;
  303. auto borders_data = BordersData {
  304. .top = computed_values().border_top(),
  305. .right = computed_values().border_right(),
  306. .bottom = computed_values().border_bottom(),
  307. .left = computed_values().border_left(),
  308. };
  309. Painting::paint_box_shadow(context, absolute_border_box_rect(), absolute_padding_box_rect(),
  310. borders_data, normalized_border_radii_data(), resolved_box_shadow_data);
  311. }
  312. BorderRadiiData PaintableBox::normalized_border_radii_data(ShrinkRadiiForBorders shrink) const
  313. {
  314. auto border_radius_data = Painting::normalized_border_radii_data(layout_box(),
  315. absolute_border_box_rect(),
  316. computed_values().border_top_left_radius(),
  317. computed_values().border_top_right_radius(),
  318. computed_values().border_bottom_right_radius(),
  319. computed_values().border_bottom_left_radius());
  320. if (shrink == ShrinkRadiiForBorders::Yes)
  321. border_radius_data.shrink(computed_values().border_top().width, computed_values().border_right().width, computed_values().border_bottom().width, computed_values().border_left().width);
  322. return border_radius_data;
  323. }
  324. Optional<CSSPixelRect> PaintableBox::calculate_overflow_clipped_rect() const
  325. {
  326. if (!m_clip_rect.has_value()) {
  327. // NOTE: stacking context should not be crossed while aggregating rectangle to
  328. // clip `overflow: hidden` because intersecting rectangles with different
  329. // transforms doesn't make sense
  330. // TODO: figure out if there are cases when stacking context should be
  331. // crossed to calculate correct clip rect
  332. if (!stacking_context() && containing_block() && containing_block()->paintable_box()) {
  333. m_clip_rect = containing_block()->paintable_box()->calculate_overflow_clipped_rect();
  334. }
  335. auto overflow_x = computed_values().overflow_x();
  336. auto overflow_y = computed_values().overflow_y();
  337. if (overflow_x != CSS::Overflow::Visible && overflow_y != CSS::Overflow::Visible) {
  338. if (m_clip_rect.has_value()) {
  339. m_clip_rect->intersect(absolute_padding_box_rect());
  340. } else {
  341. m_clip_rect = absolute_padding_box_rect();
  342. }
  343. }
  344. }
  345. return m_clip_rect;
  346. }
  347. void PaintableBox::before_children_paint(PaintContext& context, PaintPhase) const
  348. {
  349. auto scroll_offset = -this->scroll_offset();
  350. context.translate_scroll_offset_by(scroll_offset);
  351. context.painter().translate({ context.enclosing_device_pixels(scroll_offset.x()), context.enclosing_device_pixels(scroll_offset.y()) });
  352. }
  353. void PaintableBox::after_children_paint(PaintContext& context, PaintPhase) const
  354. {
  355. auto scroll_offset = this->scroll_offset();
  356. context.translate_scroll_offset_by(scroll_offset);
  357. context.painter().translate({ context.enclosing_device_pixels(scroll_offset.x()), context.enclosing_device_pixels(scroll_offset.y()) });
  358. }
  359. void PaintableBox::apply_clip_overflow_rect(PaintContext& context, PaintPhase phase) const
  360. {
  361. if (!AK::first_is_one_of(phase, PaintPhase::Background, PaintPhase::Border, PaintPhase::Foreground))
  362. return;
  363. // FIXME: Support more overflow variations.
  364. auto clip_rect = this->calculate_overflow_clipped_rect();
  365. auto overflow_x = computed_values().overflow_x();
  366. auto overflow_y = computed_values().overflow_y();
  367. auto clip = computed_values().clip();
  368. if (clip.is_rect() && layout_box().is_absolutely_positioned()) {
  369. auto border_box = absolute_border_box_rect();
  370. auto resolved_clip_rect = clip.to_rect().resolved(layout_node(), border_box.to_type<double>()).to_type<CSSPixels>();
  371. if (clip_rect.has_value()) {
  372. clip_rect->intersect(resolved_clip_rect);
  373. } else {
  374. clip_rect = resolved_clip_rect;
  375. }
  376. }
  377. if (!clip_rect.has_value())
  378. return;
  379. if (!m_clipping_overflow) {
  380. context.painter().save();
  381. auto scroll_offset = context.scroll_offset();
  382. context.painter().translate({ -context.enclosing_device_pixels(scroll_offset.x()), -context.enclosing_device_pixels(scroll_offset.y()) });
  383. context.painter().add_clip_rect(context.enclosing_device_rect(*clip_rect).to_type<int>());
  384. context.painter().translate({ context.enclosing_device_pixels(scroll_offset.x()), context.enclosing_device_pixels(scroll_offset.y()) });
  385. m_clipping_overflow = true;
  386. }
  387. if (!clip_rect->is_empty() && overflow_y == CSS::Overflow::Hidden && overflow_x == CSS::Overflow::Hidden) {
  388. auto border_radii_data = normalized_border_radii_data(ShrinkRadiiForBorders::Yes);
  389. if (border_radii_data.has_any_radius()) {
  390. auto corner_clipper = BorderRadiusCornerClipper::create(context, context.rounded_device_rect(*clip_rect), border_radii_data, CornerClip::Outside, BorderRadiusCornerClipper::UseCachedBitmap::No);
  391. if (corner_clipper.is_error()) {
  392. dbgln("Failed to create overflow border-radius corner clipper: {}", corner_clipper.error());
  393. return;
  394. }
  395. m_overflow_corner_radius_clipper = corner_clipper.release_value();
  396. m_overflow_corner_radius_clipper->sample_under_corners(context.painter());
  397. }
  398. }
  399. }
  400. void PaintableBox::clear_clip_overflow_rect(PaintContext& context, PaintPhase phase) const
  401. {
  402. if (!AK::first_is_one_of(phase, PaintPhase::Background, PaintPhase::Border, PaintPhase::Foreground))
  403. return;
  404. // FIXME: Support more overflow variations.
  405. if (m_clipping_overflow) {
  406. context.painter().restore();
  407. m_clipping_overflow = false;
  408. }
  409. if (m_overflow_corner_radius_clipper.has_value()) {
  410. m_overflow_corner_radius_clipper->blit_corner_clipping(context.painter());
  411. m_overflow_corner_radius_clipper = {};
  412. }
  413. }
  414. static void paint_cursor_if_needed(PaintContext& context, Layout::TextNode const& text_node, Layout::LineBoxFragment const& fragment)
  415. {
  416. auto const& browsing_context = text_node.browsing_context();
  417. if (!browsing_context.is_focused_context())
  418. return;
  419. if (!browsing_context.cursor_blink_state())
  420. return;
  421. if (browsing_context.cursor_position().node() != &text_node.dom_node())
  422. return;
  423. // 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.
  424. if (browsing_context.cursor_position().offset() < (unsigned)fragment.start() || browsing_context.cursor_position().offset() > (unsigned)(fragment.start() + fragment.length()))
  425. return;
  426. if (!fragment.layout_node().dom_node() || !fragment.layout_node().dom_node()->is_editable())
  427. return;
  428. auto fragment_rect = fragment.absolute_rect();
  429. CSSPixelRect cursor_rect {
  430. fragment_rect.x() + text_node.font().width(fragment.text().substring_view(0, text_node.browsing_context().cursor_position().offset() - fragment.start())),
  431. fragment_rect.top(),
  432. 1,
  433. fragment_rect.height()
  434. };
  435. auto cursor_device_rect = context.rounded_device_rect(cursor_rect).to_type<int>();
  436. context.painter().draw_rect(cursor_device_rect, text_node.computed_values().color());
  437. }
  438. static void paint_text_decoration(PaintContext& context, Gfx::Painter& painter, Layout::Node const& text_node, Layout::LineBoxFragment const& fragment)
  439. {
  440. auto& font = fragment.layout_node().font();
  441. auto fragment_box = fragment.absolute_rect();
  442. CSSPixels glyph_height = font.pixel_size();
  443. auto baseline = fragment_box.height() / 2 - (glyph_height + 4) / 2 + glyph_height;
  444. auto line_color = text_node.computed_values().text_decoration_color();
  445. CSSPixels css_line_thickness = [&] {
  446. CSS::Length computed_thickness = text_node.computed_values().text_decoration_thickness().resolved(text_node, CSS::Length(1, CSS::Length::Type::Em));
  447. if (computed_thickness.is_auto())
  448. return max(glyph_height * 0.1, 1.);
  449. return computed_thickness.to_px(text_node).to_double();
  450. }();
  451. auto device_line_thickness = context.rounded_device_pixels(css_line_thickness);
  452. auto text_decoration_lines = text_node.computed_values().text_decoration_line();
  453. for (auto line : text_decoration_lines) {
  454. DevicePixelPoint line_start_point {};
  455. DevicePixelPoint line_end_point {};
  456. switch (line) {
  457. case CSS::TextDecorationLine::None:
  458. return;
  459. case CSS::TextDecorationLine::Underline:
  460. line_start_point = context.rounded_device_point(fragment_box.top_left().translated(0, baseline + 2));
  461. line_end_point = context.rounded_device_point(fragment_box.top_right().translated(-1, baseline + 2));
  462. break;
  463. case CSS::TextDecorationLine::Overline:
  464. line_start_point = context.rounded_device_point(fragment_box.top_left().translated(0, baseline - glyph_height));
  465. line_end_point = context.rounded_device_point(fragment_box.top_right().translated(-1, baseline - glyph_height));
  466. break;
  467. case CSS::TextDecorationLine::LineThrough: {
  468. auto x_height = font.x_height();
  469. line_start_point = context.rounded_device_point(fragment_box.top_left().translated(0, baseline - x_height * 0.5f));
  470. line_end_point = context.rounded_device_point(fragment_box.top_right().translated(-1, baseline - x_height * 0.5f));
  471. break;
  472. }
  473. case CSS::TextDecorationLine::Blink:
  474. // Conforming user agents may simply not blink the text
  475. return;
  476. }
  477. switch (text_node.computed_values().text_decoration_style()) {
  478. case CSS::TextDecorationStyle::Solid:
  479. painter.draw_line(line_start_point.to_type<int>(), line_end_point.to_type<int>(), line_color, device_line_thickness.value(), Gfx::Painter::LineStyle::Solid);
  480. break;
  481. case CSS::TextDecorationStyle::Double:
  482. switch (line) {
  483. case CSS::TextDecorationLine::Underline:
  484. break;
  485. case CSS::TextDecorationLine::Overline:
  486. line_start_point.translate_by(0, -device_line_thickness - context.rounded_device_pixels(1));
  487. line_end_point.translate_by(0, -device_line_thickness - context.rounded_device_pixels(1));
  488. break;
  489. case CSS::TextDecorationLine::LineThrough:
  490. line_start_point.translate_by(0, -device_line_thickness / 2);
  491. line_end_point.translate_by(0, -device_line_thickness / 2);
  492. break;
  493. default:
  494. VERIFY_NOT_REACHED();
  495. }
  496. painter.draw_line(line_start_point.to_type<int>(), line_end_point.to_type<int>(), line_color, device_line_thickness.value());
  497. painter.draw_line(line_start_point.translated(0, device_line_thickness + 1).to_type<int>(), line_end_point.translated(0, device_line_thickness + 1).to_type<int>(), line_color, device_line_thickness.value());
  498. break;
  499. case CSS::TextDecorationStyle::Dashed:
  500. painter.draw_line(line_start_point.to_type<int>(), line_end_point.to_type<int>(), line_color, device_line_thickness.value(), Gfx::Painter::LineStyle::Dashed);
  501. break;
  502. case CSS::TextDecorationStyle::Dotted:
  503. painter.draw_line(line_start_point.to_type<int>(), line_end_point.to_type<int>(), line_color, device_line_thickness.value(), Gfx::Painter::LineStyle::Dotted);
  504. break;
  505. case CSS::TextDecorationStyle::Wavy:
  506. painter.draw_triangle_wave(line_start_point.to_type<int>(), line_end_point.to_type<int>(), line_color, device_line_thickness.value() + 1, device_line_thickness.value());
  507. break;
  508. }
  509. }
  510. }
  511. static void paint_text_fragment(PaintContext& context, Layout::TextNode const& text_node, Layout::LineBoxFragment const& fragment, PaintPhase phase)
  512. {
  513. auto& painter = context.painter();
  514. if (phase == PaintPhase::Foreground) {
  515. auto fragment_absolute_rect = fragment.absolute_rect();
  516. auto fragment_absolute_device_rect = context.enclosing_device_rect(fragment_absolute_rect);
  517. if (text_node.document().inspected_layout_node() == &text_node)
  518. context.painter().draw_rect(fragment_absolute_device_rect.to_type<int>(), Color::Magenta);
  519. auto text = text_node.text_for_rendering();
  520. DevicePixelPoint baseline_start { fragment_absolute_device_rect.x(), fragment_absolute_device_rect.y() + context.rounded_device_pixels(fragment.baseline()) };
  521. Utf8View view { text.substring_view(fragment.start(), fragment.length()) };
  522. auto& scaled_font = fragment.layout_node().scaled_font(context);
  523. painter.draw_text_run(baseline_start.to_type<int>(), view, scaled_font, text_node.computed_values().color());
  524. auto selection_rect = context.enclosing_device_rect(fragment.selection_rect(text_node.font())).to_type<int>();
  525. if (!selection_rect.is_empty()) {
  526. painter.fill_rect(selection_rect, context.palette().selection());
  527. Gfx::PainterStateSaver saver(painter);
  528. painter.add_clip_rect(selection_rect);
  529. painter.draw_text_run(baseline_start.to_type<int>(), view, scaled_font, context.palette().selection_text());
  530. }
  531. paint_text_decoration(context, painter, text_node, fragment);
  532. paint_cursor_if_needed(context, text_node, fragment);
  533. }
  534. }
  535. void PaintableWithLines::paint(PaintContext& context, PaintPhase phase) const
  536. {
  537. if (!is_visible())
  538. return;
  539. PaintableBox::paint(context, phase);
  540. if (m_line_boxes.is_empty())
  541. return;
  542. bool should_clip_overflow = computed_values().overflow_x() != CSS::Overflow::Visible && computed_values().overflow_y() != CSS::Overflow::Visible;
  543. Optional<BorderRadiusCornerClipper> corner_clipper;
  544. if (should_clip_overflow) {
  545. context.painter().save();
  546. // FIXME: Handle overflow-x and overflow-y being different values.
  547. auto clip_box = context.rounded_device_rect(absolute_padding_box_rect());
  548. context.painter().add_clip_rect(clip_box.to_type<int>());
  549. auto scroll_offset = context.rounded_device_point(this->scroll_offset());
  550. context.painter().translate(-scroll_offset.to_type<int>());
  551. auto border_radii = normalized_border_radii_data(ShrinkRadiiForBorders::Yes);
  552. if (border_radii.has_any_radius()) {
  553. auto clipper = BorderRadiusCornerClipper::create(context, clip_box, border_radii);
  554. if (!clipper.is_error()) {
  555. corner_clipper = clipper.release_value();
  556. corner_clipper->sample_under_corners(context.painter());
  557. }
  558. }
  559. }
  560. // Text shadows
  561. // This is yet another loop, but done here because all shadows should appear under all text.
  562. // So, we paint the shadows before painting any text.
  563. // FIXME: Find a smarter way to do this?
  564. if (phase == PaintPhase::Foreground) {
  565. for (auto& line_box : m_line_boxes) {
  566. for (auto& fragment : line_box.fragments()) {
  567. if (is<Layout::TextNode>(fragment.layout_node())) {
  568. auto& text_shadow = fragment.layout_node().computed_values().text_shadow();
  569. if (!text_shadow.is_empty()) {
  570. Vector<ShadowData> resolved_shadow_data;
  571. resolved_shadow_data.ensure_capacity(text_shadow.size());
  572. for (auto const& layer : text_shadow) {
  573. resolved_shadow_data.empend(
  574. layer.color,
  575. layer.offset_x.to_px(layout_box()),
  576. layer.offset_y.to_px(layout_box()),
  577. layer.blur_radius.to_px(layout_box()),
  578. layer.spread_distance.to_px(layout_box()),
  579. ShadowPlacement::Outer);
  580. }
  581. context.painter().set_font(fragment.layout_node().font());
  582. paint_text_shadow(context, fragment, resolved_shadow_data);
  583. }
  584. }
  585. }
  586. }
  587. }
  588. for (auto& line_box : m_line_boxes) {
  589. for (auto& fragment : line_box.fragments()) {
  590. auto fragment_absolute_rect = fragment.absolute_rect();
  591. auto fragment_absolute_device_rect = context.enclosing_device_rect(fragment_absolute_rect);
  592. if (context.would_be_fully_clipped_by_painter(fragment_absolute_device_rect))
  593. continue;
  594. if (context.should_show_line_box_borders()) {
  595. context.painter().draw_rect(fragment_absolute_device_rect.to_type<int>(), Color::Green);
  596. context.painter().draw_line(
  597. context.rounded_device_point(fragment_absolute_rect.top_left().translated(0, fragment.baseline())).to_type<int>(),
  598. context.rounded_device_point(fragment_absolute_rect.top_right().translated(-1, fragment.baseline())).to_type<int>(), Color::Red);
  599. }
  600. if (is<Layout::TextNode>(fragment.layout_node()))
  601. paint_text_fragment(context, static_cast<Layout::TextNode const&>(fragment.layout_node()), fragment, phase);
  602. }
  603. }
  604. if (should_clip_overflow) {
  605. context.painter().restore();
  606. if (corner_clipper.has_value())
  607. corner_clipper->blit_corner_clipping(context.painter());
  608. }
  609. }
  610. bool PaintableBox::handle_mousewheel(Badge<EventHandler>, CSSPixelPoint, unsigned, unsigned, int wheel_delta_x, int wheel_delta_y)
  611. {
  612. if (!layout_box().is_user_scrollable())
  613. return false;
  614. scroll_by(wheel_delta_x, wheel_delta_y);
  615. return true;
  616. }
  617. Layout::BlockContainer const& PaintableWithLines::layout_box() const
  618. {
  619. return static_cast<Layout::BlockContainer const&>(PaintableBox::layout_box());
  620. }
  621. Layout::BlockContainer& PaintableWithLines::layout_box()
  622. {
  623. return static_cast<Layout::BlockContainer&>(PaintableBox::layout_box());
  624. }
  625. void PaintableBox::set_stacking_context(NonnullOwnPtr<StackingContext> stacking_context)
  626. {
  627. m_stacking_context = move(stacking_context);
  628. }
  629. Optional<HitTestResult> PaintableBox::hit_test(CSSPixelPoint position, HitTestType type) const
  630. {
  631. if (!is_visible())
  632. return {};
  633. if (layout_box().is_viewport()) {
  634. const_cast<ViewportPaintable&>(static_cast<ViewportPaintable const&>(*this)).build_stacking_context_tree_if_needed();
  635. return stacking_context()->hit_test(position, type);
  636. }
  637. if (!absolute_border_box_rect().contains(position.x(), position.y()))
  638. return {};
  639. for (auto* child = first_child(); child; child = child->next_sibling()) {
  640. auto result = child->hit_test(position, type);
  641. if (!result.has_value())
  642. continue;
  643. if (!result->paintable->visible_for_hit_testing())
  644. continue;
  645. return result;
  646. }
  647. if (!visible_for_hit_testing())
  648. return {};
  649. return HitTestResult { const_cast<PaintableBox&>(*this) };
  650. }
  651. Optional<HitTestResult> PaintableWithLines::hit_test(CSSPixelPoint position, HitTestType type) const
  652. {
  653. if (!layout_box().children_are_inline())
  654. return PaintableBox::hit_test(position, type);
  655. Optional<HitTestResult> last_good_candidate;
  656. for (auto& line_box : m_line_boxes) {
  657. for (auto& fragment : line_box.fragments()) {
  658. if (is<Layout::Box>(fragment.layout_node()) && static_cast<Layout::Box const&>(fragment.layout_node()).paintable_box()->stacking_context())
  659. continue;
  660. if (!fragment.layout_node().containing_block()) {
  661. dbgln("FIXME: PaintableWithLines::hit_test(): Missing containing block on {}", fragment.layout_node().debug_description());
  662. continue;
  663. }
  664. auto fragment_absolute_rect = fragment.absolute_rect();
  665. if (fragment_absolute_rect.contains(position)) {
  666. if (is<Layout::BlockContainer>(fragment.layout_node()) && fragment.layout_node().paintable())
  667. return fragment.layout_node().paintable()->hit_test(position, type);
  668. return HitTestResult { const_cast<Paintable&>(const_cast<Paintable&>(*fragment.layout_node().paintable())), fragment.text_index_at(position.x()) };
  669. }
  670. // If we reached this point, the position is not within the fragment. However, the fragment start or end might be the place to place the cursor.
  671. // This determines whether the fragment is a good candidate for the position. The last such good fragment is chosen.
  672. // The best candidate is either the end of the line above, the beginning of the line below, or the beginning or end of the current line.
  673. // We arbitrarily choose to consider the end of the line above and ignore the beginning of the line below.
  674. // If we knew the direction of selection, we could make a better choice.
  675. if (fragment_absolute_rect.bottom() - 1 <= position.y()) { // fully below the fragment
  676. last_good_candidate = HitTestResult { const_cast<Paintable&>(*fragment.layout_node().paintable()), fragment.start() + fragment.length() };
  677. } else if (fragment_absolute_rect.top() <= position.y()) { // vertically within the fragment
  678. if (position.x() < fragment_absolute_rect.left()) { // left of the fragment
  679. if (!last_good_candidate.has_value()) { // first fragment of the line
  680. last_good_candidate = HitTestResult { const_cast<Paintable&>(*fragment.layout_node().paintable()), fragment.start() };
  681. }
  682. } else { // right of the fragment
  683. last_good_candidate = HitTestResult { const_cast<Paintable&>(*fragment.layout_node().paintable()), fragment.start() + fragment.length() };
  684. }
  685. }
  686. }
  687. }
  688. if (type == HitTestType::TextCursor && last_good_candidate.has_value())
  689. return last_good_candidate;
  690. if (is_visible() && absolute_border_box_rect().contains(position.x(), position.y()))
  691. return HitTestResult { const_cast<PaintableWithLines&>(*this) };
  692. return {};
  693. }
  694. }