This source file includes following definitions.
- m_intrinsicSize
- m_intrinsicSize
- willBeDestroyed
- styleDidChange
- layout
- intrinsicSizeChanged
- paint
- shouldPaint
- firstContainingBlockWithLogicalWidth
- hasReplacedLogicalWidth
- hasReplacedLogicalHeight
- needsPreferredWidthsRecalculation
- rendererHasAspectRatio
- computeAspectRatioInformationForRenderBox
- replacedContentRect
- computeIntrinsicRatioInformation
- computeReplacedLogicalWidth
- computeReplacedLogicalHeight
- computeIntrinsicLogicalWidths
- computePreferredLogicalWidths
- positionForPoint
- selectionRectForRepaint
- localSelectionRect
- setSelectionState
- isSelected
- clippedOverflowRectForRepaint
#include "config.h"
#include "core/rendering/RenderReplaced.h"
#include "RuntimeEnabledFeatures.h"
#include "core/rendering/GraphicsContextAnnotator.h"
#include "core/rendering/LayoutRectRecorder.h"
#include "core/rendering/LayoutRepainter.h"
#include "core/rendering/RenderBlock.h"
#include "core/rendering/RenderImage.h"
#include "core/rendering/RenderLayer.h"
#include "core/rendering/RenderView.h"
#include "platform/LengthFunctions.h"
#include "platform/graphics/GraphicsContext.h"
using namespace std;
namespace WebCore {
const int cDefaultWidth = 300;
const int cDefaultHeight = 150;
RenderReplaced::RenderReplaced(Element* element)
: RenderBox(element)
, m_intrinsicSize(cDefaultWidth, cDefaultHeight)
{
setReplaced(true);
}
RenderReplaced::RenderReplaced(Element* element, const LayoutSize& intrinsicSize)
: RenderBox(element)
, 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() && style()->effectiveZoom() != oldZoom)
intrinsicSizeChanged();
}
void RenderReplaced::layout()
{
ASSERT(needsLayout());
LayoutRectRecorder recorder(*this);
LayoutRepainter repainter(*this, checkForRepaintDuringLayout());
setHeight(minimumReplacedHeight());
updateLogicalWidth();
updateLogicalHeight();
m_overflow.clear();
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();
}
void RenderReplaced::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
{
ANNOTATE_GRAPHICS_CONTEXT(paintInfo, this);
if (!shouldPaint(paintInfo, paintOffset))
return;
LayoutPoint adjustedPaintOffset = paintOffset + location();
if (hasBoxDecorations() && (paintInfo.phase == PaintPhaseForeground || paintInfo.phase == PaintPhaseSelection))
paintBoxDecorations(paintInfo, adjustedPaintOffset);
if (paintInfo.phase == PaintPhaseMask) {
paintMask(paintInfo, adjustedPaintOffset);
return;
}
if (paintInfo.phase == PaintPhaseClippingMask && (!hasLayer() || !layer()->hasCompositedClippingMask()))
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() && paintInfo.phase != PaintPhaseClippingMask)
return;
if (!paintInfo.shouldPaintWithinRoot(this))
return;
bool drawSelectionTint = selectionState() != SelectionNone && !document().printing();
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();
RoundedRect roundedInnerRect = style()->getRoundedInnerBorderFor(paintRect,
paddingTop() + borderTop(), paddingBottom() + borderBottom(), paddingLeft() + borderLeft(), paddingRight() + borderRight(), true, true);
clipRoundedInnerRect(paintInfo.context, paintRect, roundedInnerRect);
}
}
if (!completelyClippedOut) {
if (paintInfo.phase == PaintPhaseClippingMask) {
paintClippingMask(paintInfo, adjustedPaintOffset);
} else {
paintReplaced(paintInfo, adjustedPaintOffset);
}
if (style()->hasBorderRadius())
paintInfo.context->restore();
}
if (drawSelectionTint) {
LayoutRect selectionPaintingRect = localSelectionRect();
selectionPaintingRect.moveBy(adjustedPaintOffset);
paintInfo.context->fillRect(pixelSnappedIntRect(selectionPaintingRect), selectionBackgroundColor());
}
}
bool RenderReplaced::shouldPaint(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
{
if (paintInfo.phase != PaintPhaseForeground && paintInfo.phase != PaintPhaseOutline && paintInfo.phase != PaintPhaseSelfOutline
&& paintInfo.phase != PaintPhaseSelection && paintInfo.phase != PaintPhaseMask && paintInfo.phase != PaintPhaseClippingMask)
return false;
if (!paintInfo.shouldPaintWithinRoot(this))
return false;
if (style()->visibility() != VISIBLE)
return false;
LayoutPoint adjustedPaintOffset = paintOffset + location();
LayoutUnit top = adjustedPaintOffset.y() + visualOverflowRect().y();
LayoutUnit bottom = adjustedPaintOffset.y() + visualOverflowRect().maxY();
if (isSelected() && inlineBoxWrapper()) {
LayoutUnit selTop = paintOffset.y() + inlineBoxWrapper()->root().selectionTop();
LayoutUnit selBottom = paintOffset.y() + selTop + inlineBoxWrapper()->root().selectionHeight();
top = min(selTop, top);
bottom = max(selBottom, bottom);
}
if (adjustedPaintOffset.x() + visualOverflowRect().x() >= paintInfo.rect.maxX() || adjustedPaintOffset.x() + visualOverflowRect().maxX() <= paintInfo.rect.x())
return false;
if (top >= paintInfo.rect.maxY() || bottom <= paintInfo.rect.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()
&& containingBlock->style()->logicalMinWidth().isSpecified()
&& (containingBlock->style()->logicalMaxWidth().isSpecified() || containingBlock->style()->logicalMaxWidth().isUndefined()))
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;
}
bool RenderReplaced::needsPreferredWidthsRecalculation() const
{
return hasRelativeLogicalHeight() && style()->logicalWidth().isAuto() && !hasAutoHeightOrContainingBlockWithAutoHeight();
}
static inline bool rendererHasAspectRatio(const RenderObject* renderer)
{
ASSERT(renderer);
return renderer->isImage() || renderer->isCanvas() || renderer->isVideo();
}
void RenderReplaced::computeAspectRatioInformationForRenderBox(RenderBox* contentRenderer, FloatSize& constrainedSize, double& intrinsicRatio, bool& isPercentageIntrinsicSize) const
{
FloatSize intrinsicSize;
if (contentRenderer) {
contentRenderer->computeIntrinsicRatioInformation(intrinsicSize, intrinsicRatio, isPercentageIntrinsicSize);
if (intrinsicRatio)
ASSERT(!isPercentageIntrinsicSize);
if (!isPercentageIntrinsicSize) {
intrinsicSize.scale(style()->effectiveZoom());
if (isRenderImage())
intrinsicSize.scale(toRenderImage(this)->imageDevicePixelRatio());
}
if (rendererHasAspectRatio(this) && isPercentageIntrinsicSize)
intrinsicRatio = 1;
if (intrinsicRatio && !isPercentageIntrinsicSize && !intrinsicSize.isEmpty())
m_intrinsicSize = LayoutSize(intrinsicSize);
if (!isHorizontalWritingMode()) {
if (intrinsicRatio)
intrinsicRatio = 1 / intrinsicRatio;
intrinsicSize = intrinsicSize.transposedSize();
}
} else {
computeIntrinsicRatioInformation(intrinsicSize, intrinsicRatio, isPercentageIntrinsicSize);
if (intrinsicRatio) {
ASSERT(!isPercentageIntrinsicSize);
if (!intrinsicSize.isEmpty())
m_intrinsicSize = LayoutSize(isHorizontalWritingMode() ? intrinsicSize : intrinsicSize.transposedSize());
}
}
constrainedSize = intrinsicSize;
if (intrinsicRatio && !isPercentageIntrinsicSize && !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* overriddenIntrinsicSize) const
{
LayoutRect contentRect = contentBoxRect();
ObjectFit objectFit = style()->objectFit();
if (objectFit == ObjectFitFill && style()->objectPosition() == RenderStyle::initialObjectPosition()) {
if (!isVideo() || RuntimeEnabledFeatures::objectFitPositionEnabled())
return contentRect;
objectFit = ObjectFitContain;
}
LayoutSize intrinsicSize = overriddenIntrinsicSize ? *overriddenIntrinsicSize : this->intrinsicSize();
if (!intrinsicSize.width() || !intrinsicSize.height())
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;
case ObjectFitNone:
finalRect.setSize(intrinsicSize);
break;
case ObjectFitFill:
break;
default:
ASSERT_NOT_REACHED();
}
LayoutUnit xOffset = minimumValueForLength(style()->objectPosition().x(), contentRect.width() - finalRect.width());
LayoutUnit yOffset = minimumValueForLength(style()->objectPosition().y(), contentRect.height() - finalRect.height());
finalRect.move(xOffset, yOffset);
return finalRect;
}
void RenderReplaced::computeIntrinsicRatioInformation(FloatSize& intrinsicSize, double& intrinsicRatio, bool& isPercentageIntrinsicSize) const
{
ASSERT(!embeddedContentBox());
isPercentageIntrinsicSize = false;
intrinsicSize = FloatSize(intrinsicLogicalWidth().toFloat(), intrinsicLogicalHeight().toFloat());
if (intrinsicSize.isEmpty() || !rendererHasAspectRatio(this))
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();
bool isPercentageIntrinsicSize = false;
double intrinsicRatio = 0;
FloatSize constrainedSize;
computeAspectRatioInformationForRenderBox(contentRenderer, constrainedSize, intrinsicRatio, isPercentageIntrinsicSize);
if (style()->logicalWidth().isAuto()) {
bool heightIsAuto = style()->logicalHeight().isAuto();
bool hasIntrinsicWidth = !isPercentageIntrinsicSize && constrainedSize.width() > 0;
if (heightIsAuto && hasIntrinsicWidth)
return computeReplacedLogicalWidthRespectingMinMaxWidth(constrainedSize.width(), shouldComputePreferred);
bool hasIntrinsicHeight = !isPercentageIntrinsicSize && constrainedSize.height() > 0;
if (intrinsicRatio || isPercentageIntrinsicSize) {
if (intrinsicRatio && ((heightIsAuto && !hasIntrinsicWidth && hasIntrinsicHeight) || !heightIsAuto)) {
LayoutUnit logicalHeight = computeReplacedLogicalHeight();
return computeReplacedLogicalWidthRespectingMinMaxWidth(roundToInt(round(logicalHeight * intrinsicRatio)), shouldComputePreferred);
}
if (heightIsAuto && !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 = max<LayoutUnit>(0, logicalWidth - (marginStart + marginEnd + (width() - clientWidth())));
if (isPercentageIntrinsicSize)
logicalWidth = logicalWidth * constrainedSize.width() / 100;
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();
bool isPercentageIntrinsicSize = false;
double intrinsicRatio = 0;
FloatSize constrainedSize;
computeAspectRatioInformationForRenderBox(contentRenderer, constrainedSize, intrinsicRatio, isPercentageIntrinsicSize);
bool widthIsAuto = style()->logicalWidth().isAuto();
bool hasIntrinsicHeight = !isPercentageIntrinsicSize && 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().isPercent())
computeIntrinsicLogicalWidths(m_minPreferredLogicalWidth, m_maxPreferredLogicalWidth);
else
m_minPreferredLogicalWidth = m_maxPreferredLogicalWidth = computeReplacedLogicalWidth(ComputePreferred);
RenderStyle* styleToUse = style();
if (styleToUse->logicalWidth().isPercent() || styleToUse->logicalMaxWidth().isPercent() || hasRelativeIntrinsicLogicalWidth())
m_minPreferredLogicalWidth = 0;
if (styleToUse->logicalMinWidth().isFixed() && styleToUse->logicalMinWidth().value() > 0) {
m_maxPreferredLogicalWidth = max(m_maxPreferredLogicalWidth, adjustContentBoxLogicalWidthForBoxSizing(styleToUse->logicalMinWidth().value()));
m_minPreferredLogicalWidth = max(m_minPreferredLogicalWidth, adjustContentBoxLogicalWidthForBoxSizing(styleToUse->logicalMinWidth().value()));
}
if (styleToUse->logicalMaxWidth().isFixed()) {
m_maxPreferredLogicalWidth = min(m_maxPreferredLogicalWidth, adjustContentBoxLogicalWidthForBoxSizing(styleToUse->logicalMaxWidth().value()));
m_minPreferredLogicalWidth = min(m_minPreferredLogicalWidth, adjustContentBoxLogicalWidthForBoxSizing(styleToUse->logicalMaxWidth().value()));
}
LayoutUnit borderAndPadding = borderAndPaddingLogicalWidth();
m_minPreferredLogicalWidth += borderAndPadding;
m_maxPreferredLogicalWidth += borderAndPadding;
clearPreferredLogicalWidthsDirty();
}
PositionWithAffinity RenderReplaced::positionForPoint(const LayoutPoint& point)
{
InlineBox* box = inlineBoxWrapper();
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 createPositionWithAffinity(caretMinOffset(), DOWNSTREAM);
if (blockDirectionPosition >= bottom)
return createPositionWithAffinity(caretMaxOffset(), DOWNSTREAM);
if (node()) {
if (lineDirectionPosition <= logicalLeft() + (logicalWidth() / 2))
return createPositionWithAffinity(0, DOWNSTREAM);
return createPositionWithAffinity(1, DOWNSTREAM);
}
return RenderBox::positionForPoint(point);
}
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 (!inlineBoxWrapper())
return LayoutRect(LayoutPoint(), size());
RootInlineBox& root = inlineBoxWrapper()->root();
LayoutUnit newLogicalTop = root.block().style()->isFlippedBlocksWritingMode() ? inlineBoxWrapper()->logicalBottom() - root.selectionBottom() : root.selectionTop() - inlineBoxWrapper()->logicalTop();
if (root.block().style()->isHorizontalWritingMode())
return LayoutRect(0, newLogicalTop, width(), root.selectionHeight());
return LayoutRect(newLogicalTop, 0, root.selectionHeight(), height());
}
void RenderReplaced::setSelectionState(SelectionState state)
{
RenderBox::setSelectionState(state);
if (inlineBoxWrapper() && canUpdateSelectionOnRootLineBoxes())
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 = node()->hasChildren() ? node()->countChildren() : 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());
RenderView* v = view();
if (!RuntimeEnabledFeatures::repaintAfterLayoutEnabled() && v) {
r.move(v->layoutDelta());
}
computeRectForRepaint(repaintContainer, r);
return r;
}
}