1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
|
/*
* Copyright (C) 2007, 2013 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "RenderLayoutState.h"
#include "RenderBoxModelObjectInlines.h"
#include "RenderFragmentedFlow.h"
#include "RenderInline.h"
#include "RenderLayer.h"
#include "RenderMultiColumnFlow.h"
#include "RenderObjectInlines.h"
#include "RenderStyleInlines.h"
#include "RenderView.h"
#include <wtf/TZoneMallocInlines.h>
#include <wtf/WeakPtr.h>
namespace WebCore {
WTF_MAKE_TZONE_ALLOCATED_IMPL(RenderLayoutState);
RenderLayoutState::RenderLayoutState(RenderElement& renderer)
: m_clipped(false)
, m_isPaginated(false)
, m_pageLogicalHeightChanged(false)
#if ASSERT_ENABLED
, m_layoutDeltaXSaturated(false)
, m_layoutDeltaYSaturated(false)
, m_marginTrimBlockStart(false)
, m_renderer(&renderer)
#endif
{
if (RenderElement* container = renderer.container()) {
FloatPoint absContentPoint = container->localToAbsolute(FloatPoint(), UseTransforms);
m_paintOffset = LayoutSize(absContentPoint.x(), absContentPoint.y());
if (container->hasNonVisibleOverflow()) {
m_clipped = true;
auto& containerBox = downcast<RenderBox>(*container);
m_clipRect = LayoutRect(toLayoutPoint(m_paintOffset), containerBox.cachedSizeForOverflowClip());
m_paintOffset -= toLayoutSize(containerBox.scrollPosition());
}
}
if (m_isPaginated) {
// This is just a flag for known page height (see RenderBlockFlow::checkForPaginationLogicalHeightChange).
m_pageLogicalHeight = 1;
}
}
RenderLayoutState::RenderLayoutState(const LocalFrameViewLayoutContext::LayoutStateStack& layoutStateStack, RenderBox& renderer, const LayoutSize& offset, LayoutUnit pageLogicalHeight, bool pageLogicalHeightChanged, std::optional<LineClamp> lineClamp, std::optional<LegacyLineClamp> legacyLineClamp)
: m_clipped(false)
, m_isPaginated(false)
, m_pageLogicalHeightChanged(false)
#if ASSERT_ENABLED
, m_layoutDeltaXSaturated(false)
, m_layoutDeltaYSaturated(false)
#endif
, m_marginTrimBlockStart(false)
, m_lineClamp(lineClamp)
, m_legacyLineClamp(legacyLineClamp)
#if ASSERT_ENABLED
, m_renderer(&renderer)
#endif
{
if (!layoutStateStack.isEmpty()) {
auto& ancestor = *layoutStateStack.last().get();
computeOffsets(ancestor, renderer, offset);
computeClipRect(ancestor, renderer);
}
computePaginationInformation(layoutStateStack, renderer, pageLogicalHeight, pageLogicalHeightChanged);
}
void RenderLayoutState::computeOffsets(const RenderLayoutState& ancestor, RenderBox& renderer, LayoutSize offset)
{
bool fixed = renderer.isFixedPositioned();
if (fixed) {
FloatPoint fixedOffset = renderer.view().localToAbsolute(FloatPoint(), IsFixed);
m_paintOffset = LayoutSize(fixedOffset.x(), fixedOffset.y()) + offset;
} else
m_paintOffset = ancestor.paintOffset() + offset;
if (renderer.isOutOfFlowPositioned() && !fixed) {
if (CheckedPtr container = dynamicDowncast<RenderInline>(renderer.container())) {
if (container && container->isInFlowPositioned())
m_paintOffset += container->offsetForInFlowPositionedInline(&renderer);
}
}
m_layoutOffset = m_paintOffset;
if (renderer.isInFlowPositioned() && renderer.hasLayer())
m_paintOffset += renderer.layer()->offsetForInFlowPosition();
if (renderer.hasNonVisibleOverflow())
m_paintOffset -= toLayoutSize(renderer.scrollPosition());
m_layoutDelta = ancestor.layoutDelta();
#if ASSERT_ENABLED
m_layoutDeltaXSaturated = ancestor.m_layoutDeltaXSaturated;
m_layoutDeltaYSaturated = ancestor.m_layoutDeltaYSaturated;
#endif
}
void RenderLayoutState::computeClipRect(const RenderLayoutState& ancestor, RenderBox& renderer)
{
m_clipped = !renderer.isFixedPositioned() && ancestor.isClipped();
if (m_clipped)
m_clipRect = ancestor.clipRect();
if (!renderer.hasNonVisibleOverflow())
return;
auto paintOffsetForClipRect = toLayoutPoint(m_paintOffset + toLayoutSize(renderer.scrollPosition()));
LayoutRect clipRect(paintOffsetForClipRect + renderer.view().frameView().layoutContext().layoutDelta(), renderer.cachedSizeForOverflowClip());
if (m_clipped)
m_clipRect.intersect(clipRect);
else
m_clipRect = clipRect;
m_clipped = true;
// FIXME: <http://bugs.webkit.org/show_bug.cgi?id=13443> Apply control clip if present.
}
void RenderLayoutState::computePaginationInformation(const LocalFrameViewLayoutContext::LayoutStateStack& layoutStateStack, RenderBox& renderer, LayoutUnit pageLogicalHeight, bool pageLogicalHeightChanged)
{
auto* ancestor = layoutStateStack.isEmpty() ? nullptr : layoutStateStack.last().get();
// If we establish a new page height, then cache the offset to the top of the first page.
// We can compare this later on to figure out what part of the page we're actually on.
if (pageLogicalHeight || renderer.isRenderFragmentedFlow()) {
m_pageLogicalHeight = pageLogicalHeight;
bool isFlipped = renderer.writingMode().isBlockFlipped();
m_pageOffset = LayoutSize(m_layoutOffset.width() + (!isFlipped ? renderer.borderLeft() + renderer.paddingLeft() : renderer.borderRight() + renderer.paddingRight()), m_layoutOffset.height() + (!isFlipped ? renderer.borderTop() + renderer.paddingTop() : renderer.borderBottom() + renderer.paddingBottom()));
m_pageLogicalHeightChanged = pageLogicalHeightChanged;
m_isPaginated = true;
} else if (ancestor) {
// If we don't establish a new page height, then propagate the old page height and offset down.
m_pageLogicalHeight = ancestor->pageLogicalHeight();
m_pageLogicalHeightChanged = ancestor->pageLogicalHeightChanged();
m_pageOffset = ancestor->pageOffset();
// Disable pagination for objects we don't support. For now this includes overflow:scroll/auto, inline blocks and writing mode roots.
if (renderer.isUnsplittableForPagination()) {
m_pageLogicalHeight = 0;
m_isPaginated = false;
} else
m_isPaginated = m_pageLogicalHeight || renderer.enclosingFragmentedFlow();
}
// Propagate line grid information.
if (ancestor)
propagateLineGridInfo(*ancestor, renderer);
if (lineGrid() && (lineGrid()->writingMode().computedWritingMode() == renderer.writingMode().computedWritingMode())) {
if (CheckedPtr columnFlow = dynamicDowncast<RenderMultiColumnFlow>(renderer))
computeLineGridPaginationOrigin(*columnFlow);
}
// If we have a new grid to track, then add it to our set.
if (renderer.style().lineGrid() != RenderStyle::initialLineGrid()) {
if (CheckedPtr blockFlow = dynamicDowncast<RenderBlockFlow>(renderer))
establishLineGrid(layoutStateStack, *blockFlow);
}
}
LayoutUnit RenderLayoutState::pageLogicalOffset(RenderBox* child, LayoutUnit childLogicalOffset) const
{
if (child->isHorizontalWritingMode())
return m_layoutOffset.height() + childLogicalOffset - m_pageOffset.height();
return m_layoutOffset.width() + childLogicalOffset - m_pageOffset.width();
}
void RenderLayoutState::computeLineGridPaginationOrigin(const RenderMultiColumnFlow& multicol)
{
if (!isPaginated() || !pageLogicalHeight())
return;
if (!multicol.progressionIsInline())
return;
// We need to cache a line grid pagination origin so that we understand how to reset the line grid
// at the top of each column.
// Get the current line grid and offset.
ASSERT(m_lineGrid);
// Now determine our position on the grid. Our baseline needs to be adjusted to the nearest baseline multiple
// as established by the line box.
// FIXME: Need to handle crazy line-box-contain values that cause the root line box to not be considered. I assume
// the grid should honor line-box-contain.
bool isHorizontalWritingMode = m_lineGrid->isHorizontalWritingMode();
LayoutUnit lineGridBlockOffset = isHorizontalWritingMode ? m_lineGridOffset.height() : m_lineGridOffset.width();
LayoutUnit firstLineTop = lineGridBlockOffset + m_lineGrid->borderAndPaddingBefore();
LayoutUnit pageLogicalTop = isHorizontalWritingMode ? m_pageOffset.height() : m_pageOffset.width();
if (pageLogicalTop <= firstLineTop)
return;
// Shift to the next highest line grid multiple past the page logical top. Cache the delta
// between this new value and the page logical top as the pagination origin.
auto lineBoxHeight = LayoutUnit::fromFloatCeil(m_lineGrid->style().computedLineHeight());
if (!roundToInt(lineBoxHeight))
return;
LayoutUnit remainder = roundToInt(pageLogicalTop - firstLineTop) % roundToInt(lineBoxHeight);
LayoutUnit paginationDelta = lineBoxHeight - remainder;
if (isHorizontalWritingMode)
m_lineGridPaginationOrigin.setHeight(paginationDelta);
else
m_lineGridPaginationOrigin.setWidth(paginationDelta);
}
void RenderLayoutState::propagateLineGridInfo(const RenderLayoutState& ancestor, RenderBox& renderer)
{
// Disable line grids for objects we don't support. For now this includes overflow:scroll/auto, inline blocks and
// writing mode roots.
if (renderer.isUnsplittableForPagination())
return;
m_lineGrid = ancestor.lineGrid();
m_lineGridOffset = ancestor.lineGridOffset();
m_lineGridPaginationOrigin = ancestor.lineGridPaginationOrigin();
}
void RenderLayoutState::establishLineGrid(const LocalFrameViewLayoutContext::LayoutStateStack& layoutStateStack, RenderBlockFlow& renderer)
{
// First check to see if this grid has been established already.
if (m_lineGrid) {
if (m_lineGrid->style().lineGrid() == renderer.style().lineGrid())
return;
auto* currentGrid = m_lineGrid.get();
for (int i = layoutStateStack.size() - 1; i >= 0; --i) {
auto& currentState = *layoutStateStack[i].get();
if (currentState.m_lineGrid == currentGrid)
continue;
currentGrid = currentState.lineGrid();
if (!currentGrid)
break;
if (currentGrid->style().lineGrid() == renderer.style().lineGrid()) {
m_lineGrid = currentGrid;
m_lineGridOffset = currentState.m_lineGridOffset;
return;
}
}
}
// We didn't find an already-established grid with this identifier. Our render object establishes the grid.
m_lineGrid = renderer;
m_lineGridOffset = m_layoutOffset;
}
void RenderLayoutState::addLayoutDelta(LayoutSize delta)
{
m_layoutDelta += delta;
#if ASSERT_ENABLED
m_layoutDeltaXSaturated |= m_layoutDelta.width() == LayoutUnit::max() || m_layoutDelta.width() == LayoutUnit::min();
m_layoutDeltaYSaturated |= m_layoutDelta.height() == LayoutUnit::max() || m_layoutDelta.height() == LayoutUnit::min();
#endif
}
#if ASSERT_ENABLED
bool RenderLayoutState::layoutDeltaMatches(LayoutSize delta) const
{
return (delta.width() == m_layoutDelta.width() || m_layoutDeltaXSaturated) && (delta.height() == m_layoutDelta.height() || m_layoutDeltaYSaturated);
}
#endif
LayoutStateMaintainer::LayoutStateMaintainer(RenderBox& root, LayoutSize offset, bool disablePaintOffsetCache, LayoutUnit pageHeight, bool pageHeightChanged)
: m_context(root.view().frameView().layoutContext())
, m_paintOffsetCacheIsDisabled(disablePaintOffsetCache)
{
m_didPushLayoutState = m_context.pushLayoutState(root, offset, pageHeight, pageHeightChanged);
if (m_didPushLayoutState && m_paintOffsetCacheIsDisabled)
m_context.disablePaintOffsetCache();
}
LayoutStateMaintainer::~LayoutStateMaintainer()
{
if (!m_didPushLayoutState)
return;
m_context.popLayoutState();
if (m_paintOffsetCacheIsDisabled)
m_context.enablePaintOffsetCache();
}
LayoutStateDisabler::LayoutStateDisabler(LocalFrameViewLayoutContext& context)
: m_context(context)
{
m_context.disablePaintOffsetCache();
}
LayoutStateDisabler::~LayoutStateDisabler()
{
m_context.enablePaintOffsetCache();
}
static bool shouldDisablePaintOffsetCacheForSubtree(RenderElement& subtreeLayoutRoot)
{
for (auto* renderer = &subtreeLayoutRoot; renderer; renderer = renderer->container()) {
if (renderer->isTransformed() || renderer->hasReflection())
return true;
}
return false;
}
SubtreeLayoutStateMaintainer::SubtreeLayoutStateMaintainer(RenderElement* subtreeLayoutRoot)
{
if (subtreeLayoutRoot) {
m_context = &subtreeLayoutRoot->view().frameView().layoutContext();
m_context->pushLayoutState(*subtreeLayoutRoot);
if (shouldDisablePaintOffsetCacheForSubtree(*subtreeLayoutRoot)) {
m_context->disablePaintOffsetCache();
m_didDisablePaintOffsetCache = true;
}
}
}
SubtreeLayoutStateMaintainer::~SubtreeLayoutStateMaintainer()
{
if (m_context) {
m_context->popLayoutState();
if (m_didDisablePaintOffsetCache)
m_context->enablePaintOffsetCache();
}
}
FlexPercentResolveDisabler::FlexPercentResolveDisabler(LocalFrameViewLayoutContext& layoutContext, const RenderBox& flexItem)
: m_layoutContext(layoutContext)
, m_flexItem(flexItem)
{
m_layoutContext->disablePercentHeightResolveFor(flexItem);
}
FlexPercentResolveDisabler::~FlexPercentResolveDisabler()
{
m_layoutContext->enablePercentHeightResolveFor(m_flexItem);
}
ContentVisibilityForceLayoutScope::ContentVisibilityForceLayoutScope(LocalFrameViewLayoutContext& layoutContext, const Element* element)
: m_layoutContext(layoutContext)
, m_element(element)
{
if (element)
m_layoutContext->setNeedsSkippedContentLayout(true);
}
ContentVisibilityForceLayoutScope::~ContentVisibilityForceLayoutScope()
{
if (m_element) {
ASSERT(m_layoutContext->needsSkippedContentLayout());
m_layoutContext->setNeedsSkippedContentLayout(false);
}
}
} // namespace WebCore
|