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 /* representation of one line within a block frame, a CSS line box */
8
9 #include "nsLineBox.h"
10
11 #include "mozilla/ArenaObjectID.h"
12 #include "mozilla/Assertions.h"
13 #include "mozilla/Likely.h"
14 #include "mozilla/PresShell.h"
15 #include "mozilla/Sprintf.h"
16 #include "mozilla/WritingModes.h"
17 #include "mozilla/ToString.h"
18 #include "nsBidiPresUtils.h"
19 #include "nsIFrame.h"
20 #include "nsIFrameInlines.h"
21 #include "nsPresArena.h"
22 #include "nsPrintfCString.h"
23 #include "nsWindowSizes.h"
24
25 #ifdef DEBUG
26 static int32_t ctorCount;
GetCtorCount()27 int32_t nsLineBox::GetCtorCount() { return ctorCount; }
28 #endif
29
30 #ifndef _MSC_VER
31 // static nsLineBox constant; initialized in the header file.
32 const uint32_t nsLineBox::kMinChildCountForHashtable;
33 #endif
34
35 using namespace mozilla;
36
nsLineBox(nsIFrame * aFrame,int32_t aCount,bool aIsBlock)37 nsLineBox::nsLineBox(nsIFrame* aFrame, int32_t aCount, bool aIsBlock)
38 : mFirstChild(aFrame),
39 mWritingMode(),
40 mContainerSize(-1, -1),
41 mBounds(WritingMode()), // mBounds will be initialized with the correct
42 // writing mode when it is set
43 mFrames(),
44 mAscent(),
45 mAllFlags(0),
46 mData(nullptr) {
47 // Assert that the union elements chosen for initialisation are at
48 // least as large as all other elements in their respective unions, so
49 // as to ensure that no parts are missed.
50 static_assert(sizeof(mFrames) >= sizeof(mChildCount), "nsLineBox init #1");
51 static_assert(sizeof(mAllFlags) >= sizeof(mFlags), "nsLineBox init #2");
52 static_assert(sizeof(mData) >= sizeof(mBlockData), "nsLineBox init #3");
53 static_assert(sizeof(mData) >= sizeof(mInlineData), "nsLineBox init #4");
54
55 MOZ_COUNT_CTOR(nsLineBox);
56 #ifdef DEBUG
57 ++ctorCount;
58 NS_ASSERTION(!aIsBlock || aCount == 1, "Blocks must have exactly one child");
59 nsIFrame* f = aFrame;
60 for (int32_t n = aCount; n > 0; f = f->GetNextSibling(), --n) {
61 NS_ASSERTION(aIsBlock == f->IsBlockOutside(), "wrong kind of child frame");
62 }
63 #endif
64 static_assert(static_cast<int>(StyleClear::Max) <= 15,
65 "FlagBits needs more bits to store the full range of "
66 "break type ('clear') values");
67 mChildCount = aCount;
68 MarkDirty();
69 mFlags.mBlock = aIsBlock;
70 }
71
~nsLineBox()72 nsLineBox::~nsLineBox() {
73 MOZ_COUNT_DTOR(nsLineBox);
74 if (MOZ_UNLIKELY(mFlags.mHasHashedFrames)) {
75 delete mFrames;
76 }
77 Cleanup();
78 }
79
NS_NewLineBox(PresShell * aPresShell,nsIFrame * aFrame,bool aIsBlock)80 nsLineBox* NS_NewLineBox(PresShell* aPresShell, nsIFrame* aFrame,
81 bool aIsBlock) {
82 return new (aPresShell) nsLineBox(aFrame, 1, aIsBlock);
83 }
84
NS_NewLineBox(PresShell * aPresShell,nsLineBox * aFromLine,nsIFrame * aFrame,int32_t aCount)85 nsLineBox* NS_NewLineBox(PresShell* aPresShell, nsLineBox* aFromLine,
86 nsIFrame* aFrame, int32_t aCount) {
87 nsLineBox* newLine = new (aPresShell) nsLineBox(aFrame, aCount, false);
88 newLine->NoteFramesMovedFrom(aFromLine);
89 newLine->mContainerSize = aFromLine->mContainerSize;
90 return newLine;
91 }
92
AddSizeOfExcludingThis(nsWindowSizes & aSizes) const93 void nsLineBox::AddSizeOfExcludingThis(nsWindowSizes& aSizes) const {
94 if (mFlags.mHasHashedFrames) {
95 aSizes.mLayoutFramePropertiesSize +=
96 mFrames->ShallowSizeOfIncludingThis(aSizes.mState.mMallocSizeOf);
97 }
98 }
99
StealHashTableFrom(nsLineBox * aFromLine,uint32_t aFromLineNewCount)100 void nsLineBox::StealHashTableFrom(nsLineBox* aFromLine,
101 uint32_t aFromLineNewCount) {
102 MOZ_ASSERT(!mFlags.mHasHashedFrames);
103 MOZ_ASSERT(GetChildCount() >= int32_t(aFromLineNewCount));
104 mFrames = aFromLine->mFrames;
105 mFlags.mHasHashedFrames = 1;
106 aFromLine->mFlags.mHasHashedFrames = 0;
107 aFromLine->mChildCount = aFromLineNewCount;
108 // remove aFromLine's frames that aren't on this line
109 nsIFrame* f = aFromLine->mFirstChild;
110 for (uint32_t i = 0; i < aFromLineNewCount; f = f->GetNextSibling(), ++i) {
111 mFrames->Remove(f);
112 }
113 }
114
NoteFramesMovedFrom(nsLineBox * aFromLine)115 void nsLineBox::NoteFramesMovedFrom(nsLineBox* aFromLine) {
116 uint32_t fromCount = aFromLine->GetChildCount();
117 uint32_t toCount = GetChildCount();
118 MOZ_ASSERT(toCount <= fromCount, "moved more frames than aFromLine has");
119 uint32_t fromNewCount = fromCount - toCount;
120 if (MOZ_LIKELY(!aFromLine->mFlags.mHasHashedFrames)) {
121 aFromLine->mChildCount = fromNewCount;
122 MOZ_ASSERT(toCount < kMinChildCountForHashtable);
123 } else if (fromNewCount < kMinChildCountForHashtable) {
124 // aFromLine has a hash table but will not have it after moving the frames
125 // so this line can steal the hash table if it needs it.
126 if (toCount >= kMinChildCountForHashtable) {
127 StealHashTableFrom(aFromLine, fromNewCount);
128 } else {
129 delete aFromLine->mFrames;
130 aFromLine->mFlags.mHasHashedFrames = 0;
131 aFromLine->mChildCount = fromNewCount;
132 }
133 } else {
134 // aFromLine still needs a hash table.
135 if (toCount < kMinChildCountForHashtable) {
136 // remove the moved frames from it
137 nsIFrame* f = mFirstChild;
138 for (uint32_t i = 0; i < toCount; f = f->GetNextSibling(), ++i) {
139 aFromLine->mFrames->Remove(f);
140 }
141 } else if (toCount <= fromNewCount) {
142 // This line needs a hash table, allocate a hash table for it since that
143 // means fewer hash ops.
144 nsIFrame* f = mFirstChild;
145 for (uint32_t i = 0; i < toCount; f = f->GetNextSibling(), ++i) {
146 aFromLine->mFrames->Remove(f); // toCount RemoveEntry
147 }
148 SwitchToHashtable(); // toCount PutEntry
149 } else {
150 // This line needs a hash table, but it's fewer hash ops to steal
151 // aFromLine's hash table and allocate a new hash table for that line.
152 StealHashTableFrom(aFromLine, fromNewCount); // fromNewCount RemoveEntry
153 aFromLine->SwitchToHashtable(); // fromNewCount PutEntry
154 }
155 }
156 }
157
operator new(size_t sz,PresShell * aPresShell)158 void* nsLineBox::operator new(size_t sz, PresShell* aPresShell) {
159 return aPresShell->AllocateByObjectID(eArenaObjectID_nsLineBox, sz);
160 }
161
Destroy(PresShell * aPresShell)162 void nsLineBox::Destroy(PresShell* aPresShell) {
163 this->nsLineBox::~nsLineBox();
164 aPresShell->FreeByObjectID(eArenaObjectID_nsLineBox, this);
165 }
166
Cleanup()167 void nsLineBox::Cleanup() {
168 if (mData) {
169 if (IsBlock()) {
170 delete mBlockData;
171 } else {
172 delete mInlineData;
173 }
174 mData = nullptr;
175 }
176 }
177
178 #ifdef DEBUG_FRAME_DUMP
ListFloats(FILE * out,const char * aPrefix,const nsFloatCacheList & aFloats)179 static void ListFloats(FILE* out, const char* aPrefix,
180 const nsFloatCacheList& aFloats) {
181 nsFloatCache* fc = aFloats.Head();
182 while (fc) {
183 nsCString str(aPrefix);
184 nsIFrame* frame = fc->mFloat;
185 str += nsPrintfCString("floatframe@%p ", static_cast<void*>(frame));
186 if (frame) {
187 nsAutoString frameName;
188 frame->GetFrameName(frameName);
189 str += NS_ConvertUTF16toUTF8(frameName).get();
190 } else {
191 str += "\n###!!! NULL out-of-flow frame";
192 }
193 fprintf_stderr(out, "%s\n", str.get());
194 fc = fc->Next();
195 }
196 }
197
BreakTypeToString(StyleClear aBreakType)198 /* static */ const char* nsLineBox::BreakTypeToString(StyleClear aBreakType) {
199 switch (aBreakType) {
200 case StyleClear::None:
201 return "nobr";
202 case StyleClear::Left:
203 return "leftbr";
204 case StyleClear::Right:
205 return "rightbr";
206 case StyleClear::Both:
207 return "leftbr+rightbr";
208 case StyleClear::Line:
209 return "linebr";
210 case StyleClear::Max:
211 return "leftbr+rightbr+linebr";
212 }
213 return "unknown";
214 }
215
StateToString(char * aBuf,int32_t aBufSize) const216 char* nsLineBox::StateToString(char* aBuf, int32_t aBufSize) const {
217 snprintf(aBuf, aBufSize, "%s,%s,%s,%s,%s,before:%s,after:%s[0x%x]",
218 IsBlock() ? "block" : "inline", IsDirty() ? "dirty" : "clean",
219 IsPreviousMarginDirty() ? "prevmargindirty" : "prevmarginclean",
220 IsImpactedByFloat() ? "impacted" : "not-impacted",
221 IsLineWrapped() ? "wrapped" : "not-wrapped",
222 BreakTypeToString(GetBreakTypeBefore()),
223 BreakTypeToString(GetBreakTypeAfter()), mAllFlags);
224 return aBuf;
225 }
226
List(FILE * out,int32_t aIndent,nsIFrame::ListFlags aFlags) const227 void nsLineBox::List(FILE* out, int32_t aIndent,
228 nsIFrame::ListFlags aFlags) const {
229 nsCString str;
230 while (aIndent-- > 0) {
231 str += " ";
232 }
233 List(out, str.get(), aFlags);
234 }
235
List(FILE * out,const char * aPrefix,nsIFrame::ListFlags aFlags) const236 void nsLineBox::List(FILE* out, const char* aPrefix,
237 nsIFrame::ListFlags aFlags) const {
238 nsCString str(aPrefix);
239 char cbuf[100];
240 str += nsPrintfCString("line@%p count=%d state=%s ",
241 static_cast<const void*>(this), GetChildCount(),
242 StateToString(cbuf, sizeof(cbuf)));
243 if (IsBlock() && !GetCarriedOutBEndMargin().IsZero()) {
244 const nscoord bm = GetCarriedOutBEndMargin().get();
245 str += nsPrintfCString("bm=%s ",
246 nsIFrame::ConvertToString(bm, aFlags).c_str());
247 }
248 nsRect bounds = GetPhysicalBounds();
249 str +=
250 nsPrintfCString("%s ", nsIFrame::ConvertToString(bounds, aFlags).c_str());
251 if (mWritingMode.IsVertical() || mWritingMode.IsBidiRTL()) {
252 str += nsPrintfCString(
253 "wm=%s cs=(%s) logical-rect=%s ", ToString(mWritingMode).c_str(),
254 nsIFrame::ConvertToString(mContainerSize, aFlags).c_str(),
255 nsIFrame::ConvertToString(mBounds, mWritingMode, aFlags).c_str());
256 }
257 if (mData) {
258 const nsRect vo = mData->mOverflowAreas.InkOverflow();
259 const nsRect so = mData->mOverflowAreas.ScrollableOverflow();
260 if (!vo.IsEqualEdges(bounds) || !so.IsEqualEdges(bounds)) {
261 str += nsPrintfCString("ink-overflow=%s scr-overflow=%s ",
262 nsIFrame::ConvertToString(vo, aFlags).c_str(),
263 nsIFrame::ConvertToString(so, aFlags).c_str());
264 }
265 }
266 fprintf_stderr(out, "%s<\n", str.get());
267
268 nsIFrame* frame = mFirstChild;
269 int32_t n = GetChildCount();
270 nsCString pfx(aPrefix);
271 pfx += " ";
272 while (--n >= 0) {
273 frame->List(out, pfx.get(), aFlags);
274 frame = frame->GetNextSibling();
275 }
276
277 if (HasFloats()) {
278 fprintf_stderr(out, "%s> floats <\n", aPrefix);
279 ListFloats(out, pfx.get(), mInlineData->mFloats);
280 }
281 fprintf_stderr(out, "%s>\n", aPrefix);
282 }
283
LastChild() const284 nsIFrame* nsLineBox::LastChild() const {
285 nsIFrame* frame = mFirstChild;
286 int32_t n = GetChildCount() - 1;
287 while (--n >= 0) {
288 frame = frame->GetNextSibling();
289 }
290 return frame;
291 }
292 #endif
293
IndexOf(nsIFrame * aFrame) const294 int32_t nsLineBox::IndexOf(nsIFrame* aFrame) const {
295 int32_t i, n = GetChildCount();
296 nsIFrame* frame = mFirstChild;
297 for (i = 0; i < n; i++) {
298 if (frame == aFrame) {
299 return i;
300 }
301 frame = frame->GetNextSibling();
302 }
303 return -1;
304 }
305
RIndexOf(nsIFrame * aFrame,nsIFrame * aLastFrameInLine) const306 int32_t nsLineBox::RIndexOf(nsIFrame* aFrame,
307 nsIFrame* aLastFrameInLine) const {
308 nsIFrame* frame = aLastFrameInLine;
309 for (int32_t i = GetChildCount() - 1; i >= 0; --i) {
310 MOZ_ASSERT(i != 0 || frame == mFirstChild,
311 "caller provided incorrect last frame");
312 if (frame == aFrame) {
313 return i;
314 }
315 frame = frame->GetPrevSibling();
316 }
317 return -1;
318 }
319
IsEmpty() const320 bool nsLineBox::IsEmpty() const {
321 if (IsBlock()) return mFirstChild->IsEmpty();
322
323 int32_t n;
324 nsIFrame* kid;
325 for (n = GetChildCount(), kid = mFirstChild; n > 0;
326 --n, kid = kid->GetNextSibling()) {
327 if (!kid->IsEmpty()) return false;
328 }
329 if (HasMarker()) {
330 return false;
331 }
332 return true;
333 }
334
CachedIsEmpty()335 bool nsLineBox::CachedIsEmpty() {
336 if (mFlags.mDirty) {
337 return IsEmpty();
338 }
339
340 if (mFlags.mEmptyCacheValid) {
341 return mFlags.mEmptyCacheState;
342 }
343
344 bool result;
345 if (IsBlock()) {
346 result = mFirstChild->CachedIsEmpty();
347 } else {
348 int32_t n;
349 nsIFrame* kid;
350 result = true;
351 for (n = GetChildCount(), kid = mFirstChild; n > 0;
352 --n, kid = kid->GetNextSibling()) {
353 if (!kid->CachedIsEmpty()) {
354 result = false;
355 break;
356 }
357 }
358 if (HasMarker()) {
359 result = false;
360 }
361 }
362
363 mFlags.mEmptyCacheValid = true;
364 mFlags.mEmptyCacheState = result;
365 return result;
366 }
367
DeleteLineList(nsPresContext * aPresContext,nsLineList & aLines,nsIFrame * aDestructRoot,nsFrameList * aFrames,PostDestroyData & aPostDestroyData)368 void nsLineBox::DeleteLineList(nsPresContext* aPresContext, nsLineList& aLines,
369 nsIFrame* aDestructRoot, nsFrameList* aFrames,
370 PostDestroyData& aPostDestroyData) {
371 PresShell* presShell = aPresContext->PresShell();
372
373 // Keep our line list and frame list up to date as we
374 // remove frames, in case something wants to traverse the
375 // frame tree while we're destroying.
376 while (!aLines.empty()) {
377 nsLineBox* line = aLines.front();
378 if (MOZ_UNLIKELY(line->mFlags.mHasHashedFrames)) {
379 line->SwitchToCounter(); // Avoid expensive has table removals.
380 }
381 while (line->GetChildCount() > 0) {
382 nsIFrame* child = aFrames->RemoveFirstChild();
383 MOZ_DIAGNOSTIC_ASSERT(child->PresContext() == aPresContext);
384 MOZ_DIAGNOSTIC_ASSERT(child == line->mFirstChild, "Lines out of sync");
385 line->mFirstChild = aFrames->FirstChild();
386 line->NoteFrameRemoved(child);
387 child->DestroyFrom(aDestructRoot, aPostDestroyData);
388 }
389 MOZ_DIAGNOSTIC_ASSERT(line == aLines.front(),
390 "destroying child frames messed up our lines!");
391 aLines.pop_front();
392 line->Destroy(presShell);
393 }
394 }
395
RFindLineContaining(nsIFrame * aFrame,const nsLineList::iterator & aBegin,nsLineList::iterator & aEnd,nsIFrame * aLastFrameBeforeEnd,int32_t * aFrameIndexInLine)396 bool nsLineBox::RFindLineContaining(nsIFrame* aFrame,
397 const nsLineList::iterator& aBegin,
398 nsLineList::iterator& aEnd,
399 nsIFrame* aLastFrameBeforeEnd,
400 int32_t* aFrameIndexInLine) {
401 MOZ_ASSERT(aFrame, "null ptr");
402
403 nsIFrame* curFrame = aLastFrameBeforeEnd;
404 while (aBegin != aEnd) {
405 --aEnd;
406 NS_ASSERTION(aEnd->LastChild() == curFrame, "Unexpected curFrame");
407 if (MOZ_UNLIKELY(aEnd->mFlags.mHasHashedFrames) &&
408 !aEnd->Contains(aFrame)) {
409 if (aEnd->mFirstChild) {
410 curFrame = aEnd->mFirstChild->GetPrevSibling();
411 }
412 continue;
413 }
414 // i is the index of curFrame in aEnd
415 int32_t i = aEnd->GetChildCount() - 1;
416 while (i >= 0) {
417 if (curFrame == aFrame) {
418 *aFrameIndexInLine = i;
419 return true;
420 }
421 --i;
422 curFrame = curFrame->GetPrevSibling();
423 }
424 MOZ_ASSERT(!aEnd->mFlags.mHasHashedFrames, "Contains lied to us!");
425 }
426 *aFrameIndexInLine = -1;
427 return false;
428 }
429
GetCarriedOutBEndMargin() const430 nsCollapsingMargin nsLineBox::GetCarriedOutBEndMargin() const {
431 NS_ASSERTION(IsBlock(), "GetCarriedOutBEndMargin called on non-block line.");
432 return (IsBlock() && mBlockData) ? mBlockData->mCarriedOutBEndMargin
433 : nsCollapsingMargin();
434 }
435
SetCarriedOutBEndMargin(nsCollapsingMargin aValue)436 bool nsLineBox::SetCarriedOutBEndMargin(nsCollapsingMargin aValue) {
437 bool changed = false;
438 if (IsBlock()) {
439 if (!aValue.IsZero()) {
440 if (!mBlockData) {
441 mBlockData = new ExtraBlockData(GetPhysicalBounds());
442 }
443 changed = aValue != mBlockData->mCarriedOutBEndMargin;
444 mBlockData->mCarriedOutBEndMargin = aValue;
445 } else if (mBlockData) {
446 changed = aValue != mBlockData->mCarriedOutBEndMargin;
447 mBlockData->mCarriedOutBEndMargin = aValue;
448 MaybeFreeData();
449 }
450 }
451 return changed;
452 }
453
MaybeFreeData()454 void nsLineBox::MaybeFreeData() {
455 nsRect bounds = GetPhysicalBounds();
456 if (mData && mData->mOverflowAreas == OverflowAreas(bounds, bounds)) {
457 if (IsInline()) {
458 if (mInlineData->mFloats.IsEmpty()) {
459 delete mInlineData;
460 mInlineData = nullptr;
461 }
462 } else if (mBlockData->mCarriedOutBEndMargin.IsZero()) {
463 delete mBlockData;
464 mBlockData = nullptr;
465 }
466 }
467 }
468
469 // XXX get rid of this???
GetFirstFloat()470 nsFloatCache* nsLineBox::GetFirstFloat() {
471 MOZ_ASSERT(IsInline(), "block line can't have floats");
472 return mInlineData ? mInlineData->mFloats.Head() : nullptr;
473 }
474
475 // XXX this might be too eager to free memory
FreeFloats(nsFloatCacheFreeList & aFreeList)476 void nsLineBox::FreeFloats(nsFloatCacheFreeList& aFreeList) {
477 MOZ_ASSERT(IsInline(), "block line can't have floats");
478 if (IsInline() && mInlineData) {
479 if (mInlineData->mFloats.NotEmpty()) {
480 aFreeList.Append(mInlineData->mFloats);
481 }
482 MaybeFreeData();
483 }
484 }
485
AppendFloats(nsFloatCacheFreeList & aFreeList)486 void nsLineBox::AppendFloats(nsFloatCacheFreeList& aFreeList) {
487 MOZ_ASSERT(IsInline(), "block line can't have floats");
488 if (IsInline()) {
489 if (aFreeList.NotEmpty()) {
490 if (!mInlineData) {
491 mInlineData = new ExtraInlineData(GetPhysicalBounds());
492 }
493 mInlineData->mFloats.Append(aFreeList);
494 }
495 }
496 }
497
RemoveFloat(nsIFrame * aFrame)498 bool nsLineBox::RemoveFloat(nsIFrame* aFrame) {
499 MOZ_ASSERT(IsInline(), "block line can't have floats");
500 if (IsInline() && mInlineData) {
501 nsFloatCache* fc = mInlineData->mFloats.Find(aFrame);
502 if (fc) {
503 // Note: the placeholder is part of the line's child list
504 // and will be removed later.
505 mInlineData->mFloats.Remove(fc);
506 delete fc;
507 MaybeFreeData();
508 return true;
509 }
510 }
511 return false;
512 }
513
SetFloatEdges(nscoord aStart,nscoord aEnd)514 void nsLineBox::SetFloatEdges(nscoord aStart, nscoord aEnd) {
515 MOZ_ASSERT(IsInline(), "block line can't have float edges");
516 if (!mInlineData) {
517 mInlineData = new ExtraInlineData(GetPhysicalBounds());
518 }
519 mInlineData->mFloatEdgeIStart = aStart;
520 mInlineData->mFloatEdgeIEnd = aEnd;
521 }
522
ClearFloatEdges()523 void nsLineBox::ClearFloatEdges() {
524 MOZ_ASSERT(IsInline(), "block line can't have float edges");
525 if (mInlineData) {
526 mInlineData->mFloatEdgeIStart = nscoord_MIN;
527 mInlineData->mFloatEdgeIEnd = nscoord_MIN;
528 }
529 }
530
SetOverflowAreas(const OverflowAreas & aOverflowAreas)531 void nsLineBox::SetOverflowAreas(const OverflowAreas& aOverflowAreas) {
532 #ifdef DEBUG
533 for (const auto otype : mozilla::AllOverflowTypes()) {
534 NS_ASSERTION(aOverflowAreas.Overflow(otype).width >= 0,
535 "Illegal width for an overflow area!");
536 NS_ASSERTION(aOverflowAreas.Overflow(otype).height >= 0,
537 "Illegal height for an overflow area!");
538 }
539 #endif
540
541 nsRect bounds = GetPhysicalBounds();
542 if (!aOverflowAreas.InkOverflow().IsEqualInterior(bounds) ||
543 !aOverflowAreas.ScrollableOverflow().IsEqualEdges(bounds)) {
544 if (!mData) {
545 if (IsInline()) {
546 mInlineData = new ExtraInlineData(bounds);
547 } else {
548 mBlockData = new ExtraBlockData(bounds);
549 }
550 }
551 mData->mOverflowAreas = aOverflowAreas;
552 } else if (mData) {
553 // Store away new value so that MaybeFreeData compares against
554 // the right value.
555 mData->mOverflowAreas = aOverflowAreas;
556 MaybeFreeData();
557 }
558 }
559
560 //----------------------------------------------------------------------
561
562 static nsLineBox* gDummyLines[1];
563
nsLineIterator()564 nsLineIterator::nsLineIterator() {
565 mLines = gDummyLines;
566 mNumLines = 0;
567 mIndex = 0;
568 mRightToLeft = false;
569 }
570
~nsLineIterator()571 nsLineIterator::~nsLineIterator() {
572 if (mLines != gDummyLines) {
573 delete[] mLines;
574 }
575 }
576
577 /* virtual */
DisposeLineIterator()578 void nsLineIterator::DisposeLineIterator() { delete this; }
579
Init(nsLineList & aLines,bool aRightToLeft)580 nsresult nsLineIterator::Init(nsLineList& aLines, bool aRightToLeft) {
581 mRightToLeft = aRightToLeft;
582
583 // Count the lines
584 int32_t numLines = aLines.size();
585 if (0 == numLines) {
586 // Use gDummyLines so that we don't need null pointer checks in
587 // the accessor methods
588 mLines = gDummyLines;
589 return NS_OK;
590 }
591
592 // Make a linear array of the lines
593 mLines = new nsLineBox*[numLines];
594 if (!mLines) {
595 // Use gDummyLines so that we don't need null pointer checks in
596 // the accessor methods
597 mLines = gDummyLines;
598 return NS_ERROR_OUT_OF_MEMORY;
599 }
600 nsLineBox** lp = mLines;
601 for (nsLineList::iterator line = aLines.begin(), line_end = aLines.end();
602 line != line_end; ++line) {
603 *lp++ = line;
604 }
605 mNumLines = numLines;
606 return NS_OK;
607 }
608
GetNumLines() const609 int32_t nsLineIterator::GetNumLines() const { return mNumLines; }
610
GetDirection()611 bool nsLineIterator::GetDirection() { return mRightToLeft; }
612
GetLine(int32_t aLineNumber) const613 Result<nsILineIterator::LineInfo, nsresult> nsLineIterator::GetLine(
614 int32_t aLineNumber) const {
615 if ((aLineNumber < 0) || (aLineNumber >= mNumLines)) {
616 return Err(NS_ERROR_FAILURE);
617 }
618 LineInfo structure;
619 nsLineBox* line = mLines[aLineNumber];
620 structure.mFirstFrameOnLine = line->mFirstChild;
621 structure.mNumFramesOnLine = line->GetChildCount();
622 structure.mLineBounds = line->GetPhysicalBounds();
623 structure.mIsWrapped = line->IsLineWrapped();
624 return structure;
625 }
626
FindLineContaining(nsIFrame * aFrame,int32_t aStartLine)627 int32_t nsLineIterator::FindLineContaining(nsIFrame* aFrame,
628 int32_t aStartLine) {
629 MOZ_ASSERT(aStartLine <= mNumLines, "Bogus line numbers");
630 int32_t lineNumber = aStartLine;
631 while (lineNumber != mNumLines) {
632 nsLineBox* line = mLines[lineNumber];
633 if (line->Contains(aFrame)) {
634 return lineNumber;
635 }
636 ++lineNumber;
637 }
638 return -1;
639 }
640
641 NS_IMETHODIMP
CheckLineOrder(int32_t aLine,bool * aIsReordered,nsIFrame ** aFirstVisual,nsIFrame ** aLastVisual)642 nsLineIterator::CheckLineOrder(int32_t aLine, bool* aIsReordered,
643 nsIFrame** aFirstVisual,
644 nsIFrame** aLastVisual) {
645 NS_ASSERTION(aLine >= 0 && aLine < mNumLines, "aLine out of range!");
646 nsLineBox* line = mLines[aLine];
647
648 if (!line->mFirstChild) { // empty line
649 *aIsReordered = false;
650 *aFirstVisual = nullptr;
651 *aLastVisual = nullptr;
652 return NS_OK;
653 }
654
655 nsIFrame* leftmostFrame;
656 nsIFrame* rightmostFrame;
657 *aIsReordered =
658 nsBidiPresUtils::CheckLineOrder(line->mFirstChild, line->GetChildCount(),
659 &leftmostFrame, &rightmostFrame);
660
661 // map leftmost/rightmost to first/last according to paragraph direction
662 *aFirstVisual = mRightToLeft ? rightmostFrame : leftmostFrame;
663 *aLastVisual = mRightToLeft ? leftmostFrame : rightmostFrame;
664
665 return NS_OK;
666 }
667
668 NS_IMETHODIMP
FindFrameAt(int32_t aLineNumber,nsPoint aPos,nsIFrame ** aFrameFound,bool * aPosIsBeforeFirstFrame,bool * aPosIsAfterLastFrame) const669 nsLineIterator::FindFrameAt(int32_t aLineNumber, nsPoint aPos,
670 nsIFrame** aFrameFound,
671 bool* aPosIsBeforeFirstFrame,
672 bool* aPosIsAfterLastFrame) const {
673 MOZ_ASSERT(aFrameFound && aPosIsBeforeFirstFrame && aPosIsAfterLastFrame,
674 "null OUT ptr");
675
676 if (!aFrameFound || !aPosIsBeforeFirstFrame || !aPosIsAfterLastFrame) {
677 return NS_ERROR_NULL_POINTER;
678 }
679 if ((aLineNumber < 0) || (aLineNumber >= mNumLines)) {
680 return NS_ERROR_INVALID_ARG;
681 }
682
683 nsLineBox* line = mLines[aLineNumber];
684 if (!line) {
685 *aFrameFound = nullptr;
686 *aPosIsBeforeFirstFrame = true;
687 *aPosIsAfterLastFrame = false;
688 return NS_OK;
689 }
690
691 if (line->ISize() == 0 && line->BSize() == 0) return NS_ERROR_FAILURE;
692
693 nsIFrame* frame = line->mFirstChild;
694 nsIFrame* closestFromStart = nullptr;
695 nsIFrame* closestFromEnd = nullptr;
696
697 WritingMode wm = line->mWritingMode;
698 nsSize containerSize = line->mContainerSize;
699
700 LogicalPoint pos(wm, aPos, containerSize);
701
702 int32_t n = line->GetChildCount();
703 while (n--) {
704 LogicalRect rect = frame->GetLogicalRect(wm, containerSize);
705 if (rect.ISize(wm) > 0) {
706 // If pos.I() is inside this frame - this is it
707 if (rect.IStart(wm) <= pos.I(wm) && rect.IEnd(wm) > pos.I(wm)) {
708 closestFromStart = closestFromEnd = frame;
709 break;
710 }
711 if (rect.IStart(wm) < pos.I(wm)) {
712 if (!closestFromStart ||
713 rect.IEnd(wm) >
714 closestFromStart->GetLogicalRect(wm, containerSize).IEnd(wm))
715 closestFromStart = frame;
716 } else {
717 if (!closestFromEnd ||
718 rect.IStart(wm) <
719 closestFromEnd->GetLogicalRect(wm, containerSize).IStart(wm))
720 closestFromEnd = frame;
721 }
722 }
723 frame = frame->GetNextSibling();
724 }
725 if (!closestFromStart && !closestFromEnd) {
726 // All frames were zero-width. Just take the first one.
727 closestFromStart = closestFromEnd = line->mFirstChild;
728 }
729 *aPosIsBeforeFirstFrame = mRightToLeft ? !closestFromEnd : !closestFromStart;
730 *aPosIsAfterLastFrame = mRightToLeft ? !closestFromStart : !closestFromEnd;
731 if (closestFromStart == closestFromEnd) {
732 *aFrameFound = closestFromStart;
733 } else if (!closestFromStart) {
734 *aFrameFound = closestFromEnd;
735 } else if (!closestFromEnd) {
736 *aFrameFound = closestFromStart;
737 } else { // we're between two frames
738 nscoord delta =
739 closestFromEnd->GetLogicalRect(wm, containerSize).IStart(wm) -
740 closestFromStart->GetLogicalRect(wm, containerSize).IEnd(wm);
741 if (pos.I(wm) <
742 closestFromStart->GetLogicalRect(wm, containerSize).IEnd(wm) +
743 delta / 2) {
744 *aFrameFound = closestFromStart;
745 } else {
746 *aFrameFound = closestFromEnd;
747 }
748 }
749 return NS_OK;
750 }
751
752 NS_IMETHODIMP
GetNextSiblingOnLine(nsIFrame * & aFrame,int32_t aLineNumber) const753 nsLineIterator::GetNextSiblingOnLine(nsIFrame*& aFrame,
754 int32_t aLineNumber) const {
755 aFrame = aFrame->GetNextSibling();
756 return NS_OK;
757 }
758
759 //----------------------------------------------------------------------
760
761 #ifdef NS_BUILD_REFCNT_LOGGING
nsFloatCacheList()762 nsFloatCacheList::nsFloatCacheList() : mHead(nullptr) {
763 MOZ_COUNT_CTOR(nsFloatCacheList);
764 }
765 #endif
766
~nsFloatCacheList()767 nsFloatCacheList::~nsFloatCacheList() {
768 DeleteAll();
769 MOZ_COUNT_DTOR(nsFloatCacheList);
770 }
771
DeleteAll()772 void nsFloatCacheList::DeleteAll() {
773 nsFloatCache* c = mHead;
774 while (c) {
775 nsFloatCache* next = c->Next();
776 delete c;
777 c = next;
778 }
779 mHead = nullptr;
780 }
781
Tail() const782 nsFloatCache* nsFloatCacheList::Tail() const {
783 nsFloatCache* fc = mHead;
784 while (fc) {
785 if (!fc->mNext) {
786 break;
787 }
788 fc = fc->mNext;
789 }
790 return fc;
791 }
792
Append(nsFloatCacheFreeList & aList)793 void nsFloatCacheList::Append(nsFloatCacheFreeList& aList) {
794 MOZ_ASSERT(aList.NotEmpty(), "Appending empty list will fail");
795
796 nsFloatCache* tail = Tail();
797 if (tail) {
798 NS_ASSERTION(!tail->mNext, "Bogus!");
799 tail->mNext = aList.mHead;
800 } else {
801 NS_ASSERTION(!mHead, "Bogus!");
802 mHead = aList.mHead;
803 }
804 aList.mHead = nullptr;
805 aList.mTail = nullptr;
806 }
807
Find(nsIFrame * aOutOfFlowFrame)808 nsFloatCache* nsFloatCacheList::Find(nsIFrame* aOutOfFlowFrame) {
809 nsFloatCache* fc = mHead;
810 while (fc) {
811 if (fc->mFloat == aOutOfFlowFrame) {
812 break;
813 }
814 fc = fc->Next();
815 }
816 return fc;
817 }
818
RemoveAndReturnPrev(nsFloatCache * aElement)819 nsFloatCache* nsFloatCacheList::RemoveAndReturnPrev(nsFloatCache* aElement) {
820 nsFloatCache* fc = mHead;
821 nsFloatCache* prev = nullptr;
822 while (fc) {
823 if (fc == aElement) {
824 if (prev) {
825 prev->mNext = fc->mNext;
826 } else {
827 mHead = fc->mNext;
828 }
829 return prev;
830 }
831 prev = fc;
832 fc = fc->mNext;
833 }
834 return nullptr;
835 }
836
837 //----------------------------------------------------------------------
838
839 #ifdef NS_BUILD_REFCNT_LOGGING
nsFloatCacheFreeList()840 nsFloatCacheFreeList::nsFloatCacheFreeList() : mTail(nullptr) {
841 MOZ_COUNT_CTOR(nsFloatCacheFreeList);
842 }
843
~nsFloatCacheFreeList()844 nsFloatCacheFreeList::~nsFloatCacheFreeList() {
845 MOZ_COUNT_DTOR(nsFloatCacheFreeList);
846 }
847 #endif
848
Append(nsFloatCacheList & aList)849 void nsFloatCacheFreeList::Append(nsFloatCacheList& aList) {
850 MOZ_ASSERT(aList.NotEmpty(), "Appending empty list will fail");
851
852 if (mTail) {
853 NS_ASSERTION(!mTail->mNext, "Bogus");
854 mTail->mNext = aList.mHead;
855 } else {
856 NS_ASSERTION(!mHead, "Bogus");
857 mHead = aList.mHead;
858 }
859 mTail = aList.Tail();
860 aList.mHead = nullptr;
861 }
862
Remove(nsFloatCache * aElement)863 void nsFloatCacheFreeList::Remove(nsFloatCache* aElement) {
864 nsFloatCache* prev = nsFloatCacheList::RemoveAndReturnPrev(aElement);
865 if (mTail == aElement) {
866 mTail = prev;
867 }
868 }
869
DeleteAll()870 void nsFloatCacheFreeList::DeleteAll() {
871 nsFloatCacheList::DeleteAll();
872 mTail = nullptr;
873 }
874
Alloc(nsIFrame * aFloat)875 nsFloatCache* nsFloatCacheFreeList::Alloc(nsIFrame* aFloat) {
876 MOZ_ASSERT(aFloat->HasAnyStateBits(NS_FRAME_OUT_OF_FLOW),
877 "This is a float cache, why isn't the frame out-of-flow?");
878
879 nsFloatCache* fc = mHead;
880 if (mHead) {
881 if (mHead == mTail) {
882 mHead = mTail = nullptr;
883 } else {
884 mHead = fc->mNext;
885 }
886 fc->mNext = nullptr;
887 } else {
888 fc = new nsFloatCache();
889 }
890 fc->mFloat = aFloat;
891 return fc;
892 }
893
Append(nsFloatCache * aFloat)894 void nsFloatCacheFreeList::Append(nsFloatCache* aFloat) {
895 NS_ASSERTION(!aFloat->mNext, "Bogus!");
896 aFloat->mNext = nullptr;
897 if (mTail) {
898 NS_ASSERTION(!mTail->mNext, "Bogus!");
899 mTail->mNext = aFloat;
900 mTail = aFloat;
901 } else {
902 NS_ASSERTION(!mHead, "Bogus!");
903 mHead = mTail = aFloat;
904 }
905 }
906
907 //----------------------------------------------------------------------
908
nsFloatCache()909 nsFloatCache::nsFloatCache() : mFloat(nullptr), mNext(nullptr) {
910 MOZ_COUNT_CTOR(nsFloatCache);
911 }
912
913 #ifdef NS_BUILD_REFCNT_LOGGING
~nsFloatCache()914 nsFloatCache::~nsFloatCache() { MOZ_COUNT_DTOR(nsFloatCache); }
915 #endif
916