1/*
2 * Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012, 2013 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#if ENABLE(TEXT_AUTOSIZING)
29
30#include "RenderStyle.h"
31#include <wtf/HashMap.h>
32#include <wtf/HashSet.h>
33#include <wtf/RefCounted.h>
34#include <wtf/RefPtr.h>
35
36namespace WebCore {
37
38class Document;
39class Text;
40
41// FIXME: We can probably get rid of this class entirely and use std::unique_ptr<RenderStyle> as key
42// as long as we use the right hash traits.
43class TextAutoSizingKey {
44public:
45 TextAutoSizingKey() = default;
46 enum DeletedTag { Deleted };
47 explicit TextAutoSizingKey(DeletedTag);
48 TextAutoSizingKey(const RenderStyle&, unsigned hash);
49
50 const RenderStyle* style() const { ASSERT(!isDeleted()); return m_style.get(); }
51 bool isDeleted() const { return HashTraits<std::unique_ptr<RenderStyle>>::isDeletedValue(m_style); }
52
53 unsigned hash() const { return m_hash; }
54
55private:
56 std::unique_ptr<RenderStyle> m_style;
57 unsigned m_hash { 0 };
58};
59
60inline bool operator==(const TextAutoSizingKey& a, const TextAutoSizingKey& b)
61{
62 if (a.isDeleted() || b.isDeleted())
63 return false;
64 if (!a.style() || !b.style())
65 return a.style() == b.style();
66 return a.style()->equalForTextAutosizing(*b.style());
67}
68
69struct TextAutoSizingHash {
70 static unsigned hash(const TextAutoSizingKey& key) { return key.hash(); }
71 static bool equal(const TextAutoSizingKey& a, const TextAutoSizingKey& b) { return a == b; }
72 static const bool safeToCompareToEmptyOrDeleted = true;
73};
74
75struct TextAutoSizingHashTranslator {
76 static unsigned hash(const RenderStyle& style)
77 {
78 return style.hashForTextAutosizing();
79 }
80
81 static bool equal(const TextAutoSizingKey& key, const RenderStyle& style)
82 {
83 if (key.isDeleted() || !key.style())
84 return false;
85 return key.style()->equalForTextAutosizing(style);
86 }
87
88 static void translate(TextAutoSizingKey& key, const RenderStyle& style, unsigned hash)
89 {
90 key = { style, hash };
91 }
92};
93
94class TextAutoSizingValue {
95 WTF_MAKE_FAST_ALLOCATED;
96public:
97 TextAutoSizingValue() = default;
98 ~TextAutoSizingValue();
99
100 void addTextNode(Text&, float size);
101
102 enum class StillHasNodes { No, Yes };
103 StillHasNodes adjustTextNodeSizes();
104
105private:
106 void reset();
107
108 HashSet<RefPtr<Text>> m_autoSizedNodes;
109};
110
111struct TextAutoSizingTraits : WTF::GenericHashTraits<TextAutoSizingKey> {
112 static const bool emptyValueIsZero = true;
113 static void constructDeletedValue(TextAutoSizingKey& slot) { new (NotNull, &slot) TextAutoSizingKey(TextAutoSizingKey::Deleted); }
114 static bool isDeletedValue(const TextAutoSizingKey& value) { return value.isDeleted(); }
115};
116
117class TextAutoSizing {
118 WTF_MAKE_FAST_ALLOCATED;
119public:
120 TextAutoSizing() = default;
121
122 void addTextNode(Text&, float size);
123 void updateRenderTree();
124 void reset();
125
126private:
127 HashMap<TextAutoSizingKey, std::unique_ptr<TextAutoSizingValue>, TextAutoSizingHash, TextAutoSizingTraits> m_textNodes;
128};
129
130} // namespace WebCore
131
132#endif // ENABLE(TEXT_AUTOSIZING)
133