Fix the issue that Web Audio test case fails on PR3.
[framework/web/webkit-efl.git] / Source / WebCore / rendering / RenderRubyBase.cpp
1 /*
2  * Copyright (C) 2009 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 are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "config.h"
32
33 #include "RenderRubyBase.h"
34 #include "RenderRubyRun.h"
35 #include "RenderRubyText.h"
36
37 using namespace std;
38
39 namespace WebCore {
40
41 RenderRubyBase::RenderRubyBase(Node* node)
42     : RenderBlock(node)
43 {
44     setInline(false);
45 }
46
47 RenderRubyBase::~RenderRubyBase()
48 {
49 }
50
51 bool RenderRubyBase::isChildAllowed(RenderObject* child, RenderStyle*) const
52 {
53     return child->isInline();
54 }
55
56 void RenderRubyBase::moveChildren(RenderRubyBase* toBase, RenderObject* beforeChild)
57 {
58     // This function removes all children that are before (!) beforeChild
59     // and appends them to toBase.
60     ASSERT_ARG(toBase, toBase);
61
62     if (beforeChild && beforeChild->parent() != this)
63         beforeChild = splitAnonymousBoxesAroundChild(beforeChild);
64
65     if (childrenInline())
66         moveInlineChildren(toBase, beforeChild);
67     else
68         moveBlockChildren(toBase, beforeChild);
69
70     setNeedsLayoutAndPrefWidthsRecalc();
71     toBase->setNeedsLayoutAndPrefWidthsRecalc();
72 }
73
74 void RenderRubyBase::moveInlineChildren(RenderRubyBase* toBase, RenderObject* beforeChild)
75 {
76     ASSERT(childrenInline());
77     ASSERT_ARG(toBase, toBase);
78
79     if (!firstChild())
80         return;
81
82     RenderBlock* toBlock;
83     if (toBase->childrenInline()) {
84         // The standard and easy case: move the children into the target base
85         toBlock = toBase;
86     } else {
87         // We need to wrap the inline objects into an anonymous block.
88         // If toBase has a suitable block, we re-use it, otherwise create a new one.
89         RenderObject* lastChild = toBase->lastChild();
90         if (lastChild && lastChild->isAnonymousBlock() && lastChild->childrenInline())
91             toBlock = toRenderBlock(lastChild);
92         else {
93             toBlock = toBase->createAnonymousBlock();
94             toBase->children()->appendChildNode(toBase, toBlock);
95         }
96     }
97     // Move our inline children into the target block we determined above.
98     moveChildrenTo(toBlock, firstChild(), beforeChild);
99 }
100
101 void RenderRubyBase::moveBlockChildren(RenderRubyBase* toBase, RenderObject* beforeChild)
102 {
103     ASSERT(!childrenInline());
104     ASSERT_ARG(toBase, toBase);
105
106     if (!firstChild())
107         return;
108
109     if (toBase->childrenInline())
110         toBase->makeChildrenNonInline();
111
112     // If an anonymous block would be put next to another such block, then merge those.
113     RenderObject* firstChildHere = firstChild();
114     RenderObject* lastChildThere = toBase->lastChild();
115     if (firstChildHere->isAnonymousBlock() && firstChildHere->childrenInline() 
116             && lastChildThere && lastChildThere->isAnonymousBlock() && lastChildThere->childrenInline()) {            
117         RenderBlock* anonBlockHere = toRenderBlock(firstChildHere);
118         RenderBlock* anonBlockThere = toRenderBlock(lastChildThere);
119         anonBlockHere->moveAllChildrenTo(anonBlockThere, anonBlockThere->children());
120         anonBlockHere->deleteLineBoxTree();
121         anonBlockHere->destroy();
122     }
123     // Move all remaining children normally.
124     moveChildrenTo(toBase, firstChild(), beforeChild);
125 }
126
127 RenderRubyRun* RenderRubyBase::rubyRun() const
128 {
129     ASSERT(parent());
130     ASSERT(parent()->isRubyRun());
131
132     return toRenderRubyRun(parent());
133 }
134
135 ETextAlign RenderRubyBase::textAlignmentForLine(bool /* endsWithSoftBreak */) const
136 {
137     return JUSTIFY;
138 }
139
140 void RenderRubyBase::adjustInlineDirectionLineBounds(int expansionOpportunityCount, float& logicalLeft, float& logicalWidth) const
141 {
142     int maxPreferredLogicalWidth = this->maxPreferredLogicalWidth();
143     if (maxPreferredLogicalWidth >= logicalWidth)
144         return;
145
146     // Inset the ruby base by half the inter-ideograph expansion amount.
147     float inset = (logicalWidth - maxPreferredLogicalWidth) / (expansionOpportunityCount + 1);
148
149     logicalLeft += inset / 2;
150     logicalWidth -= inset;
151 }
152
153 } // namespace WebCore