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#ifndef TextAutoSizing_h
27#define TextAutoSizing_h
28
29#if ENABLE(IOS_TEXT_AUTOSIZING)
30
31#include "RenderStyle.h"
32#include <wtf/HashSet.h>
33#include <wtf/RefCounted.h>
34#include <wtf/RefPtr.h>
35
36namespace WebCore {
37
38class Document;
39class Node;
40
41class TextAutoSizingKey {
42public:
43    TextAutoSizingKey();
44    TextAutoSizingKey(RenderStyle*, Document*);
45    ~TextAutoSizingKey();
46    TextAutoSizingKey(const TextAutoSizingKey&);
47    TextAutoSizingKey& operator=(const TextAutoSizingKey&);
48    Document* doc() const { return m_doc; }
49    RenderStyle* style() const { return m_style; }
50    inline bool isValidDoc() const { return m_doc && m_doc != deletedKeyDoc(); }
51    inline bool isValidStyle() const { return m_style && m_style != deletedKeyStyle(); }
52    static Document* deletedKeyDoc() { return reinterpret_cast<Document*>(-1); }
53    static RenderStyle* deletedKeyStyle() { return reinterpret_cast<RenderStyle*>(-1); }
54private:
55    void ref() const;
56    void deref() const;
57    RenderStyle* m_style;
58    Document* m_doc;
59};
60
61inline bool operator==(const TextAutoSizingKey& a, const TextAutoSizingKey& b)
62{
63    if (a.isValidStyle() && b.isValidStyle())
64        return a.style()->equalForTextAutosizing(b.style());
65    return a.style() == b.style();
66}
67
68struct TextAutoSizingHash {
69    static unsigned hash(const TextAutoSizingKey& key) { return key.style()->hashForTextAutosizing(); }
70    static bool equal(const TextAutoSizingKey& a, const TextAutoSizingKey& b) { return a == b; }
71    static const bool safeToCompareToEmptyOrDeleted = true;
72};
73
74class TextAutoSizingValue : public RefCounted<TextAutoSizingValue> {
75public:
76    static PassRefPtr<TextAutoSizingValue> create()
77    {
78        return adoptRef(new TextAutoSizingValue);
79    }
80
81    void addNode(Node*, float size);
82    bool adjustNodeSizes();
83    int numNodes() const;
84    void reset();
85private:
86    TextAutoSizingValue() { }
87    HashSet<RefPtr<Node> > m_autoSizedNodes;
88};
89
90} // namespace WebCore
91
92#endif // ENABLE(IOS_TEXT_AUTOSIZING)
93
94#endif // TextAutoSizing_h
95