1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
|
/*
* Copyright (C) 1999 Lars Knoll (knoll@kde.org)
* (C) 1999 Antti Koivisto (koivisto@kde.org)
* (C) 2007 David Smith (catfish.man@gmail.com)
* Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All rights reserved.
* Copyright (C) Research In Motion Limited 2010. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include "FloatingObjects.h"
#include "RenderBlockFlow.h"
#include "RenderBox.h"
#include "RenderView.h"
using namespace WTF;
namespace WebCore {
struct SameSizeAsFloatingObject {
void* pointers[2];
LayoutRect rect;
int paginationStrut;
uint32_t bitfields : 8;
};
COMPILE_ASSERT(sizeof(FloatingObject) == sizeof(SameSizeAsFloatingObject), FloatingObject_should_stay_small);
FloatingObject::FloatingObject(RenderBox& renderer)
: m_renderer(renderer)
, m_originatingLine(nullptr)
, m_paginationStrut(0)
, m_shouldPaint(true)
, m_isDescendant(false)
, m_isPlaced(false)
#ifndef NDEBUG
, m_isInPlacedTree(false)
#endif
{
EFloat type = renderer.style().floating();
ASSERT(type != NoFloat);
if (type == LeftFloat)
m_type = FloatLeft;
else if (type == RightFloat)
m_type = FloatRight;
}
FloatingObject::FloatingObject(RenderBox& renderer, Type type, const LayoutRect& frameRect, bool shouldPaint, bool isDescendant)
: m_renderer(renderer)
, m_originatingLine(nullptr)
, m_frameRect(frameRect)
, m_paginationStrut(0)
, m_type(type)
, m_shouldPaint(shouldPaint)
, m_isDescendant(isDescendant)
, m_isPlaced(true)
#ifndef NDEBUG
, m_isInPlacedTree(false)
#endif
{
}
std::unique_ptr<FloatingObject> FloatingObject::create(RenderBox& renderer)
{
auto object = std::make_unique<FloatingObject>(renderer);
object->setShouldPaint(!renderer.hasSelfPaintingLayer()); // If a layer exists, the float will paint itself. Otherwise someone else will.
object->setIsDescendant(true);
return object;
}
std::unique_ptr<FloatingObject> FloatingObject::copyToNewContainer(LayoutSize offset, bool shouldPaint, bool isDescendant) const
{
// FIXME: Use make_unique here, once we can get it to compile on all platforms we support.
return std::unique_ptr<FloatingObject>(new FloatingObject(renderer(), type(), LayoutRect(frameRect().location() - offset, frameRect().size()), shouldPaint, isDescendant));
}
std::unique_ptr<FloatingObject> FloatingObject::unsafeClone() const
{
// FIXME: Use make_unique here, once we can get it to compile on all platforms we support.
std::unique_ptr<FloatingObject> cloneObject(new FloatingObject(renderer(), type(), m_frameRect, m_shouldPaint, m_isDescendant));
cloneObject->m_paginationStrut = m_paginationStrut;
cloneObject->m_isPlaced = m_isPlaced;
return cloneObject;
}
inline static bool rangesIntersect(LayoutUnit floatTop, LayoutUnit floatBottom, LayoutUnit objectTop, LayoutUnit objectBottom)
{
if (objectTop >= floatBottom || objectBottom < floatTop)
return false;
// The top of the object overlaps the float
if (objectTop >= floatTop)
return true;
// The object encloses the float
if (objectTop < floatTop && objectBottom > floatBottom)
return true;
// The bottom of the object overlaps the float
if (objectBottom > objectTop && objectBottom > floatTop && objectBottom <= floatBottom)
return true;
return false;
}
template <FloatingObject::Type FloatTypeValue>
class ComputeFloatOffsetAdapter {
public:
typedef FloatingObjectInterval IntervalType;
ComputeFloatOffsetAdapter(const RenderBlockFlow& renderer, LayoutUnit lineTop, LayoutUnit lineBottom, LayoutUnit offset)
: m_renderer(renderer)
, m_lineTop(lineTop)
, m_lineBottom(lineBottom)
, m_offset(offset)
, m_outermostFloat(0)
{
}
virtual ~ComputeFloatOffsetAdapter() { }
LayoutUnit lowValue() const { return m_lineTop; }
LayoutUnit highValue() const { return m_lineBottom; }
void collectIfNeeded(const IntervalType&);
LayoutUnit offset() const { return m_offset; }
protected:
virtual bool updateOffsetIfNeeded(const FloatingObject&) = 0;
const RenderBlockFlow& m_renderer;
LayoutUnit m_lineTop;
LayoutUnit m_lineBottom;
LayoutUnit m_offset;
const FloatingObject* m_outermostFloat;
};
template <FloatingObject::Type FloatTypeValue>
class ComputeFloatOffsetForFloatLayoutAdapter : public ComputeFloatOffsetAdapter<FloatTypeValue> {
public:
ComputeFloatOffsetForFloatLayoutAdapter(const RenderBlockFlow& renderer, LayoutUnit lineTop, LayoutUnit lineBottom, LayoutUnit offset)
: ComputeFloatOffsetAdapter<FloatTypeValue>(renderer, lineTop, lineBottom, offset)
{
}
virtual ~ComputeFloatOffsetForFloatLayoutAdapter() { }
LayoutUnit heightRemaining() const;
protected:
virtual bool updateOffsetIfNeeded(const FloatingObject&) override final;
};
template <FloatingObject::Type FloatTypeValue>
class ComputeFloatOffsetForLineLayoutAdapter : public ComputeFloatOffsetAdapter<FloatTypeValue> {
public:
ComputeFloatOffsetForLineLayoutAdapter(const RenderBlockFlow& renderer, LayoutUnit lineTop, LayoutUnit lineBottom, LayoutUnit offset)
: ComputeFloatOffsetAdapter<FloatTypeValue>(renderer, lineTop, lineBottom, offset)
{
}
virtual ~ComputeFloatOffsetForLineLayoutAdapter() { }
protected:
virtual bool updateOffsetIfNeeded(const FloatingObject&) override final;
};
class FindNextFloatLogicalBottomAdapter {
public:
typedef FloatingObjectInterval IntervalType;
FindNextFloatLogicalBottomAdapter(const RenderBlockFlow& renderer, LayoutUnit belowLogicalHeight)
: m_renderer(renderer)
, m_belowLogicalHeight(belowLogicalHeight)
, m_aboveLogicalHeight(LayoutUnit::max())
, m_nextLogicalBottom(LayoutUnit::max())
, m_nextShapeLogicalBottom(LayoutUnit::max())
{
}
LayoutUnit lowValue() const { return m_belowLogicalHeight; }
LayoutUnit highValue() const { return m_aboveLogicalHeight; }
void collectIfNeeded(const IntervalType&);
LayoutUnit nextLogicalBottom() { return m_nextLogicalBottom == LayoutUnit::max() ? LayoutUnit() : m_nextLogicalBottom; }
LayoutUnit nextShapeLogicalBottom() { return m_nextShapeLogicalBottom == LayoutUnit::max() ? nextLogicalBottom() : m_nextShapeLogicalBottom; }
private:
const RenderBlockFlow& m_renderer;
LayoutUnit m_belowLogicalHeight;
LayoutUnit m_aboveLogicalHeight;
LayoutUnit m_nextLogicalBottom;
LayoutUnit m_nextShapeLogicalBottom;
};
inline void FindNextFloatLogicalBottomAdapter::collectIfNeeded(const IntervalType& interval)
{
const FloatingObject* floatingObject = interval.data();
if (!rangesIntersect(interval.low(), interval.high(), m_belowLogicalHeight, m_aboveLogicalHeight))
return;
// All the objects returned from the tree should be already placed.
ASSERT(floatingObject->isPlaced());
ASSERT(rangesIntersect(m_renderer.logicalTopForFloat(floatingObject), m_renderer.logicalBottomForFloat(floatingObject), m_belowLogicalHeight, m_aboveLogicalHeight));
LayoutUnit floatBottom = m_renderer.logicalBottomForFloat(floatingObject);
if (m_nextLogicalBottom < floatBottom)
return;
#if ENABLE(CSS_SHAPES)
if (ShapeOutsideInfo* shapeOutside = floatingObject->renderer().shapeOutsideInfo()) {
LayoutUnit shapeBottom = m_renderer.logicalTopForFloat(floatingObject) + m_renderer.marginBeforeForChild(floatingObject->renderer()) + shapeOutside->shapeLogicalBottom();
// Use the shapeBottom unless it extends outside of the margin box, in which case it is clipped.
m_nextShapeLogicalBottom = std::min(shapeBottom, floatBottom);
} else
m_nextShapeLogicalBottom = floatBottom;
#endif
m_nextLogicalBottom = floatBottom;
}
LayoutUnit FloatingObjects::findNextFloatLogicalBottomBelow(LayoutUnit logicalHeight)
{
FindNextFloatLogicalBottomAdapter adapter(m_renderer, logicalHeight);
placedFloatsTree().allOverlapsWithAdapter(adapter);
return adapter.nextShapeLogicalBottom();
}
LayoutUnit FloatingObjects::findNextFloatLogicalBottomBelowForBlock(LayoutUnit logicalHeight)
{
FindNextFloatLogicalBottomAdapter adapter(m_renderer, logicalHeight);
placedFloatsTree().allOverlapsWithAdapter(adapter);
return adapter.nextLogicalBottom();
}
FloatingObjects::FloatingObjects(const RenderBlockFlow& renderer)
: m_placedFloatsTree(UninitializedTree)
, m_leftObjectsCount(0)
, m_rightObjectsCount(0)
, m_horizontalWritingMode(renderer.isHorizontalWritingMode())
, m_renderer(renderer)
{
}
FloatingObjects::~FloatingObjects()
{
}
void FloatingObjects::clearLineBoxTreePointers()
{
// Clear references to originating lines, since the lines are being deleted
for (auto it = m_set.begin(), end = m_set.end(); it != end; ++it) {
ASSERT(!((*it)->originatingLine()) || &((*it)->originatingLine()->renderer()) == &m_renderer);
(*it)->setOriginatingLine(0);
}
}
void FloatingObjects::clear()
{
m_set.clear();
m_placedFloatsTree.clear();
m_leftObjectsCount = 0;
m_rightObjectsCount = 0;
}
void FloatingObjects::moveAllToFloatInfoMap(RendererToFloatInfoMap& map)
{
for (auto it = m_set.begin(), end = m_set.end(); it != end; ++it) {
auto& renderer = it->get()->renderer();
// FIXME: The only reason it is safe to move these out of the set is that
// we are about to clear it. Otherwise it would break the hash table invariant.
// A clean way to do this would be to add a takeAll function to HashSet.
map.add(&renderer, WTF::move(*it));
}
clear();
}
void FloatingObjects::increaseObjectsCount(FloatingObject::Type type)
{
if (type == FloatingObject::FloatLeft)
m_leftObjectsCount++;
else
m_rightObjectsCount++;
}
void FloatingObjects::decreaseObjectsCount(FloatingObject::Type type)
{
if (type == FloatingObject::FloatLeft)
m_leftObjectsCount--;
else
m_rightObjectsCount--;
}
FloatingObjectInterval FloatingObjects::intervalForFloatingObject(FloatingObject* floatingObject)
{
// FIXME The endpoints of the floating object interval shouldn't need to be
// floored. See http://wkb.ug/125831 for more details.
if (m_horizontalWritingMode)
return FloatingObjectInterval(floatingObject->frameRect().y().floor(), floatingObject->frameRect().maxY().floor(), floatingObject);
return FloatingObjectInterval(floatingObject->frameRect().x().floor(), floatingObject->frameRect().maxX().floor(), floatingObject);
}
void FloatingObjects::addPlacedObject(FloatingObject* floatingObject)
{
ASSERT(!floatingObject->isInPlacedTree());
floatingObject->setIsPlaced(true);
if (m_placedFloatsTree.isInitialized())
m_placedFloatsTree.add(intervalForFloatingObject(floatingObject));
#ifndef NDEBUG
floatingObject->setIsInPlacedTree(true);
#endif
}
void FloatingObjects::removePlacedObject(FloatingObject* floatingObject)
{
ASSERT(floatingObject->isPlaced() && floatingObject->isInPlacedTree());
if (m_placedFloatsTree.isInitialized()) {
bool removed = m_placedFloatsTree.remove(intervalForFloatingObject(floatingObject));
ASSERT_UNUSED(removed, removed);
}
floatingObject->setIsPlaced(false);
#ifndef NDEBUG
floatingObject->setIsInPlacedTree(false);
#endif
}
FloatingObject* FloatingObjects::add(std::unique_ptr<FloatingObject> floatingObject)
{
increaseObjectsCount(floatingObject->type());
if (floatingObject->isPlaced())
addPlacedObject(floatingObject.get());
return m_set.add(WTF::move(floatingObject)).iterator->get();
}
void FloatingObjects::remove(FloatingObject* floatingObject)
{
ASSERT((m_set.contains<FloatingObject&, FloatingObjectHashTranslator>(*floatingObject)));
decreaseObjectsCount(floatingObject->type());
ASSERT(floatingObject->isPlaced() || !floatingObject->isInPlacedTree());
if (floatingObject->isPlaced())
removePlacedObject(floatingObject);
ASSERT(!floatingObject->originatingLine());
auto it = m_set.find<FloatingObject&, FloatingObjectHashTranslator>(*floatingObject);
if (it == m_set.end())
return;
m_set.remove(it);
}
void FloatingObjects::computePlacedFloatsTree()
{
ASSERT(!m_placedFloatsTree.isInitialized());
if (m_set.isEmpty())
return;
m_placedFloatsTree.initIfNeeded(m_renderer.view().intervalArena());
for (auto it = m_set.begin(), end = m_set.end(); it != end; ++it) {
FloatingObject* floatingObject = it->get();
if (floatingObject->isPlaced())
m_placedFloatsTree.add(intervalForFloatingObject(floatingObject));
}
}
inline const FloatingObjectTree& FloatingObjects::placedFloatsTree()
{
if (!m_placedFloatsTree.isInitialized())
computePlacedFloatsTree();
return m_placedFloatsTree;
}
LayoutUnit FloatingObjects::logicalLeftOffsetForPositioningFloat(LayoutUnit fixedOffset, LayoutUnit logicalTop, LayoutUnit *heightRemaining)
{
ComputeFloatOffsetForFloatLayoutAdapter<FloatingObject::FloatLeft> adapter(m_renderer, logicalTop, logicalTop, fixedOffset);
placedFloatsTree().allOverlapsWithAdapter(adapter);
if (heightRemaining)
*heightRemaining = adapter.heightRemaining();
return adapter.offset();
}
LayoutUnit FloatingObjects::logicalRightOffsetForPositioningFloat(LayoutUnit fixedOffset, LayoutUnit logicalTop, LayoutUnit *heightRemaining)
{
ComputeFloatOffsetForFloatLayoutAdapter<FloatingObject::FloatRight> adapter(m_renderer, logicalTop, logicalTop, fixedOffset);
placedFloatsTree().allOverlapsWithAdapter(adapter);
if (heightRemaining)
*heightRemaining = adapter.heightRemaining();
return std::min(fixedOffset, adapter.offset());
}
LayoutUnit FloatingObjects::logicalLeftOffset(LayoutUnit fixedOffset, LayoutUnit logicalTop, LayoutUnit logicalHeight)
{
ComputeFloatOffsetForLineLayoutAdapter<FloatingObject::FloatLeft> adapter(m_renderer, logicalTop, logicalTop + logicalHeight, fixedOffset);
placedFloatsTree().allOverlapsWithAdapter(adapter);
return adapter.offset();
}
LayoutUnit FloatingObjects::logicalRightOffset(LayoutUnit fixedOffset, LayoutUnit logicalTop, LayoutUnit logicalHeight)
{
ComputeFloatOffsetForLineLayoutAdapter<FloatingObject::FloatRight> adapter(m_renderer, logicalTop, logicalTop + logicalHeight, fixedOffset);
placedFloatsTree().allOverlapsWithAdapter(adapter);
return std::min(fixedOffset, adapter.offset());
}
template<>
inline bool ComputeFloatOffsetForFloatLayoutAdapter<FloatingObject::FloatLeft>::updateOffsetIfNeeded(const FloatingObject& floatingObject)
{
LayoutUnit logicalRight = m_renderer.logicalRightForFloat(&floatingObject);
if (logicalRight > m_offset) {
m_offset = logicalRight;
return true;
}
return false;
}
template<>
inline bool ComputeFloatOffsetForFloatLayoutAdapter<FloatingObject::FloatRight>::updateOffsetIfNeeded(const FloatingObject& floatingObject)
{
LayoutUnit logicalLeft = m_renderer.logicalLeftForFloat(&floatingObject);
if (logicalLeft < m_offset) {
m_offset = logicalLeft;
return true;
}
return false;
}
template <FloatingObject::Type FloatTypeValue>
LayoutUnit ComputeFloatOffsetForFloatLayoutAdapter<FloatTypeValue>::heightRemaining() const
{
return this->m_outermostFloat ? this->m_renderer.logicalBottomForFloat(this->m_outermostFloat) - this->m_lineTop : LayoutUnit::fromPixel(1);
}
template <FloatingObject::Type FloatTypeValue>
inline void ComputeFloatOffsetAdapter<FloatTypeValue>::collectIfNeeded(const IntervalType& interval)
{
const FloatingObject* floatingObject = interval.data();
if (floatingObject->type() != FloatTypeValue || !rangesIntersect(interval.low(), interval.high(), m_lineTop, m_lineBottom))
return;
// All the objects returned from the tree should be already placed.
ASSERT(floatingObject->isPlaced());
ASSERT(rangesIntersect(m_renderer.logicalTopForFloat(floatingObject), m_renderer.logicalBottomForFloat(floatingObject), m_lineTop, m_lineBottom));
bool floatIsNewExtreme = updateOffsetIfNeeded(*floatingObject);
if (floatIsNewExtreme)
m_outermostFloat = floatingObject;
}
template<>
inline bool ComputeFloatOffsetForLineLayoutAdapter<FloatingObject::FloatLeft>::updateOffsetIfNeeded(const FloatingObject& floatingObject)
{
LayoutUnit logicalRight = m_renderer.logicalRightForFloat(&floatingObject);
#if ENABLE(CSS_SHAPES)
if (ShapeOutsideInfo* shapeOutside = floatingObject.renderer().shapeOutsideInfo()) {
ShapeOutsideDeltas shapeDeltas = shapeOutside->computeDeltasForContainingBlockLine(m_renderer, floatingObject, m_lineTop, m_lineBottom - m_lineTop);
if (!shapeDeltas.lineOverlapsShape())
return false;
logicalRight += shapeDeltas.rightMarginBoxDelta();
}
#endif
if (logicalRight > m_offset) {
m_offset = logicalRight;
return true;
}
return false;
}
template<>
inline bool ComputeFloatOffsetForLineLayoutAdapter<FloatingObject::FloatRight>::updateOffsetIfNeeded(const FloatingObject& floatingObject)
{
LayoutUnit logicalLeft = m_renderer.logicalLeftForFloat(&floatingObject);
#if ENABLE(CSS_SHAPES)
if (ShapeOutsideInfo* shapeOutside = floatingObject.renderer().shapeOutsideInfo()) {
ShapeOutsideDeltas shapeDeltas = shapeOutside->computeDeltasForContainingBlockLine(m_renderer, floatingObject, m_lineTop, m_lineBottom - m_lineTop);
if (!shapeDeltas.lineOverlapsShape())
return false;
logicalLeft += shapeDeltas.leftMarginBoxDelta();
}
#endif
if (logicalLeft < m_offset) {
m_offset = logicalLeft;
return true;
}
return false;
}
} // namespace WebCore
|