- add third_party src.
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / rendering / TextAutosizer.h
1 /*
2  * Copyright (C) 2012 Google 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  *
8  * 1.  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer.
10  * 2.  Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #ifndef TextAutosizer_h
27 #define TextAutosizer_h
28
29 #include "HTMLNames.h"
30 #include "platform/text/WritingMode.h"
31 #include "wtf/Noncopyable.h"
32 #include "wtf/PassOwnPtr.h"
33
34 namespace WebCore {
35
36 class Document;
37 class RenderBlock;
38 class RenderObject;
39 class RenderText;
40 struct TextAutosizingWindowInfo;
41 struct TextAutosizingClusterInfo;
42
43 class TextAutosizer FINAL {
44     WTF_MAKE_NONCOPYABLE(TextAutosizer);
45
46 public:
47     static PassOwnPtr<TextAutosizer> create(Document* document) { return adoptPtr(new TextAutosizer(document)); }
48
49     bool processSubtree(RenderObject* layoutRoot);
50     void recalculateMultipliers();
51
52     static float computeAutosizedFontSize(float specifiedSize, float multiplier);
53
54 private:
55     enum TraversalDirection {
56         FirstToLast,
57         LastToFirst
58     };
59
60     enum ContentType {
61         Unknown,
62         Default,
63         VBulletin
64     };
65
66     explicit TextAutosizer(Document*);
67
68     float clusterMultiplier(WritingMode, const TextAutosizingWindowInfo&, float textWidth) const;
69
70     void processClusterInternal(TextAutosizingClusterInfo&, RenderBlock* container, RenderObject* subtreeRoot, const TextAutosizingWindowInfo&, float multiplier);
71     void processCluster(TextAutosizingClusterInfo&, RenderBlock* container, RenderObject* subtreeRoot, const TextAutosizingWindowInfo&);
72     void processCompositeCluster(Vector<TextAutosizingClusterInfo>&, const TextAutosizingWindowInfo&);
73     void processContainer(float multiplier, RenderBlock* container, TextAutosizingClusterInfo&, RenderObject* subtreeRoot, const TextAutosizingWindowInfo&);
74
75     bool clusterShouldBeAutosized(TextAutosizingClusterInfo&, float blockWidth);
76     bool compositeClusterShouldBeAutosized(Vector<TextAutosizingClusterInfo>&, float blockWidth);
77     bool clusterContainsForumComment(const RenderBlock* container, TextAutosizingClusterInfo&);
78
79     void setMultiplier(RenderObject*, float);
80     void setMultiplierForList(RenderObject* renderer, float multiplier);
81
82     ContentType detectContentType();
83
84     static bool isAutosizingContainer(const RenderObject*);
85     static bool isNarrowDescendant(const RenderBlock*, TextAutosizingClusterInfo& parentClusterInfo);
86     static bool isWiderDescendant(const RenderBlock*, const TextAutosizingClusterInfo& parentClusterInfo);
87     static bool isIndependentDescendant(const RenderBlock*);
88     static bool isAutosizingCluster(const RenderBlock*, TextAutosizingClusterInfo& parentClusterInfo);
89
90     static bool containerShouldBeAutosized(const RenderBlock* container);
91     static bool containerContainsOneOfTags(const RenderBlock* cluster, const Vector<QualifiedName>& tags);
92     static bool containerIsRowOfLinks(const RenderObject* container);
93     static bool contentHeightIsConstrained(const RenderBlock* container);
94     static void measureDescendantTextWidth(const RenderBlock* container, TextAutosizingClusterInfo&, float minTextWidth, float& textWidth);
95
96     // Use to traverse the tree of descendants, excluding descendants of containers (but returning the containers themselves).
97     static RenderObject* nextInPreOrderSkippingDescendantsOfContainers(const RenderObject*, const RenderObject* stayWithin);
98
99     static const RenderBlock* findDeepestBlockContainingAllText(const RenderBlock* cluster);
100
101     // Depending on the traversal direction specified, finds the first or the last leaf text node child that doesn't
102     // belong to any cluster.
103     static const RenderObject* findFirstTextLeafNotInCluster(const RenderObject*, size_t& depth, TraversalDirection);
104
105     // Returns groups of narrow descendants of a given autosizing cluster. The groups are combined
106     // by the difference between the width of the descendant and the width of the parent cluster's
107     // |blockContainingAllText|.
108     static void getNarrowDescendantsGroupedByWidth(const TextAutosizingClusterInfo& parentClusterInfo, Vector<Vector<TextAutosizingClusterInfo> >&);
109
110     Document* m_document;
111     ContentType m_contentType;
112 };
113
114 } // namespace WebCore
115
116 #endif // TextAutosizer_h