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 <wtf/IsoMalloc.h>
38 class InlineFormattingState;
39 class InlineRunProvider;
41 // This class implements the layout logic for inline formatting contexts.
42 // https://www.w3.org/TR/CSS22/visuren.html#inline-formatting
43 class InlineFormattingContext : public FormattingContext {
44 WTF_MAKE_ISO_ALLOCATED(InlineFormattingContext);
46 InlineFormattingContext(const Box& formattingContextRoot);
48 void layout(LayoutContext&, FormattingState&) const override;
53 Line(InlineFormattingState&, const Box& formattingRoot);
55 void init(const Display::Box::Rect&);
57 void appendContent(const InlineLineBreaker::Run&);
59 void adjustLogicalLeft(LayoutUnit delta);
60 void adjustLogicalRight(LayoutUnit delta);
62 enum class LastLine { No, Yes };
63 void close(LastLine = LastLine::No);
65 bool hasContent() const { return m_firstRunIndex.has_value(); }
66 bool isFirstLine() const { return m_isFirstLine; }
67 LayoutUnit contentLogicalRight();
68 LayoutUnit availableWidth() const { return m_availableWidth; }
70 LayoutUnit logicalTop() const { return m_logicalRect.top(); }
71 LayoutUnit logicalBottom() const { return m_logicalRect.bottom(); }
75 void computeExpansionOpportunities(const InlineLineBreaker::Run&);
77 struct TrailingTrimmableContent {
81 std::optional<TrailingTrimmableContent> m_trailingTrimmableContent;
82 bool m_lastRunIsWhitespace { false };
83 bool m_lastRunCanExpand { false };
85 InlineFormattingState& m_formattingState;
86 const Box& m_formattingRoot;
88 Display::Box::Rect m_logicalRect;
89 LayoutUnit m_availableWidth;
91 std::optional<unsigned> m_firstRunIndex;
92 bool m_alignmentIsJustify { false };
93 bool m_isFirstLine { true };
96 void layoutInlineContent(const LayoutContext&, InlineFormattingState&, const InlineRunProvider&) const;
97 void initializeNewLine(const LayoutContext&, InlineFormattingState&, Line&) const;
99 void computeWidthAndHeight(LayoutContext&, const Box&) const;
100 void computeFloatPosition(const LayoutContext&, const FloatingContext&, Line&, const Box&) const;
101 void computeStaticPosition(const LayoutContext&, const Box&) const override;
102 void computeInFlowPositionedPosition(const LayoutContext&, const Box&) const override;
104 InstrinsicWidthConstraints instrinsicWidthConstraints(LayoutContext&, const Box&) const override;