2 * Copyright (C) 2018 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
28 #if ENABLE(LAYOUT_FORMATTING_CONTEXT)
30 #include "DisplayBox.h"
31 #include "FormattingContext.h"
32 #include "InlineLineBreaker.h"
33 #include "InlineRun.h"
34 #include <wtf/IsoMalloc.h>
39 class InlineFormattingState;
40 class InlineRunProvider;
42 // This class implements the layout logic for inline formatting contexts.
43 // https://www.w3.org/TR/CSS22/visuren.html#inline-formatting
44 class InlineFormattingContext : public FormattingContext {
45 WTF_MAKE_ISO_ALLOCATED(InlineFormattingContext);
47 InlineFormattingContext(const Box& formattingContextRoot, FormattingState&);
49 void layout() const override;
54 Line(InlineFormattingState&);
56 void init(const Display::Box::Rect&);
58 std::optional<unsigned> firstRunIndex;
59 std::optional<unsigned> lastRunIndex;
63 void appendContent(const InlineLineBreaker::Run&);
65 void adjustLogicalLeft(LayoutUnit delta);
66 void adjustLogicalRight(LayoutUnit delta);
68 bool hasContent() const { return m_firstRunIndex.has_value(); }
69 bool isClosed() const { return m_closed; }
70 bool isFirstLine() const { return m_isFirstLine; }
71 LayoutUnit contentLogicalRight() const;
72 LayoutUnit contentLogicalLeft() const { return m_logicalRect.left(); }
73 LayoutUnit availableWidth() const { return m_availableWidth; }
74 std::optional<InlineRunProvider::Run::Type> lastRunType() const { return m_lastRunType; }
76 LayoutUnit logicalTop() const { return m_logicalRect.top(); }
77 LayoutUnit logicalBottom() const { return m_logicalRect.bottom(); }
80 struct TrailingTrimmableContent {
84 std::optional<TrailingTrimmableContent> m_trailingTrimmableContent;
85 std::optional<InlineRunProvider::Run::Type> m_lastRunType;
86 bool m_lastRunCanExpand { false };
88 InlineFormattingState& m_formattingState;
90 Display::Box::Rect m_logicalRect;
91 LayoutUnit m_availableWidth;
93 std::optional<unsigned> m_firstRunIndex;
94 bool m_isFirstLine { true };
95 bool m_closed { true };
97 enum class IsLastLine { No, Yes };
99 class Geometry : public FormattingContext::Geometry {
101 static HeightAndMargin inlineBlockHeightAndMargin(const LayoutState&, const Box&);
102 static WidthAndMargin inlineBlockWidthAndMargin(LayoutState&, const Box&);
103 static void alignRuns(InlineFormattingState&, TextAlignMode, Line&, Line::RunRange, IsLastLine);
104 static void computeExpansionOpportunities(InlineFormattingState&, const InlineRunProvider::Run&, InlineRunProvider::Run::Type lastRunType);
107 static void justifyRuns(InlineFormattingState&, Line&, Line::RunRange);
110 void layoutInlineContent(const InlineRunProvider&) const;
111 void initializeNewLine(Line&) const;
112 void closeLine(Line&, IsLastLine) const;
113 void appendContentToLine(Line&, const InlineLineBreaker::Run&) const;
114 void postProcessInlineRuns(Line&, IsLastLine, Line::RunRange) const;
115 Line::RunRange splitInlineRunsIfNeeded(Line::RunRange) const WARN_UNUSED_RETURN;
116 void splitInlineRunIfNeeded(const InlineRun&, InlineRuns& splitRuns) const;
118 void layoutFormattingContextRoot(const Box&) const;
119 void computeWidthAndHeightForReplacedInlineBox(const Box&) const;
120 void computeHeightAndMargin(const Box&) const;
121 void computeWidthAndMargin(const Box&) const;
122 void computeFloatPosition(const FloatingContext&, Line&, const Box&) const;
123 void placeInFlowPositionedChildren(Line::RunRange) const;
125 void collectInlineContent(InlineRunProvider&) const;
126 void collectInlineContentForSubtree(const Box& root, InlineRunProvider&) const;
127 InstrinsicWidthConstraints instrinsicWidthConstraints() const override;
129 InlineFormattingState& inlineFormattingState() const { return downcast<InlineFormattingState>(formattingState()); }