#ifndef FloatingObjects_h
#define FloatingObjects_h
#include "PODIntervalTree.h"
#include "RootInlineBox.h"
#include <wtf/ListHashSet.h>
namespace WebCore {
class RenderBlockFlow;
class RenderBox;
class FloatingObject {
WTF_MAKE_NONCOPYABLE(FloatingObject); WTF_MAKE_FAST_ALLOCATED;
public:
enum Type { FloatLeft = 1, FloatRight = 2, FloatLeftRight = 3 };
static std::unique_ptr<FloatingObject> create(RenderBox&);
std::unique_ptr<FloatingObject> copyToNewContainer(LayoutSize, bool shouldPaint = false, bool isDescendant = false) const;
std::unique_ptr<FloatingObject> unsafeClone() const;
explicit FloatingObject(RenderBox&);
FloatingObject(RenderBox&, Type, const LayoutRect&, bool shouldPaint, bool isDescendant);
Type type() const { return static_cast<Type>(m_type); }
RenderBox& renderer() const { return m_renderer; }
bool isPlaced() const { return m_isPlaced; }
void setIsPlaced(bool placed = true) { m_isPlaced = placed; }
LayoutUnit x() const { ASSERT(isPlaced()); return m_frameRect.x(); }
LayoutUnit maxX() const { ASSERT(isPlaced()); return m_frameRect.maxX(); }
LayoutUnit y() const { ASSERT(isPlaced()); return m_frameRect.y(); }
LayoutUnit maxY() const { ASSERT(isPlaced()); return m_frameRect.maxY(); }
LayoutUnit width() const { return m_frameRect.width(); }
LayoutUnit height() const { return m_frameRect.height(); }
void setX(LayoutUnit x) { ASSERT(!isInPlacedTree()); m_frameRect.setX(x); }
void setY(LayoutUnit y) { ASSERT(!isInPlacedTree()); m_frameRect.setY(y); }
void setWidth(LayoutUnit width) { ASSERT(!isInPlacedTree()); m_frameRect.setWidth(width); }
void setHeight(LayoutUnit height) { ASSERT(!isInPlacedTree()); m_frameRect.setHeight(height); }
const LayoutRect& frameRect() const { ASSERT(isPlaced()); return m_frameRect; }
void setFrameRect(const LayoutRect& frameRect) { ASSERT(!isInPlacedTree()); m_frameRect = frameRect; }
LayoutUnit paginationStrut() const { return m_paginationStrut; }
void setPaginationStrut(LayoutUnit strut) { m_paginationStrut = strut; }
#ifndef NDEBUG
bool isInPlacedTree() const { return m_isInPlacedTree; }
void setIsInPlacedTree(bool value) { m_isInPlacedTree = value; }
#endif
bool shouldPaint() const { return m_shouldPaint; }
void setShouldPaint(bool shouldPaint) { m_shouldPaint = shouldPaint; }
bool isDescendant() const { return m_isDescendant; }
void setIsDescendant(bool isDescendant) { m_isDescendant = isDescendant; }
RootInlineBox* originatingLine() const { return m_originatingLine; }
void setOriginatingLine(RootInlineBox* line) { m_originatingLine = line; }
private:
RenderBox& m_renderer;
RootInlineBox* m_originatingLine;
LayoutRect m_frameRect;
LayoutUnit m_paginationStrut;
unsigned m_type : 2; unsigned m_shouldPaint : 1;
unsigned m_isDescendant : 1;
unsigned m_isPlaced : 1;
#ifndef NDEBUG
unsigned m_isInPlacedTree : 1;
#endif
};
struct FloatingObjectHashFunctions {
static unsigned hash(const std::unique_ptr<FloatingObject>& key) { return PtrHash<RenderBox*>::hash(&key->renderer()); }
static bool equal(const std::unique_ptr<FloatingObject>& a, const std::unique_ptr<FloatingObject>& b) { return &a->renderer() == &b->renderer(); }
static const bool safeToCompareToEmptyOrDeleted = true;
};
struct FloatingObjectHashTranslator {
static unsigned hash(const RenderBox& key) { return PtrHash<const RenderBox*>::hash(&key); }
static unsigned hash(const FloatingObject& key) { return PtrHash<RenderBox*>::hash(&key.renderer()); }
static bool equal(const std::unique_ptr<FloatingObject>& a, const RenderBox& b) { return &a->renderer() == &b; }
static bool equal(const std::unique_ptr<FloatingObject>& a, const FloatingObject& b) { return &a->renderer() == &b.renderer(); }
};
typedef ListHashSet<std::unique_ptr<FloatingObject>, FloatingObjectHashFunctions> FloatingObjectSet;
typedef PODInterval<LayoutUnit, FloatingObject*> FloatingObjectInterval;
typedef PODIntervalTree<LayoutUnit, FloatingObject*> FloatingObjectTree;
typedef HashMap<RenderBox*, std::unique_ptr<FloatingObject>> RendererToFloatInfoMap;
class FloatingObjects {
WTF_MAKE_NONCOPYABLE(FloatingObjects); WTF_MAKE_FAST_ALLOCATED;
public:
explicit FloatingObjects(const RenderBlockFlow&);
~FloatingObjects();
void clear();
void moveAllToFloatInfoMap(RendererToFloatInfoMap&);
FloatingObject* add(std::unique_ptr<FloatingObject>);
void remove(FloatingObject*);
void addPlacedObject(FloatingObject*);
void removePlacedObject(FloatingObject*);
void setHorizontalWritingMode(bool b = true) { m_horizontalWritingMode = b; }
bool hasLeftObjects() const { return m_leftObjectsCount > 0; }
bool hasRightObjects() const { return m_rightObjectsCount > 0; }
const FloatingObjectSet& set() const { return m_set; }
void clearLineBoxTreePointers();
LayoutUnit logicalLeftOffset(LayoutUnit fixedOffset, LayoutUnit logicalTop, LayoutUnit logicalHeight);
LayoutUnit logicalRightOffset(LayoutUnit fixedOffset, LayoutUnit logicalTop, LayoutUnit logicalHeight);
LayoutUnit logicalLeftOffsetForPositioningFloat(LayoutUnit fixedOffset, LayoutUnit logicalTop, LayoutUnit* heightRemaining);
LayoutUnit logicalRightOffsetForPositioningFloat(LayoutUnit fixedOffset, LayoutUnit logicalTop, LayoutUnit* heightRemaining);
LayoutUnit findNextFloatLogicalBottomBelow(LayoutUnit logicalHeight);
LayoutUnit findNextFloatLogicalBottomBelowForBlock(LayoutUnit logicalHeight);
private:
void computePlacedFloatsTree();
const FloatingObjectTree* placedFloatsTree();
void increaseObjectsCount(FloatingObject::Type);
void decreaseObjectsCount(FloatingObject::Type);
FloatingObjectInterval intervalForFloatingObject(FloatingObject*);
FloatingObjectSet m_set;
std::unique_ptr<FloatingObjectTree> m_placedFloatsTree;
unsigned m_leftObjectsCount;
unsigned m_rightObjectsCount;
bool m_horizontalWritingMode;
const RenderBlockFlow& m_renderer;
};
#ifndef NDEBUG
template<> struct ValueToString<FloatingObject*> {
static String string(const FloatingObject* floatingObject)
{
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());
}
};
#endif
}
#endif // FloatingObjects_h