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
|
/*
* Copyright 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 "HitTestRequest.h"
#include "HitTestResult.h"
#include "Node.h"
#include "PaintInfo.h"
#include "RenderBoxRegionInfo.h"
#include "RenderLayer.h"
#include "RenderRegion.h"
#include "RenderView.h"
#include "TransformState.h"
#include "WebKitNamedFlow.h"
namespace WebCore {
RenderFlowThread::RenderFlowThread(Node* node, const AtomicString& flowThread)
: RenderBlock(node)
, m_flowThread(flowThread)
, m_hasValidRegions(false)
, m_regionsInvalidated(false)
, m_regionsHaveUniformLogicalWidth(true)
, m_regionsHaveUniformLogicalHeight(true)
, m_overflow(false)
{
ASSERT(node->document()->cssRegionsEnabled());
setIsAnonymous(false);
setInRenderFlowThread();
}
PassRefPtr<RenderStyle> RenderFlowThread::createFlowThreadStyle(RenderStyle* parentStyle)
{
RefPtr<RenderStyle> newStyle(RenderStyle::create());
newStyle->inheritFrom(parentStyle);
newStyle->setDisplay(BLOCK);
newStyle->setPosition(AbsolutePosition);
newStyle->setZIndex(0);
newStyle->setLeft(Length(0, Fixed));
newStyle->setTop(Length(0, Fixed));
newStyle->setWidth(Length(100, Percent));
newStyle->setHeight(Length(100, Percent));
newStyle->font().update(0);
return newStyle.release();
}
void RenderFlowThread::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
{
RenderBlock::styleDidChange(diff, oldStyle);
if (oldStyle && oldStyle->writingMode() != style()->writingMode())
m_regionsInvalidated = true;
}
RenderObject* RenderFlowThread::nextRendererForNode(Node* node) const
{
FlowThreadChildList::const_iterator it = m_flowThreadChildList.begin();
FlowThreadChildList::const_iterator end = m_flowThreadChildList.end();
for (; it != end; ++it) {
RenderObject* child = *it;
ASSERT(child->node());
unsigned short position = node->compareDocumentPosition(child->node());
if (position & Node::DOCUMENT_POSITION_FOLLOWING)
return child;
}
return 0;
}
RenderObject* RenderFlowThread::previousRendererForNode(Node* node) const
{
if (m_flowThreadChildList.isEmpty())
return 0;
FlowThreadChildList::const_iterator begin = m_flowThreadChildList.begin();
FlowThreadChildList::const_iterator end = m_flowThreadChildList.end();
FlowThreadChildList::const_iterator it = end;
do {
--it;
RenderObject* child = *it;
ASSERT(child->node());
unsigned short position = node->compareDocumentPosition(child->node());
if (position & Node::DOCUMENT_POSITION_PRECEDING)
return child;
} while (it != begin);
return 0;
}
void RenderFlowThread::addFlowChild(RenderObject* newChild, RenderObject* beforeChild)
{
// The child list is used to sort the flow thread's children render objects
// based on their corresponding nodes DOM order. The list is needed to avoid searching the whole DOM.
// Do not add anonymous objects.
if (!newChild->node())
return;
if (beforeChild)
m_flowThreadChildList.insertBefore(beforeChild, newChild);
else
m_flowThreadChildList.add(newChild);
}
void RenderFlowThread::removeFlowChild(RenderObject* child)
{
m_flowThreadChildList.remove(child);
}
void RenderFlowThread::removeFlowChildInfo(RenderObject* child)
{
if (child->isBox()) {
removeRenderBoxRegionInfo(toRenderBox(child));
if (child->canHaveRegionStyle())
clearRenderBoxCustomStyle(toRenderBox(child));
}
}
// Compare two regions to determine in which one the content should flow first.
// The function returns true if the first passed region is "less" than the second passed region.
// If the first region appears before second region in DOM,
// the first region is "less" than the second region.
// If the first region is "less" than the second region, the first region receives content before second region.
static bool compareRenderRegions(const RenderRegion* firstRegion, const RenderRegion* secondRegion)
{
ASSERT(firstRegion);
ASSERT(secondRegion);
// If the regions have the same region-index, compare their position in dom.
ASSERT(firstRegion->node());
ASSERT(secondRegion->node());
unsigned short position = firstRegion->node()->compareDocumentPosition(secondRegion->node());
return (position & Node::DOCUMENT_POSITION_FOLLOWING);
}
bool RenderFlowThread::dependsOn(RenderFlowThread* otherRenderFlowThread) const
{
if (m_layoutBeforeThreadsSet.contains(otherRenderFlowThread))
return true;
// Recursively traverse the m_layoutBeforeThreadsSet.
RenderFlowThreadCountedSet::const_iterator iterator = m_layoutBeforeThreadsSet.begin();
RenderFlowThreadCountedSet::const_iterator end = m_layoutBeforeThreadsSet.end();
for (; iterator != end; ++iterator) {
const RenderFlowThread* beforeFlowThread = (*iterator).first;
if (beforeFlowThread->dependsOn(otherRenderFlowThread))
return true;
}
return false;
}
void RenderFlowThread::addRegionToThread(RenderRegion* renderRegion)
{
ASSERT(renderRegion);
if (m_regionList.isEmpty())
m_regionList.add(renderRegion);
else {
// Find the first region "greater" than renderRegion.
RenderRegionList::iterator it = m_regionList.begin();
while (it != m_regionList.end() && !compareRenderRegions(renderRegion, *it))
++it;
m_regionList.insertBefore(it, renderRegion);
}
ASSERT(!renderRegion->isValid());
if (renderRegion->parentFlowThread()) {
if (renderRegion->parentFlowThread()->dependsOn(this)) {
// Register ourself to get a notification when the state changes.
renderRegion->parentFlowThread()->m_observerThreadsSet.add(this);
return;
}
addDependencyOnFlowThread(renderRegion->parentFlowThread());
}
renderRegion->setIsValid(true);
invalidateRegions();
}
void RenderFlowThread::removeRegionFromThread(RenderRegion* renderRegion)
{
ASSERT(renderRegion);
m_regionRangeMap.clear();
m_regionList.remove(renderRegion);
if (renderRegion->parentFlowThread()) {
if (!renderRegion->isValid()) {
renderRegion->parentFlowThread()->m_observerThreadsSet.remove(this);
// No need to invalidate the regions rectangles. The removed region
// was not taken into account. Just return here.
return;
}
removeDependencyOnFlowThread(renderRegion->parentFlowThread());
}
invalidateRegions();
}
void RenderFlowThread::checkInvalidRegions()
{
for (RenderRegionList::iterator iter = m_regionList.begin(); iter != m_regionList.end(); ++iter) {
RenderRegion* region = *iter;
// The only reason a region would be invalid is because it has a parent flow thread.
ASSERT(region->isValid() || region->parentFlowThread());
if (region->isValid() || region->parentFlowThread()->dependsOn(this))
continue;
region->parentFlowThread()->m_observerThreadsSet.remove(this);
addDependencyOnFlowThread(region->parentFlowThread());
region->setIsValid(true);
invalidateRegions();
}
if (m_observerThreadsSet.isEmpty())
return;
// Notify all the flow threads that were dependent on this flow.
// Create a copy of the list first. That's because observers might change the list when calling checkInvalidRegions.
Vector<RenderFlowThread*> observers;
copyToVector(m_observerThreadsSet, observers);
for (size_t i = 0; i < observers.size(); ++i) {
RenderFlowThread* flowThread = observers.at(i);
flowThread->checkInvalidRegions();
}
}
void RenderFlowThread::addDependencyOnFlowThread(RenderFlowThread* otherFlowThread)
{
std::pair<RenderFlowThreadCountedSet::iterator, bool> result = m_layoutBeforeThreadsSet.add(otherFlowThread);
if (result.second) {
// This is the first time we see this dependency. Make sure we recalculate all the dependencies.
view()->setIsRenderFlowThreadOrderDirty(true);
}
}
void RenderFlowThread::removeDependencyOnFlowThread(RenderFlowThread* otherFlowThread)
{
bool removed = m_layoutBeforeThreadsSet.remove(otherFlowThread);
if (removed) {
checkInvalidRegions();
view()->setIsRenderFlowThreadOrderDirty(true);
}
}
void RenderFlowThread::pushDependencies(RenderFlowThreadList& list)
{
for (RenderFlowThreadCountedSet::iterator iter = m_layoutBeforeThreadsSet.begin(); iter != m_layoutBeforeThreadsSet.end(); ++iter) {
RenderFlowThread* flowThread = (*iter).first;
if (list.contains(flowThread))
continue;
flowThread->pushDependencies(list);
list.add(flowThread);
}
}
class CurrentRenderFlowThreadMaintainer {
WTF_MAKE_NONCOPYABLE(CurrentRenderFlowThreadMaintainer);
public:
CurrentRenderFlowThreadMaintainer(RenderFlowThread* renderFlowThread)
: m_renderFlowThread(renderFlowThread)
{
RenderView* view = m_renderFlowThread->view();
ASSERT(!view->currentRenderFlowThread());
view->setCurrentRenderFlowThread(m_renderFlowThread);
}
~CurrentRenderFlowThreadMaintainer()
{
RenderView* view = m_renderFlowThread->view();
ASSERT(view->currentRenderFlowThread() == m_renderFlowThread);
view->setCurrentRenderFlowThread(0);
}
private:
RenderFlowThread* m_renderFlowThread;
};
class CurrentRenderFlowThreadDisabler {
WTF_MAKE_NONCOPYABLE(CurrentRenderFlowThreadDisabler);
public:
CurrentRenderFlowThreadDisabler(RenderView* view)
: m_view(view)
, m_renderFlowThread(0)
{
m_renderFlowThread = m_view->currentRenderFlowThread();
if (m_renderFlowThread)
view->setCurrentRenderFlowThread(0);
}
~CurrentRenderFlowThreadDisabler()
{
if (m_renderFlowThread)
m_view->setCurrentRenderFlowThread(m_renderFlowThread);
}
private:
RenderView* m_view;
RenderFlowThread* m_renderFlowThread;
};
void RenderFlowThread::layout()
{
bool regionsChanged = m_regionsInvalidated && everHadLayout();
if (m_regionsInvalidated) {
m_regionsInvalidated = false;
m_hasValidRegions = false;
m_regionsHaveUniformLogicalWidth = true;
m_regionsHaveUniformLogicalHeight = true;
m_regionRangeMap.clear();
LayoutUnit previousRegionLogicalWidth = 0;
LayoutUnit previousRegionLogicalHeight = 0;
if (hasRegions()) {
for (RenderRegionList::iterator iter = m_regionList.begin(); iter != m_regionList.end(); ++iter) {
RenderRegion* region = *iter;
if (!region->isValid())
continue;
ASSERT(!region->needsLayout());
region->deleteAllRenderBoxRegionInfo();
LayoutUnit regionLogicalWidth;
LayoutUnit regionLogicalHeight;
if (isHorizontalWritingMode()) {
regionLogicalWidth = region->contentWidth();
regionLogicalHeight = region->contentHeight();
} else {
regionLogicalWidth = region->contentHeight();
regionLogicalHeight = region->contentWidth();
}
if (!m_hasValidRegions)
m_hasValidRegions = true;
else {
if (m_regionsHaveUniformLogicalWidth && previousRegionLogicalWidth != regionLogicalWidth)
m_regionsHaveUniformLogicalWidth = false;
if (m_regionsHaveUniformLogicalHeight && previousRegionLogicalHeight != regionLogicalHeight)
m_regionsHaveUniformLogicalHeight = false;
}
previousRegionLogicalWidth = regionLogicalWidth;
}
computeLogicalWidth(); // Called to get the maximum logical width for the region.
LayoutUnit logicalHeight = 0;
for (RenderRegionList::iterator iter = m_regionList.begin(); iter != m_regionList.end(); ++iter) {
RenderRegion* region = *iter;
if (!region->isValid())
continue;
LayoutRect regionRect;
if (isHorizontalWritingMode()) {
regionRect = LayoutRect(style()->direction() == LTR ? zeroLayoutUnit : logicalWidth() - region->contentWidth(), logicalHeight, region->contentWidth(), region->contentHeight());
logicalHeight += regionRect.height();
} else {
regionRect = LayoutRect(logicalHeight, style()->direction() == LTR ? zeroLayoutUnit : logicalWidth() - region->contentHeight(), region->contentWidth(), region->contentHeight());
logicalHeight += regionRect.width();
}
region->setRegionRect(regionRect);
}
}
}
CurrentRenderFlowThreadMaintainer currentFlowThreadSetter(this);
LayoutStateMaintainer statePusher(view(), this, regionsChanged);
RenderBlock::layout();
statePusher.pop();
}
void RenderFlowThread::computeLogicalWidth()
{
LayoutUnit logicalWidth = 0;
for (RenderRegionList::iterator iter = m_regionList.begin(); iter != m_regionList.end(); ++iter) {
RenderRegion* region = *iter;
if (!region->isValid())
continue;
ASSERT(!region->needsLayout());
logicalWidth = max(isHorizontalWritingMode() ? region->contentWidth() : region->contentHeight(), logicalWidth);
}
setLogicalWidth(logicalWidth);
// If the regions have non-uniform logical widths, then insert inset information for the RenderFlowThread.
for (RenderRegionList::iterator iter = m_regionList.begin(); iter != m_regionList.end(); ++iter) {
RenderRegion* region = *iter;
if (!region->isValid())
continue;
LayoutUnit regionLogicalWidth = isHorizontalWritingMode() ? region->contentWidth() : region->contentHeight();
if (regionLogicalWidth != logicalWidth) {
LayoutUnit logicalLeft = style()->direction() == LTR ? zeroLayoutUnit : logicalWidth - regionLogicalWidth;
region->setRenderBoxRegionInfo(this, logicalLeft, regionLogicalWidth, false);
}
}
}
void RenderFlowThread::computeLogicalHeight()
{
int logicalHeight = 0;
for (RenderRegionList::iterator iter = m_regionList.begin(); iter != m_regionList.end(); ++iter) {
RenderRegion* region = *iter;
if (!region->isValid())
continue;
ASSERT(!region->needsLayout());
logicalHeight += isHorizontalWritingMode() ? region->contentHeight() : region->contentWidth();
}
setLogicalHeight(logicalHeight);
}
void RenderFlowThread::paintIntoRegion(PaintInfo& paintInfo, RenderRegion* region, const LayoutPoint& paintOffset)
{
GraphicsContext* context = paintInfo.context;
if (!context)
return;
// Adjust the clipping rect for the region.
// paintOffset contains the offset where the painting should occur
// adjusted with the region padding and border.
LayoutRect regionRect(region->regionRect());
LayoutRect regionOverflowRect(region->regionOverflowRect());
LayoutRect regionClippingRect(paintOffset + (regionOverflowRect.location() - regionRect.location()), regionOverflowRect.size());
PaintInfo info(paintInfo);
info.rect.intersect(pixelSnappedIntRect(regionClippingRect));
if (!info.rect.isEmpty()) {
context->save();
context->clip(regionClippingRect);
// RenderFlowThread should start painting its content in a position that is offset
// from the region rect's current position. The amount of offset is equal to the location of
// region in flow coordinates.
LayoutPoint renderFlowThreadOffset;
if (style()->isFlippedBlocksWritingMode()) {
LayoutRect flippedRegionRect(regionRect);
flipForWritingMode(flippedRegionRect);
renderFlowThreadOffset = LayoutPoint(paintOffset - flippedRegionRect.location());
} else
renderFlowThreadOffset = LayoutPoint(paintOffset - regionRect.location());
context->translate(renderFlowThreadOffset.x(), renderFlowThreadOffset.y());
info.rect.moveBy(-roundedIntPoint(renderFlowThreadOffset));
layer()->paint(context, info.rect, 0, 0, region, RenderLayer::PaintLayerTemporaryClipRects);
context->restore();
}
}
bool RenderFlowThread::hitTestRegion(RenderRegion* region, const HitTestRequest& request, HitTestResult& result, const LayoutPoint& pointInContainer, const LayoutPoint& accumulatedOffset)
{
LayoutRect regionRect(region->regionRect());
LayoutRect regionOverflowRect = region->regionOverflowRect();
LayoutRect regionClippingRect(accumulatedOffset + (regionOverflowRect.location() - regionRect.location()), regionOverflowRect.size());
if (!regionClippingRect.contains(pointInContainer))
return false;
LayoutPoint renderFlowThreadOffset;
if (style()->isFlippedBlocksWritingMode()) {
LayoutRect flippedRegionRect(regionRect);
flipForWritingMode(flippedRegionRect);
renderFlowThreadOffset = LayoutPoint(accumulatedOffset - flippedRegionRect.location());
} else
renderFlowThreadOffset = LayoutPoint(accumulatedOffset - regionRect.location());
LayoutPoint transformedPoint(pointInContainer.x() - renderFlowThreadOffset.x(), pointInContainer.y() - renderFlowThreadOffset.y());
// Always ignore clipping, since the RenderFlowThread has nothing to do with the bounds of the FrameView.
HitTestRequest newRequest(request.type() | HitTestRequest::IgnoreClipping);
RenderRegion* oldRegion = result.region();
result.setRegion(region);
LayoutPoint oldPoint = result.point();
result.setPoint(transformedPoint);
bool isPointInsideFlowThread = layer()->hitTest(newRequest, result);
result.setPoint(oldPoint);
result.setRegion(oldRegion);
// FIXME: Should we set result.m_localPoint back to the RenderRegion's coordinate space or leave it in the RenderFlowThread's coordinate
// space? Right now it's staying in the RenderFlowThread's coordinate space, which may end up being ok. We will know more when we get around to
// patching positionForPoint.
return isPointInsideFlowThread;
}
bool RenderFlowThread::shouldRepaint(const LayoutRect& r) const
{
if (view()->printing() || r.isEmpty())
return false;
return true;
}
void RenderFlowThread::repaintRectangleInRegions(const LayoutRect& repaintRect, bool immediate)
{
if (!shouldRepaint(repaintRect) || !hasValidRegionInfo())
return;
for (RenderRegionList::iterator iter = m_regionList.begin(); iter != m_regionList.end(); ++iter) {
RenderRegion* region = *iter;
if (!region->isValid())
continue;
// We only have to issue a repaint in this region if the region rect intersects the repaint rect.
LayoutRect flippedRegionRect(region->regionRect());
LayoutRect flippedRegionOverflowRect(region->regionOverflowRect());
flipForWritingMode(flippedRegionRect); // Put the region rects into physical coordinates.
flipForWritingMode(flippedRegionOverflowRect);
LayoutRect clippedRect(repaintRect);
clippedRect.intersect(flippedRegionOverflowRect);
if (clippedRect.isEmpty())
continue;
// Put the region rect into the region's physical coordinate space.
clippedRect.setLocation(region->contentBoxRect().location() + (clippedRect.location() - flippedRegionRect.location()));
// Now switch to the region's writing mode coordinate space and let it repaint itself.
region->flipForWritingMode(clippedRect);
LayoutStateDisabler layoutStateDisabler(view()); // We can't use layout state to repaint, since the region is somewhere else.
// Can't use currentFlowThread as it possible to have imbricated flow threads and the wrong one could be used,
// so, we let each region figure out the proper enclosing flow thread
CurrentRenderFlowThreadDisabler disabler(view());
region->repaintRectangle(clippedRect, immediate);
}
}
RenderRegion* RenderFlowThread::renderRegionForLine(LayoutUnit position, bool extendLastRegion) const
{
ASSERT(!m_regionsInvalidated);
// If no region matches the position and extendLastRegion is true, it will return
// the last valid region. It is similar to auto extending the size of the last region.
RenderRegion* lastValidRegion = 0;
// FIXME: The regions are always in order, optimize this search.
bool useHorizontalWritingMode = isHorizontalWritingMode();
for (RenderRegionList::const_iterator iter = m_regionList.begin(); iter != m_regionList.end(); ++iter) {
RenderRegion* region = *iter;
if (!region->isValid())
continue;
if (position <= 0)
return region;
LayoutRect regionRect = region->regionRect();
if ((useHorizontalWritingMode && position < regionRect.maxY()) || (!useHorizontalWritingMode && position < regionRect.maxX()))
return region;
if (extendLastRegion)
lastValidRegion = region;
}
return lastValidRegion;
}
LayoutUnit RenderFlowThread::regionLogicalTopForLine(LayoutUnit position) const
{
RenderRegion* region = renderRegionForLine(position);
if (!region)
return 0;
return isHorizontalWritingMode() ? region->regionRect().y() : region->regionRect().x();
}
LayoutUnit RenderFlowThread::regionLogicalWidthForLine(LayoutUnit position) const
{
RenderRegion* region = renderRegionForLine(position, true);
if (!region)
return contentLogicalWidth();
return isHorizontalWritingMode() ? region->regionRect().width() : region->regionRect().height();
}
LayoutUnit RenderFlowThread::regionLogicalHeightForLine(LayoutUnit position) const
{
RenderRegion* region = renderRegionForLine(position);
if (!region)
return 0;
return isHorizontalWritingMode() ? region->regionRect().height() : region->regionRect().width();
}
LayoutUnit RenderFlowThread::regionRemainingLogicalHeightForLine(LayoutUnit position, PageBoundaryRule pageBoundaryRule) const
{
RenderRegion* region = renderRegionForLine(position);
if (!region)
return 0;
LayoutUnit regionLogicalBottom = isHorizontalWritingMode() ? region->regionRect().maxY() : region->regionRect().maxX();
LayoutUnit remainingHeight = regionLogicalBottom - position;
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.
LayoutUnit regionHeight = isHorizontalWritingMode() ? region->regionRect().height() : region->regionRect().width();
remainingHeight = layoutMod(remainingHeight, regionHeight);
}
return remainingHeight;
}
RenderRegion* RenderFlowThread::mapFromFlowToRegion(TransformState& transformState) const
{
if (!hasValidRegionInfo())
return 0;
LayoutRect boxRect = transformState.mappedQuad().enclosingBoundingBox();
flipForWritingMode(boxRect);
// FIXME: We need to refactor RenderObject::absoluteQuads to be able to split the quads across regions,
// for now we just take the center of the mapped enclosing box and map it to a region.
// Note: Using the center in order to avoid rounding errors.
LayoutPoint center = boxRect.center();
RenderRegion* renderRegion = renderRegionForLine(isHorizontalWritingMode() ? center.y() : center.x(), true);
if (!renderRegion)
return 0;
LayoutRect flippedRegionRect(renderRegion->regionRect());
flipForWritingMode(flippedRegionRect);
transformState.move(renderRegion->contentBoxRect().location() - flippedRegionRect.location());
return renderRegion;
}
void RenderFlowThread::removeRenderBoxRegionInfo(RenderBox* box)
{
if (!hasRegions())
return;
RenderRegion* startRegion;
RenderRegion* endRegion;
getRegionRangeForBox(box, startRegion, endRegion);
for (RenderRegionList::iterator iter = m_regionList.find(startRegion); iter != m_regionList.end(); ++iter) {
RenderRegion* region = *iter;
if (!region->isValid())
continue;
region->removeRenderBoxRegionInfo(box);
if (region == endRegion)
break;
}
#ifndef NDEBUG
// We have to make sure we did not left any boxes with region info attached in regions.
for (RenderRegionList::iterator iter = m_regionList.begin(); iter != m_regionList.end(); ++iter) {
RenderRegion* region = *iter;
if (!region->isValid())
continue;
ASSERT(!region->renderBoxRegionInfo(box));
}
#endif
m_regionRangeMap.remove(box);
}
bool RenderFlowThread::logicalWidthChangedInRegions(const RenderBlock* block, LayoutUnit offsetFromLogicalTopOfFirstPage)
{
if (!hasRegions() || block == this) // Not necessary, since if any region changes, we do a full pagination relayout anyway.
return false;
RenderRegion* startRegion;
RenderRegion* endRegion;
getRegionRangeForBox(block, startRegion, endRegion);
for (RenderRegionList::iterator iter = m_regionList.find(startRegion); iter != m_regionList.end(); ++iter) {
RenderRegion* region = *iter;
if (!region->isValid())
continue;
ASSERT(!region->needsLayout());
OwnPtr<RenderBoxRegionInfo> oldInfo = region->takeRenderBoxRegionInfo(block);
if (!oldInfo)
continue;
LayoutUnit oldLogicalWidth = oldInfo->logicalWidth();
RenderBoxRegionInfo* newInfo = block->renderBoxRegionInfo(region, offsetFromLogicalTopOfFirstPage);
if (!newInfo || newInfo->logicalWidth() != oldLogicalWidth)
return true;
if (region == endRegion)
break;
}
return false;
}
LayoutUnit RenderFlowThread::contentLogicalWidthOfFirstRegion() const
{
if (!hasValidRegionInfo())
return 0;
for (RenderRegionList::const_iterator iter = m_regionList.begin(); iter != m_regionList.end(); ++iter) {
RenderRegion* region = *iter;
if (!region->isValid())
continue;
return isHorizontalWritingMode() ? region->contentWidth() : region->contentHeight();
}
ASSERT_NOT_REACHED();
return 0;
}
LayoutUnit RenderFlowThread::contentLogicalHeightOfFirstRegion() const
{
if (!hasValidRegionInfo())
return 0;
for (RenderRegionList::const_iterator iter = m_regionList.begin(); iter != m_regionList.end(); ++iter) {
RenderRegion* region = *iter;
if (!region->isValid())
continue;
return isHorizontalWritingMode() ? region->contentHeight() : region->contentWidth();
}
ASSERT_NOT_REACHED();
return 0;
}
LayoutUnit RenderFlowThread::contentLogicalLeftOfFirstRegion() const
{
if (!hasValidRegionInfo())
return 0;
for (RenderRegionList::const_iterator iter = m_regionList.begin(); iter != m_regionList.end(); ++iter) {
RenderRegion* region = *iter;
if (!region->isValid())
continue;
return isHorizontalWritingMode() ? region->regionRect().x() : region->regionRect().y();
}
ASSERT_NOT_REACHED();
return 0;
}
RenderRegion* RenderFlowThread::firstRegion() const
{
if (!hasValidRegionInfo())
return 0;
for (RenderRegionList::const_iterator iter = m_regionList.begin(); iter != m_regionList.end(); ++iter) {
RenderRegion* region = *iter;
if (!region->isValid())
continue;
return region;
}
return 0;
}
RenderRegion* RenderFlowThread::lastRegion() const
{
if (!hasValidRegionInfo())
return 0;
for (RenderRegionList::const_reverse_iterator iter = m_regionList.rbegin(); iter != m_regionList.rend(); ++iter) {
RenderRegion* region = *iter;
if (!region->isValid())
continue;
return region;
}
return 0;
}
void RenderFlowThread::clearRenderBoxCustomStyle(const RenderBox* box,
const RenderRegion* oldStartRegion, const RenderRegion* oldEndRegion,
const RenderRegion* newStartRegion, const RenderRegion* newEndRegion)
{
// Clear the styles for the object in the regions.
// The styles are not cleared for the regions that are contained in both ranges.
bool insideOldRegionRange = false;
bool insideNewRegionRange = false;
for (RenderRegionList::iterator iter = m_regionList.begin(); iter != m_regionList.end(); ++iter) {
RenderRegion* region = *iter;
if (oldStartRegion == region)
insideOldRegionRange = true;
if (newStartRegion == region)
insideNewRegionRange = true;
if (!(insideOldRegionRange && insideNewRegionRange))
region->clearBoxStyleInRegion(box);
if (oldEndRegion == region)
insideOldRegionRange = false;
if (newEndRegion == region)
insideNewRegionRange = false;
}
}
void RenderFlowThread::setRegionRangeForBox(const RenderBox* box, LayoutUnit offsetFromLogicalTopOfFirstPage)
{
if (!hasRegions())
return;
// FIXME: Not right for differing writing-modes.
RenderRegion* startRegion = renderRegionForLine(offsetFromLogicalTopOfFirstPage, true);
RenderRegion* endRegion = renderRegionForLine(offsetFromLogicalTopOfFirstPage + box->logicalHeight(), true);
RenderRegionRangeMap::iterator 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->second;
if (range.startRegion() == startRegion && range.endRegion() == endRegion)
return;
// Delete any info that we find before our new startRegion and after our new endRegion.
for (RenderRegionList::iterator iter = m_regionList.begin(); iter != m_regionList.end(); ++iter) {
RenderRegion* region = *iter;
if (region == startRegion) {
iter = m_regionList.find(endRegion);
continue;
}
region->removeRenderBoxRegionInfo(box);
if (region == range.endRegion())
break;
}
clearRenderBoxCustomStyle(box, range.startRegion(), range.endRegion(), startRegion, endRegion);
range.setRange(startRegion, endRegion);
}
void RenderFlowThread::getRegionRangeForBox(const RenderBox* box, RenderRegion*& startRegion, RenderRegion*& endRegion) const
{
startRegion = 0;
endRegion = 0;
RenderRegionRangeMap::const_iterator it = m_regionRangeMap.find(box);
if (it == m_regionRangeMap.end())
return;
const RenderRegionRange& range = it->second;
startRegion = range.startRegion();
endRegion = range.endRegion();
ASSERT(m_regionList.contains(startRegion) && m_regionList.contains(endRegion));
}
WebKitNamedFlow* RenderFlowThread::ensureNamedFlow()
{
if (!m_namedFlow)
m_namedFlow = WebKitNamedFlow::create(this);
return m_namedFlow.get();
}
void RenderFlowThread::computeOverflowStateForRegions(LayoutUnit oldClientAfterEdge)
{
LayoutUnit height = oldClientAfterEdge;
// FIXME: the visual overflow of middle region (if it is the last one to contain any content in a render flow thread)
// might not be taken into account because the render flow thread height is greater that that regions height + its visual overflow
// because of how computeLogicalHeight is implemented for RenderFlowThread (as a sum of all regions height).
// This means that the middle region will be marked as fit (even if it has visual overflow flowing into the next region)
if (hasRenderOverflow())
height = isHorizontalWritingMode() ? visualOverflowRect().maxY() : visualOverflowRect().maxX();
for (RenderRegionList::iterator iter = m_regionList.begin(); iter != m_regionList.end(); ++iter) {
RenderRegion* region = *iter;
if (!region->isValid()) {
region->setRegionState(RenderRegion::RegionUndefined);
continue;
}
LayoutUnit flowMin = height - (isHorizontalWritingMode() ? region->regionRect().y() : region->regionRect().x());
LayoutUnit flowMax = height - (isHorizontalWritingMode() ? region->regionRect().maxY() : region->regionRect().maxX());
RenderRegion::RegionState state = RenderRegion::RegionFit;
if (flowMin <= 0)
state = RenderRegion::RegionEmpty;
if (flowMax > 0)
state = RenderRegion::RegionOverflow;
region->setRegionState(state);
}
// With the regions overflow state computed we can also set the overflow for the named flow.
RenderRegion* lastReg = lastRegion();
m_overflow = lastReg && (lastReg->regionState() == RenderRegion::RegionOverflow);
}
} // namespace WebCore
|