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/paint/InlinePainter.h"
     33 #include "core/rendering/HitTestResult.h"
     34 #include "core/rendering/InlineTextBox.h"
     35 #include "core/rendering/PaintInfo.h"
     36 #include "core/rendering/RenderInline.h"
     37 #include "core/rendering/RenderView.h"
     38 #include "core/rendering/RootInlineBox.h"
     39 
     40 namespace blink {
     41 
     42 #if ENABLE(ASSERT)
     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 = std::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) 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 
    183     return rangeIntersectsRect(renderer, firstLineTop, lastLineBottom, rect, offset);
    184 }
    185 
    186 bool RenderLineBoxList::lineIntersectsDirtyRect(RenderBoxModelObject* renderer, InlineFlowBox* box, const PaintInfo& paintInfo, const LayoutPoint& offset) const
    187 {
    188     RootInlineBox& root = box->root();
    189     LayoutUnit logicalTop = std::min<LayoutUnit>(box->logicalTopVisualOverflow(root.lineTop()), root.selectionTop());
    190     LayoutUnit logicalBottom = box->logicalBottomVisualOverflow(root.lineBottom());
    191 
    192     return rangeIntersectsRect(renderer, logicalTop, logicalBottom, paintInfo.rect, offset);
    193 }
    194 
    195 bool RenderLineBoxList::hitTest(RenderBoxModelObject* renderer, const HitTestRequest& request, HitTestResult& result, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, HitTestAction hitTestAction) const
    196 {
    197     if (hitTestAction != HitTestForeground)
    198         return false;
    199 
    200     ASSERT(renderer->isRenderBlock() || (renderer->isRenderInline() && renderer->hasLayer())); // The only way an inline could hit test like this is if it has a layer.
    201 
    202     // If we have no lines then we have no work to do.
    203     if (!firstLineBox())
    204         return false;
    205 
    206     LayoutPoint point = locationInContainer.point();
    207     LayoutRect rect = firstLineBox()->isHorizontal() ?
    208         IntRect(point.x(), point.y() - locationInContainer.topPadding(), 1, locationInContainer.topPadding() + locationInContainer.bottomPadding() + 1) :
    209         IntRect(point.x() - locationInContainer.leftPadding(), point.y(), locationInContainer.rightPadding() + locationInContainer.leftPadding() + 1, 1);
    210 
    211     if (!anyLineIntersectsRect(renderer, rect, accumulatedOffset))
    212         return false;
    213 
    214     // See if our root lines contain the point.  If so, then we hit test
    215     // them further.  Note that boxes can easily overlap, so we can't make any assumptions
    216     // based off positions of our first line box or our last line box.
    217     for (InlineFlowBox* curr = lastLineBox(); curr; curr = curr->prevLineBox()) {
    218         RootInlineBox& root = curr->root();
    219         if (rangeIntersectsRect(renderer, curr->logicalTopVisualOverflow(root.lineTop()), curr->logicalBottomVisualOverflow(root.lineBottom()), rect, accumulatedOffset)) {
    220             bool inside = curr->nodeAtPoint(request, result, locationInContainer, accumulatedOffset, root.lineTop(), root.lineBottom());
    221             if (inside) {
    222                 renderer->updateHitTestResult(result, locationInContainer.point() - toLayoutSize(accumulatedOffset));
    223                 return true;
    224             }
    225         }
    226     }
    227 
    228     return false;
    229 }
    230 
    231 void RenderLineBoxList::dirtyLinesFromChangedChild(RenderObject* container, RenderObject* child)
    232 {
    233     if (!container->parent() || (container->isRenderBlock() && (container->selfNeedsLayout() || !container->isRenderBlockFlow())))
    234         return;
    235 
    236     RenderInline* inlineContainer = container->isRenderInline() ? toRenderInline(container) : 0;
    237     InlineBox* firstBox = inlineContainer ? inlineContainer->firstLineBoxIncludingCulling() : firstLineBox();
    238 
    239     // If we have no first line box, then just bail early.
    240     if (!firstBox) {
    241         // For an empty inline, go ahead and propagate the check up to our parent, unless the parent
    242         // is already dirty.
    243         if (container->isInline() && !container->ancestorLineBoxDirty()) {
    244             container->parent()->dirtyLinesFromChangedChild(container);
    245             container->setAncestorLineBoxDirty(); // Mark the container to avoid dirtying the same lines again across multiple destroy() calls of the same subtree.
    246         }
    247         return;
    248     }
    249 
    250     // Try to figure out which line box we belong in.  First try to find a previous
    251     // line box by examining our siblings.  If we didn't find a line box, then use our
    252     // parent's first line box.
    253     RootInlineBox* box = 0;
    254     RenderObject* curr = 0;
    255     ListHashSet<RenderObject*, 16> potentialLineBreakObjects;
    256     potentialLineBreakObjects.add(child);
    257     for (curr = child->previousSibling(); curr; curr = curr->previousSibling()) {
    258         potentialLineBreakObjects.add(curr);
    259 
    260         if (curr->isFloatingOrOutOfFlowPositioned())
    261             continue;
    262 
    263         if (curr->isReplaced()) {
    264             InlineBox* wrapper = toRenderBox(curr)->inlineBoxWrapper();
    265             if (wrapper)
    266                 box = &wrapper->root();
    267         } else if (curr->isText()) {
    268             InlineTextBox* textBox = toRenderText(curr)->lastTextBox();
    269             if (textBox)
    270                 box = &textBox->root();
    271         } else if (curr->isRenderInline()) {
    272             InlineBox* lastSiblingBox = toRenderInline(curr)->lastLineBoxIncludingCulling();
    273             if (lastSiblingBox)
    274                 box = &lastSiblingBox->root();
    275         }
    276 
    277         if (box)
    278             break;
    279     }
    280     if (!box) {
    281         if (inlineContainer && !inlineContainer->alwaysCreateLineBoxes()) {
    282             // https://bugs.webkit.org/show_bug.cgi?id=60778
    283             // We may have just removed a <br> with no line box that was our first child. In this case
    284             // we won't find a previous sibling, but firstBox can be pointing to a following sibling.
    285             // This isn't good enough, since we won't locate the root line box that encloses the removed
    286             // <br>. We have to just over-invalidate a bit and go up to our parent.
    287             if (!inlineContainer->ancestorLineBoxDirty()) {
    288                 inlineContainer->parent()->dirtyLinesFromChangedChild(inlineContainer);
    289                 inlineContainer->setAncestorLineBoxDirty(); // Mark the container to avoid dirtying the same lines again across multiple destroy() calls of the same subtree.
    290             }
    291             return;
    292         }
    293         box = &firstBox->root();
    294     }
    295 
    296     // If we found a line box, then dirty it.
    297     if (box) {
    298         RootInlineBox* adjacentBox;
    299         box->markDirty();
    300 
    301         // dirty the adjacent lines that might be affected
    302         // NOTE: we dirty the previous line because RootInlineBox objects cache
    303         // the address of the first object on the next line after a BR, which we may be
    304         // invalidating here.  For more info, see how RenderBlock::layoutInlineChildren
    305         // calls setLineBreakInfo with the result of findNextLineBreak.  findNextLineBreak,
    306         // despite the name, actually returns the first RenderObject after the BR.
    307         // <rdar://problem/3849947> "Typing after pasting line does not appear until after window resize."
    308         adjacentBox = box->prevRootBox();
    309         if (adjacentBox)
    310             adjacentBox->markDirty();
    311         adjacentBox = box->nextRootBox();
    312         // If |child| or any of its immediately previous siblings with culled lineboxes is the object after a line-break in |box| or the linebox after it
    313         // then that means |child| actually sits on the linebox after |box| (or is its line-break object) and so we need to dirty it as well.
    314         if (adjacentBox && (potentialLineBreakObjects.contains(box->lineBreakObj()) || potentialLineBreakObjects.contains(adjacentBox->lineBreakObj()) || child->isBR() || isIsolated(container->style()->unicodeBidi())))
    315             adjacentBox->markDirty();
    316     }
    317 }
    318 
    319 #if ENABLE(ASSERT)
    320 
    321 void RenderLineBoxList::checkConsistency() const
    322 {
    323 #ifdef CHECK_CONSISTENCY
    324     const InlineFlowBox* prev = 0;
    325     for (const InlineFlowBox* child = m_firstLineBox; child != 0; child = child->nextLineBox()) {
    326         ASSERT(child->prevLineBox() == prev);
    327         prev = child;
    328     }
    329     ASSERT(prev == m_lastLineBox);
    330 #endif
    331 }
    332 
    333 #endif
    334 
    335 }
    336