FormattingContextGeometry.cpp [plain text]
#include "config.h"
#include "FormattingContext.h"
#if ENABLE(LAYOUT_FORMATTING_CONTEXT)
#include "FloatingState.h"
#include "InlineFormattingState.h"
#include "TableFormattingState.h"
namespace WebCore {
namespace Layout {
static inline bool isHeightAuto(const Box& layoutBox)
{
auto height = layoutBox.style().logicalHeight();
if (height.isAuto())
return true;
if (height.isPercent()) {
if (layoutBox.isOutOfFlowPositioned())
return false;
return !layoutBox.containingBlock()->style().logicalHeight().isFixed();
}
return false;
}
Optional<LayoutUnit> FormattingContext::Geometry::computedHeightValue(const Box& layoutBox, HeightType heightType, Optional<LayoutUnit> containingBlockHeight) const
{
auto& style = layoutBox.style();
auto height = heightType == HeightType::Normal ? style.logicalHeight() : heightType == HeightType::Min ? style.logicalMinHeight() : style.logicalMaxHeight();
if (height.isUndefined() || height.isAuto())
return { };
if (height.isFixed())
return LayoutUnit { height.value() };
if (!containingBlockHeight) {
ASSERT(!layoutBox.isOutOfFlowPositioned());
if (layoutState().inQuirksMode())
containingBlockHeight = formattingContext().quirks().heightValueOfNearestContainingBlockWithFixedHeight(layoutBox);
else {
auto containingBlockHeightFromStyle = layoutBox.containingBlock()->style().logicalHeight();
if (containingBlockHeightFromStyle.isFixed())
containingBlockHeight = LayoutUnit { containingBlockHeightFromStyle.value() };
}
}
if (!containingBlockHeight)
return { };
return valueForLength(height, *containingBlockHeight);
}
Optional<LayoutUnit> FormattingContext::Geometry::computedContentHeight(const Box& layoutBox, Optional<LayoutUnit> containingBlockHeight) const
{
if (auto height = computedHeightValue(layoutBox, HeightType::Normal, containingBlockHeight)) {
if (layoutBox.style().boxSizing() == BoxSizing::ContentBox)
return height;
auto& boxGeometry = formattingContext().geometryForBox(layoutBox);
return *height - (boxGeometry.verticalBorder() + boxGeometry.verticalPadding().valueOr(0));
}
return { };
}
Optional<LayoutUnit> FormattingContext::Geometry::computedContentWidth(const Box& layoutBox, LayoutUnit containingBlockWidth) const
{
if (auto width = computedValueIfNotAuto(layoutBox.style().logicalWidth(), containingBlockWidth)) {
if (layoutBox.style().boxSizing() == BoxSizing::ContentBox)
return width;
auto& boxGeometry = formattingContext().geometryForBox(layoutBox);
return *width - (boxGeometry.horizontalBorder() + boxGeometry.horizontalPadding().valueOr(0));
}
return { };
}
LayoutUnit FormattingContext::Geometry::contentHeightForFormattingContextRoot(const Box& layoutBox) const
{
ASSERT(isHeightAuto(layoutBox) && (layoutBox.establishesFormattingContext() || layoutBox.isDocumentBox()));
if (!is<Container>(layoutBox) || !downcast<Container>(layoutBox).hasInFlowOrFloatingChild())
return { };
auto& layoutState = this->layoutState();
auto& formattingContext = this->formattingContext();
auto& boxGeometry = formattingContext.geometryForBox(layoutBox);
auto borderAndPaddingTop = boxGeometry.borderTop() + boxGeometry.paddingTop().valueOr(0);
auto top = borderAndPaddingTop;
auto bottom = borderAndPaddingTop;
auto& formattingRootContainer = downcast<Container>(layoutBox);
if (formattingRootContainer.establishesInlineFormattingContext()) {
auto& lineBoxes = downcast<InlineFormattingState>(layoutState.establishedFormattingState(formattingRootContainer)).displayInlineContent()->lineBoxes;
ASSERT(!lineBoxes.isEmpty());
top = lineBoxes.first().logicalTop();
bottom = lineBoxes.last().logicalBottom();
} else if (formattingRootContainer.establishesBlockFormattingContext() || formattingRootContainer.establishesTableFormattingContext() || formattingRootContainer.isDocumentBox()) {
if (formattingRootContainer.hasInFlowChild()) {
auto& firstBoxGeometry = formattingContext.geometryForBox(*formattingRootContainer.firstInFlowChild(), EscapeType::AccessChildFormattingContext);
auto& lastBoxGeometry = formattingContext.geometryForBox(*formattingRootContainer.lastInFlowChild(), EscapeType::AccessChildFormattingContext);
top = firstBoxGeometry.rectWithMargin().top();
bottom = lastBoxGeometry.rectWithMargin().bottom();
}
} else
ASSERT_NOT_REACHED();
auto* formattingContextRoot = &formattingRootContainer;
if (!layoutBox.establishesFormattingContext()) {
ASSERT(layoutBox.isDocumentBox());
formattingContextRoot = &layoutBox.formattingContextRoot();
}
auto& floatingState = layoutState.establishedFormattingState(*formattingContextRoot).floatingState();
auto floatBottom = floatingState.bottom(*formattingContextRoot);
if (floatBottom) {
bottom = std::max<LayoutUnit>(*floatBottom, bottom);
auto floatTop = floatingState.top(*formattingContextRoot);
ASSERT(floatTop);
top = std::min<LayoutUnit>(*floatTop, top);
}
auto computedHeight = bottom - top;
LOG_WITH_STREAM(FormattingContextLayout, stream << "[Height] -> content height for formatting context root -> height(" << computedHeight << "px) layoutBox("<< &layoutBox << ")");
return computedHeight;
}
Optional<LayoutUnit> FormattingContext::Geometry::computedValueIfNotAuto(const Length& geometryProperty, LayoutUnit containingBlockWidth) const
{
if (geometryProperty.isUndefined())
return WTF::nullopt;
if (geometryProperty.isAuto())
return WTF::nullopt;
return valueForLength(geometryProperty, containingBlockWidth);
}
Optional<LayoutUnit> FormattingContext::Geometry::fixedValue(const Length& geometryProperty) const
{
if (!geometryProperty.isFixed())
return WTF::nullopt;
return LayoutUnit(geometryProperty.value());
}
Optional<LayoutUnit> FormattingContext::Geometry::computedMaxHeight(const Box& layoutBox, Optional<LayoutUnit> containingBlockHeight) const
{
return computedHeightValue(layoutBox, HeightType::Max, containingBlockHeight);
}
Optional<LayoutUnit> FormattingContext::Geometry::computedMinHeight(const Box& layoutBox, Optional<LayoutUnit> containingBlockHeight) const
{
if (auto minHeightValue = computedHeightValue(layoutBox, HeightType::Min, containingBlockHeight))
return minHeightValue;
return { LayoutUnit { } };
}
Optional<LayoutUnit> FormattingContext::Geometry::computedMinWidth(const Box& layoutBox, LayoutUnit containingBlockWidth) const
{
return computedValueIfNotAuto(layoutBox.style().logicalMinWidth(), containingBlockWidth);
}
Optional<LayoutUnit> FormattingContext::Geometry::computedMaxWidth(const Box& layoutBox, LayoutUnit containingBlockWidth) const
{
return computedValueIfNotAuto(layoutBox.style().logicalMaxWidth(), containingBlockWidth);
}
LayoutUnit FormattingContext::Geometry::staticVerticalPositionForOutOfFlowPositioned(const Box& layoutBox, const UsedVerticalValues& usedVerticalValues) const
{
ASSERT(layoutBox.isOutOfFlowPositioned());
auto& formattingContext = this->formattingContext();
LayoutUnit top;
if (auto* previousInFlowSibling = layoutBox.previousInFlowSibling()) {
auto& previousInFlowBoxGeometry = formattingContext.geometryForBox(*previousInFlowSibling, FormattingContext::EscapeType::AccessChildFormattingContext);
top += previousInFlowBoxGeometry.bottom() + previousInFlowBoxGeometry.nonCollapsedMarginAfter();
} else {
ASSERT(layoutBox.parent());
top = formattingContext.geometryForBox(*layoutBox.parent(), FormattingContext::EscapeType::AccessChildFormattingContext).contentBoxTop();
}
auto& containingBlock = *layoutBox.containingBlock();
for (auto* container = layoutBox.parent(); container != &containingBlock; container = container->containingBlock()) {
auto& boxGeometry = formattingContext.geometryForBox(*container, FormattingContext::EscapeType::AccessChildFormattingContext);
top += boxGeometry.top();
ASSERT(!container->isPositioned() || layoutBox.isFixedPositioned());
}
return top - usedVerticalValues.constraints.contentBoxTop;
}
LayoutUnit FormattingContext::Geometry::staticHorizontalPositionForOutOfFlowPositioned(const Box& layoutBox, const UsedHorizontalValues& usedHorizontalValues) const
{
ASSERT(layoutBox.isOutOfFlowPositioned());
auto& formattingContext = this->formattingContext();
ASSERT(layoutBox.parent());
auto left = formattingContext.geometryForBox(*layoutBox.parent(), FormattingContext::EscapeType::AccessChildFormattingContext).contentBoxLeft();
auto& containingBlock = *layoutBox.containingBlock();
for (auto* container = layoutBox.parent(); container != &containingBlock; container = container->containingBlock()) {
auto& boxGeometry = formattingContext.geometryForBox(*container, FormattingContext::EscapeType::AccessChildFormattingContext);
left += boxGeometry.left();
ASSERT(!container->isPositioned() || layoutBox.isFixedPositioned());
}
return left - usedHorizontalValues.constraints.contentBoxLeft;
}
LayoutUnit FormattingContext::Geometry::shrinkToFitWidth(const Box& formattingRoot, LayoutUnit availableWidth)
{
LOG_WITH_STREAM(FormattingContextLayout, stream << "[Width] -> shrink to fit -> unsupported -> width(" << LayoutUnit { } << "px) layoutBox: " << &formattingRoot << ")");
ASSERT(formattingRoot.establishesFormattingContext());
auto intrinsicWidthConstraints = IntrinsicWidthConstraints { };
if (is<Container>(formattingRoot)) {
auto& root = downcast<Container>(formattingRoot);
auto& formattingStateForRoot = layoutState().createFormattingStateForFormattingRootIfNeeded(root);
auto precomputedIntrinsicWidthConstraints = formattingStateForRoot.intrinsicWidthConstraints();
if (!precomputedIntrinsicWidthConstraints)
intrinsicWidthConstraints = LayoutContext::createFormattingContext(root, layoutState())->computedIntrinsicWidthConstraints();
else
intrinsicWidthConstraints = *precomputedIntrinsicWidthConstraints;
}
return std::min(std::max(intrinsicWidthConstraints.minimum, availableWidth), intrinsicWidthConstraints.maximum);
}
VerticalGeometry FormattingContext::Geometry::outOfFlowNonReplacedVerticalGeometry(const Box& layoutBox, const UsedHorizontalValues& usedHorizontalValues, const UsedVerticalValues& usedVerticalValues) const
{
ASSERT(layoutBox.isOutOfFlowPositioned() && !layoutBox.replaced());
ASSERT(usedVerticalValues.constraints.height);
auto& formattingContext = this->formattingContext();
auto& style = layoutBox.style();
auto& boxGeometry = formattingContext.geometryForBox(layoutBox);
auto containingBlockHeight = *usedVerticalValues.constraints.height;
auto containingBlockWidth = usedHorizontalValues.constraints.width;
auto top = computedValueIfNotAuto(style.logicalTop(), containingBlockWidth);
auto bottom = computedValueIfNotAuto(style.logicalBottom(), containingBlockWidth);
auto height = usedVerticalValues.height ? usedVerticalValues.height.value() : computedContentHeight(layoutBox, containingBlockHeight);
auto computedVerticalMargin = Geometry::computedVerticalMargin(layoutBox, usedHorizontalValues);
UsedVerticalMargin::NonCollapsedValues usedVerticalMargin;
auto paddingTop = boxGeometry.paddingTop().valueOr(0);
auto paddingBottom = boxGeometry.paddingBottom().valueOr(0);
auto borderTop = boxGeometry.borderTop();
auto borderBottom = boxGeometry.borderBottom();
if (!top && !height && !bottom)
top = staticVerticalPositionForOutOfFlowPositioned(layoutBox, usedVerticalValues);
if (top && height && bottom) {
if (!computedVerticalMargin.before && !computedVerticalMargin.after) {
auto marginBeforeAndAfter = containingBlockHeight - (*top + borderTop + paddingTop + *height + paddingBottom + borderBottom + *bottom);
usedVerticalMargin = { marginBeforeAndAfter / 2, marginBeforeAndAfter / 2 };
} else if (!computedVerticalMargin.before) {
usedVerticalMargin.after = *computedVerticalMargin.after;
usedVerticalMargin.before = containingBlockHeight - (*top + borderTop + paddingTop + *height + paddingBottom + borderBottom + usedVerticalMargin.after + *bottom);
} else if (!computedVerticalMargin.after) {
usedVerticalMargin.before = *computedVerticalMargin.before;
usedVerticalMargin.after = containingBlockHeight - (*top + usedVerticalMargin.before + borderTop + paddingTop + *height + paddingBottom + borderBottom + *bottom);
} else
usedVerticalMargin = { *computedVerticalMargin.before, *computedVerticalMargin.after };
auto boxHeight = *top + usedVerticalMargin.before + borderTop + paddingTop + *height + paddingBottom + borderBottom + usedVerticalMargin.after + *bottom;
if (boxHeight != containingBlockHeight)
bottom = containingBlockHeight - (*top + usedVerticalMargin.before + borderTop + paddingTop + *height + paddingBottom + borderBottom + usedVerticalMargin.after);
}
if (!top && !height && bottom) {
height = contentHeightForFormattingContextRoot(layoutBox);
usedVerticalMargin = { computedVerticalMargin.before.valueOr(0), computedVerticalMargin.after.valueOr(0) };
top = containingBlockHeight - (usedVerticalMargin.before + borderTop + paddingTop + *height + paddingBottom + borderBottom + usedVerticalMargin.after + *bottom);
}
if (!top && !bottom && height) {
top = staticVerticalPositionForOutOfFlowPositioned(layoutBox, usedVerticalValues);
usedVerticalMargin = { computedVerticalMargin.before.valueOr(0), computedVerticalMargin.after.valueOr(0) };
bottom = containingBlockHeight - (*top + usedVerticalMargin.before + borderTop + paddingTop + *height + paddingBottom + borderBottom + usedVerticalMargin.after);
}
if (!height && !bottom && top) {
height = contentHeightForFormattingContextRoot(layoutBox);
usedVerticalMargin = { computedVerticalMargin.before.valueOr(0), computedVerticalMargin.after.valueOr(0) };
bottom = containingBlockHeight - (*top + usedVerticalMargin.before + borderTop + paddingTop + *height + paddingBottom + borderBottom + usedVerticalMargin.after);
}
if (!top && height && bottom) {
usedVerticalMargin = { computedVerticalMargin.before.valueOr(0), computedVerticalMargin.after.valueOr(0) };
top = containingBlockHeight - (usedVerticalMargin.before + borderTop + paddingTop + *height + paddingBottom + borderBottom + usedVerticalMargin.after + *bottom);
}
if (!height && top && bottom) {
usedVerticalMargin = { computedVerticalMargin.before.valueOr(0), computedVerticalMargin.after.valueOr(0) };
height = containingBlockHeight - (*top + usedVerticalMargin.before + borderTop + paddingTop + paddingBottom + borderBottom + usedVerticalMargin.after + *bottom);
}
if (!bottom && top && height) {
usedVerticalMargin = { computedVerticalMargin.before.valueOr(0), computedVerticalMargin.after.valueOr(0) };
bottom = containingBlockHeight - (*top + usedVerticalMargin.before + borderTop + paddingTop + *height + paddingBottom + borderBottom + usedVerticalMargin.after);
}
ASSERT(top);
ASSERT(bottom);
ASSERT(height);
auto containingBlockPaddingVerticalEdge = usedVerticalValues.constraints.contentBoxTop;
*top += containingBlockPaddingVerticalEdge;
*bottom += containingBlockPaddingVerticalEdge;
LOG_WITH_STREAM(FormattingContextLayout, stream << "[Position][Height][Margin] -> out-of-flow non-replaced -> top(" << *top << "px) bottom(" << *bottom << "px) height(" << *height << "px) margin(" << usedVerticalMargin.before << "px, " << usedVerticalMargin.after << "px) layoutBox(" << &layoutBox << ")");
return { *top, *bottom, { *height, usedVerticalMargin } };
}
HorizontalGeometry FormattingContext::Geometry::outOfFlowNonReplacedHorizontalGeometry(const Box& layoutBox, const UsedHorizontalValues& usedHorizontalValues)
{
ASSERT(layoutBox.isOutOfFlowPositioned() && !layoutBox.replaced());
auto& formattingContext = this->formattingContext();
auto& style = layoutBox.style();
auto& boxGeometry = formattingContext.geometryForBox(layoutBox);
auto containingBlockWidth = usedHorizontalValues.constraints.width;
auto isLeftToRightDirection = layoutBox.containingBlock()->style().isLeftToRightDirection();
auto left = computedValueIfNotAuto(style.logicalLeft(), containingBlockWidth);
auto right = computedValueIfNotAuto(style.logicalRight(), containingBlockWidth);
auto width = usedHorizontalValues.width ? usedHorizontalValues.width : computedContentWidth(layoutBox, containingBlockWidth);
auto computedHorizontalMargin = Geometry::computedHorizontalMargin(layoutBox, usedHorizontalValues);
UsedHorizontalMargin usedHorizontalMargin;
auto paddingLeft = boxGeometry.paddingLeft().valueOr(0);
auto paddingRight = boxGeometry.paddingRight().valueOr(0);
auto borderLeft = boxGeometry.borderLeft();
auto borderRight = boxGeometry.borderRight();
if (!left && !width && !right) {
usedHorizontalMargin = { computedHorizontalMargin.start.valueOr(0), computedHorizontalMargin.end.valueOr(0) };
auto staticHorizontalPosition = staticHorizontalPositionForOutOfFlowPositioned(layoutBox, usedHorizontalValues);
if (isLeftToRightDirection)
left = staticHorizontalPosition;
else
right = staticHorizontalPosition;
} else if (left && width && right) {
if (!computedHorizontalMargin.start && !computedHorizontalMargin.end) {
auto marginStartAndEnd = containingBlockWidth - (*left + borderLeft + paddingLeft + *width + paddingRight + borderRight + *right);
if (marginStartAndEnd >= 0)
usedHorizontalMargin = { marginStartAndEnd / 2, marginStartAndEnd / 2 };
else {
if (isLeftToRightDirection) {
usedHorizontalMargin.start = 0_lu;
usedHorizontalMargin.end = containingBlockWidth - (*left + usedHorizontalMargin.start + borderLeft + paddingLeft + *width + paddingRight + borderRight + *right);
} else {
usedHorizontalMargin.end = 0_lu;
usedHorizontalMargin.start = containingBlockWidth - (*left + borderLeft + paddingLeft + *width + paddingRight + borderRight + usedHorizontalMargin.end + *right);
}
}
} else if (!computedHorizontalMargin.start) {
usedHorizontalMargin.end = *computedHorizontalMargin.end;
usedHorizontalMargin.start = containingBlockWidth - (*left + borderLeft + paddingLeft + *width + paddingRight + borderRight + usedHorizontalMargin.end + *right);
} else if (!computedHorizontalMargin.end) {
usedHorizontalMargin.start = *computedHorizontalMargin.start;
usedHorizontalMargin.end = containingBlockWidth - (*left + usedHorizontalMargin.start + borderLeft + paddingLeft + *width + paddingRight + borderRight + *right);
} else {
usedHorizontalMargin = { *computedHorizontalMargin.start, *computedHorizontalMargin.end };
if (isLeftToRightDirection)
right = containingBlockWidth - (usedHorizontalMargin.start + *left + borderLeft + paddingLeft + *width + paddingRight + borderRight + usedHorizontalMargin.end);
else
left = containingBlockWidth - (usedHorizontalMargin.start + borderLeft + paddingLeft + *width + paddingRight + borderRight + usedHorizontalMargin.end + *right);
}
} else {
usedHorizontalMargin = { computedHorizontalMargin.start.valueOr(0), computedHorizontalMargin.end.valueOr(0) };
}
if (!left && !width && right) {
left = LayoutUnit { 0 };
auto availableWidth = containingBlockWidth - (*left + usedHorizontalMargin.start + borderLeft + paddingLeft + paddingRight + borderRight + usedHorizontalMargin.end + *right);
width = shrinkToFitWidth(layoutBox, availableWidth);
left = containingBlockWidth - (usedHorizontalMargin.start + borderLeft + paddingLeft + *width + paddingRight + borderRight + usedHorizontalMargin.end + *right);
} else if (!left && !right && width) {
auto staticHorizontalPosition = staticHorizontalPositionForOutOfFlowPositioned(layoutBox, usedHorizontalValues);
if (isLeftToRightDirection) {
left = staticHorizontalPosition;
right = containingBlockWidth - (*left + usedHorizontalMargin.start + borderLeft + paddingLeft + *width + paddingRight + borderRight + usedHorizontalMargin.end);
} else {
right = staticHorizontalPosition;
left = containingBlockWidth - (usedHorizontalMargin.start + borderLeft + paddingLeft + *width + paddingRight + borderRight + usedHorizontalMargin.end + *right);
}
} else if (!width && !right && left) {
right = LayoutUnit { 0 };
auto availableWidth = containingBlockWidth - (*left + usedHorizontalMargin.start + borderLeft + paddingLeft + paddingRight + borderRight + usedHorizontalMargin.end + *right);
width = shrinkToFitWidth(layoutBox, availableWidth);
right = containingBlockWidth - (*left + usedHorizontalMargin.start + borderLeft + paddingLeft + *width + paddingRight + borderRight + usedHorizontalMargin.end);
} else if (!left && width && right) {
left = containingBlockWidth - (usedHorizontalMargin.start + borderLeft + paddingLeft + *width + paddingRight + borderRight + usedHorizontalMargin.end + *right);
} else if (!width && left && right) {
width = containingBlockWidth - (*left + usedHorizontalMargin.start + borderLeft + paddingLeft + paddingRight + borderRight + usedHorizontalMargin.end + *right);
} else if (!right && left && width) {
right = containingBlockWidth - (*left + usedHorizontalMargin.start + borderLeft + paddingLeft + *width + paddingRight + borderRight + usedHorizontalMargin.end);
}
ASSERT(left);
ASSERT(right);
ASSERT(width);
auto containingBlockPaddingVerticalEdge = usedHorizontalValues.constraints.contentBoxLeft;
*left += containingBlockPaddingVerticalEdge;
*right += containingBlockPaddingVerticalEdge;
LOG_WITH_STREAM(FormattingContextLayout, stream << "[Position][Width][Margin] -> out-of-flow non-replaced -> left(" << *left << "px) right(" << *right << "px) width(" << *width << "px) margin(" << usedHorizontalMargin.start << "px, " << usedHorizontalMargin.end << "px) layoutBox(" << &layoutBox << ")");
return { *left, *right, { *width, usedHorizontalMargin, computedHorizontalMargin } };
}
VerticalGeometry FormattingContext::Geometry::outOfFlowReplacedVerticalGeometry(const Box& layoutBox, const UsedHorizontalValues& usedHorizontalValues, const UsedVerticalValues& usedVerticalValues) const
{
ASSERT(layoutBox.isOutOfFlowPositioned() && layoutBox.replaced());
ASSERT(usedVerticalValues.constraints.height);
auto& formattingContext = this->formattingContext();
auto& style = layoutBox.style();
auto& boxGeometry = formattingContext.geometryForBox(layoutBox);
auto containingBlockHeight = *usedVerticalValues.constraints.height;
auto containingBlockWidth = usedHorizontalValues.constraints.width;
auto top = computedValueIfNotAuto(style.logicalTop(), containingBlockWidth);
auto bottom = computedValueIfNotAuto(style.logicalBottom(), containingBlockWidth);
auto height = inlineReplacedHeightAndMargin(layoutBox, usedHorizontalValues, usedVerticalValues).contentHeight;
auto computedVerticalMargin = Geometry::computedVerticalMargin(layoutBox, usedHorizontalValues);
Optional<LayoutUnit> usedMarginBefore = computedVerticalMargin.before;
Optional<LayoutUnit> usedMarginAfter = computedVerticalMargin.after;
auto paddingTop = boxGeometry.paddingTop().valueOr(0);
auto paddingBottom = boxGeometry.paddingBottom().valueOr(0);
auto borderTop = boxGeometry.borderTop();
auto borderBottom = boxGeometry.borderBottom();
if (!top && !bottom) {
top = staticVerticalPositionForOutOfFlowPositioned(layoutBox, usedVerticalValues);
}
if (!bottom) {
usedMarginBefore = computedVerticalMargin.before.valueOr(0);
usedMarginAfter = usedMarginBefore;
}
if (!usedMarginBefore && !usedMarginAfter) {
auto marginBeforeAndAfter = containingBlockHeight - (*top + borderTop + paddingTop + height + paddingBottom + borderBottom + *bottom);
usedMarginBefore = marginBeforeAndAfter / 2;
usedMarginAfter = usedMarginBefore;
}
if (!top)
top = containingBlockHeight - (*usedMarginBefore + borderTop + paddingTop + height + paddingBottom + borderBottom + *usedMarginAfter + *bottom);
if (!bottom)
bottom = containingBlockHeight - (*top + *usedMarginBefore + borderTop + paddingTop + height + paddingBottom + borderBottom + *usedMarginAfter);
if (!usedMarginBefore)
usedMarginBefore = containingBlockHeight - (*top + borderTop + paddingTop + height + paddingBottom + borderBottom + *usedMarginAfter + *bottom);
if (!usedMarginAfter)
usedMarginAfter = containingBlockHeight - (*top + *usedMarginBefore + borderTop + paddingTop + height + paddingBottom + borderBottom + *bottom);
auto boxHeight = *top + *usedMarginBefore + borderTop + paddingTop + height + paddingBottom + borderBottom + *usedMarginAfter + *bottom;
if (boxHeight > containingBlockHeight)
bottom = containingBlockHeight - (*top + *usedMarginBefore + borderTop + paddingTop + height + paddingBottom + borderBottom + *usedMarginAfter);
auto containingBlockPaddingVerticalEdge = usedVerticalValues.constraints.contentBoxTop;
*top += containingBlockPaddingVerticalEdge;
*bottom += containingBlockPaddingVerticalEdge;
ASSERT(top);
ASSERT(bottom);
ASSERT(usedMarginBefore);
ASSERT(usedMarginAfter);
LOG_WITH_STREAM(FormattingContextLayout, stream << "[Position][Height][Margin] -> out-of-flow replaced -> top(" << *top << "px) bottom(" << *bottom << "px) height(" << height << "px) margin(" << *usedMarginBefore << "px, " << *usedMarginAfter << "px) layoutBox(" << &layoutBox << ")");
return { *top, *bottom, { height, { *usedMarginBefore, *usedMarginAfter } } };
}
HorizontalGeometry FormattingContext::Geometry::outOfFlowReplacedHorizontalGeometry(const Box& layoutBox, const UsedHorizontalValues& usedHorizontalValues, const UsedVerticalValues& usedVerticalValues) const
{
ASSERT(layoutBox.isOutOfFlowPositioned() && layoutBox.replaced());
auto& formattingContext = this->formattingContext();
auto& style = layoutBox.style();
auto& boxGeometry = formattingContext.geometryForBox(layoutBox);
auto containingBlockWidth = usedHorizontalValues.constraints.width;
auto isLeftToRightDirection = layoutBox.containingBlock()->style().isLeftToRightDirection();
auto left = computedValueIfNotAuto(style.logicalLeft(), containingBlockWidth);
auto right = computedValueIfNotAuto(style.logicalRight(), containingBlockWidth);
auto computedHorizontalMargin = Geometry::computedHorizontalMargin(layoutBox, usedHorizontalValues);
Optional<LayoutUnit> usedMarginStart = computedHorizontalMargin.start;
Optional<LayoutUnit> usedMarginEnd = computedHorizontalMargin.end;
auto width = inlineReplacedWidthAndMargin(layoutBox, usedHorizontalValues, usedVerticalValues).contentWidth;
auto paddingLeft = boxGeometry.paddingLeft().valueOr(0);
auto paddingRight = boxGeometry.paddingRight().valueOr(0);
auto borderLeft = boxGeometry.borderLeft();
auto borderRight = boxGeometry.borderRight();
if (!left && !right) {
auto staticHorizontalPosition = staticHorizontalPositionForOutOfFlowPositioned(layoutBox, usedHorizontalValues);
if (isLeftToRightDirection)
left = staticHorizontalPosition;
else
right = staticHorizontalPosition;
}
if (!left || !right) {
usedMarginStart = computedHorizontalMargin.start.valueOr(0);
usedMarginEnd = computedHorizontalMargin.end.valueOr(0);
}
if (!usedMarginStart && !usedMarginEnd) {
auto marginStartAndEnd = containingBlockWidth - (*left + borderLeft + paddingLeft + width + paddingRight + borderRight + *right);
if (marginStartAndEnd >= 0) {
usedMarginStart = marginStartAndEnd / 2;
usedMarginEnd = usedMarginStart;
} else {
if (isLeftToRightDirection) {
usedMarginStart = 0_lu;
usedMarginEnd = containingBlockWidth - (*left + *usedMarginStart + borderLeft + paddingLeft + width + paddingRight + borderRight + *right);
} else {
usedMarginEnd = 0_lu;
usedMarginStart = containingBlockWidth - (*left + borderLeft + paddingLeft + width + paddingRight + borderRight + *usedMarginEnd + *right);
}
}
}
if (!left)
left = containingBlockWidth - (*usedMarginStart + borderLeft + paddingLeft + width + paddingRight + borderRight + *usedMarginEnd + *right);
if (!right)
right = containingBlockWidth - (*left + *usedMarginStart + borderLeft + paddingLeft + width + paddingRight + borderRight + *usedMarginEnd);
if (!usedMarginStart)
usedMarginStart = containingBlockWidth - (*left + borderLeft + paddingLeft + width + paddingRight + borderRight + *usedMarginEnd + *right);
if (!usedMarginEnd)
usedMarginEnd = containingBlockWidth - (*left + *usedMarginStart + borderLeft + paddingLeft + width + paddingRight + borderRight + *right);
auto boxWidth = (*left + *usedMarginStart + borderLeft + paddingLeft + width + paddingRight + borderRight + *usedMarginEnd + *right);
if (boxWidth > containingBlockWidth) {
if (isLeftToRightDirection)
right = containingBlockWidth - (*left + *usedMarginStart + borderLeft + paddingLeft + width + paddingRight + borderRight + *usedMarginEnd);
else
left = containingBlockWidth - (*usedMarginStart + borderLeft + paddingLeft + width + paddingRight + borderRight + *usedMarginEnd + *right);
}
ASSERT(left);
ASSERT(right);
ASSERT(usedMarginStart);
ASSERT(usedMarginEnd);
auto containingBlockPaddingVerticalEdge = usedHorizontalValues.constraints.contentBoxLeft;
*left += containingBlockPaddingVerticalEdge;
*right += containingBlockPaddingVerticalEdge;
LOG_WITH_STREAM(FormattingContextLayout, stream << "[Position][Width][Margin] -> out-of-flow replaced -> left(" << *left << "px) right(" << *right << "px) width(" << width << "px) margin(" << *usedMarginStart << "px, " << *usedMarginEnd << "px) layoutBox(" << &layoutBox << ")");
return { *left, *right, { width, { *usedMarginStart, *usedMarginEnd }, computedHorizontalMargin } };
}
ContentHeightAndMargin FormattingContext::Geometry::complicatedCases(const Box& layoutBox, const UsedHorizontalValues& usedHorizontalValues, const UsedVerticalValues& usedVerticalValues) const
{
ASSERT(!layoutBox.replaced());
ASSERT((layoutBox.isBlockLevelBox() && layoutBox.isInFlow() && !layoutBox.isOverflowVisible()) || layoutBox.isInlineBlockBox() || layoutBox.isFloatingPositioned() || layoutBox.isDocumentBox() || layoutBox.isTableBox());
auto height = usedVerticalValues.height ? usedVerticalValues.height.value() : computedContentHeight(layoutBox);
auto computedVerticalMargin = Geometry::computedVerticalMargin(layoutBox, usedHorizontalValues);
auto usedVerticalMargin = UsedVerticalMargin::NonCollapsedValues { computedVerticalMargin.before.valueOr(0), computedVerticalMargin.after.valueOr(0) };
if (!height) {
ASSERT(isHeightAuto(layoutBox));
height = contentHeightForFormattingContextRoot(layoutBox);
}
ASSERT(height);
LOG_WITH_STREAM(FormattingContextLayout, stream << "[Height][Margin] -> floating non-replaced -> height(" << *height << "px) margin(" << usedVerticalMargin.before << "px, " << usedVerticalMargin.after << "px) -> layoutBox(" << &layoutBox << ")");
return ContentHeightAndMargin { *height, usedVerticalMargin };
}
ContentWidthAndMargin FormattingContext::Geometry::floatingNonReplacedWidthAndMargin(const Box& layoutBox, const UsedHorizontalValues& usedHorizontalValues)
{
ASSERT(layoutBox.isFloatingPositioned() && !layoutBox.replaced());
auto computedHorizontalMargin = Geometry::computedHorizontalMargin(layoutBox, usedHorizontalValues);
auto usedHorizontallMargin = UsedHorizontalMargin { computedHorizontalMargin.start.valueOr(0), computedHorizontalMargin.end.valueOr(0) };
auto width = usedHorizontalValues.width ? usedHorizontalValues.width : computedContentWidth(layoutBox, usedHorizontalValues.constraints.width);
if (!width)
width = shrinkToFitWidth(layoutBox, usedHorizontalValues.constraints.width);
LOG_WITH_STREAM(FormattingContextLayout, stream << "[Width][Margin] -> floating non-replaced -> width(" << *width << "px) margin(" << usedHorizontallMargin.start << "px, " << usedHorizontallMargin.end << "px) -> layoutBox(" << &layoutBox << ")");
return ContentWidthAndMargin { *width, usedHorizontallMargin, computedHorizontalMargin };
}
ContentHeightAndMargin FormattingContext::Geometry::floatingReplacedHeightAndMargin(const Box& layoutBox, const UsedHorizontalValues& usedHorizontalValues, const UsedVerticalValues& usedVerticalValues) const
{
ASSERT(layoutBox.isFloatingPositioned() && layoutBox.replaced());
LOG_WITH_STREAM(FormattingContextLayout, stream << "[Height][Margin] -> floating replaced -> redirected to inline replaced");
return inlineReplacedHeightAndMargin(layoutBox, usedHorizontalValues, usedVerticalValues);
}
ContentWidthAndMargin FormattingContext::Geometry::floatingReplacedWidthAndMargin(const Box& layoutBox, const UsedHorizontalValues& usedHorizontalValues) const
{
ASSERT(layoutBox.isFloatingPositioned() && layoutBox.replaced());
auto computedHorizontalMargin = Geometry::computedHorizontalMargin(layoutBox, usedHorizontalValues);
LOG_WITH_STREAM(FormattingContextLayout, stream << "[Height][Margin] -> floating replaced -> redirected to inline replaced");
auto usedMargin = UsedHorizontalMargin { computedHorizontalMargin.start.valueOr(0), computedHorizontalMargin.end.valueOr(0) };
return inlineReplacedWidthAndMargin(layoutBox, UsedHorizontalValues { usedHorizontalValues.constraints, usedHorizontalValues.width, usedMargin });
}
VerticalGeometry FormattingContext::Geometry::outOfFlowVerticalGeometry(const Box& layoutBox, const UsedHorizontalValues& usedHorizontalValues, const UsedVerticalValues& usedVerticalValues) const
{
ASSERT(layoutBox.isOutOfFlowPositioned());
if (!layoutBox.replaced())
return outOfFlowNonReplacedVerticalGeometry(layoutBox, usedHorizontalValues, usedVerticalValues);
return outOfFlowReplacedVerticalGeometry(layoutBox, usedHorizontalValues, usedVerticalValues);
}
HorizontalGeometry FormattingContext::Geometry::outOfFlowHorizontalGeometry(const Box& layoutBox, const UsedHorizontalValues& usedHorizontalValues, const UsedVerticalValues& usedVerticalValues)
{
ASSERT(layoutBox.isOutOfFlowPositioned());
if (!layoutBox.replaced())
return outOfFlowNonReplacedHorizontalGeometry(layoutBox, usedHorizontalValues);
return outOfFlowReplacedHorizontalGeometry(layoutBox, usedHorizontalValues, usedVerticalValues);
}
ContentHeightAndMargin FormattingContext::Geometry::floatingHeightAndMargin(const Box& layoutBox, const UsedHorizontalValues& usedHorizontalValues, const UsedVerticalValues& usedVerticalValues) const
{
ASSERT(layoutBox.isFloatingPositioned());
if (!layoutBox.replaced())
return complicatedCases(layoutBox, usedHorizontalValues, usedVerticalValues);
return floatingReplacedHeightAndMargin(layoutBox, usedHorizontalValues, usedVerticalValues);
}
ContentWidthAndMargin FormattingContext::Geometry::floatingWidthAndMargin(const Box& layoutBox, const UsedHorizontalValues& usedHorizontalValues)
{
ASSERT(layoutBox.isFloatingPositioned());
if (!layoutBox.replaced())
return floatingNonReplacedWidthAndMargin(layoutBox, usedHorizontalValues);
return floatingReplacedWidthAndMargin(layoutBox, usedHorizontalValues);
}
ContentHeightAndMargin FormattingContext::Geometry::inlineReplacedHeightAndMargin(const Box& layoutBox, const UsedHorizontalValues& usedHorizontalValues, const UsedVerticalValues& usedVerticalValues) const
{
ASSERT((layoutBox.isOutOfFlowPositioned() || layoutBox.isFloatingPositioned() || layoutBox.isInFlow()) && layoutBox.replaced());
auto& formattingContext = this->formattingContext();
auto computedVerticalMargin = Geometry::computedVerticalMargin(layoutBox, usedHorizontalValues);
auto usedVerticalMargin = UsedVerticalMargin::NonCollapsedValues { computedVerticalMargin.before.valueOr(0), computedVerticalMargin.after.valueOr(0) };
auto& style = layoutBox.style();
auto replaced = layoutBox.replaced();
auto height = usedVerticalValues.height ? usedVerticalValues.height.value() : computedContentHeight(layoutBox, usedVerticalValues.constraints.height);
auto heightIsAuto = !usedVerticalValues.height && isHeightAuto(layoutBox);
auto widthIsAuto = style.logicalWidth().isAuto();
if (heightIsAuto && widthIsAuto && replaced->hasIntrinsicHeight()) {
height = replaced->intrinsicHeight();
} else if (heightIsAuto && replaced->hasIntrinsicRatio()) {
auto usedWidth = formattingContext.geometryForBox(layoutBox).width();
height = usedWidth / replaced->intrinsicRatio();
} else if (heightIsAuto && replaced->hasIntrinsicHeight()) {
height = replaced->intrinsicHeight();
} else if (heightIsAuto) {
height = { 150 };
}
ASSERT(height);
LOG_WITH_STREAM(FormattingContextLayout, stream << "[Height][Margin] -> inflow replaced -> height(" << *height << "px) margin(" << usedVerticalMargin.before << "px, " << usedVerticalMargin.after << "px) -> layoutBox(" << &layoutBox << ")");
return { *height, usedVerticalMargin };
}
ContentWidthAndMargin FormattingContext::Geometry::inlineReplacedWidthAndMargin(const Box& layoutBox, const UsedHorizontalValues& usedHorizontalValues, Optional<UsedVerticalValues> usedVerticalValues) const
{
ASSERT((layoutBox.isOutOfFlowPositioned() || layoutBox.isFloatingPositioned() || layoutBox.isInFlow()) && layoutBox.replaced());
auto computedHorizontalMargin = Geometry::computedHorizontalMargin(layoutBox, usedHorizontalValues);
auto usedMarginStart = [&] {
if (usedHorizontalValues.margin)
return usedHorizontalValues.margin->start;
return computedHorizontalMargin.start.valueOr(0_lu);
};
auto usedMarginEnd = [&] {
if (usedHorizontalValues.margin)
return usedHorizontalValues.margin->end;
return computedHorizontalMargin.end.valueOr(0_lu);
};
auto replaced = layoutBox.replaced();
ASSERT(replaced);
auto width = usedHorizontalValues.width ? usedHorizontalValues.width : computedContentWidth(layoutBox, usedHorizontalValues.constraints.width);
auto heightIsAuto = isHeightAuto(layoutBox);
auto height = computedContentHeight(layoutBox, usedVerticalValues ? usedVerticalValues->constraints.height : WTF::nullopt);
if (!width && heightIsAuto && replaced->hasIntrinsicWidth()) {
width = replaced->intrinsicWidth();
} else if ((!width && heightIsAuto && !replaced->hasIntrinsicWidth() && replaced->hasIntrinsicHeight() && replaced->hasIntrinsicRatio())
|| (!width && height && replaced->hasIntrinsicRatio())) {
width = height.valueOr(replaced->hasIntrinsicHeight()) * replaced->intrinsicRatio();
} else if (!width && heightIsAuto && replaced->hasIntrinsicRatio() && !replaced->hasIntrinsicWidth() && !replaced->hasIntrinsicHeight()) {
ASSERT_NOT_IMPLEMENTED_YET();
} else if (!width && replaced->hasIntrinsicWidth()) {
width = replaced->intrinsicWidth();
} else if (!width) {
width = { 300 };
}
ASSERT(width);
LOG_WITH_STREAM(FormattingContextLayout, stream << "[Width][Margin] -> inflow replaced -> width(" << *width << "px) margin(" << usedMarginStart() << "px, " << usedMarginEnd() << "px) -> layoutBox(" << &layoutBox << ")");
return { *width, { usedMarginStart(), usedMarginEnd() }, computedHorizontalMargin };
}
LayoutSize FormattingContext::Geometry::inFlowPositionedPositionOffset(const Box& layoutBox, const UsedHorizontalValues& usedHorizontalValues) const
{
ASSERT(layoutBox.isInFlowPositioned());
auto& style = layoutBox.style();
auto containingBlockWidth = usedHorizontalValues.constraints.width;
auto top = computedValueIfNotAuto(style.logicalTop(), containingBlockWidth);
auto bottom = computedValueIfNotAuto(style.logicalBottom(), containingBlockWidth);
if (!top && !bottom) {
top = bottom = { 0 };
} else if (!top) {
top = -*bottom;
} else if (!bottom) {
bottom = -*top;
} else {
bottom = WTF::nullopt;
}
auto left = computedValueIfNotAuto(style.logicalLeft(), containingBlockWidth);
auto right = computedValueIfNotAuto(style.logicalRight(), containingBlockWidth);
if (!left && !right) {
left = right = { 0 };
} else if (!left) {
left = -*right;
} else if (!right) {
right = -*left;
} else {
auto isLeftToRightDirection = layoutBox.containingBlock()->style().isLeftToRightDirection();
if (isLeftToRightDirection)
right = -*left;
else
left = WTF::nullopt;
}
ASSERT(!bottom || *top == -*bottom);
ASSERT(!left || *left == -*right);
auto topPositionOffset = *top;
auto leftPositionOffset = left.valueOr(-*right);
LOG_WITH_STREAM(FormattingContextLayout, stream << "[Position] -> positioned inflow -> top offset(" << topPositionOffset << "px) left offset(" << leftPositionOffset << "px) layoutBox(" << &layoutBox << ")");
return { leftPositionOffset, topPositionOffset };
}
Edges FormattingContext::Geometry::computedBorder(const Box& layoutBox) const
{
auto& style = layoutBox.style();
LOG_WITH_STREAM(FormattingContextLayout, stream << "[Border] -> layoutBox: " << &layoutBox);
return {
{ LayoutUnit(style.borderLeft().boxModelWidth()), LayoutUnit(style.borderRight().boxModelWidth()) },
{ LayoutUnit(style.borderTop().boxModelWidth()), LayoutUnit(style.borderBottom().boxModelWidth()) }
};
}
Optional<Edges> FormattingContext::Geometry::computedPadding(const Box& layoutBox, const UsedHorizontalValues& usedHorizontalValues) const
{
if (!layoutBox.isPaddingApplicable())
return WTF::nullopt;
auto& style = layoutBox.style();
auto containingBlockWidth = usedHorizontalValues.constraints.width;
LOG_WITH_STREAM(FormattingContextLayout, stream << "[Padding] -> layoutBox: " << &layoutBox);
return Edges {
{ valueForLength(style.paddingLeft(), containingBlockWidth), valueForLength(style.paddingRight(), containingBlockWidth) },
{ valueForLength(style.paddingTop(), containingBlockWidth), valueForLength(style.paddingBottom(), containingBlockWidth) }
};
}
ComputedHorizontalMargin FormattingContext::Geometry::computedHorizontalMargin(const Box& layoutBox, const UsedHorizontalValues& usedHorizontalValues) const
{
auto& style = layoutBox.style();
auto containingBlockWidth = usedHorizontalValues.constraints.width;
return { computedValueIfNotAuto(style.marginStart(), containingBlockWidth), computedValueIfNotAuto(style.marginEnd(), containingBlockWidth) };
}
ComputedVerticalMargin FormattingContext::Geometry::computedVerticalMargin(const Box& layoutBox, const UsedHorizontalValues& usedHorizontalValues) const
{
auto& style = layoutBox.style();
auto containingBlockWidth = usedHorizontalValues.constraints.width;
return { computedValueIfNotAuto(style.marginBefore(), containingBlockWidth), computedValueIfNotAuto(style.marginAfter(), containingBlockWidth) };
}
FormattingContext::IntrinsicWidthConstraints FormattingContext::Geometry::constrainByMinMaxWidth(const Box& layoutBox, IntrinsicWidthConstraints intrinsicWidth) const
{
auto& style = layoutBox.style();
auto minWidth = fixedValue(style.logicalMinWidth());
auto maxWidth = fixedValue(style.logicalMaxWidth());
if (!minWidth && !maxWidth)
return intrinsicWidth;
if (maxWidth) {
intrinsicWidth.minimum = std::min(*maxWidth, intrinsicWidth.minimum);
intrinsicWidth.maximum = std::min(*maxWidth, intrinsicWidth.maximum);
}
if (minWidth) {
intrinsicWidth.minimum = std::max(*minWidth, intrinsicWidth.minimum);
intrinsicWidth.maximum = std::max(*minWidth, intrinsicWidth.maximum);
}
ASSERT(intrinsicWidth.minimum <= intrinsicWidth.maximum);
return intrinsicWidth;
}
}
}
#endif