RenderReplaced.cpp [plain text]
#include "config.h"
#include "RenderReplaced.h"
#include "FloatRoundedRect.h"
#include "Frame.h"
#include "GraphicsContext.h"
#include "HTMLElement.h"
#include "InlineElementBox.h"
#include "LayoutRepainter.h"
#include "Page.h"
#include "RenderBlock.h"
#include "RenderFlowThread.h"
#include "RenderImage.h"
#include "RenderLayer.h"
#include "RenderNamedFlowFragment.h"
#include "RenderTheme.h"
#include "RenderView.h"
#include "VisiblePosition.h"
#include <wtf/StackStats.h>
namespace WebCore {
const int cDefaultWidth = 300;
const int cDefaultHeight = 150;
RenderReplaced::RenderReplaced(Element& element, Ref<RenderStyle>&& style)
: RenderBox(element, WTF::move(style), RenderReplacedFlag)
, m_intrinsicSize(cDefaultWidth, cDefaultHeight)
{
setReplaced(true);
}
RenderReplaced::RenderReplaced(Element& element, Ref<RenderStyle>&& style, const LayoutSize& intrinsicSize)
: RenderBox(element, WTF::move(style), RenderReplacedFlag)
, m_intrinsicSize(intrinsicSize)
{
setReplaced(true);
}
RenderReplaced::RenderReplaced(Document& document, Ref<RenderStyle>&& style, const LayoutSize& intrinsicSize)
: RenderBox(document, WTF::move(style), RenderReplacedFlag)
, m_intrinsicSize(intrinsicSize)
{
setReplaced(true);
}
RenderReplaced::~RenderReplaced()
{
}
void RenderReplaced::willBeDestroyed()
{
if (!documentBeingDestroyed() && parent())
parent()->dirtyLinesFromChangedChild(*this);
RenderBox::willBeDestroyed();
}
void RenderReplaced::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
{
RenderBox::styleDidChange(diff, oldStyle);
bool hadStyle = (oldStyle != 0);
float oldZoom = hadStyle ? oldStyle->effectiveZoom() : RenderStyle::initialZoom();
if (style().effectiveZoom() != oldZoom)
intrinsicSizeChanged();
}
void RenderReplaced::layout()
{
StackStats::LayoutCheckPoint layoutCheckPoint;
ASSERT(needsLayout());
LayoutRepainter repainter(*this, checkForRepaintDuringLayout());
setHeight(minimumReplacedHeight());
updateLogicalWidth();
updateLogicalHeight();
if (style().aspectRatioType() == AspectRatioFromIntrinsic && !m_intrinsicSize.isEmpty()) {
float aspectRatio = m_intrinsicSize.aspectRatio();
LayoutSize frameSize = size();
float frameAspectRatio = frameSize.aspectRatio();
if (frameAspectRatio < aspectRatio)
setHeight(computeReplacedLogicalHeightRespectingMinMaxHeight(frameSize.height() * frameAspectRatio / aspectRatio));
else if (frameAspectRatio > aspectRatio)
setWidth(computeReplacedLogicalWidthRespectingMinMaxWidth(frameSize.width() * aspectRatio / frameAspectRatio, ComputePreferred));
}
clearOverflow();
addVisualEffectOverflow();
updateLayerTransform();
invalidateBackgroundObscurationStatus();
repainter.repaintAfterLayout();
clearNeedsLayout();
}
void RenderReplaced::intrinsicSizeChanged()
{
int scaledWidth = static_cast<int>(cDefaultWidth * style().effectiveZoom());
int scaledHeight = static_cast<int>(cDefaultHeight * style().effectiveZoom());
m_intrinsicSize = IntSize(scaledWidth, scaledHeight);
setNeedsLayoutAndPrefWidthsRecalc();
}
bool RenderReplaced::shouldDrawSelectionTint() const
{
return selectionState() != SelectionNone && !document().printing();
}
void RenderReplaced::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
{
if (!shouldPaint(paintInfo, paintOffset))
return;
#ifndef NDEBUG
SetLayoutNeededForbiddenScope scope(this);
#endif
LayoutPoint adjustedPaintOffset = paintOffset + location();
if (hasBoxDecorations() && paintInfo.phase == PaintPhaseForeground)
paintBoxDecorations(paintInfo, adjustedPaintOffset);
if (paintInfo.phase == PaintPhaseMask) {
paintMask(paintInfo, adjustedPaintOffset);
return;
}
LayoutRect paintRect = LayoutRect(adjustedPaintOffset, size());
if ((paintInfo.phase == PaintPhaseOutline || paintInfo.phase == PaintPhaseSelfOutline) && style().outlineWidth())
paintOutline(paintInfo, paintRect);
if (paintInfo.phase != PaintPhaseForeground && paintInfo.phase != PaintPhaseSelection && !canHaveChildren())
return;
if (!paintInfo.shouldPaintWithinRoot(*this))
return;
bool drawSelectionTint = shouldDrawSelectionTint();
if (paintInfo.phase == PaintPhaseSelection) {
if (selectionState() == SelectionNone)
return;
drawSelectionTint = false;
}
bool completelyClippedOut = false;
if (style().hasBorderRadius()) {
LayoutRect borderRect = LayoutRect(adjustedPaintOffset, size());
if (borderRect.isEmpty())
completelyClippedOut = true;
else {
paintInfo.context->save();
FloatRoundedRect roundedInnerRect = FloatRoundedRect(style().getRoundedInnerBorderFor(paintRect,
paddingTop() + borderTop(), paddingBottom() + borderBottom(), paddingLeft() + borderLeft(), paddingRight() + borderRight(), true, true));
clipRoundedInnerRect(paintInfo.context, paintRect, roundedInnerRect);
}
}
if (!completelyClippedOut) {
paintReplaced(paintInfo, adjustedPaintOffset);
if (style().hasBorderRadius())
paintInfo.context->restore();
}
if (drawSelectionTint) {
LayoutRect selectionPaintingRect = localSelectionRect();
selectionPaintingRect.moveBy(adjustedPaintOffset);
paintInfo.context->fillRect(snappedIntRect(selectionPaintingRect), selectionBackgroundColor(), style().colorSpace());
}
}
bool RenderReplaced::shouldPaint(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
{
if (paintInfo.phase != PaintPhaseForeground && paintInfo.phase != PaintPhaseOutline && paintInfo.phase != PaintPhaseSelfOutline
&& paintInfo.phase != PaintPhaseSelection && paintInfo.phase != PaintPhaseMask)
return false;
if (!paintInfo.shouldPaintWithinRoot(*this))
return false;
if (style().visibility() != VISIBLE)
return false;
RenderNamedFlowFragment* namedFlowFragment = currentRenderNamedFlowFragment();
if (namedFlowFragment && !namedFlowFragment->flowThread()->objectShouldFragmentInFlowRegion(this, namedFlowFragment))
return false;
LayoutPoint adjustedPaintOffset = paintOffset + location();
LayoutUnit top = adjustedPaintOffset.y() + visualOverflowRect().y();
LayoutUnit bottom = adjustedPaintOffset.y() + visualOverflowRect().maxY();
if (isSelected() && m_inlineBoxWrapper) {
const RootInlineBox& rootBox = m_inlineBoxWrapper->root();
LayoutUnit selTop = paintOffset.y() + rootBox.selectionTop();
LayoutUnit selBottom = paintOffset.y() + selTop + rootBox.selectionHeight();
top = std::min(selTop, top);
bottom = std::max(selBottom, bottom);
}
LayoutRect localRepaintRect = paintInfo.rect;
localRepaintRect.inflate(maximalOutlineSize(paintInfo.phase));
if (adjustedPaintOffset.x() + visualOverflowRect().x() >= localRepaintRect.maxX() || adjustedPaintOffset.x() + visualOverflowRect().maxX() <= localRepaintRect.x())
return false;
if (top >= localRepaintRect.maxY() || bottom <= localRepaintRect.y())
return false;
return true;
}
static inline RenderBlock* firstContainingBlockWithLogicalWidth(const RenderReplaced* replaced)
{
RenderBlock* containingBlock = replaced->containingBlock();
if (!containingBlock)
return 0;
for (; !containingBlock->isRenderView() && !containingBlock->isBody(); containingBlock = containingBlock->containingBlock()) {
if (containingBlock->style().logicalWidth().isSpecified())
return containingBlock;
}
return 0;
}
bool RenderReplaced::hasReplacedLogicalWidth() const
{
if (style().logicalWidth().isSpecified())
return true;
if (style().logicalWidth().isAuto())
return false;
return firstContainingBlockWithLogicalWidth(this);
}
bool RenderReplaced::hasReplacedLogicalHeight() const
{
if (style().logicalHeight().isAuto())
return false;
if (style().logicalHeight().isSpecified()) {
if (hasAutoHeightOrContainingBlockWithAutoHeight())
return false;
return true;
}
if (style().logicalHeight().isIntrinsic())
return true;
return false;
}
void RenderReplaced::computeAspectRatioInformationForRenderBox(RenderBox* contentRenderer, FloatSize& constrainedSize, double& intrinsicRatio) const
{
FloatSize intrinsicSize;
if (contentRenderer) {
contentRenderer->computeIntrinsicRatioInformation(intrinsicSize, intrinsicRatio);
intrinsicSize.scale(style().effectiveZoom());
if (is<RenderImage>(*this))
intrinsicSize.scale(downcast<RenderImage>(*this).imageDevicePixelRatio());
if (intrinsicRatio && !intrinsicSize.isEmpty())
m_intrinsicSize = LayoutSize(intrinsicSize);
if (!isHorizontalWritingMode()) {
if (intrinsicRatio)
intrinsicRatio = 1 / intrinsicRatio;
intrinsicSize = intrinsicSize.transposedSize();
}
} else {
computeIntrinsicRatioInformation(intrinsicSize, intrinsicRatio);
if (intrinsicRatio && !intrinsicSize.isEmpty())
m_intrinsicSize = LayoutSize(isHorizontalWritingMode() ? intrinsicSize : intrinsicSize.transposedSize());
}
constrainedSize = intrinsicSize;
if (intrinsicRatio && !intrinsicSize.isEmpty() && style().logicalWidth().isAuto() && style().logicalHeight().isAuto()) {
constrainedSize.setWidth(RenderBox::computeReplacedLogicalHeight() * intrinsicSize.width() / intrinsicSize.height());
constrainedSize.setHeight(RenderBox::computeReplacedLogicalWidth() * intrinsicSize.height() / intrinsicSize.width());
}
}
LayoutRect RenderReplaced::replacedContentRect(const LayoutSize& intrinsicSize) const
{
LayoutRect contentRect = contentBoxRect();
if (intrinsicSize.isEmpty())
return contentRect;
ObjectFit objectFit = style().objectFit();
if (objectFit == ObjectFitFill)
return contentRect;
LayoutRect finalRect = contentRect;
switch (objectFit) {
case ObjectFitContain:
case ObjectFitScaleDown:
case ObjectFitCover:
finalRect.setSize(finalRect.size().fitToAspectRatio(intrinsicSize, objectFit == ObjectFitCover ? AspectRatioFitGrow : AspectRatioFitShrink));
if (objectFit != ObjectFitScaleDown || finalRect.width() <= intrinsicSize.width())
break;
FALLTHROUGH;
case ObjectFitNone:
finalRect.setSize(intrinsicSize);
break;
case ObjectFitFill:
ASSERT_NOT_REACHED();
}
LayoutUnit xOffset = (contentRect.width() - finalRect.width()) / 2;
LayoutUnit yOffset = (contentRect.height() - finalRect.height()) / 2;
finalRect.move(xOffset, yOffset);
return finalRect;
}
void RenderReplaced::computeIntrinsicRatioInformation(FloatSize& intrinsicSize, double& intrinsicRatio) const
{
ASSERT(!embeddedContentBox());
intrinsicSize = FloatSize(intrinsicLogicalWidth(), intrinsicLogicalHeight());
if (intrinsicSize.isEmpty() || !hasAspectRatio())
return;
intrinsicRatio = intrinsicSize.width() / intrinsicSize.height();
}
LayoutUnit RenderReplaced::computeReplacedLogicalWidth(ShouldComputePreferred shouldComputePreferred) const
{
if (style().logicalWidth().isSpecified() || style().logicalWidth().isIntrinsic())
return computeReplacedLogicalWidthRespectingMinMaxWidth(computeReplacedLogicalWidthUsing(style().logicalWidth()), shouldComputePreferred);
RenderBox* contentRenderer = embeddedContentBox();
double intrinsicRatio = 0;
FloatSize constrainedSize;
computeAspectRatioInformationForRenderBox(contentRenderer, constrainedSize, intrinsicRatio);
if (style().logicalWidth().isAuto()) {
bool computedHeightIsAuto = hasAutoHeightOrContainingBlockWithAutoHeight();
bool hasIntrinsicWidth = constrainedSize.width() > 0;
if (computedHeightIsAuto && hasIntrinsicWidth)
return computeReplacedLogicalWidthRespectingMinMaxWidth(constrainedSize.width(), shouldComputePreferred);
bool hasIntrinsicHeight = constrainedSize.height() > 0;
if (intrinsicRatio) {
if (intrinsicRatio && ((computedHeightIsAuto && !hasIntrinsicWidth && hasIntrinsicHeight) || !computedHeightIsAuto)) {
LayoutUnit logicalHeight = computeReplacedLogicalHeight();
return computeReplacedLogicalWidthRespectingMinMaxWidth(roundToInt(round(logicalHeight * intrinsicRatio)), shouldComputePreferred);
}
if (computedHeightIsAuto && !hasIntrinsicWidth && !hasIntrinsicHeight) {
LayoutUnit logicalWidth;
if (RenderBlock* blockWithWidth = firstContainingBlockWithLogicalWidth(this))
logicalWidth = blockWithWidth->computeReplacedLogicalWidthRespectingMinMaxWidth(blockWithWidth->computeReplacedLogicalWidthUsing(blockWithWidth->style().logicalWidth()), shouldComputePreferred);
else
logicalWidth = containingBlock()->availableLogicalWidth();
LayoutUnit marginStart = minimumValueForLength(style().marginStart(), logicalWidth);
LayoutUnit marginEnd = minimumValueForLength(style().marginEnd(), logicalWidth);
logicalWidth = std::max<LayoutUnit>(0, logicalWidth - (marginStart + marginEnd + (width() - clientWidth())));
return computeReplacedLogicalWidthRespectingMinMaxWidth(logicalWidth, shouldComputePreferred);
}
}
if (hasIntrinsicWidth)
return computeReplacedLogicalWidthRespectingMinMaxWidth(constrainedSize.width(), shouldComputePreferred);
}
return computeReplacedLogicalWidthRespectingMinMaxWidth(intrinsicLogicalWidth(), shouldComputePreferred);
}
LayoutUnit RenderReplaced::computeReplacedLogicalHeight() const
{
if (hasReplacedLogicalHeight())
return computeReplacedLogicalHeightRespectingMinMaxHeight(computeReplacedLogicalHeightUsing(style().logicalHeight()));
RenderBox* contentRenderer = embeddedContentBox();
double intrinsicRatio = 0;
FloatSize constrainedSize;
computeAspectRatioInformationForRenderBox(contentRenderer, constrainedSize, intrinsicRatio);
bool widthIsAuto = style().logicalWidth().isAuto();
bool hasIntrinsicHeight = constrainedSize.height() > 0;
if (widthIsAuto && hasIntrinsicHeight)
return computeReplacedLogicalHeightRespectingMinMaxHeight(constrainedSize.height());
if (intrinsicRatio)
return computeReplacedLogicalHeightRespectingMinMaxHeight(roundToInt(round(availableLogicalWidth() / intrinsicRatio)));
if (hasIntrinsicHeight)
return computeReplacedLogicalHeightRespectingMinMaxHeight(constrainedSize.height());
return computeReplacedLogicalHeightRespectingMinMaxHeight(intrinsicLogicalHeight());
}
void RenderReplaced::computeIntrinsicLogicalWidths(LayoutUnit& minLogicalWidth, LayoutUnit& maxLogicalWidth) const
{
minLogicalWidth = maxLogicalWidth = intrinsicLogicalWidth();
}
void RenderReplaced::computePreferredLogicalWidths()
{
ASSERT(preferredLogicalWidthsDirty());
if (style().logicalWidth().isPercentOrCalculated())
computeIntrinsicLogicalWidths(m_minPreferredLogicalWidth, m_maxPreferredLogicalWidth);
else
m_minPreferredLogicalWidth = m_maxPreferredLogicalWidth = computeReplacedLogicalWidth(ComputePreferred);
const RenderStyle& styleToUse = style();
if (styleToUse.logicalWidth().isPercentOrCalculated() || styleToUse.logicalMaxWidth().isPercentOrCalculated())
m_minPreferredLogicalWidth = 0;
if (styleToUse.logicalMinWidth().isFixed() && styleToUse.logicalMinWidth().value() > 0) {
m_maxPreferredLogicalWidth = std::max(m_maxPreferredLogicalWidth, adjustContentBoxLogicalWidthForBoxSizing(styleToUse.logicalMinWidth().value()));
m_minPreferredLogicalWidth = std::max(m_minPreferredLogicalWidth, adjustContentBoxLogicalWidthForBoxSizing(styleToUse.logicalMinWidth().value()));
}
if (styleToUse.logicalMaxWidth().isFixed()) {
m_maxPreferredLogicalWidth = std::min(m_maxPreferredLogicalWidth, adjustContentBoxLogicalWidthForBoxSizing(styleToUse.logicalMaxWidth().value()));
m_minPreferredLogicalWidth = std::min(m_minPreferredLogicalWidth, adjustContentBoxLogicalWidthForBoxSizing(styleToUse.logicalMaxWidth().value()));
}
LayoutUnit borderAndPadding = borderAndPaddingLogicalWidth();
m_minPreferredLogicalWidth += borderAndPadding;
m_maxPreferredLogicalWidth += borderAndPadding;
setPreferredLogicalWidthsDirty(false);
}
VisiblePosition RenderReplaced::positionForPoint(const LayoutPoint& point, const RenderRegion* region)
{
InlineBox* box = inlineBoxWrapper();
const RootInlineBox* rootBox = box ? &box->root() : 0;
LayoutUnit top = rootBox ? rootBox->selectionTop() : logicalTop();
LayoutUnit bottom = rootBox ? rootBox->selectionBottom() : logicalBottom();
LayoutUnit blockDirectionPosition = isHorizontalWritingMode() ? point.y() + y() : point.x() + x();
LayoutUnit lineDirectionPosition = isHorizontalWritingMode() ? point.x() + x() : point.y() + y();
if (blockDirectionPosition < top)
return createVisiblePosition(caretMinOffset(), DOWNSTREAM);
if (blockDirectionPosition >= bottom)
return createVisiblePosition(caretMaxOffset(), DOWNSTREAM);
if (element()) {
if (lineDirectionPosition <= logicalLeft() + (logicalWidth() / 2))
return createVisiblePosition(0, DOWNSTREAM);
return createVisiblePosition(1, DOWNSTREAM);
}
return RenderBox::positionForPoint(point, region);
}
LayoutRect RenderReplaced::selectionRectForRepaint(const RenderLayerModelObject* repaintContainer, bool clipToVisibleContent)
{
ASSERT(!needsLayout());
if (!isSelected())
return LayoutRect();
LayoutRect rect = localSelectionRect();
if (clipToVisibleContent)
computeRectForRepaint(repaintContainer, rect);
else
rect = localToContainerQuad(FloatRect(rect), repaintContainer).enclosingBoundingBox();
return rect;
}
LayoutRect RenderReplaced::localSelectionRect(bool checkWhetherSelected) const
{
if (checkWhetherSelected && !isSelected())
return LayoutRect();
if (!m_inlineBoxWrapper)
return LayoutRect(LayoutPoint(), size());
const RootInlineBox& rootBox = m_inlineBoxWrapper->root();
LayoutUnit newLogicalTop = rootBox.blockFlow().style().isFlippedBlocksWritingMode() ? m_inlineBoxWrapper->logicalBottom() - rootBox.selectionBottom() : rootBox.selectionTop() - m_inlineBoxWrapper->logicalTop();
if (rootBox.blockFlow().style().isHorizontalWritingMode())
return LayoutRect(0, newLogicalTop, width(), rootBox.selectionHeight());
return LayoutRect(newLogicalTop, 0, rootBox.selectionHeight(), height());
}
void RenderReplaced::setSelectionState(SelectionState state)
{
RenderBox::setSelectionState(state);
if (m_inlineBoxWrapper && canUpdateSelectionOnRootLineBoxes())
m_inlineBoxWrapper->root().setHasSelectedChildren(isSelected());
}
bool RenderReplaced::isSelected() const
{
SelectionState s = selectionState();
if (s == SelectionNone)
return false;
if (s == SelectionInside)
return true;
int selectionStart, selectionEnd;
selectionStartEnd(selectionStart, selectionEnd);
if (s == SelectionStart)
return selectionStart == 0;
int end = element()->hasChildNodes() ? element()->countChildNodes() : 1;
if (s == SelectionEnd)
return selectionEnd == end;
if (s == SelectionBoth)
return selectionStart == 0 && selectionEnd == end;
ASSERT(0);
return false;
}
LayoutRect RenderReplaced::clippedOverflowRectForRepaint(const RenderLayerModelObject* repaintContainer) const
{
if (style().visibility() != VISIBLE && !enclosingLayer()->hasVisibleContent())
return LayoutRect();
LayoutRect r = unionRect(localSelectionRect(false), visualOverflowRect());
r.move(view().layoutDelta());
r.inflate(style().outlineSize());
computeRectForRepaint(repaintContainer, r);
return r;
}
}