mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-10-19 15:43:20 +00:00

Previously, we were collapsing whitespace in Layout::TextNode and then passed the resulting string for further processing through ChunkIterator -> InlineLevelIterator -> InlineFormattingContext -> LineBuilder -> LineBoxFragment -> PaintableFragment. Our painting tree is where we deal with things like range offsets into the underlying text nodes, but since we modified the original string, the offsets were wrong. This changes the way we generate fragments: * Layout::TextNode no longer collapses whitespace as part of its stored "text for rendering", but moves this logic to ChunkIterator which splits up this text into separate views whenever whitespace needs to be collapsed. * Layout::LineBox now only extends the last fragment if its end offset is equal to the new fragment's start offset. Otherwise, there's a gap caused by collapsing whitespace and we need to generate a separate fragment for that in order to have a correct start offset. Some tests need new baselines because of the fixed start offsets. Fixes #566.
105 lines
3.5 KiB
C++
105 lines
3.5 KiB
C++
/*
|
|
* Copyright (c) 2022, Andreas Kling <andreas@ladybird.org>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <AK/Noncopyable.h>
|
|
#include <LibWeb/Layout/BlockContainer.h>
|
|
#include <LibWeb/Layout/LayoutState.h>
|
|
#include <LibWeb/Layout/TextNode.h>
|
|
|
|
namespace Web::Layout {
|
|
|
|
// This class iterates over all the inline-level objects within an inline formatting context.
|
|
// By repeatedly calling next() with the remaining available width on the current line,
|
|
// it returns an "Item" representing the next piece of inline-level content to be placed on the line.
|
|
class InlineLevelIterator {
|
|
AK_MAKE_NONCOPYABLE(InlineLevelIterator);
|
|
AK_MAKE_NONMOVABLE(InlineLevelIterator);
|
|
|
|
public:
|
|
struct Item {
|
|
enum class Type {
|
|
Text,
|
|
Element,
|
|
ForcedBreak,
|
|
AbsolutelyPositionedElement,
|
|
FloatingElement,
|
|
};
|
|
Type type {};
|
|
GC::Ptr<Layout::Node const> node {};
|
|
RefPtr<Gfx::GlyphRun> glyph_run {};
|
|
size_t offset_in_node { 0 };
|
|
size_t length_in_node { 0 };
|
|
CSSPixels width { 0.0f };
|
|
CSSPixels padding_start { 0.0f };
|
|
CSSPixels padding_end { 0.0f };
|
|
CSSPixels border_start { 0.0f };
|
|
CSSPixels border_end { 0.0f };
|
|
CSSPixels margin_start { 0.0f };
|
|
CSSPixels margin_end { 0.0f };
|
|
bool is_collapsible_whitespace { false };
|
|
|
|
CSSPixels border_box_width() const
|
|
{
|
|
return border_start + padding_start + width + padding_end + border_end;
|
|
}
|
|
};
|
|
|
|
InlineLevelIterator(Layout::InlineFormattingContext&, LayoutState&, Layout::BlockContainer const& containing_block, LayoutState::UsedValues const& containing_block_used_values, LayoutMode);
|
|
|
|
Optional<Item> next();
|
|
CSSPixels next_non_whitespace_sequence_width();
|
|
|
|
private:
|
|
Optional<Item> next_without_lookahead();
|
|
Gfx::GlyphRun::TextType resolve_text_direction_from_context();
|
|
void skip_to_next();
|
|
void compute_next();
|
|
|
|
void enter_text_node(Layout::TextNode const&);
|
|
|
|
void enter_node_with_box_model_metrics(Layout::NodeWithStyleAndBoxModelMetrics const&);
|
|
void exit_node_with_box_model_metrics();
|
|
|
|
void add_extra_box_model_metrics_to_item(Item&, bool add_leading_metrics, bool add_trailing_metrics);
|
|
|
|
HashMap<StringView, u8> shape_features_map() const;
|
|
Gfx::ShapeFeatures create_and_merge_font_features() const;
|
|
|
|
Layout::Node const* next_inline_node_in_pre_order(Layout::Node const& current, Layout::Node const* stay_within);
|
|
|
|
Layout::InlineFormattingContext& m_inline_formatting_context;
|
|
Layout::LayoutState& m_layout_state;
|
|
GC::Ref<BlockContainer const> m_containing_block;
|
|
LayoutState::UsedValues const& m_containing_block_used_values;
|
|
GC::Ptr<Layout::Node const> m_current_node;
|
|
GC::Ptr<Layout::Node const> m_next_node;
|
|
LayoutMode const m_layout_mode;
|
|
|
|
struct TextNodeContext {
|
|
bool is_first_chunk {};
|
|
bool is_last_chunk {};
|
|
TextNode::ChunkIterator chunk_iterator;
|
|
Optional<Gfx::GlyphRun::TextType> last_known_direction {};
|
|
};
|
|
|
|
Optional<TextNodeContext> m_text_node_context;
|
|
|
|
struct ExtraBoxMetrics {
|
|
CSSPixels margin { 0 };
|
|
CSSPixels border { 0 };
|
|
CSSPixels padding { 0 };
|
|
};
|
|
|
|
Optional<ExtraBoxMetrics> m_extra_leading_metrics;
|
|
Optional<ExtraBoxMetrics> m_extra_trailing_metrics;
|
|
|
|
Vector<GC::Ref<NodeWithStyleAndBoxModelMetrics const>> m_box_model_node_stack;
|
|
Queue<InlineLevelIterator::Item> m_lookahead_items;
|
|
};
|
|
|
|
}
|