Home | History | Annotate | Download | only in rendering
      1 /*
      2  * Copyright (C) 2008 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  *
      8  * 1.  Redistributions of source code must retain the above copyright
      9  *     notice, this list of conditions and the following disclaimer.
     10  * 2.  Redistributions in binary form must reproduce the above copyright
     11  *     notice, this list of conditions and the following disclaimer in the
     12  *     documentation and/or other materials provided with the distribution.
     13  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
     14  *     its contributors may be used to endorse or promote products derived
     15  *     from this software without specific prior written permission.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
     18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     20  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
     21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include "config.h"
     30 #include "core/rendering/RenderLineBoxList.h"
     31 
     32 #include "core/rendering/HitTestResult.h"
     33 #include "core/rendering/InlineTextBox.h"
     34 #include "core/rendering/PaintInfo.h"
     35 #include "core/rendering/RenderInline.h"
     36 #include "core/rendering/RootInlineBox.h"
     37 
     38 using namespace std;
     39 
     40 namespace WebCore {
     41 
     42 #ifndef NDEBUG
     43 RenderLineBoxList::~RenderLineBoxList()
     44 {
     45     ASSERT(!m_firstLineBox);
     46     ASSERT(!m_lastLineBox);
     47 }
     48 #endif
     49 
     50 void RenderLineBoxList::appendLineBox(InlineFlowBox* box)
     51 {
     52     checkConsistency();
     53 
     54     if (!m_firstLineBox)
     55         m_firstLineBox = m_lastLineBox = box;
     56     else {
     57         m_lastLineBox->setNextLineBox(box);
     58         box->setPreviousLineBox(m_lastLineBox);
     59         m_lastLineBox = box;
     60     }
     61 
     62     checkConsistency();
     63 }
     64 
     65 void RenderLineBoxList::deleteLineBoxTree()
     66 {
     67     InlineFlowBox* line = m_firstLineBox;
     68     InlineFlowBox* nextLine;
     69     while (line) {
     70         nextLine = line->nextLineBox();
     71         line->deleteLine();
     72         line = nextLine;
     73     }
     74     m_firstLineBox = m_lastLineBox = 0;
     75 }
     76 
     77 void RenderLineBoxList::extractLineBox(InlineFlowBox* box)
     78 {
     79     checkConsistency();
     80 
     81     m_lastLineBox = box->prevLineBox();
     82     if (box == m_firstLineBox)
     83         m_firstLineBox = 0;
     84     if (box->prevLineBox())
     85         box->prevLineBox()->setNextLineBox(0);
     86     box->setPreviousLineBox(0);
     87     for (InlineFlowBox* curr = box; curr; curr = curr->nextLineBox())
     88         curr->setExtracted();
     89 
     90     checkConsistency();
     91 }
     92 
     93 void RenderLineBoxList::attachLineBox(InlineFlowBox* box)
     94 {
     95     checkConsistency();
     96 
     97     if (m_lastLineBox) {
     98         m_lastLineBox->setNextLineBox(box);
     99         box->setPreviousLineBox(m_lastLineBox);
    100     } else
    101         m_firstLineBox = box;
    102     InlineFlowBox* last = box;
    103     for (InlineFlowBox* curr = box; curr; curr = curr->nextLineBox()) {
    104         curr->setExtracted(false);
    105         last = curr;
    106     }
    107     m_lastLineBox = last;
    108 
    109     checkConsistency();
    110 }
    111 
    112 void RenderLineBoxList::removeLineBox(InlineFlowBox* box)
    113 {
    114     checkConsistency();
    115 
    116     if (box == m_firstLineBox)
    117         m_firstLineBox = box->nextLineBox();
    118     if (box == m_lastLineBox)
    119         m_lastLineBox = box->prevLineBox();
    120     if (box->nextLineBox())
    121         box->nextLineBox()->setPreviousLineBox(box->prevLineBox());
    122     if (box->prevLineBox())
    123         box->prevLineBox()->setNextLineBox(box->nextLineBox());
    124 
    125     checkConsistency();
    126 }
    127 
    128 void RenderLineBoxList::deleteLineBoxes()
    129 {
    130     if (m_firstLineBox) {
    131         InlineFlowBox* next;
    132         for (InlineFlowBox* curr = m_firstLineBox; curr; curr = next) {
    133             next = curr->nextLineBox();
    134             curr->destroy();
    135         }
    136         m_firstLineBox = 0;
    137         m_lastLineBox = 0;
    138     }
    139 }
    140 
    141 void RenderLineBoxList::dirtyLineBoxes()
    142 {
    143     for (InlineFlowBox* curr = firstLineBox(); curr; curr = curr->nextLineBox())
    144         curr->dirtyLineBoxes();
    145 }
    146 
    147 bool RenderLineBoxList::rangeIntersectsRect(RenderBoxModelObject* renderer, LayoutUnit logicalTop, LayoutUnit logicalBottom, const LayoutRect& rect, const LayoutPoint& offset) const
    148 {
    149     RenderBox* block;
    150     if (renderer->isBox())
    151         block = toRenderBox(renderer);
    152     else
    153         block = renderer->containingBlock();
    154     LayoutUnit physicalStart = block->flipForWritingMode(logicalTop);
    155     LayoutUnit physicalEnd = block->flipForWritingMode(logicalBottom);
    156     LayoutUnit physicalExtent = absoluteValue(physicalEnd - physicalStart);
    157     physicalStart = min(physicalStart, physicalEnd);
    158 
    159     if (renderer->style()->isHorizontalWritingMode()) {
    160         physicalStart += offset.y();
    161         if (physicalStart >= rect.maxY() || physicalStart + physicalExtent <= rect.y())
    162             return false;
    163     } else {
    164         physicalStart += offset.x();
    165         if (physicalStart >= rect.maxX() || physicalStart + physicalExtent <= rect.x())
    166             return false;
    167     }
    168 
    169     return true;
    170 }
    171 
    172 bool RenderLineBoxList::anyLineIntersectsRect(RenderBoxModelObject* renderer, const LayoutRect& rect, const LayoutPoint& offset, LayoutUnit outlineSize) const
    173 {
    174     // We can check the first box and last box and avoid painting/hit testing if we don't
    175     // intersect.  This is a quick short-circuit that we can take to avoid walking any lines.
    176     // FIXME: This check is flawed in the following extremely obscure way:
    177     // if some line in the middle has a huge overflow, it might actually extend below the last line.
    178     RootInlineBox* firstRootBox = firstLineBox()->root();
    179     RootInlineBox* lastRootBox = lastLineBox()->root();
    180     LayoutUnit firstLineTop = firstLineBox()->logicalTopVisualOverflow(firstRootBox->lineTop());
    181     LayoutUnit lastLineBottom = lastLineBox()->logicalBottomVisualOverflow(lastRootBox->lineBottom());
    182     LayoutUnit logicalTop = firstLineTop - outlineSize;
    183     LayoutUnit logicalBottom = outlineSize + lastLineBottom;
    184 
    185     return rangeIntersectsRect(renderer, logicalTop, logicalBottom, rect, offset);
    186 }
    187 
    188 bool RenderLineBoxList::lineIntersectsDirtyRect(RenderBoxModelObject* renderer, InlineFlowBox* box, const PaintInfo& paintInfo, const LayoutPoint& offset) const
    189 {
    190     RootInlineBox* root = box->root();
    191     LayoutUnit logicalTop = min<LayoutUnit>(box->logicalTopVisualOverflow(root->lineTop()), root->selectionTop()) - renderer->maximalOutlineSize(paintInfo.phase);
    192     LayoutUnit logicalBottom = box->logicalBottomVisualOverflow(root->lineBottom()) + renderer->maximalOutlineSize(paintInfo.phase);
    193 
    194     return rangeIntersectsRect(renderer, logicalTop, logicalBottom, paintInfo.rect, offset);
    195 }
    196 
    197 void RenderLineBoxList::paint(RenderBoxModelObject* renderer, PaintInfo& paintInfo, const LayoutPoint& paintOffset) const
    198 {
    199     // Only paint during the foreground/selection phases.
    200     if (paintInfo.phase != PaintPhaseForeground && paintInfo.phase != PaintPhaseSelection && paintInfo.phase != PaintPhaseOutline
    201         && paintInfo.phase != PaintPhaseSelfOutline && paintInfo.phase != PaintPhaseChildOutlines && paintInfo.phase != PaintPhaseTextClip
    202         && paintInfo.phase != PaintPhaseMask)
    203         return;
    204 
    205     ASSERT(renderer->isRenderBlock() || (renderer->isRenderInline() && renderer->hasLayer())); // The only way an inline could paint like this is if it has a layer.
    206 
    207     // If we have no lines then we have no work to do.
    208     if (!firstLineBox())
    209         return;
    210 
    211     LayoutUnit outlineSize = renderer->maximalOutlineSize(paintInfo.phase);
    212     if (!anyLineIntersectsRect(renderer, paintInfo.rect, paintOffset, outlineSize))
    213         return;
    214 
    215     PaintInfo info(paintInfo);
    216     ListHashSet<RenderInline*> outlineObjects;
    217     info.outlineObjects = &outlineObjects;
    218 
    219     // See if our root lines intersect with the dirty rect.  If so, then we paint
    220     // them.  Note that boxes can easily overlap, so we can't make any assumptions
    221     // based off positions of our first line box or our last line box.
    222     for (InlineFlowBox* curr = firstLineBox(); curr; curr = curr->nextLineBox()) {
    223         if (lineIntersectsDirtyRect(renderer, curr, info, paintOffset)) {
    224             RootInlineBox* root = curr->root();
    225             curr->paint(info, paintOffset, root->lineTop(), root->lineBottom());
    226         }
    227     }
    228 
    229     if (info.phase == PaintPhaseOutline || info.phase == PaintPhaseSelfOutline || info.phase == PaintPhaseChildOutlines) {
    230         ListHashSet<RenderInline*>::iterator end = info.outlineObjects->end();
    231         for (ListHashSet<RenderInline*>::iterator it = info.outlineObjects->begin(); it != end; ++it) {
    232             RenderInline* flow = *it;
    233             flow->paintOutline(info, paintOffset);
    234         }
    235         info.outlineObjects->clear();
    236     }
    237 }
    238 
    239 bool RenderLineBoxList::hitTest(RenderBoxModelObject* renderer, const HitTestRequest& request, HitTestResult& result, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, HitTestAction hitTestAction) const
    240 {
    241     if (hitTestAction != HitTestForeground)
    242         return false;
    243 
    244     ASSERT(renderer->isRenderBlock() || (renderer->isRenderInline() && renderer->hasLayer())); // The only way an inline could hit test like this is if it has a layer.
    245 
    246     // If we have no lines then we have no work to do.
    247     if (!firstLineBox())
    248         return false;
    249 
    250     LayoutPoint point = locationInContainer.point();
    251     LayoutRect rect = firstLineBox()->isHorizontal() ?
    252         IntRect(point.x(), point.y() - locationInContainer.topPadding(), 1, locationInContainer.topPadding() + locationInContainer.bottomPadding() + 1) :
    253         IntRect(point.x() - locationInContainer.leftPadding(), point.y(), locationInContainer.rightPadding() + locationInContainer.leftPadding() + 1, 1);
    254 
    255     if (!anyLineIntersectsRect(renderer, rect, accumulatedOffset))
    256         return false;
    257 
    258     // See if our root lines contain the point.  If so, then we hit test
    259     // them further.  Note that boxes can easily overlap, so we can't make any assumptions
    260     // based off positions of our first line box or our last line box.
    261     for (InlineFlowBox* curr = lastLineBox(); curr; curr = curr->prevLineBox()) {
    262         RootInlineBox* root = curr->root();
    263         if (rangeIntersectsRect(renderer, curr->logicalTopVisualOverflow(root->lineTop()), curr->logicalBottomVisualOverflow(root->lineBottom()), rect, accumulatedOffset)) {
    264             bool inside = curr->nodeAtPoint(request, result, locationInContainer, accumulatedOffset, root->lineTop(), root->lineBottom());
    265             if (inside) {
    266                 renderer->updateHitTestResult(result, locationInContainer.point() - toLayoutSize(accumulatedOffset));
    267                 return true;
    268             }
    269         }
    270     }
    271 
    272     return false;
    273 }
    274 
    275 void RenderLineBoxList::dirtyLinesFromChangedChild(RenderObject* container, RenderObject* child)
    276 {
    277     if (!container->parent() || (container->isRenderBlock() && (container->selfNeedsLayout() || !container->isBlockFlow())))
    278         return;
    279 
    280     RenderInline* inlineContainer = container->isRenderInline() ? toRenderInline(container) : 0;
    281     InlineBox* firstBox = inlineContainer ? inlineContainer->firstLineBoxIncludingCulling() : firstLineBox();
    282 
    283     // If we have no first line box, then just bail early.
    284     if (!firstBox) {
    285         // For an empty inline, go ahead and propagate the check up to our parent, unless the parent
    286         // is already dirty.
    287         if (container->isInline() && !container->ancestorLineBoxDirty()) {
    288             container->parent()->dirtyLinesFromChangedChild(container);
    289             container->setAncestorLineBoxDirty(); // Mark the container to avoid dirtying the same lines again across multiple destroy() calls of the same subtree.
    290         }
    291         return;
    292     }
    293 
    294     // Try to figure out which line box we belong in.  First try to find a previous
    295     // line box by examining our siblings.  If we didn't find a line box, then use our
    296     // parent's first line box.
    297     RootInlineBox* box = 0;
    298     RenderObject* curr = 0;
    299     for (curr = child->previousSibling(); curr; curr = curr->previousSibling()) {
    300         if (curr->isFloatingOrOutOfFlowPositioned())
    301             continue;
    302 
    303         if (curr->isReplaced()) {
    304             InlineBox* wrapper = toRenderBox(curr)->inlineBoxWrapper();
    305             if (wrapper)
    306                 box = wrapper->root();
    307         } else if (curr->isText()) {
    308             InlineTextBox* textBox = toRenderText(curr)->lastTextBox();
    309             if (textBox)
    310                 box = textBox->root();
    311         } else if (curr->isRenderInline()) {
    312             InlineBox* lastSiblingBox = toRenderInline(curr)->lastLineBoxIncludingCulling();
    313             if (lastSiblingBox)
    314                 box = lastSiblingBox->root();
    315         }
    316 
    317         if (box)
    318             break;
    319     }
    320     if (!box) {
    321         if (inlineContainer && !inlineContainer->alwaysCreateLineBoxes()) {
    322             // https://bugs.webkit.org/show_bug.cgi?id=60778
    323             // We may have just removed a <br> with no line box that was our first child. In this case
    324             // we won't find a previous sibling, but firstBox can be pointing to a following sibling.
    325             // This isn't good enough, since we won't locate the root line box that encloses the removed
    326             // <br>. We have to just over-invalidate a bit and go up to our parent.
    327             if (!inlineContainer->ancestorLineBoxDirty()) {
    328                 inlineContainer->parent()->dirtyLinesFromChangedChild(inlineContainer);
    329                 inlineContainer->setAncestorLineBoxDirty(); // Mark the container to avoid dirtying the same lines again across multiple destroy() calls of the same subtree.
    330             }
    331             return;
    332         }
    333         box = firstBox->root();
    334     }
    335 
    336     // If we found a line box, then dirty it.
    337     if (box) {
    338         RootInlineBox* adjacentBox;
    339         box->markDirty();
    340 
    341         // dirty the adjacent lines that might be affected
    342         // NOTE: we dirty the previous line because RootInlineBox objects cache
    343         // the address of the first object on the next line after a BR, which we may be
    344         // invalidating here.  For more info, see how RenderBlock::layoutInlineChildren
    345         // calls setLineBreakInfo with the result of findNextLineBreak.  findNextLineBreak,
    346         // despite the name, actually returns the first RenderObject after the BR.
    347         // <rdar://problem/3849947> "Typing after pasting line does not appear until after window resize."
    348         adjacentBox = box->prevRootBox();
    349         if (adjacentBox)
    350             adjacentBox->markDirty();
    351         adjacentBox = box->nextRootBox();
    352         // If |child| has been inserted before the first element in the linebox, but after collapsed leading
    353         // space, the search for |child|'s linebox will go past the leading space to the previous linebox and select that
    354         // one as |box|. If we hit that situation here, dirty the |box| actually containing the child too.
    355         bool insertedAfterLeadingSpace = box->lineBreakObj() == child->previousSibling();
    356         if (adjacentBox && (adjacentBox->lineBreakObj() == child || child->isBR() || (curr && curr->isBR())
    357             || insertedAfterLeadingSpace || isIsolated(container->style()->unicodeBidi()))) {
    358             adjacentBox->markDirty();
    359         }
    360     }
    361 }
    362 
    363 #ifndef NDEBUG
    364 
    365 void RenderLineBoxList::checkConsistency() const
    366 {
    367 #ifdef CHECK_CONSISTENCY
    368     const InlineFlowBox* prev = 0;
    369     for (const InlineFlowBox* child = m_firstLineBox; child != 0; child = child->nextLineBox()) {
    370         ASSERT(child->prevLineBox() == prev);
    371         prev = child;
    372     }
    373     ASSERT(prev == m_lastLineBox);
    374 #endif
    375 }
    376 
    377 #endif
    378 
    379 }
    380