This repository was archived by the owner on Jul 9, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathnsFloatManager.cpp
More file actions
1238 lines (1084 loc) · 41.9 KB
/
Copy pathnsFloatManager.cpp
File metadata and controls
1238 lines (1084 loc) · 41.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/* class that manages rules for positioning floats */
#include "nsFloatManager.h"
#include <algorithm>
#include <initializer_list>
#include "mozilla/ReflowInput.h"
#include "mozilla/ShapeUtils.h"
#include "nsBlockFrame.h"
#include "nsError.h"
#include "nsIPresShell.h"
#include "nsMemory.h"
using namespace mozilla;
int32_t nsFloatManager::sCachedFloatManagerCount = 0;
void* nsFloatManager::sCachedFloatManagers[NS_FLOAT_MANAGER_CACHE_SIZE];
/////////////////////////////////////////////////////////////////////////////
// nsFloatManager
nsFloatManager::nsFloatManager(nsIPresShell* aPresShell,
mozilla::WritingMode aWM)
:
#ifdef DEBUG
mWritingMode(aWM),
#endif
mLineLeft(0), mBlockStart(0),
mFloatDamage(aPresShell),
mPushedLeftFloatPastBreak(false),
mPushedRightFloatPastBreak(false),
mSplitLeftFloatAcrossBreak(false),
mSplitRightFloatAcrossBreak(false)
{
MOZ_COUNT_CTOR(nsFloatManager);
}
nsFloatManager::~nsFloatManager()
{
MOZ_COUNT_DTOR(nsFloatManager);
}
// static
void* nsFloatManager::operator new(size_t aSize) CPP_THROW_NEW
{
if (sCachedFloatManagerCount > 0) {
// We have cached unused instances of this class, return a cached
// instance in stead of always creating a new one.
return sCachedFloatManagers[--sCachedFloatManagerCount];
}
// The cache is empty, this means we have to create a new instance using
// the global |operator new|.
return moz_xmalloc(aSize);
}
void
nsFloatManager::operator delete(void* aPtr, size_t aSize)
{
if (!aPtr)
return;
// This float manager is no longer used, if there's still room in
// the cache we'll cache this float manager, unless the layout
// module was already shut down.
if (sCachedFloatManagerCount < NS_FLOAT_MANAGER_CACHE_SIZE &&
sCachedFloatManagerCount >= 0) {
// There's still space in the cache for more instances, put this
// instance in the cache in stead of deleting it.
sCachedFloatManagers[sCachedFloatManagerCount++] = aPtr;
return;
}
// The cache is full, or the layout module has been shut down,
// delete this float manager.
free(aPtr);
}
/* static */
void nsFloatManager::Shutdown()
{
// The layout module is being shut down, clean up the cache and
// disable further caching.
int32_t i;
for (i = 0; i < sCachedFloatManagerCount; i++) {
void* floatManager = sCachedFloatManagers[i];
if (floatManager)
free(floatManager);
}
// Disable further caching.
sCachedFloatManagerCount = -1;
}
#define CHECK_BLOCK_AND_LINE_DIR(aWM) \
NS_ASSERTION((aWM).GetBlockDir() == mWritingMode.GetBlockDir() && \
(aWM).IsLineInverted() == mWritingMode.IsLineInverted(), \
"incompatible writing modes")
nsFlowAreaRect
nsFloatManager::GetFlowArea(WritingMode aWM, nscoord aBCoord, nscoord aBSize,
BandInfoType aBandInfoType, ShapeType aShapeType,
LogicalRect aContentArea, SavedState* aState,
const nsSize& aContainerSize) const
{
CHECK_BLOCK_AND_LINE_DIR(aWM);
NS_ASSERTION(aBSize >= 0, "unexpected max block size");
NS_ASSERTION(aContentArea.ISize(aWM) >= 0,
"unexpected content area inline size");
nscoord blockStart = aBCoord + mBlockStart;
if (blockStart < nscoord_MIN) {
NS_WARNING("bad value");
blockStart = nscoord_MIN;
}
// Determine the last float that we should consider.
uint32_t floatCount;
if (aState) {
// Use the provided state.
floatCount = aState->mFloatInfoCount;
MOZ_ASSERT(floatCount <= mFloats.Length(), "bad state");
} else {
// Use our current state.
floatCount = mFloats.Length();
}
// If there are no floats at all, or we're below the last one, return
// quickly.
if (floatCount == 0 ||
(mFloats[floatCount-1].mLeftBEnd <= blockStart &&
mFloats[floatCount-1].mRightBEnd <= blockStart)) {
return nsFlowAreaRect(aWM, aContentArea.IStart(aWM), aBCoord,
aContentArea.ISize(aWM), aBSize, false);
}
nscoord blockEnd;
if (aBSize == nscoord_MAX) {
// This warning (and the two below) are possible to hit on pages
// with really large objects.
NS_WARNING_ASSERTION(aBandInfoType == BandInfoType::BandFromPoint, "bad height");
blockEnd = nscoord_MAX;
} else {
blockEnd = blockStart + aBSize;
if (blockEnd < blockStart || blockEnd > nscoord_MAX) {
NS_WARNING("bad value");
blockEnd = nscoord_MAX;
}
}
nscoord lineLeft = mLineLeft + aContentArea.LineLeft(aWM, aContainerSize);
nscoord lineRight = mLineLeft + aContentArea.LineRight(aWM, aContainerSize);
if (lineRight < lineLeft) {
NS_WARNING("bad value");
lineRight = lineLeft;
}
// Walk backwards through the floats until we either hit the front of
// the list or we're above |blockStart|.
bool haveFloats = false;
for (uint32_t i = floatCount; i > 0; --i) {
const FloatInfo &fi = mFloats[i-1];
if (fi.mLeftBEnd <= blockStart && fi.mRightBEnd <= blockStart) {
// There aren't any more floats that could intersect this band.
break;
}
if (fi.IsEmpty(aShapeType)) {
// For compatibility, ignore floats with empty rects, even though it
// disagrees with the spec. (We might want to fix this in the
// future, though.)
continue;
}
nscoord floatBStart = fi.BStart(aShapeType);
nscoord floatBEnd = fi.BEnd(aShapeType);
if (blockStart < floatBStart && aBandInfoType == BandInfoType::BandFromPoint) {
// This float is below our band. Shrink our band's height if needed.
if (floatBStart < blockEnd) {
blockEnd = floatBStart;
}
}
// If blockStart == blockEnd (which happens only with WidthWithinHeight),
// we include floats that begin at our 0-height vertical area. We
// need to do this to satisfy the invariant that a
// WidthWithinHeight call is at least as narrow on both sides as a
// BandFromPoint call beginning at its blockStart.
else if (blockStart < floatBEnd &&
(floatBStart < blockEnd ||
(floatBStart == blockEnd && blockStart == blockEnd))) {
// This float is in our band.
// Shrink our band's width if needed.
StyleFloat floatStyle = fi.mFrame->StyleDisplay()->PhysicalFloats(aWM);
// When aBandInfoType is BandFromPoint, we're only intended to
// consider a point along the y axis rather than a band.
const nscoord bandBlockEnd =
aBandInfoType == BandInfoType::BandFromPoint ? blockStart : blockEnd;
if (floatStyle == StyleFloat::Left) {
// A left float
nscoord lineRightEdge =
fi.LineRight(aShapeType, blockStart, bandBlockEnd);
if (lineRightEdge > lineLeft) {
lineLeft = lineRightEdge;
// Only set haveFloats to true if the float is inside our
// containing block. This matches the spec for what some
// callers want and disagrees for other callers, so we should
// probably provide better information at some point.
haveFloats = true;
}
} else {
// A right float
nscoord lineLeftEdge =
fi.LineLeft(aShapeType, blockStart, bandBlockEnd);
if (lineLeftEdge < lineRight) {
lineRight = lineLeftEdge;
// See above.
haveFloats = true;
}
}
// Shrink our band's height if needed.
if (floatBEnd < blockEnd && aBandInfoType == BandInfoType::BandFromPoint) {
blockEnd = floatBEnd;
}
}
}
nscoord blockSize = (blockEnd == nscoord_MAX) ?
nscoord_MAX : (blockEnd - blockStart);
// convert back from LineLeft/Right to IStart
nscoord inlineStart = aWM.IsBidiLTR()
? lineLeft - mLineLeft
: mLineLeft - lineRight +
LogicalSize(aWM, aContainerSize).ISize(aWM);
return nsFlowAreaRect(aWM, inlineStart, blockStart - mBlockStart,
lineRight - lineLeft, blockSize, haveFloats);
}
void
nsFloatManager::AddFloat(nsIFrame* aFloatFrame, const LogicalRect& aMarginRect,
WritingMode aWM, const nsSize& aContainerSize)
{
CHECK_BLOCK_AND_LINE_DIR(aWM);
NS_ASSERTION(aMarginRect.ISize(aWM) >= 0, "negative inline size!");
NS_ASSERTION(aMarginRect.BSize(aWM) >= 0, "negative block size!");
FloatInfo info(aFloatFrame, mLineLeft, mBlockStart, aMarginRect, aWM,
aContainerSize);
// Set mLeftBEnd and mRightBEnd.
if (HasAnyFloats()) {
FloatInfo &tail = mFloats[mFloats.Length() - 1];
info.mLeftBEnd = tail.mLeftBEnd;
info.mRightBEnd = tail.mRightBEnd;
} else {
info.mLeftBEnd = nscoord_MIN;
info.mRightBEnd = nscoord_MIN;
}
StyleFloat floatStyle = aFloatFrame->StyleDisplay()->PhysicalFloats(aWM);
MOZ_ASSERT(floatStyle == StyleFloat::Left || floatStyle == StyleFloat::Right,
"Unexpected float style!");
nscoord& sideBEnd =
floatStyle == StyleFloat::Left ? info.mLeftBEnd : info.mRightBEnd;
nscoord thisBEnd = info.BEnd();
if (thisBEnd > sideBEnd)
sideBEnd = thisBEnd;
mFloats.AppendElement(Move(info));
}
// static
LogicalRect
nsFloatManager::CalculateRegionFor(WritingMode aWM,
nsIFrame* aFloat,
const LogicalMargin& aMargin,
const nsSize& aContainerSize)
{
// We consider relatively positioned frames at their original position.
LogicalRect region(aWM, nsRect(aFloat->GetNormalPosition(),
aFloat->GetSize()),
aContainerSize);
// Float region includes its margin
region.Inflate(aWM, aMargin);
// Don't store rectangles with negative margin-box width or height in
// the float manager; it can't deal with them.
if (region.ISize(aWM) < 0) {
// Preserve the right margin-edge for left floats and the left
// margin-edge for right floats
const nsStyleDisplay* display = aFloat->StyleDisplay();
StyleFloat floatStyle = display->PhysicalFloats(aWM);
if ((StyleFloat::Left == floatStyle) == aWM.IsBidiLTR()) {
region.IStart(aWM) = region.IEnd(aWM);
}
region.ISize(aWM) = 0;
}
if (region.BSize(aWM) < 0) {
region.BSize(aWM) = 0;
}
return region;
}
NS_DECLARE_FRAME_PROPERTY_DELETABLE(FloatRegionProperty, nsMargin)
LogicalRect
nsFloatManager::GetRegionFor(WritingMode aWM, nsIFrame* aFloat,
const nsSize& aContainerSize)
{
LogicalRect region = aFloat->GetLogicalRect(aWM, aContainerSize);
void* storedRegion = aFloat->GetProperty(FloatRegionProperty());
if (storedRegion) {
nsMargin margin = *static_cast<nsMargin*>(storedRegion);
region.Inflate(aWM, LogicalMargin(aWM, margin));
}
return region;
}
void
nsFloatManager::StoreRegionFor(WritingMode aWM, nsIFrame* aFloat,
const LogicalRect& aRegion,
const nsSize& aContainerSize)
{
nsRect region = aRegion.GetPhysicalRect(aWM, aContainerSize);
nsRect rect = aFloat->GetRect();
if (region.IsEqualEdges(rect)) {
aFloat->DeleteProperty(FloatRegionProperty());
}
else {
nsMargin* storedMargin = aFloat->GetProperty(FloatRegionProperty());
if (!storedMargin) {
storedMargin = new nsMargin();
aFloat->SetProperty(FloatRegionProperty(), storedMargin);
}
*storedMargin = region - rect;
}
}
nsresult
nsFloatManager::RemoveTrailingRegions(nsIFrame* aFrameList)
{
if (!aFrameList) {
return NS_OK;
}
// This could be a good bit simpler if we could guarantee that the
// floats given were at the end of our list, so we could just search
// for the head of aFrameList. (But we can't;
// layout/reftests/bugs/421710-1.html crashes.)
nsTHashtable<nsPtrHashKey<nsIFrame> > frameSet(1);
for (nsIFrame* f = aFrameList; f; f = f->GetNextSibling()) {
frameSet.PutEntry(f);
}
uint32_t newLength = mFloats.Length();
while (newLength > 0) {
if (!frameSet.Contains(mFloats[newLength - 1].mFrame)) {
break;
}
--newLength;
}
mFloats.TruncateLength(newLength);
#ifdef DEBUG
for (uint32_t i = 0; i < mFloats.Length(); ++i) {
NS_ASSERTION(!frameSet.Contains(mFloats[i].mFrame),
"Frame region deletion was requested but we couldn't delete it");
}
#endif
return NS_OK;
}
void
nsFloatManager::PushState(SavedState* aState)
{
NS_PRECONDITION(aState, "Need a place to save state");
// This is a cheap push implementation, which
// only saves the (x,y) and last frame in the mFrameInfoMap
// which is enough info to get us back to where we should be
// when pop is called.
//
// This push/pop mechanism is used to undo any
// floats that were added during the unconstrained reflow
// in nsBlockReflowContext::DoReflowBlock(). (See bug 96736)
//
// It should also be noted that the state for mFloatDamage is
// intentionally not saved or restored in PushState() and PopState(),
// since that could lead to bugs where damage is missed/dropped when
// we move from position A to B (during the intermediate incremental
// reflow mentioned above) and then from B to C during the subsequent
// reflow. In the typical case A and C will be the same, but not always.
// Allowing mFloatDamage to accumulate the damage incurred during both
// reflows ensures that nothing gets missed.
aState->mLineLeft = mLineLeft;
aState->mBlockStart = mBlockStart;
aState->mPushedLeftFloatPastBreak = mPushedLeftFloatPastBreak;
aState->mPushedRightFloatPastBreak = mPushedRightFloatPastBreak;
aState->mSplitLeftFloatAcrossBreak = mSplitLeftFloatAcrossBreak;
aState->mSplitRightFloatAcrossBreak = mSplitRightFloatAcrossBreak;
aState->mFloatInfoCount = mFloats.Length();
}
void
nsFloatManager::PopState(SavedState* aState)
{
NS_PRECONDITION(aState, "No state to restore?");
mLineLeft = aState->mLineLeft;
mBlockStart = aState->mBlockStart;
mPushedLeftFloatPastBreak = aState->mPushedLeftFloatPastBreak;
mPushedRightFloatPastBreak = aState->mPushedRightFloatPastBreak;
mSplitLeftFloatAcrossBreak = aState->mSplitLeftFloatAcrossBreak;
mSplitRightFloatAcrossBreak = aState->mSplitRightFloatAcrossBreak;
NS_ASSERTION(aState->mFloatInfoCount <= mFloats.Length(),
"somebody misused PushState/PopState");
mFloats.TruncateLength(aState->mFloatInfoCount);
}
nscoord
nsFloatManager::GetLowestFloatTop() const
{
if (mPushedLeftFloatPastBreak || mPushedRightFloatPastBreak) {
return nscoord_MAX;
}
if (!HasAnyFloats()) {
return nscoord_MIN;
}
return mFloats[mFloats.Length() -1].BStart() - mBlockStart;
}
#ifdef DEBUG_FRAME_DUMP
void
DebugListFloatManager(const nsFloatManager *aFloatManager)
{
aFloatManager->List(stdout);
}
nsresult
nsFloatManager::List(FILE* out) const
{
if (!HasAnyFloats())
return NS_OK;
for (uint32_t i = 0; i < mFloats.Length(); ++i) {
const FloatInfo &fi = mFloats[i];
fprintf_stderr(out, "Float %u: frame=%p rect={%d,%d,%d,%d} BEnd={l:%d, r:%d}\n",
i, static_cast<void*>(fi.mFrame),
fi.LineLeft(), fi.BStart(), fi.ISize(), fi.BSize(),
fi.mLeftBEnd, fi.mRightBEnd);
}
return NS_OK;
}
#endif
nscoord
nsFloatManager::ClearFloats(nscoord aBCoord, StyleClear aBreakType,
uint32_t aFlags) const
{
if (!(aFlags & DONT_CLEAR_PUSHED_FLOATS) && ClearContinues(aBreakType)) {
return nscoord_MAX;
}
if (!HasAnyFloats()) {
return aBCoord;
}
nscoord blockEnd = aBCoord + mBlockStart;
const FloatInfo &tail = mFloats[mFloats.Length() - 1];
switch (aBreakType) {
case StyleClear::Both:
blockEnd = std::max(blockEnd, tail.mLeftBEnd);
blockEnd = std::max(blockEnd, tail.mRightBEnd);
break;
case StyleClear::Left:
blockEnd = std::max(blockEnd, tail.mLeftBEnd);
break;
case StyleClear::Right:
blockEnd = std::max(blockEnd, tail.mRightBEnd);
break;
default:
// Do nothing
break;
}
blockEnd -= mBlockStart;
return blockEnd;
}
bool
nsFloatManager::ClearContinues(StyleClear aBreakType) const
{
return ((mPushedLeftFloatPastBreak || mSplitLeftFloatAcrossBreak) &&
(aBreakType == StyleClear::Both ||
aBreakType == StyleClear::Left)) ||
((mPushedRightFloatPastBreak || mSplitRightFloatAcrossBreak) &&
(aBreakType == StyleClear::Both ||
aBreakType == StyleClear::Right));
}
/////////////////////////////////////////////////////////////////////////////
// RoundedBoxShapeInfo
nscoord
nsFloatManager::RoundedBoxShapeInfo::LineLeft(const nscoord aBStart,
const nscoord aBEnd) const
{
if (!mRadii) {
return mRect.x;
}
nscoord lineLeftDiff =
ComputeEllipseLineInterceptDiff(
mRect.y, mRect.YMost(),
mRadii[eCornerTopLeftX], mRadii[eCornerTopLeftY],
mRadii[eCornerBottomLeftX], mRadii[eCornerBottomLeftY],
aBStart, aBEnd);
return mRect.x + lineLeftDiff;
}
nscoord
nsFloatManager::RoundedBoxShapeInfo::LineRight(const nscoord aBStart,
const nscoord aBEnd) const
{
if (!mRadii) {
return mRect.XMost();
}
nscoord lineRightDiff =
ComputeEllipseLineInterceptDiff(
mRect.y, mRect.YMost(),
mRadii[eCornerTopRightX], mRadii[eCornerTopRightY],
mRadii[eCornerBottomRightX], mRadii[eCornerBottomRightY],
aBStart, aBEnd);
return mRect.XMost() - lineRightDiff;
}
/////////////////////////////////////////////////////////////////////////////
// EllipseShapeInfo
nscoord
nsFloatManager::EllipseShapeInfo::LineLeft(const nscoord aBStart,
const nscoord aBEnd) const
{
nscoord lineLeftDiff =
ComputeEllipseLineInterceptDiff(BStart(), BEnd(),
mRadii.width, mRadii.height,
mRadii.width, mRadii.height,
aBStart, aBEnd);
return mCenter.x - mRadii.width + lineLeftDiff;
}
nscoord
nsFloatManager::EllipseShapeInfo::LineRight(const nscoord aBStart,
const nscoord aBEnd) const
{
nscoord lineRightDiff =
ComputeEllipseLineInterceptDiff(BStart(), BEnd(),
mRadii.width, mRadii.height,
mRadii.width, mRadii.height,
aBStart, aBEnd);
return mCenter.x + mRadii.width - lineRightDiff;
}
/////////////////////////////////////////////////////////////////////////////
// PolygonShapeInfo
nsFloatManager::PolygonShapeInfo::PolygonShapeInfo(nsTArray<nsPoint>&& aVertices)
: mVertices(aVertices)
{
// Polygons with fewer than three vertices result in an empty area.
// https://drafts.csswg.org/css-shapes/#funcdef-polygon
if (mVertices.Length() < 3) {
mEmpty = true;
return;
}
auto Determinant = [] (const nsPoint& aP0, const nsPoint& aP1) {
// Returns the determinant of the 2x2 matrix [aP0 aP1].
// https://en.wikipedia.org/wiki/Determinant#2_.C3.97_2_matrices
return aP0.x * aP1.y - aP0.y * aP1.x;
};
// See if we have any vertices that are non-collinear with the first two.
// (If a polygon's vertices are all collinear, it encloses no area.)
bool isEntirelyCollinear = true;
const nsPoint& p0 = mVertices[0];
const nsPoint& p1 = mVertices[1];
for (size_t i = 2; i < mVertices.Length(); ++i) {
const nsPoint& p2 = mVertices[i];
// If the determinant of the matrix formed by two points is 0, that
// means they're collinear with respect to the origin. Here, if it's
// nonzero, then p1 and p2 are non-collinear with respect to p0, i.e.
// the three points are non-collinear.
if (Determinant(p2 - p0, p1 - p0) != 0) {
isEntirelyCollinear = false;
break;
}
}
if (isEntirelyCollinear) {
mEmpty = true;
return;
}
// mBStart and mBEnd are the lower and the upper bounds of all the
// vertex.y, respectively. The vertex.y is actually on the block-axis of
// the float manager's writing mode.
for (const nsPoint& vertex : mVertices) {
mBStart = std::min(mBStart, vertex.y);
mBEnd = std::max(mBEnd, vertex.y);
}
}
nscoord
nsFloatManager::PolygonShapeInfo::LineLeft(const nscoord aBStart,
const nscoord aBEnd) const
{
MOZ_ASSERT(!mEmpty, "Shouldn't be called if the polygon encloses no area.");
// We want the line-left-most inline-axis coordinate where the
// (block-axis) aBStart/aBEnd band crosses a line segment of the polygon.
// To get that, we start as line-right as possible (at nscoord_MAX). Then
// we iterate each line segment to compute its intersection point with the
// band (if any) and using std::min() successively to get the smallest
// inline-coordinates among those intersection points.
//
// Note: std::min<nscoord> means the function std::min() with template
// parameter nscoord, not the minimum value of nscoord.
return ComputeLineIntercept(aBStart, aBEnd, std::min<nscoord>, nscoord_MAX);
}
nscoord
nsFloatManager::PolygonShapeInfo::LineRight(const nscoord aBStart,
const nscoord aBEnd) const
{
MOZ_ASSERT(!mEmpty, "Shouldn't be called if the polygon encloses no area.");
// Similar to LineLeft(). Though here, we want the line-right-most
// inline-axis coordinate, so we instead start at nscoord_MIN and use
// std::max() to get the biggest inline-coordinate among those
// intersection points.
return ComputeLineIntercept(aBStart, aBEnd, std::max<nscoord>, nscoord_MIN);
}
nscoord
nsFloatManager::PolygonShapeInfo::ComputeLineIntercept(
const nscoord aBStart,
const nscoord aBEnd,
nscoord (*aCompareOp) (std::initializer_list<nscoord>),
const nscoord aLineInterceptInitialValue) const
{
MOZ_ASSERT(aBStart <= aBEnd,
"The band's block start is greater than its block end?");
const size_t len = mVertices.Length();
nscoord lineIntercept = aLineInterceptInitialValue;
// Iterate each line segment {p0, p1}, {p1, p2}, ..., {pn, p0}.
for (size_t i = 0; i < len; ++i) {
const nsPoint* smallYVertex = &mVertices[i];
const nsPoint* bigYVertex = &mVertices[(i + 1) % len];
// Swap the two points to satisfy the requirement for calling
// XInterceptAtY.
if (smallYVertex->y > bigYVertex->y) {
std::swap(smallYVertex, bigYVertex);
}
if (aBStart >= bigYVertex->y || aBEnd <= smallYVertex->y ||
smallYVertex->y == bigYVertex->y) {
// Skip computing the intercept if a) the band doesn't intersect the
// line segment (even if it crosses one of two the vertices); or b)
// the line segment is horizontal. It's OK because the two end points
// forming this horizontal segment will still be considered if each of
// them is forming another non-horizontal segment with other points.
continue;
}
nscoord bStartLineIntercept =
aBStart <= smallYVertex->y
? smallYVertex->x
: XInterceptAtY(aBStart, *smallYVertex, *bigYVertex);
nscoord bEndLineIntercept =
aBEnd >= bigYVertex->y
? bigYVertex->x
: XInterceptAtY(aBEnd, *smallYVertex, *bigYVertex);
// If either new intercept is more extreme than lineIntercept (per
// aCompareOp), then update lineIntercept to that value.
lineIntercept =
aCompareOp({lineIntercept, bStartLineIntercept, bEndLineIntercept});
}
return lineIntercept;
}
void
nsFloatManager::PolygonShapeInfo::Translate(nscoord aLineLeft,
nscoord aBlockStart)
{
for (nsPoint& vertex : mVertices) {
vertex.MoveBy(aLineLeft, aBlockStart);
}
mBStart += aBlockStart;
mBEnd += aBlockStart;
}
/* static */ nscoord
nsFloatManager::PolygonShapeInfo::XInterceptAtY(const nscoord aY,
const nsPoint& aP1,
const nsPoint& aP2)
{
// Solve for x in the linear equation: x = x1 + (y-y1) * (x2-x1) / (y2-y1),
// where aP1 = (x1, y1) and aP2 = (x2, y2).
MOZ_ASSERT(aP1.y <= aY && aY <= aP2.y,
"This function won't work if the horizontal line at aY and "
"the line segment (aP1, aP2) do not intersect!");
MOZ_ASSERT(aP1.y != aP2.y,
"A horizontal line segment results in dividing by zero error!");
return aP1.x + (aY - aP1.y) * (aP2.x - aP1.x) / (aP2.y - aP1.y);
}
/////////////////////////////////////////////////////////////////////////////
// FloatInfo
nsFloatManager::FloatInfo::FloatInfo(nsIFrame* aFrame,
nscoord aLineLeft, nscoord aBlockStart,
const LogicalRect& aMarginRect,
WritingMode aWM,
const nsSize& aContainerSize)
: mFrame(aFrame)
, mRect(ShapeInfo::ConvertToFloatLogical(aMarginRect, aWM, aContainerSize) +
nsPoint(aLineLeft, aBlockStart))
{
MOZ_COUNT_CTOR(nsFloatManager::FloatInfo);
const StyleShapeSource& shapeOutside = mFrame->StyleDisplay()->mShapeOutside;
if (shapeOutside.GetType() == StyleShapeSourceType::None) {
return;
}
if (shapeOutside.GetType() == StyleShapeSourceType::URL) {
// Bug 1265343: Implement 'shape-image-threshold'. Early return
// here because shape-outside with url() value doesn't have a
// reference box, and GetReferenceBox() asserts that.
return;
}
// Initialize <shape-box>'s reference rect.
LogicalRect shapeBoxRect =
ShapeInfo::ComputeShapeBoxRect(shapeOutside, mFrame, aMarginRect, aWM);
if (shapeOutside.GetType() == StyleShapeSourceType::Box) {
mShapeInfo = ShapeInfo::CreateShapeBox(mFrame, shapeBoxRect, aWM,
aContainerSize);
} else if (shapeOutside.GetType() == StyleShapeSourceType::Shape) {
StyleBasicShape* const basicShape = shapeOutside.GetBasicShape();
switch (basicShape->GetShapeType()) {
case StyleBasicShapeType::Polygon:
mShapeInfo =
ShapeInfo::CreatePolygon(basicShape, shapeBoxRect, aWM,
aContainerSize);
break;
case StyleBasicShapeType::Circle:
case StyleBasicShapeType::Ellipse:
mShapeInfo =
ShapeInfo::CreateCircleOrEllipse(basicShape, shapeBoxRect, aWM,
aContainerSize);
break;
case StyleBasicShapeType::Inset:
mShapeInfo =
ShapeInfo::CreateInset(basicShape, shapeBoxRect, aWM, aContainerSize);
break;
}
} else {
MOZ_ASSERT_UNREACHABLE("Unknown StyleShapeSourceType!");
}
MOZ_ASSERT(mShapeInfo,
"All shape-outside values except none should have mShapeInfo!");
// Translate the shape to the same origin as nsFloatManager.
mShapeInfo->Translate(aLineLeft, aBlockStart);
}
#ifdef NS_BUILD_REFCNT_LOGGING
nsFloatManager::FloatInfo::FloatInfo(FloatInfo&& aOther)
: mFrame(Move(aOther.mFrame))
, mLeftBEnd(Move(aOther.mLeftBEnd))
, mRightBEnd(Move(aOther.mRightBEnd))
, mRect(Move(aOther.mRect))
, mShapeInfo(Move(aOther.mShapeInfo))
{
MOZ_COUNT_CTOR(nsFloatManager::FloatInfo);
}
nsFloatManager::FloatInfo::~FloatInfo()
{
MOZ_COUNT_DTOR(nsFloatManager::FloatInfo);
}
#endif
nscoord
nsFloatManager::FloatInfo::LineLeft(ShapeType aShapeType,
const nscoord aBStart,
const nscoord aBEnd) const
{
if (aShapeType == ShapeType::Margin) {
return LineLeft();
}
MOZ_ASSERT(aShapeType == ShapeType::ShapeOutside);
if (!mShapeInfo) {
return LineLeft();
}
// Clip the flow area to the margin-box because
// https://drafts.csswg.org/css-shapes-1/#relation-to-box-model-and-float-behavior
// says "When a shape is used to define a float area, the shape is clipped
// to the float’s margin box."
return std::max(LineLeft(), mShapeInfo->LineLeft(aBStart, aBEnd));
}
nscoord
nsFloatManager::FloatInfo::LineRight(ShapeType aShapeType,
const nscoord aBStart,
const nscoord aBEnd) const
{
if (aShapeType == ShapeType::Margin) {
return LineRight();
}
MOZ_ASSERT(aShapeType == ShapeType::ShapeOutside);
if (!mShapeInfo) {
return LineRight();
}
// Clip the flow area to the margin-box. See LineLeft().
return std::min(LineRight(), mShapeInfo->LineRight(aBStart, aBEnd));
}
nscoord
nsFloatManager::FloatInfo::BStart(ShapeType aShapeType) const
{
if (aShapeType == ShapeType::Margin) {
return BStart();
}
MOZ_ASSERT(aShapeType == ShapeType::ShapeOutside);
if (!mShapeInfo) {
return BStart();
}
// Clip the flow area to the margin-box. See LineLeft().
return std::max(BStart(), mShapeInfo->BStart());
}
nscoord
nsFloatManager::FloatInfo::BEnd(ShapeType aShapeType) const
{
if (aShapeType == ShapeType::Margin) {
return BEnd();
}
MOZ_ASSERT(aShapeType == ShapeType::ShapeOutside);
if (!mShapeInfo) {
return BEnd();
}
// Clip the flow area to the margin-box. See LineLeft().
return std::min(BEnd(), mShapeInfo->BEnd());
}
bool
nsFloatManager::FloatInfo::IsEmpty(ShapeType aShapeType) const
{
if (aShapeType == ShapeType::Margin) {
return IsEmpty();
}
MOZ_ASSERT(aShapeType == ShapeType::ShapeOutside);
if (!mShapeInfo) {
return IsEmpty();
}
return mShapeInfo->IsEmpty();
}
/////////////////////////////////////////////////////////////////////////////
// ShapeInfo
/* static */ LogicalRect
nsFloatManager::ShapeInfo::ComputeShapeBoxRect(
const StyleShapeSource& aShapeOutside,
nsIFrame* const aFrame,
const mozilla::LogicalRect& aMarginRect,
mozilla::WritingMode aWM)
{
LogicalRect rect = aMarginRect;
switch (aShapeOutside.GetReferenceBox()) {
case StyleGeometryBox::ContentBox:
rect.Deflate(aWM, aFrame->GetLogicalUsedPadding(aWM));
MOZ_FALLTHROUGH;
case StyleGeometryBox::PaddingBox:
rect.Deflate(aWM, aFrame->GetLogicalUsedBorder(aWM));
MOZ_FALLTHROUGH;
case StyleGeometryBox::BorderBox:
rect.Deflate(aWM, aFrame->GetLogicalUsedMargin(aWM));
break;
case StyleGeometryBox::MarginBox:
// Do nothing. rect is already a margin rect.
break;
case StyleGeometryBox::NoBox:
default:
MOZ_ASSERT(aShapeOutside.GetType() != StyleShapeSourceType::Box,
"Box source type must have <shape-box> specified!");
break;
}
return rect;
}
/* static */ UniquePtr<nsFloatManager::ShapeInfo>
nsFloatManager::ShapeInfo::CreateShapeBox(
nsIFrame* const aFrame,
const LogicalRect& aShapeBoxRect,
WritingMode aWM,
const nsSize& aContainerSize)
{
nsRect logicalShapeBoxRect
= ConvertToFloatLogical(aShapeBoxRect, aWM, aContainerSize);
nscoord physicalRadii[8];
bool hasRadii = aFrame->GetShapeBoxBorderRadii(physicalRadii);
if (!hasRadii) {
return MakeUnique<RoundedBoxShapeInfo>(logicalShapeBoxRect,
UniquePtr<nscoord[]>());
}
return MakeUnique<RoundedBoxShapeInfo>(logicalShapeBoxRect,
ConvertToFloatLogical(physicalRadii,
aWM));
}
/* static */ UniquePtr<nsFloatManager::ShapeInfo>
nsFloatManager::ShapeInfo::CreateInset(
const StyleBasicShape* aBasicShape,
const LogicalRect& aShapeBoxRect,
WritingMode aWM,
const nsSize& aContainerSize)
{
// Use physical coordinates to compute inset() because the top, right,
// bottom and left offsets are physical.
// https://drafts.csswg.org/css-shapes-1/#funcdef-inset
nsRect physicalShapeBoxRect =
aShapeBoxRect.GetPhysicalRect(aWM, aContainerSize);
nsRect insetRect =
ShapeUtils::ComputeInsetRect(aBasicShape, physicalShapeBoxRect);
nsRect logicalInsetRect =
ConvertToFloatLogical(LogicalRect(aWM, insetRect, aContainerSize),
aWM, aContainerSize);
nscoord physicalRadii[8];
bool hasRadii =
ShapeUtils::ComputeInsetRadii(aBasicShape, insetRect, physicalShapeBoxRect,
physicalRadii);
if (!hasRadii) {
return MakeUnique<RoundedBoxShapeInfo>(logicalInsetRect,
UniquePtr<nscoord[]>());
}
return MakeUnique<RoundedBoxShapeInfo>(logicalInsetRect,
ConvertToFloatLogical(physicalRadii,
aWM));
}
/* static */ UniquePtr<nsFloatManager::ShapeInfo>
nsFloatManager::ShapeInfo::CreateCircleOrEllipse(
const StyleBasicShape* aBasicShape,
const LogicalRect& aShapeBoxRect,
WritingMode aWM,
const nsSize& aContainerSize)
{