Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <AK/Utf8View.h>
28#include <LibWeb/Layout/LayoutNode.h>
29#include <LibWeb/Layout/LayoutText.h>
30#include <LibWeb/Layout/LineBox.h>
31#include <ctype.h>
32
33namespace Web {
34
35void LineBox::add_fragment(const LayoutNode& layout_node, int start, int length, int width, int height)
36{
37 bool text_align_is_justify = layout_node.style().string_or_fallback(CSS::PropertyID::TextAlign, "left") == "justify";
38 if (!text_align_is_justify && !m_fragments.is_empty() && &m_fragments.last().layout_node() == &layout_node) {
39 // The fragment we're adding is from the last LayoutNode on the line.
40 // Expand the last fragment instead of adding a new one with the same LayoutNode.
41 m_fragments.last().m_length = (start - m_fragments.last().m_start) + length;
42 m_fragments.last().m_rect.set_width(m_fragments.last().m_rect.width() + width);
43 } else {
44 m_fragments.empend(layout_node, start, length, Gfx::FloatRect(m_width, 0, width, height));
45 }
46 m_width += width;
47}
48
49void LineBox::trim_trailing_whitespace()
50{
51 while (!m_fragments.is_empty() && m_fragments.last().is_justifiable_whitespace()) {
52 auto fragment = m_fragments.take_last();
53 m_width -= fragment.width();
54 }
55
56 if (m_fragments.is_empty())
57 return;
58
59 auto last_text = m_fragments.last().text();
60 if (last_text.is_null())
61 return;
62 auto& last_fragment = m_fragments.last();
63
64 int space_width = last_fragment.layout_node().style().font().glyph_width(' ');
65 while (last_fragment.length() && isspace(last_text[last_fragment.length() - 1])) {
66 last_fragment.m_length -= 1;
67 last_fragment.m_rect.set_width(last_fragment.m_rect.width() - space_width);
68 m_width -= space_width;
69 }
70}
71
72}