1 | /* |
2 | * Copyright (C) 1999 Lars Knoll (knoll@kde.org) |
3 | * (C) 1999 Antti Koivisto (koivisto@kde.org) |
4 | * (C) 2007 David Smith (catfish.man@gmail.com) |
5 | * Copyright (C) 2003-2019 Apple Inc. All rights reserved. |
6 | * Copyright (C) Research In Motion Limited 2010. All rights reserved. |
7 | * |
8 | * This library is free software; you can redistribute it and/or |
9 | * modify it under the terms of the GNU Library General Public |
10 | * License as published by the Free Software Foundation; either |
11 | * version 2 of the License, or (at your option) any later version. |
12 | * |
13 | * This library is distributed in the hope that it will be useful, |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
16 | * Library General Public License for more details. |
17 | * |
18 | * You should have received a copy of the GNU Library General Public License |
19 | * along with this library; see the file COPYING.LIB. If not, write to |
20 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
21 | * Boston, MA 02110-1301, USA. |
22 | */ |
23 | |
24 | #pragma once |
25 | |
26 | #include "PODIntervalTree.h" |
27 | #include "RootInlineBox.h" |
28 | #include <wtf/ListHashSet.h> |
29 | #include <wtf/WeakPtr.h> |
30 | |
31 | namespace WebCore { |
32 | |
33 | class RenderBlockFlow; |
34 | class RenderBox; |
35 | |
36 | class FloatingObject { |
37 | WTF_MAKE_NONCOPYABLE(FloatingObject); WTF_MAKE_FAST_ALLOCATED; |
38 | public: |
39 | // Note that Type uses bits so you can use FloatLeftRight as a mask to query for both left and right. |
40 | enum Type { FloatLeft = 1, FloatRight = 2, FloatLeftRight = 3 }; |
41 | |
42 | static std::unique_ptr<FloatingObject> create(RenderBox&); |
43 | std::unique_ptr<FloatingObject> copyToNewContainer(LayoutSize, bool shouldPaint = false, bool isDescendant = false) const; |
44 | std::unique_ptr<FloatingObject> cloneForNewParent() const; |
45 | |
46 | explicit FloatingObject(RenderBox&); |
47 | FloatingObject(RenderBox&, Type, const LayoutRect&, const LayoutSize&, bool shouldPaint, bool isDescendant); |
48 | |
49 | Type type() const { return static_cast<Type>(m_type); } |
50 | RenderBox& renderer() const { return *m_renderer; } |
51 | |
52 | bool isPlaced() const { return m_isPlaced; } |
53 | void setIsPlaced(bool placed = true) { m_isPlaced = placed; } |
54 | |
55 | LayoutUnit x() const { ASSERT(isPlaced()); return m_frameRect.x(); } |
56 | LayoutUnit maxX() const { ASSERT(isPlaced()); return m_frameRect.maxX(); } |
57 | LayoutUnit y() const { ASSERT(isPlaced()); return m_frameRect.y(); } |
58 | LayoutUnit maxY() const { ASSERT(isPlaced()); return m_frameRect.maxY(); } |
59 | LayoutUnit width() const { return m_frameRect.width(); } |
60 | LayoutUnit height() const { return m_frameRect.height(); } |
61 | |
62 | void setX(LayoutUnit x) { ASSERT(!isInPlacedTree()); m_frameRect.setX(x); } |
63 | void setY(LayoutUnit y) { ASSERT(!isInPlacedTree()); m_frameRect.setY(y); } |
64 | void setWidth(LayoutUnit width) { ASSERT(!isInPlacedTree()); m_frameRect.setWidth(width); } |
65 | void setHeight(LayoutUnit height) { ASSERT(!isInPlacedTree()); m_frameRect.setHeight(height); } |
66 | |
67 | void setMarginOffset(LayoutSize offset) { ASSERT(!isInPlacedTree()); m_marginOffset = offset; } |
68 | |
69 | const LayoutRect& frameRect() const { ASSERT(isPlaced()); return m_frameRect; } |
70 | void setFrameRect(const LayoutRect& frameRect) { ASSERT(!isInPlacedTree()); m_frameRect = frameRect; } |
71 | |
72 | LayoutUnit () const { return m_paginationStrut; } |
73 | void (LayoutUnit strut) { m_paginationStrut = strut; } |
74 | |
75 | #ifndef NDEBUG |
76 | bool isInPlacedTree() const { return m_isInPlacedTree; } |
77 | void setIsInPlacedTree(bool value) { m_isInPlacedTree = value; } |
78 | #endif |
79 | |
80 | bool shouldPaint() const { return m_shouldPaint; } |
81 | void setShouldPaint(bool shouldPaint) { m_shouldPaint = shouldPaint; } |
82 | bool isDescendant() const { return m_isDescendant; } |
83 | void setIsDescendant(bool isDescendant) { m_isDescendant = isDescendant; } |
84 | |
85 | // FIXME: Callers of these methods are dangerous and should be whitelisted explicitly or removed. |
86 | RootInlineBox* originatingLine() const { return m_originatingLine.get(); } |
87 | void clearOriginatingLine() { m_originatingLine = nullptr; } |
88 | void setOriginatingLine(RootInlineBox& line) { m_originatingLine = makeWeakPtr(line); } |
89 | |
90 | LayoutSize locationOffsetOfBorderBox() const |
91 | { |
92 | ASSERT(isPlaced()); |
93 | return LayoutSize(m_frameRect.location().x() + m_marginOffset.width(), m_frameRect.location().y() + m_marginOffset.height()); |
94 | } |
95 | LayoutSize marginOffset() const { ASSERT(isPlaced()); return m_marginOffset; } |
96 | LayoutSize translationOffsetToAncestor() const; |
97 | |
98 | String debugString() const; |
99 | |
100 | private: |
101 | WeakPtr<RenderBox> m_renderer; |
102 | WeakPtr<RootInlineBox> m_originatingLine; |
103 | LayoutRect m_frameRect; |
104 | LayoutUnit ; |
105 | LayoutSize m_marginOffset; |
106 | |
107 | unsigned m_type : 2; // Type (left or right aligned) |
108 | unsigned m_shouldPaint : 1; |
109 | unsigned m_isDescendant : 1; |
110 | unsigned m_isPlaced : 1; |
111 | #ifndef NDEBUG |
112 | unsigned m_isInPlacedTree : 1; |
113 | #endif |
114 | }; |
115 | |
116 | // FIXME: This could be simplified if we made it inherit from PtrHash<std::unique_ptr<FloatingObject>> and |
117 | // changed PtrHashBase to have all of its hash and equal functions bottleneck through single functions (as |
118 | // is done here). That would allow us to only override those master hash and equal functions. |
119 | struct FloatingObjectHashFunctions { |
120 | typedef std::unique_ptr<FloatingObject> T; |
121 | typedef typename WTF::GetPtrHelper<T>::PtrType PtrType; |
122 | |
123 | static unsigned hash(PtrType key) { return PtrHash<RenderBox*>::hash(&key->renderer()); } |
124 | static bool equal(PtrType a, PtrType b) { return &a->renderer() == &b->renderer(); } |
125 | static const bool safeToCompareToEmptyOrDeleted = true; |
126 | |
127 | static unsigned hash(const T& key) { return hash(WTF::getPtr(key)); } |
128 | static bool equal(const T& a, const T& b) { return equal(WTF::getPtr(a), WTF::getPtr(b)); } |
129 | static bool equal(PtrType a, const T& b) { return equal(a, WTF::getPtr(b)); } |
130 | static bool equal(const T& a, PtrType b) { return equal(WTF::getPtr(a), b); } |
131 | }; |
132 | struct FloatingObjectHashTranslator { |
133 | static unsigned hash(const RenderBox& key) { return PtrHash<const RenderBox*>::hash(&key); } |
134 | static bool equal(const std::unique_ptr<FloatingObject>& a, const RenderBox& b) { return &a->renderer() == &b; } |
135 | }; |
136 | |
137 | typedef ListHashSet<std::unique_ptr<FloatingObject>, FloatingObjectHashFunctions> FloatingObjectSet; |
138 | |
139 | typedef PODInterval<LayoutUnit, FloatingObject*> FloatingObjectInterval; |
140 | typedef PODIntervalTree<LayoutUnit, FloatingObject*> FloatingObjectTree; |
141 | |
142 | // FIXME: This is really the same thing as FloatingObjectSet. |
143 | // Change clients to use that set directly, and replace the moveAllToFloatInfoMap function with a takeSet function. |
144 | typedef HashMap<RenderBox*, std::unique_ptr<FloatingObject>> RendererToFloatInfoMap; |
145 | |
146 | class FloatingObjects { |
147 | WTF_MAKE_NONCOPYABLE(FloatingObjects); WTF_MAKE_FAST_ALLOCATED; |
148 | public: |
149 | explicit FloatingObjects(const RenderBlockFlow&); |
150 | ~FloatingObjects(); |
151 | |
152 | void clear(); |
153 | void moveAllToFloatInfoMap(RendererToFloatInfoMap&); |
154 | FloatingObject* add(std::unique_ptr<FloatingObject>); |
155 | void remove(FloatingObject*); |
156 | void addPlacedObject(FloatingObject*); |
157 | void removePlacedObject(FloatingObject*); |
158 | void setHorizontalWritingMode(bool b = true) { m_horizontalWritingMode = b; } |
159 | |
160 | bool hasLeftObjects() const { return m_leftObjectsCount > 0; } |
161 | bool hasRightObjects() const { return m_rightObjectsCount > 0; } |
162 | const FloatingObjectSet& set() const { return m_set; } |
163 | void clearLineBoxTreePointers(); |
164 | |
165 | LayoutUnit logicalLeftOffset(LayoutUnit fixedOffset, LayoutUnit logicalTop, LayoutUnit logicalHeight); |
166 | LayoutUnit logicalRightOffset(LayoutUnit fixedOffset, LayoutUnit logicalTop, LayoutUnit logicalHeight); |
167 | |
168 | LayoutUnit logicalLeftOffsetForPositioningFloat(LayoutUnit fixedOffset, LayoutUnit logicalTop, LayoutUnit* heightRemaining); |
169 | LayoutUnit logicalRightOffsetForPositioningFloat(LayoutUnit fixedOffset, LayoutUnit logicalTop, LayoutUnit* heightRemaining); |
170 | |
171 | LayoutUnit findNextFloatLogicalBottomBelow(LayoutUnit logicalHeight); |
172 | LayoutUnit findNextFloatLogicalBottomBelowForBlock(LayoutUnit logicalHeight); |
173 | |
174 | private: |
175 | const RenderBlockFlow& renderer() const { return *m_renderer; } |
176 | void computePlacedFloatsTree(); |
177 | const FloatingObjectTree* placedFloatsTree(); |
178 | void increaseObjectsCount(FloatingObject::Type); |
179 | void decreaseObjectsCount(FloatingObject::Type); |
180 | FloatingObjectInterval intervalForFloatingObject(FloatingObject*); |
181 | |
182 | FloatingObjectSet m_set; |
183 | std::unique_ptr<FloatingObjectTree> m_placedFloatsTree; |
184 | unsigned m_leftObjectsCount; |
185 | unsigned m_rightObjectsCount; |
186 | bool m_horizontalWritingMode; |
187 | WeakPtr<const RenderBlockFlow> m_renderer; |
188 | }; |
189 | |
190 | } // namespace WebCore |
191 | |
192 | #ifndef NDEBUG |
193 | |
194 | namespace WTF { |
195 | |
196 | // This helper is used by PODIntervalTree for debugging purposes. |
197 | template<> struct ValueToString<WebCore::FloatingObject*> { |
198 | static String string(const WebCore::FloatingObject* floatingObject) { return floatingObject->debugString(); } |
199 | }; |
200 | |
201 | } // namespace WTF |
202 | |
203 | #endif |
204 | |