• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 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 #ifndef FloatingObjects_h
25 #define FloatingObjects_h
26 
27 #include "core/rendering/RootInlineBox.h"
28 #include "platform/PODFreeListArena.h"
29 #include "platform/PODIntervalTree.h"
30 #include "wtf/ListHashSet.h"
31 #include "wtf/OwnPtr.h"
32 
33 namespace blink {
34 
35 class RenderBlockFlow;
36 class RenderBox;
37 
38 // FIXME this should be removed once RenderBlockFlow::nextFloatLogicalBottomBelow doesn't need it anymore. (Bug 123931)
39 enum ShapeOutsideFloatOffsetMode { ShapeOutsideFloatShapeOffset, ShapeOutsideFloatMarginBoxOffset };
40 
41 class FloatingObject {
42     WTF_MAKE_NONCOPYABLE(FloatingObject); WTF_MAKE_FAST_ALLOCATED;
43 public:
44 #ifndef NDEBUG
45     // Used by the PODIntervalTree for debugging the FloatingObject.
46     template <class> friend struct ValueToString;
47 #endif
48 
49     // Note that Type uses bits so you can use FloatLeftRight as a mask to query for both left and right.
50     enum Type { FloatLeft = 1, FloatRight = 2, FloatLeftRight = 3 };
51 
52     static PassOwnPtr<FloatingObject> create(RenderBox*);
53 
54     PassOwnPtr<FloatingObject> copyToNewContainer(LayoutSize, bool shouldPaint = false, bool isDescendant = false) const;
55 
56     PassOwnPtr<FloatingObject> unsafeClone() const;
57 
type()58     Type type() const { return static_cast<Type>(m_type); }
renderer()59     RenderBox* renderer() const { return m_renderer; }
60 
isPlaced()61     bool isPlaced() const { return m_isPlaced; }
62     void setIsPlaced(bool placed = true) { m_isPlaced = placed; }
63 
x()64     LayoutUnit x() const { ASSERT(isPlaced()); return m_frameRect.x(); }
maxX()65     LayoutUnit maxX() const { ASSERT(isPlaced()); return m_frameRect.maxX(); }
y()66     LayoutUnit y() const { ASSERT(isPlaced()); return m_frameRect.y(); }
maxY()67     LayoutUnit maxY() const { ASSERT(isPlaced()); return m_frameRect.maxY(); }
width()68     LayoutUnit width() const { return m_frameRect.width(); }
height()69     LayoutUnit height() const { return m_frameRect.height(); }
70 
setX(LayoutUnit x)71     void setX(LayoutUnit x) { ASSERT(!isInPlacedTree()); m_frameRect.setX(x); }
setY(LayoutUnit y)72     void setY(LayoutUnit y) { ASSERT(!isInPlacedTree()); m_frameRect.setY(y); }
setWidth(LayoutUnit width)73     void setWidth(LayoutUnit width) { ASSERT(!isInPlacedTree()); m_frameRect.setWidth(width); }
setHeight(LayoutUnit height)74     void setHeight(LayoutUnit height) { ASSERT(!isInPlacedTree()); m_frameRect.setHeight(height); }
75 
frameRect()76     const LayoutRect& frameRect() const { ASSERT(isPlaced()); return m_frameRect; }
setFrameRect(const LayoutRect & frameRect)77     void setFrameRect(const LayoutRect& frameRect) { ASSERT(!isInPlacedTree()); m_frameRect = frameRect; }
78 
paginationStrut()79     int paginationStrut() const { return m_paginationStrut; }
setPaginationStrut(int strut)80     void setPaginationStrut(int strut) { m_paginationStrut = strut; }
81 
82 #if ENABLE(ASSERT)
isInPlacedTree()83     bool isInPlacedTree() const { return m_isInPlacedTree; }
setIsInPlacedTree(bool value)84     void setIsInPlacedTree(bool value) { m_isInPlacedTree = value; }
85 #endif
86 
shouldPaint()87     bool shouldPaint() const { return m_shouldPaint; }
setShouldPaint(bool shouldPaint)88     void setShouldPaint(bool shouldPaint) { m_shouldPaint = shouldPaint; }
isDescendant()89     bool isDescendant() const { return m_isDescendant; }
setIsDescendant(bool isDescendant)90     void setIsDescendant(bool isDescendant) { m_isDescendant = isDescendant; }
91 
92     // FIXME: Callers of these methods are dangerous and should be whitelisted explicitly or removed.
originatingLine()93     RootInlineBox* originatingLine() const { return m_originatingLine; }
setOriginatingLine(RootInlineBox * line)94     void setOriginatingLine(RootInlineBox* line) { m_originatingLine = line; }
95 
96 private:
97     explicit FloatingObject(RenderBox*);
98     FloatingObject(RenderBox*, Type, const LayoutRect&, bool shouldPaint, bool isDescendant);
99 
100     RenderBox* m_renderer;
101     RootInlineBox* m_originatingLine;
102     LayoutRect m_frameRect;
103     int m_paginationStrut; // FIXME: Is this class size-sensitive? Does this need 32-bits?
104 
105     unsigned m_type : 2; // Type (left or right aligned)
106     unsigned m_shouldPaint : 1;
107     unsigned m_isDescendant : 1;
108     unsigned m_isPlaced : 1;
109 #if ENABLE(ASSERT)
110     unsigned m_isInPlacedTree : 1;
111 #endif
112 };
113 
114 struct FloatingObjectHashFunctions {
hashFloatingObjectHashFunctions115     static unsigned hash(FloatingObject* key) { return DefaultHash<RenderBox*>::Hash::hash(key->renderer()); }
hashFloatingObjectHashFunctions116     static unsigned hash(const OwnPtr<FloatingObject>& key) { return hash(key.get()); }
hashFloatingObjectHashFunctions117     static unsigned hash(const PassOwnPtr<FloatingObject>& key) { return hash(key.get()); }
equalFloatingObjectHashFunctions118     static bool equal(OwnPtr<FloatingObject>& a, FloatingObject* b) { return a->renderer() == b->renderer(); }
equalFloatingObjectHashFunctions119     static bool equal(OwnPtr<FloatingObject>& a, const OwnPtr<FloatingObject>& b) { return equal(a, b.get()); }
equalFloatingObjectHashFunctions120     static bool equal(OwnPtr<FloatingObject>& a, const PassOwnPtr<FloatingObject>& b) { return equal(a, b.get()); }
121 
122     static const bool safeToCompareToEmptyOrDeleted = true;
123 };
124 struct FloatingObjectHashTranslator {
hashFloatingObjectHashTranslator125     static unsigned hash(RenderBox* key) { return DefaultHash<RenderBox*>::Hash::hash(key); }
equalFloatingObjectHashTranslator126     static bool equal(FloatingObject* a, RenderBox* b) { return a->renderer() == b; }
equalFloatingObjectHashTranslator127     static bool equal(const OwnPtr<FloatingObject>& a, RenderBox* b) { return a->renderer() == b; }
128 };
129 typedef ListHashSet<OwnPtr<FloatingObject>, 4, FloatingObjectHashFunctions> FloatingObjectSet;
130 typedef FloatingObjectSet::const_iterator FloatingObjectSetIterator;
131 typedef PODInterval<int, FloatingObject*> FloatingObjectInterval;
132 typedef PODIntervalTree<int, FloatingObject*> FloatingObjectTree;
133 typedef PODFreeListArena<PODRedBlackTree<FloatingObjectInterval>::Node> IntervalArena;
134 typedef HashMap<RenderBox*, OwnPtr<FloatingObject> > RendererToFloatInfoMap;
135 
136 class FloatingObjects {
137     WTF_MAKE_NONCOPYABLE(FloatingObjects); WTF_MAKE_FAST_ALLOCATED;
138 public:
139     FloatingObjects(const RenderBlockFlow*, bool horizontalWritingMode);
140     ~FloatingObjects();
141 
142     void clear();
143     void moveAllToFloatInfoMap(RendererToFloatInfoMap&);
144     FloatingObject* add(PassOwnPtr<FloatingObject>);
145     void remove(FloatingObject*);
146     void addPlacedObject(FloatingObject*);
147     void removePlacedObject(FloatingObject*);
148     void setHorizontalWritingMode(bool b = true) { m_horizontalWritingMode = b; }
149 
hasLeftObjects()150     bool hasLeftObjects() const { return m_leftObjectsCount > 0; }
hasRightObjects()151     bool hasRightObjects() const { return m_rightObjectsCount > 0; }
set()152     const FloatingObjectSet& set() const { return m_set; }
153     void clearLineBoxTreePointers();
154 
155     LayoutUnit logicalLeftOffset(LayoutUnit fixedOffset, LayoutUnit logicalTop, LayoutUnit logicalHeight);
156     LayoutUnit logicalRightOffset(LayoutUnit fixedOffset, LayoutUnit logicalTop, LayoutUnit logicalHeight);
157 
158     LayoutUnit logicalLeftOffsetForPositioningFloat(LayoutUnit fixedOffset, LayoutUnit logicalTop, LayoutUnit* heightRemaining);
159     LayoutUnit logicalRightOffsetForPositioningFloat(LayoutUnit fixedOffset, LayoutUnit logicalTop, LayoutUnit* heightRemaining);
160 
161     LayoutUnit lowestFloatLogicalBottom(FloatingObject::Type);
162 
163 private:
164     bool hasLowestFloatLogicalBottomCached(bool isHorizontal, FloatingObject::Type floatType) const;
165     LayoutUnit getCachedlowestFloatLogicalBottom(FloatingObject::Type floatType) const;
166     void setCachedLowestFloatLogicalBottom(bool isHorizontal, FloatingObject::Type floatType, LayoutUnit value);
167     void markLowestFloatLogicalBottomCacheAsDirty();
168 
169     void computePlacedFloatsTree();
placedFloatsTree()170     const FloatingObjectTree& placedFloatsTree()
171     {
172         if (!m_placedFloatsTree.isInitialized())
173             computePlacedFloatsTree();
174         return m_placedFloatsTree;
175     }
176     void increaseObjectsCount(FloatingObject::Type);
177     void decreaseObjectsCount(FloatingObject::Type);
178     FloatingObjectInterval intervalForFloatingObject(FloatingObject*);
179 
180     FloatingObjectSet m_set;
181     FloatingObjectTree m_placedFloatsTree;
182     unsigned m_leftObjectsCount;
183     unsigned m_rightObjectsCount;
184     bool m_horizontalWritingMode;
185     const RenderBlockFlow* m_renderer;
186 
187     struct FloatBottomCachedValue {
188         FloatBottomCachedValue();
189         LayoutUnit value;
190         bool dirty;
191     };
192     FloatBottomCachedValue m_lowestFloatBottomCache[2];
193     bool m_cachedHorizontalWritingMode;
194 };
195 
196 #ifndef NDEBUG
197 // These structures are used by PODIntervalTree for debugging purposes.
198 template <> struct ValueToString<int> {
199     static String string(const int value);
200 };
201 template<> struct ValueToString<FloatingObject*> {
202     static String string(const FloatingObject*);
203 };
204 #endif
205 
206 } // namespace blink
207 
208 #endif // FloatingObjects_h
209