Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / 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 "core/rendering/LayoutState.h"
28
29 #include "core/rendering/RenderInline.h"
30 #include "core/rendering/RenderLayer.h"
31 #include "core/rendering/RenderView.h"
32 #include "platform/Partitions.h"
33
34 namespace WebCore {
35
36 LayoutState::LayoutState(LayoutState* prev, RenderBox* renderer, const LayoutSize& offset, LayoutUnit pageLogicalHeight, bool pageLogicalHeightChanged, ColumnInfo* columnInfo)
37     : m_columnInfo(columnInfo)
38     , m_next(prev)
39     , m_shapeInsideInfo(0)
40 #ifndef NDEBUG
41     , m_renderer(renderer)
42 #endif
43 {
44     ASSERT(m_next);
45
46     bool fixed = renderer->isOutOfFlowPositioned() && renderer->style()->position() == FixedPosition;
47     if (fixed) {
48         // FIXME: This doesn't work correctly with transforms.
49         FloatPoint fixedOffset = renderer->view()->localToAbsolute(FloatPoint(), IsFixed);
50         m_paintOffset = LayoutSize(fixedOffset.x(), fixedOffset.y()) + offset;
51     } else
52         m_paintOffset = prev->m_paintOffset + offset;
53
54     if (renderer->isOutOfFlowPositioned() && !fixed) {
55         if (RenderObject* container = renderer->container()) {
56             if (container->isInFlowPositioned() && container->isRenderInline())
57                 m_paintOffset += toRenderInline(container)->offsetForInFlowPositionedInline(renderer);
58         }
59     }
60
61     m_layoutOffset = m_paintOffset;
62
63     if (renderer->isInFlowPositioned() && renderer->hasLayer())
64         m_paintOffset += renderer->layer()->offsetForInFlowPosition();
65
66     m_clipped = !fixed && prev->m_clipped;
67     if (m_clipped)
68         m_clipRect = prev->m_clipRect;
69
70     if (renderer->hasOverflowClip()) {
71         LayoutSize deltaSize = RuntimeEnabledFeatures::repaintAfterLayoutEnabled() ? LayoutSize() : renderer->view()->layoutDelta();
72
73         LayoutRect clipRect(toPoint(m_paintOffset) + deltaSize, 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 || renderer->isRenderFlowThread()) {
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     if (!m_columnInfo)
105         m_columnInfo = m_next->m_columnInfo;
106
107     if (renderer->isRenderBlock()) {
108         const RenderBlock* renderBlock = toRenderBlock(renderer);
109         m_shapeInsideInfo = renderBlock->shapeInsideInfo();
110         if (!m_shapeInsideInfo && m_next->m_shapeInsideInfo && renderBlock->allowsShapeInsideInfoSharing(m_next->m_shapeInsideInfo->owner()))
111             m_shapeInsideInfo = m_next->m_shapeInsideInfo;
112     }
113
114     if (!RuntimeEnabledFeatures::repaintAfterLayoutEnabled()) {
115         m_layoutDelta = m_next->m_layoutDelta;
116 #if !ASSERT_DISABLED
117         m_layoutDeltaXSaturated = m_next->m_layoutDeltaXSaturated;
118         m_layoutDeltaYSaturated = m_next->m_layoutDeltaYSaturated;
119 #endif
120     }
121
122     m_isPaginated = m_pageLogicalHeight || m_columnInfo || renderer->isRenderFlowThread();
123
124     // FIXME: <http://bugs.webkit.org/show_bug.cgi?id=13443> Apply control clip if present.
125 }
126
127 LayoutState::LayoutState(RenderObject* root)
128     : m_clipped(false)
129     , m_isPaginated(false)
130     , m_pageLogicalHeightChanged(false)
131 #if !ASSERT_DISABLED
132     , m_layoutDeltaXSaturated(false)
133     , m_layoutDeltaYSaturated(false)
134 #endif
135     , m_columnInfo(0)
136     , m_next(0)
137     , m_shapeInsideInfo(0)
138     , m_pageLogicalHeight(0)
139 #ifndef NDEBUG
140     , m_renderer(root)
141 #endif
142 {
143     RenderObject* container = root->container();
144     FloatPoint absContentPoint = container->localToAbsolute(FloatPoint(), UseTransforms);
145     m_paintOffset = LayoutSize(absContentPoint.x(), absContentPoint.y());
146
147     if (container->hasOverflowClip()) {
148         m_clipped = true;
149         RenderBox* containerBox = toRenderBox(container);
150         m_clipRect = LayoutRect(toPoint(m_paintOffset), containerBox->cachedSizeForOverflowClip());
151         m_paintOffset -= containerBox->scrolledContentOffset();
152     }
153 }
154
155 void* LayoutState::operator new(size_t sz)
156 {
157     return partitionAlloc(Partitions::getRenderingPartition(), sz);
158 }
159
160 void LayoutState::operator delete(void* ptr)
161 {
162     partitionFree(ptr);
163 }
164
165 void LayoutState::clearPaginationInformation()
166 {
167     m_pageLogicalHeight = m_next->m_pageLogicalHeight;
168     m_pageOffset = m_next->m_pageOffset;
169     m_columnInfo = m_next->m_columnInfo;
170 }
171
172 LayoutUnit LayoutState::pageLogicalOffset(const RenderBox* child, LayoutUnit childLogicalOffset) const
173 {
174     if (child->isHorizontalWritingMode())
175         return m_layoutOffset.height() + childLogicalOffset - m_pageOffset.height();
176     return m_layoutOffset.width() + childLogicalOffset - m_pageOffset.width();
177 }
178
179 void LayoutState::addForcedColumnBreak(RenderBox* child, LayoutUnit childLogicalOffset)
180 {
181     if (!m_columnInfo || m_columnInfo->columnHeight())
182         return;
183     m_columnInfo->addForcedBreak(pageLogicalOffset(child, childLogicalOffset));
184 }
185
186 } // namespace WebCore