| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2024-10-04 13:19:50 +02:00
										 |  |  |  * Copyright (c) 2018-2023, Andreas Kling <andreas@ladybird.org> | 
					
						
							| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-04-22 01:24:48 -07:00
										 |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-15 00:27:50 +01:00
										 |  |  | #include <AK/Utf8View.h>
 | 
					
						
							| 
									
										
										
										
											2022-07-16 23:30:32 +02:00
										 |  |  | #include <LibWeb/Layout/LayoutState.h>
 | 
					
						
							| 
									
										
										
										
											2024-10-14 16:07:56 +02:00
										 |  |  | #include <LibWeb/Layout/TextNode.h>
 | 
					
						
							| 
									
										
										
										
											2020-06-13 14:59:17 +02:00
										 |  |  | #include <ctype.h>
 | 
					
						
							| 
									
										
										
										
											2019-10-03 15:20:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-22 15:53:01 +01:00
										 |  |  | namespace Web::Layout { | 
					
						
							| 
									
										
										
										
											2020-03-07 10:27:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-29 11:32:59 +00:00
										 |  |  | LineBoxFragment::LineBoxFragment(Node const& layout_node, int start, int length, CSSPixels inline_offset, CSSPixels block_offset, CSSPixels inline_length, CSSPixels block_length, CSSPixels border_box_top, CSS::Direction direction, CSS::WritingMode writing_mode, RefPtr<Gfx::GlyphRun> glyph_run) | 
					
						
							| 
									
										
										
										
											2024-08-18 17:58:05 +01:00
										 |  |  |     : m_layout_node(layout_node) | 
					
						
							|  |  |  |     , m_start(start) | 
					
						
							|  |  |  |     , m_length(length) | 
					
						
							| 
									
										
										
										
											2024-10-29 11:32:59 +00:00
										 |  |  |     , m_inline_offset(inline_offset) | 
					
						
							|  |  |  |     , m_block_offset(block_offset) | 
					
						
							|  |  |  |     , m_inline_length(inline_length) | 
					
						
							|  |  |  |     , m_block_length(block_length) | 
					
						
							| 
									
										
										
										
											2024-08-18 17:58:05 +01:00
										 |  |  |     , m_border_box_top(border_box_top) | 
					
						
							|  |  |  |     , m_direction(direction) | 
					
						
							| 
									
										
										
										
											2024-10-29 11:32:59 +00:00
										 |  |  |     , m_writing_mode(writing_mode) | 
					
						
							| 
									
										
										
										
											2024-08-18 17:58:05 +01:00
										 |  |  |     , m_glyph_run(move(glyph_run)) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (m_glyph_run) { | 
					
						
							|  |  |  |         m_current_insert_direction = resolve_glyph_run_direction(m_glyph_run->text_type()); | 
					
						
							|  |  |  |         if (m_direction == CSS::Direction::Rtl) | 
					
						
							| 
									
										
										
										
											2024-10-29 11:32:59 +00:00
										 |  |  |             m_insert_position = m_inline_length.to_float(); | 
					
						
							| 
									
										
										
										
											2024-08-18 17:58:05 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-29 11:32:59 +00:00
										 |  |  | CSSPixelPoint LineBoxFragment::offset() const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2024-10-29 11:32:59 +00:00
										 |  |  |     if (m_writing_mode != CSS::WritingMode::HorizontalTb) | 
					
						
							|  |  |  |         return { m_block_offset, m_inline_offset }; | 
					
						
							| 
									
										
										
										
											2024-10-29 11:32:59 +00:00
										 |  |  |     return { m_inline_offset, m_block_offset }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | CSSPixelSize LineBoxFragment::size() const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2024-10-29 11:32:59 +00:00
										 |  |  |     if (m_writing_mode != CSS::WritingMode::HorizontalTb) | 
					
						
							|  |  |  |         return { m_block_length, m_inline_length }; | 
					
						
							| 
									
										
										
										
											2024-10-29 11:32:59 +00:00
										 |  |  |     return { m_inline_length, m_block_length }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-13 14:59:17 +02:00
										 |  |  | bool LineBoxFragment::ends_in_whitespace() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto text = this->text(); | 
					
						
							|  |  |  |     if (text.is_empty()) | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     return isspace(text[text.length() - 1]); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-20 12:30:25 +02:00
										 |  |  | bool LineBoxFragment::is_justifiable_whitespace() const | 
					
						
							| 
									
										
										
										
											2019-10-20 17:18:28 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     return text() == " "; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | StringView LineBoxFragment::text() const | 
					
						
							| 
									
										
										
										
											2019-10-20 12:30:25 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-11-22 15:53:01 +01:00
										 |  |  |     if (!is<TextNode>(layout_node())) | 
					
						
							| 
									
										
										
										
											2019-10-20 17:18:28 +02:00
										 |  |  |         return {}; | 
					
						
							| 
									
										
										
										
											2025-01-21 09:12:05 -05:00
										 |  |  |     return as<TextNode>(layout_node()).text_for_rendering().bytes_as_string_view().substring_view(m_start, m_length); | 
					
						
							| 
									
										
										
										
											2019-10-20 12:30:25 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-11-05 22:13:26 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-14 10:28:01 +01:00
										 |  |  | bool LineBoxFragment::is_atomic_inline() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return layout_node().is_replaced_box() || (layout_node().display().is_inline_outside() && !layout_node().display().is_flow_inside()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-18 17:58:05 +01:00
										 |  |  | CSS::Direction LineBoxFragment::resolve_glyph_run_direction(Gfx::GlyphRun::TextType text_type) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     switch (text_type) { | 
					
						
							|  |  |  |     case Gfx::GlyphRun::TextType::Common: | 
					
						
							|  |  |  |     case Gfx::GlyphRun::TextType::ContextDependent: | 
					
						
							|  |  |  |     case Gfx::GlyphRun::TextType::EndPadding: | 
					
						
							|  |  |  |         return m_direction; | 
					
						
							|  |  |  |     case Gfx::GlyphRun::TextType::Ltr: | 
					
						
							|  |  |  |         return CSS::Direction::Ltr; | 
					
						
							|  |  |  |     case Gfx::GlyphRun::TextType::Rtl: | 
					
						
							|  |  |  |         return CSS::Direction::Rtl; | 
					
						
							|  |  |  |     default: | 
					
						
							|  |  |  |         VERIFY_NOT_REACHED(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void LineBoxFragment::append_glyph_run(RefPtr<Gfx::GlyphRun> const& glyph_run, CSSPixels run_width) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     switch (m_direction) { | 
					
						
							|  |  |  |     case CSS::Direction::Ltr: | 
					
						
							|  |  |  |         append_glyph_run_ltr(glyph_run, run_width); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case CSS::Direction::Rtl: | 
					
						
							|  |  |  |         append_glyph_run_rtl(glyph_run, run_width); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void LineBoxFragment::append_glyph_run_ltr(RefPtr<Gfx::GlyphRun> const& glyph_run, CSSPixels run_width) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto run_direction = resolve_glyph_run_direction(glyph_run->text_type()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (m_current_insert_direction != run_direction) { | 
					
						
							|  |  |  |         if (run_direction == CSS::Direction::Rtl) | 
					
						
							| 
									
										
										
										
											2024-10-29 11:32:59 +00:00
										 |  |  |             m_insert_position = m_inline_length.to_float(); | 
					
						
							| 
									
										
										
										
											2024-08-18 17:58:05 +01:00
										 |  |  |         m_current_insert_direction = run_direction; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     switch (run_direction) { | 
					
						
							|  |  |  |     case CSS::Direction::Ltr: | 
					
						
							|  |  |  |         for (auto& glyph : glyph_run->glyphs()) { | 
					
						
							| 
									
										
										
										
											2024-10-29 11:32:59 +00:00
										 |  |  |             glyph.position.translate_by(m_inline_length.to_float(), 0); | 
					
						
							| 
									
										
										
										
											2024-08-18 17:58:05 +01:00
										 |  |  |             m_glyph_run->append(glyph); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case CSS::Direction::Rtl: | 
					
						
							|  |  |  |         for (auto& glyph : m_glyph_run->glyphs()) { | 
					
						
							| 
									
										
										
										
											2024-09-06 01:19:53 +02:00
										 |  |  |             if (glyph.position.x() >= m_insert_position) | 
					
						
							|  |  |  |                 glyph.position.translate_by(run_width.to_float(), 0); | 
					
						
							| 
									
										
										
										
											2024-08-18 17:58:05 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |         for (auto& glyph : glyph_run->glyphs()) { | 
					
						
							| 
									
										
										
										
											2024-09-06 01:19:53 +02:00
										 |  |  |             glyph.position.translate_by(m_insert_position, 0); | 
					
						
							| 
									
										
										
										
											2024-08-18 17:58:05 +01:00
										 |  |  |             m_glyph_run->append(glyph); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-29 11:32:59 +00:00
										 |  |  |     m_inline_length += run_width; | 
					
						
							| 
									
										
										
										
											2024-08-18 17:58:05 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void LineBoxFragment::append_glyph_run_rtl(RefPtr<Gfx::GlyphRun> const& glyph_run, CSSPixels run_width) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto run_direction = resolve_glyph_run_direction(glyph_run->text_type()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (m_current_insert_direction != run_direction) { | 
					
						
							|  |  |  |         if (run_direction == CSS::Direction::Ltr) | 
					
						
							|  |  |  |             m_insert_position = 0; | 
					
						
							|  |  |  |         m_current_insert_direction = run_direction; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     switch (run_direction) { | 
					
						
							|  |  |  |     case CSS::Direction::Ltr: | 
					
						
							|  |  |  |         for (auto& glyph : m_glyph_run->glyphs()) { | 
					
						
							| 
									
										
										
										
											2024-09-06 01:19:53 +02:00
										 |  |  |             if (glyph.position.x() >= m_insert_position) | 
					
						
							|  |  |  |                 glyph.position.translate_by(run_width.to_float(), 0); | 
					
						
							| 
									
										
										
										
											2024-08-18 17:58:05 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |         for (auto& glyph : glyph_run->glyphs()) { | 
					
						
							| 
									
										
										
										
											2024-09-06 01:19:53 +02:00
										 |  |  |             glyph.position.translate_by(m_insert_position, 0); | 
					
						
							| 
									
										
										
										
											2024-08-18 17:58:05 +01:00
										 |  |  |             m_glyph_run->append(glyph); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case CSS::Direction::Rtl: | 
					
						
							|  |  |  |         if (glyph_run->text_type() != Gfx::GlyphRun::TextType::EndPadding) { | 
					
						
							|  |  |  |             for (auto& glyph : m_glyph_run->glyphs()) { | 
					
						
							| 
									
										
										
										
											2024-09-06 01:19:53 +02:00
										 |  |  |                 glyph.position.translate_by(run_width.to_float(), 0); | 
					
						
							| 
									
										
										
										
											2024-08-18 17:58:05 +01:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         for (auto& glyph : glyph_run->glyphs()) { | 
					
						
							|  |  |  |             m_glyph_run->append(glyph); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-29 11:32:59 +00:00
										 |  |  |     m_inline_length += run_width; | 
					
						
							| 
									
										
										
										
											2024-08-18 17:58:05 +01:00
										 |  |  |     m_insert_position += run_width.to_float(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-07 10:27:02 +01:00
										 |  |  | } |