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 "LayoutBox.h"
32#include "LayoutContainer.h"
33#include <wtf/IsoMalloc.h>
34#include <wtf/Ref.h>
35#include <wtf/WeakPtr.h>
36
37namespace WebCore {
38
39namespace Layout {
40
41class FormattingState;
42class LayoutState;
43
44// FloatingState holds the floating boxes per formatting context.
45class FloatingState : public RefCounted<FloatingState> {
46 WTF_MAKE_ISO_ALLOCATED(FloatingState);
47public:
48 static Ref<FloatingState> create(LayoutState& layoutState, const Box& formattingContextRoot) { return adoptRef(*new FloatingState(layoutState, formattingContextRoot)); }
49
50 void append(const Box& layoutBox);
51 void remove(const Box& layoutBox);
52
53 bool isEmpty() const { return m_floats.isEmpty(); }
54
55 const Box& root() const { return *m_formattingContextRoot; }
56
57 Optional<PositionInContextRoot> top(const Box& formattingContextRoot) const;
58 Optional<PositionInContextRoot> leftBottom(const Box& formattingContextRoot) const;
59 Optional<PositionInContextRoot> rightBottom(const Box& formattingContextRoot) const;
60 Optional<PositionInContextRoot> bottom(const Box& formattingContextRoot) const;
61
62 struct Constraints {
63 Optional<PositionInContextRoot> left;
64 Optional<PositionInContextRoot> right;
65 };
66 Constraints constraints(PositionInContextRoot verticalPosition, const Box& formattingContextRoot) const;
67
68 class FloatItem {
69 public:
70 FloatItem(const Box&, const FloatingState&);
71
72 bool operator==(const Box& layoutBox) const { return m_layoutBox.get() == &layoutBox; }
73
74 bool isLeftPositioned() const { return m_layoutBox->isLeftFloatingPositioned(); }
75 bool isDescendantOfFormattingRoot(const Box&) const;
76
77 Display::Box::Rect rectWithMargin() const { return m_absoluteDisplayBox.rectWithMargin(); }
78 PositionInContextRoot bottom() const { return { m_absoluteDisplayBox.bottom() }; }
79
80 private:
81 WeakPtr<const Box> m_layoutBox;
82 Display::Box m_absoluteDisplayBox;
83 };
84 using FloatList = Vector<FloatItem>;
85 const FloatList& floats() const { return m_floats; }
86 const FloatItem* last() const { return isEmpty() ? nullptr : &m_floats.last(); }
87
88private:
89 friend class FloatingContext;
90 FloatingState(LayoutState&, const Box& formattingContextRoot);
91
92 LayoutState& layoutState() const { return m_layoutState; }
93
94 Optional<PositionInContextRoot> bottom(const Box& formattingContextRoot, Clear) const;
95
96 LayoutState& m_layoutState;
97 WeakPtr<const Box> m_formattingContextRoot;
98 FloatList m_floats;
99};
100
101inline Optional<PositionInContextRoot> FloatingState::leftBottom(const Box& formattingContextRoot) const
102{
103 ASSERT(formattingContextRoot.establishesFormattingContext());
104 return bottom(formattingContextRoot, Clear::Left);
105}
106
107inline Optional<PositionInContextRoot> FloatingState::rightBottom(const Box& formattingContextRoot) const
108{
109 ASSERT(formattingContextRoot.establishesFormattingContext());
110 return bottom(formattingContextRoot, Clear::Right);
111}
112
113inline Optional<PositionInContextRoot> FloatingState::bottom(const Box& formattingContextRoot) const
114{
115 ASSERT(formattingContextRoot.establishesFormattingContext());
116 return bottom(formattingContextRoot, Clear::Both);
117}
118
119inline bool FloatingState::FloatItem::isDescendantOfFormattingRoot(const Box& formattingContextRoot) const
120{
121 ASSERT(formattingContextRoot.establishesFormattingContext());
122 if (!is<Container>(formattingContextRoot))
123 return false;
124 return m_layoutBox->isDescendantOf(downcast<Container>(formattingContextRoot));
125}
126
127}
128}
129#endif
130