| 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 <wtf/IsoMalloc.h> |
| 32 | #include <wtf/WeakPtr.h> |
| 33 | |
| 34 | namespace WebCore { |
| 35 | |
| 36 | class LayoutPoint; |
| 37 | class LayoutUnit; |
| 38 | |
| 39 | namespace Layout { |
| 40 | |
| 41 | class Box; |
| 42 | class Container; |
| 43 | class FormattingState; |
| 44 | class LayoutState; |
| 45 | |
| 46 | class FormattingContext { |
| 47 | WTF_MAKE_ISO_ALLOCATED(FormattingContext); |
| 48 | public: |
| 49 | FormattingContext(const Box& formattingContextRoot, FormattingState&); |
| 50 | virtual ~FormattingContext(); |
| 51 | |
| 52 | virtual void layout() const = 0; |
| 53 | void layoutOutOfFlowDescendants(const Box&) const; |
| 54 | |
| 55 | struct IntrinsicWidthConstraints { |
| 56 | LayoutUnit minimum; |
| 57 | LayoutUnit maximum; |
| 58 | }; |
| 59 | virtual void computeIntrinsicWidthConstraints() const = 0; |
| 60 | |
| 61 | static Display::Box mapBoxToAncestor(const LayoutState&, const Box&, const Container& ancestor); |
| 62 | static LayoutUnit mapTopToAncestor(const LayoutState&, const Box&, const Container& ancestor); |
| 63 | static Point mapCoordinateToAncestor(const LayoutState&, Point, const Container& containingBlock, const Container& ancestor); |
| 64 | |
| 65 | protected: |
| 66 | using LayoutQueue = Vector<const Box*>; |
| 67 | |
| 68 | LayoutState& layoutState() const; |
| 69 | FormattingState& formattingState() const { return m_formattingState; } |
| 70 | const Box& root() const { return *m_root; } |
| 71 | |
| 72 | void computeBorderAndPadding(const Box&, Optional<UsedHorizontalValues> = WTF::nullopt) const; |
| 73 | |
| 74 | #ifndef NDEBUG |
| 75 | virtual void validateGeometryConstraintsAfterLayout() const; |
| 76 | #endif |
| 77 | |
| 78 | // This class implements generic positioning and sizing. |
| 79 | class Geometry { |
| 80 | public: |
| 81 | static VerticalGeometry outOfFlowVerticalGeometry(const LayoutState&, const Box&, UsedVerticalValues); |
| 82 | static HorizontalGeometry outOfFlowHorizontalGeometry(LayoutState&, const Box&, UsedHorizontalValues); |
| 83 | |
| 84 | static HeightAndMargin floatingHeightAndMargin(const LayoutState&, const Box&, UsedVerticalValues, UsedHorizontalValues); |
| 85 | static WidthAndMargin floatingWidthAndMargin(LayoutState&, const Box&, UsedHorizontalValues); |
| 86 | |
| 87 | static HeightAndMargin inlineReplacedHeightAndMargin(const LayoutState&, const Box&, UsedVerticalValues); |
| 88 | static WidthAndMargin inlineReplacedWidthAndMargin(const LayoutState&, const Box&, UsedHorizontalValues); |
| 89 | |
| 90 | static LayoutSize inFlowPositionedPositionOffset(const LayoutState&, const Box&); |
| 91 | |
| 92 | static HeightAndMargin complicatedCases(const LayoutState&, const Box&, UsedVerticalValues, UsedHorizontalValues); |
| 93 | static LayoutUnit shrinkToFitWidth(LayoutState&, const Box&, UsedHorizontalValues); |
| 94 | |
| 95 | static Edges computedBorder(const Box&); |
| 96 | static Optional<Edges> computedPadding(const Box&, UsedHorizontalValues); |
| 97 | |
| 98 | static ComputedHorizontalMargin computedHorizontalMargin(const Box&, UsedHorizontalValues); |
| 99 | static ComputedVerticalMargin computedVerticalMargin(const Box&, UsedHorizontalValues); |
| 100 | |
| 101 | static Optional<LayoutUnit> computedValueIfNotAuto(const Length& geometryProperty, LayoutUnit containingBlockWidth); |
| 102 | static Optional<LayoutUnit> fixedValue(const Length& geometryProperty); |
| 103 | |
| 104 | static Optional<LayoutUnit> computedMinHeight(const LayoutState&, const Box&); |
| 105 | static Optional<LayoutUnit> computedMaxHeight(const LayoutState&, const Box&); |
| 106 | |
| 107 | static FormattingContext::IntrinsicWidthConstraints constrainByMinMaxWidth(const Box&, IntrinsicWidthConstraints); |
| 108 | |
| 109 | protected: |
| 110 | enum class HeightType { Min, Max, Normal }; |
| 111 | static Optional<LayoutUnit> computedHeightValue(const LayoutState&, const Box&, HeightType); |
| 112 | |
| 113 | private: |
| 114 | static VerticalGeometry outOfFlowReplacedVerticalGeometry(const LayoutState&, const Box&, UsedVerticalValues); |
| 115 | static HorizontalGeometry outOfFlowReplacedHorizontalGeometry(const LayoutState&, const Box&, UsedHorizontalValues); |
| 116 | |
| 117 | static VerticalGeometry outOfFlowNonReplacedVerticalGeometry(const LayoutState&, const Box&, UsedVerticalValues); |
| 118 | static HorizontalGeometry outOfFlowNonReplacedHorizontalGeometry(LayoutState&, const Box&, UsedHorizontalValues); |
| 119 | |
| 120 | static HeightAndMargin floatingReplacedHeightAndMargin(const LayoutState&, const Box&, UsedVerticalValues); |
| 121 | static WidthAndMargin floatingReplacedWidthAndMargin(const LayoutState&, const Box&, UsedHorizontalValues); |
| 122 | |
| 123 | static WidthAndMargin floatingNonReplacedWidthAndMargin(LayoutState&, const Box&, UsedHorizontalValues); |
| 124 | }; |
| 125 | |
| 126 | class Quirks { |
| 127 | public: |
| 128 | static LayoutUnit heightValueOfNearestContainingBlockWithFixedHeight(const LayoutState&, const Box&); |
| 129 | }; |
| 130 | |
| 131 | private: |
| 132 | void computeOutOfFlowVerticalGeometry(const Box&) const; |
| 133 | void computeOutOfFlowHorizontalGeometry(const Box&) const; |
| 134 | |
| 135 | WeakPtr<const Box> m_root; |
| 136 | FormattingState& m_formattingState; |
| 137 | }; |
| 138 | |
| 139 | } |
| 140 | } |
| 141 | #endif |
| 142 | |