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 "RenderView.h"
37 #include "VerticalPositionCache.h"
38 #include <wtf/unicode/Unicode.h>
44 typedef WTF::HashMap<const RootInlineBox*, EllipsisBox*> EllipsisBoxMap;
45 static EllipsisBoxMap* gEllipsisBoxMap = 0;
47 RootInlineBox::RootInlineBox(RenderBlock* block)
48 : InlineFlowBox(block)
53 , m_lineTopWithLeading(0)
54 , m_lineBottomWithLeading(0)
55 , m_paginationStrut(0)
56 , m_paginatedLineWidth(0)
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 LayoutUnit RootInlineBox::baselinePosition(FontBaseline baselineType) const
105 return boxModelObject()->baselinePosition(baselineType, isFirstLineStyle(), isHorizontal() ? HorizontalLine : VerticalLine, PositionOfInteriorLineBoxes);
108 LayoutUnit RootInlineBox::lineHeight() const
110 return boxModelObject()->lineHeight(isFirstLineStyle(), isHorizontal() ? HorizontalLine : VerticalLine, PositionOfInteriorLineBoxes);
113 bool RootInlineBox::lineCanAccommodateEllipsis(bool ltr, int blockEdge, int lineBoxEdge, int ellipsisWidth)
115 // First sanity-check the unoverflowed width of the whole line to see if there is sufficient room.
116 int delta = ltr ? lineBoxEdge - blockEdge : blockEdge - lineBoxEdge;
117 if (logicalWidth() - delta < ellipsisWidth)
120 // Next iterate over all the line boxes on the line. If we find a replaced element that intersects
121 // then we refuse to accommodate the ellipsis. Otherwise we're ok.
122 return InlineFlowBox::canAccommodateEllipsis(ltr, blockEdge, ellipsisWidth);
125 float RootInlineBox::placeEllipsis(const AtomicString& ellipsisStr, bool ltr, float blockLeftEdge, float blockRightEdge, float ellipsisWidth,
126 InlineBox* markupBox)
128 // Create an ellipsis box.
129 EllipsisBox* ellipsisBox = new (renderer()->renderArena()) EllipsisBox(renderer(), ellipsisStr, this,
130 ellipsisWidth - (markupBox ? markupBox->logicalWidth() : 0), logicalHeight(),
131 y(), !prevRootBox(), isHorizontal(), markupBox);
133 if (!gEllipsisBoxMap)
134 gEllipsisBoxMap = new EllipsisBoxMap();
135 gEllipsisBoxMap->add(this, ellipsisBox);
136 setHasEllipsisBox(true);
138 // FIXME: Do we need an RTL version of this?
139 if (ltr && (x() + logicalWidth() + ellipsisWidth) <= blockRightEdge) {
140 ellipsisBox->setX(x() + logicalWidth());
141 return logicalWidth() + ellipsisWidth;
144 // Now attempt to find the nearest glyph horizontally and place just to the right (or left in RTL)
145 // of that glyph. Mark all of the objects that intersect the ellipsis box as not painting (as being
147 bool foundBox = false;
148 float truncatedWidth = 0;
149 float position = placeEllipsisBox(ltr, blockLeftEdge, blockRightEdge, ellipsisWidth, truncatedWidth, foundBox);
150 ellipsisBox->setX(position);
151 return truncatedWidth;
154 float RootInlineBox::placeEllipsisBox(bool ltr, float blockLeftEdge, float blockRightEdge, float ellipsisWidth, float &truncatedWidth, bool& foundBox)
156 float result = InlineFlowBox::placeEllipsisBox(ltr, blockLeftEdge, blockRightEdge, ellipsisWidth, truncatedWidth, foundBox);
158 result = ltr ? blockRightEdge - ellipsisWidth : blockLeftEdge;
159 truncatedWidth = blockRightEdge - blockLeftEdge;
164 void RootInlineBox::paintEllipsisBox(PaintInfo& paintInfo, const LayoutPoint& paintOffset, LayoutUnit lineTop, LayoutUnit lineBottom) const
166 if (hasEllipsisBox() && paintInfo.shouldPaintWithinRoot(renderer()) && renderer()->style()->visibility() == VISIBLE
167 && paintInfo.phase == PaintPhaseForeground)
168 ellipsisBox()->paint(paintInfo, paintOffset, lineTop, lineBottom);
173 void RootInlineBox::addHighlightOverflow()
175 Frame* frame = renderer()->frame();
178 Page* page = frame->page();
182 // Highlight acts as a selection inflation.
183 FloatRect rootRect(0, selectionTop(), logicalWidth(), selectionHeight());
184 IntRect inflatedRect = enclosingIntRect(page->chrome()->client()->customHighlightRect(renderer()->node(), renderer()->style()->highlight(), rootRect));
185 setOverflowFromLogicalRects(inflatedRect, inflatedRect, lineTop(), lineBottom());
188 void RootInlineBox::paintCustomHighlight(PaintInfo& paintInfo, const LayoutPoint& paintOffset, const AtomicString& highlightType)
190 if (!paintInfo.shouldPaintWithinRoot(renderer()) || renderer()->style()->visibility() != VISIBLE || paintInfo.phase != PaintPhaseForeground)
193 Frame* frame = renderer()->frame();
196 Page* page = frame->page();
200 // Get the inflated rect so that we can properly hit test.
201 FloatRect rootRect(paintOffset.x() + x(), paintOffset.y() + selectionTop(), logicalWidth(), selectionHeight());
202 FloatRect inflatedRect = page->chrome()->client()->customHighlightRect(renderer()->node(), highlightType, rootRect);
203 if (inflatedRect.intersects(paintInfo.rect))
204 page->chrome()->client()->paintCustomHighlight(renderer()->node(), highlightType, rootRect, rootRect, false, true);
209 void RootInlineBox::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset, LayoutUnit lineTop, LayoutUnit lineBottom)
211 InlineFlowBox::paint(paintInfo, paintOffset, lineTop, lineBottom);
212 paintEllipsisBox(paintInfo, paintOffset, lineTop, lineBottom);
214 RenderStyle* styleToUse = renderer()->style(isFirstLineStyle());
215 if (styleToUse->highlight() != nullAtom && !paintInfo.context->paintingDisabled())
216 paintCustomHighlight(paintInfo, paintOffset, styleToUse->highlight());
220 bool RootInlineBox::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, const HitTestPoint& pointInContainer, const LayoutPoint& accumulatedOffset, LayoutUnit lineTop, LayoutUnit lineBottom)
222 if (hasEllipsisBox() && visibleToHitTesting()) {
223 if (ellipsisBox()->nodeAtPoint(request, result, pointInContainer, accumulatedOffset, lineTop, lineBottom)) {
224 renderer()->updateHitTestResult(result, pointInContainer.point() - toLayoutSize(accumulatedOffset));
228 return InlineFlowBox::nodeAtPoint(request, result, pointInContainer, accumulatedOffset, lineTop, lineBottom);
231 void RootInlineBox::adjustPosition(float dx, float dy)
233 InlineFlowBox::adjustPosition(dx, dy);
234 LayoutUnit blockDirectionDelta = isHorizontal() ? dy : dx; // The block direction delta is a LayoutUnit.
235 m_lineTop += blockDirectionDelta;
236 m_lineBottom += blockDirectionDelta;
237 m_lineTopWithLeading += blockDirectionDelta;
238 m_lineBottomWithLeading += blockDirectionDelta;
239 if (hasEllipsisBox())
240 ellipsisBox()->adjustPosition(dx, dy);
243 void RootInlineBox::childRemoved(InlineBox* box)
245 if (box->renderer() == m_lineBreakObj)
246 setLineBreakInfo(0, 0, BidiStatus());
248 for (RootInlineBox* prev = prevRootBox(); prev && prev->lineBreakObj() == box->renderer(); prev = prev->prevRootBox()) {
249 prev->setLineBreakInfo(0, 0, BidiStatus());
254 LayoutUnit RootInlineBox::alignBoxesInBlockDirection(LayoutUnit heightOfBlock, GlyphOverflowAndFallbackFontsMap& textBoxDataMap, VerticalPositionCache& verticalPositionCache)
257 // SVG will handle vertical alignment on its own.
258 if (isSVGRootInlineBox())
262 LayoutUnit maxPositionTop = 0;
263 LayoutUnit maxPositionBottom = 0;
264 LayoutUnit maxAscent = 0;
265 LayoutUnit maxDescent = 0;
266 bool setMaxAscent = false;
267 bool setMaxDescent = false;
269 // Figure out if we're in no-quirks mode.
270 bool noQuirksMode = renderer()->document()->inNoQuirksMode();
272 m_baselineType = requiresIdeographicBaseline(textBoxDataMap) ? IdeographicBaseline : AlphabeticBaseline;
274 computeLogicalBoxHeights(this, maxPositionTop, maxPositionBottom, maxAscent, maxDescent, setMaxAscent, setMaxDescent, noQuirksMode,
275 textBoxDataMap, baselineType(), verticalPositionCache);
277 if (maxAscent + maxDescent < max(maxPositionTop, maxPositionBottom))
278 adjustMaxAscentAndDescent(maxAscent, maxDescent, maxPositionTop, maxPositionBottom);
280 LayoutUnit maxHeight = maxAscent + maxDescent;
281 LayoutUnit lineTop = heightOfBlock;
282 LayoutUnit lineBottom = heightOfBlock;
283 LayoutUnit lineTopIncludingMargins = heightOfBlock;
284 LayoutUnit lineBottomIncludingMargins = heightOfBlock;
285 bool setLineTop = false;
286 bool hasAnnotationsBefore = false;
287 bool hasAnnotationsAfter = false;
288 placeBoxesInBlockDirection(heightOfBlock, maxHeight, maxAscent, noQuirksMode, lineTop, lineBottom, setLineTop,
289 lineTopIncludingMargins, lineBottomIncludingMargins, hasAnnotationsBefore, hasAnnotationsAfter, baselineType());
290 m_hasAnnotationsBefore = hasAnnotationsBefore;
291 m_hasAnnotationsAfter = hasAnnotationsAfter;
293 maxHeight = max<LayoutUnit>(0, maxHeight); // FIXME: Is this really necessary?
295 setLineTopBottomPositions(lineTop, lineBottom, heightOfBlock, heightOfBlock + maxHeight);
296 setPaginatedLineWidth(block()->availableLogicalWidthForContent(heightOfBlock));
298 LayoutUnit annotationsAdjustment = beforeAnnotationsAdjustment();
299 if (annotationsAdjustment) {
300 // FIXME: Need to handle pagination here. We might have to move to the next page/column as a result of the
302 adjustBlockDirectionPosition(annotationsAdjustment);
303 heightOfBlock += annotationsAdjustment;
306 LayoutUnit gridSnapAdjustment = lineSnapAdjustment();
307 if (gridSnapAdjustment) {
308 adjustBlockDirectionPosition(gridSnapAdjustment);
309 heightOfBlock += gridSnapAdjustment;
312 return heightOfBlock + maxHeight;
315 LayoutUnit RootInlineBox::beforeAnnotationsAdjustment() const
317 LayoutUnit result = 0;
319 if (!renderer()->style()->isFlippedLinesWritingMode()) {
320 // Annotations under the previous line may push us down.
321 if (prevRootBox() && prevRootBox()->hasAnnotationsAfter())
322 result = prevRootBox()->computeUnderAnnotationAdjustment(lineTop());
324 if (!hasAnnotationsBefore())
327 // Annotations over this line may push us further down.
328 LayoutUnit highestAllowedPosition = prevRootBox() ? min(prevRootBox()->lineBottom(), lineTop()) + result : static_cast<LayoutUnit>(block()->borderBefore());
329 result = computeOverAnnotationAdjustment(highestAllowedPosition);
331 // Annotations under this line may push us up.
332 if (hasAnnotationsBefore())
333 result = computeUnderAnnotationAdjustment(prevRootBox() ? prevRootBox()->lineBottom() : static_cast<LayoutUnit>(block()->borderBefore()));
335 if (!prevRootBox() || !prevRootBox()->hasAnnotationsAfter())
338 // We have to compute the expansion for annotations over the previous line to see how much we should move.
339 LayoutUnit lowestAllowedPosition = max(prevRootBox()->lineBottom(), lineTop()) - result;
340 result = prevRootBox()->computeOverAnnotationAdjustment(lowestAllowedPosition);
346 LayoutUnit RootInlineBox::lineSnapAdjustment(LayoutUnit delta) const
348 // If our block doesn't have snapping turned on, do nothing.
349 // FIXME: Implement bounds snapping.
350 if (block()->style()->lineSnap() == LineSnapNone)
353 // Get the current line grid and offset.
354 LayoutState* layoutState = block()->view()->layoutState();
355 RenderBlock* lineGrid = layoutState->lineGrid();
356 LayoutSize lineGridOffset = layoutState->lineGridOffset();
357 if (!lineGrid || lineGrid->style()->writingMode() != block()->style()->writingMode())
360 // Get the hypothetical line box used to establish the grid.
361 RootInlineBox* lineGridBox = lineGrid->lineGridBox();
365 LayoutUnit lineGridBlockOffset = lineGrid->isHorizontalWritingMode() ? lineGridOffset.height() : lineGridOffset.width();
366 LayoutUnit blockOffset = block()->isHorizontalWritingMode() ? layoutState->layoutOffset().height() : layoutState->layoutOffset().width();
368 // Now determine our position on the grid. Our baseline needs to be adjusted to the nearest baseline multiple
369 // as established by the line box.
370 // FIXME: Need to handle crazy line-box-contain values that cause the root line box to not be considered. I assume
371 // the grid should honor line-box-contain.
372 LayoutUnit gridLineHeight = lineGridBox->lineBottomWithLeading() - lineGridBox->lineTopWithLeading();
376 LayoutUnit lineGridFontAscent = lineGrid->style()->fontMetrics().ascent(baselineType());
377 LayoutUnit lineGridFontHeight = lineGridBox->logicalHeight();
378 LayoutUnit firstTextTop = lineGridBlockOffset + lineGridBox->logicalTop();
379 LayoutUnit firstLineTopWithLeading = lineGridBlockOffset + lineGridBox->lineTopWithLeading();
380 LayoutUnit firstBaselinePosition = firstTextTop + lineGridFontAscent;
382 LayoutUnit currentTextTop = blockOffset + logicalTop() + delta;
383 LayoutUnit currentFontAscent = block()->style()->fontMetrics().ascent(baselineType());
384 LayoutUnit currentBaselinePosition = currentTextTop + currentFontAscent;
386 LayoutUnit lineGridPaginationOrigin = isHorizontal() ? layoutState->lineGridPaginationOrigin().height() : layoutState->lineGridPaginationOrigin().width();
388 // If we're paginated, see if we're on a page after the first one. If so, the grid resets on subsequent pages.
389 // FIXME: If the grid is an ancestor of the pagination establisher, then this is incorrect.
390 LayoutUnit pageLogicalTop = 0;
391 if (layoutState->isPaginated() && layoutState->pageLogicalHeight()) {
392 pageLogicalTop = block()->pageLogicalTopForOffset(lineTopWithLeading() + delta);
393 if (pageLogicalTop > firstLineTopWithLeading)
394 firstTextTop = pageLogicalTop + lineGridBox->logicalTop() - lineGrid->borderBefore() - lineGrid->paddingBefore() + lineGridPaginationOrigin;
397 if (block()->style()->lineSnap() == LineSnapContain) {
398 // Compute the desired offset from the text-top of a grid line.
399 // Look at our height (logicalHeight()).
400 // Look at the total available height. It's going to be (textBottom - textTop) + (n-1)*(multiple with leading)
401 // where n is number of grid lines required to enclose us.
402 if (logicalHeight() <= lineGridFontHeight)
403 firstTextTop += (lineGridFontHeight - logicalHeight()) / 2;
405 LayoutUnit numberOfLinesWithLeading = ceilf(static_cast<float>(logicalHeight() - lineGridFontHeight) / gridLineHeight);
406 LayoutUnit totalHeight = lineGridFontHeight + numberOfLinesWithLeading * gridLineHeight;
407 firstTextTop += (totalHeight - logicalHeight()) / 2;
409 firstBaselinePosition = firstTextTop + currentFontAscent;
411 firstBaselinePosition = firstTextTop + lineGridFontAscent;
413 // If we're above the first line, just push to the first line.
414 if (currentBaselinePosition < firstBaselinePosition)
415 return delta + firstBaselinePosition - currentBaselinePosition;
417 // Otherwise we're in the middle of the grid somewhere. Just push to the next line.
418 LayoutUnit baselineOffset = currentBaselinePosition - firstBaselinePosition;
419 LayoutUnit remainder = roundToInt(baselineOffset) % roundToInt(gridLineHeight);
420 LayoutUnit result = delta;
422 result += gridLineHeight - remainder;
424 // If we aren't paginated we can return the result.
425 if (!layoutState->isPaginated() || !layoutState->pageLogicalHeight() || result == delta)
428 // We may end up shifted to a new page. We need to do a re-snap when that happens.
429 LayoutUnit newPageLogicalTop = block()->pageLogicalTopForOffset(lineBottomWithLeading() + result);
430 if (newPageLogicalTop == pageLogicalTop)
433 // Put ourselves at the top of the next page to force a snap onto the new grid established by that page.
434 return lineSnapAdjustment(newPageLogicalTop - (blockOffset + lineTopWithLeading()));
437 GapRects RootInlineBox::lineSelectionGap(RenderBlock* rootBlock, const LayoutPoint& rootBlockPhysicalPosition, const LayoutSize& offsetFromRootBlock,
438 LayoutUnit selTop, LayoutUnit selHeight, const PaintInfo* paintInfo)
440 RenderObject::SelectionState lineState = selectionState();
442 bool leftGap, rightGap;
443 block()->getSelectionGapInfo(lineState, leftGap, rightGap);
447 InlineBox* firstBox = firstSelectedBox();
448 InlineBox* lastBox = lastSelectedBox();
450 result.uniteLeft(block()->logicalLeftSelectionGap(rootBlock, rootBlockPhysicalPosition, offsetFromRootBlock,
451 firstBox->parent()->renderer(), firstBox->logicalLeft(), selTop, selHeight, paintInfo));
453 result.uniteRight(block()->logicalRightSelectionGap(rootBlock, rootBlockPhysicalPosition, offsetFromRootBlock,
454 lastBox->parent()->renderer(), lastBox->logicalRight(), selTop, selHeight, paintInfo));
456 // When dealing with bidi text, a non-contiguous selection region is possible.
457 // e.g. The logical text aaaAAAbbb (capitals denote RTL text and non-capitals LTR) is layed out
458 // visually as 3 text runs |aaa|bbb|AAA| if we select 4 characters from the start of the text the
459 // selection will look like (underline denotes selection):
462 // We can see that the |bbb| run is not part of the selection while the runs around it are.
463 if (firstBox && firstBox != lastBox) {
464 // Now fill in any gaps on the line that occurred between two selected elements.
465 LayoutUnit lastLogicalLeft = firstBox->logicalRight();
466 bool isPreviousBoxSelected = firstBox->selectionState() != RenderObject::SelectionNone;
467 for (InlineBox* box = firstBox->nextLeafChild(); box; box = box->nextLeafChild()) {
468 if (box->selectionState() != RenderObject::SelectionNone) {
469 LayoutRect logicalRect(lastLogicalLeft, selTop, box->logicalLeft() - lastLogicalLeft, selHeight);
470 logicalRect.move(renderer()->isHorizontalWritingMode() ? offsetFromRootBlock : LayoutSize(offsetFromRootBlock.height(), offsetFromRootBlock.width()));
471 LayoutRect gapRect = rootBlock->logicalRectToPhysicalRect(rootBlockPhysicalPosition, logicalRect);
472 if (isPreviousBoxSelected && gapRect.width() > 0 && gapRect.height() > 0) {
473 if (paintInfo && box->parent()->renderer()->style()->visibility() == VISIBLE)
474 paintInfo->context->fillRect(gapRect, box->parent()->renderer()->selectionBackgroundColor(), box->parent()->renderer()->style()->colorSpace());
475 // VisibleSelection may be non-contiguous, see comment above.
476 result.uniteCenter(gapRect);
478 lastLogicalLeft = box->logicalRight();
482 isPreviousBoxSelected = box->selectionState() != RenderObject::SelectionNone;
489 RenderObject::SelectionState RootInlineBox::selectionState()
491 // Walk over all of the selected boxes.
492 RenderObject::SelectionState state = RenderObject::SelectionNone;
493 for (InlineBox* box = firstLeafChild(); box; box = box->nextLeafChild()) {
494 RenderObject::SelectionState boxState = box->selectionState();
495 if ((boxState == RenderObject::SelectionStart && state == RenderObject::SelectionEnd) ||
496 (boxState == RenderObject::SelectionEnd && state == RenderObject::SelectionStart))
497 state = RenderObject::SelectionBoth;
498 else if (state == RenderObject::SelectionNone ||
499 ((boxState == RenderObject::SelectionStart || boxState == RenderObject::SelectionEnd) &&
500 (state == RenderObject::SelectionNone || state == RenderObject::SelectionInside)))
502 if (state == RenderObject::SelectionBoth)
509 InlineBox* RootInlineBox::firstSelectedBox()
511 for (InlineBox* box = firstLeafChild(); box; box = box->nextLeafChild()) {
512 if (box->selectionState() != RenderObject::SelectionNone)
519 InlineBox* RootInlineBox::lastSelectedBox()
521 for (InlineBox* box = lastLeafChild(); box; box = box->prevLeafChild()) {
522 if (box->selectionState() != RenderObject::SelectionNone)
529 LayoutUnit RootInlineBox::selectionTop() const
531 LayoutUnit selectionTop = m_lineTop;
533 if (m_hasAnnotationsBefore)
534 selectionTop -= !renderer()->style()->isFlippedLinesWritingMode() ? computeOverAnnotationAdjustment(m_lineTop) : computeUnderAnnotationAdjustment(m_lineTop);
536 if (renderer()->style()->isFlippedLinesWritingMode())
539 LayoutUnit prevBottom = prevRootBox() ? prevRootBox()->selectionBottom() : block()->borderBefore() + block()->paddingBefore();
540 if (prevBottom < selectionTop && block()->containsFloats()) {
541 // This line has actually been moved further down, probably from a large line-height, but possibly because the
542 // line was forced to clear floats. If so, let's check the offsets, and only be willing to use the previous
543 // line's bottom if the offsets are greater on both sides.
544 LayoutUnit prevLeft = block()->logicalLeftOffsetForLine(prevBottom, false);
545 LayoutUnit prevRight = block()->logicalRightOffsetForLine(prevBottom, false);
546 LayoutUnit newLeft = block()->logicalLeftOffsetForLine(selectionTop, false);
547 LayoutUnit newRight = block()->logicalRightOffsetForLine(selectionTop, false);
548 #if ENABLE(TIZEN_ROOT_INLINE_BOX_SELECTION_TOP)
549 /* To avoid too high highlight on bbc.co.uk
550 * This issue is reported on bugzilla:
551 * https://bugs.webkit.org/show_bug.cgi?id=65307
552 * You can find there simple pages to test it.
554 if (prevLeft >= newLeft || prevRight <= newRight)
556 if (prevLeft > newLeft || prevRight < newRight)
564 LayoutUnit RootInlineBox::selectionTopAdjustedForPrecedingBlock() const
566 LayoutUnit top = selectionTop();
568 RenderObject::SelectionState blockSelectionState = root()->block()->selectionState();
569 if (blockSelectionState != RenderObject::SelectionInside && blockSelectionState != RenderObject::SelectionEnd)
572 LayoutSize offsetToBlockBefore;
573 if (RenderBlock* block = root()->block()->blockBeforeWithinSelectionRoot(offsetToBlockBefore)) {
574 if (RootInlineBox* lastLine = block->lastRootBox()) {
575 RenderObject::SelectionState lastLineSelectionState = lastLine->selectionState();
576 if (lastLineSelectionState != RenderObject::SelectionInside && lastLineSelectionState != RenderObject::SelectionStart)
579 LayoutUnit lastLineSelectionBottom = lastLine->selectionBottom() + offsetToBlockBefore.height();
580 top = max(top, lastLineSelectionBottom);
587 LayoutUnit RootInlineBox::selectionBottom() const
589 LayoutUnit selectionBottom = m_lineBottom;
591 if (m_hasAnnotationsAfter)
592 selectionBottom += !renderer()->style()->isFlippedLinesWritingMode() ? computeUnderAnnotationAdjustment(m_lineBottom) : computeOverAnnotationAdjustment(m_lineBottom);
594 if (!renderer()->style()->isFlippedLinesWritingMode() || !nextRootBox())
595 return selectionBottom;
597 LayoutUnit nextTop = nextRootBox()->selectionTop();
598 if (nextTop > selectionBottom && block()->containsFloats()) {
599 // The next line has actually been moved further over, probably from a large line-height, but possibly because the
600 // line was forced to clear floats. If so, let's check the offsets, and only be willing to use the next
601 // line's top if the offsets are greater on both sides.
602 LayoutUnit nextLeft = block()->logicalLeftOffsetForLine(nextTop, false);
603 LayoutUnit nextRight = block()->logicalRightOffsetForLine(nextTop, false);
604 LayoutUnit newLeft = block()->logicalLeftOffsetForLine(selectionBottom, false);
605 LayoutUnit newRight = block()->logicalRightOffsetForLine(selectionBottom, false);
606 if (nextLeft > newLeft || nextRight < newRight)
607 return selectionBottom;
613 int RootInlineBox::blockDirectionPointInLine() const
615 return !block()->style()->isFlippedBlocksWritingMode() ? max(lineTop(), selectionTop()) : min(lineBottom(), selectionBottom());
618 RenderBlock* RootInlineBox::block() const
620 return toRenderBlock(renderer());
623 static bool isEditableLeaf(InlineBox* leaf)
625 return leaf && leaf->renderer() && leaf->renderer()->node() && leaf->renderer()->node()->rendererIsEditable();
628 InlineBox* RootInlineBox::closestLeafChildForPoint(const IntPoint& pointInContents, bool onlyEditableLeaves)
630 return closestLeafChildForLogicalLeftPosition(block()->isHorizontalWritingMode() ? pointInContents.x() : pointInContents.y(), onlyEditableLeaves);
633 InlineBox* RootInlineBox::closestLeafChildForLogicalLeftPosition(int leftPosition, bool onlyEditableLeaves)
635 InlineBox* firstLeaf = firstLeafChild();
636 InlineBox* lastLeaf = lastLeafChild();
638 if (firstLeaf != lastLeaf) {
639 if (firstLeaf->isLineBreak())
640 firstLeaf = firstLeaf->nextLeafChildIgnoringLineBreak();
641 else if (lastLeaf->isLineBreak())
642 lastLeaf = lastLeaf->prevLeafChildIgnoringLineBreak();
645 if (firstLeaf == lastLeaf && (!onlyEditableLeaves || isEditableLeaf(firstLeaf)))
648 // Avoid returning a list marker when possible.
649 if (leftPosition <= firstLeaf->logicalLeft() && !firstLeaf->renderer()->isListMarker() && (!onlyEditableLeaves || isEditableLeaf(firstLeaf)))
650 // The leftPosition coordinate is less or equal to left edge of the firstLeaf.
654 if (leftPosition >= lastLeaf->logicalRight() && !lastLeaf->renderer()->isListMarker() && (!onlyEditableLeaves || isEditableLeaf(lastLeaf)))
655 // The leftPosition coordinate is greater or equal to right edge of the lastLeaf.
659 InlineBox* closestLeaf = 0;
660 for (InlineBox* leaf = firstLeaf; leaf; leaf = leaf->nextLeafChildIgnoringLineBreak()) {
661 if (!leaf->renderer()->isListMarker() && (!onlyEditableLeaves || isEditableLeaf(leaf))) {
663 if (leftPosition < leaf->logicalRight())
664 // The x coordinate is less than the right edge of the box.
670 return closestLeaf ? closestLeaf : lastLeaf;
673 BidiStatus RootInlineBox::lineBreakBidiStatus() const
675 return BidiStatus(static_cast<WTF::Unicode::Direction>(m_lineBreakBidiStatusEor), static_cast<WTF::Unicode::Direction>(m_lineBreakBidiStatusLastStrong), static_cast<WTF::Unicode::Direction>(m_lineBreakBidiStatusLast), m_lineBreakContext);
678 void RootInlineBox::setLineBreakInfo(RenderObject* obj, unsigned breakPos, const BidiStatus& status)
680 m_lineBreakObj = obj;
681 m_lineBreakPos = breakPos;
682 m_lineBreakBidiStatusEor = status.eor;
683 m_lineBreakBidiStatusLastStrong = status.lastStrong;
684 m_lineBreakBidiStatusLast = status.last;
685 m_lineBreakContext = status.context;
688 EllipsisBox* RootInlineBox::ellipsisBox() const
690 if (!hasEllipsisBox())
692 return gEllipsisBoxMap->get(this);
695 void RootInlineBox::removeLineBoxFromRenderObject()
697 block()->lineBoxes()->removeLineBox(this);
700 void RootInlineBox::extractLineBoxFromRenderObject()
702 block()->lineBoxes()->extractLineBox(this);
705 void RootInlineBox::attachLineBoxToRenderObject()
707 block()->lineBoxes()->attachLineBox(this);
710 LayoutRect RootInlineBox::paddedLayoutOverflowRect(LayoutUnit endPadding) const
712 LayoutRect lineLayoutOverflow = layoutOverflowRect(lineTop(), lineBottom());
714 return lineLayoutOverflow;
716 // FIXME: Audit whether to use pixel snapped values when not using integers for layout: https://bugs.webkit.org/show_bug.cgi?id=63656
717 if (isHorizontal()) {
718 if (isLeftToRightDirection())
719 lineLayoutOverflow.shiftMaxXEdgeTo(max<LayoutUnit>(lineLayoutOverflow.maxX(), pixelSnappedLogicalRight() + endPadding));
721 lineLayoutOverflow.shiftXEdgeTo(min<LayoutUnit>(lineLayoutOverflow.x(), pixelSnappedLogicalLeft() - endPadding));
723 if (isLeftToRightDirection())
724 lineLayoutOverflow.shiftMaxYEdgeTo(max<LayoutUnit>(lineLayoutOverflow.maxY(), pixelSnappedLogicalRight() + endPadding));
726 lineLayoutOverflow.shiftYEdgeTo(min<LayoutUnit>(lineLayoutOverflow.y(), pixelSnappedLogicalLeft() - endPadding));
729 return lineLayoutOverflow;
732 static void setAscentAndDescent(int& ascent, int& descent, int newAscent, int newDescent, bool& ascentDescentSet)
734 if (!ascentDescentSet) {
735 ascentDescentSet = true;
737 descent = newDescent;
739 ascent = max(ascent, newAscent);
740 descent = max(descent, newDescent);
744 void RootInlineBox::ascentAndDescentForBox(InlineBox* box, GlyphOverflowAndFallbackFontsMap& textBoxDataMap, int& ascent, int& descent,
745 bool& affectsAscent, bool& affectsDescent) const
747 bool ascentDescentSet = false;
749 // Replaced boxes will return 0 for the line-height if line-box-contain says they are
750 // not to be included.
751 if (box->renderer()->isReplaced()) {
752 if (renderer()->style(isFirstLineStyle())->lineBoxContain() & LineBoxContainReplaced) {
753 ascent = box->baselinePosition(baselineType());
754 descent = box->lineHeight() - ascent;
756 // Replaced elements always affect both the ascent and descent.
757 affectsAscent = true;
758 affectsDescent = true;
763 Vector<const SimpleFontData*>* usedFonts = 0;
764 GlyphOverflow* glyphOverflow = 0;
766 GlyphOverflowAndFallbackFontsMap::iterator it = textBoxDataMap.find(toInlineTextBox(box));
767 usedFonts = it == textBoxDataMap.end() ? 0 : &it->second.first;
768 glyphOverflow = it == textBoxDataMap.end() ? 0 : &it->second.second;
771 bool includeLeading = includeLeadingForBox(box);
772 bool includeFont = includeFontForBox(box);
774 bool setUsedFont = false;
775 bool setUsedFontWithLeading = false;
777 if (usedFonts && !usedFonts->isEmpty() && (includeFont || (box->renderer()->style(isFirstLineStyle())->lineHeight().isNegative() && includeLeading))) {
778 usedFonts->append(box->renderer()->style(isFirstLineStyle())->font().primaryFont());
779 for (size_t i = 0; i < usedFonts->size(); ++i) {
780 const FontMetrics& fontMetrics = usedFonts->at(i)->fontMetrics();
781 int usedFontAscent = fontMetrics.ascent(baselineType());
782 int usedFontDescent = fontMetrics.descent(baselineType());
783 int halfLeading = (fontMetrics.lineSpacing() - fontMetrics.height()) / 2;
784 int usedFontAscentAndLeading = usedFontAscent + halfLeading;
785 int usedFontDescentAndLeading = fontMetrics.lineSpacing() - usedFontAscentAndLeading;
787 setAscentAndDescent(ascent, descent, usedFontAscent, usedFontDescent, ascentDescentSet);
790 if (includeLeading) {
791 setAscentAndDescent(ascent, descent, usedFontAscentAndLeading, usedFontDescentAndLeading, ascentDescentSet);
792 setUsedFontWithLeading = true;
795 affectsAscent = usedFontAscent - box->logicalTop() > 0;
797 affectsDescent = usedFontDescent + box->logicalTop() > 0;
801 // If leading is included for the box, then we compute that box.
802 if (includeLeading && !setUsedFontWithLeading) {
803 int ascentWithLeading = box->baselinePosition(baselineType());
804 int descentWithLeading = box->lineHeight() - ascentWithLeading;
805 setAscentAndDescent(ascent, descent, ascentWithLeading, descentWithLeading, ascentDescentSet);
807 // Examine the font box for inline flows and text boxes to see if any part of it is above the baseline.
808 // If the top of our font box relative to the root box baseline is above the root box baseline, then
809 // we are contributing to the maxAscent value. Descent is similar. If any part of our font box is below
810 // the root box's baseline, then we contribute to the maxDescent value.
811 affectsAscent = ascentWithLeading - box->logicalTop() > 0;
812 affectsDescent = descentWithLeading + box->logicalTop() > 0;
815 if (includeFontForBox(box) && !setUsedFont) {
816 int fontAscent = box->renderer()->style(isFirstLineStyle())->fontMetrics().ascent();
817 int fontDescent = box->renderer()->style(isFirstLineStyle())->fontMetrics().descent();
818 setAscentAndDescent(ascent, descent, fontAscent, fontDescent, ascentDescentSet);
819 affectsAscent = fontAscent - box->logicalTop() > 0;
820 affectsDescent = fontDescent + box->logicalTop() > 0;
823 if (includeGlyphsForBox(box) && glyphOverflow && glyphOverflow->computeBounds) {
824 setAscentAndDescent(ascent, descent, glyphOverflow->top, glyphOverflow->bottom, ascentDescentSet);
825 affectsAscent = glyphOverflow->top - box->logicalTop() > 0;
826 affectsDescent = glyphOverflow->bottom + box->logicalTop() > 0;
827 glyphOverflow->top = min(glyphOverflow->top, max(0, glyphOverflow->top - box->renderer()->style(isFirstLineStyle())->fontMetrics().ascent()));
828 glyphOverflow->bottom = min(glyphOverflow->bottom, max(0, glyphOverflow->bottom - box->renderer()->style(isFirstLineStyle())->fontMetrics().descent()));
831 if (includeMarginForBox(box)) {
832 LayoutUnit ascentWithMargin = box->renderer()->style(isFirstLineStyle())->fontMetrics().ascent();
833 LayoutUnit descentWithMargin = box->renderer()->style(isFirstLineStyle())->fontMetrics().descent();
834 if (box->parent() && !box->renderer()->isText()) {
835 ascentWithMargin += box->boxModelObject()->borderBefore() + box->boxModelObject()->paddingBefore() + box->boxModelObject()->marginBefore();
836 descentWithMargin += box->boxModelObject()->borderAfter() + box->boxModelObject()->paddingAfter() + box->boxModelObject()->marginAfter();
838 setAscentAndDescent(ascent, descent, ascentWithMargin, descentWithMargin, ascentDescentSet);
840 // Treat like a replaced element, since we're using the margin box.
841 affectsAscent = true;
842 affectsDescent = true;
846 LayoutUnit RootInlineBox::verticalPositionForBox(InlineBox* box, VerticalPositionCache& verticalPositionCache)
848 if (box->renderer()->isText())
849 return box->parent()->logicalTop();
851 RenderBoxModelObject* renderer = box->boxModelObject();
852 ASSERT(renderer->isInline());
853 if (!renderer->isInline())
856 // This method determines the vertical position for inline elements.
857 bool firstLine = isFirstLineStyle();
858 if (firstLine && !renderer->document()->usesFirstLineRules())
862 bool isRenderInline = renderer->isRenderInline();
863 if (isRenderInline && !firstLine) {
864 LayoutUnit verticalPosition = verticalPositionCache.get(renderer, baselineType());
865 if (verticalPosition != PositionUndefined)
866 return verticalPosition;
869 LayoutUnit verticalPosition = 0;
870 EVerticalAlign verticalAlign = renderer->style()->verticalAlign();
871 if (verticalAlign == TOP || verticalAlign == BOTTOM)
874 RenderObject* parent = renderer->parent();
875 if (parent->isRenderInline() && parent->style()->verticalAlign() != TOP && parent->style()->verticalAlign() != BOTTOM)
876 verticalPosition = box->parent()->logicalTop();
878 if (verticalAlign != BASELINE) {
879 const Font& font = parent->style(firstLine)->font();
880 const FontMetrics& fontMetrics = font.fontMetrics();
881 int fontSize = font.pixelSize();
883 LineDirectionMode lineDirection = parent->isHorizontalWritingMode() ? HorizontalLine : VerticalLine;
885 if (verticalAlign == SUB)
886 verticalPosition += fontSize / 5 + 1;
887 else if (verticalAlign == SUPER)
888 verticalPosition -= fontSize / 3 + 1;
889 else if (verticalAlign == TEXT_TOP)
890 verticalPosition += renderer->baselinePosition(baselineType(), firstLine, lineDirection) - fontMetrics.ascent(baselineType());
891 else if (verticalAlign == MIDDLE)
892 verticalPosition = (verticalPosition - static_cast<LayoutUnit>(fontMetrics.xHeight() / 2) - renderer->lineHeight(firstLine, lineDirection) / 2 + renderer->baselinePosition(baselineType(), firstLine, lineDirection)).round();
893 else if (verticalAlign == TEXT_BOTTOM) {
894 verticalPosition += fontMetrics.descent(baselineType());
895 // lineHeight - baselinePosition is always 0 for replaced elements (except inline blocks), so don't bother wasting time in that case.
896 if (!renderer->isReplaced() || renderer->isInlineBlockOrInlineTable())
897 verticalPosition -= (renderer->lineHeight(firstLine, lineDirection) - renderer->baselinePosition(baselineType(), firstLine, lineDirection));
898 } else if (verticalAlign == BASELINE_MIDDLE)
899 verticalPosition += -renderer->lineHeight(firstLine, lineDirection) / 2 + renderer->baselinePosition(baselineType(), firstLine, lineDirection);
900 else if (verticalAlign == LENGTH) {
901 LayoutUnit lineHeight;
902 //Per http://www.w3.org/TR/CSS21/visudet.html#propdef-vertical-align: 'Percentages: refer to the 'line-height' of the element itself'.
903 if (renderer->style()->verticalAlignLength().isPercent())
904 lineHeight = renderer->style()->computedLineHeight();
906 lineHeight = renderer->lineHeight(firstLine, lineDirection);
907 verticalPosition -= valueForLength(renderer->style()->verticalAlignLength(), lineHeight, renderer->view());
911 // Store the cached value.
912 if (isRenderInline && !firstLine)
913 verticalPositionCache.set(renderer, baselineType(), verticalPosition);
915 return verticalPosition;
918 bool RootInlineBox::includeLeadingForBox(InlineBox* box) const
920 if (box->renderer()->isReplaced() || (box->renderer()->isText() && !box->isText()))
923 LineBoxContain lineBoxContain = renderer()->style()->lineBoxContain();
924 return (lineBoxContain & LineBoxContainInline) || (box == this && (lineBoxContain & LineBoxContainBlock));
927 bool RootInlineBox::includeFontForBox(InlineBox* box) const
929 if (box->renderer()->isReplaced() || (box->renderer()->isText() && !box->isText()))
932 if (!box->isText() && box->isInlineFlowBox() && !toInlineFlowBox(box)->hasTextChildren())
935 // For now map "glyphs" to "font" in vertical text mode until the bounds returned by glyphs aren't garbage.
936 LineBoxContain lineBoxContain = renderer()->style()->lineBoxContain();
937 return (lineBoxContain & LineBoxContainFont) || (!isHorizontal() && (lineBoxContain & LineBoxContainGlyphs));
940 bool RootInlineBox::includeGlyphsForBox(InlineBox* box) const
942 if (box->renderer()->isReplaced() || (box->renderer()->isText() && !box->isText()))
945 if (!box->isText() && box->isInlineFlowBox() && !toInlineFlowBox(box)->hasTextChildren())
948 // FIXME: We can't fit to glyphs yet for vertical text, since the bounds returned are garbage.
949 LineBoxContain lineBoxContain = renderer()->style()->lineBoxContain();
950 return isHorizontal() && (lineBoxContain & LineBoxContainGlyphs);
953 bool RootInlineBox::includeMarginForBox(InlineBox* box) const
955 if (box->renderer()->isReplaced() || (box->renderer()->isText() && !box->isText()))
958 LineBoxContain lineBoxContain = renderer()->style()->lineBoxContain();
959 return lineBoxContain & LineBoxContainInlineBox;
963 bool RootInlineBox::fitsToGlyphs() const
965 // FIXME: We can't fit to glyphs yet for vertical text, since the bounds returned are garbage.
966 LineBoxContain lineBoxContain = renderer()->style()->lineBoxContain();
967 return isHorizontal() && (lineBoxContain & LineBoxContainGlyphs);
970 bool RootInlineBox::includesRootLineBoxFontOrLeading() const
972 LineBoxContain lineBoxContain = renderer()->style()->lineBoxContain();
973 return (lineBoxContain & LineBoxContainBlock) || (lineBoxContain & LineBoxContainInline) || (lineBoxContain & LineBoxContainFont);
976 Node* RootInlineBox::getLogicalStartBoxWithNode(InlineBox*& startBox) const
978 Vector<InlineBox*> leafBoxesInLogicalOrder;
979 collectLeafBoxesInLogicalOrder(leafBoxesInLogicalOrder);
980 for (size_t i = 0; i < leafBoxesInLogicalOrder.size(); ++i) {
981 if (leafBoxesInLogicalOrder[i]->renderer()->node()) {
982 startBox = leafBoxesInLogicalOrder[i];
983 return startBox->renderer()->node();
990 Node* RootInlineBox::getLogicalEndBoxWithNode(InlineBox*& endBox) const
992 Vector<InlineBox*> leafBoxesInLogicalOrder;
993 collectLeafBoxesInLogicalOrder(leafBoxesInLogicalOrder);
994 for (size_t i = leafBoxesInLogicalOrder.size(); i > 0; --i) {
995 if (leafBoxesInLogicalOrder[i - 1]->renderer()->node()) {
996 endBox = leafBoxesInLogicalOrder[i - 1];
997 return endBox->renderer()->node();
1005 const char* RootInlineBox::boxName() const
1007 return "RootInlineBox";
1011 } // namespace WebCore