|
| 1 | +/* |
| 2 | + * Copyright (c) 2022, Andreas Kling <kling@serenityos.org> |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: BSD-2-Clause |
| 5 | + */ |
| 6 | + |
| 7 | +#include <LibWeb/Layout/LineBuilder.h> |
| 8 | +#include <LibWeb/Layout/TextNode.h> |
| 9 | + |
| 10 | +namespace Web::Layout { |
| 11 | + |
| 12 | +LineBuilder::LineBuilder(InlineFormattingContext& context) |
| 13 | + : m_context(context) |
| 14 | +{ |
| 15 | +} |
| 16 | + |
| 17 | +LineBuilder::~LineBuilder() |
| 18 | +{ |
| 19 | + update_last_line(); |
| 20 | +} |
| 21 | + |
| 22 | +void LineBuilder::break_line() |
| 23 | +{ |
| 24 | + update_last_line(); |
| 25 | + m_context.containing_block().line_boxes().append(LineBox()); |
| 26 | + begin_new_line(); |
| 27 | +} |
| 28 | + |
| 29 | +void LineBuilder::begin_new_line() |
| 30 | +{ |
| 31 | + m_current_y += m_max_height_on_current_line; |
| 32 | + auto space = m_context.available_space_for_line(m_current_y); |
| 33 | + m_available_width_for_current_line = space.right - space.left; |
| 34 | + m_max_height_on_current_line = 0; |
| 35 | +} |
| 36 | + |
| 37 | +void LineBuilder::append_box(Box& box) |
| 38 | +{ |
| 39 | + m_context.containing_block().line_boxes().last().add_fragment(box, 0, 0, box.width(), box.height()); |
| 40 | + m_max_height_on_current_line = max(m_max_height_on_current_line, box.height()); |
| 41 | +} |
| 42 | + |
| 43 | +void LineBuilder::append_text_chunk(TextNode& text_node, size_t offset_in_node, size_t length_in_node, float width, float height) |
| 44 | +{ |
| 45 | + m_context.containing_block().line_boxes().last().add_fragment(text_node, offset_in_node, length_in_node, width, height); |
| 46 | + m_max_height_on_current_line = max(m_max_height_on_current_line, height); |
| 47 | +} |
| 48 | + |
| 49 | +void LineBuilder::break_if_needed(LayoutMode layout_mode, float next_item_width) |
| 50 | +{ |
| 51 | + if (layout_mode == LayoutMode::AllPossibleLineBreaks |
| 52 | + || (m_context.containing_block().line_boxes().last().width() + next_item_width) > m_available_width_for_current_line) |
| 53 | + break_line(); |
| 54 | +} |
| 55 | + |
| 56 | +void LineBuilder::update_last_line() |
| 57 | +{ |
| 58 | + if (m_context.containing_block().line_boxes().is_empty()) |
| 59 | + return; |
| 60 | + |
| 61 | + auto& line_box = m_context.containing_block().line_boxes().last(); |
| 62 | + |
| 63 | + auto text_align = m_context.containing_block().computed_values().text_align(); |
| 64 | + float x_offset = m_context.available_space_for_line(m_current_y).left; |
| 65 | + |
| 66 | + float excess_horizontal_space = m_context.containing_block().width() - line_box.width(); |
| 67 | + |
| 68 | + switch (text_align) { |
| 69 | + case CSS::TextAlign::Center: |
| 70 | + case CSS::TextAlign::LibwebCenter: |
| 71 | + x_offset += excess_horizontal_space / 2; |
| 72 | + break; |
| 73 | + case CSS::TextAlign::Right: |
| 74 | + x_offset += excess_horizontal_space; |
| 75 | + break; |
| 76 | + case CSS::TextAlign::Left: |
| 77 | + case CSS::TextAlign::Justify: |
| 78 | + default: |
| 79 | + break; |
| 80 | + } |
| 81 | + |
| 82 | + float excess_horizontal_space_including_whitespace = excess_horizontal_space; |
| 83 | + size_t whitespace_count = 0; |
| 84 | + if (text_align == CSS::TextAlign::Justify) { |
| 85 | + for (auto& fragment : line_box.fragments()) { |
| 86 | + if (fragment.is_justifiable_whitespace()) { |
| 87 | + ++whitespace_count; |
| 88 | + excess_horizontal_space_including_whitespace += fragment.width(); |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + float justified_space_width = whitespace_count > 0 ? (excess_horizontal_space_including_whitespace / static_cast<float>(whitespace_count)) : 0; |
| 94 | + |
| 95 | + for (size_t i = 0; i < line_box.fragments().size(); ++i) { |
| 96 | + auto& fragment = line_box.fragments()[i]; |
| 97 | + |
| 98 | + // Vertically align everyone's bottom to the line. |
| 99 | + // FIXME: Support other kinds of vertical alignment. |
| 100 | + fragment.set_offset({ roundf(x_offset + fragment.offset().x()), m_current_y + (m_max_height_on_current_line - fragment.height()) }); |
| 101 | + |
| 102 | + if (text_align == CSS::TextAlign::Justify |
| 103 | + && fragment.is_justifiable_whitespace() |
| 104 | + && fragment.width() != justified_space_width) { |
| 105 | + float diff = justified_space_width - fragment.width(); |
| 106 | + fragment.set_width(justified_space_width); |
| 107 | + // Shift subsequent sibling fragments to the right to adjust for change in width. |
| 108 | + for (size_t j = i + 1; j < line_box.fragments().size(); ++j) { |
| 109 | + auto offset = line_box.fragments()[j].offset(); |
| 110 | + offset.translate_by(diff, 0); |
| 111 | + line_box.fragments()[j].set_offset(offset); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + if (!line_box.fragments().is_empty()) { |
| 117 | + float left_edge = line_box.fragments().first().offset().x(); |
| 118 | + float right_edge = line_box.fragments().last().offset().x() + line_box.fragments().last().width(); |
| 119 | + float final_line_box_width = right_edge - left_edge; |
| 120 | + line_box.m_width = final_line_box_width; |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +} |
0 commit comments