Backed out 2 changesets (bug 1906804, bug 1882553) for causing artifact build bustage...
[gecko.git] / layout / generic / ReflowInput.h
blobbaf492b83d82e9f8faf7c010273767f4e5b41960
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 /* struct containing the input to nsIFrame::Reflow */
9 #ifndef mozilla_ReflowInput_h
10 #define mozilla_ReflowInput_h
12 #include "nsMargin.h"
13 #include "nsStyleConsts.h"
14 #include "mozilla/Assertions.h"
15 #include "mozilla/EnumSet.h"
16 #include "mozilla/Maybe.h"
17 #include "mozilla/WritingModes.h"
18 #include "LayoutConstants.h"
19 #include "ReflowOutput.h"
20 #include <algorithm>
22 class gfxContext;
23 class nsFloatManager;
24 struct nsHypotheticalPosition;
25 class nsIPercentBSizeObserver;
26 class nsLineLayout;
27 class nsPlaceholderFrame;
28 class nsPresContext;
29 class nsReflowStatus;
31 namespace mozilla {
32 enum class LayoutFrameType : uint8_t;
34 /**
35 * A set of StyleSizes used as an input parameter to various functions that
36 * compute sizes like nsIFrame::ComputeSize(). If any of the member fields has a
37 * value, the function may use the value instead of retrieving it from the
38 * frame's style.
40 * The logical sizes are assumed to be in the associated frame's writing-mode.
42 struct StyleSizeOverrides {
43 Maybe<StyleSize> mStyleISize;
44 Maybe<StyleSize> mStyleBSize;
45 Maybe<AspectRatio> mAspectRatio;
47 bool HasAnyOverrides() const { return mStyleISize || mStyleBSize; }
48 bool HasAnyLengthOverrides() const {
49 return (mStyleISize && mStyleISize->ConvertsToLength()) ||
50 (mStyleBSize && mStyleBSize->ConvertsToLength());
53 // By default, table wrapper frame considers the size overrides applied to
54 // itself, so it creates any length size overrides for inner table frame by
55 // subtracting the area occupied by the caption and border & padding according
56 // to box-sizing.
58 // When this flag is true, table wrapper frame is required to apply the size
59 // overrides to the inner table frame directly, without any modification,
60 // which is useful for flex container to override the inner table frame's
61 // preferred main size with 'flex-basis'.
63 // Note: if mStyleISize is a LengthPercentage, the inner table frame will
64 // comply with the inline-size override without enforcing its min-content
65 // inline-size in nsTableFrame::ComputeSize(). This is necessary so that small
66 // flex-basis values like 'flex-basis:1%' can be resolved correctly; the
67 // flexbox layout algorithm does still explicitly clamp to min-sizes *at a
68 // later step*, after the flex-basis has been resolved -- so this flag won't
69 // actually produce any user-visible tables whose final inline size is smaller
70 // than their min-content inline size.
71 bool mApplyOverridesVerbatim = false;
73 } // namespace mozilla
75 /**
76 * @return aValue clamped to [aMinValue, aMaxValue].
78 * @note This function needs to handle aMinValue > aMaxValue. In that case,
79 * aMinValue is returned. That's why we cannot use std::clamp() and
80 * mozilla::clamped() since they both assert max >= min.
81 * @note If aMinValue and aMaxValue are computed min block-size and max
82 * block-size, it is simpler to use ReflowInput::ApplyMinMaxBSize().
83 * Similarly, there is ReflowInput::ApplyMinMaxISize() for clamping an
84 * inline-size.
85 * @see http://www.w3.org/TR/CSS21/visudet.html#min-max-widths
86 * @see http://www.w3.org/TR/CSS21/visudet.html#min-max-heights
88 template <class NumericType>
89 NumericType NS_CSS_MINMAX(NumericType aValue, NumericType aMinValue,
90 NumericType aMaxValue) {
91 NumericType result = aValue;
92 if (aMaxValue < result) result = aMaxValue;
93 if (aMinValue > result) result = aMinValue;
94 return result;
97 namespace mozilla {
99 // A base class of ReflowInput that computes only the padding,
100 // border, and margin, since those values are needed more often.
101 struct SizeComputationInput {
102 public:
103 // The frame being reflowed.
104 nsIFrame* const mFrame;
106 // Rendering context to use for measurement.
107 gfxContext* mRenderingContext;
109 nsMargin ComputedPhysicalMargin() const {
110 return mComputedMargin.GetPhysicalMargin(mWritingMode);
112 nsMargin ComputedPhysicalBorderPadding() const {
113 return mComputedBorderPadding.GetPhysicalMargin(mWritingMode);
115 nsMargin ComputedPhysicalBorder() const {
116 return ComputedLogicalBorder(mWritingMode).GetPhysicalMargin(mWritingMode);
118 nsMargin ComputedPhysicalPadding() const {
119 return mComputedPadding.GetPhysicalMargin(mWritingMode);
122 mozilla::LogicalMargin ComputedLogicalMargin(mozilla::WritingMode aWM) const {
123 return mComputedMargin.ConvertTo(aWM, mWritingMode);
125 mozilla::LogicalMargin ComputedLogicalBorderPadding(
126 mozilla::WritingMode aWM) const {
127 return mComputedBorderPadding.ConvertTo(aWM, mWritingMode);
129 mozilla::LogicalMargin ComputedLogicalPadding(
130 mozilla::WritingMode aWM) const {
131 return mComputedPadding.ConvertTo(aWM, mWritingMode);
133 mozilla::LogicalMargin ComputedLogicalBorder(mozilla::WritingMode aWM) const {
134 return (mComputedBorderPadding - mComputedPadding)
135 .ConvertTo(aWM, mWritingMode);
138 void SetComputedLogicalMargin(mozilla::WritingMode aWM,
139 const mozilla::LogicalMargin& aMargin) {
140 mComputedMargin = aMargin.ConvertTo(mWritingMode, aWM);
142 void SetComputedLogicalBorderPadding(
143 mozilla::WritingMode aWM, const mozilla::LogicalMargin& aBorderPadding) {
144 mComputedBorderPadding = aBorderPadding.ConvertTo(mWritingMode, aWM);
146 void SetComputedLogicalPadding(mozilla::WritingMode aWM,
147 const mozilla::LogicalMargin& aPadding) {
148 mComputedPadding = aPadding.ConvertTo(mWritingMode, aWM);
151 mozilla::WritingMode GetWritingMode() const { return mWritingMode; }
153 protected:
154 // cached copy of the frame's writing-mode, for logical coordinates
155 const mozilla::WritingMode mWritingMode;
157 // Cached mFrame->IsThemed().
158 const bool mIsThemed = false;
160 // Computed margin values
161 mozilla::LogicalMargin mComputedMargin;
163 // Cached copy of the border + padding values
164 mozilla::LogicalMargin mComputedBorderPadding;
166 // Computed padding values
167 mozilla::LogicalMargin mComputedPadding;
169 public:
170 // Callers using this constructor must call InitOffsets on their own.
171 SizeComputationInput(nsIFrame* aFrame, gfxContext* aRenderingContext);
173 SizeComputationInput(nsIFrame* aFrame, gfxContext* aRenderingContext,
174 mozilla::WritingMode aContainingBlockWritingMode,
175 nscoord aContainingBlockISize,
176 const mozilla::Maybe<mozilla::LogicalMargin>& aBorder =
177 mozilla::Nothing(),
178 const mozilla::Maybe<mozilla::LogicalMargin>& aPadding =
179 mozilla::Nothing());
181 private:
183 * Computes margin values from the specified margin style information, and
184 * fills in the mComputedMargin member.
186 * @param aCBWM Writing mode of the containing block
187 * @param aPercentBasis
188 * Inline size of the containing block (in its own writing mode), to use
189 * for resolving percentage margin values in the inline and block axes.
190 * @return true if the margin is dependent on the containing block size.
192 bool ComputeMargin(mozilla::WritingMode aCBWM, nscoord aPercentBasis,
193 mozilla::LayoutFrameType aFrameType);
196 * Computes padding values from the specified padding style information, and
197 * fills in the mComputedPadding member.
199 * @param aCBWM Writing mode of the containing block
200 * @param aPercentBasis
201 * Inline size of the containing block (in its own writing mode), to use
202 * for resolving percentage padding values in the inline and block axes.
203 * @return true if the padding is dependent on the containing block size.
205 bool ComputePadding(mozilla::WritingMode aCBWM, nscoord aPercentBasis,
206 mozilla::LayoutFrameType aFrameType);
208 protected:
209 void InitOffsets(mozilla::WritingMode aCBWM, nscoord aPercentBasis,
210 mozilla::LayoutFrameType aFrameType,
211 mozilla::ComputeSizeFlags aFlags,
212 const mozilla::Maybe<mozilla::LogicalMargin>& aBorder,
213 const mozilla::Maybe<mozilla::LogicalMargin>& aPadding,
214 const nsStyleDisplay* aDisplay = nullptr);
217 * Convert StyleSize or StyleMaxSize to nscoord when percentages depend on the
218 * inline size of the containing block, and enumerated values are for inline
219 * size, min-inline-size, or max-inline-size. Does not handle auto inline
220 * sizes.
222 template <typename SizeOrMaxSize>
223 inline nscoord ComputeISizeValue(const LogicalSize& aContainingBlockSize,
224 mozilla::StyleBoxSizing aBoxSizing,
225 const SizeOrMaxSize&) const;
227 nscoord ComputeBSizeValue(nscoord aContainingBlockBSize,
228 mozilla::StyleBoxSizing aBoxSizing,
229 const mozilla::LengthPercentage& aCoord) const;
233 * State passed to a frame during reflow or intrinsic size calculation.
235 * XXX Refactor so only a base class (nsSizingState?) is used for intrinsic
236 * size calculation.
238 * @see nsIFrame#Reflow()
240 struct ReflowInput : public SizeComputationInput {
241 // the reflow inputs are linked together. this is the pointer to the
242 // parent's reflow input
243 const ReflowInput* mParentReflowInput = nullptr;
245 // A non-owning pointer to the float manager associated with this area,
246 // which points to the object owned by nsAutoFloatManager::mNew.
247 nsFloatManager* mFloatManager = nullptr;
249 // LineLayout object (only for inline reflow; set to nullptr otherwise)
250 nsLineLayout* mLineLayout = nullptr;
252 // The appropriate reflow input for the containing block (for
253 // percentage widths, etc.) of this reflow input's frame. It will be setup
254 // properly in InitCBReflowInput().
255 const ReflowInput* mCBReflowInput = nullptr;
257 // The amount the in-flow position of the block is moving vertically relative
258 // to its previous in-flow position (i.e. the amount the line containing the
259 // block is moving).
260 // This should be zero for anything which is not a block outside, and it
261 // should be zero for anything which has a non-block parent.
262 // The intended use of this value is to allow the accurate determination
263 // of the potential impact of a float
264 // This takes on an arbitrary value the first time a block is reflowed
265 nscoord mBlockDelta = 0;
267 // If a ReflowInput finds itself initialized with an unconstrained
268 // inline-size, it will look up its parentReflowInput chain for a reflow input
269 // with an orthogonal writing mode and a non-NS_UNCONSTRAINEDSIZE value for
270 // orthogonal limit; when it finds such a reflow input, it will use its
271 // orthogonal-limit value to constrain inline-size.
272 // This is initialized to NS_UNCONSTRAINEDSIZE (so it will be ignored),
273 // but reset to a suitable value for the reflow root by PresShell.
274 nscoord mOrthogonalLimit = NS_UNCONSTRAINEDSIZE;
276 // Physical accessors for the private fields. They are needed for
277 // compatibility with not-yet-updated code. New code should use the accessors
278 // for logical coordinates, unless the code really works on physical
279 // coordinates.
280 nscoord AvailableWidth() const { return mAvailableSize.Width(mWritingMode); }
281 nscoord AvailableHeight() const {
282 return mAvailableSize.Height(mWritingMode);
284 nscoord ComputedWidth() const { return mComputedSize.Width(mWritingMode); }
285 nscoord ComputedHeight() const { return mComputedSize.Height(mWritingMode); }
286 nscoord ComputedMinWidth() const {
287 return mComputedMinSize.Width(mWritingMode);
289 nscoord ComputedMaxWidth() const {
290 return mComputedMaxSize.Width(mWritingMode);
292 nscoord ComputedMinHeight() const {
293 return mComputedMinSize.Height(mWritingMode);
295 nscoord ComputedMaxHeight() const {
296 return mComputedMaxSize.Height(mWritingMode);
299 // Logical accessors for private fields in mWritingMode.
300 nscoord AvailableISize() const { return mAvailableSize.ISize(mWritingMode); }
301 nscoord AvailableBSize() const { return mAvailableSize.BSize(mWritingMode); }
302 nscoord ComputedISize() const { return mComputedSize.ISize(mWritingMode); }
303 nscoord ComputedBSize() const { return mComputedSize.BSize(mWritingMode); }
304 nscoord ComputedMinISize() const {
305 return mComputedMinSize.ISize(mWritingMode);
307 nscoord ComputedMaxISize() const {
308 return mComputedMaxSize.ISize(mWritingMode);
310 nscoord ComputedMinBSize() const {
311 return mComputedMinSize.BSize(mWritingMode);
313 nscoord ComputedMaxBSize() const {
314 return mComputedMaxSize.BSize(mWritingMode);
317 // WARNING: In general, adjusting available inline-size or block-size is not
318 // safe because ReflowInput has members whose values depend on the available
319 // size passing through the constructor. For example,
320 // CalculateBlockSideMargins() is called during initialization, and uses
321 // AvailableSize(). Make sure your use case doesn't lead to stale member
322 // values in ReflowInput!
323 void SetAvailableISize(nscoord aAvailableISize) {
324 mAvailableSize.ISize(mWritingMode) = aAvailableISize;
326 void SetAvailableBSize(nscoord aAvailableBSize) {
327 mAvailableSize.BSize(mWritingMode) = aAvailableBSize;
330 void SetComputedMinISize(nscoord aMinISize) {
331 mComputedMinSize.ISize(mWritingMode) = aMinISize;
333 void SetComputedMaxISize(nscoord aMaxISize) {
334 mComputedMaxSize.ISize(mWritingMode) = aMaxISize;
336 void SetComputedMinBSize(nscoord aMinBSize) {
337 mComputedMinSize.BSize(mWritingMode) = aMinBSize;
339 void SetComputedMaxBSize(nscoord aMaxBSize) {
340 mComputedMaxSize.BSize(mWritingMode) = aMaxBSize;
342 void SetPercentageBasisInBlockAxis(nscoord aBSize) {
343 mPercentageBasisInBlockAxis = Some(aBSize);
346 mozilla::LogicalSize AvailableSize() const { return mAvailableSize; }
347 mozilla::LogicalSize ComputedSize() const { return mComputedSize; }
349 template <typename F>
350 mozilla::LogicalSize ComputedSizeWithBSizeFallback(F&& aFallback) const {
351 auto size = mComputedSize;
352 if (size.BSize(mWritingMode) == NS_UNCONSTRAINEDSIZE) {
353 size.BSize(mWritingMode) = ApplyMinMaxBSize(aFallback());
355 return size;
358 mozilla::LogicalSize ComputedMinSize() const { return mComputedMinSize; }
359 mozilla::LogicalSize ComputedMaxSize() const { return mComputedMaxSize; }
361 mozilla::LogicalSize AvailableSize(mozilla::WritingMode aWM) const {
362 return AvailableSize().ConvertTo(aWM, mWritingMode);
364 mozilla::LogicalSize ComputedSize(mozilla::WritingMode aWM) const {
365 return ComputedSize().ConvertTo(aWM, mWritingMode);
367 mozilla::LogicalSize ComputedMinSize(mozilla::WritingMode aWM) const {
368 return ComputedMinSize().ConvertTo(aWM, mWritingMode);
370 mozilla::LogicalSize ComputedMaxSize(mozilla::WritingMode aWM) const {
371 return ComputedMaxSize().ConvertTo(aWM, mWritingMode);
374 mozilla::LogicalSize ComputedSizeWithPadding(mozilla::WritingMode aWM) const {
375 return ComputedSize(aWM) + ComputedLogicalPadding(aWM).Size(aWM);
378 mozilla::LogicalSize ComputedSizeWithBorderPadding(
379 mozilla::WritingMode aWM) const {
380 return ComputedSize(aWM) + ComputedLogicalBorderPadding(aWM).Size(aWM);
383 mozilla::LogicalSize ComputedSizeWithMarginBorderPadding(
384 mozilla::WritingMode aWM) const {
385 return ComputedSizeWithBorderPadding(aWM) +
386 ComputedLogicalMargin(aWM).Size(aWM);
389 nsSize ComputedPhysicalSize() const {
390 return mComputedSize.GetPhysicalSize(mWritingMode);
393 nsMargin ComputedPhysicalOffsets() const {
394 return mComputedOffsets.GetPhysicalMargin(mWritingMode);
397 LogicalMargin ComputedLogicalOffsets(mozilla::WritingMode aWM) const {
398 return mComputedOffsets.ConvertTo(aWM, mWritingMode);
401 void SetComputedLogicalOffsets(mozilla::WritingMode aWM,
402 const LogicalMargin& aOffsets) {
403 mComputedOffsets = aOffsets.ConvertTo(mWritingMode, aWM);
406 // Return ReflowInput's computed size including border-padding, with
407 // unconstrained dimensions replaced by zero.
408 nsSize ComputedSizeAsContainerIfConstrained() const;
410 // Our saved containing block dimensions.
411 LogicalSize mContainingBlockSize{mWritingMode};
413 // Cached pointers to the various style structs used during initialization.
414 const nsStyleDisplay* mStyleDisplay = nullptr;
415 const nsStylePosition* mStylePosition = nullptr;
416 const nsStyleBorder* mStyleBorder = nullptr;
417 const nsStyleMargin* mStyleMargin = nullptr;
419 enum class BreakType : uint8_t {
420 Auto,
421 Column,
422 Page,
424 BreakType mBreakType = BreakType::Auto;
426 // a frame (e.g. nsTableCellFrame) which may need to generate a special
427 // reflow for percent bsize calculations
428 nsIPercentBSizeObserver* mPercentBSizeObserver = nullptr;
430 // CSS margin collapsing sometimes requires us to reflow
431 // optimistically assuming that margins collapse to see if clearance
432 // is required. When we discover that clearance is required, we
433 // store the frame in which clearance was discovered to the location
434 // requested here.
435 nsIFrame** mDiscoveredClearance = nullptr;
437 struct Flags {
438 Flags() { memset(this, 0, sizeof(*this)); }
440 // Cached mFrame->IsReplaced().
441 bool mIsReplaced : 1;
443 // used by tables to communicate special reflow (in process) to handle
444 // percent bsize frames inside cells which may not have computed bsizes
445 bool mSpecialBSizeReflow : 1;
447 // nothing in the frame's next-in-flow (or its descendants) is changing
448 bool mNextInFlowUntouched : 1;
450 // Is the current context at the top of a page? When true, we force
451 // something that's too tall for a page/column to fit anyway to avoid
452 // infinite loops.
453 bool mIsTopOfPage : 1;
455 // parent frame is an ScrollContainerFrame and it is assuming a horizontal
456 // scrollbar
457 bool mAssumingHScrollbar : 1;
459 // parent frame is an ScrollContainerFrame and it is assuming a vertical
460 // scrollbar
461 bool mAssumingVScrollbar : 1;
463 // Is frame a different inline-size than before?
464 bool mIsIResize : 1;
466 // Is frame (potentially) a different block-size than before?
467 // This includes cases where the block-size is 'auto' and the
468 // contents or width have changed.
469 bool mIsBResize : 1;
471 // Has this frame changed block-size in a way that affects
472 // block-size percentages on frames for which it is the containing
473 // block? This includes a change between 'auto' and a length that
474 // doesn't actually change the frame's block-size. It does not
475 // include cases where the block-size is 'auto' and the frame's
476 // contents have changed.
478 // In the current code, this is only true when mIsBResize is also
479 // true, although it doesn't necessarily need to be that way (e.g.,
480 // in the case of a frame changing from 'auto' to a length that
481 // produces the same height).
482 bool mIsBResizeForPercentages : 1;
484 // tables are splittable, this should happen only inside a page and never
485 // insider a column frame
486 bool mTableIsSplittable : 1;
488 // Does frame height depend on an ancestor table-cell?
489 bool mHeightDependsOnAncestorCell : 1;
491 // nsColumnSetFrame is balancing columns
492 bool mIsColumnBalancing : 1;
494 // We have an ancestor nsColumnSetFrame performing the last column balancing
495 // reflow. The available block-size of the last column might become
496 // unconstrained.
497 bool mIsInLastColumnBalancingReflow : 1;
499 // True if ColumnSetWrapperFrame has a constrained block-size, and is going
500 // to consume all of its block-size in this fragment. This bit is passed to
501 // nsColumnSetFrame to determine whether to give up balancing and create
502 // overflow columns.
503 bool mColumnSetWrapperHasNoBSizeLeft : 1;
505 // If this flag is set, the BSize of this frame should be considered
506 // indefinite for the purposes of percent resolution on child frames (we
507 // should behave as if ComputedBSize() were NS_UNCONSTRAINEDSIZE when doing
508 // percent resolution against this.ComputedBSize()). For example: flex
509 // items may have their ComputedBSize() resolved ahead-of-time by their
510 // flex container, and yet their BSize might have to be considered
511 // indefinite per https://drafts.csswg.org/css-flexbox/#definite-sizes
512 bool mTreatBSizeAsIndefinite : 1;
514 // a "fake" reflow input made in order to be the parent of a real one
515 bool mDummyParentReflowInput : 1;
517 // Should this frame reflow its place-holder children? If the available
518 // height of this frame didn't change, but its in a paginated environment
519 // (e.g. columns), it should always reflow its placeholder children.
520 bool mMustReflowPlaceholders : 1;
522 // the STATIC_POS_IS_CB_ORIGIN ctor flag
523 bool mStaticPosIsCBOrigin : 1;
525 // If set, the following two flags indicate that:
526 // (1) this frame is absolutely-positioned (or fixed-positioned).
527 // (2) this frame's static position depends on the CSS Box Alignment.
528 // (3) we do need to compute the static position, because the frame's
529 // {Inline and/or Block} offsets actually depend on it.
530 // When these bits are set, the offset values (IStart/IEnd, BStart/BEnd)
531 // represent the "start" edge of the frame's CSS Box Alignment container
532 // area, in that axis -- and these offsets need to be further-resolved
533 // (with CSS Box Alignment) after we know the OOF frame's size.
534 // NOTE: The "I" and "B" (for "Inline" and "Block") refer the axes of the
535 // *containing block's writing-mode*, NOT mFrame's own writing-mode. This
536 // is purely for convenience, since that's the writing-mode we're dealing
537 // with when we set & react to these bits.
538 bool mIOffsetsNeedCSSAlign : 1;
539 bool mBOffsetsNeedCSSAlign : 1;
541 // Is this frame or one of its ancestors being reflowed in a different
542 // continuation than the one in which it was previously reflowed? In
543 // other words, has it moved to a different column or page than it was in
544 // the previous reflow?
546 // FIXME: For now, we only ensure that this is set correctly for blocks.
547 // This is okay because the only thing that uses it only cares about
548 // whether there's been a fragment change within the same block formatting
549 // context.
550 bool mMovedBlockFragments : 1;
552 // Is the block-size computed by aspect-ratio and inline size (i.e. block
553 // axis is the ratio-dependent axis)? We set this flag so that we can check
554 // whether to apply automatic content-based minimum sizes once we know the
555 // children's block-size (after reflowing them).
556 // https://drafts.csswg.org/css-sizing-4/#aspect-ratio-minimum
557 bool mIsBSizeSetByAspectRatio : 1;
559 // If true, then children of this frame can generate class A breakpoints
560 // for paginated reflow.
561 bool mCanHaveClassABreakpoints : 1;
563 Flags mFlags;
565 mozilla::StyleSizeOverrides mStyleSizeOverrides;
567 mozilla::ComputeSizeFlags mComputeSizeFlags;
569 // This value keeps track of how deeply nested a given reflow input
570 // is from the top of the frame tree.
571 int16_t mReflowDepth = 0;
573 // Logical and physical accessors for the resize flags.
574 bool IsHResize() const {
575 return mWritingMode.IsVertical() ? mFlags.mIsBResize : mFlags.mIsIResize;
577 bool IsVResize() const {
578 return mWritingMode.IsVertical() ? mFlags.mIsIResize : mFlags.mIsBResize;
580 bool IsIResize() const { return mFlags.mIsIResize; }
581 bool IsBResize() const { return mFlags.mIsBResize; }
582 bool IsBResizeForWM(mozilla::WritingMode aWM) const {
583 return aWM.IsOrthogonalTo(mWritingMode) ? mFlags.mIsIResize
584 : mFlags.mIsBResize;
586 bool IsBResizeForPercentagesForWM(mozilla::WritingMode aWM) const {
587 // This uses the relatively-accurate mIsBResizeForPercentages flag
588 // when the writing modes are parallel, and is a bit more
589 // pessimistic when orthogonal.
590 return !aWM.IsOrthogonalTo(mWritingMode) ? mFlags.mIsBResizeForPercentages
591 : IsIResize();
593 void SetHResize(bool aValue) {
594 if (mWritingMode.IsVertical()) {
595 mFlags.mIsBResize = aValue;
596 } else {
597 mFlags.mIsIResize = aValue;
600 void SetVResize(bool aValue) {
601 if (mWritingMode.IsVertical()) {
602 mFlags.mIsIResize = aValue;
603 } else {
604 mFlags.mIsBResize = aValue;
607 void SetIResize(bool aValue) { mFlags.mIsIResize = aValue; }
608 void SetBResize(bool aValue) { mFlags.mIsBResize = aValue; }
610 // Values for |aFlags| passed to constructor
611 enum class InitFlag : uint8_t {
612 // Indicates that the parent of this reflow input is "fake" (see
613 // mDummyParentReflowInput in mFlags).
614 DummyParentReflowInput,
616 // Indicates that the calling function will initialize the reflow input, and
617 // that the constructor should not call Init().
618 CallerWillInit,
620 // The caller wants the abs.pos. static-position resolved at the origin of
621 // the containing block, i.e. at LogicalPoint(0, 0). (Note that this
622 // doesn't necessarily mean that (0, 0) is the *correct* static position
623 // for the frame in question.)
624 // @note In a Grid container's masonry axis we'll always use
625 // the placeholder's position in that axis regardless of this flag.
626 StaticPosIsCBOrigin,
628 using InitFlags = mozilla::EnumSet<InitFlag>;
630 // Note: The copy constructor is written by the compiler automatically. You
631 // can use that and then override specific values if you want, or you can
632 // call Init as desired...
635 * Initialize a ROOT reflow input.
637 * @param aPresContext Must be equal to aFrame->PresContext().
638 * @param aFrame The frame for whose reflow input is being constructed.
639 * @param aRenderingContext The rendering context to be used for measurements.
640 * @param aAvailableSpace The available space to reflow aFrame (in aFrame's
641 * writing-mode). See comments for mAvailableSize for more information.
642 * @param aFlags A set of flags used for additional boolean parameters (see
643 * InitFlags above).
645 ReflowInput(nsPresContext* aPresContext, nsIFrame* aFrame,
646 gfxContext* aRenderingContext,
647 const mozilla::LogicalSize& aAvailableSpace,
648 InitFlags aFlags = {});
651 * Initialize a reflow input for a child frame's reflow. Some parts of the
652 * state are copied from the parent's reflow input. The remainder is computed.
654 * @param aPresContext Must be equal to aFrame->PresContext().
655 * @param aParentReflowInput A reference to an ReflowInput object that
656 * is to be the parent of this object.
657 * @param aFrame The frame for whose reflow input is being constructed.
658 * @param aAvailableSpace The available space to reflow aFrame (in aFrame's
659 * writing-mode). See comments for mAvailableSize for more information.
660 * @param aContainingBlockSize An optional size (in aFrame's writing mode),
661 * specifying the containing block size to use instead of the default
662 * size computed by ComputeContainingBlockRectangle(). If
663 * InitFlag::CallerWillInit is used, this is ignored. Pass it via
664 * Init() instead.
665 * @param aFlags A set of flags used for additional boolean parameters (see
666 * InitFlags above).
667 * @param aStyleSizeOverrides The style data used to override mFrame's when we
668 * call nsIFrame::ComputeSize() internally.
669 * @param aComputeSizeFlags A set of flags used when we call
670 * nsIFrame::ComputeSize() internally.
672 ReflowInput(nsPresContext* aPresContext,
673 const ReflowInput& aParentReflowInput, nsIFrame* aFrame,
674 const mozilla::LogicalSize& aAvailableSpace,
675 const mozilla::Maybe<mozilla::LogicalSize>& aContainingBlockSize =
676 mozilla::Nothing(),
677 InitFlags aFlags = {},
678 const mozilla::StyleSizeOverrides& aSizeOverrides = {},
679 mozilla::ComputeSizeFlags aComputeSizeFlags = {});
682 * This method initializes various data members. It is automatically called by
683 * the constructors if InitFlags::CallerWillInit is *not* used.
685 * @param aContainingBlockSize An optional size (in mFrame's writing mode),
686 * specifying the containing block size to use instead of the default
687 * size computed by ComputeContainingBlockRectangle().
688 * @param aBorder An optional border (in mFrame's writing mode). If given, use
689 * it instead of the border computed from mFrame's StyleBorder.
690 * @param aPadding An optional padding (in mFrame's writing mode). If given,
691 * use it instead of the padding computing from mFrame's StylePadding.
693 void Init(nsPresContext* aPresContext,
694 const mozilla::Maybe<mozilla::LogicalSize>& aContainingBlockSize =
695 mozilla::Nothing(),
696 const mozilla::Maybe<mozilla::LogicalMargin>& aBorder =
697 mozilla::Nothing(),
698 const mozilla::Maybe<mozilla::LogicalMargin>& aPadding =
699 mozilla::Nothing());
702 * Get the used line-height property. The return value will be >= 0.
704 nscoord GetLineHeight() const;
707 * Set the used line-height. aLineHeight must be >= 0.
709 void SetLineHeight(nscoord aLineHeight);
712 * Calculate the used line-height property without a reflow input instance.
713 * The return value will be >= 0.
715 * @param aBlockBSize The computed block size of the content rect of the block
716 * that the line should fill. Only used with
717 * line-height:-moz-block-height. NS_UNCONSTRAINEDSIZE
718 * results in a normal line-height for
719 * line-height:-moz-block-height.
720 * @param aFontSizeInflation The result of the appropriate
721 * nsLayoutUtils::FontSizeInflationFor call,
722 * or 1.0 if during intrinsic size
723 * calculation.
725 static nscoord CalcLineHeight(const ComputedStyle&,
726 nsPresContext* aPresContext,
727 const nsIContent* aContent, nscoord aBlockBSize,
728 float aFontSizeInflation);
730 static nscoord CalcLineHeight(const StyleLineHeight&,
731 const nsStyleFont& aRelativeToFont,
732 nsPresContext* aPresContext, bool aIsVertical,
733 const nsIContent* aContent, nscoord aBlockBSize,
734 float aFontSizeInflation);
736 static nscoord CalcLineHeightForCanvas(const StyleLineHeight& aLh,
737 const nsFont& aRelativeToFont,
738 nsAtom* aLanguage,
739 bool aExplicitLanguage,
740 nsPresContext* aPresContext,
741 mozilla::WritingMode aWM);
743 static constexpr float kNormalLineHeightFactor = 1.2f;
745 mozilla::LogicalSize ComputeContainingBlockRectangle(
746 nsPresContext* aPresContext, const ReflowInput* aContainingBlockRI) const;
749 * Apply the mComputed(Min/Max)ISize constraints to the content
750 * size computed so far.
752 nscoord ApplyMinMaxISize(nscoord aISize) const {
753 if (NS_UNCONSTRAINEDSIZE != ComputedMaxISize()) {
754 aISize = std::min(aISize, ComputedMaxISize());
756 return std::max(aISize, ComputedMinISize());
760 * Apply the mComputed(Min/Max)BSize constraints to the content
761 * size computed so far.
763 * @param aBSize The block-size that we've computed an to which we want to
764 * apply min/max constraints.
765 * @param aConsumed The amount of the computed block-size that was consumed by
766 * our prev-in-flows.
768 nscoord ApplyMinMaxBSize(nscoord aBSize, nscoord aConsumed = 0) const {
769 aBSize += aConsumed;
771 if (NS_UNCONSTRAINEDSIZE != ComputedMaxBSize()) {
772 aBSize = std::min(aBSize, ComputedMaxBSize());
775 if (NS_UNCONSTRAINEDSIZE != ComputedMinBSize()) {
776 aBSize = std::max(aBSize, ComputedMinBSize());
779 return aBSize - aConsumed;
782 bool ShouldReflowAllKids() const;
784 // This method doesn't apply min/max computed widths to the value passed in.
785 void SetComputedWidth(nscoord aComputedWidth) {
786 if (mWritingMode.IsVertical()) {
787 SetComputedBSize(aComputedWidth);
788 } else {
789 SetComputedISize(aComputedWidth);
793 // This method doesn't apply min/max computed heights to the value passed in.
794 void SetComputedHeight(nscoord aComputedHeight) {
795 if (mWritingMode.IsVertical()) {
796 SetComputedISize(aComputedHeight);
797 } else {
798 SetComputedBSize(aComputedHeight);
802 // Use "No" to request SetComputedISize/SetComputedBSize not to reset resize
803 // flags.
804 enum class ResetResizeFlags : bool { No, Yes };
806 // This method doesn't apply min/max computed inline-sizes to the value passed
807 // in.
808 void SetComputedISize(nscoord aComputedISize,
809 ResetResizeFlags aFlags = ResetResizeFlags::Yes);
811 // These methods don't apply min/max computed block-sizes to the value passed
812 // in.
813 void SetComputedBSize(nscoord aComputedBSize,
814 ResetResizeFlags aFlags = ResetResizeFlags::Yes);
816 bool WillReflowAgainForClearance() const {
817 return mDiscoveredClearance && *mDiscoveredClearance;
820 // Returns true if we should apply automatic minimum on the block axis.
822 // The automatic minimum size in the ratio-dependent axis of a box with a
823 // preferred aspect ratio that is neither a replaced element nor a scroll
824 // container is its min-content size clamped from above by its maximum size.
826 // https://drafts.csswg.org/css-sizing-4/#aspect-ratio-minimum
827 bool ShouldApplyAutomaticMinimumOnBlockAxis() const;
829 // Returns true if mFrame has a constrained available block-size, or if mFrame
830 // is a continuation. When this method returns true, mFrame can be considered
831 // to be in a "fragmented context."
833 // Note: this method usually returns true when mFrame is in a paged
834 // environment (e.g. printing) or has a multi-column container ancestor.
835 // However, this doesn't include several cases when we're intentionally
836 // performing layout in a fragmentation-ignoring way, e.g. 1) mFrame is a flex
837 // or grid item, and this ReflowInput is for a measuring reflow with an
838 // unconstrained available block-size, or 2) mFrame is (or is inside of) an
839 // element that forms an orthogonal writing-mode.
840 bool IsInFragmentedContext() const;
842 // Compute the offsets for a relative position element
844 // @param aWM the writing mode of aCBSize and the returned offsets.
845 static mozilla::LogicalMargin ComputeRelativeOffsets(
846 mozilla::WritingMode aWM, nsIFrame* aFrame,
847 const mozilla::LogicalSize& aCBSize);
849 // If aFrame is a relatively or sticky positioned element, adjust aPosition
850 // appropriately.
852 // @param aComputedOffsets aFrame's relative offset, either from the cached
853 // nsIFrame::ComputedOffsetProperty() or ComputedPhysicalOffsets().
854 // Note: This parameter is used only when aFrame is relatively
855 // positioned, not sticky positioned.
856 // @param aPosition [in/out] Pass aFrame's normal position (pre-relative
857 // positioning), and this method will update it to indicate aFrame's
858 // actual position.
859 static void ApplyRelativePositioning(nsIFrame* aFrame,
860 const nsMargin& aComputedOffsets,
861 nsPoint* aPosition);
863 static void ApplyRelativePositioning(
864 nsIFrame* aFrame, mozilla::WritingMode aWritingMode,
865 const mozilla::LogicalMargin& aComputedOffsets,
866 mozilla::LogicalPoint* aPosition, const nsSize& aContainerSize);
868 // Resolve any block-axis 'auto' margins (if any) for an absolutely positioned
869 // frame. aMargin and aOffsets are both outparams (though we only touch
870 // aOffsets if the position is overconstrained)
871 static void ComputeAbsPosBlockAutoMargin(nscoord aAvailMarginSpace,
872 WritingMode aContainingBlockWM,
873 bool aIsMarginBStartAuto,
874 bool aIsMarginBEndAuto,
875 LogicalMargin& aMargin,
876 LogicalMargin& aOffsets);
878 // Resolve any inline-axis 'auto' margins (if any) for an absolutely
879 // positioned frame. aMargin and aOffsets are both outparams (though we only
880 // touch aOffsets if the position is overconstrained)
881 static void ComputeAbsPosInlineAutoMargin(nscoord aAvailMarginSpace,
882 WritingMode aContainingBlockWM,
883 bool aIsMarginIStartAuto,
884 bool aIsMarginIEndAuto,
885 LogicalMargin& aMargin,
886 LogicalMargin& aOffsets);
888 protected:
889 void InitCBReflowInput();
890 void InitResizeFlags(nsPresContext* aPresContext,
891 mozilla::LayoutFrameType aFrameType);
892 void InitDynamicReflowRoot();
894 void InitConstraints(
895 nsPresContext* aPresContext,
896 const mozilla::Maybe<mozilla::LogicalSize>& aContainingBlockSize,
897 const mozilla::Maybe<mozilla::LogicalMargin>& aBorder,
898 const mozilla::Maybe<mozilla::LogicalMargin>& aPadding,
899 mozilla::LayoutFrameType aFrameType);
901 // Returns the nearest containing block or block frame (whether or not
902 // it is a containing block) for the specified frame. Also returns
903 // the inline-start edge and logical size of the containing block's
904 // content area.
905 // These are returned in the coordinate space of the containing block.
906 nsIFrame* GetHypotheticalBoxContainer(nsIFrame* aFrame,
907 nscoord& aCBIStartEdge,
908 mozilla::LogicalSize& aCBSize) const;
910 // Calculate the position of the hypothetical box that the placeholder frame
911 // (for a position:fixed/absolute element) would have if it were in the flow
912 // (i.e., positioned statically).
914 // The position of the hypothetical box is relative to the padding edge of the
915 // absolute containing block (aCBReflowInput->mFrame). The writing mode of the
916 // hypothetical box will have the same block direction as the absolute
917 // containing block, but it may differ in the inline direction.
918 void CalculateHypotheticalPosition(
919 nsPlaceholderFrame* aPlaceholderFrame, const ReflowInput* aCBReflowInput,
920 nsHypotheticalPosition& aHypotheticalPos) const;
922 // Check if we can use the resolved auto block size (by insets) to compute
923 // the inline size through aspect-ratio on absolute-positioned elements.
924 // This is only needed for non-replaced elements.
925 // https://drafts.csswg.org/css-position/#abspos-auto-size
926 bool IsInlineSizeComputableByBlockSizeAndAspectRatio(
927 nscoord aBlockSize) const;
929 // This calculates the size by using the resolved auto block size (from
930 // non-auto block insets), according to the writing mode of current block.
931 LogicalSize CalculateAbsoluteSizeWithResolvedAutoBlockSize(
932 nscoord aAutoBSize, const LogicalSize& aTentativeComputedSize);
934 void InitAbsoluteConstraints(const ReflowInput* aCBReflowInput,
935 const LogicalSize& aCBSize);
937 // Calculates the computed values for the 'min-inline-size',
938 // 'max-inline-size', 'min-block-size', and 'max-block-size' properties, and
939 // stores them in the assorted data members
940 void ComputeMinMaxValues(const mozilla::LogicalSize& aCBSize);
942 // aInsideBoxSizing returns the part of the padding, border, and margin
943 // in the aAxis dimension that goes inside the edge given by box-sizing;
944 // aOutsideBoxSizing returns the rest.
945 void CalculateBorderPaddingMargin(mozilla::LogicalAxis aAxis,
946 nscoord aContainingBlockSize,
947 nscoord* aInsideBoxSizing,
948 nscoord* aOutsideBoxSizing) const;
950 void CalculateBlockSideMargins();
953 * @return true if mFrame is an internal table frame, i.e. an
954 * ns[RowGroup|ColGroup|Row|Cell]Frame. (We exclude nsTableColFrame
955 * here since we never setup a ReflowInput for those.)
957 bool IsInternalTableFrame() const;
959 private:
960 // The available size in which to reflow the frame. The space represents the
961 // amount of room for the frame's margin, border, padding, and content area.
963 // The available inline-size should be constrained. The frame's inline-size
964 // you choose should fit within it.
966 // In galley mode, the available block-size is always unconstrained, and only
967 // page mode or multi-column layout involves a constrained available
968 // block-size.
970 // An unconstrained available block-size means you can choose whatever size
971 // you want. If the value is constrained, the frame's block-start border,
972 // padding, and content, must fit. If a frame is fully-complete after reflow,
973 // then its block-end border, padding, and margin (and similar for its
974 // fully-complete ancestors) will need to fit within this available
975 // block-size. However, if a frame is monolithic, it may choose a block-size
976 // larger than the available block-size.
977 mozilla::LogicalSize mAvailableSize{mWritingMode};
979 // The computed size specifies the frame's content area, and it does not apply
980 // to inline non-replaced elements.
982 // For block-level frames, the computed inline-size is based on the
983 // inline-size of the containing block, the margin/border/padding areas, and
984 // the min/max inline-size.
986 // For non-replaced block-level frames in the flow and floated, if the
987 // computed block-size is NS_UNCONSTRAINEDSIZE, you should choose a block-size
988 // to shrink wrap around the normal flow child frames. The block-size must be
989 // within the limit of the min/max block-size if there is such a limit.
990 mozilla::LogicalSize mComputedSize{mWritingMode};
992 // Computed values for 'inset' properties. Only applies to 'positioned'
993 // elements.
994 mozilla::LogicalMargin mComputedOffsets{mWritingMode};
996 // Computed value for 'min-inline-size'/'min-block-size'.
997 mozilla::LogicalSize mComputedMinSize{mWritingMode};
999 // Computed value for 'max-inline-size'/'max-block-size'.
1000 mozilla::LogicalSize mComputedMaxSize{mWritingMode, NS_UNCONSTRAINEDSIZE,
1001 NS_UNCONSTRAINEDSIZE};
1003 // Percentage basis in the block axis for the purpose of percentage resolution
1004 // on children.
1006 // This will be ignored when mTreatBSizeAsIndefinite flag is true, or when a
1007 // customized containing block size is provided via ReflowInput's constructor
1008 // or Init(). When this percentage basis exists, it will be used to replace
1009 // the containing block's ComputedBSize() in
1010 // ComputeContainingBlockRectangle().
1012 // This is currently used in a special scenario where we treat certain
1013 // sized-to-content flex items as having an 'auto' block-size for their final
1014 // reflow to accomodate fragmentation-imposed block-size growth. This sort of
1015 // flex item does nonetheless have a known block-size (from the flex layout
1016 // algorithm) that it needs to use as a definite percentage-basis for its
1017 // children during its final reflow; and we represent that here.
1018 Maybe<nscoord> mPercentageBasisInBlockAxis;
1020 // Cache the used line-height property.
1021 mutable nscoord mLineHeight = NS_UNCONSTRAINEDSIZE;
1024 } // namespace mozilla
1026 #endif // mozilla_ReflowInput_h