Fix the issue that Web Audio test case fails on PR3.
[framework/web/webkit-efl.git] / Source / WebCore / rendering / LayoutState.cpp
1 /*
2  * Copyright (C) 2007 Apple Inc.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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. 
24  */
25
26 #include "config.h"
27 #include "LayoutState.h"
28
29 #include "ColumnInfo.h"
30 #include "RenderArena.h"
31 #include "RenderFlowThread.h"
32 #include "RenderInline.h"
33 #include "RenderLayer.h"
34 #include "RenderView.h"
35
36 namespace WebCore {
37
38 LayoutState::LayoutState(LayoutState* prev, RenderBox* renderer, const LayoutSize& offset, LayoutUnit pageLogicalHeight, bool pageLogicalHeightChanged, ColumnInfo* columnInfo)
39     : m_columnInfo(columnInfo)
40     , m_lineGrid(0)
41     , m_next(prev)
42 #ifndef NDEBUG
43     , m_renderer(renderer)
44 #endif
45 {
46     ASSERT(m_next);
47
48     bool fixed = renderer->isOutOfFlowPositioned() && renderer->style()->position() == FixedPosition;
49     if (fixed) {
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;
53     } else
54         m_paintOffset = prev->m_paintOffset + offset;
55
56     if (renderer->isOutOfFlowPositioned() && !fixed) {
57         if (RenderObject* container = renderer->container()) {
58             if (container->isRelPositioned() && container->isRenderInline())
59                 m_paintOffset += toRenderInline(container)->relativePositionedInlineOffset(renderer);
60         }
61     }
62
63     m_layoutOffset = m_paintOffset;
64
65     if (renderer->isRelPositioned() && renderer->hasLayer())
66         m_paintOffset += renderer->layer()->relativePositionOffset();
67
68     m_clipped = !fixed && prev->m_clipped;
69     if (m_clipped)
70         m_clipRect = prev->m_clipRect;
71
72     if (renderer->hasOverflowClip()) {
73         LayoutRect clipRect(toPoint(m_paintOffset) + renderer->view()->layoutDelta(), renderer->cachedSizeForOverflowClip());
74         if (m_clipped)
75             m_clipRect.intersect(clipRect);
76         else {
77             m_clipRect = clipRect;
78             m_clipped = true;
79         }
80
81         m_paintOffset -= renderer->scrolledContentOffset();
82     }
83
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;
92     } else {
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;
97         
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;
102     }
103     
104     // Propagate line grid information.
105     propagateLineGridInfo(renderer);
106
107     if (!m_columnInfo)
108         m_columnInfo = m_next->m_columnInfo;
109
110     m_layoutDelta = m_next->m_layoutDelta;
111     
112     m_isPaginated = m_pageLogicalHeight || m_columnInfo;
113
114     if (lineGrid() && renderer->hasColumns() && renderer->style()->hasInlineColumnAxis())
115         computeLineGridPaginationOrigin(renderer);
116
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));
120
121     // FIXME: <http://bugs.webkit.org/show_bug.cgi?id=13443> Apply control clip if present.
122 }
123
124 LayoutState::LayoutState(LayoutState* prev, RenderFlowThread* flowThread, bool regionsChanged)
125     : m_clipped(false)
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)
129     , m_columnInfo(0)
130     , m_lineGrid(0)
131     , m_next(prev)
132 #ifndef NDEBUG
133     , m_renderer(flowThread)
134 #endif
135 {
136 #ifdef NDEBUG
137     UNUSED_PARAM(flowThread);
138 #endif
139 }
140
141 LayoutState::LayoutState(RenderObject* root)
142     : m_clipped(false)
143     , m_isPaginated(false)
144     , m_pageLogicalHeight(0)
145     , m_pageLogicalHeightChanged(false)
146     , m_columnInfo(0)
147     , m_lineGrid(0)
148     , m_next(0)
149 #ifndef NDEBUG
150     , m_renderer(root)
151 #endif
152 {
153     RenderObject* container = root->container();
154     FloatPoint absContentPoint = container->localToAbsolute(FloatPoint(), false, true);
155     m_paintOffset = LayoutSize(absContentPoint.x(), absContentPoint.y());
156
157     if (container->hasOverflowClip()) {
158         m_clipped = true;
159         RenderBox* containerBox = toRenderBox(container);
160         m_clipRect = LayoutRect(toPoint(m_paintOffset), containerBox->cachedSizeForOverflowClip());
161         m_paintOffset -= containerBox->scrolledContentOffset();
162     }
163 }
164
165 #ifndef NDEBUG
166 static bool inLayoutStateDestroy;
167 #endif
168
169 void LayoutState::destroy(RenderArena* renderArena)
170 {
171 #ifndef NDEBUG
172     inLayoutStateDestroy = true;
173 #endif
174     delete this;
175 #ifndef NDEBUG
176     inLayoutStateDestroy = false;
177 #endif
178     renderArena->free(*(size_t*)this, this);
179 }
180
181 void* LayoutState::operator new(size_t sz, RenderArena* renderArena)
182 {
183     return renderArena->allocate(sz);
184 }
185
186 void LayoutState::operator delete(void* ptr, size_t sz)
187 {
188     ASSERT(inLayoutStateDestroy);
189     *(size_t*)ptr = sz;
190 }
191
192 void LayoutState::clearPaginationInformation()
193 {
194     m_pageLogicalHeight = m_next->m_pageLogicalHeight;
195     m_pageOffset = m_next->m_pageOffset;
196     m_columnInfo = m_next->m_columnInfo;
197 }
198
199 LayoutUnit LayoutState::pageLogicalOffset(RenderBox* child, LayoutUnit childLogicalOffset) const
200 {
201     if (child->isHorizontalWritingMode())
202         return m_layoutOffset.height() + childLogicalOffset - m_pageOffset.height();
203     return m_layoutOffset.width() + childLogicalOffset - m_pageOffset.width();
204 }
205
206 void LayoutState::addForcedColumnBreak(RenderBox* child, LayoutUnit childLogicalOffset)
207 {
208     if (!m_columnInfo || m_columnInfo->columnHeight())
209         return;
210     m_columnInfo->addForcedBreak(pageLogicalOffset(child, childLogicalOffset));
211 }
212
213 void LayoutState::propagateLineGridInfo(RenderBox* renderer)
214 {
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())
218         return;
219
220     m_lineGrid = m_next->m_lineGrid;
221     m_lineGridOffset = m_next->m_lineGridOffset;
222     m_lineGridPaginationOrigin = m_next->m_lineGridPaginationOrigin;
223 }
224
225 void LayoutState::establishLineGrid(RenderBlock* block)
226 {
227     // First check to see if this grid has been established already.
228     if (m_lineGrid) {
229         if (m_lineGrid->style()->lineGrid() == block->style()->lineGrid())
230             return;
231         RenderBlock* currentGrid = m_lineGrid;
232         for (LayoutState* currentState = m_next; currentState; currentState = currentState->m_next) {
233             if (currentState->m_lineGrid == currentGrid)
234                 continue;
235             currentGrid = currentState->m_lineGrid;
236             if (!currentGrid)
237                 break;
238             if (currentGrid->style()->lineGrid() == block->style()->lineGrid()) {
239                 m_lineGrid = currentGrid;
240                 m_lineGridOffset = currentState->m_lineGridOffset;
241                 return;
242             }
243         }
244     }
245     
246     // We didn't find an already-established grid with this identifier. Our render object establishes the grid.
247     m_lineGrid = block;
248     m_lineGridOffset = m_layoutOffset; 
249 }
250
251 void LayoutState::computeLineGridPaginationOrigin(RenderBox* renderer)
252 {
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())
257         return;
258
259     // Get the hypothetical line box used to establish the grid.
260     RootInlineBox* lineGridBox = lineGrid()->lineGridBox();
261     if (!lineGridBox)
262         return;
263     
264     bool isHorizontalWritingMode = lineGrid()->isHorizontalWritingMode();
265
266     LayoutUnit lineGridBlockOffset = isHorizontalWritingMode ? lineGridOffset().height() : lineGridOffset().width();
267
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();
273     if (!gridLineHeight)
274         return;
275
276     LayoutUnit firstLineTopWithLeading = lineGridBlockOffset + lineGridBox->lineTopWithLeading();
277     
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);
287             else
288                 m_lineGridPaginationOrigin.setWidth(paginationDelta);
289         }
290     }
291 }
292
293 } // namespace WebCore