| 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 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 "RenderLineBoxList.h" |
| 31 | |
| 32 | #include "HitTestResult.h" |
| 33 | #include "InlineElementBox.h" |
| 34 | #include "InlineTextBox.h" |
| 35 | #include "PaintInfo.h" |
| 36 | #include "RenderBlockFlow.h" |
| 37 | #include "RenderInline.h" |
| 38 | #include "RenderLineBreak.h" |
| 39 | #include "RenderView.h" |
| 40 | #include "RootInlineBox.h" |
| 41 | |
| 42 | namespace WebCore { |
| 43 | |
| 44 | #ifndef NDEBUG |
| 45 | RenderLineBoxList::~RenderLineBoxList() |
| 46 | { |
| 47 | ASSERT(!m_firstLineBox); |
| 48 | ASSERT(!m_lastLineBox); |
| 49 | } |
| 50 | #endif |
| 51 | |
| 52 | void RenderLineBoxList::appendLineBox(std::unique_ptr<InlineFlowBox> box) |
| 53 | { |
| 54 | checkConsistency(); |
| 55 | |
| 56 | InlineFlowBox* boxPtr = box.release(); |
| 57 | |
| 58 | if (!m_firstLineBox) { |
| 59 | m_firstLineBox = boxPtr; |
| 60 | m_lastLineBox = boxPtr; |
| 61 | } else { |
| 62 | m_lastLineBox->setNextLineBox(boxPtr); |
| 63 | boxPtr->setPreviousLineBox(m_lastLineBox); |
| 64 | m_lastLineBox = boxPtr; |
| 65 | } |
| 66 | |
| 67 | checkConsistency(); |
| 68 | } |
| 69 | |
| 70 | void RenderLineBoxList::deleteLineBoxTree() |
| 71 | { |
| 72 | InlineFlowBox* line = m_firstLineBox; |
| 73 | InlineFlowBox* nextLine; |
| 74 | while (line) { |
| 75 | nextLine = line->nextLineBox(); |
| 76 | line->deleteLine(); |
| 77 | line = nextLine; |
| 78 | } |
| 79 | m_firstLineBox = m_lastLineBox = nullptr; |
| 80 | } |
| 81 | |
| 82 | void RenderLineBoxList::(InlineFlowBox* box) |
| 83 | { |
| 84 | checkConsistency(); |
| 85 | |
| 86 | m_lastLineBox = box->prevLineBox(); |
| 87 | if (box == m_firstLineBox) |
| 88 | m_firstLineBox = 0; |
| 89 | if (box->prevLineBox()) |
| 90 | box->prevLineBox()->setNextLineBox(nullptr); |
| 91 | box->setPreviousLineBox(nullptr); |
| 92 | for (InlineFlowBox* curr = box; curr; curr = curr->nextLineBox()) |
| 93 | curr->setExtracted(); |
| 94 | |
| 95 | checkConsistency(); |
| 96 | } |
| 97 | |
| 98 | void RenderLineBoxList::attachLineBox(InlineFlowBox* box) |
| 99 | { |
| 100 | checkConsistency(); |
| 101 | |
| 102 | if (m_lastLineBox) { |
| 103 | m_lastLineBox->setNextLineBox(box); |
| 104 | box->setPreviousLineBox(m_lastLineBox); |
| 105 | } else |
| 106 | m_firstLineBox = box; |
| 107 | InlineFlowBox* last = box; |
| 108 | for (InlineFlowBox* curr = box; curr; curr = curr->nextLineBox()) { |
| 109 | curr->setExtracted(false); |
| 110 | last = curr; |
| 111 | } |
| 112 | m_lastLineBox = last; |
| 113 | |
| 114 | checkConsistency(); |
| 115 | } |
| 116 | |
| 117 | void RenderLineBoxList::removeLineBox(InlineFlowBox* box) |
| 118 | { |
| 119 | checkConsistency(); |
| 120 | |
| 121 | if (box == m_firstLineBox) |
| 122 | m_firstLineBox = box->nextLineBox(); |
| 123 | if (box == m_lastLineBox) |
| 124 | m_lastLineBox = box->prevLineBox(); |
| 125 | if (box->nextLineBox()) |
| 126 | box->nextLineBox()->setPreviousLineBox(box->prevLineBox()); |
| 127 | if (box->prevLineBox()) |
| 128 | box->prevLineBox()->setNextLineBox(box->nextLineBox()); |
| 129 | |
| 130 | checkConsistency(); |
| 131 | } |
| 132 | |
| 133 | void RenderLineBoxList::deleteLineBoxes() |
| 134 | { |
| 135 | if (m_firstLineBox) { |
| 136 | InlineFlowBox* next; |
| 137 | for (InlineFlowBox* curr = m_firstLineBox; curr; curr = next) { |
| 138 | next = curr->nextLineBox(); |
| 139 | delete curr; |
| 140 | } |
| 141 | m_firstLineBox = nullptr; |
| 142 | m_lastLineBox = nullptr; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | void RenderLineBoxList::dirtyLineBoxes() |
| 147 | { |
| 148 | for (InlineFlowBox* curr = firstLineBox(); curr; curr = curr->nextLineBox()) |
| 149 | curr->dirtyLineBoxes(); |
| 150 | } |
| 151 | |
| 152 | // FIXME: This should take a RenderBoxModelObject&. |
| 153 | bool RenderLineBoxList::rangeIntersectsRect(RenderBoxModelObject* renderer, LayoutUnit logicalTop, LayoutUnit logicalBottom, const LayoutRect& rect, const LayoutPoint& offset) const |
| 154 | { |
| 155 | LayoutUnit physicalStart = logicalTop; |
| 156 | LayoutUnit physicalEnd = logicalBottom; |
| 157 | if (renderer->view().frameView().hasFlippedBlockRenderers()) { |
| 158 | RenderBox* block; |
| 159 | if (is<RenderBox>(*renderer)) |
| 160 | block = downcast<RenderBox>(renderer); |
| 161 | else |
| 162 | block = renderer->containingBlock(); |
| 163 | physicalStart = block->flipForWritingMode(logicalTop); |
| 164 | physicalEnd = block->flipForWritingMode(logicalBottom); |
| 165 | } |
| 166 | |
| 167 | LayoutUnit physicalExtent = absoluteValue(physicalEnd - physicalStart); |
| 168 | physicalStart = std::min(physicalStart, physicalEnd); |
| 169 | |
| 170 | if (renderer->style().isHorizontalWritingMode()) { |
| 171 | physicalStart += offset.y(); |
| 172 | if (physicalStart >= rect.maxY() || physicalStart + physicalExtent <= rect.y()) |
| 173 | return false; |
| 174 | } else { |
| 175 | physicalStart += offset.x(); |
| 176 | if (physicalStart >= rect.maxX() || physicalStart + physicalExtent <= rect.x()) |
| 177 | return false; |
| 178 | } |
| 179 | |
| 180 | return true; |
| 181 | } |
| 182 | |
| 183 | bool RenderLineBoxList::anyLineIntersectsRect(RenderBoxModelObject* renderer, const LayoutRect& rect, const LayoutPoint& offset, bool usePrintRect) const |
| 184 | { |
| 185 | // We can check the first box and last box and avoid painting/hit testing if we don't |
| 186 | // intersect. This is a quick short-circuit that we can take to avoid walking any lines. |
| 187 | // FIXME: This check is flawed in the following extremely obscure way: |
| 188 | // if some line in the middle has a huge overflow, it might actually extend below the last line. |
| 189 | const RootInlineBox& firstRootBox = firstLineBox()->root(); |
| 190 | const RootInlineBox& lastRootBox = lastLineBox()->root(); |
| 191 | LayoutUnit firstLineTop = firstLineBox()->logicalTopVisualOverflow(firstRootBox.lineTop()); |
| 192 | if (usePrintRect && !firstLineBox()->parent()) |
| 193 | firstLineTop = std::min(firstLineTop, firstRootBox.lineTop()); |
| 194 | LayoutUnit lastLineBottom = lastLineBox()->logicalBottomVisualOverflow(lastRootBox.lineBottom()); |
| 195 | if (usePrintRect && !lastLineBox()->parent()) |
| 196 | lastLineBottom = std::max(lastLineBottom, lastRootBox.lineBottom()); |
| 197 | return rangeIntersectsRect(renderer, firstLineTop, lastLineBottom, rect, offset); |
| 198 | } |
| 199 | |
| 200 | bool RenderLineBoxList::lineIntersectsDirtyRect(RenderBoxModelObject* renderer, InlineFlowBox* box, const PaintInfo& paintInfo, const LayoutPoint& offset) const |
| 201 | { |
| 202 | const RootInlineBox& rootBox = box->root(); |
| 203 | LayoutUnit logicalTop = std::min(box->logicalTopVisualOverflow(rootBox.lineTop()), rootBox.selectionTop()); |
| 204 | LayoutUnit logicalBottom = box->logicalBottomVisualOverflow(rootBox.lineBottom()); |
| 205 | return rangeIntersectsRect(renderer, logicalTop, logicalBottom, paintInfo.rect, offset); |
| 206 | } |
| 207 | |
| 208 | void RenderLineBoxList::paint(RenderBoxModelObject* renderer, PaintInfo& paintInfo, const LayoutPoint& paintOffset) const |
| 209 | { |
| 210 | ASSERT(renderer->isRenderBlock() || (renderer->isRenderInline() && renderer->hasLayer())); // The only way an inline could paint like this is if it has a layer. |
| 211 | |
| 212 | // If we have no lines then we have no work to do. |
| 213 | if (!firstLineBox()) |
| 214 | return; |
| 215 | |
| 216 | // FIXME: Paint-time pagination is obsolete and is now only used by embedded WebViews inside AppKit |
| 217 | // NSViews. Do not add any more code for this. |
| 218 | RenderView& v = renderer->view(); |
| 219 | bool usePrintRect = !v.printRect().isEmpty(); |
| 220 | if (!anyLineIntersectsRect(renderer, paintInfo.rect, paintOffset, usePrintRect)) |
| 221 | return; |
| 222 | |
| 223 | PaintInfo info(paintInfo); |
| 224 | ListHashSet<RenderInline*> outlineObjects; |
| 225 | info.outlineObjects = &outlineObjects; |
| 226 | |
| 227 | // See if our root lines intersect with the dirty rect. If so, then we paint |
| 228 | // them. Note that boxes can easily overlap, so we can't make any assumptions |
| 229 | // based off positions of our first line box or our last line box. |
| 230 | for (InlineFlowBox* curr = firstLineBox(); curr; curr = curr->nextLineBox()) { |
| 231 | if (usePrintRect) { |
| 232 | // FIXME: This is the deprecated pagination model that is still needed |
| 233 | // for embedded views inside AppKit. AppKit is incapable of paginating vertical |
| 234 | // text pages, so we don't have to deal with vertical lines at all here. |
| 235 | const RootInlineBox& rootBox = curr->root(); |
| 236 | LayoutUnit = curr->logicalTopVisualOverflow(rootBox.lineTop()); |
| 237 | LayoutUnit = curr->logicalLeftVisualOverflow(); |
| 238 | if (!curr->parent()) { |
| 239 | // We're a root box. Use lineTop and lineBottom as well here. |
| 240 | topForPaginationCheck = std::min(topForPaginationCheck, rootBox.lineTop()); |
| 241 | bottomForPaginationCheck = std::max(bottomForPaginationCheck, rootBox.lineBottom()); |
| 242 | } |
| 243 | if (bottomForPaginationCheck - topForPaginationCheck <= v.printRect().height()) { |
| 244 | if (paintOffset.y() + bottomForPaginationCheck > v.printRect().maxY()) { |
| 245 | if (RootInlineBox* nextRootBox = rootBox.nextRootBox()) |
| 246 | bottomForPaginationCheck = std::min(bottomForPaginationCheck, std::min<LayoutUnit>(nextRootBox->logicalTopVisualOverflow(), nextRootBox->lineTop())); |
| 247 | } |
| 248 | if (paintOffset.y() + bottomForPaginationCheck > v.printRect().maxY()) { |
| 249 | if (paintOffset.y() + topForPaginationCheck < v.truncatedAt()) |
| 250 | v.setBestTruncatedAt(paintOffset.y() + topForPaginationCheck, renderer); |
| 251 | // If we were able to truncate, don't paint. |
| 252 | if (paintOffset.y() + topForPaginationCheck >= v.truncatedAt()) |
| 253 | break; |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | if (lineIntersectsDirtyRect(renderer, curr, info, paintOffset)) { |
| 259 | const RootInlineBox& rootBox = curr->root(); |
| 260 | curr->paint(info, paintOffset, rootBox.lineTop(), rootBox.lineBottom()); |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | if (info.phase == PaintPhase::Outline || info.phase == PaintPhase::SelfOutline || info.phase == PaintPhase::ChildOutlines) { |
| 265 | ListHashSet<RenderInline*>::iterator end = info.outlineObjects->end(); |
| 266 | for (ListHashSet<RenderInline*>::iterator it = info.outlineObjects->begin(); it != end; ++it) { |
| 267 | RenderInline* flow = *it; |
| 268 | flow->paintOutline(info, paintOffset); |
| 269 | } |
| 270 | info.outlineObjects->clear(); |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | bool RenderLineBoxList::hitTest(RenderBoxModelObject* renderer, const HitTestRequest& request, HitTestResult& result, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, HitTestAction hitTestAction) const |
| 275 | { |
| 276 | ASSERT(renderer->isRenderBlock() || (renderer->isRenderInline() && renderer->hasLayer())); // The only way an inline could hit test like this is if it has a layer. |
| 277 | |
| 278 | // If we have no lines then we have no work to do. |
| 279 | if (!firstLineBox()) |
| 280 | return false; |
| 281 | |
| 282 | LayoutPoint point = locationInContainer.point(); |
| 283 | LayoutRect rect = firstLineBox()->isHorizontal() ? |
| 284 | IntRect(point.x(), point.y() - locationInContainer.topPadding(), 1, locationInContainer.topPadding() + locationInContainer.bottomPadding() + 1) : |
| 285 | IntRect(point.x() - locationInContainer.leftPadding(), point.y(), locationInContainer.rightPadding() + locationInContainer.leftPadding() + 1, 1); |
| 286 | |
| 287 | if (!anyLineIntersectsRect(renderer, rect, accumulatedOffset)) |
| 288 | return false; |
| 289 | |
| 290 | // See if our root lines contain the point. If so, then we hit test |
| 291 | // them further. Note that boxes can easily overlap, so we can't make any assumptions |
| 292 | // based off positions of our first line box or our last line box. |
| 293 | for (InlineFlowBox* curr = lastLineBox(); curr; curr = curr->prevLineBox()) { |
| 294 | const RootInlineBox& rootBox = curr->root(); |
| 295 | if (rangeIntersectsRect(renderer, curr->logicalTopVisualOverflow(rootBox.lineTop()), curr->logicalBottomVisualOverflow(rootBox.lineBottom()), rect, accumulatedOffset)) { |
| 296 | bool inside = curr->nodeAtPoint(request, result, locationInContainer, accumulatedOffset, rootBox.lineTop(), rootBox.lineBottom(), hitTestAction); |
| 297 | if (inside) { |
| 298 | renderer->updateHitTestResult(result, locationInContainer.point() - toLayoutSize(accumulatedOffset)); |
| 299 | return true; |
| 300 | } |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | return false; |
| 305 | } |
| 306 | |
| 307 | void RenderLineBoxList::dirtyLinesFromChangedChild(RenderBoxModelObject& container, RenderObject& child) |
| 308 | { |
| 309 | ASSERT(is<RenderInline>(container) || is<RenderBlockFlow>(container)); |
| 310 | if (!container.parent() || (is<RenderBlockFlow>(container) && container.selfNeedsLayout())) |
| 311 | return; |
| 312 | |
| 313 | RenderInline* inlineContainer = is<RenderInline>(container) ? &downcast<RenderInline>(container) : nullptr; |
| 314 | InlineBox* firstBox = inlineContainer ? inlineContainer->firstLineBoxIncludingCulling() : firstLineBox(); |
| 315 | |
| 316 | // If we have no first line box, then just bail early. |
| 317 | if (!firstBox) { |
| 318 | // For an empty inline, propagate the check up to our parent, unless the parent is already dirty. |
| 319 | if (container.isInline() && !container.ancestorLineBoxDirty()) { |
| 320 | container.parent()->dirtyLinesFromChangedChild(container); |
| 321 | container.setAncestorLineBoxDirty(); // Mark the container to avoid dirtying the same lines again across multiple destroy() calls of the same subtree. |
| 322 | } |
| 323 | return; |
| 324 | } |
| 325 | |
| 326 | // Try to figure out which line box we belong in. First try to find a previous |
| 327 | // line box by examining our siblings. If we didn't find a line box, then use our |
| 328 | // parent's first line box. |
| 329 | RootInlineBox* box = nullptr; |
| 330 | RenderObject* current; |
| 331 | for (current = child.previousSibling(); current; current = current->previousSibling()) { |
| 332 | if (current->isFloatingOrOutOfFlowPositioned()) |
| 333 | continue; |
| 334 | |
| 335 | if (current->isReplaced()) { |
| 336 | if (auto wrapper = downcast<RenderBox>(*current).inlineBoxWrapper()) |
| 337 | box = &wrapper->root(); |
| 338 | } if (is<RenderLineBreak>(*current)) { |
| 339 | if (auto wrapper = downcast<RenderLineBreak>(*current).inlineBoxWrapper()) |
| 340 | box = &wrapper->root(); |
| 341 | } else if (is<RenderText>(*current)) { |
| 342 | if (InlineTextBox* textBox = downcast<RenderText>(*current).lastTextBox()) |
| 343 | box = &textBox->root(); |
| 344 | } else if (is<RenderInline>(*current)) { |
| 345 | InlineBox* lastSiblingBox = downcast<RenderInline>(*current).lastLineBoxIncludingCulling(); |
| 346 | if (lastSiblingBox) |
| 347 | box = &lastSiblingBox->root(); |
| 348 | } |
| 349 | |
| 350 | if (box) |
| 351 | break; |
| 352 | } |
| 353 | if (!box) { |
| 354 | if (inlineContainer && !inlineContainer->alwaysCreateLineBoxes()) { |
| 355 | // https://bugs.webkit.org/show_bug.cgi?id=60778 |
| 356 | // We may have just removed a <br> with no line box that was our first child. In this case |
| 357 | // we won't find a previous sibling, but firstBox can be pointing to a following sibling. |
| 358 | // This isn't good enough, since we won't locate the root line box that encloses the removed |
| 359 | // <br>. We have to just over-invalidate a bit and go up to our parent. |
| 360 | if (!inlineContainer->ancestorLineBoxDirty()) { |
| 361 | inlineContainer->parent()->dirtyLinesFromChangedChild(*inlineContainer); |
| 362 | inlineContainer->setAncestorLineBoxDirty(); // Mark the container to avoid dirtying the same lines again across multiple destroy() calls of the same subtree. |
| 363 | } |
| 364 | return; |
| 365 | } |
| 366 | box = &firstBox->root(); |
| 367 | } |
| 368 | |
| 369 | // If we found a line box, then dirty it. |
| 370 | if (box) { |
| 371 | box->markDirty(); |
| 372 | |
| 373 | // Dirty the adjacent lines that might be affected. |
| 374 | // NOTE: we dirty the previous line because RootInlineBox objects cache |
| 375 | // the address of the first object on the next line after a BR, which we may be |
| 376 | // invalidating here. For more info, see how RenderBlock::layoutInlineChildren |
| 377 | // calls setLineBreakInfo with the result of findNextLineBreak. findNextLineBreak, |
| 378 | // despite the name, actually returns the first RenderObject after the BR. |
| 379 | // <rdar://problem/3849947> "Typing after pasting line does not appear until after window resize." |
| 380 | if (RootInlineBox* prevBox = box->prevRootBox()) |
| 381 | prevBox->markDirty(); |
| 382 | |
| 383 | // FIXME: We shouldn't need to always dirty the next line. This is only strictly |
| 384 | // necessary some of the time, in situations involving BRs. |
| 385 | if (RootInlineBox* nextBox = box->nextRootBox()) { |
| 386 | nextBox->markDirty(); |
| 387 | // Dedicated linebox for floats may be added as the last rootbox. If this occurs with BRs inside inlines that propagte their lineboxes to |
| 388 | // the parent flow, we need to invalidate it explicitly. |
| 389 | // FIXME: We should be able to figure out the actual "changed child" even when we are calling through empty inlines recursively. |
| 390 | if (is<RenderInline>(child) && !downcast<RenderInline>(child).firstLineBoxIncludingCulling()) { |
| 391 | auto* lastRootBox = nextBox->blockFlow().lastRootBox(); |
| 392 | if (lastRootBox->isTrailingFloatsRootInlineBox() && !lastRootBox->isDirty()) |
| 393 | lastRootBox->markDirty(); |
| 394 | } |
| 395 | } |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | #ifndef NDEBUG |
| 400 | |
| 401 | void RenderLineBoxList::checkConsistency() const |
| 402 | { |
| 403 | #ifdef CHECK_CONSISTENCY |
| 404 | const InlineFlowBox* prev = nullptr; |
| 405 | for (const InlineFlowBox* child = m_firstLineBox; child != nullptr; child = child->nextLineBox()) { |
| 406 | ASSERT(child->prevLineBox() == prev); |
| 407 | prev = child; |
| 408 | } |
| 409 | ASSERT(prev == m_lastLineBox); |
| 410 | #endif |
| 411 | } |
| 412 | |
| 413 | #endif |
| 414 | |
| 415 | } |
| 416 | |