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 virtual ~ComputeFloatOffsetAdapter() { }
117 int lowValue() const { return m_lineTop; }
118 int highValue() const { return m_lineBottom; }
119 void collectIfNeeded(const IntervalType&);
121 LayoutUnit offset() const { return m_offset; }
124 virtual bool updateOffsetIfNeeded(const FloatingObject*) = 0;
126 const RenderBlockFlow* m_renderer;
130 const FloatingObject* m_outermostFloat;
133 template <FloatingObject::Type FloatTypeValue>
134 class ComputeFloatOffsetForFloatLayoutAdapter : public ComputeFloatOffsetAdapter<FloatTypeValue> {
136 ComputeFloatOffsetForFloatLayoutAdapter(const RenderBlockFlow* renderer, LayoutUnit lineTop, LayoutUnit lineBottom, LayoutUnit offset)
137 : ComputeFloatOffsetAdapter<FloatTypeValue>(renderer, lineTop, lineBottom, offset)
141 virtual ~ComputeFloatOffsetForFloatLayoutAdapter() { }
143 LayoutUnit heightRemaining() const;
146 virtual bool updateOffsetIfNeeded(const FloatingObject*) OVERRIDE FINAL;
149 template <FloatingObject::Type FloatTypeValue>
150 class ComputeFloatOffsetForLineLayoutAdapter : public ComputeFloatOffsetAdapter<FloatTypeValue> {
152 ComputeFloatOffsetForLineLayoutAdapter(const RenderBlockFlow* renderer, LayoutUnit lineTop, LayoutUnit lineBottom, LayoutUnit offset)
153 : ComputeFloatOffsetAdapter<FloatTypeValue>(renderer, lineTop, lineBottom, offset)
157 virtual ~ComputeFloatOffsetForLineLayoutAdapter() { }
160 virtual bool updateOffsetIfNeeded(const FloatingObject*) OVERRIDE FINAL;
164 FloatingObjects::~FloatingObjects()
167 void FloatingObjects::clearLineBoxTreePointers()
169 // Clear references to originating lines, since the lines are being deleted
170 FloatingObjectSetIterator end = m_set.end();
171 for (FloatingObjectSetIterator it = m_set.begin(); it != end; ++it) {
172 ASSERT(!((*it)->originatingLine()) || (*it)->originatingLine()->renderer() == m_renderer);
173 (*it)->setOriginatingLine(0);
177 FloatingObjects::FloatingObjects(const RenderBlockFlow* renderer, bool horizontalWritingMode)
178 : m_placedFloatsTree(UninitializedTree)
179 , m_leftObjectsCount(0)
180 , m_rightObjectsCount(0)
181 , m_horizontalWritingMode(horizontalWritingMode)
182 , m_renderer(renderer)
183 , m_cachedHorizontalWritingMode(false)
187 void FloatingObjects::clear()
190 m_placedFloatsTree.clear();
191 m_leftObjectsCount = 0;
192 m_rightObjectsCount = 0;
193 markLowestFloatLogicalBottomCacheAsDirty();
196 LayoutUnit FloatingObjects::lowestFloatLogicalBottom(FloatingObject::Type floatType)
198 bool isInHorizontalWritingMode = m_horizontalWritingMode;
199 if (floatType != FloatingObject::FloatLeftRight) {
200 if (hasLowestFloatLogicalBottomCached(isInHorizontalWritingMode, floatType))
201 return getCachedlowestFloatLogicalBottom(floatType);
203 if (hasLowestFloatLogicalBottomCached(isInHorizontalWritingMode, FloatingObject::FloatLeft) && hasLowestFloatLogicalBottomCached(isInHorizontalWritingMode, FloatingObject::FloatRight)) {
204 return max(getCachedlowestFloatLogicalBottom(FloatingObject::FloatLeft),
205 getCachedlowestFloatLogicalBottom(FloatingObject::FloatRight));
209 LayoutUnit lowestFloatBottom = 0;
210 const FloatingObjectSet& floatingObjectSet = set();
211 FloatingObjectSetIterator end = floatingObjectSet.end();
212 if (floatType == FloatingObject::FloatLeftRight) {
213 LayoutUnit lowestFloatBottomLeft = 0;
214 LayoutUnit lowestFloatBottomRight = 0;
215 for (FloatingObjectSetIterator it = floatingObjectSet.begin(); it != end; ++it) {
216 FloatingObject* floatingObject = it->get();
217 if (floatingObject->isPlaced()) {
218 FloatingObject::Type curType = floatingObject->type();
219 LayoutUnit curFloatLogicalBottom = m_renderer->logicalBottomForFloat(floatingObject);
220 if (curType & FloatingObject::FloatLeft)
221 lowestFloatBottomLeft = max(lowestFloatBottomLeft, curFloatLogicalBottom);
222 if (curType & FloatingObject::FloatRight)
223 lowestFloatBottomRight = max(lowestFloatBottomRight, curFloatLogicalBottom);
226 lowestFloatBottom = max(lowestFloatBottomLeft, lowestFloatBottomRight);
227 setCachedLowestFloatLogicalBottom(isInHorizontalWritingMode, FloatingObject::FloatLeft, lowestFloatBottomLeft);
228 setCachedLowestFloatLogicalBottom(isInHorizontalWritingMode, FloatingObject::FloatRight, lowestFloatBottomRight);
230 for (FloatingObjectSetIterator it = floatingObjectSet.begin(); it != end; ++it) {
231 FloatingObject* floatingObject = it->get();
232 if (floatingObject->isPlaced() && floatingObject->type() == floatType)
233 lowestFloatBottom = max(lowestFloatBottom, m_renderer->logicalBottomForFloat(floatingObject));
235 setCachedLowestFloatLogicalBottom(isInHorizontalWritingMode, floatType, lowestFloatBottom);
238 return lowestFloatBottom;
241 bool FloatingObjects::hasLowestFloatLogicalBottomCached(bool isHorizontal, FloatingObject::Type type) const
243 int floatIndex = static_cast<int>(type) - 1;
244 ASSERT(floatIndex < static_cast<int>(sizeof(m_lowestFloatBottomCache) / sizeof(FloatBottomCachedValue)));
245 ASSERT(floatIndex >= 0);
246 return (m_cachedHorizontalWritingMode == isHorizontal && !m_lowestFloatBottomCache[floatIndex].dirty);
249 LayoutUnit FloatingObjects::getCachedlowestFloatLogicalBottom(FloatingObject::Type type) const
251 int floatIndex = static_cast<int>(type) - 1;
252 ASSERT(floatIndex < static_cast<int>(sizeof(m_lowestFloatBottomCache) / sizeof(FloatBottomCachedValue)));
253 ASSERT(floatIndex >= 0);
254 return m_lowestFloatBottomCache[floatIndex].value;
257 void FloatingObjects::setCachedLowestFloatLogicalBottom(bool isHorizontal, FloatingObject::Type type, LayoutUnit value)
259 int floatIndex = static_cast<int>(type) - 1;
260 ASSERT(floatIndex < static_cast<int>(sizeof(m_lowestFloatBottomCache) / sizeof(FloatBottomCachedValue)));
261 ASSERT(floatIndex >= 0);
262 m_cachedHorizontalWritingMode = isHorizontal;
263 m_lowestFloatBottomCache[floatIndex].value = value;
264 m_lowestFloatBottomCache[floatIndex].dirty = false;
267 void FloatingObjects::markLowestFloatLogicalBottomCacheAsDirty()
269 for (size_t i = 0; i < sizeof(m_lowestFloatBottomCache) / sizeof(FloatBottomCachedValue); ++i)
270 m_lowestFloatBottomCache[i].dirty = true;
273 void FloatingObjects::moveAllToFloatInfoMap(RendererToFloatInfoMap& map)
275 while (!m_set.isEmpty()) {
276 OwnPtr<FloatingObject> floatingObject = m_set.takeFirst();
277 RenderBox* renderer = floatingObject->renderer();
278 map.add(renderer, floatingObject.release());
283 inline void FloatingObjects::increaseObjectsCount(FloatingObject::Type type)
285 if (type == FloatingObject::FloatLeft)
286 m_leftObjectsCount++;
288 m_rightObjectsCount++;
291 inline void FloatingObjects::decreaseObjectsCount(FloatingObject::Type type)
293 if (type == FloatingObject::FloatLeft)
294 m_leftObjectsCount--;
296 m_rightObjectsCount--;
299 inline FloatingObjectInterval FloatingObjects::intervalForFloatingObject(FloatingObject* floatingObject)
301 if (m_horizontalWritingMode)
302 return FloatingObjectInterval(floatingObject->frameRect().pixelSnappedY(), floatingObject->frameRect().pixelSnappedMaxY(), floatingObject);
303 return FloatingObjectInterval(floatingObject->frameRect().pixelSnappedX(), floatingObject->frameRect().pixelSnappedMaxX(), floatingObject);
306 void FloatingObjects::addPlacedObject(FloatingObject* floatingObject)
308 ASSERT(!floatingObject->isInPlacedTree());
310 floatingObject->setIsPlaced(true);
311 if (m_placedFloatsTree.isInitialized())
312 m_placedFloatsTree.add(intervalForFloatingObject(floatingObject));
315 floatingObject->setIsInPlacedTree(true);
317 markLowestFloatLogicalBottomCacheAsDirty();
320 void FloatingObjects::removePlacedObject(FloatingObject* floatingObject)
322 ASSERT(floatingObject->isPlaced() && floatingObject->isInPlacedTree());
324 if (m_placedFloatsTree.isInitialized()) {
325 bool removed = m_placedFloatsTree.remove(intervalForFloatingObject(floatingObject));
326 ASSERT_UNUSED(removed, removed);
329 floatingObject->setIsPlaced(false);
331 floatingObject->setIsInPlacedTree(false);
333 markLowestFloatLogicalBottomCacheAsDirty();
336 FloatingObject* FloatingObjects::add(PassOwnPtr<FloatingObject> floatingObject)
338 FloatingObject* newObject = floatingObject.leakPtr();
339 increaseObjectsCount(newObject->type());
340 m_set.add(adoptPtr(newObject));
341 if (newObject->isPlaced())
342 addPlacedObject(newObject);
343 markLowestFloatLogicalBottomCacheAsDirty();
347 void FloatingObjects::remove(FloatingObject* toBeRemoved)
349 decreaseObjectsCount(toBeRemoved->type());
350 OwnPtr<FloatingObject> floatingObject = m_set.take(toBeRemoved);
351 ASSERT(floatingObject->isPlaced() || !floatingObject->isInPlacedTree());
352 if (floatingObject->isPlaced())
353 removePlacedObject(floatingObject.get());
354 markLowestFloatLogicalBottomCacheAsDirty();
355 ASSERT(!floatingObject->originatingLine());
358 void FloatingObjects::computePlacedFloatsTree()
360 ASSERT(!m_placedFloatsTree.isInitialized());
363 m_placedFloatsTree.initIfNeeded(m_renderer->view()->intervalArena());
364 FloatingObjectSetIterator it = m_set.begin();
365 FloatingObjectSetIterator end = m_set.end();
366 for (; it != end; ++it) {
367 FloatingObject* floatingObject = it->get();
368 if (floatingObject->isPlaced())
369 m_placedFloatsTree.add(intervalForFloatingObject(floatingObject));
373 LayoutUnit FloatingObjects::logicalLeftOffsetForPositioningFloat(LayoutUnit fixedOffset, LayoutUnit logicalTop, LayoutUnit *heightRemaining)
375 int logicalTopAsInt = roundToInt(logicalTop);
376 ComputeFloatOffsetForFloatLayoutAdapter<FloatingObject::FloatLeft> adapter(m_renderer, logicalTopAsInt, logicalTopAsInt, fixedOffset);
377 placedFloatsTree().allOverlapsWithAdapter(adapter);
380 *heightRemaining = adapter.heightRemaining();
382 return adapter.offset();
385 LayoutUnit FloatingObjects::logicalRightOffsetForPositioningFloat(LayoutUnit fixedOffset, LayoutUnit logicalTop, LayoutUnit *heightRemaining)
387 int logicalTopAsInt = roundToInt(logicalTop);
388 ComputeFloatOffsetForFloatLayoutAdapter<FloatingObject::FloatRight> adapter(m_renderer, logicalTopAsInt, logicalTopAsInt, fixedOffset);
389 placedFloatsTree().allOverlapsWithAdapter(adapter);
392 *heightRemaining = adapter.heightRemaining();
394 return min(fixedOffset, adapter.offset());
397 LayoutUnit FloatingObjects::logicalLeftOffset(LayoutUnit fixedOffset, LayoutUnit logicalTop, LayoutUnit logicalHeight)
399 ComputeFloatOffsetForLineLayoutAdapter<FloatingObject::FloatLeft> adapter(m_renderer, roundToInt(logicalTop), roundToInt(logicalTop + logicalHeight), fixedOffset);
400 placedFloatsTree().allOverlapsWithAdapter(adapter);
402 return adapter.offset();
405 LayoutUnit FloatingObjects::logicalRightOffset(LayoutUnit fixedOffset, LayoutUnit logicalTop, LayoutUnit logicalHeight)
407 ComputeFloatOffsetForLineLayoutAdapter<FloatingObject::FloatRight> adapter(m_renderer, roundToInt(logicalTop), roundToInt(logicalTop + logicalHeight), fixedOffset);
408 placedFloatsTree().allOverlapsWithAdapter(adapter);
410 return min(fixedOffset, adapter.offset());
413 FloatingObjects::FloatBottomCachedValue::FloatBottomCachedValue()
419 inline static bool rangesIntersect(int floatTop, int floatBottom, int objectTop, int objectBottom)
421 if (objectTop >= floatBottom || objectBottom < floatTop)
424 // The top of the object overlaps the float
425 if (objectTop >= floatTop)
428 // The object encloses the float
429 if (objectTop < floatTop && objectBottom > floatBottom)
432 // The bottom of the object overlaps the float
433 if (objectBottom > objectTop && objectBottom > floatTop && objectBottom <= floatBottom)
440 inline bool ComputeFloatOffsetForFloatLayoutAdapter<FloatingObject::FloatLeft>::updateOffsetIfNeeded(const FloatingObject* floatingObject)
442 LayoutUnit logicalRight = m_renderer->logicalRightForFloat(floatingObject);
443 if (logicalRight > m_offset) {
444 m_offset = logicalRight;
451 inline bool ComputeFloatOffsetForFloatLayoutAdapter<FloatingObject::FloatRight>::updateOffsetIfNeeded(const FloatingObject* floatingObject)
453 LayoutUnit logicalLeft = m_renderer->logicalLeftForFloat(floatingObject);
454 if (logicalLeft < m_offset) {
455 m_offset = logicalLeft;
461 template <FloatingObject::Type FloatTypeValue>
462 LayoutUnit ComputeFloatOffsetForFloatLayoutAdapter<FloatTypeValue>::heightRemaining() const
464 return this->m_outermostFloat ? this->m_renderer->logicalBottomForFloat(this->m_outermostFloat) - this->m_lineTop : LayoutUnit(1);
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 static inline ShapeOutsideInfo* shapeInfoForFloat(const FloatingObject& floatingObject, const RenderBlockFlow& containingBlock, LayoutUnit lineTop, LayoutUnit lineBottom)
486 if (ShapeOutsideInfo* shapeOutside = floatingObject.renderer()->shapeOutsideInfo()) {
487 shapeOutside->updateDeltasForContainingBlockLine(containingBlock, floatingObject, lineTop, lineBottom - lineTop);
495 inline bool ComputeFloatOffsetForLineLayoutAdapter<FloatingObject::FloatLeft>::updateOffsetIfNeeded(const FloatingObject* floatingObject)
497 ASSERT(floatingObject);
498 LayoutUnit logicalRight = m_renderer->logicalRightForFloat(floatingObject);
499 if (ShapeOutsideInfo* shapeOutside = shapeInfoForFloat(*floatingObject, *m_renderer, m_lineTop, m_lineBottom)) {
500 if (!shapeOutside->lineOverlapsShape())
503 logicalRight += shapeOutside->rightMarginBoxDelta();
505 if (logicalRight > m_offset) {
506 m_offset = logicalRight;
514 inline bool ComputeFloatOffsetForLineLayoutAdapter<FloatingObject::FloatRight>::updateOffsetIfNeeded(const FloatingObject* floatingObject)
516 ASSERT(floatingObject);
517 LayoutUnit logicalLeft = m_renderer->logicalLeftForFloat(floatingObject);
518 if (ShapeOutsideInfo* shapeOutside = shapeInfoForFloat(*floatingObject, *m_renderer, m_lineTop, m_lineBottom)) {
519 if (!shapeOutside->lineOverlapsShape())
522 logicalLeft += shapeOutside->leftMarginBoxDelta();
524 if (logicalLeft < m_offset) {
525 m_offset = logicalLeft;
533 // These helpers are only used by the PODIntervalTree for debugging purposes.
534 String ValueToString<int>::string(const int value)
536 return String::number(value);
539 String ValueToString<FloatingObject*>::string(const FloatingObject* floatingObject)
541 return String::format("%p (%dx%d %dx%d)", floatingObject, floatingObject->frameRect().pixelSnappedX(), floatingObject->frameRect().pixelSnappedY(), floatingObject->frameRect().pixelSnappedMaxX(), floatingObject->frameRect().pixelSnappedMaxY());
546 } // namespace WebCore