2 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
21 #include "core/rendering/InlineFlowBox.h"
23 #include "core/CSSPropertyNames.h"
24 #include "core/dom/Document.h"
25 #include "core/paint/BoxPainter.h"
26 #include "core/paint/InlineFlowBoxPainter.h"
27 #include "core/rendering/HitTestResult.h"
28 #include "core/rendering/InlineTextBox.h"
29 #include "core/rendering/RenderBlock.h"
30 #include "core/rendering/RenderInline.h"
31 #include "core/rendering/RenderLayer.h"
32 #include "core/rendering/RenderListMarker.h"
33 #include "core/rendering/RenderObjectInlines.h"
34 #include "core/rendering/RenderRubyBase.h"
35 #include "core/rendering/RenderRubyRun.h"
36 #include "core/rendering/RenderRubyText.h"
37 #include "core/rendering/RenderView.h"
38 #include "core/rendering/RootInlineBox.h"
39 #include "platform/fonts/Font.h"
40 #include "platform/graphics/GraphicsContextStateSaver.h"
46 struct SameSizeAsInlineFlowBox : public InlineBox {
48 uint32_t bitfields : 23;
51 COMPILE_ASSERT(sizeof(InlineFlowBox) == sizeof(SameSizeAsInlineFlowBox), InlineFlowBox_should_stay_small);
55 InlineFlowBox::~InlineFlowBox()
57 if (!m_hasBadChildList)
58 for (InlineBox* child = firstChild(); child; child = child->nextOnLine())
59 child->setHasBadParent();
64 LayoutUnit InlineFlowBox::getFlowSpacingLogicalWidth()
66 LayoutUnit totWidth = marginBorderPaddingLogicalLeft() + marginBorderPaddingLogicalRight();
67 for (InlineBox* curr = firstChild(); curr; curr = curr->nextOnLine()) {
68 if (curr->isInlineFlowBox())
69 totWidth += toInlineFlowBox(curr)->getFlowSpacingLogicalWidth();
74 IntRect InlineFlowBox::roundedFrameRect() const
76 // Begin by snapping the x and y coordinates to the nearest pixel.
77 int snappedX = lroundf(x());
78 int snappedY = lroundf(y());
80 int snappedMaxX = lroundf(x() + width());
81 int snappedMaxY = lroundf(y() + height());
83 return IntRect(snappedX, snappedY, snappedMaxX - snappedX, snappedMaxY - snappedY);
86 static void setHasTextDescendantsOnAncestors(InlineFlowBox* box)
88 while (box && !box->hasTextDescendants()) {
89 box->setHasTextDescendants();
94 void InlineFlowBox::addToLine(InlineBox* child)
96 ASSERT(!child->parent());
97 ASSERT(!child->nextOnLine());
98 ASSERT(!child->prevOnLine());
101 child->setParent(this);
103 m_firstChild = child;
106 m_lastChild->setNextOnLine(child);
107 child->setPrevOnLine(m_lastChild);
110 child->setFirstLineStyleBit(isFirstLineStyle());
111 child->setIsHorizontal(isHorizontal());
112 if (child->isText()) {
113 if (child->renderer().parent() == renderer())
114 m_hasTextChildren = true;
115 setHasTextDescendantsOnAncestors(this);
116 } else if (child->isInlineFlowBox()) {
117 if (toInlineFlowBox(child)->hasTextDescendants())
118 setHasTextDescendantsOnAncestors(this);
121 if (descendantsHaveSameLineHeightAndBaseline() && !child->renderer().isOutOfFlowPositioned()) {
122 RenderStyle* parentStyle = renderer().style(isFirstLineStyle());
123 RenderStyle* childStyle = child->renderer().style(isFirstLineStyle());
124 bool shouldClearDescendantsHaveSameLineHeightAndBaseline = false;
125 if (child->renderer().isReplaced())
126 shouldClearDescendantsHaveSameLineHeightAndBaseline = true;
127 else if (child->isText()) {
128 if (child->renderer().isBR() || child->renderer().parent() != renderer()) {
129 if (!parentStyle->font().fontMetrics().hasIdenticalAscentDescentAndLineGap(childStyle->font().fontMetrics())
130 || parentStyle->lineHeight() != childStyle->lineHeight()
131 || (parentStyle->verticalAlign() != BASELINE && !isRootInlineBox()) || childStyle->verticalAlign() != BASELINE)
132 shouldClearDescendantsHaveSameLineHeightAndBaseline = true;
134 if (childStyle->hasTextCombine() || childStyle->textEmphasisMark() != TextEmphasisMarkNone)
135 shouldClearDescendantsHaveSameLineHeightAndBaseline = true;
137 if (child->renderer().isBR()) {
138 // FIXME: This is dumb. We only turn off because current layout test results expect the <br> to be 0-height on the baseline.
139 // Other than making a zillion tests have to regenerate results, there's no reason to ditch the optimization here.
140 shouldClearDescendantsHaveSameLineHeightAndBaseline = true;
142 ASSERT(isInlineFlowBox());
143 InlineFlowBox* childFlowBox = toInlineFlowBox(child);
144 // Check the child's bit, and then also check for differences in font, line-height, vertical-align
145 if (!childFlowBox->descendantsHaveSameLineHeightAndBaseline()
146 || !parentStyle->font().fontMetrics().hasIdenticalAscentDescentAndLineGap(childStyle->font().fontMetrics())
147 || parentStyle->lineHeight() != childStyle->lineHeight()
148 || (parentStyle->verticalAlign() != BASELINE && !isRootInlineBox()) || childStyle->verticalAlign() != BASELINE
149 || childStyle->hasBorder() || childStyle->hasPadding() || childStyle->hasTextCombine())
150 shouldClearDescendantsHaveSameLineHeightAndBaseline = true;
154 if (shouldClearDescendantsHaveSameLineHeightAndBaseline)
155 clearDescendantsHaveSameLineHeightAndBaseline();
158 if (!child->renderer().isOutOfFlowPositioned()) {
159 if (child->isText()) {
160 RenderStyle* childStyle = child->renderer().style(isFirstLineStyle());
161 if (childStyle->letterSpacing() < 0 || childStyle->textShadow() || childStyle->textEmphasisMark() != TextEmphasisMarkNone || childStyle->textStrokeWidth())
162 child->clearKnownToHaveNoOverflow();
163 } else if (child->renderer().isReplaced()) {
164 RenderBox& box = toRenderBox(child->renderer());
165 if (box.hasRenderOverflow() || box.hasSelfPaintingLayer())
166 child->clearKnownToHaveNoOverflow();
167 } else if (!child->renderer().isBR() && (child->renderer().style(isFirstLineStyle())->boxShadow() || child->boxModelObject()->hasSelfPaintingLayer()
168 || (child->renderer().isListMarker() && !toRenderListMarker(child->renderer()).isInside())
169 || child->renderer().style(isFirstLineStyle())->hasBorderImageOutsets()
170 || child->renderer().style(isFirstLineStyle())->hasOutline())) {
171 child->clearKnownToHaveNoOverflow();
174 if (knownToHaveNoOverflow() && child->isInlineFlowBox() && !toInlineFlowBox(child)->knownToHaveNoOverflow())
175 clearKnownToHaveNoOverflow();
181 void InlineFlowBox::removeChild(InlineBox* child, MarkLineBoxes markDirty)
185 if (markDirty == MarkLineBoxesDirty && !isDirty())
188 root().childRemoved(child);
190 if (child == m_firstChild)
191 m_firstChild = child->nextOnLine();
192 if (child == m_lastChild)
193 m_lastChild = child->prevOnLine();
194 if (child->nextOnLine())
195 child->nextOnLine()->setPrevOnLine(child->prevOnLine());
196 if (child->prevOnLine())
197 child->prevOnLine()->setNextOnLine(child->nextOnLine());
204 void InlineFlowBox::deleteLine()
206 InlineBox* child = firstChild();
209 ASSERT(this == child->parent());
210 next = child->nextOnLine();
222 removeLineBoxFromRenderObject();
226 void InlineFlowBox::removeLineBoxFromRenderObject()
228 rendererLineBoxes()->removeLineBox(this);
231 void InlineFlowBox::extractLine()
234 extractLineBoxFromRenderObject();
235 for (InlineBox* child = firstChild(); child; child = child->nextOnLine())
236 child->extractLine();
239 void InlineFlowBox::extractLineBoxFromRenderObject()
241 rendererLineBoxes()->extractLineBox(this);
244 void InlineFlowBox::attachLine()
247 attachLineBoxToRenderObject();
248 for (InlineBox* child = firstChild(); child; child = child->nextOnLine())
252 void InlineFlowBox::attachLineBoxToRenderObject()
254 rendererLineBoxes()->attachLineBox(this);
257 void InlineFlowBox::adjustPosition(float dx, float dy)
259 InlineBox::adjustPosition(dx, dy);
260 for (InlineBox* child = firstChild(); child; child = child->nextOnLine())
261 child->adjustPosition(dx, dy);
263 m_overflow->move(dx, dy); // FIXME: Rounding error here since overflow was pixel snapped, but nobody other than list markers passes non-integral values here.
266 RenderLineBoxList* InlineFlowBox::rendererLineBoxes() const
268 return toRenderInline(renderer()).lineBoxes();
271 static inline bool isLastChildForRenderer(RenderObject* ancestor, RenderObject* child)
276 if (child == ancestor)
279 RenderObject* curr = child;
280 RenderObject* parent = curr->parent();
281 while (parent && (!parent->isRenderBlock() || parent->isInline())) {
282 if (parent->slowLastChild() != curr)
284 if (parent == ancestor)
288 parent = curr->parent();
294 static bool isAnsectorAndWithinBlock(RenderObject* ancestor, RenderObject* child)
296 RenderObject* object = child;
297 while (object && (!object->isRenderBlock() || object->isInline())) {
298 if (object == ancestor)
300 object = object->parent();
305 void InlineFlowBox::determineSpacingForFlowBoxes(bool lastLine, bool isLogicallyLastRunWrapped, RenderObject* logicallyLastRunRenderer)
307 // All boxes start off open. They will not apply any margins/border/padding on
309 bool includeLeftEdge = false;
310 bool includeRightEdge = false;
312 // The root inline box never has borders/margins/padding.
314 bool ltr = renderer().style()->isLeftToRightDirection();
316 // Check to see if all initial lines are unconstructed. If so, then
317 // we know the inline began on this line (unless we are a continuation).
318 RenderLineBoxList* lineBoxList = rendererLineBoxes();
319 if (!lineBoxList->firstLineBox()->isConstructed() && !renderer().isInlineElementContinuation()) {
320 if (renderer().style()->boxDecorationBreak() == DCLONE)
321 includeLeftEdge = includeRightEdge = true;
322 else if (ltr && lineBoxList->firstLineBox() == this)
323 includeLeftEdge = true;
324 else if (!ltr && lineBoxList->lastLineBox() == this)
325 includeRightEdge = true;
328 if (!lineBoxList->lastLineBox()->isConstructed()) {
329 RenderInline& inlineFlow = toRenderInline(renderer());
330 bool isLastObjectOnLine = !isAnsectorAndWithinBlock(&renderer(), logicallyLastRunRenderer) || (isLastChildForRenderer(&renderer(), logicallyLastRunRenderer) && !isLogicallyLastRunWrapped);
332 // We include the border under these conditions:
333 // (1) The next line was not created, or it is constructed. We check the previous line for rtl.
334 // (2) The logicallyLastRun is not a descendant of this renderer.
335 // (3) The logicallyLastRun is a descendant of this renderer, but it is the last child of this renderer and it does not wrap to the next line.
336 // (4) The decoration break is set to clone therefore there will be borders on every sides.
337 if (renderer().style()->boxDecorationBreak() == DCLONE)
338 includeLeftEdge = includeRightEdge = true;
341 && ((lastLine || isLastObjectOnLine) && !inlineFlow.continuation()))
342 includeRightEdge = true;
344 if ((!prevLineBox() || prevLineBox()->isConstructed())
345 && ((lastLine || isLastObjectOnLine) && !inlineFlow.continuation()))
346 includeLeftEdge = true;
351 setEdges(includeLeftEdge, includeRightEdge);
353 // Recur into our children.
354 for (InlineBox* currChild = firstChild(); currChild; currChild = currChild->nextOnLine()) {
355 if (currChild->isInlineFlowBox()) {
356 InlineFlowBox* currFlow = toInlineFlowBox(currChild);
357 currFlow->determineSpacingForFlowBoxes(lastLine, isLogicallyLastRunWrapped, logicallyLastRunRenderer);
362 float InlineFlowBox::placeBoxesInInlineDirection(float logicalLeft, bool& needsWordSpacing)
364 // Set our x position.
365 beginPlacingBoxRangesInInlineDirection(logicalLeft);
367 float startLogicalLeft = logicalLeft;
368 logicalLeft += borderLogicalLeft() + paddingLogicalLeft();
370 float minLogicalLeft = startLogicalLeft;
371 float maxLogicalRight = logicalLeft;
373 placeBoxRangeInInlineDirection(firstChild(), 0, logicalLeft, minLogicalLeft, maxLogicalRight, needsWordSpacing);
375 logicalLeft += borderLogicalRight() + paddingLogicalRight();
376 endPlacingBoxRangesInInlineDirection(startLogicalLeft, logicalLeft, minLogicalLeft, maxLogicalRight);
380 float InlineFlowBox::placeBoxRangeInInlineDirection(InlineBox* firstChild, InlineBox* lastChild,
381 float& logicalLeft, float& minLogicalLeft, float& maxLogicalRight, bool& needsWordSpacing)
383 for (InlineBox* curr = firstChild; curr && curr != lastChild; curr = curr->nextOnLine()) {
384 if (curr->renderer().isText()) {
385 InlineTextBox* text = toInlineTextBox(curr);
386 RenderText& rt = text->renderer();
388 if (rt.textLength()) {
389 if (needsWordSpacing && isSpaceOrNewline(rt.characterAt(text->start())))
390 space = rt.style(isFirstLineStyle())->font().fontDescription().wordSpacing();
391 needsWordSpacing = !isSpaceOrNewline(rt.characterAt(text->end()));
393 if (isLeftToRightDirection()) {
394 logicalLeft += space;
395 text->setLogicalLeft(logicalLeft);
397 text->setLogicalLeft(logicalLeft);
398 logicalLeft += space;
400 if (knownToHaveNoOverflow())
401 minLogicalLeft = std::min(logicalLeft, minLogicalLeft);
402 logicalLeft += text->logicalWidth();
403 if (knownToHaveNoOverflow())
404 maxLogicalRight = std::max(logicalLeft, maxLogicalRight);
406 if (curr->renderer().isOutOfFlowPositioned()) {
407 if (curr->renderer().parent()->style()->isLeftToRightDirection()) {
408 curr->setLogicalLeft(logicalLeft);
410 // Our offset that we cache needs to be from the edge of the right border box and
411 // not the left border box. We have to subtract |x| from the width of the block
412 // (which can be obtained from the root line box).
413 curr->setLogicalLeft(root().block().logicalWidth() - logicalLeft);
415 continue; // The positioned object has no effect on the width.
417 if (curr->renderer().isRenderInline()) {
418 InlineFlowBox* flow = toInlineFlowBox(curr);
419 logicalLeft += flow->marginLogicalLeft();
420 if (knownToHaveNoOverflow())
421 minLogicalLeft = std::min(logicalLeft, minLogicalLeft);
422 logicalLeft = flow->placeBoxesInInlineDirection(logicalLeft, needsWordSpacing);
423 if (knownToHaveNoOverflow())
424 maxLogicalRight = std::max(logicalLeft, maxLogicalRight);
425 logicalLeft += flow->marginLogicalRight();
426 } else if (!curr->renderer().isListMarker() || toRenderListMarker(curr->renderer()).isInside()) {
427 // The box can have a different writing-mode than the overall line, so this is a bit complicated.
428 // Just get all the physical margin and overflow values by hand based off |isVertical|.
429 LayoutUnit logicalLeftMargin = isHorizontal() ? curr->boxModelObject()->marginLeft() : curr->boxModelObject()->marginTop();
430 LayoutUnit logicalRightMargin = isHorizontal() ? curr->boxModelObject()->marginRight() : curr->boxModelObject()->marginBottom();
432 logicalLeft += logicalLeftMargin;
433 curr->setLogicalLeft(logicalLeft);
434 if (knownToHaveNoOverflow())
435 minLogicalLeft = std::min(logicalLeft, minLogicalLeft);
436 logicalLeft += curr->logicalWidth();
437 if (knownToHaveNoOverflow())
438 maxLogicalRight = std::max(logicalLeft, maxLogicalRight);
439 logicalLeft += logicalRightMargin;
440 // If we encounter any space after this inline block then ensure it is treated as the space between two words.
441 needsWordSpacing = true;
448 bool InlineFlowBox::requiresIdeographicBaseline(const GlyphOverflowAndFallbackFontsMap& textBoxDataMap) const
453 if (renderer().style(isFirstLineStyle())->fontDescription().nonCJKGlyphOrientation() == NonCJKGlyphOrientationUpright
454 || renderer().style(isFirstLineStyle())->font().primaryFont()->hasVerticalGlyphs())
457 for (InlineBox* curr = firstChild(); curr; curr = curr->nextOnLine()) {
458 if (curr->renderer().isOutOfFlowPositioned())
459 continue; // Positioned placeholders don't affect calculations.
461 if (curr->isInlineFlowBox()) {
462 if (toInlineFlowBox(curr)->requiresIdeographicBaseline(textBoxDataMap))
465 if (curr->renderer().style(isFirstLineStyle())->font().primaryFont()->hasVerticalGlyphs())
468 const Vector<const SimpleFontData*>* usedFonts = 0;
469 if (curr->isInlineTextBox()) {
470 GlyphOverflowAndFallbackFontsMap::const_iterator it = textBoxDataMap.find(toInlineTextBox(curr));
471 usedFonts = it == textBoxDataMap.end() ? 0 : &it->value.first;
475 for (size_t i = 0; i < usedFonts->size(); ++i) {
476 if (usedFonts->at(i)->hasVerticalGlyphs())
486 void InlineFlowBox::adjustMaxAscentAndDescent(int& maxAscent, int& maxDescent, int maxPositionTop, int maxPositionBottom)
488 for (InlineBox* curr = firstChild(); curr; curr = curr->nextOnLine()) {
489 // The computed lineheight needs to be extended for the
490 // positioned elements
491 if (curr->renderer().isOutOfFlowPositioned())
492 continue; // Positioned placeholders don't affect calculations.
493 if (curr->verticalAlign() == TOP || curr->verticalAlign() == BOTTOM) {
494 int lineHeight = curr->lineHeight();
495 if (curr->verticalAlign() == TOP) {
496 if (maxAscent + maxDescent < lineHeight)
497 maxDescent = lineHeight - maxAscent;
500 if (maxAscent + maxDescent < lineHeight)
501 maxAscent = lineHeight - maxDescent;
504 if (maxAscent + maxDescent >= std::max(maxPositionTop, maxPositionBottom))
508 if (curr->isInlineFlowBox())
509 toInlineFlowBox(curr)->adjustMaxAscentAndDescent(maxAscent, maxDescent, maxPositionTop, maxPositionBottom);
513 void InlineFlowBox::computeLogicalBoxHeights(RootInlineBox* rootBox, LayoutUnit& maxPositionTop, LayoutUnit& maxPositionBottom,
514 int& maxAscent, int& maxDescent, bool& setMaxAscent, bool& setMaxDescent,
515 bool strictMode, GlyphOverflowAndFallbackFontsMap& textBoxDataMap,
516 FontBaseline baselineType, VerticalPositionCache& verticalPositionCache)
518 // The primary purpose of this function is to compute the maximal ascent and descent values for
519 // a line. These values are computed based off the block's line-box-contain property, which indicates
520 // what parts of descendant boxes have to fit within the line.
522 // The maxAscent value represents the distance of the highest point of any box (typically including line-height) from
523 // the root box's baseline. The maxDescent value represents the distance of the lowest point of any box
524 // (also typically including line-height) from the root box baseline. These values can be negative.
526 // A secondary purpose of this function is to store the offset of every box's baseline from the root box's
527 // baseline. This information is cached in the logicalTop() of every box. We're effectively just using
528 // the logicalTop() as scratch space.
530 // Because a box can be positioned such that it ends up fully above or fully below the
531 // root line box, we only consider it to affect the maxAscent and maxDescent values if some
532 // part of the box (EXCLUDING leading) is above (for ascent) or below (for descent) the root box's baseline.
533 bool affectsAscent = false;
534 bool affectsDescent = false;
535 bool checkChildren = !descendantsHaveSameLineHeightAndBaseline();
537 if (isRootInlineBox()) {
538 // Examine our root box.
541 rootBox->ascentAndDescentForBox(rootBox, textBoxDataMap, ascent, descent, affectsAscent, affectsDescent);
542 if (strictMode || hasTextChildren() || (!checkChildren && hasTextDescendants())) {
543 if (maxAscent < ascent || !setMaxAscent) {
547 if (maxDescent < descent || !setMaxDescent) {
548 maxDescent = descent;
549 setMaxDescent = true;
557 for (InlineBox* curr = firstChild(); curr; curr = curr->nextOnLine()) {
558 if (curr->renderer().isOutOfFlowPositioned())
559 continue; // Positioned placeholders don't affect calculations.
561 InlineFlowBox* inlineFlowBox = curr->isInlineFlowBox() ? toInlineFlowBox(curr) : 0;
563 bool affectsAscent = false;
564 bool affectsDescent = false;
566 // The verticalPositionForBox function returns the distance between the child box's baseline
567 // and the root box's baseline. The value is negative if the child box's baseline is above the
568 // root box's baseline, and it is positive if the child box's baseline is below the root box's baseline.
569 curr->setLogicalTop(rootBox->verticalPositionForBox(curr, verticalPositionCache).toFloat());
573 rootBox->ascentAndDescentForBox(curr, textBoxDataMap, ascent, descent, affectsAscent, affectsDescent);
575 LayoutUnit boxHeight = ascent + descent;
576 if (curr->verticalAlign() == TOP) {
577 if (maxPositionTop < boxHeight)
578 maxPositionTop = boxHeight;
579 } else if (curr->verticalAlign() == BOTTOM) {
580 if (maxPositionBottom < boxHeight)
581 maxPositionBottom = boxHeight;
582 } else if (!inlineFlowBox || strictMode || inlineFlowBox->hasTextChildren() || (inlineFlowBox->descendantsHaveSameLineHeightAndBaseline() && inlineFlowBox->hasTextDescendants())
583 || inlineFlowBox->boxModelObject()->hasInlineDirectionBordersOrPadding()) {
584 // Note that these values can be negative. Even though we only affect the maxAscent and maxDescent values
585 // if our box (excluding line-height) was above (for ascent) or below (for descent) the root baseline, once you factor in line-height
586 // the final box can end up being fully above or fully below the root box's baseline! This is ok, but what it
587 // means is that ascent and descent (including leading), can end up being negative. The setMaxAscent and
588 // setMaxDescent booleans are used to ensure that we're willing to initially set maxAscent/Descent to negative
590 ascent -= curr->logicalTop();
591 descent += curr->logicalTop();
592 if (affectsAscent && (maxAscent < ascent || !setMaxAscent)) {
597 if (affectsDescent && (maxDescent < descent || !setMaxDescent)) {
598 maxDescent = descent;
599 setMaxDescent = true;
604 inlineFlowBox->computeLogicalBoxHeights(rootBox, maxPositionTop, maxPositionBottom, maxAscent, maxDescent,
605 setMaxAscent, setMaxDescent, strictMode, textBoxDataMap,
606 baselineType, verticalPositionCache);
610 void InlineFlowBox::placeBoxesInBlockDirection(LayoutUnit top, LayoutUnit maxHeight, int maxAscent, bool strictMode, LayoutUnit& lineTop, LayoutUnit& lineBottom, LayoutUnit& selectionBottom, bool& setLineTop,
611 LayoutUnit& lineTopIncludingMargins, LayoutUnit& lineBottomIncludingMargins, bool& hasAnnotationsBefore, bool& hasAnnotationsAfter, FontBaseline baselineType)
613 bool isRootBox = isRootInlineBox();
615 const FontMetrics& fontMetrics = renderer().style(isFirstLineStyle())->fontMetrics();
616 // RootInlineBoxes are always placed on at pixel boundaries in their logical y direction. Not doing
617 // so results in incorrect rendering of text decorations, most notably underlines.
618 setLogicalTop(roundToInt(top + maxAscent - fontMetrics.ascent(baselineType)));
621 LayoutUnit adjustmentForChildrenWithSameLineHeightAndBaseline = 0;
622 if (descendantsHaveSameLineHeightAndBaseline()) {
623 adjustmentForChildrenWithSameLineHeightAndBaseline = logicalTop();
625 adjustmentForChildrenWithSameLineHeightAndBaseline += (boxModelObject()->borderBefore() + boxModelObject()->paddingBefore());
628 for (InlineBox* curr = firstChild(); curr; curr = curr->nextOnLine()) {
629 if (curr->renderer().isOutOfFlowPositioned())
630 continue; // Positioned placeholders don't affect calculations.
632 if (descendantsHaveSameLineHeightAndBaseline()) {
633 curr->adjustBlockDirectionPosition(adjustmentForChildrenWithSameLineHeightAndBaseline.toFloat());
637 InlineFlowBox* inlineFlowBox = curr->isInlineFlowBox() ? toInlineFlowBox(curr) : 0;
638 bool childAffectsTopBottomPos = true;
639 if (curr->verticalAlign() == TOP)
640 curr->setLogicalTop(top.toFloat());
641 else if (curr->verticalAlign() == BOTTOM)
642 curr->setLogicalTop((top + maxHeight - curr->lineHeight()).toFloat());
644 if (!strictMode && inlineFlowBox && !inlineFlowBox->hasTextChildren() && !curr->boxModelObject()->hasInlineDirectionBordersOrPadding()
645 && !(inlineFlowBox->descendantsHaveSameLineHeightAndBaseline() && inlineFlowBox->hasTextDescendants()))
646 childAffectsTopBottomPos = false;
647 LayoutUnit posAdjust = maxAscent - curr->baselinePosition(baselineType);
648 curr->setLogicalTop(curr->logicalTop() + top + posAdjust);
651 LayoutUnit newLogicalTop = curr->logicalTop();
652 LayoutUnit newLogicalTopIncludingMargins = newLogicalTop;
653 LayoutUnit boxHeight = curr->logicalHeight();
654 LayoutUnit boxHeightIncludingMargins = boxHeight;
655 LayoutUnit borderPaddingHeight = 0;
656 if (curr->isText() || curr->isInlineFlowBox()) {
657 const FontMetrics& fontMetrics = curr->renderer().style(isFirstLineStyle())->fontMetrics();
658 newLogicalTop += curr->baselinePosition(baselineType) - fontMetrics.ascent(baselineType);
659 if (curr->isInlineFlowBox()) {
660 RenderBoxModelObject& boxObject = toRenderBoxModelObject(curr->renderer());
661 newLogicalTop -= boxObject.style(isFirstLineStyle())->isHorizontalWritingMode() ? boxObject.borderTop() + boxObject.paddingTop() :
662 boxObject.borderRight() + boxObject.paddingRight();
663 borderPaddingHeight = boxObject.borderAndPaddingLogicalHeight();
665 newLogicalTopIncludingMargins = newLogicalTop;
666 } else if (!curr->renderer().isBR()) {
667 RenderBox& box = toRenderBox(curr->renderer());
668 newLogicalTopIncludingMargins = newLogicalTop;
669 LayoutUnit overSideMargin = curr->isHorizontal() ? box.marginTop() : box.marginRight();
670 LayoutUnit underSideMargin = curr->isHorizontal() ? box.marginBottom() : box.marginLeft();
671 newLogicalTop += overSideMargin;
672 boxHeightIncludingMargins += overSideMargin + underSideMargin;
675 curr->setLogicalTop(newLogicalTop.toFloat());
677 if (childAffectsTopBottomPos) {
678 if (curr->renderer().isRubyRun()) {
679 // Treat the leading on the first and last lines of ruby runs as not being part of the overall lineTop/lineBottom.
680 // Really this is a workaround hack for the fact that ruby should have been done as line layout and not done using
682 if (renderer().style()->isFlippedLinesWritingMode() == (curr->renderer().style()->rubyPosition() == RubyPositionAfter))
683 hasAnnotationsBefore = true;
685 hasAnnotationsAfter = true;
687 RenderRubyRun& rubyRun = toRenderRubyRun(curr->renderer());
688 if (RenderRubyBase* rubyBase = rubyRun.rubyBase()) {
689 LayoutUnit bottomRubyBaseLeading = (curr->logicalHeight() - rubyBase->logicalBottom()) + rubyBase->logicalHeight() - (rubyBase->lastRootBox() ? rubyBase->lastRootBox()->lineBottom() : LayoutUnit());
690 LayoutUnit topRubyBaseLeading = rubyBase->logicalTop() + (rubyBase->firstRootBox() ? rubyBase->firstRootBox()->lineTop() : LayoutUnit());
691 newLogicalTop += !renderer().style()->isFlippedLinesWritingMode() ? topRubyBaseLeading : bottomRubyBaseLeading;
692 boxHeight -= (topRubyBaseLeading + bottomRubyBaseLeading);
695 if (curr->isInlineTextBox()) {
696 TextEmphasisPosition emphasisMarkPosition;
697 if (toInlineTextBox(curr)->getEmphasisMarkPosition(curr->renderer().style(isFirstLineStyle()), emphasisMarkPosition)) {
698 bool emphasisMarkIsOver = emphasisMarkPosition == TextEmphasisPositionOver;
699 if (emphasisMarkIsOver != curr->renderer().style(isFirstLineStyle())->isFlippedLinesWritingMode())
700 hasAnnotationsBefore = true;
702 hasAnnotationsAfter = true;
708 lineTop = newLogicalTop;
709 lineTopIncludingMargins = std::min(lineTop, newLogicalTopIncludingMargins);
711 lineTop = std::min(lineTop, newLogicalTop);
712 lineTopIncludingMargins = std::min(lineTop, std::min(lineTopIncludingMargins, newLogicalTopIncludingMargins));
714 selectionBottom = std::max(selectionBottom, newLogicalTop + boxHeight - borderPaddingHeight);
715 lineBottom = std::max(lineBottom, newLogicalTop + boxHeight);
716 lineBottomIncludingMargins = std::max(lineBottom, std::max(lineBottomIncludingMargins, newLogicalTopIncludingMargins + boxHeightIncludingMargins));
719 // Adjust boxes to use their real box y/height and not the logical height (as dictated by
722 inlineFlowBox->placeBoxesInBlockDirection(top, maxHeight, maxAscent, strictMode, lineTop, lineBottom, selectionBottom, setLineTop,
723 lineTopIncludingMargins, lineBottomIncludingMargins, hasAnnotationsBefore, hasAnnotationsAfter, baselineType);
727 if (strictMode || hasTextChildren() || (descendantsHaveSameLineHeightAndBaseline() && hasTextDescendants())) {
730 lineTop = pixelSnappedLogicalTop();
731 lineTopIncludingMargins = lineTop;
733 lineTop = std::min<LayoutUnit>(lineTop, pixelSnappedLogicalTop());
734 lineTopIncludingMargins = std::min(lineTop, lineTopIncludingMargins);
736 selectionBottom = std::max<LayoutUnit>(selectionBottom, pixelSnappedLogicalBottom());
737 lineBottom = std::max<LayoutUnit>(lineBottom, pixelSnappedLogicalBottom());
738 lineBottomIncludingMargins = std::max(lineBottom, lineBottomIncludingMargins);
741 if (renderer().style()->isFlippedLinesWritingMode())
742 flipLinesInBlockDirection(lineTopIncludingMargins, lineBottomIncludingMargins);
746 void InlineFlowBox::computeMaxLogicalTop(float& maxLogicalTop) const
748 for (InlineBox* curr = firstChild(); curr; curr = curr->nextOnLine()) {
749 if (curr->renderer().isOutOfFlowPositioned())
750 continue; // Positioned placeholders don't affect calculations.
752 if (descendantsHaveSameLineHeightAndBaseline())
755 maxLogicalTop = std::max<float>(maxLogicalTop, curr->y());
756 float localMaxLogicalTop = 0;
757 if (curr->isInlineFlowBox())
758 toInlineFlowBox(curr)->computeMaxLogicalTop(localMaxLogicalTop);
759 maxLogicalTop = std::max<float>(maxLogicalTop, localMaxLogicalTop);
763 void InlineFlowBox::flipLinesInBlockDirection(LayoutUnit lineTop, LayoutUnit lineBottom)
765 // Flip the box on the line such that the top is now relative to the lineBottom instead of the lineTop.
766 setLogicalTop(lineBottom - (logicalTop() - lineTop) - logicalHeight());
768 for (InlineBox* curr = firstChild(); curr; curr = curr->nextOnLine()) {
769 if (curr->renderer().isOutOfFlowPositioned())
770 continue; // Positioned placeholders aren't affected here.
772 if (curr->isInlineFlowBox())
773 toInlineFlowBox(curr)->flipLinesInBlockDirection(lineTop, lineBottom);
775 curr->setLogicalTop(lineBottom - (curr->logicalTop() - lineTop) - curr->logicalHeight());
779 inline void InlineFlowBox::addBoxShadowVisualOverflow(LayoutRect& logicalVisualOverflow)
781 // box-shadow on root line boxes is applying to the block and not to the lines.
785 RenderStyle* style = renderer().style(isFirstLineStyle());
786 if (!style->boxShadow())
789 LayoutUnit boxShadowLogicalTop;
790 LayoutUnit boxShadowLogicalBottom;
791 style->getBoxShadowBlockDirectionExtent(boxShadowLogicalTop, boxShadowLogicalBottom);
793 // Similar to how glyph overflow works, if our lines are flipped, then it's actually the opposite shadow that applies, since
794 // the line is "upside down" in terms of block coordinates.
795 LayoutUnit shadowLogicalTop = style->isFlippedLinesWritingMode() ? -boxShadowLogicalBottom : boxShadowLogicalTop;
796 LayoutUnit shadowLogicalBottom = style->isFlippedLinesWritingMode() ? -boxShadowLogicalTop : boxShadowLogicalBottom;
798 LayoutUnit logicalTopVisualOverflow = std::min(pixelSnappedLogicalTop() + shadowLogicalTop, logicalVisualOverflow.y());
799 LayoutUnit logicalBottomVisualOverflow = std::max(pixelSnappedLogicalBottom() + shadowLogicalBottom, logicalVisualOverflow.maxY());
801 LayoutUnit boxShadowLogicalLeft;
802 LayoutUnit boxShadowLogicalRight;
803 style->getBoxShadowInlineDirectionExtent(boxShadowLogicalLeft, boxShadowLogicalRight);
805 LayoutUnit logicalLeftVisualOverflow = std::min(pixelSnappedLogicalLeft() + boxShadowLogicalLeft, logicalVisualOverflow.x());
806 LayoutUnit logicalRightVisualOverflow = std::max(pixelSnappedLogicalRight() + boxShadowLogicalRight, logicalVisualOverflow.maxX());
808 logicalVisualOverflow = LayoutRect(logicalLeftVisualOverflow, logicalTopVisualOverflow,
809 logicalRightVisualOverflow - logicalLeftVisualOverflow, logicalBottomVisualOverflow - logicalTopVisualOverflow);
812 inline void InlineFlowBox::addBorderOutsetVisualOverflow(LayoutRect& logicalVisualOverflow)
814 // border-image-outset on root line boxes is applying to the block and not to the lines.
818 RenderStyle* style = renderer().style(isFirstLineStyle());
819 if (!style->hasBorderImageOutsets())
822 LayoutBoxExtent borderOutsets = style->borderImageOutsets();
824 LayoutUnit borderOutsetLogicalTop = borderOutsets.logicalTop(style->writingMode());
825 LayoutUnit borderOutsetLogicalBottom = borderOutsets.logicalBottom(style->writingMode());
826 LayoutUnit borderOutsetLogicalLeft = borderOutsets.logicalLeft(style->writingMode());
827 LayoutUnit borderOutsetLogicalRight = borderOutsets.logicalRight(style->writingMode());
829 // Similar to how glyph overflow works, if our lines are flipped, then it's actually the opposite border that applies, since
830 // the line is "upside down" in terms of block coordinates. vertical-rl and horizontal-bt are the flipped line modes.
831 LayoutUnit outsetLogicalTop = style->isFlippedLinesWritingMode() ? borderOutsetLogicalBottom : borderOutsetLogicalTop;
832 LayoutUnit outsetLogicalBottom = style->isFlippedLinesWritingMode() ? borderOutsetLogicalTop : borderOutsetLogicalBottom;
834 LayoutUnit logicalTopVisualOverflow = std::min(pixelSnappedLogicalTop() - outsetLogicalTop, logicalVisualOverflow.y());
835 LayoutUnit logicalBottomVisualOverflow = std::max(pixelSnappedLogicalBottom() + outsetLogicalBottom, logicalVisualOverflow.maxY());
837 LayoutUnit outsetLogicalLeft = includeLogicalLeftEdge() ? borderOutsetLogicalLeft : LayoutUnit();
838 LayoutUnit outsetLogicalRight = includeLogicalRightEdge() ? borderOutsetLogicalRight : LayoutUnit();
840 LayoutUnit logicalLeftVisualOverflow = std::min(pixelSnappedLogicalLeft() - outsetLogicalLeft, logicalVisualOverflow.x());
841 LayoutUnit logicalRightVisualOverflow = std::max(pixelSnappedLogicalRight() + outsetLogicalRight, logicalVisualOverflow.maxX());
843 logicalVisualOverflow = LayoutRect(logicalLeftVisualOverflow, logicalTopVisualOverflow,
844 logicalRightVisualOverflow - logicalLeftVisualOverflow, logicalBottomVisualOverflow - logicalTopVisualOverflow);
847 inline void InlineFlowBox::addOutlineVisualOverflow(LayoutRect& logicalVisualOverflow)
849 // Outline on root line boxes is applied to the block and not to the lines.
853 RenderStyle* style = renderer().style(isFirstLineStyle());
854 if (!style->hasOutline())
857 logicalVisualOverflow.inflate(style->outlineSize());
860 inline void InlineFlowBox::addTextBoxVisualOverflow(InlineTextBox* textBox, GlyphOverflowAndFallbackFontsMap& textBoxDataMap, LayoutRect& logicalVisualOverflow)
862 if (textBox->knownToHaveNoOverflow())
865 RenderStyle* style = textBox->renderer().style(isFirstLineStyle());
867 GlyphOverflowAndFallbackFontsMap::iterator it = textBoxDataMap.find(textBox);
868 GlyphOverflow* glyphOverflow = it == textBoxDataMap.end() ? 0 : &it->value.second;
869 bool isFlippedLine = style->isFlippedLinesWritingMode();
871 int topGlyphEdge = glyphOverflow ? (isFlippedLine ? glyphOverflow->bottom : glyphOverflow->top) : 0;
872 int bottomGlyphEdge = glyphOverflow ? (isFlippedLine ? glyphOverflow->top : glyphOverflow->bottom) : 0;
873 int leftGlyphEdge = glyphOverflow ? glyphOverflow->left : 0;
874 int rightGlyphEdge = glyphOverflow ? glyphOverflow->right : 0;
876 int strokeOverflow = static_cast<int>(ceilf(style->textStrokeWidth() / 2.0f));
877 int topGlyphOverflow = -strokeOverflow - topGlyphEdge;
878 int bottomGlyphOverflow = strokeOverflow + bottomGlyphEdge;
879 int leftGlyphOverflow = -strokeOverflow - leftGlyphEdge;
880 int rightGlyphOverflow = strokeOverflow + rightGlyphEdge;
882 TextEmphasisPosition emphasisMarkPosition;
883 if (style->textEmphasisMark() != TextEmphasisMarkNone && textBox->getEmphasisMarkPosition(style, emphasisMarkPosition)) {
884 int emphasisMarkHeight = style->font().emphasisMarkHeight(style->textEmphasisMarkString());
885 if ((emphasisMarkPosition == TextEmphasisPositionOver) == (!style->isFlippedLinesWritingMode()))
886 topGlyphOverflow = std::min(topGlyphOverflow, -emphasisMarkHeight);
888 bottomGlyphOverflow = std::max(bottomGlyphOverflow, emphasisMarkHeight);
891 // If letter-spacing is negative, we should factor that into right layout overflow. (Even in RTL, letter-spacing is
892 // applied to the right, so this is not an issue with left overflow.
893 rightGlyphOverflow -= std::min(0, (int)style->font().fontDescription().letterSpacing());
895 LayoutUnit textShadowLogicalTop;
896 LayoutUnit textShadowLogicalBottom;
897 style->getTextShadowBlockDirectionExtent(textShadowLogicalTop, textShadowLogicalBottom);
899 LayoutUnit childOverflowLogicalTop = std::min<LayoutUnit>(textShadowLogicalTop + topGlyphOverflow, topGlyphOverflow);
900 LayoutUnit childOverflowLogicalBottom = std::max<LayoutUnit>(textShadowLogicalBottom + bottomGlyphOverflow, bottomGlyphOverflow);
902 LayoutUnit textShadowLogicalLeft;
903 LayoutUnit textShadowLogicalRight;
904 style->getTextShadowInlineDirectionExtent(textShadowLogicalLeft, textShadowLogicalRight);
906 LayoutUnit childOverflowLogicalLeft = std::min<LayoutUnit>(textShadowLogicalLeft + leftGlyphOverflow, leftGlyphOverflow);
907 LayoutUnit childOverflowLogicalRight = std::max<LayoutUnit>(textShadowLogicalRight + rightGlyphOverflow, rightGlyphOverflow);
909 LayoutUnit logicalTopVisualOverflow = std::min(textBox->pixelSnappedLogicalTop() + childOverflowLogicalTop, logicalVisualOverflow.y());
910 LayoutUnit logicalBottomVisualOverflow = std::max(textBox->pixelSnappedLogicalBottom() + childOverflowLogicalBottom, logicalVisualOverflow.maxY());
911 LayoutUnit logicalLeftVisualOverflow = std::min(textBox->pixelSnappedLogicalLeft() + childOverflowLogicalLeft, logicalVisualOverflow.x());
912 LayoutUnit logicalRightVisualOverflow = std::max(textBox->pixelSnappedLogicalRight() + childOverflowLogicalRight, logicalVisualOverflow.maxX());
914 logicalVisualOverflow = LayoutRect(logicalLeftVisualOverflow, logicalTopVisualOverflow,
915 logicalRightVisualOverflow - logicalLeftVisualOverflow, logicalBottomVisualOverflow - logicalTopVisualOverflow);
917 textBox->setLogicalOverflowRect(logicalVisualOverflow);
920 inline void InlineFlowBox::addReplacedChildOverflow(const InlineBox* inlineBox, LayoutRect& logicalLayoutOverflow, LayoutRect& logicalVisualOverflow)
922 RenderBox& box = toRenderBox(inlineBox->renderer());
924 // Visual overflow only propagates if the box doesn't have a self-painting layer. This rectangle does not include
925 // transforms or relative positioning (since those objects always have self-painting layers), but it does need to be adjusted
926 // for writing-mode differences.
927 if (!box.hasSelfPaintingLayer()) {
928 LayoutRect childLogicalVisualOverflow = box.logicalVisualOverflowRectForPropagation(renderer().style());
929 childLogicalVisualOverflow.move(inlineBox->logicalLeft(), inlineBox->logicalTop());
930 logicalVisualOverflow.unite(childLogicalVisualOverflow);
933 // Layout overflow internal to the child box only propagates if the child box doesn't have overflow clip set.
934 // Otherwise the child border box propagates as layout overflow. This rectangle must include transforms and relative positioning
935 // and be adjusted for writing-mode differences.
936 LayoutRect childLogicalLayoutOverflow = box.logicalLayoutOverflowRectForPropagation(renderer().style());
937 childLogicalLayoutOverflow.move(inlineBox->logicalLeft(), inlineBox->logicalTop());
938 logicalLayoutOverflow.unite(childLogicalLayoutOverflow);
941 void InlineFlowBox::computeOverflow(LayoutUnit lineTop, LayoutUnit lineBottom, GlyphOverflowAndFallbackFontsMap& textBoxDataMap)
943 // If we know we have no overflow, we can just bail.
944 if (knownToHaveNoOverflow()) {
952 // Visual overflow just includes overflow for stuff we need to issues paint invalidations for ourselves. Self-painting layers are ignored.
953 // Layout overflow is used to determine scrolling extent, so it still includes child layers and also factors in
954 // transforms, relative positioning, etc.
955 LayoutRect logicalLayoutOverflow(enclosingLayoutRect(logicalFrameRectIncludingLineHeight(lineTop, lineBottom)));
956 LayoutRect logicalVisualOverflow(logicalLayoutOverflow);
958 addBoxShadowVisualOverflow(logicalVisualOverflow);
959 addBorderOutsetVisualOverflow(logicalVisualOverflow);
960 addOutlineVisualOverflow(logicalVisualOverflow);
962 for (InlineBox* curr = firstChild(); curr; curr = curr->nextOnLine()) {
963 if (curr->renderer().isOutOfFlowPositioned())
964 continue; // Positioned placeholders don't affect calculations.
966 if (curr->renderer().isText()) {
967 InlineTextBox* text = toInlineTextBox(curr);
968 RenderText& rt = text->renderer();
971 LayoutRect textBoxOverflow(enclosingLayoutRect(text->logicalFrameRect()));
972 addTextBoxVisualOverflow(text, textBoxDataMap, textBoxOverflow);
973 logicalVisualOverflow.unite(textBoxOverflow);
974 } else if (curr->renderer().isRenderInline()) {
975 InlineFlowBox* flow = toInlineFlowBox(curr);
976 flow->computeOverflow(lineTop, lineBottom, textBoxDataMap);
977 if (!flow->boxModelObject()->hasSelfPaintingLayer())
978 logicalVisualOverflow.unite(flow->logicalVisualOverflowRect(lineTop, lineBottom));
979 LayoutRect childLayoutOverflow = flow->logicalLayoutOverflowRect(lineTop, lineBottom);
980 childLayoutOverflow.move(flow->boxModelObject()->relativePositionLogicalOffset());
981 logicalLayoutOverflow.unite(childLayoutOverflow);
983 addReplacedChildOverflow(curr, logicalLayoutOverflow, logicalVisualOverflow);
987 setOverflowFromLogicalRects(logicalLayoutOverflow, logicalVisualOverflow, lineTop, lineBottom);
990 void InlineFlowBox::setLayoutOverflow(const LayoutRect& rect, const LayoutRect& frameBox)
992 if (frameBox.contains(rect) || rect.isEmpty())
996 m_overflow = adoptPtr(new RenderOverflow(frameBox, frameBox));
998 m_overflow->setLayoutOverflow(rect);
1001 void InlineFlowBox::setVisualOverflow(const LayoutRect& rect, const LayoutRect& frameBox)
1003 if (frameBox.contains(rect) || rect.isEmpty())
1007 m_overflow = adoptPtr(new RenderOverflow(frameBox, frameBox));
1009 m_overflow->setVisualOverflow(rect);
1012 void InlineFlowBox::setOverflowFromLogicalRects(const LayoutRect& logicalLayoutOverflow, const LayoutRect& logicalVisualOverflow, LayoutUnit lineTop, LayoutUnit lineBottom)
1014 LayoutRect frameBox = enclosingLayoutRect(frameRectIncludingLineHeight(lineTop, lineBottom));
1016 LayoutRect layoutOverflow(isHorizontal() ? logicalLayoutOverflow : logicalLayoutOverflow.transposedRect());
1017 setLayoutOverflow(layoutOverflow, frameBox);
1019 LayoutRect visualOverflow(isHorizontal() ? logicalVisualOverflow : logicalVisualOverflow.transposedRect());
1020 setVisualOverflow(visualOverflow, frameBox);
1023 bool InlineFlowBox::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, LayoutUnit lineTop, LayoutUnit lineBottom)
1025 LayoutRect overflowRect(visualOverflowRect(lineTop, lineBottom));
1026 flipForWritingMode(overflowRect);
1027 overflowRect.moveBy(accumulatedOffset);
1028 if (!locationInContainer.intersects(overflowRect))
1031 // Check children first.
1032 // We need to account for culled inline parents of the hit-tested nodes, so that they may also get included in area-based hit-tests.
1033 RenderObject* culledParent = 0;
1034 for (InlineBox* curr = lastChild(); curr; curr = curr->prevOnLine()) {
1035 if (curr->renderer().isText() || !curr->boxModelObject()->hasSelfPaintingLayer()) {
1036 RenderObject* newParent = 0;
1037 // Culled parents are only relevant for area-based hit-tests, so ignore it in point-based ones.
1038 if (locationInContainer.isRectBasedTest()) {
1039 newParent = curr->renderer().parent();
1040 if (newParent == renderer())
1043 // Check the culled parent after all its children have been checked, to do this we wait until
1044 // we are about to test an element with a different parent.
1045 if (newParent != culledParent) {
1046 if (!newParent || !newParent->isDescendantOf(culledParent)) {
1047 while (culledParent && culledParent != renderer() && culledParent != newParent) {
1048 if (culledParent->isRenderInline() && toRenderInline(culledParent)->hitTestCulledInline(request, result, locationInContainer, accumulatedOffset))
1050 culledParent = culledParent->parent();
1053 culledParent = newParent;
1055 if (curr->nodeAtPoint(request, result, locationInContainer, accumulatedOffset, lineTop, lineBottom)) {
1056 renderer().updateHitTestResult(result, locationInContainer.point() - toLayoutSize(accumulatedOffset));
1061 // Check any culled ancestor of the final children tested.
1062 while (culledParent && culledParent != renderer()) {
1063 if (culledParent->isRenderInline() && toRenderInline(culledParent)->hitTestCulledInline(request, result, locationInContainer, accumulatedOffset))
1065 culledParent = culledParent->parent();
1068 // Now check ourselves. Pixel snap hit testing.
1069 LayoutRect frameRect = roundedFrameRect();
1070 LayoutUnit minX = frameRect.x();
1071 LayoutUnit minY = frameRect.y();
1072 LayoutUnit width = frameRect.width();
1073 LayoutUnit height = frameRect.height();
1075 // Constrain our hit testing to the line top and bottom if necessary.
1076 bool noQuirksMode = renderer().document().inNoQuirksMode();
1077 if (!noQuirksMode && !hasTextChildren() && !(descendantsHaveSameLineHeightAndBaseline() && hasTextDescendants())) {
1078 RootInlineBox& rootBox = root();
1079 LayoutUnit& top = isHorizontal() ? minY : minX;
1080 LayoutUnit& logicalHeight = isHorizontal() ? height : width;
1081 LayoutUnit bottom = std::min(rootBox.lineBottom(), top + logicalHeight);
1082 top = std::max(rootBox.lineTop(), top);
1083 logicalHeight = bottom - top;
1086 // Move x/y to our coordinates.
1087 LayoutRect rect(minX, minY, width, height);
1088 flipForWritingMode(rect);
1089 rect.moveBy(accumulatedOffset);
1091 if (visibleToHitTestRequest(request) && locationInContainer.intersects(rect)) {
1092 renderer().updateHitTestResult(result, flipForWritingMode(locationInContainer.point() - toLayoutSize(accumulatedOffset))); // Don't add in m_x or m_y here, we want coords in the containing block's space.
1093 if (!result.addNodeToRectBasedTestResult(renderer().node(), request, locationInContainer, rect))
1100 void InlineFlowBox::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset, LayoutUnit lineTop, LayoutUnit lineBottom)
1102 InlineFlowBoxPainter(*this).paint(paintInfo, paintOffset, lineTop, lineBottom);
1105 bool InlineFlowBox::boxShadowCanBeAppliedToBackground(const FillLayer& lastBackgroundLayer) const
1107 // The checks here match how paintFillLayer() decides whether to clip (if it does, the shadow
1108 // would be clipped out, so it has to be drawn separately).
1109 StyleImage* image = lastBackgroundLayer.image();
1110 bool hasFillImage = image && image->canRender(renderer(), renderer().style()->effectiveZoom());
1111 return (!hasFillImage && !renderer().style()->hasBorderRadius()) || (!prevLineBox() && !nextLineBox()) || !parent();
1114 void InlineFlowBox::constrainToLineTopAndBottomIfNeeded(LayoutRect& rect) const
1116 bool noQuirksMode = renderer().document().inNoQuirksMode();
1117 if (!noQuirksMode && !hasTextChildren() && !(descendantsHaveSameLineHeightAndBaseline() && hasTextDescendants())) {
1118 const RootInlineBox& rootBox = root();
1119 LayoutUnit logicalTop = isHorizontal() ? rect.y() : rect.x();
1120 LayoutUnit logicalHeight = isHorizontal() ? rect.height() : rect.width();
1121 LayoutUnit bottom = std::min(rootBox.lineBottom(), logicalTop + logicalHeight);
1122 logicalTop = std::max(rootBox.lineTop(), logicalTop);
1123 logicalHeight = bottom - logicalTop;
1124 if (isHorizontal()) {
1125 rect.setY(logicalTop);
1126 rect.setHeight(logicalHeight);
1128 rect.setX(logicalTop);
1129 rect.setWidth(logicalHeight);
1134 InlineBox* InlineFlowBox::firstLeafChild() const
1136 InlineBox* leaf = 0;
1137 for (InlineBox* child = firstChild(); child && !leaf; child = child->nextOnLine())
1138 leaf = child->isLeaf() ? child : toInlineFlowBox(child)->firstLeafChild();
1142 InlineBox* InlineFlowBox::lastLeafChild() const
1144 InlineBox* leaf = 0;
1145 for (InlineBox* child = lastChild(); child && !leaf; child = child->prevOnLine())
1146 leaf = child->isLeaf() ? child : toInlineFlowBox(child)->lastLeafChild();
1150 RenderObject::SelectionState InlineFlowBox::selectionState() const
1152 return RenderObject::SelectionNone;
1155 bool InlineFlowBox::canAccommodateEllipsis(bool ltr, int blockEdge, int ellipsisWidth) const
1157 for (InlineBox *box = firstChild(); box; box = box->nextOnLine()) {
1158 if (!box->canAccommodateEllipsis(ltr, blockEdge, ellipsisWidth))
1164 float InlineFlowBox::placeEllipsisBox(bool ltr, float blockLeftEdge, float blockRightEdge, float ellipsisWidth, float &truncatedWidth, bool& foundBox)
1167 // We iterate over all children, the foundBox variable tells us when we've found the
1168 // box containing the ellipsis. All boxes after that one in the flow are hidden.
1169 // If our flow is ltr then iterate over the boxes from left to right, otherwise iterate
1170 // from right to left. Varying the order allows us to correctly hide the boxes following the ellipsis.
1171 InlineBox* box = ltr ? firstChild() : lastChild();
1173 // NOTE: these will cross after foundBox = true.
1174 int visibleLeftEdge = blockLeftEdge;
1175 int visibleRightEdge = blockRightEdge;
1178 int currResult = box->placeEllipsisBox(ltr, visibleLeftEdge, visibleRightEdge, ellipsisWidth, truncatedWidth, foundBox);
1179 if (currResult != -1 && result == -1)
1180 result = currResult;
1183 visibleLeftEdge += box->logicalWidth();
1184 box = box->nextOnLine();
1187 visibleRightEdge -= box->logicalWidth();
1188 box = box->prevOnLine();
1194 void InlineFlowBox::clearTruncation()
1196 for (InlineBox *box = firstChild(); box; box = box->nextOnLine())
1197 box->clearTruncation();
1200 LayoutUnit InlineFlowBox::computeOverAnnotationAdjustment(LayoutUnit allowedPosition) const
1202 LayoutUnit result = 0;
1203 for (InlineBox* curr = firstChild(); curr; curr = curr->nextOnLine()) {
1204 if (curr->renderer().isOutOfFlowPositioned())
1205 continue; // Positioned placeholders don't affect calculations.
1207 if (curr->isInlineFlowBox())
1208 result = std::max(result, toInlineFlowBox(curr)->computeOverAnnotationAdjustment(allowedPosition));
1210 if (curr->renderer().isReplaced() && curr->renderer().isRubyRun() && curr->renderer().style()->rubyPosition() == RubyPositionBefore) {
1211 RenderRubyRun& rubyRun = toRenderRubyRun(curr->renderer());
1212 RenderRubyText* rubyText = rubyRun.rubyText();
1216 if (!rubyRun.style()->isFlippedLinesWritingMode()) {
1217 LayoutUnit topOfFirstRubyTextLine = rubyText->logicalTop() + (rubyText->firstRootBox() ? rubyText->firstRootBox()->lineTop() : LayoutUnit());
1218 if (topOfFirstRubyTextLine >= 0)
1220 topOfFirstRubyTextLine += curr->logicalTop();
1221 result = std::max(result, allowedPosition - topOfFirstRubyTextLine);
1223 LayoutUnit bottomOfLastRubyTextLine = rubyText->logicalTop() + (rubyText->lastRootBox() ? rubyText->lastRootBox()->lineBottom() : rubyText->logicalHeight());
1224 if (bottomOfLastRubyTextLine <= curr->logicalHeight())
1226 bottomOfLastRubyTextLine += curr->logicalTop();
1227 result = std::max(result, bottomOfLastRubyTextLine - allowedPosition);
1231 if (curr->isInlineTextBox()) {
1232 RenderStyle* style = curr->renderer().style(isFirstLineStyle());
1233 TextEmphasisPosition emphasisMarkPosition;
1234 if (style->textEmphasisMark() != TextEmphasisMarkNone && toInlineTextBox(curr)->getEmphasisMarkPosition(style, emphasisMarkPosition) && emphasisMarkPosition == TextEmphasisPositionOver) {
1235 if (!style->isFlippedLinesWritingMode()) {
1236 int topOfEmphasisMark = curr->logicalTop() - style->font().emphasisMarkHeight(style->textEmphasisMarkString());
1237 result = std::max(result, allowedPosition - topOfEmphasisMark);
1239 int bottomOfEmphasisMark = curr->logicalBottom() + style->font().emphasisMarkHeight(style->textEmphasisMarkString());
1240 result = std::max(result, bottomOfEmphasisMark - allowedPosition);
1248 LayoutUnit InlineFlowBox::computeUnderAnnotationAdjustment(LayoutUnit allowedPosition) const
1250 LayoutUnit result = 0;
1251 for (InlineBox* curr = firstChild(); curr; curr = curr->nextOnLine()) {
1252 if (curr->renderer().isOutOfFlowPositioned())
1253 continue; // Positioned placeholders don't affect calculations.
1255 if (curr->isInlineFlowBox())
1256 result = std::max(result, toInlineFlowBox(curr)->computeUnderAnnotationAdjustment(allowedPosition));
1258 if (curr->renderer().isReplaced() && curr->renderer().isRubyRun() && curr->renderer().style()->rubyPosition() == RubyPositionAfter) {
1259 RenderRubyRun& rubyRun = toRenderRubyRun(curr->renderer());
1260 RenderRubyText* rubyText = rubyRun.rubyText();
1264 if (rubyRun.style()->isFlippedLinesWritingMode()) {
1265 LayoutUnit topOfFirstRubyTextLine = rubyText->logicalTop() + (rubyText->firstRootBox() ? rubyText->firstRootBox()->lineTop() : LayoutUnit());
1266 if (topOfFirstRubyTextLine >= 0)
1268 topOfFirstRubyTextLine += curr->logicalTop();
1269 result = std::max(result, allowedPosition - topOfFirstRubyTextLine);
1271 LayoutUnit bottomOfLastRubyTextLine = rubyText->logicalTop() + (rubyText->lastRootBox() ? rubyText->lastRootBox()->lineBottom() : rubyText->logicalHeight());
1272 if (bottomOfLastRubyTextLine <= curr->logicalHeight())
1274 bottomOfLastRubyTextLine += curr->logicalTop();
1275 result = std::max(result, bottomOfLastRubyTextLine - allowedPosition);
1279 if (curr->isInlineTextBox()) {
1280 RenderStyle* style = curr->renderer().style(isFirstLineStyle());
1281 if (style->textEmphasisMark() != TextEmphasisMarkNone && style->textEmphasisPosition() == TextEmphasisPositionUnder) {
1282 if (!style->isFlippedLinesWritingMode()) {
1283 LayoutUnit bottomOfEmphasisMark = curr->logicalBottom() + style->font().emphasisMarkHeight(style->textEmphasisMarkString());
1284 result = std::max(result, bottomOfEmphasisMark - allowedPosition);
1286 LayoutUnit topOfEmphasisMark = curr->logicalTop() - style->font().emphasisMarkHeight(style->textEmphasisMarkString());
1287 result = std::max(result, allowedPosition - topOfEmphasisMark);
1295 void InlineFlowBox::collectLeafBoxesInLogicalOrder(Vector<InlineBox*>& leafBoxesInLogicalOrder, CustomInlineBoxRangeReverse customReverseImplementation, void* userData) const
1297 InlineBox* leaf = firstLeafChild();
1299 // FIXME: The reordering code is a copy of parts from BidiResolver::createBidiRunsForLine, operating directly on InlineBoxes, instead of BidiRuns.
1300 // Investigate on how this code could possibly be shared.
1301 unsigned char minLevel = 128;
1302 unsigned char maxLevel = 0;
1304 // First find highest and lowest levels, and initialize leafBoxesInLogicalOrder with the leaf boxes in visual order.
1305 for (; leaf; leaf = leaf->nextLeafChild()) {
1306 minLevel = std::min(minLevel, leaf->bidiLevel());
1307 maxLevel = std::max(maxLevel, leaf->bidiLevel());
1308 leafBoxesInLogicalOrder.append(leaf);
1311 if (renderer().style()->rtlOrdering() == VisualOrder)
1314 // Reverse of reordering of the line (L2 according to Bidi spec):
1315 // L2. From the highest level found in the text to the lowest odd level on each line,
1316 // reverse any contiguous sequence of characters that are at that level or higher.
1318 // Reversing the reordering of the line is only done up to the lowest odd level.
1319 if (!(minLevel % 2))
1322 Vector<InlineBox*>::iterator end = leafBoxesInLogicalOrder.end();
1323 while (minLevel <= maxLevel) {
1324 Vector<InlineBox*>::iterator it = leafBoxesInLogicalOrder.begin();
1327 if ((*it)->bidiLevel() >= minLevel)
1331 Vector<InlineBox*>::iterator first = it;
1333 if ((*it)->bidiLevel() < minLevel)
1337 Vector<InlineBox*>::iterator last = it;
1338 if (customReverseImplementation) {
1340 (*customReverseImplementation)(userData, first, last);
1342 std::reverse(first, last);
1350 const char* InlineFlowBox::boxName() const
1352 return "InlineFlowBox";
1355 void InlineFlowBox::showLineTreeAndMark(const InlineBox* markedBox1, const char* markedLabel1, const InlineBox* markedBox2, const char* markedLabel2, const RenderObject* obj, int depth) const
1357 InlineBox::showLineTreeAndMark(markedBox1, markedLabel1, markedBox2, markedLabel2, obj, depth);
1358 for (const InlineBox* box = firstChild(); box; box = box->nextOnLine())
1359 box->showLineTreeAndMark(markedBox1, markedLabel1, markedBox2, markedLabel2, obj, depth + 1);
1365 void InlineFlowBox::checkConsistency() const
1367 #ifdef CHECK_CONSISTENCY
1368 ASSERT(!m_hasBadChildList);
1369 const InlineBox* prev = 0;
1370 for (const InlineBox* child = m_firstChild; child; child = child->nextOnLine()) {
1371 ASSERT(child->parent() == this);
1372 ASSERT(child->prevOnLine() == prev);
1375 ASSERT(prev == m_lastChild);
1381 } // namespace blink