#pragma once
#if ENABLE(TEXT_AUTOSIZING)
#include "RenderStyle.h"
#include <wtf/HashMap.h>
#include <wtf/HashSet.h>
#include <wtf/RefCounted.h>
#include <wtf/RefPtr.h>
namespace WebCore {
class Document;
class Text;
class TextAutoSizingKey {
public:
TextAutoSizingKey() = default;
enum DeletedTag { Deleted };
explicit TextAutoSizingKey(DeletedTag);
TextAutoSizingKey(const RenderStyle&, unsigned hash);
const RenderStyle* style() const { ASSERT(!isDeleted()); return m_style.get(); }
bool isDeleted() const { return HashTraits<std::unique_ptr<RenderStyle>>::isDeletedValue(m_style); }
unsigned hash() const { return m_hash; }
private:
std::unique_ptr<RenderStyle> m_style;
unsigned m_hash { 0 };
};
inline bool operator==(const TextAutoSizingKey& a, const TextAutoSizingKey& b)
{
if (a.isDeleted() || b.isDeleted())
return false;
if (!a.style() || !b.style())
return a.style() == b.style();
return a.style()->equalForTextAutosizing(*b.style());
}
struct TextAutoSizingHash {
static unsigned hash(const TextAutoSizingKey& key) { return key.hash(); }
static bool equal(const TextAutoSizingKey& a, const TextAutoSizingKey& b) { return a == b; }
static const bool safeToCompareToEmptyOrDeleted = true;
};
struct TextAutoSizingHashTranslator {
static unsigned hash(const RenderStyle& style)
{
return style.hashForTextAutosizing();
}
static bool equal(const TextAutoSizingKey& key, const RenderStyle& style)
{
if (key.isDeleted() || !key.style())
return false;
return key.style()->equalForTextAutosizing(style);
}
static void translate(TextAutoSizingKey& key, const RenderStyle& style, unsigned hash)
{
key = { style, hash };
}
};
class TextAutoSizingValue {
WTF_MAKE_FAST_ALLOCATED;
public:
TextAutoSizingValue() = default;
~TextAutoSizingValue();
void addTextNode(Text&, float size);
enum class StillHasNodes { No, Yes };
StillHasNodes adjustTextNodeSizes();
private:
void reset();
HashSet<RefPtr<Text>> m_autoSizedNodes;
};
struct TextAutoSizingTraits : WTF::GenericHashTraits<TextAutoSizingKey> {
static const bool emptyValueIsZero = true;
static void constructDeletedValue(TextAutoSizingKey& slot) { new (NotNull, &slot) TextAutoSizingKey(TextAutoSizingKey::Deleted); }
static bool isDeletedValue(const TextAutoSizingKey& value) { return value.isDeleted(); }
};
class TextAutoSizing {
WTF_MAKE_FAST_ALLOCATED;
public:
TextAutoSizing() = default;
void addTextNode(Text&, float size);
void updateRenderTree();
void reset();
private:
HashMap<TextAutoSizingKey, std::unique_ptr<TextAutoSizingValue>, TextAutoSizingHash, TextAutoSizingTraits> m_textNodes;
};
}
#endif // ENABLE(TEXT_AUTOSIZING)