michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set shiftwidth=2 tabstop=8 autoindent cindent expandtab: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "TextOverflow.h" michael@0: #include michael@0: michael@0: // Please maintain alphabetical order below michael@0: #include "nsBlockFrame.h" michael@0: #include "nsCaret.h" michael@0: #include "nsContentUtils.h" michael@0: #include "nsCSSAnonBoxes.h" michael@0: #include "nsGfxScrollFrame.h" michael@0: #include "nsIScrollableFrame.h" michael@0: #include "nsLayoutUtils.h" michael@0: #include "nsPresContext.h" michael@0: #include "nsRect.h" michael@0: #include "nsRenderingContext.h" michael@0: #include "nsTextFrame.h" michael@0: #include "nsIFrameInlines.h" michael@0: #include "mozilla/ArrayUtils.h" michael@0: #include "mozilla/Likely.h" michael@0: michael@0: namespace mozilla { michael@0: namespace css { michael@0: michael@0: class LazyReferenceRenderingContextGetterFromFrame MOZ_FINAL : michael@0: public gfxFontGroup::LazyReferenceContextGetter { michael@0: public: michael@0: LazyReferenceRenderingContextGetterFromFrame(nsIFrame* aFrame) michael@0: : mFrame(aFrame) {} michael@0: virtual already_AddRefed GetRefContext() MOZ_OVERRIDE michael@0: { michael@0: nsRefPtr rc = michael@0: mFrame->PresContext()->PresShell()->CreateReferenceRenderingContext(); michael@0: nsRefPtr ctx = rc->ThebesContext(); michael@0: return ctx.forget(); michael@0: } michael@0: private: michael@0: nsIFrame* mFrame; michael@0: }; michael@0: michael@0: static gfxTextRun* michael@0: GetEllipsisTextRun(nsIFrame* aFrame) michael@0: { michael@0: nsRefPtr fm; michael@0: nsLayoutUtils::GetFontMetricsForFrame(aFrame, getter_AddRefs(fm), michael@0: nsLayoutUtils::FontSizeInflationFor(aFrame)); michael@0: LazyReferenceRenderingContextGetterFromFrame lazyRefContextGetter(aFrame); michael@0: return fm->GetThebesFontGroup()->GetEllipsisTextRun( michael@0: aFrame->PresContext()->AppUnitsPerDevPixel(), lazyRefContextGetter); michael@0: } michael@0: michael@0: static nsIFrame* michael@0: GetSelfOrNearestBlock(nsIFrame* aFrame) michael@0: { michael@0: return nsLayoutUtils::GetAsBlock(aFrame) ? aFrame : michael@0: nsLayoutUtils::FindNearestBlockAncestor(aFrame); michael@0: } michael@0: michael@0: // Return true if the frame is an atomic inline-level element. michael@0: // It's not supposed to be called for block frames since we never michael@0: // process block descendants for text-overflow. michael@0: static bool michael@0: IsAtomicElement(nsIFrame* aFrame, const nsIAtom* aFrameType) michael@0: { michael@0: NS_PRECONDITION(!nsLayoutUtils::GetAsBlock(aFrame) || michael@0: !aFrame->IsBlockOutside(), michael@0: "unexpected block frame"); michael@0: NS_PRECONDITION(aFrameType != nsGkAtoms::placeholderFrame, michael@0: "unexpected placeholder frame"); michael@0: return !aFrame->IsFrameOfType(nsIFrame::eLineParticipant); michael@0: } michael@0: michael@0: static bool michael@0: IsFullyClipped(nsTextFrame* aFrame, nscoord aLeft, nscoord aRight, michael@0: nscoord* aSnappedLeft, nscoord* aSnappedRight) michael@0: { michael@0: *aSnappedLeft = aLeft; michael@0: *aSnappedRight = aRight; michael@0: if (aLeft <= 0 && aRight <= 0) { michael@0: return false; michael@0: } michael@0: return !aFrame->MeasureCharClippedText(aLeft, aRight, michael@0: aSnappedLeft, aSnappedRight); michael@0: } michael@0: michael@0: static bool michael@0: IsHorizontalOverflowVisible(nsIFrame* aFrame) michael@0: { michael@0: NS_PRECONDITION(nsLayoutUtils::GetAsBlock(aFrame) != nullptr, michael@0: "expected a block frame"); michael@0: michael@0: nsIFrame* f = aFrame; michael@0: while (f && f->StyleContext()->GetPseudo() && michael@0: f->GetType() != nsGkAtoms::scrollFrame) { michael@0: f = f->GetParent(); michael@0: } michael@0: return !f || f->StyleDisplay()->mOverflowX == NS_STYLE_OVERFLOW_VISIBLE; michael@0: } michael@0: michael@0: static void michael@0: ClipMarker(const nsRect& aContentArea, michael@0: const nsRect& aMarkerRect, michael@0: DisplayListClipState::AutoSaveRestore& aClipState) michael@0: { michael@0: nscoord rightOverflow = aMarkerRect.XMost() - aContentArea.XMost(); michael@0: nsRect markerRect = aMarkerRect; michael@0: if (rightOverflow > 0) { michael@0: // Marker overflows on the right side (content width < marker width). michael@0: markerRect.width -= rightOverflow; michael@0: aClipState.ClipContentDescendants(markerRect); michael@0: } else { michael@0: nscoord leftOverflow = aContentArea.x - aMarkerRect.x; michael@0: if (leftOverflow > 0) { michael@0: // Marker overflows on the left side michael@0: markerRect.width -= leftOverflow; michael@0: markerRect.x += leftOverflow; michael@0: aClipState.ClipContentDescendants(markerRect); michael@0: } michael@0: } michael@0: } michael@0: michael@0: static void michael@0: InflateLeft(nsRect* aRect, nscoord aDelta) michael@0: { michael@0: nscoord xmost = aRect->XMost(); michael@0: aRect->x -= aDelta; michael@0: aRect->width = std::max(xmost - aRect->x, 0); michael@0: } michael@0: michael@0: static void michael@0: InflateRight(nsRect* aRect, nscoord aDelta) michael@0: { michael@0: aRect->width = std::max(aRect->width + aDelta, 0); michael@0: } michael@0: michael@0: static bool michael@0: IsFrameDescendantOfAny(nsIFrame* aChild, michael@0: const TextOverflow::FrameHashtable& aSetOfFrames, michael@0: nsIFrame* aCommonAncestor) michael@0: { michael@0: for (nsIFrame* f = aChild; f && f != aCommonAncestor; michael@0: f = nsLayoutUtils::GetCrossDocParentFrame(f)) { michael@0: if (aSetOfFrames.GetEntry(f)) { michael@0: return true; michael@0: } michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: class nsDisplayTextOverflowMarker : public nsDisplayItem michael@0: { michael@0: public: michael@0: nsDisplayTextOverflowMarker(nsDisplayListBuilder* aBuilder, nsIFrame* aFrame, michael@0: const nsRect& aRect, nscoord aAscent, michael@0: const nsStyleTextOverflowSide* aStyle, michael@0: uint32_t aIndex) michael@0: : nsDisplayItem(aBuilder, aFrame), mRect(aRect), michael@0: mStyle(aStyle), mAscent(aAscent), mIndex(aIndex) { michael@0: MOZ_COUNT_CTOR(nsDisplayTextOverflowMarker); michael@0: } michael@0: #ifdef NS_BUILD_REFCNT_LOGGING michael@0: virtual ~nsDisplayTextOverflowMarker() { michael@0: MOZ_COUNT_DTOR(nsDisplayTextOverflowMarker); michael@0: } michael@0: #endif michael@0: virtual nsRect GetBounds(nsDisplayListBuilder* aBuilder, michael@0: bool* aSnap) MOZ_OVERRIDE { michael@0: *aSnap = false; michael@0: nsRect shadowRect = michael@0: nsLayoutUtils::GetTextShadowRectsUnion(mRect, mFrame); michael@0: return mRect.Union(shadowRect); michael@0: } michael@0: virtual void Paint(nsDisplayListBuilder* aBuilder, michael@0: nsRenderingContext* aCtx) MOZ_OVERRIDE; michael@0: michael@0: virtual uint32_t GetPerFrameKey() MOZ_OVERRIDE { michael@0: return (mIndex << nsDisplayItem::TYPE_BITS) | nsDisplayItem::GetPerFrameKey(); michael@0: } michael@0: void PaintTextToContext(nsRenderingContext* aCtx, michael@0: nsPoint aOffsetFromRect); michael@0: NS_DISPLAY_DECL_NAME("TextOverflow", TYPE_TEXT_OVERFLOW) michael@0: private: michael@0: nsRect mRect; // in reference frame coordinates michael@0: const nsStyleTextOverflowSide* mStyle; michael@0: nscoord mAscent; // baseline for the marker text in mRect michael@0: uint32_t mIndex; michael@0: }; michael@0: michael@0: static void michael@0: PaintTextShadowCallback(nsRenderingContext* aCtx, michael@0: nsPoint aShadowOffset, michael@0: const nscolor& aShadowColor, michael@0: void* aData) michael@0: { michael@0: reinterpret_cast(aData)-> michael@0: PaintTextToContext(aCtx, aShadowOffset); michael@0: } michael@0: michael@0: void michael@0: nsDisplayTextOverflowMarker::Paint(nsDisplayListBuilder* aBuilder, michael@0: nsRenderingContext* aCtx) michael@0: { michael@0: nscolor foregroundColor = michael@0: nsLayoutUtils::GetColor(mFrame, eCSSProperty_color); michael@0: michael@0: // Paint the text-shadows for the overflow marker michael@0: nsLayoutUtils::PaintTextShadow(mFrame, aCtx, mRect, mVisibleRect, michael@0: foregroundColor, PaintTextShadowCallback, michael@0: (void*)this); michael@0: aCtx->SetColor(foregroundColor); michael@0: PaintTextToContext(aCtx, nsPoint(0, 0)); michael@0: } michael@0: michael@0: void michael@0: nsDisplayTextOverflowMarker::PaintTextToContext(nsRenderingContext* aCtx, michael@0: nsPoint aOffsetFromRect) michael@0: { michael@0: gfxFloat y = nsLayoutUtils::GetSnappedBaselineY(mFrame, aCtx->ThebesContext(), michael@0: mRect.y, mAscent); michael@0: nsPoint baselinePt(mRect.x, NSToCoordFloor(y)); michael@0: nsPoint pt = baselinePt + aOffsetFromRect; michael@0: michael@0: if (mStyle->mType == NS_STYLE_TEXT_OVERFLOW_ELLIPSIS) { michael@0: gfxTextRun* textRun = GetEllipsisTextRun(mFrame); michael@0: if (textRun) { michael@0: NS_ASSERTION(!textRun->IsRightToLeft(), michael@0: "Ellipsis textruns should always be LTR!"); michael@0: gfxPoint gfxPt(pt.x, pt.y); michael@0: textRun->Draw(aCtx->ThebesContext(), gfxPt, DrawMode::GLYPH_FILL, michael@0: 0, textRun->GetLength(), nullptr, nullptr, nullptr); michael@0: } michael@0: } else { michael@0: nsRefPtr fm; michael@0: nsLayoutUtils::GetFontMetricsForFrame(mFrame, getter_AddRefs(fm), michael@0: nsLayoutUtils::FontSizeInflationFor(mFrame)); michael@0: aCtx->SetFont(fm); michael@0: nsLayoutUtils::DrawString(mFrame, aCtx, mStyle->mString.get(), michael@0: mStyle->mString.Length(), pt); michael@0: } michael@0: } michael@0: michael@0: void michael@0: TextOverflow::Init(nsDisplayListBuilder* aBuilder, michael@0: nsIFrame* aBlockFrame) michael@0: { michael@0: mBuilder = aBuilder; michael@0: mBlock = aBlockFrame; michael@0: mContentArea = aBlockFrame->GetContentRectRelativeToSelf(); michael@0: mScrollableFrame = nsLayoutUtils::GetScrollableFrameFor(aBlockFrame); michael@0: uint8_t direction = aBlockFrame->StyleVisibility()->mDirection; michael@0: mBlockIsRTL = direction == NS_STYLE_DIRECTION_RTL; michael@0: mAdjustForPixelSnapping = false; michael@0: #ifdef MOZ_XUL michael@0: if (!mScrollableFrame) { michael@0: nsIAtom* pseudoType = aBlockFrame->StyleContext()->GetPseudo(); michael@0: if (pseudoType == nsCSSAnonBoxes::mozXULAnonymousBlock) { michael@0: mScrollableFrame = michael@0: nsLayoutUtils::GetScrollableFrameFor(aBlockFrame->GetParent()); michael@0: // nsXULScrollFrame::ClampAndSetBounds rounds to nearest pixels michael@0: // for RTL blocks (also for overflow:hidden), so we need to move michael@0: // the edges 1px outward in ExamineLineFrames to avoid triggering michael@0: // a text-overflow marker in this case. michael@0: mAdjustForPixelSnapping = mBlockIsRTL; michael@0: } michael@0: } michael@0: #endif michael@0: mCanHaveHorizontalScrollbar = false; michael@0: if (mScrollableFrame) { michael@0: mCanHaveHorizontalScrollbar = michael@0: mScrollableFrame->GetScrollbarStyles().mHorizontal != NS_STYLE_OVERFLOW_HIDDEN; michael@0: if (!mAdjustForPixelSnapping) { michael@0: // Scrolling to the end position can leave some text still overflowing due michael@0: // to pixel snapping behaviour in our scrolling code. michael@0: mAdjustForPixelSnapping = mCanHaveHorizontalScrollbar; michael@0: } michael@0: mContentArea.MoveBy(mScrollableFrame->GetScrollPosition()); michael@0: nsIFrame* scrollFrame = do_QueryFrame(mScrollableFrame); michael@0: scrollFrame->AddStateBits(NS_SCROLLFRAME_INVALIDATE_CONTENTS_ON_SCROLL); michael@0: } michael@0: const nsStyleTextReset* style = aBlockFrame->StyleTextReset(); michael@0: mLeft.Init(style->mTextOverflow.GetLeft(direction)); michael@0: mRight.Init(style->mTextOverflow.GetRight(direction)); michael@0: // The left/right marker string is setup in ExamineLineFrames when a line michael@0: // has overflow on that side. michael@0: } michael@0: michael@0: /* static */ TextOverflow* michael@0: TextOverflow::WillProcessLines(nsDisplayListBuilder* aBuilder, michael@0: nsIFrame* aBlockFrame) michael@0: { michael@0: if (!CanHaveTextOverflow(aBuilder, aBlockFrame)) { michael@0: return nullptr; michael@0: } michael@0: nsAutoPtr textOverflow(new TextOverflow); michael@0: textOverflow->Init(aBuilder, aBlockFrame); michael@0: return textOverflow.forget(); michael@0: } michael@0: michael@0: void michael@0: TextOverflow::ExamineFrameSubtree(nsIFrame* aFrame, michael@0: const nsRect& aContentArea, michael@0: const nsRect& aInsideMarkersArea, michael@0: FrameHashtable* aFramesToHide, michael@0: AlignmentEdges* aAlignmentEdges, michael@0: bool* aFoundVisibleTextOrAtomic, michael@0: InnerClipEdges* aClippedMarkerEdges) michael@0: { michael@0: const nsIAtom* frameType = aFrame->GetType(); michael@0: if (frameType == nsGkAtoms::brFrame || michael@0: frameType == nsGkAtoms::placeholderFrame) { michael@0: return; michael@0: } michael@0: const bool isAtomic = IsAtomicElement(aFrame, frameType); michael@0: if (aFrame->StyleVisibility()->IsVisible()) { michael@0: nsRect childRect = aFrame->GetScrollableOverflowRect() + michael@0: aFrame->GetOffsetTo(mBlock); michael@0: bool overflowLeft = childRect.x < aContentArea.x; michael@0: bool overflowRight = childRect.XMost() > aContentArea.XMost(); michael@0: if (overflowLeft) { michael@0: mLeft.mHasOverflow = true; michael@0: } michael@0: if (overflowRight) { michael@0: mRight.mHasOverflow = true; michael@0: } michael@0: if (isAtomic && ((mLeft.mActive && overflowLeft) || michael@0: (mRight.mActive && overflowRight))) { michael@0: aFramesToHide->PutEntry(aFrame); michael@0: } else if (isAtomic || frameType == nsGkAtoms::textFrame) { michael@0: AnalyzeMarkerEdges(aFrame, frameType, aInsideMarkersArea, michael@0: aFramesToHide, aAlignmentEdges, michael@0: aFoundVisibleTextOrAtomic, michael@0: aClippedMarkerEdges); michael@0: } michael@0: } michael@0: if (isAtomic) { michael@0: return; michael@0: } michael@0: michael@0: nsIFrame* child = aFrame->GetFirstPrincipalChild(); michael@0: while (child) { michael@0: ExamineFrameSubtree(child, aContentArea, aInsideMarkersArea, michael@0: aFramesToHide, aAlignmentEdges, michael@0: aFoundVisibleTextOrAtomic, michael@0: aClippedMarkerEdges); michael@0: child = child->GetNextSibling(); michael@0: } michael@0: } michael@0: michael@0: void michael@0: TextOverflow::AnalyzeMarkerEdges(nsIFrame* aFrame, michael@0: const nsIAtom* aFrameType, michael@0: const nsRect& aInsideMarkersArea, michael@0: FrameHashtable* aFramesToHide, michael@0: AlignmentEdges* aAlignmentEdges, michael@0: bool* aFoundVisibleTextOrAtomic, michael@0: InnerClipEdges* aClippedMarkerEdges) michael@0: { michael@0: nsRect borderRect(aFrame->GetOffsetTo(mBlock), aFrame->GetSize()); michael@0: nscoord leftOverlap = michael@0: std::max(aInsideMarkersArea.x - borderRect.x, 0); michael@0: nscoord rightOverlap = michael@0: std::max(borderRect.XMost() - aInsideMarkersArea.XMost(), 0); michael@0: bool insideLeftEdge = aInsideMarkersArea.x <= borderRect.XMost(); michael@0: bool insideRightEdge = borderRect.x <= aInsideMarkersArea.XMost(); michael@0: michael@0: if (leftOverlap > 0) { michael@0: aClippedMarkerEdges->AccumulateLeft(borderRect); michael@0: if (!mLeft.mActive) { michael@0: leftOverlap = 0; michael@0: } michael@0: } michael@0: if (rightOverlap > 0) { michael@0: aClippedMarkerEdges->AccumulateRight(borderRect); michael@0: if (!mRight.mActive) { michael@0: rightOverlap = 0; michael@0: } michael@0: } michael@0: michael@0: if ((leftOverlap > 0 && insideLeftEdge) || michael@0: (rightOverlap > 0 && insideRightEdge)) { michael@0: if (aFrameType == nsGkAtoms::textFrame) { michael@0: if (aInsideMarkersArea.x < aInsideMarkersArea.XMost()) { michael@0: // a clipped text frame and there is some room between the markers michael@0: nscoord snappedLeft, snappedRight; michael@0: bool isFullyClipped = michael@0: IsFullyClipped(static_cast(aFrame), michael@0: leftOverlap, rightOverlap, &snappedLeft, &snappedRight); michael@0: if (!isFullyClipped) { michael@0: nsRect snappedRect = borderRect; michael@0: if (leftOverlap > 0) { michael@0: snappedRect.x += snappedLeft; michael@0: snappedRect.width -= snappedLeft; michael@0: } michael@0: if (rightOverlap > 0) { michael@0: snappedRect.width -= snappedRight; michael@0: } michael@0: aAlignmentEdges->Accumulate(snappedRect); michael@0: *aFoundVisibleTextOrAtomic = true; michael@0: } michael@0: } michael@0: } else { michael@0: aFramesToHide->PutEntry(aFrame); michael@0: } michael@0: } else if (!insideLeftEdge || !insideRightEdge) { michael@0: // frame is outside michael@0: if (IsAtomicElement(aFrame, aFrameType)) { michael@0: aFramesToHide->PutEntry(aFrame); michael@0: } michael@0: } else { michael@0: // frame is inside michael@0: aAlignmentEdges->Accumulate(borderRect); michael@0: *aFoundVisibleTextOrAtomic = true; michael@0: } michael@0: } michael@0: michael@0: void michael@0: TextOverflow::ExamineLineFrames(nsLineBox* aLine, michael@0: FrameHashtable* aFramesToHide, michael@0: AlignmentEdges* aAlignmentEdges) michael@0: { michael@0: // No ellipsing for 'clip' style. michael@0: bool suppressLeft = mLeft.mStyle->mType == NS_STYLE_TEXT_OVERFLOW_CLIP; michael@0: bool suppressRight = mRight.mStyle->mType == NS_STYLE_TEXT_OVERFLOW_CLIP; michael@0: if (mCanHaveHorizontalScrollbar) { michael@0: nsPoint pos = mScrollableFrame->GetScrollPosition(); michael@0: nsRect scrollRange = mScrollableFrame->GetScrollRange(); michael@0: // No ellipsing when nothing to scroll to on that side (this includes michael@0: // overflow:auto that doesn't trigger a horizontal scrollbar). michael@0: if (pos.x <= scrollRange.x) { michael@0: suppressLeft = true; michael@0: } michael@0: if (pos.x >= scrollRange.XMost()) { michael@0: suppressRight = true; michael@0: } michael@0: } michael@0: michael@0: nsRect contentArea = mContentArea; michael@0: const nscoord scrollAdjust = mAdjustForPixelSnapping ? michael@0: mBlock->PresContext()->AppUnitsPerDevPixel() : 0; michael@0: InflateLeft(&contentArea, scrollAdjust); michael@0: InflateRight(&contentArea, scrollAdjust); michael@0: nsRect lineRect = aLine->GetScrollableOverflowArea(); michael@0: const bool leftOverflow = michael@0: !suppressLeft && lineRect.x < contentArea.x; michael@0: const bool rightOverflow = michael@0: !suppressRight && lineRect.XMost() > contentArea.XMost(); michael@0: if (!leftOverflow && !rightOverflow) { michael@0: // The line does not overflow on a side we should ellipsize. michael@0: return; michael@0: } michael@0: michael@0: int pass = 0; michael@0: bool retryEmptyLine = true; michael@0: bool guessLeft = leftOverflow; michael@0: bool guessRight = rightOverflow; michael@0: mLeft.mActive = leftOverflow; michael@0: mRight.mActive = rightOverflow; michael@0: bool clippedLeftMarker = false; michael@0: bool clippedRightMarker = false; michael@0: do { michael@0: // Setup marker strings as needed. michael@0: if (guessLeft) { michael@0: mLeft.SetupString(mBlock); michael@0: } michael@0: if (guessRight) { michael@0: mRight.SetupString(mBlock); michael@0: } michael@0: michael@0: // If there is insufficient space for both markers then keep the one on the michael@0: // end side per the block's 'direction'. michael@0: nscoord rightMarkerWidth = mRight.mActive ? mRight.mWidth : 0; michael@0: nscoord leftMarkerWidth = mLeft.mActive ? mLeft.mWidth : 0; michael@0: if (leftMarkerWidth && rightMarkerWidth && michael@0: leftMarkerWidth + rightMarkerWidth > contentArea.width) { michael@0: if (mBlockIsRTL) { michael@0: rightMarkerWidth = 0; michael@0: } else { michael@0: leftMarkerWidth = 0; michael@0: } michael@0: } michael@0: michael@0: // Calculate the area between the potential markers aligned at the michael@0: // block's edge. michael@0: nsRect insideMarkersArea = mContentArea; michael@0: if (guessLeft) { michael@0: InflateLeft(&insideMarkersArea, -leftMarkerWidth); michael@0: } michael@0: if (guessRight) { michael@0: InflateRight(&insideMarkersArea, -rightMarkerWidth); michael@0: } michael@0: michael@0: // Analyze the frames on aLine for the overflow situation at the content michael@0: // edges and at the edges of the area between the markers. michael@0: bool foundVisibleTextOrAtomic = false; michael@0: int32_t n = aLine->GetChildCount(); michael@0: nsIFrame* child = aLine->mFirstChild; michael@0: InnerClipEdges clippedMarkerEdges; michael@0: for (; n-- > 0; child = child->GetNextSibling()) { michael@0: ExamineFrameSubtree(child, contentArea, insideMarkersArea, michael@0: aFramesToHide, aAlignmentEdges, michael@0: &foundVisibleTextOrAtomic, michael@0: &clippedMarkerEdges); michael@0: } michael@0: if (!foundVisibleTextOrAtomic && retryEmptyLine) { michael@0: aAlignmentEdges->mAssigned = false; michael@0: aFramesToHide->Clear(); michael@0: pass = -1; michael@0: if (mLeft.IsNeeded() && mLeft.mActive && !clippedLeftMarker) { michael@0: if (clippedMarkerEdges.mAssignedLeft && michael@0: clippedMarkerEdges.mLeft - mContentArea.X() > 0) { michael@0: mLeft.mWidth = clippedMarkerEdges.mLeft - mContentArea.X(); michael@0: NS_ASSERTION(mLeft.mWidth < mLeft.mIntrinsicWidth, michael@0: "clipping a marker should make it strictly smaller"); michael@0: clippedLeftMarker = true; michael@0: } else { michael@0: mLeft.mActive = guessLeft = false; michael@0: } michael@0: continue; michael@0: } michael@0: if (mRight.IsNeeded() && mRight.mActive && !clippedRightMarker) { michael@0: if (clippedMarkerEdges.mAssignedRight && michael@0: mContentArea.XMost() - clippedMarkerEdges.mRight > 0) { michael@0: mRight.mWidth = mContentArea.XMost() - clippedMarkerEdges.mRight; michael@0: NS_ASSERTION(mRight.mWidth < mRight.mIntrinsicWidth, michael@0: "clipping a marker should make it strictly smaller"); michael@0: clippedRightMarker = true; michael@0: } else { michael@0: mRight.mActive = guessRight = false; michael@0: } michael@0: continue; michael@0: } michael@0: // The line simply has no visible content even without markers, michael@0: // so examine the line again without suppressing markers. michael@0: retryEmptyLine = false; michael@0: mLeft.mWidth = mLeft.mIntrinsicWidth; michael@0: mLeft.mActive = guessLeft = leftOverflow; michael@0: mRight.mWidth = mRight.mIntrinsicWidth; michael@0: mRight.mActive = guessRight = rightOverflow; michael@0: continue; michael@0: } michael@0: if (guessLeft == (mLeft.mActive && mLeft.IsNeeded()) && michael@0: guessRight == (mRight.mActive && mRight.IsNeeded())) { michael@0: break; michael@0: } else { michael@0: guessLeft = mLeft.mActive && mLeft.IsNeeded(); michael@0: guessRight = mRight.mActive && mRight.IsNeeded(); michael@0: mLeft.Reset(); michael@0: mRight.Reset(); michael@0: aFramesToHide->Clear(); michael@0: } michael@0: NS_ASSERTION(pass == 0, "2nd pass should never guess wrong"); michael@0: } while (++pass != 2); michael@0: if (!leftOverflow || !mLeft.mActive) { michael@0: mLeft.Reset(); michael@0: } michael@0: if (!rightOverflow || !mRight.mActive) { michael@0: mRight.Reset(); michael@0: } michael@0: } michael@0: michael@0: void michael@0: TextOverflow::ProcessLine(const nsDisplayListSet& aLists, michael@0: nsLineBox* aLine) michael@0: { michael@0: NS_ASSERTION(mLeft.mStyle->mType != NS_STYLE_TEXT_OVERFLOW_CLIP || michael@0: mRight.mStyle->mType != NS_STYLE_TEXT_OVERFLOW_CLIP, michael@0: "TextOverflow with 'clip' for both sides"); michael@0: mLeft.Reset(); michael@0: mLeft.mActive = mLeft.mStyle->mType != NS_STYLE_TEXT_OVERFLOW_CLIP; michael@0: mRight.Reset(); michael@0: mRight.mActive = mRight.mStyle->mType != NS_STYLE_TEXT_OVERFLOW_CLIP; michael@0: michael@0: FrameHashtable framesToHide(100); michael@0: AlignmentEdges alignmentEdges; michael@0: ExamineLineFrames(aLine, &framesToHide, &alignmentEdges); michael@0: bool needLeft = mLeft.IsNeeded(); michael@0: bool needRight = mRight.IsNeeded(); michael@0: if (!needLeft && !needRight) { michael@0: return; michael@0: } michael@0: NS_ASSERTION(mLeft.mStyle->mType != NS_STYLE_TEXT_OVERFLOW_CLIP || michael@0: !needLeft, "left marker for 'clip'"); michael@0: NS_ASSERTION(mRight.mStyle->mType != NS_STYLE_TEXT_OVERFLOW_CLIP || michael@0: !needRight, "right marker for 'clip'"); michael@0: michael@0: // If there is insufficient space for both markers then keep the one on the michael@0: // end side per the block's 'direction'. michael@0: if (needLeft && needRight && michael@0: mLeft.mWidth + mRight.mWidth > mContentArea.width) { michael@0: if (mBlockIsRTL) { michael@0: needRight = false; michael@0: } else { michael@0: needLeft = false; michael@0: } michael@0: } michael@0: nsRect insideMarkersArea = mContentArea; michael@0: if (needLeft) { michael@0: InflateLeft(&insideMarkersArea, -mLeft.mWidth); michael@0: } michael@0: if (needRight) { michael@0: InflateRight(&insideMarkersArea, -mRight.mWidth); michael@0: } michael@0: if (!mCanHaveHorizontalScrollbar && alignmentEdges.mAssigned) { michael@0: nsRect alignmentRect = nsRect(alignmentEdges.x, insideMarkersArea.y, michael@0: alignmentEdges.Width(), 1); michael@0: insideMarkersArea.IntersectRect(insideMarkersArea, alignmentRect); michael@0: } michael@0: michael@0: // Clip and remove display items as needed at the final marker edges. michael@0: nsDisplayList* lists[] = { aLists.Content(), aLists.PositionedDescendants() }; michael@0: for (uint32_t i = 0; i < ArrayLength(lists); ++i) { michael@0: PruneDisplayListContents(lists[i], framesToHide, insideMarkersArea); michael@0: } michael@0: CreateMarkers(aLine, needLeft, needRight, insideMarkersArea); michael@0: } michael@0: michael@0: void michael@0: TextOverflow::PruneDisplayListContents(nsDisplayList* aList, michael@0: const FrameHashtable& aFramesToHide, michael@0: const nsRect& aInsideMarkersArea) michael@0: { michael@0: nsDisplayList saved; michael@0: nsDisplayItem* item; michael@0: while ((item = aList->RemoveBottom())) { michael@0: nsIFrame* itemFrame = item->Frame(); michael@0: if (IsFrameDescendantOfAny(itemFrame, aFramesToHide, mBlock)) { michael@0: item->~nsDisplayItem(); michael@0: continue; michael@0: } michael@0: michael@0: nsDisplayList* wrapper = item->GetSameCoordinateSystemChildren(); michael@0: if (wrapper) { michael@0: if (!itemFrame || GetSelfOrNearestBlock(itemFrame) == mBlock) { michael@0: PruneDisplayListContents(wrapper, aFramesToHide, aInsideMarkersArea); michael@0: } michael@0: } michael@0: michael@0: nsCharClipDisplayItem* charClip = itemFrame ? michael@0: nsCharClipDisplayItem::CheckCast(item) : nullptr; michael@0: if (charClip && GetSelfOrNearestBlock(itemFrame) == mBlock) { michael@0: nsRect rect = itemFrame->GetScrollableOverflowRect() + michael@0: itemFrame->GetOffsetTo(mBlock); michael@0: if (mLeft.IsNeeded() && rect.x < aInsideMarkersArea.x) { michael@0: nscoord left = aInsideMarkersArea.x - rect.x; michael@0: if (MOZ_UNLIKELY(left < 0)) { michael@0: item->~nsDisplayItem(); michael@0: continue; michael@0: } michael@0: charClip->mLeftEdge = left; michael@0: } michael@0: if (mRight.IsNeeded() && rect.XMost() > aInsideMarkersArea.XMost()) { michael@0: nscoord right = rect.XMost() - aInsideMarkersArea.XMost(); michael@0: if (MOZ_UNLIKELY(right < 0)) { michael@0: item->~nsDisplayItem(); michael@0: continue; michael@0: } michael@0: charClip->mRightEdge = right; michael@0: } michael@0: } michael@0: michael@0: saved.AppendToTop(item); michael@0: } michael@0: aList->AppendToTop(&saved); michael@0: } michael@0: michael@0: /* static */ bool michael@0: TextOverflow::CanHaveTextOverflow(nsDisplayListBuilder* aBuilder, michael@0: nsIFrame* aBlockFrame) michael@0: { michael@0: const nsStyleTextReset* style = aBlockFrame->StyleTextReset(); michael@0: // Nothing to do for text-overflow:clip or if 'overflow-x:visible' michael@0: // or if we're just building items for event processing. michael@0: if ((style->mTextOverflow.mLeft.mType == NS_STYLE_TEXT_OVERFLOW_CLIP && michael@0: style->mTextOverflow.mRight.mType == NS_STYLE_TEXT_OVERFLOW_CLIP) || michael@0: IsHorizontalOverflowVisible(aBlockFrame) || michael@0: aBuilder->IsForEventDelivery()) { michael@0: return false; michael@0: } michael@0: michael@0: // Skip ComboboxControlFrame because it would clip the drop-down arrow. michael@0: // Its anon block inherits 'text-overflow' and does what is expected. michael@0: if (aBlockFrame->GetType() == nsGkAtoms::comboboxControlFrame) { michael@0: return false; michael@0: } michael@0: michael@0: // Inhibit the markers if a descendant content owns the caret. michael@0: nsRefPtr caret = aBlockFrame->PresContext()->PresShell()->GetCaret(); michael@0: bool visible = false; michael@0: if (caret && NS_SUCCEEDED(caret->GetCaretVisible(&visible)) && visible) { michael@0: nsCOMPtr domSelection = caret->GetCaretDOMSelection(); michael@0: if (domSelection) { michael@0: nsCOMPtr node; michael@0: domSelection->GetFocusNode(getter_AddRefs(node)); michael@0: nsCOMPtr content = do_QueryInterface(node); michael@0: if (content && nsContentUtils::ContentIsDescendantOf(content, michael@0: aBlockFrame->GetContent())) { michael@0: return false; michael@0: } michael@0: } michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: void michael@0: TextOverflow::CreateMarkers(const nsLineBox* aLine, michael@0: bool aCreateLeft, michael@0: bool aCreateRight, michael@0: const nsRect& aInsideMarkersArea) michael@0: { michael@0: if (aCreateLeft) { michael@0: DisplayListClipState::AutoSaveRestore clipState(mBuilder); michael@0: michael@0: nsRect markerRect = nsRect(aInsideMarkersArea.x - mLeft.mIntrinsicWidth, michael@0: aLine->BStart(), michael@0: mLeft.mIntrinsicWidth, aLine->BSize()); michael@0: markerRect += mBuilder->ToReferenceFrame(mBlock); michael@0: ClipMarker(mContentArea + mBuilder->ToReferenceFrame(mBlock), michael@0: markerRect, clipState); michael@0: nsDisplayItem* marker = new (mBuilder) michael@0: nsDisplayTextOverflowMarker(mBuilder, mBlock, markerRect, michael@0: aLine->GetAscent(), mLeft.mStyle, 0); michael@0: mMarkerList.AppendNewToTop(marker); michael@0: } michael@0: michael@0: if (aCreateRight) { michael@0: DisplayListClipState::AutoSaveRestore clipState(mBuilder); michael@0: michael@0: nsRect markerRect = nsRect(aInsideMarkersArea.XMost(), michael@0: aLine->BStart(), michael@0: mRight.mIntrinsicWidth, aLine->BSize()); michael@0: markerRect += mBuilder->ToReferenceFrame(mBlock); michael@0: ClipMarker(mContentArea + mBuilder->ToReferenceFrame(mBlock), michael@0: markerRect, clipState); michael@0: nsDisplayItem* marker = new (mBuilder) michael@0: nsDisplayTextOverflowMarker(mBuilder, mBlock, markerRect, michael@0: aLine->GetAscent(), mRight.mStyle, 1); michael@0: mMarkerList.AppendNewToTop(marker); michael@0: } michael@0: } michael@0: michael@0: void michael@0: TextOverflow::Marker::SetupString(nsIFrame* aFrame) michael@0: { michael@0: if (mInitialized) { michael@0: return; michael@0: } michael@0: michael@0: if (mStyle->mType == NS_STYLE_TEXT_OVERFLOW_ELLIPSIS) { michael@0: gfxTextRun* textRun = GetEllipsisTextRun(aFrame); michael@0: if (textRun) { michael@0: mWidth = textRun->GetAdvanceWidth(0, textRun->GetLength(), nullptr); michael@0: } else { michael@0: mWidth = 0; michael@0: } michael@0: } else { michael@0: nsRefPtr rc = michael@0: aFrame->PresContext()->PresShell()->CreateReferenceRenderingContext(); michael@0: nsRefPtr fm; michael@0: nsLayoutUtils::GetFontMetricsForFrame(aFrame, getter_AddRefs(fm), michael@0: nsLayoutUtils::FontSizeInflationFor(aFrame)); michael@0: rc->SetFont(fm); michael@0: mWidth = nsLayoutUtils::GetStringWidth(aFrame, rc, mStyle->mString.get(), michael@0: mStyle->mString.Length()); michael@0: } michael@0: mIntrinsicWidth = mWidth; michael@0: mInitialized = true; michael@0: } michael@0: michael@0: } // namespace css michael@0: } // namespace mozilla