PaintableBox.cpp 37 KB

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