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 "RenderRubyRun.h"
35 #include "RenderRubyBase.h"
36 #include "RenderRubyText.h"
37 #include "RenderText.h"
38 #include "RenderView.h"
44 RenderRubyRun::RenderRubyRun(Node* node)
51 RenderRubyRun::~RenderRubyRun()
55 bool RenderRubyRun::hasRubyText() const
57 // The only place where a ruby text can be is in the first position
58 // Note: As anonymous blocks, ruby runs do not have ':before' or ':after' content themselves.
59 return firstChild() && firstChild()->isRubyText();
62 bool RenderRubyRun::hasRubyBase() const
64 // The only place where a ruby base can be is in the last position
65 // Note: As anonymous blocks, ruby runs do not have ':before' or ':after' content themselves.
66 return lastChild() && lastChild()->isRubyBase();
69 bool RenderRubyRun::isEmpty() const
71 return !hasRubyText() && !hasRubyBase();
74 RenderRubyText* RenderRubyRun::rubyText() const
76 RenderObject* child = firstChild();
77 // If in future it becomes necessary to support floating or positioned ruby text,
78 // layout will have to be changed to handle them properly.
79 ASSERT(!child || !child->isRubyText() || !child->isFloatingOrOutOfFlowPositioned());
80 return child && child->isRubyText() ? static_cast<RenderRubyText*>(child) : 0;
83 RenderRubyBase* RenderRubyRun::rubyBase() const
85 RenderObject* child = lastChild();
86 return child && child->isRubyBase() ? static_cast<RenderRubyBase*>(child) : 0;
89 RenderRubyBase* RenderRubyRun::rubyBaseSafe()
91 RenderRubyBase* base = rubyBase();
93 base = createRubyBase();
94 RenderBlock::addChild(base);
99 RenderBlock* RenderRubyRun::firstLineBlock() const
104 void RenderRubyRun::updateFirstLetter()
108 bool RenderRubyRun::isChildAllowed(RenderObject* child, RenderStyle*) const
110 return child->isRubyText() || child->isInline();
113 void RenderRubyRun::addChild(RenderObject* child, RenderObject* beforeChild)
117 if (child->isRubyText()) {
119 // RenderRuby has already ascertained that we can add the child here.
120 ASSERT(!hasRubyText());
121 // prepend ruby texts as first child
122 RenderBlock::addChild(child, firstChild());
123 } else if (beforeChild->isRubyText()) {
124 // New text is inserted just before another.
125 // In this case the new text takes the place of the old one, and
126 // the old text goes into a new run that is inserted as next sibling.
127 ASSERT(beforeChild->parent() == this);
128 RenderObject* ruby = parent();
129 ASSERT(ruby->isRuby());
130 RenderBlock* newRun = staticCreateRubyRun(ruby);
131 ruby->addChild(newRun, nextSibling());
132 // Add the new ruby text and move the old one to the new run
133 // Note: Doing it in this order and not using RenderRubyRun's methods,
134 // in order to avoid automatic removal of the ruby run in case there is no
135 // other child besides the old ruby text.
136 RenderBlock::addChild(child, beforeChild);
137 RenderBlock::removeChild(beforeChild);
138 newRun->addChild(beforeChild);
139 } else if (hasRubyBase()) {
140 // Insertion before a ruby base object.
141 // In this case we need insert a new run before the current one and split the base.
142 RenderObject* ruby = parent();
143 RenderRubyRun* newRun = staticCreateRubyRun(ruby);
144 ruby->addChild(newRun, this);
145 newRun->addChild(child);
146 rubyBaseSafe()->moveChildren(newRun->rubyBaseSafe(), beforeChild);
149 // child is not a text -> insert it into the base
150 // (append it instead if beforeChild is the ruby text)
151 if (beforeChild && beforeChild->isRubyText())
153 rubyBaseSafe()->addChild(child, beforeChild);
157 void RenderRubyRun::removeChild(RenderObject* child)
159 // If the child is a ruby text, then merge the ruby base with the base of
160 // the right sibling run, if possible.
161 if (!beingDestroyed() && !documentBeingDestroyed() && child->isRubyText()) {
162 RenderRubyBase* base = rubyBase();
163 RenderObject* rightNeighbour = nextSibling();
164 if (base && rightNeighbour && rightNeighbour->isRubyRun()) {
165 // Ruby run without a base can happen only at the first run.
166 RenderRubyRun* rightRun = toRenderRubyRun(rightNeighbour);
167 if (rightRun->hasRubyBase()) {
168 RenderRubyBase* rightBase = rightRun->rubyBaseSafe();
169 // Collect all children in a single base, then swap the bases.
170 rightBase->moveChildren(base);
171 moveChildTo(rightRun, base);
172 rightRun->moveChildTo(this, rightBase);
173 // The now empty ruby base will be removed below.
174 ASSERT(!rubyBase()->firstChild());
179 RenderBlock::removeChild(child);
181 if (!beingDestroyed() && !documentBeingDestroyed()) {
182 // Check if our base (if any) is now empty. If so, destroy it.
183 RenderBlock* base = rubyBase();
184 if (base && !base->firstChild()) {
185 RenderBlock::removeChild(base);
186 base->deleteLineBoxTree();
190 // If any of the above leaves the run empty, destroy it as well.
192 parent()->removeChild(this);
199 RenderRubyBase* RenderRubyRun::createRubyBase() const
201 RenderRubyBase* rb = new (renderArena()) RenderRubyBase(document() /* anonymous */);
202 RefPtr<RenderStyle> newStyle = RenderStyle::createAnonymousStyleWithDisplay(style(), BLOCK);
203 newStyle->setTextAlign(CENTER); // FIXME: use WEBKIT_CENTER?
204 rb->setStyle(newStyle.release());
208 RenderRubyRun* RenderRubyRun::staticCreateRubyRun(const RenderObject* parentRuby)
210 ASSERT(parentRuby && parentRuby->isRuby());
211 RenderRubyRun* rr = new (parentRuby->renderArena()) RenderRubyRun(parentRuby->document() /* anonymous */);
212 RefPtr<RenderStyle> newStyle = RenderStyle::createAnonymousStyleWithDisplay(parentRuby->style(), INLINE_BLOCK);
213 rr->setStyle(newStyle.release());
217 RenderObject* RenderRubyRun::layoutSpecialExcludedChild(bool relayoutChildren)
219 // Don't bother positioning the RenderRubyRun yet.
220 RenderRubyText* rt = rubyText();
223 if (relayoutChildren)
224 rt->setChildNeedsLayout(true, MarkOnlyThis);
225 rt->layoutIfNeeded();
229 void RenderRubyRun::layout()
231 RenderBlock::layout();
233 // Place the RenderRubyText such that its bottom is flush with the lineTop of the first line of the RenderRubyBase.
234 RenderRubyText* rt = rubyText();
238 LayoutUnit lastLineRubyTextBottom = rt->logicalHeight();
239 LayoutUnit firstLineRubyTextTop = 0;
240 RootInlineBox* rootBox = rt->lastRootBox();
242 // In order to align, we have to ignore negative leading.
243 firstLineRubyTextTop = rt->firstRootBox()->logicalTopLayoutOverflow();
244 lastLineRubyTextBottom = rootBox->logicalBottomLayoutOverflow();
247 if (!style()->isFlippedLinesWritingMode()) {
248 LayoutUnit firstLineTop = 0;
249 if (RenderRubyBase* rb = rubyBase()) {
250 RootInlineBox* rootBox = rb->firstRootBox();
252 firstLineTop = rootBox->logicalTopLayoutOverflow();
253 firstLineTop += rb->logicalTop();
256 rt->setLogicalTop(-lastLineRubyTextBottom + firstLineTop);
258 LayoutUnit lastLineBottom = logicalHeight();
259 if (RenderRubyBase* rb = rubyBase()) {
260 RootInlineBox* rootBox = rb->lastRootBox();
262 lastLineBottom = rootBox->logicalBottomLayoutOverflow();
263 lastLineBottom += rb->logicalTop();
266 rt->setLogicalTop(-firstLineRubyTextTop + lastLineBottom);
269 // Update our overflow to account for the new RenderRubyText position.
271 computeOverflow(clientLogicalBottom());
274 void RenderRubyRun::getOverhang(bool firstLine, RenderObject* startRenderer, RenderObject* endRenderer, int& startOverhang, int& endOverhang) const
276 ASSERT(!needsLayout());
281 RenderRubyBase* rubyBase = this->rubyBase();
282 RenderRubyText* rubyText = this->rubyText();
284 if (!rubyBase || !rubyText)
287 if (!rubyBase->firstRootBox())
290 int logicalWidth = this->logicalWidth();
291 int logicalLeftOverhang = numeric_limits<int>::max();
292 int logicalRightOverhang = numeric_limits<int>::max();
293 for (RootInlineBox* rootInlineBox = rubyBase->firstRootBox(); rootInlineBox; rootInlineBox = rootInlineBox->nextRootBox()) {
294 logicalLeftOverhang = min<int>(logicalLeftOverhang, rootInlineBox->logicalLeft());
295 logicalRightOverhang = min<int>(logicalRightOverhang, logicalWidth - rootInlineBox->logicalRight());
298 startOverhang = style()->isLeftToRightDirection() ? logicalLeftOverhang : logicalRightOverhang;
299 endOverhang = style()->isLeftToRightDirection() ? logicalRightOverhang : logicalLeftOverhang;
301 if (!startRenderer || !startRenderer->isText() || startRenderer->style(firstLine)->fontSize() > rubyBase->style(firstLine)->fontSize())
304 if (!endRenderer || !endRenderer->isText() || endRenderer->style(firstLine)->fontSize() > rubyBase->style(firstLine)->fontSize())
307 // We overhang a ruby only if the neighboring render object is a text.
308 // We can overhang the ruby by no more than half the width of the neighboring text
309 // and no more than half the font size.
310 int halfWidthOfFontSize = rubyText->style(firstLine)->fontSize() / 2;
312 startOverhang = min<int>(startOverhang, min<int>(toRenderText(startRenderer)->minLogicalWidth(), halfWidthOfFontSize));
314 endOverhang = min<int>(endOverhang, min<int>(toRenderText(endRenderer)->minLogicalWidth(), halfWidthOfFontSize));
317 } // namespace WebCore