Coverage Report

Created: 2018-09-25 14:53

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