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.
25 #include "core/rendering/FloatingObjects.h"
27 #include "core/rendering/RenderBlockFlow.h"
28 #include "core/rendering/RenderBox.h"
29 #include "core/rendering/RenderView.h"
36 struct SameSizeAsFloatingObject {
40 uint32_t bitfields : 8;
43 COMPILE_ASSERT(sizeof(FloatingObject) == sizeof(SameSizeAsFloatingObject), FloatingObject_should_stay_small);
45 FloatingObject::FloatingObject(RenderBox* renderer)
46 : m_renderer(renderer)
47 , m_originatingLine(0)
48 , m_paginationStrut(0)
50 , m_isDescendant(false)
53 , m_isInPlacedTree(false)
56 EFloat type = renderer->style()->floating();
57 ASSERT(type != NoFloat);
58 if (type == LeftFloat)
60 else if (type == RightFloat)
64 FloatingObject::FloatingObject(RenderBox* renderer, Type type, const LayoutRect& frameRect, bool shouldPaint, bool isDescendant)
65 : m_renderer(renderer)
66 , m_originatingLine(0)
67 , m_frameRect(frameRect)
68 , m_paginationStrut(0)
70 , m_shouldPaint(shouldPaint)
71 , m_isDescendant(isDescendant)
74 , m_isInPlacedTree(false)
79 PassOwnPtr<FloatingObject> FloatingObject::create(RenderBox* renderer)
81 OwnPtr<FloatingObject> newObj = adoptPtr(new FloatingObject(renderer));
82 newObj->setShouldPaint(!renderer->hasSelfPaintingLayer()); // If a layer exists, the float will paint itself. Otherwise someone else will.
83 newObj->setIsDescendant(true);
85 return newObj.release();
88 PassOwnPtr<FloatingObject> FloatingObject::copyToNewContainer(LayoutSize offset, bool shouldPaint, bool isDescendant) const
90 return adoptPtr(new FloatingObject(renderer(), type(), LayoutRect(frameRect().location() - offset, frameRect().size()), shouldPaint, isDescendant));
93 PassOwnPtr<FloatingObject> FloatingObject::unsafeClone() const
95 OwnPtr<FloatingObject> cloneObject = adoptPtr(new FloatingObject(renderer(), type(), m_frameRect, m_shouldPaint, m_isDescendant));
96 cloneObject->m_paginationStrut = m_paginationStrut;
97 cloneObject->m_isPlaced = m_isPlaced;
98 return cloneObject.release();
101 template <FloatingObject::Type FloatTypeValue>
102 class ComputeFloatOffsetAdapter {
104 typedef FloatingObjectInterval IntervalType;
106 ComputeFloatOffsetAdapter(const RenderBlockFlow* renderer, int lineTop, int lineBottom, LayoutUnit offset)
107 : m_renderer(renderer)
109 , m_lineBottom(lineBottom)
111 , m_outermostFloat(0)
115 int lowValue() const { return m_lineTop; }
116 int highValue() const { return m_lineBottom; }
117 void collectIfNeeded(const IntervalType&);
119 LayoutUnit offset() const { return m_offset; }
120 LayoutUnit shapeOffset() const;
121 LayoutUnit heightRemaining() const;
124 bool updateOffsetIfNeeded(const FloatingObject*);
126 const RenderBlockFlow* m_renderer;
130 const FloatingObject* m_outermostFloat;
134 FloatingObjects::~FloatingObjects()
136 // FIXME: m_set should use OwnPtr instead.
137 deleteAllValues(m_set);
139 void FloatingObjects::clearLineBoxTreePointers()
141 // Clear references to originating lines, since the lines are being deleted
142 FloatingObjectSetIterator end = m_set.end();
143 for (FloatingObjectSetIterator it = m_set.begin(); it != end; ++it) {
144 ASSERT(!((*it)->originatingLine()) || (*it)->originatingLine()->renderer() == m_renderer);
145 (*it)->setOriginatingLine(0);
150 inline bool ComputeFloatOffsetAdapter<FloatingObject::FloatLeft>::updateOffsetIfNeeded(const FloatingObject* floatingObject)
152 LayoutUnit logicalRight = m_renderer->logicalRightForFloat(floatingObject);
153 if (logicalRight > m_offset) {
154 m_offset = logicalRight;
160 FloatingObjects::FloatingObjects(const RenderBlockFlow* renderer, bool horizontalWritingMode)
161 : m_placedFloatsTree(UninitializedTree)
162 , m_leftObjectsCount(0)
163 , m_rightObjectsCount(0)
164 , m_horizontalWritingMode(horizontalWritingMode)
165 , m_renderer(renderer)
166 , m_cachedHorizontalWritingMode(false)
170 void FloatingObjects::clear()
172 deleteAllValues(m_set);
174 m_placedFloatsTree.clear();
175 m_leftObjectsCount = 0;
176 m_rightObjectsCount = 0;
177 markLowestFloatLogicalBottomCacheAsDirty();
180 LayoutUnit FloatingObjects::lowestFloatLogicalBottom(FloatingObject::Type floatType)
182 bool isInHorizontalWritingMode = m_horizontalWritingMode;
183 if (floatType != FloatingObject::FloatLeftRight) {
184 if (hasLowestFloatLogicalBottomCached(isInHorizontalWritingMode, floatType))
185 return getCachedlowestFloatLogicalBottom(floatType);
187 if (hasLowestFloatLogicalBottomCached(isInHorizontalWritingMode, FloatingObject::FloatLeft) && hasLowestFloatLogicalBottomCached(isInHorizontalWritingMode, FloatingObject::FloatRight)) {
188 return max(getCachedlowestFloatLogicalBottom(FloatingObject::FloatLeft),
189 getCachedlowestFloatLogicalBottom(FloatingObject::FloatRight));
193 LayoutUnit lowestFloatBottom = 0;
194 const FloatingObjectSet& floatingObjectSet = set();
195 FloatingObjectSetIterator end = floatingObjectSet.end();
196 if (floatType == FloatingObject::FloatLeftRight) {
197 LayoutUnit lowestFloatBottomLeft = 0;
198 LayoutUnit lowestFloatBottomRight = 0;
199 for (FloatingObjectSetIterator it = floatingObjectSet.begin(); it != end; ++it) {
200 FloatingObject* floatingObject = *it;
201 if (floatingObject->isPlaced()) {
202 FloatingObject::Type curType = floatingObject->type();
203 LayoutUnit curFloatLogicalBottom = m_renderer->logicalBottomForFloat(floatingObject);
204 if (curType & FloatingObject::FloatLeft)
205 lowestFloatBottomLeft = max(lowestFloatBottomLeft, curFloatLogicalBottom);
206 if (curType & FloatingObject::FloatRight)
207 lowestFloatBottomRight = max(lowestFloatBottomRight, curFloatLogicalBottom);
210 lowestFloatBottom = max(lowestFloatBottomLeft, lowestFloatBottomRight);
211 setCachedLowestFloatLogicalBottom(isInHorizontalWritingMode, FloatingObject::FloatLeft, lowestFloatBottomLeft);
212 setCachedLowestFloatLogicalBottom(isInHorizontalWritingMode, FloatingObject::FloatRight, lowestFloatBottomRight);
214 for (FloatingObjectSetIterator it = floatingObjectSet.begin(); it != end; ++it) {
215 FloatingObject* floatingObject = *it;
216 if (floatingObject->isPlaced() && floatingObject->type() == floatType)
217 lowestFloatBottom = max(lowestFloatBottom, m_renderer->logicalBottomForFloat(floatingObject));
219 setCachedLowestFloatLogicalBottom(isInHorizontalWritingMode, floatType, lowestFloatBottom);
222 return lowestFloatBottom;
225 bool FloatingObjects::hasLowestFloatLogicalBottomCached(bool isHorizontal, FloatingObject::Type type) const
227 int floatIndex = static_cast<int>(type) - 1;
228 ASSERT(floatIndex < static_cast<int>(sizeof(m_lowestFloatBottomCache) / sizeof(FloatBottomCachedValue)));
229 ASSERT(floatIndex >= 0);
230 return (m_cachedHorizontalWritingMode == isHorizontal && !m_lowestFloatBottomCache[floatIndex].dirty);
233 LayoutUnit FloatingObjects::getCachedlowestFloatLogicalBottom(FloatingObject::Type type) const
235 int floatIndex = static_cast<int>(type) - 1;
236 ASSERT(floatIndex < static_cast<int>(sizeof(m_lowestFloatBottomCache) / sizeof(FloatBottomCachedValue)));
237 ASSERT(floatIndex >= 0);
238 return m_lowestFloatBottomCache[floatIndex].value;
241 void FloatingObjects::setCachedLowestFloatLogicalBottom(bool isHorizontal, FloatingObject::Type type, LayoutUnit value)
243 int floatIndex = static_cast<int>(type) - 1;
244 ASSERT(floatIndex < static_cast<int>(sizeof(m_lowestFloatBottomCache) / sizeof(FloatBottomCachedValue)));
245 ASSERT(floatIndex >= 0);
246 m_cachedHorizontalWritingMode = isHorizontal;
247 m_lowestFloatBottomCache[floatIndex].value = value;
248 m_lowestFloatBottomCache[floatIndex].dirty = false;
251 void FloatingObjects::markLowestFloatLogicalBottomCacheAsDirty()
253 for (size_t i = 0; i < sizeof(m_lowestFloatBottomCache) / sizeof(FloatBottomCachedValue); ++i)
254 m_lowestFloatBottomCache[i].dirty = true;
257 void FloatingObjects::moveAllToFloatInfoMap(RendererToFloatInfoMap& map)
259 FloatingObjectSetIterator end = m_set.end();
260 for (FloatingObjectSetIterator it = m_set.begin(); it != end; ++it)
261 map.add((*it)->renderer(), *it);
263 // clear set before clearing this because we don't want to delete all of
264 // the objects we have just transferred.
269 inline void FloatingObjects::increaseObjectsCount(FloatingObject::Type type)
271 if (type == FloatingObject::FloatLeft)
272 m_leftObjectsCount++;
274 m_rightObjectsCount++;
277 inline void FloatingObjects::decreaseObjectsCount(FloatingObject::Type type)
279 if (type == FloatingObject::FloatLeft)
280 m_leftObjectsCount--;
282 m_rightObjectsCount--;
285 inline FloatingObjectInterval FloatingObjects::intervalForFloatingObject(FloatingObject* floatingObject)
287 if (m_horizontalWritingMode)
288 return FloatingObjectInterval(floatingObject->frameRect().pixelSnappedY(), floatingObject->frameRect().pixelSnappedMaxY(), floatingObject);
289 return FloatingObjectInterval(floatingObject->frameRect().pixelSnappedX(), floatingObject->frameRect().pixelSnappedMaxX(), floatingObject);
292 void FloatingObjects::addPlacedObject(FloatingObject* floatingObject)
294 ASSERT(!floatingObject->isInPlacedTree());
296 floatingObject->setIsPlaced(true);
297 if (m_placedFloatsTree.isInitialized())
298 m_placedFloatsTree.add(intervalForFloatingObject(floatingObject));
301 floatingObject->setIsInPlacedTree(true);
303 markLowestFloatLogicalBottomCacheAsDirty();
306 void FloatingObjects::removePlacedObject(FloatingObject* floatingObject)
308 ASSERT(floatingObject->isPlaced() && floatingObject->isInPlacedTree());
310 if (m_placedFloatsTree.isInitialized()) {
311 bool removed = m_placedFloatsTree.remove(intervalForFloatingObject(floatingObject));
312 ASSERT_UNUSED(removed, removed);
315 floatingObject->setIsPlaced(false);
317 floatingObject->setIsInPlacedTree(false);
319 markLowestFloatLogicalBottomCacheAsDirty();
322 FloatingObject* FloatingObjects::add(PassOwnPtr<FloatingObject> floatingObject)
324 FloatingObject* newObject = floatingObject.leakPtr();
325 increaseObjectsCount(newObject->type());
326 m_set.add(newObject);
327 if (newObject->isPlaced())
328 addPlacedObject(newObject);
329 markLowestFloatLogicalBottomCacheAsDirty();
333 void FloatingObjects::remove(FloatingObject* floatingObject)
335 decreaseObjectsCount(floatingObject->type());
336 m_set.remove(floatingObject);
337 ASSERT(floatingObject->isPlaced() || !floatingObject->isInPlacedTree());
338 if (floatingObject->isPlaced())
339 removePlacedObject(floatingObject);
340 markLowestFloatLogicalBottomCacheAsDirty();
341 ASSERT(!floatingObject->originatingLine());
342 delete floatingObject;
345 void FloatingObjects::computePlacedFloatsTree()
347 ASSERT(!m_placedFloatsTree.isInitialized());
350 m_placedFloatsTree.initIfNeeded(m_renderer->view()->intervalArena());
351 FloatingObjectSetIterator it = m_set.begin();
352 FloatingObjectSetIterator end = m_set.end();
353 for (; it != end; ++it) {
354 FloatingObject* floatingObject = *it;
355 if (floatingObject->isPlaced())
356 m_placedFloatsTree.add(intervalForFloatingObject(floatingObject));
360 static inline ShapeOutsideInfo* shapeInfoForFloat(const FloatingObject* floatingObject, const RenderBlockFlow* containingBlock, LayoutUnit lineTop, LayoutUnit lineBottom)
362 if (floatingObject) {
363 if (ShapeOutsideInfo* shapeOutside = floatingObject->renderer()->shapeOutsideInfo()) {
364 shapeOutside->updateDeltasForContainingBlockLine(containingBlock, floatingObject, lineTop, lineBottom - lineTop);
373 inline LayoutUnit ComputeFloatOffsetAdapter<FloatingObject::FloatLeft>::shapeOffset() const
375 if (ShapeOutsideInfo* shapeOutside = shapeInfoForFloat(m_outermostFloat, m_renderer, m_lineTop, m_lineBottom))
376 return m_offset + shapeOutside->rightMarginBoxDelta();
382 inline LayoutUnit ComputeFloatOffsetAdapter<FloatingObject::FloatRight>::shapeOffset() const
384 if (ShapeOutsideInfo* shapeOutside = shapeInfoForFloat(m_outermostFloat, m_renderer, m_lineTop, m_lineBottom))
385 return m_offset + shapeOutside->leftMarginBoxDelta();
390 LayoutUnit FloatingObjects::logicalLeftOffsetForPositioningFloat(LayoutUnit fixedOffset, LayoutUnit logicalTop, LayoutUnit *heightRemaining)
392 int logicalTopAsInt = roundToInt(logicalTop);
393 ComputeFloatOffsetAdapter<FloatingObject::FloatLeft> adapter(m_renderer, logicalTopAsInt, logicalTopAsInt, fixedOffset);
394 placedFloatsTree().allOverlapsWithAdapter(adapter);
397 *heightRemaining = adapter.heightRemaining();
399 return adapter.offset();
402 LayoutUnit FloatingObjects::logicalRightOffsetForPositioningFloat(LayoutUnit fixedOffset, LayoutUnit logicalTop, LayoutUnit *heightRemaining)
404 int logicalTopAsInt = roundToInt(logicalTop);
405 ComputeFloatOffsetAdapter<FloatingObject::FloatRight> adapter(m_renderer, logicalTopAsInt, logicalTopAsInt, fixedOffset);
406 placedFloatsTree().allOverlapsWithAdapter(adapter);
409 *heightRemaining = adapter.heightRemaining();
411 return min(fixedOffset, adapter.offset());
414 LayoutUnit FloatingObjects::logicalLeftOffset(LayoutUnit fixedOffset, LayoutUnit logicalTop, LayoutUnit logicalHeight)
416 ComputeFloatOffsetAdapter<FloatingObject::FloatLeft> adapter(m_renderer, roundToInt(logicalTop), roundToInt(logicalTop + logicalHeight), fixedOffset);
417 placedFloatsTree().allOverlapsWithAdapter(adapter);
419 return adapter.shapeOffset();
422 LayoutUnit FloatingObjects::logicalRightOffset(LayoutUnit fixedOffset, LayoutUnit logicalTop, LayoutUnit logicalHeight)
424 ComputeFloatOffsetAdapter<FloatingObject::FloatRight> adapter(m_renderer, roundToInt(logicalTop), roundToInt(logicalTop + logicalHeight), fixedOffset);
425 placedFloatsTree().allOverlapsWithAdapter(adapter);
427 return min(fixedOffset, adapter.shapeOffset());
430 FloatingObjects::FloatBottomCachedValue::FloatBottomCachedValue()
436 inline static bool rangesIntersect(int floatTop, int floatBottom, int objectTop, int objectBottom)
438 if (objectTop >= floatBottom || objectBottom < floatTop)
441 // The top of the object overlaps the float
442 if (objectTop >= floatTop)
445 // The object encloses the float
446 if (objectTop < floatTop && objectBottom > floatBottom)
449 // The bottom of the object overlaps the float
450 if (objectBottom > objectTop && objectBottom > floatTop && objectBottom <= floatBottom)
457 inline bool ComputeFloatOffsetAdapter<FloatingObject::FloatRight>::updateOffsetIfNeeded(const FloatingObject* floatingObject)
459 LayoutUnit logicalLeft = m_renderer->logicalLeftForFloat(floatingObject);
460 if (logicalLeft < m_offset) {
461 m_offset = logicalLeft;
467 template <FloatingObject::Type FloatTypeValue>
468 inline void ComputeFloatOffsetAdapter<FloatTypeValue>::collectIfNeeded(const IntervalType& interval)
470 const FloatingObject* floatingObject = interval.data();
471 if (floatingObject->type() != FloatTypeValue || !rangesIntersect(interval.low(), interval.high(), m_lineTop, m_lineBottom))
474 // Make sure the float hasn't changed since it was added to the placed floats tree.
475 ASSERT(floatingObject->isPlaced());
476 ASSERT(interval.low() == m_renderer->pixelSnappedLogicalTopForFloat(floatingObject));
477 ASSERT(interval.high() == m_renderer->pixelSnappedLogicalBottomForFloat(floatingObject));
479 bool floatIsNewExtreme = updateOffsetIfNeeded(floatingObject);
480 if (floatIsNewExtreme)
481 m_outermostFloat = floatingObject;
484 template <FloatingObject::Type FloatTypeValue>
485 LayoutUnit ComputeFloatOffsetAdapter<FloatTypeValue>::heightRemaining() const
487 return m_outermostFloat ? m_renderer->logicalBottomForFloat(m_outermostFloat) - m_lineTop : LayoutUnit(1);
491 // These helpers are only used by the PODIntervalTree for debugging purposes.
492 String ValueToString<int>::string(const int value)
494 return String::number(value);
497 String ValueToString<FloatingObject*>::string(const FloatingObject* floatingObject)
499 return String::format("%p (%dx%d %dx%d)", floatingObject, floatingObject->frameRect().pixelSnappedX(), floatingObject->frameRect().pixelSnappedY(), floatingObject->frameRect().pixelSnappedMaxX(), floatingObject->frameRect().pixelSnappedMaxY());
504 } // namespace WebCore