2 * Copyright (C) 2009 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
33 #include "RenderRubyBase.h"
34 #include "RenderRubyRun.h"
35 #include "RenderRubyText.h"
41 RenderRubyBase::RenderRubyBase(Node* node)
47 RenderRubyBase::~RenderRubyBase()
51 bool RenderRubyBase::isChildAllowed(RenderObject* child, RenderStyle*) const
53 return child->isInline();
56 void RenderRubyBase::moveChildren(RenderRubyBase* toBase, RenderObject* beforeChild)
58 // This function removes all children that are before (!) beforeChild
59 // and appends them to toBase.
60 ASSERT_ARG(toBase, toBase);
62 if (beforeChild && beforeChild->parent() != this)
63 beforeChild = splitAnonymousBoxesAroundChild(beforeChild);
66 moveInlineChildren(toBase, beforeChild);
68 moveBlockChildren(toBase, beforeChild);
70 setNeedsLayoutAndPrefWidthsRecalc();
71 toBase->setNeedsLayoutAndPrefWidthsRecalc();
74 void RenderRubyBase::moveInlineChildren(RenderRubyBase* toBase, RenderObject* beforeChild)
76 ASSERT(childrenInline());
77 ASSERT_ARG(toBase, toBase);
83 if (toBase->childrenInline()) {
84 // The standard and easy case: move the children into the target base
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);
93 toBlock = toBase->createAnonymousBlock();
94 toBase->children()->appendChildNode(toBase, toBlock);
97 // Move our inline children into the target block we determined above.
98 moveChildrenTo(toBlock, firstChild(), beforeChild);
101 void RenderRubyBase::moveBlockChildren(RenderRubyBase* toBase, RenderObject* beforeChild)
103 ASSERT(!childrenInline());
104 ASSERT_ARG(toBase, toBase);
109 if (toBase->childrenInline())
110 toBase->makeChildrenNonInline();
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();
123 // Move all remaining children normally.
124 moveChildrenTo(toBase, firstChild(), beforeChild);
127 RenderRubyRun* RenderRubyBase::rubyRun() const
130 ASSERT(parent()->isRubyRun());
132 return toRenderRubyRun(parent());
135 ETextAlign RenderRubyBase::textAlignmentForLine(bool /* endsWithSoftBreak */) const
140 void RenderRubyBase::adjustInlineDirectionLineBounds(int expansionOpportunityCount, float& logicalLeft, float& logicalWidth) const
142 int maxPreferredLogicalWidth = this->maxPreferredLogicalWidth();
143 if (maxPreferredLogicalWidth >= logicalWidth)
146 // Inset the ruby base by half the inter-ideograph expansion amount.
147 float inset = (logicalWidth - maxPreferredLogicalWidth) / (expansionOpportunityCount + 1);
149 logicalLeft += inset / 2;
150 logicalWidth -= inset;
153 } // namespace WebCore