PaintableBox.cpp 34 KB

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