Serenity Operating System
at master 69 lines 2.7 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 */ 6 7#include <AK/CharacterTypes.h> 8#include <AK/TypeCasts.h> 9#include <AK/Utf8View.h> 10#include <LibWeb/Layout/Box.h> 11#include <LibWeb/Layout/BreakNode.h> 12#include <LibWeb/Layout/LineBox.h> 13#include <LibWeb/Layout/Node.h> 14#include <LibWeb/Layout/TextNode.h> 15 16namespace Web::Layout { 17 18void 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, LineBoxFragment::Type fragment_type) 19{ 20 bool text_align_is_justify = layout_node.computed_values().text_align() == CSS::TextAlign::Justify; 21 if (!text_align_is_justify && !m_fragments.is_empty() && &m_fragments.last().layout_node() == &layout_node) { 22 // The fragment we're adding is from the last Layout::Node on the line. 23 // Expand the last fragment instead of adding a new one with the same Layout::Node. 24 m_fragments.last().m_length = (start - m_fragments.last().m_start) + length; 25 m_fragments.last().set_width(m_fragments.last().width() + content_width); 26 } else { 27 CSSPixels x_offset = leading_margin + leading_size + m_width; 28 CSSPixels y_offset = 0.0f; 29 m_fragments.append(LineBoxFragment { layout_node, start, length, CSSPixelPoint(x_offset, y_offset), CSSPixelSize(content_width, content_height), border_box_top, border_box_bottom, fragment_type }); 30 } 31 m_width += leading_margin + leading_size + content_width + trailing_size + trailing_margin; 32} 33 34void LineBox::trim_trailing_whitespace() 35{ 36 while (!m_fragments.is_empty() && m_fragments.last().is_justifiable_whitespace()) { 37 auto fragment = m_fragments.take_last(); 38 m_width -= fragment.width().value(); 39 } 40 41 if (m_fragments.is_empty()) 42 return; 43 44 auto& last_fragment = m_fragments.last(); 45 auto last_text = last_fragment.text(); 46 if (last_text.is_null()) 47 return; 48 49 while (last_fragment.length()) { 50 auto last_character = last_text[last_fragment.length() - 1]; 51 if (!is_ascii_space(last_character)) 52 break; 53 54 int last_character_width = last_fragment.layout_node().font().glyph_width(last_character); 55 last_fragment.m_length -= 1; 56 last_fragment.set_width(last_fragment.width() - last_character_width); 57 m_width -= last_character_width; 58 } 59} 60 61bool LineBox::is_empty_or_ends_in_whitespace() const 62{ 63 if (m_fragments.is_empty()) 64 return true; 65 66 return m_fragments.last().ends_in_whitespace(); 67} 68 69}