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, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All rights reserved.
6 * Copyright (C) Research In Motion Limited 2010. All rights reserved.
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.
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.
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.
26 #include "PODIntervalTree.h"
27 #include "RootInlineBox.h"
28 #include <wtf/ListHashSet.h>
32 class RenderBlockFlow;
35 class FloatingObject {
36 WTF_MAKE_NONCOPYABLE(FloatingObject); WTF_MAKE_FAST_ALLOCATED;
38 // Note that Type uses bits so you can use FloatLeftRight as a mask to query for both left and right.
39 enum Type { FloatLeft = 1, FloatRight = 2, FloatLeftRight = 3 };
41 static std::unique_ptr<FloatingObject> create(RenderBox&);
42 std::unique_ptr<FloatingObject> copyToNewContainer(LayoutSize, bool shouldPaint = false, bool isDescendant = false) const;
43 std::unique_ptr<FloatingObject> cloneForNewParent() const;
45 explicit FloatingObject(RenderBox&);
46 FloatingObject(RenderBox&, Type, const LayoutRect&, const LayoutSize&, bool shouldPaint, bool isDescendant);
48 Type type() const { return static_cast<Type>(m_type); }
49 RenderBox& renderer() const { return m_renderer; }
51 bool isPlaced() const { return m_isPlaced; }
52 void setIsPlaced(bool placed = true) { m_isPlaced = placed; }
54 LayoutUnit x() const { ASSERT(isPlaced()); return m_frameRect.x(); }
55 LayoutUnit maxX() const { ASSERT(isPlaced()); return m_frameRect.maxX(); }
56 LayoutUnit y() const { ASSERT(isPlaced()); return m_frameRect.y(); }
57 LayoutUnit maxY() const { ASSERT(isPlaced()); return m_frameRect.maxY(); }
58 LayoutUnit width() const { return m_frameRect.width(); }
59 LayoutUnit height() const { return m_frameRect.height(); }
61 void setX(LayoutUnit x) { ASSERT(!isInPlacedTree()); m_frameRect.setX(x); }
62 void setY(LayoutUnit y) { ASSERT(!isInPlacedTree()); m_frameRect.setY(y); }
63 void setWidth(LayoutUnit width) { ASSERT(!isInPlacedTree()); m_frameRect.setWidth(width); }
64 void setHeight(LayoutUnit height) { ASSERT(!isInPlacedTree()); m_frameRect.setHeight(height); }
66 void setMarginOffset(LayoutSize offset) { ASSERT(!isInPlacedTree()); m_marginOffset = offset; }
68 const LayoutRect& frameRect() const { ASSERT(isPlaced()); return m_frameRect; }
69 void setFrameRect(const LayoutRect& frameRect) { ASSERT(!isInPlacedTree()); m_frameRect = frameRect; }
71 LayoutUnit paginationStrut() const { return m_paginationStrut; }
72 void setPaginationStrut(LayoutUnit strut) { m_paginationStrut = strut; }
75 bool isInPlacedTree() const { return m_isInPlacedTree; }
76 void setIsInPlacedTree(bool value) { m_isInPlacedTree = value; }
79 bool shouldPaint() const { return m_shouldPaint; }
80 void setShouldPaint(bool shouldPaint) { m_shouldPaint = shouldPaint; }
81 bool isDescendant() const { return m_isDescendant; }
82 void setIsDescendant(bool isDescendant) { m_isDescendant = isDescendant; }
84 // FIXME: Callers of these methods are dangerous and should be whitelisted explicitly or removed.
85 RootInlineBox* originatingLine() const { return m_originatingLine; }
86 void setOriginatingLine(RootInlineBox* line) { m_originatingLine = line; }
88 LayoutSize locationOffsetOfBorderBox() const
91 return LayoutSize(m_frameRect.location().x() + m_marginOffset.width(), m_frameRect.location().y() + m_marginOffset.height());
93 LayoutSize marginOffset() const { ASSERT(isPlaced()); return m_marginOffset; }
94 LayoutSize translationOffsetToAncestor() const;
97 RenderBox& m_renderer;
98 RootInlineBox* m_originatingLine { nullptr };
99 LayoutRect m_frameRect;
100 LayoutUnit m_paginationStrut;
101 LayoutSize m_marginOffset;
103 unsigned m_type : 2; // Type (left or right aligned)
104 unsigned m_shouldPaint : 1;
105 unsigned m_isDescendant : 1;
106 unsigned m_isPlaced : 1;
108 unsigned m_isInPlacedTree : 1;
112 struct FloatingObjectHashFunctions {
113 static unsigned hash(const std::unique_ptr<FloatingObject>& key) { return PtrHash<RenderBox*>::hash(&key->renderer()); }
114 static bool equal(const std::unique_ptr<FloatingObject>& a, const std::unique_ptr<FloatingObject>& b) { return &a->renderer() == &b->renderer(); }
115 static const bool safeToCompareToEmptyOrDeleted = true;
117 struct FloatingObjectHashTranslator {
118 static unsigned hash(const RenderBox& key) { return PtrHash<const RenderBox*>::hash(&key); }
119 static unsigned hash(const FloatingObject& key) { return PtrHash<RenderBox*>::hash(&key.renderer()); }
120 static bool equal(const std::unique_ptr<FloatingObject>& a, const RenderBox& b) { return &a->renderer() == &b; }
121 static bool equal(const std::unique_ptr<FloatingObject>& a, const FloatingObject& b) { return &a->renderer() == &b.renderer(); }
124 typedef ListHashSet<std::unique_ptr<FloatingObject>, FloatingObjectHashFunctions> FloatingObjectSet;
126 typedef PODInterval<LayoutUnit, FloatingObject*> FloatingObjectInterval;
127 typedef PODIntervalTree<LayoutUnit, FloatingObject*> FloatingObjectTree;
129 // FIXME: This is really the same thing as FloatingObjectSet.
130 // Change clients to use that set directly, and replace the moveAllToFloatInfoMap function with a takeSet function.
131 typedef HashMap<RenderBox*, std::unique_ptr<FloatingObject>> RendererToFloatInfoMap;
133 class FloatingObjects {
134 WTF_MAKE_NONCOPYABLE(FloatingObjects); WTF_MAKE_FAST_ALLOCATED;
136 explicit FloatingObjects(const RenderBlockFlow&);
140 void moveAllToFloatInfoMap(RendererToFloatInfoMap&);
141 FloatingObject* add(std::unique_ptr<FloatingObject>);
142 void remove(FloatingObject*);
143 void addPlacedObject(FloatingObject*);
144 void removePlacedObject(FloatingObject*);
145 void setHorizontalWritingMode(bool b = true) { m_horizontalWritingMode = b; }
147 bool hasLeftObjects() const { return m_leftObjectsCount > 0; }
148 bool hasRightObjects() const { return m_rightObjectsCount > 0; }
149 const FloatingObjectSet& set() const { return m_set; }
150 void clearLineBoxTreePointers();
152 LayoutUnit logicalLeftOffset(LayoutUnit fixedOffset, LayoutUnit logicalTop, LayoutUnit logicalHeight);
153 LayoutUnit logicalRightOffset(LayoutUnit fixedOffset, LayoutUnit logicalTop, LayoutUnit logicalHeight);
155 LayoutUnit logicalLeftOffsetForPositioningFloat(LayoutUnit fixedOffset, LayoutUnit logicalTop, LayoutUnit* heightRemaining);
156 LayoutUnit logicalRightOffsetForPositioningFloat(LayoutUnit fixedOffset, LayoutUnit logicalTop, LayoutUnit* heightRemaining);
158 LayoutUnit findNextFloatLogicalBottomBelow(LayoutUnit logicalHeight);
159 LayoutUnit findNextFloatLogicalBottomBelowForBlock(LayoutUnit logicalHeight);
162 void computePlacedFloatsTree();
163 const FloatingObjectTree* placedFloatsTree();
164 void increaseObjectsCount(FloatingObject::Type);
165 void decreaseObjectsCount(FloatingObject::Type);
166 FloatingObjectInterval intervalForFloatingObject(FloatingObject*);
168 FloatingObjectSet m_set;
169 std::unique_ptr<FloatingObjectTree> m_placedFloatsTree;
170 unsigned m_leftObjectsCount;
171 unsigned m_rightObjectsCount;
172 bool m_horizontalWritingMode;
173 const RenderBlockFlow& m_renderer;
176 } // namespace WebCore
181 // This helper is used by PODIntervalTree for debugging purposes.
182 template<> struct ValueToString<WebCore::FloatingObject*> {
183 static String string(const WebCore::FloatingObject* floatingObject)
185 return String::format("%p (%ix%i %ix%i)", floatingObject, floatingObject->frameRect().x().toInt(), floatingObject->frameRect().y().toInt(), floatingObject->frameRect().maxX().toInt(), floatingObject->frameRect().maxY().toInt());