PaintableBox.cpp 34 KB

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