mirror of
https://github.com/mozilla/gecko-dev.git
synced 2025-02-04 13:07:52 +00:00
Bug 1651776 - Move Compute{Inline|Block}SizeFromAspectRatio() into AspectRatio struct. r=TYLin
We add a new file, AspectRatio.cpp, to definte the method, ComputeRatioDependentSize. So we don't have to move this function to WritingModes.h Besides, we miss the forward declaration of IPC::ParamTraits, so add it into AspectRatio.h. Differential Revision: https://phabricator.services.mozilla.com/D95822
This commit is contained in:
parent
6af16546a9
commit
d494534798
31
layout/generic/AspectRatio.cpp
Normal file
31
layout/generic/AspectRatio.cpp
Normal file
@ -0,0 +1,31 @@
|
||||
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
||||
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
||||
/* 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/. */
|
||||
|
||||
#include "AspectRatio.h"
|
||||
|
||||
#include "mozilla/WritingModes.h"
|
||||
|
||||
namespace mozilla {
|
||||
|
||||
nscoord AspectRatio::ComputeRatioDependentSize(
|
||||
LogicalAxis aRatioDependentAxis, const WritingMode& aWM,
|
||||
nscoord aRatioDeterminingSize,
|
||||
const LogicalSize& aContentBoxSizeToBoxSizingAdjust) const {
|
||||
MOZ_ASSERT(*this,
|
||||
"Infinite or zero ratio may have undefined behavior when "
|
||||
"computing the size");
|
||||
return aRatioDependentAxis == LogicalAxis::eLogicalAxisInline
|
||||
? ConvertToWritingMode(aWM).ApplyTo(
|
||||
aRatioDeterminingSize +
|
||||
aContentBoxSizeToBoxSizingAdjust.BSize(aWM)) -
|
||||
aContentBoxSizeToBoxSizingAdjust.ISize(aWM)
|
||||
: ConvertToWritingMode(aWM).Inverted().ApplyTo(
|
||||
aRatioDeterminingSize +
|
||||
aContentBoxSizeToBoxSizingAdjust.ISize(aWM)) -
|
||||
aContentBoxSizeToBoxSizingAdjust.BSize(aWM);
|
||||
}
|
||||
|
||||
} // namespace mozilla
|
@ -15,8 +15,15 @@
|
||||
#include <algorithm>
|
||||
#include <limits>
|
||||
|
||||
namespace IPC {
|
||||
template <typename T>
|
||||
struct ParamTraits;
|
||||
} // namespace IPC
|
||||
|
||||
namespace mozilla {
|
||||
|
||||
enum LogicalAxis : uint8_t;
|
||||
class LogicalSize;
|
||||
class WritingMode;
|
||||
|
||||
struct AspectRatio {
|
||||
@ -64,6 +71,38 @@ struct AspectRatio {
|
||||
[[nodiscard]] inline AspectRatio ConvertToWritingMode(
|
||||
const WritingMode& aWM) const;
|
||||
|
||||
/**
|
||||
* This method computes the ratio-dependent size by the ratio-determining size
|
||||
* and aspect-ratio (i.e. preferred aspect ratio). Basically this function
|
||||
* will be used in the calculation of 'auto' sizes when the preferred
|
||||
* aspect ratio is not 'auto'.
|
||||
*
|
||||
* @param aRatioDependentAxis The ratio depenedent axis of the box.
|
||||
* @param aWM The writing mode of the box.
|
||||
* @param aRatioDetermingSize The size on the ratio determining axis.
|
||||
* Basically, we use this size and |mRatio| to
|
||||
* compute the size on the ratio-dependent axis.
|
||||
* @param aContentBoxSizeToBoxSizingAdjust The border padding box size
|
||||
* adjustment. We need this because
|
||||
* aspect-ratio should take the
|
||||
* box-sizing into account if its
|
||||
* style is '<ratio>'. If its style
|
||||
* is 'auto & <ratio>', we should use
|
||||
* content-box dimensions always.
|
||||
* If the callers want the ratio to
|
||||
* apply to the content-box size, we
|
||||
* should pass a zero LogicalSize.
|
||||
*
|
||||
* The return value is the content-box size on the ratio-dependent axis.
|
||||
* Plese see the definition of the ratio-dependent axis and the
|
||||
* ratio-determining axis in the spec:
|
||||
* https://drafts.csswg.org/css-sizing-4/#aspect-ratio
|
||||
*/
|
||||
[[nodiscard]] nscoord ComputeRatioDependentSize(
|
||||
LogicalAxis aRatioDependentAxis, const WritingMode& aWM,
|
||||
nscoord aRatioDeterminingSize,
|
||||
const LogicalSize& aContentBoxSizeToBoxSizingAdjust) const;
|
||||
|
||||
bool operator==(const AspectRatio& aOther) const {
|
||||
return mRatio == aOther.mRatio;
|
||||
}
|
||||
|
@ -164,6 +164,7 @@ EXPORTS.mozilla.layout += [
|
||||
]
|
||||
|
||||
UNIFIED_SOURCES += [
|
||||
"AspectRatio.cpp",
|
||||
"BlockReflowInput.cpp",
|
||||
"BRFrame.cpp",
|
||||
"ColumnSetWrapperFrame.cpp",
|
||||
|
@ -5999,33 +5999,6 @@ AspectRatio nsIFrame::GetAspectRatio() const {
|
||||
/* virtual */
|
||||
AspectRatio nsIFrame::GetIntrinsicRatio() const { return AspectRatio(); }
|
||||
|
||||
static nscoord ComputeInlineSizeFromAspectRatio(
|
||||
WritingMode aWM, const StyleAspectRatio& aAspectRatio, nscoord aBlockSize,
|
||||
const LogicalSize& aBoxSizingAdjustment) {
|
||||
MOZ_ASSERT(aAspectRatio.HasFiniteRatio(),
|
||||
"Infinite or zero ratio may have undefined behavior when "
|
||||
"computing the size");
|
||||
return aAspectRatio.ratio.AsRatio()
|
||||
.ToLayoutRatio()
|
||||
.ConvertToWritingMode(aWM)
|
||||
.ApplyTo(aBlockSize + aBoxSizingAdjustment.BSize(aWM)) -
|
||||
aBoxSizingAdjustment.ISize(aWM);
|
||||
}
|
||||
|
||||
static nscoord ComputeBlockSizeFromAspectRatio(
|
||||
WritingMode aWM, const StyleAspectRatio& aAspectRatio, nscoord aInlineSize,
|
||||
const LogicalSize& aBoxSizingAdjustment) {
|
||||
MOZ_ASSERT(aAspectRatio.HasFiniteRatio(),
|
||||
"Infinite or zero ratio may have undefined behavior when "
|
||||
"computing the size");
|
||||
return aAspectRatio.ratio.AsRatio()
|
||||
.ToLayoutRatio()
|
||||
.ConvertToWritingMode(aWM)
|
||||
.Inverted()
|
||||
.ApplyTo(aInlineSize + aBoxSizingAdjustment.ISize(aWM)) -
|
||||
aBoxSizingAdjustment.BSize(aWM);
|
||||
}
|
||||
|
||||
static bool ShouldApplyAutomaticMinimumOnInlineAxis(
|
||||
WritingMode aWM, const nsStyleDisplay* aDisplay,
|
||||
const nsStylePosition* aPosition) {
|
||||
@ -6066,13 +6039,17 @@ static MinMaxSize ComputeTransferredMinMaxInlineSize(
|
||||
MinMaxSize transferredISize;
|
||||
|
||||
if (aMinMaxBSize.mMinSize > 0) {
|
||||
transferredISize.mMinSize = ComputeInlineSizeFromAspectRatio(
|
||||
aWM, aAspectRatio, aMinMaxBSize.mMinSize, aBoxSizingAdjustment);
|
||||
transferredISize.mMinSize =
|
||||
aAspectRatio.ToLayoutRatio().ComputeRatioDependentSize(
|
||||
LogicalAxis::eLogicalAxisInline, aWM, aMinMaxBSize.mMinSize,
|
||||
aBoxSizingAdjustment);
|
||||
}
|
||||
|
||||
if (aMinMaxBSize.mMaxSize != NS_UNCONSTRAINEDSIZE) {
|
||||
transferredISize.mMaxSize = ComputeInlineSizeFromAspectRatio(
|
||||
aWM, aAspectRatio, aMinMaxBSize.mMaxSize, aBoxSizingAdjustment);
|
||||
transferredISize.mMaxSize =
|
||||
aAspectRatio.ToLayoutRatio().ComputeRatioDependentSize(
|
||||
LogicalAxis::eLogicalAxisInline, aWM, aMinMaxBSize.mMaxSize,
|
||||
aBoxSizingAdjustment);
|
||||
}
|
||||
|
||||
// Minimum size wins over maximum size.
|
||||
@ -6206,8 +6183,9 @@ nsIFrame::SizeComputationResult nsIFrame::ComputeSize(
|
||||
auto bSize = nsLayoutUtils::ComputeBSizeValue(
|
||||
aCBSize.BSize(aWM), boxSizingAdjust.BSize(aWM),
|
||||
blockStyleCoord->AsLengthPercentage());
|
||||
result.ISize(aWM) = ComputeInlineSizeFromAspectRatio(
|
||||
aWM, stylePos->mAspectRatio, bSize, boxSizingAdjust);
|
||||
result.ISize(aWM) =
|
||||
stylePos->mAspectRatio.ToLayoutRatio().ComputeRatioDependentSize(
|
||||
LogicalAxis::eLogicalAxisInline, aWM, bSize, boxSizingAdjust);
|
||||
aspectRatioUsage = AspectRatioUsage::ToComputeISize;
|
||||
} else if (MOZ_UNLIKELY(isGridItem) && !IsTrueOverflowContainer()) {
|
||||
// 'auto' inline-size for grid-level box - fill the CB for 'stretch' /
|
||||
@ -6339,8 +6317,10 @@ nsIFrame::SizeComputationResult nsIFrame::ComputeSize(
|
||||
blockStyleCoord->AsLengthPercentage());
|
||||
} else if (stylePos->mAspectRatio.HasFiniteRatio() &&
|
||||
result.ISize(aWM) != NS_UNCONSTRAINEDSIZE) {
|
||||
result.BSize(aWM) = ComputeBlockSizeFromAspectRatio(
|
||||
aWM, stylePos->mAspectRatio, result.ISize(aWM), boxSizingAdjust);
|
||||
result.BSize(aWM) =
|
||||
stylePos->mAspectRatio.ToLayoutRatio().ComputeRatioDependentSize(
|
||||
LogicalAxis::eLogicalAxisBlock, aWM, result.ISize(aWM),
|
||||
boxSizingAdjust);
|
||||
MOZ_ASSERT(aspectRatioUsage == AspectRatioUsage::None);
|
||||
aspectRatioUsage = AspectRatioUsage::ToComputeBSize;
|
||||
} else if (MOZ_UNLIKELY(isGridItem) && blockStyleCoord->IsAuto() &&
|
||||
|
@ -981,6 +981,11 @@ inline AspectRatio StyleRatio<StyleNonNegativeNumber>::ToLayoutRatio() const {
|
||||
return AspectRatio::FromSize(_0, _1);
|
||||
}
|
||||
|
||||
template <>
|
||||
inline AspectRatio StyleAspectRatio::ToLayoutRatio() const {
|
||||
return HasRatio() ? ratio.AsRatio().ToLayoutRatio() : AspectRatio();
|
||||
}
|
||||
|
||||
} // namespace mozilla
|
||||
|
||||
#endif
|
||||
|
@ -838,6 +838,7 @@ renaming_overrides_prefixing = true
|
||||
return HasRatio() && ratio.AsRatio().ToLayoutRatio();
|
||||
}
|
||||
bool BehavesAsAuto() const { return auto_ || !HasFiniteRatio(); }
|
||||
inline AspectRatio ToLayoutRatio() const;
|
||||
|
||||
static StyleGenericAspectRatio Auto() {
|
||||
return {true, StylePreferredRatio<N>::None()};
|
||||
|
Loading…
x
Reference in New Issue
Block a user