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 "core/rendering/RenderRubyBase.h"
34 #include "core/rendering/RenderRubyRun.h"
38 RenderRubyBase::RenderRubyBase()
44 RenderRubyBase::~RenderRubyBase()
48 RenderRubyBase* RenderRubyBase::createAnonymous(Document* document)
50 RenderRubyBase* renderer = new RenderRubyBase();
51 renderer->setDocumentForAnonymous(document);
55 bool RenderRubyBase::isChildAllowed(RenderObject* child, RenderStyle*) const
57 return child->isInline();
60 void RenderRubyBase::moveChildren(RenderRubyBase* toBase, RenderObject* beforeChild)
62 // This function removes all children that are before (!) beforeChild
63 // and appends them to toBase.
64 ASSERT_ARG(toBase, toBase);
66 if (beforeChild && beforeChild->parent() != this)
67 beforeChild = splitAnonymousBoxesAroundChild(beforeChild);
70 moveInlineChildren(toBase, beforeChild);
72 moveBlockChildren(toBase, beforeChild);
74 setNeedsLayoutAndPrefWidthsRecalcAndFullPaintInvalidation();
75 toBase->setNeedsLayoutAndPrefWidthsRecalcAndFullPaintInvalidation();
78 void RenderRubyBase::moveInlineChildren(RenderRubyBase* toBase, RenderObject* beforeChild)
80 ASSERT(childrenInline());
81 ASSERT_ARG(toBase, toBase);
87 if (toBase->childrenInline()) {
88 // The standard and easy case: move the children into the target base
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);
97 toBlock = toBase->createAnonymousBlock();
98 toBase->children()->appendChildNode(toBase, toBlock);
101 // Move our inline children into the target block we determined above.
102 moveChildrenTo(toBlock, firstChild(), beforeChild);
105 void RenderRubyBase::moveBlockChildren(RenderRubyBase* toBase, RenderObject* beforeChild)
107 ASSERT(!childrenInline());
108 ASSERT_ARG(toBase, toBase);
113 if (toBase->childrenInline())
114 toBase->makeChildrenNonInline();
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();
127 // Move all remaining children normally.
128 moveChildrenTo(toBase, firstChild(), beforeChild);
131 ETextAlign RenderRubyBase::textAlignmentForLine(bool /* endsWithSoftBreak */) const
136 void RenderRubyBase::adjustInlineDirectionLineBounds(unsigned expansionOpportunityCount, float& logicalLeft, float& logicalWidth) const
138 int maxPreferredLogicalWidth = this->maxPreferredLogicalWidth();
139 if (maxPreferredLogicalWidth >= logicalWidth)
142 // Inset the ruby base by half the inter-ideograph expansion amount.
143 float inset = (logicalWidth - maxPreferredLogicalWidth) / (expansionOpportunityCount + 1);
145 logicalLeft += inset / 2;
146 logicalWidth -= inset;