| 1 | /* |
| 2 | * Copyright (C) 2018 Apple Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 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. |
| 12 | * |
| 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. |
| 24 | */ |
| 25 | |
| 26 | #pragma once |
| 27 | |
| 28 | #if ENABLE(LAYOUT_FORMATTING_CONTEXT) |
| 29 | |
| 30 | #include "DisplayBox.h" |
| 31 | #include "FormattingContext.h" |
| 32 | #include "InlineFormattingState.h" |
| 33 | #include "InlineRun.h" |
| 34 | #include <wtf/IsoMalloc.h> |
| 35 | |
| 36 | namespace WebCore { |
| 37 | namespace Layout { |
| 38 | |
| 39 | class FloatingState; |
| 40 | class InlineContainer; |
| 41 | class InlineRunProvider; |
| 42 | class Line; |
| 43 | |
| 44 | // This class implements the layout logic for inline formatting contexts. |
| 45 | // https://www.w3.org/TR/CSS22/visuren.html#inline-formatting |
| 46 | class InlineFormattingContext : public FormattingContext { |
| 47 | WTF_MAKE_ISO_ALLOCATED(InlineFormattingContext); |
| 48 | public: |
| 49 | InlineFormattingContext(const Box& formattingContextRoot, InlineFormattingState&); |
| 50 | void layout() const override; |
| 51 | |
| 52 | private: |
| 53 | void computeIntrinsicWidthConstraints() const override; |
| 54 | |
| 55 | class LineLayout { |
| 56 | public: |
| 57 | LineLayout(const InlineFormattingContext&); |
| 58 | void layout(const InlineRunProvider&) const; |
| 59 | |
| 60 | private: |
| 61 | enum class IsLastLine { No, Yes }; |
| 62 | void initializeNewLine(Line&) const; |
| 63 | void closeLine(Line&, IsLastLine) const; |
| 64 | void appendContentToLine(Line&, const InlineRunProvider::Run&, const LayoutSize&) const; |
| 65 | void postProcessInlineRuns(Line&, IsLastLine) const; |
| 66 | void createFinalRuns(Line&) const; |
| 67 | void splitInlineRunIfNeeded(const InlineRun&, InlineRuns& splitRuns) const; |
| 68 | void computeFloatPosition(const FloatingContext&, Line&, const Box&) const; |
| 69 | void placeInFlowPositionedChildren(unsigned firstRunIndex) const; |
| 70 | void alignRuns(TextAlignMode, Line&, IsLastLine) const; |
| 71 | void computeExpansionOpportunities(Line&, const InlineRunProvider::Run&, InlineRunProvider::Run::Type lastRunType) const; |
| 72 | LayoutUnit runWidth(const InlineContent&, const InlineItem&, ItemPosition from, unsigned length, LayoutUnit contentLogicalLeft) const; |
| 73 | |
| 74 | private: |
| 75 | static void justifyRuns(Line&); |
| 76 | |
| 77 | private: |
| 78 | const InlineFormattingContext& m_formattingContext; |
| 79 | InlineFormattingState& m_formattingState; |
| 80 | FloatingState& m_floatingState; |
| 81 | const Container& m_formattingRoot; |
| 82 | }; |
| 83 | |
| 84 | class Geometry : public FormattingContext::Geometry { |
| 85 | public: |
| 86 | static HeightAndMargin inlineBlockHeightAndMargin(const LayoutState&, const Box&); |
| 87 | static WidthAndMargin inlineBlockWidthAndMargin(LayoutState&, const Box&, UsedHorizontalValues); |
| 88 | }; |
| 89 | |
| 90 | void layoutFormattingContextRoot(const Box&, UsedHorizontalValues) const; |
| 91 | void computeIntrinsicWidthForFloatBox(const Box&) const; |
| 92 | void computeIntrinsicWidthForInlineBlock(const Box&) const; |
| 93 | void computeWidthAndHeightForReplacedInlineBox(const Box&, UsedHorizontalValues) const; |
| 94 | void computeMargin(const Box&, UsedHorizontalValues) const; |
| 95 | void computeHeightAndMargin(const Box&) const; |
| 96 | void computeWidthAndMargin(const Box&, UsedHorizontalValues) const; |
| 97 | |
| 98 | void collectInlineContent(InlineRunProvider&) const; |
| 99 | |
| 100 | InlineFormattingState& formattingState() const { return downcast<InlineFormattingState>(FormattingContext::formattingState()); } |
| 101 | }; |
| 102 | |
| 103 | } |
| 104 | } |
| 105 | #endif |
| 106 | |