Move parseFontFaceDescriptor to CSSPropertyParser.cpp
[chromium-blink-merge.git] / third_party / WebKit / Source / core / layout / LayoutRubyBase.cpp
blob2e2bae4195e2ba49801fb0b13782dac1584fdddf
1 /*
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
6 * met:
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.
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.
31 #include "config.h"
33 #include "core/layout/LayoutRubyBase.h"
35 #include "core/layout/LayoutRubyRun.h"
37 namespace blink {
39 LayoutRubyBase::LayoutRubyBase()
40 : LayoutBlockFlow(nullptr)
42 setInline(false);
45 LayoutRubyBase::~LayoutRubyBase()
49 LayoutRubyBase* LayoutRubyBase::createAnonymous(Document* document)
51 LayoutRubyBase* layoutObject = new LayoutRubyBase();
52 layoutObject->setDocumentForAnonymous(document);
53 return layoutObject;
56 bool LayoutRubyBase::isChildAllowed(LayoutObject* child, const ComputedStyle&) const
58 return child->isInline();
61 void LayoutRubyBase::moveChildren(LayoutRubyBase* toBase, LayoutObject* beforeChild)
63 // This function removes all children that are before (!) beforeChild
64 // and appends them to toBase.
65 ASSERT_ARG(toBase, toBase);
67 if (beforeChild && beforeChild->parent() != this)
68 beforeChild = splitAnonymousBoxesAroundChild(beforeChild);
70 if (childrenInline())
71 moveInlineChildren(toBase, beforeChild);
72 else
73 moveBlockChildren(toBase, beforeChild);
75 setNeedsLayoutAndPrefWidthsRecalcAndFullPaintInvalidation(LayoutInvalidationReason::Unknown);
76 toBase->setNeedsLayoutAndPrefWidthsRecalcAndFullPaintInvalidation(LayoutInvalidationReason::Unknown);
79 void LayoutRubyBase::moveInlineChildren(LayoutRubyBase* toBase, LayoutObject* beforeChild)
81 ASSERT(childrenInline());
82 ASSERT_ARG(toBase, toBase);
84 if (!firstChild())
85 return;
87 LayoutBlock* toBlock;
88 if (toBase->childrenInline()) {
89 // The standard and easy case: move the children into the target base
90 toBlock = toBase;
91 } else {
92 // We need to wrap the inline objects into an anonymous block.
93 // If toBase has a suitable block, we re-use it, otherwise create a new one.
94 LayoutObject* lastChild = toBase->lastChild();
95 if (lastChild && lastChild->isAnonymousBlock() && lastChild->childrenInline()) {
96 toBlock = toLayoutBlock(lastChild);
97 } else {
98 toBlock = toBase->createAnonymousBlock();
99 toBase->children()->appendChildNode(toBase, toBlock);
102 // Move our inline children into the target block we determined above.
103 moveChildrenTo(toBlock, firstChild(), beforeChild);
106 void LayoutRubyBase::moveBlockChildren(LayoutRubyBase* toBase, LayoutObject* beforeChild)
108 ASSERT(!childrenInline());
109 ASSERT_ARG(toBase, toBase);
111 if (!firstChild())
112 return;
114 if (toBase->childrenInline())
115 toBase->makeChildrenNonInline();
117 // If an anonymous block would be put next to another such block, then merge those.
118 LayoutObject* firstChildHere = firstChild();
119 LayoutObject* lastChildThere = toBase->lastChild();
120 if (firstChildHere->isAnonymousBlock() && firstChildHere->childrenInline()
121 && lastChildThere && lastChildThere->isAnonymousBlock() && lastChildThere->childrenInline()) {
122 LayoutBlock* anonBlockHere = toLayoutBlock(firstChildHere);
123 LayoutBlock* anonBlockThere = toLayoutBlock(lastChildThere);
124 anonBlockHere->moveAllChildrenTo(anonBlockThere, anonBlockThere->children());
125 anonBlockHere->deleteLineBoxTree();
126 anonBlockHere->destroy();
128 // Move all remaining children normally.
129 moveChildrenTo(toBase, firstChild(), beforeChild);
132 ETextAlign LayoutRubyBase::textAlignmentForLine(bool /* endsWithSoftBreak */) const
134 return JUSTIFY;
137 void LayoutRubyBase::adjustInlineDirectionLineBounds(unsigned expansionOpportunityCount, LayoutUnit& logicalLeft, LayoutUnit& logicalWidth) const
139 int maxPreferredLogicalWidth = this->maxPreferredLogicalWidth();
140 if (maxPreferredLogicalWidth >= logicalWidth)
141 return;
143 unsigned maxCount = static_cast<unsigned>(LayoutUnit::max().floor());
144 if (expansionOpportunityCount > maxCount)
145 expansionOpportunityCount = maxCount;
147 // Inset the ruby base by half the inter-ideograph expansion amount.
148 LayoutUnit inset = (logicalWidth - maxPreferredLogicalWidth) / (expansionOpportunityCount + 1);
150 logicalLeft += inset / 2;
151 logicalWidth -= inset;
154 } // namespace blink