TextOverflow.cpp (36100B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include "TextOverflow.h" 8 9 #include <algorithm> 10 11 // Please maintain alphabetical order below 12 #include "TextDrawTarget.h" 13 #include "gfxContext.h" 14 #include "mozilla/PresShell.h" 15 #include "mozilla/ScrollContainerFrame.h" 16 #include "mozilla/dom/Selection.h" 17 #include "nsBlockFrame.h" 18 #include "nsCSSAnonBoxes.h" 19 #include "nsCaret.h" 20 #include "nsContentUtils.h" 21 #include "nsFontMetrics.h" 22 #include "nsIFrameInlines.h" 23 #include "nsLayoutUtils.h" 24 #include "nsPresContext.h" 25 #include "nsRect.h" 26 #include "nsTextFrame.h" 27 28 using mozilla::layout::TextDrawTarget; 29 30 namespace mozilla::css { 31 32 class LazyReferenceRenderingDrawTargetGetterFromFrame final 33 : public gfxFontGroup::LazyReferenceDrawTargetGetter { 34 public: 35 typedef mozilla::gfx::DrawTarget DrawTarget; 36 37 explicit LazyReferenceRenderingDrawTargetGetterFromFrame(nsIFrame* aFrame) 38 : mFrame(aFrame) {} 39 virtual already_AddRefed<DrawTarget> GetRefDrawTarget() override { 40 UniquePtr<gfxContext> ctx = 41 mFrame->PresShell()->CreateReferenceRenderingContext(); 42 RefPtr<DrawTarget> dt = ctx->GetDrawTarget(); 43 return dt.forget(); 44 } 45 46 private: 47 nsIFrame* mFrame; 48 }; 49 50 static gfxTextRun* GetEllipsisTextRun(nsIFrame* aFrame) { 51 RefPtr<nsFontMetrics> fm = 52 nsLayoutUtils::GetInflatedFontMetricsForFrame(aFrame); 53 LazyReferenceRenderingDrawTargetGetterFromFrame lazyRefDrawTargetGetter( 54 aFrame); 55 return fm->GetThebesFontGroup()->GetEllipsisTextRun( 56 aFrame->PresContext()->AppUnitsPerDevPixel(), 57 nsLayoutUtils::GetTextRunOrientFlagsForStyle(aFrame->Style()), 58 lazyRefDrawTargetGetter); 59 } 60 61 static nsIFrame* GetSelfOrNearestBlock(nsIFrame* aFrame) { 62 MOZ_ASSERT(aFrame); 63 return aFrame->IsBlockFrameOrSubclass() 64 ? aFrame 65 : nsLayoutUtils::FindNearestBlockAncestor(aFrame); 66 } 67 68 // Return true if the frame is an atomic inline-level element. 69 // It's not supposed to be called for block frames since we never 70 // process block descendants for text-overflow. 71 static bool IsAtomicElement(nsIFrame* aFrame, LayoutFrameType aFrameType) { 72 MOZ_ASSERT(!aFrame->IsBlockFrameOrSubclass() || !aFrame->IsBlockOutside(), 73 "unexpected block frame"); 74 MOZ_ASSERT(aFrameType != LayoutFrameType::Placeholder, 75 "unexpected placeholder frame"); 76 return !aFrame->IsLineParticipant(); 77 } 78 79 static bool IsFullyClipped(nsTextFrame* aFrame, nscoord aLeft, nscoord aRight, 80 nscoord* aSnappedLeft, nscoord* aSnappedRight) { 81 *aSnappedLeft = aLeft; 82 *aSnappedRight = aRight; 83 if (aLeft <= 0 && aRight <= 0) { 84 return false; 85 } 86 return !aFrame->MeasureCharClippedText(aLeft, aRight, aSnappedLeft, 87 aSnappedRight); 88 } 89 90 static bool IsInlineAxisOverflowVisible(nsIFrame* aFrame) { 91 MOZ_ASSERT(aFrame && aFrame->IsBlockFrameOrSubclass(), 92 "expected a block frame"); 93 94 nsIFrame* f = aFrame; 95 while (f && f->Style()->IsAnonBox() && !f->IsScrollContainerFrame()) { 96 f = f->GetParent(); 97 } 98 if (!f) { 99 return true; 100 } 101 auto overflow = aFrame->GetWritingMode().IsVertical() 102 ? f->StyleDisplay()->mOverflowY 103 : f->StyleDisplay()->mOverflowX; 104 return overflow == StyleOverflow::Visible; 105 } 106 107 static void ClipMarker(const nsRect& aContentArea, const nsRect& aMarkerRect, 108 DisplayListClipState::AutoSaveRestore& aClipState) { 109 nscoord rightOverflow = aMarkerRect.XMost() - aContentArea.XMost(); 110 nsRect markerRect = aMarkerRect; 111 if (rightOverflow > 0) { 112 // Marker overflows on the right side (content width < marker width). 113 markerRect.width -= rightOverflow; 114 aClipState.ClipContentDescendants(markerRect); 115 } else { 116 nscoord leftOverflow = aContentArea.x - aMarkerRect.x; 117 if (leftOverflow > 0) { 118 // Marker overflows on the left side 119 markerRect.width -= leftOverflow; 120 markerRect.x += leftOverflow; 121 aClipState.ClipContentDescendants(markerRect); 122 } 123 } 124 } 125 126 static void InflateIStart(WritingMode aWM, LogicalRect* aRect, nscoord aDelta) { 127 nscoord iend = aRect->IEnd(aWM); 128 aRect->IStart(aWM) -= aDelta; 129 aRect->ISize(aWM) = std::max(iend - aRect->IStart(aWM), 0); 130 } 131 132 static void InflateIEnd(WritingMode aWM, LogicalRect* aRect, nscoord aDelta) { 133 aRect->ISize(aWM) = std::max(aRect->ISize(aWM) + aDelta, 0); 134 } 135 136 static bool IsFrameDescendantOfAny( 137 nsIFrame* aChild, const TextOverflow::FrameHashtable& aSetOfFrames, 138 nsIFrame* aCommonAncestor) { 139 for (nsIFrame* f = aChild; f && f != aCommonAncestor; 140 f = nsLayoutUtils::GetCrossDocParentFrameInProcess(f)) { 141 if (aSetOfFrames.Contains(f)) { 142 return true; 143 } 144 } 145 return false; 146 } 147 148 class nsDisplayTextOverflowMarker final : public nsPaintedDisplayItem { 149 public: 150 nsDisplayTextOverflowMarker(nsDisplayListBuilder* aBuilder, nsIFrame* aFrame, 151 const nsRect& aRect, nscoord aAscent, 152 const StyleTextOverflowSide& aStyle) 153 : nsPaintedDisplayItem(aBuilder, aFrame), 154 mRect(aRect), 155 mStyle(aStyle), 156 mAscent(aAscent) { 157 MOZ_COUNT_CTOR(nsDisplayTextOverflowMarker); 158 } 159 160 MOZ_COUNTED_DTOR_FINAL(nsDisplayTextOverflowMarker) 161 162 virtual nsRect GetBounds(nsDisplayListBuilder* aBuilder, 163 bool* aSnap) const override { 164 *aSnap = false; 165 return nsLayoutUtils::GetTextShadowRectsUnion(mRect, mFrame); 166 } 167 168 virtual nsRect GetComponentAlphaBounds( 169 nsDisplayListBuilder* aBuilder) const override { 170 if (gfxPlatform::GetPlatform()->RespectsFontStyleSmoothing()) { 171 // On OS X, web authors can turn off subpixel text rendering using the 172 // CSS property -moz-osx-font-smoothing. If they do that, we don't need 173 // to use component alpha layers for the affected text. 174 if (mFrame->StyleFont()->mFont.smoothing == NS_FONT_SMOOTHING_GRAYSCALE) { 175 return nsRect(); 176 } 177 } 178 bool snap; 179 return GetBounds(aBuilder, &snap); 180 } 181 182 virtual void Paint(nsDisplayListBuilder* aBuilder, gfxContext* aCtx) override; 183 184 void PaintTextToContext(gfxContext* aCtx, nsPoint aOffsetFromRect); 185 186 virtual bool CreateWebRenderCommands( 187 mozilla::wr::DisplayListBuilder& aBuilder, 188 mozilla::wr::IpcResourceUpdateQueue& aResources, 189 const StackingContextHelper& aSc, 190 layers::RenderRootStateManager* aManager, 191 nsDisplayListBuilder* aDisplayListBuilder) override; 192 193 NS_DISPLAY_DECL_NAME("TextOverflow", TYPE_TEXT_OVERFLOW) 194 private: 195 nsRect mRect; // in reference frame coordinates 196 const StyleTextOverflowSide mStyle; 197 nscoord mAscent; // baseline for the marker text in mRect 198 }; 199 200 static void PaintTextShadowCallback(gfxContext* aCtx, nsPoint aShadowOffset, 201 const nscolor& aShadowColor, void* aData) { 202 reinterpret_cast<nsDisplayTextOverflowMarker*>(aData)->PaintTextToContext( 203 aCtx, aShadowOffset); 204 } 205 206 void nsDisplayTextOverflowMarker::Paint(nsDisplayListBuilder* aBuilder, 207 gfxContext* aCtx) { 208 nscolor foregroundColor = 209 nsLayoutUtils::GetTextColor(mFrame, &nsStyleText::mWebkitTextFillColor); 210 211 // Paint the text-shadows for the overflow marker 212 nsLayoutUtils::PaintTextShadow(mFrame, aCtx, mRect, 213 GetPaintRect(aBuilder, aCtx), foregroundColor, 214 PaintTextShadowCallback, (void*)this); 215 aCtx->SetColor(gfx::sRGBColor::FromABGR(foregroundColor)); 216 PaintTextToContext(aCtx, nsPoint(0, 0)); 217 } 218 219 void nsDisplayTextOverflowMarker::PaintTextToContext(gfxContext* aCtx, 220 nsPoint aOffsetFromRect) { 221 WritingMode wm = mFrame->GetWritingMode(); 222 nsPoint pt(mRect.x, mRect.y); 223 if (wm.IsVertical()) { 224 if (wm.IsVerticalLR()) { 225 pt.x = NSToCoordFloor( 226 nsLayoutUtils::GetMaybeSnappedBaselineX(mFrame, aCtx, pt.x, mAscent)); 227 } else { 228 pt.x = NSToCoordFloor(nsLayoutUtils::GetMaybeSnappedBaselineX( 229 mFrame, aCtx, pt.x + mRect.width, -mAscent)); 230 } 231 } else { 232 pt.y = NSToCoordFloor( 233 nsLayoutUtils::GetMaybeSnappedBaselineY(mFrame, aCtx, pt.y, mAscent)); 234 } 235 pt += aOffsetFromRect; 236 237 if (mStyle.IsEllipsis()) { 238 gfxTextRun* textRun = GetEllipsisTextRun(mFrame); 239 if (textRun) { 240 NS_ASSERTION(!textRun->IsRightToLeft(), 241 "Ellipsis textruns should always be LTR!"); 242 gfx::Point gfxPt(pt.x, pt.y); 243 auto& paletteCache = mFrame->PresContext()->FontPaletteCache(); 244 textRun->Draw(gfxTextRun::Range(textRun), gfxPt, 245 gfxTextRun::DrawParams(aCtx, paletteCache)); 246 } 247 } else { 248 RefPtr<nsFontMetrics> fm = 249 nsLayoutUtils::GetInflatedFontMetricsForFrame(mFrame); 250 nsDependentAtomString str16(mStyle.AsString().AsAtom()); 251 nsLayoutUtils::DrawString(mFrame, *fm, aCtx, str16.get(), str16.Length(), 252 pt); 253 } 254 } 255 256 bool nsDisplayTextOverflowMarker::CreateWebRenderCommands( 257 mozilla::wr::DisplayListBuilder& aBuilder, 258 mozilla::wr::IpcResourceUpdateQueue& aResources, 259 const StackingContextHelper& aSc, layers::RenderRootStateManager* aManager, 260 nsDisplayListBuilder* aDisplayListBuilder) { 261 bool snap; 262 nsRect bounds = GetBounds(aDisplayListBuilder, &snap); 263 if (bounds.IsEmpty()) { 264 return true; 265 } 266 267 // Run the rendering algorithm to capture the glyphs and shadows 268 RefPtr<TextDrawTarget> textDrawer = 269 new TextDrawTarget(aBuilder, aResources, aSc, aManager, this, bounds); 270 MOZ_ASSERT(textDrawer->IsValid()); 271 if (!textDrawer->IsValid()) { 272 return false; 273 } 274 gfxContext captureCtx(textDrawer); 275 Paint(aDisplayListBuilder, &captureCtx); 276 textDrawer->TerminateShadows(); 277 278 return textDrawer->Finish(); 279 } 280 281 TextOverflow::TextOverflow(nsDisplayListBuilder* aBuilder, 282 nsBlockFrame* aBlockFrame) 283 : mContentArea(aBlockFrame->GetWritingMode(), 284 aBlockFrame->GetContentRectRelativeToSelf(), 285 aBlockFrame->GetSize()), 286 mBuilder(aBuilder), 287 mBlock(aBlockFrame), 288 mScrollContainerFrame( 289 nsLayoutUtils::GetScrollContainerFrameFor(aBlockFrame)), 290 mMarkerList(aBuilder), 291 mBlockSize(aBlockFrame->GetSize()), 292 mBlockWM(aBlockFrame->GetWritingMode()), 293 mCanHaveInlineAxisScrollbar(false), 294 mInLineClampContext(aBlockFrame->IsInLineClampContext()), 295 mAdjustForPixelSnapping(false) { 296 if (mScrollContainerFrame) { 297 auto scrollbarStyle = 298 mBlockWM.IsVertical() 299 ? mScrollContainerFrame->GetScrollStyles().mVertical 300 : mScrollContainerFrame->GetScrollStyles().mHorizontal; 301 mCanHaveInlineAxisScrollbar = scrollbarStyle != StyleOverflow::Hidden; 302 if (!mAdjustForPixelSnapping) { 303 // Scrolling to the end position can leave some text still overflowing due 304 // to pixel snapping behaviour in our scrolling code. 305 mAdjustForPixelSnapping = mCanHaveInlineAxisScrollbar; 306 } 307 // Use a null containerSize to convert a vector from logical to physical. 308 const nsSize nullContainerSize; 309 mContentArea.MoveBy( 310 mBlockWM, 311 LogicalPoint(mBlockWM, mScrollContainerFrame->GetScrollPosition(), 312 nullContainerSize)); 313 } 314 StyleDirection direction = aBlockFrame->StyleVisibility()->mDirection; 315 const nsStyleTextReset* style = aBlockFrame->StyleTextReset(); 316 317 const auto& textOverflow = style->mTextOverflow; 318 bool shouldToggleDirection = 319 textOverflow.sides_are_logical && (direction == StyleDirection::Rtl); 320 const auto& leftSide = 321 shouldToggleDirection ? textOverflow.second : textOverflow.first; 322 const auto& rightSide = 323 shouldToggleDirection ? textOverflow.first : textOverflow.second; 324 325 if (mBlockWM.IsBidiLTR()) { 326 mIStart.Init(leftSide); 327 mIEnd.Init(rightSide); 328 } else { 329 mIStart.Init(rightSide); 330 mIEnd.Init(leftSide); 331 } 332 // The left/right marker string is setup in ExamineLineFrames when a line 333 // has overflow on that side. 334 } 335 336 /* static */ 337 Maybe<TextOverflow> TextOverflow::WillProcessLines( 338 nsDisplayListBuilder* aBuilder, nsBlockFrame* aBlockFrame) { 339 // Ignore text-overflow and -webkit-line-clamp for event and frame visibility 340 // processing. 341 if (aBuilder->IsForEventDelivery() || aBuilder->IsForFrameVisibility() || 342 !CanHaveOverflowMarkers(aBlockFrame)) { 343 return Nothing(); 344 } 345 ScrollContainerFrame* scrollContainerFrame = 346 nsLayoutUtils::GetScrollContainerFrameFor(aBlockFrame); 347 if (scrollContainerFrame && scrollContainerFrame->IsTransformingByAPZ()) { 348 // If the APZ is actively scrolling this, don't bother with markers. 349 return Nothing(); 350 } 351 return Some(TextOverflow(aBuilder, aBlockFrame)); 352 } 353 354 void TextOverflow::ExamineFrameSubtree(nsIFrame* aFrame, 355 const LogicalRect& aContentArea, 356 const LogicalRect& aInsideMarkersArea, 357 FrameHashtable* aFramesToHide, 358 AlignmentEdges* aAlignmentEdges, 359 bool* aFoundVisibleTextOrAtomic, 360 InnerClipEdges* aClippedMarkerEdges) { 361 const LayoutFrameType frameType = aFrame->Type(); 362 if (frameType == LayoutFrameType::Br || 363 frameType == LayoutFrameType::Placeholder) { 364 return; 365 } 366 const bool isAtomic = IsAtomicElement(aFrame, frameType); 367 if (aFrame->StyleVisibility()->IsVisible()) { 368 LogicalRect childRect = 369 GetLogicalScrollableOverflowRectRelativeToBlock(aFrame); 370 bool overflowIStart = 371 childRect.IStart(mBlockWM) < aContentArea.IStart(mBlockWM); 372 bool overflowIEnd = childRect.IEnd(mBlockWM) > aContentArea.IEnd(mBlockWM); 373 if (overflowIStart) { 374 mIStart.mHasOverflow = true; 375 } 376 if (overflowIEnd) { 377 mIEnd.mHasOverflow = true; 378 } 379 if (isAtomic && ((mIStart.mActive && overflowIStart) || 380 (mIEnd.mActive && overflowIEnd))) { 381 aFramesToHide->Insert(aFrame); 382 } else if (isAtomic || frameType == LayoutFrameType::Text) { 383 AnalyzeMarkerEdges(aFrame, frameType, aInsideMarkersArea, aFramesToHide, 384 aAlignmentEdges, aFoundVisibleTextOrAtomic, 385 aClippedMarkerEdges); 386 } 387 } 388 if (isAtomic) { 389 return; 390 } 391 392 for (nsIFrame* child : aFrame->PrincipalChildList()) { 393 ExamineFrameSubtree(child, aContentArea, aInsideMarkersArea, aFramesToHide, 394 aAlignmentEdges, aFoundVisibleTextOrAtomic, 395 aClippedMarkerEdges); 396 } 397 } 398 399 void TextOverflow::AnalyzeMarkerEdges(nsIFrame* aFrame, 400 LayoutFrameType aFrameType, 401 const LogicalRect& aInsideMarkersArea, 402 FrameHashtable* aFramesToHide, 403 AlignmentEdges* aAlignmentEdges, 404 bool* aFoundVisibleTextOrAtomic, 405 InnerClipEdges* aClippedMarkerEdges) { 406 MOZ_ASSERT(aFrameType == LayoutFrameType::Text || 407 IsAtomicElement(aFrame, aFrameType)); 408 LogicalRect borderRect(mBlockWM, 409 nsRect(aFrame->GetOffsetTo(mBlock), aFrame->GetSize()), 410 mBlockSize); 411 nscoord istartOverlap = std::max( 412 aInsideMarkersArea.IStart(mBlockWM) - borderRect.IStart(mBlockWM), 0); 413 nscoord iendOverlap = std::max( 414 borderRect.IEnd(mBlockWM) - aInsideMarkersArea.IEnd(mBlockWM), 0); 415 bool insideIStartEdge = 416 aInsideMarkersArea.IStart(mBlockWM) <= borderRect.IEnd(mBlockWM); 417 bool insideIEndEdge = 418 borderRect.IStart(mBlockWM) <= aInsideMarkersArea.IEnd(mBlockWM); 419 420 if (istartOverlap > 0) { 421 aClippedMarkerEdges->AccumulateIStart(mBlockWM, borderRect); 422 if (!mIStart.mActive) { 423 istartOverlap = 0; 424 } 425 } 426 if (iendOverlap > 0) { 427 aClippedMarkerEdges->AccumulateIEnd(mBlockWM, borderRect); 428 if (!mIEnd.mActive) { 429 iendOverlap = 0; 430 } 431 } 432 433 if ((istartOverlap > 0 && insideIStartEdge) || 434 (iendOverlap > 0 && insideIEndEdge)) { 435 if (aFrameType == LayoutFrameType::Text) { 436 auto textFrame = static_cast<nsTextFrame*>(aFrame); 437 if ((aInsideMarkersArea.IStart(mBlockWM) < 438 aInsideMarkersArea.IEnd(mBlockWM)) && 439 textFrame->HasNonSuppressedText()) { 440 // a clipped text frame and there is some room between the markers 441 nscoord snappedIStart, snappedIEnd; 442 bool isFullyClipped = 443 mBlockWM.IsBidiLTR() 444 ? IsFullyClipped(textFrame, istartOverlap, iendOverlap, 445 &snappedIStart, &snappedIEnd) 446 : IsFullyClipped(textFrame, iendOverlap, istartOverlap, 447 &snappedIEnd, &snappedIStart); 448 if (!isFullyClipped) { 449 LogicalRect snappedRect = borderRect; 450 if (istartOverlap > 0) { 451 snappedRect.IStart(mBlockWM) += snappedIStart; 452 snappedRect.ISize(mBlockWM) -= snappedIStart; 453 } 454 if (iendOverlap > 0) { 455 snappedRect.ISize(mBlockWM) -= snappedIEnd; 456 } 457 aAlignmentEdges->AccumulateInner(mBlockWM, snappedRect); 458 *aFoundVisibleTextOrAtomic = true; 459 } 460 } 461 } else { 462 aFramesToHide->Insert(aFrame); 463 } 464 } else if (!insideIStartEdge || !insideIEndEdge) { 465 // frame is outside 466 if (!insideIStartEdge) { 467 aAlignmentEdges->AccumulateOuter(mBlockWM, borderRect); 468 } 469 if (IsAtomicElement(aFrame, aFrameType)) { 470 aFramesToHide->Insert(aFrame); 471 } 472 } else { 473 // frame is inside 474 aAlignmentEdges->AccumulateInner(mBlockWM, borderRect); 475 if (aFrameType == LayoutFrameType::Text) { 476 auto textFrame = static_cast<nsTextFrame*>(aFrame); 477 if (textFrame->HasNonSuppressedText()) { 478 *aFoundVisibleTextOrAtomic = true; 479 } 480 } else { 481 *aFoundVisibleTextOrAtomic = true; 482 } 483 } 484 } 485 486 LogicalRect TextOverflow::ExamineLineFrames(nsLineBox* aLine, 487 FrameHashtable* aFramesToHide, 488 AlignmentEdges* aAlignmentEdges) { 489 // No ellipsing for 'clip' style. 490 bool suppressIStart = mIStart.IsSuppressed(mInLineClampContext); 491 bool suppressIEnd = mIEnd.IsSuppressed(mInLineClampContext); 492 if (mCanHaveInlineAxisScrollbar) { 493 LogicalPoint pos(mBlockWM, mScrollContainerFrame->GetScrollPosition(), 494 mBlockSize); 495 LogicalRect scrollRange(mBlockWM, mScrollContainerFrame->GetScrollRange(), 496 mBlockSize); 497 // No ellipsing when nothing to scroll to on that side (this includes 498 // overflow:auto that doesn't trigger a horizontal scrollbar). 499 if (pos.I(mBlockWM) <= scrollRange.IStart(mBlockWM)) { 500 suppressIStart = true; 501 } 502 if (pos.I(mBlockWM) >= scrollRange.IEnd(mBlockWM)) { 503 // Except that we always want to display a -webkit-line-clamp ellipsis. 504 if (!mIEnd.mHasBlockEllipsis) { 505 suppressIEnd = true; 506 } 507 } 508 } 509 510 LogicalRect contentArea = mContentArea; 511 bool snapStart = true, snapEnd = true; 512 nscoord startEdge, endEdge; 513 if (aLine->GetFloatEdges(&startEdge, &endEdge)) { 514 // Narrow the |contentArea| to account for any floats on this line, and 515 // don't bother with the snapping quirk on whichever side(s) we narrow. 516 nscoord delta = endEdge - contentArea.IEnd(mBlockWM); 517 if (delta < 0) { 518 nscoord newSize = contentArea.ISize(mBlockWM) + delta; 519 contentArea.ISize(mBlockWM) = std::max(nscoord(0), newSize); 520 snapEnd = false; 521 } 522 delta = startEdge - contentArea.IStart(mBlockWM); 523 if (delta > 0) { 524 contentArea.IStart(mBlockWM) = startEdge; 525 nscoord newSize = contentArea.ISize(mBlockWM) - delta; 526 contentArea.ISize(mBlockWM) = std::max(nscoord(0), newSize); 527 snapStart = false; 528 } 529 } 530 // Save the non-snapped area since that's what we want to use when placing 531 // the markers (our return value). The snapped area is only for analysis. 532 LogicalRect nonSnappedContentArea = contentArea; 533 if (mAdjustForPixelSnapping) { 534 const nscoord scrollAdjust = mBlock->PresContext()->AppUnitsPerDevPixel(); 535 if (snapStart) { 536 InflateIStart(mBlockWM, &contentArea, scrollAdjust); 537 } 538 if (snapEnd) { 539 InflateIEnd(mBlockWM, &contentArea, scrollAdjust); 540 } 541 } 542 543 LogicalRect lineRect(mBlockWM, aLine->ScrollableOverflowRect(), mBlockSize); 544 const bool istartWantsMarker = 545 !suppressIStart && 546 lineRect.IStart(mBlockWM) < contentArea.IStart(mBlockWM); 547 const bool iendWantsTextOverflowMarker = 548 !suppressIEnd && lineRect.IEnd(mBlockWM) > contentArea.IEnd(mBlockWM); 549 const bool iendWantsBlockEllipsisMarker = 550 !suppressIEnd && mIEnd.mHasBlockEllipsis; 551 const bool iendWantsMarker = 552 iendWantsTextOverflowMarker || iendWantsBlockEllipsisMarker; 553 if (!istartWantsMarker && !iendWantsMarker) { 554 // We don't need any markers on this line. 555 return nonSnappedContentArea; 556 } 557 558 int pass = 0; 559 bool retryEmptyLine = true; 560 bool guessIStart = istartWantsMarker; 561 bool guessIEnd = iendWantsMarker; 562 mIStart.mActive = istartWantsMarker; 563 mIEnd.mActive = iendWantsMarker; 564 mIStart.mEdgeAligned = mCanHaveInlineAxisScrollbar && istartWantsMarker; 565 mIEnd.mEdgeAligned = 566 mCanHaveInlineAxisScrollbar && iendWantsTextOverflowMarker; 567 bool clippedIStartMarker = false; 568 bool clippedIEndMarker = false; 569 do { 570 // Setup marker strings as needed. 571 if (guessIStart) { 572 mIStart.SetupString(mBlock); 573 } 574 if (guessIEnd) { 575 mIEnd.SetupString(mBlock); 576 } 577 578 // If there is insufficient space for both markers then keep the one on the 579 // end side per the block's 'direction'. 580 nscoord istartMarkerISize = mIStart.mActive ? mIStart.mISize : 0; 581 nscoord iendMarkerISize = mIEnd.mActive ? mIEnd.mISize : 0; 582 if (istartMarkerISize && iendMarkerISize && 583 istartMarkerISize + iendMarkerISize > contentArea.ISize(mBlockWM)) { 584 istartMarkerISize = 0; 585 } 586 587 // Calculate the area between the potential markers aligned at the 588 // block's edge. 589 LogicalRect insideMarkersArea = nonSnappedContentArea; 590 if (guessIStart) { 591 InflateIStart(mBlockWM, &insideMarkersArea, -istartMarkerISize); 592 } 593 if (guessIEnd) { 594 InflateIEnd(mBlockWM, &insideMarkersArea, -iendMarkerISize); 595 } 596 597 // Analyze the frames on aLine for the overflow situation at the content 598 // edges and at the edges of the area between the markers. 599 bool foundVisibleTextOrAtomic = false; 600 int32_t n = aLine->GetChildCount(); 601 nsIFrame* child = aLine->mFirstChild; 602 InnerClipEdges clippedMarkerEdges; 603 for (; n-- > 0; child = child->GetNextSibling()) { 604 ExamineFrameSubtree(child, contentArea, insideMarkersArea, aFramesToHide, 605 aAlignmentEdges, &foundVisibleTextOrAtomic, 606 &clippedMarkerEdges); 607 } 608 if (!foundVisibleTextOrAtomic && retryEmptyLine) { 609 aAlignmentEdges->mAssignedInner = false; 610 aAlignmentEdges->mIEndOuter = 0; 611 aFramesToHide->Clear(); 612 pass = -1; 613 if (mIStart.IsNeeded() && mIStart.mActive && !clippedIStartMarker) { 614 if (clippedMarkerEdges.mAssignedIStart && 615 clippedMarkerEdges.mIStart > 616 nonSnappedContentArea.IStart(mBlockWM)) { 617 mIStart.mISize = clippedMarkerEdges.mIStart - 618 nonSnappedContentArea.IStart(mBlockWM); 619 NS_ASSERTION(mIStart.mISize < mIStart.mIntrinsicISize, 620 "clipping a marker should make it strictly smaller"); 621 clippedIStartMarker = true; 622 } else { 623 mIStart.mActive = guessIStart = false; 624 } 625 continue; 626 } 627 if (mIEnd.IsNeeded() && mIEnd.mActive && !clippedIEndMarker) { 628 if (clippedMarkerEdges.mAssignedIEnd && 629 nonSnappedContentArea.IEnd(mBlockWM) > clippedMarkerEdges.mIEnd) { 630 mIEnd.mISize = 631 nonSnappedContentArea.IEnd(mBlockWM) - clippedMarkerEdges.mIEnd; 632 NS_ASSERTION(mIEnd.mISize < mIEnd.mIntrinsicISize, 633 "clipping a marker should make it strictly smaller"); 634 clippedIEndMarker = true; 635 } else { 636 mIEnd.mActive = guessIEnd = false; 637 } 638 continue; 639 } 640 // The line simply has no visible content even without markers, 641 // so examine the line again without suppressing markers. 642 retryEmptyLine = false; 643 mIStart.mISize = mIStart.mIntrinsicISize; 644 mIStart.mActive = guessIStart = istartWantsMarker; 645 mIEnd.mISize = mIEnd.mIntrinsicISize; 646 mIEnd.mActive = guessIEnd = iendWantsMarker; 647 // If we wanted to place a block ellipsis but didn't, due to not having 648 // any visible content to align to or the line's content being scrolled 649 // out of view, then clip the ellipsis so that it looks like it is aligned 650 // with the out of view content. 651 if (mIEnd.IsNeeded() && mIEnd.mActive && mIEnd.mHasBlockEllipsis) { 652 NS_ASSERTION(nonSnappedContentArea.IStart(mBlockWM) > 653 aAlignmentEdges->mIEndOuter, 654 "Expected the alignment edge for the out of view content " 655 "to be before the start of the content area"); 656 mIEnd.mISize = std::max( 657 mIEnd.mIntrinsicISize - (nonSnappedContentArea.IStart(mBlockWM) - 658 aAlignmentEdges->mIEndOuter), 659 0); 660 } 661 continue; 662 } 663 if (guessIStart == (mIStart.mActive && mIStart.IsNeeded()) && 664 guessIEnd == (mIEnd.mActive && mIEnd.IsNeeded())) { 665 break; 666 } else { 667 guessIStart = mIStart.mActive && mIStart.IsNeeded(); 668 guessIEnd = mIEnd.mActive && mIEnd.IsNeeded(); 669 mIStart.Reset(); 670 mIEnd.Reset(); 671 aFramesToHide->Clear(); 672 } 673 NS_ASSERTION(pass == 0, "2nd pass should never guess wrong"); 674 } while (++pass != 2); 675 if (!istartWantsMarker || !mIStart.mActive) { 676 mIStart.Reset(); 677 } 678 if (!iendWantsMarker || !mIEnd.mActive) { 679 mIEnd.Reset(); 680 } 681 return nonSnappedContentArea; 682 } 683 684 void TextOverflow::ProcessLine(const nsDisplayListSet& aLists, nsLineBox* aLine, 685 uint32_t aLineNumber) { 686 if (mIStart.mStyle->IsClip() && mIEnd.mStyle->IsClip() && 687 !aLine->HasLineClampEllipsis()) { 688 return; 689 } 690 691 mIStart.Reset(); 692 mIStart.mActive = !mIStart.mStyle->IsClip(); 693 mIEnd.Reset(); 694 mIEnd.mHasBlockEllipsis = aLine->HasLineClampEllipsis(); 695 mIEnd.mActive = !mIEnd.IsSuppressed(mInLineClampContext); 696 697 FrameHashtable framesToHide(64); 698 AlignmentEdges alignmentEdges; 699 const LogicalRect contentArea = 700 ExamineLineFrames(aLine, &framesToHide, &alignmentEdges); 701 bool needIStart = mIStart.IsNeeded(); 702 bool needIEnd = mIEnd.IsNeeded(); 703 if (!needIStart && !needIEnd) { 704 return; 705 } 706 NS_ASSERTION(!mIStart.IsSuppressed(mInLineClampContext) || !needIStart, 707 "left marker when not needed"); 708 NS_ASSERTION(!mIEnd.IsSuppressed(mInLineClampContext) || !needIEnd, 709 "right marker when not needed"); 710 711 // If there is insufficient space for both markers then keep the one on the 712 // end side per the block's 'direction'. 713 if (needIStart && needIEnd && 714 mIStart.mISize + mIEnd.mISize > contentArea.ISize(mBlockWM)) { 715 needIStart = false; 716 } 717 LogicalRect insideMarkersArea = contentArea; 718 if (needIStart) { 719 InflateIStart(mBlockWM, &insideMarkersArea, -mIStart.mISize); 720 } 721 if (needIEnd) { 722 InflateIEnd(mBlockWM, &insideMarkersArea, -mIEnd.mISize); 723 } 724 725 if (alignmentEdges.mAssignedInner) { 726 if (mIStart.mEdgeAligned) { 727 alignmentEdges.mIStart = insideMarkersArea.IStart(mBlockWM); 728 } 729 if (mIEnd.mEdgeAligned) { 730 alignmentEdges.mIEnd = insideMarkersArea.IEnd(mBlockWM); 731 } 732 LogicalRect alignmentRect(mBlockWM, alignmentEdges.mIStart, 733 insideMarkersArea.BStart(mBlockWM), 734 alignmentEdges.ISize(), 1); 735 insideMarkersArea.IntersectRect(insideMarkersArea, alignmentRect); 736 } else { 737 // There was no content on the line that was visible at the current scolled 738 // position. If we wanted to place a block ellipsis but failed due to 739 // having no visible content to align it to, we still need to ensure it 740 // is displayed. It goes at the start of the line, even though it's an 741 // IEnd marker, since that is the side of the line that the content has 742 // been scrolled past. We set the insideMarkersArea to a zero-sized 743 // rectangle placed next to the scrolled-out-of-view content. 744 if (mIEnd.mHasBlockEllipsis) { 745 insideMarkersArea = LogicalRect(mBlockWM, alignmentEdges.mIEndOuter, 746 insideMarkersArea.BStart(mBlockWM), 0, 1); 747 } 748 } 749 750 // Clip and remove display items as needed at the final marker edges. 751 nsDisplayList* lists[] = {aLists.Content(), aLists.PositionedDescendants()}; 752 for (uint32_t i = 0; i < std::size(lists); ++i) { 753 PruneDisplayListContents(lists[i], framesToHide, insideMarkersArea); 754 } 755 CreateMarkers(aLine, needIStart, needIEnd, insideMarkersArea, contentArea, 756 aLineNumber); 757 } 758 759 void TextOverflow::PruneDisplayListContents( 760 nsDisplayList* aList, const FrameHashtable& aFramesToHide, 761 const LogicalRect& aInsideMarkersArea) { 762 for (nsDisplayItem* item : aList->TakeItems()) { 763 nsIFrame* itemFrame = item->Frame(); 764 if (IsFrameDescendantOfAny(itemFrame, aFramesToHide, mBlock)) { 765 item->Destroy(mBuilder); 766 continue; 767 } 768 769 nsDisplayList* wrapper = item->GetSameCoordinateSystemChildren(); 770 if (wrapper) { 771 if (!itemFrame || GetSelfOrNearestBlock(itemFrame) == mBlock) { 772 PruneDisplayListContents(wrapper, aFramesToHide, aInsideMarkersArea); 773 } 774 } 775 776 nsDisplayText* textItem = 777 itemFrame ? nsDisplayText::CheckCast(item) : nullptr; 778 if (textItem && GetSelfOrNearestBlock(itemFrame) == mBlock) { 779 LogicalRect rect = 780 GetLogicalScrollableOverflowRectRelativeToBlock(itemFrame); 781 if (mIStart.IsNeeded()) { 782 nscoord istart = 783 aInsideMarkersArea.IStart(mBlockWM) - rect.IStart(mBlockWM); 784 if (istart > 0) { 785 (mBlockWM.IsBidiLTR() ? textItem->VisIStartEdge() 786 : textItem->VisIEndEdge()) = istart; 787 } 788 } 789 if (mIEnd.IsNeeded()) { 790 nscoord iend = rect.IEnd(mBlockWM) - aInsideMarkersArea.IEnd(mBlockWM); 791 if (iend > 0) { 792 (mBlockWM.IsBidiLTR() ? textItem->VisIEndEdge() 793 : textItem->VisIStartEdge()) = iend; 794 } 795 } 796 } 797 798 aList->AppendToTop(item); 799 } 800 } 801 802 /* static */ 803 bool TextOverflow::HasClippedTextOverflow(nsIFrame* aBlockFrame) { 804 const nsStyleTextReset* style = aBlockFrame->StyleTextReset(); 805 return style->mTextOverflow.first.IsClip() && 806 style->mTextOverflow.second.IsClip(); 807 } 808 809 /* static */ 810 bool TextOverflow::HasBlockEllipsis(nsIFrame* aBlockFrame) { 811 nsBlockFrame* f = do_QueryFrame(aBlockFrame); 812 return f && f->HasLineClampEllipsis(); 813 } 814 815 static bool BlockCanHaveLineClampEllipsis(nsBlockFrame* aBlockFrame, 816 bool aBeforeReflow) { 817 if (aBeforeReflow) { 818 return aBlockFrame->IsInLineClampContext(); 819 } 820 return aBlockFrame->HasLineClampEllipsis(); 821 } 822 823 /* static */ 824 bool TextOverflow::CanHaveOverflowMarkers(nsBlockFrame* aBlockFrame, 825 BeforeReflow aBeforeReflow) { 826 if (BlockCanHaveLineClampEllipsis(aBlockFrame, 827 aBeforeReflow == BeforeReflow::Yes)) { 828 return true; 829 } 830 831 // Nothing to do for text-overflow:clip or if 'overflow-x/y:visible'. 832 if (HasClippedTextOverflow(aBlockFrame) || 833 IsInlineAxisOverflowVisible(aBlockFrame)) { 834 return false; 835 } 836 837 // Skip the combobox anonymous block because it would clip the drop-down 838 // arrow. The inner label inherits 'text-overflow' and does the right thing. 839 if (aBlockFrame->IsComboboxControlFrame()) { 840 return false; 841 } 842 843 // Inhibit the markers if a descendant content owns the caret. 844 RefPtr<nsCaret> caret = aBlockFrame->PresShell()->GetCaret(); 845 if (caret && caret->IsVisible()) { 846 RefPtr<dom::Selection> domSelection = caret->GetSelection(); 847 if (domSelection) { 848 nsCOMPtr<nsIContent> content = 849 nsIContent::FromNodeOrNull(domSelection->GetFocusNode()); 850 if (content && 851 content->IsInclusiveDescendantOf(aBlockFrame->GetContent())) { 852 return false; 853 } 854 } 855 } 856 return true; 857 } 858 859 void TextOverflow::CreateMarkers(const nsLineBox* aLine, bool aCreateIStart, 860 bool aCreateIEnd, 861 const LogicalRect& aInsideMarkersArea, 862 const LogicalRect& aContentArea, 863 uint32_t aLineNumber) { 864 if (!mBlock->IsVisibleForPainting()) { 865 return; 866 } 867 868 if (aCreateIStart) { 869 DisplayListClipState::AutoSaveRestore clipState(mBuilder); 870 871 LogicalRect markerLogicalRect( 872 mBlockWM, aInsideMarkersArea.IStart(mBlockWM) - mIStart.mIntrinsicISize, 873 aLine->BStart(), mIStart.mIntrinsicISize, aLine->BSize()); 874 nsPoint offset = mBuilder->ToReferenceFrame(mBlock); 875 nsRect markerRect = 876 markerLogicalRect.GetPhysicalRect(mBlockWM, mBlockSize) + offset; 877 ClipMarker(aContentArea.GetPhysicalRect(mBlockWM, mBlockSize) + offset, 878 markerRect, clipState); 879 880 mMarkerList.AppendNewToTopWithIndex<nsDisplayTextOverflowMarker>( 881 mBuilder, mBlock, /* aIndex = */ (aLineNumber << 1) + 0, markerRect, 882 aLine->GetLogicalAscent(), *mIStart.mStyle); 883 } 884 885 if (aCreateIEnd) { 886 DisplayListClipState::AutoSaveRestore clipState(mBuilder); 887 888 LogicalRect markerLogicalRect(mBlockWM, aInsideMarkersArea.IEnd(mBlockWM), 889 aLine->BStart(), mIEnd.mIntrinsicISize, 890 aLine->BSize()); 891 nsPoint offset = mBuilder->ToReferenceFrame(mBlock); 892 nsRect markerRect = 893 markerLogicalRect.GetPhysicalRect(mBlockWM, mBlockSize) + offset; 894 ClipMarker(aContentArea.GetPhysicalRect(mBlockWM, mBlockSize) + offset, 895 markerRect, clipState); 896 897 mMarkerList.AppendNewToTopWithIndex<nsDisplayTextOverflowMarker>( 898 mBuilder, mBlock, /* aIndex = */ (aLineNumber << 1) + 1, markerRect, 899 aLine->GetLogicalAscent(), 900 mIEnd.mHasBlockEllipsis ? StyleTextOverflowSide::Ellipsis() 901 : *mIEnd.mStyle); 902 } 903 } 904 905 void TextOverflow::Marker::SetupString(nsIFrame* aFrame) { 906 if (mInitialized) { 907 return; 908 } 909 910 // A limitation here is that at the IEnd of a line, we only ever render one of 911 // a text-overflow marker and a -webkit-line-clamp block ellipsis. Since we 912 // don't track the block ellipsis string and the text-overflow marker string 913 // separately, if both apply to the element, we will always use "…" as the 914 // string for text-overflow. 915 if (HasBlockEllipsis(aFrame) || mStyle->IsEllipsis()) { 916 gfxTextRun* textRun = GetEllipsisTextRun(aFrame); 917 if (textRun) { 918 mISize = textRun->GetAdvanceWidth(); 919 } else { 920 mISize = 0; 921 } 922 } else { 923 UniquePtr<gfxContext> rc = 924 aFrame->PresShell()->CreateReferenceRenderingContext(); 925 RefPtr<nsFontMetrics> fm = 926 nsLayoutUtils::GetInflatedFontMetricsForFrame(aFrame); 927 mISize = nsLayoutUtils::AppUnitWidthOfStringBidi( 928 nsDependentAtomString(mStyle->AsString().AsAtom()), aFrame, *fm, *rc); 929 } 930 mIntrinsicISize = mISize; 931 mInitialized = true; 932 } 933 934 } // namespace mozilla::css