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 <LibHTML/Layout/LayoutNode.h>
29#include <LibHTML/Layout/LayoutText.h>
30#include <LibHTML/Layout/LineBox.h>
31#include <ctype.h>
32
33void LineBox::add_fragment(const LayoutNode& layout_node, int start, int length, int width, int height)
34{
35 bool text_align_is_justify = layout_node.style().string_or_fallback(CSS::PropertyID::TextAlign, "left") == "justify";
36 if (!text_align_is_justify && !m_fragments.is_empty() && &m_fragments.last().layout_node() == &layout_node) {
37 // The fragment we're adding is from the last LayoutNode on the line.
38 // Expand the last fragment instead of adding a new one with the same LayoutNode.
39 m_fragments.last().m_length = (start - m_fragments.last().m_start) + length;
40 m_fragments.last().m_rect.set_width(m_fragments.last().m_rect.width() + width);
41 } else {
42 m_fragments.empend(layout_node, start, length, Gfx::FloatRect(m_width, 0, width, height));
43 }
44 m_width += width;
45}
46
47void LineBox::trim_trailing_whitespace()
48{
49 while (!m_fragments.is_empty() && m_fragments.last().is_justifiable_whitespace()) {
50 auto fragment = m_fragments.take_last();
51 m_width -= fragment.width();
52 }
53
54 if (m_fragments.is_empty())
55 return;
56
57 auto last_text = m_fragments.last().text();
58 if (last_text.is_null())
59 return;
60 auto& last_fragment = m_fragments.last();
61
62 int space_width = last_fragment.layout_node().style().font().glyph_width(' ');
63 while (last_fragment.length() && isspace(last_text[last_fragment.length() - 1])) {
64 last_fragment.m_length -= 1;
65 last_fragment.m_rect.set_width(last_fragment.m_rect.width() - space_width);
66 m_width -= space_width;
67 }
68}