| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-04-22 01:24:48 -07:00
										 |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-28 02:08:15 +00:00
										 |  |  | #include <AK/CharacterTypes.h>
 | 
					
						
							| 
									
										
										
										
											2021-10-26 16:15:53 +02:00
										 |  |  | #include <AK/TypeCasts.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-15 00:27:50 +01:00
										 |  |  | #include <AK/Utf8View.h>
 | 
					
						
							| 
									
										
										
										
											2020-11-22 15:53:01 +01:00
										 |  |  | #include <LibWeb/Layout/Box.h>
 | 
					
						
							| 
									
										
										
										
											2021-10-26 16:15:53 +02:00
										 |  |  | #include <LibWeb/Layout/BreakNode.h>
 | 
					
						
							| 
									
										
										
										
											2020-03-07 10:32:51 +01:00
										 |  |  | #include <LibWeb/Layout/LineBox.h>
 | 
					
						
							| 
									
										
										
										
											2020-11-22 15:53:01 +01:00
										 |  |  | #include <LibWeb/Layout/Node.h>
 | 
					
						
							|  |  |  | #include <LibWeb/Layout/TextNode.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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-17 17:12:54 +02:00
										 |  |  | void LineBox::add_fragment(Node const& layout_node, int start, int length, CSSPixels leading_size, CSSPixels trailing_size, CSSPixels leading_margin, CSSPixels trailing_margin, CSSPixels content_width, CSSPixels content_height, CSSPixels border_box_top, CSSPixels border_box_bottom) | 
					
						
							| 
									
										
										
										
											2019-10-03 15:20:13 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-01-06 11:07:02 +01:00
										 |  |  |     bool text_align_is_justify = layout_node.computed_values().text_align() == CSS::TextAlign::Justify; | 
					
						
							| 
									
										
										
										
											2019-10-20 12:30:25 +02:00
										 |  |  |     if (!text_align_is_justify && !m_fragments.is_empty() && &m_fragments.last().layout_node() == &layout_node) { | 
					
						
							| 
									
										
										
										
											2020-11-22 15:53:01 +01:00
										 |  |  |         // The fragment we're adding is from the last Layout::Node on the line.
 | 
					
						
							|  |  |  |         // Expand the last fragment instead of adding a new one with the same Layout::Node.
 | 
					
						
							| 
									
										
										
										
											2019-10-03 15:20:13 +02:00
										 |  |  |         m_fragments.last().m_length = (start - m_fragments.last().m_start) + length; | 
					
						
							| 
									
										
										
										
											2022-02-14 15:52:29 +01:00
										 |  |  |         m_fragments.last().set_width(m_fragments.last().width() + content_width); | 
					
						
							| 
									
										
										
										
											2019-10-03 15:20:13 +02:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2022-11-04 17:19:11 +00:00
										 |  |  |         CSSPixels x_offset = leading_margin + leading_size + m_width; | 
					
						
							| 
									
										
										
										
											2023-08-26 15:03:04 +01:00
										 |  |  |         CSSPixels y_offset = 0; | 
					
						
							| 
									
										
										
										
											2023-07-17 17:12:54 +02:00
										 |  |  |         m_fragments.append(LineBoxFragment { layout_node, start, length, CSSPixelPoint(x_offset, y_offset), CSSPixelSize(content_width, content_height), border_box_top, border_box_bottom }); | 
					
						
							| 
									
										
										
										
											2019-10-03 15:20:13 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-03-09 18:46:07 +01:00
										 |  |  |     m_width += leading_margin + leading_size + content_width + trailing_size + trailing_margin; | 
					
						
							| 
									
										
										
										
											2019-10-03 15:20:13 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-10-20 17:18:28 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | void LineBox::trim_trailing_whitespace() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-07-16 14:10:47 +02:00
										 |  |  |     auto should_trim = [](LineBoxFragment* fragment) { | 
					
						
							|  |  |  |         auto ws = fragment->layout_node().computed_values().white_space(); | 
					
						
							|  |  |  |         return ws == CSS::WhiteSpace::Normal || ws == CSS::WhiteSpace::Nowrap || ws == CSS::WhiteSpace::PreLine; | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2019-10-20 17:18:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-16 14:10:47 +02:00
										 |  |  |     LineBoxFragment* last_fragment = nullptr; | 
					
						
							|  |  |  |     for (;;) { | 
					
						
							|  |  |  |         if (m_fragments.is_empty()) | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         // last_fragment cannot be null from here on down, as m_fragments is not empty.
 | 
					
						
							|  |  |  |         last_fragment = &m_fragments.last(); | 
					
						
							|  |  |  |         if (!should_trim(last_fragment)) | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         if (last_fragment->is_justifiable_whitespace()) { | 
					
						
							|  |  |  |             m_width -= last_fragment->width(); | 
					
						
							|  |  |  |             m_fragments.remove(m_fragments.size() - 1); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-10-20 17:18:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-16 14:10:47 +02:00
										 |  |  |     auto last_text = last_fragment->text(); | 
					
						
							| 
									
										
										
										
											2019-10-20 17:18:28 +02:00
										 |  |  |     if (last_text.is_null()) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-16 14:10:47 +02:00
										 |  |  |     while (last_fragment->length()) { | 
					
						
							|  |  |  |         auto last_character = last_text[last_fragment->length() - 1]; | 
					
						
							| 
									
										
										
										
											2021-08-28 02:08:15 +00:00
										 |  |  |         if (!is_ascii_space(last_character)) | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-16 14:10:47 +02:00
										 |  |  |         int last_character_width = last_fragment->layout_node().font().glyph_width(last_character); | 
					
						
							|  |  |  |         last_fragment->m_length -= 1; | 
					
						
							|  |  |  |         last_fragment->set_width(last_fragment->width() - last_character_width); | 
					
						
							| 
									
										
										
										
											2021-08-28 02:08:15 +00:00
										 |  |  |         m_width -= last_character_width; | 
					
						
							| 
									
										
										
										
											2019-10-20 17:18:28 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-03-07 10:27:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-17 20:17:29 +01:00
										 |  |  | bool LineBox::is_empty_or_ends_in_whitespace() const | 
					
						
							| 
									
										
										
										
											2020-06-13 14:59:17 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     if (m_fragments.is_empty()) | 
					
						
							| 
									
										
										
										
											2020-12-17 20:17:29 +01:00
										 |  |  |         return true; | 
					
						
							| 
									
										
										
										
											2021-10-26 16:15:53 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-13 14:59:17 +02:00
										 |  |  |     return m_fragments.last().ends_in_whitespace(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-07 10:27:02 +01:00
										 |  |  | } |