#ifndef FastTextAutosizer_h
#define FastTextAutosizer_h
#include "core/rendering/RenderObject.h"
#include "core/rendering/RenderTable.h"
#include "wtf/HashMap.h"
#include "wtf/HashSet.h"
#include "wtf/Noncopyable.h"
#include "wtf/OwnPtr.h"
#include "wtf/PassOwnPtr.h"
namespace WebCore {
class Document;
class RenderBlock;
class RenderListItem;
class RenderListMarker;
class FastTextAutosizer FINAL {
WTF_MAKE_NONCOPYABLE(FastTextAutosizer);
public:
static PassOwnPtr<FastTextAutosizer> create(const Document* document)
{
return adoptPtr(new FastTextAutosizer(document));
}
void updatePageInfoInAllFrames();
void updatePageInfo();
void record(const RenderBlock*);
void destroy(const RenderBlock*);
void inflateListItem(RenderListItem*, RenderListMarker*);
class LayoutScope {
public:
explicit LayoutScope(RenderBlock*);
~LayoutScope();
private:
FastTextAutosizer* m_textAutosizer;
RenderBlock* m_block;
};
class DeferUpdatePageInfo {
public:
explicit DeferUpdatePageInfo(Page*);
~DeferUpdatePageInfo();
private:
RefPtr<LocalFrame> m_mainFrame;
};
private:
typedef HashSet<const RenderBlock*> BlockSet;
enum HasEnoughTextToAutosize {
UnknownAmountOfText,
HasEnoughText,
NotEnoughText
};
enum RelayoutBehavior {
AlreadyInLayout,
LayoutNeeded
};
struct Supercluster {
explicit Supercluster(const BlockSet* roots)
: m_roots(roots)
, m_multiplier(0)
{
}
const BlockSet* const m_roots;
float m_multiplier;
};
struct Cluster {
explicit Cluster(const RenderBlock* root, bool autosize, Cluster* parent, Supercluster* supercluster = 0)
: m_root(root)
, m_deepestBlockContainingAllText(0)
, m_parent(parent)
, m_autosize(autosize)
, m_multiplier(0)
, m_hasEnoughTextToAutosize(UnknownAmountOfText)
, m_supercluster(supercluster)
, m_hasTableAncestor(root->isTableCell() || (m_parent && m_parent->m_hasTableAncestor))
{
}
const RenderBlock* const m_root;
const RenderBlock* m_deepestBlockContainingAllText;
Cluster* m_parent;
bool m_autosize;
float m_multiplier;
HasEnoughTextToAutosize m_hasEnoughTextToAutosize;
Supercluster* m_supercluster;
bool m_hasTableAncestor;
};
enum TextLeafSearch {
First,
Last
};
struct FingerprintSourceData {
FingerprintSourceData()
: m_parentHash(0)
, m_qualifiedNameHash(0)
, m_packedStyleProperties(0)
, m_column(0)
, m_width(0)
{
}
unsigned m_parentHash;
unsigned m_qualifiedNameHash;
unsigned m_packedStyleProperties;
unsigned m_column;
float m_width;
};
COMPILE_ASSERT(!(sizeof(FingerprintSourceData) % sizeof(UChar)),
Sizeof_FingerprintSourceData_must_be_multiple_of_UChar);
typedef unsigned Fingerprint;
typedef HashMap<Fingerprint, OwnPtr<Supercluster> > SuperclusterMap;
typedef Vector<OwnPtr<Cluster> > ClusterStack;
class FingerprintMapper {
public:
void add(const RenderObject*, Fingerprint);
void addTentativeClusterRoot(const RenderBlock*, Fingerprint);
void remove(const RenderObject*);
Fingerprint get(const RenderObject*);
BlockSet& getTentativeClusterRoots(Fingerprint);
private:
typedef HashMap<const RenderObject*, Fingerprint> FingerprintMap;
typedef HashMap<Fingerprint, OwnPtr<BlockSet> > ReverseFingerprintMap;
FingerprintMap m_fingerprints;
ReverseFingerprintMap m_blocksForFingerprint;
#ifndef NDEBUG
void assertMapsAreConsistent();
#endif
};
explicit FastTextAutosizer(const Document*);
void beginLayout(RenderBlock*);
void endLayout(RenderBlock*);
void inflateTable(RenderTable*);
void inflate(RenderBlock*);
bool enabled() const;
bool shouldHandleLayout() const;
void setAllTextNeedsLayout();
void resetMultipliers();
void prepareClusterStack(const RenderObject*);
bool isFingerprintingCandidate(const RenderBlock*);
bool clusterHasEnoughTextToAutosize(Cluster*, const RenderBlock* widthProvider = 0);
bool anyClusterHasEnoughTextToAutosize(const BlockSet* roots, const RenderBlock* widthProvider = 0);
bool clusterWouldHaveEnoughTextToAutosize(const RenderBlock* root, const RenderBlock* widthProvider = 0);
Fingerprint getFingerprint(const RenderObject*);
Fingerprint computeFingerprint(const RenderObject*);
Cluster* maybeCreateCluster(const RenderBlock*);
Supercluster* getSupercluster(const RenderBlock*);
const RenderBlock* deepestCommonAncestor(BlockSet&);
float clusterMultiplier(Cluster*);
float superclusterMultiplier(Cluster*);
const RenderBlock* clusterWidthProvider(const RenderBlock*);
float widthFromBlock(const RenderBlock*);
float multiplierFromBlock(const RenderBlock*);
void applyMultiplier(RenderObject*, float, RelayoutBehavior = AlreadyInLayout);
bool isWiderOrNarrowerDescendant(Cluster*);
bool isLayoutRoot(const RenderBlock*) const;
Cluster* currentCluster() const;
RenderObject* nextChildSkippingChildrenOfBlocks(const RenderObject*, const RenderObject*);
const RenderBlock* deepestBlockContainingAllText(Cluster*);
const RenderBlock* deepestBlockContainingAllText(const RenderBlock*);
const RenderObject* findTextLeaf(const RenderObject*, size_t&, TextLeafSearch);
const Document* m_document;
int m_frameWidth;
int m_layoutWidth;
float m_baseMultiplier;
bool m_pageNeedsAutosizing;
bool m_previouslyAutosized;
bool m_updatePageInfoDeferred;
const RenderBlock* m_firstBlock;
#ifndef NDEBUG
BlockSet m_blocksThatHaveBegunLayout;
#endif
SuperclusterMap m_superclusters;
ClusterStack m_clusterStack;
FingerprintMapper m_fingerprintMapper;
};
}
#endif