2 * Copyright (C) 2003, 2006, 2008 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 "RootInlineBox.h"
23 #include "BidiResolver.h"
25 #include "ChromeClient.h"
27 #include "EllipsisBox.h"
29 #include "GraphicsContext.h"
30 #include "HitTestResult.h"
31 #include "InlineTextBox.h"
33 #include "PaintInfo.h"
34 #include "RenderArena.h"
35 #include "RenderBlock.h"
36 #include "VerticalPositionCache.h"
42 typedef WTF::HashMap<const RootInlineBox*, EllipsisBox*> EllipsisBoxMap;
43 static EllipsisBoxMap* gEllipsisBoxMap = 0;
45 RootInlineBox::RootInlineBox(RenderBlock* block)
46 : InlineFlowBox(block)
51 , m_lineTopWithLeading(0)
52 , m_lineBottomWithLeading(0)
53 , m_paginationStrut(0)
54 , m_baselineType(AlphabeticBaseline)
55 , m_hasAnnotationsBefore(false)
56 , m_hasAnnotationsAfter(false)
58 setIsHorizontal(block->isHorizontalWritingMode());
62 void RootInlineBox::destroy(RenderArena* arena)
64 detachEllipsisBox(arena);
65 InlineFlowBox::destroy(arena);
68 void RootInlineBox::detachEllipsisBox(RenderArena* arena)
70 if (hasEllipsisBox()) {
71 EllipsisBox* box = gEllipsisBoxMap->take(this);
74 setHasEllipsisBox(false);
78 RenderLineBoxList* RootInlineBox::rendererLineBoxes() const
80 return block()->lineBoxes();
83 void RootInlineBox::clearTruncation()
85 if (hasEllipsisBox()) {
86 detachEllipsisBox(renderer()->renderArena());
87 InlineFlowBox::clearTruncation();
91 bool RootInlineBox::isHyphenated() const
93 for (InlineBox* box = firstLeafChild(); box; box = box->nextLeafChild()) {
94 if (box->isInlineTextBox()) {
95 if (toInlineTextBox(box)->hasHyphen())
103 bool RootInlineBox::lineCanAccommodateEllipsis(bool ltr, int blockEdge, int lineBoxEdge, int ellipsisWidth)
105 // First sanity-check the unoverflowed width of the whole line to see if there is sufficient room.
106 int delta = ltr ? lineBoxEdge - blockEdge : blockEdge - lineBoxEdge;
107 if (logicalWidth() - delta < ellipsisWidth)
110 // Next iterate over all the line boxes on the line. If we find a replaced element that intersects
111 // then we refuse to accommodate the ellipsis. Otherwise we're ok.
112 return InlineFlowBox::canAccommodateEllipsis(ltr, blockEdge, ellipsisWidth);
115 void RootInlineBox::placeEllipsis(const AtomicString& ellipsisStr, bool ltr, float blockLeftEdge, float blockRightEdge, float ellipsisWidth,
116 InlineBox* markupBox)
118 // Create an ellipsis box.
119 EllipsisBox* ellipsisBox = new (renderer()->renderArena()) EllipsisBox(renderer(), ellipsisStr, this,
120 ellipsisWidth - (markupBox ? markupBox->logicalWidth() : 0), logicalHeight(),
121 y(), !prevRootBox(), isHorizontal(), markupBox);
123 if (!gEllipsisBoxMap)
124 gEllipsisBoxMap = new EllipsisBoxMap();
125 gEllipsisBoxMap->add(this, ellipsisBox);
126 setHasEllipsisBox(true);
128 // FIXME: Do we need an RTL version of this?
129 if (ltr && (x() + logicalWidth() + ellipsisWidth) <= blockRightEdge) {
130 ellipsisBox->setX(x() + logicalWidth());
134 // Now attempt to find the nearest glyph horizontally and place just to the right (or left in RTL)
135 // of that glyph. Mark all of the objects that intersect the ellipsis box as not painting (as being
137 bool foundBox = false;
138 ellipsisBox->setX(placeEllipsisBox(ltr, blockLeftEdge, blockRightEdge, ellipsisWidth, foundBox));
141 float RootInlineBox::placeEllipsisBox(bool ltr, float blockLeftEdge, float blockRightEdge, float ellipsisWidth, bool& foundBox)
143 float result = InlineFlowBox::placeEllipsisBox(ltr, blockLeftEdge, blockRightEdge, ellipsisWidth, foundBox);
145 result = ltr ? blockRightEdge - ellipsisWidth : blockLeftEdge;
149 void RootInlineBox::paintEllipsisBox(PaintInfo& paintInfo, const LayoutPoint& paintOffset, LayoutUnit lineTop, LayoutUnit lineBottom) const
151 if (hasEllipsisBox() && paintInfo.shouldPaintWithinRoot(renderer()) && renderer()->style()->visibility() == VISIBLE
152 && paintInfo.phase == PaintPhaseForeground)
153 ellipsisBox()->paint(paintInfo, paintOffset, lineTop, lineBottom);
158 void RootInlineBox::addHighlightOverflow()
160 Frame* frame = renderer()->frame();
163 Page* page = frame->page();
167 // Highlight acts as a selection inflation.
168 FloatRect rootRect(0, selectionTop(), logicalWidth(), selectionHeight());
169 IntRect inflatedRect = enclosingIntRect(page->chrome()->client()->customHighlightRect(renderer()->node(), renderer()->style()->highlight(), rootRect));
170 setOverflowFromLogicalRects(inflatedRect, inflatedRect, lineTop(), lineBottom());
173 void RootInlineBox::paintCustomHighlight(PaintInfo& paintInfo, const LayoutPoint& paintOffset, const AtomicString& highlightType)
175 if (!paintInfo.shouldPaintWithinRoot(renderer()) || renderer()->style()->visibility() != VISIBLE || paintInfo.phase != PaintPhaseForeground)
178 Frame* frame = renderer()->frame();
181 Page* page = frame->page();
185 // Get the inflated rect so that we can properly hit test.
186 FloatRect rootRect(paintOffset.x() + x(), paintOffset.y() + selectionTop(), logicalWidth(), selectionHeight());
187 FloatRect inflatedRect = page->chrome()->client()->customHighlightRect(renderer()->node(), highlightType, rootRect);
188 if (inflatedRect.intersects(paintInfo.rect))
189 page->chrome()->client()->paintCustomHighlight(renderer()->node(), highlightType, rootRect, rootRect, false, true);
194 void RootInlineBox::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset, LayoutUnit lineTop, LayoutUnit lineBottom)
196 InlineFlowBox::paint(paintInfo, paintOffset, lineTop, lineBottom);
197 paintEllipsisBox(paintInfo, paintOffset, lineTop, lineBottom);
199 RenderStyle* styleToUse = renderer()->style(m_firstLine);
200 if (styleToUse->highlight() != nullAtom && !paintInfo.context->paintingDisabled())
201 paintCustomHighlight(paintInfo, paintOffset, styleToUse->highlight());
205 bool RootInlineBox::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, const LayoutPoint& pointInContainer, const LayoutPoint& accumulatedOffset, LayoutUnit lineTop, LayoutUnit lineBottom)
207 if (hasEllipsisBox() && visibleToHitTesting()) {
208 if (ellipsisBox()->nodeAtPoint(request, result, pointInContainer, accumulatedOffset, lineTop, lineBottom)) {
209 renderer()->updateHitTestResult(result, pointInContainer - toLayoutSize(accumulatedOffset));
213 return InlineFlowBox::nodeAtPoint(request, result, pointInContainer, accumulatedOffset, lineTop, lineBottom);
216 void RootInlineBox::adjustPosition(float dx, float dy)
218 InlineFlowBox::adjustPosition(dx, dy);
219 LayoutUnit blockDirectionDelta = isHorizontal() ? dy : dx; // The block direction delta is a LayoutUnit.
220 m_lineTop += blockDirectionDelta;
221 m_lineBottom += blockDirectionDelta;
222 m_lineTopWithLeading += blockDirectionDelta;
223 m_lineBottomWithLeading += blockDirectionDelta;
226 void RootInlineBox::childRemoved(InlineBox* box)
228 if (box->renderer() == m_lineBreakObj)
229 setLineBreakInfo(0, 0, BidiStatus());
231 for (RootInlineBox* prev = prevRootBox(); prev && prev->lineBreakObj() == box->renderer(); prev = prev->prevRootBox()) {
232 prev->setLineBreakInfo(0, 0, BidiStatus());
237 LayoutUnit RootInlineBox::alignBoxesInBlockDirection(LayoutUnit heightOfBlock, GlyphOverflowAndFallbackFontsMap& textBoxDataMap, VerticalPositionCache& verticalPositionCache)
240 // SVG will handle vertical alignment on its own.
241 if (isSVGRootInlineBox())
245 LayoutUnit maxPositionTop = 0;
246 LayoutUnit maxPositionBottom = 0;
247 LayoutUnit maxAscent = 0;
248 LayoutUnit maxDescent = 0;
249 bool setMaxAscent = false;
250 bool setMaxDescent = false;
252 // Figure out if we're in no-quirks mode.
253 bool noQuirksMode = renderer()->document()->inNoQuirksMode();
255 m_baselineType = requiresIdeographicBaseline(textBoxDataMap) ? IdeographicBaseline : AlphabeticBaseline;
257 computeLogicalBoxHeights(this, maxPositionTop, maxPositionBottom, maxAscent, maxDescent, setMaxAscent, setMaxDescent, noQuirksMode,
258 textBoxDataMap, baselineType(), verticalPositionCache);
260 if (maxAscent + maxDescent < max(maxPositionTop, maxPositionBottom))
261 adjustMaxAscentAndDescent(maxAscent, maxDescent, maxPositionTop, maxPositionBottom);
263 LayoutUnit maxHeight = maxAscent + maxDescent;
264 LayoutUnit lineTop = heightOfBlock;
265 LayoutUnit lineBottom = heightOfBlock;
266 LayoutUnit lineTopIncludingMargins = heightOfBlock;
267 LayoutUnit lineBottomIncludingMargins = heightOfBlock;
268 bool setLineTop = false;
269 bool hasAnnotationsBefore = false;
270 bool hasAnnotationsAfter = false;
271 placeBoxesInBlockDirection(heightOfBlock, maxHeight, maxAscent, noQuirksMode, lineTop, lineBottom, setLineTop,
272 lineTopIncludingMargins, lineBottomIncludingMargins, hasAnnotationsBefore, hasAnnotationsAfter, baselineType());
273 m_hasAnnotationsBefore = hasAnnotationsBefore;
274 m_hasAnnotationsAfter = hasAnnotationsAfter;
276 maxHeight = max<LayoutUnit>(0, maxHeight); // FIXME: Is this really necessary?
278 setLineTopBottomPositions(lineTop, lineBottom, heightOfBlock, heightOfBlock + maxHeight);
280 int annotationsAdjustment = beforeAnnotationsAdjustment();
281 if (annotationsAdjustment) {
282 // FIXME: Need to handle pagination here. We might have to move to the next page/column as a result of the
284 adjustBlockDirectionPosition(annotationsAdjustment);
285 heightOfBlock += annotationsAdjustment;
288 return heightOfBlock + maxHeight;
291 int RootInlineBox::beforeAnnotationsAdjustment() const
295 if (!renderer()->style()->isFlippedLinesWritingMode()) {
296 // Annotations under the previous line may push us down.
297 if (prevRootBox() && prevRootBox()->hasAnnotationsAfter())
298 result = prevRootBox()->computeUnderAnnotationAdjustment(lineTop());
300 if (!hasAnnotationsBefore())
303 // Annotations over this line may push us further down.
304 int highestAllowedPosition = prevRootBox() ? min(prevRootBox()->lineBottom(), lineTop()) + result : block()->borderBefore();
305 result = computeOverAnnotationAdjustment(highestAllowedPosition);
307 // Annotations under this line may push us up.
308 if (hasAnnotationsBefore())
309 result = computeUnderAnnotationAdjustment(prevRootBox() ? prevRootBox()->lineBottom() : block()->borderBefore());
311 if (!prevRootBox() || !prevRootBox()->hasAnnotationsAfter())
314 // We have to compute the expansion for annotations over the previous line to see how much we should move.
315 int lowestAllowedPosition = max(prevRootBox()->lineBottom(), lineTop()) - result;
316 result = prevRootBox()->computeOverAnnotationAdjustment(lowestAllowedPosition);
322 GapRects RootInlineBox::lineSelectionGap(RenderBlock* rootBlock, const LayoutPoint& rootBlockPhysicalPosition, const LayoutSize& offsetFromRootBlock,
323 LayoutUnit selTop, LayoutUnit selHeight, const PaintInfo* paintInfo)
325 RenderObject::SelectionState lineState = selectionState();
327 bool leftGap, rightGap;
328 block()->getSelectionGapInfo(lineState, leftGap, rightGap);
332 InlineBox* firstBox = firstSelectedBox();
333 InlineBox* lastBox = lastSelectedBox();
335 result.uniteLeft(block()->logicalLeftSelectionGap(rootBlock, rootBlockPhysicalPosition, offsetFromRootBlock,
336 firstBox->parent()->renderer(), firstBox->logicalLeft(), selTop, selHeight, paintInfo));
338 result.uniteRight(block()->logicalRightSelectionGap(rootBlock, rootBlockPhysicalPosition, offsetFromRootBlock,
339 lastBox->parent()->renderer(), lastBox->logicalRight(), selTop, selHeight, paintInfo));
341 // When dealing with bidi text, a non-contiguous selection region is possible.
342 // e.g. The logical text aaaAAAbbb (capitals denote RTL text and non-capitals LTR) is layed out
343 // visually as 3 text runs |aaa|bbb|AAA| if we select 4 characters from the start of the text the
344 // selection will look like (underline denotes selection):
347 // We can see that the |bbb| run is not part of the selection while the runs around it are.
348 if (firstBox && firstBox != lastBox) {
349 // Now fill in any gaps on the line that occurred between two selected elements.
350 LayoutUnit lastLogicalLeft = firstBox->logicalRight();
351 bool isPreviousBoxSelected = firstBox->selectionState() != RenderObject::SelectionNone;
352 for (InlineBox* box = firstBox->nextLeafChild(); box; box = box->nextLeafChild()) {
353 if (box->selectionState() != RenderObject::SelectionNone) {
354 LayoutRect logicalRect(lastLogicalLeft, selTop, box->logicalLeft() - lastLogicalLeft, selHeight);
355 logicalRect.move(renderer()->isHorizontalWritingMode() ? offsetFromRootBlock : LayoutSize(offsetFromRootBlock.height(), offsetFromRootBlock.width()));
356 LayoutRect gapRect = rootBlock->logicalRectToPhysicalRect(rootBlockPhysicalPosition, logicalRect);
357 if (isPreviousBoxSelected && gapRect.width() > 0 && gapRect.height() > 0) {
358 if (paintInfo && box->parent()->renderer()->style()->visibility() == VISIBLE)
359 paintInfo->context->fillRect(gapRect, box->parent()->renderer()->selectionBackgroundColor(), box->parent()->renderer()->style()->colorSpace());
360 // VisibleSelection may be non-contiguous, see comment above.
361 result.uniteCenter(gapRect);
363 lastLogicalLeft = box->logicalRight();
367 isPreviousBoxSelected = box->selectionState() != RenderObject::SelectionNone;
374 RenderObject::SelectionState RootInlineBox::selectionState()
376 // Walk over all of the selected boxes.
377 RenderObject::SelectionState state = RenderObject::SelectionNone;
378 for (InlineBox* box = firstLeafChild(); box; box = box->nextLeafChild()) {
379 RenderObject::SelectionState boxState = box->selectionState();
380 if ((boxState == RenderObject::SelectionStart && state == RenderObject::SelectionEnd) ||
381 (boxState == RenderObject::SelectionEnd && state == RenderObject::SelectionStart))
382 state = RenderObject::SelectionBoth;
383 else if (state == RenderObject::SelectionNone ||
384 ((boxState == RenderObject::SelectionStart || boxState == RenderObject::SelectionEnd) &&
385 (state == RenderObject::SelectionNone || state == RenderObject::SelectionInside)))
387 if (state == RenderObject::SelectionBoth)
394 InlineBox* RootInlineBox::firstSelectedBox()
396 for (InlineBox* box = firstLeafChild(); box; box = box->nextLeafChild()) {
397 if (box->selectionState() != RenderObject::SelectionNone)
404 InlineBox* RootInlineBox::lastSelectedBox()
406 for (InlineBox* box = lastLeafChild(); box; box = box->prevLeafChild()) {
407 if (box->selectionState() != RenderObject::SelectionNone)
414 LayoutUnit RootInlineBox::selectionTop() const
416 LayoutUnit selectionTop = m_lineTop;
418 if (m_hasAnnotationsBefore)
419 selectionTop -= !renderer()->style()->isFlippedLinesWritingMode() ? computeOverAnnotationAdjustment(m_lineTop) : computeUnderAnnotationAdjustment(m_lineTop);
421 if (renderer()->style()->isFlippedLinesWritingMode())
424 LayoutUnit prevBottom = prevRootBox() ? prevRootBox()->selectionBottom() : block()->borderBefore() + block()->paddingBefore();
425 if (prevBottom < selectionTop && block()->containsFloats()) {
426 // This line has actually been moved further down, probably from a large line-height, but possibly because the
427 // line was forced to clear floats. If so, let's check the offsets, and only be willing to use the previous
428 // line's bottom if the offsets are greater on both sides.
429 LayoutUnit prevLeft = block()->logicalLeftOffsetForLine(prevBottom, false);
430 LayoutUnit prevRight = block()->logicalRightOffsetForLine(prevBottom, false);
431 LayoutUnit newLeft = block()->logicalLeftOffsetForLine(selectionTop, false);
432 LayoutUnit newRight = block()->logicalRightOffsetForLine(selectionTop, false);
433 if (prevLeft > newLeft || prevRight < newRight)
440 LayoutUnit RootInlineBox::selectionBottom() const
442 LayoutUnit selectionBottom = m_lineBottom;
444 if (m_hasAnnotationsAfter)
445 selectionBottom += !renderer()->style()->isFlippedLinesWritingMode() ? computeUnderAnnotationAdjustment(m_lineBottom) : computeOverAnnotationAdjustment(m_lineBottom);
447 if (!renderer()->style()->isFlippedLinesWritingMode() || !nextRootBox())
448 return selectionBottom;
450 LayoutUnit nextTop = nextRootBox()->selectionTop();
451 if (nextTop > selectionBottom && block()->containsFloats()) {
452 // The next line has actually been moved further over, probably from a large line-height, but possibly because the
453 // line was forced to clear floats. If so, let's check the offsets, and only be willing to use the next
454 // line's top if the offsets are greater on both sides.
455 LayoutUnit nextLeft = block()->logicalLeftOffsetForLine(nextTop, false);
456 LayoutUnit nextRight = block()->logicalRightOffsetForLine(nextTop, false);
457 LayoutUnit newLeft = block()->logicalLeftOffsetForLine(selectionBottom, false);
458 LayoutUnit newRight = block()->logicalRightOffsetForLine(selectionBottom, false);
459 if (nextLeft > newLeft || nextRight < newRight)
460 return selectionBottom;
466 RenderBlock* RootInlineBox::block() const
468 return toRenderBlock(renderer());
471 static bool isEditableLeaf(InlineBox* leaf)
473 return leaf && leaf->renderer() && leaf->renderer()->node() && leaf->renderer()->node()->rendererIsEditable();
476 InlineBox* RootInlineBox::closestLeafChildForPoint(const IntPoint& pointInContents, bool onlyEditableLeaves)
478 return closestLeafChildForLogicalLeftPosition(block()->isHorizontalWritingMode() ? pointInContents.x() : pointInContents.y(), onlyEditableLeaves);
481 InlineBox* RootInlineBox::closestLeafChildForLogicalLeftPosition(int leftPosition, bool onlyEditableLeaves)
483 InlineBox* firstLeaf = firstLeafChild();
484 InlineBox* lastLeaf = lastLeafChild();
485 if (firstLeaf == lastLeaf && (!onlyEditableLeaves || isEditableLeaf(firstLeaf)))
488 // Avoid returning a list marker when possible.
489 if (leftPosition <= firstLeaf->logicalLeft() && !firstLeaf->renderer()->isListMarker() && (!onlyEditableLeaves || isEditableLeaf(firstLeaf)))
490 // The leftPosition coordinate is less or equal to left edge of the firstLeaf.
494 if (leftPosition >= lastLeaf->logicalRight() && !lastLeaf->renderer()->isListMarker() && (!onlyEditableLeaves || isEditableLeaf(lastLeaf)))
495 // The leftPosition coordinate is greater or equal to right edge of the lastLeaf.
499 InlineBox* closestLeaf = 0;
500 for (InlineBox* leaf = firstLeaf; leaf; leaf = leaf->nextLeafChild()) {
501 if (!leaf->renderer()->isListMarker() && (!onlyEditableLeaves || isEditableLeaf(leaf))) {
503 if (leftPosition < leaf->logicalRight())
504 // The x coordinate is less than the right edge of the box.
510 return closestLeaf ? closestLeaf : lastLeaf;
513 BidiStatus RootInlineBox::lineBreakBidiStatus() const
515 return BidiStatus(m_lineBreakBidiStatusEor, m_lineBreakBidiStatusLastStrong, m_lineBreakBidiStatusLast, m_lineBreakContext);
518 void RootInlineBox::setLineBreakInfo(RenderObject* obj, unsigned breakPos, const BidiStatus& status)
520 m_lineBreakObj = obj;
521 m_lineBreakPos = breakPos;
522 m_lineBreakBidiStatusEor = status.eor;
523 m_lineBreakBidiStatusLastStrong = status.lastStrong;
524 m_lineBreakBidiStatusLast = status.last;
525 m_lineBreakContext = status.context;
528 EllipsisBox* RootInlineBox::ellipsisBox() const
530 if (!hasEllipsisBox())
532 return gEllipsisBoxMap->get(this);
535 void RootInlineBox::removeLineBoxFromRenderObject()
537 block()->lineBoxes()->removeLineBox(this);
540 void RootInlineBox::extractLineBoxFromRenderObject()
542 block()->lineBoxes()->extractLineBox(this);
545 void RootInlineBox::attachLineBoxToRenderObject()
547 block()->lineBoxes()->attachLineBox(this);
550 LayoutRect RootInlineBox::paddedLayoutOverflowRect(LayoutUnit endPadding) const
552 LayoutRect lineLayoutOverflow = layoutOverflowRect(lineTop(), lineBottom());
554 return lineLayoutOverflow;
556 // FIXME: Audit whether to use pixel snapped values when not using integers for layout: https://bugs.webkit.org/show_bug.cgi?id=63656
557 if (isHorizontal()) {
558 if (isLeftToRightDirection())
559 lineLayoutOverflow.shiftMaxXEdgeTo(max<LayoutUnit>(lineLayoutOverflow.maxX(), pixelSnappedLogicalRight() + endPadding));
561 lineLayoutOverflow.shiftXEdgeTo(min<LayoutUnit>(lineLayoutOverflow.x(), pixelSnappedLogicalLeft() - endPadding));
563 if (isLeftToRightDirection())
564 lineLayoutOverflow.shiftMaxYEdgeTo(max<LayoutUnit>(lineLayoutOverflow.maxY(), pixelSnappedLogicalRight() + endPadding));
566 lineLayoutOverflow.shiftYEdgeTo(min<LayoutUnit>(lineLayoutOverflow.y(), pixelSnappedLogicalLeft() - endPadding));
569 return lineLayoutOverflow;
572 static void setAscentAndDescent(LayoutUnit& ascent, LayoutUnit& descent, LayoutUnit newAscent, LayoutUnit newDescent, bool& ascentDescentSet)
574 if (!ascentDescentSet) {
575 ascentDescentSet = true;
577 descent = newDescent;
579 ascent = max(ascent, newAscent);
580 descent = max(descent, newDescent);
584 void RootInlineBox::ascentAndDescentForBox(InlineBox* box, GlyphOverflowAndFallbackFontsMap& textBoxDataMap, LayoutUnit& ascent, LayoutUnit& descent,
585 bool& affectsAscent, bool& affectsDescent) const
587 bool ascentDescentSet = false;
589 // Replaced boxes will return 0 for the line-height if line-box-contain says they are
590 // not to be included.
591 if (box->renderer()->isReplaced()) {
592 if (renderer()->style(m_firstLine)->lineBoxContain() & LineBoxContainReplaced) {
593 ascent = box->baselinePosition(baselineType());
594 descent = box->lineHeight() - ascent;
596 // Replaced elements always affect both the ascent and descent.
597 affectsAscent = true;
598 affectsDescent = true;
603 Vector<const SimpleFontData*>* usedFonts = 0;
604 GlyphOverflow* glyphOverflow = 0;
606 GlyphOverflowAndFallbackFontsMap::iterator it = textBoxDataMap.find(toInlineTextBox(box));
607 usedFonts = it == textBoxDataMap.end() ? 0 : &it->second.first;
608 glyphOverflow = it == textBoxDataMap.end() ? 0 : &it->second.second;
611 bool includeLeading = includeLeadingForBox(box);
612 bool includeFont = includeFontForBox(box);
614 bool setUsedFont = false;
615 bool setUsedFontWithLeading = false;
617 if (usedFonts && !usedFonts->isEmpty() && (includeFont || (box->renderer()->style(m_firstLine)->lineHeight().isNegative() && includeLeading))) {
618 usedFonts->append(box->renderer()->style(m_firstLine)->font().primaryFont());
619 for (size_t i = 0; i < usedFonts->size(); ++i) {
620 const FontMetrics& fontMetrics = usedFonts->at(i)->fontMetrics();
621 LayoutUnit usedFontAscent = fontMetrics.ascent(baselineType());
622 LayoutUnit usedFontDescent = fontMetrics.descent(baselineType());
623 LayoutUnit halfLeading = (fontMetrics.lineSpacing() - fontMetrics.height()) / 2;
624 LayoutUnit usedFontAscentAndLeading = usedFontAscent + halfLeading;
625 LayoutUnit usedFontDescentAndLeading = fontMetrics.lineSpacing() - usedFontAscentAndLeading;
627 setAscentAndDescent(ascent, descent, usedFontAscent, usedFontDescent, ascentDescentSet);
630 if (includeLeading) {
631 setAscentAndDescent(ascent, descent, usedFontAscentAndLeading, usedFontDescentAndLeading, ascentDescentSet);
632 setUsedFontWithLeading = true;
635 affectsAscent = usedFontAscent - box->logicalTop() > 0;
637 affectsDescent = usedFontDescent + box->logicalTop() > 0;
641 // If leading is included for the box, then we compute that box.
642 if (includeLeading && !setUsedFontWithLeading) {
643 int ascentWithLeading = box->baselinePosition(baselineType());
644 int descentWithLeading = box->lineHeight() - ascentWithLeading;
645 setAscentAndDescent(ascent, descent, ascentWithLeading, descentWithLeading, ascentDescentSet);
647 // Examine the font box for inline flows and text boxes to see if any part of it is above the baseline.
648 // If the top of our font box relative to the root box baseline is above the root box baseline, then
649 // we are contributing to the maxAscent value. Descent is similar. If any part of our font box is below
650 // the root box's baseline, then we contribute to the maxDescent value.
651 affectsAscent = ascentWithLeading - box->logicalTop() > 0;
652 affectsDescent = descentWithLeading + box->logicalTop() > 0;
655 if (includeFontForBox(box) && !setUsedFont) {
656 int fontAscent = box->renderer()->style(m_firstLine)->fontMetrics().ascent();
657 int fontDescent = box->renderer()->style(m_firstLine)->fontMetrics().descent();
658 setAscentAndDescent(ascent, descent, fontAscent, fontDescent, ascentDescentSet);
659 affectsAscent = fontAscent - box->logicalTop() > 0;
660 affectsDescent = fontDescent + box->logicalTop() > 0;
663 if (includeGlyphsForBox(box) && glyphOverflow && glyphOverflow->computeBounds) {
664 setAscentAndDescent(ascent, descent, glyphOverflow->top, glyphOverflow->bottom, ascentDescentSet);
665 affectsAscent = glyphOverflow->top - box->logicalTop() > 0;
666 affectsDescent = glyphOverflow->bottom + box->logicalTop() > 0;
667 glyphOverflow->top = min(glyphOverflow->top, max(0, glyphOverflow->top - box->renderer()->style(m_firstLine)->fontMetrics().ascent()));
668 glyphOverflow->bottom = min(glyphOverflow->bottom, max(0, glyphOverflow->bottom - box->renderer()->style(m_firstLine)->fontMetrics().descent()));
671 if (includeMarginForBox(box)) {
672 int ascentWithMargin = box->renderer()->style(m_firstLine)->fontMetrics().ascent();
673 int descentWithMargin = box->renderer()->style(m_firstLine)->fontMetrics().descent();
674 if (box->parent() && !box->renderer()->isText()) {
675 ascentWithMargin += box->boxModelObject()->borderBefore() + box->boxModelObject()->paddingBefore() + box->boxModelObject()->marginBefore();
676 descentWithMargin += box->boxModelObject()->borderAfter() + box->boxModelObject()->paddingAfter() + box->boxModelObject()->marginAfter();
678 setAscentAndDescent(ascent, descent, ascentWithMargin, descentWithMargin, ascentDescentSet);
680 // Treat like a replaced element, since we're using the margin box.
681 affectsAscent = true;
682 affectsDescent = true;
686 LayoutUnit RootInlineBox::verticalPositionForBox(InlineBox* box, VerticalPositionCache& verticalPositionCache)
688 if (box->renderer()->isText())
689 return box->parent()->logicalTop();
691 RenderBoxModelObject* renderer = box->boxModelObject();
692 ASSERT(renderer->isInline());
693 if (!renderer->isInline())
696 // This method determines the vertical position for inline elements.
697 bool firstLine = m_firstLine;
698 if (firstLine && !renderer->document()->usesFirstLineRules())
702 bool isRenderInline = renderer->isRenderInline();
703 if (isRenderInline && !firstLine) {
704 LayoutUnit verticalPosition = verticalPositionCache.get(renderer, baselineType());
705 if (verticalPosition != PositionUndefined)
706 return verticalPosition;
709 int verticalPosition = 0;
710 EVerticalAlign verticalAlign = renderer->style()->verticalAlign();
711 if (verticalAlign == TOP || verticalAlign == BOTTOM)
714 RenderObject* parent = renderer->parent();
715 if (parent->isRenderInline() && parent->style()->verticalAlign() != TOP && parent->style()->verticalAlign() != BOTTOM)
716 verticalPosition = box->parent()->logicalTop();
718 if (verticalAlign != BASELINE) {
719 const Font& font = parent->style(firstLine)->font();
720 const FontMetrics& fontMetrics = font.fontMetrics();
721 int fontSize = font.pixelSize();
723 LineDirectionMode lineDirection = parent->isHorizontalWritingMode() ? HorizontalLine : VerticalLine;
725 if (verticalAlign == SUB)
726 verticalPosition += fontSize / 5 + 1;
727 else if (verticalAlign == SUPER)
728 verticalPosition -= fontSize / 3 + 1;
729 else if (verticalAlign == TEXT_TOP)
730 verticalPosition += renderer->baselinePosition(baselineType(), firstLine, lineDirection) - fontMetrics.ascent(baselineType());
731 else if (verticalAlign == MIDDLE)
732 verticalPosition += -static_cast<int>(fontMetrics.xHeight() / 2) - renderer->lineHeight(firstLine, lineDirection) / 2 + renderer->baselinePosition(baselineType(), firstLine, lineDirection);
733 else if (verticalAlign == TEXT_BOTTOM) {
734 verticalPosition += fontMetrics.descent(baselineType());
735 // lineHeight - baselinePosition is always 0 for replaced elements (except inline blocks), so don't bother wasting time in that case.
736 if (!renderer->isReplaced() || renderer->isInlineBlockOrInlineTable())
737 verticalPosition -= (renderer->lineHeight(firstLine, lineDirection) - renderer->baselinePosition(baselineType(), firstLine, lineDirection));
738 } else if (verticalAlign == BASELINE_MIDDLE)
739 verticalPosition += -renderer->lineHeight(firstLine, lineDirection) / 2 + renderer->baselinePosition(baselineType(), firstLine, lineDirection);
740 else if (verticalAlign == LENGTH)
741 verticalPosition -= renderer->style()->verticalAlignLength().calcValue(renderer->lineHeight(firstLine, lineDirection));
744 // Store the cached value.
745 if (isRenderInline && !firstLine)
746 verticalPositionCache.set(renderer, baselineType(), verticalPosition);
748 return verticalPosition;
751 bool RootInlineBox::includeLeadingForBox(InlineBox* box) const
753 if (box->renderer()->isReplaced() || (box->renderer()->isText() && !box->isText()))
756 LineBoxContain lineBoxContain = renderer()->style()->lineBoxContain();
757 return (lineBoxContain & LineBoxContainInline) || (box == this && (lineBoxContain & LineBoxContainBlock));
760 bool RootInlineBox::includeFontForBox(InlineBox* box) const
762 if (box->renderer()->isReplaced() || (box->renderer()->isText() && !box->isText()))
765 if (!box->isText() && box->isInlineFlowBox() && !toInlineFlowBox(box)->hasTextChildren())
768 // For now map "glyphs" to "font" in vertical text mode until the bounds returned by glyphs aren't garbage.
769 LineBoxContain lineBoxContain = renderer()->style()->lineBoxContain();
770 return (lineBoxContain & LineBoxContainFont) || (!isHorizontal() && (lineBoxContain & LineBoxContainGlyphs));
773 bool RootInlineBox::includeGlyphsForBox(InlineBox* box) const
775 if (box->renderer()->isReplaced() || (box->renderer()->isText() && !box->isText()))
778 if (!box->isText() && box->isInlineFlowBox() && !toInlineFlowBox(box)->hasTextChildren())
781 // FIXME: We can't fit to glyphs yet for vertical text, since the bounds returned are garbage.
782 LineBoxContain lineBoxContain = renderer()->style()->lineBoxContain();
783 return isHorizontal() && (lineBoxContain & LineBoxContainGlyphs);
786 bool RootInlineBox::includeMarginForBox(InlineBox* box) const
788 if (box->renderer()->isReplaced() || (box->renderer()->isText() && !box->isText()))
791 LineBoxContain lineBoxContain = renderer()->style()->lineBoxContain();
792 return lineBoxContain & LineBoxContainInlineBox;
796 bool RootInlineBox::fitsToGlyphs() const
798 // FIXME: We can't fit to glyphs yet for vertical text, since the bounds returned are garbage.
799 LineBoxContain lineBoxContain = renderer()->style()->lineBoxContain();
800 return isHorizontal() && (lineBoxContain & LineBoxContainGlyphs);
803 bool RootInlineBox::includesRootLineBoxFontOrLeading() const
805 LineBoxContain lineBoxContain = renderer()->style()->lineBoxContain();
806 return (lineBoxContain & LineBoxContainBlock) || (lineBoxContain & LineBoxContainInline) || (lineBoxContain & LineBoxContainFont);
809 Node* RootInlineBox::getLogicalStartBoxWithNode(InlineBox*& startBox) const
811 Vector<InlineBox*> leafBoxesInLogicalOrder;
812 collectLeafBoxesInLogicalOrder(leafBoxesInLogicalOrder);
813 for (size_t i = 0; i < leafBoxesInLogicalOrder.size(); ++i) {
814 if (leafBoxesInLogicalOrder[i]->renderer()->node()) {
815 startBox = leafBoxesInLogicalOrder[i];
816 return startBox->renderer()->node();
823 Node* RootInlineBox::getLogicalEndBoxWithNode(InlineBox*& endBox) const
825 Vector<InlineBox*> leafBoxesInLogicalOrder;
826 collectLeafBoxesInLogicalOrder(leafBoxesInLogicalOrder);
827 for (size_t i = leafBoxesInLogicalOrder.size(); i > 0; --i) {
828 if (leafBoxesInLogicalOrder[i - 1]->renderer()->node()) {
829 endBox = leafBoxesInLogicalOrder[i - 1];
830 return endBox->renderer()->node();
838 const char* RootInlineBox::boxName() const
840 return "RootInlineBox";
844 } // namespace WebCore