1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181
|
/*
* Copyright (C) 2019 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "LayoutIntegrationLineLayout.h"
#include "BlockFormattingState.h"
#include "BlockLayoutState.h"
#include "ElementInlines.h"
#include "EventRegion.h"
#include "FormattingContextBoxIterator.h"
#include "HitTestLocation.h"
#include "HitTestRequest.h"
#include "HitTestResult.h"
#include "InlineContentCache.h"
#include "InlineDamage.h"
#include "InlineFormattingContext.h"
#include "InlineInvalidation.h"
#include "InlineItemsBuilder.h"
#include "LayoutBoxGeometry.h"
#include "LayoutIntegrationCoverage.h"
#include "LayoutIntegrationInlineContentBuilder.h"
#include "LayoutIntegrationInlineContentPainter.h"
#include "LayoutIntegrationPagination.h"
#include "LayoutTreeBuilder.h"
#include "PaintInfo.h"
#include "PlacedFloats.h"
#include "RenderBlockFlow.h"
#include "RenderBoxInlines.h"
#include "RenderChildIterator.h"
#include "RenderDescendantIterator.h"
#include "RenderElementInlines.h"
#include "RenderFrameSet.h"
#include "RenderInline.h"
#include "RenderLayer.h"
#include "RenderLayoutState.h"
#include "RenderView.h"
#include "Settings.h"
#include "ShapeOutsideInfo.h"
#include "TextBreakingPositionCache.h"
#include <wtf/Assertions.h>
#include <wtf/Range.h>
namespace WebCore {
namespace LayoutIntegration {
DEFINE_ALLOCATOR_WITH_HEAP_IDENTIFIER(LayoutIntegration_LineLayout);
static inline std::pair<LayoutRect, LayoutRect> toMarginAndBorderBoxVisualRect(const Layout::BoxGeometry& logicalGeometry, LayoutUnit containerLogicalWidth, WritingMode writingMode, bool isLeftToRightDirection)
{
auto isFlippedBlocksWritingMode = WebCore::isFlippedWritingMode(writingMode);
auto isHorizontalWritingMode = WebCore::isHorizontalWritingMode(writingMode);
auto borderBoxLogicalRect = Layout::BoxGeometry::borderBoxRect(logicalGeometry);
auto horizontalMargin = Layout::BoxGeometry::HorizontalEdges { logicalGeometry.marginStart(), logicalGeometry.marginEnd() };
auto verticalMargin = Layout::BoxGeometry::VerticalEdges { logicalGeometry.marginBefore(), logicalGeometry.marginAfter() };
auto flipMarginsIfApplicable = [&] {
if (isHorizontalWritingMode && isLeftToRightDirection && !isFlippedBlocksWritingMode)
return;
if (!isHorizontalWritingMode) {
auto logicalHorizontalMargin = horizontalMargin;
horizontalMargin = !isFlippedBlocksWritingMode ? Layout::BoxGeometry::HorizontalEdges { verticalMargin.after, verticalMargin.before } : Layout::BoxGeometry::HorizontalEdges { verticalMargin.before, verticalMargin.after };
verticalMargin = { logicalHorizontalMargin.start, logicalHorizontalMargin.end };
}
if (!isLeftToRightDirection) {
if (isHorizontalWritingMode)
horizontalMargin = { horizontalMargin.end, horizontalMargin.start };
else
verticalMargin = { verticalMargin.after, verticalMargin.before };
}
};
flipMarginsIfApplicable();
auto borderBoxVisualTopLeft = LayoutPoint { };
auto borderBoxLeft = isLeftToRightDirection ? borderBoxLogicalRect.left() : containerLogicalWidth - (borderBoxLogicalRect.left() + borderBoxLogicalRect.width());
if (isHorizontalWritingMode)
borderBoxVisualTopLeft = { borderBoxLeft, borderBoxLogicalRect.top() };
else {
auto marginBoxVisualLeft = borderBoxLogicalRect.top() - logicalGeometry.marginBefore();
auto marginBoxVisualTop = borderBoxLeft - logicalGeometry.marginStart();
if (isLeftToRightDirection)
borderBoxVisualTopLeft = { marginBoxVisualLeft + horizontalMargin.start, marginBoxVisualTop + verticalMargin.before };
else
borderBoxVisualTopLeft = { marginBoxVisualLeft + horizontalMargin.start, marginBoxVisualTop + verticalMargin.after };
}
auto borderBoxVisualRect = LayoutRect { borderBoxVisualTopLeft, isHorizontalWritingMode ? borderBoxLogicalRect.size() : borderBoxLogicalRect.size().transposedSize() };
auto marginBoxVisualRect = borderBoxVisualRect;
marginBoxVisualRect.move(-horizontalMargin.start, -verticalMargin.before);
marginBoxVisualRect.expand(horizontalMargin.start + horizontalMargin.end, verticalMargin.before + verticalMargin.after);
return { marginBoxVisualRect, borderBoxVisualRect };
}
static const InlineDisplay::Line& lastLineWithInlineContent(const InlineDisplay::Lines& lines)
{
// Out-of-flow/float content only don't produce lines with inline content. They should not be taken into
// account when computing content box height/baselines.
for (auto& line : makeReversedRange(lines)) {
ASSERT(line.boxCount());
if (line.boxCount() > 1)
return line;
}
return lines.first();
}
LineLayout::LineLayout(RenderBlockFlow& flow)
: m_boxTree(flow)
, m_layoutState(flow.view().layoutState())
, m_blockFormattingState(layoutState().ensureBlockFormattingState(rootLayoutBox()))
, m_inlineContentCache(layoutState().inlineContentCache(rootLayoutBox()))
, m_boxGeometryUpdater(m_boxTree, flow.view().layoutState())
{
}
LineLayout::~LineLayout()
{
if (!isDamaged() && !flow().document().renderTreeBeingDestroyed())
Layout::InlineItemsBuilder::populateBreakingPositionCache(m_inlineContentCache.inlineItems().content(), flow().document());
clearInlineContent();
layoutState().destroyInlineContentCache(rootLayoutBox());
layoutState().destroyBlockFormattingState(rootLayoutBox());
}
static inline bool isContentRenderer(const RenderObject& renderer)
{
// FIXME: These fake renderers have their parent set but are not actually in the tree.
return !renderer.isRenderReplica() && !renderer.isRenderScrollbarPart();
}
RenderBlockFlow* LineLayout::blockContainer(const RenderObject& renderer)
{
if (!isContentRenderer(renderer))
return nullptr;
for (auto* parent = renderer.parent(); parent; parent = parent->parent()) {
if (!parent->childrenInline())
return nullptr;
if (auto* renderBlockFlow = dynamicDowncast<RenderBlockFlow>(*parent))
return renderBlockFlow;
}
return nullptr;
}
bool LineLayout::contains(const RenderElement& renderer) const
{
if (!m_boxTree.contains(renderer))
return false;
return layoutState().hasBoxGeometry(*renderer.layoutBox());
}
LineLayout* LineLayout::containing(RenderObject& renderer)
{
if (!isContentRenderer(renderer))
return nullptr;
if (!renderer.isInline()) {
// IFC may contain block level boxes (floats and out-of-flow boxes).
if (renderer.isRenderSVGBlock()) {
// SVG content inside svg root shows up as block (see RenderSVGBlock). We only support inline root svg as "atomic content".
return nullptr;
}
if (renderer.isRenderFrameSet()) {
// Since RenderFrameSet is not a RenderBlock, finding container for nested framesets can't use containingBlock ancestor walk.
if (auto* parent = dynamicDowncast<RenderBlockFlow>(renderer.parent()))
return parent->modernLineLayout();
return nullptr;
}
auto adjustedContainingBlock = [&] {
RenderElement* containingBlock = nullptr;
// Only out of flow and floating block level boxes may participate in IFC.
if (renderer.isOutOfFlowPositioned()) {
// Here we are looking for the containing block as if the out-of-flow box was inflow (for static position purpose).
containingBlock = renderer.parent();
if (is<RenderInline>(containingBlock))
containingBlock = containingBlock->containingBlock();
} else if (renderer.isFloating()) {
// Note that containigBlock() on boxes in top layer (i.e. dialog) may return incorrect result during style change even with not-yet-updated style.
containingBlock = RenderObject::containingBlockForPositionType(renderer.style().position(), renderer);
}
return dynamicDowncast<RenderBlockFlow>(containingBlock);
};
if (auto* blockContainer = adjustedContainingBlock())
return blockContainer->modernLineLayout();
return nullptr;
}
if (auto* container = blockContainer(renderer))
return container->modernLineLayout();
return nullptr;
}
const LineLayout* LineLayout::containing(const RenderObject& renderer)
{
return containing(const_cast<RenderObject&>(renderer));
}
bool LineLayout::canUseFor(const RenderBlockFlow& flow)
{
return canUseForLineLayout(flow);
}
bool LineLayout::canUseForPreferredWidthComputation(const RenderBlockFlow& flow)
{
return LayoutIntegration::canUseForPreferredWidthComputation(flow);
}
bool LineLayout::shouldInvalidateLineLayoutPathAfterContentChange(const RenderBlockFlow& parent, const RenderObject& rendererWithNewContent, const LineLayout& lineLayout)
{
return shouldInvalidateLineLayoutPathAfterChangeFor(parent, rendererWithNewContent, lineLayout, TypeOfChangeForInvalidation::NodeMutation);
}
bool LineLayout::shouldInvalidateLineLayoutPathAfterTreeMutation(const RenderBlockFlow& parent, const RenderObject& renderer, const LineLayout& lineLayout, bool isRemoval)
{
return shouldInvalidateLineLayoutPathAfterChangeFor(parent, renderer, lineLayout, isRemoval ? TypeOfChangeForInvalidation::NodeRemoval : TypeOfChangeForInvalidation::NodeInsertion);
}
void LineLayout::updateInlineContentDimensions()
{
m_boxGeometryUpdater.setGeometriesForLayout();
}
void LineLayout::updateStyle(const RenderObject& renderer)
{
BoxTree::updateStyle(renderer);
}
bool LineLayout::rootStyleWillChange(const RenderBlockFlow& root, const RenderStyle& newStyle)
{
if (!root.layoutBox() || !root.layoutBox()->isElementBox()) {
ASSERT_NOT_REACHED();
return false;
}
if (!m_inlineContent)
return false;
return Layout::InlineInvalidation { ensureLineDamage(), m_inlineContentCache.inlineItems().content(), m_inlineContent->displayContent() }.rootStyleWillChange(downcast<Layout::ElementBox>(*root.layoutBox()), newStyle);
}
bool LineLayout::styleWillChange(const RenderElement& renderer, const RenderStyle& newStyle)
{
if (!renderer.layoutBox()) {
ASSERT_NOT_REACHED();
return false;
}
if (!m_inlineContent)
return false;
return Layout::InlineInvalidation { ensureLineDamage(), m_inlineContentCache.inlineItems().content(), m_inlineContent->displayContent() }.styleWillChange(*renderer.layoutBox(), newStyle);
}
bool LineLayout::boxContentWillChange(const RenderBox& renderer)
{
if (!m_inlineContent || !renderer.layoutBox())
return false;
return Layout::InlineInvalidation { ensureLineDamage(), m_inlineContentCache.inlineItems().content(), m_inlineContent->displayContent() }.inlineLevelBoxContentWillChange(*renderer.layoutBox());
}
void LineLayout::updateOverflow()
{
InlineContentBuilder { flow(), m_boxTree }.updateLineOverflow(*m_inlineContent);
}
std::pair<LayoutUnit, LayoutUnit> LineLayout::computeIntrinsicWidthConstraints()
{
auto parentBlockLayoutState = Layout::BlockLayoutState { m_blockFormattingState.placedFloats() };
auto inlineFormattingContext = Layout::InlineFormattingContext { rootLayoutBox(), layoutState(), parentBlockLayoutState };
if (m_lineDamage)
m_inlineContentCache.resetMinimumMaximumContentSizes();
// FIXME: This is where we need to switch between minimum and maximum box geometries.
// Currently we only support content where min == max.
m_boxGeometryUpdater.setGeometriesForIntrinsicWidth(Layout::IntrinsicWidthMode::Minimum);
auto [minimumContentSize, maximumContentSize] = inlineFormattingContext.minimumMaximumContentSize(m_lineDamage.get());
return { minimumContentSize, maximumContentSize };
}
static inline std::optional<Layout::BlockLayoutState::LineClamp> lineClamp(const RenderBlockFlow& rootRenderer)
{
auto& layoutState = *rootRenderer.view().frameView().layoutContext().layoutState();
if (auto lineClamp = layoutState.lineClamp())
return Layout::BlockLayoutState::LineClamp { lineClamp->maximumLineCount, lineClamp->currentLineCount };
return { };
}
static inline Layout::BlockLayoutState::TextBoxTrim textBoxTrim(const RenderBlockFlow& rootRenderer)
{
auto* layoutState = rootRenderer.view().frameView().layoutContext().layoutState();
if (!layoutState)
return { };
auto textBoxTrimForIFC = Layout::BlockLayoutState::TextBoxTrim { };
auto isFlippedLinesWritingMode = rootRenderer.style().isFlippedLinesWritingMode();
if (layoutState->hasTextBoxTrimStart())
textBoxTrimForIFC.add(isFlippedLinesWritingMode ? Layout::BlockLayoutState::TextBoxTrimSide::End : Layout::BlockLayoutState::TextBoxTrimSide::Start);
if (layoutState->hasTextBoxTrimEnd(rootRenderer))
textBoxTrimForIFC.add(isFlippedLinesWritingMode ? Layout::BlockLayoutState::TextBoxTrimSide::Start : Layout::BlockLayoutState::TextBoxTrimSide::End);
return textBoxTrimForIFC;
}
static inline TextEdge textBoxEdge(const RenderBlockFlow& rootRenderer)
{
auto* layoutState = rootRenderer.view().frameView().layoutContext().layoutState();
if (!layoutState)
return { };
if (auto textBoxTrim = layoutState->textBoxTrim())
return textBoxTrim->propagatedTextBoxEdge;
return { };
}
static inline std::optional<Layout::BlockLayoutState::LineGrid> lineGrid(const RenderBlockFlow& rootRenderer)
{
auto& layoutState = *rootRenderer.view().frameView().layoutContext().layoutState();
if (auto* lineGrid = layoutState.lineGrid()) {
if (lineGrid->style().writingMode() != rootRenderer.style().writingMode())
return { };
auto layoutOffset = layoutState.layoutOffset();
auto lineGridOffset = layoutState.lineGridOffset();
if (lineGrid->style().isVerticalWritingMode()) {
layoutOffset = layoutOffset.transposedSize();
lineGridOffset = lineGridOffset.transposedSize();
}
auto columnWidth = lineGrid->style().fontCascade().primaryFont().maxCharWidth();
auto rowHeight = LayoutUnit::fromFloatCeil(lineGrid->style().computedLineHeight());
auto topRowOffset = lineGrid->borderAndPaddingBefore();
std::optional<LayoutSize> paginationOrigin;
auto pageLogicalTop = 0_lu;
if (layoutState.isPaginated()) {
paginationOrigin = layoutState.lineGridPaginationOrigin();
if (lineGrid->style().isVerticalWritingMode())
paginationOrigin = paginationOrigin->transposedSize();
pageLogicalTop = rootRenderer.pageLogicalTopForOffset(0_lu);
}
return Layout::BlockLayoutState::LineGrid { layoutOffset, lineGridOffset, columnWidth, rowHeight, topRowOffset, lineGrid->style().fontCascade().primaryFont(), paginationOrigin, pageLogicalTop };
}
return { };
}
std::optional<LayoutRect> LineLayout::layout()
{
preparePlacedFloats();
auto isPartialLayout = Layout::InlineInvalidation::mayOnlyNeedPartialLayout(m_lineDamage.get());
if (!isPartialLayout) {
// FIXME: Partial layout should not rely on previous inline display content.
clearInlineContent();
}
ASSERT(m_inlineContentConstraints);
auto intrusiveInitialLetterBottom = [&]() -> std::optional<LayoutUnit> {
if (auto lowestInitialLetterLogicalBottom = flow().lowestInitialLetterLogicalBottom())
return { *lowestInitialLetterLogicalBottom - m_inlineContentConstraints->logicalTop() };
return { };
};
auto inlineContentConstraints = [&]() -> Layout::ConstraintsForInlineContent {
if (!isPartialLayout || !m_inlineContent)
return *m_inlineContentConstraints;
auto damagedLineIndex = m_lineDamage->layoutStartPosition()->lineIndex;
if (!damagedLineIndex)
return *m_inlineContentConstraints;
if (damagedLineIndex >= m_inlineContent->displayContent().lines.size()) {
ASSERT_NOT_REACHED();
return *m_inlineContentConstraints;
}
auto constraintsForInFlowContent = Layout::ConstraintsForInFlowContent { m_inlineContentConstraints->horizontal(), m_lineDamage->layoutStartPosition()->partialContentTop };
return { constraintsForInFlowContent, m_inlineContentConstraints->visualLeft() };
};
auto parentBlockLayoutState = Layout::BlockLayoutState {
m_blockFormattingState.placedFloats(),
lineClamp(flow()),
textBoxTrim(flow()),
textBoxEdge(flow()),
intrusiveInitialLetterBottom(),
lineGrid(flow())
};
auto inlineFormattingContext = Layout::InlineFormattingContext { rootLayoutBox(), layoutState(), parentBlockLayoutState };
// Temporary, integration only.
inlineFormattingContext.layoutState().setNestedListMarkerOffsets(m_boxGeometryUpdater.takeNestedListMarkerOffsets());
auto layoutResult = inlineFormattingContext.layout(inlineContentConstraints(), m_lineDamage.get());
auto repaintRect = LayoutRect { constructContent(inlineFormattingContext.layoutState(), WTFMove(layoutResult)) };
m_lineDamage = { };
auto adjustments = adjustContentForPagination(parentBlockLayoutState, isPartialLayout);
updateRenderTreePositions(adjustments, inlineFormattingContext.layoutState());
if (m_lineDamage) {
// Pagination may require another layout pass.
layout();
ASSERT(!m_lineDamage);
}
return isPartialLayout ? std::make_optional(repaintRect) : std::nullopt;
}
FloatRect LineLayout::constructContent(const Layout::InlineLayoutState& inlineLayoutState, Layout::InlineLayoutResult&& layoutResult)
{
auto damagedRect = InlineContentBuilder { flow(), m_boxTree }.build(WTFMove(layoutResult), ensureInlineContent(), m_lineDamage.get());
m_inlineContent->clearGapBeforeFirstLine = inlineLayoutState.clearGapBeforeFirstLine();
m_inlineContent->clearGapAfterLastLine = inlineLayoutState.clearGapAfterLastLine();
m_inlineContent->shrinkToFit();
m_inlineContentCache.inlineItems().shrinkToFit();
m_blockFormattingState.shrinkToFit();
// FIXME: These needs to be incorporated into the partial damage.
auto additionalHeight = m_inlineContent->firstLinePaginationOffset + m_inlineContent->clearGapBeforeFirstLine + m_inlineContent->clearGapAfterLastLine;
damagedRect.expand({ 0, additionalHeight });
return damagedRect;
}
void LineLayout::updateRenderTreePositions(const Vector<LineAdjustment>& lineAdjustments, const Layout::InlineLayoutState& inlineLayoutState)
{
if (!m_inlineContent)
return;
auto& blockFlow = flow();
auto& rootStyle = blockFlow.style();
auto isLeftToRightPlacedFloatsInlineDirection = m_blockFormattingState.placedFloats().isLeftToRightDirection();
auto writingMode = rootStyle.writingMode();
auto isHorizontalWritingMode = WebCore::isHorizontalWritingMode(writingMode);
auto visualAdjustmentOffset = [&](auto lineIndex) {
if (lineAdjustments.isEmpty())
return LayoutSize { };
if (!isHorizontalWritingMode)
return LayoutSize { lineAdjustments[lineIndex].offset, 0_lu };
return LayoutSize { 0_lu, lineAdjustments[lineIndex].offset };
};
for (auto& box : m_inlineContent->displayContent().boxes) {
if (box.isInlineBox() || box.isText())
continue;
auto& layoutBox = box.layoutBox();
if (!layoutBox.isAtomicInlineBox())
continue;
auto& renderer = downcast<RenderBox>(*box.layoutBox().rendererForIntegration());
if (auto* layer = renderer.layer())
layer->setIsHiddenByOverflowTruncation(box.isFullyTruncated());
renderer.setLocation(Layout::toLayoutPoint(box.visualRectIgnoringBlockDirection().location()));
}
HashMap<CheckedRef<const Layout::Box>, LayoutSize> floatPaginationOffsetMap;
if (!lineAdjustments.isEmpty()) {
for (auto& floatItem : m_blockFormattingState.placedFloats().list()) {
if (!floatItem.layoutBox() || !floatItem.placedByLine())
continue;
auto adjustmentOffset = visualAdjustmentOffset(*floatItem.placedByLine());
floatPaginationOffsetMap.add(*floatItem.layoutBox(), adjustmentOffset);
}
}
for (auto& layoutBox : formattingContextBoxes(rootLayoutBox())) {
if (!layoutBox.isFloatingPositioned() && !layoutBox.isOutOfFlowPositioned())
continue;
if (layoutBox.isLineBreakBox())
continue;
auto& renderer = downcast<RenderBox>(*layoutBox.rendererForIntegration());
auto& logicalGeometry = layoutState().geometryForBox(layoutBox);
if (layoutBox.isFloatingPositioned()) {
auto isInitialLetter = layoutBox.style().pseudoElementType() == PseudoId::FirstLetter;
auto& floatingObject = flow().insertFloatingObjectForIFC(renderer);
auto containerLogicalWidth = m_inlineContentConstraints->visualLeft() + m_inlineContentConstraints->horizontal().logicalWidth + m_inlineContentConstraints->horizontal().logicalLeft;
auto [marginBoxVisualRect, borderBoxVisualRect] = toMarginAndBorderBoxVisualRect(logicalGeometry, containerLogicalWidth, writingMode, isLeftToRightPlacedFloatsInlineDirection);
auto paginationOffset = floatPaginationOffsetMap.getOptional(layoutBox);
if (paginationOffset) {
marginBoxVisualRect.move(*paginationOffset);
borderBoxVisualRect.move(*paginationOffset);
}
if (isInitialLetter) {
auto firstLineTrim = LayoutUnit { inlineLayoutState.firstLineStartTrimForInitialLetter() };
marginBoxVisualRect.move(0_lu, -firstLineTrim);
borderBoxVisualRect.move(0_lu, -firstLineTrim);
}
floatingObject.setFrameRect(marginBoxVisualRect);
floatingObject.setMarginOffset({ borderBoxVisualRect.x() - marginBoxVisualRect.x(), borderBoxVisualRect.y() - marginBoxVisualRect.y() });
floatingObject.setIsPlaced(true);
auto oldRect = renderer.frameRect();
renderer.setLocation(borderBoxVisualRect.location());
if (renderer.checkForRepaintDuringLayout()) {
auto hasMoved = oldRect.location() != renderer.location();
if (hasMoved)
renderer.repaintDuringLayoutIfMoved(oldRect);
else
renderer.repaint();
}
if (paginationOffset) {
// Float content may be affected by the new position.
renderer.markForPaginationRelayoutIfNeeded();
renderer.layoutIfNeeded();
}
continue;
}
if (layoutBox.isOutOfFlowPositioned()) {
ASSERT(renderer.layer());
auto& layer = *renderer.layer();
auto borderBoxLogicalTopLeft = Layout::BoxGeometry::borderBoxRect(logicalGeometry).topLeft();
auto previousStaticPosition = LayoutPoint { layer.staticInlinePosition(), layer.staticBlockPosition() };
auto delta = borderBoxLogicalTopLeft - previousStaticPosition;
auto hasStaticInlinePositioning = layoutBox.style().hasStaticInlinePosition(renderer.isHorizontalWritingMode());
if (layoutBox.style().isOriginalDisplayInlineType()) {
blockFlow.setStaticInlinePositionForChild(renderer, borderBoxLogicalTopLeft.y(), borderBoxLogicalTopLeft.x());
if (hasStaticInlinePositioning)
renderer.move(delta.width(), delta.height());
}
layer.setStaticBlockPosition(borderBoxLogicalTopLeft.y());
layer.setStaticInlinePosition(borderBoxLogicalTopLeft.x());
if (!delta.isZero() && hasStaticInlinePositioning)
renderer.setChildNeedsLayout(MarkOnlyThis);
continue;
}
}
}
void LineLayout::updateInlineContentConstraints()
{
m_inlineContentConstraints = m_boxGeometryUpdater.updateInlineContentConstraints();
}
void LineLayout::preparePlacedFloats()
{
auto& placedFloats = m_blockFormattingState.placedFloats();
placedFloats.clear();
if (!flow().containsFloats())
return;
auto isHorizontalWritingMode = flow().containingBlock() ? flow().containingBlock()->style().isHorizontalWritingMode() : true;
auto placedFloatsIsLeftToRightInlineDirection = flow().containingBlock() ? flow().containingBlock()->style().isLeftToRightDirection() : true;
placedFloats.setIsLeftToRightDirection(placedFloatsIsLeftToRightInlineDirection);
for (auto& floatingObject : *flow().floatingObjectSet()) {
auto& visualRect = floatingObject->frameRect();
auto logicalPosition = [&] {
switch (floatingObject->renderer().style().floating()) {
case Float::Left:
return placedFloatsIsLeftToRightInlineDirection ? Layout::PlacedFloats::Item::Position::Left : Layout::PlacedFloats::Item::Position::Right;
case Float::Right:
return placedFloatsIsLeftToRightInlineDirection ? Layout::PlacedFloats::Item::Position::Right : Layout::PlacedFloats::Item::Position::Left;
case Float::InlineStart: {
auto* floatBoxContainingBlock = floatingObject->renderer().containingBlock();
if (floatBoxContainingBlock)
return floatBoxContainingBlock->style().isLeftToRightDirection() == placedFloatsIsLeftToRightInlineDirection ? Layout::PlacedFloats::Item::Position::Left : Layout::PlacedFloats::Item::Position::Right;
return Layout::PlacedFloats::Item::Position::Left;
}
case Float::InlineEnd: {
auto* floatBoxContainingBlock = floatingObject->renderer().containingBlock();
if (floatBoxContainingBlock)
return floatBoxContainingBlock->style().isLeftToRightDirection() == placedFloatsIsLeftToRightInlineDirection ? Layout::PlacedFloats::Item::Position::Right : Layout::PlacedFloats::Item::Position::Left;
return Layout::PlacedFloats::Item::Position::Right;
}
default:
ASSERT_NOT_REACHED();
return Layout::PlacedFloats::Item::Position::Left;
}
};
auto boxGeometry = Layout::BoxGeometry { };
auto logicalRect = [&] {
// FIXME: We are flooring here for legacy compatibility. See FloatingObjects::intervalForFloatingObject and RenderBlockFlow::clearFloats.
auto logicalTop = isHorizontalWritingMode ? LayoutUnit(visualRect.y().floor()) : visualRect.x();
auto logicalLeft = isHorizontalWritingMode ? visualRect.x() : LayoutUnit(visualRect.y().floor());
auto logicalHeight = (isHorizontalWritingMode ? LayoutUnit(visualRect.maxY().floor()) : visualRect.maxX()) - logicalTop;
auto logicalWidth = (isHorizontalWritingMode ? visualRect.maxX() : LayoutUnit(visualRect.maxY().floor())) - logicalLeft;
if (!placedFloatsIsLeftToRightInlineDirection) {
auto rootBorderBoxWidth = m_inlineContentConstraints->visualLeft() + m_inlineContentConstraints->horizontal().logicalWidth + m_inlineContentConstraints->horizontal().logicalLeft;
logicalLeft = rootBorderBoxWidth - (logicalLeft + logicalWidth);
}
return LayoutRect { logicalLeft, logicalTop, logicalWidth, logicalHeight };
}();
boxGeometry.setTopLeft(logicalRect.location());
boxGeometry.setContentBoxWidth(logicalRect.width());
boxGeometry.setContentBoxHeight(logicalRect.height());
boxGeometry.setBorder({ });
boxGeometry.setPadding({ });
boxGeometry.setHorizontalMargin({ });
boxGeometry.setVerticalMargin({ });
auto shapeOutsideInfo = floatingObject->renderer().shapeOutsideInfo();
auto* shape = shapeOutsideInfo ? &shapeOutsideInfo->computedShape() : nullptr;
placedFloats.append({ logicalPosition(), boxGeometry, logicalRect.location(), shape });
}
}
bool LineLayout::isPaginated() const
{
return m_inlineContent && m_inlineContent->isPaginated;
}
std::optional<LayoutUnit> LineLayout::clampedContentLogicalHeight() const
{
if (!m_inlineContent)
return { };
auto& lines = m_inlineContent->displayContent().lines;
if (lines.isEmpty()) {
// Out-of-flow only content (and/or with floats) may produce blank inline content.
return { };
}
auto firstTruncatedLineIndex = [&]() -> std::optional<size_t> {
for (size_t lineIndex = 0; lineIndex < lines.size(); ++lineIndex) {
if (lines[lineIndex].isTruncatedInBlockDirection())
return lineIndex;
}
return { };
}();
if (!firstTruncatedLineIndex)
return { };
if (!*firstTruncatedLineIndex) {
// This content is fully truncated in the block direction.
return LayoutUnit { };
}
auto contentHeight = lines[*firstTruncatedLineIndex - 1].lineBoxLogicalRect().maxY() - lines.first().lineBoxLogicalRect().y();
auto additionalHeight = m_inlineContent->firstLinePaginationOffset + m_inlineContent->clearGapBeforeFirstLine + m_inlineContent->clearGapAfterLastLine;
return LayoutUnit { contentHeight + additionalHeight };
}
LayoutUnit LineLayout::contentBoxLogicalHeight() const
{
if (!m_inlineContent)
return { };
auto& lines = m_inlineContent->displayContent().lines;
if (lines.isEmpty()) {
// Out-of-flow only content (and/or with floats) may produce blank inline content.
return { };
}
auto contentHeight = lastLineWithInlineContent(lines).lineBoxLogicalRect().maxY() - lines.first().lineBoxLogicalRect().y();
auto additionalHeight = m_inlineContent->firstLinePaginationOffset + m_inlineContent->clearGapBeforeFirstLine + m_inlineContent->clearGapAfterLastLine;
return LayoutUnit { contentHeight + additionalHeight };
}
size_t LineLayout::lineCount() const
{
if (!m_inlineContent)
return 0;
if (!m_inlineContent->hasContent())
return 0;
return m_inlineContent->displayContent().lines.size();
}
bool LineLayout::hasVisualOverflow() const
{
return m_inlineContent && m_inlineContent->hasVisualOverflow();
}
LayoutUnit LineLayout::firstLinePhysicalBaseline() const
{
if (!m_inlineContent || m_inlineContent->displayContent().boxes.isEmpty()) {
ASSERT_NOT_REACHED();
return { };
}
auto& firstLine = m_inlineContent->displayContent().lines.first();
return physicalBaselineForLine(firstLine);
}
LayoutUnit LineLayout::lastLinePhysicalBaseline() const
{
if (!m_inlineContent || m_inlineContent->displayContent().lines.isEmpty()) {
ASSERT_NOT_REACHED();
return { };
}
return physicalBaselineForLine(lastLineWithInlineContent(m_inlineContent->displayContent().lines));
}
LayoutUnit LineLayout::physicalBaselineForLine(const InlineDisplay::Line& line) const
{
switch (writingModeToBlockFlowDirection(rootLayoutBox().style().writingMode())) {
case BlockFlowDirection::TopToBottom:
case BlockFlowDirection::BottomToTop:
return LayoutUnit { line.lineBoxTop() + line.baseline() };
case BlockFlowDirection::LeftToRight:
return LayoutUnit { line.lineBoxLeft() + (line.lineBoxWidth() - line.baseline()) };
case BlockFlowDirection::RightToLeft:
return LayoutUnit { line.lineBoxLeft() + line.baseline() };
default:
ASSERT_NOT_REACHED();
return { };
}
}
LayoutUnit LineLayout::lastLineLogicalBaseline() const
{
if (!m_inlineContent || m_inlineContent->displayContent().lines.isEmpty()) {
ASSERT_NOT_REACHED();
return { };
}
auto& lastLine = lastLineWithInlineContent(m_inlineContent->displayContent().lines);
switch (writingModeToBlockFlowDirection(rootLayoutBox().style().writingMode())) {
case BlockFlowDirection::TopToBottom:
case BlockFlowDirection::BottomToTop:
return LayoutUnit { lastLine.lineBoxTop() + lastLine.baseline() };
case BlockFlowDirection::LeftToRight: {
// FIXME: We should set the computed height on the root's box geometry (in RenderBlockFlow) so that
// we could call m_layoutState.geometryForRootBox().borderBoxHeight() instead.
// Line is always visual coordinates while logicalHeight is not (i.e. this translate to "box visual width" - "line visual right")
auto lineLogicalTop = flow().logicalHeight() - lastLine.lineBoxRight();
return LayoutUnit { lineLogicalTop + lastLine.baseline() };
}
case BlockFlowDirection::RightToLeft:
return LayoutUnit { lastLine.lineBoxLeft() + lastLine.baseline() };
default:
ASSERT_NOT_REACHED();
return { };
}
}
Vector<LineAdjustment> LineLayout::adjustContentForPagination(const Layout::BlockLayoutState& blockLayoutState, bool isPartialLayout)
{
ASSERT(!m_lineDamage);
if (!m_inlineContent)
return { };
auto& layoutState = *flow().view().frameView().layoutContext().layoutState();
if (!layoutState.isPaginated())
return { };
bool allowLayoutRestart = !isPartialLayout;
auto [adjustments, layoutRestartLine] = computeAdjustmentsForPagination(*m_inlineContent, m_blockFormattingState.placedFloats(), allowLayoutRestart, blockLayoutState, flow());
adjustLinePositionsForPagination(*m_inlineContent, adjustments);
if (layoutRestartLine) {
auto invalidation = Layout::InlineInvalidation { ensureLineDamage(), m_inlineContentCache.inlineItems().content(), m_inlineContent->displayContent() };
auto canRestart = invalidation.restartForPagination(layoutRestartLine->index, layoutRestartLine->offset);
if (!canRestart)
m_lineDamage = { };
}
return adjustments;
}
void LineLayout::collectOverflow()
{
if (!m_inlineContent)
return;
for (auto& line : m_inlineContent->displayContent().lines) {
flow().addLayoutOverflow(Layout::toLayoutRect(line.scrollableOverflow()));
if (!flow().hasNonVisibleOverflow())
flow().addVisualOverflow(Layout::toLayoutRect(line.inkOverflow()));
}
}
InlineContent& LineLayout::ensureInlineContent()
{
if (!m_inlineContent)
m_inlineContent = makeUnique<InlineContent>(*this);
return *m_inlineContent;
}
InlineIterator::TextBoxIterator LineLayout::textBoxesFor(const RenderText& renderText) const
{
if (!m_inlineContent)
return { };
auto& layoutBox = *renderText.layoutBox();
auto firstIndex = m_inlineContent->firstBoxIndexForLayoutBox(layoutBox);
if (!firstIndex)
return { };
return InlineIterator::textBoxFor(*m_inlineContent, *firstIndex);
}
InlineIterator::LeafBoxIterator LineLayout::boxFor(const RenderElement& renderElement) const
{
if (!m_inlineContent)
return { };
auto& layoutBox = *renderElement.layoutBox();
auto firstIndex = m_inlineContent->firstBoxIndexForLayoutBox(layoutBox);
if (!firstIndex)
return { };
return InlineIterator::boxFor(*m_inlineContent, *firstIndex);
}
InlineIterator::InlineBoxIterator LineLayout::firstInlineBoxFor(const RenderInline& renderInline) const
{
if (!m_inlineContent)
return { };
auto& layoutBox = *renderInline.layoutBox();
auto* box = m_inlineContent->firstBoxForLayoutBox(layoutBox);
if (!box)
return { };
return InlineIterator::inlineBoxFor(*m_inlineContent, *box);
}
InlineIterator::InlineBoxIterator LineLayout::firstRootInlineBox() const
{
if (!m_inlineContent || !m_inlineContent->hasContent())
return { };
return InlineIterator::inlineBoxFor(*m_inlineContent, m_inlineContent->displayContent().boxes[0]);
}
InlineIterator::LineBoxIterator LineLayout::firstLineBox() const
{
if (!m_inlineContent)
return { };
return { InlineIterator::LineBoxIteratorModernPath(*m_inlineContent, 0) };
}
InlineIterator::LineBoxIterator LineLayout::lastLineBox() const
{
if (!m_inlineContent)
return { };
return { InlineIterator::LineBoxIteratorModernPath(*m_inlineContent, m_inlineContent->displayContent().lines.isEmpty() ? 0 : m_inlineContent->displayContent().lines.size() - 1) };
}
LayoutRect LineLayout::firstInlineBoxRect(const RenderInline& renderInline) const
{
if (!m_inlineContent)
return { };
auto& layoutBox = *renderInline.layoutBox();
auto* firstBox = m_inlineContent->firstBoxForLayoutBox(layoutBox);
if (!firstBox)
return { };
// FIXME: We should be able to flip the display boxes soon after the root block
// is finished sizing in one go.
auto firstBoxRect = Layout::toLayoutRect(firstBox->visualRectIgnoringBlockDirection());
switch (writingModeToBlockFlowDirection(rootLayoutBox().style().writingMode())) {
case BlockFlowDirection::TopToBottom:
case BlockFlowDirection::BottomToTop:
case BlockFlowDirection::LeftToRight:
return firstBoxRect;
case BlockFlowDirection::RightToLeft:
firstBoxRect.setX(flow().width() - firstBoxRect.maxX());
return firstBoxRect;
default:
ASSERT_NOT_REACHED();
return firstBoxRect;
}
}
LayoutRect LineLayout::enclosingBorderBoxRectFor(const RenderInline& renderInline) const
{
if (!m_inlineContent)
return { };
// FIXME: This keeps the existing output.
if (!m_inlineContent->hasContent())
return { };
auto borderBoxLogicalRect = LayoutRect { Layout::BoxGeometry::borderBoxRect(layoutState().geometryForBox(*renderInline.layoutBox())) };
return WebCore::isHorizontalWritingMode(flow().style().writingMode()) ? borderBoxLogicalRect : borderBoxLogicalRect.transposedRect();
}
LayoutRect LineLayout::visualOverflowBoundingBoxRectFor(const RenderInline& renderInline) const
{
if (!m_inlineContent)
return { };
auto& layoutBox = *renderInline.layoutBox();
LayoutRect result;
m_inlineContent->traverseNonRootInlineBoxes(layoutBox, [&](auto& inlineBox) {
result.unite(Layout::toLayoutRect(inlineBox.inkOverflow()));
});
return result;
}
Vector<FloatRect> LineLayout::collectInlineBoxRects(const RenderInline& renderInline) const
{
if (!m_inlineContent)
return { };
auto& layoutBox = *renderInline.layoutBox();
Vector<FloatRect> result;
m_inlineContent->traverseNonRootInlineBoxes(layoutBox, [&](auto& inlineBox) {
result.append(inlineBox.visualRectIgnoringBlockDirection());
});
return result;
}
const Layout::ElementBox& LineLayout::rootLayoutBox() const
{
return m_boxTree.rootLayoutBox();
}
Layout::ElementBox& LineLayout::rootLayoutBox()
{
return m_boxTree.rootLayoutBox();
}
static LayoutPoint flippedContentOffsetIfNeeded(const RenderBlockFlow& root, const RenderBox& childRenderer, LayoutPoint contentOffset)
{
if (root.style().isFlippedBlocksWritingMode())
return root.flipForWritingModeForChild(childRenderer, contentOffset);
return contentOffset;
}
static LayoutRect flippedRectForWritingMode(const RenderBlockFlow& root, const FloatRect& rect)
{
auto flippedRect = LayoutRect { rect };
root.flipForWritingMode(flippedRect);
return flippedRect;
}
void LineLayout::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset, const RenderInline* layerRenderer)
{
if (!m_inlineContent)
return;
auto shouldPaintForPhase = [&] {
switch (paintInfo.phase) {
case PaintPhase::Accessibility:
case PaintPhase::Foreground:
case PaintPhase::EventRegion:
case PaintPhase::TextClip:
case PaintPhase::Mask:
case PaintPhase::Selection:
case PaintPhase::Outline:
case PaintPhase::ChildOutlines:
case PaintPhase::SelfOutline:
return true;
default:
return false;
}
};
if (!shouldPaintForPhase())
return;
InlineContentPainter { paintInfo, paintOffset, layerRenderer, *m_inlineContent, m_boxTree }.paint();
}
bool LineLayout::hitTest(const HitTestRequest& request, HitTestResult& result, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, HitTestAction hitTestAction, const RenderInline* layerRenderer)
{
if (hitTestAction != HitTestForeground)
return false;
if (!m_inlineContent)
return false;
auto hitTestBoundingBox = locationInContainer.boundingBox();
hitTestBoundingBox.moveBy(-accumulatedOffset);
auto boxRange = m_inlineContent->boxesForRect(hitTestBoundingBox);
LayerPaintScope layerPaintScope(m_boxTree, layerRenderer);
for (auto& box : makeReversedRange(boxRange)) {
bool visibleForHitTesting = request.userTriggered() ? box.isVisible() : box.isVisibleIgnoringUsedVisibility();
if (!visibleForHitTesting)
continue;
auto& renderer = *box.layoutBox().rendererForIntegration();
if (!layerPaintScope.includes(box))
continue;
if (box.isAtomicInlineBox()) {
if (renderer.hitTest(request, result, locationInContainer, flippedContentOffsetIfNeeded(flow(), downcast<RenderBox>(renderer), accumulatedOffset)))
return true;
continue;
}
auto& currentLine = m_inlineContent->displayContent().lines[box.lineIndex()];
auto boxRect = flippedRectForWritingMode(flow(), InlineDisplay::Box::visibleRectIgnoringBlockDirection(box, currentLine.visibleRectIgnoringBlockDirection()));
boxRect.moveBy(accumulatedOffset);
if (!locationInContainer.intersects(boxRect))
continue;
auto& elementRenderer = *[&]() {
auto* renderElement = dynamicDowncast<RenderElement>(renderer);
return renderElement ? renderElement : renderer.parent();
}();
if (!elementRenderer.visibleToHitTesting(request))
continue;
renderer.updateHitTestResult(result, flow().flipForWritingMode(locationInContainer.point() - toLayoutSize(accumulatedOffset)));
if (result.addNodeToListBasedTestResult(renderer.protectedNodeForHitTest().get(), request, locationInContainer, boxRect) == HitTestProgress::Stop)
return true;
}
return false;
}
void LineLayout::shiftLinesBy(LayoutUnit blockShift)
{
if (!m_inlineContent)
return;
bool isHorizontalWritingMode = WebCore::isHorizontalWritingMode(flow().style().writingMode());
for (auto& line : m_inlineContent->displayContent().lines)
line.moveInBlockDirection(blockShift, isHorizontalWritingMode);
LayoutUnit deltaX = isHorizontalWritingMode ? 0_lu : blockShift;
LayoutUnit deltaY = isHorizontalWritingMode ? blockShift : 0_lu;
for (auto& box : m_inlineContent->displayContent().boxes) {
if (isHorizontalWritingMode)
box.moveVertically(blockShift);
else
box.moveHorizontally(blockShift);
if (box.isAtomicInlineBox()) {
CheckedRef renderer = downcast<RenderBox>(*box.layoutBox().rendererForIntegration());
renderer->move(deltaX, deltaY);
}
}
for (auto& layoutBox : formattingContextBoxes(rootLayoutBox())) {
if (layoutBox.isOutOfFlowPositioned() && layoutBox.style().hasStaticBlockPosition(isHorizontalWritingMode)) {
CheckedRef renderer = downcast<RenderLayerModelObject>(*layoutBox.rendererForIntegration());
if (!renderer->layer())
continue;
CheckedRef layer = *renderer->layer();
layer->setStaticBlockPosition(layer->staticBlockPosition() + blockShift);
renderer->setChildNeedsLayout(MarkOnlyThis);
}
}
}
bool LineLayout::insertedIntoTree(const RenderElement& parent, RenderObject& child)
{
if (!m_inlineContent) {
// This should only be called on partial layout.
ASSERT_NOT_REACHED();
return false;
}
auto& childLayoutBox = m_boxTree.insert(parent, child, child.previousSibling());
if (auto* childInlineTextBox = dynamicDowncast<Layout::InlineTextBox>(childLayoutBox)) {
auto invalidation = Layout::InlineInvalidation { ensureLineDamage(), m_inlineContentCache.inlineItems().content(), m_inlineContent->displayContent() };
return invalidation.textInserted(*childInlineTextBox);
}
if (childLayoutBox.isLineBreakBox() || childLayoutBox.isReplacedBox() || childLayoutBox.isInlineBox()) {
auto invalidation = Layout::InlineInvalidation { ensureLineDamage(), m_inlineContentCache.inlineItems().content(), m_inlineContent->displayContent() };
return invalidation.inlineLevelBoxInserted(childLayoutBox);
}
ASSERT_NOT_IMPLEMENTED_YET();
return false;
}
bool LineLayout::removedFromTree(const RenderElement& parent, RenderObject& child)
{
if (!m_inlineContent) {
// This should only be called on partial layout.
ASSERT_NOT_REACHED();
return false;
}
auto& childLayoutBox = *child.layoutBox();
auto* childInlineTextBox = dynamicDowncast<Layout::InlineTextBox>(childLayoutBox);
auto invalidation = Layout::InlineInvalidation { ensureLineDamage(), m_inlineContentCache.inlineItems().content(), m_inlineContent->displayContent() };
auto boxIsInvalidated = childInlineTextBox ? invalidation.textWillBeRemoved(*childInlineTextBox) : childLayoutBox.isLineBreakBox() ? invalidation.inlineLevelBoxWillBeRemoved(childLayoutBox) : false;
if (boxIsInvalidated)
m_lineDamage->addDetachedBox(m_boxTree.remove(parent, child));
return boxIsInvalidated;
}
bool LineLayout::updateTextContent(const RenderText& textRenderer, size_t offset, int delta)
{
if (!m_inlineContent) {
// This is supposed to be only called on partial layout, but
// RenderText::setText may be (force) called after min/max size computation and before layout.
// We may need to invalidate anyway to clean up inline item list.
return false;
}
m_boxTree.updateContent(textRenderer);
auto invalidation = Layout::InlineInvalidation { ensureLineDamage(), m_inlineContentCache.inlineItems().content(), m_inlineContent->displayContent() };
auto& inlineTextBox = *textRenderer.layoutBox();
return delta >= 0 ? invalidation.textInserted(inlineTextBox, offset) : invalidation.textWillBeRemoved(inlineTextBox, offset);
}
void LineLayout::releaseCaches(RenderView& view)
{
for (auto& renderer : descendantsOfType<RenderBlockFlow>(view)) {
if (auto* lineLayout = renderer.modernLineLayout())
lineLayout->releaseCachesAndResetDamage();
}
}
void LineLayout::releaseCachesAndResetDamage()
{
m_inlineContentCache.inlineItems().content().clear();
if (m_inlineContent)
m_inlineContent->releaseCaches();
if (m_lineDamage)
Layout::InlineInvalidation::resetInlineDamage(*m_lineDamage);
}
void LineLayout::clearInlineContent()
{
if (!m_inlineContent)
return;
m_inlineContent = nullptr;
}
Layout::InlineDamage& LineLayout::ensureLineDamage()
{
if (!m_lineDamage)
m_lineDamage = makeUnique<Layout::InlineDamage>();
return *m_lineDamage;
}
bool LineLayout::contentNeedsVisualReordering() const
{
return m_inlineContentCache.inlineItems().requiresVisualReordering();
}
#if ENABLE(TREE_DEBUGGING)
void LineLayout::outputLineTree(WTF::TextStream& stream, size_t depth) const
{
if (m_inlineContent)
showInlineContent(stream, *m_inlineContent, depth, isDamaged());
}
#endif
}
}
|