forked from qt/qtwebkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRenderFlowThread.cpp
More file actions
1518 lines (1245 loc) · 59.2 KB
/
Copy pathRenderFlowThread.cpp
File metadata and controls
1518 lines (1245 loc) · 59.2 KB
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
/*
* Copyright (C) 2011 Adobe Systems Incorporated. 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 THE COPYRIGHT HOLDER "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 THE COPYRIGHT HOLDER 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 "RenderFlowThread.h"
#include "FlowThreadController.h"
#include "HitTestRequest.h"
#include "HitTestResult.h"
#include "InlineElementBox.h"
#include "Node.h"
#include "PODIntervalTree.h"
#include "PaintInfo.h"
#include "RenderBoxRegionInfo.h"
#include "RenderInline.h"
#include "RenderLayer.h"
#include "RenderLayerCompositor.h"
#include "RenderNamedFlowFragment.h"
#include "RenderNamedFlowThread.h"
#include "RenderRegion.h"
#include "RenderTheme.h"
#include "RenderView.h"
#include "TransformState.h"
#include "WebKitNamedFlow.h"
#include <wtf/StackStats.h>
namespace WebCore {
RenderFlowThread::RenderFlowThread(Document& document, Ref<RenderStyle>&& style)
: RenderBlockFlow(document, WTFMove(style))
, m_previousRegionCount(0)
, m_autoLogicalHeightRegionsCount(0)
, m_currentRegionMaintainer(nullptr)
, m_regionsInvalidated(false)
, m_regionsHaveUniformLogicalWidth(true)
, m_regionsHaveUniformLogicalHeight(true)
, m_pageLogicalSizeChanged(false)
, m_layoutPhase(LayoutPhaseMeasureContent)
, m_needsTwoPhasesLayout(false)
, m_layersToRegionMappingsDirty(true)
{
setIsRenderFlowThread(true);
setFlowThreadState(InsideOutOfFlowThread);
}
Ref<RenderStyle> RenderFlowThread::createFlowThreadStyle(RenderStyle* parentStyle)
{
auto newStyle = RenderStyle::create();
newStyle.get().inheritFrom(parentStyle);
newStyle.get().setDisplay(BLOCK);
newStyle.get().setPosition(AbsolutePosition);
newStyle.get().setZIndex(0);
newStyle.get().setLeft(Length(0, Fixed));
newStyle.get().setTop(Length(0, Fixed));
newStyle.get().setWidth(Length(100, Percent));
newStyle.get().setHeight(Length(100, Percent));
newStyle.get().fontCascade().update(nullptr);
return newStyle;
}
void RenderFlowThread::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
{
RenderBlockFlow::styleDidChange(diff, oldStyle);
if (oldStyle && oldStyle->writingMode() != style().writingMode())
invalidateRegions();
}
void RenderFlowThread::removeFlowChildInfo(RenderObject* child)
{
if (is<RenderBlockFlow>(*child))
removeLineRegionInfo(downcast<RenderBlockFlow>(child));
if (is<RenderBox>(*child))
removeRenderBoxRegionInfo(downcast<RenderBox>(child));
}
void RenderFlowThread::removeRegionFromThread(RenderRegion* renderRegion)
{
ASSERT(renderRegion);
m_regionList.remove(renderRegion);
}
void RenderFlowThread::invalidateRegions(MarkingBehavior markingParents)
{
ASSERT(!inFinalLayoutPhase());
if (m_regionsInvalidated) {
ASSERT(selfNeedsLayout());
return;
}
m_regionRangeMap.clear();
m_breakBeforeToRegionMap.clear();
m_breakAfterToRegionMap.clear();
if (m_layerToRegionMap)
m_layerToRegionMap->clear();
if (m_regionToLayerListMap)
m_regionToLayerListMap->clear();
if (m_lineToRegionMap)
m_lineToRegionMap->clear();
m_layersToRegionMappingsDirty = true;
setNeedsLayout(markingParents);
m_regionsInvalidated = true;
}
void RenderFlowThread::validateRegions()
{
if (m_regionsInvalidated) {
m_regionsInvalidated = false;
m_regionsHaveUniformLogicalWidth = true;
m_regionsHaveUniformLogicalHeight = true;
if (hasRegions()) {
LayoutUnit previousRegionLogicalWidth = 0;
LayoutUnit previousRegionLogicalHeight = 0;
bool firstRegionVisited = false;
for (auto& region : m_regionList) {
ASSERT(!region->needsLayout() || region->isRenderRegionSet());
region->deleteAllRenderBoxRegionInfo();
// In the measure content layout phase we need to initialize the computedAutoHeight for auto-height regions.
// See initializeRegionsComputedAutoHeight for the explanation.
// Also, if we have auto-height regions we can't assume m_regionsHaveUniformLogicalHeight to be true in the first phase
// because the auto-height regions don't have their height computed yet.
if (inMeasureContentLayoutPhase() && region->hasAutoLogicalHeight()) {
auto& namedFlowFragment = downcast<RenderNamedFlowFragment>(*region);
namedFlowFragment.setComputedAutoHeight(namedFlowFragment.maxPageLogicalHeight());
m_regionsHaveUniformLogicalHeight = false;
}
LayoutUnit regionLogicalWidth = region->pageLogicalWidth();
LayoutUnit regionLogicalHeight = region->pageLogicalHeight();
if (!firstRegionVisited)
firstRegionVisited = true;
else {
if (m_regionsHaveUniformLogicalWidth && previousRegionLogicalWidth != regionLogicalWidth)
m_regionsHaveUniformLogicalWidth = false;
if (m_regionsHaveUniformLogicalHeight && previousRegionLogicalHeight != regionLogicalHeight)
m_regionsHaveUniformLogicalHeight = false;
}
previousRegionLogicalWidth = regionLogicalWidth;
}
setRegionRangeForBox(this, m_regionList.first(), m_regionList.last());
}
}
updateLogicalWidth(); // Called to get the maximum logical width for the region.
updateRegionsFlowThreadPortionRect();
}
void RenderFlowThread::layout()
{
StackStats::LayoutCheckPoint layoutCheckPoint;
m_pageLogicalSizeChanged = m_regionsInvalidated && everHadLayout();
// In case this is the second pass of the measure content phase we need to update the auto-height regions to their initial value.
// If the region chain was invalidated this will happen anyway.
if (!m_regionsInvalidated && inMeasureContentLayoutPhase())
initializeRegionsComputedAutoHeight();
// This is the first phase of the layout and because we have auto-height regions we'll need a second
// pass to update the flow with the computed auto-height regions.
// It's also possible to need a secondary layout if the overflow computation invalidated the region chain (e.g. overflow: auto scrollbars
// shrunk some regions) so repropagation is required.
m_needsTwoPhasesLayout = (inMeasureContentLayoutPhase() && hasAutoLogicalHeightRegions()) || (inOverflowLayoutPhase() && m_regionsInvalidated);
validateRegions();
RenderBlockFlow::layout();
m_pageLogicalSizeChanged = false;
// If there are children layers in the RenderFlowThread then we need to make sure that the
// composited children layers will land in the right RenderRegions. Also, the parent RenderRegions
// will get RenderLayers and become composited as needed.
// Note that there's no need to do so for the inline multi-column as we are not moving layers into different
// containers, but just adjusting the position of the RenderLayerBacking.
if (!m_needsTwoPhasesLayout) {
// If we have layers that moved from one region to another, we trigger
// a composited layers rebuild in here to make sure that the regions will collect the right layers.
if (updateAllLayerToRegionMappings())
layer()->compositor().setCompositingLayersNeedRebuild();
}
}
bool RenderFlowThread::hasCompositingRegionDescendant() const
{
for (auto& region : m_regionList) {
if (downcast<RenderNamedFlowFragment>(*region).layerOwner().layer()->hasCompositingDescendant())
return true;
}
return false;
}
const RenderLayerList* RenderFlowThread::getLayerListForRegion(RenderNamedFlowFragment* region) const
{
ASSERT(m_regionToLayerListMap);
auto iterator = m_regionToLayerListMap->find(region);
return iterator == m_regionToLayerListMap->end() ? nullptr : &iterator->value;
}
RenderNamedFlowFragment* RenderFlowThread::regionForCompositedLayer(RenderLayer& childLayer) const
{
if (childLayer.renderer().fixedPositionedWithNamedFlowContainingBlock())
return nullptr;
if (childLayer.renderBox()) {
RenderRegion* startRegion = nullptr;
RenderRegion* endRegion = nullptr;
if (getRegionRangeForBox(childLayer.renderBox(), startRegion, endRegion))
return downcast<RenderNamedFlowFragment>(startRegion);
}
// FIXME: remove this when we'll have region ranges for inlines as well.
LayoutPoint flowThreadOffset = flooredLayoutPoint(childLayer.renderer().localToContainerPoint(LayoutPoint(), this, ApplyContainerFlip));
return downcast<RenderNamedFlowFragment>(regionAtBlockOffset(0, flipForWritingMode(isHorizontalWritingMode() ? flowThreadOffset.y() : flowThreadOffset.x()), true));
}
RenderNamedFlowFragment* RenderFlowThread::cachedRegionForCompositedLayer(RenderLayer& childLayer) const
{
if (!m_layerToRegionMap) {
ASSERT(needsLayout());
ASSERT(m_layersToRegionMappingsDirty);
return nullptr;
}
RenderNamedFlowFragment* namedFlowFragment = m_layerToRegionMap->get(&childLayer);
ASSERT(!namedFlowFragment || m_regionList.contains(namedFlowFragment));
return namedFlowFragment;
}
void RenderFlowThread::updateLayerToRegionMappings(RenderLayer& layer, LayerToRegionMap& layerToRegionMap, RegionToLayerListMap& regionToLayerListMap, bool& needsLayerUpdate)
{
RenderNamedFlowFragment* region = regionForCompositedLayer(layer);
if (!needsLayerUpdate) {
// Figure out if we moved this layer from a region to the other.
RenderNamedFlowFragment* previousRegion = cachedRegionForCompositedLayer(layer);
if (previousRegion != region)
needsLayerUpdate = true;
}
if (!region)
return;
layerToRegionMap.set(&layer, region);
auto iterator = regionToLayerListMap.find(region);
RenderLayerList& list = iterator == regionToLayerListMap.end() ? regionToLayerListMap.set(region, RenderLayerList()).iterator->value : iterator->value;
ASSERT(!list.contains(&layer));
list.append(&layer);
}
bool RenderFlowThread::updateAllLayerToRegionMappings()
{
if (!collectsGraphicsLayersUnderRegions())
return false;
// If the RenderFlowThread had a z-index layer update, then we need to update the composited layers too.
bool needsLayerUpdate = layer()->isDirtyRenderFlowThread() || m_layersToRegionMappingsDirty || !m_layerToRegionMap.get();
layer()->updateLayerListsIfNeeded();
LayerToRegionMap layerToRegionMap;
RegionToLayerListMap regionToLayerListMap;
RenderLayerList* lists[] = { layer()->negZOrderList(), layer()->normalFlowList(), layer()->posZOrderList() };
for (size_t listIndex = 0; listIndex < sizeof(lists) / sizeof(lists[0]); ++listIndex) {
if (RenderLayerList* list = lists[listIndex]) {
for (size_t i = 0, listSize = list->size(); i < listSize; ++i)
updateLayerToRegionMappings(*list->at(i), layerToRegionMap, regionToLayerListMap, needsLayerUpdate);
}
}
if (needsLayerUpdate) {
if (!m_layerToRegionMap)
m_layerToRegionMap = std::make_unique<LayerToRegionMap>();
m_layerToRegionMap->swap(layerToRegionMap);
if (!m_regionToLayerListMap)
m_regionToLayerListMap = std::make_unique<RegionToLayerListMap>();
m_regionToLayerListMap->swap(regionToLayerListMap);
}
m_layersToRegionMappingsDirty = false;
return needsLayerUpdate;
}
bool RenderFlowThread::collectsGraphicsLayersUnderRegions() const
{
// We only need to map layers to regions for named flow threads.
// Multi-column threads are displayed on top of the regions and do not require
// distributing the layers.
return false;
}
void RenderFlowThread::updateLogicalWidth()
{
LayoutUnit logicalWidth = initialLogicalWidth();
for (auto& region : m_regionList) {
ASSERT(!region->needsLayout() || region->isRenderRegionSet());
logicalWidth = std::max(region->pageLogicalWidth(), logicalWidth);
}
setLogicalWidth(logicalWidth);
// If the regions have non-uniform logical widths, then insert inset information for the RenderFlowThread.
for (auto& region : m_regionList) {
LayoutUnit regionLogicalWidth = region->pageLogicalWidth();
LayoutUnit logicalLeft = style().direction() == LTR ? LayoutUnit() : logicalWidth - regionLogicalWidth;
region->setRenderBoxRegionInfo(this, logicalLeft, regionLogicalWidth, false);
}
}
void RenderFlowThread::computeLogicalHeight(LayoutUnit, LayoutUnit logicalTop, LogicalExtentComputedValues& computedValues) const
{
computedValues.m_position = logicalTop;
computedValues.m_extent = 0;
const LayoutUnit maxFlowSize = RenderFlowThread::maxLogicalHeight();
for (auto& region : m_regionList) {
ASSERT(!region->needsLayout() || region->isRenderRegionSet());
LayoutUnit distanceToMaxSize = maxFlowSize - computedValues.m_extent;
computedValues.m_extent += std::min(distanceToMaxSize, region->logicalHeightOfAllFlowThreadContent());
// If we reached the maximum size there's no point in going further.
if (computedValues.m_extent == maxFlowSize)
return;
}
}
bool RenderFlowThread::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, HitTestAction hitTestAction)
{
if (hitTestAction == HitTestBlockBackground)
return false;
return RenderBlockFlow::nodeAtPoint(request, result, locationInContainer, accumulatedOffset, hitTestAction);
}
bool RenderFlowThread::shouldRepaint(const LayoutRect& r) const
{
if (view().printing() || r.isEmpty())
return false;
return true;
}
void RenderFlowThread::repaintRectangleInRegions(const LayoutRect& repaintRect) const
{
if (!shouldRepaint(repaintRect) || !hasValidRegionInfo())
return;
LayoutStateDisabler layoutStateDisabler(view()); // We can't use layout state to repaint, since the regions are somewhere else.
for (auto& region : m_regionList)
region->repaintFlowThreadContent(repaintRect);
}
RenderRegion* RenderFlowThread::regionAtBlockOffset(const RenderBox* clampBox, LayoutUnit offset, bool extendLastRegion) const
{
ASSERT(!m_regionsInvalidated);
if (m_regionList.isEmpty())
return nullptr;
if (m_regionList.size() == 1 && extendLastRegion)
return m_regionList.first();
if (offset <= 0)
return clampBox ? clampBox->clampToStartAndEndRegions(m_regionList.first()) : m_regionList.first();
RegionSearchAdapter adapter(offset);
m_regionIntervalTree.allOverlapsWithAdapter<RegionSearchAdapter>(adapter);
// If no region was found, the offset is in the flow thread overflow.
// The last region will contain the offset if extendLastRegion is set or if the last region is a set.
if (!adapter.result() && (extendLastRegion || m_regionList.last()->isRenderRegionSet()))
return clampBox ? clampBox->clampToStartAndEndRegions(m_regionList.last()) : m_regionList.last();
RenderRegion* region = adapter.result();
if (!clampBox)
return region;
return region ? clampBox->clampToStartAndEndRegions(region) : nullptr;
}
LayoutPoint RenderFlowThread::adjustedPositionRelativeToOffsetParent(const RenderBoxModelObject& boxModelObject, const LayoutPoint& startPoint) const
{
LayoutPoint referencePoint = startPoint;
const RenderBlock* objContainingBlock = boxModelObject.containingBlock();
// FIXME: This needs to be adapted for different writing modes inside the flow thread.
RenderRegion* startRegion = regionAtBlockOffset(objContainingBlock, referencePoint.y());
if (startRegion) {
// Take into account the offset coordinates of the region.
RenderBoxModelObject* startRegionBox = is<RenderNamedFlowFragment>(*startRegion) ? downcast<RenderBoxModelObject>(startRegion->parent()) : startRegion;
RenderBoxModelObject* currObject = startRegionBox;
RenderBoxModelObject* currOffsetParent;
while ((currOffsetParent = currObject->offsetParent())) {
referencePoint.move(currObject->offsetLeft(), currObject->offsetTop());
// Since we're looking for the offset relative to the body, we must also
// take into consideration the borders of the region's offsetParent.
if (is<RenderBox>(*currOffsetParent) && !currOffsetParent->isBody())
referencePoint.move(downcast<RenderBox>(*currOffsetParent).borderLeft(), downcast<RenderBox>(*currOffsetParent).borderTop());
currObject = currOffsetParent;
}
// We need to check if any of this box's containing blocks start in a different region
// and if so, drop the object's top position (which was computed relative to its containing block
// and is no longer valid) and recompute it using the region in which it flows as reference.
bool wasComputedRelativeToOtherRegion = false;
while (objContainingBlock && !is<RenderView>(*objContainingBlock) && !objContainingBlock->isRenderNamedFlowThread()) {
// Check if this object is in a different region.
RenderRegion* parentStartRegion = nullptr;
RenderRegion* parentEndRegion = nullptr;
if (getRegionRangeForBox(objContainingBlock, parentStartRegion, parentEndRegion) && parentStartRegion != startRegion) {
wasComputedRelativeToOtherRegion = true;
break;
}
objContainingBlock = objContainingBlock->containingBlock();
}
if (wasComputedRelativeToOtherRegion) {
if (is<RenderBox>(boxModelObject)) {
// Use borderBoxRectInRegion to account for variations such as percentage margins.
LayoutRect borderBoxRect = downcast<RenderBox>(boxModelObject).borderBoxRectInRegion(startRegion, RenderBox::DoNotCacheRenderBoxRegionInfo);
referencePoint.move(borderBoxRect.location().x(), 0);
}
// Get the logical top coordinate of the current object.
LayoutUnit top = 0;
if (is<RenderBlock>(boxModelObject))
top = downcast<RenderBlock>(boxModelObject).offsetFromLogicalTopOfFirstPage();
else {
if (boxModelObject.containingBlock())
top = boxModelObject.containingBlock()->offsetFromLogicalTopOfFirstPage();
if (is<RenderBox>(boxModelObject))
top += downcast<RenderBox>(boxModelObject).topLeftLocation().y();
else if (is<RenderInline>(boxModelObject))
top -= downcast<RenderInline>(boxModelObject).borderTop();
}
// Get the logical top of the region this object starts in
// and compute the object's top, relative to the region's top.
LayoutUnit regionLogicalTop = startRegion->pageLogicalTopForOffset(top);
LayoutUnit topRelativeToRegion = top - regionLogicalTop;
referencePoint.setY(startRegionBox->offsetTop() + topRelativeToRegion);
// Since the top has been overriden, check if the
// relative/sticky positioning must be reconsidered.
if (boxModelObject.isRelPositioned())
referencePoint.move(0, boxModelObject.relativePositionOffset().height());
else if (boxModelObject.isStickyPositioned())
referencePoint.move(0, boxModelObject.stickyPositionOffset().height());
}
// Since we're looking for the offset relative to the body, we must also
// take into consideration the borders of the region.
referencePoint.move(startRegionBox->borderLeft(), startRegionBox->borderTop());
}
return referencePoint;
}
LayoutUnit RenderFlowThread::pageLogicalTopForOffset(LayoutUnit offset) const
{
RenderRegion* region = regionAtBlockOffset(0, offset, false);
return region ? region->pageLogicalTopForOffset(offset) : LayoutUnit();
}
LayoutUnit RenderFlowThread::pageLogicalWidthForOffset(LayoutUnit offset) const
{
RenderRegion* region = regionAtBlockOffset(0, offset, true);
return region ? region->pageLogicalWidth() : contentLogicalWidth();
}
LayoutUnit RenderFlowThread::pageLogicalHeightForOffset(LayoutUnit offset) const
{
RenderRegion* region = regionAtBlockOffset(0, offset, false);
if (!region)
return 0;
return region->pageLogicalHeight();
}
LayoutUnit RenderFlowThread::pageRemainingLogicalHeightForOffset(LayoutUnit offset, PageBoundaryRule pageBoundaryRule) const
{
RenderRegion* region = regionAtBlockOffset(0, offset, false);
if (!region)
return 0;
LayoutUnit pageLogicalTop = region->pageLogicalTopForOffset(offset);
LayoutUnit pageLogicalHeight = region->pageLogicalHeight();
LayoutUnit pageLogicalBottom = pageLogicalTop + pageLogicalHeight;
LayoutUnit remainingHeight = pageLogicalBottom - offset;
if (pageBoundaryRule == IncludePageBoundary) {
// If IncludePageBoundary is set, the line exactly on the top edge of a
// region will act as being part of the previous region.
remainingHeight = intMod(remainingHeight, pageLogicalHeight);
}
return remainingHeight;
}
RenderRegion* RenderFlowThread::mapFromFlowToRegion(TransformState& transformState) const
{
if (!hasValidRegionInfo())
return nullptr;
RenderRegion* renderRegion = currentRegion();
if (!renderRegion) {
LayoutRect boxRect = transformState.mappedQuad().enclosingBoundingBox();
flipForWritingMode(boxRect);
LayoutPoint center = boxRect.center();
renderRegion = regionAtBlockOffset(this, isHorizontalWritingMode() ? center.y() : center.x(), true);
if (!renderRegion)
return nullptr;
}
LayoutRect flippedRegionRect(renderRegion->flowThreadPortionRect());
flipForWritingMode(flippedRegionRect);
transformState.move(renderRegion->contentBoxRect().location() - flippedRegionRect.location());
return renderRegion;
}
void RenderFlowThread::removeRenderBoxRegionInfo(RenderBox* box)
{
if (!hasRegions())
return;
// If the region chain was invalidated the next layout will clear the box information from all the regions.
if (m_regionsInvalidated) {
ASSERT(selfNeedsLayout());
return;
}
RenderRegion* startRegion = nullptr;
RenderRegion* endRegion = nullptr;
if (getRegionRangeForBox(box, startRegion, endRegion)) {
for (auto it = m_regionList.find(startRegion), end = m_regionList.end(); it != end; ++it) {
RenderRegion* region = *it;
region->removeRenderBoxRegionInfo(box);
if (region == endRegion)
break;
}
}
#ifndef NDEBUG
// We have to make sure we did not leave any RenderBoxRegionInfo attached.
for (auto& region : m_regionList)
ASSERT(!region->renderBoxRegionInfo(box));
#endif
m_regionRangeMap.remove(box);
}
void RenderFlowThread::removeLineRegionInfo(const RenderBlockFlow* blockFlow)
{
if (!m_lineToRegionMap || blockFlow->lineLayoutPath() == SimpleLinesPath)
return;
for (RootInlineBox* curr = blockFlow->firstRootBox(); curr; curr = curr->nextRootBox()) {
if (m_lineToRegionMap->contains(curr))
m_lineToRegionMap->remove(curr);
}
ASSERT_WITH_SECURITY_IMPLICATION(checkLinesConsistency(blockFlow));
}
void RenderFlowThread::logicalWidthChangedInRegionsForBlock(const RenderBlock* block, bool& relayoutChildren)
{
if (!hasValidRegionInfo()) {
// FIXME: Remove once we stop laying out flow threads without regions.
// If we had regions but don't any more, relayout the children because the code below
// can't properly detect this scenario.
relayoutChildren |= previousRegionCountChanged();
return;
}
auto it = m_regionRangeMap.find(block);
if (it == m_regionRangeMap.end())
return;
RenderRegionRange& range = it->value;
bool rangeInvalidated = range.rangeInvalidated();
range.clearRangeInvalidated();
// If there will be a relayout anyway skip the next steps because they only verify
// the state of the ranges.
if (relayoutChildren)
return;
// Not necessary for the flow thread, since we already computed the correct info for it.
// If the regions have changed invalidate the children.
if (block == this) {
relayoutChildren = m_pageLogicalSizeChanged;
return;
}
RenderRegion* startRegion = nullptr;
RenderRegion* endRegion = nullptr;
if (!getRegionRangeForBox(block, startRegion, endRegion))
return;
for (auto it = m_regionList.find(startRegion), end = m_regionList.end(); it != end; ++it) {
RenderRegion* region = *it;
ASSERT(!region->needsLayout() || region->isRenderRegionSet());
// We have no information computed for this region so we need to do it.
std::unique_ptr<RenderBoxRegionInfo> oldInfo = region->takeRenderBoxRegionInfo(block);
if (!oldInfo) {
relayoutChildren = rangeInvalidated;
return;
}
LayoutUnit oldLogicalWidth = oldInfo->logicalWidth();
RenderBoxRegionInfo* newInfo = block->renderBoxRegionInfo(region);
if (!newInfo || newInfo->logicalWidth() != oldLogicalWidth) {
relayoutChildren = true;
return;
}
if (region == endRegion)
break;
}
}
LayoutUnit RenderFlowThread::contentLogicalWidthOfFirstRegion() const
{
RenderRegion* firstValidRegionInFlow = firstRegion();
if (!firstValidRegionInFlow)
return 0;
return isHorizontalWritingMode() ? firstValidRegionInFlow->contentWidth() : firstValidRegionInFlow->contentHeight();
}
LayoutUnit RenderFlowThread::contentLogicalHeightOfFirstRegion() const
{
RenderRegion* firstValidRegionInFlow = firstRegion();
if (!firstValidRegionInFlow)
return 0;
return isHorizontalWritingMode() ? firstValidRegionInFlow->contentHeight() : firstValidRegionInFlow->contentWidth();
}
LayoutUnit RenderFlowThread::contentLogicalLeftOfFirstRegion() const
{
RenderRegion* firstValidRegionInFlow = firstRegion();
if (!firstValidRegionInFlow)
return 0;
return isHorizontalWritingMode() ? firstValidRegionInFlow->flowThreadPortionRect().x() : firstValidRegionInFlow->flowThreadPortionRect().y();
}
RenderRegion* RenderFlowThread::firstRegion() const
{
if (!hasRegions())
return nullptr;
return m_regionList.first();
}
RenderRegion* RenderFlowThread::lastRegion() const
{
if (!hasRegions())
return nullptr;
return m_regionList.last();
}
void RenderFlowThread::clearRenderBoxRegionInfoAndCustomStyle(const RenderBox* box,
const RenderRegion* newStartRegion, const RenderRegion* newEndRegion,
const RenderRegion* oldStartRegion, const RenderRegion* oldEndRegion)
{
ASSERT(newStartRegion && newEndRegion && oldStartRegion && oldEndRegion);
bool insideOldRegionRange = false;
bool insideNewRegionRange = false;
for (auto& region : m_regionList) {
if (oldStartRegion == region)
insideOldRegionRange = true;
if (newStartRegion == region)
insideNewRegionRange = true;
if (!(insideOldRegionRange && insideNewRegionRange)) {
if (is<RenderNamedFlowFragment>(*region))
downcast<RenderNamedFlowFragment>(*region).clearObjectStyleInRegion(box);
if (region->renderBoxRegionInfo(box))
region->removeRenderBoxRegionInfo(box);
}
if (oldEndRegion == region)
insideOldRegionRange = false;
if (newEndRegion == region)
insideNewRegionRange = false;
}
}
void RenderFlowThread::setRegionRangeForBox(const RenderBox* box, RenderRegion* startRegion, RenderRegion* endRegion)
{
ASSERT(hasRegions());
ASSERT(startRegion && endRegion && startRegion->flowThread() == this && endRegion->flowThread() == this);
auto it = m_regionRangeMap.find(box);
if (it == m_regionRangeMap.end()) {
m_regionRangeMap.set(box, RenderRegionRange(startRegion, endRegion));
return;
}
// If nothing changed, just bail.
RenderRegionRange& range = it->value;
if (range.startRegion() == startRegion && range.endRegion() == endRegion)
return;
clearRenderBoxRegionInfoAndCustomStyle(box, startRegion, endRegion, range.startRegion(), range.endRegion());
range.setRange(startRegion, endRegion);
}
bool RenderFlowThread::hasCachedRegionRangeForBox(const RenderBox* box) const
{
ASSERT(box);
return m_regionRangeMap.contains(box);
}
bool RenderFlowThread::getRegionRangeForBoxFromCachedInfo(const RenderBox* box, RenderRegion*& startRegion, RenderRegion*& endRegion) const
{
ASSERT(box);
ASSERT(hasValidRegionInfo());
ASSERT((startRegion == nullptr) && (endRegion == nullptr));
auto it = m_regionRangeMap.find(box);
if (it != m_regionRangeMap.end()) {
const RenderRegionRange& range = it->value;
startRegion = range.startRegion();
endRegion = range.endRegion();
ASSERT(m_regionList.contains(startRegion) && m_regionList.contains(endRegion));
return true;
}
return false;
}
bool RenderFlowThread::getRegionRangeForBox(const RenderBox* box, RenderRegion*& startRegion, RenderRegion*& endRegion) const
{
ASSERT(box);
startRegion = endRegion = nullptr;
if (!hasValidRegionInfo()) // We clear the ranges when we invalidate the regions.
return false;
if (m_regionList.size() == 1) {
startRegion = endRegion = m_regionList.first();
return true;
}
if (getRegionRangeForBoxFromCachedInfo(box, startRegion, endRegion))
return true;
return false;
}
bool RenderFlowThread::computedRegionRangeForBox(const RenderBox* box, RenderRegion*& startRegion, RenderRegion*& endRegion) const
{
ASSERT(box);
startRegion = endRegion = nullptr;
if (!hasValidRegionInfo()) // We clear the ranges when we invalidate the regions.
return false;
if (getRegionRangeForBox(box, startRegion, endRegion))
return true;
// Search the region range using the information provided by the
// containing block chain.
RenderBox* cb = const_cast<RenderBox*>(box);
while (!cb->isRenderFlowThread()) {
InlineElementBox* boxWrapper = cb->inlineBoxWrapper();
if (boxWrapper && boxWrapper->root().containingRegion()) {
startRegion = endRegion = boxWrapper->root().containingRegion();
ASSERT(m_regionList.contains(startRegion));
return true;
}
// FIXME: Use the containingBlock() value once we patch all the layout systems to be region range aware
// (e.g. if we use containingBlock() the shadow controls of a video element won't get the range from the
// video box because it's not a block; they need to be patched separately).
ASSERT(cb->parent());
cb = &cb->parent()->enclosingBox();
ASSERT(cb);
// If a box doesn't have a cached region range it usually means the box belongs to a line so startRegion should be equal with endRegion.
// FIXME: Find the cases when this startRegion should not be equal with endRegion and make sure these boxes have cached region ranges.
if (hasCachedRegionRangeForBox(cb)) {
startRegion = endRegion = regionAtBlockOffset(cb, box->offsetFromLogicalTopOfFirstPage(), true);
return true;
}
}
ASSERT_NOT_REACHED();
return false;
}
bool RenderFlowThread::regionInRange(const RenderRegion* targetRegion, const RenderRegion* startRegion, const RenderRegion* endRegion) const
{
ASSERT(targetRegion);
for (auto it = m_regionList.find(const_cast<RenderRegion*>(startRegion)), end = m_regionList.end(); it != end; ++it) {
const RenderRegion* currRegion = *it;
if (targetRegion == currRegion)
return true;
if (currRegion == endRegion)
break;
}
return false;
}
bool RenderFlowThread::objectShouldFragmentInFlowRegion(const RenderObject* object, const RenderRegion* region) const
{
ASSERT(object);
ASSERT(region);
RenderFlowThread* flowThread = object->flowThreadContainingBlock();
if (flowThread != this)
return false;
if (!m_regionList.contains(const_cast<RenderRegion*>(region)))
return false;
RenderRegion* enclosingBoxStartRegion = nullptr;
RenderRegion* enclosingBoxEndRegion = nullptr;
// If the box has no range, do not check regionInRange. Boxes inside inlines do not get ranges.
// Instead, the containing RootInlineBox will abort when trying to paint inside the wrong region.
if (computedRegionRangeForBox(&object->enclosingBox(), enclosingBoxStartRegion, enclosingBoxEndRegion)
&& !regionInRange(region, enclosingBoxStartRegion, enclosingBoxEndRegion))
return false;
return object->isBox() || object->isRenderInline();
}
bool RenderFlowThread::objectInFlowRegion(const RenderObject* object, const RenderRegion* region) const
{
ASSERT(object);
ASSERT(region);
RenderFlowThread* flowThread = object->flowThreadContainingBlock();
if (flowThread != this)
return false;
if (!m_regionList.contains(const_cast<RenderRegion*>(region)))
return false;
RenderRegion* enclosingBoxStartRegion = nullptr;
RenderRegion* enclosingBoxEndRegion = nullptr;
if (!getRegionRangeForBox(&object->enclosingBox(), enclosingBoxStartRegion, enclosingBoxEndRegion))
return false;
if (!regionInRange(region, enclosingBoxStartRegion, enclosingBoxEndRegion))
return false;
if (object->isBox())
return true;
LayoutRect objectABBRect = object->absoluteBoundingBoxRect(true);
if (!objectABBRect.width())
objectABBRect.setWidth(1);
if (!objectABBRect.height())
objectABBRect.setHeight(1);
if (objectABBRect.intersects(region->absoluteBoundingBoxRect(true)))
return true;
if (region == lastRegion()) {
// If the object does not intersect any of the enclosing box regions
// then the object is in last region.
for (auto it = m_regionList.find(enclosingBoxStartRegion), end = m_regionList.end(); it != end; ++it) {
const RenderRegion* currRegion = *it;
if (currRegion == region)
break;
if (objectABBRect.intersects(currRegion->absoluteBoundingBoxRect(true)))
return false;
}
return true;
}
return false;
}
#ifndef NDEBUG
bool RenderFlowThread::isAutoLogicalHeightRegionsCountConsistent() const
{
unsigned autoLogicalHeightRegions = 0;
for (const auto& region : m_regionList) {
if (region->hasAutoLogicalHeight())
autoLogicalHeightRegions++;
}
return autoLogicalHeightRegions == m_autoLogicalHeightRegionsCount;
}
#endif
#if !ASSERT_WITH_SECURITY_IMPLICATION_DISABLED
bool RenderFlowThread::checkLinesConsistency(const RenderBlockFlow* removedBlock) const
{
if (!m_lineToRegionMap)
return true;
for (auto& linePair : *m_lineToRegionMap.get()) {
const RootInlineBox* line = linePair.key;
RenderRegion* region = linePair.value;
if (&line->blockFlow() == removedBlock)
return false;
if (line->blockFlow().flowThreadState() == NotInsideFlowThread)
return false;
if (!m_regionList.contains(region))
return false;
}
return true;
}
#endif
void RenderFlowThread::clearLinesToRegionMap()
{
if (m_lineToRegionMap)
m_lineToRegionMap->clear();
}
void RenderFlowThread::deleteLines()
{
clearLinesToRegionMap();
RenderBlockFlow::deleteLines();
}
void RenderFlowThread::willBeDestroyed()
{
clearLinesToRegionMap();
RenderBlockFlow::willBeDestroyed();
}
// During the measure content layout phase of the named flow the regions are initialized with a height equal to their max-height.
// This way unforced breaks are automatically placed when a region is full and the content height/position correctly estimated.
// Also, the region where a forced break falls is exactly the region found at the forced break offset inside the flow content.
void RenderFlowThread::initializeRegionsComputedAutoHeight(RenderRegion* startRegion)
{
ASSERT(inMeasureContentLayoutPhase());
if (!hasAutoLogicalHeightRegions())
return;
for (auto regionIter = startRegion ? m_regionList.find(startRegion) : m_regionList.begin(), end = m_regionList.end(); regionIter != end; ++regionIter) {
RenderRegion& region = **regionIter;
if (region.hasAutoLogicalHeight()) {
auto& namedFlowFragment = downcast<RenderNamedFlowFragment>(region);
namedFlowFragment.setComputedAutoHeight(namedFlowFragment.maxPageLogicalHeight());
}
}
}
void RenderFlowThread::markAutoLogicalHeightRegionsForLayout()
{
ASSERT(hasAutoLogicalHeightRegions());
for (auto& region : m_regionList) {
if (!region->hasAutoLogicalHeight())
continue;
// FIXME: We need to find a way to avoid marking all the regions ancestors for layout
// as we are already inside layout.