Home | History | Annotate | Download | only in rendering
      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(LayoutUnit pageLogicalHeight, bool pageLogicalHeightChanged, RenderView& view)
     37     : m_clipped(false)
     38     , m_isPaginated(pageLogicalHeight)
     39     , m_pageLogicalHeightChanged(pageLogicalHeightChanged)
     40     , m_cachedOffsetsEnabled(true)
     41 #if ASSERT_ENABLED
     42     , m_layoutDeltaXSaturated(false)
     43     , m_layoutDeltaYSaturated(false)
     44 #endif
     45     , m_columnInfo(0)
     46     , m_next(0)
     47     , m_pageLogicalHeight(pageLogicalHeight)
     48     , m_renderer(view)
     49 {
     50     ASSERT(!view.layoutState());
     51     view.pushLayoutState(*this);
     52 }
     53 
     54 LayoutState::LayoutState(RenderBox& renderer, const LayoutSize& offset, LayoutUnit pageLogicalHeight, bool pageLogicalHeightChanged, ColumnInfo* columnInfo)
     55     : m_columnInfo(columnInfo)
     56     , m_next(renderer.view()->layoutState())
     57     , m_renderer(renderer)
     58 {
     59     renderer.view()->pushLayoutState(*this);
     60     m_cachedOffsetsEnabled = m_next->m_cachedOffsetsEnabled && renderer.supportsLayoutStateCachedOffsets();
     61     bool fixed = renderer.isOutOfFlowPositioned() && renderer.style()->position() == FixedPosition;
     62     if (fixed) {
     63         // FIXME: This doesn't work correctly with transforms.
     64         FloatPoint fixedOffset = renderer.view()->localToAbsolute(FloatPoint(), IsFixed);
     65         m_paintOffset = LayoutSize(fixedOffset.x(), fixedOffset.y()) + offset;
     66     } else {
     67         m_paintOffset = m_next->m_paintOffset + offset;
     68     }
     69 
     70     if (renderer.isOutOfFlowPositioned() && !fixed) {
     71         if (RenderObject* container = renderer.container()) {
     72             if (container->isInFlowPositioned() && container->isRenderInline())
     73                 m_paintOffset += toRenderInline(container)->offsetForInFlowPositionedInline(renderer);
     74         }
     75     }
     76 
     77     m_layoutOffset = m_paintOffset;
     78 
     79     if (renderer.isInFlowPositioned() && renderer.hasLayer())
     80         m_paintOffset += renderer.layer()->offsetForInFlowPosition();
     81 
     82     m_clipped = !fixed && m_next->m_clipped;
     83     if (m_clipped)
     84         m_clipRect = m_next->m_clipRect;
     85 
     86     if (renderer.hasOverflowClip()) {
     87         LayoutSize deltaSize = RuntimeEnabledFeatures::repaintAfterLayoutEnabled() ? LayoutSize() : renderer.view()->layoutDelta();
     88 
     89         LayoutRect clipRect(toPoint(m_paintOffset) + deltaSize, renderer.cachedSizeForOverflowClip());
     90         if (m_clipped)
     91             m_clipRect.intersect(clipRect);
     92         else {
     93             m_clipRect = clipRect;
     94             m_clipped = true;
     95         }
     96 
     97         m_paintOffset -= renderer.scrolledContentOffset();
     98     }
     99 
    100     // If we establish a new page height, then cache the offset to the top of the first page.
    101     // We can compare this later on to figure out what part of the page we're actually on,
    102     if (pageLogicalHeight || m_columnInfo || renderer.isRenderFlowThread()) {
    103         m_pageLogicalHeight = pageLogicalHeight;
    104         bool isFlipped = renderer.style()->isFlippedBlocksWritingMode();
    105         m_pageOffset = LayoutSize(m_layoutOffset.width() + (!isFlipped ? renderer.borderLeft() + renderer.paddingLeft() : renderer.borderRight() + renderer.paddingRight()),
    106             m_layoutOffset.height() + (!isFlipped ? renderer.borderTop() + renderer.paddingTop() : renderer.borderBottom() + renderer.paddingBottom()));
    107         m_pageLogicalHeightChanged = pageLogicalHeightChanged;
    108         m_isPaginated = true;
    109     } else {
    110         // If we don't establish a new page height, then propagate the old page height and offset down.
    111         m_pageLogicalHeight = m_next->m_pageLogicalHeight;
    112         m_pageLogicalHeightChanged = m_next->m_pageLogicalHeightChanged;
    113         m_pageOffset = m_next->m_pageOffset;
    114 
    115         // Disable pagination for objects we don't support. For now this includes overflow:scroll/auto, inline blocks and
    116         // writing mode roots.
    117         if (renderer.isUnsplittableForPagination()) {
    118             m_pageLogicalHeight = 0;
    119             m_isPaginated = false;
    120         } else {
    121             m_isPaginated = m_pageLogicalHeight || m_next->m_columnInfo || renderer.flowThreadContainingBlock();
    122         }
    123     }
    124 
    125     if (!m_columnInfo)
    126         m_columnInfo = m_next->m_columnInfo;
    127 
    128     if (!RuntimeEnabledFeatures::repaintAfterLayoutEnabled()) {
    129         m_layoutDelta = m_next->m_layoutDelta;
    130 #if ASSERT_ENABLED
    131         m_layoutDeltaXSaturated = m_next->m_layoutDeltaXSaturated;
    132         m_layoutDeltaYSaturated = m_next->m_layoutDeltaYSaturated;
    133 #endif
    134     }
    135 
    136     // FIXME: <http://bugs.webkit.org/show_bug.cgi?id=13443> Apply control clip if present.
    137 }
    138 
    139 inline static bool shouldDisableLayoutStateForSubtree(RenderObject& renderer)
    140 {
    141     RenderObject* object = &renderer;
    142     while (object) {
    143         if (object->supportsLayoutStateCachedOffsets())
    144             return true;
    145         object = object->container();
    146     }
    147     return false;
    148 }
    149 
    150 LayoutState::LayoutState(RenderObject& root)
    151     : m_clipped(false)
    152     , m_isPaginated(false)
    153     , m_pageLogicalHeightChanged(false)
    154     , m_cachedOffsetsEnabled(shouldDisableLayoutStateForSubtree(root))
    155 #if ASSERT_ENABLED
    156     , m_layoutDeltaXSaturated(false)
    157     , m_layoutDeltaYSaturated(false)
    158 #endif
    159     , m_columnInfo(0)
    160     , m_next(root.view()->layoutState())
    161     , m_pageLogicalHeight(0)
    162     , m_renderer(root)
    163 {
    164     // FIXME: Why does RenderTableSection create this wonky LayoutState?
    165     ASSERT(!m_next || root.isTableSection());
    166     // We'll end up pushing in RenderView itself, so don't bother adding it.
    167     if (root.isRenderView())
    168         return;
    169 
    170     root.view()->pushLayoutState(*this);
    171 
    172     RenderObject* container = root.container();
    173     FloatPoint absContentPoint = container->localToAbsolute(FloatPoint(), UseTransforms);
    174     m_paintOffset = LayoutSize(absContentPoint.x(), absContentPoint.y());
    175 
    176     if (container->hasOverflowClip()) {
    177         m_clipped = true;
    178         RenderBox* containerBox = toRenderBox(container);
    179         m_clipRect = LayoutRect(toPoint(m_paintOffset), containerBox->cachedSizeForOverflowClip());
    180         m_paintOffset -= containerBox->scrolledContentOffset();
    181     }
    182 }
    183 
    184 LayoutState::~LayoutState()
    185 {
    186     if (m_renderer.view()->layoutState()) {
    187         ASSERT(m_renderer.view()->layoutState() == this);
    188         m_renderer.view()->popLayoutState();
    189     }
    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(const RenderBox& child, const 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(const RenderBox& child, const LayoutUnit& childLogicalOffset)
    207 {
    208     if (!m_columnInfo || m_columnInfo->columnHeight())
    209         return;
    210     m_columnInfo->addForcedBreak(pageLogicalOffset(child, childLogicalOffset));
    211 }
    212 
    213 } // namespace WebCore
    214