2 * Copyright (C) 2007 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "LayoutState.h"
29 #include "ColumnInfo.h"
30 #include "RenderArena.h"
31 #include "RenderFlowThread.h"
32 #include "RenderInline.h"
33 #include "RenderLayer.h"
34 #include "RenderView.h"
38 LayoutState::LayoutState(LayoutState* prev, RenderBox* renderer, const LayoutSize& offset, LayoutUnit pageLogicalHeight, bool pageLogicalHeightChanged, ColumnInfo* columnInfo)
39 : m_columnInfo(columnInfo)
43 , m_renderer(renderer)
48 bool fixed = renderer->isOutOfFlowPositioned() && renderer->style()->position() == FixedPosition;
50 // FIXME: This doesn't work correctly with transforms.
51 FloatPoint fixedOffset = renderer->view()->localToAbsolute(FloatPoint(), true);
52 m_paintOffset = LayoutSize(fixedOffset.x(), fixedOffset.y()) + offset;
54 m_paintOffset = prev->m_paintOffset + offset;
56 if (renderer->isOutOfFlowPositioned() && !fixed) {
57 if (RenderObject* container = renderer->container()) {
58 if (container->isRelPositioned() && container->isRenderInline())
59 m_paintOffset += toRenderInline(container)->relativePositionedInlineOffset(renderer);
63 m_layoutOffset = m_paintOffset;
65 if (renderer->isRelPositioned() && renderer->hasLayer())
66 m_paintOffset += renderer->layer()->relativePositionOffset();
68 m_clipped = !fixed && prev->m_clipped;
70 m_clipRect = prev->m_clipRect;
72 if (renderer->hasOverflowClip()) {
73 LayoutRect clipRect(toPoint(m_paintOffset) + renderer->view()->layoutDelta(), renderer->cachedSizeForOverflowClip());
75 m_clipRect.intersect(clipRect);
77 m_clipRect = clipRect;
81 m_paintOffset -= renderer->scrolledContentOffset();
84 // If we establish a new page height, then cache the offset to the top of the first page.
85 // We can compare this later on to figure out what part of the page we're actually on,
86 if (pageLogicalHeight || m_columnInfo) {
87 m_pageLogicalHeight = pageLogicalHeight;
88 bool isFlipped = renderer->style()->isFlippedBlocksWritingMode();
89 m_pageOffset = LayoutSize(m_layoutOffset.width() + (!isFlipped ? renderer->borderLeft() + renderer->paddingLeft() : renderer->borderRight() + renderer->paddingRight()),
90 m_layoutOffset.height() + (!isFlipped ? renderer->borderTop() + renderer->paddingTop() : renderer->borderBottom() + renderer->paddingBottom()));
91 m_pageLogicalHeightChanged = pageLogicalHeightChanged;
93 // If we don't establish a new page height, then propagate the old page height and offset down.
94 m_pageLogicalHeight = m_next->m_pageLogicalHeight;
95 m_pageLogicalHeightChanged = m_next->m_pageLogicalHeightChanged;
96 m_pageOffset = m_next->m_pageOffset;
98 // Disable pagination for objects we don't support. For now this includes overflow:scroll/auto, inline blocks and
99 // writing mode roots.
100 if (renderer->isUnsplittableForPagination())
101 m_pageLogicalHeight = 0;
104 // Propagate line grid information.
105 propagateLineGridInfo(renderer);
108 m_columnInfo = m_next->m_columnInfo;
110 m_layoutDelta = m_next->m_layoutDelta;
112 m_isPaginated = m_pageLogicalHeight || m_columnInfo;
114 if (lineGrid() && renderer->hasColumns() && renderer->style()->hasInlineColumnAxis())
115 computeLineGridPaginationOrigin(renderer);
117 // If we have a new grid to track, then add it to our set.
118 if (renderer->style()->lineGrid() != RenderStyle::initialLineGrid() && renderer->isBlockFlow())
119 establishLineGrid(toRenderBlock(renderer));
121 // FIXME: <http://bugs.webkit.org/show_bug.cgi?id=13443> Apply control clip if present.
124 LayoutState::LayoutState(LayoutState* prev, RenderFlowThread* flowThread, bool regionsChanged)
126 , m_isPaginated(true)
127 , m_pageLogicalHeight(1) // Use a fake height here. That value is not important, just needs to be non-zero.
128 , m_pageLogicalHeightChanged(regionsChanged)
133 , m_renderer(flowThread)
137 UNUSED_PARAM(flowThread);
141 LayoutState::LayoutState(RenderObject* root)
143 , m_isPaginated(false)
144 , m_pageLogicalHeight(0)
145 , m_pageLogicalHeightChanged(false)
153 RenderObject* container = root->container();
154 FloatPoint absContentPoint = container->localToAbsolute(FloatPoint(), false, true);
155 m_paintOffset = LayoutSize(absContentPoint.x(), absContentPoint.y());
157 if (container->hasOverflowClip()) {
159 RenderBox* containerBox = toRenderBox(container);
160 m_clipRect = LayoutRect(toPoint(m_paintOffset), containerBox->cachedSizeForOverflowClip());
161 m_paintOffset -= containerBox->scrolledContentOffset();
166 static bool inLayoutStateDestroy;
169 void LayoutState::destroy(RenderArena* renderArena)
172 inLayoutStateDestroy = true;
176 inLayoutStateDestroy = false;
178 renderArena->free(*(size_t*)this, this);
181 void* LayoutState::operator new(size_t sz, RenderArena* renderArena)
183 return renderArena->allocate(sz);
186 void LayoutState::operator delete(void* ptr, size_t sz)
188 ASSERT(inLayoutStateDestroy);
192 void LayoutState::clearPaginationInformation()
194 m_pageLogicalHeight = m_next->m_pageLogicalHeight;
195 m_pageOffset = m_next->m_pageOffset;
196 m_columnInfo = m_next->m_columnInfo;
199 LayoutUnit LayoutState::pageLogicalOffset(RenderBox* child, LayoutUnit childLogicalOffset) const
201 if (child->isHorizontalWritingMode())
202 return m_layoutOffset.height() + childLogicalOffset - m_pageOffset.height();
203 return m_layoutOffset.width() + childLogicalOffset - m_pageOffset.width();
206 void LayoutState::addForcedColumnBreak(RenderBox* child, LayoutUnit childLogicalOffset)
208 if (!m_columnInfo || m_columnInfo->columnHeight())
210 m_columnInfo->addForcedBreak(pageLogicalOffset(child, childLogicalOffset));
213 void LayoutState::propagateLineGridInfo(RenderBox* renderer)
215 // Disable line grids for objects we don't support. For now this includes overflow:scroll/auto, inline blocks and
216 // writing mode roots.
217 if (!m_next || renderer->isUnsplittableForPagination())
220 m_lineGrid = m_next->m_lineGrid;
221 m_lineGridOffset = m_next->m_lineGridOffset;
222 m_lineGridPaginationOrigin = m_next->m_lineGridPaginationOrigin;
225 void LayoutState::establishLineGrid(RenderBlock* block)
227 // First check to see if this grid has been established already.
229 if (m_lineGrid->style()->lineGrid() == block->style()->lineGrid())
231 RenderBlock* currentGrid = m_lineGrid;
232 for (LayoutState* currentState = m_next; currentState; currentState = currentState->m_next) {
233 if (currentState->m_lineGrid == currentGrid)
235 currentGrid = currentState->m_lineGrid;
238 if (currentGrid->style()->lineGrid() == block->style()->lineGrid()) {
239 m_lineGrid = currentGrid;
240 m_lineGridOffset = currentState->m_lineGridOffset;
246 // We didn't find an already-established grid with this identifier. Our render object establishes the grid.
248 m_lineGridOffset = m_layoutOffset;
251 void LayoutState::computeLineGridPaginationOrigin(RenderBox* renderer)
253 // We need to cache a line grid pagination origin so that we understand how to reset the line grid
254 // at the top of each column.
255 // Get the current line grid and offset.
256 if (!lineGrid() || lineGrid()->style()->writingMode() != renderer->style()->writingMode())
259 // Get the hypothetical line box used to establish the grid.
260 RootInlineBox* lineGridBox = lineGrid()->lineGridBox();
264 bool isHorizontalWritingMode = lineGrid()->isHorizontalWritingMode();
266 LayoutUnit lineGridBlockOffset = isHorizontalWritingMode ? lineGridOffset().height() : lineGridOffset().width();
268 // Now determine our position on the grid. Our baseline needs to be adjusted to the nearest baseline multiple
269 // as established by the line box.
270 // FIXME: Need to handle crazy line-box-contain values that cause the root line box to not be considered. I assume
271 // the grid should honor line-box-contain.
272 LayoutUnit gridLineHeight = lineGridBox->lineBottomWithLeading() - lineGridBox->lineTopWithLeading();
276 LayoutUnit firstLineTopWithLeading = lineGridBlockOffset + lineGridBox->lineTopWithLeading();
278 if (isPaginated() && pageLogicalHeight()) {
279 LayoutUnit pageLogicalTop = renderer->isHorizontalWritingMode() ? m_pageOffset.height() : m_pageOffset.width();
280 if (pageLogicalTop > firstLineTopWithLeading) {
281 // Shift to the next highest line grid multiple past the page logical top. Cache the delta
282 // between this new value and the page logical top as the pagination origin.
283 LayoutUnit remainder = roundToInt(pageLogicalTop - firstLineTopWithLeading) % roundToInt(gridLineHeight);
284 LayoutUnit paginationDelta = gridLineHeight - remainder;
285 if (isHorizontalWritingMode)
286 m_lineGridPaginationOrigin.setHeight(paginationDelta);
288 m_lineGridPaginationOrigin.setWidth(paginationDelta);
293 } // namespace WebCore