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/. */
7 /* class that manages rules for positioning floats */
9 #ifndef nsFloatManager_h_
10 #define nsFloatManager_h_
12 #include "mozilla/Attributes.h"
13 #include "mozilla/TypedEnumBits.h"
14 #include "mozilla/UniquePtr.h"
15 #include "mozilla/WritingModes.h"
17 #include "nsFrameList.h" // for DEBUG_FRAME_DUMP
18 #include "nsIntervalSet.h"
27 } // namespace mozilla
29 enum class nsFlowAreaRectFlags
: uint32_t {
33 ISizeIsActuallyNegative
= 1 << 2,
35 MOZ_MAKE_ENUM_CLASS_BITWISE_OPERATORS(nsFlowAreaRectFlags
)
38 * The available space for content not occupied by floats is divided
39 * into a sequence of rectangles in the block direction. However, we
40 * need to know not only the rectangle, but also whether it was reduced
41 * (from the content rectangle) by floats that actually intruded into
42 * the content rectangle. If it has been reduced by floats, then we also
43 * track whether the flow area might widen as the floats narrow in the
46 struct nsFlowAreaRect
{
47 mozilla::LogicalRect mRect
;
49 nsFlowAreaRectFlags mAreaFlags
;
51 nsFlowAreaRect(mozilla::WritingMode aWritingMode
, nscoord aICoord
,
52 nscoord aBCoord
, nscoord aISize
, nscoord aBSize
,
53 nsFlowAreaRectFlags aAreaFlags
)
54 : mRect(aWritingMode
, aICoord
, aBCoord
, aISize
, aBSize
),
55 mAreaFlags(aAreaFlags
) {}
57 bool HasFloats() const {
58 return (bool)(mAreaFlags
& nsFlowAreaRectFlags::HasFloats
);
60 bool MayWiden() const {
61 return (bool)(mAreaFlags
& nsFlowAreaRectFlags::MayWiden
);
63 bool ISizeIsActuallyNegative() const {
64 return (bool)(mAreaFlags
& nsFlowAreaRectFlags::ISizeIsActuallyNegative
);
68 #define NS_FLOAT_MANAGER_CACHE_SIZE 64
71 * nsFloatManager is responsible for implementing CSS's rules for
72 * positioning floats. An nsFloatManager object is created during reflow for
73 * any block with NS_BLOCK_FLOAT_MGR. During reflow, the float manager for
74 * the nearest such ancestor block is found in ReflowInput::mFloatManager.
76 * According to the line-relative mappings in CSS Writing Modes spec [1],
77 * line-right and line-left are calculated with respect to the writing mode
78 * of the containing block of the floats. All the writing modes passed to
79 * nsFloatManager methods should be the containing block's writing mode.
81 * However, according to the abstract-to-physical mappings table [2], the
82 * 'direction' property of the containing block doesn't affect the
83 * interpretation of line-right and line-left. We actually implement this by
84 * passing in the writing mode of the block formatting context (BFC), i.e.
85 * the of BlockReflowState's writing mode.
87 * nsFloatManager uses a special logical coordinate space with inline
88 * coordinates on the line-axis and block coordinates on the block-axis
89 * based on the writing mode of the block formatting context. All the
90 * physical types like nsRect, nsPoint, etc. use this coordinate space. See
91 * FloatInfo::mRect for an example.
93 * [1] https://drafts.csswg.org/css-writing-modes/#line-mappings
94 * [2] https://drafts.csswg.org/css-writing-modes/#logical-to-physical
96 class nsFloatManager
{
98 explicit nsFloatManager(mozilla::PresShell
* aPresShell
,
99 mozilla::WritingMode aWM
);
102 void* operator new(size_t aSize
) noexcept(true);
103 void operator delete(void* aPtr
, size_t aSize
);
105 static void Shutdown();
108 * Get float region stored on the frame. (Defaults to mRect if it's
109 * not there.) The float region is the area impacted by this float;
110 * the coordinates are relative to the containing block frame.
112 static mozilla::LogicalRect
GetRegionFor(mozilla::WritingMode aWM
,
113 nsIFrame
* aFloatFrame
,
114 const nsSize
& aContainerSize
);
116 * Calculate the float region for this frame using aMargin and the
117 * frame's mRect. The region includes the margins around the float,
118 * but doesn't include the relative offsets.
119 * Note that if the frame is or has a continuation, aMargin's top
120 * and/or bottom must be zeroed by the caller.
122 static mozilla::LogicalRect
CalculateRegionFor(
123 mozilla::WritingMode aWM
, nsIFrame
* aFloatFrame
,
124 const mozilla::LogicalMargin
& aMargin
, const nsSize
& aContainerSize
);
126 * Store the float region on the frame. The region is stored
127 * as a delta against the mRect, so repositioning the frame will
128 * also reposition the float region.
130 static void StoreRegionFor(mozilla::WritingMode aWM
, nsIFrame
* aFloat
,
131 const mozilla::LogicalRect
& aRegion
,
132 const nsSize
& aContainerSize
);
134 // Structure that stores the current state of a float manager for
135 // Save/Restore purposes.
137 explicit SavedState()
138 : mFloatInfoCount(0),
141 mPushedLeftFloatPastBreak(false),
142 mPushedRightFloatPastBreak(false),
143 mSplitLeftFloatAcrossBreak(false),
144 mSplitRightFloatAcrossBreak(false) {}
147 uint32_t mFloatInfoCount
;
148 nscoord mLineLeft
, mBlockStart
;
149 bool mPushedLeftFloatPastBreak
;
150 bool mPushedRightFloatPastBreak
;
151 bool mSplitLeftFloatAcrossBreak
;
152 bool mSplitRightFloatAcrossBreak
;
154 friend class nsFloatManager
;
158 * Translate the current origin by the specified offsets. This
159 * creates a new local coordinate space relative to the current
162 void Translate(nscoord aLineLeft
, nscoord aBlockStart
) {
163 mLineLeft
+= aLineLeft
;
164 mBlockStart
+= aBlockStart
;
168 * Returns the current translation from local coordinate space to
169 * world coordinate space. This represents the accumulated calls to
172 void GetTranslation(nscoord
& aLineLeft
, nscoord
& aBlockStart
) const {
173 aLineLeft
= mLineLeft
;
174 aBlockStart
= mBlockStart
;
178 * Get information about the area available to content that flows
179 * around floats. Two different types of space can be requested:
180 * BandFromPoint: returns the band containing block-dir coordinate
181 * |aBCoord| (though actually with the top truncated to begin at
182 * aBCoord), but up to at most |aBSize| (which may be nscoord_MAX).
183 * This will return the tallest rectangle whose block start is
184 * |aBCoord| and in which there are no changes in what floats are
185 * on the sides of that rectangle, but will limit the block size
186 * of the rectangle to |aBSize|. The inline start and end edges
187 * of the rectangle give the area available for line boxes in that
188 * space. The inline size of this resulting rectangle will not be
190 * WidthWithinHeight: This returns a rectangle whose block start
191 * is aBCoord and whose block size is exactly aBSize. Its inline
192 * start and end edges give the corresponding edges of the space
193 * that can be used for line boxes *throughout* that space. (It
194 * is possible that more inline space could be used in part of the
195 * space if a float begins or ends in it.) The inline size of the
196 * resulting rectangle can be negative.
198 * ShapeType can be used to request two different types of flow areas.
199 * (This is the float area defined in CSS Shapes Module Level 1 ยง1.4):
200 * Margin: uses the float element's margin-box to request the flow area.
201 * ShapeOutside: uses the float element's shape-outside value to request
204 * @param aBCoord [in] block-dir coordinate for block start of available space
205 * desired, which are positioned relative to the current translation.
206 * @param aBSize [in] see above
207 * @param aContentArea [in] an nsRect representing the content area
208 * @param aState [in] If null, use the current state, otherwise, do
209 * computation based only on floats present in the given
211 * @return An nsFlowAreaRect whose:
212 * mRect is the resulting rectangle for line boxes. It will not
213 * extend beyond aContentArea's inline bounds, but may be
214 * narrower when floats are present.
215 * mHasFloats is whether there are floats at the sides of the
216 * return value including those that do not reduce the line box
217 * inline size at all (because they are entirely in the margins)
219 enum class BandInfoType
{ BandFromPoint
, WidthWithinHeight
};
220 enum class ShapeType
{ Margin
, ShapeOutside
};
221 nsFlowAreaRect
GetFlowArea(mozilla::WritingMode aWM
, nscoord aBCoord
,
222 nscoord aBSize
, BandInfoType aBandInfoType
,
223 ShapeType aShapeType
,
224 mozilla::LogicalRect aContentArea
,
226 const nsSize
& aContainerSize
) const;
229 * Add a float that comes after all floats previously added. Its
230 * block start must be even with or below the top of all previous
233 * aMarginRect is relative to the current translation. The caller
234 * must ensure aMarginRect.height >= 0 and aMarginRect.width >= 0.
236 void AddFloat(nsIFrame
* aFloatFrame
, const mozilla::LogicalRect
& aMarginRect
,
237 mozilla::WritingMode aWM
, const nsSize
& aContainerSize
);
240 * Notify that we tried to place a float that could not fit at all and
241 * had to be pushed to the next page/column? (If so, we can't place
242 * any more floats in this page/column because of the rule that the
243 * top of a float cannot be above the top of an earlier float. It
244 * also means that any clear needs to continue to the next column.)
246 void SetPushedLeftFloatPastBreak() { mPushedLeftFloatPastBreak
= true; }
247 void SetPushedRightFloatPastBreak() { mPushedRightFloatPastBreak
= true; }
250 * Notify that we split a float, with part of it needing to be pushed
251 * to the next page/column. (This means that any 'clear' needs to
252 * continue to the next page/column.)
254 void SetSplitLeftFloatAcrossBreak() { mSplitLeftFloatAcrossBreak
= true; }
255 void SetSplitRightFloatAcrossBreak() { mSplitRightFloatAcrossBreak
= true; }
258 * Remove the regions associated with this floating frame and its
259 * next-sibling list. Some of the frames may never have been added;
260 * we just skip those. This is not fully general; it only works as
261 * long as the N frames to be removed are the last N frames to have
262 * been added; if there's a frame in the middle of them that should
263 * not be removed, YOU LOSE.
265 nsresult
RemoveTrailingRegions(nsIFrame
* aFrameList
);
267 bool HasAnyFloats() const { return !mFloats
.IsEmpty(); }
270 * Methods for dealing with the propagation of float damage during
273 bool HasFloatDamage() const { return !mFloatDamage
.IsEmpty(); }
275 void IncludeInDamage(nscoord aIntervalBegin
, nscoord aIntervalEnd
) {
276 mFloatDamage
.IncludeInterval(aIntervalBegin
+ mBlockStart
,
277 aIntervalEnd
+ mBlockStart
);
280 bool IntersectsDamage(nscoord aIntervalBegin
, nscoord aIntervalEnd
) const {
281 return mFloatDamage
.Intersects(aIntervalBegin
+ mBlockStart
,
282 aIntervalEnd
+ mBlockStart
);
286 * Saves the current state of the float manager into aState.
288 void PushState(SavedState
* aState
);
291 * Restores the float manager to the saved state.
293 * These states must be managed using stack discipline. PopState can only
294 * be used after PushState has been used to save the state, and it can only
295 * be used once --- although it can be omitted; saved states can be ignored.
296 * States must be popped in the reverse order they were pushed. A
297 * call to PopState invalidates any saved states Pushed after the
298 * state passed to PopState was pushed.
300 void PopState(SavedState
* aState
);
303 * Get the block start of the last float placed into the float
304 * manager, to enforce the rule that a float can't be above an earlier
305 * float. Returns the minimum nscoord value if there are no floats.
307 * The result is relative to the current translation.
309 nscoord
LowestFloatBStart() const;
312 * Return the coordinate of the lowest float matching aClearType in
313 * this float manager. Returns aBCoord if there are no matching
316 * Both aBCoord and the result are relative to the current translation.
318 nscoord
ClearFloats(nscoord aBCoord
, mozilla::StyleClear aClearType
) const;
321 * Checks if clear would pass into the floats' BFC's next-in-flow,
322 * i.e. whether floats affecting this clear have continuations.
324 bool ClearContinues(mozilla::StyleClear aClearType
) const;
326 void AssertStateMatches(SavedState
* aState
) const {
328 aState
->mLineLeft
== mLineLeft
&& aState
->mBlockStart
== mBlockStart
&&
329 aState
->mPushedLeftFloatPastBreak
== mPushedLeftFloatPastBreak
&&
330 aState
->mPushedRightFloatPastBreak
== mPushedRightFloatPastBreak
&&
331 aState
->mSplitLeftFloatAcrossBreak
== mSplitLeftFloatAcrossBreak
&&
332 aState
->mSplitRightFloatAcrossBreak
==
333 mSplitRightFloatAcrossBreak
&&
334 aState
->mFloatInfoCount
== mFloats
.Length(),
335 "float manager state should match saved state");
338 #ifdef DEBUG_FRAME_DUMP
340 * Dump the state of the float manager out to a file.
342 nsresult
List(FILE* out
) const;
347 class RoundedBoxShapeInfo
;
348 class EllipseShapeInfo
;
349 class PolygonShapeInfo
;
350 class ImageShapeInfo
;
353 nsIFrame
* const mFrame
;
354 // The lowest block-ends of left/right floats up to and including
356 nscoord mLeftBEnd
, mRightBEnd
;
358 FloatInfo(nsIFrame
* aFrame
, nscoord aLineLeft
, nscoord aBlockStart
,
359 const mozilla::LogicalRect
& aMarginRect
, mozilla::WritingMode aWM
,
360 const nsSize
& aContainerSize
);
362 nscoord
LineLeft() const { return mRect
.x
; }
363 nscoord
LineRight() const { return mRect
.XMost(); }
364 nscoord
ISize() const { return mRect
.width
; }
365 nscoord
BStart() const { return mRect
.y
; }
366 nscoord
BEnd() const { return mRect
.YMost(); }
367 nscoord
BSize() const { return mRect
.height
; }
368 bool IsEmpty() const { return mRect
.IsEmpty(); }
370 // aBStart and aBEnd are the starting and ending coordinate of a band.
371 // LineLeft() and LineRight() return the innermost line-left extent and
372 // line-right extent within the given band, respectively.
373 nscoord
LineLeft(ShapeType aShapeType
, const nscoord aBStart
,
374 const nscoord aBEnd
) const;
375 nscoord
LineRight(ShapeType aShapeType
, const nscoord aBStart
,
376 const nscoord aBEnd
) const;
377 nscoord
BStart(ShapeType aShapeType
) const;
378 nscoord
BEnd(ShapeType aShapeType
) const;
379 bool IsEmpty(ShapeType aShapeType
) const;
380 bool MayNarrowInBlockDirection(ShapeType aShapeType
) const;
382 #ifdef NS_BUILD_REFCNT_LOGGING
383 FloatInfo(FloatInfo
&& aOther
);
387 // NB! This is really a logical rect in a writing mode suitable for
388 // placing floats, which is not necessarily the actual writing mode
389 // either of the block which created the float manager or the block
390 // that is calling the float manager. The inline coordinates are in
391 // the line-relative axis of the float manager and its block
392 // coordinates are in the float manager's block direction.
394 // Pointer to a concrete subclass of ShapeInfo or null, which means that
395 // there is no shape-outside.
396 mozilla::UniquePtr
<ShapeInfo
> mShapeInfo
;
400 // Store the writing mode from the block frame which establishes the block
401 // formatting context (BFC) when the nsFloatManager is created.
402 mozilla::WritingMode mWritingMode
;
405 // Translation from local to global coordinate space.
406 nscoord mLineLeft
, mBlockStart
;
407 // We use 11 here in order to fill up the jemalloc allocatoed chunk nicely,
408 // see https://bugzilla.mozilla.org/show_bug.cgi?id=1362876#c6.
409 AutoTArray
<FloatInfo
, 11> mFloats
;
410 nsIntervalSet mFloatDamage
;
412 // Did we try to place a float that could not fit at all and had to be
413 // pushed to the next page/column? If so, we can't place any more
414 // floats in this page/column because of the rule that the top of a
415 // float cannot be above the top of an earlier float. And we also
416 // need to apply this information to 'clear', and thus need to
417 // separate left and right floats.
418 bool mPushedLeftFloatPastBreak
;
419 bool mPushedRightFloatPastBreak
;
421 // Did we split a float, with part of it needing to be pushed to the
422 // next page/column. This means that any 'clear' needs to continue to
423 // the next page/column.
424 bool mSplitLeftFloatAcrossBreak
;
425 bool mSplitRightFloatAcrossBreak
;
427 static int32_t sCachedFloatManagerCount
;
428 static void* sCachedFloatManagers
[NS_FLOAT_MANAGER_CACHE_SIZE
];
430 nsFloatManager(const nsFloatManager
&) = delete;
431 void operator=(const nsFloatManager
&) = delete;
435 * A helper class to manage maintenance of the float manager during
436 * nsBlockFrame::Reflow. It automatically restores the old float
437 * manager in the reflow input when the object goes out of scope.
439 class nsAutoFloatManager
{
440 using ReflowInput
= mozilla::ReflowInput
;
443 explicit nsAutoFloatManager(ReflowInput
& aReflowInput
)
444 : mReflowInput(aReflowInput
), mOld(nullptr) {}
446 ~nsAutoFloatManager();
449 * Create a new float manager for the specified frame. This will
450 * `remember' the old float manager, and install the new float
451 * manager in the reflow input.
453 void CreateFloatManager(nsPresContext
* aPresContext
);
456 ReflowInput
& mReflowInput
;
457 mozilla::UniquePtr
<nsFloatManager
> mNew
;
459 // A non-owning pointer, which points to the object owned by
460 // nsAutoFloatManager::mNew.
461 nsFloatManager
* mOld
;
464 #endif /* !defined(nsFloatManager_h_) */