Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / 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 "core/rendering/RenderRubyBase.h"
34 #include "core/rendering/RenderRubyRun.h"
35
36 namespace blink {
37
38 RenderRubyBase::RenderRubyBase()
39     : RenderBlockFlow(0)
40 {
41     setInline(false);
42 }
43
44 RenderRubyBase::~RenderRubyBase()
45 {
46 }
47
48 RenderRubyBase* RenderRubyBase::createAnonymous(Document* document)
49 {
50     RenderRubyBase* renderer = new RenderRubyBase();
51     renderer->setDocumentForAnonymous(document);
52     return renderer;
53 }
54
55 bool RenderRubyBase::isChildAllowed(RenderObject* child, RenderStyle*) const
56 {
57     return child->isInline();
58 }
59
60 void RenderRubyBase::moveChildren(RenderRubyBase* toBase, RenderObject* beforeChild)
61 {
62     // This function removes all children that are before (!) beforeChild
63     // and appends them to toBase.
64     ASSERT_ARG(toBase, toBase);
65
66     if (beforeChild && beforeChild->parent() != this)
67         beforeChild = splitAnonymousBoxesAroundChild(beforeChild);
68
69     if (childrenInline())
70         moveInlineChildren(toBase, beforeChild);
71     else
72         moveBlockChildren(toBase, beforeChild);
73
74     setNeedsLayoutAndPrefWidthsRecalcAndFullPaintInvalidation();
75     toBase->setNeedsLayoutAndPrefWidthsRecalcAndFullPaintInvalidation();
76 }
77
78 void RenderRubyBase::moveInlineChildren(RenderRubyBase* toBase, RenderObject* beforeChild)
79 {
80     ASSERT(childrenInline());
81     ASSERT_ARG(toBase, toBase);
82
83     if (!firstChild())
84         return;
85
86     RenderBlock* toBlock;
87     if (toBase->childrenInline()) {
88         // The standard and easy case: move the children into the target base
89         toBlock = toBase;
90     } else {
91         // We need to wrap the inline objects into an anonymous block.
92         // If toBase has a suitable block, we re-use it, otherwise create a new one.
93         RenderObject* lastChild = toBase->lastChild();
94         if (lastChild && lastChild->isAnonymousBlock() && lastChild->childrenInline())
95             toBlock = toRenderBlock(lastChild);
96         else {
97             toBlock = toBase->createAnonymousBlock();
98             toBase->children()->appendChildNode(toBase, toBlock);
99         }
100     }
101     // Move our inline children into the target block we determined above.
102     moveChildrenTo(toBlock, firstChild(), beforeChild);
103 }
104
105 void RenderRubyBase::moveBlockChildren(RenderRubyBase* toBase, RenderObject* beforeChild)
106 {
107     ASSERT(!childrenInline());
108     ASSERT_ARG(toBase, toBase);
109
110     if (!firstChild())
111         return;
112
113     if (toBase->childrenInline())
114         toBase->makeChildrenNonInline();
115
116     // If an anonymous block would be put next to another such block, then merge those.
117     RenderObject* firstChildHere = firstChild();
118     RenderObject* lastChildThere = toBase->lastChild();
119     if (firstChildHere->isAnonymousBlock() && firstChildHere->childrenInline()
120             && lastChildThere && lastChildThere->isAnonymousBlock() && lastChildThere->childrenInline()) {
121         RenderBlock* anonBlockHere = toRenderBlock(firstChildHere);
122         RenderBlock* anonBlockThere = toRenderBlock(lastChildThere);
123         anonBlockHere->moveAllChildrenTo(anonBlockThere, anonBlockThere->children());
124         anonBlockHere->deleteLineBoxTree();
125         anonBlockHere->destroy();
126     }
127     // Move all remaining children normally.
128     moveChildrenTo(toBase, firstChild(), beforeChild);
129 }
130
131 ETextAlign RenderRubyBase::textAlignmentForLine(bool /* endsWithSoftBreak */) const
132 {
133     return JUSTIFY;
134 }
135
136 void RenderRubyBase::adjustInlineDirectionLineBounds(unsigned expansionOpportunityCount, float& logicalLeft, float& logicalWidth) const
137 {
138     int maxPreferredLogicalWidth = this->maxPreferredLogicalWidth();
139     if (maxPreferredLogicalWidth >= logicalWidth)
140         return;
141
142     // Inset the ruby base by half the inter-ideograph expansion amount.
143     float inset = (logicalWidth - maxPreferredLogicalWidth) / (expansionOpportunityCount + 1);
144
145     logicalLeft += inset / 2;
146     logicalWidth -= inset;
147 }
148
149 } // namespace blink