#include "config.h"
#include "RenderBox.h"
#include "CachedImage.h"
#include "Chrome.h"
#include "ChromeClient.h"
#include "Document.h"
#include "FrameView.h"
#include "GraphicsContext.h"
#include "HitTestResult.h"
#include "htmlediting.h"
#include "HTMLElement.h"
#include "HTMLNames.h"
#include "ImageBuffer.h"
#include "FloatQuad.h"
#include "Frame.h"
#include "Page.h"
#include "PaintInfo.h"
#include "RenderArena.h"
#include "RenderFlexibleBox.h"
#include "RenderInline.h"
#include "RenderLayer.h"
#include "RenderTableCell.h"
#include "RenderTheme.h"
#include "RenderView.h"
#include "ScrollbarTheme.h"
#include "TransformState.h"
#include <algorithm>
#include <math.h>
using namespace std;
namespace WebCore {
using namespace HTMLNames;
typedef WTF::HashMap<const RenderBox*, int> OverrideSizeMap;
static OverrideSizeMap* gOverrideSizeMap = 0;
bool RenderBox::s_hadOverflowClip = false;
RenderBox::RenderBox(Node* node)
: RenderBoxModelObject(node)
, m_marginLeft(0)
, m_marginRight(0)
, m_marginTop(0)
, m_marginBottom(0)
, m_minPreferredLogicalWidth(-1)
, m_maxPreferredLogicalWidth(-1)
, m_inlineBoxWrapper(0)
{
setIsBox();
}
RenderBox::~RenderBox()
{
}
int RenderBox::marginBefore() const
{
switch (style()->writingMode()) {
case TopToBottomWritingMode:
return m_marginTop;
case BottomToTopWritingMode:
return m_marginBottom;
case LeftToRightWritingMode:
return m_marginLeft;
case RightToLeftWritingMode:
return m_marginRight;
}
ASSERT_NOT_REACHED();
return m_marginTop;
}
int RenderBox::marginAfter() const
{
switch (style()->writingMode()) {
case TopToBottomWritingMode:
return m_marginBottom;
case BottomToTopWritingMode:
return m_marginTop;
case LeftToRightWritingMode:
return m_marginRight;
case RightToLeftWritingMode:
return m_marginLeft;
}
ASSERT_NOT_REACHED();
return m_marginBottom;
}
int RenderBox::marginStart() const
{
if (isHorizontalWritingMode())
return style()->isLeftToRightDirection() ? m_marginLeft : m_marginRight;
return style()->isLeftToRightDirection() ? m_marginTop : m_marginBottom;
}
int RenderBox::marginEnd() const
{
if (isHorizontalWritingMode())
return style()->isLeftToRightDirection() ? m_marginRight : m_marginLeft;
return style()->isLeftToRightDirection() ? m_marginBottom : m_marginTop;
}
void RenderBox::setMarginStart(int margin)
{
if (isHorizontalWritingMode()) {
if (style()->isLeftToRightDirection())
m_marginLeft = margin;
else
m_marginRight = margin;
} else {
if (style()->isLeftToRightDirection())
m_marginTop = margin;
else
m_marginBottom = margin;
}
}
void RenderBox::setMarginEnd(int margin)
{
if (isHorizontalWritingMode()) {
if (style()->isLeftToRightDirection())
m_marginRight = margin;
else
m_marginLeft = margin;
} else {
if (style()->isLeftToRightDirection())
m_marginBottom = margin;
else
m_marginTop = margin;
}
}
void RenderBox::setMarginBefore(int margin)
{
switch (style()->writingMode()) {
case TopToBottomWritingMode:
m_marginTop = margin;
break;
case BottomToTopWritingMode:
m_marginBottom = margin;
break;
case LeftToRightWritingMode:
m_marginLeft = margin;
break;
case RightToLeftWritingMode:
m_marginRight = margin;
break;
}
}
void RenderBox::setMarginAfter(int margin)
{
switch (style()->writingMode()) {
case TopToBottomWritingMode:
m_marginBottom = margin;
break;
case BottomToTopWritingMode:
m_marginTop = margin;
break;
case LeftToRightWritingMode:
m_marginRight = margin;
break;
case RightToLeftWritingMode:
m_marginLeft = margin;
break;
}
}
void RenderBox::destroy()
{
if (hasOverrideSize())
gOverrideSizeMap->remove(this);
if (style() && (style()->logicalHeight().isPercent() || style()->logicalMinHeight().isPercent() || style()->logicalMaxHeight().isPercent()))
RenderBlock::removePercentHeightDescendant(this);
if (style() && style()->hasPseudoStyle(SCROLLBAR) && frame() && frame()->view())
frame()->view()->clearOwningRendererForCustomScrollbars(this);
RenderBoxModelObject::destroy();
}
void RenderBox::removeFloatingOrPositionedChildFromBlockLists()
{
ASSERT(isFloatingOrPositioned());
if (documentBeingDestroyed())
return;
if (isFloating()) {
RenderBlock* parentBlock = 0;
for (RenderObject* curr = parent(); curr && !curr->isRenderView(); curr = curr->parent()) {
if (curr->isRenderBlock()) {
RenderBlock* currBlock = toRenderBlock(curr);
if (!parentBlock || currBlock->containsFloat(this))
parentBlock = currBlock;
}
}
if (parentBlock) {
RenderObject* parent = parentBlock->parent();
if (parent && parent->isFlexibleBox())
parentBlock = toRenderBlock(parent);
parentBlock->markAllDescendantsWithFloatsForLayout(this, false);
}
}
if (isPositioned()) {
for (RenderObject* curr = parent(); curr; curr = curr->parent()) {
if (curr->isRenderBlock())
toRenderBlock(curr)->removePositionedObject(this);
}
}
}
void RenderBox::styleWillChange(StyleDifference diff, const RenderStyle* newStyle)
{
s_hadOverflowClip = hasOverflowClip();
if (style()) {
if (diff >= StyleDifferenceRepaint && node() &&
(node()->hasTagName(htmlTag) || node()->hasTagName(bodyTag)))
view()->repaint();
if (diff == StyleDifferenceLayout && parent() && style()->position() != newStyle->position()) {
markContainingBlocksForLayout();
if (style()->position() == StaticPosition)
repaint();
else if (newStyle->position() == AbsolutePosition || newStyle->position() == FixedPosition)
parent()->setChildNeedsLayout(true);
if (isFloating() && !isPositioned() && (newStyle->position() == AbsolutePosition || newStyle->position() == FixedPosition))
removeFloatingOrPositionedChildFromBlockLists();
}
} else if (newStyle && isBody())
view()->repaint();
if (FrameView *frameView = view()->frameView()) {
bool newStyleIsFixed = newStyle && newStyle->position() == FixedPosition;
bool oldStyleIsFixed = style() && style()->position() == FixedPosition;
if (newStyleIsFixed != oldStyleIsFixed) {
if (newStyleIsFixed)
frameView->addFixedObject();
else
frameView->removeFixedObject();
}
}
RenderBoxModelObject::styleWillChange(diff, newStyle);
}
void RenderBox::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
{
RenderBoxModelObject::styleDidChange(diff, oldStyle);
if (needsLayout() && oldStyle) {
if (oldStyle && (oldStyle->logicalHeight().isPercent() || oldStyle->logicalMinHeight().isPercent() || oldStyle->logicalMaxHeight().isPercent()))
RenderBlock::removePercentHeightDescendant(this);
if (isPositioned() && style()->hasStaticBlockPosition(isHorizontalWritingMode()) && oldStyle->marginBefore() != style()->marginBefore()
&& parent() && !parent()->normalChildNeedsLayout())
parent()->setChildNeedsLayout(true);
}
if (hasOverflowClip() && oldStyle && style() && oldStyle->effectiveZoom() != style()->effectiveZoom()) {
if (int left = layer()->scrollXOffset()) {
left = (left / oldStyle->effectiveZoom()) * style()->effectiveZoom();
layer()->scrollToXOffset(left);
}
if (int top = layer()->scrollYOffset()) {
top = (top / oldStyle->effectiveZoom()) * style()->effectiveZoom();
layer()->scrollToYOffset(top);
}
}
bool isBodyRenderer = isBody();
bool isRootRenderer = isRoot();
if (isBodyRenderer)
document()->setTextColor(style()->visitedDependentColor(CSSPropertyColor));
if (isRootRenderer || isBodyRenderer) {
RenderView* viewRenderer = view();
RenderStyle* viewStyle = viewRenderer->style();
if (viewStyle->direction() != style()->direction() && (isRootRenderer || !document()->directionSetOnDocumentElement())) {
viewStyle->setDirection(style()->direction());
if (isBodyRenderer)
document()->documentElement()->renderer()->style()->setDirection(style()->direction());
setNeedsLayoutAndPrefWidthsRecalc();
}
if (viewStyle->writingMode() != style()->writingMode() && (isRootRenderer || !document()->writingModeSetOnDocumentElement())) {
viewStyle->setWritingMode(style()->writingMode());
viewRenderer->setHorizontalWritingMode(style()->isHorizontalWritingMode());
if (isBodyRenderer) {
document()->documentElement()->renderer()->style()->setWritingMode(style()->writingMode());
document()->documentElement()->renderer()->setHorizontalWritingMode(style()->isHorizontalWritingMode());
}
setNeedsLayoutAndPrefWidthsRecalc();
}
frame()->view()->recalculateScrollbarOverlayStyle();
}
}
void RenderBox::updateBoxModelInfoFromStyle()
{
RenderBoxModelObject::updateBoxModelInfoFromStyle();
bool isRootObject = isRoot();
bool isViewObject = isRenderView();
if (isRootObject || isViewObject)
setHasBoxDecorations(true);
setPositioned(style()->position() == AbsolutePosition || style()->position() == FixedPosition);
setFloating(!isPositioned() && style()->isFloating());
if (style()->overflowX() != OVISIBLE && !isRootObject && (isRenderBlock() || isTableRow() || isTableSection())) {
bool boxHasOverflowClip = true;
if (isBody()) {
if (document()->documentElement()->hasTagName(htmlTag) &&
document()->body() == node() &&
document()->documentElement()->renderer()->style()->overflowX() == OVISIBLE)
boxHasOverflowClip = false;
}
if (boxHasOverflowClip) {
if (!s_hadOverflowClip)
repaint();
setHasOverflowClip();
}
}
setHasTransform(style()->hasTransformRelatedProperty());
setHasReflection(style()->boxReflect());
}
void RenderBox::layout()
{
ASSERT(needsLayout());
RenderObject* child = firstChild();
if (!child) {
setNeedsLayout(false);
return;
}
LayoutStateMaintainer statePusher(view(), this, IntSize(x(), y()), style()->isFlippedBlocksWritingMode());
while (child) {
child->layoutIfNeeded();
ASSERT(!child->needsLayout());
child = child->nextSibling();
}
statePusher.pop();
setNeedsLayout(false);
}
int RenderBox::clientWidth() const
{
return width() - borderLeft() - borderRight() - verticalScrollbarWidth();
}
int RenderBox::clientHeight() const
{
return height() - borderTop() - borderBottom() - horizontalScrollbarHeight();
}
int RenderBox::scrollWidth() const
{
if (hasOverflowClip())
return layer()->scrollWidth();
if (style()->isLeftToRightDirection())
return max(clientWidth(), maxXLayoutOverflow() - borderLeft());
return clientWidth() - min(0, minXLayoutOverflow() - borderLeft());
}
int RenderBox::scrollHeight() const
{
if (hasOverflowClip())
return layer()->scrollHeight();
return max(clientHeight(), maxYLayoutOverflow() - borderTop());
}
int RenderBox::scrollLeft() const
{
return hasOverflowClip() ? layer()->scrollXOffset() : 0;
}
int RenderBox::scrollTop() const
{
return hasOverflowClip() ? layer()->scrollYOffset() : 0;
}
void RenderBox::setScrollLeft(int newLeft)
{
if (hasOverflowClip())
layer()->scrollToXOffset(newLeft);
}
void RenderBox::setScrollTop(int newTop)
{
if (hasOverflowClip())
layer()->scrollToYOffset(newTop);
}
void RenderBox::absoluteRects(Vector<IntRect>& rects, int tx, int ty)
{
rects.append(IntRect(tx, ty, width(), height()));
}
void RenderBox::absoluteQuads(Vector<FloatQuad>& quads)
{
quads.append(localToAbsoluteQuad(FloatRect(0, 0, width(), height())));
}
void RenderBox::updateLayerTransform()
{
if (hasLayer())
layer()->updateTransform();
}
IntRect RenderBox::absoluteContentBox() const
{
IntRect rect = contentBoxRect();
FloatPoint absPos = localToAbsolute(FloatPoint());
rect.move(absPos.x(), absPos.y());
return rect;
}
FloatQuad RenderBox::absoluteContentQuad() const
{
IntRect rect = contentBoxRect();
return localToAbsoluteQuad(FloatRect(rect));
}
IntRect RenderBox::outlineBoundsForRepaint(RenderBoxModelObject* repaintContainer, IntPoint* cachedOffsetToRepaintContainer) const
{
IntRect box = borderBoundingBox();
adjustRectForOutlineAndShadow(box);
FloatQuad containerRelativeQuad = FloatRect(box);
if (cachedOffsetToRepaintContainer)
containerRelativeQuad.move(cachedOffsetToRepaintContainer->x(), cachedOffsetToRepaintContainer->y());
else
containerRelativeQuad = localToContainerQuad(containerRelativeQuad, repaintContainer);
box = containerRelativeQuad.enclosingBoundingBox();
box.move(view()->layoutDelta());
return box;
}
void RenderBox::addFocusRingRects(Vector<IntRect>& rects, int tx, int ty)
{
if (width() && height())
rects.append(IntRect(tx, ty, width(), height()));
}
IntRect RenderBox::reflectionBox() const
{
IntRect result;
if (!style()->boxReflect())
return result;
IntRect box = borderBoxRect();
result = box;
switch (style()->boxReflect()->direction()) {
case ReflectionBelow:
result.move(0, box.height() + reflectionOffset());
break;
case ReflectionAbove:
result.move(0, -box.height() - reflectionOffset());
break;
case ReflectionLeft:
result.move(-box.width() - reflectionOffset(), 0);
break;
case ReflectionRight:
result.move(box.width() + reflectionOffset(), 0);
break;
}
return result;
}
int RenderBox::reflectionOffset() const
{
if (!style()->boxReflect())
return 0;
if (style()->boxReflect()->direction() == ReflectionLeft || style()->boxReflect()->direction() == ReflectionRight)
return style()->boxReflect()->offset().calcValue(borderBoxRect().width());
return style()->boxReflect()->offset().calcValue(borderBoxRect().height());
}
IntRect RenderBox::reflectedRect(const IntRect& r) const
{
if (!style()->boxReflect())
return IntRect();
IntRect box = borderBoxRect();
IntRect result = r;
switch (style()->boxReflect()->direction()) {
case ReflectionBelow:
result.setY(box.maxY() + reflectionOffset() + (box.maxY() - r.maxY()));
break;
case ReflectionAbove:
result.setY(box.y() - reflectionOffset() - box.height() + (box.maxY() - r.maxY()));
break;
case ReflectionLeft:
result.setX(box.x() - reflectionOffset() - box.width() + (box.maxX() - r.maxX()));
break;
case ReflectionRight:
result.setX(box.maxX() + reflectionOffset() + (box.maxX() - r.maxX()));
break;
}
return result;
}
bool RenderBox::includeVerticalScrollbarSize() const
{
return hasOverflowClip() && !layer()->hasOverlayScrollbars()
&& (style()->overflowY() == OSCROLL || style()->overflowY() == OAUTO);
}
bool RenderBox::includeHorizontalScrollbarSize() const
{
return hasOverflowClip() && !layer()->hasOverlayScrollbars()
&& (style()->overflowX() == OSCROLL || style()->overflowX() == OAUTO);
}
int RenderBox::verticalScrollbarWidth() const
{
return includeVerticalScrollbarSize() ? layer()->verticalScrollbarWidth() : 0;
}
int RenderBox::horizontalScrollbarHeight() const
{
return includeHorizontalScrollbarSize() ? layer()->horizontalScrollbarHeight() : 0;
}
bool RenderBox::scroll(ScrollDirection direction, ScrollGranularity granularity, float multiplier, Node** stopNode)
{
RenderLayer* l = layer();
if (l && l->scroll(direction, granularity, multiplier)) {
if (stopNode)
*stopNode = node();
return true;
}
if (stopNode && *stopNode && *stopNode == node())
return true;
RenderBlock* b = containingBlock();
if (b && !b->isRenderView())
return b->scroll(direction, granularity, multiplier, stopNode);
return false;
}
bool RenderBox::logicalScroll(ScrollLogicalDirection direction, ScrollGranularity granularity, float multiplier, Node** stopNode)
{
bool scrolled = false;
RenderLayer* l = layer();
if (l) {
#if PLATFORM(MAC)
if (granularity == ScrollByDocument)
scrolled = l->scroll(logicalToPhysical(ScrollInlineDirectionBackward, isHorizontalWritingMode(), style()->isFlippedBlocksWritingMode()), ScrollByDocument, multiplier);
#endif
if (l->scroll(logicalToPhysical(direction, isHorizontalWritingMode(), style()->isFlippedBlocksWritingMode()), granularity, multiplier))
scrolled = true;
if (scrolled) {
if (stopNode)
*stopNode = node();
return true;
}
}
if (stopNode && *stopNode && *stopNode == node())
return true;
RenderBlock* b = containingBlock();
if (b && !b->isRenderView())
return b->logicalScroll(direction, granularity, multiplier, stopNode);
return false;
}
bool RenderBox::canBeScrolledAndHasScrollableArea() const
{
return canBeProgramaticallyScrolled(false) && (scrollHeight() != clientHeight() || scrollWidth() != clientWidth());
}
bool RenderBox::canBeProgramaticallyScrolled(bool) const
{
return (hasOverflowClip() && (scrollsOverflow() || (node() && node()->rendererIsEditable()))) || (node() && node()->isDocumentNode());
}
void RenderBox::autoscroll()
{
if (layer())
layer()->autoscroll();
}
void RenderBox::panScroll(const IntPoint& source)
{
if (layer())
layer()->panScrollFromPoint(source);
}
int RenderBox::minPreferredLogicalWidth() const
{
if (preferredLogicalWidthsDirty())
const_cast<RenderBox*>(this)->computePreferredLogicalWidths();
return m_minPreferredLogicalWidth;
}
int RenderBox::maxPreferredLogicalWidth() const
{
if (preferredLogicalWidthsDirty())
const_cast<RenderBox*>(this)->computePreferredLogicalWidths();
return m_maxPreferredLogicalWidth;
}
int RenderBox::overrideSize() const
{
if (!hasOverrideSize())
return -1;
return gOverrideSizeMap->get(this);
}
void RenderBox::setOverrideSize(int s)
{
if (s == -1) {
if (hasOverrideSize()) {
setHasOverrideSize(false);
gOverrideSizeMap->remove(this);
}
} else {
if (!gOverrideSizeMap)
gOverrideSizeMap = new OverrideSizeMap();
setHasOverrideSize(true);
gOverrideSizeMap->set(this, s);
}
}
int RenderBox::overrideWidth() const
{
return hasOverrideSize() ? overrideSize() : width();
}
int RenderBox::overrideHeight() const
{
return hasOverrideSize() ? overrideSize() : height();
}
int RenderBox::computeBorderBoxLogicalWidth(int width) const
{
int bordersPlusPadding = borderAndPaddingLogicalWidth();
if (style()->boxSizing() == CONTENT_BOX)
return width + bordersPlusPadding;
return max(width, bordersPlusPadding);
}
int RenderBox::computeBorderBoxLogicalHeight(int height) const
{
int bordersPlusPadding = borderAndPaddingLogicalHeight();
if (style()->boxSizing() == CONTENT_BOX)
return height + bordersPlusPadding;
return max(height, bordersPlusPadding);
}
int RenderBox::computeContentBoxLogicalWidth(int width) const
{
if (style()->boxSizing() == BORDER_BOX)
width -= borderAndPaddingLogicalWidth();
return max(0, width);
}
int RenderBox::computeContentBoxLogicalHeight(int height) const
{
if (style()->boxSizing() == BORDER_BOX)
height -= borderAndPaddingLogicalHeight();
return max(0, height);
}
bool RenderBox::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, const IntPoint& pointInContainer, int tx, int ty, HitTestAction action)
{
tx += x();
ty += y();
for (RenderObject* child = lastChild(); child; child = child->previousSibling()) {
if (!child->hasLayer() && child->nodeAtPoint(request, result, pointInContainer, tx, ty, action)) {
updateHitTestResult(result, pointInContainer - IntSize(tx, ty));
return true;
}
}
IntRect boundsRect = IntRect(tx, ty, width(), height());
if (visibleToHitTesting() && action == HitTestForeground && boundsRect.intersects(result.rectForPoint(pointInContainer))) {
updateHitTestResult(result, pointInContainer - IntSize(tx, ty));
if (!result.addNodeToRectBasedTestResult(node(), pointInContainer.x(), pointInContainer.y(), boundsRect))
return true;
}
return false;
}
void RenderBox::paint(PaintInfo& paintInfo, int tx, int ty)
{
tx += x();
ty += y();
PaintInfo childInfo(paintInfo);
childInfo.updatePaintingRootForChildren(this);
for (RenderObject* child = firstChild(); child; child = child->nextSibling())
child->paint(childInfo, tx, ty);
}
void RenderBox::paintRootBoxFillLayers(const PaintInfo& paintInfo)
{
const FillLayer* bgLayer = style()->backgroundLayers();
Color bgColor = style()->visitedDependentColor(CSSPropertyBackgroundColor);
RenderObject* bodyObject = 0;
if (!hasBackground() && node() && node()->hasTagName(HTMLNames::htmlTag)) {
HTMLElement* body = document()->body();
bodyObject = (body && body->hasLocalName(bodyTag)) ? body->renderer() : 0;
if (bodyObject) {
bgLayer = bodyObject->style()->backgroundLayers();
bgColor = bodyObject->style()->visitedDependentColor(CSSPropertyBackgroundColor);
}
}
paintFillLayers(paintInfo, bgColor, bgLayer, view()->unscaledDocumentRect(), BackgroundBleedNone, CompositeSourceOver, bodyObject);
}
void RenderBox::paintBoxDecorations(PaintInfo& paintInfo, int tx, int ty)
{
if (paintInfo.shouldPaintWithinRoot(this))
paintBoxDecorationsWithSize(paintInfo, IntRect(tx, ty, width(), height()));
}
BackgroundBleedAvoidance RenderBox::determineBackgroundBleedAvoidance(GraphicsContext* context) const
{
if (context->paintingDisabled())
return BackgroundBleedNone;
const RenderStyle* style = this->style();
if (!style->hasBackground() || !style->hasBorder() || !style->hasBorderRadius() || borderImageIsLoadedAndCanBeRendered())
return BackgroundBleedNone;
AffineTransform ctm = context->getCTM();
FloatSize contextScaling(static_cast<float>(ctm.xScale()), static_cast<float>(ctm.yScale()));
if (borderObscuresBackgroundEdge(contextScaling))
return BackgroundBleedShrinkBackground;
return BackgroundBleedUseTransparencyLayer;
}
void RenderBox::paintBoxDecorationsWithSize(PaintInfo& paintInfo, IntRect paintRect)
{
borderFitAdjust(paintRect);
paintBoxShadow(paintInfo.context, paintRect, style(), Normal);
BackgroundBleedAvoidance bleedAvoidance = determineBackgroundBleedAvoidance(paintInfo.context);
GraphicsContextStateSaver stateSaver(*paintInfo.context, false);
if (bleedAvoidance == BackgroundBleedUseTransparencyLayer) {
RoundedIntRect border = style()->getRoundedBorderFor(paintRect);
stateSaver.save();
paintInfo.context->addRoundedRectClip(border);
paintInfo.context->beginTransparencyLayer(1);
}
bool themePainted = style()->hasAppearance() && !theme()->paint(this, paintInfo, paintRect);
if (!themePainted) {
if (isRoot())
paintRootBoxFillLayers(paintInfo);
else if (!isBody() || document()->documentElement()->renderer()->hasBackground()) {
paintFillLayers(paintInfo, style()->visitedDependentColor(CSSPropertyBackgroundColor), style()->backgroundLayers(), paintRect, bleedAvoidance);
}
if (style()->hasAppearance())
theme()->paintDecorations(this, paintInfo, paintRect);
}
paintBoxShadow(paintInfo.context, paintRect, style(), Inset);
if ((!style()->hasAppearance() || (!themePainted && theme()->paintBorderOnly(this, paintInfo, paintRect))) && style()->hasBorder())
paintBorder(paintInfo.context, paintRect, style(), bleedAvoidance);
if (bleedAvoidance == BackgroundBleedUseTransparencyLayer)
paintInfo.context->endTransparencyLayer();
}
void RenderBox::paintMask(PaintInfo& paintInfo, IntSize paintOffset)
{
if (!paintInfo.shouldPaintWithinRoot(this) || style()->visibility() != VISIBLE || paintInfo.phase != PaintPhaseMask || paintInfo.context->paintingDisabled())
return;
IntRect paintRect = IntRect(toPoint(paintOffset), size());
borderFitAdjust(paintRect);
paintMaskImages(paintInfo, paintRect);
}
void RenderBox::paintMaskImages(const PaintInfo& paintInfo, const IntRect& paintRect)
{
bool pushTransparencyLayer = false;
bool compositedMask = hasLayer() && layer()->hasCompositedMask();
CompositeOperator compositeOp = CompositeSourceOver;
bool allMaskImagesLoaded = true;
if (!compositedMask) {
const AffineTransform& currentCTM = paintInfo.context->getCTM();
pushTransparencyLayer = !currentCTM.isIdentityOrTranslationOrFlipped();
StyleImage* maskBoxImage = style()->maskBoxImage().image();
const FillLayer* maskLayers = style()->maskLayers();
if (maskBoxImage)
allMaskImagesLoaded &= maskBoxImage->isLoaded();
if (maskLayers)
allMaskImagesLoaded &= maskLayers->imagesAreLoaded();
if (!allMaskImagesLoaded)
pushTransparencyLayer = true;
if (maskBoxImage && maskLayers->hasImage()) {
pushTransparencyLayer = true;
} else {
for (const FillLayer* fillLayer = maskLayers->next(); fillLayer; fillLayer = fillLayer->next()) {
if (fillLayer->hasImage() && fillLayer->image()->canRender(style()->effectiveZoom())) {
pushTransparencyLayer = true;
break;
}
}
}
compositeOp = CompositeDestinationIn;
if (pushTransparencyLayer) {
paintInfo.context->setCompositeOperation(CompositeDestinationIn);
paintInfo.context->beginTransparencyLayer(1.0f);
compositeOp = CompositeSourceOver;
}
}
if (allMaskImagesLoaded) {
paintFillLayers(paintInfo, Color(), style()->maskLayers(), paintRect, BackgroundBleedNone, compositeOp);
paintNinePieceImage(paintInfo.context, paintRect, style(), style()->maskBoxImage(), compositeOp);
}
if (pushTransparencyLayer)
paintInfo.context->endTransparencyLayer();
}
IntRect RenderBox::maskClipRect()
{
const NinePieceImage& maskBoxImage = style()->maskBoxImage();
if (maskBoxImage.image()) {
IntRect borderImageRect = borderBoxRect();
int topOutset;
int rightOutset;
int bottomOutset;
int leftOutset;
style()->getMaskBoxImageOutsets(topOutset, rightOutset, bottomOutset, leftOutset);
borderImageRect.setX(borderImageRect.x() - leftOutset);
borderImageRect.setY(borderImageRect.y() - topOutset);
borderImageRect.setWidth(borderImageRect.width() + leftOutset + rightOutset);
borderImageRect.setHeight(borderImageRect.height() + topOutset + bottomOutset);
return borderImageRect;
}
IntRect result;
IntRect borderBox = borderBoxRect();
for (const FillLayer* maskLayer = style()->maskLayers(); maskLayer; maskLayer = maskLayer->next()) {
if (maskLayer->image()) {
IntRect maskRect;
IntPoint phase;
IntSize tileSize;
calculateBackgroundImageGeometry(maskLayer, borderBox, maskRect, phase, tileSize);
result.unite(maskRect);
}
}
return result;
}
void RenderBox::paintFillLayers(const PaintInfo& paintInfo, const Color& c, const FillLayer* fillLayer, const IntRect& rect,
BackgroundBleedAvoidance bleedAvoidance, CompositeOperator op, RenderObject* backgroundObject)
{
if (!fillLayer)
return;
paintFillLayers(paintInfo, c, fillLayer->next(), rect, bleedAvoidance, op, backgroundObject);
paintFillLayer(paintInfo, c, fillLayer, rect, bleedAvoidance, op, backgroundObject);
}
void RenderBox::paintFillLayer(const PaintInfo& paintInfo, const Color& c, const FillLayer* fillLayer, const IntRect& rect,
BackgroundBleedAvoidance bleedAvoidance, CompositeOperator op, RenderObject* backgroundObject)
{
paintFillLayerExtended(paintInfo, c, fillLayer, rect, bleedAvoidance, 0, IntSize(), op, backgroundObject);
}
#if USE(ACCELERATED_COMPOSITING)
static bool layersUseImage(WrappedImagePtr image, const FillLayer* layers)
{
for (const FillLayer* curLayer = layers; curLayer; curLayer = curLayer->next()) {
if (curLayer->image() && image == curLayer->image()->data())
return true;
}
return false;
}
#endif
void RenderBox::imageChanged(WrappedImagePtr image, const IntRect*)
{
if (!parent())
return;
if ((style()->borderImage().image() && style()->borderImage().image()->data() == image) ||
(style()->maskBoxImage().image() && style()->maskBoxImage().image()->data() == image)) {
repaint();
return;
}
bool didFullRepaint = repaintLayerRectsForImage(image, style()->backgroundLayers(), true);
if (!didFullRepaint)
repaintLayerRectsForImage(image, style()->maskLayers(), false);
#if USE(ACCELERATED_COMPOSITING)
if (hasLayer() && layer()->hasCompositedMask() && layersUseImage(image, style()->maskLayers()))
layer()->contentChanged(RenderLayer::MaskImageChanged);
#endif
}
bool RenderBox::repaintLayerRectsForImage(WrappedImagePtr image, const FillLayer* layers, bool drawingBackground)
{
IntRect rendererRect;
RenderBox* layerRenderer = 0;
for (const FillLayer* curLayer = layers; curLayer; curLayer = curLayer->next()) {
if (curLayer->image() && image == curLayer->image()->data() && curLayer->image()->canRender(style()->effectiveZoom())) {
if (!layerRenderer) {
bool drawingRootBackground = drawingBackground && (isRoot() || (isBody() && !document()->documentElement()->renderer()->hasBackground()));
if (drawingRootBackground) {
layerRenderer = view();
int rw;
int rh;
if (FrameView* frameView = toRenderView(layerRenderer)->frameView()) {
rw = frameView->contentsWidth();
rh = frameView->contentsHeight();
} else {
rw = layerRenderer->width();
rh = layerRenderer->height();
}
rendererRect = IntRect(-layerRenderer->marginLeft(),
-layerRenderer->marginTop(),
max(layerRenderer->width() + layerRenderer->marginLeft() + layerRenderer->marginRight() + layerRenderer->borderLeft() + layerRenderer->borderRight(), rw),
max(layerRenderer->height() + layerRenderer->marginTop() + layerRenderer->marginBottom() + layerRenderer->borderTop() + layerRenderer->borderBottom(), rh));
} else {
layerRenderer = this;
rendererRect = borderBoxRect();
}
}
IntRect repaintRect;
IntPoint phase;
IntSize tileSize;
layerRenderer->calculateBackgroundImageGeometry(curLayer, rendererRect, repaintRect, phase, tileSize);
layerRenderer->repaintRectangle(repaintRect);
if (repaintRect == rendererRect)
return true;
}
}
return false;
}
#if PLATFORM(MAC)
void RenderBox::paintCustomHighlight(int tx, int ty, const AtomicString& type, bool behindText)
{
Frame* frame = this->frame();
if (!frame)
return;
Page* page = frame->page();
if (!page)
return;
InlineBox* boxWrap = inlineBoxWrapper();
RootInlineBox* r = boxWrap ? boxWrap->root() : 0;
if (r) {
FloatRect rootRect(tx + r->x(), ty + r->selectionTop(), r->logicalWidth(), r->selectionHeight());
FloatRect imageRect(tx + x(), rootRect.y(), width(), rootRect.height());
page->chrome()->client()->paintCustomHighlight(node(), type, imageRect, rootRect, behindText, false);
} else {
FloatRect imageRect(tx + x(), ty + y(), width(), height());
page->chrome()->client()->paintCustomHighlight(node(), type, imageRect, imageRect, behindText, false);
}
}
#endif
bool RenderBox::pushContentsClip(PaintInfo& paintInfo, int tx, int ty)
{
if (paintInfo.phase == PaintPhaseBlockBackground || paintInfo.phase == PaintPhaseSelfOutline || paintInfo.phase == PaintPhaseMask)
return false;
bool isControlClip = hasControlClip();
bool isOverflowClip = hasOverflowClip() && !layer()->isSelfPaintingLayer();
if (!isControlClip && !isOverflowClip)
return false;
if (paintInfo.phase == PaintPhaseOutline)
paintInfo.phase = PaintPhaseChildOutlines;
else if (paintInfo.phase == PaintPhaseChildBlockBackground) {
paintInfo.phase = PaintPhaseBlockBackground;
paintObject(paintInfo, tx, ty);
paintInfo.phase = PaintPhaseChildBlockBackgrounds;
}
IntRect clipRect(isControlClip ? controlClipRect(tx, ty) : overflowClipRect(tx, ty));
paintInfo.context->save();
if (style()->hasBorderRadius())
paintInfo.context->addRoundedRectClip(style()->getRoundedBorderFor(IntRect(tx, ty, width(), height())));
paintInfo.context->clip(clipRect);
return true;
}
void RenderBox::popContentsClip(PaintInfo& paintInfo, PaintPhase originalPhase, int tx, int ty)
{
ASSERT(hasControlClip() || (hasOverflowClip() && !layer()->isSelfPaintingLayer()));
paintInfo.context->restore();
if (originalPhase == PaintPhaseOutline) {
paintInfo.phase = PaintPhaseSelfOutline;
paintObject(paintInfo, tx, ty);
paintInfo.phase = originalPhase;
} else if (originalPhase == PaintPhaseChildBlockBackground)
paintInfo.phase = originalPhase;
}
IntRect RenderBox::overflowClipRect(int tx, int ty, OverlayScrollbarSizeRelevancy relevancy)
{
int bLeft = borderLeft();
int bTop = borderTop();
int clipX = tx + bLeft;
int clipY = ty + bTop;
int clipWidth = width() - bLeft - borderRight();
int clipHeight = height() - bTop - borderBottom();
if (layer()) {
clipWidth -= layer()->verticalScrollbarWidth(relevancy);
clipHeight -= layer()->horizontalScrollbarHeight(relevancy);
}
return IntRect(clipX, clipY, clipWidth, clipHeight);
}
IntRect RenderBox::clipRect(int tx, int ty)
{
int clipX = tx;
int clipY = ty;
int clipWidth = width();
int clipHeight = height();
if (!style()->clipLeft().isAuto()) {
int c = style()->clipLeft().calcValue(width());
clipX += c;
clipWidth -= c;
}
if (!style()->clipRight().isAuto())
clipWidth -= width() - style()->clipRight().calcValue(width());
if (!style()->clipTop().isAuto()) {
int c = style()->clipTop().calcValue(height());
clipY += c;
clipHeight -= c;
}
if (!style()->clipBottom().isAuto())
clipHeight -= height() - style()->clipBottom().calcValue(height());
return IntRect(clipX, clipY, clipWidth, clipHeight);
}
int RenderBox::containingBlockLogicalWidthForContent() const
{
RenderBlock* cb = containingBlock();
if (shrinkToAvoidFloats())
return cb->availableLogicalWidthForLine(y(), false);
return cb->availableLogicalWidth();
}
int RenderBox::perpendicularContainingBlockLogicalHeight() const
{
RenderBlock* cb = containingBlock();
RenderStyle* containingBlockStyle = cb->style();
Length logicalHeightLength = containingBlockStyle->logicalHeight();
if (!logicalHeightLength.isFixed()) {
return containingBlockStyle->isHorizontalWritingMode() ? view()->frameView()->visibleHeight() : view()->frameView()->visibleWidth();
}
return cb->computeContentBoxLogicalHeight(logicalHeightLength.value());
}
void RenderBox::mapLocalToContainer(RenderBoxModelObject* repaintContainer, bool fixed, bool useTransforms, TransformState& transformState) const
{
if (repaintContainer == this)
return;
if (RenderView* v = view()) {
if (v->layoutStateEnabled() && !repaintContainer) {
LayoutState* layoutState = v->layoutState();
IntSize offset = layoutState->m_paintOffset;
offset.expand(x(), y());
if (style()->position() == RelativePosition && layer())
offset += layer()->relativePositionOffset();
transformState.move(offset);
return;
}
}
bool containerSkipped;
RenderObject* o = container(repaintContainer, &containerSkipped);
if (!o)
return;
bool isFixedPos = style()->position() == FixedPosition;
bool hasTransform = hasLayer() && layer()->transform();
if (hasTransform) {
fixed &= isFixedPos;
} else
fixed |= isFixedPos;
IntSize containerOffset = offsetFromContainer(o, roundedIntPoint(transformState.mappedPoint()));
bool preserve3D = useTransforms && (o->style()->preserves3D() || style()->preserves3D());
if (useTransforms && shouldUseTransformFromContainer(o)) {
TransformationMatrix t;
getTransformFromContainer(o, containerOffset, t);
transformState.applyTransform(t, preserve3D ? TransformState::AccumulateTransform : TransformState::FlattenTransform);
} else
transformState.move(containerOffset.width(), containerOffset.height(), preserve3D ? TransformState::AccumulateTransform : TransformState::FlattenTransform);
if (containerSkipped) {
IntSize containerOffset = repaintContainer->offsetFromAncestorContainer(o);
transformState.move(-containerOffset.width(), -containerOffset.height(), preserve3D ? TransformState::AccumulateTransform : TransformState::FlattenTransform);
return;
}
o->mapLocalToContainer(repaintContainer, fixed, useTransforms, transformState);
}
void RenderBox::mapAbsoluteToLocalPoint(bool fixed, bool useTransforms, TransformState& transformState) const
{
ASSERT(!view() || !view()->layoutStateEnabled());
bool isFixedPos = style()->position() == FixedPosition;
bool hasTransform = hasLayer() && layer()->transform();
if (hasTransform) {
fixed &= isFixedPos;
} else
fixed |= isFixedPos;
RenderObject* o = container();
if (!o)
return;
o->mapAbsoluteToLocalPoint(fixed, useTransforms, transformState);
IntSize containerOffset = offsetFromContainer(o, IntPoint());
bool preserve3D = useTransforms && (o->style()->preserves3D() || style()->preserves3D());
if (useTransforms && shouldUseTransformFromContainer(o)) {
TransformationMatrix t;
getTransformFromContainer(o, containerOffset, t);
transformState.applyTransform(t, preserve3D ? TransformState::AccumulateTransform : TransformState::FlattenTransform);
} else
transformState.move(-containerOffset.width(), -containerOffset.height(), preserve3D ? TransformState::AccumulateTransform : TransformState::FlattenTransform);
}
IntSize RenderBox::offsetFromContainer(RenderObject* o, const IntPoint& point) const
{
ASSERT(o == container());
IntSize offset;
if (isRelPositioned())
offset += relativePositionOffset();
if (!isInline() || isReplaced()) {
if (style()->position() != AbsolutePosition && style()->position() != FixedPosition) {
if (o->hasColumns()) {
IntRect columnRect(frameRect());
toRenderBlock(o)->flipForWritingModeIncludingColumns(columnRect);
offset += IntSize(columnRect.location().x(), columnRect.location().y());
columnRect.move(point.x(), point.y());
o->adjustForColumns(offset, columnRect.location());
} else
offset += locationOffsetIncludingFlipping();
} else
offset += locationOffsetIncludingFlipping();
}
if (o->hasOverflowClip())
offset -= toRenderBox(o)->layer()->scrolledContentOffset();
if (style()->position() == AbsolutePosition && o->isRelPositioned() && o->isRenderInline())
offset += toRenderInline(o)->relativePositionedInlineOffset(this);
return offset;
}
InlineBox* RenderBox::createInlineBox()
{
return new (renderArena()) InlineBox(this);
}
void RenderBox::dirtyLineBoxes(bool fullLayout)
{
if (m_inlineBoxWrapper) {
if (fullLayout) {
m_inlineBoxWrapper->destroy(renderArena());
m_inlineBoxWrapper = 0;
} else
m_inlineBoxWrapper->dirtyLineBoxes();
}
}
void RenderBox::positionLineBox(InlineBox* box)
{
if (isPositioned()) {
bool wasInline = style()->isOriginalDisplayInlineType();
if (wasInline) {
layer()->setStaticInlinePosition(lroundf(box->logicalLeft()));
if (style()->hasStaticInlinePosition(box->isHorizontal()))
setChildNeedsLayout(true, false); } else {
layer()->setStaticBlockPosition(box->logicalTop());
if (style()->hasStaticBlockPosition(box->isHorizontal()))
setChildNeedsLayout(true, false); }
box->remove();
box->destroy(renderArena());
} else if (isReplaced()) {
setLocation(roundedIntPoint(FloatPoint(box->x(), box->y())));
ASSERT(!m_inlineBoxWrapper);
m_inlineBoxWrapper = box;
}
}
void RenderBox::deleteLineBoxWrapper()
{
if (m_inlineBoxWrapper) {
if (!documentBeingDestroyed())
m_inlineBoxWrapper->remove();
m_inlineBoxWrapper->destroy(renderArena());
m_inlineBoxWrapper = 0;
}
}
IntRect RenderBox::clippedOverflowRectForRepaint(RenderBoxModelObject* repaintContainer)
{
if (style()->visibility() != VISIBLE && !enclosingLayer()->hasVisibleContent())
return IntRect();
IntRect r = visualOverflowRect();
RenderView* v = view();
if (v) {
r.move(v->layoutDelta());
}
if (style()) {
if (style()->hasAppearance())
theme()->adjustRepaintRect(this, r);
if (v) {
ASSERT(style()->outlineSize() <= v->maximalOutlineSize());
r.inflate(v->maximalOutlineSize());
}
}
computeRectForRepaint(repaintContainer, r);
return r;
}
void RenderBox::computeRectForRepaint(RenderBoxModelObject* repaintContainer, IntRect& rect, bool fixed)
{
if (RenderView* v = view()) {
if (v->layoutStateEnabled() && !repaintContainer && style()->position() != FixedPosition) {
LayoutState* layoutState = v->layoutState();
if (layer() && layer()->transform())
rect = layer()->transform()->mapRect(rect);
if (style()->position() == RelativePosition && layer())
rect.move(layer()->relativePositionOffset());
rect.move(x(), y());
rect.move(layoutState->m_paintOffset);
if (layoutState->m_clipped)
rect.intersect(layoutState->m_clipRect);
return;
}
}
if (hasReflection())
rect.unite(reflectedRect(rect));
if (repaintContainer == this) {
if (repaintContainer->style()->isFlippedBlocksWritingMode())
flipForWritingMode(rect);
return;
}
bool containerSkipped;
RenderObject* o = container(repaintContainer, &containerSkipped);
if (!o)
return;
if (isWritingModeRoot() && !isPositioned())
flipForWritingMode(rect);
IntPoint topLeft = rect.location();
topLeft.move(x(), y());
EPosition position = style()->position();
if (layer() && layer()->transform()) {
fixed = position == FixedPosition;
rect = layer()->transform()->mapRect(rect);
topLeft = rect.location();
topLeft.move(x(), y());
} else if (position == FixedPosition)
fixed = true;
if (position == AbsolutePosition && o->isRelPositioned() && o->isRenderInline())
topLeft += toRenderInline(o)->relativePositionedInlineOffset(this);
else if (position == RelativePosition && layer()) {
topLeft += layer()->relativePositionOffset();
}
if (o->isBlockFlow() && position != AbsolutePosition && position != FixedPosition) {
RenderBlock* cb = toRenderBlock(o);
if (cb->hasColumns()) {
IntRect repaintRect(topLeft, rect.size());
cb->adjustRectForColumns(repaintRect);
topLeft = repaintRect.location();
rect = repaintRect;
}
}
if (o->hasOverflowClip()) {
RenderBox* containerBox = toRenderBox(o);
topLeft -= containerBox->layer()->scrolledContentOffset();
IntRect repaintRect(topLeft, rect.size());
IntRect boxRect(0, 0, containerBox->layer()->width(), containerBox->layer()->height());
rect = intersection(repaintRect, boxRect);
if (rect.isEmpty())
return;
} else
rect.setLocation(topLeft);
if (containerSkipped) {
IntSize containerOffset = repaintContainer->offsetFromAncestorContainer(o);
rect.move(-containerOffset);
return;
}
o->computeRectForRepaint(repaintContainer, rect, fixed);
}
void RenderBox::repaintDuringLayoutIfMoved(const IntRect& rect)
{
int newX = x();
int newY = y();
int newWidth = width();
int newHeight = height();
if (rect.x() != newX || rect.y() != newY) {
m_frameRect = rect;
repaint();
repaintOverhangingFloats(true);
m_frameRect = IntRect(newX, newY, newWidth, newHeight);
repaint();
repaintOverhangingFloats(true);
}
}
void RenderBox::computeLogicalWidth()
{
if (isPositioned()) {
computePositionedLogicalWidth();
return;
}
if (node() && view()->frameView() && view()->frameView()->layoutRoot(true) == this)
return;
if (hasOverrideSize() && parent()->style()->boxOrient() == HORIZONTAL
&& parent()->isFlexibleBox() && parent()->isFlexingChildren()) {
setLogicalWidth(overrideSize());
return;
}
bool inVerticalBox = parent()->isFlexibleBox() && (parent()->style()->boxOrient() == VERTICAL);
bool stretching = (parent()->style()->boxAlign() == BSTRETCH);
bool treatAsReplaced = shouldComputeSizeAsReplaced() && (!inVerticalBox || !stretching);
Length logicalWidthLength = (treatAsReplaced) ? Length(computeReplacedLogicalWidth(), Fixed) : style()->logicalWidth();
RenderBlock* cb = containingBlock();
int containerLogicalWidth = max(0, containingBlockLogicalWidthForContent());
bool hasPerpendicularContainingBlock = cb->isHorizontalWritingMode() != isHorizontalWritingMode();
int containerWidthInInlineDirection = containerLogicalWidth;
if (hasPerpendicularContainingBlock)
containerWidthInInlineDirection = perpendicularContainingBlockLogicalHeight();
if (isInline() && !isInlineBlockOrInlineTable()) {
setMarginStart(style()->marginStart().calcMinValue(containerLogicalWidth));
setMarginEnd(style()->marginEnd().calcMinValue(containerLogicalWidth));
if (treatAsReplaced)
setLogicalWidth(max(logicalWidthLength.value() + borderAndPaddingLogicalWidth(), minPreferredLogicalWidth()));
return;
}
if (treatAsReplaced)
setLogicalWidth(logicalWidthLength.value() + borderAndPaddingLogicalWidth());
else {
setLogicalWidth(computeLogicalWidthUsing(LogicalWidth, containerWidthInInlineDirection));
if (!style()->logicalMaxWidth().isUndefined()) {
int maxLogicalWidth = computeLogicalWidthUsing(MaxLogicalWidth, containerWidthInInlineDirection);
if (logicalWidth() > maxLogicalWidth) {
setLogicalWidth(maxLogicalWidth);
logicalWidthLength = style()->logicalMaxWidth();
}
}
int minLogicalWidth = computeLogicalWidthUsing(MinLogicalWidth, containerWidthInInlineDirection);
if (logicalWidth() < minLogicalWidth) {
setLogicalWidth(minLogicalWidth);
logicalWidthLength = style()->logicalMinWidth();
}
}
if (stretchesToMinIntrinsicLogicalWidth()) {
setLogicalWidth(max(logicalWidth(), minPreferredLogicalWidth()));
logicalWidthLength = Length(logicalWidth(), Fixed);
}
if (logicalWidthLength.isAuto() || hasPerpendicularContainingBlock || isFloating() || isInline()) {
setMarginStart(style()->marginStart().calcMinValue(containerLogicalWidth));
setMarginEnd(style()->marginEnd().calcMinValue(containerLogicalWidth));
} else
computeInlineDirectionMargins(cb, containerLogicalWidth, logicalWidth());
if (!hasPerpendicularContainingBlock && containerLogicalWidth && containerLogicalWidth != (logicalWidth() + marginStart() + marginEnd())
&& !isFloating() && !isInline() && !cb->isFlexibleBox())
cb->setMarginEndForChild(this, containerLogicalWidth - logicalWidth() - cb->marginStartForChild(this));
}
int RenderBox::computeLogicalWidthUsing(LogicalWidthType widthType, int availableLogicalWidth)
{
int logicalWidthResult = logicalWidth();
Length logicalWidth;
if (widthType == LogicalWidth)
logicalWidth = style()->logicalWidth();
else if (widthType == MinLogicalWidth)
logicalWidth = style()->logicalMinWidth();
else
logicalWidth = style()->logicalMaxWidth();
if (logicalWidth.isIntrinsicOrAuto()) {
int marginStart = style()->marginStart().calcMinValue(availableLogicalWidth);
int marginEnd = style()->marginEnd().calcMinValue(availableLogicalWidth);
if (availableLogicalWidth)
logicalWidthResult = availableLogicalWidth - marginStart - marginEnd;
if (sizesToIntrinsicLogicalWidth(widthType)) {
logicalWidthResult = max(logicalWidthResult, minPreferredLogicalWidth());
logicalWidthResult = min(logicalWidthResult, maxPreferredLogicalWidth());
}
} else logicalWidthResult = computeBorderBoxLogicalWidth(logicalWidth.calcValue(availableLogicalWidth));
return logicalWidthResult;
}
bool RenderBox::sizesToIntrinsicLogicalWidth(LogicalWidthType widthType) const
{
if (isFloating() || (isInlineBlockOrInlineTable() && !isHTMLMarquee()))
return true;
Length logicalWidth = (widthType == MaxLogicalWidth) ? style()->logicalMaxWidth() : style()->logicalWidth();
if (logicalWidth.type() == Intrinsic)
return true;
if (parent()->style()->overflowX() == OMARQUEE) {
EMarqueeDirection dir = parent()->style()->marqueeDirection();
if (dir == MAUTO || dir == MFORWARD || dir == MBACKWARD || dir == MLEFT || dir == MRIGHT)
return true;
}
if (parent()->isFlexibleBox()
&& (parent()->style()->boxOrient() == HORIZONTAL || parent()->style()->boxAlign() != BSTRETCH))
return true;
if (logicalWidth.type() == Auto && !(parent()->isFlexibleBox() && parent()->style()->boxOrient() == VERTICAL && parent()->style()->boxAlign() == BSTRETCH) && node() && (node()->hasTagName(inputTag) || node()->hasTagName(selectTag) || node()->hasTagName(buttonTag) || node()->hasTagName(textareaTag) || node()->hasTagName(legendTag)))
return true;
return false;
}
void RenderBox::computeInlineDirectionMargins(RenderBlock* containingBlock, int containerWidth, int childWidth)
{
const RenderStyle* containingBlockStyle = containingBlock->style();
Length marginStartLength = style()->marginStartUsing(containingBlockStyle);
Length marginEndLength = style()->marginEndUsing(containingBlockStyle);
if ((marginStartLength.isAuto() && marginEndLength.isAuto() && childWidth < containerWidth)
|| (!marginStartLength.isAuto() && !marginEndLength.isAuto() && containingBlock->style()->textAlign() == WEBKIT_CENTER)) {
containingBlock->setMarginStartForChild(this, max(0, (containerWidth - childWidth) / 2));
containingBlock->setMarginEndForChild(this, containerWidth - childWidth - containingBlock->marginStartForChild(this));
return;
}
if (marginEndLength.isAuto() && childWidth < containerWidth) {
containingBlock->setMarginStartForChild(this, marginStartLength.calcValue(containerWidth));
containingBlock->setMarginEndForChild(this, containerWidth - childWidth - containingBlock->marginStartForChild(this));
return;
}
bool pushToEndFromTextAlign = !marginEndLength.isAuto() && ((!containingBlockStyle->isLeftToRightDirection() && containingBlockStyle->textAlign() == WEBKIT_LEFT)
|| (containingBlockStyle->isLeftToRightDirection() && containingBlockStyle->textAlign() == WEBKIT_RIGHT));
if ((marginStartLength.isAuto() && childWidth < containerWidth) || pushToEndFromTextAlign) {
containingBlock->setMarginEndForChild(this, marginEndLength.calcValue(containerWidth));
containingBlock->setMarginStartForChild(this, containerWidth - childWidth - containingBlock->marginEndForChild(this));
return;
}
containingBlock->setMarginStartForChild(this, marginStartLength.calcMinValue(containerWidth));
containingBlock->setMarginEndForChild(this, marginEndLength.calcMinValue(containerWidth));
}
void RenderBox::computeLogicalHeight()
{
if (isTableCell() || (isInline() && !isReplaced()))
return;
Length h;
if (isPositioned()) {
computePositionedLogicalHeight();
} else {
RenderBlock* cb = containingBlock();
bool hasPerpendicularContainingBlock = cb->isHorizontalWritingMode() != isHorizontalWritingMode();
if (!hasPerpendicularContainingBlock)
computeBlockDirectionMargins(cb);
if (isTable()) {
if (hasPerpendicularContainingBlock)
computeInlineDirectionMargins(cb, containingBlockLogicalWidthForContent(), logicalHeight());
return;
}
bool inHorizontalBox = parent()->isFlexibleBox() && parent()->style()->boxOrient() == HORIZONTAL;
bool stretching = parent()->style()->boxAlign() == BSTRETCH;
bool treatAsReplaced = shouldComputeSizeAsReplaced() && (!inHorizontalBox || !stretching);
bool checkMinMaxHeight = false;
if (hasOverrideSize() && parent()->isFlexibleBox() && parent()->style()->boxOrient() == VERTICAL
&& parent()->isFlexingChildren())
h = Length(overrideSize() - borderAndPaddingLogicalHeight(), Fixed);
else if (treatAsReplaced)
h = Length(computeReplacedLogicalHeight(), Fixed);
else {
h = style()->logicalHeight();
checkMinMaxHeight = true;
}
if (h.isAuto() && parent()->isFlexibleBox() && parent()->style()->boxOrient() == HORIZONTAL
&& parent()->isStretchingChildren()) {
h = Length(parentBox()->contentLogicalHeight() - marginBefore() - marginAfter() - borderAndPaddingLogicalHeight(), Fixed);
checkMinMaxHeight = false;
}
int heightResult;
if (checkMinMaxHeight) {
heightResult = computeLogicalHeightUsing(style()->logicalHeight());
if (heightResult == -1)
heightResult = logicalHeight();
int minH = computeLogicalHeightUsing(style()->logicalMinHeight()); int maxH = style()->logicalMaxHeight().isUndefined() ? heightResult : computeLogicalHeightUsing(style()->logicalMaxHeight());
if (maxH == -1)
maxH = heightResult;
heightResult = min(maxH, heightResult);
heightResult = max(minH, heightResult);
} else {
heightResult = h.value() + borderAndPaddingLogicalHeight();
}
setLogicalHeight(heightResult);
if (hasPerpendicularContainingBlock)
computeInlineDirectionMargins(cb, containingBlockLogicalWidthForContent(), heightResult);
}
bool paginatedContentNeedsBaseHeight = document()->printing() && h.isPercent()
&& (isRoot() || (isBody() && document()->documentElement()->renderer()->style()->logicalHeight().isPercent()));
if (stretchesToViewport() || paginatedContentNeedsBaseHeight) {
int margins = collapsedMarginBefore() + collapsedMarginAfter();
int visHeight;
if (document()->printing())
visHeight = static_cast<int>(view()->pageLogicalHeight());
else {
if (isHorizontalWritingMode())
visHeight = view()->viewHeight();
else
visHeight = view()->viewWidth();
}
if (isRoot())
setLogicalHeight(max(logicalHeight(), visHeight - margins));
else {
int marginsBordersPadding = margins + parentBox()->marginBefore() + parentBox()->marginAfter() + parentBox()->borderAndPaddingLogicalHeight();
setLogicalHeight(max(logicalHeight(), visHeight - marginsBordersPadding));
}
}
}
int RenderBox::computeLogicalHeightUsing(const Length& h)
{
int logicalHeight = -1;
if (!h.isAuto()) {
if (h.isFixed())
logicalHeight = h.value();
else if (h.isPercent())
logicalHeight = computePercentageLogicalHeight(h);
if (logicalHeight != -1) {
logicalHeight = computeBorderBoxLogicalHeight(logicalHeight);
return logicalHeight;
}
}
return logicalHeight;
}
int RenderBox::computePercentageLogicalHeight(const Length& height)
{
int result = -1;
bool skippedAutoHeightContainingBlock = false;
RenderBlock* cb = containingBlock();
while (!cb->isRenderView() && !cb->isBody() && !cb->isTableCell() && !cb->isPositioned() && cb->style()->logicalHeight().isAuto()) {
if (!document()->inQuirksMode() && !cb->isAnonymousBlock())
break;
skippedAutoHeightContainingBlock = true;
cb = cb->containingBlock();
cb->addPercentHeightDescendant(this);
}
bool isPositionedWithSpecifiedHeight = cb->isPositioned() && (!cb->style()->logicalHeight().isAuto() || (!cb->style()->top().isAuto() && !cb->style()->bottom().isAuto()));
bool includeBorderPadding = isTable();
if (cb->isTableCell()) {
if (!skippedAutoHeightContainingBlock) {
result = cb->overrideSize();
if (result == -1) {
RenderTableCell* cell = toRenderTableCell(cb);
if (scrollsOverflowY() && (!cell->style()->logicalHeight().isAuto() || !cell->table()->style()->logicalHeight().isAuto()))
return 0;
return -1;
}
includeBorderPadding = true;
}
}
else if (cb->style()->logicalHeight().isFixed())
result = cb->computeContentBoxLogicalHeight(cb->style()->logicalHeight().value());
else if (cb->style()->logicalHeight().isPercent() && !isPositionedWithSpecifiedHeight) {
result = cb->computePercentageLogicalHeight(cb->style()->logicalHeight());
if (result != -1)
result = cb->computeContentBoxLogicalHeight(result);
} else if (cb->isRenderView() || (cb->isBody() && document()->inQuirksMode()) || isPositionedWithSpecifiedHeight) {
int oldHeight = cb->logicalHeight();
cb->computeLogicalHeight();
result = cb->contentLogicalHeight();
cb->setLogicalHeight(oldHeight);
} else if (cb->isRoot() && isPositioned())
result = cb->computeContentBoxLogicalHeight(cb->availableLogicalHeight());
if (result != -1) {
result = height.calcValue(result);
if (includeBorderPadding) {
result -= borderAndPaddingLogicalHeight();
result = max(0, result);
}
}
return result;
}
int RenderBox::computeReplacedLogicalWidth(bool includeMaxWidth) const
{
int logicalWidth = computeReplacedLogicalWidthUsing(style()->logicalWidth());
int minLogicalWidth = computeReplacedLogicalWidthUsing(style()->logicalMinWidth());
int maxLogicalWidth = !includeMaxWidth || style()->logicalMaxWidth().isUndefined() ? logicalWidth : computeReplacedLogicalWidthUsing(style()->logicalMaxWidth());
return max(minLogicalWidth, min(logicalWidth, maxLogicalWidth));
}
int RenderBox::computeReplacedLogicalWidthUsing(Length logicalWidth) const
{
switch (logicalWidth.type()) {
case Fixed:
return computeContentBoxLogicalWidth(logicalWidth.value());
case Percent: {
const int cw = isPositioned() ? containingBlockLogicalWidthForPositioned(toRenderBoxModelObject(container())) : containingBlockLogicalWidthForContent();
if (cw > 0)
return computeContentBoxLogicalWidth(logicalWidth.calcMinValue(cw));
}
default:
return intrinsicLogicalWidth();
}
}
int RenderBox::computeReplacedLogicalHeight() const
{
int logicalHeight = computeReplacedLogicalHeightUsing(style()->logicalHeight());
int minLogicalHeight = computeReplacedLogicalHeightUsing(style()->logicalMinHeight());
int maxLogicalHeight = style()->logicalMaxHeight().isUndefined() ? logicalHeight : computeReplacedLogicalHeightUsing(style()->logicalMaxHeight());
return max(minLogicalHeight, min(logicalHeight, maxLogicalHeight));
}
int RenderBox::computeReplacedLogicalHeightUsing(Length logicalHeight) const
{
switch (logicalHeight.type()) {
case Fixed:
return computeContentBoxLogicalHeight(logicalHeight.value());
case Percent:
{
RenderObject* cb = isPositioned() ? container() : containingBlock();
while (cb->isAnonymous()) {
cb = cb->containingBlock();
toRenderBlock(cb)->addPercentHeightDescendant(const_cast<RenderBox*>(this));
}
if (cb->isPositioned() && cb->style()->height().isAuto() && !(cb->style()->top().isAuto() || cb->style()->bottom().isAuto())) {
ASSERT(cb->isRenderBlock());
RenderBlock* block = toRenderBlock(cb);
int oldHeight = block->height();
block->computeLogicalHeight();
int newHeight = block->computeContentBoxLogicalHeight(block->contentHeight());
block->setHeight(oldHeight);
return computeContentBoxLogicalHeight(logicalHeight.calcValue(newHeight));
}
int availableHeight;
if (isPositioned())
availableHeight = containingBlockLogicalHeightForPositioned(toRenderBoxModelObject(cb));
else {
availableHeight = toRenderBox(cb)->availableLogicalHeight();
while (cb && !cb->isRenderView() && (cb->style()->logicalHeight().isAuto() || cb->style()->logicalHeight().isPercent())) {
if (cb->isTableCell()) {
availableHeight = max(availableHeight, intrinsicLogicalHeight());
return logicalHeight.calcValue(availableHeight - borderAndPaddingLogicalHeight());
}
cb = cb->containingBlock();
}
}
return computeContentBoxLogicalHeight(logicalHeight.calcValue(availableHeight));
}
default:
return intrinsicLogicalHeight();
}
}
int RenderBox::availableLogicalHeight() const
{
return availableLogicalHeightUsing(style()->logicalHeight());
}
int RenderBox::availableLogicalHeightUsing(const Length& h) const
{
if (h.isFixed())
return computeContentBoxLogicalHeight(h.value());
if (isRenderView())
return isHorizontalWritingMode() ? toRenderView(this)->frameView()->visibleHeight() : toRenderView(this)->frameView()->visibleWidth();
if (isTableCell() && (h.isAuto() || h.isPercent()))
return overrideSize() - borderAndPaddingLogicalWidth();
if (h.isPercent())
return computeContentBoxLogicalHeight(h.calcValue(containingBlock()->availableLogicalHeight()));
if (isRenderBlock() && isPositioned() && style()->height().isAuto() && !(style()->top().isAuto() || style()->bottom().isAuto())) {
RenderBlock* block = const_cast<RenderBlock*>(toRenderBlock(this));
int oldHeight = block->logicalHeight();
block->computeLogicalHeight();
int newHeight = block->computeContentBoxLogicalHeight(block->contentLogicalHeight());
block->setLogicalHeight(oldHeight);
return computeContentBoxLogicalHeight(newHeight);
}
return containingBlock()->availableLogicalHeight();
}
void RenderBox::computeBlockDirectionMargins(RenderBlock* containingBlock)
{
if (isTableCell()) {
setMarginBefore(0);
setMarginAfter(0);
return;
}
int cw = containingBlockLogicalWidthForContent();
RenderStyle* containingBlockStyle = containingBlock->style();
containingBlock->setMarginBeforeForChild(this, style()->marginBeforeUsing(containingBlockStyle).calcMinValue(cw));
containingBlock->setMarginAfterForChild(this, style()->marginAfterUsing(containingBlockStyle).calcMinValue(cw));
}
int RenderBox::containingBlockLogicalWidthForPositioned(const RenderBoxModelObject* containingBlock, bool checkForPerpendicularWritingMode) const
{
if (checkForPerpendicularWritingMode && containingBlock->isHorizontalWritingMode() != isHorizontalWritingMode())
return containingBlockLogicalHeightForPositioned(containingBlock, false);
if (containingBlock->isBox())
return toRenderBox(containingBlock)->clientLogicalWidth();
ASSERT(containingBlock->isRenderInline() && containingBlock->isRelPositioned());
const RenderInline* flow = toRenderInline(containingBlock);
InlineFlowBox* first = flow->firstLineBox();
InlineFlowBox* last = flow->lastLineBox();
if (!first || !last)
return 0;
int fromLeft;
int fromRight;
if (containingBlock->style()->isLeftToRightDirection()) {
fromLeft = first->logicalLeft() + first->borderLogicalLeft();
fromRight = last->logicalLeft() + last->logicalWidth() - last->borderLogicalRight();
} else {
fromRight = first->logicalLeft() + first->logicalWidth() - first->borderLogicalRight();
fromLeft = last->logicalLeft() + last->borderLogicalLeft();
}
return max(0, (fromRight - fromLeft));
}
int RenderBox::containingBlockLogicalHeightForPositioned(const RenderBoxModelObject* containingBlock, bool checkForPerpendicularWritingMode) const
{
if (checkForPerpendicularWritingMode && containingBlock->isHorizontalWritingMode() != isHorizontalWritingMode())
return containingBlockLogicalWidthForPositioned(containingBlock, false);
if (containingBlock->isBox())
return toRenderBox(containingBlock)->clientLogicalHeight();
ASSERT(containingBlock->isRenderInline() && containingBlock->isRelPositioned());
const RenderInline* flow = toRenderInline(containingBlock);
InlineFlowBox* first = flow->firstLineBox();
InlineFlowBox* last = flow->lastLineBox();
if (!first || !last)
return 0;
int heightResult;
IntRect boundingBox = flow->linesBoundingBox();
if (containingBlock->isHorizontalWritingMode())
heightResult = boundingBox.height();
else
heightResult = boundingBox.width();
heightResult -= (containingBlock->borderBefore() + containingBlock->borderAfter());
return heightResult;
}
static void computeInlineStaticDistance(Length& logicalLeft, Length& logicalRight, const RenderBox* child, const RenderBoxModelObject* containerBlock, int containerLogicalWidth,
TextDirection containerDirection)
{
if (!logicalLeft.isAuto() || !logicalRight.isAuto())
return;
if (containerDirection == LTR) {
int staticPosition = child->layer()->staticInlinePosition() - containerBlock->borderLogicalLeft();
for (RenderObject* curr = child->parent(); curr && curr != containerBlock; curr = curr->container()) {
if (curr->isBox())
staticPosition += toRenderBox(curr)->logicalLeft();
}
logicalLeft.setValue(Fixed, staticPosition);
} else {
RenderBox* enclosingBox = child->parent()->enclosingBox();
int staticPosition = child->layer()->staticInlinePosition() + containerLogicalWidth + containerBlock->borderLogicalRight();
staticPosition -= enclosingBox->logicalWidth();
for (RenderObject* curr = enclosingBox; curr && curr != containerBlock; curr = curr->container()) {
if (curr->isBox())
staticPosition -= toRenderBox(curr)->logicalLeft();
}
logicalRight.setValue(Fixed, staticPosition);
}
}
void RenderBox::computePositionedLogicalWidth()
{
if (isReplaced()) {
computePositionedLogicalWidthReplaced();
return;
}
const RenderBoxModelObject* containerBlock = toRenderBoxModelObject(container());
const int containerLogicalWidth = containingBlockLogicalWidthForPositioned(containerBlock);
TextDirection containerDirection = (document()->inQuirksMode()) ? parent()->style()->direction() : containerBlock->style()->direction();
bool isHorizontal = isHorizontalWritingMode();
const int bordersPlusPadding = borderAndPaddingLogicalWidth();
const Length marginLogicalLeft = isHorizontal ? style()->marginLeft() : style()->marginTop();
const Length marginLogicalRight = isHorizontal ? style()->marginRight() : style()->marginBottom();
int& marginLogicalLeftAlias = isHorizontal ? m_marginLeft : m_marginTop;
int& marginLogicalRightAlias = isHorizontal ? m_marginRight : m_marginBottom;
Length logicalLeft = style()->logicalLeft();
Length logicalRight = style()->logicalRight();
computeInlineStaticDistance(logicalLeft, logicalRight, this, containerBlock, containerLogicalWidth, containerDirection);
int logicalWidthResult;
int logicalLeftResult;
computePositionedLogicalWidthUsing(style()->logicalWidth(), containerBlock, containerDirection,
containerLogicalWidth, bordersPlusPadding,
logicalLeft, logicalRight, marginLogicalLeft, marginLogicalRight,
logicalWidthResult, marginLogicalLeftAlias, marginLogicalRightAlias, logicalLeftResult);
setLogicalWidth(logicalWidthResult);
setLogicalLeft(logicalLeftResult);
if (!style()->logicalMaxWidth().isUndefined()) {
int maxLogicalWidth;
int maxMarginLogicalLeft;
int maxMarginLogicalRight;
int maxLogicalLeftPos;
computePositionedLogicalWidthUsing(style()->logicalMaxWidth(), containerBlock, containerDirection,
containerLogicalWidth, bordersPlusPadding,
logicalLeft, logicalRight, marginLogicalLeft, marginLogicalRight,
maxLogicalWidth, maxMarginLogicalLeft, maxMarginLogicalRight, maxLogicalLeftPos);
if (logicalWidth() > maxLogicalWidth) {
setLogicalWidth(maxLogicalWidth);
marginLogicalLeftAlias = maxMarginLogicalLeft;
marginLogicalRightAlias = maxMarginLogicalRight;
setLogicalLeft(maxLogicalLeftPos);
}
}
if (!style()->logicalMinWidth().isZero()) {
int minLogicalWidth;
int minMarginLogicalLeft;
int minMarginLogicalRight;
int minLogicalLeftPos;
computePositionedLogicalWidthUsing(style()->logicalMinWidth(), containerBlock, containerDirection,
containerLogicalWidth, bordersPlusPadding,
logicalLeft, logicalRight, marginLogicalLeft, marginLogicalRight,
minLogicalWidth, minMarginLogicalLeft, minMarginLogicalRight, minLogicalLeftPos);
if (logicalWidth() < minLogicalWidth) {
setLogicalWidth(minLogicalWidth);
marginLogicalLeftAlias = minMarginLogicalLeft;
marginLogicalRightAlias = minMarginLogicalRight;
setLogicalLeft(minLogicalLeftPos);
}
}
if (stretchesToMinIntrinsicLogicalWidth() && logicalWidth() < minPreferredLogicalWidth() - bordersPlusPadding) {
computePositionedLogicalWidthUsing(Length(minPreferredLogicalWidth() - bordersPlusPadding, Fixed), containerBlock, containerDirection,
containerLogicalWidth, bordersPlusPadding,
logicalLeft, logicalRight, marginLogicalLeft, marginLogicalRight,
logicalWidthResult, marginLogicalLeftAlias, marginLogicalRightAlias, logicalLeftResult);
setLogicalWidth(logicalWidthResult);
setLogicalLeft(logicalLeftResult);
}
setLogicalWidth(logicalWidth() + bordersPlusPadding);
}
static void computeLogicalLeftPositionedOffset(int& logicalLeftPos, const RenderBox* child, int logicalWidthValue, const RenderBoxModelObject* containerBlock, int containerLogicalWidth)
{
if (containerBlock->isHorizontalWritingMode() != child->isHorizontalWritingMode() && containerBlock->style()->isFlippedBlocksWritingMode()) {
logicalLeftPos = containerLogicalWidth - logicalWidthValue - logicalLeftPos;
logicalLeftPos += (child->isHorizontalWritingMode() ? containerBlock->borderRight() : containerBlock->borderBottom());
} else
logicalLeftPos += (child->isHorizontalWritingMode() ? containerBlock->borderLeft() : containerBlock->borderTop());
}
void RenderBox::computePositionedLogicalWidthUsing(Length logicalWidth, const RenderBoxModelObject* containerBlock, TextDirection containerDirection,
int containerLogicalWidth, int bordersPlusPadding,
Length logicalLeft, Length logicalRight, Length marginLogicalLeft, Length marginLogicalRight,
int& logicalWidthValue, int& marginLogicalLeftValue, int& marginLogicalRightValue, int& logicalLeftPos)
{
ASSERT(!(logicalLeft.isAuto() && logicalRight.isAuto()));
int logicalLeftValue = 0;
bool logicalWidthIsAuto = logicalWidth.isIntrinsicOrAuto();
bool logicalLeftIsAuto = logicalLeft.isAuto();
bool logicalRightIsAuto = logicalRight.isAuto();
if (!logicalLeftIsAuto && !logicalWidthIsAuto && !logicalRightIsAuto) {
logicalLeftValue = logicalLeft.calcValue(containerLogicalWidth);
logicalWidthValue = computeContentBoxLogicalWidth(logicalWidth.calcValue(containerLogicalWidth));
const int availableSpace = containerLogicalWidth - (logicalLeftValue + logicalWidthValue + logicalRight.calcValue(containerLogicalWidth) + bordersPlusPadding);
if (marginLogicalLeft.isAuto() && marginLogicalRight.isAuto()) {
if (availableSpace >= 0) {
marginLogicalLeftValue = availableSpace / 2; marginLogicalRightValue = availableSpace - marginLogicalLeftValue; } else {
if (containerDirection == LTR) {
marginLogicalLeftValue = 0;
marginLogicalRightValue = availableSpace; } else {
marginLogicalLeftValue = availableSpace; marginLogicalRightValue = 0;
}
}
} else if (marginLogicalLeft.isAuto()) {
marginLogicalRightValue = marginLogicalRight.calcValue(containerLogicalWidth);
marginLogicalLeftValue = availableSpace - marginLogicalRightValue;
} else if (marginLogicalRight.isAuto()) {
marginLogicalLeftValue = marginLogicalLeft.calcValue(containerLogicalWidth);
marginLogicalRightValue = availableSpace - marginLogicalLeftValue;
} else {
marginLogicalLeftValue = marginLogicalLeft.calcValue(containerLogicalWidth);
marginLogicalRightValue = marginLogicalRight.calcValue(containerLogicalWidth);
if (containerDirection == RTL)
logicalLeftValue = (availableSpace + logicalLeftValue) - marginLogicalLeftValue - marginLogicalRightValue;
}
} else {
marginLogicalLeftValue = marginLogicalLeft.calcMinValue(containerLogicalWidth);
marginLogicalRightValue = marginLogicalRight.calcMinValue(containerLogicalWidth);
const int availableSpace = containerLogicalWidth - (marginLogicalLeftValue + marginLogicalRightValue + bordersPlusPadding);
if (logicalLeftIsAuto && logicalWidthIsAuto && !logicalRightIsAuto) {
int logicalRightValue = logicalRight.calcValue(containerLogicalWidth);
int preferredWidth = maxPreferredLogicalWidth() - bordersPlusPadding;
int preferredMinWidth = minPreferredLogicalWidth() - bordersPlusPadding;
int availableWidth = availableSpace - logicalRightValue;
logicalWidthValue = min(max(preferredMinWidth, availableWidth), preferredWidth);
logicalLeftValue = availableSpace - (logicalWidthValue + logicalRightValue);
} else if (!logicalLeftIsAuto && logicalWidthIsAuto && logicalRightIsAuto) {
logicalLeftValue = logicalLeft.calcValue(containerLogicalWidth);
int preferredWidth = maxPreferredLogicalWidth() - bordersPlusPadding;
int preferredMinWidth = minPreferredLogicalWidth() - bordersPlusPadding;
int availableWidth = availableSpace - logicalLeftValue;
logicalWidthValue = min(max(preferredMinWidth, availableWidth), preferredWidth);
} else if (logicalLeftIsAuto && !logicalWidthIsAuto && !logicalRightIsAuto) {
logicalWidthValue = computeContentBoxLogicalWidth(logicalWidth.calcValue(containerLogicalWidth));
logicalLeftValue = availableSpace - (logicalWidthValue + logicalRight.calcValue(containerLogicalWidth));
} else if (!logicalLeftIsAuto && logicalWidthIsAuto && !logicalRightIsAuto) {
logicalLeftValue = logicalLeft.calcValue(containerLogicalWidth);
logicalWidthValue = availableSpace - (logicalLeftValue + logicalRight.calcValue(containerLogicalWidth));
} else if (!logicalLeftIsAuto && !logicalWidthIsAuto && logicalRightIsAuto) {
logicalLeftValue = logicalLeft.calcValue(containerLogicalWidth);
logicalWidthValue = computeContentBoxLogicalWidth(logicalWidth.calcValue(containerLogicalWidth));
}
}
if (containerBlock->isRenderInline() && !containerBlock->style()->isLeftToRightDirection()) {
const RenderInline* flow = toRenderInline(containerBlock);
InlineFlowBox* firstLine = flow->firstLineBox();
InlineFlowBox* lastLine = flow->lastLineBox();
if (firstLine && lastLine && firstLine != lastLine) {
logicalLeftPos = logicalLeftValue + marginLogicalLeftValue + lastLine->borderLogicalLeft() + (lastLine->logicalLeft() - firstLine->logicalLeft());
return;
}
}
logicalLeftPos = logicalLeftValue + marginLogicalLeftValue;
computeLogicalLeftPositionedOffset(logicalLeftPos, this, logicalWidthValue, containerBlock, containerLogicalWidth);
}
static void computeBlockStaticDistance(Length& logicalTop, Length& logicalBottom, const RenderBox* child, const RenderBoxModelObject* containerBlock)
{
if (!logicalTop.isAuto() || !logicalBottom.isAuto())
return;
int staticLogicalTop = child->layer()->staticBlockPosition() - containerBlock->borderBefore();
for (RenderObject* curr = child->parent(); curr && curr != containerBlock; curr = curr->container()) {
if (curr->isBox() && !curr->isTableRow())
staticLogicalTop += toRenderBox(curr)->logicalTop();
}
logicalTop.setValue(Fixed, staticLogicalTop);
}
void RenderBox::computePositionedLogicalHeight()
{
if (isReplaced()) {
computePositionedLogicalHeightReplaced();
return;
}
const RenderBoxModelObject* containerBlock = toRenderBoxModelObject(container());
const int containerLogicalHeight = containingBlockLogicalHeightForPositioned(containerBlock);
bool isHorizontal = isHorizontalWritingMode();
bool isFlipped = style()->isFlippedBlocksWritingMode();
const int bordersPlusPadding = borderAndPaddingLogicalHeight();
const Length marginBefore = style()->marginBefore();
const Length marginAfter = style()->marginAfter();
int& marginBeforeAlias = isHorizontal ? (isFlipped ? m_marginBottom : m_marginTop) : (isFlipped ? m_marginRight: m_marginLeft);
int& marginAfterAlias = isHorizontal ? (isFlipped ? m_marginTop : m_marginBottom) : (isFlipped ? m_marginLeft: m_marginRight);
Length logicalTop = style()->logicalTop();
Length logicalBottom = style()->logicalBottom();
computeBlockStaticDistance(logicalTop, logicalBottom, this, containerBlock);
int logicalHeightResult; int logicalTopPos;
computePositionedLogicalHeightUsing(style()->logicalHeight(), containerBlock, containerLogicalHeight, bordersPlusPadding,
logicalTop, logicalBottom, marginBefore, marginAfter,
logicalHeightResult, marginBeforeAlias, marginAfterAlias, logicalTopPos);
setLogicalTop(logicalTopPos);
if (!style()->logicalMaxHeight().isUndefined()) {
int maxLogicalHeight;
int maxMarginBefore;
int maxMarginAfter;
int maxLogicalTopPos;
computePositionedLogicalHeightUsing(style()->logicalMaxHeight(), containerBlock, containerLogicalHeight, bordersPlusPadding,
logicalTop, logicalBottom, marginBefore, marginAfter,
maxLogicalHeight, maxMarginBefore, maxMarginAfter, maxLogicalTopPos);
if (logicalHeightResult > maxLogicalHeight) {
logicalHeightResult = maxLogicalHeight;
marginBeforeAlias = maxMarginBefore;
marginAfterAlias = maxMarginAfter;
setLogicalTop(maxLogicalTopPos);
}
}
if (!style()->logicalMinHeight().isZero()) {
int minLogicalHeight;
int minMarginBefore;
int minMarginAfter;
int minLogicalTopPos;
computePositionedLogicalHeightUsing(style()->logicalMinHeight(), containerBlock, containerLogicalHeight, bordersPlusPadding,
logicalTop, logicalBottom, marginBefore, marginAfter,
minLogicalHeight, minMarginBefore, minMarginAfter, minLogicalTopPos);
if (logicalHeightResult < minLogicalHeight) {
logicalHeightResult = minLogicalHeight;
marginBeforeAlias = minMarginBefore;
marginAfterAlias = minMarginAfter;
setLogicalTop(minLogicalTopPos);
}
}
setLogicalHeight(logicalHeightResult + bordersPlusPadding);
}
static void computeLogicalTopPositionedOffset(int& logicalTopPos, const RenderBox* child, int logicalHeightValue, const RenderBoxModelObject* containerBlock, int containerLogicalHeight)
{
if ((child->style()->isFlippedBlocksWritingMode() && child->isHorizontalWritingMode() != containerBlock->isHorizontalWritingMode())
|| (child->style()->isFlippedBlocksWritingMode() != containerBlock->style()->isFlippedBlocksWritingMode() && child->isHorizontalWritingMode() == containerBlock->isHorizontalWritingMode()))
logicalTopPos = containerLogicalHeight - logicalHeightValue - logicalTopPos;
if (containerBlock->style()->isFlippedBlocksWritingMode() && child->isHorizontalWritingMode() == containerBlock->isHorizontalWritingMode()) {
if (child->isHorizontalWritingMode())
logicalTopPos += containerBlock->borderBottom();
else
logicalTopPos += containerBlock->borderRight();
} else {
if (child->isHorizontalWritingMode())
logicalTopPos += containerBlock->borderTop();
else
logicalTopPos += containerBlock->borderLeft();
}
}
void RenderBox::computePositionedLogicalHeightUsing(Length logicalHeightLength, const RenderBoxModelObject* containerBlock,
int containerLogicalHeight, int bordersPlusPadding,
Length logicalTop, Length logicalBottom, Length marginBefore, Length marginAfter,
int& logicalHeightValue, int& marginBeforeValue, int& marginAfterValue, int& logicalTopPos)
{
ASSERT(!(logicalTop.isAuto() && logicalBottom.isAuto()));
int contentLogicalHeight = logicalHeight() - bordersPlusPadding;
int logicalTopValue = 0;
bool logicalHeightIsAuto = logicalHeightLength.isAuto();
bool logicalTopIsAuto = logicalTop.isAuto();
bool logicalBottomIsAuto = logicalBottom.isAuto();
if (isTable()) {
logicalHeightLength.setValue(Fixed, contentLogicalHeight);
logicalHeightIsAuto = false;
}
if (!logicalTopIsAuto && !logicalHeightIsAuto && !logicalBottomIsAuto) {
logicalHeightValue = computeContentBoxLogicalHeight(logicalHeightLength.calcValue(containerLogicalHeight));
logicalTopValue = logicalTop.calcValue(containerLogicalHeight);
const int availableSpace = containerLogicalHeight - (logicalTopValue + logicalHeightValue + logicalBottom.calcValue(containerLogicalHeight) + bordersPlusPadding);
if (marginBefore.isAuto() && marginAfter.isAuto()) {
marginBeforeValue = availableSpace / 2; marginAfterValue = availableSpace - marginBeforeValue; } else if (marginBefore.isAuto()) {
marginAfterValue = marginAfter.calcValue(containerLogicalHeight);
marginBeforeValue = availableSpace - marginAfterValue;
} else if (marginAfter.isAuto()) {
marginBeforeValue = marginBefore.calcValue(containerLogicalHeight);
marginAfterValue = availableSpace - marginBeforeValue;
} else {
marginBeforeValue = marginBefore.calcValue(containerLogicalHeight);
marginAfterValue = marginAfter.calcValue(containerLogicalHeight);
}
} else {
marginBeforeValue = marginBefore.calcMinValue(containerLogicalHeight);
marginAfterValue = marginAfter.calcMinValue(containerLogicalHeight);
const int availableSpace = containerLogicalHeight - (marginBeforeValue + marginAfterValue + bordersPlusPadding);
if (logicalTopIsAuto && logicalHeightIsAuto && !logicalBottomIsAuto) {
logicalHeightValue = contentLogicalHeight;
logicalTopValue = availableSpace - (logicalHeightValue + logicalBottom.calcValue(containerLogicalHeight));
} else if (!logicalTopIsAuto && logicalHeightIsAuto && logicalBottomIsAuto) {
logicalTopValue = logicalTop.calcValue(containerLogicalHeight);
logicalHeightValue = contentLogicalHeight;
} else if (logicalTopIsAuto && !logicalHeightIsAuto && !logicalBottomIsAuto) {
logicalHeightValue = computeContentBoxLogicalHeight(logicalHeightLength.calcValue(containerLogicalHeight));
logicalTopValue = availableSpace - (logicalHeightValue + logicalBottom.calcValue(containerLogicalHeight));
} else if (!logicalTopIsAuto && logicalHeightIsAuto && !logicalBottomIsAuto) {
logicalTopValue = logicalTop.calcValue(containerLogicalHeight);
logicalHeightValue = max(0, availableSpace - (logicalTopValue + logicalBottom.calcValue(containerLogicalHeight)));
} else if (!logicalTopIsAuto && !logicalHeightIsAuto && logicalBottomIsAuto) {
logicalHeightValue = computeContentBoxLogicalHeight(logicalHeightLength.calcValue(containerLogicalHeight));
logicalTopValue = logicalTop.calcValue(containerLogicalHeight);
}
}
logicalTopPos = logicalTopValue + marginBeforeValue;
computeLogicalTopPositionedOffset(logicalTopPos, this, logicalHeightValue, containerBlock, containerLogicalHeight);
}
void RenderBox::computePositionedLogicalWidthReplaced()
{
const RenderBoxModelObject* containerBlock = toRenderBoxModelObject(container());
const int containerLogicalWidth = containingBlockLogicalWidthForPositioned(containerBlock);
TextDirection containerDirection = (document()->inQuirksMode()) ? parent()->style()->direction() : containerBlock->style()->direction();
bool isHorizontal = isHorizontalWritingMode();
Length logicalLeft = style()->logicalLeft();
Length logicalRight = style()->logicalRight();
Length marginLogicalLeft = isHorizontal ? style()->marginLeft() : style()->marginTop();
Length marginLogicalRight = isHorizontal ? style()->marginRight() : style()->marginBottom();
int& marginLogicalLeftAlias = isHorizontal ? m_marginLeft : m_marginTop;
int& marginLogicalRightAlias = isHorizontal ? m_marginRight : m_marginBottom;
setLogicalWidth(computeReplacedLogicalWidth() + borderAndPaddingLogicalWidth());
const int availableSpace = containerLogicalWidth - logicalWidth();
computeInlineStaticDistance(logicalLeft, logicalRight, this, containerBlock, containerLogicalWidth, containerDirection);
if (logicalLeft.isAuto() || logicalRight.isAuto()) {
if (marginLogicalLeft.isAuto())
marginLogicalLeft.setValue(Fixed, 0);
if (marginLogicalRight.isAuto())
marginLogicalRight.setValue(Fixed, 0);
}
int logicalLeftValue = 0;
int logicalRightValue = 0;
if (marginLogicalLeft.isAuto() && marginLogicalRight.isAuto()) {
ASSERT(!(logicalLeft.isAuto() && logicalRight.isAuto()));
logicalLeftValue = logicalLeft.calcValue(containerLogicalWidth);
logicalRightValue = logicalRight.calcValue(containerLogicalWidth);
int difference = availableSpace - (logicalLeftValue + logicalRightValue);
if (difference > 0) {
marginLogicalLeftAlias = difference / 2; marginLogicalRightAlias = difference - marginLogicalLeftAlias; } else {
if (containerDirection == LTR) {
marginLogicalLeftAlias = 0;
marginLogicalRightAlias = difference; } else {
marginLogicalLeftAlias = difference; marginLogicalRightAlias = 0;
}
}
} else if (logicalLeft.isAuto()) {
marginLogicalLeftAlias = marginLogicalLeft.calcValue(containerLogicalWidth);
marginLogicalRightAlias = marginLogicalRight.calcValue(containerLogicalWidth);
logicalRightValue = logicalRight.calcValue(containerLogicalWidth);
logicalLeftValue = availableSpace - (logicalRightValue + marginLogicalLeftAlias + marginLogicalRightAlias);
} else if (logicalRight.isAuto()) {
marginLogicalLeftAlias = marginLogicalLeft.calcValue(containerLogicalWidth);
marginLogicalRightAlias = marginLogicalRight.calcValue(containerLogicalWidth);
logicalLeftValue = logicalLeft.calcValue(containerLogicalWidth);
logicalRightValue = availableSpace - (logicalLeftValue + marginLogicalLeftAlias + marginLogicalRightAlias);
} else if (marginLogicalLeft.isAuto()) {
marginLogicalRightAlias = marginLogicalRight.calcValue(containerLogicalWidth);
logicalLeftValue = logicalLeft.calcValue(containerLogicalWidth);
logicalRightValue = logicalRight.calcValue(containerLogicalWidth);
marginLogicalLeftAlias = availableSpace - (logicalLeftValue + logicalRightValue + marginLogicalRightAlias);
} else if (marginLogicalRight.isAuto()) {
marginLogicalLeftAlias = marginLogicalLeft.calcValue(containerLogicalWidth);
logicalLeftValue = logicalLeft.calcValue(containerLogicalWidth);
logicalRightValue = logicalRight.calcValue(containerLogicalWidth);
marginLogicalRightAlias = availableSpace - (logicalLeftValue + logicalRightValue + marginLogicalLeftAlias);
} else {
marginLogicalLeftAlias = marginLogicalLeft.calcValue(containerLogicalWidth);
marginLogicalRightAlias = marginLogicalRight.calcValue(containerLogicalWidth);
logicalRightValue = logicalRight.calcValue(containerLogicalWidth);
logicalLeftValue = logicalLeft.calcValue(containerLogicalWidth);
}
int totalLogicalWidth = logicalWidth() + logicalLeftValue + logicalRightValue + marginLogicalLeftAlias + marginLogicalRightAlias;
if (totalLogicalWidth > containerLogicalWidth && (containerDirection == RTL))
logicalLeftValue = containerLogicalWidth - (totalLogicalWidth - logicalLeftValue);
if (containerBlock->isRenderInline() && !containerBlock->style()->isLeftToRightDirection()) {
const RenderInline* flow = toRenderInline(containerBlock);
InlineFlowBox* firstLine = flow->firstLineBox();
InlineFlowBox* lastLine = flow->lastLineBox();
if (firstLine && lastLine && firstLine != lastLine) {
setLogicalLeft(logicalLeftValue + marginLogicalLeftAlias + lastLine->borderLogicalLeft() + (lastLine->logicalLeft() - firstLine->logicalLeft()));
return;
}
}
int logicalLeftPos = logicalLeftValue + marginLogicalLeftAlias;
computeLogicalLeftPositionedOffset(logicalLeftPos, this, logicalWidth(), containerBlock, containerLogicalWidth);
setLogicalLeft(logicalLeftPos);
}
void RenderBox::computePositionedLogicalHeightReplaced()
{
const RenderBoxModelObject* containerBlock = toRenderBoxModelObject(container());
const int containerLogicalHeight = containingBlockLogicalHeightForPositioned(containerBlock);
bool isHorizontal = isHorizontalWritingMode();
bool isFlipped = style()->isFlippedBlocksWritingMode();
Length marginBefore = style()->marginBefore();
Length marginAfter = style()->marginAfter();
int& marginBeforeAlias = isHorizontal ? (isFlipped ? m_marginBottom : m_marginTop) : (isFlipped ? m_marginRight: m_marginLeft);
int& marginAfterAlias = isHorizontal ? (isFlipped ? m_marginTop : m_marginBottom) : (isFlipped ? m_marginLeft: m_marginRight);
Length logicalTop = style()->logicalTop();
Length logicalBottom = style()->logicalBottom();
setLogicalHeight(computeReplacedLogicalHeight() + borderAndPaddingLogicalHeight());
const int availableSpace = containerLogicalHeight - logicalHeight();
computeBlockStaticDistance(logicalTop, logicalBottom, this, containerBlock);
if (logicalTop.isAuto() || logicalBottom.isAuto()) {
if (marginBefore.isAuto())
marginBefore.setValue(Fixed, 0);
if (marginAfter.isAuto())
marginAfter.setValue(Fixed, 0);
}
int logicalTopValue = 0;
int logicalBottomValue = 0;
if (marginBefore.isAuto() && marginAfter.isAuto()) {
ASSERT(!(logicalTop.isAuto() || logicalBottom.isAuto()));
logicalTopValue = logicalTop.calcValue(containerLogicalHeight);
logicalBottomValue = logicalBottom.calcValue(containerLogicalHeight);
int difference = availableSpace - (logicalTopValue + logicalBottomValue);
marginBeforeAlias = difference / 2; marginAfterAlias = difference - marginBeforeAlias;
} else if (logicalTop.isAuto()) {
marginBeforeAlias = marginBefore.calcValue(containerLogicalHeight);
marginAfterAlias = marginAfter.calcValue(containerLogicalHeight);
logicalBottomValue = logicalBottom.calcValue(containerLogicalHeight);
logicalTopValue = availableSpace - (logicalBottomValue + marginBeforeAlias + marginAfterAlias);
} else if (logicalBottom.isAuto()) {
marginBeforeAlias = marginBefore.calcValue(containerLogicalHeight);
marginAfterAlias = marginAfter.calcValue(containerLogicalHeight);
logicalTopValue = logicalTop.calcValue(containerLogicalHeight);
} else if (marginBefore.isAuto()) {
marginAfterAlias = marginAfter.calcValue(containerLogicalHeight);
logicalTopValue = logicalTop.calcValue(containerLogicalHeight);
logicalBottomValue = logicalBottom.calcValue(containerLogicalHeight);
marginBeforeAlias = availableSpace - (logicalTopValue + logicalBottomValue + marginAfterAlias);
} else if (marginAfter.isAuto()) {
marginBeforeAlias = marginBefore.calcValue(containerLogicalHeight);
logicalTopValue = logicalTop.calcValue(containerLogicalHeight);
logicalBottomValue = logicalBottom.calcValue(containerLogicalHeight);
marginAfterAlias = availableSpace - (logicalTopValue + logicalBottomValue + marginBeforeAlias);
} else {
marginBeforeAlias = marginBefore.calcValue(containerLogicalHeight);
marginAfterAlias = marginAfter.calcValue(containerLogicalHeight);
logicalTopValue = logicalTop.calcValue(containerLogicalHeight);
}
int logicalTopPos = logicalTopValue + marginBeforeAlias;
computeLogicalTopPositionedOffset(logicalTopPos, this, logicalHeight(), containerBlock, containerLogicalHeight);
setLogicalTop(logicalTopPos);
}
IntRect RenderBox::localCaretRect(InlineBox* box, int caretOffset, int* extraWidthToEndOfLine)
{
IntRect rect(x(), y(), caretWidth, height());
bool ltr = box ? box->isLeftToRightDirection() : style()->isLeftToRightDirection();
if ((!caretOffset) ^ ltr)
rect.move(IntSize(width() - caretWidth, 0));
if (box) {
RootInlineBox* rootBox = box->root();
int top = rootBox->lineTop();
rect.setY(top);
rect.setHeight(rootBox->lineBottom() - top);
}
int fontHeight = style()->fontMetrics().height();
if (fontHeight > rect.height() || (!isReplaced() && !isTable()))
rect.setHeight(fontHeight);
if (extraWidthToEndOfLine)
*extraWidthToEndOfLine = x() + width() - rect.maxX();
rect.move(-x(), -y());
return rect;
}
VisiblePosition RenderBox::positionForPoint(const IntPoint& point)
{
if (!firstChild())
return createVisiblePosition(node() ? firstPositionInOrBeforeNode(node()) : Position(0, 0));
int xPos = point.x();
int yPos = point.y();
if (isTable() && node()) {
int right = contentWidth() + borderAndPaddingWidth();
int bottom = contentHeight() + borderAndPaddingHeight();
if (xPos < 0 || xPos > right || yPos < 0 || yPos > bottom) {
if (xPos <= right / 2)
return createVisiblePosition(firstPositionInOrBeforeNode(node()));
return createVisiblePosition(lastPositionInOrAfterNode(node()));
}
}
int minDist = INT_MAX;
RenderBox* closestRenderer = 0;
int newX = xPos;
int newY = yPos;
if (isTableRow()) {
newX += x();
newY += y();
}
for (RenderObject* renderObject = firstChild(); renderObject; renderObject = renderObject->nextSibling()) {
if ((!renderObject->firstChild() && !renderObject->isInline() && !renderObject->isBlockFlow() )
|| renderObject->style()->visibility() != VISIBLE)
continue;
if (!renderObject->isBox())
continue;
RenderBox* renderer = toRenderBox(renderObject);
int top = renderer->borderTop() + renderer->paddingTop() + (isTableRow() ? 0 : renderer->y());
int bottom = top + renderer->contentHeight();
int left = renderer->borderLeft() + renderer->paddingLeft() + (isTableRow() ? 0 : renderer->x());
int right = left + renderer->contentWidth();
if (xPos <= right && xPos >= left && yPos <= top && yPos >= bottom) {
if (renderer->isTableRow())
return renderer->positionForCoordinates(xPos + newX - renderer->x(), yPos + newY - renderer->y());
return renderer->positionForCoordinates(xPos - renderer->x(), yPos - renderer->y());
}
IntPoint cmp;
if (xPos > right) {
if (yPos < top)
cmp = IntPoint(right, top);
else if (yPos > bottom)
cmp = IntPoint(right, bottom);
else
cmp = IntPoint(right, yPos);
} else if (xPos < left) {
if (yPos < top)
cmp = IntPoint(left, top);
else if (yPos > bottom)
cmp = IntPoint(left, bottom);
else
cmp = IntPoint(left, yPos);
} else {
if (yPos < top)
cmp = IntPoint(xPos, top);
else
cmp = IntPoint(xPos, bottom);
}
int x1minusx2 = cmp.x() - xPos;
int y1minusy2 = cmp.y() - yPos;
int dist = x1minusx2 * x1minusx2 + y1minusy2 * y1minusy2;
if (dist < minDist) {
closestRenderer = renderer;
minDist = dist;
}
}
if (closestRenderer)
return closestRenderer->positionForCoordinates(newX - closestRenderer->x(), newY - closestRenderer->y());
return createVisiblePosition(firstPositionInOrBeforeNode(node()));
}
bool RenderBox::shrinkToAvoidFloats() const
{
if ((isInline() && !isHTMLMarquee()) || !avoidsFloats() || isFloating())
return false;
return style()->width().isAuto();
}
bool RenderBox::avoidsFloats() const
{
return isReplaced() || hasOverflowClip() || isHR() || isLegend() || isWritingModeRoot() || isDeprecatedFlexItem();
}
void RenderBox::addBoxShadowAndBorderOverflow()
{
if (!style()->boxShadow() && !style()->hasBorderImageOutsets())
return;
bool isFlipped = style()->isFlippedBlocksWritingMode();
bool isHorizontal = isHorizontalWritingMode();
IntRect borderBox = borderBoxRect();
int overflowMinX = borderBox.x();
int overflowMaxX = borderBox.maxX();
int overflowMinY = borderBox.y();
int overflowMaxY = borderBox.maxY();
if (style()->boxShadow()) {
int shadowLeft;
int shadowRight;
int shadowTop;
int shadowBottom;
style()->getBoxShadowExtent(shadowTop, shadowRight, shadowBottom, shadowLeft);
overflowMinX = borderBox.x() + ((!isFlipped || isHorizontal) ? shadowLeft : -shadowRight);
overflowMaxX = borderBox.maxX() + ((!isFlipped || isHorizontal) ? shadowRight : -shadowLeft);
overflowMinY = borderBox.y() + ((!isFlipped || !isHorizontal) ? shadowTop : -shadowBottom);
overflowMaxY = borderBox.maxY() + ((!isFlipped || !isHorizontal) ? shadowBottom : -shadowTop);
}
if (style()->hasBorderImageOutsets()) {
int borderOutsetLeft;
int borderOutsetRight;
int borderOutsetTop;
int borderOutsetBottom;
style()->getBorderImageOutsets(borderOutsetTop, borderOutsetRight, borderOutsetBottom, borderOutsetLeft);
overflowMinX = min(overflowMinX, borderBox.x() - ((!isFlipped || isHorizontal) ? borderOutsetLeft : borderOutsetRight));
overflowMaxX = max(overflowMaxX, borderBox.maxX() + ((!isFlipped || isHorizontal) ? borderOutsetRight : borderOutsetLeft));
overflowMinY = min(overflowMinY, borderBox.y() - ((!isFlipped || !isHorizontal) ? borderOutsetTop : borderOutsetBottom));
overflowMaxY = max(overflowMaxY, borderBox.maxY() + ((!isFlipped || !isHorizontal) ? borderOutsetBottom : borderOutsetTop));
}
addVisualOverflow(IntRect(overflowMinX, overflowMinY, overflowMaxX - overflowMinX, overflowMaxY - overflowMinY));
}
void RenderBox::addOverflowFromChild(RenderBox* child, const IntSize& delta)
{
IntRect childLayoutOverflowRect = child->layoutOverflowRectForPropagation(style());
childLayoutOverflowRect.move(delta);
addLayoutOverflow(childLayoutOverflowRect);
if (child->hasSelfPaintingLayer() || hasOverflowClip())
return;
IntRect childVisualOverflowRect = child->visualOverflowRectForPropagation(style());
childVisualOverflowRect.move(delta);
addVisualOverflow(childVisualOverflowRect);
}
void RenderBox::addLayoutOverflow(const IntRect& rect)
{
IntRect clientBox = clientBoxRect();
if (clientBox.contains(rect) || rect.isEmpty())
return;
IntRect overflowRect(rect);
if (hasOverflowClip() || isRenderView()) {
bool hasTopOverflow = !style()->isLeftToRightDirection() && !isHorizontalWritingMode();
bool hasLeftOverflow = !style()->isLeftToRightDirection() && isHorizontalWritingMode();
if (!hasTopOverflow)
overflowRect.shiftYEdgeTo(max(overflowRect.y(), clientBox.y()));
else
overflowRect.shiftMaxYEdgeTo(min(overflowRect.maxY(), clientBox.maxY()));
if (!hasLeftOverflow)
overflowRect.shiftXEdgeTo(max(overflowRect.x(), clientBox.x()));
else
overflowRect.shiftMaxXEdgeTo(min(overflowRect.maxX(), clientBox.maxX()));
if (clientBox.contains(overflowRect) || overflowRect.isEmpty())
return;
}
if (!m_overflow)
m_overflow = adoptPtr(new RenderOverflow(clientBox, borderBoxRect()));
m_overflow->addLayoutOverflow(overflowRect);
}
void RenderBox::addVisualOverflow(const IntRect& rect)
{
IntRect borderBox = borderBoxRect();
if (borderBox.contains(rect) || rect.isEmpty())
return;
if (!m_overflow)
m_overflow = adoptPtr(new RenderOverflow(clientBoxRect(), borderBox));
m_overflow->addVisualOverflow(rect);
}
void RenderBox::clearLayoutOverflow()
{
if (!m_overflow)
return;
if (visualOverflowRect() == borderBoxRect()) {
m_overflow.clear();
return;
}
m_overflow->resetLayoutOverflow(borderBoxRect());
}
int RenderBox::lineHeight(bool , LineDirectionMode direction, LinePositionMode ) const
{
if (isReplaced())
return direction == HorizontalLine ? m_marginTop + height() + m_marginBottom : m_marginRight + width() + m_marginLeft;
return 0;
}
int RenderBox::baselinePosition(FontBaseline baselineType, bool , LineDirectionMode direction, LinePositionMode ) const
{
if (isReplaced()) {
int result = direction == HorizontalLine ? m_marginTop + height() + m_marginBottom : m_marginRight + width() + m_marginLeft;
if (baselineType == AlphabeticBaseline)
return result;
return result - result / 2;
}
return 0;
}
RenderLayer* RenderBox::enclosingFloatPaintingLayer() const
{
const RenderObject* curr = this;
while (curr) {
RenderLayer* layer = curr->hasLayer() && curr->isBox() ? toRenderBoxModelObject(curr)->layer() : 0;
if (layer && layer->isSelfPaintingLayer())
return layer;
curr = curr->parent();
}
return 0;
}
IntRect RenderBox::logicalVisualOverflowRectForPropagation(RenderStyle* parentStyle) const
{
IntRect rect = visualOverflowRectForPropagation(parentStyle);
if (!parentStyle->isHorizontalWritingMode())
return rect.transposedRect();
return rect;
}
IntRect RenderBox::visualOverflowRectForPropagation(RenderStyle* parentStyle) const
{
IntRect rect = visualOverflowRect();
if (parentStyle->writingMode() == style()->writingMode())
return rect;
if (style()->writingMode() == RightToLeftWritingMode || parentStyle->writingMode() == RightToLeftWritingMode)
rect.setX(width() - rect.maxX());
else if (style()->writingMode() == BottomToTopWritingMode || parentStyle->writingMode() == BottomToTopWritingMode)
rect.setY(height() - rect.maxY());
return rect;
}
IntRect RenderBox::logicalLayoutOverflowRectForPropagation(RenderStyle* parentStyle) const
{
IntRect rect = layoutOverflowRectForPropagation(parentStyle);
if (!parentStyle->isHorizontalWritingMode())
return rect.transposedRect();
return rect;
}
IntRect RenderBox::layoutOverflowRectForPropagation(RenderStyle* parentStyle) const
{
IntRect rect = borderBoxRect();
if (!hasOverflowClip())
rect.unite(layoutOverflowRect());
bool hasTransform = hasLayer() && layer()->transform();
if (isRelPositioned() || hasTransform) {
flipForWritingMode(rect);
if (hasTransform)
rect = layer()->currentTransform().mapRect(rect);
if (isRelPositioned())
rect.move(relativePositionOffsetX(), relativePositionOffsetY());
flipForWritingMode(rect);
}
if (parentStyle->writingMode() == style()->writingMode())
return rect;
if (style()->writingMode() == RightToLeftWritingMode || parentStyle->writingMode() == RightToLeftWritingMode)
rect.setX(width() - rect.maxX());
else if (style()->writingMode() == BottomToTopWritingMode || parentStyle->writingMode() == BottomToTopWritingMode)
rect.setY(height() - rect.maxY());
return rect;
}
IntPoint RenderBox::flipForWritingMode(const RenderBox* child, const IntPoint& point, FlippingAdjustment adjustment) const
{
if (!style()->isFlippedBlocksWritingMode())
return point;
if (isHorizontalWritingMode())
return IntPoint(point.x(), point.y() + height() - child->height() - child->y() - (adjustment == ParentToChildFlippingAdjustment ? child->y() : 0));
return IntPoint(point.x() + width() - child->width() - child->x() - (adjustment == ParentToChildFlippingAdjustment ? child->x() : 0), point.y());
}
void RenderBox::flipForWritingMode(IntRect& rect) const
{
if (!style()->isFlippedBlocksWritingMode())
return;
if (isHorizontalWritingMode())
rect.setY(height() - rect.maxY());
else
rect.setX(width() - rect.maxX());
}
int RenderBox::flipForWritingMode(int position) const
{
if (!style()->isFlippedBlocksWritingMode())
return position;
return logicalHeight() - position;
}
IntPoint RenderBox::flipForWritingMode(const IntPoint& position) const
{
if (!style()->isFlippedBlocksWritingMode())
return position;
return isHorizontalWritingMode() ? IntPoint(position.x(), height() - position.y()) : IntPoint(width() - position.x(), position.y());
}
IntPoint RenderBox::flipForWritingModeIncludingColumns(const IntPoint& point) const
{
if (!hasColumns() || !style()->isFlippedBlocksWritingMode())
return flipForWritingMode(point);
return toRenderBlock(this)->flipForWritingModeIncludingColumns(point);
}
IntSize RenderBox::flipForWritingMode(const IntSize& offset) const
{
if (!style()->isFlippedBlocksWritingMode())
return offset;
return isHorizontalWritingMode() ? IntSize(offset.width(), height() - offset.height()) : IntSize(width() - offset.width(), offset.height());
}
FloatPoint RenderBox::flipForWritingMode(const FloatPoint& position) const
{
if (!style()->isFlippedBlocksWritingMode())
return position;
return isHorizontalWritingMode() ? FloatPoint(position.x(), height() - position.y()) : FloatPoint(width() - position.x(), position.y());
}
void RenderBox::flipForWritingMode(FloatRect& rect) const
{
if (!style()->isFlippedBlocksWritingMode())
return;
if (isHorizontalWritingMode())
rect.setY(height() - rect.maxY());
else
rect.setX(width() - rect.maxX());
}
IntSize RenderBox::locationOffsetIncludingFlipping() const
{
RenderBlock* containerBlock = containingBlock();
if (!containerBlock || containerBlock == this)
return locationOffset();
IntRect rect(frameRect());
containerBlock->flipForWritingMode(rect); return IntSize(rect.x(), rect.y());
}
}