Serenity Operating System
1/*
2 * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#pragma once
8
9#include <LibWeb/Layout/InlineFormattingContext.h>
10
11namespace Web::Layout {
12
13class LineBuilder {
14 AK_MAKE_NONCOPYABLE(LineBuilder);
15 AK_MAKE_NONMOVABLE(LineBuilder);
16
17public:
18 LineBuilder(InlineFormattingContext&, LayoutState&);
19 ~LineBuilder();
20
21 void break_line(Optional<CSSPixels> next_item_width = {});
22 void append_box(Box const&, CSSPixels leading_size, CSSPixels trailing_size, CSSPixels leading_margin, CSSPixels trailing_margin);
23 void append_text_chunk(TextNode const&, size_t offset_in_node, size_t length_in_node, CSSPixels leading_size, CSSPixels trailing_size, CSSPixels leading_margin, CSSPixels trailing_margin, CSSPixels content_width, CSSPixels content_height);
24
25 // Returns whether a line break occurred.
26 bool break_if_needed(CSSPixels next_item_width)
27 {
28 if (should_break(next_item_width)) {
29 break_line(next_item_width);
30 return true;
31 }
32 return false;
33 }
34
35 CSSPixels available_width_for_current_line() const { return m_available_width_for_current_line; }
36
37 void update_last_line();
38
39 void remove_last_line_if_empty();
40
41 CSSPixels current_y() const { return m_current_y; }
42 void set_current_y(CSSPixels y) { m_current_y = y; }
43
44 void recalculate_available_space();
45 CSSPixels y_for_float_to_be_inserted_here(Box const&);
46
47private:
48 void begin_new_line(bool increment_y, bool is_first_break_in_sequence = true);
49
50 bool should_break(CSSPixels next_item_width);
51
52 LineBox& ensure_last_line_box();
53
54 InlineFormattingContext& m_context;
55 LayoutState& m_layout_state;
56 LayoutState::UsedValues& m_containing_block_state;
57 CSSPixels m_available_width_for_current_line { 0 };
58 CSSPixels m_current_y { 0 };
59 CSSPixels m_max_height_on_current_line { 0 };
60
61 bool m_last_line_needs_update { false };
62};
63
64}