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 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342
|
/*
* 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 "RenderLineBreak.h"
#include "RenderView.h"
#include "SVGTextFragment.h"
#include "Settings.h"
#include "ShapeOutsideInfo.h"
#include <wtf/Assertions.h>
#include <wtf/Range.h>
namespace WebCore {
namespace LayoutIntegration {
DEFINE_ALLOCATOR_WITH_HEAP_IDENTIFIER(LayoutIntegration_LineLayout);
enum class TypeOfChangeForInvalidation : uint8_t {
NodeInsertion,
NodeRemoval,
NodeMutation
};
static bool shouldInvalidateLineLayoutPathAfterChangeFor(const RenderBlockFlow& rootBlockContainer, const RenderObject& renderer, const LineLayout& lineLayout, TypeOfChangeForInvalidation typeOfChange)
{
auto isSupportedRendererWithChange = [&](auto& renderer) {
if (is<RenderText>(renderer))
return true;
if (!renderer.isInFlow())
return false;
if (is<RenderLineBreak>(renderer))
return true;
if (auto* renderBox = dynamicDowncast<RenderBox>(renderer); renderBox && renderBox->hasRelativeDimensions())
return false;
if (is<RenderReplaced>(renderer))
return typeOfChange == TypeOfChangeForInvalidation::NodeInsertion;
if (auto* inlineRenderer = dynamicDowncast<RenderInline>(renderer))
return typeOfChange == TypeOfChangeForInvalidation::NodeInsertion && !inlineRenderer->firstChild();
return false;
};
if (!isSupportedRendererWithChange(renderer))
return true;
auto isSupportedParent = [&] {
auto* parent = renderer.parent();
// Content append under existing inline box is not yet supported.
return is<RenderBlockFlow>(parent) || (is<RenderInline>(parent) && !parent->everHadLayout());
};
if (!isSupportedParent())
return true;
if (rootBlockContainer.containsFloats())
return true;
auto isBidiContent = [&] {
if (lineLayout.contentNeedsVisualReordering())
return true;
if (auto* textRenderer = dynamicDowncast<RenderText>(renderer)) {
auto hasStrongDirectionalityContent = textRenderer->hasStrongDirectionalityContent();
if (!hasStrongDirectionalityContent) {
hasStrongDirectionalityContent = Layout::TextUtil::containsStrongDirectionalityText(textRenderer->text());
const_cast<RenderText*>(textRenderer)->setHasStrongDirectionalityContent(*hasStrongDirectionalityContent);
}
return *hasStrongDirectionalityContent;
}
if (is<RenderInline>(renderer)) {
auto& style = renderer.style();
return style.writingMode().isBidiRTL() || (style.rtlOrdering() == Order::Logical && style.unicodeBidi() != UnicodeBidi::Normal);
}
return false;
};
if (isBidiContent()) {
// FIXME: InlineItemsBuilder needs some work to support paragraph level bidi handling.
return true;
}
auto hasFirstLetter = [&] {
// FIXME: RenderTreeUpdater::updateTextRenderer produces odd values for offset/length when first-letter is present webkit.org/b/263343
if (rootBlockContainer.style().hasPseudoStyle(PseudoId::FirstLetter))
return true;
if (rootBlockContainer.isAnonymous())
return rootBlockContainer.containingBlock() && rootBlockContainer.containingBlock()->style().hasPseudoStyle(PseudoId::FirstLetter);
return false;
};
if (hasFirstLetter())
return true;
if (auto* previousDamage = lineLayout.damage(); previousDamage && (previousDamage->reasons() != Layout::InlineDamage::Reason::Append || !previousDamage->layoutStartPosition())) {
// Only support subsequent append operations where we managed to invalidate the content for partial layout.
return true;
}
auto& rootBlockContainerStyle = rootBlockContainer.style();
auto shouldBalance = rootBlockContainerStyle.textWrapMode() == TextWrapMode::Wrap && rootBlockContainerStyle.textWrapStyle() == TextWrapStyle::Balance;
auto shouldPrettify = rootBlockContainerStyle.textWrapMode() == TextWrapMode::Wrap && rootBlockContainerStyle.textWrapStyle() == TextWrapStyle::Pretty;
auto hasAutospace = !rootBlockContainerStyle.textAutospace().isNoAutospace();
if (rootBlockContainer.writingMode().isBidiRTL() || shouldBalance || shouldPrettify || hasAutospace)
return true;
auto rootHasNonSupportedRenderer = [&] (bool shouldOnlyCheckForRelativeDimension = false) {
for (auto* sibling = rootBlockContainer.firstChild(); sibling; sibling = sibling->nextSibling()) {
auto siblingHasRelativeDimensions = false;
if (auto* renderBox = dynamicDowncast<RenderBox>(*sibling); renderBox && renderBox->hasRelativeDimensions())
siblingHasRelativeDimensions = true;
if (shouldOnlyCheckForRelativeDimension && !siblingHasRelativeDimensions)
continue;
if (siblingHasRelativeDimensions || (!is<RenderText>(*sibling) && !is<RenderLineBreak>(*sibling) && !is<RenderReplaced>(*sibling)))
return true;
}
return !canUseForLineLayout(rootBlockContainer);
};
switch (typeOfChange) {
case TypeOfChangeForInvalidation::NodeRemoval:
return (!renderer.previousSibling() && !renderer.nextSibling()) || rootHasNonSupportedRenderer();
case TypeOfChangeForInvalidation::NodeInsertion:
return rootHasNonSupportedRenderer(!renderer.nextSibling());
case TypeOfChangeForInvalidation::NodeMutation:
return rootHasNonSupportedRenderer();
default:
ASSERT_NOT_REACHED();
return true;
}
}
static inline std::pair<LayoutRect, LayoutRect> toMarginAndBorderBoxVisualRect(const Layout::BoxGeometry& logicalGeometry, const LayoutSize& containerSize, WritingMode writingMode)
{
// In certain writing modes, IFC gets the border box position wrong;
// but the margin box is correct, so use it to derive the border box.
auto marginBoxLogicalRect = Layout::BoxGeometry::marginBoxRect(logicalGeometry);
auto containerLogicalWidth = writingMode.isHorizontal()
? containerSize.width()
: containerSize.height();
auto marginBoxLogicalX = writingMode.isInlineFlipped()
? containerLogicalWidth - marginBoxLogicalRect.right()
: marginBoxLogicalRect.left();
auto marginBoxVisualRect = writingMode.isHorizontal()
? LayoutRect {
marginBoxLogicalX, marginBoxLogicalRect.top(),
marginBoxLogicalRect.width(), marginBoxLogicalRect.height() }
: LayoutRect {
marginBoxLogicalRect.top(), marginBoxLogicalX,
marginBoxLogicalRect.height(), marginBoxLogicalRect.width() };
auto borderBoxVisualRect = marginBoxVisualRect;
LayoutUnit marginLeft, marginTop, marginWidth, marginHeight;
if (writingMode.isHorizontal()) {
marginLeft = writingMode.isInlineLeftToRight()
? logicalGeometry.marginStart() : logicalGeometry.marginEnd();
marginTop = writingMode.isBlockTopToBottom()
? logicalGeometry.marginBefore() : logicalGeometry.marginAfter();
marginWidth = logicalGeometry.marginStart() + logicalGeometry.marginEnd();
marginHeight = logicalGeometry.marginBefore() + logicalGeometry.marginAfter();
} else {
marginLeft = writingMode.isLineInverted()
// Invert verticalLogicalMargin() *and* convert to unflipped coords.
? logicalGeometry.marginAfter() : logicalGeometry.marginBefore();
marginTop = writingMode.isInlineTopToBottom()
? logicalGeometry.marginStart() : logicalGeometry.marginEnd();
marginWidth = logicalGeometry.marginBefore() + logicalGeometry.marginAfter();
marginHeight = logicalGeometry.marginStart() + logicalGeometry.marginEnd();
}
borderBoxVisualRect.expand(-marginWidth, -marginHeight);
borderBoxVisualRect.move(marginLeft, marginTop);
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_rootLayoutBox(BoxTreeUpdater { flow }.build())
, m_layoutState(flow.view().layoutState())
, m_blockFormattingState(layoutState().ensureBlockFormattingState(rootLayoutBox()))
, m_inlineContentCache(layoutState().inlineContentCache(rootLayoutBox()))
, m_boxGeometryUpdater(flow.view().layoutState(), rootLayoutBox())
{
}
LineLayout::~LineLayout()
{
auto& rootRenderer = flow();
auto shouldPopulateBreakingPositionCache = [&] {
if (rootRenderer.document().renderTreeBeingDestroyed() || isDamaged())
return false;
return !m_inlineContentCache.inlineItems().isPopulatedFromCache();
};
if (shouldPopulateBreakingPositionCache())
Layout::InlineItemsBuilder::populateBreakingPositionCache(m_inlineContentCache.inlineItems().content(), rootRenderer.document());
clearInlineContent();
layoutState().destroyInlineContentCache(rootLayoutBox());
layoutState().destroyBlockFormattingState(rootLayoutBox());
m_boxGeometryUpdater.clear();
m_lineDamage = { };
m_rootLayoutBox = nullptr;
BoxTreeUpdater { rootRenderer }.tearDown();
}
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 (!renderer.layoutBox())
return false;
if (!renderer.layoutBox()->isInFormattingContextEstablishedBy(rootLayoutBox()))
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->inlineLayout();
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->inlineLayout();
return nullptr;
}
if (auto* container = blockContainer(renderer))
return container->inlineLayout();
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::updateFormattingContexGeometries(LayoutUnit availableLogicalWidth)
{
m_boxGeometryUpdater.setFormattingContextRootGeometry(availableLogicalWidth);
m_inlineContentConstraints = m_boxGeometryUpdater.formattingContextConstraints(availableLogicalWidth);
m_boxGeometryUpdater.setFormattingContextContentGeometry(m_inlineContentConstraints->horizontal().logicalWidth, { });
}
void LineLayout::updateStyle(const RenderObject& renderer)
{
BoxTreeUpdater::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, StyleDifference diff)
{
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, diff);
}
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() }.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.setFormattingContextContentGeometry({ }, 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 legacyLineClamp = layoutState.legacyLineClamp())
return Layout::BlockLayoutState::LineClamp { std::max(legacyLineClamp->maximumLineCount - legacyLineClamp->currentLineCount, static_cast<size_t>(0)), false, true };
if (auto lineClamp = layoutState.lineClamp())
return Layout::BlockLayoutState::LineClamp { lineClamp->maximumLines, lineClamp->shouldDiscardOverflow, false };
return { };
}
static inline Layout::BlockLayoutState::TextBoxTrim textBoxTrim(const RenderBlockFlow& rootRenderer)
{
auto textBoxTrim = rootRenderer.view().frameView().layoutContext().textBoxTrim();
if (!textBoxTrim)
return { };
auto textBoxTrimForIFC = Layout::BlockLayoutState::TextBoxTrim { };
auto isLineInverted = rootRenderer.writingMode().isLineInverted();
if (textBoxTrim->trimFirstFormattedLine)
textBoxTrimForIFC.add(isLineInverted ? Layout::BlockLayoutState::TextBoxTrimSide::End : Layout::BlockLayoutState::TextBoxTrimSide::Start);
if (textBoxTrim->lastFormattedLineRoot.get() == &rootRenderer)
textBoxTrimForIFC.add(isLineInverted ? Layout::BlockLayoutState::TextBoxTrimSide::Start : Layout::BlockLayoutState::TextBoxTrimSide::End);
return textBoxTrimForIFC;
}
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->writingMode().computedWritingMode() != rootRenderer.writingMode().computedWritingMode())
return { };
auto layoutOffset = layoutState.layoutOffset();
auto lineGridOffset = layoutState.lineGridOffset();
if (lineGrid->style().writingMode().isVertical()) {
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->writingMode().isVertical())
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(), m_inlineContentConstraints->containerRenderSize() };
};
auto parentBlockLayoutState = Layout::BlockLayoutState {
m_blockFormattingState.placedFloats(),
lineClamp(flow()),
textBoxTrim(flow()),
flow().style().textBoxEdge(),
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(), layoutResult.didDiscardContent);
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() }.build(WTFMove(layoutResult), ensureInlineContent(), m_lineDamage.get());
m_inlineContent->setClearGapBeforeFirstLine(inlineLayoutState.clearGapBeforeFirstLine());
m_inlineContent->setClearGapAfterLastLine(inlineLayoutState.clearGapAfterLastLine());
m_inlineContent->shrinkToFit();
m_inlineContentCache.inlineItems().shrinkToFit();
m_blockFormattingState.shrinkToFit();
// FIXME: These needs to be incorporated into the partial damage.
auto offsetAndGaps = m_inlineContent->firstLinePaginationOffset() + m_inlineContent->clearBeforeAfterGaps();
damagedRect.expand({ 0, offsetAndGaps });
return damagedRect;
}
void LineLayout::updateRenderTreePositions(const Vector<LineAdjustment>& lineAdjustments, const Layout::InlineLayoutState& inlineLayoutState, bool didDiscardContent)
{
if (!m_inlineContent && !didDiscardContent)
return;
auto& blockFlow = flow();
auto placedFloatsWritingMode = m_blockFormattingState.placedFloats().blockFormattingContextRoot().style().writingMode();
auto visualAdjustmentOffset = [&](auto lineIndex) {
if (lineAdjustments.isEmpty())
return LayoutSize { };
if (!placedFloatsWritingMode.isHorizontal())
return LayoutSize { lineAdjustments[lineIndex].offset, 0_lu };
return LayoutSize { 0_lu, lineAdjustments[lineIndex].offset };
};
if (m_inlineContent) {
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()));
}
}
UncheckedKeyHashMap<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 (didDiscardContent)
layoutBox.rendererForIntegration()->clearNeedsLayout();
if (!layoutBox.isFloatingPositioned() && !layoutBox.isOutOfFlowPositioned())
continue;
if (layoutBox.isLineBreakBox())
continue;
auto& renderer = downcast<RenderBox>(*layoutBox.rendererForIntegration());
auto& logicalGeometry = layoutState().geometryForBox(layoutBox);
if (layoutBox.isFloatingPositioned()) {
// FIXME: Find out what to do with discarded (see line-clamp) floats in render tree.
auto isInitialLetter = layoutBox.style().pseudoElementType() == PseudoId::FirstLetter;
auto& floatingObject = flow().insertFloatingObjectForIFC(renderer);
auto [marginBoxVisualRect, borderBoxVisualRect] = toMarginAndBorderBoxVisualRect(logicalGeometry, m_inlineContentConstraints->containerRenderSize(), placedFloatsWritingMode);
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.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;
}
}
}
FloatRect LineLayout::applySVGTextFragments(SVGTextFragmentMap&& fragmentMap)
{
auto& boxes = m_inlineContent->displayContent().boxes;
auto& lines = m_inlineContent->displayContent().lines;
auto& fragments = m_inlineContent->svgTextFragmentsForBoxes();
fragments.resize(m_inlineContent->displayContent().boxes.size());
FloatRect fullBoundaries;
struct Parent {
size_t index;
FloatRect boundaries;
};
Vector<Parent, 8> parentStack;
auto popParent = [&](const Layout::Box* parent) {
while (!parentStack.isEmpty() && &boxes[parentStack.last().index].layoutBox() != parent) {
auto boundaries = parentStack.last().boundaries;
boxes[parentStack.last().index].setRect(boundaries, boundaries);
parentStack.removeLast();
if (!parentStack.isEmpty())
parentStack.last().boundaries.unite(boundaries);
else
fullBoundaries = boundaries;
}
};
for (size_t i = 0; i < boxes.size(); ++i) {
popParent(&boxes[i].layoutBox().parent());
auto textBox = InlineIterator::svgTextBoxFor(*m_inlineContent, i);
if (!textBox) {
parentStack.append({ i, { } });
continue;
}
auto it = fragmentMap.find(makeKey(*textBox));
if (it != fragmentMap.end())
fragments[i] = WTFMove(it->value);
auto boundaries = textBox->calculateBoundariesIncludingSVGTransform();
boxes[i].setRect(boundaries, boundaries);
parentStack.last().boundaries.unite(boundaries);
}
popParent(nullptr);
// Move so the top-left text box is at (0, 0).
for (auto& box : boxes) {
box.moveHorizontally(-fullBoundaries.x());
box.moveVertically(-fullBoundaries.y());
}
if (lines.size())
lines[0].setLineBoxRectForSVGText(FloatRect { { }, fullBoundaries.size() });
return fullBoundaries;
}
void LineLayout::preparePlacedFloats()
{
auto& placedFloats = m_blockFormattingState.placedFloats();
placedFloats.clear();
if (!flow().containsFloats())
return;
auto placedFloatsWritingMode = placedFloats.blockFormattingContextRoot().style().writingMode();
auto placedFloatsIsLeftToRight = placedFloatsWritingMode.isLogicalLeftInlineStart();
auto isHorizontalWritingMode = placedFloatsWritingMode.isHorizontal();
for (auto& floatingObject : *flow().floatingObjectSet()) {
auto& visualRect = floatingObject->frameRect();
auto usedPosition = RenderStyle::usedFloat(floatingObject->renderer());
auto logicalPosition = (usedPosition == UsedFloat::Left) == placedFloatsIsLeftToRight
? Layout::PlacedFloats::Item::Position::Start
: Layout::PlacedFloats::Item::Position::End;
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 (!placedFloatsIsLeftToRight) {
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();
}
bool LineLayout::hasEllipsisInBlockDirectionOnLastFormattedLine() const
{
if (!m_inlineContent)
return false;
for (auto& line : makeReversedRange(m_inlineContent->displayContent().lines)) {
if (line.boxCount() == 1) {
// Out-of-flow content could initiate a line with no inline content.
continue;
}
auto lastFormattedLineEllipsis = line.ellipsis();
return lastFormattedLineEllipsis && lastFormattedLineEllipsis->type == InlineDisplay::Line::Ellipsis::Type::Block;
}
return false;
}
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].isFullyTruncatedInBlockDirection())
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 offsetAndGaps = m_inlineContent->firstLinePaginationOffset() + m_inlineContent->clearBeforeAfterGaps();
return LayoutUnit { contentHeight + offsetAndGaps };
}
LayoutUnit LineLayout::contentLogicalHeight() 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 offsetAndGaps = m_inlineContent->firstLinePaginationOffset() + m_inlineContent->clearBeforeAfterGaps();
return LayoutUnit { contentHeight + offsetAndGaps };
}
size_t LineLayout::lineCount() const
{
if (!m_inlineContent)
return 0;
if (!m_inlineContent->hasContent())
return 0;
auto& lines = m_inlineContent->displayContent().lines;
if (lines.isEmpty())
return 0;
// In some cases (trailing out-of-flow, non-contentful content after <br>) we produce last line with no content but root inline box only.
return lines.last().boxCount() > 1 ? lines.size() : lines.size() - 1;
}
bool LineLayout::hasInkOverflow() const
{
return m_inlineContent && m_inlineContent->hasInkOverflow();
}
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 (rootLayoutBox().writingMode().blockDirection()) {
case FlowDirection::TopToBottom:
case FlowDirection::BottomToTop:
return LayoutUnit { line.lineBoxTop() + line.baseline() };
case FlowDirection::LeftToRight:
return LayoutUnit { line.lineBoxLeft() + (line.lineBoxWidth() - line.baseline()) };
case FlowDirection::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 (rootLayoutBox().writingMode().blockDirection()) {
case FlowDirection::TopToBottom:
case FlowDirection::BottomToTop:
return LayoutUnit { lastLine.lineBoxTop() + lastLine.baseline() };
case FlowDirection::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 FlowDirection::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());
if (!adjustments.isEmpty()) {
adjustLinePositionsForPagination(*m_inlineContent, adjustments);
m_inlineContent->setFirstLinePaginationOffset(adjustments[0].offset);
}
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;
flow().addLayoutOverflow(LayoutRect { m_inlineContent->scrollableOverflow() });
if (!flow().hasNonVisibleOverflow())
flow().addVisualOverflow(LayoutRect { m_inlineContent->inkOverflow() });
}
InlineContent& LineLayout::ensureInlineContent()
{
if (!m_inlineContent)
m_inlineContent = makeUnique<InlineContent>(flow());
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 (rootLayoutBox().writingMode().blockDirection()) {
case FlowDirection::TopToBottom:
case FlowDirection::BottomToTop:
case FlowDirection::LeftToRight:
return firstBoxRect;
case FlowDirection::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 flow().writingMode().isHorizontal() ? borderBoxLogicalRect : borderBoxLogicalRect.transposedRect();
}
LayoutRect LineLayout::inkOverflowBoundingBoxRectFor(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;
}
static LayoutPoint flippedContentOffsetIfNeeded(const RenderBlockFlow& root, const RenderBox& childRenderer, LayoutPoint contentOffset)
{
if (root.writingMode().isBlockFlipped())
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, flow() }.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(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 = flow().writingMode().isHorizontal();
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 = BoxTreeUpdater { flow() }.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(BoxTreeUpdater { flow() }.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;
}
BoxTreeUpdater::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.inlineLayout())
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
}
}
|